Skip to content

Commit cb9f6f3

Browse files
committed
- Improved name prefixing in new instantiation.
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18463 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent b3b1f89 commit cb9f6f3

File tree

5 files changed

+127
-72
lines changed

5 files changed

+127
-72
lines changed

Compiler/FrontEnd/Absyn.mo

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3788,20 +3788,45 @@ algorithm
37883788
outPath := match (inPath)
37893789
local
37903790
Ident str;
3791-
Path p_1,p;
3792-
case (IDENT(name = _)) then fail();
3793-
case (QUALIFIED(name = str,path = IDENT(name = _))) then IDENT(str);
3794-
case (QUALIFIED(name = str,path = p))
3791+
Path p;
3792+
3793+
case QUALIFIED(name = str, path = IDENT(name = _))
3794+
then IDENT(str);
3795+
3796+
case QUALIFIED(name = str, path = p)
37953797
equation
3796-
p_1 = stripLast(p);
3798+
p = stripLast(p);
37973799
then
3798-
QUALIFIED(str,p_1);
3799-
case (FULLYQUALIFIED(p)) equation
3800-
p_1 = stripLast(p);
3801-
then FULLYQUALIFIED(p_1);
3800+
QUALIFIED(str, p);
3801+
3802+
case FULLYQUALIFIED(p)
3803+
equation
3804+
p = stripLast(p);
3805+
then
3806+
FULLYQUALIFIED(p);
3807+
38023808
end match;
38033809
end stripLast;
38043810

3811+
public function stripLastOpt
3812+
input Path inPath;
3813+
output Option<Path> outPath;
3814+
algorithm
3815+
outPath := match(inPath)
3816+
local
3817+
Path p;
3818+
3819+
case IDENT(name = _) then NONE();
3820+
3821+
else
3822+
equation
3823+
p = stripLast(inPath);
3824+
then
3825+
SOME(p);
3826+
3827+
end match;
3828+
end stripLastOpt;
3829+
38053830
public function crefStripLast "Returns the path given as argument to
38063831
the function minus the last ident."
38073832
input ComponentRef inCref;

Compiler/FrontEnd/NFInst.mo

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ algorithm
981981
Absyn.QUALIFIED(name = enum_idx_str, path = tpath) = tpath;
982982
enum_idx = stringInt(enum_idx_str);
983983

984-
(cls_entry, env) = NFLookup.lookupClassName(tpath, inEnv, info);
984+
(cls_entry, env) = NFLookup.lookupScopeEntry(inEnv);
985985
prefix = NFEnv.scopePrefix(inEnv);
986986
path = NFInstPrefix.prefixPath(Absyn.IDENT(name), prefix);
987987

@@ -2219,7 +2219,7 @@ algorithm
22192219

22202220
// If the cref refers to a package constant, make sure it's instantiated
22212221
// and added to the symbol table.
2222-
opt_prefix = makePackageConstantPrefix(is_class, opt_prefix, env, base_env);
2222+
opt_prefix = makePackageConstantPrefix(is_class, opt_prefix, inCrefPath, prefix);
22232223
globals = instPackageConstant(cref, entry, env, opt_prefix, inInfo, inGlobals);
22242224
then
22252225
(cref, globals);
@@ -2278,52 +2278,34 @@ protected function makePackageConstantPrefix
22782278
a package constant that needs to be instantiated."
22792279
input Boolean inIsClass;
22802280
input Option<Prefix> inPrefix;
2281-
input Env inEnv;
2282-
input Env inBaseEnv;
2281+
input Absyn.Path inCrefPath;
2282+
input Prefix inCrefPrefix;
22832283
output Option<Prefix> outPrefix;
22842284
algorithm
2285-
outPrefix := matchcontinue(inIsClass, inPrefix, inEnv, inBaseEnv)
2285+
outPrefix :=
2286+
match(inIsClass, inPrefix, inCrefPath, inCrefPrefix)
22862287
local
22872288
Prefix prefix;
2288-
list<String> env_strl, base_env_strl;
2289-
2290-
// The first part of the cref refers to a class and the scope has a prefix.
2291-
// This happens when for example a cref refer to a package constant in a
2292-
// local class. Add the difference of the environment the cref was found in
2293-
// and the environment where the first identifier of the cref was found in
2294-
// to the scope's prefix.
2295-
case (true, SOME(prefix), _, _)
2296-
equation
2297-
env_strl = NFEnv.scopeNames(inEnv);
2298-
base_env_strl = NFEnv.scopeNames(inBaseEnv);
2299-
(env_strl, _) =
2300-
List.removeEqualPrefix(env_strl, base_env_strl, stringEq);
2301-
prefix = NFInstPrefix.toPackagePrefix(prefix);
2302-
prefix = NFInstPrefix.addStringList(env_strl, prefix);
2303-
then
2304-
SOME(prefix);
23052289

2306-
// A component in a scope with a package prefix, i.e. a package constant
2307-
// which should be instantiated because it's a dependency of another package
2308-
// constant (due to e.g. a binding or modifier). Use the given prefix.
2290+
// A component found in a scope with a prefix. If the prefix is a package
2291+
// prefix then the cref is a package constant which should be instantiated
2292+
// because it's a dependency of another package constant, in which case the
2293+
// given prefix should be used. Otherwise it's not a package constant and
2294+
// will be instantiated normally, in which case NONE() is returned.
23092295
case (false, SOME(prefix), _, _)
2296+
then Util.if_(NFInstPrefix.isPackagePrefix(prefix), inPrefix, NONE());
2297+
2298+
// Otherwise we have a component without a prefix or a class.
2299+
else
23102300
equation
2311-
true = NFInstPrefix.isPackagePrefix(prefix);
2312-
then
2313-
inPrefix;
2314-
2315-
// A component in a scope with a non-package prefix, not a package constant.
2316-
case (false, SOME(_), _, _) then NONE();
2317-
2318-
// Anything in a scope without a prefix is a package constant that should be
2319-
// prefixed with the environment path.
2320-
case (_, NONE(), _, _)
2321-
equation
2322-
prefix = NFEnv.envPrefix(inEnv);
2301+
// If the scope has a prefix, use that. Otherwise use the cref prefix.
2302+
prefix = Util.getOptionOrDefault(inPrefix, inCrefPrefix);
2303+
// Add the cref path except for the last identifier to the prefix.
2304+
prefix = NFInstPrefix.addOptPath(Absyn.stripLastOpt(inCrefPath), prefix);
23232305
then
23242306
SOME(prefix);
23252307

2326-
end matchcontinue;
2308+
end match;
23272309
end makePackageConstantPrefix;
23282310

23292311
protected function instPackageConstant

Compiler/FrontEnd/NFInstPrefix.mo

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ algorithm
9292
outPrefix := fromPath2(inPath, inPrefix);
9393
end addPath;
9494

95+
public function addOptPath
96+
input Option<Absyn.Path> inOptPath;
97+
input Prefix inPrefix;
98+
output Prefix outPrefix;
99+
algorithm
100+
outPrefix := match(inOptPath, inPrefix)
101+
local
102+
Absyn.Path p;
103+
104+
case (NONE(), _) then inPrefix;
105+
case (SOME(p), _) then addPath(p, inPrefix);
106+
107+
end match;
108+
end addOptPath;
109+
95110
public function addString
96111
"Prefixes the prefix with the given String."
97112
input String inName;

Compiler/FrontEnd/NFLookup.mo

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,28 @@ algorithm
241241
end match;
242242
end lookupTypeSpec;
243243

244+
public function lookupScopeEntry
245+
input Env inEnv;
246+
output Entry outEntry;
247+
output Env outEnv;
248+
algorithm
249+
(outEntry, outEnv) := match(inEnv)
250+
local
251+
String scope_name;
252+
Env env;
253+
Entry entry;
254+
255+
case _
256+
equation
257+
scope_name = NFEnv.scopeName(inEnv);
258+
env = NFEnv.exitScope(inEnv);
259+
entry = NFEnv.lookupEntry(scope_name, env);
260+
then
261+
(entry, env);
262+
263+
end match;
264+
end lookupScopeEntry;
265+
244266
protected function makeDummyMetaType
245267
input String inTypeName;
246268
output SCode.Element outClass;
@@ -1044,7 +1066,7 @@ algorithm
10441066
inOrigins, inEnv, inSplitFunc, inInfo, inAccumEnv)
10451067
local
10461068
list<SCode.Element> elems, cls_vars, exts, imps;
1047-
Env env;
1069+
Env env, der_env;
10481070
list<EntryOrigin> origin;
10491071
Entry entry;
10501072
list<SCode.Enum> enums;
@@ -1056,6 +1078,8 @@ algorithm
10561078
SCode.Mod smod;
10571079
Modifier mod;
10581080
String enum_name;
1081+
Absyn.ArrayDim ad;
1082+
Integer dim_count;
10591083

10601084
case (SCode.PARTS(elementLst = elems), _, _, _, _, _, _, env)
10611085
equation
@@ -1082,22 +1106,25 @@ algorithm
10821106
then
10831107
(env, ext_mods);
10841108

1085-
case (SCode.DERIVED(typeSpec = ty, modifications = smod), _, _, _, _, _, _, _)
1109+
case (SCode.DERIVED(typeSpec = ty, modifications = smod,
1110+
attributes = SCode.ATTR(arrayDims = ad)), _, _, _, _, _, _, _)
10861111
equation
10871112
// Apply the modifier from the derived declaration.
1088-
// TODO: The prefix and dimensions are wrong.
1089-
mod = NFMod.translateMod(smod, "", 0, inEnv);
1113+
dim_count = listLength(ad);
1114+
mod = NFMod.translateMod(smod, "", dim_count, inEnv);
10901115
mod = NFMod.mergeMod(inModifier, mod);
10911116

1092-
(entry, env) = lookupTypeSpec(ty, inEnv, inInfo);
1117+
(entry, der_env) = lookupTypeSpec(ty, inEnv, inInfo);
10931118
(el as SCode.CLASS(classDef = cdef)) = NFEnv.entryElement(entry);
10941119
// TODO: Only create this environment if needed, i.e. if the cdef
10951120
// contains extends.
1096-
env = openClassScope(el, NONE(), env);
1097-
(env, _) = populateEnvWithClassDef(cdef, mod, inVisibility,
1121+
env = openClassScope(el, NONE(), der_env);
1122+
(der_env, _) = populateEnvWithClassDef(cdef, mod, inVisibility,
10981123
inOrigins, env, elementSplitterExtends, inInfo, inAccumEnv);
1124+
1125+
env = NFEnv.copyScopePrefix(inEnv, env);
10991126
(env, ext_mods) = populateEnvWithClassDef(cdef, mod,
1100-
inVisibility, inOrigins, env, inSplitFunc, inInfo, inAccumEnv);
1127+
inVisibility, inOrigins, der_env, inSplitFunc, inInfo, env);
11011128
then
11021129
(env, ext_mods);
11031130

Compiler/Util/Util.mo

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,11 +2323,12 @@ public function stringOption "author: PA
23232323
input Option<String> inStringOption;
23242324
output String outString;
23252325
algorithm
2326-
outString:=
2327-
match (inStringOption)
2328-
local String s;
2329-
case (NONE()) then "";
2330-
case (SOME(s)) then s;
2326+
outString:= match(inStringOption)
2327+
local
2328+
String s;
2329+
2330+
case SOME(s) then s;
2331+
else "";
23312332
end match;
23322333
end stringOption;
23332334

@@ -2344,26 +2345,31 @@ end getOption;
23442345
public function getOptionOrDefault
23452346
"Returns an option value if SOME, otherwise the default"
23462347
input Option<Type_a> inOption;
2347-
input Type_a default;
2348-
output Type_a unOption;
2348+
input Type_a inDefault;
2349+
output Type_a outOption;
23492350
replaceable type Type_a subtypeof Any;
23502351
algorithm
2351-
unOption := matchcontinue (inOption,default)
2352-
local Type_a item;
2353-
case (SOME(item),_) then item;
2354-
case (_,_) then default;
2355-
end matchcontinue;
2352+
outOption := match(inOption, inDefault)
2353+
local
2354+
Type_a item;
2355+
2356+
case (SOME(item), _) then item;
2357+
else inDefault;
2358+
end match;
23562359
end getOptionOrDefault;
23572360

23582361
public function genericOption "author: BZ
23592362
Returns a list with single value or an empty list if there is no optional value."
23602363
input Option<Type_a> inOption;
2361-
output list<Type_a> unOption;
2364+
output list<Type_a> outOption;
23622365
replaceable type Type_a subtypeof Any;
2363-
algorithm unOption := match (inOption)
2364-
local Type_a item;
2365-
case (NONE()) then {};
2366-
case (SOME(item)) then {item};
2366+
algorithm
2367+
outOption := match(inOption)
2368+
local
2369+
Type_a item;
2370+
2371+
case SOME(item) then {item};
2372+
else {};
23672373
end match;
23682374
end genericOption;
23692375

0 commit comments

Comments
 (0)