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

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

Update SBI to rev. 207.

  • Property mode set to 100644
File size: 17.4 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
39/** Allocate new module.
40 *
41 * @return New module
42 */
43stree_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 */
62stree_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 */
81stree_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_ref = NULL;
95 list_init(&csi->members);
96 return csi;
97}
98
99/** Allocate new CSI member.
100 *
101 * @param cc CSI member class
102 * @return New CSI member
103 */
104stree_csimbr_t *stree_csimbr_new(csimbr_class_t cc)
105{
106 stree_csimbr_t *csimbr;
107
108 csimbr = calloc(1, sizeof(stree_csimbr_t));
109 if (csimbr == NULL) {
110 printf("Memory allocation failed.\n");
111 exit(1);
112 }
113
114 csimbr->cc = cc;
115 return csimbr;
116}
117
118/** Allocate new member delegate.
119 *
120 * @return New member delegate
121 */
122stree_deleg_t *stree_deleg_new(void)
123{
124 stree_deleg_t *deleg;
125
126 deleg = calloc(1, sizeof(stree_deleg_t));
127 if (deleg == NULL) {
128 printf("Memory allocation failed.\n");
129 exit(1);
130 }
131
132 return deleg;
133}
134
135/** Allocate new member function.
136 *
137 * @return New member function
138 */
139stree_fun_t *stree_fun_new(void)
140{
141 stree_fun_t *fun;
142
143 fun = calloc(1, sizeof(stree_fun_t));
144 if (fun == NULL) {
145 printf("Memory allocation failed.\n");
146 exit(1);
147 }
148
149 return fun;
150}
151
152/** Allocate new member variable.
153 *
154 * @return New member variable
155 */
156stree_var_t *stree_var_new(void)
157{
158 stree_var_t *var;
159
160 var = calloc(1, sizeof(stree_var_t));
161 if (var == NULL) {
162 printf("Memory allocation failed.\n");
163 exit(1);
164 }
165
166 return var;
167}
168
169/** Allocate new property.
170 *
171 * @return New property
172 */
173stree_prop_t *stree_prop_new(void)
174{
175 stree_prop_t *prop;
176
177 prop = calloc(1, sizeof(stree_prop_t));
178 if (prop == NULL) {
179 printf("Memory allocation failed.\n");
180 exit(1);
181 }
182
183 return prop;
184}
185
186/** Allocate new type argument.
187 *
188 * @return New type argument
189 */
190stree_targ_t *stree_targ_new(void)
191{
192 stree_targ_t *targ;
193
194 targ = calloc(1, sizeof(stree_targ_t));
195 if (targ == NULL) {
196 printf("Memory allocation failed.\n");
197 exit(1);
198 }
199
200 return targ;
201}
202
203/** Allocate new symbol attribute.
204 *
205 * @param sac Symbol attribute class
206 * @return New symbol attribute
207 */
208stree_symbol_attr_t *stree_symbol_attr_new(symbol_attr_class_t sac)
209{
210 stree_symbol_attr_t *symbol_attr;
211
212 symbol_attr = calloc(1, sizeof(stree_symbol_attr_t));
213 if (symbol_attr == NULL) {
214 printf("Memory allocation failed.\n");
215 exit(1);
216 }
217
218 symbol_attr->sac = sac;
219 return symbol_attr;
220}
221
222/** Allocate new procedure.
223 *
224 * @return New procedure
225 */
226stree_proc_t *stree_proc_new(void)
227{
228 stree_proc_t *proc;
229
230 proc = calloc(1, sizeof(stree_proc_t));
231 if (proc == NULL) {
232 printf("Memory allocation failed.\n");
233 exit(1);
234 }
235
236 return proc;
237}
238
239/** Allocate new procedure argument.
240 *
241 * @return New procedure argument
242 */
243stree_proc_arg_t *stree_proc_arg_new(void)
244{
245 stree_proc_arg_t *proc_arg;
246
247 proc_arg = calloc(1, sizeof(stree_proc_arg_t));
248 if (proc_arg == NULL) {
249 printf("Memory allocation failed.\n");
250 exit(1);
251 }
252
253 return proc_arg;
254}
255
256/** Allocate new function signature.
257 *
258 * @return New procedure argument
259 */
260stree_fun_sig_t *stree_fun_sig_new(void)
261{
262 stree_fun_sig_t *fun_sig;
263
264 fun_sig = calloc(1, sizeof(stree_fun_sig_t));
265 if (fun_sig == NULL) {
266 printf("Memory allocation failed.\n");
267 exit(1);
268 }
269
270 return fun_sig;
271}
272
273/** Allocate new procedure argument attribute.
274 *
275 * @param Argument attribute class
276 * @return New procedure argument attribute
277 */
278stree_arg_attr_t *stree_arg_attr_new(arg_attr_class_t aac)
279{
280 stree_arg_attr_t *arg_attr;
281
282 arg_attr = calloc(1, sizeof(stree_arg_attr_t));
283 if (arg_attr == NULL) {
284 printf("Memory allocation failed.\n");
285 exit(1);
286 }
287
288 arg_attr->aac = aac;
289 return arg_attr;
290}
291
292/** Allocate new statement.
293 *
294 * @param sc Statement class
295 * @return New statement
296 */
297stree_stat_t *stree_stat_new(stat_class_t sc)
298{
299 stree_stat_t *stat;
300
301 stat = calloc(1, sizeof(stree_stat_t));
302 if (stat == NULL) {
303 printf("Memory allocation failed.\n");
304 exit(1);
305 }
306
307 stat->sc = sc;
308 return stat;
309}
310
311/** Allocate new local variable declaration.
312 *
313 * @return New local variable declaration
314 */
315stree_vdecl_t *stree_vdecl_new(void)
316{
317 stree_vdecl_t *vdecl_s;
318
319 vdecl_s = calloc(1, sizeof(stree_vdecl_t));
320 if (vdecl_s == NULL) {
321 printf("Memory allocation failed.\n");
322 exit(1);
323 }
324
325 return vdecl_s;
326}
327
328/** Allocate new @c if statement.
329 *
330 * @return New @c if statement
331 */
332stree_if_t *stree_if_new(void)
333{
334 stree_if_t *if_s;
335
336 if_s = calloc(1, sizeof(stree_if_t));
337 if (if_s == NULL) {
338 printf("Memory allocation failed.\n");
339 exit(1);
340 }
341
342 return if_s;
343}
344
345/** Allocate new @c while statement.
346 *
347 * @return New @c while statement
348 */
349stree_while_t *stree_while_new(void)
350{
351 stree_while_t *while_s;
352
353 while_s = calloc(1, sizeof(stree_while_t));
354 if (while_s == NULL) {
355 printf("Memory allocation failed.\n");
356 exit(1);
357 }
358
359 return while_s;
360}
361
362/** Allocate new @c for statement.
363 *
364 * @return New @c for statement
365 */
366stree_for_t *stree_for_new(void)
367{
368 stree_for_t *for_s;
369
370 for_s = calloc(1, sizeof(stree_for_t));
371 if (for_s == NULL) {
372 printf("Memory allocation failed.\n");
373 exit(1);
374 }
375
376 return for_s;
377}
378
379/** Allocate new @c raise statement.
380 *
381 * @return New @c raise statement
382 */
383stree_raise_t *stree_raise_new(void)
384{
385 stree_raise_t *raise_s;
386
387 raise_s = calloc(1, sizeof(stree_raise_t));
388 if (raise_s == NULL) {
389 printf("Memory allocation failed.\n");
390 exit(1);
391 }
392
393 return raise_s;
394}
395
396/** Allocate new @c return statement.
397 *
398 * @return New @c return statement
399 */
400stree_return_t *stree_return_new(void)
401{
402 stree_return_t *return_s;
403
404 return_s = calloc(1, sizeof(stree_return_t));
405 if (return_s == NULL) {
406 printf("Memory allocation failed.\n");
407 exit(1);
408 }
409
410 return return_s;
411}
412
413/** Allocate new with-except-finally statement.
414 *
415 * @return New with-except-finally statement.
416 */
417stree_wef_t *stree_wef_new(void)
418{
419 stree_wef_t *wef_s;
420
421 wef_s = calloc(1, sizeof(stree_wef_t));
422 if (wef_s == NULL) {
423 printf("Memory allocation failed.\n");
424 exit(1);
425 }
426
427 return wef_s;
428}
429
430/** Allocate new expression statement.
431 *
432 * @return New expression statement
433 */
434stree_exps_t *stree_exps_new(void)
435{
436 stree_exps_t *exp_s;
437
438 exp_s = calloc(1, sizeof(stree_exps_t));
439 if (exp_s == NULL) {
440 printf("Memory allocation failed.\n");
441 exit(1);
442 }
443
444 return exp_s;
445}
446
447/** Allocate new @c except clause.
448 *
449 * @return New @c except clause
450 */
451stree_except_t *stree_except_new(void)
452{
453 stree_except_t *except_c;
454
455 except_c = calloc(1, sizeof(stree_except_t));
456 if (except_c == NULL) {
457 printf("Memory allocation failed.\n");
458 exit(1);
459 }
460
461 return except_c;
462}
463
464/** Allocate new statement block.
465 *
466 * @return New statement block
467 */
468stree_block_t *stree_block_new(void)
469{
470 stree_block_t *block;
471
472 block = calloc(1, sizeof(stree_block_t));
473 if (block == NULL) {
474 printf("Memory allocation failed.\n");
475 exit(1);
476 }
477
478 return block;
479}
480
481/** Allocate new expression.
482 *
483 * @param ec Expression class
484 * @return New expression
485 */
486stree_expr_t *stree_expr_new(expr_class_t ec)
487{
488 stree_expr_t *expr;
489
490 expr = calloc(1, sizeof(stree_expr_t));
491 if (expr == NULL) {
492 printf("Memory allocation failed.\n");
493 exit(1);
494 }
495
496 expr->ec = ec;
497 return expr;
498}
499
500/** Allocate new assignment.
501 *
502 * @param ac Assignment class
503 * @return New assignment
504 */
505stree_assign_t *stree_assign_new(assign_class_t ac)
506{
507 stree_assign_t *assign;
508
509 assign = calloc(1, sizeof(stree_assign_t));
510 if (assign == NULL) {
511 printf("Memory allocation failed.\n");
512 exit(1);
513 }
514
515 assign->ac = ac;
516 return assign;
517}
518
519/** Allocate new binary operation.
520 *
521 * @return New binary operation
522 */
523stree_binop_t *stree_binop_new(binop_class_t bc)
524{
525 stree_binop_t *binop;
526
527 binop = calloc(1, sizeof(stree_binop_t));
528 if (binop == NULL) {
529 printf("Memory allocation failed.\n");
530 exit(1);
531 }
532
533 binop->bc = bc;
534 return binop;
535}
536
537/** Allocate new unary operation.
538 *
539 * @param uc Unary operation class
540 * @return New unary operation
541 */
542stree_unop_t *stree_unop_new(unop_class_t uc)
543{
544 stree_unop_t *unop;
545
546 unop = calloc(1, sizeof(stree_unop_t));
547 if (unop == NULL) {
548 printf("Memory allocation failed.\n");
549 exit(1);
550 }
551
552 unop->uc = uc;
553 return unop;
554}
555
556/** Allocate new @c new operation.
557 *
558 * @return New @c new operation
559 */
560stree_new_t *stree_new_new(void)
561{
562 stree_new_t *new_op;
563
564 new_op = calloc(1, sizeof(stree_new_t));
565 if (new_op == NULL) {
566 printf("Memory allocation failed.\n");
567 exit(1);
568 }
569
570 return new_op;
571}
572
573/** Allocate new .
574 *
575 * @return New
576 */
577stree_access_t *stree_access_new(void)
578{
579 stree_access_t *access;
580
581 access = calloc(1, sizeof(stree_access_t));
582 if (access == NULL) {
583 printf("Memory allocation failed.\n");
584 exit(1);
585 }
586
587 return access;
588}
589
590/** Allocate new function call operation.
591 *
592 * @return New function call operation
593 */
594stree_call_t *stree_call_new(void)
595{
596 stree_call_t *call;
597
598 call = calloc(1, sizeof(stree_call_t));
599 if (call == NULL) {
600 printf("Memory allocation failed.\n");
601 exit(1);
602 }
603
604 return call;
605}
606
607/** Allocate new indexing operation.
608 *
609 * @return New indexing operation
610 */
611stree_index_t *stree_index_new(void)
612{
613 stree_index_t *index;
614
615 index = calloc(1, sizeof(stree_index_t));
616 if (index == NULL) {
617 printf("Memory allocation failed.\n");
618 exit(1);
619 }
620
621 return index;
622}
623
624/** Allocate new as conversion.
625 *
626 * @return New as conversion
627 */
628stree_as_t *stree_as_new(void)
629{
630 stree_as_t *as_expr;
631
632 as_expr = calloc(1, sizeof(stree_as_t));
633 if (as_expr == NULL) {
634 printf("Memory allocation failed.\n");
635 exit(1);
636 }
637
638 return as_expr;
639}
640
641/** Allocate new boxing operation.
642 *
643 * @return New boxing operation
644 */
645stree_box_t *stree_box_new(void)
646{
647 stree_box_t *box_expr;
648
649 box_expr = calloc(1, sizeof(stree_box_t));
650 if (box_expr == NULL) {
651 printf("Memory allocation failed.\n");
652 exit(1);
653 }
654
655 return box_expr;
656}
657
658/** Allocate new name reference operation.
659 *
660 * @return New name reference operation
661 */
662stree_nameref_t *stree_nameref_new(void)
663{
664 stree_nameref_t *nameref;
665
666 nameref = calloc(1, sizeof(stree_nameref_t));
667 if (nameref == NULL) {
668 printf("Memory allocation failed.\n");
669 exit(1);
670 }
671
672 return nameref;
673}
674
675/** Allocate new identifier.
676 *
677 * @return New identifier
678 */
679stree_ident_t *stree_ident_new(void)
680{
681 stree_ident_t *ident;
682
683 ident = calloc(1, sizeof(stree_ident_t));
684 if (ident == NULL) {
685 printf("Memory allocation failed.\n");
686 exit(1);
687 }
688
689 return ident;
690}
691
692/** Allocate new literal.
693 *
694 * @param ltc Literal class
695 * @return New literal
696 */
697stree_literal_t *stree_literal_new(literal_class_t ltc)
698{
699 stree_literal_t *literal;
700
701 literal = calloc(1, sizeof(stree_literal_t));
702 if (literal == NULL) {
703 printf("Memory allocation failed.\n");
704 exit(1);
705 }
706
707 literal->ltc = ltc;
708 return literal;
709}
710
711/** Allocate new @c self reference.
712 *
713 * @return New @c self reference
714 */
715stree_self_ref_t *stree_self_ref_new(void)
716{
717 stree_self_ref_t *self_ref;
718
719 self_ref = calloc(1, sizeof(stree_self_ref_t));
720 if (self_ref == NULL) {
721 printf("Memory allocation failed.\n");
722 exit(1);
723 }
724
725 return self_ref;
726}
727
728/** Allocate new type expression
729 *
730 * @return New type expression
731 */
732stree_texpr_t *stree_texpr_new(texpr_class_t tc)
733{
734 stree_texpr_t *texpr;
735
736 texpr = calloc(1, sizeof(stree_texpr_t));
737 if (texpr == NULL) {
738 printf("Memory allocation failed.\n");
739 exit(1);
740 }
741
742 texpr->tc = tc;
743 return texpr;
744}
745
746/** Allocate new type access operation.
747 *
748 * @return New type access operation
749 */
750stree_taccess_t *stree_taccess_new(void)
751{
752 stree_taccess_t *taccess;
753
754 taccess = calloc(1, sizeof(stree_taccess_t));
755 if (taccess == NULL) {
756 printf("Memory allocation failed.\n");
757 exit(1);
758 }
759
760 return taccess;
761}
762
763/** Allocate new type application operation.
764 *
765 * @return New type application operation
766 */
767stree_tapply_t *stree_tapply_new(void)
768{
769 stree_tapply_t *tapply;
770
771 tapply = calloc(1, sizeof(stree_tapply_t));
772 if (tapply == NULL) {
773 printf("Memory allocation failed.\n");
774 exit(1);
775 }
776
777 return tapply;
778}
779
780/** Allocate new type indexing operation.
781 *
782 * @return New type indexing operation
783 */
784stree_tindex_t *stree_tindex_new(void)
785{
786 stree_tindex_t *tindex;
787
788 tindex = calloc(1, sizeof(stree_tindex_t));
789 if (tindex == NULL) {
790 printf("Memory allocation failed.\n");
791 exit(1);
792 }
793
794 return tindex;
795}
796
797/** Allocate new type literal.
798 *
799 * @return New type literal
800 */
801stree_tliteral_t *stree_tliteral_new(tliteral_class_t tlc)
802{
803 stree_tliteral_t *tliteral;
804
805 tliteral = calloc(1, sizeof(stree_tliteral_t));
806 if (tliteral == NULL) {
807 printf("Memory allocation failed.\n");
808 exit(1);
809 }
810
811 tliteral->tlc = tlc;
812 return tliteral;
813}
814
815/** Allocate new type name reference.
816 *
817 * @return New type name reference
818 */
819stree_tnameref_t *stree_tnameref_new(void)
820{
821 stree_tnameref_t *tnameref;
822
823 tnameref = calloc(1, sizeof(stree_tnameref_t));
824 if (tnameref == NULL) {
825 printf("Memory allocation failed.\n");
826 exit(1);
827 }
828
829 return tnameref;
830}
831
832/** Allocate new symbol.
833 *
834 * @return New symbol
835 */
836stree_symbol_t *stree_symbol_new(symbol_class_t sc)
837{
838 stree_symbol_t *symbol;
839
840 symbol = calloc(1, sizeof(stree_symbol_t));
841 if (symbol == NULL) {
842 printf("Memory allocation failed.\n");
843 exit(1);
844 }
845
846 symbol->sc = sc;
847 return symbol;
848}
849
850/** Allocate new program.
851 *
852 * @return New program
853 */
854stree_program_t *stree_program_new(void)
855{
856 stree_program_t *program;
857
858 program = calloc(1, sizeof(stree_program_t));
859 if (program == NULL) {
860 printf("Memory allocation failed.\n");
861 exit(1);
862 }
863
864 return program;
865}
866
867/** Determine if @a symbol has attribute of class @a sac.
868 *
869 * @param symbol Symbol
870 * @param sac Symbol attribute class
871 * @return @c b_true if yes, @c b_false if no.
872 */
873bool_t stree_symbol_has_attr(stree_symbol_t *symbol, symbol_attr_class_t sac)
874{
875 list_node_t *node;
876 stree_symbol_attr_t *attr;
877
878 node = list_first(&symbol->attr);
879 while (node != NULL) {
880 attr = list_node_data(node, stree_symbol_attr_t *);
881 if (attr->sac == sac)
882 return b_true;
883
884 node = list_next(&symbol->attr, node);
885 }
886
887 return b_false;
888}
889
890/** Determine if argument @a arg has attribute of class @a aac.
891 *
892 * @param arg Formal procedure argument
893 * @param aac Argument attribute class
894 * @return @c b_true if yes, @c b_false if no.
895 */
896bool_t stree_arg_has_attr(stree_proc_arg_t *arg, arg_attr_class_t aac)
897{
898 list_node_t *node;
899 stree_arg_attr_t *attr;
900
901 node = list_first(&arg->attr);
902 while (node != NULL) {
903 attr = list_node_data(node, stree_arg_attr_t *);
904 if (attr->aac == aac)
905 return b_true;
906
907 node = list_next(&arg->attr, node);
908 }
909
910 return b_false;
911}
912
913/** Determine wheter @a a is derived (transitively) from @a b.
914 *
915 * XXX This does not work right with generics.
916 *
917 * @param a Derived CSI.
918 * @param b Base CSI.
919 * @return @c b_true if @a a is equal to or directly or indirectly
920 * derived from @a b.
921 */
922bool_t stree_is_csi_derived_from_csi(stree_csi_t *a, stree_csi_t *b)
923{
924 stree_csi_t *csi;
925
926 csi = a;
927 while (csi != NULL) {
928 if (csi == b)
929 return b_true;
930
931 csi = csi->base_csi;
932 }
933
934 /* We went all the way to the root and did not find b. */
935 return b_false;
936}
937
938/** Search for CSI type argument of the given name.
939 *
940 * @param csi CSI to look in.
941 * @param ident Identifier of the type argument.
942 * @return Type argument definition or @c NULL if not found.
943 */
944stree_targ_t *stree_csi_find_targ(stree_csi_t *csi, stree_ident_t *ident)
945{
946 list_node_t *targ_n;
947 stree_targ_t *targ;
948
949 targ_n = list_first(&csi->targ);
950 while (targ_n != NULL) {
951 targ = list_node_data(targ_n, stree_targ_t *);
952 if (targ->name->sid == ident->sid)
953 return targ;
954
955 targ_n = list_next(&csi->targ, targ_n);
956 }
957
958 /* No match */
959 return NULL;
960}
Note: See TracBrowser for help on using the repository browser.