use of kalang.ast.ObjectFieldExpr in project kalang by kasonyang.
the class Ast2Class method visitFieldExpr.
@Override
public Object visitFieldExpr(FieldExpr node) {
int opc;
String owner = internalName(node.getField().getFieldNode().getClassNode());
if (node instanceof ObjectFieldExpr) {
ExprNode target = ((ObjectFieldExpr) node).getTarget();
visit(target);
opc = GETFIELD;
} else if (node instanceof StaticFieldExpr) {
opc = GETSTATIC;
} else {
throw new UnsupportedOperationException("unsupported field type:" + node);
}
md.visitFieldInsn(opc, owner, node.getField().getName(), getTypeDescriptor(node.getType()));
return null;
}
use of kalang.ast.ObjectFieldExpr in project kalang by kasonyang.
the class AstBuilder method visitFieldDecl.
@Override
public Void visitFieldDecl(FieldDeclContext ctx) {
int fieldModifier = this.parseModifier(ctx.varModifier());
for (VarDeclContext vd : ctx.varDecl()) {
ExprNode initExpr;
if (vd.expression() != null) {
initExpr = visitExpression(vd.expression());
} else {
initExpr = null;
}
VarInfo varInfo = varDecl(vd, initExpr == null ? Types.getRootType() : initExpr.getType());
varInfo.modifier |= fieldModifier;
FieldNode fieldNode = thisClazz.createField(varInfo.type, varInfo.name, varInfo.modifier);
// TODO simplify it
if (initExpr != null) {
if (AstUtil.isStatic(fieldNode.modifier)) {
thisClazz.staticInitStmts.add(new ExprStmt(new AssignExpr(new StaticFieldExpr(new ClassReference(thisClazz), fieldNode), initExpr)));
} else {
thisClazz.initStmts.add(new ExprStmt(new AssignExpr(new ObjectFieldExpr(new ThisExpr(getThisType()), fieldNode), initExpr)));
}
}
}
return null;
}
use of kalang.ast.ObjectFieldExpr in project kalang by kasonyang.
the class ClassNodeMetaBuilder method visitFieldDecl.
@Override
public Void visitFieldDecl(KalangParser.FieldDeclContext ctx) {
int fieldModifier = astBuilder.parseModifier(ctx.varModifier());
for (KalangParser.VarDeclContext vd : ctx.varDecl()) {
ExprNode initExpr;
if (vd.expression() != null) {
initExpr = astBuilder.visitExpression(vd.expression());
} else {
initExpr = null;
}
AstBuilder.VarInfo varInfo = astBuilder.varDecl(vd, initExpr == null ? Types.getRootType() : initExpr.getType());
varInfo.modifier |= fieldModifier;
FieldNode fieldNode = thisClazz.createField(varInfo.type, varInfo.name, varInfo.modifier);
// TODO simplify it
if (initExpr != null) {
if (AstUtil.isStatic(fieldNode.modifier)) {
thisClazz.staticInitStmts.add(new ExprStmt(new AssignExpr(new StaticFieldExpr(new ClassReference(thisClazz), fieldNode), initExpr)));
} else {
thisClazz.initStmts.add(new ExprStmt(new AssignExpr(new ObjectFieldExpr(new ThisExpr(Types.getClassType(thisClazz)), fieldNode), initExpr)));
}
}
}
return null;
}
use of kalang.ast.ObjectFieldExpr 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.ObjectFieldExpr 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