Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Generate function definitions in list(OpenModelica...) again
- Cache the initial SCode.Program


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@21070 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jun 12, 2014
1 parent 2782105 commit c571ce2
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 123 deletions.
38 changes: 22 additions & 16 deletions Compiler/FrontEnd/Builtin.mo
Expand Up @@ -667,8 +667,8 @@ algorithm
env = initialEnvMetaModelica(env);

// add the builtin classes from ModelicaBuiltin.mo and MetaModelicaBuiltin.mo
Absyn.PROGRAM(classes=initialClasses) = getInitialFunctions();
env = Env.extendFrameClasses(env, listReverse(List.fold(initialClasses, SCodeUtil.translate2, {})), SOME(Env.BUILTIN()));
(_,initialProgram) = getInitialFunctions();
env = Env.extendFrameClasses(env, initialProgram, SOME(Env.BUILTIN()));
cache = Env.setCachedInitialEnv(cache,env);
_ = getSetInitialEnv(SOME(env));
then
Expand Down Expand Up @@ -698,12 +698,15 @@ end initialEnvMetaModelica;
public function getInitialFunctions
"Fetches the Absyn.Program representation of the functions (and other classes) in the initial environment"
output Absyn.Program initialProgram;
output SCode.Program initialSCodeProgram;
algorithm
initialProgram := matchcontinue ()
(initialProgram,initialSCodeProgram) := matchcontinue ()
local
String fileModelica,fileMetaModelica,fileParModelica;
list<tuple<Integer,Absyn.Program>> assocLst;
list<tuple<Integer,tuple<Absyn.Program,SCode.Program>>> assocLst;
list<Absyn.Class> classes,classes1,classes2;
Absyn.Program p;
SCode.Program sp;
case ()
equation
failure(_ = getGlobalRoot(Global.builtinIndex));
Expand All @@ -712,7 +715,8 @@ algorithm
case ()
equation
assocLst = getGlobalRoot(Global.builtinIndex);
then Util.assoc(Flags.getConfigEnum(Flags.GRAMMAR), assocLst);
((p,sp)) = Util.assoc(Flags.getConfigEnum(Flags.GRAMMAR), assocLst);
then (p,sp);
case ()
equation
true = intEq(Flags.getConfigEnum(Flags.GRAMMAR), Flags.METAMODELICA);
Expand All @@ -723,10 +727,11 @@ algorithm
Absyn.PROGRAM(classes=classes1,within_=Absyn.TOP()) = Parser.parsebuiltin(fileModelica,"UTF-8");
Absyn.PROGRAM(classes=classes2,within_=Absyn.TOP()) = Parser.parsebuiltin(fileMetaModelica,"UTF-8");
classes = listAppend(classes1,classes2);
initialProgram = Absyn.PROGRAM(classes,Absyn.TOP(),Absyn.dummyTimeStamp);
p = Absyn.PROGRAM(classes,Absyn.TOP(),Absyn.dummyTimeStamp);
sp = List.map(classes, SCodeUtil.translateClass);
assocLst = getGlobalRoot(Global.builtinIndex);
setGlobalRoot(Global.builtinIndex, (Flags.METAMODELICA,initialProgram)::assocLst);
then initialProgram;
setGlobalRoot(Global.builtinIndex, (Flags.METAMODELICA,(p,sp))::assocLst);
then (p,sp);
case ()
equation
true = intEq(Flags.getConfigEnum(Flags.GRAMMAR), Flags.PARMODELICA);
Expand All @@ -737,19 +742,21 @@ algorithm
Absyn.PROGRAM(classes=classes1,within_=Absyn.TOP()) = Parser.parsebuiltin(fileModelica,"UTF-8");
Absyn.PROGRAM(classes=classes2,within_=Absyn.TOP()) = Parser.parsebuiltin(fileParModelica,"UTF-8");
classes = listAppend(classes1,classes2);
initialProgram = Absyn.PROGRAM(classes,Absyn.TOP(),Absyn.dummyTimeStamp);
p = Absyn.PROGRAM(classes,Absyn.TOP(),Absyn.dummyTimeStamp);
sp = List.map(classes, SCodeUtil.translateClass);
assocLst = getGlobalRoot(Global.builtinIndex);
setGlobalRoot(Global.builtinIndex, (Flags.PARMODELICA,initialProgram)::assocLst);
then initialProgram;
setGlobalRoot(Global.builtinIndex, (Flags.PARMODELICA,(p,sp))::assocLst);
then (p,sp);
case ()
equation
true = intEq(Flags.getConfigEnum(Flags.GRAMMAR), Flags.MODELICA) or intEq(Flags.getConfigEnum(Flags.GRAMMAR), Flags.OPTIMICA);
fileModelica = Settings.getInstallationDirectoryPath() +& "/lib/omc/ModelicaBuiltin.mo";
Error.assertionOrAddSourceMessage(System.regularFileExists(fileModelica),Error.FILE_NOT_FOUND_ERROR,{fileModelica},Absyn.dummyInfo);
initialProgram = Parser.parsebuiltin(fileModelica,"UTF-8");
(p as Absyn.PROGRAM(classes=classes)) = Parser.parsebuiltin(fileModelica,"UTF-8");
sp = List.map(classes, SCodeUtil.translateClass);
assocLst = getGlobalRoot(Global.builtinIndex);
setGlobalRoot(Global.builtinIndex, (Flags.MODELICA,initialProgram)::assocLst);
then initialProgram;
setGlobalRoot(Global.builtinIndex, (Flags.MODELICA,(p,sp))::assocLst);
then (p,sp);
else
equation
Error.addMessage(Error.INTERNAL_ERROR, {"Builtin.getInitialFunctions failed."});
Expand Down Expand Up @@ -869,8 +876,7 @@ algorithm

graph = initialFGraphMetaModelica(graph);

Absyn.PROGRAM(classes=initialClasses) = getInitialFunctions();
initialProgram = listReverse(List.fold(initialClasses, SCodeUtil.translate2, {}));
(_,initialProgram) = getInitialFunctions();
// add the ModelicaBuiltin/MetaModelicaBuiltin classes in the initial graph
graph = FGraphBuild.mkProgramGraph(initialProgram, FCore.BUILTIN(), graph);

Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/NFSCodeEnv.mo
Expand Up @@ -1746,8 +1746,8 @@ algorithm
outInitialEnv := {FRAME(NONE(), NORMAL_SCOPE(), tree, exts, imps, SOME(is_used))};

// add the builtin classes from ModelicaBuiltin.mo and MetaModelicaBuiltin.mo
Absyn.PROGRAM(classes=initialClasses) := Builtin.getInitialFunctions();
outInitialEnv := extendEnvWithClasses(listReverse(List.fold(initialClasses, SCodeUtil.translate2, {})), outInitialEnv);
(_,p) := Builtin.getInitialFunctions();
outInitialEnv := extendEnvWithClasses(p, outInitialEnv);
end buildInitialEnv;

protected function addDummyClassToTree
Expand Down
148 changes: 71 additions & 77 deletions Compiler/FrontEnd/SCode.mo
Expand Up @@ -4064,83 +4064,6 @@ algorithm
end matchcontinue;
end replaceOrAddElementWithId;

public function getElementWithId
"returns the element from the program having the name as the id.
if the element does not exist it fails"
input Program inProgram;
input Ident inId;
output Element outElement;
algorithm
outElement := matchcontinue(inProgram, inId)
local
Program sp, rest;
Element c, e;
Absyn.Path p;
Absyn.Ident i, n;

case ((e as CLASS(name = n))::_, i)
equation
true = stringEq(n, i);
then
e;

case ((e as COMPONENT(name = n))::_, i)
equation
true = stringEq(n, i);
then
e;

case ((e as EXTENDS(baseClassPath = p))::_, i)
equation
true = stringEq(Absyn.pathString(p), i);
then
e;

case (_::rest, i)
then getElementWithId(rest, i);

// not found, fail
case ({}, i)
equation
print("SCode.getElementWithId: an element with name: " +& i +& " was not found in the given program.");
then
fail();
end matchcontinue;
end getElementWithId;

public function getElementWithPath
"returns the element from the program having the name as the id.
if the element does not exist it fails"
input Program inProgram;
input Absyn.Path inPath;
output Element outElement;
algorithm
outElement := match(inProgram, inPath)
local
Program sp, rest;
Element c, e;
Absyn.Path p;
Absyn.Ident i, n;

case (_, Absyn.FULLYQUALIFIED(p))
then getElementWithPath(inProgram, p);

case (_, Absyn.IDENT(i))
equation
e = getElementWithId(inProgram, i);
then
e;

case (_, Absyn.QUALIFIED(i, p))
equation
e = getElementWithId(inProgram, i);
sp = getElementsFromElement(inProgram, e);
e = getElementWithPath(sp, p);
then
e;
end match;
end getElementWithPath;

public function getElementsFromElement
input Program inProgram;
input Element inElement;
Expand Down Expand Up @@ -4281,6 +4204,77 @@ algorithm
end matchcontinue;
end replaceElementsInClassDef;

protected function getElementWithId
"returns the element from the program having the name as the id.
if the element does not exist it fails"
input Program inProgram;
input String inId;
output Element outElement;
algorithm
outElement := matchcontinue(inProgram, inId)
local
Program sp, rest;
Element c, e;
Absyn.Path p;
Absyn.Ident i, n;

case ((e as CLASS(name = n))::_, i)
equation
true = stringEq(n, i);
then
e;

case ((e as COMPONENT(name = n))::_, i)
equation
true = stringEq(n, i);
then
e;

case ((e as EXTENDS(baseClassPath = p))::_, i)
equation
true = stringEq(Absyn.pathString(p), i);
then
e;

case (_::rest, i)
then getElementWithId(rest, i);

end matchcontinue;
end getElementWithId;

public function getElementWithPath
"returns the element from the program having the name as the id.
if the element does not exist it fails"
input Program inProgram;
input Absyn.Path inPath;
output Element outElement;
algorithm
outElement := match (inProgram, inPath)
local
Program sp, rest;
Element c, e;
Absyn.Path p;
Absyn.Ident i, n;

case (_, Absyn.FULLYQUALIFIED(p))
then getElementWithPath(inProgram, p);

case (_, Absyn.IDENT(i))
equation
e = getElementWithId(inProgram, i);
then
e;

case (_, Absyn.QUALIFIED(i, p))
equation
e = getElementWithId(inProgram, i);
sp = getElementsFromElement(inProgram, e);
e = getElementWithPath(sp, p);
then
e;
end match;
end getElementWithPath;

public function getElementName ""
input Element e;
output String s;
Expand Down
27 changes: 26 additions & 1 deletion Compiler/FrontEnd/SCodeUtil.mo
Expand Up @@ -48,15 +48,16 @@ encapsulated package SCodeUtil
public import Absyn;
public import SCode;

protected import Builtin;
protected import Debug;
protected import Error;
protected import Flags;
protected import Inst;
protected import List;
protected import MetaUtil;
protected import SCodeDump;
protected import System;
protected import Util;
protected import SCodeDump;

public function translateAbsyn2SCode
"This function takes an Absyn.Program
Expand Down Expand Up @@ -2618,4 +2619,28 @@ algorithm
outSCodeElementLst := List.map1(inEnumLst, SCode.makeEnumType, info);
end makeEnumComponents;

public function getElementWithPathCheckBuiltin
"returns the element from the program having the name as the id.
if the element does not exist it fails"
input SCode.Program inProgram;
input Absyn.Path inPath;
output SCode.Element outElement;
algorithm
outElement := matchcontinue (inProgram, inPath)
local
SCode.Program sp, rest;
SCode.Element c, e;
Absyn.Path p;
Absyn.Ident i, n;

case (_, _)
then SCode.getElementWithPath(inProgram, inPath);

else
equation
(_,sp) = Builtin.getInitialFunctions();
then SCode.getElementWithPath(sp, inPath);
end matchcontinue;
end getElementWithPathCheckBuiltin;

end SCodeUtil;
20 changes: 12 additions & 8 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -833,7 +833,7 @@ algorithm
Option<list<SCode.Element>> fp;
Env.Env env;
GlobalScript.SymbolTable newst,st_1,st;
Absyn.Program p,pnew,newp,ptot;
Absyn.Program p,ip,pnew,newp,ptot;
list<Absyn.Program> newps;
list<GlobalScript.InstantiatedClass> ic,ic_1;
list<GlobalScript.Variable> iv;
Expand Down Expand Up @@ -958,7 +958,8 @@ algorithm

case (cache,_,"getClassNames",{Values.CODE(Absyn.C_TYPENAME(Absyn.IDENT("AllLoadedClasses"))),Values.BOOL(false),_,Values.BOOL(sort),Values.BOOL(builtin),Values.BOOL(_)},st as GlobalScript.SYMBOLTABLE(ast = p),_)
equation
p = Debug.bcallret2(builtin,Interactive.updateProgram,p,Builtin.getInitialFunctions(),p);
(ip,_) = Builtin.getInitialFunctions();
p = Debug.bcallret2(builtin,Interactive.updateProgram,p,ip,p);
paths = Interactive.getTopClassnames(p);
paths = Debug.bcallret2(sort, List.sort, paths, Absyn.pathGe, paths);
vals = List.map(paths,ValuesUtil.makeCodeTypeName);
Expand All @@ -967,7 +968,8 @@ algorithm

case (cache,_,"getClassNames",{Values.CODE(Absyn.C_TYPENAME(path)),Values.BOOL(false),Values.BOOL(b),Values.BOOL(sort),Values.BOOL(builtin),Values.BOOL(showProtected)},st as GlobalScript.SYMBOLTABLE(ast = p),_)
equation
p = Debug.bcallret2(builtin,Interactive.updateProgram,p,Builtin.getInitialFunctions(),p);
(ip,_) = Builtin.getInitialFunctions();
p = Debug.bcallret2(builtin,Interactive.updateProgram,p,ip,p);
paths = Interactive.getClassnamesInPath(path, p, showProtected);
paths = Debug.bcallret3(b,List.map1r,paths,Absyn.joinPaths,path,paths);
paths = Debug.bcallret2(sort, List.sort, paths, Absyn.pathGe, paths);
Expand All @@ -977,7 +979,8 @@ algorithm

case (cache,_,"getClassNames",{Values.CODE(Absyn.C_TYPENAME(Absyn.IDENT("AllLoadedClasses"))),Values.BOOL(true),_,Values.BOOL(sort),Values.BOOL(builtin),Values.BOOL(showProtected)},st as GlobalScript.SYMBOLTABLE(ast = p),_)
equation
p = Debug.bcallret2(builtin,Interactive.updateProgram,p,Builtin.getInitialFunctions(),p);
(ip,_) = Builtin.getInitialFunctions();
p = Debug.bcallret2(builtin,Interactive.updateProgram,p,ip,p);
(_,paths) = Interactive.getClassNamesRecursive(NONE(),p,showProtected,{});
paths = listReverse(paths);
paths = Debug.bcallret2(sort, List.sort, paths, Absyn.pathGe, paths);
Expand All @@ -987,7 +990,8 @@ algorithm

case (cache,_,"getClassNames",{Values.CODE(Absyn.C_TYPENAME(path)),Values.BOOL(true),_,Values.BOOL(sort),Values.BOOL(builtin),Values.BOOL(showProtected)},st as GlobalScript.SYMBOLTABLE(ast = p),_)
equation
p = Debug.bcallret2(builtin,Interactive.updateProgram,p,Builtin.getInitialFunctions(),p);
(ip,_) = Builtin.getInitialFunctions();
p = Debug.bcallret2(builtin,Interactive.updateProgram,p,ip,p);
(_,paths) = Interactive.getClassNamesRecursive(SOME(path),p,showProtected,{});
paths = listReverse(paths);
paths = Debug.bcallret2(sort, List.sort, paths, Absyn.pathGe, paths);
Expand Down Expand Up @@ -1129,6 +1133,7 @@ algorithm

case (cache,_,"list",{Values.CODE(Absyn.C_TYPENAME(className)),Values.BOOL(b1),Values.BOOL(b2),Values.ENUM_LITERAL(name=path)},(st as GlobalScript.SYMBOLTABLE(ast = p)),_)
equation
false = valueEq(Absyn.IDENT("AllLoadedClasses"),className);
(scodeP,st) = Interactive.symbolTableToSCode(st);
name = Absyn.pathLastIdent(path);
absynClass = Interactive.getPathedClassInProgram(className, p);
Expand All @@ -1137,7 +1142,7 @@ algorithm
p = Absyn.PROGRAM({absynClass},Absyn.TOP(),Absyn.TIMESTAMP(0.0,0.0));
str = Debug.bcallret2(name ==& "Absyn", Dump.unparseStr, p, false, "");
str = Debug.bcallret1(name ==& "Internal", System.anyStringCode, p, str);
cl = SCode.getElementWithPath(scodeP, className);
cl = SCodeUtil.getElementWithPathCheckBuiltin(scodeP, className);
str = Debug.bcallret2(name ==& "SCode", SCodeDump.unparseElementStr, cl, SCodeDump.defaultOptions, str);
str = Debug.bcallret2(name ==& "MetaModelicaInterface", SCodeDump.unparseElementStr, cl, SCodeDump.OPTIONS(true,false,true,true,true), str);
then
Expand Down Expand Up @@ -3464,8 +3469,7 @@ algorithm
// remove extends Modelica.Icons.*
//scodeP = SCodeSimplify.simplifyProgram(scodeP);

p_builtin = Builtin.getInitialFunctions();
scode_builtin = SCodeUtil.translateAbsyn2SCode(p_builtin);
(p_builtin,scode_builtin) = Builtin.getInitialFunctions();

nfenv = NFEnv.buildInitialEnv(scodeP, scode_builtin);
(dae, funcs) = NFInst.instClass(className, nfenv);
Expand Down

0 comments on commit c571ce2

Please sign in to comment.