1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @file Stree (syntax tree) intermediate representation. */
|
---|
30 |
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <assert.h>
|
---|
34 | #include "list.h"
|
---|
35 | #include "mytypes.h"
|
---|
36 |
|
---|
37 | #include "stree.h"
|
---|
38 |
|
---|
39 | /** Allocate new module.
|
---|
40 | *
|
---|
41 | * @return New module
|
---|
42 | */
|
---|
43 | stree_module_t *stree_module_new(void)
|
---|
44 | {
|
---|
45 | stree_module_t *module;
|
---|
46 |
|
---|
47 | module = calloc(1, sizeof(stree_module_t));
|
---|
48 | if (module == NULL) {
|
---|
49 | printf("Memory allocation failed.\n");
|
---|
50 | exit(1);
|
---|
51 | }
|
---|
52 |
|
---|
53 | list_init(&module->members);
|
---|
54 | return module;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** Allocate new module member.
|
---|
58 | *
|
---|
59 | * @param mc Module member class
|
---|
60 | * @return New module member
|
---|
61 | */
|
---|
62 | stree_modm_t *stree_modm_new(modm_class_t mc)
|
---|
63 | {
|
---|
64 | stree_modm_t *modm;
|
---|
65 |
|
---|
66 | modm = calloc(1, sizeof(stree_modm_t));
|
---|
67 | if (modm == NULL) {
|
---|
68 | printf("Memory allocation failed.\n");
|
---|
69 | exit(1);
|
---|
70 | }
|
---|
71 |
|
---|
72 | modm->mc = mc;
|
---|
73 | return modm;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Allocate new CSI.
|
---|
77 | *
|
---|
78 | * @param cc CSI class
|
---|
79 | * @return New CSI
|
---|
80 | */
|
---|
81 | stree_csi_t *stree_csi_new(csi_class_t cc)
|
---|
82 | {
|
---|
83 | stree_csi_t *csi;
|
---|
84 |
|
---|
85 | csi = calloc(1, sizeof(stree_csi_t));
|
---|
86 | if (csi == NULL) {
|
---|
87 | printf("Memory allocation failed.\n");
|
---|
88 | exit(1);
|
---|
89 | }
|
---|
90 |
|
---|
91 | csi->cc = cc;
|
---|
92 | csi->ancr_state = ws_unvisited;
|
---|
93 | csi->name = NULL;
|
---|
94 | csi->base_csi = NULL;
|
---|
95 | list_init(&csi->inherit);
|
---|
96 | list_init(&csi->impl_if_ti);
|
---|
97 | list_init(&csi->members);
|
---|
98 |
|
---|
99 | return csi;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Allocate new CSI member.
|
---|
103 | *
|
---|
104 | * @param cc CSI member class
|
---|
105 | * @return New CSI member
|
---|
106 | */
|
---|
107 | stree_csimbr_t *stree_csimbr_new(csimbr_class_t cc)
|
---|
108 | {
|
---|
109 | stree_csimbr_t *csimbr;
|
---|
110 |
|
---|
111 | csimbr = calloc(1, sizeof(stree_csimbr_t));
|
---|
112 | if (csimbr == NULL) {
|
---|
113 | printf("Memory allocation failed.\n");
|
---|
114 | exit(1);
|
---|
115 | }
|
---|
116 |
|
---|
117 | csimbr->cc = cc;
|
---|
118 | return csimbr;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Allocate new constructor.
|
---|
122 | *
|
---|
123 | * @return New constructor
|
---|
124 | */
|
---|
125 | stree_ctor_t *stree_ctor_new(void)
|
---|
126 | {
|
---|
127 | stree_ctor_t *ctor;
|
---|
128 |
|
---|
129 | ctor = calloc(1, sizeof(stree_ctor_t));
|
---|
130 | if (ctor == NULL) {
|
---|
131 | printf("Memory allocation failed.\n");
|
---|
132 | exit(1);
|
---|
133 | }
|
---|
134 |
|
---|
135 | return ctor;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** Allocate new member delegate.
|
---|
139 | *
|
---|
140 | * @return New member delegate
|
---|
141 | */
|
---|
142 | stree_deleg_t *stree_deleg_new(void)
|
---|
143 | {
|
---|
144 | stree_deleg_t *deleg;
|
---|
145 |
|
---|
146 | deleg = calloc(1, sizeof(stree_deleg_t));
|
---|
147 | if (deleg == NULL) {
|
---|
148 | printf("Memory allocation failed.\n");
|
---|
149 | exit(1);
|
---|
150 | }
|
---|
151 |
|
---|
152 | return deleg;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** Allocate new enum.
|
---|
156 | *
|
---|
157 | * @return New enum
|
---|
158 | */
|
---|
159 | stree_enum_t *stree_enum_new(void)
|
---|
160 | {
|
---|
161 | stree_enum_t *enum_d;
|
---|
162 |
|
---|
163 | enum_d = calloc(1, sizeof(stree_enum_t));
|
---|
164 | if (enum_d == NULL) {
|
---|
165 | printf("Memory allocation failed.\n");
|
---|
166 | exit(1);
|
---|
167 | }
|
---|
168 |
|
---|
169 | return enum_d;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /** Allocate new enum member.
|
---|
173 | *
|
---|
174 | * @return New enum member
|
---|
175 | */
|
---|
176 | stree_embr_t *stree_embr_new(void)
|
---|
177 | {
|
---|
178 | stree_embr_t *embr;
|
---|
179 |
|
---|
180 | embr = calloc(1, sizeof(stree_embr_t));
|
---|
181 | if (embr == NULL) {
|
---|
182 | printf("Memory allocation failed.\n");
|
---|
183 | exit(1);
|
---|
184 | }
|
---|
185 |
|
---|
186 | return embr;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Allocate new member function.
|
---|
190 | *
|
---|
191 | * @return New member function
|
---|
192 | */
|
---|
193 | stree_fun_t *stree_fun_new(void)
|
---|
194 | {
|
---|
195 | stree_fun_t *fun;
|
---|
196 |
|
---|
197 | fun = calloc(1, sizeof(stree_fun_t));
|
---|
198 | if (fun == NULL) {
|
---|
199 | printf("Memory allocation failed.\n");
|
---|
200 | exit(1);
|
---|
201 | }
|
---|
202 |
|
---|
203 | return fun;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /** Allocate new member variable.
|
---|
207 | *
|
---|
208 | * @return New member variable
|
---|
209 | */
|
---|
210 | stree_var_t *stree_var_new(void)
|
---|
211 | {
|
---|
212 | stree_var_t *var;
|
---|
213 |
|
---|
214 | var = calloc(1, sizeof(stree_var_t));
|
---|
215 | if (var == NULL) {
|
---|
216 | printf("Memory allocation failed.\n");
|
---|
217 | exit(1);
|
---|
218 | }
|
---|
219 |
|
---|
220 | return var;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Allocate new property.
|
---|
224 | *
|
---|
225 | * @return New property
|
---|
226 | */
|
---|
227 | stree_prop_t *stree_prop_new(void)
|
---|
228 | {
|
---|
229 | stree_prop_t *prop;
|
---|
230 |
|
---|
231 | prop = calloc(1, sizeof(stree_prop_t));
|
---|
232 | if (prop == NULL) {
|
---|
233 | printf("Memory allocation failed.\n");
|
---|
234 | exit(1);
|
---|
235 | }
|
---|
236 |
|
---|
237 | return prop;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /** Allocate new type argument.
|
---|
241 | *
|
---|
242 | * @return New type argument
|
---|
243 | */
|
---|
244 | stree_targ_t *stree_targ_new(void)
|
---|
245 | {
|
---|
246 | stree_targ_t *targ;
|
---|
247 |
|
---|
248 | targ = calloc(1, sizeof(stree_targ_t));
|
---|
249 | if (targ == NULL) {
|
---|
250 | printf("Memory allocation failed.\n");
|
---|
251 | exit(1);
|
---|
252 | }
|
---|
253 |
|
---|
254 | return targ;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** Allocate new symbol attribute.
|
---|
258 | *
|
---|
259 | * @param sac Symbol attribute class
|
---|
260 | * @return New symbol attribute
|
---|
261 | */
|
---|
262 | stree_symbol_attr_t *stree_symbol_attr_new(symbol_attr_class_t sac)
|
---|
263 | {
|
---|
264 | stree_symbol_attr_t *symbol_attr;
|
---|
265 |
|
---|
266 | symbol_attr = calloc(1, sizeof(stree_symbol_attr_t));
|
---|
267 | if (symbol_attr == NULL) {
|
---|
268 | printf("Memory allocation failed.\n");
|
---|
269 | exit(1);
|
---|
270 | }
|
---|
271 |
|
---|
272 | symbol_attr->sac = sac;
|
---|
273 | return symbol_attr;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /** Allocate new procedure.
|
---|
277 | *
|
---|
278 | * @return New procedure
|
---|
279 | */
|
---|
280 | stree_proc_t *stree_proc_new(void)
|
---|
281 | {
|
---|
282 | stree_proc_t *proc;
|
---|
283 |
|
---|
284 | proc = calloc(1, sizeof(stree_proc_t));
|
---|
285 | if (proc == NULL) {
|
---|
286 | printf("Memory allocation failed.\n");
|
---|
287 | exit(1);
|
---|
288 | }
|
---|
289 |
|
---|
290 | return proc;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** Allocate new procedure argument.
|
---|
294 | *
|
---|
295 | * @return New procedure argument
|
---|
296 | */
|
---|
297 | stree_proc_arg_t *stree_proc_arg_new(void)
|
---|
298 | {
|
---|
299 | stree_proc_arg_t *proc_arg;
|
---|
300 |
|
---|
301 | proc_arg = calloc(1, sizeof(stree_proc_arg_t));
|
---|
302 | if (proc_arg == NULL) {
|
---|
303 | printf("Memory allocation failed.\n");
|
---|
304 | exit(1);
|
---|
305 | }
|
---|
306 |
|
---|
307 | return proc_arg;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /** Allocate new function signature.
|
---|
311 | *
|
---|
312 | * @return New procedure argument
|
---|
313 | */
|
---|
314 | stree_fun_sig_t *stree_fun_sig_new(void)
|
---|
315 | {
|
---|
316 | stree_fun_sig_t *fun_sig;
|
---|
317 |
|
---|
318 | fun_sig = calloc(1, sizeof(stree_fun_sig_t));
|
---|
319 | if (fun_sig == NULL) {
|
---|
320 | printf("Memory allocation failed.\n");
|
---|
321 | exit(1);
|
---|
322 | }
|
---|
323 |
|
---|
324 | return fun_sig;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /** Allocate new procedure argument attribute.
|
---|
328 | *
|
---|
329 | * @param Argument attribute class
|
---|
330 | * @return New procedure argument attribute
|
---|
331 | */
|
---|
332 | stree_arg_attr_t *stree_arg_attr_new(arg_attr_class_t aac)
|
---|
333 | {
|
---|
334 | stree_arg_attr_t *arg_attr;
|
---|
335 |
|
---|
336 | arg_attr = calloc(1, sizeof(stree_arg_attr_t));
|
---|
337 | if (arg_attr == NULL) {
|
---|
338 | printf("Memory allocation failed.\n");
|
---|
339 | exit(1);
|
---|
340 | }
|
---|
341 |
|
---|
342 | arg_attr->aac = aac;
|
---|
343 | return arg_attr;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /** Allocate new statement.
|
---|
347 | *
|
---|
348 | * @param sc Statement class
|
---|
349 | * @return New statement
|
---|
350 | */
|
---|
351 | stree_stat_t *stree_stat_new(stat_class_t sc)
|
---|
352 | {
|
---|
353 | stree_stat_t *stat;
|
---|
354 |
|
---|
355 | stat = calloc(1, sizeof(stree_stat_t));
|
---|
356 | if (stat == NULL) {
|
---|
357 | printf("Memory allocation failed.\n");
|
---|
358 | exit(1);
|
---|
359 | }
|
---|
360 |
|
---|
361 | stat->sc = sc;
|
---|
362 | return stat;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /** Allocate new local variable declaration.
|
---|
366 | *
|
---|
367 | * @return New local variable declaration
|
---|
368 | */
|
---|
369 | stree_vdecl_t *stree_vdecl_new(void)
|
---|
370 | {
|
---|
371 | stree_vdecl_t *vdecl_s;
|
---|
372 |
|
---|
373 | vdecl_s = calloc(1, sizeof(stree_vdecl_t));
|
---|
374 | if (vdecl_s == NULL) {
|
---|
375 | printf("Memory allocation failed.\n");
|
---|
376 | exit(1);
|
---|
377 | }
|
---|
378 |
|
---|
379 | return vdecl_s;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /** Allocate new @c if statement.
|
---|
383 | *
|
---|
384 | * @return New @c if statement
|
---|
385 | */
|
---|
386 | stree_if_t *stree_if_new(void)
|
---|
387 | {
|
---|
388 | stree_if_t *if_s;
|
---|
389 |
|
---|
390 | if_s = calloc(1, sizeof(stree_if_t));
|
---|
391 | if (if_s == NULL) {
|
---|
392 | printf("Memory allocation failed.\n");
|
---|
393 | exit(1);
|
---|
394 | }
|
---|
395 |
|
---|
396 | return if_s;
|
---|
397 | }
|
---|
398 |
|
---|
399 | /** Allocate new @c switch statement.
|
---|
400 | *
|
---|
401 | * @return New @c if statement
|
---|
402 | */
|
---|
403 | stree_switch_t *stree_switch_new(void)
|
---|
404 | {
|
---|
405 | stree_switch_t *switch_s;
|
---|
406 |
|
---|
407 | switch_s = calloc(1, sizeof(stree_switch_t));
|
---|
408 | if (switch_s == NULL) {
|
---|
409 | printf("Memory allocation failed.\n");
|
---|
410 | exit(1);
|
---|
411 | }
|
---|
412 |
|
---|
413 | return switch_s;
|
---|
414 | }
|
---|
415 |
|
---|
416 | /** Allocate new @c while statement.
|
---|
417 | *
|
---|
418 | * @return New @c while statement
|
---|
419 | */
|
---|
420 | stree_while_t *stree_while_new(void)
|
---|
421 | {
|
---|
422 | stree_while_t *while_s;
|
---|
423 |
|
---|
424 | while_s = calloc(1, sizeof(stree_while_t));
|
---|
425 | if (while_s == NULL) {
|
---|
426 | printf("Memory allocation failed.\n");
|
---|
427 | exit(1);
|
---|
428 | }
|
---|
429 |
|
---|
430 | return while_s;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /** Allocate new @c for statement.
|
---|
434 | *
|
---|
435 | * @return New @c for statement
|
---|
436 | */
|
---|
437 | stree_for_t *stree_for_new(void)
|
---|
438 | {
|
---|
439 | stree_for_t *for_s;
|
---|
440 |
|
---|
441 | for_s = calloc(1, sizeof(stree_for_t));
|
---|
442 | if (for_s == NULL) {
|
---|
443 | printf("Memory allocation failed.\n");
|
---|
444 | exit(1);
|
---|
445 | }
|
---|
446 |
|
---|
447 | return for_s;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /** Allocate new @c raise statement.
|
---|
451 | *
|
---|
452 | * @return New @c raise statement
|
---|
453 | */
|
---|
454 | stree_raise_t *stree_raise_new(void)
|
---|
455 | {
|
---|
456 | stree_raise_t *raise_s;
|
---|
457 |
|
---|
458 | raise_s = calloc(1, sizeof(stree_raise_t));
|
---|
459 | if (raise_s == NULL) {
|
---|
460 | printf("Memory allocation failed.\n");
|
---|
461 | exit(1);
|
---|
462 | }
|
---|
463 |
|
---|
464 | return raise_s;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /** Allocate new @c break statement.
|
---|
468 | *
|
---|
469 | * @return New @c break statement
|
---|
470 | */
|
---|
471 | stree_break_t *stree_break_new(void)
|
---|
472 | {
|
---|
473 | stree_break_t *break_s;
|
---|
474 |
|
---|
475 | break_s = calloc(1, sizeof(stree_break_t));
|
---|
476 | if (break_s == NULL) {
|
---|
477 | printf("Memory allocation failed.\n");
|
---|
478 | exit(1);
|
---|
479 | }
|
---|
480 |
|
---|
481 | return break_s;
|
---|
482 | }
|
---|
483 |
|
---|
484 | /** Allocate new @c return statement.
|
---|
485 | *
|
---|
486 | * @return New @c return statement
|
---|
487 | */
|
---|
488 | stree_return_t *stree_return_new(void)
|
---|
489 | {
|
---|
490 | stree_return_t *return_s;
|
---|
491 |
|
---|
492 | return_s = calloc(1, sizeof(stree_return_t));
|
---|
493 | if (return_s == NULL) {
|
---|
494 | printf("Memory allocation failed.\n");
|
---|
495 | exit(1);
|
---|
496 | }
|
---|
497 |
|
---|
498 | return return_s;
|
---|
499 | }
|
---|
500 |
|
---|
501 | /** Allocate new with-except-finally statement.
|
---|
502 | *
|
---|
503 | * @return New with-except-finally statement.
|
---|
504 | */
|
---|
505 | stree_wef_t *stree_wef_new(void)
|
---|
506 | {
|
---|
507 | stree_wef_t *wef_s;
|
---|
508 |
|
---|
509 | wef_s = calloc(1, sizeof(stree_wef_t));
|
---|
510 | if (wef_s == NULL) {
|
---|
511 | printf("Memory allocation failed.\n");
|
---|
512 | exit(1);
|
---|
513 | }
|
---|
514 |
|
---|
515 | return wef_s;
|
---|
516 | }
|
---|
517 |
|
---|
518 | /** Allocate new expression statement.
|
---|
519 | *
|
---|
520 | * @return New expression statement
|
---|
521 | */
|
---|
522 | stree_exps_t *stree_exps_new(void)
|
---|
523 | {
|
---|
524 | stree_exps_t *exp_s;
|
---|
525 |
|
---|
526 | exp_s = calloc(1, sizeof(stree_exps_t));
|
---|
527 | if (exp_s == NULL) {
|
---|
528 | printf("Memory allocation failed.\n");
|
---|
529 | exit(1);
|
---|
530 | }
|
---|
531 |
|
---|
532 | return exp_s;
|
---|
533 | }
|
---|
534 |
|
---|
535 | /** Allocate new @c except clause.
|
---|
536 | *
|
---|
537 | * @return New @c except clause
|
---|
538 | */
|
---|
539 | stree_except_t *stree_except_new(void)
|
---|
540 | {
|
---|
541 | stree_except_t *except_c;
|
---|
542 |
|
---|
543 | except_c = calloc(1, sizeof(stree_except_t));
|
---|
544 | if (except_c == NULL) {
|
---|
545 | printf("Memory allocation failed.\n");
|
---|
546 | exit(1);
|
---|
547 | }
|
---|
548 |
|
---|
549 | return except_c;
|
---|
550 | }
|
---|
551 |
|
---|
552 | /** Allocate new @c if/elif clause.
|
---|
553 | *
|
---|
554 | * @return New @c if/elif clause
|
---|
555 | */
|
---|
556 | stree_if_clause_t *stree_if_clause_new(void)
|
---|
557 | {
|
---|
558 | stree_if_clause_t *if_clause;
|
---|
559 |
|
---|
560 | if_clause = calloc(1, sizeof(stree_if_clause_t));
|
---|
561 | if (if_clause == NULL) {
|
---|
562 | printf("Memory allocation failed.\n");
|
---|
563 | exit(1);
|
---|
564 | }
|
---|
565 |
|
---|
566 | return if_clause;
|
---|
567 | }
|
---|
568 |
|
---|
569 | /** Allocate new @c when clause.
|
---|
570 | *
|
---|
571 | * @return New @c when clause
|
---|
572 | */
|
---|
573 | stree_when_t *stree_when_new(void)
|
---|
574 | {
|
---|
575 | stree_when_t *when_c;
|
---|
576 |
|
---|
577 | when_c = calloc(1, sizeof(stree_when_t));
|
---|
578 | if (when_c == NULL) {
|
---|
579 | printf("Memory allocation failed.\n");
|
---|
580 | exit(1);
|
---|
581 | }
|
---|
582 |
|
---|
583 | return when_c;
|
---|
584 | }
|
---|
585 |
|
---|
586 | /** Allocate new statement block.
|
---|
587 | *
|
---|
588 | * @return New statement block
|
---|
589 | */
|
---|
590 | stree_block_t *stree_block_new(void)
|
---|
591 | {
|
---|
592 | stree_block_t *block;
|
---|
593 |
|
---|
594 | block = calloc(1, sizeof(stree_block_t));
|
---|
595 | if (block == NULL) {
|
---|
596 | printf("Memory allocation failed.\n");
|
---|
597 | exit(1);
|
---|
598 | }
|
---|
599 |
|
---|
600 | return block;
|
---|
601 | }
|
---|
602 |
|
---|
603 | /** Allocate new expression.
|
---|
604 | *
|
---|
605 | * @param ec Expression class
|
---|
606 | * @return New expression
|
---|
607 | */
|
---|
608 | stree_expr_t *stree_expr_new(expr_class_t ec)
|
---|
609 | {
|
---|
610 | stree_expr_t *expr;
|
---|
611 |
|
---|
612 | expr = calloc(1, sizeof(stree_expr_t));
|
---|
613 | if (expr == NULL) {
|
---|
614 | printf("Memory allocation failed.\n");
|
---|
615 | exit(1);
|
---|
616 | }
|
---|
617 |
|
---|
618 | expr->ec = ec;
|
---|
619 | return expr;
|
---|
620 | }
|
---|
621 |
|
---|
622 | /** Allocate new assignment.
|
---|
623 | *
|
---|
624 | * @param ac Assignment class
|
---|
625 | * @return New assignment
|
---|
626 | */
|
---|
627 | stree_assign_t *stree_assign_new(assign_class_t ac)
|
---|
628 | {
|
---|
629 | stree_assign_t *assign;
|
---|
630 |
|
---|
631 | assign = calloc(1, sizeof(stree_assign_t));
|
---|
632 | if (assign == NULL) {
|
---|
633 | printf("Memory allocation failed.\n");
|
---|
634 | exit(1);
|
---|
635 | }
|
---|
636 |
|
---|
637 | assign->ac = ac;
|
---|
638 | return assign;
|
---|
639 | }
|
---|
640 |
|
---|
641 | /** Allocate new binary operation.
|
---|
642 | *
|
---|
643 | * @return New binary operation
|
---|
644 | */
|
---|
645 | stree_binop_t *stree_binop_new(binop_class_t bc)
|
---|
646 | {
|
---|
647 | stree_binop_t *binop;
|
---|
648 |
|
---|
649 | binop = calloc(1, sizeof(stree_binop_t));
|
---|
650 | if (binop == NULL) {
|
---|
651 | printf("Memory allocation failed.\n");
|
---|
652 | exit(1);
|
---|
653 | }
|
---|
654 |
|
---|
655 | binop->bc = bc;
|
---|
656 | return binop;
|
---|
657 | }
|
---|
658 |
|
---|
659 | /** Allocate new unary operation.
|
---|
660 | *
|
---|
661 | * @param uc Unary operation class
|
---|
662 | * @return New unary operation
|
---|
663 | */
|
---|
664 | stree_unop_t *stree_unop_new(unop_class_t uc)
|
---|
665 | {
|
---|
666 | stree_unop_t *unop;
|
---|
667 |
|
---|
668 | unop = calloc(1, sizeof(stree_unop_t));
|
---|
669 | if (unop == NULL) {
|
---|
670 | printf("Memory allocation failed.\n");
|
---|
671 | exit(1);
|
---|
672 | }
|
---|
673 |
|
---|
674 | unop->uc = uc;
|
---|
675 | return unop;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /** Allocate new @c new operation.
|
---|
679 | *
|
---|
680 | * @return New @c new operation
|
---|
681 | */
|
---|
682 | stree_new_t *stree_new_new(void)
|
---|
683 | {
|
---|
684 | stree_new_t *new_op;
|
---|
685 |
|
---|
686 | new_op = calloc(1, sizeof(stree_new_t));
|
---|
687 | if (new_op == NULL) {
|
---|
688 | printf("Memory allocation failed.\n");
|
---|
689 | exit(1);
|
---|
690 | }
|
---|
691 |
|
---|
692 | return new_op;
|
---|
693 | }
|
---|
694 |
|
---|
695 | /** Allocate new .
|
---|
696 | *
|
---|
697 | * @return New
|
---|
698 | */
|
---|
699 | stree_access_t *stree_access_new(void)
|
---|
700 | {
|
---|
701 | stree_access_t *access;
|
---|
702 |
|
---|
703 | access = calloc(1, sizeof(stree_access_t));
|
---|
704 | if (access == NULL) {
|
---|
705 | printf("Memory allocation failed.\n");
|
---|
706 | exit(1);
|
---|
707 | }
|
---|
708 |
|
---|
709 | return access;
|
---|
710 | }
|
---|
711 |
|
---|
712 | /** Allocate new function call operation.
|
---|
713 | *
|
---|
714 | * @return New function call operation
|
---|
715 | */
|
---|
716 | stree_call_t *stree_call_new(void)
|
---|
717 | {
|
---|
718 | stree_call_t *call;
|
---|
719 |
|
---|
720 | call = calloc(1, sizeof(stree_call_t));
|
---|
721 | if (call == NULL) {
|
---|
722 | printf("Memory allocation failed.\n");
|
---|
723 | exit(1);
|
---|
724 | }
|
---|
725 |
|
---|
726 | return call;
|
---|
727 | }
|
---|
728 |
|
---|
729 | /** Allocate new indexing operation.
|
---|
730 | *
|
---|
731 | * @return New indexing operation
|
---|
732 | */
|
---|
733 | stree_index_t *stree_index_new(void)
|
---|
734 | {
|
---|
735 | stree_index_t *index;
|
---|
736 |
|
---|
737 | index = calloc(1, sizeof(stree_index_t));
|
---|
738 | if (index == NULL) {
|
---|
739 | printf("Memory allocation failed.\n");
|
---|
740 | exit(1);
|
---|
741 | }
|
---|
742 |
|
---|
743 | return index;
|
---|
744 | }
|
---|
745 |
|
---|
746 | /** Allocate new as conversion.
|
---|
747 | *
|
---|
748 | * @return New as conversion
|
---|
749 | */
|
---|
750 | stree_as_t *stree_as_new(void)
|
---|
751 | {
|
---|
752 | stree_as_t *as_expr;
|
---|
753 |
|
---|
754 | as_expr = calloc(1, sizeof(stree_as_t));
|
---|
755 | if (as_expr == NULL) {
|
---|
756 | printf("Memory allocation failed.\n");
|
---|
757 | exit(1);
|
---|
758 | }
|
---|
759 |
|
---|
760 | return as_expr;
|
---|
761 | }
|
---|
762 |
|
---|
763 | /** Allocate new boxing operation.
|
---|
764 | *
|
---|
765 | * @return New boxing operation
|
---|
766 | */
|
---|
767 | stree_box_t *stree_box_new(void)
|
---|
768 | {
|
---|
769 | stree_box_t *box_expr;
|
---|
770 |
|
---|
771 | box_expr = calloc(1, sizeof(stree_box_t));
|
---|
772 | if (box_expr == NULL) {
|
---|
773 | printf("Memory allocation failed.\n");
|
---|
774 | exit(1);
|
---|
775 | }
|
---|
776 |
|
---|
777 | return box_expr;
|
---|
778 | }
|
---|
779 |
|
---|
780 | /** Allocate new name reference operation.
|
---|
781 | *
|
---|
782 | * @return New name reference operation
|
---|
783 | */
|
---|
784 | stree_nameref_t *stree_nameref_new(void)
|
---|
785 | {
|
---|
786 | stree_nameref_t *nameref;
|
---|
787 |
|
---|
788 | nameref = calloc(1, sizeof(stree_nameref_t));
|
---|
789 | if (nameref == NULL) {
|
---|
790 | printf("Memory allocation failed.\n");
|
---|
791 | exit(1);
|
---|
792 | }
|
---|
793 |
|
---|
794 | return nameref;
|
---|
795 | }
|
---|
796 |
|
---|
797 | /** Allocate new identifier.
|
---|
798 | *
|
---|
799 | * @return New identifier
|
---|
800 | */
|
---|
801 | stree_ident_t *stree_ident_new(void)
|
---|
802 | {
|
---|
803 | stree_ident_t *ident;
|
---|
804 |
|
---|
805 | ident = calloc(1, sizeof(stree_ident_t));
|
---|
806 | if (ident == NULL) {
|
---|
807 | printf("Memory allocation failed.\n");
|
---|
808 | exit(1);
|
---|
809 | }
|
---|
810 |
|
---|
811 | return ident;
|
---|
812 | }
|
---|
813 |
|
---|
814 | /** Allocate new literal.
|
---|
815 | *
|
---|
816 | * @param ltc Literal class
|
---|
817 | * @return New literal
|
---|
818 | */
|
---|
819 | stree_literal_t *stree_literal_new(literal_class_t ltc)
|
---|
820 | {
|
---|
821 | stree_literal_t *literal;
|
---|
822 |
|
---|
823 | literal = calloc(1, sizeof(stree_literal_t));
|
---|
824 | if (literal == NULL) {
|
---|
825 | printf("Memory allocation failed.\n");
|
---|
826 | exit(1);
|
---|
827 | }
|
---|
828 |
|
---|
829 | literal->ltc = ltc;
|
---|
830 | return literal;
|
---|
831 | }
|
---|
832 |
|
---|
833 | /** Allocate new @c self reference.
|
---|
834 | *
|
---|
835 | * @return New @c self reference
|
---|
836 | */
|
---|
837 | stree_self_ref_t *stree_self_ref_new(void)
|
---|
838 | {
|
---|
839 | stree_self_ref_t *self_ref;
|
---|
840 |
|
---|
841 | self_ref = calloc(1, sizeof(stree_self_ref_t));
|
---|
842 | if (self_ref == NULL) {
|
---|
843 | printf("Memory allocation failed.\n");
|
---|
844 | exit(1);
|
---|
845 | }
|
---|
846 |
|
---|
847 | return self_ref;
|
---|
848 | }
|
---|
849 |
|
---|
850 | /** Allocate new type expression
|
---|
851 | *
|
---|
852 | * @return New type expression
|
---|
853 | */
|
---|
854 | stree_texpr_t *stree_texpr_new(texpr_class_t tc)
|
---|
855 | {
|
---|
856 | stree_texpr_t *texpr;
|
---|
857 |
|
---|
858 | texpr = calloc(1, sizeof(stree_texpr_t));
|
---|
859 | if (texpr == NULL) {
|
---|
860 | printf("Memory allocation failed.\n");
|
---|
861 | exit(1);
|
---|
862 | }
|
---|
863 |
|
---|
864 | texpr->tc = tc;
|
---|
865 | return texpr;
|
---|
866 | }
|
---|
867 |
|
---|
868 | /** Allocate new type access operation.
|
---|
869 | *
|
---|
870 | * @return New type access operation
|
---|
871 | */
|
---|
872 | stree_taccess_t *stree_taccess_new(void)
|
---|
873 | {
|
---|
874 | stree_taccess_t *taccess;
|
---|
875 |
|
---|
876 | taccess = calloc(1, sizeof(stree_taccess_t));
|
---|
877 | if (taccess == NULL) {
|
---|
878 | printf("Memory allocation failed.\n");
|
---|
879 | exit(1);
|
---|
880 | }
|
---|
881 |
|
---|
882 | return taccess;
|
---|
883 | }
|
---|
884 |
|
---|
885 | /** Allocate new type application operation.
|
---|
886 | *
|
---|
887 | * @return New type application operation
|
---|
888 | */
|
---|
889 | stree_tapply_t *stree_tapply_new(void)
|
---|
890 | {
|
---|
891 | stree_tapply_t *tapply;
|
---|
892 |
|
---|
893 | tapply = calloc(1, sizeof(stree_tapply_t));
|
---|
894 | if (tapply == NULL) {
|
---|
895 | printf("Memory allocation failed.\n");
|
---|
896 | exit(1);
|
---|
897 | }
|
---|
898 |
|
---|
899 | return tapply;
|
---|
900 | }
|
---|
901 |
|
---|
902 | /** Allocate new type indexing operation.
|
---|
903 | *
|
---|
904 | * @return New type indexing operation
|
---|
905 | */
|
---|
906 | stree_tindex_t *stree_tindex_new(void)
|
---|
907 | {
|
---|
908 | stree_tindex_t *tindex;
|
---|
909 |
|
---|
910 | tindex = calloc(1, sizeof(stree_tindex_t));
|
---|
911 | if (tindex == NULL) {
|
---|
912 | printf("Memory allocation failed.\n");
|
---|
913 | exit(1);
|
---|
914 | }
|
---|
915 |
|
---|
916 | return tindex;
|
---|
917 | }
|
---|
918 |
|
---|
919 | /** Allocate new type literal.
|
---|
920 | *
|
---|
921 | * @return New type literal
|
---|
922 | */
|
---|
923 | stree_tliteral_t *stree_tliteral_new(tliteral_class_t tlc)
|
---|
924 | {
|
---|
925 | stree_tliteral_t *tliteral;
|
---|
926 |
|
---|
927 | tliteral = calloc(1, sizeof(stree_tliteral_t));
|
---|
928 | if (tliteral == NULL) {
|
---|
929 | printf("Memory allocation failed.\n");
|
---|
930 | exit(1);
|
---|
931 | }
|
---|
932 |
|
---|
933 | tliteral->tlc = tlc;
|
---|
934 | return tliteral;
|
---|
935 | }
|
---|
936 |
|
---|
937 | /** Allocate new type name reference.
|
---|
938 | *
|
---|
939 | * @return New type name reference
|
---|
940 | */
|
---|
941 | stree_tnameref_t *stree_tnameref_new(void)
|
---|
942 | {
|
---|
943 | stree_tnameref_t *tnameref;
|
---|
944 |
|
---|
945 | tnameref = calloc(1, sizeof(stree_tnameref_t));
|
---|
946 | if (tnameref == NULL) {
|
---|
947 | printf("Memory allocation failed.\n");
|
---|
948 | exit(1);
|
---|
949 | }
|
---|
950 |
|
---|
951 | return tnameref;
|
---|
952 | }
|
---|
953 |
|
---|
954 | /** Allocate new symbol.
|
---|
955 | *
|
---|
956 | * @return New symbol
|
---|
957 | */
|
---|
958 | stree_symbol_t *stree_symbol_new(symbol_class_t sc)
|
---|
959 | {
|
---|
960 | stree_symbol_t *symbol;
|
---|
961 |
|
---|
962 | symbol = calloc(1, sizeof(stree_symbol_t));
|
---|
963 | if (symbol == NULL) {
|
---|
964 | printf("Memory allocation failed.\n");
|
---|
965 | exit(1);
|
---|
966 | }
|
---|
967 |
|
---|
968 | symbol->sc = sc;
|
---|
969 | list_init(&symbol->attr);
|
---|
970 |
|
---|
971 | return symbol;
|
---|
972 | }
|
---|
973 |
|
---|
974 | /** Allocate new program.
|
---|
975 | *
|
---|
976 | * @return New program
|
---|
977 | */
|
---|
978 | stree_program_t *stree_program_new(void)
|
---|
979 | {
|
---|
980 | stree_program_t *program;
|
---|
981 |
|
---|
982 | program = calloc(1, sizeof(stree_program_t));
|
---|
983 | if (program == NULL) {
|
---|
984 | printf("Memory allocation failed.\n");
|
---|
985 | exit(1);
|
---|
986 | }
|
---|
987 |
|
---|
988 | return program;
|
---|
989 | }
|
---|
990 |
|
---|
991 | /** Determine if @a symbol has attribute of class @a sac.
|
---|
992 | *
|
---|
993 | * @param symbol Symbol
|
---|
994 | * @param sac Symbol attribute class
|
---|
995 | * @return @c b_true if yes, @c b_false if no
|
---|
996 | */
|
---|
997 | bool_t stree_symbol_has_attr(stree_symbol_t *symbol, symbol_attr_class_t sac)
|
---|
998 | {
|
---|
999 | list_node_t *node;
|
---|
1000 | stree_symbol_attr_t *attr;
|
---|
1001 |
|
---|
1002 | node = list_first(&symbol->attr);
|
---|
1003 | while (node != NULL) {
|
---|
1004 | attr = list_node_data(node, stree_symbol_attr_t *);
|
---|
1005 | if (attr->sac == sac)
|
---|
1006 | return b_true;
|
---|
1007 |
|
---|
1008 | node = list_next(&symbol->attr, node);
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | return b_false;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | /** Determine if argument @a arg has attribute of class @a aac.
|
---|
1015 | *
|
---|
1016 | * @param arg Formal procedure argument
|
---|
1017 | * @param aac Argument attribute class
|
---|
1018 | * @return @c b_true if yes, @c b_false if no.
|
---|
1019 | */
|
---|
1020 | bool_t stree_arg_has_attr(stree_proc_arg_t *arg, arg_attr_class_t aac)
|
---|
1021 | {
|
---|
1022 | list_node_t *node;
|
---|
1023 | stree_arg_attr_t *attr;
|
---|
1024 |
|
---|
1025 | node = list_first(&arg->attr);
|
---|
1026 | while (node != NULL) {
|
---|
1027 | attr = list_node_data(node, stree_arg_attr_t *);
|
---|
1028 | if (attr->aac == aac)
|
---|
1029 | return b_true;
|
---|
1030 |
|
---|
1031 | node = list_next(&arg->attr, node);
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | return b_false;
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | /** Determine wheter @a a is derived (transitively) from @a b.
|
---|
1038 | *
|
---|
1039 | * XXX This does not work right with generics.
|
---|
1040 | *
|
---|
1041 | * @param a Derived CSI.
|
---|
1042 | * @param b Base CSI.
|
---|
1043 | * @return @c b_true if @a a is equal to or directly or indirectly
|
---|
1044 | * derived from @a b.
|
---|
1045 | */
|
---|
1046 | bool_t stree_is_csi_derived_from_csi(stree_csi_t *a, stree_csi_t *b)
|
---|
1047 | {
|
---|
1048 | stree_csi_t *csi;
|
---|
1049 |
|
---|
1050 | csi = a;
|
---|
1051 | while (csi != NULL) {
|
---|
1052 | if (csi == b)
|
---|
1053 | return b_true;
|
---|
1054 |
|
---|
1055 | csi = csi->base_csi;
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | /* We went all the way to the root and did not find b. */
|
---|
1059 | return b_false;
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | /** Determine if @a symbol is static.
|
---|
1063 | *
|
---|
1064 | * @param symbol Symbol
|
---|
1065 | * @return @c b_true if symbol is static, @c b_false otherwise
|
---|
1066 | */
|
---|
1067 | bool_t stree_symbol_is_static(stree_symbol_t *symbol)
|
---|
1068 | {
|
---|
1069 | /* Module-wide symbols are static. */
|
---|
1070 | if (symbol->outer_csi == NULL)
|
---|
1071 | return b_true;
|
---|
1072 |
|
---|
1073 | /* Symbols with @c static attribute are static. */
|
---|
1074 | if (stree_symbol_has_attr(symbol, sac_static))
|
---|
1075 | return b_true;
|
---|
1076 |
|
---|
1077 | switch (symbol->sc) {
|
---|
1078 | case sc_csi:
|
---|
1079 | case sc_deleg:
|
---|
1080 | case sc_enum:
|
---|
1081 | return b_true;
|
---|
1082 | case sc_ctor:
|
---|
1083 | case sc_fun:
|
---|
1084 | case sc_var:
|
---|
1085 | case sc_prop:
|
---|
1086 | break;
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | return b_false;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | /** Search for CSI type argument of the given name.
|
---|
1093 | *
|
---|
1094 | * @param csi CSI to look in
|
---|
1095 | * @param ident Identifier of the type argument
|
---|
1096 | * @return Type argument declaration or @c NULL if not found
|
---|
1097 | */
|
---|
1098 | stree_targ_t *stree_csi_find_targ(stree_csi_t *csi, stree_ident_t *ident)
|
---|
1099 | {
|
---|
1100 | list_node_t *targ_n;
|
---|
1101 | stree_targ_t *targ;
|
---|
1102 |
|
---|
1103 | targ_n = list_first(&csi->targ);
|
---|
1104 | while (targ_n != NULL) {
|
---|
1105 | targ = list_node_data(targ_n, stree_targ_t *);
|
---|
1106 | if (targ->name->sid == ident->sid)
|
---|
1107 | return targ;
|
---|
1108 |
|
---|
1109 | targ_n = list_next(&csi->targ, targ_n);
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | /* No match */
|
---|
1113 | return NULL;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | /** Search for enum member of the given name.
|
---|
1117 | *
|
---|
1118 | * @param enum_d Enum to look in
|
---|
1119 | * @param ident Identifier of the enum member
|
---|
1120 | * @return Enum member declaration or @c NULL if not found
|
---|
1121 | */
|
---|
1122 | stree_embr_t *stree_enum_find_mbr(stree_enum_t *enum_d, stree_ident_t *ident)
|
---|
1123 | {
|
---|
1124 | list_node_t *embr_n;
|
---|
1125 | stree_embr_t *embr;
|
---|
1126 |
|
---|
1127 | embr_n = list_first(&enum_d->members);
|
---|
1128 | while (embr_n != NULL) {
|
---|
1129 | embr = list_node_data(embr_n, stree_embr_t *);
|
---|
1130 | if (embr->name->sid == ident->sid)
|
---|
1131 | return embr;
|
---|
1132 |
|
---|
1133 | embr_n = list_next(&enum_d->members, embr_n);
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | /* No match */
|
---|
1137 | return NULL;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | /** Get CSI member name.
|
---|
1141 | *
|
---|
1142 | * @param csimbr CSI member
|
---|
1143 | * @return Member name
|
---|
1144 | */
|
---|
1145 | stree_ident_t *stree_csimbr_get_name(stree_csimbr_t *csimbr)
|
---|
1146 | {
|
---|
1147 | stree_ident_t *mbr_name;
|
---|
1148 |
|
---|
1149 | /* Keep compiler happy. */
|
---|
1150 | mbr_name = NULL;
|
---|
1151 |
|
---|
1152 | switch (csimbr->cc) {
|
---|
1153 | case csimbr_csi:
|
---|
1154 | mbr_name = csimbr->u.csi->name;
|
---|
1155 | break;
|
---|
1156 | case csimbr_ctor:
|
---|
1157 | mbr_name = csimbr->u.ctor->name;
|
---|
1158 | break;
|
---|
1159 | case csimbr_deleg:
|
---|
1160 | mbr_name = csimbr->u.deleg->name;
|
---|
1161 | break;
|
---|
1162 | case csimbr_enum:
|
---|
1163 | mbr_name = csimbr->u.enum_d->name;
|
---|
1164 | break;
|
---|
1165 | case csimbr_fun:
|
---|
1166 | mbr_name = csimbr->u.fun->name;
|
---|
1167 | break;
|
---|
1168 | case csimbr_var:
|
---|
1169 | mbr_name = csimbr->u.var->name;
|
---|
1170 | break;
|
---|
1171 | case csimbr_prop:
|
---|
1172 | mbr_name = csimbr->u.prop->name;
|
---|
1173 | break;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | return mbr_name;
|
---|
1177 | }
|
---|