} else exptypes.add(t);
public PTypeSet(PType t, IAstAssistantFactory af) { super(new PTypeComparator()); assistantFactory = af; add(t); }
public PType getPossibleType(LinkedList<PPattern> plist, ILexLocation location) { switch (plist.size()) { case 0: return AstFactory.newAUnknownType(location); case 1: return af.createPPatternAssistant().getPossibleType(plist.get(0)); default: PTypeSet list = new PTypeSet(af); for (PPattern p : plist) { list.add(af.createPPatternAssistant().getPossibleType(p)); } return list.getType(location); // NB. a union of types } }
@Override public PType caseAUnionPattern(AUnionPattern pattern) throws AnalysisException { PTypeSet set = new PTypeSet(af); set.add(af.createPPatternAssistant().getPossibleType(pattern.getLeft())); set.add(af.createPPatternAssistant().getPossibleType(pattern.getRight())); PType s = set.getType(pattern.getLocation()); return af.createPTypeAssistant().isUnknown(s) ? AstFactory.newASetSetType(pattern.getLocation(), AstFactory.newAUnknownType(pattern.getLocation())) : s; }
@Override public PType caseAConcatenationPattern(AConcatenationPattern pattern) throws AnalysisException { PTypeSet set = new PTypeSet(af); set.add(af.createPPatternAssistant().getPossibleType(pattern.getLeft())); set.add(af.createPPatternAssistant().getPossibleType(pattern.getRight())); PType s = set.getType(pattern.getLocation()); return af.createPTypeAssistant().isUnknown(s) ? AstFactory.newASeqSeqType(pattern.getLocation(), AstFactory.newAUnknownType(pattern.getLocation())) : s; }
@Override public PType caseACasesExp(ACasesExp node, TypeCheckInfo question) throws AnalysisException { TypeCheckInfo noConstraint = question.newConstraint(null); question.qualifiers = null; PType expType = node.getExpression().apply(THIS, noConstraint); PTypeSet rtypes = new PTypeSet(question.assistantFactory); for (ACaseAlternative c : node.getCases()) { rtypes.add(typeCheck(c, THIS, question, expType)); } if (node.getOthers() != null) { rtypes.add(node.getOthers().apply(THIS, question)); } node.setType(rtypes.getType(node.getLocation())); return node.getType(); }
@Override public PType caseACasesStm(ACasesStm node, TypeCheckInfo question) throws AnalysisException { PType expType = node.getExp().apply(THIS, question); PTypeSet rtypes = new PTypeSet(question.assistantFactory); boolean always = false; for (ACaseAlternativeStm c : node.getCases()) { c.setCtype(expType); rtypes.add(c.apply(THIS, question)); always = always || question.assistantFactory.createPPatternAssistant().alwaysMatches(c.getPattern(), expType); } if (node.getOthers() != null) { rtypes.add(node.getOthers().apply(THIS, question)); } else if (!always) { rtypes.add(AstFactory.newAVoidType(node.getLocation())); } node.setType(rtypes.getType(node.getLocation())); return node.getType(); }
@Override public PType caseAUnionType(AUnionType type, Newquestion question) throws AnalysisException { // return AUnionTypeAssistantTC.polymorph(type, question.pname, question.actualType); PTypeSet polytypes = new PTypeSet(af); for (PType ptype : ((AUnionType) type).getTypes()) { polytypes.add(af.createPTypeAssistant().polymorph(ptype, question.pname, question.actualType)); } // TODO: Types in unionType should be a SET PTypeList result = new PTypeList(); result.addAll(polytypes); return AstFactory.newAUnionType(type.getLocation(), result); }
@Override public PType caseAMapEnumMapExp(AMapEnumMapExp node, TypeCheckInfo question) throws AnalysisException { node.setDomTypes(new Vector<PType>()); node.setRngTypes(new Vector<PType>()); if (node.getMembers().isEmpty()) { node.setType(AstFactory.newAMapMapType(node.getLocation())); return node.getType(); } PTypeSet dom = new PTypeSet(question.assistantFactory); PTypeSet rng = new PTypeSet(question.assistantFactory); for (AMapletExp ex : node.getMembers()) { PType mt = ex.apply(THIS, question); if (!question.assistantFactory.createPTypeAssistant().isMap(mt)) { TypeCheckerErrors.report(3121, "Element is not of maplet type", node.getLocation(), node); } else { SMapType maplet = question.assistantFactory.createPTypeAssistant().getMap(mt); dom.add(maplet.getFrom()); node.getDomTypes().add(maplet.getFrom()); rng.add(maplet.getTo()); node.getRngTypes().add(maplet.getTo()); } } node.setType(AstFactory.newAMapMapType(node.getLocation(), dom.getType(node.getLocation()), rng.getType(node.getLocation()))); return question.assistantFactory.createPTypeAssistant().possibleConstraint(question.constraint, node.getType(), node.getLocation()); }
@Override public SMapType caseAUnionType(AUnionType type) throws AnalysisException { ILexLocation location = type.getLocation(); if (!type.getMapDone()) { type.setMapDone(true); // Mark early to avoid recursion. // type.setMapType(PTypeAssistantTC.getMap(AstFactory.newAUnknownType(location))); // Rewritten in an none static form. type.setMapType(af.createPTypeAssistant().getMap(AstFactory.newAUnknownType(location))); PTypeSet from = new PTypeSet(af); PTypeSet to = new PTypeSet(af); for (PType t : type.getTypes()) { if (af.createPTypeAssistant().isMap(t)) { // from.add(PTypeAssistantTC.getMap(t).getFrom()); //Original Code from.add(t.apply(THIS).getFrom()); // My change George. // to.add(PTypeAssistantTC.getMap(t).getTo());//Original code. to.add(t.apply(THIS).getTo());// My change George. } } type.setMapType(from.isEmpty() ? null : AstFactory.newAMapMapType(location, from.getType(location), to.getType(location))); } return type.getMapType(); }
@Override public PType caseASeqEnumSeqExp(ASeqEnumSeqExp node, TypeCheckInfo question) throws AnalysisException { PTypeSet ts = new PTypeSet(question.assistantFactory); node.setTypes(new LinkedList<PType>()); List<PType> types = node.getTypes(); TypeCheckInfo elemConstraint = question; if (question.constraint != null && question.assistantFactory.createPTypeAssistant().isSeq(question.constraint)) { PType stype = question.assistantFactory.createPTypeAssistant().getSeq(question.constraint).getSeqof(); elemConstraint = question.newConstraint(stype); } for (PExp ex : node.getMembers()) { question.qualifiers = null; PType mt = ex.apply(THIS, elemConstraint); ts.add(mt); types.add(mt); } node.setType(ts.isEmpty() ? AstFactory.newASeqSeqType(node.getLocation()) : AstFactory.newASeq1SeqType(node.getLocation(), ts.getType(node.getLocation()))); return question.assistantFactory.createPTypeAssistant().possibleConstraint(question.constraint, node.getType(), node.getLocation()); }
@Override public PType caseASetEnumSetExp(ASetEnumSetExp node, TypeCheckInfo question) throws AnalysisException { PTypeSet ts = new PTypeSet(question.assistantFactory); node.setTypes(new LinkedList<PType>()); List<PType> types = node.getTypes(); TypeCheckInfo elemConstraint = question; if (question.constraint != null && question.assistantFactory.createPTypeAssistant().isSet(question.constraint)) { PType setType = question.assistantFactory.createPTypeAssistant().getSet(question.constraint).getSetof(); elemConstraint = question.newConstraint(setType); } for (PExp ex : node.getMembers()) { question.qualifiers = null; PType mt = ex.apply(THIS, elemConstraint); ts.add(mt); types.add(mt); } node.setType(ts.isEmpty() ? AstFactory.newASetSetType(node.getLocation()) : AstFactory.newASet1SetType(node.getLocation(), ts.getType(node.getLocation()))); return question.assistantFactory.createPTypeAssistant().possibleConstraint(question.constraint, node.getType(), node.getLocation()); }
@Override public PType caseATrapStm(ATrapStm node, TypeCheckInfo question) throws AnalysisException { PTypeSet rtypes = new PTypeSet(question.assistantFactory); PStm body = node.getBody(); PType bt = body.apply(THIS, question); rtypes.add(bt); PTypeSet extype = exitCheck(body, question); PType ptype = null; if (extype.isEmpty()) { TypeCheckerErrors.report(3241, "Body of trap statement does not throw exceptions", node.getLocation(), node); ptype = AstFactory.newAUnknownType(body.getLocation()); } else { ptype = extype.getType(body.getLocation()); } node.setType(ptype); node.getPatternBind().apply(THIS, question); // TODO: PatternBind stuff List<PDefinition> defs = getDefinitions(node.getPatternBind()); question.assistantFactory.createPDefinitionListAssistant().typeCheck(defs, THIS, question); Environment local = new FlatCheckedEnvironment(question.assistantFactory, defs, question.env, question.scope); rtypes.add(node.getWith().apply(THIS, new TypeCheckInfo(question.assistantFactory, local, question.scope, question.qualifiers))); node.setType(rtypes.getType(node.getLocation())); return node.getType(); }
@Override public SSetType caseAUnionType(AUnionType type) throws AnalysisException { ILexLocation location = type.getLocation(); if (!type.getSetDone()) { type.setSetDone(true); // Mark early to avoid recursion. // type.setSetType(PTypeAssistantTC.getSet(AstFactory.newAUnknownType(location))); type.setSetType(af.createPTypeAssistant().getSet(AstFactory.newAUnknownType(location))); PTypeSet set = new PTypeSet(af); for (PType t : type.getTypes()) { if (af.createPTypeAssistant().isSet(t)) { // set.add(PTypeAssistantTC.getSet(t).getSetof()); set.add(t.apply(THIS).getSetof()); } } type.setSetType(set.isEmpty() ? null : AstFactory.newASetSetType(location, set.getType(location))); } return type.getSetType(); }
@Override public SSeqType caseAUnionType(AUnionType type) throws AnalysisException { // return AUnionTypeAssistantTC.getSeq(type); if (!type.getSeqDone()) { type.setSeqDone(true); // Mark early to avoid recursion. // type.setSeqType(PTypeAssistantTC.getSeq(AstFactory.newAUnknownType(type.getLocation()))); type.setSeqType(af.createPTypeAssistant().getSeq(AstFactory.newAUnknownType(type.getLocation()))); PTypeSet set = new PTypeSet(af); for (PType t : type.getTypes()) { if (af.createPTypeAssistant().isSeq(t)) { set.add(t.apply(THIS).getSeqof()); } } type.setSeqType(set.isEmpty() ? null : AstFactory.newASeqSeqType(type.getLocation(), set.getType(type.getLocation()))); } return type.getSeqType(); }