Search in sources :

Example 1 with ThisExpr

use of kalang.ast.ThisExpr in project kalang by kasonyang.

the class AstBuilder method getNodeById.

@Nullable
private AstNode getNodeById(@Nonnull String name, @Nullable Token token) {
    // find local var
    LocalVarNode var = this.getNamedLocalVar(name);
    if (var != null) {
        VarExpr ve = new VarExpr(var, getVarObjectType(var));
        if (token != null)
            mapAst(ve, token);
        return ve;
    }
    // find parameters
    ParameterNode paramNode = this.getNamedParameter(name);
    if (paramNode != null) {
        ParameterExpr ve = new ParameterExpr(paramNode, this.getVarObjectType(paramNode));
        if (token != null)
            mapAst(ve, token);
        return ve;
    }
    // find field
    ExprNode fieldExpr = this.getObjectFieldExpr(new ThisExpr(this.getThisType()), name, ParserRuleContext.EMPTY);
    if (fieldExpr == null)
        fieldExpr = this.getStaticFieldExpr(new ClassReference(thisClazz), name, ParserRuleContext.EMPTY);
    if (fieldExpr != null)
        return fieldExpr;
    ExprNode outerClassInstanceExpr = this.getOuterClassInstanceExpr(new ThisExpr(this.getThisType()));
    while (outerClassInstanceExpr != null) {
        ExprNode fe = this.getObjectFieldExpr(outerClassInstanceExpr, name, ParserRuleContext.EMPTY);
        if (fe == null)
            fe = this.getStaticFieldExpr(new ClassReference(thisClazz), name, ParserRuleContext.EMPTY);
        if (fe != null)
            return fe;
        outerClassInstanceExpr = this.getOuterClassInstanceExpr(outerClassInstanceExpr);
    }
    String resolvedTypeName = this.typeNameResolver.resolve(name, topClass, thisClazz);
    if (resolvedTypeName != null) {
        ClassReference clsRef = new ClassReference(requireAst(resolvedTypeName, token));
        if (token != null)
            mapAst(clsRef, token);
        return clsRef;
    }
    return null;
}
Also used : ExprNode(kalang.ast.ExprNode) ParameterNode(kalang.ast.ParameterNode) ParameterExpr(kalang.ast.ParameterExpr) VarExpr(kalang.ast.VarExpr) ClassReference(kalang.ast.ClassReference) LocalVarNode(kalang.ast.LocalVarNode) ThisExpr(kalang.ast.ThisExpr) Nullable(javax.annotation.Nullable)

Example 2 with ThisExpr

use of kalang.ast.ThisExpr 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;
}
Also used : ExprNode(kalang.ast.ExprNode) StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) FieldNode(kalang.ast.FieldNode) ExprStmt(kalang.ast.ExprStmt) ClassReference(kalang.ast.ClassReference) ThisExpr(kalang.ast.ThisExpr) VarDeclContext(kalang.antlr.KalangParser.VarDeclContext) LocalVarDeclContext(kalang.antlr.KalangParser.LocalVarDeclContext) AssignExpr(kalang.ast.AssignExpr)

Example 3 with ThisExpr

use of kalang.ast.ThisExpr in project kalang by kasonyang.

the class AstBuilder method getImplicitInvokeExpr.

@Nullable
private ExprNode getImplicitInvokeExpr(String methodName, ExprNode[] args, ParserRuleContext ctx) {
    ExprNode expr = null;
    try {
        ObjectType clazzType = getThisType();
        InvocationExpr.MethodSelection ms = InvocationExpr.applyMethod(clazzType, methodName, args, clazzType.getMethodDescriptors(thisClazz, true, true));
        if (Modifier.isStatic(ms.selectedMethod.getModifier())) {
            expr = new StaticInvokeExpr(new ClassReference(thisClazz), ms.selectedMethod, ms.appliedArguments);
        } else {
            expr = new ObjectInvokeExpr(new ThisExpr(getThisType()), ms.selectedMethod, ms.appliedArguments);
        }
    } catch (MethodNotFoundException ex) {
        if (args.length == 1 && (methodName.equals("print") || methodName.equals("println"))) {
            try {
                StaticFieldExpr fieldExpr = StaticFieldExpr.create(new ClassReference(Types.requireClassType("java.lang.System").getClassNode()), "out", null);
                expr = getObjectInvokeExpr(fieldExpr, methodName, args, ctx);
            } catch (FieldNotFoundException fnfEx) {
                throw Exceptions.unexceptedException(fnfEx);
            }
        }
        if (expr == null) {
            this.methodNotFound(ctx.getStart(), className, methodName, args);
            expr = new UnknownInvocationExpr(null, methodName, args);
        }
    } catch (AmbiguousMethodException ex) {
        methodIsAmbiguous(ctx.start, ex);
        return null;
    }
    mapAst(expr, ctx);
    return expr;
}
Also used : FieldNotFoundException(kalang.FieldNotFoundException) ExprNode(kalang.ast.ExprNode) StaticInvokeExpr(kalang.ast.StaticInvokeExpr) ObjectType(kalang.core.ObjectType) StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) ClassReference(kalang.ast.ClassReference) MethodNotFoundException(kalang.MethodNotFoundException) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) InvocationExpr(kalang.ast.InvocationExpr) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) ThisExpr(kalang.ast.ThisExpr) AmbiguousMethodException(kalang.AmbiguousMethodException) Nullable(javax.annotation.Nullable)

Example 4 with ThisExpr

use of kalang.ast.ThisExpr 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;
}
Also used : FieldNode(kalang.ast.FieldNode) AssignExpr(kalang.ast.AssignExpr) ExprNode(kalang.ast.ExprNode) StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) KalangParser(kalang.antlr.KalangParser) ExprStmt(kalang.ast.ExprStmt) ClassReference(kalang.ast.ClassReference) ThisExpr(kalang.ast.ThisExpr)

Example 5 with ThisExpr

use of kalang.ast.ThisExpr in project kalang by kasonyang.

the class ClassNodeMetaBuilder method visitClassDef.

@Override
public Object visitClassDef(KalangParser.ClassDefContext ctx) {
    thisClazz.annotations.addAll(astBuilder.getAnnotations(ctx.annotation()));
    thisClazz.modifier = astBuilder.parseModifier(ctx.varModifier());
    List<Token> gnrTypes = ctx.genericTypes;
    if (gnrTypes != null && !gnrTypes.isEmpty()) {
        for (Token g : gnrTypes) {
            // TODO suport generic type bounds in syntax
            GenericType gt = new GenericType(g.getText(), Types.getRootType(), null, NullableKind.NONNULL);
            thisClazz.declareGenericType(gt);
        }
    }
    ObjectType superType = null;
    if (ctx.parentClass != null) {
        ObjectType parentClass = astBuilder.parseClassType(ctx.parentClass);
        if (parentClass != null) {
            superType = parentClass;
        }
    } else {
        superType = Types.getRootType();
    }
    if (Modifier.isInterface(thisClazz.modifier)) {
        // TODO update syntax to support:interface extends T1,T2...
        thisClazz.addInterface(superType);
    } else {
        thisClazz.setSuperType(superType);
    }
    if (ctx.interfaces != null && ctx.interfaces.size() > 0) {
        for (KalangParser.ClassTypeContext itf : ctx.interfaces) {
            ObjectType itfClz = astBuilder.parseClassType(itf);
            if (itfClz != null) {
                thisClazz.addInterface(itfClz);
            }
        }
    }
    if (this.isDeclaringNonStaticInnerClass()) {
        ClassNode parentClass = thisClazz.enclosingClass;
        if (parentClass == null) {
            throw Exceptions.unexceptedValue(parentClass);
        }
        thisClazz.createField(Types.getClassType(parentClass), "this$0", Modifier.PRIVATE | ModifierConstant.SYNTHETIC);
    }
    visit(ctx.classBody());
    if (!ModifierUtil.isInterface(thisClazz.modifier) && !AstUtil.containsConstructor(thisClazz) && !AstUtil.createEmptyConstructor(thisClazz)) {
        this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "failed to create constructor with no parameters", ctx);
    }
    MethodNode[] methods = thisClazz.getDeclaredMethodNodes();
    for (int i = 0; i < methods.length; i++) {
        MethodNode node = methods[i];
        BlockStmt body = node.getBody();
        if (body != null) {
            if (AstUtil.isConstructor(node)) {
                // constructor
                if (this.isDeclaringNonStaticInnerClass()) {
                    ClassNode enclosingClass = thisClazz.enclosingClass;
                    if (enclosingClass == null) {
                        throw Exceptions.unexceptedValue(enclosingClass);
                    }
                    ParameterNode outerInstanceParam = node.createParameter(0, Types.getClassType(enclosingClass), "this$0");
                    ExprNode parentFieldExpr = astBuilder.getObjectFieldExpr(new ThisExpr(Types.getClassType(thisClazz)), "this$0", ParserRuleContext.EMPTY);
                    if (parentFieldExpr == null) {
                        throw Exceptions.unexceptedValue(parentFieldExpr);
                    }
                    body.statements.add(1, new ExprStmt(new AssignExpr((AssignableExpr) parentFieldExpr, new ParameterExpr(outerInstanceParam))));
                }
            }
        }
    }
    for (FieldNode fieldNode : thisClazz.getFields()) {
        int mdf = fieldNode.modifier;
        if (!AstUtil.hasGetter(thisClazz, fieldNode)) {
            AstUtil.createGetter(thisClazz, fieldNode, mdf);
        }
        if (!AstUtil.hasSetter(thisClazz, fieldNode)) {
            AstUtil.createSetter(thisClazz, fieldNode, mdf);
        }
        fieldNode.modifier = ModifierUtil.setPrivate(mdf);
    }
    return null;
}
Also used : ClassNode(kalang.ast.ClassNode) GenericType(kalang.core.GenericType) FieldNode(kalang.ast.FieldNode) ParameterExpr(kalang.ast.ParameterExpr) BlockStmt(kalang.ast.BlockStmt) Token(org.antlr.v4.runtime.Token) AssignExpr(kalang.ast.AssignExpr) ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) KalangParser(kalang.antlr.KalangParser) MethodNode(kalang.ast.MethodNode) ParameterNode(kalang.ast.ParameterNode) ExprStmt(kalang.ast.ExprStmt) 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