use of kalang.ast.ThisExpr 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.ThisExpr in project kalang by kasonyang.
the class AstBuilder method visitSelfRefExpr.
@Override
public AstNode visitSelfRefExpr(SelfRefExprContext ctx) {
String key = ctx.ref.getText();
AstNode expr;
if (key.equals("this")) {
expr = new ThisExpr(getThisType());
} else if (key.equals("super")) {
expr = new SuperExpr(thisClazz);
} else {
throw Exceptions.unknownValue(key);
}
mapAst(expr, ctx);
return expr;
}
use of kalang.ast.ThisExpr in project kalang by kasonyang.
the class AstBuilder method visitMemberInvocationExpr.
@Override
public ExprNode visitMemberInvocationExpr(MemberInvocationExprContext ctx) {
String methodName;
ExprNode target;
ObjectType clazz;
if (ctx.key != null) {
methodName = ctx.key.getText();
} else {
methodName = ctx.Identifier().getText();
}
if (methodName.equals("this")) {
methodName = "<init>";
target = new ThisExpr(this.getThisType());
clazz = this.getThisType();
} else if (methodName.equals("super")) {
methodName = "<init>";
target = new SuperExpr(thisClazz);
clazz = thisClazz.getSuperType();
} else {
target = new ThisExpr(this.getThisType());
clazz = this.getThisType();
}
List<Object> argsList = visitAll(ctx.params);
if (argsList.contains(null))
return null;
ExprNode[] args = argsList.toArray(new ExprNode[argsList.size()]);
ExprNode ie;
if (methodName.equals("<init>")) {
if (clazz == null)
throw Exceptions.unexceptedValue(clazz);
try {
InvocationExpr.MethodSelection apply = InvocationExpr.applyMethod(clazz, methodName, args, clazz.getConstructorDescriptors(thisClazz));
ie = new ObjectInvokeExpr(target, apply.selectedMethod, apply.appliedArguments);
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
this.methodNotFound(ctx.start, clazz.getName(), methodName, args);
return null;
}
} else {
ie = getImplicitInvokeExpr(methodName, args, ctx);
}
return ie;
}
use of kalang.ast.ThisExpr in project kalang by kasonyang.
the class AstBuilder method visitNewExpr.
@Override
public AstNode visitNewExpr(NewExprContext ctx) {
ObjectType clsType = parseClassType(ctx.classType());
if (clsType == null)
return null;
ExprNode[] params = visitAll(ctx.params).toArray(new ExprNode[0]);
List<ExprNode> paramList = new LinkedList(Arrays.asList(params));
NewObjectExpr newExpr;
try {
if (this.isNonStaticInnerClass(clsType.getClassNode())) {
paramList.add(0, new ThisExpr(this.getThisType()));
}
params = paramList.toArray(new ExprNode[paramList.size()]);
newExpr = new NewObjectExpr(clsType, params);
mapAst(newExpr, ctx);
return newExpr;
} catch (MethodNotFoundException ex) {
methodNotFound(ctx.classType().rawClass, clsType.getName(), "<init>", params);
return null;
} catch (AmbiguousMethodException ex) {
methodIsAmbiguous(ctx.classType().rawClass, ex);
return null;
}
}
use of kalang.ast.ThisExpr 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