| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | import java.util.Collections;
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | import java.util.Vector;
 | 
					
						
							|  |  |  | import java.util.stream.Collectors;
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public class VariableType {
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 	 * The base type of this variable which remove all ending stars. Each item is a
 | 
					
						
							|  |  |  | 	 * part of namespace string. If no namespace, this Vector will only have one
 | 
					
						
							|  |  |  | 	 * item.
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 	 */
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 	private Vector<String> mBaseType;
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * The pointer level of this type. It is equal with the count of stars.
 | 
					
						
							|  |  |  | 	 */
 | 
					
						
							|  |  |  | 	private int mPointerLevel;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 	public VariableType() {
 | 
					
						
							|  |  |  | 		mBaseType = new Vector<String>();
 | 
					
						
							|  |  |  | 		mPointerLevel = 0;
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	private VariableType(Vector<String> base_type, int pointer_level) {
 | 
					
						
							| 
									
										
										
										
											2023-11-02 22:02:39 +08:00
										 |  |  | 		mBaseType = (Vector<String>) base_type.clone();
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 		mPointerLevel = pointer_level;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public void fromCType(String ctype) {
 | 
					
						
							|  |  |  | 		if (ctype.isEmpty())
 | 
					
						
							|  |  |  | 			throw new IllegalArgumentException("empty string can not be parsed.");
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 		// get pointer part and name part
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 		int len = ctype.length();
 | 
					
						
							|  |  |  | 		int star_pos = ctype.indexOf('*');
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 		String namepart;
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 		if (star_pos == -1) {
 | 
					
						
							|  |  |  | 			// no star
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 			namepart = ctype;
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 			mPointerLevel = 0;
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			// has star
 | 
					
						
							|  |  |  | 			if (star_pos == 0)
 | 
					
						
							|  |  |  | 				throw new IllegalArgumentException("base type not found.");
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 			namepart = ctype.substring(0, star_pos);
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 			mPointerLevel = len - star_pos;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 		
 | 
					
						
							|  |  |  | 		// resolve name part
 | 
					
						
							|  |  |  | 		mBaseType.clear();
 | 
					
						
							|  |  |  | 		for (String item : namepart.split("::")) {
 | 
					
						
							|  |  |  | 			mBaseType.add(item);
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public String toCType() {
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 		return mBaseType.stream().collect(Collectors.joining("::"))
 | 
					
						
							|  |  |  | 				+ String.join("", Collections.nCopies(mPointerLevel, "*"));
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public String getBaseType() {
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 		return mBaseType.lastElement();
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public boolean isPointer() {
 | 
					
						
							|  |  |  | 		return mPointerLevel != 0;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2023-11-02 22:02:39 +08:00
										 |  |  | 	
 | 
					
						
							|  |  |  | 	public int getPointerLevel() {
 | 
					
						
							|  |  |  | 		return mPointerLevel;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 	public boolean isValid() {
 | 
					
						
							|  |  |  | 		return mBaseType.size() != 0;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | 	public VariableType getPointerOfThis() {
 | 
					
						
							|  |  |  | 		return new VariableType(mBaseType, mPointerLevel);
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2023-11-02 12:40:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-02 10:53:16 +08:00
										 |  |  | }
 |