Using inline functions instead of macros for accessing function/element encodings
[satune.git] / src / AST / element.c
1 #include "element.h"
2 #include "structs.h"
3 #include "set.h"
4 #include "constraint.h"
5
6 Element *allocElementSet(Set * s) {
7         ElementSet * tmp=(ElementSet *)ourmalloc(sizeof(ElementSet));
8         GETELEMENTTYPE(tmp)= ELEMSET;
9         tmp->set=s;
10         allocInlineDefVectorASTNode(GETELEMENTPARENTS(tmp));
11         initElementEncoding(&tmp->encoding, (Element *) tmp);
12         return &tmp->base;
13 }
14
15 Element* allocElementFunction(Function * function, Element ** array, uint numArrays, Boolean * overflowstatus){
16         ElementFunction* tmp = (ElementFunction*) ourmalloc(sizeof(ElementFunction));
17         GETELEMENTTYPE(tmp)= ELEMFUNCRETURN;
18         tmp->function=function;
19         tmp->overflowstatus = overflowstatus;
20         allocInlineArrayInitElement(&tmp->inputs, array, numArrays);
21         allocInlineDefVectorASTNode(GETELEMENTPARENTS(tmp));
22         for(uint i=0;i<numArrays;i++)
23                 pushVectorASTNode(GETELEMENTPARENTS(array[i]), (ASTNode *) tmp);
24         initElementEncoding(&tmp->domainencoding, (Element *) tmp);
25         initFunctionEncoding(&tmp->functionencoding, (Element *) tmp);
26         return &tmp->base;
27 }
28
29 uint getElementSize(Element* This){
30         switch(GETELEMENTTYPE(This)){
31                 case ELEMSET:
32                         return getSetSize( ((ElementSet*)This)->set );
33                         break;
34                 case ELEMFUNCRETURN:
35                         ASSERT(0);
36                 default:
37                         ASSERT(0);
38         }
39         return -1;
40 }
41
42
43 Constraint * getElementValueConstraint(Element* This, uint64_t value) {
44         switch(GETELEMENTTYPE(This)){
45                 case ELEMSET:
46                         ; //Statement is needed for a label and This is a NOPE
47                         ElementSet* elemSet= ((ElementSet*)This);
48                         uint size = getSetSize(elemSet->set);
49                         for(uint i=0; i<size; i++){
50                                 if( getElementEncoding(elemSet)->encodingArray[i]==value){
51                                         return generateBinaryConstraint(getElementEncoding(elemSet)->numVars,
52                                                 getElementEncoding(elemSet)->variables, i);
53                                 }
54                         }
55                         break;
56                 case ELEMFUNCRETURN:
57                         break;
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->domainencoding);
71                 deleteFunctionEncoding(&ef->functionencoding);
72                 break;
73         }
74         case ELEMSET: {
75                 ElementSet *es = (ElementSet *) This;
76                 deleteElementEncoding(&es->encoding);
77                 break;
78         }
79         default:
80                 ;
81         }
82         deleteVectorArrayASTNode(GETELEMENTPARENTS(This));
83
84         ourfree(This);
85 }