Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions rascal.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
},
{
"path": "../typepal"
},
{
"path": "../compiled-rascal"
}
],
"settings": {
Expand Down
61 changes: 9 additions & 52 deletions src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import lang::rascalcore::compile::muRascal::AST;
import lang::rascalcore::check::CheckerCommon;
import lang::rascalcore::check::BasicRascalConfig;
import lang::rascalcore::check::ModuleLocations;
import lang::rascalcore::check::LogicalLocations;

import Location;
import util::FileSystem;
Expand Down Expand Up @@ -77,62 +78,15 @@ bool rascalMayOverload(set[loc] defs, map[loc, Define] defines){
}
}
}

// oldResult = rascalMayOverloadOld(defs, defines);
// if(result != oldResult){
// println("rascalMayOverload, new: <result>, old: <oldResult>, for <roles>");
// for(def <- defs){
// println(defines[def]);
// if(result){
// scopes = {defines[d].scope | d <- defs};
// if(size(scopes) != 1){
// result = all(s <- scopes, s in defines, defines[s].idRole == moduleId());
// }
// throw "rascalMayOverload";
// }
return result;
}

// // Define the name overloading that is allowed
// bool rascalMayOverloadOld(set[loc] defs, map[loc, Define] defines){
// bool seenVAR = false;
// bool seenNT = false;
// bool seenLEX = false;
// bool seenLAY = false;
// bool seenKEY = false;
// bool seenALIAS = false;
// bool seenFUNCTION = false;

// for(def <- defs){
// // Forbid:
// // - overloading of variables/formals/pattern variables
// // - overloading of incompatible syntax definitions
// // - alias and most other names
// switch(defines[def].idRole){
// case functionId():
// { if(seenVAR || seenALIAS) return false; seenFUNCTION = true; }
// case variableId():
// { if(seenVAR || seenFUNCTION || seenALIAS) return false; seenVAR = true;}
// case moduleVariableId():
// { if(seenVAR || seenFUNCTION || seenALIAS) return false; seenVAR = true;}
// case formalId():
// { if(seenVAR || seenFUNCTION || seenALIAS) return false; seenVAR = true;}
// case keywordFormalId():
// { if(seenVAR || seenFUNCTION || seenALIAS) return false; seenVAR = true;}
// case patternVariableId():
// { if(seenVAR || seenFUNCTION || seenALIAS) return false; seenVAR = true;}
// case nonterminalId():
// { if(seenLEX || seenLAY || seenKEY || seenALIAS){ return false; } seenNT = true; }
// case lexicalId():
// { if(seenNT || seenLAY || seenKEY || seenALIAS) { return false; } seenLEX= true; }
// case layoutId():
// { if(seenNT || seenLEX || seenKEY || seenALIAS) { return false; } seenLAY = true; }
// case keywordId():
// { if(seenNT || seenLAY || seenLEX || seenALIAS) { return false; } seenKEY = true; }
// case aliasId():
// { if(seenALIAS || seenVAR || seenFUNCTION || seenNT || seenLEX || seenLAY || seenKEY) return false; seenALIAS = true; }
// }
// }
// return true;
// }


// Name resolution filters

set[IdRole] defBeforeUseRoles = {variableId(), moduleVariableId(), formalId(), keywordFormalId(), patternVariableId()};
Expand Down Expand Up @@ -457,6 +411,8 @@ void checkOverloading(map[str,Tree] namedTrees, Solver s){
facts = s.getFacts();
moduleScopes = { t.src | t <- range(namedTrees) };

bool definedInReachableModule(loc l) = isModuleId(l) && any(ms <- moduleScopes, s.isContainedIn(l, ms));

funDefs = {<define.id, define> | define <- defines, define.idRole == functionId() };
funIds = domain(funDefs);
for(id <- funIds){
Expand All @@ -465,7 +421,8 @@ void checkOverloading(map[str,Tree] namedTrees, Solver s){
for(d1 <- defs, d2 <- defs, d1.defined != d2.defined,
d1.defined in facts, AType t1 := facts[d1.defined],
d2.defined in facts, AType t2 := facts[d2.defined],
d1.scope in moduleScopes, d2.scope in moduleScopes, size(t1.formals) == size(t2.formals)
size(t1.formals) == size(t2.formals),
definedInReachableModule(d1.scope), definedInReachableModule(d2.scope)
){
if(isEmpty(t1.formals)){
msgs = [ error("Nullary function `<id>` should not be overloaded", d1.defined),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,12 @@ test bool overloadedVoidFunctionInitializerIsRejected() = checkModuleAndFilter("
void main() { int a = f(1); }
", ["Variable initialization should not have type `void`"]);

test bool nonMatchingVoidOverloadInitializerIsAccepted() = checkModuleOK("
module NonMatchingVoidOverloadInitializerIsAccepted
test bool nonMatchingVoidOverloadInitializerIsNotAccepted() = checkModuleAndFilter("
module NonMatchingVoidOverloadInitializerIsNotAccepted
int f(int x) = x;
void f(str x) {}
void main() { int a = f(1); }
");
", ["Declaration clashes with other declaration of function _ with non-`void` result type"]);

test bool voidKeywordParameterDefaultIsRejected() = checkModuleAndFilter("
module VoidKeywordParameterDefaultIsRejected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,8 @@ list[str] unexpectedDeclarationMsgs = [
"Self import not allowed",
"Extend cycle not allowed",
"Mixed import/extend cycle not allowed",
"Module name _ is incompatible with its file location"
"Module name _ is incompatible with its file location",
"Declaration clashes with other declaration of function _ with non-`void` result type"
];

bool unexpectedDeclarationInModule(str moduleText, PathConfig pathConfig = getDefaultTestingPathConfig())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ POSSIBILITY OF SUCH DAMAGE.
}
module lang::rascalcore::compile::Examples::Tst4

void main() { }
void main() { }
Original file line number Diff line number Diff line change
Expand Up @@ -1160,11 +1160,13 @@ MuExp translatePat(p:(Pattern) `<Pattern expression> ( <{Pattern ","}* arguments

MuExp arg_val = muTmpIValue(nextTmp("arg<i>_"), fuid, avalue());
AType arg_type = getType(lpats[i]);
//TODO: can we really do without this code?
// Replace all occurrences of the argument by the var representing its computed value
body = visit(body){
case muVar(str name, loc fuid, int pos, AType atype, IdRole idRole) => arg_val
when name == arg_type.alabel, arg_type := atype
};
// body = visit(body){
// case m:muVar(str name, loc fuid, int pos, AType atype, IdRole idRole) => arg_val
// when name == arg_type.alabel, arg_type := atype, bprintln("<m> =\> <arg_val>")
// };
// iprintln(body);

body = muBlock([ muVarInit(arg_val, arg),
muIfElse(muValueIsComparable(arg_val, arg_type),
Expand All @@ -1191,11 +1193,12 @@ MuExp translatePat(p:(Pattern) `<Pattern expression> ( <{Pattern ","}* arguments
}
fun_name = asUnqualifiedName(prettyPrintName(qname));
code = muBlock([subjectInit, muIfElse(muHasNameAndArity(subjectType, expType, muCon(fun_name), size(lpats), subject), body, falseCont)]);
// iprintln(code);
return code;
} else if(expression is literal){ // StringConstant
fun_name = prettyPrintName("<expression>"[1..-1]); //TODO escapes in string
code = muBlock([subjectInit, muIfElse(muHasNameAndArity(subjectType, expType, muCon(fun_name), size(lpats), subject), body, falseCont)]);
// iprintln(code);
return code;
} else {
fun_name_subject = muTmpIValue(nextTmp("fun_name_subject"), fuid, expType);
Expand All @@ -1212,6 +1215,7 @@ MuExp translatePat(p:(Pattern) `<Pattern expression> ( <{Pattern ","}* arguments
]);
}
code = muExists(getEnter(p, btscopes), code);
// iprintln(code);
return code;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,15 @@ tuple[JCode, JCode, JCode, list[value]] muRascal2Java(MuModule m, ModuleStatus m
bool hasMainFunction = false;
str mainName = "main";
AType mainType = avoid();
bool isMainType(AType atype){
assert isFunctionAType(atype);
argTypes = getFunctionOrConstructorArgumentTypes(atype);
return argTypes == [] || [alist(astr())] := argTypes;

}
MuFunction mainFunction;
for(f <- m.functions){
if(isOuterScopeName(f.scopeIn) && isMainName(f.name)) {
if(isOuterScopeName(f.scopeIn) && isMainName(f.name) && isMainType(f.ftype)) {
hasMainFunction = true;
mainFunction = f;
mainType = f.ftype;
Expand Down Expand Up @@ -307,9 +313,12 @@ tuple[JCode, JCode, JCode, list[value]] muRascal2Java(MuModule m, ModuleStatus m
str newValueRef(str name, str jtype, str code, JGenie jg)
= "new ValueRef\<<jtype>\>(\"<asJavaName(name)>\", <code>)";

str newValueRef(str vname, AType atype, v:muVar(str name, loc _, int pos, AType _, IdRole idRole), JGenie jg)
= jg.isRef(v) ? "new ValueRef\<<atype2javatype(atype)>\>(\"<vname>\", <varName(v, jg)>.getValue())" : newValueRef(vname, atype2javatype(atype), varName(v, jg), jg)
str newValueRef(str vname, AType atype, v:muVar(str name, loc _, int pos, AType _, IdRole idRole), JGenie jg){
jtype = atype2javatype(atype);
res = jg.isRef(v) ? "new ValueRef\<<jtype>\>(\"<vname>\", <varName(v, jg)>.getValue())" : newValueRef(vname, jtype, varName(v, jg), jg)
;
return res;
}

default str newValueRef(str name, AType atype, MuExp exp, JGenie jg)
= "new ValueRef\<<atype2javatype(atype)>\>(\"<asJavaName(name)>\", <transWithCast(atype,exp,jg)>)";
Expand Down Expand Up @@ -553,7 +562,8 @@ JCode trans(muFun(FUNID uid, AType ftype), JGenie jg){
actuals = intercalate(", ", ["<needs_no_cast ? "" : "(<atype2javatype(ftype.formals[i])>)">$<fname>_<i>" | i <- [0..nformals]]);

if(!isEmpty(ftype.kwFormals)){
actuals = isEmpty(actuals) ? "$kwpActuals" : "<actuals>, $kwpActuals";
// actuals = isEmpty(actuals) ? "$kwpActuals" : "<actuals>, $kwpActuals";
actuals = isEmpty(actuals) ? " Util.kwpMap()" : "<actuals>, Util.kwpMap()";
}
// else if(muFunctions[uid]?){
// fun = muFunctions[uid];
Expand Down Expand Up @@ -607,21 +617,24 @@ JCode trans(muOFun(list[FUNID] funIds, AType ftype), JGenie jg){
fun = muFunctions[funId];
fun_kwFormals += fun.ftype.kwFormals;
if(!isEmpty(fun.externalRefs)){
ext_actuals += [varName(v, jg) | v <- fun.externalRefs];
//ext_actuals += [varName(v, jg) | v <- fun.externalRefs];
ext_actuals += [newValueRef(varName(v, jg), getType(v), v, jg) | v <- fun.externalRefs];
}
}
}

if(!isEmpty(fun_kwFormals)){
actualsWithCast += ["$kwpActuals"];
// actualsWithCast += ["$kwpActuals"];
actualsWithCast += ["Util.kwpMap()"];
}
actualsWithCast += ext_actuals;

funInstance = "new TypedFunctionInstance<nformals>\<<"IValue"><sep><intercalate(",", ["IValue" | int _ <- [0 ..nformals]])>\>";

formalsAsStr = intercalate(", ", formals);
actualsWithCastAsStr = intercalate(", ", actualsWithCast);
return "<funInstance>((<formalsAsStr>) -\> { return <fname>(<actualsWithCastAsStr>); }, <jg.accessType(ftype)>)";
res = "<funInstance>((<formalsAsStr>) -\> { return <fname>(<actualsWithCastAsStr>); }, <jg.accessType(ftype)>)";
return res;
}

// ---- muComposedFun ---------------------------------------------------------
Expand Down Expand Up @@ -779,10 +792,8 @@ JCode trans(muVarInit(v: muVar(str name, loc fuid, int pos, AType atype, IdRole
} else if(isSameVarModuloRole(v, exp)) {
return "";
} else {
x = varName(v, jg);
y = newValueRef(name, atype, exp, jg);
return jg.isRef(v) ? "final ValueRef\<<jtype>\> <varName(v, jg)> = <newValueRef(name, atype, exp, jg)>;\n" //new ValueRef\<<jtype>\>(<transWithCast(atype,exp,jg)>);\n"
: "<jtype> <varName(v, jg)> = <transWithCast(atype, exp, jg)>;\n";
: "<jtype> <varName(v, jg)> = (<jtype>)<transWithCast(atype, exp, jg)>;\n";
}
}

Expand Down Expand Up @@ -824,11 +835,11 @@ JCode trans(muVarInit(var: muTmpNative(str name, loc fuid, NativeKind nkind), Mu
if(isSameVarModuloRole(v, exp)){
return "";
}
jtype = atype2javatype(atype);
jtype = atype2javatype(alub(atype, getType(exp)));
if(jg.isRef(v)){
return "final ValueRef\<<jtype>\> <varName(v, jg)> = <newValueRef(name, atype, exp, jg)>;\n"; //new ValueRef\<<jtype>\>(<transWithCast(atype,exp,jg)>);\n";
}
return "<jtype> <varName(v, jg)> = <transWithCast(atype, exp, jg)>;\n";
return "<jtype> <varName(v, jg)> = (<jtype>)<transWithCast(atype, exp, jg)>;\n";
}

JCode trans(muConInit(v:muTmpIValue(str name, loc fuid, AType atype), MuExp exp), JGenie jg){
Expand Down Expand Up @@ -992,9 +1003,9 @@ JCode trans(muOCall(MuExp fun, AType ftype, list[MuExp] largs, lrel[str kwpName,
if(jg.isContainedIn(uid, jg.getModuleLoc())){
fn = muFunctions[uid];
arg_list = "(<intercalate(", ", actuals + kwactuals + externals)>)";

fun_name = isGlobalScope(fn.scopeIn)
? "$me.<asJavaName(getFunctionName(fn))>"
? (isMainName(fn.name) ? fn.name : "$me.<asJavaName(getFunctionName(fn))>")
: (isClosureName(fn.name)
? fn.name
: jg.getFunctionNameInContext(fn.scopeIn, fn.name)
Expand Down Expand Up @@ -1041,7 +1052,8 @@ JCode trans(muReturnFirstSucceeds(list[str] formals, list[MuExp] exps), JGenie j
// ---- muGetKwField ------------------------------------------------------

str prefix(str moduleName, JGenie jg){
res = (isEmpty(moduleName) || isEqualModule(moduleName, jg.getModuleName())) ? "" : (module2field(moduleName) + ".");
mn = jg.getModuleName();
res = (isEmpty(moduleName) || moduleName == jg.getModuleName()) ? "" : (module2field(moduleName) + ".");
return res;
}
JCode trans(muGetKwField(AType resultType, adtType:aadt(_,_,_), MuExp cons, str fieldName, str moduleName), JGenie jg){
Expand Down Expand Up @@ -1386,7 +1398,7 @@ JCode trans(muHasKwp(MuExp exp, str kwName), JGenie jg)
JCode trans(muGetKwp(MuExp exp, AType atype, str kwpName), JGenie jg){
if(acons(AType adtType, list[AType] _, list[Keyword] _) := atype){
adtName = getUniqueADTName(adtType);
return "$getkw_<adtName>_<asJavaName(kwpName,completeId=false)>(<transWithCast(adtType, exp, jg)>)";
return "<jg.getATypeAccessor(atype)>$getkw_<adtName>_<asJavaName(kwpName,completeId=false)>(<transWithCast(adtType, exp, jg)>)";
} else if(anode(_) := atype){
return "<trans(exp, jg)>.asWithKeywordParameters().getParameter(\"<unescape(kwpName)>\")";
} else if(a: aadt(str _, list[AType] _, SyntaxRole _) := atype){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ str generateResolver(MODID moduleId, str functionName, set[Define] fun_defs, map
call_actuals += "(<atype2javatype(def_type_formals[i])>) $P<i>";
}

actuals_text = intercalate(", ", call_actuals);
actuals_base = intercalate(", ", call_actuals);

activeKwpFormals = [];
if(def in relevant_fun_defs /*local_fun_defs*/){
Expand All @@ -436,22 +436,24 @@ str generateResolver(MODID moduleId, str functionName, set[Define] fun_defs, map
activeKwpFormals += tp.kwFormals;
}
}

actuals_with_kwp = actuals_base;
if(hasKeywordParameters(def_type) /*|| (def.scope notin module_scopes && !isEmpty(activeKwpFormals))*/){
actuals_text = isEmpty(actuals_text) ? "$kwpActuals" : "<actuals_text>, $kwpActuals";
actuals_with_kwp = isEmpty(actuals_base) ? "$kwpActuals" : "<actuals_base>, $kwpActuals";
}

if(!onlyGlobalFuns){
externalRefs = getExternalRefs(def, fid2muFunction);
if(!isEmpty(externalRefs)){
actuals_text += (isEmpty(actuals_text) ? "" : ", ") + intercalate(", ", [ varName(var) | var <- externalRefs ]);
actuals_with_kwp += (isEmpty(actuals_with_kwp) ? "" : ", ") + intercalate(", ", [ varName(var) | var <- externalRefs ]);
}
}
key = isConstructorAType(def_type) ? 0 : (def_type.isConcreteArg ? def_type.concreteFingerprint : def_type.abstractFingerprint);
//key = /*def_type.isDefault ? 0 :*/ (def_type.isConcreteArg ? def_type.concreteFingerprint : def_type.abstractFingerprint);

call_code = base_call = "";
cst = resolver_returns_void ? "" : "(<atype2javatype(resolver_return_type)>)";
if(isFunctionAType(def_type)){
cst = resolver_returns_void ? "" : "(<atype2javatype(resolver_return_type)>)";
if(jg.isContainedIn(def.defined, module_scope)){
pref = "";
if(def.scope != module_scope){
Expand All @@ -461,9 +463,9 @@ str generateResolver(MODID moduleId, str functionName, set[Define] fun_defs, map
}
}
}
call_code = "<cst><pref><uniqueName>(<actuals_text>)";
call_code = "<cst><pref><uniqueName>(<actuals_with_kwp>)";
} else if(/*jg.isContainedIn(def.defined, def.scope),*/ def.scope in module_scopes){
call_code = "<cst><module2field(def.scope)>.<uniqueName>(<actuals_text>)"; // was uniqueName
call_code = "<cst><module2field(def.scope)>.<uniqueName>(<actuals_with_kwp>)"; // was uniqueName
} else {
return; //do nothing
}
Expand All @@ -475,9 +477,9 @@ str generateResolver(MODID moduleId, str functionName, set[Define] fun_defs, map
}
} else {
if(hasKeywordParameters(def_type)){
base_call = "return $RVF.constructor(<jg.getATypeAccessor(def_type)><atype2idpart(def_type)>, new IValue[]{<actuals_text>}, $kwpActuals);";
base_call = "return $RVF.constructor(<jg.getATypeAccessor(def_type)><atype2idpart(def_type)>, new IValue[]{<actuals_base>}, $kwpActuals);";
} else {
base_call = "return $RVF.constructor(<jg.getATypeAccessor(def_type)><atype2idpart(def_type)>, new IValue[]{<actuals_text>});";
base_call = "return $RVF.constructor(<jg.getATypeAccessor(def_type)><atype2idpart(def_type)>, new IValue[]{<actuals_base>});";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ POSSIBILITY OF SUCH DAMAGE.
}
module lang::rascalcore::compile::util::Names

import IO;
import String;
import List;
import util::Reflective;
Expand Down Expand Up @@ -267,12 +268,12 @@ str unescapeName(str s){
return s[0] == "\\" ? s[1..] : s;
}

bool isEqualModule(str name1, name2){
if(contains(name1, "::") && contains(name2, "::")) return name1 == name2;
name1a = replaceAll(name1, "::", "_");
name2a = replaceAll(name2, "::", "_");
res = endsWith(name1a, name2a) || endsWith(name2a, name1a);
//println("isEqualModule(<name1>, <name2>) =\> <res>");
return res;
}
// bool isEqualModule(str name1, name2){
// if(contains(name1, "::") && contains(name2, "::")) return name1 == name2;
// name1a = replaceAll(name1, "::", "_");
// name2a = replaceAll(name2, "::", "_");
// res = endsWith(name1a, name2a) || endsWith(name2a, name1a);
// // println("isEqualModule(<name1>, <name2>) =\> <res>");
// return res;
// }

Loading
Loading