/** Type check the expression. */ public Node typeCheck(TypeChecker tc) throws SemanticException { TypeSystem ts = tc.typeSystem(); if (!ts.isCastValid(expr.type(), castType.type())) { throw new SemanticException( "Cannot cast the expression of type \"" + expr.type() + "\" to type \"" + castType.type() + "\".", position()); } return type(castType.type()); }
public List throwTypes(TypeSystem ts) { if (expr.type().isReference()) { return Collections.singletonList(ts.ClassCastException()); } return Collections.EMPTY_LIST; }
public Node typeCheck(TypeChecker tc) throws SemanticException { ETValueHolder vh = (ETValueHolder) ((csTypeSystem_c) tc.typeSystem()).getValueHolder(); if (!vh.isDynamicValue(((CSObjectType) obj.type()).getAliasName(), ETTYPE.MODE)) throw new SemanticException("Using argument must be dynamic", obj.position()); return this; }
public Node typeCheck(TypeChecker tc) throws SemanticException { JL5Call_c n = null; JL5TypeSystem ts = (JL5TypeSystem) tc.typeSystem(); JL5Context c = (JL5Context) tc.context(); ReferenceType targetType = null; List<Type> explicitTypeArgs = null; List<Type> paramTypes = new ArrayList<Type>(); if (typeArguments != null && !typeArguments.isEmpty()) { explicitTypeArgs = new ArrayList<Type>(); if (target() == null) { // should not actually happen. grammar doesn't allow it throw new SemanticException( "Explicit target required when using explicit type arguments", position()); } for (Iterator it = typeArguments().iterator(); it.hasNext(); ) { explicitTypeArgs.add(((TypeNode) it.next()).type()); } } for (Iterator i = this.arguments().iterator(); i.hasNext(); ) { Expr e = (Expr) i.next(); paramTypes.add(e.type()); } JL5MethodInstance mi; // JLS 15.12.1 if (target == null) { return typeCheckNullTarget(tc, paramTypes, explicitTypeArgs); } else { targetType = this.findTargetType(); mi = ts.findJL5Method(targetType, name, paramTypes, explicitTypeArgs, c); } boolean staticContext = (this.target instanceof TypeNode); if (staticContext && !mi.flags().isStatic()) { throw new SemanticException( "Cannot call non-static method " + this.name + " of " + targetType + " in static " + "context.", this.position()); } if (this.target instanceof Special && ((Special) this.target).kind() == Special.SUPER && mi.flags().isAbstract()) { throw new SemanticException( "Cannot call an abstract method " + "of the super class", this.position()); } n = (JL5Call_c) this.methodInstance(mi).type(mi.returnType()); // n.checkConsistency(c); return n; }
/** Flatten complex expressions within the AST */ public Node leave(Node old, Node n, NodeVisitor v) { if (n == noFlatten) { noFlatten = null; return n; } if (n instanceof Block) { List l = (List) stack.removeFirst(); return ((Block) n).statements(l); } else if (n instanceof Stmt && !(n instanceof LocalDecl)) { List l = (List) stack.getFirst(); l.add(n); return n; } else if (n instanceof Expr && !(n instanceof Lit) && !(n instanceof Special) && !(n instanceof Local)) { Expr e = (Expr) n; if (e instanceof Assign) { return n; } // create a local temp, initialized to the value of the complex // expression String name = newID(); LocalDecl def = nf.LocalDecl( e.position(), Flags.FINAL, nf.CanonicalTypeNode(e.position(), e.type()), name, e); def = def.localInstance(ts.localInstance(e.position(), Flags.FINAL, e.type(), name)); List l = (List) stack.getFirst(); l.add(def); // return the local temp instead of the complex expression Local use = nf.Local(e.position(), name); use = (Local) use.type(e.type()); use = use.localInstance(ts.localInstance(e.position(), Flags.FINAL, e.type(), name)); return use; } return n; }
public Type childExpectedType(Expr child, AscriptionVisitor av) { TypeSystem ts = av.typeSystem(); if (child == cond) { return ts.Boolean(); } return child.type(); }
public ConstructorCall mangledCall(ConstructorCall cc, AJNodeFactory nf, AJTypeSystem ts) { if (flags().isPrivate() || flags.isPackage()) { Expr nl = nf.NullLit(cc.position()); nl = nl.type(mangleType); List args = new LinkedList(cc.arguments()); args.add(nl); ConstructorCall nc = (ConstructorCall) cc.arguments(args); return nc.constructorInstance(mangled()); } else return cc; }
public Type childExpectedType(Expr child, AscriptionVisitor av) { TypeSystem ts = av.typeSystem(); if (child == expr) { if (castType.type().isReference()) { return ts.Object(); } else if (castType.type().isNumeric()) { return ts.Double(); } else if (castType.type().isBoolean()) { return ts.Boolean(); } } return child.type(); }
/** Type check the statement. */ public Node typeCheck(TypeChecker tc) throws SemanticException { TypeSystem ts = tc.typeSystem(); // Check that all initializers have the same type. // This should be enforced by the parser, but check again here, // just to be sure. Type t = null; for (Iterator i = inits.iterator(); i.hasNext(); ) { ForInit s = (ForInit) i.next(); if (s instanceof LocalDecl) { LocalDecl d = (LocalDecl) s; Type dt = d.type().type(); if (t == null) { t = dt; } else if (!t.equals(dt)) { throw new InternalCompilerError( "Local variable " + "declarations in a for loop initializer must all " + "be the same type, in this case " + t + ", not " + dt + ".", d.position()); } } } if (cond != null && !ts.isImplicitCastValid(cond.type(), ts.Boolean())) { throw new SemanticException( "The condition of a for statement must have boolean type.", cond.position()); } return this; }
/** Type check the expression. */ public Node typeCheck(ContextVisitor tc) { return type(result == null ? tc.typeSystem().Void() : result.type()); }
private Node translateExtForArray(ExtendedFor n, List<String> labels) throws SemanticException { Position pos = Position.compilerGenerated(); Type iteratedType = n.decl().type().type(); // translate "L1,...,Ln: for (C x: e) b" to // "{ C[] arr = e; int iter = 0; L1,...,Ln: while (iter < arr.length) { C x = arr[iter]; b ; // iter = iter + 1; }" List<Stmt> stmts = new ArrayList<Stmt>(); // add the declaration of arr: "C[] arr = e" Id arrID = freshName("arr"); LocalInstance arrLI = ts.localInstance(pos, Flags.NONE, n.expr().type(), arrID.id()); { LocalDecl ld = nodeFactory() .LocalDecl( pos, Flags.NONE, nodeFactory().CanonicalTypeNode(pos, arrLI.type()), arrID); ld = ld.localInstance(arrLI); ld = ld.init(n.expr()); stmts.add(ld); } // add the declaration of iterator: "int iter = 0" Id iterID = freshName("iter"); LocalInstance iterLI = ts.localInstance(pos, Flags.NONE, ts.Int(), iterID.id()); { LocalDecl ld = nodeFactory() .LocalDecl( pos, Flags.NONE, nodeFactory().CanonicalTypeNode(pos, iterLI.type()), iterID); ld = ld.localInstance(iterLI); ld = ld.init(nodeFactory().IntLit(pos, IntLit.INT, 0).type(ts.Int())); stmts.add(ld); } // build the conditional "iter < arr.length" Expr cond; { Local iterLocal = (Local) nodeFactory().Local(pos, iterID).localInstance(iterLI).type(ts.Int()); Local arrLocal = (Local) nodeFactory().Local(pos, arrID).localInstance(arrLI).type(arrLI.type()); Id id = nodeFactory().Id(pos, "length"); Field field = (Field) nodeFactory().Field(pos, arrLocal, id).type(ts.Int()); field = field.fieldInstance(ts.findField(arrLI.type().toReference(), "length")); cond = nodeFactory().Binary(pos, iterLocal, Binary.LT, field).type(ts.Boolean()); } // build the initlizer for the local decl: arr[iter] Expr init; { Local iterLocal = (Local) nodeFactory().Local(pos, iterID).localInstance(iterLI).type(ts.Int()); Local arrLocal = (Local) nodeFactory().Local(pos, arrID).localInstance(arrLI).type(arrLI.type()); init = nodeFactory().ArrayAccess(pos, arrLocal, iterLocal); init = init.type(iteratedType); } // build the increment for iter (iter = iter + 1;) Stmt inc; { Local iterLocal = (Local) nodeFactory().Local(pos, iterID).localInstance(iterLI).type(ts.Int()); Expr incExpr = nodeFactory() .Binary( pos, iterLocal.type(ts.Int()), Binary.ADD, nodeFactory().IntLit(pos, IntLit.INT, 1).type(ts.Int())) .type(ts.Int()); Assign incStore = (Assign) nodeFactory().Assign(pos, iterLocal, Assign.ASSIGN, incExpr).type(ts.Int()); inc = nodeFactory().Eval(pos, incStore); } // build the while loop { // Create a new loop body from the old body followed by the increment Block loopBody = nodeFactory().Block(pos, n.decl().init(init), n.body(), inc); While loop = nodeFactory().While(pos, cond, loopBody); stmts.add(labelStmt(loop, labels)); } return nodeFactory().Block(pos, stmts); }
private Node unbox(Expr expr) { Position pos = Position.compilerGenerated(null); return xnf.X10Cast( pos, xnf.CanonicalTypeNode(pos, expr.type()), expr, Converter.ConversionType.UNBOXING) .type(expr.type()); }