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;
}
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;
}
}
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;
}
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;
}
Aggregations