Search in sources :

Example 6 with ThisExpr

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)));
}
Also used : ParameterExpr(kalang.ast.ParameterExpr) BlockStmt(kalang.ast.BlockStmt) AssignExpr(kalang.ast.AssignExpr) ExprNode(kalang.ast.ExprNode) StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) MethodNode(kalang.ast.MethodNode) ParameterNode(kalang.ast.ParameterNode) ExprStmt(kalang.ast.ExprStmt) ClassReference(kalang.ast.ClassReference) StaticFieldExpr(kalang.ast.StaticFieldExpr) FieldExpr(kalang.ast.FieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) ThisExpr(kalang.ast.ThisExpr)

Example 7 with ThisExpr

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;
}
Also used : SuperExpr(kalang.ast.SuperExpr) AstNode(kalang.ast.AstNode) ThisExpr(kalang.ast.ThisExpr)

Example 8 with ThisExpr

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;
}
Also used : SuperExpr(kalang.ast.SuperExpr) ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) VarObject(kalang.ast.VarObject) MethodNotFoundException(kalang.MethodNotFoundException) ThisExpr(kalang.ast.ThisExpr) InvocationExpr(kalang.ast.InvocationExpr) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 9 with ThisExpr

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;
    }
}
Also used : ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) NewObjectExpr(kalang.ast.NewObjectExpr) MethodNotFoundException(kalang.MethodNotFoundException) LinkedList(java.util.LinkedList) ThisExpr(kalang.ast.ThisExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 10 with ThisExpr

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));
}
Also used : StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) MethodNode(kalang.ast.MethodNode) BlockStmt(kalang.ast.BlockStmt) ClassReference(kalang.ast.ClassReference) StaticFieldExpr(kalang.ast.StaticFieldExpr) FieldExpr(kalang.ast.FieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) ReturnStmt(kalang.ast.ReturnStmt) ThisExpr(kalang.ast.ThisExpr)

Aggregations

ThisExpr (kalang.ast.ThisExpr)10 ExprNode (kalang.ast.ExprNode)8 ClassReference (kalang.ast.ClassReference)6 StaticFieldExpr (kalang.ast.StaticFieldExpr)5 AssignExpr (kalang.ast.AssignExpr)4 ExprStmt (kalang.ast.ExprStmt)4 ObjectFieldExpr (kalang.ast.ObjectFieldExpr)4 ObjectType (kalang.core.ObjectType)4 AmbiguousMethodException (kalang.AmbiguousMethodException)3 MethodNotFoundException (kalang.MethodNotFoundException)3 BlockStmt (kalang.ast.BlockStmt)3 FieldNode (kalang.ast.FieldNode)3 MethodNode (kalang.ast.MethodNode)3 ParameterExpr (kalang.ast.ParameterExpr)3 ParameterNode (kalang.ast.ParameterNode)3 Nullable (javax.annotation.Nullable)2 KalangParser (kalang.antlr.KalangParser)2 FieldExpr (kalang.ast.FieldExpr)2 InvocationExpr (kalang.ast.InvocationExpr)2 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)2