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
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
/**
|
|
|
|
* The class represent the type of each parameters and function return value.
|
|
|
|
*/
|
2023-11-02 10:53:16 +08:00
|
|
|
public class VariableType {
|
|
|
|
/**
|
2024-04-23 16:01:26 +08:00
|
|
|
* The base type of this variable removing all ending stars (remove all pointer
|
|
|
|
* levels) Each item in this a part of namespace and the last one must be the
|
|
|
|
* type itself (without any namespace restriction). If no namespace restriction
|
|
|
|
* for this type, this Vector will only have one item.
|
|
|
|
* <p>
|
|
|
|
* For end user, it is enough that knowing the last item is type itself.
|
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
|
|
|
/**
|
2024-04-23 16:01:26 +08:00
|
|
|
* The pointer level of this type. It is equal to the count of trailing star of
|
|
|
|
* this field in C style representation.
|
2023-11-02 10:53:16 +08:00
|
|
|
*/
|
|
|
|
private int mPointerLevel;
|
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
/**
|
|
|
|
* Construct an empty varible type. This is commonly used constructor.
|
|
|
|
*/
|
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
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
/**
|
|
|
|
* The constructor used for cloning self. This constructor is only can be used
|
|
|
|
* by self.
|
|
|
|
*
|
|
|
|
* @param base_type The hierarchy of the variable type.
|
|
|
|
* @param pointer_level The pointer level of new created variable type.
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
/**
|
|
|
|
* Set this variable type with a type string in C/C++ style. For example
|
|
|
|
* "NSTest::NSTest2::MyType**".
|
|
|
|
*
|
|
|
|
* @param ctype The type string in C/C++ style.
|
|
|
|
*/
|
2023-11-02 10:53:16 +08:00
|
|
|
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;
|
|
|
|
}
|
2024-04-23 16:01:26 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
/**
|
|
|
|
* Build a type string represented by this variable type in C/C++ style.
|
|
|
|
*
|
|
|
|
* @return The type string in C/C++ style.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
/**
|
|
|
|
* Get the base type of this variable type without any namespace. It just simply
|
|
|
|
* get the last entry in type hierarchy.
|
|
|
|
*
|
|
|
|
* @return The base type string without namespace prefix.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
/**
|
|
|
|
* Check whether this variable type is a pointer. This function just check
|
|
|
|
* whether the pointer level of this variavle type is zero.
|
|
|
|
*
|
|
|
|
* @return True if it is pointer, otherwise false.
|
|
|
|
*/
|
2023-11-02 10:53:16 +08:00
|
|
|
public boolean isPointer() {
|
|
|
|
return mPointerLevel != 0;
|
|
|
|
}
|
2024-04-23 16:01:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the pointer level of this variable type. You can simply assume the
|
|
|
|
* pointer level is equal to the count of trailing star.
|
|
|
|
*
|
|
|
|
* @return The pointer level integer. Zero means that this type is not a
|
|
|
|
* pointer.
|
|
|
|
*/
|
2023-11-02 22:02:39 +08:00
|
|
|
public int getPointerLevel() {
|
|
|
|
return mPointerLevel;
|
|
|
|
}
|
2024-04-23 16:01:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the clone of the type hierarchy of this variable type.
|
|
|
|
* <p>
|
|
|
|
* It is rarely used. This only should be used when you need the namespace
|
|
|
|
* hierarchy of this variable type.
|
|
|
|
*
|
|
|
|
* @return The clone of current variable type hierarchy.
|
|
|
|
*/
|
2023-11-07 15:55:07 +08:00
|
|
|
public Vector<String> getBaseTypeHierarchy() {
|
|
|
|
return (Vector<String>) mBaseType.clone();
|
|
|
|
}
|
2023-11-02 10:53:16 +08:00
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
/**
|
|
|
|
* Check whether this type is a valid one. It actually check whether type
|
|
|
|
* hierarchy include at least one entry.
|
|
|
|
*
|
|
|
|
* @return True if no problem of this type, otherwise false.
|
|
|
|
*/
|
2023-11-02 12:40:50 +08:00
|
|
|
public boolean isValid() {
|
|
|
|
return mBaseType.size() != 0;
|
|
|
|
}
|
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
/**
|
|
|
|
* Return a new created variable type which is the pointer of this variable
|
|
|
|
* type.
|
|
|
|
* <p>
|
|
|
|
* In internal implementation, it just create a clone of current variable type
|
|
|
|
* with the increase of pointer level by 1.
|
|
|
|
*
|
|
|
|
* @return The new created pointer type of this variable type.
|
|
|
|
*/
|
2023-11-02 10:53:16 +08:00
|
|
|
public VariableType getPointerOfThis() {
|
2023-11-03 10:43:06 +08:00
|
|
|
return new VariableType(mBaseType, mPointerLevel + 1);
|
2023-11-02 10:53:16 +08:00
|
|
|
}
|
2023-11-02 12:40:50 +08:00
|
|
|
|
2023-11-02 10:53:16 +08:00
|
|
|
}
|