Skip to content

Commit

Permalink
- Added check for invalid modifiers on external objects.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@20362 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed May 5, 2014
1 parent 4e6148b commit d3c6bd8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
7 changes: 4 additions & 3 deletions Compiler/FrontEnd/Inst.mo
Expand Up @@ -2213,17 +2213,17 @@ algorithm
then
fail();*/

// This case instantiates external objects. An external object inherits from ExternalOBject
// This case instantiates external objects. An external object inherits from ExternalObject
// and have two local functions: constructor and destructor (and no other elements).
case (cache,env,ih,store,_,_,ci_state,_,
case (cache,env,ih,store,mods,_,ci_state,_,
SCode.PARTS(elementLst = els,
normalEquationLst = _, initialEquationLst = _,
normalAlgorithmLst = _, initialAlgorithmLst = _),
_,_,_,_,_,impl,_,graph,_,_,_,_)
equation
false = Util.getStatefulBoolean(stopInst);
true = SCode.isExternalObject(els);
(cache,env,ih,dae,ci_state) = InstFunction.instantiateExternalObject(cache,env,ih,els,impl);
(cache,env,ih,dae,ci_state) = InstFunction.instantiateExternalObject(cache,env,ih,els,mods,impl);
then
(cache,env,ih,store,dae,inSets,ci_state,{},NONE(),NONE(),NONE(),graph);

Expand All @@ -2237,6 +2237,7 @@ algorithm
re,_,_,_,inst_dims,impl,callscope,graph,csets,_,_,_)
equation
false = Util.getStatefulBoolean(stopInst);
false = SCode.isExternalObject(els);
Debug.bcall0(Flags.getConfigBool(Flags.UNIT_CHECKING),UnitParserExt.checkpoint);
//Debug.traceln(" Instclassdef for: " +& PrefixUtil.printPrefixStr(pre) +& "." +& className +& " mods: " +& Mod.printModStr(mods));
ci_state1 = ClassInf.trans(ci_state, ClassInf.NEWDEF());
Expand Down
52 changes: 41 additions & 11 deletions Compiler/FrontEnd/InstFunction.mo
Expand Up @@ -180,14 +180,15 @@ public function instantiateExternalObject
input Env.Env env "environment";
input InnerOuter.InstHierarchy inIH;
input list<SCode.Element> els "elements";
input DAE.Mod inMod;
input Boolean impl;
output Env.Cache outCache;
output Env.Env outEnv;
output InnerOuter.InstHierarchy outIH;
output DAE.DAElist dae "resulting dae";
output ClassInf.State ciState;
algorithm
(outCache,outEnv,outIH,dae,ciState) := matchcontinue(inCache,env,inIH,els,impl)
(outCache,outEnv,outIH,dae,ciState) := matchcontinue(inCache,env,inIH,els,inMod,impl)
local
SCode.Element destr,constr;
Env.Env env1;
Expand All @@ -200,13 +201,14 @@ algorithm
InstanceHierarchy ih;
DAE.ElementSource source "the origin of the element";
// Explicit instantiation, generate constructor and destructor and the function type.
case (cache,_,ih,_,false)
case (cache,_,ih,_,_,false)
equation
className=Env.getClassName(env); // The external object classname is in top frame of environment.
checkExternalObjectMod(inMod, className);
destr = SCode.getExternalObjectDestructor(els);
constr = SCode.getExternalObjectConstructor(els);
(cache,ih) = instantiateExternalObjectDestructor(cache,env,ih,destr);
(cache,ih,functp) = instantiateExternalObjectConstructor(cache,env,ih,constr);
className=Env.getClassName(env); // The external object classname is in top frame of environment.
SOME(classNameFQ)= Env.getEnvPath(env); // Fully qualified classname
// Extend the frame with the type, one frame up at the same place as the class.
f::fs = env;
Expand All @@ -219,20 +221,49 @@ algorithm
(cache,env1,ih,DAE.DAE({DAE.EXTOBJECTCLASS(classNameFQ,source)}),ClassInf.EXTERNAL_OBJ(classNameFQ));

// Implicit, do not instantiate constructor and destructor.
case (cache,_,ih,_,true)
case (cache,_,ih,_,_,true)
equation
SOME(classNameFQ)= Env.getEnvPath(env); // Fully qualified classname
then
(cache,env,ih,DAE.emptyDae,ClassInf.EXTERNAL_OBJ(classNameFQ));

// failed
case (_,_,_,_,_)
else
equation
print("Inst.instantiateExternalObject failed\n");
Debug.fprintln(Flags.FAILTRACE, "- InstFunction.instantiateExternalObject failed.");
then fail();
end matchcontinue;
end instantiateExternalObject;

protected function checkExternalObjectMod
"Checks that an external object instance does not have any modifiers. This is
done because an external object may only have two elements, a constructor and
a destructor, and there's no point in modifying these."
input DAE.Mod inMod;
input String inClassName;
algorithm
_ := match(inMod, inClassName)
local
DAE.Ident id;
DAE.Mod mod;
Absyn.Info info;

case (DAE.NOMOD(), _) then ();
case (DAE.MOD(subModLst = {}), _) then ();

// The modifier contains a list of submods. Print an error for the first one
// to make it look like a normal modifier error.
case (DAE.MOD(subModLst = DAE.NAMEMOD(ident = id, mod = mod) :: _), _)
equation
info = Mod.getModInfo(mod);
Error.addSourceMessage(Error.MISSING_MODIFIED_ELEMENT,
{id, inClassName}, info);
then
fail();

end match;
end checkExternalObjectMod;

protected function instantiateExternalObjectDestructor
"instantiates the destructor function of an external object"
input Env.Cache inCache;
Expand All @@ -253,10 +284,9 @@ algorithm
(cache,_,ih) = implicitFunctionInstantiation(cache,env,ih,DAE.NOMOD(),Prefix.NOPRE(),cl,{});
then
(cache,ih);
// failure
case (_,_,_,_)
else
equation
print("Inst.instantiateExternalObjectDestructor failed\n");
Debug.fprintln(Flags.FAILTRACE, "- InstFunction.instantiateExternalObjectDestructor failed.");
then fail();
end matchcontinue;
end instantiateExternalObjectDestructor;
Expand Down Expand Up @@ -284,9 +314,9 @@ algorithm
(cache,ty,_) = Lookup.lookupType(cache,env1,Absyn.IDENT("constructor"),NONE());
then
(cache,ih,ty);
case (_,_,_,_)
else
equation
print("Inst.instantiateExternalObjectConstructor failed\n");
Debug.fprintln(Flags.FAILTRACE, "- InstFunction.instantiateExternalObjectConstructor failed.");
then fail();
end matchcontinue;
end instantiateExternalObjectConstructor;
Expand Down

0 comments on commit d3c6bd8

Please sign in to comment.