Merge branch 'master' into brian
[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 * tmp=(ElementSet *)ourmalloc(sizeof(ElementSet));
10         GETELEMENTTYPE(tmp)= ELEMSET;
11         tmp->set=s;
12         allocInlineDefVectorASTNode(GETELEMENTPARENTS(tmp));
13         initElementEncoding(&tmp->encoding, (Element *) tmp);
14         return &tmp->base;
15 }
16
17 Element* allocElementFunction(Function * function, Element ** array, uint numArrays, Boolean * overflowstatus){
18         ElementFunction* tmp = (ElementFunction*) ourmalloc(sizeof(ElementFunction));
19         GETELEMENTTYPE(tmp)= ELEMFUNCRETURN;
20         tmp->function=function;
21         tmp->overflowstatus = overflowstatus;
22         allocInlineArrayInitElement(&tmp->inputs, array, numArrays);
23         allocInlineDefVectorASTNode(GETELEMENTPARENTS(tmp));
24         for(uint i=0;i<numArrays;i++)
25                 pushVectorASTNode(GETELEMENTPARENTS(array[i]), (ASTNode *) tmp);
26         initElementEncoding(&tmp->domainencoding, (Element *) tmp);
27         initFunctionEncoding(&tmp->functionencoding, (Element *) tmp);
28         return &tmp->base;
29 }
30
31 Set* getElementSet(Element* This){
32         switch(GETELEMENTTYPE(This)){
33                 case ELEMSET:
34                         return ((ElementSet*)This)->set;
35                 case ELEMFUNCRETURN:
36                         ;//Nope is needed for label assignment. i.e. next instruction isn't a statement 
37                         Function* func = ((ElementFunction*)This)->function;
38                         switch(GETFUNCTIONTYPE(func)){
39                                 case TABLEFUNC:
40                                         return ((FunctionTable*)func)->table->range;
41                                 case OPERATORFUNC:
42                                         return ((FunctionOperator*)func)->range;
43                                 default:
44                                         ASSERT(0);
45                         }
46                 default:
47                         ASSERT(0);
48         }
49         ASSERT(0);
50         return NULL;
51 }
52
53 void deleteElement(Element *This) {
54         switch(GETELEMENTTYPE(This)) {
55         case ELEMFUNCRETURN: {
56                 ElementFunction *ef = (ElementFunction *) This;
57                 deleteInlineArrayElement(&ef->inputs);
58                 deleteElementEncoding(&ef->domainencoding);
59                 deleteFunctionEncoding(&ef->functionencoding);
60                 break;
61         }
62         case ELEMSET: {
63                 ElementSet *es = (ElementSet *) This;
64                 deleteElementEncoding(&es->encoding);
65                 break;
66         }
67         default:
68                 ;
69         }
70         deleteVectorArrayASTNode(GETELEMENTPARENTS(This));
71
72         ourfree(This);
73 }