libcmo21/CodeGen/BMapBindings/VariableType.java

80 lines
1.8 KiB
Java
Raw Normal View History

2023-11-02 10:53:16 +08:00
import java.util.Collections;
import java.util.Vector;
import java.util.stream.Collectors;
2023-11-02 10:53:16 +08:00
public class VariableType {
/**
* 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
*/
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;
public VariableType() {
mBaseType = new Vector<String>();
mPointerLevel = 0;
2023-11-02 10:53:16 +08:00
}
private VariableType(Vector<String> base_type, int pointer_level) {
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.");
// get pointer part and name part
2023-11-02 10:53:16 +08:00
int len = ctype.length();
int star_pos = ctype.indexOf('*');
String namepart;
2023-11-02 10:53:16 +08:00
if (star_pos == -1) {
// no star
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.");
namepart = ctype.substring(0, star_pos);
2023-11-02 10:53:16 +08:00
mPointerLevel = len - star_pos;
}
// resolve name part
mBaseType.clear();
for (String item : namepart.split("::")) {
mBaseType.add(item);
}
2023-11-02 10:53:16 +08:00
}
public String toCType() {
return mBaseType.stream().collect(Collectors.joining("::"))
+ String.join("", Collections.nCopies(mPointerLevel, "*"));
2023-11-02 10:53:16 +08:00
}
public String getBaseType() {
return mBaseType.lastElement();
2023-11-02 10:53:16 +08:00
}
public boolean isPointer() {
return mPointerLevel != 0;
}
public int getPointerLevel() {
return mPointerLevel;
}
2023-11-02 10:53:16 +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 10:53:16 +08:00
}