Ignore:
Timestamp:
2012-11-05T16:24:03+01:00 (12 years ago)
Author:
perost
Message:
  • Flatten the class structure in SCodeInst.instClassItem, so we can avoid handling EXTENDED_ELEMENTS everywhere.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/FrontEnd/InstFlatten.mo

    r13755 r13800  
    118118end newSymbolTable;
    119119
     120public type Elements = list<Element>;
     121public type Equations = list<Equation>;
     122public type Algorithms = list<list<Statement>>;
     123
     124public function flattenClass
     125  input Class inClass;
     126  input Boolean inContainsExtends;
     127  output Class outFlatClass;
     128algorithm
     129  outFlatClass := matchcontinue(inClass, inContainsExtends)
     130    local
     131      Absyn.Path name;
     132      Elements el;
     133      Equations eq, ieq;
     134      Algorithms alg, ialg;
     135      list<Class> sections;
     136      Integer el_count;
     137      Class cls;
     138
     139    // If we have no extends then we don't need to do anything.
     140    case (_, false) then inClass;
     141
     142    case (InstTypes.COMPLEX_CLASS(name, el, eq, ieq, alg, ialg), _)
     143      equation
     144        (sections, el_count) =
     145          List.accumulateMapFold(el, collectInheritedSections, 0);
     146        el = flattenElements(el, el_count, name);
     147        cls = InstTypes.COMPLEX_CLASS(name, el, eq, ieq, alg, ialg);
     148        cls = List.fold(sections, flattenSections, cls);
     149      then
     150        cls;
     151
     152    else
     153      equation
     154        true = Flags.isSet(Flags.FAILTRACE);
     155        Debug.traceln("- InstFlatten.flattenClass failed for " +&
     156          Absyn.pathString(InstUtil.getClassName(inClass)) +& "\n");
     157      then
     158        fail();
     159
     160  end matchcontinue;
     161end flattenClass;
     162
     163protected function collectInheritedSections
     164  input Element inElements;
     165  input Integer inElementCount;
     166  input list<Class> inAccumSections;
     167  output list<Class> outSections;
     168  output Integer outElementCount;
     169algorithm
     170  (outSections, outElementCount) :=
     171  match(inElements, inElementCount, inAccumSections)
     172    local
     173      list<Class> accum;
     174      Integer el_count;
     175      Elements el;
     176      Class cls;
     177
     178    case (InstTypes.EXTENDED_ELEMENTS(cls = cls as InstTypes.COMPLEX_CLASS(
     179        components = el)), _, _)
     180      equation
     181        el_count = listLength(el) + inElementCount;
     182      then
     183        (cls :: inAccumSections, el_count);
     184
     185    else (inAccumSections, inElementCount + 1);
     186
     187  end match;
     188end collectInheritedSections;
     189
     190protected function flattenSections
     191  input Class inSections;
     192  input Class inAccumSections;
     193  output Class outSections;
     194algorithm
     195  outSections := match(inSections, inAccumSections)
     196    local
     197      Elements el;
     198      Equations eq1, eq2, ieq1, ieq2;
     199      Algorithms alg1, alg2, ialg1, ialg2;
     200      Absyn.Path name;
     201
     202    case (InstTypes.COMPLEX_CLASS(_, _, eq1, ieq1, alg1, ialg1),
     203        InstTypes.COMPLEX_CLASS(name, el, eq2, ieq2, alg2, ialg2))
     204      equation
     205        eq1 = listAppend(eq1, eq2);
     206        ieq1 = listAppend(ieq1, ieq2);
     207        alg1 = listAppend(alg1, alg2);
     208        ialg1 = listAppend(ialg1, ialg2);
     209      then
     210        InstTypes.COMPLEX_CLASS(name, el, eq1, ieq1, alg1, ialg1);
     211
     212  end match;
     213end flattenSections;
     214
    120215public function flattenElements
    121216  input list<Element> inElements;
     217  input Integer inElementCount;
    122218  input Absyn.Path inClassPath;
    123   input Boolean inContainsExtends;
    124219  output list<Element> outElements;
    125220algorithm
    126   outElements := matchcontinue(inElements, inClassPath, inContainsExtends)
     221  outElements := matchcontinue(inElements, inElementCount, inClassPath)
    127222    local
    128223      SymbolTable st;
    129       Integer el_count;
    130224      list<Element> flat_el;
    131 
    132     // The elements are already flattened if we have no extends.
    133     case (_, _, false) then inElements;
     225      Class sections;
    134226
    135227    case (_, _, _)
    136228      equation
    137         el_count = listLength(inElements);
    138         st = newSymbolTable(intDiv(el_count * 4, 3) + 1);
     229        st = newSymbolTable(intDiv(inElementCount * 4, 3) + 1);
    139230        (flat_el, _) = flattenElements2(inElements, st, {}, inClassPath, {});
    140231      then
     
    217308        // For extended elements we can use the names from the type, which are
    218309        // already the last identifiers.
    219         var_names = List.map(vars, Types.getVarName);
     310        var_names = List.mapReverse(vars, Types.getVarName);
    220311        (accum_el, st) = flattenExtendedElements(ext_el, var_names,
    221312          bc :: inExtendPath, inClassPath, st, accum_el);
     
    256347      list<Element> accum_el;
    257348      SymbolTable st;
    258 
    259     // Try to add the component to the
     349      Absyn.Path name;
     350      Component comp;
     351
     352    // Try to add the component to the symbol table.
    260353    case (_, _, _, _, st, accum_el)
    261354      equation
     
    264357        (true, st);
    265358
     359    // If we couldn't add the component to the symbol table it means it already
     360    // exists, so we need to check that it's identical to the already existing
     361    // component.
    266362    case (_, _, _, _, st, accum_el)
    267363      equation
    268         print("Removing equal " +& inName +& "\n");
    269         // Look up the already existing component here and check that they are
    270         // equal.
     364        /**********************************************************************/
     365        // TODO: Look up the already existing component here and check that they
     366        // are equal.
     367        /**********************************************************************/
    271368      then
    272369        (false, st);
Note: See TracChangeset for help on using the changeset viewer.