source: mainline/uspace/app/sbi/src/stree.c@ a95310e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a95310e was 37f527b, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Update SBI to rev. 144.

  • Property mode set to 100644
File size: 11.9 KB
Line 
1/*
2 * Copyright (c) 2010 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
39stree_module_t *stree_module_new(void)
40{
41 stree_module_t *module;
42
43 module = calloc(1, sizeof(stree_module_t));
44 if (module == NULL) {
45 printf("Memory allocation failed.\n");
46 exit(1);
47 }
48
49 list_init(&module->members);
50 return module;
51}
52
53stree_modm_t *stree_modm_new(modm_class_t mc)
54{
55 stree_modm_t *modm;
56
57 modm = calloc(1, sizeof(stree_modm_t));
58 if (modm == NULL) {
59 printf("Memory allocation failed.\n");
60 exit(1);
61 }
62
63 modm->mc = mc;
64 return modm;
65}
66
67stree_csi_t *stree_csi_new(csi_class_t cc)
68{
69 stree_csi_t *csi;
70
71 csi = calloc(1, sizeof(stree_csi_t));
72 if (csi == NULL) {
73 printf("Memory allocation failed.\n");
74 exit(1);
75 }
76
77 csi->cc = cc;
78 csi->ancr_state = ws_unvisited;
79 csi->name = NULL;
80 csi->base_csi_ref = NULL;
81 list_init(&csi->members);
82 return csi;
83}
84
85stree_csimbr_t *stree_csimbr_new(csimbr_class_t cc)
86{
87 stree_csimbr_t *csimbr;
88
89 csimbr = calloc(1, sizeof(stree_csimbr_t));
90 if (csimbr == NULL) {
91 printf("Memory allocation failed.\n");
92 exit(1);
93 }
94
95 csimbr->cc = cc;
96 return csimbr;
97}
98
99stree_fun_t *stree_fun_new(void)
100{
101 stree_fun_t *fun;
102
103 fun = calloc(1, sizeof(stree_fun_t));
104 if (fun == NULL) {
105 printf("Memory allocation failed.\n");
106 exit(1);
107 }
108
109 return fun;
110}
111
112stree_var_t *stree_var_new(void)
113{
114 stree_var_t *var;
115
116 var = calloc(1, sizeof(stree_var_t));
117 if (var == NULL) {
118 printf("Memory allocation failed.\n");
119 exit(1);
120 }
121
122 return var;
123}
124
125stree_prop_t *stree_prop_new(void)
126{
127 stree_prop_t *prop;
128
129 prop = calloc(1, sizeof(stree_prop_t));
130 if (prop == NULL) {
131 printf("Memory allocation failed.\n");
132 exit(1);
133 }
134
135 return prop;
136}
137
138stree_symbol_attr_t *stree_symbol_attr_new(symbol_attr_class_t sac)
139{
140 stree_symbol_attr_t *symbol_attr;
141
142 symbol_attr = calloc(1, sizeof(stree_symbol_attr_t));
143 if (symbol_attr == NULL) {
144 printf("Memory allocation failed.\n");
145 exit(1);
146 }
147
148 symbol_attr->sac = sac;
149 return symbol_attr;
150}
151
152stree_proc_t *stree_proc_new(void)
153{
154 stree_proc_t *proc;
155
156 proc = calloc(1, sizeof(stree_proc_t));
157 if (proc == NULL) {
158 printf("Memory allocation failed.\n");
159 exit(1);
160 }
161
162 return proc;
163}
164
165stree_proc_arg_t *stree_proc_arg_new(void)
166{
167 stree_proc_arg_t *proc_arg;
168
169 proc_arg = calloc(1, sizeof(stree_proc_arg_t));
170 if (proc_arg == NULL) {
171 printf("Memory allocation failed.\n");
172 exit(1);
173 }
174
175 return proc_arg;
176}
177
178stree_arg_attr_t *stree_arg_attr_new(arg_attr_class_t aac)
179{
180 stree_arg_attr_t *arg_attr;
181
182 arg_attr = calloc(1, sizeof(stree_arg_attr_t));
183 if (arg_attr == NULL) {
184 printf("Memory allocation failed.\n");
185 exit(1);
186 }
187
188 arg_attr->aac = aac;
189 return arg_attr;
190}
191
192stree_stat_t *stree_stat_new(stat_class_t sc)
193{
194 stree_stat_t *stat;
195
196 stat = calloc(1, sizeof(stree_stat_t));
197 if (stat == NULL) {
198 printf("Memory allocation failed.\n");
199 exit(1);
200 }
201
202 stat->sc = sc;
203 return stat;
204}
205
206stree_vdecl_t *stree_vdecl_new(void)
207{
208 stree_vdecl_t *vdecl_s;
209
210 vdecl_s = calloc(1, sizeof(stree_vdecl_t));
211 if (vdecl_s == NULL) {
212 printf("Memory allocation failed.\n");
213 exit(1);
214 }
215
216 return vdecl_s;
217}
218
219stree_if_t *stree_if_new(void)
220{
221 stree_if_t *if_s;
222
223 if_s = calloc(1, sizeof(stree_if_t));
224 if (if_s == NULL) {
225 printf("Memory allocation failed.\n");
226 exit(1);
227 }
228
229 return if_s;
230}
231
232stree_while_t *stree_while_new(void)
233{
234 stree_while_t *while_s;
235
236 while_s = calloc(1, sizeof(stree_while_t));
237 if (while_s == NULL) {
238 printf("Memory allocation failed.\n");
239 exit(1);
240 }
241
242 return while_s;
243}
244
245stree_for_t *stree_for_new(void)
246{
247 stree_for_t *for_s;
248
249 for_s = calloc(1, sizeof(stree_for_t));
250 if (for_s == NULL) {
251 printf("Memory allocation failed.\n");
252 exit(1);
253 }
254
255 return for_s;
256}
257
258stree_raise_t *stree_raise_new(void)
259{
260 stree_raise_t *raise_s;
261
262 raise_s = calloc(1, sizeof(stree_raise_t));
263 if (raise_s == NULL) {
264 printf("Memory allocation failed.\n");
265 exit(1);
266 }
267
268 return raise_s;
269}
270
271stree_return_t *stree_return_new(void)
272{
273 stree_return_t *return_s;
274
275 return_s = calloc(1, sizeof(stree_return_t));
276 if (return_s == NULL) {
277 printf("Memory allocation failed.\n");
278 exit(1);
279 }
280
281 return return_s;
282}
283
284stree_wef_t *stree_wef_new(void)
285{
286 stree_wef_t *wef_s;
287
288 wef_s = calloc(1, sizeof(stree_wef_t));
289 if (wef_s == NULL) {
290 printf("Memory allocation failed.\n");
291 exit(1);
292 }
293
294 return wef_s;
295}
296
297stree_exps_t *stree_exps_new(void)
298{
299 stree_exps_t *exp_s;
300
301 exp_s = calloc(1, sizeof(stree_exps_t));
302 if (exp_s == NULL) {
303 printf("Memory allocation failed.\n");
304 exit(1);
305 }
306
307 return exp_s;
308}
309
310stree_except_t *stree_except_new(void)
311{
312 stree_except_t *except_c;
313
314 except_c = calloc(1, sizeof(stree_except_t));
315 if (except_c == NULL) {
316 printf("Memory allocation failed.\n");
317 exit(1);
318 }
319
320 return except_c;
321}
322
323stree_block_t *stree_block_new(void)
324{
325 stree_block_t *block;
326
327 block = calloc(1, sizeof(stree_block_t));
328 if (block == NULL) {
329 printf("Memory allocation failed.\n");
330 exit(1);
331 }
332
333 return block;
334}
335
336stree_expr_t *stree_expr_new(expr_class_t ec)
337{
338 stree_expr_t *expr;
339
340 expr = calloc(1, sizeof(stree_expr_t));
341 if (expr == NULL) {
342 printf("Memory allocation failed.\n");
343 exit(1);
344 }
345
346 expr->ec = ec;
347 return expr;
348}
349
350stree_assign_t *stree_assign_new(assign_class_t ac)
351{
352 stree_assign_t *assign;
353
354 assign = calloc(1, sizeof(stree_assign_t));
355 if (assign == NULL) {
356 printf("Memory allocation failed.\n");
357 exit(1);
358 }
359
360 assign->ac = ac;
361 return assign;
362}
363
364stree_binop_t *stree_binop_new(binop_class_t bc)
365{
366 stree_binop_t *binop;
367
368 binop = calloc(1, sizeof(stree_binop_t));
369 if (binop == NULL) {
370 printf("Memory allocation failed.\n");
371 exit(1);
372 }
373
374 binop->bc = bc;
375 return binop;
376}
377
378stree_new_t *stree_new_new(void)
379{
380 stree_new_t *new_op;
381
382 new_op = calloc(1, sizeof(stree_new_t));
383 if (new_op == NULL) {
384 printf("Memory allocation failed.\n");
385 exit(1);
386 }
387
388 return new_op;
389}
390
391stree_access_t *stree_access_new(void)
392{
393 stree_access_t *access;
394
395 access = calloc(1, sizeof(stree_access_t));
396 if (access == NULL) {
397 printf("Memory allocation failed.\n");
398 exit(1);
399 }
400
401 return access;
402}
403
404stree_call_t *stree_call_new(void)
405{
406 stree_call_t *call;
407
408 call = calloc(1, sizeof(stree_call_t));
409 if (call == NULL) {
410 printf("Memory allocation failed.\n");
411 exit(1);
412 }
413
414 return call;
415}
416
417stree_index_t *stree_index_new(void)
418{
419 stree_index_t *index;
420
421 index = calloc(1, sizeof(stree_index_t));
422 if (index == NULL) {
423 printf("Memory allocation failed.\n");
424 exit(1);
425 }
426
427 return index;
428}
429
430stree_as_t *stree_as_new(void)
431{
432 stree_as_t *as_expr;
433
434 as_expr = calloc(1, sizeof(stree_as_t));
435 if (as_expr == NULL) {
436 printf("Memory allocation failed.\n");
437 exit(1);
438 }
439
440 return as_expr;
441}
442
443stree_nameref_t *stree_nameref_new(void)
444{
445 stree_nameref_t *nameref;
446
447 nameref = calloc(1, sizeof(stree_nameref_t));
448 if (nameref == NULL) {
449 printf("Memory allocation failed.\n");
450 exit(1);
451 }
452
453 return nameref;
454}
455
456stree_ident_t *stree_ident_new(void)
457{
458 stree_ident_t *ident;
459
460 ident = calloc(1, sizeof(stree_ident_t));
461 if (ident == NULL) {
462 printf("Memory allocation failed.\n");
463 exit(1);
464 }
465
466 return ident;
467}
468
469stree_literal_t *stree_literal_new(literal_class_t ltc)
470{
471 stree_literal_t *literal;
472
473 literal = calloc(1, sizeof(stree_literal_t));
474 if (literal == NULL) {
475 printf("Memory allocation failed.\n");
476 exit(1);
477 }
478
479 literal->ltc = ltc;
480 return literal;
481}
482
483stree_self_ref_t *stree_self_ref_new(void)
484{
485 stree_self_ref_t *self_ref;
486
487 self_ref = calloc(1, sizeof(stree_self_ref_t));
488 if (self_ref == NULL) {
489 printf("Memory allocation failed.\n");
490 exit(1);
491 }
492
493 return self_ref;
494}
495
496stree_texpr_t *stree_texpr_new(texpr_class_t tc)
497{
498 stree_texpr_t *texpr;
499
500 texpr = calloc(1, sizeof(stree_texpr_t));
501 if (texpr == NULL) {
502 printf("Memory allocation failed.\n");
503 exit(1);
504 }
505
506 texpr->tc = tc;
507 return texpr;
508}
509
510stree_taccess_t *stree_taccess_new(void)
511{
512 stree_taccess_t *taccess;
513
514 taccess = calloc(1, sizeof(stree_taccess_t));
515 if (taccess == NULL) {
516 printf("Memory allocation failed.\n");
517 exit(1);
518 }
519
520 return taccess;
521}
522
523stree_tapply_t *stree_tapply_new(void)
524{
525 stree_tapply_t *tapply;
526
527 tapply = calloc(1, sizeof(stree_tapply_t));
528 if (tapply == NULL) {
529 printf("Memory allocation failed.\n");
530 exit(1);
531 }
532
533 return tapply;
534}
535
536stree_tindex_t *stree_tindex_new(void)
537{
538 stree_tindex_t *tindex;
539
540 tindex = calloc(1, sizeof(stree_tindex_t));
541 if (tindex == NULL) {
542 printf("Memory allocation failed.\n");
543 exit(1);
544 }
545
546 return tindex;
547}
548
549stree_tliteral_t *stree_tliteral_new(tliteral_class_t tlc)
550{
551 stree_tliteral_t *tliteral;
552
553 tliteral = calloc(1, sizeof(stree_tliteral_t));
554 if (tliteral == NULL) {
555 printf("Memory allocation failed.\n");
556 exit(1);
557 }
558
559 tliteral->tlc = tlc;
560 return tliteral;
561}
562
563stree_tnameref_t *stree_tnameref_new(void)
564{
565 stree_tnameref_t *tnameref;
566
567 tnameref = calloc(1, sizeof(stree_tnameref_t));
568 if (tnameref == NULL) {
569 printf("Memory allocation failed.\n");
570 exit(1);
571 }
572
573 return tnameref;
574}
575
576stree_symbol_t *stree_symbol_new(symbol_class_t sc)
577{
578 stree_symbol_t *symbol;
579
580 symbol = calloc(1, sizeof(stree_symbol_t));
581 if (symbol == NULL) {
582 printf("Memory allocation failed.\n");
583 exit(1);
584 }
585
586 symbol->sc = sc;
587 return symbol;
588}
589
590stree_program_t *stree_program_new(void)
591{
592 stree_program_t *program;
593
594 program = calloc(1, sizeof(stree_program_t));
595 if (program == NULL) {
596 printf("Memory allocation failed.\n");
597 exit(1);
598 }
599
600 return program;
601}
602
603/** Determine if @a symbol has attribute of class @a sac. */
604bool_t stree_symbol_has_attr(stree_symbol_t *symbol, symbol_attr_class_t sac)
605{
606 list_node_t *node;
607 stree_symbol_attr_t *attr;
608
609 node = list_first(&symbol->attr);
610 while (node != NULL) {
611 attr = list_node_data(node, stree_symbol_attr_t *);
612 if (attr->sac == sac)
613 return b_true;
614
615 node = list_next(&symbol->attr, node);
616 }
617
618 return b_false;
619}
620
621/** Determine if argument @a arg has attribute of class @a aac. */
622bool_t stree_arg_has_attr(stree_proc_arg_t *arg, arg_attr_class_t aac)
623{
624 list_node_t *node;
625 stree_arg_attr_t *attr;
626
627 node = list_first(&arg->attr);
628 while (node != NULL) {
629 attr = list_node_data(node, stree_arg_attr_t *);
630 if (attr->aac == aac)
631 return b_true;
632
633 node = list_next(&arg->attr, node);
634 }
635
636 return b_false;
637}
638
639/** Determine wheter @a a is derived (transitively) from @a b.
640 *
641 * @param a Derived CSI.
642 * @param b Base CSI.
643 * @return @c b_true if @a a is equal to or directly or indirectly
644 * derived from @a b.
645 */
646bool_t stree_is_csi_derived_from_csi(stree_csi_t *a, stree_csi_t *b)
647{
648 stree_csi_t *csi;
649
650 csi = a;
651 while (csi != NULL) {
652 if (csi == b)
653 return b_true;
654
655 csi = csi->base_csi;
656 }
657
658 /* We went all the way to the root and did not find b. */
659 return b_false;
660}
Note: See TracBrowser for help on using the repository browser.