Skip to content

Commit

Permalink
Merge branch 'better-md5s'
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulKlint committed Jul 9, 2024
2 parents cf7b04d + 9fa4757 commit bd4e97b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ void collect (current: (Declaration) `<Tags tags> <Visibility visibility> data <
void collect (current: (Declaration) `<Tags tags> <Visibility visibility> data <UserType user> <CommonKeywordParameters commonKeywordParameters> = <{Variant "|"}+ variants> ;`, Collector c)
= dataDeclaration(tags, current, [v | v <- variants], c);

int dataCounter = 0;

void dataDeclaration(Tags tags, Declaration current, list[Variant] variants, Collector c){
userType = current.user;
adtName = prettyPrintName(userType.name);
Expand All @@ -41,7 +43,8 @@ void dataDeclaration(Tags tags, Declaration current, list[Variant] variants, Col
dt = isEmpty(typeParameters) ? defType(aadt(adtName, [], dataSyntax()))
: defType(typeParameters, AType(Solver s) { return aadt(adtName, [ s.getType(tp)[closed=true] | tp <- typeParameters], dataSyntax()); });

dt.md5 = md5Hash("<current>");
dt.md5 = md5Hash("<adtName><dataCounter>");
dataCounter += 1;
if(!isEmpty(commonKeywordParameterList)) dt.commonKeywordFields = commonKeywordParameterList;
c.define(adtName, dataId(), current, dt);

Expand All @@ -64,50 +67,54 @@ void dataDeclaration(Tags tags, Declaration current, list[Variant] variants, Col
AType(Solver) makeFieldType(str fieldName, Tree fieldType)
= AType(Solver s) { return s.getType(fieldType)[alabel=fieldName]; };

int variantCounter = 0;

void collect(current:(Variant) `<Name name> ( <{TypeArg ","}* arguments> <KeywordFormals keywordArguments> )`, Collector c){
formals = getFormals(current);
kwFormals = getKwFormals(current);

declaredFieldNames = {};

// Define all fields in the outer scope of the data declaration in order to be easily found there.

for(ta <- formals){
if(ta is named){
fieldName = prettyPrintName(ta.name);
if(fieldName in declaredFieldNames) c.report(error(ta, "Double declaration of field `%v`", fieldName));
if(<Declaration adt, list[TypeVar] dataTypeParameters, list[KeywordFormal] commonKwFormals, loc adtParentScope> := c.top(currentAdt)
&& str currentModuleName := c.top(key_current_module)
&& str adtName := "<adt.user.name>"
){
formals = getFormals(current);
kwFormals = getKwFormals(current);

declaredFieldNames = {};

// Define all fields in the outer scope of the data declaration in order to be easily found there.

for(ta <- formals){
if(ta is named){
fieldName = prettyPrintName(ta.name);
if(fieldName in declaredFieldNames) c.report(error(ta, "Double declaration of field `%v`", fieldName));
declaredFieldNames += fieldName;
fieldType = ta.\type;
dt = defType([fieldType], makeFieldType(fieldName, fieldType));
dt.md5 = md5Hash("<currentModuleName><adtName><dataCounter><variantCounter><ta>");
c.define(fieldName, fieldId(), ta.name, dt);
}
}

for(KeywordFormal kwf <- kwFormals){
fieldName = prettyPrintName(kwf.name);
if(fieldName in declaredFieldNames) c.report(error(kwf, "Double declaration of field `%v`", fieldName));
declaredFieldNames += fieldName;
fieldType = ta.\type;
dt = defType([fieldType], makeFieldType(fieldName, fieldType));
dt.md5 = md5Hash("<c.getScope()><current><ta>");
c.define(fieldName, fieldId(), ta.name, dt);
kwfType = kwf.\type;
dt = defType([kwfType], makeFieldType(fieldName, kwfType));
dt.md5 = md5Hash("<currentModuleName><adtName><dataCounter><variantCounter><kwfType><fieldName>");
c.define(fieldName, keywordFieldId(), kwf.name, dt);
c.requireSubType(kwf.expression, kwfType, error(kwf, "Default expression of type %t expected, found %t", kwfType, kwf.expression));
}
}

for(KeywordFormal kwf <- kwFormals){
fieldName = prettyPrintName(kwf.name);
if(fieldName in declaredFieldNames) c.report(error(kwf, "Double declaration of field `%v`", fieldName));
declaredFieldNames += fieldName;
kwfType = kwf.\type;
dt = defType([kwfType], makeFieldType(fieldName, kwfType));
dt.md5 = md5Hash("<c.getScope()><current><kwfType><fieldName>");
c.define(fieldName, keywordFieldId(), kwf.name, dt);
c.requireSubType(kwf.expression, kwfType, error(kwf, "Default expression of type %t expected, found %t", kwfType, kwf.expression));

}

scope = c.getScope();

if(<Tree adt, list[TypeVar] dataTypeParameters, list[KeywordFormal] commonKwFormals, loc adtParentScope> := c.top(currentAdt) &&
str currentModuleName := c.top(key_current_module)){
scope = c.getScope();
c.enterScope(current);
c.defineInScope(adtParentScope, prettyPrintName(name), constructorId(), name, defType(adt + formals + kwFormals + commonKwFormals,
AType(Solver s){
adtType = s.getType(adt);
kwFormalTypes = [kwField(s.getType(kwf.\type)[alabel=prettyPrintName(kwf.name)], prettyPrintName(kwf.name), currentModuleName, kwf.expression) | kwf <- kwFormals /*+ commonKwFormals*/];
formalTypes = [f is named ? s.getType(f)[alabel=prettyPrintName(f.name)] : s.getType(f) | f <- formals];
return acons(adtType, formalTypes, kwFormalTypes)[alabel=asUnqualifiedName(prettyPrintName(name))];
})[md5=md5Hash(current)]);
})[md5 = md5Hash("<currentModuleName><adtName><dataCounter><name><variantCounter>(<arguments>")]);
variantCounter += 1;
c.fact(current, name);
beginUseTypeParameters(c, closed=false);
// The standard rules would declare arguments and kwFormals as variableId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import util::Reflective;

void collect(Module current: (Module) `<Header header> <Body body>`, Collector c){

dataCounter = 0;
variantCounter = 0;

mloc = getLoc(current);
mname = prettyPrintName(header.name);
checkModuleName(getLoc(current), header.name, c);
Expand Down Expand Up @@ -220,6 +223,8 @@ void collect(current: (FunctionDeclaration) `<FunctionDeclaration decl>`, Collec
c.report(info(current, "Ignoring function declaration for `<decl.signature.name>`"));
return;
}
c.push(currentFunction, ppfname);
md5Contrib = md5Contrib4signature(signature);

<expected, expectedTagString> = getExpected(decl.tags);
if(expected){
Expand Down Expand Up @@ -260,6 +265,7 @@ void collect(current: (FunctionDeclaration) `<FunctionDeclaration decl>`, Collec

if("default" in modifiers){
ft.isDefault = true;
md5Contrib = "default" + md5Contrib;
}

if("test" in modifiers){
Expand Down Expand Up @@ -287,11 +293,7 @@ void collect(current: (FunctionDeclaration) `<FunctionDeclaration decl>`, Collec
if(!isEmpty(tagsMap)) dt.tags = tagsMap;
alwaysSucceeds = all(pat <- getFormals(signature.parameters), pat is typedVariable) && !(decl is conditional) && !(decl is \default && /(Statement) `fail <Target _>;` := decl.body);
if(!alwaysSucceeds) dt.canFail = true;
dt.md5 = md5Hash("<parentScope><decl>");
if(!isEmpty(modifiers)) dt.modifiers = modifiers;
c.defineInScope(parentScope, prettyPrintName(fname), functionId(), current, dt);
beginUseBoundedTypeParameters(tpbounds, c);
Expand Down Expand Up @@ -319,6 +321,9 @@ void collect(current: (FunctionDeclaration) `<FunctionDeclaration decl>`, Collec
if(!myReturnsViaAllPath && "<signature.\type>" != "void"){
c.report(error(decl.signature, "Missing return statement"));
}
if(!alwaysSucceeds){
md5Contrib += "<decl.body>";
}
}
if(decl is expression || decl is conditional){
Expand All @@ -333,6 +338,8 @@ void collect(current: (FunctionDeclaration) `<FunctionDeclaration decl>`, Collec
}
if(decl is conditional){
conditions = [cond | cond <- decl.conditions];
md5Contrib += "<decl.conditions>";
storeAllowUseBeforeDef(decl, decl.expression, c);
c.require("when conditions", decl.conditions, conditions,
void (Solver s){
Expand All @@ -351,13 +358,23 @@ void collect(current: (FunctionDeclaration) `<FunctionDeclaration decl>`, Collec
endUseBoundedTypeParameters(c);
surroundingFuns = c.getStack(currentFunction);
dt.md5 = md5Hash(size(surroundingFuns) == 1 ? md5Contrib : "<intercalate("/", surroundingFuns)><md5Contrib>");
c.defineInScope(parentScope, prettyPrintName(fname), functionId(), current, dt);
c.leaveScope(decl);
c.pop(currentFunction);
}
void collect(current: (FunctionBody) `{ <Statement* statements> }`, Collector c){
collect(statements, c);
}
str md5Contrib4signature(Signature signature){
return "<signature.\type><signature.name><signature.parameters.formals>";
}
tuple[set[str], rel[str,Type]] collectSignature(Signature signature, Collector c){
returnType = signature.\type;
parameters = signature.parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void collect (current: (SyntaxDefinition) `<Start strt> syntax <Sym defined> = <
}

int nalternatives = 0;
int syndefCounter = 0;

void declareSyntax(SyntaxDefinition current, SyntaxRole syntaxRole, IdRole idRole, Collector c, Vis vis=publicVis()){
//println("declareSyntax: <current>");
Expand All @@ -52,7 +53,8 @@ void declareSyntax(SyntaxDefinition current, SyntaxRole syntaxRole, IdRole idRol

dt = defType(nonterminalType);
dt.vis = vis;
dt.md5 = md5Hash("<current>");
dt.md5 = md5Hash("<adtName><syndefCounter>");
syndefCounter += 1;

// Define the syntax symbol itself and all labelled alternatives as constructors
c.define(adtName, idRole, current, dt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import lang::rascal::\syntax::Rascal;
public /*const*/ str patternContainer = "patternContainer";
public /*const*/ str patternNames = "patternNames";

public /*const*/ str currentAdt = "currentAdt"; // used to mark data declarations
public /*const*/ str inAlternative = "inAlternative"; // used to mark top-level alternative in syntax declaration
public /*const*/ str currentAdt = "currentAdt"; // used to mark data declarations
public /*const*/ str currentFunction = "currentFunction"; // used to mark function declarations
public /*const*/ str inAlternative = "inAlternative"; // used to mark top-level alternative in syntax declaration
public /*const*/ str typeContainer = "typeContainer";
public /*const*/ str inConcreteLiteral = "concreteLiteral"; // used to mark that we are inside a concrete literal

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
module lang::rascalcore::compile::Examples::Tst4

//alias T = tuple[int, str];
void main() {
//set[tuple[int, str]] r = {};

// Error
// `Initialization of `ts1` should be subtype of `rel[int, str]`, found `rel[value, value]``
//set[tuple[int, str]] ts1 = {t | t:<int _, _> <- r};

set[str] a = {};
set[str] b = { x| t: x <- a};

//// No errors
//set[T] ts2 = {t | t <- r};
//set[T] ts3 = {t | T t:<_, _> <- r};
//set[T] ts4 = {<i, s> | <i, s> <- r};
//set[T] ts5 = {t | t:<int _, str _> <- r};
int outer1(){
int f(int n) = n;
return f(3);
}

//value main() = _f(3);
int outer2(){
int f(int n) = n;
return f(4);
}

//data Tree;
//anno set[int] Tree@messages;
Expand Down

0 comments on commit bd4e97b

Please sign in to comment.