Skip to content

Commit

Permalink
- Fixed proper handling of encapsulated in SCodeFlatten.
Browse files Browse the repository at this point in the history
- Fixed test mofiles/Sequence and added mofiles/Encapsulated4.
- Detabbed some comments.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7843 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Feb 1, 2011
1 parent 140bca3 commit 5d8d0ec
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 46 deletions.
1 change: 1 addition & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -198,6 +198,7 @@ function homotopy
input Real actual;
input Real simplified;
output Real outValue;
external "builtin";
end homotopy;

// Dummy functions that can't be properly defined in Modelica, but used by
Expand Down
75 changes: 40 additions & 35 deletions Compiler/FrontEnd/SCodeEnv.mo
Expand Up @@ -55,12 +55,12 @@ public type Import = Absyn.Import;

public uniontype ImportTable
record IMPORT_TABLE
// Imports should not be inherited, but removing them from the environment
// when doing lookup through extends causes problems for the lookup later
// on, because for example components may have types that depends on imports.
// The hidden flag allows the lookup to 'hide' the imports temporarily,
// without actually removing them.
Boolean hidden "If true means that the imports are hidden.";
// Imports should not be inherited, but removing them from the environment
// when doing lookup through extends causes problems for the lookup later
// on, because for example components may have types that depends on
// imports. The hidden flag allows the lookup to 'hide' the imports
// temporarily, without actually removing them.
Boolean hidden "If true means that the imports are hidden.";
list<Import> qualifiedImports;
list<Import> unqualifiedImports;
end IMPORT_TABLE;
Expand Down Expand Up @@ -109,6 +109,7 @@ public uniontype Item

record BUILTIN
String name;
Env env;
end BUILTIN;
end Item;

Expand Down Expand Up @@ -278,9 +279,11 @@ public function enterScope
protected
Frame cls_env;
AvlTree cls_and_vars;
Item item;
algorithm
FRAME(clsAndVars = cls_and_vars) :: _ := inEnv;
CLASS(env = {cls_env}) := avlTreeGet(cls_and_vars, inName);
item := avlTreeGet(cls_and_vars, inName);
{cls_env} := getItemEnv(item);
outEnv := cls_env :: inEnv;
end enterScope;

Expand Down Expand Up @@ -353,6 +356,15 @@ algorithm
SCode.ClassDef cdef;
Absyn.Path cls_path;

case (SCode.CLASS(name = cls_name, classDef = cdef as SCode.PARTS(
externalDecl = SOME(Absyn.EXTERNALDECL(lang = SOME("builtin"))))), _)
equation
class_env = openScope(emptyEnv, inClass);
class_env = extendEnvWithClassComponents(cls_name, cdef, class_env);
env = extendEnvWithItem(BUILTIN(cls_name, class_env), inEnv, cls_name);
then
env;

case (SCode.CLASS(classDef = SCode.CLASS_EXTENDS(baseClassName = _)), _)
then addClassExtendsToEnvExtendsTable(inClass, inEnv);

Expand Down Expand Up @@ -384,8 +396,8 @@ algorithm
end removeExtendsFromLocalScope;

public function setImportTableHidden
"Sets the 'hidden' flag in the import table in the local scope of the given
environment."
"Sets the 'hidden' flag in the import table in the local scope of the given
environment."
input Env inEnv;
input Boolean inHidden;
output Env outEnv;
Expand All @@ -405,8 +417,8 @@ algorithm
end setImportTableHidden;

public function setImportsInItemHidden
"Sets the 'hidden' flag in the import table for the given items environment if
the item is a class. Otherwise does nothing."
"Sets the 'hidden' flag in the import table for the given items environment if
the item is a class. Otherwise does nothing."
input Item inItem;
input Boolean inHidden;
output Item outItem;
Expand Down Expand Up @@ -1252,30 +1264,10 @@ algorithm
exts := newExtendsTable();
imps := newImportTable();

tree := avlTreeAdd(tree, "time", VAR(
SCode.COMPONENT("time", Absyn.UNSPECIFIED(), false, false, false,
SCode.ATTR({}, false, false, SCode.RO(), SCode.VAR(), Absyn.INPUT()),
Absyn.TPATH(Absyn.IDENT("Real"), NONE()), SCode.NOMOD(),
NONE(), NONE(), Absyn.dummyInfo, NONE())));

tree := avlTreeAdd(tree, "String", CLASS(
SCode.CLASS("String", false, false, SCode.R_FUNCTION(),
SCode.PARTS({}, {}, {}, {}, {}, NONE(), {}, NONE()), Absyn.dummyInfo),
emptyEnv));

tree := avlTreeAdd(tree, "Integer", CLASS(
SCode.CLASS("Integer", false, false, SCode.R_FUNCTION(),
SCode.PARTS({}, {}, {}, {}, {}, NONE(), {}, NONE()), Absyn.dummyInfo),
emptyEnv));

// Modelica.Fluid.Pipes.BaseClasses.HeatTransfer.LocalPipeFlowHeatTransfer
// tries to call a function called spliceFunction. This seems to be a built-in
// Dymola function, so until this has been fixed we'll just pretend that we
// also have it.
tree := avlTreeAdd(tree, "spliceFunction", CLASS(
SCode.CLASS("spliceFunction", false, false, SCode.R_FUNCTION(),
SCode.PARTS({}, {}, {}, {}, {}, NONE(), {}, NONE()), Absyn.dummyInfo),
emptyEnv));
tree := avlTreeAdd(tree, "time", BUILTIN("time", emptyEnv));
tree := avlTreeAdd(tree, "String", BUILTIN("String", emptyEnv));
tree := avlTreeAdd(tree, "Integer", BUILTIN("Integer", emptyEnv));
tree := avlTreeAdd(tree, "spliceFunction", BUILTIN("spliceFunction", emptyEnv));

outInitialEnv := {FRAME(NONE(), NORMAL_SCOPE(), tree, exts, imps)};
end buildInitialEnv;
Expand Down Expand Up @@ -1538,6 +1530,19 @@ algorithm
end match;
end getItemInfo;

protected function getItemEnv
input Item inItem;
output Env outEnv;
algorithm
outEnv := match(inItem)
local
Env env;

case (CLASS(env = env)) then env;
case (BUILTIN(env = env)) then env;
end match;
end getItemEnv;

public function avlTreeGet
"Get a value from the binary tree given a key."
input AvlTree inAvlTree;
Expand Down
47 changes: 38 additions & 9 deletions Compiler/FrontEnd/SCodeLookup.mo
Expand Up @@ -55,12 +55,18 @@ public type Import = Absyn.Import;

protected import SCodeFlattenImports;

public constant Item BUILTIN_REAL = SCodeEnv.BUILTIN("Real");
public constant Item BUILTIN_INTEGER = SCodeEnv.BUILTIN("Integer");
public constant Item BUILTIN_BOOLEAN = SCodeEnv.BUILTIN("Boolean");
public constant Item BUILTIN_STRING = SCodeEnv.BUILTIN("String");
public constant Item BUILTIN_STATESELECT = SCodeEnv.BUILTIN("StateSelect");
public constant Item BUILTIN_EXTERNALOBJECT = SCodeEnv.BUILTIN("ExternalObject");
public constant Item BUILTIN_REAL =
SCodeEnv.BUILTIN("Real", SCodeEnv.emptyEnv);
public constant Item BUILTIN_INTEGER =
SCodeEnv.BUILTIN("Integer", SCodeEnv.emptyEnv);
public constant Item BUILTIN_BOOLEAN =
SCodeEnv.BUILTIN("Boolean", SCodeEnv.emptyEnv);
public constant Item BUILTIN_STRING =
SCodeEnv.BUILTIN("String", SCodeEnv.emptyEnv);
public constant Item BUILTIN_STATESELECT =
SCodeEnv.BUILTIN("StateSelect", SCodeEnv.emptyEnv);
public constant Item BUILTIN_EXTERNALOBJECT =
SCodeEnv.BUILTIN("ExternalObject", SCodeEnv.emptyEnv);

public function lookupSimpleName
"Looks up a simple identifier in the environment and returns the environment
Expand Down Expand Up @@ -108,6 +114,17 @@ algorithm
then
(opt_item, opt_path, opt_env);

// If the current frame is encapsulated, check for builtin types and
// functions in the top scope.
case (_, SCodeEnv.FRAME(frameType = SCodeEnv.ENCAPSULATED_SCOPE()) ::
rest_env)
equation
rest_env = SCodeEnv.getEnvTopScope(rest_env);
(opt_item, opt_path, opt_env) = lookupSimpleName2(inName, rest_env);
checkBuiltinItem(opt_item);
then
(opt_item, opt_path, opt_env);

end matchcontinue;
end lookupSimpleName2;

Expand All @@ -121,6 +138,18 @@ algorithm
end match;
end frameNotEncapsulated;

protected function checkBuiltinItem
input Option<Item> inItem;
algorithm
_ := match(inItem)
local
String name;

case (SOME(SCodeEnv.BUILTIN(name = _))) then ();
case (NONE()) then ();
end match;
end checkBuiltinItem;

public function lookupInLocalScope
"Looks up a simple identifier in the environment. Returns SOME(item) if an
item is found, NONE() if a partial match was found (for example when the name
Expand Down Expand Up @@ -237,8 +266,8 @@ algorithm
equation
// Find the base class.
(item, _, SOME(env)) = lookupBaseClassName(bc, inEnv, info);
// Hide the imports to make sure that we don't find the name via them
// (imports are not inherited).
// Hide the imports to make sure that we don't find the name via them
// (imports are not inherited).
item = SCodeEnv.setImportsInItemHidden(item, true);
// Look in the base class.
(item, env) = SCodeEnv.replaceRedeclaredClassesInEnv(redecls, item, env, inEnv);
Expand Down Expand Up @@ -806,7 +835,7 @@ algorithm

// A MetaModelica type such as list or tuple.
case (Absyn.TCOMPLEX(path = Absyn.IDENT(name = name)), _, _)
then (SCodeEnv.BUILTIN(name), SCodeEnv.emptyEnv);
then (SCodeEnv.BUILTIN(name, SCodeEnv.emptyEnv), SCodeEnv.emptyEnv);

end match;
end lookupTypeSpec;
Expand Down
11 changes: 10 additions & 1 deletion Compiler/FrontEnd/Static.mo
Expand Up @@ -6456,7 +6456,7 @@ algorithm
list<Absyn.NamedArg> nargs;
Boolean impl;
Absyn.Path fn_1;
Ident fnstr,argstr,prestr,s,name;
Ident fnstr,argstr,prestr,s,name,env_str;
list<Ident> argstrs;
Env.Cache cache;
Prefix.Prefix pre;
Expand Down Expand Up @@ -6484,6 +6484,15 @@ algorithm
Error.addSourceMessage(Error.WRONG_TYPE_OR_NO_OF_ARGS, {s,prestr}, info);
then fail();

case (_, _, Absyn.CREF_INVALID(componentRef = fn), _, _, _, _, _, _, _)
equation
fnstr = Absyn.printComponentRefStr(fn);
env_str = Env.printEnvPathStr(inEnv);
Error.addSourceMessage(Error.LOOKUP_FUNCTION_ERROR,
{fnstr, env_str}, info);
then
fail();

/* Interactive mode */
case (cache,env,fn,args,nargs,(impl as true),st,pre,info,numErrorMessages)
equation
Expand Down
5 changes: 4 additions & 1 deletion Compiler/Util/Error.mo
Expand Up @@ -249,7 +249,8 @@ public constant ErrorID DOUBLE_DECLARATION_OF_ELEMENTS=161;
public constant ErrorID INVALID_REDECLARATION_OF_CLASS=162;
public constant ErrorID MULTIPLE_QUALIFIED_IMPORTS_WITH_SAME_NAME=163;
public constant ErrorID EXTENDS_INHERITED_FROM_LOCAL_EXTENDS=164;
public constant ErrorID ELAB_CODE_EXP_FAILED=165;
public constant ErrorID LOOKUP_FUNCTION_ERROR=165;
public constant ErrorID ELAB_CODE_EXP_FAILED=166;

public constant ErrorID UNBOUND_PARAMETER_WITH_START_VALUE_WARNING=499;
public constant ErrorID UNBOUND_PARAMETER_WARNING=500;
Expand Down Expand Up @@ -699,6 +700,8 @@ protected constant list<tuple<Integer, MessageType, Severity, String>> errorTabl
"Qualified import name %s already exists in this scope."),
(EXTENDS_INHERITED_FROM_LOCAL_EXTENDS,TRANSLATION(),ERROR(),
"Extends %s depends on inherited element %s."),
(LOOKUP_FUNCTION_ERROR,TRANSLATION(),ERROR(),
"Function %s not found in scope %s."),
(MATCHCONTINUE_TO_MATCH_OPTIMIZATION,TRANSLATION(),NOTIFICATION(),"This matchcontinue expression has no overlapping patterns and should be using match instead of matchcontinue."),
(META_DEAD_CODE,TRANSLATION(),NOTIFICATION(),"Dead code elimination: %s."),
(META_UNUSED_DECL,TRANSLATION(),NOTIFICATION(),"Unused local variable: %s."),
Expand Down

0 comments on commit 5d8d0ec

Please sign in to comment.