Merge branch 'master' of ssh://demsky.eecs.uci.edu/home/git/constraint_compiler into...
[satune.git] / src / AST / element.cc
1 #include "element.h"
2 #include "structs.h"
3 #include "set.h"
4 #include "constraint.h"
5 #include "function.h"
6 #include "table.h"
7 #include "csolver.h"
8
9 Element::Element(ASTNodeType _type) :
10         ASTNode(_type),
11         encoding(this) {
12 }
13
14 ElementSet::ElementSet(Set *s) :
15         Element(ELEMSET),
16         set(s) {
17 }
18
19 ElementFunction::ElementFunction(Function *_function, Element **array, uint numArrays, BooleanEdge _overflowstatus) :
20         Element(ELEMFUNCRETURN),
21         function(_function),
22         inputs(array, numArrays),
23         overflowstatus(_overflowstatus),
24         functionencoding(this) {
25 }
26
27 ElementConst::ElementConst(uint64_t _value, VarType _type, Set *_set) :
28         Element(ELEMCONST),
29         set(_set),
30         value(_value) {
31 }
32
33 Set *getElementSet(Element *This) {
34         switch (This->type) {
35         case ELEMSET:
36                 return ((ElementSet *)This)->set;
37         case ELEMCONST:
38                 return ((ElementConst *)This)->set;
39         case ELEMFUNCRETURN: {
40                 Function *func = ((ElementFunction *)This)->function;
41                 switch (func->type) {
42                 case TABLEFUNC:
43                         return ((FunctionTable *)func)->table->range;
44                 case OPERATORFUNC:
45                         return ((FunctionOperator *)func)->range;
46                 default:
47                         ASSERT(0);
48                 }
49         }
50         default:
51                 ASSERT(0);
52         }
53         ASSERT(0);
54         return NULL;
55 }
56
57 Element *ElementConst::clone(CSolver *solver, CloneMap *map) {
58         return solver->getElementConst(type, value);
59 }
60
61 Element *ElementSet::clone(CSolver *solver, CloneMap *map) {
62         Element *e = (Element *) map->get(this);
63         if (e != NULL)
64                 return e;
65         e = solver->getElementVar(set->clone(solver, map));
66         map->put(e, e);
67         return e;
68 }
69
70 Element *ElementFunction::clone(CSolver *solver, CloneMap *map) {
71         Element *array[inputs.getSize()];
72         for (uint i = 0; i < inputs.getSize(); i++) {
73                 array[i] = inputs.get(i)->clone(solver, map);
74         }
75         Element *e = solver->applyFunction(function->clone(solver, map), array, inputs.getSize(), overflowstatus->clone(solver, map));
76         return e;
77 }
78
79 void ElementFunction::updateParents() {
80         for(uint i=0;i < inputs.getSize(); i++) inputs.get(i)->parents.push(this);
81 }