diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/AType.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/AType.rsc index 2ec6fd344fa..0222ae52280 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/AType.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/AType.rsc @@ -65,8 +65,8 @@ default bool asubtype(AType l, AType r){ case tvar(_): throw TypeUnavailable(); case overloadedAType(overloads): - //TODO: Alternative: return asubtype(l, (\avoid() | alub(it, tp) | <_, _, tp> <- overloads)); - return isEmpty(overloads) ? asubtype(l, avoid()) : any(<_, _, tp> <- overloads, asubtype(l, tp)); + //TODO: Alternative: return asubtype(l, (\avoid() | alub(it, tp) | tp <- overloads<2>)); + return isEmpty(overloads) ? asubtype(l, avoid()) : any(tp <- overloads<2>, asubtype(l, tp)); case avalue(): return true; case anum(): @@ -176,8 +176,8 @@ bool asubtypeLeftTypeParam(p1:aparameter(str n1, AType b1, closed=true), AType r } bool asubtype(overloadedAType(overloads), AType r) - //TODO: Alternative: = asubtype((\avoid() | alub(it, tp) | <_, _, tp> <- overloads), r); - = isEmpty(overloads) ? asubtype(avoid(), r) : any(<_, _, tp> <- overloads, asubtype(tp, r)); + //TODO: Alternative: = asubtype((\avoid() | alub(it, tp) | tp <- overloads<2>), r); + = isEmpty(overloads) ? asubtype(avoid(), r) : any(tp <- overloads<2>, asubtype(tp, r)); bool asubtype(avoid(), AType _) = true; default bool asubtype(AType _, avalue()) = true; @@ -572,12 +572,12 @@ default AType alub(AType s, AType t) AType alub(atypeList(ts1), atypeList(ts2)) = atypeList(alubList(ts1, ts2)); AType alub(overloadedAType(overloads), AType t2) - // TODO:Alternative: alub((\avoid() | alub(it, tp) | <_, _, tp> <- overloads), t2); - = isEmpty(overloads) ? t2 : alub((avalue() | aglb(it, tp) | <_, _, tp> <- overloads), t2); + // TODO:Alternative: alub((\avoid() | alub(it, tp) | tp <- overloads<2>), t2); + = isEmpty(overloads) ? t2 : alub((avalue() | aglb(it, tp) | tp <- overloads<2>), t2); AType alub(AType t1, overloadedAType(overloads)) - //TODO: Alternative: = alub(t1, (\avoid() | alub(it, tp) | <_, _, tp> <- overloads)); - = isEmpty(overloads) ? t1 : alub(t1, (avalue() | aglb(it, tp) | <_, _, tp> <- overloads)); + //TODO: Alternative: = alub(t1, (\avoid() | alub(it, tp) | tp <- overloads<2>)); + = isEmpty(overloads) ? t1 : alub(t1, (avalue() | aglb(it, tp) | tp <- overloads<2>)); AType alub(avalue(), AType t) = avalue(); AType alub(AType s, avalue()) = avalue(); @@ -783,12 +783,12 @@ public AType aglb(arel(AType s), aset(AType t)) = aset(aglb(atuple(s), t)); AType aglb(arel(atypeList(list[AType] l)), arel(atypeList(list[AType] r))) = size(l) == size(r) ? arel(atypeList(aglbList(l, r))) : aset(avalue()); AType aglb(overloadedAType(overloads), AType t2) - //TODO: Alternative: = aglb((\avoid() | alub(it, tp) | <_, _, tp> <- overloads), t2); - = isEmpty(overloads) ? t2 : aglb((avoid() | alub(it, tp) | <_, _, tp> <- overloads), t2); + //TODO: Alternative: = aglb((\avoid() | alub(it, tp) | tp <- overloads<2>), t2); + = isEmpty(overloads) ? t2 : aglb((avoid() | alub(it, tp) | tp <- overloads<2>), t2); AType aglb(AType t1, overloadedAType(overloads)) - //TODO: Alternative: = aglb(t1, (\avoid() | alub(it, tp) | <_, _, tp> <- overloads)); - = isEmpty(overloads) ? t1 : aglb(t1, (avoid() | alub(it, tp) | <_, _, tp> <- overloads)); + //TODO: Alternative: = aglb(t1, (\avoid() | alub(it, tp) | tp <- overloads<2>)); + = isEmpty(overloads) ? t1 : aglb(t1, (avoid() | alub(it, tp) | tp <- overloads<2>)); public AType aglb(alist(AType s), alist(AType t)) = alist(aglb(s, t)); public AType aglb(alist(AType s), alrel(AType t)) = alist(aglb(s,atuple(t))); diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeBase.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeBase.rsc index bda7c86441b..b33340fa405 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeBase.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeBase.rsc @@ -92,12 +92,12 @@ data AType (str alabel = "") @memo={expireAfter(minutes=5),maximumSize(1000)} AType overloadedAType(rel[loc, IdRole, AType] overloads){ - if(all( <- overloads, aadt(_, _, _) := t)){ + if(all(aadt(_, _, _) <- overloads<2>)){ str adtName = ""; list[AType] adtParams = []; synRoles = {}; nformals = -1; - for( <- overloads, aadt(adtName1, params1, syntaxRole1) := t){ + for(aadt(adtName1, params1, syntaxRole1) <- overloads<2>){ if(!isEmpty(adtName) && adtName != adtName1) fail overloadedAType; // overloading of different ADTs. if(nformals >= 0 && size(params1) != nformals) fail overloadedAType; else nformals = size(params1); // different type parameter arities diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeParamBase.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeParamBase.rsc index fbc04550e51..4886940bb29 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeParamBase.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeParamBase.rsc @@ -96,7 +96,7 @@ set[AType] collectAndUnlabelRascalTypeParams(set[AProduction] prods) { } @doc{See if a type contains any type parameters.} -bool typeContainsRascalTypeParams(AType t) = size(collectRascalTypeParams(t)) > 0; +bool typeContainsRascalTypeParams(AType t) = /aparameter(_,_) := t; @doc{Return the names of all type variables in the given type.} set[str] typeParamNames(AType t) { diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeUtils.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeUtils.rsc index 697640d2347..1c33f0f0017 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeUtils.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeUtils.rsc @@ -102,7 +102,7 @@ str prettyAType(areified(AType t)) = "type[]"; // utilities str prettyAType(overloadedAType(rel[loc, IdRole, AType] overloads)) - = intercalateOr([prettyAType(t1) | t1 <- {t | <_, _, t> <- overloads} ]); + = intercalateOr([prettyAType(t1) | t1 <- overloads<2> ]); str prettyAType(list[AType] atypes) = intercalate(", ", [prettyAType(t) | t <- atypes]); @@ -211,7 +211,7 @@ Symbol atype2symbol1(areified(AType t)) = Symbol::reified(atype2symbol(t)); // overloaded types (union types) do not exist at the Rascal runtime level, // so we lub the alternatives here which is what happens at run-time as well. Symbol atype2symbol1(overloadedAType(rel[loc, IdRole, AType] overloads)) - = atype2symbol((\avoid() | alub(it, alt) | <_, _, alt> <- overloads)); + = atype2symbol((\avoid() | alub(it, alt) | alt <- overloads<2>)); list[Symbol] atypes2symbols(list[AType] atypes) = [atype2symbol(t) | t <- atypes]; @@ -658,7 +658,7 @@ bool hasKeywordParameters(acons(AType adt, list[AType] fields, list[Keyword] kwF = !isEmpty(kwFields); bool hasKeywordParameters(overloadedAType(overloads)) - = any( <- overloads, hasKeywordParameters(t)); + = any(t <- overloads<2>, hasKeywordParameters(t)); default bool hasKeywordParameters(AType t) = false; @@ -1503,4 +1503,29 @@ public AType filterOverloads(overloadedAType(rel[loc, IdRole, AType] overloads), if({<_,_,t>} := reduced) return t; return overloadedAType(reduced); } -public default AType filterOverloads(AType t, set[IdRole] roles) = t; \ No newline at end of file +public default AType filterOverloads(AType t, set[IdRole] roles) = t; + +@synopsis{Filter overloads by ((AType)).} +rel[loc, IdRole, AType] filterOverloads(rel[loc, IdRole, AType] overloads, set[AType] atypes) + = {t | t:<_, _, atype> <- overloads, atype in atypes}; + +@synopsis{Filter overloads by ((AType)) and map their type.} +rel[loc, IdRole, AType] filterAndRetypeOverloads(rel[loc, IdRole, AType] overloads, rel[AType, AType] atypes) + = {> | <- overloads, atype <- atypes, atype<0> == ovlType}; + + +AType computeOverloads(overloadedAType(rel[loc def, IdRole idRole, AType atype] overloads), AType(AType) f) { + rel[AType, AType] computedTypes = {}; + for (tp <- overloads.atype) { + try { + computedTypes += ; + } catch checkFailed(list[FailMessage] _): /* continue with next overload */; + catch NoBinding(): /* continue with next overload */; + catch _: /* continue with next overload */; + } + + if (isEmpty(filteredTypes)) return overloadedAType({}); + return overloadedAType(filterAndRetypeOverloads(overloads, computedTypes)); +} + +default AType computeOverloads(AType at, AType(AType) _) = at; diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectExpression.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectExpression.rsc index fa5b8e11aa1..e897dabaafc 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectExpression.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectExpression.rsc @@ -724,10 +724,10 @@ void reportMissingNonTerminalCases(Expression current, rel[loc def, IdRole idRol for(int i <- index(actuals)){ actual_type = s.getType(actuals[i]); if(isNonTerminalAType(actual_type)){ - if(!isEmpty(validOverloads) && all(ovl <- validOverloads, - arg_types := getFunctionOrConstructorArgumentTypes(ovl.atype), + if(!isEmpty(validOverloads) && all(atype <- validOverloads<2>, + arg_types := getFunctionOrConstructorArgumentTypes(atype), size(arg_types) == size(actuals), isADTAType(arg_types[i]), getADTName(arg_types[i]) == "Tree")){ - nont = [ovl.atype | ovl <- overloads, arg_types := getFunctionOrConstructorArgumentTypes(ovl.atype), size(arg_types) == size(actuals), isNonTerminalAType(arg_types[i]) ]; + nont = [atype | atype <- overloads<2>, arg_types := getFunctionOrConstructorArgumentTypes(atype), size(arg_types) == size(actuals), isNonTerminalAType(arg_types[i]) ]; if(!isEmpty(nont)){ s.report(info(current, "Actual #%v has nonterminal type %t, but only comparable overloads with `Tree` argument exist, maybe missing import/extend?", i, actual_type)); } diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc index 3672c7899d7..c81a27f52d6 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc @@ -48,7 +48,7 @@ void collect(current: (Expression) ` is `, Collector c){ private AType do_computeIsType(Tree current, AType t1, Solver s){ // TODO: check that name exists if(overloadedAType(rel[loc, IdRole, AType] overloads) := t1){ - for(<_, _, tp> <- overloads){ + for(tp <- overloads<2>){ try return do_computeIsType(current, tp, s); catch checkFailed(_): /* ignore, try next */; catch NoBinding(): /* ignore, try next */; @@ -67,7 +67,7 @@ void collect(current: (Expression) ` has `, Collector c){ private AType _computeHasType(Tree current, AType t1, Solver s){ if(overloadedAType(rel[loc, IdRole, AType] overloads) := t1){ - for(<_, _, tp> <- overloads){ + for(tp <- overloads<2>){ try return _computeHasType(current, tp, s); catch checkFailed(_): /* ignore, try next */; catch NoBinding(): /* ignore, try next */; @@ -650,7 +650,7 @@ void collect(current: (Expression) ` \<- `, s.fact(pat, patType); } if(overloadedAType(rel[loc, IdRole, AType] overloads) := elmType){ - for(<_, _, tp> <- overloads){ + for(tp <- overloads<2>){ if(comparable(patType, tp)) return abool(); } s.report(error(pat, "Pattern of type %t cannot be used to enumerate over %t", patType, exprType)); @@ -671,18 +671,11 @@ AType computeEnumeratorElementType(Expression current, AType etype, Solver s) { etype = s.instantiate(etype); - if(overloadedAType(rel[loc, IdRole, AType] overloads) := etype){ - // A void alternative would otherwise be silently dropped by the loop below - checkNonVoid(current, etype, s, "Enumerated expression"); - filtered_overloads = {}; - for( <- overloads){ - try { - filtered_overloads += ; - } catch checkFailed(_): /* ignore, try next */; - catch NoBinding(): /* ignore, try next */; - //>> catch e: /* ignore, try next */; - } - if(!isEmpty(filtered_overloads)) return overloadedAType(filtered_overloads); + // A void alternative would otherwise be silently dropped by the loop below + checkNonVoid(current, etype, s, "Enumerated expression"); + + if(ovl:overloadedAType(filtered_overloads:{_, *_}) := computeOverloads(etype, AType(AType tp) { return computeEnumeratorElementType(current, tp, s); })) { + if(!isEmpty(filtered_overloads)) return ovl; s.report(error(current, "Type %t is not enumerable", etype)); } @@ -699,7 +692,7 @@ AType computeEnumeratorElementType(Expression current, AType etype, Solver s) { } else if (isOptType(etype)) { return getOptType(etype); } else if(overloadedAType(rel[loc, IdRole, AType] overloads) := etype){ - for(<_, _, tp> <- overloads, isEnumeratorType(tp)){ + for(tp <- overloads<2>, isEnumeratorType(tp)){ try { return computeEnumeratorElementType(current, tp, s); } catch checkFailed(list[FailMessage] _): /* do nothing and try next overload*/; diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectStatement.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectStatement.rsc index 95255f68322..201d11d7e03 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectStatement.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectStatement.rsc @@ -855,16 +855,16 @@ private AType computeSubscriptAssignableType(Statement current, AType receiverTy checkNonVoid(current, rhs, s, "Righthand side of subscript assignment"); if(overloadedAType(rel[loc, IdRole, AType] overloads) := receiverType){ - sub_overloads = {}; - for( <- overloads){ + sub_overloadTypes = {}; + for(tp <- overloads<2>){ try { - sub_overloads += ; + sub_overloadTypes += computeSubscriptAssignableType(current, tp, subscript, operator, rhs, s); } catch checkFailed(list[FailMessage] _): /* do nothing and try next overload */; catch NoBinding(): /* do nothing and try next overload */; //>> catch e: /* do nothing and try next overload */; } - if(isEmpty(sub_overloads)) s.report(error(current, "Subscript %q of %t cannot be resolved", subscript, receiverType)); - return overloadedAType(sub_overloads); + if(isEmpty(sub_overloadTypes)) s.report(error(current, "Subscript %q of %t cannot be resolved", subscript, receiverType)); + return overloadedAType(filterOverloads(overloads, sub_overloadTypes)); } subscriptType = s.getType(subscript); // TODO: overloaded? @@ -1152,15 +1152,7 @@ private AType computeAnnoAssignableType(Statement current, AType receiverType, N if(!s.isFullyInstantiated(receiverType)) throw TypeUnavailable(); if(!s.isFullyInstantiated(rhs)) throw TypeUnavailable(); - if(overloadedAType(rel[loc, IdRole, AType] overloads) := receiverType){ - anno_overloads = {}; - for( <- overloads){ - try { - anno_overloads += ; - } catch checkFailed(list[FailMessage] fms): /* do nothing and try next overload */; - catch NoBinding(): /* do nothing and try next overload */; - //>> catch e: /* do nothing and try next overload */; - } + if(overloadedAType(anno_overloads:{_, *_}) := computeOverloads(receiverType, AType(AType tp) { return computeAnnoAssignableType(current, tp, annoName, operator, rhs, scope, s); })) { if(isEmpty(anno_overloads)) s.report(error(current, "Annotation on %t cannot be resolved", receiverType)); return overloadedAType(anno_overloads); } @@ -1170,11 +1162,11 @@ private AType computeAnnoAssignableType(Statement current, AType receiverType, N if (isNodeAType(receiverType) || isADTAType(receiverType) || isNonTerminalAType(receiverType)) { if(overloadedAType(rel[loc, IdRole, AType] overloads) := annoNameType){ anno_overloads = {}; - for( <- overloads, aanno(_, onType, tp2) := tp1, asubtype(receiverType, onType)){ - anno_overloads += ; + for( <- overloads, asubtype(receiverType, onType)){ + anno_overloads += tp1; } if(isEmpty(anno_overloads)) s.report(error(current, "Annotation on %t cannot be resolved from %t", receiverType, annoNameType)); - return overloadedAType(anno_overloads); + return overloadedAType({ | <- overloads, atype in anno_overloads}); } else if(aanno(_, onType, annoType) := annoNameType){ return annoNameType; diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/ComputeType.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/ComputeType.rsc index f6361a449c0..62726423cc7 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/ComputeType.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/ComputeType.rsc @@ -84,31 +84,32 @@ private default bool hasVoidAlternative(AType t) = isVoidAType(t); AType(Solver) makeGetSyntaxType(Type varType) = AType(Solver s) { Tree t = varType; return getSyntaxType(t, s); }; -rel[loc key, IdRole idRole, AType atype] - checkAndFilterOverloads(Tree expr, rel[loc key, IdRole idRole, AType atype] overloads, AType expected, Solver s){ - - rel[loc key, IdRole idRole, AType atype] filteredOverloads = {}; - rel[loc key, IdRole idRole, AType atype] unResolvedConstructorOverloads = {}; - for(tup:<_, _, tp> <- overloads){ +rel[loc key, IdRole idRole, AType atype] checkAndFilterOverloads(Tree expr, rel[loc, IdRole, AType] overloads, AType expected, Solver s) + = filterOverloads(overloads, checkAndFilterOverloadTypes(expr, overloads, expected, s)); + +set[AType] checkAndFilterOverloadTypes(Tree expr, rel[loc key, IdRole idRole, AType atype] overloads, AType expected, Solver s) { + set[AType] filteredTypes = {}; + set[AType] unResolvedConstructorOverloads = {}; + for(tp <- overloads<2>){ if(isValueAType(expected)){ - filteredOverloads += tup; + filteredTypes += tp; if(isConstructorAType(tp) || isADTAType(tp)){ - unResolvedConstructorOverloads += tup; + unResolvedConstructorOverloads += tp; } } else if(asubtype(tp, expected)){ - filteredOverloads += tup; + filteredTypes += tp; } else if(isConstructorAType(tp) || isADTAType(tp)){ - unResolvedConstructorOverloads += tup; + unResolvedConstructorOverloads += tp; } } if(size(unResolvedConstructorOverloads) > 1){ - adtNames = { getADTName(tp) | <_, _, tp> <- unResolvedConstructorOverloads }; + adtNames = { getADTName(tp) | tp <- unResolvedConstructorOverloads }; qualifyHint = size(adtNames) > 1 ? "you may use as qualifier" : ""; argHint = "make argument type(s) more precise"; msg = error(expr, "Expression `` is overloaded, to resolve it "); s.report(msg); } - return filteredOverloads; + return filteredTypes; } void(Solver) makeVarInitRequirement(Variable var) @@ -128,8 +129,8 @@ void(Solver) makeVarInitRequirement(Variable var) initialTypeU = instantiateRascalTypeParameters(var, initialTypeU, bindings, s); if(s.isFullyInstantiated(initialTypeU)){ if(overloadedAType(overloads) := initialTypeU){ - filteredOverloads = checkAndFilterOverloads(var.initial, overloads, varTypeU, s); - for(<_, _, tp> <- filteredOverloads){ + types = checkAndFilterOverloadTypes(var.initial, overloads, varTypeU, s); + for(tp <- types){ s.requireSubType(tp, varTypeU, error(var, "Initialization of %q should be subtype of %t, found overloaded type %t", "", var.name, deUnique(initialTypeU))); } } else { @@ -149,19 +150,12 @@ AType unaryOp(str op, AType(Tree, AType, Solver) computeType, Tree current, ATyp requireFullyInstantiated(s, t1); - if(overloadedAType(rel[loc, IdRole, AType] overloads) := t1){ - // A void alternative would otherwise be silently dropped by the loop below - if(hasVoidAlternative(t1)) s.report(error(current, "%q cannot be applied to argument of type `void`", op)); - bin_overloads = {}; - for( <- overloads){ - try { - bin_overloads += ; - } catch checkFailed(list[FailMessage] _): /* continue with next overload */; - catch NoBinding(): /* continue with next overload */; -//>> catch e: /* continue with next overload */; - } + // A void alternative would otherwise be silently dropped by the loop below + if(hasVoidAlternative(t1)) s.report(error(current, "%q cannot be applied to argument of type `void`", op)); + + if(ovl:overloadedAType(bin_overloads) := computeOverloads(t1, AType(AType tp) { return unaryOp(op, computeType, current, tp, s); })) { if(isEmpty(bin_overloads)) s.report(error(current, "%q cannot be applied to %t", op, t1)); - return overloadedAType(bin_overloads); + return ovl; } if(!maybeVoid && isVoidAType(t1)) s.report(error(current, "%q cannot be applied to argument of type `void`", op)); return computeType(current, t1, s); @@ -174,30 +168,14 @@ AType binaryOp(str op, AType(Tree, AType, AType, Solver) computeType, Tree curre // Checked before the overload loops below, since they would silently drop a void alternative if(hasVoidAlternative(t1) || hasVoidAlternative(t2)) s.report(error(current, "%q cannot be applied to argument of type `void`", op)); - if(overloadedAType(rel[loc, IdRole, AType] overloads) := t1){ - bin_overloads = {}; - for( <- overloads){ - try { - bin_overloads += ; - } catch checkFailed(list[FailMessage] _): /* continue with next overload */; - catch NoBinding(): /* continue with next overload */; -//>> catch e: /* continue with next overload */; - } + if(ovl:overloadedAType(bin_overloads) := computeOverloads(t1, AType(AType tp) { return binaryOp(op, computeType, current, tp, t2, s); })) { if(isEmpty(bin_overloads)) s.report(error(current, "%q cannot be applied to %t and %t", op, t1, t2)); - return overloadedAType(bin_overloads); + return ovl; } - if(overloadedAType(rel[loc, IdRole, AType] overloads) := t2){ - bin_overloads = {}; - for( <- overloads){ - try { - bin_overloads += < key, idr, binaryOp(op, computeType, current, t1, tp, s)>; - } catch checkFailed(list[FailMessage] _):/* continue with next overload */; - catch NoBinding(): /* continue with next overload */; -//>> catch e: /* continue with next overload */; - } + if(ovl:overloadedAType(bin_overloads) := computeOverloads(t2, AType(AType tp) { return binaryOp(op, computeType, current, t1, tp, s); })) { if(isEmpty(bin_overloads)) s.report(error(current, "%q cannot be applied to %t and %t", op, t1, t2)); - return overloadedAType(bin_overloads); + return ovl; } return computeType(current, t1, t2, s); } @@ -209,41 +187,17 @@ AType ternaryOp(str op, AType(Tree, AType, AType, AType, Solver) computeType, Tr // Checked before the overload loops below, since they would silently drop a void alternative if(hasVoidAlternative(t1) || hasVoidAlternative(t2) || hasVoidAlternative(t3)) s.report(error(current, "%q cannot be applied to argument of type `void`", op)); - if(overloadedAType(rel[loc, IdRole, AType] overloads) := t1){ - tern_overloads = {}; - for( <- overloads){ - try { - tern_overloads += ; - } catch checkFailed(list[FailMessage] _): /* continue with next overload */; - catch NoBinding(): /* continue with next overload */; - catch _: /* continue with next overload */; - } + if(ovl:overloadedAType(tern_overloads) := computeOverloads(t1, AType(AType tp) { return ternaryOp(op, computeType, current, tp, t2, t3, s); })) { if(isEmpty(tern_overloads)) s.report(error(current, "%q cannot be applied to %t, %t, and %t", op, t1, t2, t3)); - return overloadedAType(tern_overloads); + return ovl; } - if(overloadedAType(rel[loc, IdRole, AType] overloads) := t2){ - tern_overloads = {}; - for( <- overloads){ - try { - tern_overloads += < key, idr, ternaryOp(op, computeType, current, t1, tp, t3, s)>; - } catch checkFailed(list[FailMessage] _):/* continue with next overload */; - catch NoBinding(): /* continue with next overload */; - //>> catch e:/* continue with next overload */; - } + if(ovl:overloadedAType(tern_overloads) := computeOverloads(t2, AType(AType tp) { return ternaryOp(op, computeType, current, t1, tp, t3, s); })) { if(isEmpty(tern_overloads)) s.report(error(current, "%q cannot be applied to %t, %t, and %t", op, t1, t2, t3)); return overloadedAType(tern_overloads); } - if(overloadedAType(rel[loc, IdRole, AType] overloads) := t3){ - tern_overloads = {}; - for( <- overloads){ - try { - tern_overloads += < key, idr, ternaryOp(op, computeType, current, t1, t2, tp, s)>; - } catch checkFailed(list[FailMessage] _): /* continue with next overload */; - catch NoBinding(): /* continue with next overload */; - //>> catch e: /* continue with next overload */; - } + if(overloadedAType(tern_overloads) := computeOverloads(t3, AType(AType tp) { return ternaryOp(op, computeType, current, t1, t2, tp, s); })) { if(isEmpty(tern_overloads)) s.report(error(current, "%q cannot be applied to %t, %t, and %t", op, t1, t2, t3)); return overloadedAType(tern_overloads); } @@ -344,7 +298,7 @@ AType computeADTReturnType(Tree current, str adtName, loc scope, list[AType] for list[AType] parameters = []; if(overloadedAType(rel[loc, IdRole, AType] overloads) := adtType){ params = {}; - for(<_, _, tp> <- overloads){ + for(tp <- overloads<2>){ if(isADTAType(tp)) params += toSet(getADTTypeParameters(tp)); } parameters = toList(params); @@ -472,7 +426,7 @@ list[Keyword] getCommonKeywords(aadt(str adtName, list[AType] parameters, _), lo } -list[Keyword] getCommonKeywords(overloadedAType(rel[loc, IdRole, AType] overloads), loc scope, Solver s) = [ *getCommonKeywords(adt, scope, s) | <_, _, adt> <- overloads ]; +list[Keyword] getCommonKeywords(overloadedAType(rel[loc, IdRole, AType] overloads), loc scope, Solver s) = [ *getCommonKeywords(adt, scope, s) | adt <- overloads<2> ]; default list[Keyword] getCommonKeywords(AType atype, loc scope, Solver s) = []; public AType computeFieldTypeWithADT(AType containerType, Tree field, loc scope, Solver s) { @@ -587,16 +541,7 @@ AType computeSubscriptionType(Tree current, AType t1, list[AType] tl, list[Expre if(!s.isFullyInstantiated(tp)) throw TypeUnavailable(); } - if(overloadedAType(rel[loc, IdRole, AType] overloads) := t1){ - //println("computeSubscriptionType: , "); - subscript_overloads = {}; - for( <- overloads){ - try { - subscript_overloads += ; - } catch checkFailed(list[FailMessage] _):/* continue with next overload */; - catch NoBinding(): /* continue with next overload */; - //>>> catch e: /* continue with next overload */; - } + if(overloadedAType(subscript_overloads:{_, *_}) := computeOverloads(t1, AType(AType tp) { return computeSubscriptionType(current, tp, tl, indexList, s); })) { //println("computeSubscriptionType: , , ==\> "); if(isEmpty(subscript_overloads)) s.report(error(current, "Expressions of type %t cannot be subscripted", t1)); return overloadedAType(subscript_overloads); @@ -1273,19 +1218,18 @@ private bool acceptable(int i, AType a, AType b, list[bool] uninstantiated) tuple[rel[loc, IdRole, AType], list[bool]] filterOverloadedConstructors(rel[loc, IdRole, AType] overloads, list[AType] argTypes, AType subjectType, Solver s){ int arity = size(argTypes); - rel[loc, IdRole, AType] filteredOverloads = {}; + set[AType] filteredTypes = {}; list[bool] identicalFields = [true | int _ <- [0 .. arity]]; uninstantiated = [ !s.isFullyInstantiated(argTypes[i]) | int i <- [0 .. arity] ]; - for(ovl: <- overloads){ - if(acons(ret:aadt(_, list[AType] _, _), list[AType] fields, list[Keyword] _) := tp, comparable(ret, subjectType)){ - if(size(fields) == arity && (arity == 0 || all(int i <- index(fields), acceptable(i, fields[i], argTypes[i], uninstantiated)))){ - filteredOverloads += ovl; - } + for(tp:acons(ret:aadt(_, list[AType] _, _), list[AType] fields, list[Keyword] _) <- overloads<2>, comparable(ret, subjectType)) { + if(size(fields) == arity && (arity == 0 || all(int i <- index(fields), acceptable(i, fields[i], argTypes[i], uninstantiated)))){ + filteredTypes += tp; } } - return ; + + return ; } AType computePatternNodeType(Tree current, loc scope, list[Pattern] patList, (KeywordArguments[Pattern]) ``, Solver s, AType subjectType){ diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc index c5eea71a920..d8f4e7a2688 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc @@ -545,10 +545,10 @@ void checkOverloadedConstructors(Tree t, Solver s){ } void reportConstructorOverload(Expression current, overloadedAType(rel[loc def, IdRole idRole, AType atype] overloads), Solver s){ - coverloads = [ ovl | ovl <- overloads, isConstructorAType(ovl.atype) ]; - if(size(coverloads) > 1){ + coverloads = [ovl | ovl <- overloads, isConstructorAType(ovl.atype)]; + if(size(coverloads) > 1) { ovl1 = coverloads[0]; - adtNames = { adtName | <- overloads, acons(ret:aadt(adtName, list[AType] _, _), list[AType] fields, list[Keyword] kwFields) := tp }; + adtNames = { adtName | acons(ret:aadt(adtName, list[AType] _, _), list[AType] fields, list[Keyword] kwFields) <- overloads<2>}; qualifyHint = size(adtNames) > 1 ? " you may use as qualifier" : ""; argHint = "make argument type(s) more precise"; msg = error("Constructor `` is overloaded, maybe", diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/compile/Rascal2muRascal/RascalPattern.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/compile/Rascal2muRascal/RascalPattern.rsc index fc3c34e23ca..e1abcea5fb5 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/compile/Rascal2muRascal/RascalPattern.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/compile/Rascal2muRascal/RascalPattern.rsc @@ -1180,8 +1180,8 @@ MuExp translatePat(p:(Pattern) ` ( <{Pattern ","}* arguments if(expType.alabel?){ qname = expType.alabel; } else if(overloadedAType(rel[loc, IdRole, AType] overloads) := expType, - any(<_, _, tp> <- overloads, tp.alabel?)){ - for(<_, _, tp> <- overloads){ + any(tp <- overloads<2>, tp.alabel?)){ + for(tp <- overloads<2>){ if(tp.alabel?){ qname = tp.alabel; break; } diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Conversions.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Conversions.rsc index 5e90764f230..b04ec98f3fa 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Conversions.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Conversions.rsc @@ -138,7 +138,7 @@ str atype2idpart(t:acons(AType adt, list[AType] fields, list[Keyword] kwFields)) str atype2idpart(overloadedAType(rel[loc, IdRole, AType] overloads)){ resType = avoid(); formalsType = avoid(); - for(<_, _, tp> <- overloads){ + for(tp <- overloads<2>){ resType = alub(resType, getResult(tp)); formalsType = alub(formalsType, atypeList(getFormals(tp))); } @@ -264,7 +264,7 @@ str atype2IValue1(at:acons(AType adt, list[AType] fields, list[Keyword] kwFields str atype2IValue1(overloadedAType(rel[loc, IdRole, AType] overloads), map[AType, set[AType]] defs){ resType = avoid(); formalsType = avoid(); - for(<_, _, tp> <- overloads){ + for(tp <- overloads<2>){ resType = alub(resType, getResult(tp)); formalsType = alub(formalsType, atypeList(getFormals(tp))); } diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Resolvers.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Resolvers.rsc index a8f6f6918e4..8f47ea0be3c 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Resolvers.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Resolvers.rsc @@ -105,7 +105,7 @@ str varName(muVarKwp(str name, loc _, AType _)){ // duplicate, see Co //str vtypeisatype(str e, AType t, JGenie jg) = ".getType().comparable()"; str vtypeisatype(str e, str etype, overloadedAType(rel[loc, IdRole, AType] overloads), JGenie jg) - = " instanceof IConstructor && ).getConstructorType().equivalent()" | <_, _, tp> <- overloads])>"; + = " instanceof IConstructor && ).getConstructorType().equivalent()" | tp <- overloads<2>])>"; //str vtypeisatype(str e, t:acons(AType adt, list[AType] fields, list[Keyword] kwFields), JGenie jg) // = ".getConstructorType().comparable()";