Search in sources :

Example 1 with SuperExpr

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

the class Ast2Class method visitInvocationExpr.

@Override
public Object visitInvocationExpr(InvocationExpr node) {
    int opc;
    ExecutableDescriptor method = node.getMethod();
    // = internalName(node.getMethod().classNode);
    String ownerClass;
    if (node instanceof StaticInvokeExpr) {
        opc = INVOKESTATIC;
        ownerClass = internalName(((StaticInvokeExpr) node).getInvokeClass().getReferencedClassNode());
    } else if (node instanceof ObjectInvokeExpr) {
        ObjectInvokeExpr oie = (ObjectInvokeExpr) node;
        ObjectType targetType = (ObjectType) oie.getInvokeTarget().getType();
        ownerClass = internalName(targetType);
        ExprNode target = oie.getInvokeTarget();
        visit(target);
        if (Modifier.isPrivate(method.getModifier()) || (target instanceof SuperExpr) || method.getName().equals("<init>")) {
            opc = INVOKESPECIAL;
        } else {
            opc = ModifierUtil.isInterface(targetType.getClassNode().modifier) ? INVOKEINTERFACE : INVOKEVIRTUAL;
        }
    } else {
        throw Exceptions.unsupportedTypeException(node);
    }
    visitAll(node.getArguments());
    md.visitMethodInsn(opc, ownerClass, method.getName(), getMethodDescriptor(method.getMethodNode()));
    return null;
}
Also used : StaticInvokeExpr(kalang.ast.StaticInvokeExpr) ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) SuperExpr(kalang.ast.SuperExpr) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Example 2 with SuperExpr

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

the class AstUtil method createEmptyConstructor.

public static boolean createEmptyConstructor(ClassNode clazzNode) {
    ObjectType supType = clazzNode.getSuperType();
    if (supType == null) {
        throw new RuntimeException("super type is null:" + clazzNode.name);
    }
    ConstructorDescriptor[] constructors = supType.getConstructorDescriptors(clazzNode);
    ConstructorDescriptor m = MethodUtil.getConstructorDescriptor(constructors, null);
    if (m != null) {
        MethodNode mm = clazzNode.createMethodNode(Types.VOID_TYPE, m.getName(), m.getModifier());
        for (Type e : m.getExceptionTypes()) mm.addExceptionType(e);
        ParameterDescriptor[] pds = m.getParameterDescriptors();
        for (ParameterDescriptor pd : pds) {
            ParameterNode p = mm.createParameter(pd.getType(), pd.getName());
            // TODO update mm.createParameter
            p.modifier = pd.getModifier();
        }
        BlockStmt body = mm.getBody();
        ParameterNode[] parameters = mm.getParameters();
        ExprNode[] params = new ExprNode[parameters.length];
        for (int i = 0; i < params.length; i++) {
            params[i] = new ParameterExpr(parameters[i]);
        }
        body.statements.add(new ExprStmt(new ObjectInvokeExpr(new SuperExpr(clazzNode), m, params)));
        return true;
    } else {
        return false;
    }
}
Also used : SuperExpr(kalang.ast.SuperExpr) ParameterExpr(kalang.ast.ParameterExpr) ParameterDescriptor(kalang.core.ParameterDescriptor) BlockStmt(kalang.ast.BlockStmt) ConstructorDescriptor(kalang.core.ConstructorDescriptor) ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) ClassType(kalang.core.ClassType) MethodNode(kalang.ast.MethodNode) ParameterNode(kalang.ast.ParameterNode) ExprStmt(kalang.ast.ExprStmt) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr)

Example 3 with SuperExpr

use of kalang.ast.SuperExpr 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 4 with SuperExpr

use of kalang.ast.SuperExpr 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)

Aggregations

SuperExpr (kalang.ast.SuperExpr)4 ExprNode (kalang.ast.ExprNode)3 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)3 ObjectType (kalang.core.ObjectType)3 ThisExpr (kalang.ast.ThisExpr)2 AmbiguousMethodException (kalang.AmbiguousMethodException)1 MethodNotFoundException (kalang.MethodNotFoundException)1 AstNode (kalang.ast.AstNode)1 BlockStmt (kalang.ast.BlockStmt)1 ExprStmt (kalang.ast.ExprStmt)1 InvocationExpr (kalang.ast.InvocationExpr)1 MethodNode (kalang.ast.MethodNode)1 ParameterExpr (kalang.ast.ParameterExpr)1 ParameterNode (kalang.ast.ParameterNode)1 StaticInvokeExpr (kalang.ast.StaticInvokeExpr)1 UnknownInvocationExpr (kalang.ast.UnknownInvocationExpr)1 VarObject (kalang.ast.VarObject)1 ArrayType (kalang.core.ArrayType)1 ClassType (kalang.core.ClassType)1 ConstructorDescriptor (kalang.core.ConstructorDescriptor)1