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 | #ifndef STREE_T_H_
|
---|
30 | #define STREE_T_H_
|
---|
31 |
|
---|
32 | #include "list_t.h"
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * Arithmetic expressions
|
---|
36 | */
|
---|
37 |
|
---|
38 | struct stree_expr;
|
---|
39 |
|
---|
40 | /** Identifier */
|
---|
41 | typedef struct {
|
---|
42 | int sid;
|
---|
43 | } stree_ident_t;
|
---|
44 |
|
---|
45 | /** Name reference */
|
---|
46 | typedef struct {
|
---|
47 | stree_ident_t *name;
|
---|
48 | } stree_nameref_t;
|
---|
49 |
|
---|
50 | /** Reference to currently active object. */
|
---|
51 | typedef struct {
|
---|
52 | } stree_self_ref_t;
|
---|
53 |
|
---|
54 | typedef struct {
|
---|
55 | int value;
|
---|
56 | } stree_lit_int_t;
|
---|
57 |
|
---|
58 | /** Reference literal (there is only one: @c nil). */
|
---|
59 | typedef struct {
|
---|
60 | } stree_lit_ref_t;
|
---|
61 |
|
---|
62 | typedef struct {
|
---|
63 | char *value;
|
---|
64 | } stree_lit_string_t;
|
---|
65 |
|
---|
66 | typedef enum {
|
---|
67 | ltc_int,
|
---|
68 | ltc_ref,
|
---|
69 | ltc_string
|
---|
70 | } literal_class_t;
|
---|
71 |
|
---|
72 | /** Literal */
|
---|
73 | typedef struct {
|
---|
74 | literal_class_t ltc;
|
---|
75 | union {
|
---|
76 | stree_lit_int_t lit_int;
|
---|
77 | stree_lit_ref_t lit_ref;
|
---|
78 | stree_lit_string_t lit_string;
|
---|
79 | } u;
|
---|
80 | } stree_literal_t;
|
---|
81 |
|
---|
82 | /** Binary operation class */
|
---|
83 | typedef enum {
|
---|
84 | bo_equal,
|
---|
85 | bo_notequal,
|
---|
86 | bo_lt,
|
---|
87 | bo_gt,
|
---|
88 | bo_lt_equal,
|
---|
89 | bo_gt_equal,
|
---|
90 | bo_plus
|
---|
91 | } binop_class_t;
|
---|
92 |
|
---|
93 | /** Unary operation class */
|
---|
94 | typedef enum {
|
---|
95 | uo_plus
|
---|
96 | } unop_class_t;
|
---|
97 |
|
---|
98 | /** Binary operation */
|
---|
99 | typedef struct {
|
---|
100 | /** Binary operation class */
|
---|
101 | binop_class_t bc;
|
---|
102 |
|
---|
103 | /** Arguments */
|
---|
104 | struct stree_expr *arg1, *arg2;
|
---|
105 | } stree_binop_t;
|
---|
106 |
|
---|
107 | /** Unary operation */
|
---|
108 | typedef struct {
|
---|
109 | /** Operation class */
|
---|
110 | unop_class_t oc;
|
---|
111 |
|
---|
112 | /** Argument */
|
---|
113 | struct stree_expr *arg;
|
---|
114 | } stree_unop_t;
|
---|
115 |
|
---|
116 | /** New operation */
|
---|
117 | typedef struct {
|
---|
118 | /** Type of object to construct. */
|
---|
119 | struct stree_texpr *texpr;
|
---|
120 | } stree_new_t;
|
---|
121 |
|
---|
122 | /** Member access operation */
|
---|
123 | typedef struct {
|
---|
124 | /** Argument */
|
---|
125 | struct stree_expr *arg;
|
---|
126 | /** Name of member being accessed. */
|
---|
127 | stree_ident_t *member_name;
|
---|
128 | } stree_access_t;
|
---|
129 |
|
---|
130 | /** Function call operation */
|
---|
131 | typedef struct {
|
---|
132 | /** Function */
|
---|
133 | struct stree_expr *fun;
|
---|
134 |
|
---|
135 | /** Arguments */
|
---|
136 | list_t args; /* of stree_expr_t */
|
---|
137 | } stree_call_t;
|
---|
138 |
|
---|
139 | typedef enum {
|
---|
140 | ac_set,
|
---|
141 | ac_increase
|
---|
142 | } assign_class_t;
|
---|
143 |
|
---|
144 | /** Assignment */
|
---|
145 | typedef struct {
|
---|
146 | assign_class_t ac;
|
---|
147 | struct stree_expr *dest, *src;
|
---|
148 | } stree_assign_t;
|
---|
149 |
|
---|
150 | /** Indexing operation */
|
---|
151 | typedef struct {
|
---|
152 | /** Base */
|
---|
153 | struct stree_expr *base;
|
---|
154 |
|
---|
155 | /** Arguments (indices) */
|
---|
156 | list_t args; /* of stree_expr_t */
|
---|
157 | } stree_index_t;
|
---|
158 |
|
---|
159 | /** Arithmetic expression class */
|
---|
160 | typedef enum {
|
---|
161 | ec_nameref,
|
---|
162 | ec_literal,
|
---|
163 | ec_self_ref,
|
---|
164 | ec_binop,
|
---|
165 | ec_unop,
|
---|
166 | ec_new,
|
---|
167 | ec_access,
|
---|
168 | ec_call,
|
---|
169 | ec_assign,
|
---|
170 | ec_index
|
---|
171 | } expr_class_t;
|
---|
172 |
|
---|
173 | /** Arithmetic expression */
|
---|
174 | typedef struct stree_expr {
|
---|
175 | expr_class_t ec;
|
---|
176 |
|
---|
177 | struct tdata_item *titem;
|
---|
178 |
|
---|
179 | union {
|
---|
180 | stree_nameref_t *nameref;
|
---|
181 | stree_literal_t *literal;
|
---|
182 | stree_self_ref_t *self_ref;
|
---|
183 | stree_binop_t *binop;
|
---|
184 | stree_unop_t *unop;
|
---|
185 | stree_new_t *new_op;
|
---|
186 | stree_access_t *access;
|
---|
187 | stree_call_t *call;
|
---|
188 | stree_index_t *index;
|
---|
189 | stree_assign_t *assign;
|
---|
190 | } u;
|
---|
191 | } stree_expr_t;
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * Type expressions
|
---|
195 | */
|
---|
196 |
|
---|
197 | struct stree_texpr;
|
---|
198 |
|
---|
199 | /** Type literal class */
|
---|
200 | typedef enum {
|
---|
201 | tlc_int,
|
---|
202 | tlc_string
|
---|
203 | } tliteral_class_t;
|
---|
204 |
|
---|
205 | /** Type literal */
|
---|
206 | typedef struct {
|
---|
207 | tliteral_class_t tlc;
|
---|
208 | } stree_tliteral_t;
|
---|
209 |
|
---|
210 | /** Type name reference */
|
---|
211 | typedef struct {
|
---|
212 | stree_ident_t *name;
|
---|
213 | } stree_tnameref_t;
|
---|
214 |
|
---|
215 | /** Type member access operation */
|
---|
216 | typedef struct {
|
---|
217 | /** Argument */
|
---|
218 | struct stree_texpr *arg;
|
---|
219 | /** Name of member being accessed. */
|
---|
220 | stree_ident_t *member_name;
|
---|
221 | } stree_taccess_t;
|
---|
222 |
|
---|
223 | /** Type application operation */
|
---|
224 | typedef struct {
|
---|
225 | /** Arguments */
|
---|
226 | struct stree_texpr *gtype, *targ;
|
---|
227 | } stree_tapply_t;
|
---|
228 |
|
---|
229 | /** Type index operation */
|
---|
230 | typedef struct {
|
---|
231 | /** Base type */
|
---|
232 | struct stree_texpr *base_type;
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Number of arguments (rank). Needed when only rank is specified
|
---|
236 | * and @c args are not used.
|
---|
237 | */
|
---|
238 | int n_args;
|
---|
239 |
|
---|
240 | /** Arguments (extents) */
|
---|
241 | list_t args; /* of stree_expr_t */
|
---|
242 | } stree_tindex_t;
|
---|
243 |
|
---|
244 | /** Type expression class */
|
---|
245 | typedef enum {
|
---|
246 | tc_tliteral,
|
---|
247 | tc_tnameref,
|
---|
248 | tc_taccess,
|
---|
249 | tc_tapply,
|
---|
250 | tc_tindex
|
---|
251 | } texpr_class_t;
|
---|
252 |
|
---|
253 | /** Arithmetic expression */
|
---|
254 | typedef struct stree_texpr {
|
---|
255 | texpr_class_t tc;
|
---|
256 |
|
---|
257 | union {
|
---|
258 | stree_tliteral_t *tliteral;
|
---|
259 | stree_tnameref_t *tnameref;
|
---|
260 | stree_taccess_t *taccess;
|
---|
261 | stree_tapply_t *tapply;
|
---|
262 | stree_tindex_t *tindex;
|
---|
263 | } u;
|
---|
264 | } stree_texpr_t;
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * Statements, class members and module members.
|
---|
268 | */
|
---|
269 |
|
---|
270 | /** Statement block */
|
---|
271 | typedef struct stree_block {
|
---|
272 | /** List of statements in the block */
|
---|
273 | list_t stats; /* of stree_stat_t */
|
---|
274 | } stree_block_t;
|
---|
275 |
|
---|
276 | /** Variable declaration */
|
---|
277 | typedef struct {
|
---|
278 | stree_ident_t *name;
|
---|
279 | stree_texpr_t *type;
|
---|
280 | } stree_vdecl_t;
|
---|
281 |
|
---|
282 | /** @c except clause */
|
---|
283 | typedef struct {
|
---|
284 | stree_ident_t *evar;
|
---|
285 | stree_texpr_t *etype;
|
---|
286 | stree_block_t *block;
|
---|
287 | } stree_except_t;
|
---|
288 |
|
---|
289 | /** If statement */
|
---|
290 | typedef struct {
|
---|
291 | stree_expr_t *cond;
|
---|
292 | stree_block_t *if_block;
|
---|
293 | stree_block_t *else_block;
|
---|
294 | } stree_if_t;
|
---|
295 |
|
---|
296 | /** While statement */
|
---|
297 | typedef struct {
|
---|
298 | stree_expr_t *cond;
|
---|
299 | stree_block_t *body;
|
---|
300 | } stree_while_t;
|
---|
301 |
|
---|
302 | /** For statement */
|
---|
303 | typedef struct {
|
---|
304 | stree_block_t *body;
|
---|
305 | } stree_for_t;
|
---|
306 |
|
---|
307 | /** Raise statement */
|
---|
308 | typedef struct {
|
---|
309 | stree_expr_t *expr;
|
---|
310 | } stree_raise_t;
|
---|
311 |
|
---|
312 | /** Return statement */
|
---|
313 | typedef struct {
|
---|
314 | stree_expr_t *expr;
|
---|
315 | } stree_return_t;
|
---|
316 |
|
---|
317 | /** Expression statement */
|
---|
318 | typedef struct {
|
---|
319 | stree_expr_t *expr;
|
---|
320 | } stree_exps_t;
|
---|
321 |
|
---|
322 | /** With-try-except-finally statement (WEF) */
|
---|
323 | typedef struct {
|
---|
324 | stree_block_t *with_block;
|
---|
325 | list_t except_clauses; /* of stree_except_t */
|
---|
326 | stree_block_t *finally_block;
|
---|
327 | } stree_wef_t;
|
---|
328 |
|
---|
329 | /** Statement class */
|
---|
330 | typedef enum {
|
---|
331 | st_vdecl,
|
---|
332 | st_if,
|
---|
333 | st_while,
|
---|
334 | st_for,
|
---|
335 | st_raise,
|
---|
336 | st_return,
|
---|
337 | st_exps,
|
---|
338 | st_wef
|
---|
339 | } stat_class_t;
|
---|
340 |
|
---|
341 | /** Statement */
|
---|
342 | typedef struct {
|
---|
343 | stat_class_t sc;
|
---|
344 |
|
---|
345 | union {
|
---|
346 | stree_vdecl_t *vdecl_s;
|
---|
347 | stree_if_t *if_s;
|
---|
348 | stree_while_t *while_s;
|
---|
349 | stree_for_t *for_s;
|
---|
350 | stree_raise_t *raise_s;
|
---|
351 | stree_return_t *return_s;
|
---|
352 | stree_exps_t *exp_s;
|
---|
353 | stree_wef_t *wef_s;
|
---|
354 | } u;
|
---|
355 | } stree_stat_t;
|
---|
356 |
|
---|
357 | /** Argument attribute class */
|
---|
358 | typedef enum {
|
---|
359 | /** Packed argument (for variadic functions) */
|
---|
360 | aac_packed
|
---|
361 | } arg_attr_class_t;
|
---|
362 |
|
---|
363 | /** Argument atribute */
|
---|
364 | typedef struct {
|
---|
365 | arg_attr_class_t aac;
|
---|
366 | } stree_arg_attr_t;
|
---|
367 |
|
---|
368 | /** Formal function parameter */
|
---|
369 | typedef struct {
|
---|
370 | /* Argument name */
|
---|
371 | stree_ident_t *name;
|
---|
372 |
|
---|
373 | /* Argument type */
|
---|
374 | stree_texpr_t *type;
|
---|
375 |
|
---|
376 | /* Attributes */
|
---|
377 | list_t attr; /* of stree_arg_attr_t */
|
---|
378 | } stree_proc_arg_t;
|
---|
379 |
|
---|
380 | /** Procedure
|
---|
381 | *
|
---|
382 | * Procedure is the common term for a getter, setter or function body.
|
---|
383 | * A procedure can be invoked. However, the arguments are specified by
|
---|
384 | * the containing symbol.
|
---|
385 | */
|
---|
386 | typedef struct stree_proc {
|
---|
387 | /** Symbol (function or property) containing the procedure */
|
---|
388 | struct stree_symbol *outer_symbol;
|
---|
389 |
|
---|
390 | /** Main block */
|
---|
391 | stree_block_t *body;
|
---|
392 | } stree_proc_t;
|
---|
393 |
|
---|
394 | /** Member function declaration */
|
---|
395 | typedef struct stree_fun {
|
---|
396 | /** Function name */
|
---|
397 | stree_ident_t *name;
|
---|
398 |
|
---|
399 | /** Symbol */
|
---|
400 | struct stree_symbol *symbol;
|
---|
401 |
|
---|
402 | /** Formal parameters */
|
---|
403 | list_t args; /* of stree_proc_arg_t */
|
---|
404 |
|
---|
405 | /** Variadic argument or @c NULL if none. */
|
---|
406 | stree_proc_arg_t *varg;
|
---|
407 |
|
---|
408 | /** Return type */
|
---|
409 | stree_texpr_t *rtype;
|
---|
410 |
|
---|
411 | /** Function implementation */
|
---|
412 | stree_proc_t *proc;
|
---|
413 | } stree_fun_t;
|
---|
414 |
|
---|
415 | /** Member variable declaration */
|
---|
416 | typedef struct stree_var {
|
---|
417 | stree_ident_t *name;
|
---|
418 | struct stree_symbol *symbol;
|
---|
419 | stree_texpr_t *type;
|
---|
420 | } stree_var_t;
|
---|
421 |
|
---|
422 | /** Member property declaration */
|
---|
423 | typedef struct stree_prop {
|
---|
424 | stree_ident_t *name;
|
---|
425 | struct stree_symbol *symbol;
|
---|
426 | stree_texpr_t *type;
|
---|
427 |
|
---|
428 | stree_proc_t *getter;
|
---|
429 |
|
---|
430 | stree_proc_t *setter;
|
---|
431 | stree_proc_arg_t *setter_arg;
|
---|
432 |
|
---|
433 | /** Formal parameters (for indexed properties) */
|
---|
434 | list_t args; /* of stree_proc_arg_t */
|
---|
435 |
|
---|
436 | /** Variadic argument or @c NULL if none. */
|
---|
437 | stree_proc_arg_t *varg;
|
---|
438 | } stree_prop_t;
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Fake identifiers used with symbols that do not really have one.
|
---|
442 | * (Mostly for error messages.)
|
---|
443 | */
|
---|
444 | #define INDEXER_IDENT "$indexer"
|
---|
445 |
|
---|
446 | typedef enum {
|
---|
447 | csimbr_csi,
|
---|
448 | csimbr_fun,
|
---|
449 | csimbr_var,
|
---|
450 | csimbr_prop
|
---|
451 | } csimbr_class_t;
|
---|
452 |
|
---|
453 | /** Class, struct or interface member */
|
---|
454 | typedef struct {
|
---|
455 | csimbr_class_t cc;
|
---|
456 |
|
---|
457 | union {
|
---|
458 | struct stree_csi *csi;
|
---|
459 | stree_fun_t *fun;
|
---|
460 | stree_var_t *var;
|
---|
461 | stree_prop_t *prop;
|
---|
462 | } u;
|
---|
463 | } stree_csimbr_t;
|
---|
464 |
|
---|
465 | typedef enum {
|
---|
466 | csi_class,
|
---|
467 | csi_struct,
|
---|
468 | csi_interface
|
---|
469 | } csi_class_t;
|
---|
470 |
|
---|
471 | /** Class, struct or interface declaration */
|
---|
472 | typedef struct stree_csi {
|
---|
473 | /** Which of class, struct or interface */
|
---|
474 | csi_class_t cc;
|
---|
475 |
|
---|
476 | /** Name of this CSI */
|
---|
477 | stree_ident_t *name;
|
---|
478 |
|
---|
479 | /** Symbol for this CSI */
|
---|
480 | struct stree_symbol *symbol;
|
---|
481 |
|
---|
482 | /** Type expression referencing base CSI. */
|
---|
483 | stree_texpr_t *base_csi_ref;
|
---|
484 |
|
---|
485 | /** Base CSI. Only available when ancr_state == ws_visited. */
|
---|
486 | struct stree_csi *base_csi;
|
---|
487 |
|
---|
488 | /** Node state for ancr walks. */
|
---|
489 | walk_state_t ancr_state;
|
---|
490 |
|
---|
491 | /** List of CSI members */
|
---|
492 | list_t members; /* of stree_csimbr_t */
|
---|
493 | } stree_csi_t;
|
---|
494 |
|
---|
495 | typedef enum {
|
---|
496 | /* Class, struct or interface declaration */
|
---|
497 | mc_csi
|
---|
498 | } modm_class_t;
|
---|
499 |
|
---|
500 | /** Module member */
|
---|
501 | typedef struct {
|
---|
502 | modm_class_t mc;
|
---|
503 | union {
|
---|
504 | stree_csi_t *csi;
|
---|
505 | } u;
|
---|
506 | } stree_modm_t;
|
---|
507 |
|
---|
508 | /** Module */
|
---|
509 | typedef struct stree_module {
|
---|
510 | /** List of module members */
|
---|
511 | list_t members; /* of stree_modm_t */
|
---|
512 | } stree_module_t;
|
---|
513 |
|
---|
514 | typedef enum {
|
---|
515 | sc_csi,
|
---|
516 | sc_fun,
|
---|
517 | sc_var,
|
---|
518 | sc_prop
|
---|
519 | } symbol_class_t;
|
---|
520 |
|
---|
521 | /** Symbol
|
---|
522 | *
|
---|
523 | * A symbol is a common superclass of different program elements that
|
---|
524 | * allow us to refer to them, print their fully qualified names, etc.
|
---|
525 | */
|
---|
526 | typedef struct stree_symbol {
|
---|
527 | symbol_class_t sc;
|
---|
528 |
|
---|
529 | union {
|
---|
530 | struct stree_csi *csi;
|
---|
531 | stree_fun_t *fun;
|
---|
532 | stree_var_t *var;
|
---|
533 | stree_prop_t *prop;
|
---|
534 | } u;
|
---|
535 |
|
---|
536 | /** Containing CSI (for all symbols) */
|
---|
537 | stree_csi_t *outer_csi;
|
---|
538 |
|
---|
539 | /** Containing block (for block-level symbols) */
|
---|
540 | stree_block_t *outer_block;
|
---|
541 | } stree_symbol_t;
|
---|
542 |
|
---|
543 | /** Program */
|
---|
544 | typedef struct stree_program {
|
---|
545 | /** The one and only module in the program */
|
---|
546 | stree_module_t *module;
|
---|
547 | } stree_program_t;
|
---|
548 |
|
---|
549 | #endif
|
---|