/** 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 List acceptCFG(CFGBuilder v, List succs) { v.visitCFGList(inits, (cond != null ? cond.entry() : body.entry())); if (cond != null) { if (condIsConstantTrue()) { v.visitCFG(cond, body.entry()); } else { v.visitCFG(cond, FlowGraph.EDGE_KEY_TRUE, body.entry(), FlowGraph.EDGE_KEY_FALSE, this); } } v.push(this).visitCFG(body, continueTarget()); v.visitCFGList(iters, (cond != null ? cond.entry() : body.entry())); return succs; }
/** 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(); }
/** 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; }
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(); }
public Object constantValue() { Object v = expr.constantValue(); if (v == null) { return null; } if (v instanceof Boolean) { if (castType.type().isBoolean()) return v; } if (v instanceof String) { TypeSystem ts = castType.type().typeSystem(); if (castType.type().equals(ts.String())) return v; } if (v instanceof Double) { double vv = ((Double) v).doubleValue(); if (castType.type().isDouble()) return new Double((double) vv); if (castType.type().isFloat()) return new Float((float) vv); if (castType.type().isLong()) return new Long((long) vv); if (castType.type().isInt()) return new Integer((int) vv); if (castType.type().isChar()) return new Character((char) vv); if (castType.type().isShort()) return new Short((short) vv); if (castType.type().isByte()) return new Byte((byte) vv); } if (v instanceof Float) { float vv = ((Float) v).floatValue(); if (castType.type().isDouble()) return new Double((double) vv); if (castType.type().isFloat()) return new Float((float) vv); if (castType.type().isLong()) return new Long((long) vv); if (castType.type().isInt()) return new Integer((int) vv); if (castType.type().isChar()) return new Character((char) vv); if (castType.type().isShort()) return new Short((short) vv); if (castType.type().isByte()) return new Byte((byte) vv); } if (v instanceof Number) { long vv = ((Number) v).longValue(); if (castType.type().isDouble()) return new Double((double) vv); if (castType.type().isFloat()) return new Float((float) vv); if (castType.type().isLong()) return new Long((long) vv); if (castType.type().isInt()) return new Integer((int) vv); if (castType.type().isChar()) return new Character((char) vv); if (castType.type().isShort()) return new Short((short) vv); if (castType.type().isByte()) return new Byte((byte) vv); } if (v instanceof Character) { char vv = ((Character) v).charValue(); if (castType.type().isDouble()) return new Double((double) vv); if (castType.type().isFloat()) return new Float((float) vv); if (castType.type().isLong()) return new Long((long) vv); if (castType.type().isInt()) return new Integer((int) vv); if (castType.type().isChar()) return new Character((char) vv); if (castType.type().isShort()) return new Short((short) vv); if (castType.type().isByte()) return new Byte((byte) vv); } // not a constant return null; }
public boolean isConstant() { return expr.isConstant() && castType.type().isPrimitive(); }
public Term entry() { return expr.entry(); }
public Term continueTarget() { return listEntry(iters, (cond != null ? cond.entry() : body.entry())); }
public Term entry() { return listEntry(inits, (cond != null ? cond.entry() : body.entry())); }