b57f1db31588e9472a44a61de1309ef37264801a
[m6w6/ext-psi] / idl / types.h
1
2 typedef struct decl_type {
3 text *name;
4 token_t type;
5 } decl_type;
6
7 static inline decl_type *init_decl_type(token_t type, text *name) {
8 decl_type *t = malloc(sizeof(*t));
9 t->type = type;
10 t->name = (text *) strdup((const char *) name);
11 return t;
12 }
13
14 static inline void free_decl_type(decl_type *type) {
15 free(type->name);
16 free(type);
17 }
18
19 typedef struct decl_var {
20 text *name;
21 unsigned pointer_level;
22 } decl_var;
23
24 static inline decl_var *init_decl_var(text *name, unsigned pl) {
25 decl_var *v = malloc(sizeof(*v));
26 v->name = (text *) strdup((const char *) name);
27 v->pointer_level = pl;
28 return v;
29 }
30
31 static inline void free_decl_var(decl_var *var) {
32 free(var->name);
33 free(var);
34 }
35
36 typedef struct decl_arg {
37 decl_type *type;
38 decl_var *var;
39 } decl_arg;
40
41 static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) {
42 decl_arg *arg = malloc(sizeof(*arg));
43 arg->type = type;
44 arg->var = var;
45 return arg;
46 }
47
48 static inline void free_decl_arg(decl_arg *arg) {
49 free_decl_type(arg->type);
50 free_decl_var(arg->var);
51 free(arg);
52 }
53
54 typedef struct decl_vars {
55 decl_var **vars;
56 size_t count;
57 } decl_vars;
58
59 static inline decl_vars *init_decl_vars(decl_var *var) {
60 decl_vars *vars = malloc(sizeof(*vars));
61 vars->count = 1;
62 vars->vars = malloc(sizeof(*vars->vars));
63 vars->vars[0] = var;
64 return vars;
65 }
66
67 static inline decl_vars *add_decl_var(decl_vars *vars, decl_var *var) {
68 vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars));
69 vars->vars[vars->count-1] = var;
70 return vars;
71 }
72
73 static inline void free_decl_vars(decl_vars *vars) {
74 size_t i;
75
76 for (i = 0; i < vars->count; ++i) {
77 free_decl_var(vars->vars[i]);
78 }
79 free(vars->vars);
80 free(vars);
81 }
82
83 typedef struct decl_args {
84 decl_arg **args;
85 size_t count;
86 } decl_args;
87
88 static inline decl_args *init_decl_args(decl_arg *arg) {
89 decl_args *args = malloc(sizeof(*args));
90 args->count = 1;
91 args->args = malloc(sizeof(*args->args));
92 args->args[0] = arg;
93 return args;
94 }
95
96 static inline decl_args *add_decl_arg(decl_args *args, decl_arg *arg) {
97 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
98 args->args[args->count-1] = arg;
99 return args;
100 }
101
102 static inline void free_decl_args(decl_args *args) {
103 size_t i;
104
105 for (i = 0; i < args->count; ++i) {
106 free_decl_arg(args->args[i]);
107 }
108 free(args->args);
109 free(args);
110 }
111
112 typedef struct decl {
113 decl_arg *func;
114 decl_args *args;
115 } decl;
116
117 static inline decl* init_decl(decl_arg *func, decl_args *args) {
118 decl *d = malloc(sizeof(*d));
119 d->func = func;
120 d->args = args;
121 return d;
122 }
123
124 static inline void free_decl(decl *d) {
125 free_decl_arg(d->func);
126 free_decl_args(d->args);
127 free(d);
128 }
129
130 typedef struct impl_type {
131 text *name;
132 token_t type;
133 } impl_type;
134
135 static inline impl_type *init_impl_type(token_t type, text *name) {
136 impl_type *t = malloc(sizeof(*t));
137
138 t->type = type;
139 t->name = (text *) strdup((const char *) name);
140 return t;
141 }
142
143 static inline void free_impl_type(impl_type *type) {
144 free(type->name);
145 free(type);
146 }
147
148 typedef struct impl_var {
149 text *name;
150 unsigned reference:1;
151 } impl_var;
152
153 static inline impl_var *init_impl_var(text *name, int is_reference) {
154 impl_var *var = malloc(sizeof(*var));
155 var->name = (text *) strdup((const char *) name);
156 var->reference = is_reference;
157 return var;
158 }
159
160 static inline void free_impl_var(impl_var *var) {
161 free(var->name);
162 free(var);
163 }
164
165 typedef struct impl_def_val {
166 token_t type;
167 union {
168 int64_t digits;
169 double decimals;
170 } v;
171 unsigned is_null:1;
172 } impl_def_val;
173
174 static inline impl_def_val *init_impl_def_val() {
175 impl_def_val *def = malloc(sizeof(*def));
176 def->type = 0;
177 def->is_null = 1;
178 return def;
179 }
180
181 static inline void free_impl_def_val(impl_def_val *def) {
182 free(def);
183 }
184
185 typedef struct impl_arg {
186 impl_type *type;
187 impl_var *var;
188 impl_def_val *def;
189 } impl_arg;
190
191 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
192 impl_arg *arg = malloc(sizeof(*arg));
193 arg->type = type;
194 arg->var = var;
195 arg->def = def;
196 return arg;
197 }
198
199 static inline void free_impl_arg(impl_arg *arg) {
200 free_impl_type(arg->type);
201 free_impl_var(arg->var);
202 if (arg->def) {
203 free_impl_def_val(arg->def);
204 }
205 free(arg);
206 }
207
208 typedef struct impl_args {
209 impl_arg **args;
210 size_t count;
211 } impl_args;
212
213 static inline impl_args *init_impl_args(impl_arg *arg) {
214 impl_args *args = malloc(sizeof(*args));
215 args->count = 1;
216 args->args = malloc(sizeof(*args->args));
217 args->args[0] = arg;
218 return args;
219 }
220
221 static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) {
222 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
223 args->args[args->count-1] = arg;
224 return args;
225 }
226
227 static inline void free_impl_args(impl_args *args) {
228 size_t i;
229
230 for (i = 0; i < args->count; ++i) {
231 free_impl_arg(args->args[i]);
232 }
233 free(args->args);
234 free(args);
235 }
236
237 typedef struct impl_func {
238 text *name;
239 impl_args *args;
240 impl_type *return_type;
241 } impl_func;
242
243 static inline impl_func *init_impl_func(text *name, impl_args *args, impl_type *type) {
244 impl_func *func = malloc(sizeof(*func));
245 func->name = (text *) strdup((const char *) name);
246 func->args = args;
247 func->return_type = type;
248 return func;
249 }
250
251 static inline void free_impl_func(impl_func *f) {
252 free_impl_type(f->return_type);
253 free_impl_args(f->args);
254 free(f->name);
255 free(f);
256 }
257
258 typedef struct let_func {
259 token_t type;
260 text *name;
261 } let_func;
262
263 static inline let_func *init_let_func(token_t type, text *name) {
264 let_func *func = malloc(sizeof(*func));
265 func->type = type;
266 func->name = (text *) strdup((const char *) name);
267 return func;
268 }
269
270 static inline void free_let_func(let_func *func) {
271 free(func->name);
272 free(func);
273 }
274
275 typedef struct let_value {
276 let_func *func;
277 impl_var *var;
278 unsigned null_pointer_ref:1;
279 } let_value;
280
281 static inline let_value *init_let_value(let_func *func, impl_var *var) {
282 let_value *val = malloc(sizeof(*val));
283
284 if (!func || !var) {
285 val->null_pointer_ref = 1;
286 } else {
287 val->null_pointer_ref = 0;
288 }
289 val->func = func;
290 val->var = var;
291
292 return val;
293 }
294
295 static inline void free_let_value(let_value *val) {
296 if (val->func) {
297 free_let_func(val->func);
298 }
299 if (val->var) {
300 free_impl_var(val->var);
301 }
302 free(val);
303 }
304
305 typedef struct let_stmt {
306 decl_var *var;
307 let_value *val;
308 } let_stmt;
309
310 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
311 let_stmt *let = malloc(sizeof(*let));
312 let->var = var;
313 let->val = val;
314 return let;
315 }
316
317 static inline void free_let_stmt(let_stmt *stmt) {
318 free_decl_var(stmt->var);
319 free_let_value(stmt->val);
320 free(stmt);
321 }
322
323 typedef struct set_func {
324 token_t type;
325 text *name;
326 } set_func;
327
328 static inline set_func *init_set_func(token_t type, text *name) {
329 set_func *func = malloc(sizeof(*func));
330 func->type = type;
331 func->name = (text *) strdup((const char *) name);
332 return func;
333 }
334
335 static inline void free_set_func(set_func *func) {
336 free(func->name);
337 free(func);
338 }
339
340 typedef struct set_value {
341 set_func *func;
342 decl_vars *vars;
343 } set_value;
344
345 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
346 set_value *val = malloc(sizeof(*val));
347 val->func = func;
348 val->vars = vars;
349 return val;
350 }
351
352 static inline void free_set_value(set_value *val) {
353 free_set_func(val->func);
354 free_decl_vars(val->vars);
355 free(val);
356 }
357
358 typedef struct set_stmt {
359 impl_var *var;
360 set_value *val;
361 } set_stmt;
362
363 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
364 set_stmt *set = malloc(sizeof(*set));
365 set->var = var;
366 set->val = val;
367 return set;
368 }
369
370 static inline void free_set_stmt(set_stmt *set) {
371 free_impl_var(set->var);
372 free_set_value(set->val);
373 free(set);
374 }
375
376 typedef struct ret_stmt {
377 set_func *func;
378 decl_var *decl;
379 } ret_stmt;
380
381 static inline ret_stmt *init_ret_stmt(set_func *func, decl_var *decl) {
382 ret_stmt *ret = malloc(sizeof(*ret));
383 ret->func = func;
384 ret->decl = decl;
385 return ret;
386 }
387
388 static inline void free_ret_stmt(ret_stmt *ret) {
389 free_set_func(ret->func);
390 free_decl_var(ret->decl);
391 free(ret);
392 }
393
394 typedef struct impl_stmt {
395 token_t type;
396 union {
397 let_stmt *let;
398 set_stmt *set;
399 ret_stmt *ret;
400 void *ptr;
401 } s;
402 } impl_stmt;
403
404 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
405 impl_stmt *stmt = malloc(sizeof(*stmt));
406 stmt->type = type;
407 stmt->s.ptr = ptr;
408 return stmt;
409 }
410
411 static inline void free_impl_stmt(impl_stmt *stmt) {
412 switch (stmt->type) {
413 case PSI_T_LET:
414 free_let_stmt(stmt->s.let);
415 break;
416 case PSI_T_SET:
417 free_set_stmt(stmt->s.set);
418 break;
419 case PSI_T_RET:
420 free_ret_stmt(stmt->s.ret);
421 break;
422 }
423 free(stmt);
424 }
425
426 typedef struct impl_stmts {
427 impl_stmt **stmts;
428 size_t count;
429 } impl_stmts;
430
431 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
432 impl_stmts *stmts = malloc(sizeof(*stmts));
433 stmts->count = 1;
434 stmts->stmts = malloc(sizeof(*stmts->stmts));
435 stmts->stmts[0] = stmt;
436 return stmts;
437 }
438
439 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
440 stmts->stmts = realloc(stmts->stmts, ++stmts->count * sizeof(*stmts->stmts));
441 stmts->stmts[stmts->count-1] = stmt;
442 return stmts;
443 }
444
445 static inline void free_impl_stmts(impl_stmts *stmts) {
446 size_t i;
447
448 for (i = 0; i < stmts->count; ++i) {
449 free_impl_stmt(stmts->stmts[i]);
450 }
451 free(stmts->stmts);
452 free(stmts);
453 }
454
455 typedef struct impl {
456 impl_func *func;
457 impl_stmts *stmts;
458 } impl;
459
460 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
461 impl *i = malloc(sizeof(*i));
462 i->func = func;
463 i->stmts = stmts;
464 return i;
465 }
466
467 static inline void free_impl(impl *impl) {
468 free_impl_func(impl->func);
469 free_impl_stmts(impl->stmts);
470 free(impl);
471 }