use of kalang.ast.FieldExpr in project kalang by kasonyang.
the class SemanticAnalyzer method validateAssign.
public boolean validateAssign(AssignableExpr to, ExprNode from, OffsetRange offset) {
if (to instanceof VarExpr) {
LocalVarNode varObject = ((VarExpr) to).getVar();
if (Modifier.isFinal(varObject.modifier)) {
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, String.format("%s is readonly", varObject.getName()), offset);
return false;
}
} else if (to instanceof FieldExpr) {
FieldDescriptor field = ((FieldExpr) to).getField();
if (Modifier.isFinal(field.getModifier())) {
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, String.format("%s is readonly", field.getName()), offset);
return false;
}
}
Type toType = to.getType();
Type fromType = from.getType();
if (!toType.isAssignableFrom(fromType)) {
diagnosisReporter.report(Diagnosis.Kind.ERROR, String.format("incompatible types: %s cannot be converted to %s", fromType, toType), offset);
return false;
}
return true;
}
use of kalang.ast.FieldExpr in project kalang by kasonyang.
the class AstUtil method createSetter.
public static void createSetter(ClassNode clazz, FieldDescriptor field, int accessModifier) {
String fn = field.getName();
String setterName = "set" + NameUtil.firstCharToUpperCase(fn);
boolean isStatic = isStatic(field.getModifier());
if (isStatic) {
accessModifier |= Modifier.STATIC;
}
MethodNode setter = clazz.createMethodNode(Types.VOID_TYPE, setterName, accessModifier);
// setter.offset = field.offset;
ParameterNode param = setter.createParameter(field.getType(), field.getName());
BlockStmt body = setter.getBody();
FieldExpr fe;
ExprNode paramVal = new ParameterExpr(param);
ClassReference cr = new ClassReference(clazz);
if (isStatic) {
fe = new StaticFieldExpr(cr, field);
} else {
fe = new ObjectFieldExpr(new ThisExpr(Types.getClassType(clazz)), field);
}
body.statements.add(new ExprStmt(new AssignExpr(fe, paramVal)));
}
use of kalang.ast.FieldExpr in project kalang by kasonyang.
the class Ast2Class method assign.
private void assign(ExprNode to, ExprNode from) {
if (to instanceof FieldExpr) {
FieldExpr toField = (FieldExpr) to;
assignField(toField, from);
} else if (to instanceof VarExpr) {
assignVarObject(((VarExpr) to).getVar(), from);
} else if (to instanceof ElementExpr) {
ElementExpr elementExpr = (ElementExpr) to;
assignArrayElement(elementExpr.getArrayExpr(), elementExpr.getIndex(), from);
} else {
throw new UnknownError("unknown expression:" + to);
}
}
use of kalang.ast.FieldExpr in project kalang by kasonyang.
the class AstUtil method createGetter.
public static void createGetter(ClassNode clazz, FieldDescriptor field, int accessModifier) {
String fn = field.getName();
String getterName = "get" + NameUtil.firstCharToUpperCase(fn);
boolean isStatic = isStatic(field.getModifier());
if (isStatic) {
accessModifier |= Modifier.STATIC;
}
MethodNode getter = clazz.createMethodNode(field.getType(), getterName, accessModifier);
// getter.offset = field.offset;
BlockStmt body = getter.getBody();
FieldExpr fe;
ClassReference cr = new ClassReference(clazz);
if (isStatic) {
fe = new StaticFieldExpr(cr, field);
} else {
fe = new ObjectFieldExpr(new ThisExpr(Types.getClassType(clazz)), field);
}
body.statements.add(new ReturnStmt(fe));
}
Aggregations