Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/org/rascalmpl/compiler/lang/rascalcore/check/AType.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ data AType (str alabel = "")

@memo={expireAfter(minutes=5),maximumSize(1000)}
AType overloadedAType(rel[loc, IdRole, AType] overloads){
if(all(<loc _, IdRole _, AType t> <- overloads, aadt(_, _, _) := t)){
if(all(aadt(_, _, _) <- overloads<2>)){
str adtName = "";
list[AType] adtParams = [];
synRoles = {};
nformals = -1;
for(<loc _, IdRole _, AType t> <- 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
33 changes: 29 additions & 4 deletions src/org/rascalmpl/compiler/lang/rascalcore/check/ATypeUtils.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ str prettyAType(areified(AType t)) = "type[<prettyAType(t)>]";

// 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]);

Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -658,7 +658,7 @@ bool hasKeywordParameters(acons(AType adt, list[AType] fields, list[Keyword] kwF
= !isEmpty(kwFields);

bool hasKeywordParameters(overloadedAType(overloads))
= any(<loc _, IdRole _, AType t> <- overloads, hasKeywordParameters(t));
= any(t <- overloads<2>, hasKeywordParameters(t));

default bool hasKeywordParameters(AType t) = false;

Expand Down Expand Up @@ -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;
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)
= {<def, idRole, atype<1>> | <def, idRole, ovlType> <- 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 += <tp, f(tp)>;
} 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;
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void collect(current: (Expression) `<Expression e> is <Name n>`, 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 */;
Expand All @@ -67,7 +67,7 @@ void collect(current: (Expression) `<Expression e> has <Name n>`, 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 */;
Expand Down Expand Up @@ -650,7 +650,7 @@ void collect(current: (Expression) `<Pattern pat> \<- <Expression 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));
Expand All @@ -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(<key, role, tp> <- overloads){
try {
filtered_overloads += <key, role, computeEnumeratorElementType(current, tp, s)>;
} 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));
}

Expand All @@ -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*/;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(<key, idr, tp> <- overloads){
sub_overloadTypes = {};
for(tp <- overloads<2>){
try {
sub_overloads += <key, idr, computeSubscriptAssignableType(current, tp, subscript, operator, rhs, s)>;
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?
Expand Down Expand Up @@ -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(<key, idr, tp> <- overloads){
try {
anno_overloads += <key, idr, computeAnnoAssignableType(current, tp, annoName, operator, rhs, scope, s)>;
} 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);
}
Expand All @@ -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(<key, annoId(), tp1> <- overloads, aanno(_, onType, tp2) := tp1, asubtype(receiverType, onType)){
anno_overloads += <key, annoId(), tp1>;
for(<key, annoId(), tp1:aanno(_, onType, _)> <- 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({<def, annoId(), atype> | <def, annoId(), atype> <- overloads, atype in anno_overloads});
} else
if(aanno(_, onType, annoType) := annoNameType){
return annoNameType;
Expand Down
Loading
Loading