diff --git a/Compiler/FrontEnd/Inst.mo b/Compiler/FrontEnd/Inst.mo index 3c430a428eb..0d0818c3fa1 100644 --- a/Compiler/FrontEnd/Inst.mo +++ b/Compiler/FrontEnd/Inst.mo @@ -3293,7 +3293,7 @@ algorithm if SCode.finalBool(final_prefix) then m = InstUtil.traverseModAddFinal(m); end if; - comp = SCode.COMPONENT(name, prefixes, attr, ts, m, comment, cond, info); + comp = if referenceEq(el.modifications, m) then el else SCode.COMPONENT(name, prefixes, attr, ts, m, comment, cond, info); // Fails if multiple decls not identical already_declared = InstUtil.checkMultiplyDeclared(cache, env, mods, pre, ci_state, (comp, cmod), inst_dims, impl); @@ -4125,9 +4125,6 @@ algorithm // If first part of ident is a class, e.g StateSelect.None, nothing to update case (cache,env,ih,_,_,_,_,_,_,_) - equation - _ = Absyn.crefFirstIdent(cref); - // (cache,_,_) = Lookup.lookupClass(cache,env, Absyn.IDENT(id)); then (cache,env,ih,inUpdatedComps); // report an error! diff --git a/Compiler/FrontEnd/InstExtends.mo b/Compiler/FrontEnd/InstExtends.mo index ecb33fc4e0e..eb8fe3f9cde 100644 --- a/Compiler/FrontEnd/InstExtends.mo +++ b/Compiler/FrontEnd/InstExtends.mo @@ -1533,7 +1533,7 @@ algorithm equation id = Absyn.crefFirstIdent(cref); //print("Try lookupC " + id + "\n"); - (_,c,denv) = Lookup.lookupClass(arrayGet(cache,1),env,Absyn.IDENT(id)); + (_,c,denv) = Lookup.lookupClassIdent(arrayGet(cache,1),env,id); // isOutside = FGraph.graphPrefixOf(denv, env); // id might come from named import, make sure you use the actual class name! id = SCode.getElementName(c); diff --git a/Compiler/FrontEnd/InstUtil.mo b/Compiler/FrontEnd/InstUtil.mo index e129e8daeb8..5a6a11a517c 100644 --- a/Compiler/FrontEnd/InstUtil.mo +++ b/Compiler/FrontEnd/InstUtil.mo @@ -3560,7 +3560,7 @@ algorithm false = FGraph.isTopScope(inNewEnv); id = FNode.refName(FGraph.lastScopeRef(inNewEnv)); (rest, _) = FGraph.stripLastScopeRef(inNewEnv); - (_, cls, _) = Lookup.lookupClass(inCache, rest, Absyn.IDENT(id)); + (_, cls, _) = Lookup.lookupClassIdent(inCache, rest, id); ci_state = ClassInf.start(SCode.getClassRestriction(cls), FGraph.getGraphName(inNewEnv)); then ci_state; @@ -6618,30 +6618,29 @@ end extractCorrectClassMod2; public function traverseModAddFinal "Helper function for traverseModAddFinal" - input SCode.Mod mod; - output SCode.Mod mod2; + input output SCode.Mod mod; algorithm - mod2 := matchcontinue(mod) + mod := matchcontinue(mod) local - SCode.Element element; + SCode.Element element1,element2; SCode.Each each_; - list subs; + list subs1,subs2; Option eq; SourceInfo info; + SCode.Final f; - case(SCode.NOMOD()) then SCode.NOMOD(); + case SCode.NOMOD() then mod; - case(SCode.REDECL(eachPrefix = each_, element = element)) + case SCode.REDECL(eachPrefix = each_, element = element1) equation - element = traverseModAddFinal3(element); - then - SCode.REDECL(SCode.FINAL(),each_,element); + element2 = traverseModAddFinal3(element1); + then if referenceEq(element1,element2) then mod else SCode.REDECL(SCode.FINAL(),each_,element2); - case(SCode.MOD(_,each_,subs,eq,info)) + case(SCode.MOD(f,each_,subs1,eq,info)) equation - subs = traverseModAddFinal4(subs); + subs2 = List.mapCheckReferenceEq(subs1, traverseModAddFinal4); then - SCode.MOD(SCode.FINAL(),each_,subs,eq,info); + if valueEq(SCode.FINAL(),f) and referenceEq(subs1, subs2) then mod else SCode.MOD(SCode.FINAL(),each_,subs2,eq,info); else equation @@ -6674,15 +6673,15 @@ algorithm equation mod = traverseModAddFinal(oldmod); then - SCode.COMPONENT(name,prefixes,attr,tySpec,mod,cmt,cond,info); + if referenceEq(oldmod,mod) then inElement else SCode.COMPONENT(name,prefixes,attr,tySpec,mod,cmt,cond,info); case SCode.IMPORT() then inElement; case SCode.CLASS() then inElement; - case SCode.EXTENDS(p,vis,mod,ann,info) + case SCode.EXTENDS(p,vis,oldmod,ann,info) equation - mod = traverseModAddFinal(mod); - then SCode.EXTENDS(p,vis,mod,ann,info); + mod = traverseModAddFinal(oldmod); + then if referenceEq(oldmod,mod) then inElement else SCode.EXTENDS(p,vis,mod,ann,info); else equation @@ -6695,25 +6694,14 @@ end traverseModAddFinal3; protected function traverseModAddFinal4 "Helper function for traverseModAddFinal2" - input list subs; - output list osubs; -algorithm osubs:= matchcontinue(subs) - local - String ident; - SCode.Mod mod; - list intList; - list rest; - case({}) then {}; - case((SCode.NAMEMOD(ident,mod))::rest ) - equation - rest = traverseModAddFinal4(rest); - mod = traverseModAddFinal(mod); - then - SCode.NAMEMOD(ident,mod)::rest; - else - equation print(" we failed with traverseModAddFinal4\n"); - then fail(); -end matchcontinue; + input output SCode.SubMod sub; +protected + SCode.Mod mod; +algorithm + mod := traverseModAddFinal(sub.mod); + if not referenceEq(sub.mod, mod) then + sub.mod := mod; + end if; end traverseModAddFinal4; public function traverseModAddDims diff --git a/Compiler/FrontEnd/Lookup.mo b/Compiler/FrontEnd/Lookup.mo index cd85594e4df..a50d99994b5 100644 --- a/Compiler/FrontEnd/Lookup.mo +++ b/Compiler/FrontEnd/Lookup.mo @@ -425,6 +425,18 @@ algorithm // print("Lookup C2: " + " outenv: " + FGraph.printGraphPathStr(outEnv) + "\n"); end lookupClass; +public function lookupClassIdent "Like lookupClass, but takes a String as ident for input (avoids Absyn.IDENT() creation)" + input FCore.Cache inCache; + input FCore.Graph inEnv "Where to look"; + input String ident; + input Option inInfo = NONE(); + output FCore.Cache outCache; + output SCode.Element outClass; + output FCore.Graph outEnv; +algorithm + (outCache,outClass,outEnv) := lookupClassInEnv(inCache, inEnv, ident, {}, Util.makeStatefulBoolean(false), inInfo); +end lookupClassIdent; + protected function lookupClass1 "help function to lookupClass, does all the work." input FCore.Cache inCache; input FCore.Graph inEnv; @@ -547,7 +559,7 @@ algorithm // Qualified names in package and non-package case (cache,env,_,_,NONE(),_) equation - (cache,c,env,prevFrames) = lookupClass2(cache,env,Absyn.IDENT(id),{},inState,inInfo); + (cache,c,env,prevFrames) = lookupClassInEnv(cache,env,id,{},inState,inInfo); (optFrame,prevFrames) = lookupPrevFrames(id,prevFrames); (cache,c,env,prevFrames) = lookupClassQualified2(cache,env,path,c,optFrame,prevFrames,inState,inInfo); then @@ -870,7 +882,7 @@ algorithm Util.setStatefulBoolean(inState,true); r::prevFrames = listReverse(FGraph.currentScope(env)); env = FGraph.setScope(env, {r}); - (cache,c,env_1,prevFrames) = lookupClass2(cache,env,Absyn.IDENT(id),prevFrames,Util.makeStatefulBoolean(false),inInfo); + (cache,c,env_1,prevFrames) = lookupClassInEnv(cache,env,id,prevFrames,Util.makeStatefulBoolean(false),inInfo); then (cache,c,env_1,prevFrames); @@ -1012,7 +1024,7 @@ algorithm Inst.partialInstClassIn(cache, env2, InnerOuter.emptyInstHierarchy, mod, Prefix.NOPRE(), ci_state, c, SCode.PUBLIC(), {}, 0); // Restrict import to the imported scope only, not its parents, thus {f} below - (cache,c_1,env2,prevFrames) = lookupClass2(cache,env2,Absyn.IDENT(ident),prevFrames,Util.makeStatefulBoolean(true),inInfo) "Restrict import to the imported scope only, not its parents..." ; + (cache,c_1,env2,prevFrames) = lookupClassInEnv(cache,env2,ident,prevFrames,Util.makeStatefulBoolean(true),inInfo) "Restrict import to the imported scope only, not its parents..." ; (cache,more) = moreLookupUnqualifiedImportedClassInFrame(cache, rest, env, ident); unique = boolNot(more); then @@ -1410,9 +1422,9 @@ algorithm case (NONE()) equation (cache,(c as SCode.CLASS(name=n,encapsulatedPrefix=encflag,restriction=r)),env2,prevFrames) = - lookupClass2(cache, + lookupClassInEnv(cache, env, - Absyn.IDENT(id), + id, prevFrames, Util.makeStatefulBoolean(true), // In order to use the prevFrames, we need to make sure we can't instantiate one of the classes too soon! NONE()); diff --git a/Compiler/FrontEnd/SCode.mo b/Compiler/FrontEnd/SCode.mo index 8adbe02a407..86ee3768249 100644 --- a/Compiler/FrontEnd/SCode.mo +++ b/Compiler/FrontEnd/SCode.mo @@ -5260,16 +5260,22 @@ algorithm SourceInfo i1, i2; Mod m; - case (NOMOD(), _) then inOldMod; case (_, NOMOD()) then inNewMod; + case (NOMOD(), _) then inOldMod; case (REDECL(), _) then inNewMod; case (MOD(f1, e1, sl1, b1, i1), - MOD(_, _, sl2, b2, _)) + MOD(f2, e2, sl2, b2, _)) equation b = mergeBindings(b1, b2); sl = mergeSubMods(sl1, sl2); - m = MOD(f1, e1, sl, b, i1); + if referenceEq(b, b1) and referenceEq(sl, sl1) then + m = inNewMod; + elseif referenceEq(b, b2) and referenceEq(sl, sl2) and valueEq(f1, f2) and valueEq(e1, e2) then + m = inOldMod; + else + m = MOD(f1, e1, sl, b, i1); + end if; then m; @@ -5324,7 +5330,7 @@ algorithm list idxs1, idxs2; SubMod s; - case (_, {}) then {}; + case (_, {}) then inOld; case (NAMEMOD(ident = id1), NAMEMOD(ident = id2)::rest) equation diff --git a/Compiler/FrontEnd/Static.mo b/Compiler/FrontEnd/Static.mo index 5e6f7d103d5..17db280c128 100644 --- a/Compiler/FrontEnd/Static.mo +++ b/Compiler/FrontEnd/Static.mo @@ -7508,7 +7508,7 @@ algorithm case (cache,env,fn,args,nargs,impl,_,st,pre,_,_) equation (cache,cl as SCode.CLASS(restriction = SCode.R_PACKAGE()),_) = - Lookup.lookupClass(cache, env, Absyn.IDENT("GraphicalAnnotationsProgram____")); + Lookup.lookupClassIdent(cache, env, "GraphicalAnnotationsProgram____"); (cache,cl as SCode.CLASS( restriction = SCode.R_RECORD(_)),env_1) = Lookup.lookupClass(cache, env, fn); (cache,cl,env_2) = Lookup.lookupRecordConstructorClass(cache, env_1 /* env */, fn); (_,_::names) = SCode.getClassComponents(cl); // remove the fist one as it is the result! @@ -12680,7 +12680,7 @@ algorithm case () // not a class or OpenModelica, continue equation - failure((_,_,_) = Lookup.lookupClass(cache, env, Absyn.IDENT(id))); + failure((_,_,_) = Lookup.lookupClassIdent(cache, env, id)); (_,dexp,prop,_) = elabExpInExpression(cache,env,exp,false,st,false,Prefix.NOPRE(),info); then (); diff --git a/Compiler/Script/Interactive.mo b/Compiler/Script/Interactive.mo index b980980c7fc..3338d8426f0 100644 --- a/Compiler/Script/Interactive.mo +++ b/Compiler/Script/Interactive.mo @@ -11567,7 +11567,7 @@ algorithm // adrpo: handle the case for model extends baseClassName end baseClassName; case (Absyn.CLASS(body = Absyn.CLASS_EXTENDS(baseClassName, _, _, parts = parts)),env) equation - (_,_,cenv) = Lookup.lookupClass(FCore.emptyCache(), env, Absyn.IDENT(baseClassName), SOME(inClass.info)); + (_,_,cenv) = Lookup.lookupClassIdent(FCore.emptyCache(), env, baseClassName, SOME(inClass.info)); SOME(envpath) = FGraph.getScopePath(cenv); p1 = Absyn.joinPaths(envpath, Absyn.IDENT(baseClassName)); cref = Absyn.pathToCref(p1); @@ -12922,7 +12922,7 @@ algorithm algorithm (cache, env, _, outCache) := buildEnvForGraphicProgram(outCache, mod); - (cache, c, env2) := Lookup.lookupClass(cache, inEnv, Absyn.IDENT(ann_name)); + (cache, c, env2) := Lookup.lookupClassIdent(cache, inEnv, ann_name); smod := SCodeUtil.translateMod(SOME(Absyn.CLASSMOD(mod, Absyn.NOMOD())), SCode.NOT_FINAL(), SCode.NOT_EACH(), info); (cache, dmod) := Mod.elabMod(cache, env, InnerOuter.emptyInstHierarchy, Prefix.NOPRE(), @@ -12946,7 +12946,7 @@ algorithm algorithm (cache, _, _, outCache) := buildEnvForGraphicProgram(outCache, {}); - (cache, c, env) := Lookup.lookupClass(cache, inEnv, Absyn.IDENT(ann_name)); + (cache, c, env) := Lookup.lookupClassIdent(cache, inEnv, ann_name); c := SCode.classSetPartial(c, SCode.NOT_PARTIAL()); (_, _, _, _, dae) := Inst.instClass(cache, env, InnerOuter.emptyInstHierarchy, UnitAbsyn.noStore, DAE.NOMOD(), Prefix.NOPRE(), c, {}, false,