6926ce032f8af7097d1d7663ff046f423826b826
[satune.git] / src / AST / element.c
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
8 Element *allocElementSet(Set * s) {
9         ElementSet * This=(ElementSet *)ourmalloc(sizeof(ElementSet));
10         GETELEMENTTYPE(This)= ELEMSET;
11         This->set=s;
12         initDefVectorASTNode(GETELEMENTPARENTS(This));
13         initElementEncoding(&This->encoding, (Element *) This);
14         return &This->base;
15 }
16
17 Element* allocElementFunction(Function * function, Element ** array, uint numArrays, Boolean * overflowstatus){
18         ElementFunction* This = (ElementFunction*) ourmalloc(sizeof(ElementFunction));
19         GETELEMENTTYPE(This)= ELEMFUNCRETURN;
20         This->function=function;
21         This->overflowstatus = overflowstatus;
22         initArrayInitElement(&This->inputs, array, numArrays);
23         initDefVectorASTNode(GETELEMENTPARENTS(This));
24         for(uint i=0;i<numArrays;i++)
25                 pushVectorASTNode(GETELEMENTPARENTS(array[i]), (ASTNode *) This);
26         initElementEncoding(&This->rangeencoding, (Element *) This);
27         initFunctionEncoding(&This->functionencoding, (Element *) This);
28         return &This->base;
29 }
30
31 Element * allocElementConst(uint64_t value, VarType type) {
32         ElementConst * This=(ElementConst *)ourmalloc(sizeof(ElementConst));
33         GETELEMENTTYPE(This)= ELEMCONST;
34         This->value=value;
35         This->set=allocSet(type, (uint64_t[]){value}, 1);
36         initDefVectorASTNode(GETELEMENTPARENTS(This));
37         initElementEncoding(&This->encoding, (Element *) This);
38         return &This->base;
39 }
40
41 Set* getElementSet(Element* This){
42         switch(GETELEMENTTYPE(This)){
43         case ELEMSET:
44                 return ((ElementSet*)This)->set;
45         case ELEMCONST:
46                 return ((ElementConst*)This)->set;
47         case ELEMFUNCRETURN: {
48                 Function* func = ((ElementFunction*)This)->function;
49                 switch(GETFUNCTIONTYPE(func)){
50                 case TABLEFUNC:
51                         return ((FunctionTable*)func)->table->range;
52                 case OPERATORFUNC:
53                         return ((FunctionOperator*)func)->range;
54                 default:
55                         ASSERT(0);
56                 }
57         }
58         default:
59                 ASSERT(0);
60         }
61         ASSERT(0);
62         return NULL;
63 }
64
65 void deleteElement(Element *This) {
66         switch(GETELEMENTTYPE(This)) {
67         case ELEMFUNCRETURN: {
68                 ElementFunction *ef = (ElementFunction *) This;
69                 deleteInlineArrayElement(&ef->inputs);
70                 deleteElementEncoding(&ef->rangeencoding);
71                 deleteFunctionEncoding(&ef->functionencoding);
72                 break;
73         }
74         case ELEMSET: {
75                 ElementSet *es = (ElementSet *) This;
76                 deleteElementEncoding(&es->encoding);
77                 break;
78         }
79         case ELEMCONST: {
80                 ElementConst *ec = (ElementConst *) This;
81                 deleteSet(ec->set);//Client did not create, so we free it
82                 deleteElementEncoding(&ec->encoding);
83                 break;
84         }
85         default:
86                 ASSERT(0);
87         }
88         deleteVectorArrayASTNode(GETELEMENTPARENTS(This));
89         ourfree(This);
90 }