use of kalang.ast.ObjectInvokeExpr 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.ObjectInvokeExpr in project kalang by kasonyang.
the class AstBuilder method getObjectInvokeExpr.
@Nullable
private ExprNode getObjectInvokeExpr(ExprNode target, String methodName, ExprNode[] args, ParserRuleContext ctx) {
if ("<init>".equals(methodName)) {
throw Exceptions.unexceptedException("Don't get constructor by this method.");
}
Type targetType = target.getType();
if (!(targetType instanceof ObjectType)) {
handleSyntaxError("class type required.", ctx);
return null;
}
ObjectType targetClassType = (ObjectType) targetType;
if (targetClassType.getNullable() == NullableKind.NULLABLE) {
handleSyntaxError("expression may be null", ctx);
return null;
}
ExprNode expr;
try {
ObjectInvokeExpr invoke = ObjectInvokeExpr.create(target, methodName, args, thisClazz);
if (invoke.getMethod().getMethodNode().getType() instanceof GenericType) {
Type invokeType = invoke.getType();
if (invokeType instanceof ObjectType) {
expr = new CastExpr(invokeType, invoke);
} else {
expr = invoke;
}
} else {
expr = invoke;
}
} catch (MethodNotFoundException ex) {
methodNotFound(ctx.start, className, methodName, args);
expr = new UnknownInvocationExpr(target, methodName, args);
} catch (AmbiguousMethodException ex) {
methodIsAmbiguous(ctx.start, ex);
return null;
}
mapAst(expr, ctx);
return expr;
}
use of kalang.ast.ObjectInvokeExpr 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;
}
use of kalang.ast.ObjectInvokeExpr 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.ObjectInvokeExpr in project kalang by kasonyang.
the class AstBuilder method visitForEachStat.
@Override
public Object visitForEachStat(KalangParser.ForEachStatContext ctx) {
BlockStmt block = newBlock();
ExprNode expr = this.visitExpression(ctx.expression());
Type exprType = expr.getType();
List<TerminalNode> idsCtx = ctx.Identifier();
VarExpr indexVarExpr = null;
TerminalNode varId;
if (idsCtx.size() == 1) {
varId = idsCtx.get(0);
} else {
TerminalNode indexId = idsCtx.get(0);
LocalVarNode indexVar = this.declareLocalVar(indexId.getText(), Types.INT_TYPE, Modifier.FINAL, ctx);
if (indexVar == null)
return null;
block.statements.add(new VarDeclStmt(indexVar));
indexVarExpr = new VarExpr(indexVar);
varId = idsCtx.get(1);
}
LoopStmt loopStmt;
if (exprType instanceof ArrayType) {
LocalVarNode localVarNode = this.declareLocalVar(varId.getText(), ((ArrayType) exprType).getComponentType(), Modifier.FINAL, ctx);
if (localVarNode == null)
return null;
VarExpr localVariable = new VarExpr(localVarNode);
block.statements.add(new VarDeclStmt(localVarNode));
LocalVarNode lenVar = this.declareTempLocalVar(Types.INT_TYPE);
LocalVarNode counterVar = this.declareTempLocalVar(Types.INT_TYPE);
// var len
block.statements.add(new VarDeclStmt(lenVar));
// var i
block.statements.add(new VarDeclStmt(counterVar));
VarExpr counterVarExpr = new VarExpr(counterVar);
VarExpr lenVarExpr = new VarExpr(lenVar);
block.statements.add(new ExprStmt(new AssignExpr(lenVarExpr, new ArrayLengthExpr(expr))));
// l = array.length
block.statements.add(new ExprStmt(new AssignExpr(counterVarExpr, new ConstExpr(0))));
// i=0
ExprNode cnd = new CompareExpr(counterVarExpr, lenVarExpr, CompareExpr.OP_LT);
BlockStmt loopBody = this.newBlock();
loopBody.statements.add(new ExprStmt(new AssignExpr(localVariable, new ElementExpr(expr, counterVarExpr))));
if (indexVarExpr != null) {
loopBody.statements.add(new ExprStmt(new AssignExpr(indexVarExpr, counterVarExpr)));
}
loopBody.statements.add(visitStat(ctx.stat()));
popBlock();
BlockStmt updateBs = newBlock();
// increment counter
updateBs.statements.add(new ExprStmt(new AssignExpr(counterVarExpr, new MathExpr(counterVarExpr, new ConstExpr(1), MathExpr.OP_ADD))));
popBlock();
loopStmt = new LoopStmt(cnd, null, loopBody, updateBs);
} else {
ObjectType iterType = Types.getIterableClassType();
if (iterType.isAssignableFrom(exprType)) {
ObjectInvokeExpr getIterableExpr;
try {
getIterableExpr = ObjectInvokeExpr.create(expr, "iterator", null);
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
LocalVarNode iterableVarNode = this.declareTempLocalVar(getIterableExpr.getType());
block.statements.add(new VarDeclStmt(iterableVarNode));
VarExpr iterableVarExpr = new VarExpr(iterableVarNode);
block.statements.add(new ExprStmt(new AssignExpr(iterableVarExpr, getIterableExpr)));
// set index = 0
if (indexVarExpr != null) {
block.statements.add(new ExprStmt(new AssignExpr(indexVarExpr, new ConstExpr(0))));
}
ObjectInvokeExpr cnd;
try {
cnd = ObjectInvokeExpr.create(iterableVarExpr, "hasNext", null);
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
BlockStmt loopBody = this.newBlock();
ObjectInvokeExpr nextInvokeExpr;
try {
nextInvokeExpr = ObjectInvokeExpr.create(iterableVarExpr, "next", null);
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
LocalVarNode localVarNode = this.declareLocalVar(varId.getText(), nextInvokeExpr.getType(), Modifier.FINAL, ctx);
if (localVarNode == null)
return null;
VarExpr localVariable = new VarExpr(localVarNode);
loopBody.statements.add(new VarDeclStmt(localVarNode));
loopBody.statements.add(new ExprStmt(new AssignExpr(localVariable, new CastExpr(localVariable.getType(), nextInvokeExpr))));
loopBody.statements.add(visitStat(ctx.stat()));
popBlock();
BlockStmt updateBs = newBlock();
if (indexVarExpr != null) {
// do index++
updateBs.statements.add(new ExprStmt(new AssignExpr(indexVarExpr, new MathExpr(indexVarExpr, new ConstExpr(1), BinaryExpr.OP_ADD))));
}
popBlock();
loopStmt = new LoopStmt(cnd, null, loopBody, updateBs);
} else {
this.handleSyntaxError("require array type or iterable type", ctx.expression());
loopStmt = null;
}
}
popBlock();
if (loopStmt != null)
block.statements.add(loopStmt);
return block;
}
Aggregations