edits
[cdsspec-compiler.git] / notes / definition.cc
1 #include <iostream>
2 #include <vector>
3 #include <string>
4 #include <iterator>
5 #include <algorithm>
6 #include <set>
7
8 using namespace std;
9
10 typedef struct MethodCall {
11         string interfaceName; // The interface label name
12         void *value; // The pointer that points to the struct that have the return
13                                  // value and the arguments
14         void *localState; // The pointer that points to the struct that represents
15                                           // the (local) state
16         vector<MethodCall*> *prev; // Method calls that are hb right before me
17         vector<MethodCall*> *next; // Method calls that are hb right after me
18         vector<MethodCall*> *concurrent; // Method calls that are concurrent with me
19 } MethodCall;
20
21 typedef MethodCall *Method;
22 typedef vector<Method> *MethodSet;
23
24 typedef vector<int> IntList;
25
26 #define NewSet new vector<Method>
27
28 #define CAT(a, b) CAT_HELPER(a, b) /* Concatenate two symbols for macros! */
29 #define CAT_HELPER(a, b) a ## b
30 #define X(name) CAT(__##name, __LINE__) /* unique variable */
31
32 /**
33         The set here is a vector<MethodCall*>* type, or the MethodSet type. And the
34         item would become the MethodCall* type, or the Method type
35 */
36 #define ForEach(item, set) \
37         int X(i) = 0; \
38         for (Method item = (set)->size() > 0 ? (*(set))[0] : NULL; \
39                 X(i) < (set)->size(); X(i)++, item = X(i) < (set)->size() ? (*(set))[X(i)] : NULL)
40
41 inline MethodSet Subset(MethodSet set, string name) {
42         MethodSet _subset = NewSet;
43         ForEach (_m, set) {
44                 if (_m->interfaceName == name)
45                         _subset->push_back(_m);
46         }
47         return _subset;
48 }
49
50 #define Size(set) (set->size())
51
52 #define Belong(set, method) (std::find(set->begin(), set->end(), method) \
53         != set->end())
54
55 inline MethodSet Intersect(MethodSet set1, MethodSet set2) {
56         MethodSet res = NewSet;
57         ForEach (m, set1) {
58         if (Belong(set2, m))
59                 res->push_back(m);
60         }
61         return res;
62 }
63
64 inline MethodSet Union(MethodSet set1, MethodSet set2) {
65         MethodSet res = NewSet(*set1);
66         ForEach (m, set2) {
67                 if (!Belong(set1, m))
68                         res->push_back(m);
69         }
70         return res;
71 }
72
73 inline MethodSet Subtract(MethodSet set1, MethodSet set2) {
74         MethodSet res = NewSet;
75         ForEach (m, set1) {
76                 if (!Belong(set2, m))
77                         res->push_back(m);
78         }
79         return res;
80 }
81
82 inline bool Insert(MethodSet set, Method m) {
83         if (Belong(set, m))
84                 return false;
85         else {
86                 set->push_back(m);
87                 return true;
88         }
89 }
90
91 inline MethodSet MakeSet(int count, ...) {
92         va_list ap;
93         MethodSet res;
94
95         va_start (ap, count);
96         res = NewSet;
97         for (int i = 0; i < count; i++) {
98                 Method m = va_arg (ap, Method);
99                 if (!Belong(res, m))
100                         res->push_back(m);
101         }
102         va_end (ap);
103         return res;
104 }
105
106
107 #define Local(method, field) ((StateStruct*) method->localState)->field
108
109 #define Value(method, type, field) ((type*) method->value)->field
110
111 #define Label(method) method->interfaceName
112
113 #define Prev(method) method->prev
114 #define PREV ME->prev
115
116 #define Next(method) method->next
117 #define NEXT ME->next
118
119 #define Concurrent(method) method->concurrent
120 #define CONCURRENT  ME->concurrent
121
122 // This auto-generated struct can have different fields according to the read
123 // state declaration. Here it's just a test example
124 typedef struct StateStruct {
125         IntList *list;
126 } StateStruct;
127
128 // These auto-generated struct can have different fields according to the return
129 // value and arguments of the corresponding interface. The struct will have the
130 // same name as the interface name. Here it's just a test example
131 typedef struct Store {
132         int *loc;
133         int val;
134 } Store;
135
136 typedef struct Load {
137         int RET;
138         int *loc;
139 } Load;
140
141 int main() {
142         Method ME = NULL;
143         MethodSet set = NewSet;
144         ForEach (m, Subset(set, "Store")) {
145                 IntList *l = Local(m, list);
146                 int ret = Value(m, Load, RET);
147                 string name = Label(m);
148         }
149         ForEach (m, Prev(ME)) {
150
151         }
152
153         int size = Size(PREV);
154         bool flag = Belong(CONCURRENT, ME);
155         flag = Belong(MakeSet(3, ME, ME, ME), ME);
156         return 0;
157 }