public PType possibleConstraint(PType constraint, PType actual, ILexLocation location) { if (constraint != null) { if (!af.getTypeComparator().compatible(constraint, actual)) { TypeChecker.report(3327, "Value is not of the right type", location); TypeChecker.detail2("Actual", actual, "Expected", constraint); } } return actual; }
/** * Test whether one type is a subtype of another. * * @param sub * @param sup * @return True if sub is a subtype of sup. */ public synchronized boolean isSubType(PType sub, PType sup) { return isSubType(sub, sup, false); }
@Override public boolean equals(Object other) { if (super.equals(other)) { LexNameToken lother = (LexNameToken)other; if (typeQualifier != null && lother.typeQualifier != null) { TypeComparator comp = new TypeComparator(assistantFactory); return comp.compatible(typeQualifier, lother.typeQualifier); } else { return true; } } else { return false; } }
@Override public PType caseAAssignmentDefinition(AAssignmentDefinition node, TypeCheckInfo question) throws AnalysisException { question.qualifiers = null; node.setType(question.assistantFactory.createPTypeAssistant().typeResolve(question.assistantFactory.createPDefinitionAssistant().getType(node), null, THIS, question)); ExcludedDefinitions.setExcluded(node); node.setExpType(node.getExpression().apply(THIS, question)); ExcludedDefinitions.clearExcluded(); question.assistantFactory.getTypeComparator().checkComposeTypes(node.getType(), question.env, false); if (node.getExpType() instanceof AVoidType) { TypeCheckerErrors.report(3048, "Expression does not return a value", node.getExpression().getLocation(), node.getExpression()); } if (!question.assistantFactory.getTypeComparator().compatible(node.getType(), node.getExpType())) { TypeCheckerErrors.report(3000, "Expression does not match declared type", node.getExpression().getLocation(), node); TypeCheckerErrors.detail2("Declared", node.getType(), "Expression", node.getExpType()); } return node.getType(); }
if (assistantFactory.getTypeComparator().compatible(u, patternType)) if (!assistantFactory.getTypeComparator().isSubType(type, compatible)) if (!assistantFactory.getTypeComparator().isSubType(question.checkType(exp, node.getExpType()), type))
if (!tc.compatible(to, type)) List<PType> domain = tc.narrowest(type.getParameters(), restrictedType.getParameters()); PType range = tc.narrowest(type.getResult(), restrictedType.getResult()); AFunctionType newType = AstFactory.newAFunctionType(location, true, domain, range);
@Override public PType caseAMultiBindListDefinition(AMultiBindListDefinition node, TypeCheckInfo question) throws AnalysisException { if (node.getType() != null) { question.assistantFactory.getTypeComparator().checkComposeTypes(node.getType(), question.env, false); } List<PDefinition> defs = new Vector<PDefinition>(); for (PMultipleBind mb : node.getBindings()) { PType type = mb.apply(THIS, question); defs.addAll(question.assistantFactory.createPMultipleBindAssistant().getDefinitions(mb, type, question)); } question.assistantFactory.createPDefinitionListAssistant().typeCheck(defs, THIS, question); node.setDefs(defs); return null; }
if (searchSubType(suba, sup, invignore) == Result.No) if (searchSubType(sub, supt, invignore) == Result.Yes) return searchSubType(subn.getType(), sup, invignore); } else if (sup instanceof AOptionalType) return searchSubType(sub, op.getType(), invignore); } else if (sub instanceof SNumericBasicType) return allSubTypes(subl, supl, invignore); } else if (sub instanceof SMapType) if (searchSubType(subm.getFrom(), supm.getFrom(), invignore) == Result.Yes && searchSubType(subm.getTo(), supm.getTo(), invignore) == Result.Yes) if (searchSubType(subs.getSetof(), sups.getSetof(), invignore) == Result.Yes) if (searchSubType(subs.getSeqof(), sups.getSeqof(), invignore) == Result.Yes) return allSubTypes(subf.getParameters(), supf.getParameters(), invignore) == Result.Yes && searchSubType(subf.getResult(), supf.getResult(), invignore) == Result.Yes ? Result.Yes : Result.No; } else if (sub instanceof AOperationType) return allSubTypes(subo.getParameters(), supo.getParameters(), invignore) == Result.Yes && searchSubType(subo.getResult(), supo.getResult(), invignore) == Result.Yes ? Result.Yes : Result.No; } else if (sub instanceof ARecordInvariantType)
if (searchCompatible(ta, from, paramOnly) == Result.Yes) if (searchCompatible(to, tb, paramOnly) == Result.Yes) return allCompatible(ta, tb, paramOnly); } else if (to instanceof SMapType) || searchCompatible(ma.getFrom(), mb.getFrom(), paramOnly) == Result.Yes && searchCompatible(ma.getTo(), mb.getTo(), paramOnly) == Result.Yes ? Result.Yes : Result.No; } else if (to instanceof SSetType) // Includes set1 || searchCompatible(sa.getSetof(), sb.getSetof(), paramOnly) == Result.Yes ? Result.Yes : Result.No; } else if (to instanceof SSeqType) // Includes seq1 || searchCompatible(sa.getSeqof(), sb.getSeqof(), paramOnly) == Result.Yes ? Result.Yes : Result.No; } else if (to instanceof AFunctionType) return allCompatible(fa.getParameters(), fb.getParameters(), paramOnly) == Result.Yes && (paramOnly || searchCompatible(fa.getResult(), fb.getResult(), paramOnly) == Result.Yes) ? Result.Yes : Result.No; } else if (to instanceof AOperationType) return allCompatible(fa.getParameters(), fb.getParameters(), paramOnly) == Result.Yes && (paramOnly || searchCompatible(fa.getResult(), fb.getResult(), paramOnly) == Result.Yes) ? Result.Yes : Result.No; } else if (to instanceof ARecordInvariantType)
/** * Return the narrowest of two types/type lists. */ public synchronized List<PType> narrowest(List<PType> t1, List<PType> t2) { return allSubTypes(t1, t2, false) == Result.Yes ? t1 : t2; }
@Override public TypeComparator getTypeComparator() { if (typeComp == null) { typeComp = new TypeComparator(this); } return typeComp; }
/** * Compare two type lists for placewise compatibility. , assistantFactory @param to * * @param to * @param from * @return True if all types compatible. */ public synchronized boolean compatible(List<PType> to, List<PType> from) { done.clear(); return allCompatible(to, from, false) == Result.Yes; }
&& !assistant.isUnknown(rset)) PType interTypes = question.assistantFactory.getTypeComparator().intersect(lset, rset);
@Override public PType caseANarrowExp(ANarrowExp node, TypeCheckInfo question) throws AnalysisException { node.getTest().setType(node.getTest().apply(THIS, question.newConstraint(null))); PType result = null; if (node.getBasicType() != null) { node.setBasicType(question.assistantFactory.createPTypeAssistant().typeResolve(node.getBasicType(), null, THIS, question)); result = node.getBasicType(); question.assistantFactory.getTypeComparator().checkComposeTypes(result, question.env, false); } else { node.setTypedef(question.env.findType(node.getTypeName(), node.getLocation().getModule())); if (node.getTypedef() == null) { TypeCheckerErrors.report(3113, "Unknown type name '" + node.getTypeName() + "'", node.getLocation(), node); result = AstFactory.newAUnknownType(node.getLocation()); } else { result = question.assistantFactory.createPDefinitionAssistant().getType(node.getTypedef()); } } if (!question.assistantFactory.getTypeComparator().compatible(result, node.getTest().getType())) { TypeCheckerErrors.report(3317, "Expression can never match narrow type", node.getLocation(), node); } return question.assistantFactory.createPTypeAssistant().possibleConstraint(question.constraint, result, node.getLocation()); }
if (assistantFactory.getTypeComparator().compatible(u, patternType)) if (!assistantFactory.getTypeComparator().isSubType(question.checkType(node.getTest(), node.getExpType()), compatible)) if (!assistantFactory.getTypeComparator().isSubType(question.checkType(node.getTest(), node.getExpType()), node.getDefType()))
for (PType compose : question.assistantFactory.getTypeComparator().checkComposeTypes(ntype.getType(), question.env, true)) question.assistantFactory.getTypeComparator().checkComposeTypes(field.getType(), question.env, false);
public boolean matches(PPattern pattern, PType expType) { return af.getTypeComparator().compatible(af.createPPatternAssistant().getPossibleType(pattern), expType); }
question.assistantFactory.getTypeComparator().checkComposeTypes(node.getType(), question.env, false); } else if (type != null && !(type instanceof AUnknownType)) if (!question.assistantFactory.getTypeComparator().compatible(type, expType))
public synchronized PType narrowest(PType t1, PType t2) { return isSubType(t1, t2) ? t1 : t2; } }
if (assistantFactory.getTypeComparator().isSubType(atype, etype)) if (assistantFactory.getTypeComparator().isSubType(exp.getType(), etype)) || assistantFactory.getTypeComparator().compatible(pos, atype))