Skip to content

Commit

Permalink
Fixed instantiation bug with extends.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@915 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Feb 6, 2003
1 parent dbfa87c commit fe6946b
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 70 deletions.
5 changes: 2 additions & 3 deletions modeq/classloader.rml
Expand Up @@ -99,9 +99,8 @@ relation load_class_from_mp: (Absyn.Ident, string) => Absyn.Program =
exist_directory_file dirfile &
exist_regular_file packfile &
Print.print_buf "Class is package stored in a directory, loading whole package(incl. subdir)\n" &
load_complete_package_from_mp(class,mp,Absyn.TOP,Absyn.PROGRAM([],Absyn.TOP)) => p &
Interactive.build_env_from_symboltable(Interactive.SYMBOLTABLE(p,[],[],[],[])) => env &
Lookup.lookup_class(env,Absyn.IDENT(class),true) => (_,_)
load_complete_package_from_mp(class,mp,Absyn.TOP,
Absyn.PROGRAM([],Absyn.TOP)) => p
--------------------------------------
load_class_from_mp(class,mp) => p

Expand Down
2 changes: 1 addition & 1 deletion modeq/dae.rml
Expand Up @@ -268,7 +268,7 @@ end
relation dump_comp_element_str : Element => string =

rule RTOpts.modelica_output => false &
string_append("fclass",n) => s1 &
string_append("fclass ",n) => s1 &
string_append(s1,"\n") => s2 &
dump_elements_str(l) => s3 &
string_append(s2,s3) => s4 &
Expand Down
2 changes: 1 addition & 1 deletion modeq/dump.rml
Expand Up @@ -593,7 +593,7 @@ relation unparse_elementspec_str: Absyn.ElementSpec => string =
-------------------------
unparse_elementspec_str(Absyn.CLASSDEF(repl,cl)) => str

rule Print.print_buf "EXTENDS(" & Absyn.path_string p => s1 &
rule Absyn.path_string p => s1 &
string_append("extends ",s1) => s2 &
get_string_list(l,unparse_element_arg_str,",") => s3 &
string_append(s2,s3) => str
Expand Down
63 changes: 57 additions & 6 deletions modeq/env.rml
Expand Up @@ -67,10 +67,10 @@ module Env:
relation extend_frame_t : (Env,Ident,Types.Type) => Env
relation extend_frame_i : (Env, Absyn.Import) => Env
relation top_frame : Env => Frame

relation merge_inherited_content : (Frame, Env) => Env

relation print_env : Env => ()

relation print_env_graphviz : (Env * string) => ()

relation print_frame : Frame => ()
Expand Down Expand Up @@ -187,9 +187,23 @@ relation update_frame_v : (Env,Types.Var,bool) => Env =
update_frame_v(FRAME((id,VAR(v1 as Types.VAR(v,_,_,_,_),c,i1))::rest,encflag)::rest2,v2 as Types.VAR(n,_,_,_,_),i)
=> FRAME((id,VAR(v1,c,i1))::res,encflag)::reslst


axiom update_frame_v(FRAME([],encflag)::fs,v as Types.VAR(n,_,_,_,_),_)
=> (FRAME([],encflag)::fs)
rule update_frame_v(FRAME(rest,encflag)::rest2,v2,i) => FRAME(res,encflag)::reslst
-------------------------------------------
update_frame_v(FRAME((id,notvar)::rest,encflag)::rest2,v2 as Types.VAR(n,_,_,_,_),i)
=> FRAME((id,notvar)::res,encflag)::reslst

rule Print.print_buf "- update_frame_v, variable " & Print.print_buf n &
Print.print_buf " not found\n rest of env:" &
print_env fs & Print.print_buf "\n"
--------------
update_frame_v(FRAME([],encflag)::fs,v as Types.VAR(n,_,_,_,_),_)
=> (FRAME([],encflag)::fs)

rule Print.print_buf "- update_frame_v failed\n" &
Print.print_buf " - variable: " & Types.print_var v &
Print.print_buf "\n"
----------------
update_frame_v(_,v as Types.VAR(id,_,_,_,_),_) => fail
end

(** relation: extend_frame_t
Expand All @@ -200,7 +214,7 @@ end
** there is a class definition with the same name in the
** environment.
**)

relation extend_frame_t : (Env,Ident,Types.Type) => Env =

(*
Expand Down Expand Up @@ -234,6 +248,43 @@ relation top_frame : Env => Frame =
top_frame(elt::(lst as (_::_))) => fr
end

(** relation: merge_inherited_content
** This relation is used when instantiating inherited classes.
** It takes a `Frame� ,from the in instantiated base class, and an environment `Env�,
** for the class containing the extends element and returns an updated environment `Env�
** An inherited class is first looked up, resulting in the environment for the
** inherited class. Then, this class is instantiated, giving a new environment where
** the top frame contains all variables, local classes, types and import statements of
** the class to inherit. Then, the variables, local classes and types should be inserted into
** the environment,top frame, of the class containing the extends element.
**)

relation merge_inherited_content: (Frame, Env) => Env =

axiom merge_inherited_content(FRAME([],enc),env) => env

rule merge_inherited_content(FRAME(rest,enc),FRAME(lst,enc2)::restenv) => FRAME(lst',enc')::restenv'
--------------------------------------------------
merge_inherited_content(FRAME((id,VAR(t,el,b))::rest,enc),FRAME(lst,enc2)::restenv)
=> FRAME((id,VAR(t,el,b))::lst',enc')::restenv'

rule merge_inherited_content(FRAME(rest,enc),FRAME(lst,enc2)::restenv) => FRAME(lst',enc')::restenv'
--------------------------------------------------
merge_inherited_content(FRAME((id,CLASS(cl,env))::rest,enc),FRAME(lst,enc2)::restenv)
=> FRAME((id,CLASS(cl,env))::lst',enc')::restenv'

rule merge_inherited_content(FRAME(rest,enc),FRAME(lst,enc2)::restenv) => FRAME(lst',enc')::restenv'
--------------------------------------------------
merge_inherited_content(FRAME((id,TYPE(t))::rest,enc),FRAME(lst,enc2)::restenv)
=> FRAME((id,TYPE(t))::lst',enc')::restenv'

rule (* imports are not inherited *)
merge_inherited_content(FRAME(rest,enc),FRAME(lst,enc2)::restenv) => FRAME(lst',enc')::restenv'
--------------------------------------------------
merge_inherited_content(FRAME((id,IMPORT(imp))::rest,enc),FRAME(lst,enc2)::restenv)
=> FRAME(lst',enc')::restenv'
end

relation print_env : Env => () =

rule Print.print_buf "Empty env\n"
Expand Down
70 changes: 48 additions & 22 deletions modeq/inst.rml
Expand Up @@ -199,19 +199,16 @@ relation instantiate_class : (SCode.Program, SCode.Path) => DAE.DAElist =
instantiate_class(cdecls, path as Absyn.IDENT(name2)) => DAE.DAE([DAE.COMP(name2,DAE.DAE(dae))])

rule Builtin.initial_env => env &
print "calling inst_class_decls.. \n" &
inst_class_decls(env,cdecls,path)=> (env',_) &
print "done \n" &
Lookup.lookup_class(env',path,true) => (cdef, env'') &
Lookup.lookup_class(env',path,true) => (cdef as SCode.CLASS(n,_,_,_,_), env'') &
inst_class(env'',Mod.NOMOD,Prefix.NOPRE,[],cdef,[],false) => (dae,_,_,_) &
Absyn.path_string path => pathstr
--------------------------------------------
instantiate_class(cdecls, path as Absyn.QUALIFIED(name,_))
=> DAE.DAE([DAE.COMP(pathstr,DAE.DAE(dae))])


rule Print.print_buf "- instantiate_class failed\n" &
print "instantiate_class failed\n"
rule Print.print_buf "- instantiate_class failed\n"
------------
instantiate_class(_,_) => fail
end
Expand Down Expand Up @@ -517,12 +514,16 @@ relation inst_element_list : (Env, Mod, Prefix, Connect.Sets, ClassInf.State,
extends_elts(elts) => extendselts &
(* 1. CLASSDEF & IMPORT nodes and COMPONENT nodes(add to env)*)
inst_element_list2(env,mod,pre,csets,ci_state,cdefelts,inst_dims, impl) => (_,env',csets',ci_state',tys) &

Debug.fprint ("decl", "calling extend_components_to_env, env=\n") &
Debug.fcall ("decl",Env.print_env, env') &
(* print "calling extend_components_to_env, env=" &
Print.clear_buf& Env.print_env env' & Print.get_string => str & print str & print "\n" &
Debug.fprint ("extend", "calling extend_components_to_env, env=\n") & *)
Debug.fcall ("extend",Env.print_env, env') &
extend_components_to_env(env',mod,pre,csets,ci_state,compelts,inst_dims,impl) => env'' &
Debug.fprint("decl"," extended components to env, env=") &
Debug.fcall ("decl",Env.print_env, env'') &
(* print "updated extend_components_to_env, env=" &
Print.clear_buf& Env.print_env env'' & Print.get_string => str & print str & print "\n" &
print "prefix = " & Prefix.print_prefix_str pre => prefixstr & print prefixstr & print "\n" & *)
Debug.fprint("extend"," extended components to env, env=") &
Debug.fcall ("extend",Env.print_env, env'') &
(*2. EXTENDS Nodes*)
inst_element_list2(env'',mod,pre,csets',ci_state',extendselts,inst_dims,impl) => (dae2,env''',csets'',ci_state'',tys2) &

Expand All @@ -537,6 +538,10 @@ relation inst_element_list : (Env, Mod, Prefix, Connect.Sets, ClassInf.State,
----------------------------------------------------------------------------------------------------
inst_element_list(env,mod,pre,csets,ci_state,elts,inst_dims, impl)
=> (totdae,env'''',csets''',ci_state''',tottys)

rule Print.print_buf "- inst_element_list failed\n"
---------------------
inst_element_list(_,_,_,_,_,_,_,_) => fail
end


Expand Down Expand Up @@ -613,6 +618,11 @@ relation extend_components_to_env : (Env, Mod, Prefix, Connect.Sets, ClassInf.St
attr as SCode.ATTR(ad,flow,acc,param,dir),
t,m))::xs,inst_dims,impl) => env''
axiom extend_components_to_env(env,_,_,_,_,[],_,_) => env

rule Print.print_buf "- extend_components_to_env failed\n" &
Print.print_buf " - env: \n" & Env.print_env env & Print.print_buf "\n"
--------------------
extend_components_to_env(env,_,_,_,_,_,_,_) => fail
end

relation inst_element_list2 : (Env, Mod, Prefix, Connect.Sets, ClassInf.State,
Expand Down Expand Up @@ -688,11 +698,17 @@ relation inst_element : (Env, Mod, Prefix, Connect.Sets, ClassInf.State,
Debug.fprintln ("insttr", "Modifications merged again") &

(* Can't use inst_class, as that creates a new frame *)
(* The extends node is instantiated in the environment beloning to *)
(* extended class, i.e. cenv. However, the variables, classes and types inserted in this *)
(* environment (top frame of env'), by inst_class_in, should be *)
(* copied into the resulting environment *)

inst_class_in(cenv,mods'',pre,csets,ci_state,c,false,inst_dims,impl)
=> (dae,env',csets',ci_state',vars)
=> (dae,fr::env',csets',ci_state',vars) &
Env.merge_inherited_content(fr,env) => env''
----------------------------------
inst_element(env,mods,pre,csets,ci_state,SCode.EXTENDS(cn,m),inst_dims,impl)
=> (dae,env',csets',ci_state',vars)
=> (dae,env'',csets',ci_state',vars)

(** Rules to catch redeclarations and name collsions
**)
Expand Down Expand Up @@ -797,7 +813,7 @@ relation inst_element : (Env, Mod, Prefix, Connect.Sets, ClassInf.State,
(* The types in the environment does not have correct Binding.
We must update those variables that is found in m into a new environment.*)
get_cref_from_mod(m) => crefs &
update_variables_in_env(mods,crefs,env,impl) => env2' &
update_variables_in_env(mods,crefs, env ,impl) => env2' &

get_cref_from_dim(ad) => crefs2 &
update_variables_in_env(mods,crefs2,env2',impl) => env2 &
Expand All @@ -823,9 +839,7 @@ relation inst_element : (Env, Mod, Prefix, Connect.Sets, ClassInf.State,
Debug.fprintln ("insttr", "mod equation done") &
(** The variable declaration and the (optional) equation *)
(** modification are inspected for array dimensions. *)
Print.get_string () => errstr & Print.clear_buf() &
elab_arraydim(env2,ad,eq,impl,NONE) => dims &
Print.clear_buf() & Print.print_buf errstr &
Debug.fprintln ("insttr", "arraydim elabed") &
(** Instantiate the component *)
inst_var(cenv,mod',pre,csets,n,cl,attr,dims,[],inst_dims,impl)
Expand Down Expand Up @@ -970,6 +984,10 @@ relation get_cref_from_mod: SCode.Mod => Absyn.ComponentRef list =
get_cref_from_mod(SCode.MOD(_,submods,NONE)) => res

axiom get_cref_from_mod(_) => []

rule Print.print_buf "- get_cref_from_mod failed\n"
----------
get_cref_from_mod(_) => fail
end

relation get_cref_from_dim: Absyn.ArrayDim => Absyn.ComponentRef list =
Expand All @@ -985,6 +1003,10 @@ relation get_cref_from_dim: Absyn.ArrayDim => Absyn.ComponentRef list =
get_cref_from_dim(Absyn.NOSUB::rest) => res

axiom get_cref_from_dim([]) => []

rule Print.print_buf "- get_cref_from_dim failed\n"
------------
get_cref_from_dim(_) => fail
end

relation get_cref_from_submods: (SCode.SubMod list) => Absyn.ComponentRef list =
Expand Down Expand Up @@ -1139,10 +1161,7 @@ relation update_variable_in_env: (Mod.Mod,Absyn.ComponentRef, Env.Env, bool)

(** The variable declaration and the (optional) equation *)
(** modification are inspected for array dimensions. *)
Print.get_string () => errstr & Print.clear_buf() &
elab_arraydim(env2,ad,eq,impl,NONE) => dims &
Print.clear_buf() & Print.print_buf errstr &
print_dims dims &

(** Instantiate the component *)
inst_var(env2,mod',Prefix.NOPRE,[],n,cl,attr,dims,[],[],false)
Expand Down Expand Up @@ -1575,11 +1594,12 @@ relation inst_class_decl : (Env.Env, Mod.Mod, Prefix.Prefix,
Connect.Sets, SCode.Class,InstDims)
=> (Env.Env, DAE.Element list) =

rule Debug.fprint("insttr","inst_class_decl called\n") &
rule Debug.fprint("insttr","+\tinst_class_decl called\n") &
Env.extend_frame_c(env,c) => env' &
Debug.fprint("insttr","extended frame\n") &
Debug.fprint("insttr","+\textended frame\n") &
implicit_instantiation(env',Mod.NOMOD,pre,csets,c,inst_dims) => (env'',dae) &
Debug.fprint("insttr","inst_class_def finished\n")
Debug.fprint("insttr","+\timplicit instantiation of ") &
Debug.fprint("insttr",n) & Debug.fprint("insttr", " finshed\n\n")
----------------------------------
inst_class_decl(env,mod,pre,csets,c as SCode.CLASS(n,_,_,restr,_),inst_dims)
=> (env'', dae)
Expand All @@ -1591,6 +1611,7 @@ relation inst_class_decl : (Env.Env, Mod.Mod, Prefix.Prefix,
end

(** relation implicit_instantiation
** This relation adds types to the environment.
**
** If a class definition is a function or a package or an enumeration ,
** it is implicitly instantiated and added as a type binding under the
Expand All @@ -1604,13 +1625,17 @@ relation implicit_instantiation : (Env.Env, Mod.Mod, Prefix.Prefix,
(* LS:This rule extends the frame with the package as a type, but
inst_class also extends the frame with the package as a class. Is this
correct?
* PA: No, since we have now a special relation for looking up variables, first checking
* the types and thereafter checking classes.
*)

(* Removed
rule Prefix.prefix_add(n,[],pre) => pre' &
(* let pre' = pre & *)
Debug.fprint ("insttr","implicit_instantiation called.\n") &
print "implicit_instantiation of " & print n & print " called.\n" &
inst_class(env,mod,pre',csets,c,inst_dims,true) => (dae, csets', ty, st) &
Debug.fprint ("insttr","implicit_instantiation inst class done.\n") &
print "implicit_instantiation of " & print n & print " class done.\n" &
Env.extend_frame_t(env, n, ty) => env' &
(* let env' = env & *)
Debug.fprintl ("insttr", ["implicit_instantiation done:", n, "\n"])
Expand All @@ -1619,6 +1644,7 @@ correct?
c as SCode.CLASS(n,_,_,SCode.R_PACKAGE,_),
inst_dims)
=> (env', dae)
Removed *)

(* LS: changed from returning empty dae to returning a DAE.FUNCTION *)
(* LS: removed prefix because function elements shouldn't have
Expand Down
20 changes: 12 additions & 8 deletions modeq/interactive.rml
Expand Up @@ -116,16 +116,12 @@ relation evaluate: (InteractiveStmts, InteractiveSymbolTable) => (string,Intera

rule SCode.elaborate(p) => p' &
Absyn.cref_to_path(cr) => path &
print "elaborated\n" &
Inst.instantiate_class(p',path) => dae &
DAE.dump_debug dae &
Print.print_buf "dumped dae:\n" &
DAE.dump_str (dae) => str &
Print.get_string => s2 &
string_append(s2,str) => str'
Print.print_buf "instanitated\n" &
DAE.dump_str (dae) => str
--------------------------------------
evaluate(ISTMTS([IEXP(Absyn.CALL(Absyn.CREF_IDENT("translateClass",_),Absyn.FUNCTIONARGS([ Absyn.CREF(cr)],[])))]),st as SYMBOLTABLE(p,_,_,_,_))
=> (str',st)
=> (str,st)

rule Print.get_string() => s1 &
string_append("#Error instanitating class\n",s1) => str
Expand Down Expand Up @@ -366,6 +362,15 @@ end

relation evaluate_graphical_api: (InteractiveStmts, InteractiveSymbolTable) =>
(string, InteractiveSymbolTable) =

rule let ISTMTS([IEXP(Absyn.CALL(Absyn.CREF_IDENT("buildEnv",_),arg))]) = stmt &
let Absyn.FUNCTIONARGS([],[]) = arg &
SCode.elaborate(p) => p' &
Inst.instantiate_implicit(p') => dae &
let newst = SYMBOLTABLE(p, s, ic, iv, cf)
---------------------------------------------------
evaluate_graphical_api(stmt, st as SYMBOLTABLE(p,s,ic,iv,cf)) => ("succeded\n",newst)


rule let ISTMTS([IEXP(Absyn.CALL(Absyn.CREF_IDENT("loadClass",_),arg))]) = stmt &
let Absyn.FUNCTIONARGS([ Absyn.CREF(cref) ],[]) = arg &
Expand All @@ -387,7 +392,6 @@ relation evaluate_graphical_api: (InteractiveStmts, InteractiveSymbolTable) =>
evaluate_graphical_api(stmt, st as SYMBOLTABLE(p,s,ic,iv,cf)) => (str,st)

rule Parser.parse(name) => p1 &
print "Parsed program\n" &
update_program(p1,p) => newp
---------------------------------------------------
evaluate_graphical_api(ISTMTS([IEXP(Absyn.CALL(Absyn.CREF_IDENT("loadFile",_),
Expand Down

0 comments on commit fe6946b

Please sign in to comment.