use of kalang.ast.InvocationExpr in project kalang by kasonyang.
the class AstBuilder method visitMapExpr.
@Override
public MultiStmtExpr visitMapExpr(KalangParser.MapExprContext ctx) {
Type keyType = ctx.keyType != null ? requireClassType(ctx.keyType) : Types.getRootType();
Type valueType = ctx.valueType != null ? requireClassType(ctx.valueType) : Types.getRootType();
if (keyType == null || valueType == null)
return null;
LocalVarNode vo = declareTempLocalVar(Types.getClassType(Types.getMapImplClassType().getClassNode(), new Type[] { keyType, valueType }));
VarDeclStmt vds = new VarDeclStmt(vo);
NewObjectExpr newExpr;
try {
newExpr = new NewObjectExpr(Types.getMapImplClassType());
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
List<Statement> stmts = new LinkedList<>();
stmts.add(vds);
stmts.add(new ExprStmt(new AssignExpr(new VarExpr(vo), newExpr)));
VarExpr ve = new VarExpr(vo);
List<TerminalNode> ids = ctx.Identifier();
for (int i = 0; i < ids.size(); i++) {
ExpressionContext e = ctx.expression(i);
ExprNode v = (ExprNode) visit(e);
ConstExpr k = new ConstExpr(ctx.Identifier(i).getText());
ExprNode[] args = new ExprNode[] { k, v };
InvocationExpr iv;
try {
iv = ObjectInvokeExpr.create(ve, "put", args);
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
ExprStmt es = new ExprStmt(iv);
stmts.add(es);
}
MultiStmtExpr mse = new MultiStmtExpr(stmts, ve);
mapAst(mse, ctx);
return mse;
}
use of kalang.ast.InvocationExpr in project kalang by kasonyang.
the class BoxUtil method castPrimitive2Object.
private static ExprNode castPrimitive2Object(ExprNode expr, PrimitiveType fromType) {
ObjectType classType = Types.getClassType(fromType);
if (classType == null) {
throw new UnknownError("unknown primitive type:" + fromType);
}
InvocationExpr inv;
try {
inv = StaticInvokeExpr.create(new ClassReference(classType.getClassNode()), "valueOf", new ExprNode[] { expr });
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw new RuntimeException(ex);
}
return inv;
}
use of kalang.ast.InvocationExpr in project kalang by kasonyang.
the class AstUtil method isConstructorCallStatement.
public static boolean isConstructorCallStatement(Statement stmt) {
try {
ExprStmt exprStmt = (ExprStmt) stmt;
InvocationExpr invExpr = (InvocationExpr) exprStmt.getExpr();
ExecutableDescriptor method = invExpr.getMethod();
return method.getName().equals("<init>") && !Modifier.isStatic(method.getModifier());
} catch (ClassCastException ex) {
return false;
}
}
Aggregations