source: mainline/uspace/app/sbi/src/stree_t.h

Last change on this file was 051b3db8, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Update SBI to rev. 344 from upstream. What's new:

  • Builtin.WriteLine() renamed to Console.WriteLine()
  • Implemented 'switch' statement
  • Significantly reduced memory consumption (also increases execution speed in some cases)
  • Properties can be accessed via unqualified names
  • Exceptions raised during property accesses are now handled correctly
  • Some missing checks against expressions returning no value added
  • Property mode set to 100644
File size: 16.2 KB
Line 
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#ifndef STREE_T_H_
30#define STREE_T_H_
31
32#include "bigint_t.h"
33#include "list_t.h"
34#include "builtin_t.h"
35
36/*
37 * Arithmetic expressions
38 */
39
40struct stree_expr;
41
42/** Identifier */
43typedef struct {
44 int sid;
45 struct cspan *cspan;
46} stree_ident_t;
47
48/** Name reference */
49typedef struct {
50 /** Expression backlink */
51 struct stree_expr *expr;
52
53 stree_ident_t *name;
54} stree_nameref_t;
55
56/** Boolean literal */
57typedef struct {
58 bool_t value;
59} stree_lit_bool_t;
60
61/** Character literal */
62typedef struct {
63 bigint_t value;
64} stree_lit_char_t;
65
66/** Integer literal */
67typedef struct {
68 bigint_t value;
69} stree_lit_int_t;
70
71/** Reference literal (there is only one: @c nil). */
72typedef struct {
73} stree_lit_ref_t;
74
75/** String literal */
76typedef struct {
77 char *value;
78} stree_lit_string_t;
79
80typedef enum {
81 ltc_bool,
82 ltc_char,
83 ltc_int,
84 ltc_ref,
85 ltc_string
86} literal_class_t;
87
88/** Literal */
89typedef struct {
90 /** Expression backlink */
91 struct stree_expr *expr;
92
93 literal_class_t ltc;
94 union {
95 stree_lit_bool_t lit_bool;
96 stree_lit_char_t lit_char;
97 stree_lit_int_t lit_int;
98 stree_lit_ref_t lit_ref;
99 stree_lit_string_t lit_string;
100 } u;
101} stree_literal_t;
102
103/** Reference to currently active object. */
104typedef struct {
105 /** Expression backlink */
106 struct stree_expr *expr;
107} stree_self_ref_t;
108
109/** Binary operation class */
110typedef enum {
111 bo_equal,
112 bo_notequal,
113 bo_lt,
114 bo_gt,
115 bo_lt_equal,
116 bo_gt_equal,
117 bo_plus,
118 bo_minus,
119 bo_mult,
120 bo_and,
121 bo_or
122} binop_class_t;
123
124/** Unary operation class */
125typedef enum {
126 uo_plus,
127 uo_minus,
128 uo_not
129} unop_class_t;
130
131/** Binary operation */
132typedef struct {
133 /** Expression backlink */
134 struct stree_expr *expr;
135
136 /** Binary operation class */
137 binop_class_t bc;
138
139 /** Arguments */
140 struct stree_expr *arg1, *arg2;
141} stree_binop_t;
142
143/** Unary operation */
144typedef struct {
145 /** Expression backlink */
146 struct stree_expr *expr;
147
148 /** Operation class */
149 unop_class_t uc;
150
151 /** Argument */
152 struct stree_expr *arg;
153} stree_unop_t;
154
155/** New operation */
156typedef struct {
157 /** Expression backlink */
158 struct stree_expr *expr;
159
160 /** Type of object to construct. */
161 struct stree_texpr *texpr;
162
163 /** Constructor arguments */
164 list_t ctor_args; /* of stree_expr_t */
165} stree_new_t;
166
167/** Member access operation */
168typedef struct {
169 /** Expression backlink */
170 struct stree_expr *expr;
171
172 /** Argument */
173 struct stree_expr *arg;
174 /** Name of member being accessed. */
175 stree_ident_t *member_name;
176} stree_access_t;
177
178/** Function call operation */
179typedef struct {
180 /** Expression backlink */
181 struct stree_expr *expr;
182
183 /** Function */
184 struct stree_expr *fun;
185
186 /** Arguments */
187 list_t args; /* of stree_expr_t */
188} stree_call_t;
189
190typedef enum {
191 ac_set,
192 ac_increase
193} assign_class_t;
194
195/** Assignment */
196typedef struct {
197 /** Expression backlink */
198 struct stree_expr *expr;
199
200 assign_class_t ac;
201 struct stree_expr *dest, *src;
202} stree_assign_t;
203
204/** Indexing operation */
205typedef struct {
206 /** Expression backlink */
207 struct stree_expr *expr;
208
209 /** Base */
210 struct stree_expr *base;
211
212 /** Arguments (indices) */
213 list_t args; /* of stree_expr_t */
214} stree_index_t;
215
216/** @c as conversion operation */
217typedef struct {
218 /** Expression backlink */
219 struct stree_expr *expr;
220
221 /** Expression to convert */
222 struct stree_expr *arg;
223
224 /** Destination type of conversion. */
225 struct stree_texpr *dtype;
226} stree_as_t;
227
228/** Boxing of primitive type (pseudo)
229 *
230 * This pseudo-node is used internally to box a value of primitive type.
231 * It is implicitly inserted by stype_convert(). It does not correspond
232 * to a an explicit program construct.
233 */
234typedef struct {
235 /** Expression backlink */
236 struct stree_expr *expr;
237
238 /* Primitive type expression */
239 struct stree_expr *arg;
240} stree_box_t;
241
242/** Arithmetic expression class */
243typedef enum {
244 ec_nameref,
245 ec_literal,
246 ec_self_ref,
247 ec_binop,
248 ec_unop,
249 ec_new,
250 ec_access,
251 ec_call,
252 ec_assign,
253 ec_index,
254 ec_as,
255 ec_box
256} expr_class_t;
257
258/** Arithmetic expression */
259typedef struct stree_expr {
260 expr_class_t ec;
261
262 /** Type of this expression or @c NULL if not typed yet */
263 struct tdata_item *titem;
264
265 /** Coordinate span */
266 struct cspan *cspan;
267
268 union {
269 stree_nameref_t *nameref;
270 stree_literal_t *literal;
271 stree_self_ref_t *self_ref;
272 stree_binop_t *binop;
273 stree_unop_t *unop;
274 stree_new_t *new_op;
275 stree_access_t *access;
276 stree_call_t *call;
277 stree_index_t *index;
278 stree_assign_t *assign;
279 stree_as_t *as_op;
280 stree_box_t *box;
281 } u;
282} stree_expr_t;
283
284/*
285 * Type expressions
286 */
287
288struct stree_texpr;
289
290/** Type literal class */
291typedef enum {
292 tlc_bool,
293 tlc_char,
294 tlc_int,
295 tlc_resource,
296 tlc_string
297} tliteral_class_t;
298
299/** Type literal */
300typedef struct {
301 /** Type expression backlink */
302 struct stree_texpr *texpr;
303
304 tliteral_class_t tlc;
305} stree_tliteral_t;
306
307/** Type name reference */
308typedef struct {
309 /** Type expression backlink */
310 struct stree_texpr *texpr;
311
312 stree_ident_t *name;
313} stree_tnameref_t;
314
315/** Type member access operation */
316typedef struct {
317 /** Type expression backlink */
318 struct stree_texpr *texpr;
319
320 /** Argument */
321 struct stree_texpr *arg;
322
323 /** Name of member being accessed. */
324 stree_ident_t *member_name;
325} stree_taccess_t;
326
327/** Type application operation */
328typedef struct {
329 /** Type expression backlink */
330 struct stree_texpr *texpr;
331
332 /* Base type */
333 struct stree_texpr *gtype;
334
335 /** (Formal) type arguments */
336 list_t targs; /* of stree_texpr_t */
337} stree_tapply_t;
338
339/** Type index operation */
340typedef struct {
341 /** Type expression backlink */
342 struct stree_texpr *texpr;
343
344 /** Base type */
345 struct stree_texpr *base_type;
346
347 /**
348 * Number of arguments (rank). Needed when only rank is specified
349 * and @c args are not used.
350 */
351 int n_args;
352
353 /** Arguments (extents) */
354 list_t args; /* of stree_expr_t */
355} stree_tindex_t;
356
357/** Type expression class */
358typedef enum {
359 tc_tliteral,
360 tc_tnameref,
361 tc_taccess,
362 tc_tapply,
363 tc_tindex
364} texpr_class_t;
365
366/** Type expression */
367typedef struct stree_texpr {
368 texpr_class_t tc;
369
370 /** Coordinate span */
371 struct cspan *cspan;
372
373 union {
374 stree_tliteral_t *tliteral;
375 stree_tnameref_t *tnameref;
376 stree_taccess_t *taccess;
377 stree_tapply_t *tapply;
378 stree_tindex_t *tindex;
379 } u;
380} stree_texpr_t;
381
382/*
383 * Statements, class members and module members.
384 */
385
386/** Statement block */
387typedef struct stree_block {
388 /** List of statements in the block */
389 list_t stats; /* of stree_stat_t */
390} stree_block_t;
391
392/** Variable declaration */
393typedef struct {
394 stree_ident_t *name;
395 stree_texpr_t *type;
396
397 /** Type of this variable or @c NULL if not typed yet */
398 struct tdata_item *titem;
399} stree_vdecl_t;
400
401/** @c except clause */
402typedef struct {
403 stree_ident_t *evar;
404 stree_texpr_t *etype;
405 stree_block_t *block;
406
407 /** Evaluated etype or @c NULL if not typed yet */
408 struct tdata_item *titem;
409} stree_except_t;
410
411/** @c if or @c elif clause */
412typedef struct {
413 stree_expr_t *cond;
414 stree_block_t *block;
415} stree_if_clause_t;
416
417/** If statement */
418typedef struct {
419 /** If and elif clauses */
420 list_t if_clauses; /* of stree_if_clause_t */
421
422 /** Else block */
423 stree_block_t *else_block;
424} stree_if_t;
425
426/** @c when clause */
427typedef struct {
428 /** List of expressions -- cases -- for this clause */
429 list_t exprs; /* of stree_expr_t */
430 stree_block_t *block;
431} stree_when_t;
432
433/** Switch statement */
434typedef struct {
435 /** Switch expression */
436 stree_expr_t *expr;
437
438 /** When clauses */
439 list_t when_clauses; /* of stree_when_t */
440
441 /** Else block */
442 stree_block_t *else_block;
443} stree_switch_t;
444
445/** While statement */
446typedef struct {
447 stree_expr_t *cond;
448 stree_block_t *body;
449} stree_while_t;
450
451/** For statement */
452typedef struct {
453 stree_block_t *body;
454} stree_for_t;
455
456/** Raise statement */
457typedef struct {
458 stree_expr_t *expr;
459} stree_raise_t;
460
461/** Break statement */
462typedef struct {
463} stree_break_t;
464
465/** Return statement */
466typedef struct {
467 stree_expr_t *expr;
468} stree_return_t;
469
470/** Expression statement */
471typedef struct {
472 stree_expr_t *expr;
473} stree_exps_t;
474
475/** With-try-except-finally (WEF) statement */
476typedef struct {
477 stree_block_t *with_block;
478 list_t except_clauses; /* of stree_except_t */
479 stree_block_t *finally_block;
480} stree_wef_t;
481
482/** Statement class */
483typedef enum {
484 st_vdecl,
485 st_if,
486 st_switch,
487 st_while,
488 st_for,
489 st_raise,
490 st_break,
491 st_return,
492 st_exps,
493 st_wef
494} stat_class_t;
495
496/** Statement */
497typedef struct {
498 stat_class_t sc;
499
500 union {
501 stree_vdecl_t *vdecl_s;
502 stree_if_t *if_s;
503 stree_switch_t *switch_s;
504 stree_while_t *while_s;
505 stree_for_t *for_s;
506 stree_raise_t *raise_s;
507 stree_break_t *break_s;
508 stree_return_t *return_s;
509 stree_exps_t *exp_s;
510 stree_wef_t *wef_s;
511 } u;
512} stree_stat_t;
513
514/** Argument attribute class */
515typedef enum {
516 /** Packed argument (for variadic functions) */
517 aac_packed
518} arg_attr_class_t;
519
520/** Argument atribute */
521typedef struct {
522 arg_attr_class_t aac;
523} stree_arg_attr_t;
524
525/** Formal function parameter */
526typedef struct {
527 /* Argument name */
528 stree_ident_t *name;
529
530 /* Argument type */
531 stree_texpr_t *type;
532
533 /* Attributes */
534 list_t attr; /* of stree_arg_attr_t */
535} stree_proc_arg_t;
536
537/** Function signature.
538 *
539 * Formal parameters and return type. This is common to function and delegate
540 * delcarations.
541 */
542typedef struct {
543 /** Formal parameters */
544 list_t args; /* of stree_proc_arg_t */
545
546 /** Variadic argument or @c NULL if none. */
547 stree_proc_arg_t *varg;
548
549 /** Return type */
550 stree_texpr_t *rtype;
551} stree_fun_sig_t;
552
553/** Procedure
554 *
555 * Procedure is the common term for a getter, setter or function body.
556 * A procedure can be invoked. However, the arguments are specified by
557 * the containing symbol.
558 */
559typedef struct stree_proc {
560 /** Symbol (function or property) containing the procedure */
561 struct stree_symbol *outer_symbol;
562
563 /** Main block for regular procedures */
564 stree_block_t *body;
565
566 /** Builtin handler for builtin procedures */
567 builtin_proc_t bi_handler;
568} stree_proc_t;
569
570/** Constructor declaration */
571typedef struct stree_ctor {
572 /** Constructor 'name'. Points to the @c new keyword. */
573 stree_ident_t *name;
574
575 /** Symbol */
576 struct stree_symbol *symbol;
577
578 /** Signature (arguments, return type is always none) */
579 stree_fun_sig_t *sig;
580
581 /** Constructor implementation */
582 stree_proc_t *proc;
583
584 /** Type item describing the constructor */
585 struct tdata_item *titem;
586} stree_ctor_t;
587
588/** Delegate declaration */
589typedef struct stree_deleg {
590 /** Delegate name */
591 stree_ident_t *name;
592
593 /** Symbol */
594 struct stree_symbol *symbol;
595
596 /** Signature (arguments and return type) */
597 stree_fun_sig_t *sig;
598
599 /** Type item describing the delegate */
600 struct tdata_item *titem;
601} stree_deleg_t;
602
603/** Enum member */
604typedef struct stree_embr {
605 /** Enum containing this declaration */
606 struct stree_enum *outer_enum;
607
608 /** Enum member name */
609 stree_ident_t *name;
610} stree_embr_t;
611
612/** Enum declaration */
613typedef struct stree_enum {
614 /** Enum name */
615 stree_ident_t *name;
616
617 /** Symbol */
618 struct stree_symbol *symbol;
619
620 /** List of enum members */
621 list_t members; /* of stree_embr_t */
622
623 /** Type item describing the enum */
624 struct tdata_item *titem;
625} stree_enum_t;
626
627/** Member function declaration */
628typedef struct stree_fun {
629 /** Function name */
630 stree_ident_t *name;
631
632 /** Symbol */
633 struct stree_symbol *symbol;
634
635 /** Signature (arguments and return type) */
636 stree_fun_sig_t *sig;
637
638 /** Function implementation */
639 stree_proc_t *proc;
640
641 /** Type item describing the function */
642 struct tdata_item *titem;
643} stree_fun_t;
644
645/** Member variable declaration */
646typedef struct stree_var {
647 stree_ident_t *name;
648 struct stree_symbol *symbol;
649 stree_texpr_t *type;
650} stree_var_t;
651
652/** Member property declaration */
653typedef struct stree_prop {
654 stree_ident_t *name;
655 struct stree_symbol *symbol;
656 stree_texpr_t *type;
657
658 stree_proc_t *getter;
659
660 stree_proc_t *setter;
661 stree_proc_arg_t *setter_arg;
662
663 /** Formal parameters (for indexed properties) */
664 list_t args; /* of stree_proc_arg_t */
665
666 /** Variadic argument or @c NULL if none. */
667 stree_proc_arg_t *varg;
668
669 /** Type of the property */
670 struct tdata_item *titem;
671} stree_prop_t;
672
673/**
674 * Fake identifiers used with symbols that do not really have one.
675 */
676#define CTOR_IDENT "$ctor"
677#define INDEXER_IDENT "$indexer"
678
679typedef enum {
680 csimbr_csi,
681 csimbr_ctor,
682 csimbr_deleg,
683 csimbr_enum,
684 csimbr_fun,
685 csimbr_var,
686 csimbr_prop
687} csimbr_class_t;
688
689/** Class, struct or interface member */
690typedef struct {
691 csimbr_class_t cc;
692
693 union {
694 struct stree_csi *csi;
695 stree_ctor_t *ctor;
696 stree_deleg_t *deleg;
697 stree_enum_t *enum_d;
698 stree_fun_t *fun;
699 stree_var_t *var;
700 stree_prop_t *prop;
701 } u;
702} stree_csimbr_t;
703
704typedef enum {
705 csi_class,
706 csi_struct,
707 csi_interface
708} csi_class_t;
709
710/** CSI formal type argument */
711typedef struct stree_targ {
712 stree_ident_t *name;
713 struct stree_symbol *symbol;
714} stree_targ_t;
715
716/** Class, struct or interface declaration */
717typedef struct stree_csi {
718 /** Which of class, struct or interface */
719 csi_class_t cc;
720
721 /** Name of this CSI */
722 stree_ident_t *name;
723
724 /** List of type arguments */
725 list_t targ; /* of stree_targ_t */
726
727 /** Symbol for this CSI */
728 struct stree_symbol *symbol;
729
730 /** Type expressions referencing inherited CSIs. */
731 list_t inherit; /* of stree_texpr_t */
732
733 /** Base CSI. Only available when ancr_state == ws_visited. */
734 struct stree_csi *base_csi;
735
736 /** Types of implemented or accumulated interfaces. */
737 list_t impl_if_ti; /* of tdata_item_t */
738
739 /** Node state for ancr walks. */
740 walk_state_t ancr_state;
741
742 /** List of CSI members */
743 list_t members; /* of stree_csimbr_t */
744} stree_csi_t;
745
746typedef enum {
747 /* Class, struct or interface declaration */
748 mc_csi,
749 /* Enum declaration */
750 mc_enum
751} modm_class_t;
752
753/** Module member */
754typedef struct {
755 modm_class_t mc;
756 union {
757 stree_csi_t *csi;
758 stree_enum_t *enum_d;
759 } u;
760} stree_modm_t;
761
762/** Module */
763typedef struct stree_module {
764 /** List of module members */
765 list_t members; /* of stree_modm_t */
766} stree_module_t;
767
768/** Symbol attribute class */
769typedef enum {
770 /** Builtin symbol (interpreter hook) */
771 sac_builtin,
772
773 /** Static symbol */
774 sac_static
775} symbol_attr_class_t;
776
777/** Symbol atribute */
778typedef struct {
779 symbol_attr_class_t sac;
780} stree_symbol_attr_t;
781
782typedef enum {
783 /** CSI (class, struct or interface) */
784 sc_csi,
785 /** Constructor */
786 sc_ctor,
787 /** Member delegate */
788 sc_deleg,
789 /** Enum */
790 sc_enum,
791 /** Member function */
792 sc_fun,
793 /** Member variable */
794 sc_var,
795 /** Member property */
796 sc_prop
797} symbol_class_t;
798
799/** Symbol
800 *
801 * A symbol is a common superclass of different program elements that
802 * allow us to refer to them, print their fully qualified names, etc.
803 */
804typedef struct stree_symbol {
805 symbol_class_t sc;
806
807 union {
808 struct stree_csi *csi;
809 stree_ctor_t *ctor;
810 stree_deleg_t *deleg;
811 stree_enum_t *enum_d;
812 stree_fun_t *fun;
813 stree_var_t *var;
814 stree_prop_t *prop;
815 } u;
816
817 /** Containing CSI */
818 stree_csi_t *outer_csi;
819
820 /** Symbol attributes */
821 list_t attr; /* of stree_symbol_attr_t */
822} stree_symbol_t;
823
824/** Program */
825typedef struct stree_program {
826 /** The one and only module in the program */
827 stree_module_t *module;
828
829 /** Builtin symbols binding. */
830 struct builtin *builtin;
831} stree_program_t;
832
833#endif
Note: See TracBrowser for help on using the repository browser.