Skip to content

Commit

Permalink
Add support for type variables in uniontypes
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Mar 8, 2016
1 parent 22263b6 commit 7e8eeef
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 165 deletions.
1 change: 1 addition & 0 deletions Compiler/FrontEnd/Absyn.mo
Expand Up @@ -1041,6 +1041,7 @@ uniontype Restriction "These constructors each correspond to a different kind of
Integer index; //Index in the uniontype
Boolean singleton;
Boolean moved; // true if moved outside uniontype, otherwise false.
list<String> typeVars;
end R_METARECORD;
record R_UNKNOWN "Helper restriction" end R_UNKNOWN; /* added by simbj */
end Restriction;
Expand Down
39 changes: 8 additions & 31 deletions Compiler/FrontEnd/ClassInf.mo
Expand Up @@ -153,6 +153,7 @@ uniontype State "- Machine states, the string contains the classname."

record META_UNIONTYPE
Absyn.Path path;
list<String> typeVars;
end META_UNIONTYPE;

record META_ARRAY
Expand Down Expand Up @@ -435,7 +436,7 @@ algorithm
then TYPE_CLOCK(p);
case (SCode.R_PREDEFINED_ENUMERATION(),p) then TYPE_ENUM(p);
/* Meta Modelica extensions */
case (SCode.R_UNIONTYPE(),p) then META_UNIONTYPE(p);
case (SCode.R_UNIONTYPE(),p) then META_UNIONTYPE(p, inRestriction.typeVars);
case (SCode.R_METARECORD(),p) then META_RECORD(p);
end match;
end start_dispatch;
Expand Down Expand Up @@ -767,39 +768,15 @@ algorithm
end match;
end isRecord;

public function stateToSCodeRestriction
"@author: adrpo
ClassInf.State -> SCode.Restriction"
public function isMetaRecord
input State inState;
output SCode.Restriction outRestriction;
output Absyn.Path outPath;
output Boolean outIsRecord;
algorithm
(outRestriction, outPath) := match (inState)
local Absyn.Path p; Boolean isExpandable, isImpure;

case UNKNOWN(p) then (SCode.R_CLASS(),p);
case OPTIMIZATION(p) then (SCode.R_OPTIMIZATION(),p);
case MODEL(p) then (SCode.R_MODEL(),p);
// mahge: TODO ClassInf.RECORD should contain isOperator.
case RECORD(p) then (SCode.R_RECORD(false),p);
case BLOCK(p) then (SCode.R_BLOCK(),p) ;
case CONNECTOR(p,isExpandable) then (SCode.R_CONNECTOR(isExpandable),p);
case TYPE(p) then (SCode.R_TYPE(),p);
case PACKAGE(p) then (SCode.R_PACKAGE(),p) ;
case FUNCTION(p,isImpure) then (SCode.R_FUNCTION(SCode.FR_NORMAL_FUNCTION(isImpure)),p);
case ENUMERATION(p) then (SCode.R_ENUMERATION(),p);
case TYPE_INTEGER(p) then (SCode.R_PREDEFINED_INTEGER(),p);
case TYPE_REAL(p) then (SCode.R_PREDEFINED_REAL(),p);
case TYPE_STRING(p) then (SCode.R_PREDEFINED_STRING(),p);
case TYPE_BOOL(p) then (SCode.R_PREDEFINED_BOOLEAN(),p);
// BTH
case TYPE_CLOCK(p) then (SCode.R_PREDEFINED_CLOCK(),p);
case TYPE_ENUM(p) then (SCode.R_PREDEFINED_ENUMERATION(),p);
/* Meta Modelica extensions */
case META_UNIONTYPE(p) then (SCode.R_UNIONTYPE(),p);
case META_RECORD(p) then (SCode.R_METARECORD(p, 0, false, false),p);
outIsRecord := match inState
case META_RECORD() then true;
else false;
end match;
end stateToSCodeRestriction;
end isMetaRecord;

annotation(__OpenModelica_Interface="frontend");
end ClassInf;
Expand Down
8 changes: 6 additions & 2 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -830,7 +830,7 @@ constant Type T_METATYPE_DEFAULT = T_METATYPE(T_UNKNOWN_DEFAULT, emptyTypeSou
constant Type T_COMPLEX_DEFAULT = T_COMPLEX(ClassInf.UNKNOWN(Absyn.IDENT("")), {}, NONE(), emptyTypeSource) "default complex with unknown CiState";
constant Type T_COMPLEX_DEFAULT_RECORD = T_COMPLEX(ClassInf.RECORD(Absyn.IDENT("")), {}, NONE(), emptyTypeSource) "default complex with record CiState";

constant Type T_SOURCEINFO_DEFAULT_METARECORD = T_METARECORD(Absyn.QUALIFIED("SourceInfo",Absyn.IDENT("SOURCEINFO")), 1, {
constant Type T_SOURCEINFO_DEFAULT_METARECORD = T_METARECORD(Absyn.QUALIFIED("SourceInfo",Absyn.IDENT("SOURCEINFO")), {}, 1, {
TYPES_VAR("fileName", dummyAttrVar, T_STRING_DEFAULT, UNBOUND(), NONE()),
TYPES_VAR("isReadOnly", dummyAttrVar, T_BOOL_DEFAULT, UNBOUND(), NONE()),
TYPES_VAR("lineNumberStart", dummyAttrVar, T_INTEGER_DEFAULT, UNBOUND(), NONE()),
Expand All @@ -839,7 +839,7 @@ constant Type T_SOURCEINFO_DEFAULT_METARECORD = T_METARECORD(Absyn.QUALIFIED("So
TYPES_VAR("columnNumberEnd", dummyAttrVar, T_INTEGER_DEFAULT, UNBOUND(), NONE()),
TYPES_VAR("lastModification", dummyAttrVar, T_REAL_DEFAULT, UNBOUND(), NONE())
}, true, emptyTypeSource);
constant Type T_SOURCEINFO_DEFAULT = T_METAUNIONTYPE({Absyn.QUALIFIED("SourceInfo",Absyn.IDENT("SOURCEINFO"))},true,EVAL_SINGLETON_KNOWN_TYPE(T_SOURCEINFO_DEFAULT_METARECORD),Absyn.IDENT("SourceInfo")::{});
constant Type T_SOURCEINFO_DEFAULT = T_METAUNIONTYPE({Absyn.QUALIFIED("SourceInfo",Absyn.IDENT("SOURCEINFO"))},{},true,EVAL_SINGLETON_KNOWN_TYPE(T_SOURCEINFO_DEFAULT_METARECORD),Absyn.IDENT("SourceInfo")::{});

// Arrays of unknown dimension, eg. Real[:]
public constant Type T_ARRAY_REAL_NODIM = T_ARRAY(T_REAL_DEFAULT,{DIM_UNKNOWN()}, emptyTypeSource);
Expand Down Expand Up @@ -970,6 +970,7 @@ public uniontype Type "models the different front-end and back-end types"
record T_METAUNIONTYPE "MetaModelica Uniontype, added by simbj"
// TODO: You can't trust these fields as it seems MetaUtil.fixUniontype is sent empty elements when running dependency analysis
list<Absyn.Path> paths;
list<Type> typeVars;
Boolean knownSingleton "The runtime system (dynload), does not know if the value is a singleton. But optimizations are safe if this is true.";
EvaluateSingletonType singletonType;
TypeSource source;
Expand All @@ -979,6 +980,7 @@ public uniontype Type "models the different front-end and back-end types"
Absyn.Path utPath "the path to its uniontype; this is what we match the type against";
// If the metarecord constructor was added to the FunctionTree, this would
// not be needed. They are used to create the datatype in the runtime...
list<Type> typeVars;
Integer index; //The index in the uniontype
list<Var> fields;
Boolean knownSingleton "The runtime system (dynload), does not know if the value is a singleton. But optimizations are safe if this is true.";
Expand Down Expand Up @@ -1469,6 +1471,7 @@ uniontype Exp "Expressions
list<Exp> args;
list<String> fieldNames;
Integer index; //Index in the uniontype
list<Type> typeVars;
end METARECORDCALL;

record MATCHEXPRESSION
Expand Down Expand Up @@ -1619,6 +1622,7 @@ public uniontype Pattern "Patterns deconstruct expressions"
Integer index;
list<Pattern> patterns;
list<Var> fields; // Needed to be able to bind a variable to the fields
list<Type> typeVars;
Boolean knownSingleton "The runtime system (dynload), does not know if the value is a singleton. But optimizations are safe if this is true.";
end PAT_CALL;
record PAT_CALL_NAMED "RECORD(pat1,...,patn); all patterns are named"
Expand Down
26 changes: 15 additions & 11 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -2252,7 +2252,7 @@ algorithm
Absyn.Path p;
String msg;
DAE.Type ty, iterTp, operTp;
list<DAE.Type> tys;
list<DAE.Type> tys, typeVars;
Integer i,i1,i2;
DAE.Dimension dim;
DAE.Dimensions iterdims;
Expand Down Expand Up @@ -2321,11 +2321,11 @@ algorithm
then
DAE.T_TUPLE(tys, NONE(), DAE.emptyTypeSource);
case (DAE.META_OPTION(_))then DAE.T_METATYPE(DAE.T_NONE_DEFAULT, DAE.emptyTypeSource);
case (DAE.METARECORDCALL(path=p, index = i))
case (DAE.METARECORDCALL(path=p, index = i, typeVars=typeVars))
equation

then
DAE.T_METATYPE(DAE.T_METARECORD(p, i, {}, false, DAE.emptyTypeSource), DAE.emptyTypeSource);
DAE.T_METATYPE(DAE.T_METARECORD(p, typeVars, i, {}, false, DAE.emptyTypeSource), DAE.emptyTypeSource);
case (DAE.BOX(e))
equation
ty = typeof(e);
Expand Down Expand Up @@ -5044,6 +5044,7 @@ algorithm
DAE.ComponentRef cr, cr_1;
list<list<String>> aliases;
DAE.ClockKind clk, clk1;
list<DAE.Type> typeVars;

case DAE.EMPTY() equation
(e, ext_arg) = inFunc(inExp, inExtArg);
Expand Down Expand Up @@ -5269,9 +5270,9 @@ algorithm
(e, ext_arg) = inFunc(e, ext_arg);
then (e, ext_arg);

case DAE.METARECORDCALL(fn, expl, fieldNames, i) equation
case DAE.METARECORDCALL(fn, expl, fieldNames, i, typeVars) equation
(expl_1, ext_arg) = traverseExpList(expl, inFunc, inExtArg);
e = if referenceEq(expl, expl_1) then inExp else DAE.METARECORDCALL(fn, expl_1, fieldNames, i);
e = if referenceEq(expl, expl_1) then inExp else DAE.METARECORDCALL(fn, expl_1, fieldNames, i, typeVars);
(e, ext_arg) = inFunc(e, ext_arg);
then (e, ext_arg);
// ---------------------
Expand Down Expand Up @@ -5641,6 +5642,7 @@ algorithm
ComponentRef cr,cr_1;
list<list<String>> aliases;
DAE.ClockKind clk, clk1;
list<DAE.Type> typeVars;

case (false,_,_,_) then (inExp,inArg);
case (_,DAE.ICONST(_),_,ext_arg) then (inExp,ext_arg);
Expand Down Expand Up @@ -5818,10 +5820,10 @@ algorithm
(cases, ext_arg) = Patternm.traverseCasesTopDown(cases, rel, ext_arg);
then (DAE.MATCHEXPRESSION(matchType,expl,aliases,localDecls,cases,et),ext_arg);

case (_,DAE.METARECORDCALL(fn,expl,fieldNames,i),rel,ext_arg)
case (_,DAE.METARECORDCALL(fn,expl,fieldNames,i,typeVars),rel,ext_arg)
equation
(expl_1,ext_arg_1) = traverseExpListTopDown(expl, rel, ext_arg);
then (DAE.METARECORDCALL(fn,expl_1,fieldNames,i),ext_arg_1);
then (DAE.METARECORDCALL(fn,expl_1,fieldNames,i,typeVars),ext_arg_1);

case (_,DAE.UNBOX(e1,tp),rel,ext_arg)
equation
Expand Down Expand Up @@ -6823,6 +6825,7 @@ algorithm
DAE.CallAttributes attr;
list<list<String>> aliases;
ArgT arg;
list<DAE.Type> typeVars;

case DAE.ICONST() then (inExp, inArg);
case DAE.RCONST() then (inExp, inArg);
Expand Down Expand Up @@ -6985,11 +6988,11 @@ algorithm
then
(DAE.META_OPTION(oe1), arg);

case DAE.METARECORDCALL(path = path, args = expl, fieldNames = strl, index = index)
case DAE.METARECORDCALL(path = path, args = expl, fieldNames = strl, index = index, typeVars = typeVars)
equation
(expl, arg) = traverseExpListBidir(expl, inEnterFunc, inExitFunc, inArg);
then
(DAE.METARECORDCALL(path, expl, strl, index), arg);
(DAE.METARECORDCALL(path, expl, strl, index, typeVars), arg);

case DAE.MATCHEXPRESSION(matchType = match_ty, inputs = expl, aliases=aliases,
localDecls = match_decls, cases = match_cases, et = ty)
Expand Down Expand Up @@ -11758,6 +11761,7 @@ algorithm
DAE.ReductionIterators riters,riters_1;
DAE.ComponentRef cr,cr_1;
list<list<String>> aliases;
list<DAE.Type> typeVars;

case ((e as DAE.EMPTY()),rel,ext_arg)
equation
Expand Down Expand Up @@ -12002,10 +12006,10 @@ algorithm
(e,ext_arg) = rel(e,ext_arg);
then (e,ext_arg);

case (DAE.METARECORDCALL(fn,expl,fieldNames,i),rel,ext_arg)
case (DAE.METARECORDCALL(fn,expl,fieldNames,i,typeVars),rel,ext_arg)
equation
(expl_1,ext_arg) = traverseExpDerPreStartList(expl, rel, ext_arg);
e = if referenceEq(expl,expl_1) then inExp else DAE.METARECORDCALL(fn,expl_1,fieldNames,i);
e = if referenceEq(expl,expl_1) then inExp else DAE.METARECORDCALL(fn,expl_1,fieldNames,i,typeVars);
(e,ext_arg) = rel(e,ext_arg);
then (e,ext_arg);
// ---------------------
Expand Down

0 comments on commit 7e8eeef

Please sign in to comment.