Add AST Hashing and Equals Functions
[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         idNumber(0) {
13 }
14
15 ElementSet::ElementSet(Set *s) :
16         Element(ELEMSET),
17         set(s) {
18 }
19
20 ElementFunction::ElementFunction(Function *_function, Element **array, uint numArrays, Boolean *_overflowstatus) :
21         Element(ELEMFUNCRETURN),
22         function(_function),
23         inputs(array, numArrays),
24         overflowstatus(_overflowstatus),
25         functionencoding(this) {
26         for (uint i = 0; i < numArrays; i++)
27                 array[i]->parents.push(this);
28 }
29
30 ElementConst::ElementConst(uint64_t _value, VarType _type, Set *_set) :
31         Element(ELEMCONST),
32         set(_set),
33         value(_value) {
34 }
35
36 Set *getElementSet(Element *This) {
37         switch (This->type) {
38         case ELEMSET:
39                 return ((ElementSet *)This)->set;
40         case ELEMCONST:
41                 return ((ElementConst *)This)->set;
42         case ELEMFUNCRETURN: {
43                 Function *func = ((ElementFunction *)This)->function;
44                 switch (func->type) {
45                 case TABLEFUNC:
46                         return ((FunctionTable *)func)->table->range;
47                 case OPERATORFUNC:
48                         return ((FunctionOperator *)func)->range;
49                 default:
50                         ASSERT(0);
51                 }
52         }
53         default:
54                 ASSERT(0);
55         }
56         ASSERT(0);
57         return NULL;
58 }
59
60 Element *ElementConst::clone(CSolver *solver, CloneMap *map) {
61         return solver->getElementConst(type, value);
62 }
63
64 Element *ElementSet::clone(CSolver *solver, CloneMap *map) {
65         Element *e = (Element *) map->get(this);
66         if (e != NULL)
67                 return e;
68         e = solver->getElementVar(set->clone(solver, map));
69         map->put(e, e);
70         return e;
71 }
72
73 Element *ElementFunction::clone(CSolver *solver, CloneMap *map) {
74         Element *array[inputs.getSize()];
75         for (uint i = 0; i < inputs.getSize(); i++) {
76                 array[i] = inputs.get(i)->clone(solver, map);
77         }
78         Element *e = solver->applyFunction(function->clone(solver, map), array, inputs.getSize(), overflowstatus->clone(solver, map));
79         return e;
80 }