public Type childExpectedType(Expr child, AscriptionVisitor av) { TypeSystem ts = av.typeSystem(); if (child == cond) { return ts.Boolean(); } return child.type(); }
/** 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()); }
/** Invalidates the given type, so that no properties on it will be renamed. */ private void addInvalidatingType(JSType type) { type = type.restrictByNotNullOrUndefined(); if (type instanceof UnionType) { for (JSType alt : ((UnionType) type).getAlternates()) { addInvalidatingType(alt); } return; } typeSystem.addInvalidatingType(type); ObjectType objType = ObjectType.cast(type); if (objType != null && objType.getImplicitPrototype() != null) { typeSystem.addInvalidatingType(objType.getImplicitPrototype()); } }
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(); }
protected Object resolveObject(Object o) { if (!enableReplace) { return o; } String s = ""; if (reporter.should_report(reporter.serialize, 2)) { try { s = o.toString(); } catch (NullPointerException e) { s = "<NullPointerException thrown>"; } } if (!enableReplace) { return o; } else if (o instanceof Internable) { if (reporter.should_report(Reporter.serialize, 2)) { reporter.report(2, "- Interning " + s + " : " + o.getClass()); } return ((Internable) o).intern(); } else if (o instanceof Goal) { return ((Goal) o).intern(ts.extensionInfo().scheduler()); } else { if (reporter.should_report(Reporter.serialize, 2)) { reporter.report(2, "- " + s + " : " + o.getClass()); } return o; } }
private IType getTheRef() { if (this instanceof INonLoadableType) { return this; } else { return TypeSystem.getOrCreateTypeReference(this); } }
protected void checkVarDef(Stmt.VarDef def) { // Observe that we cannot use the declared type here, rather we have to // use the resolved type! Type t = def.type().attribute(Type.class); for (Triple<String, Integer, Expr> d : def.definitions()) { if (d.third() != null) { checkExpression(d.third()); Type nt = t; for (int i = 0; i != d.second(); ++i) { nt = new Type.Array(nt); } Type i_t = d.third().attribute(Type.class); try { if (!types.subtype(nt, i_t, loader)) { ErrorHandler.handleTypeMismatch( new TypeMismatchException(d.third(), nt, loader, types), d.third().attribute(SourceLocation.class)); } } catch (ClassNotFoundException ex) { syntax_error(ex.getMessage(), def); } } } }
public List throwTypes(TypeSystem ts) { if (expr.type().isReference()) { return Collections.singletonList(ts.ClassCastException()); } return Collections.EMPTY_LIST; }
protected void checkInstanceOf(Expr.InstanceOf e) throws ClassNotFoundException { checkExpression(e.lhs()); Type lhs_t = e.lhs().attribute(Type.class); final Type rhs_t = e.rhs().attribute(Type.class); if (lhs_t instanceof Type.Primitive) { ErrorHandler.handleTypeMismatch( new TypeMismatchException( e.lhs(), new Type.Wildcard(JAVA_LANG_OBJECT, null), loader, types), e.lhs().attribute(SourceLocation.class)); } else if (!(rhs_t instanceof Type.Reference)) { // This is an unusual case - the error is in the source code, so we can't // make a suggestion by reflection. We just make a generic suggestion syntax_error( "Syntax Error: Can only check if object is instance of an array or class type", e.rhs()); } else if ((lhs_t instanceof Type.Array || rhs_t instanceof Type.Array) && !(types.subtype(lhs_t, rhs_t, loader))) { ErrorHandler.handleTypeMismatch( new TypeMismatchException(e.lhs(), rhs_t, loader, types), e.lhs().attribute(SourceLocation.class)); } }
protected void checkReturn(Stmt.Return ret) { JavaMethod method = (JavaMethod) getEnclosingScope(JavaMethod.class); if (method instanceof JavaConstructor) { return; // could do better than this. } Type retType = method.returnType().attribute(Type.class); if (ret.expr() != null) { checkExpression(ret.expr()); Type ret_t = ret.expr().attribute(Type.class); try { if (ret_t.equals(new Type.Void())) { syntax_error("cannot return a value from method whose result type is void", ret); } else if (!types.subtype(retType, ret_t, loader)) { syntax_error("required return type " + retType + ", found type " + ret_t, ret); } } catch (ClassNotFoundException ex) { syntax_error(ex.getMessage(), ret); } } else if (!(retType instanceof Type.Void)) { syntax_error("missing return value", ret); } }
/** Find a type by name. */ @Override public Named find(String name) throws SemanticException { if (Report.should_report(report_topics, 3)) Report.report(3, "MemberCR.find(" + name + ")"); if (nocache.contains(name)) { throw new NoClassException(name); } Named n = ts.systemResolver().check(name); if (n != null) { return n; } SemanticException error = null; // First, just try the long name. try { if (Report.should_report(report_topics, 2)) Report.report(2, "MCR: loading " + name + " from " + inner); return inner.find(name); } catch (SemanticException e) { if (Report.should_report(report_topics, 2)) Report.report(2, "MCR: " + e.getMessage()); if (StringUtil.isNameShort(name)) { throw e; } error = e; } boolean install = true; // Now try the prefix of the name and look for a member class // within it named with the suffix. String prefix = StringUtil.getPackageComponent(name); String suffix = StringUtil.getShortNameComponent(name); // Try the full name of the prefix first, then the raw class name, // so that encoded type information and source files are preferred // to the raw class file. try { if (Report.should_report(report_topics, 2)) Report.report(2, "MCR: loading prefix " + prefix); n = find(prefix); // This may be called during deserialization; n's // member classes might not be initialized yet. if (n instanceof ParsedTypeObject) { return findMember(n, suffix); } } catch (SemanticException e) { } if (install) { nocache.add(name); } throw error; }
/** 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 void process(Node externs, Node root) { for (TypeMismatch mis : compiler.getTypeValidator().getMismatches()) { addInvalidatingType(mis.typeA); addInvalidatingType(mis.typeB); } StaticScope<T> scope = typeSystem.getRootScope(); NodeTraversal.traverse(compiler, externs, new FindExternProperties()); NodeTraversal.traverse(compiler, root, new FindRenameableProperties()); renameProperties(); }
protected void checkCast(Expr.Cast e) { Type e_t = e.expr().attribute(Type.class); Type c_t = e.type().attribute(Type.class); try { if (e_t instanceof Type.Clazz && c_t instanceof Type.Clazz) { Clazz c_c = loader.loadClass((Type.Clazz) c_t); Clazz e_c = loader.loadClass((Type.Clazz) e_t); // the trick here, is that javac will never reject a cast // between an interface and a class or interface. However, if we // have a cast from one class to another class, then it will // reject this if neither is a subclass of the other. if (c_c.isInterface() || e_c.isInterface()) { // cast cannot fail here. return; } } else if ((e_t instanceof Type.Variable || e_t instanceof Type.Wildcard) && c_t instanceof Type.Reference) { // javac always lets this pass, no matter what return; } if (types.boxSubtype(c_t, e_t, loader) || types.boxSubtype(e_t, c_t, loader)) { // this is OK return; } else if (c_t instanceof Type.Primitive && e_t instanceof Type.Primitive) { if (e_t instanceof Type.Char && (c_t instanceof Type.Byte || c_t instanceof Type.Short)) { return; } else if (c_t instanceof Type.Char && (e_t instanceof Type.Byte || e_t instanceof Type.Short)) { return; } } ErrorHandler.handleTypeMismatch( new TypeMismatchException(e.expr(), c_t, loader, types), e.attribute(SourceLocation.class)); } catch (ClassNotFoundException ex) { syntax_error(ex.getMessage(), e); } }
protected void checkConvert(Expr.Convert e) { Type rhs_t = e.expr().attribute(Type.class); Type c_t = (Type) e.type().attribute(Type.class); try { if (!types.subtype(c_t, rhs_t, loader)) { ErrorHandler.handleTypeMismatch( new TypeMismatchException(e.expr(), c_t, loader, types), e.expr().attribute(SourceLocation.class)); } } catch (ClassNotFoundException ex) { syntax_error(ex.getMessage(), e); } }
public TypeInputStream(InputStream in, TypeSystem ts, Map<Object, Object> cache) throws IOException { super(in); enableResolveObject(true); this.ts = ts; this.reporter = ts.extensionInfo().getOptions().reporter; this.cache = cache; this.failed = false; this.enableReplace = true; this.placeHoldersUsed = CollectionFactory.newHashSet(); }
@Test public void test1() throws AtlasException { TypeSystem ts = getTypeSystem(); defineDeptEmployeeTypes(ts); Referenceable hrDept = createDeptEg1(ts); ClassType deptType = ts.getDataType(ClassType.class, "Department"); ITypedReferenceableInstance hrDept2 = deptType.convert(hrDept, Multiplicity.REQUIRED); Assert.assertEquals( hrDept2.toString(), "{\n" + "\tid : (type: Department, id: <unassigned>)\n" + "\tname : \thr\n" + "\temployees : \t[{\n" + "\tid : (type: Person, id: <unassigned>)\n" + "\tname : \tJohn\n" + "\tdepartment : (type: Department, id: <unassigned>)\n" + "\tmanager : (type: Manager, id: <unassigned>)\n" + "}, {\n" + "\tid : (type: Manager, id: <unassigned>)\n" + "\tsubordinates : \t[{\n" + "\tid : (type: Person, id: <unassigned>)\n" + "\tname : \tJohn\n" + "\tdepartment : (type: Department, id: <unassigned>)\n" + "\tmanager : (type: Manager, id: <unassigned>)\n" + "}]\n" + "\tname : \tJane\n" + "\tdepartment : (type: Department, id: <unassigned>)\n" + "\tmanager : <null>\n" + "\n" + "\tSecurityClearance : \t{\n" + "\t\tlevel : \t\t1\n" + "\t}}]\n" + "}"); }
/** 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; }
protected void checkAssignment(Stmt.Assignment def) { checkExpression(def.lhs()); checkExpression(def.rhs()); Type lhs_t = def.lhs().attribute(Type.class); Type rhs_t = def.rhs().attribute(Type.class); try { if (!types.subtype(lhs_t, rhs_t, loader)) { ErrorHandler.handleTypeMismatch( new TypeMismatchException(def.rhs(), lhs_t, loader, types), def.rhs().attribute(SourceLocation.class)); } } catch (ClassNotFoundException ex) { syntax_error(ex.getMessage(), def); } }
// TODO - AHK - Duplicate of method in AbstractElementTransformer @Override public IType getOwningIType() { IType owningType; if (_constructor instanceof IJavaConstructorInfo) { // We have to get the owner type from the method because it may be // different from the owning type e.g., entity aspects see ContactGosuAspect.AllAdresses IJavaClassConstructor m = ((IJavaConstructorInfo) _constructor).getJavaConstructor(); if (m != null) { owningType = TypeSystem.get(m.getEnclosingClass()); } else { owningType = _constructor.getOwnersType(); } } else { owningType = _constructor.getOwnersType(); } return owningType; }
protected void checkTryCatchBlock(Stmt.TryCatchBlock block) { checkBlock(block); checkBlock(block.finaly()); for (final Stmt.CatchBlock cb : block.handlers()) { checkBlock(cb); try { if (!types.subtype(JAVA_LANG_THROWABLE, cb.type().attribute(Type.class), loader)) { // This is a unique case - no substitution can be found by reflection // (as the error is within the actual source code text), so we make a generic // suggestion syntax_error("Syntax Error: Exception type must extend java.lang.Throwable", cb.type()); } } catch (ClassNotFoundException ex) { syntax_error(ex.getMessage(), block); } } }
protected void checkField(JavaField d) { checkExpression(d.initialiser()); Type lhs_t = d.type().attribute(Type.class); if (d.initialiser() != null) { Type rhs_t = d.initialiser().attribute(Type.class); try { if (!types.subtype(lhs_t, rhs_t, loader)) { ErrorHandler.handleTypeMismatch( new TypeMismatchException(d.initialiser(), lhs_t, loader, types), d.initialiser().attribute(SourceLocation.class)); } } catch (ClassNotFoundException ex) { syntax_error(ex.getMessage(), d); } } }
protected void checkForEach(Stmt.ForEach stmt) { checkExpression(stmt.source()); checkStatement(stmt.body()); // need to check that the static type of the source expression // implements java.lang.iterable Type s_t = stmt.source().attribute(Type.class); try { if (!(s_t instanceof Type.Array) && !types.subtype(new Type.Clazz("java.lang", "Iterable"), s_t, loader)) { ErrorHandler.handleTypeMismatch( new TypeMismatchException( stmt.source(), new Type.Wildcard(new Type.Clazz("java.lang", "Iterable"), null), loader, types), stmt.source().attribute(SourceLocation.class)); } } catch (ClassNotFoundException ex) { syntax_error(ex.getMessage(), stmt); } }
public IType getExceptionType() { return TypeSystem.getByFullNameIfValid(getName()); }
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 for testing. */ T getTypeWithProperty(String field, T type) { return typeSystem.getTypeWithProperty(field, type); }