1 #ifndef __TRNOD_HPP__
2 #define __TRNOD_HPP__
3 
4 
5 #include "tpexpr.h"
6 
7 enum {
8   ctx_program,      // program
9   ctx_module,       // list of declaration
10   ctx_object,       // list of object components
11   ctx_value,        // value of expression
12   ctx_rvalue,       // rvalue part of assignment
13   ctx_lvalue,       // lvalue part of assignment
14   ctx_lvalarray,    // array element is used as lvalue
15   ctx_procptr,      // pointer to procedure
16   ctx_apply,        // functions is applied to arguments
17   ctx_varpar,       // parameter passed by reference
18   ctx_valpar,       // parameter passed by value
19   ctx_statement,    // statement
20   ctx_condition,    // result is used in condition expression
21   ctx_component,    // type od array component
22   ctx_block,        // block context
23   ctx_access,       // result is used in access to record component operation
24   ctx_array,        // result is used as base value in index expression
25   ctx_variant,      // variant part of record
26   ctx_reftyp,       // reference definition type
27   ctx_record,       // initializer for record constant
28   ctx_union,        // record with only variant component
29   ctx_constant,     // constant expression
30   ctx_toascii       // convertion of number to string
31 };
32 
33 
34 
35 class decl_node;
36 class expr_node;
37 class block_node;
38 class compound_node;
39 class expr_group_node;
40 
41 class node : public heap_object {
42   public:
43     token*    f_tkn;		/* First token */
44     token*    l_tkn;		/* Last token */
45 
46     void force_semicolon();
47     void swallow_semicolon();
48 
node()49     node() { f_tkn = l_tkn = NULL; }
50 
51     virtual void attrib(int ctx);
52     virtual void translate(int ctx);
53 };
54 
55 class token_list : public heap_object {
56   public:
57     token*           ident;
58     symbol*          var;
59     token_list*      next;
60 
61     token_list(token* id, token_list* chain = NULL) {
62 	ident = id;
63 	next = chain;
64     }
65 };
66 
67 //=============================================================================
68 // Program level grammar:
69 //   program         ::= PROGRAM ident [ '(' parameter_list ')' ] block '.'
70 //   parameter_list  ::= ident { ',' ident }
71 //   block           ::= decl_part_list compoundst
72 //   decl_part_list  ::= { decl_part }
73 //   decl_part       ::= label_decl_part | const_def_part | type_def_part
74 //                       | var_decl_part | proc_decl proc_fwd_decl
75 //=============================================================================
76 
77 class import_list_node : public node {
78   public:
79     token*                lpar;
80     token_list*           params;
81     token*                rpar;
82 
83     import_list_node(token* lpar, token_list* params, token* rpar);
84 
85     virtual void attrib(int ctx);
86     virtual void translate(int ctx);
87 };
88 
89 
90 class program_node : public node {
91   public:
92     token*                program;
93     token*                name;
94     import_list_node*     params;
95     token*                semi;
96     block_node*           block;
97     token*                end;    // '.'
98     proc_tp*              main;
99 
100     program_node(token* program, token* name, import_list_node* params,
101 		 token* semi, block_node* body, token* end);
102 
103     virtual void attrib(int ctx);
104     virtual void translate(int ctx);
105 };
106 
107 
108 class module_node : public node {
109   public:
110     token*                program;
111     token*                name;
112     import_list_node*     params;
113     token*                semi;
114     decl_node*            decls;
115     token*                t_dot;
116     module_node(token* program, token* name, import_list_node* params,
117 		token* semi, decl_node* decls, token* t_dot);
118 
119     virtual void attrib(int ctx);
120     virtual void translate(int ctx);
121 };
122 
123 class unit_node : public node {
124   public:
125     token*                t_unit;
126     token*                t_name;
127     token*                t_semi;
128     token*                t_interface;
129     decl_node*            unit_decl;
130     token*                t_implementation;
131     decl_node*            unit_def;
132     compound_node*        initializer;
133     token*                t_end;
134     token*                t_dot; // '.'
135     proc_tp*              main;
136 
137     static bool           interface_part; // translate interface part of module
138     static char*          unit_name;
139 
140     unit_node(token* t_unit, token* t_name, token* t_semi, token* t_interface,
141 	      decl_node* unit_decl, token* t_implementation,
142 	      decl_node* unit_def, compound_node* initializer,
143 	      token* t_end, token* t_dot);
144 
145 
146     virtual void attrib(int ctx);
147     virtual void translate(int ctx);
148 };
149 
150 
151 class block_node : public node {
152   public:
153     decl_node*             decls;
154     compound_node*         body;
155 
156     block_node(decl_node* decls, compound_node* body);
157 
158     virtual void attrib(int ctx);
159     virtual void translate(int ctx);
160 };
161 
162 //=============================================================================
163 // Statement level grammar
164 //   statement       ::= compoundst | assignmentst | gotost | switchst | ifst
165 //                    | forst | whilest | repeatst | procst | returnst
166 //                    | withst | labelst | emtyst | writest | readst
167 //   sequence        ::= statement { ';' statement }
168 //   compoundst      ::= BEGIN sequence END
169 //   assignmentst    ::= expr ':=' expr
170 //   gotost          ::= GOTO ident
171 //   labelst         ::= ident ':'
172 //   switchst        ::= CASE expr OF case_list END
173 //   case_list       ::= case_list_elem { ';' case-list-elem } [ ';' ]
174 //   case_list_elem  ::= expr_list ':' statement | OTHERWISE statement
175 //   ifst            ::= IF expr THEN statement [ELSE statement]
176 //   forst           ::= FOR identifier := expr (TO | DOWNTO) expr DO statement
177 //   repeatst        ::= REPEAT sequence UNTIL expr
178 //   whilest         ::= WHILE expr DO statement
179 //   withst          ::= WITH expr_list DO statement
180 //   procst          ::= ident [ expr_group ]
181 //   writest         ::= (WRITE | WRITELN) [ write_list ]
182 //   readst          ::= (READ | READLN) [ expr_group ]
183 //   returnst        ::= RETURN
184 //   emptyst         ::=
185 //=============================================================================
186 
187 
188 
189 class stmt_node : public node {
190   public:
191     stmt_node*   next;
stmt_node()192     stmt_node() : next(NULL) {}
193 
194     virtual bool is_compound();
195 };
196 
197 class label_node : public stmt_node {
198   public:
199     token*       ident;		/* label name */
200     token*       colon;		/* colon */
201     stmt_node*   stmt;
202 
203     label_node(token* ident, token* colon, stmt_node* stmt);
204 
205     virtual void attrib(int ctx);
206     virtual void translate(int ctx);
207 };
208 
209 class with_node : public stmt_node {
210   public:
211     token*      t_with;
212     expr_node*  ptrs;
213     token*      t_do;
214     stmt_node*  body;
215     int         nested_counter;
216 
217     static int  nested;
218 
219     with_node(token* t_with, expr_node* ptrs, token* t_do, stmt_node* body);
220 
221     virtual void attrib(int ctx);
222     virtual void translate(int ctx);
223 };
224 
225 class pcall_node : public stmt_node {
226   public:
227     expr_node*   fcall;
228 
229     pcall_node(expr_node* fcall);
230 
231     virtual void attrib(int ctx);
232     virtual void translate(int ctx);
233 };
234 
235 class read_node : public stmt_node {
236   public:
237     token*           t_read;
238     expr_group_node* params;
239 
240     read_node(token* t_read, expr_group_node* params = NULL);
241 
242     virtual void attrib(int ctx);
243     virtual void translate(int ctx);
244 };
245 
246 class write_list_node;
247 
248 class write_node : public stmt_node {
249   public:
250     token*              t_write;
251     write_list_node*    params;
252 
253     write_node(token* t_write, write_list_node* params = NULL);
254 
255     virtual void attrib(int ctx);
256     virtual void translate(int ctx);
257 };
258 
259 
260 class compound_node : public stmt_node {
261   public:
262     token        *t_begin, *t_end;
263     stmt_node*   body;
264 
265     compound_node(token* t_begin,  stmt_node* body, token* t_end);
266 
267     virtual void attrib(int ctx);
268     virtual void translate(int ctx);
269     virtual bool is_compound();
270 };
271 
272 
273 class assign_node : public stmt_node {
274   public:
275     expr_node*   lval;
276     token*       assign;
277     expr_node*   rval;
278 
279     static assign_node* stmt;
280 
281     assign_node(expr_node* lval, token* assign, expr_node* rval);
282 
283     virtual void attrib(int ctx);
284     virtual void translate(int ctx);
285 };
286 
287 class goto_node : public stmt_node {
288   public:
289     token*       t_goto;
290     token*       t_label;
291 
292     goto_node(token* t_goto, token* t_label);
293 
294     virtual void attrib(int ctx);
295     virtual void translate(int ctx);
296 };
297 
298 
299 class case_node : public node {
300   public:
301     case_node*          next;
302     expr_node*          list;
303     token*              coln; // ':'
304     stmt_node*          stmt;
305 
306     case_node(expr_node*, token* coln, stmt_node* stmt);
307 
308     virtual void attrib(int ctx);
309     virtual void translate(int ctx);
310 };
311 
312 class switch_node : public stmt_node {
313   public:
314     token        *t_case, *t_of;
315     expr_node*   expr;
316     case_node*   cases;
317     token*       t_end;
318 
319     switch_node(token* t_case, expr_node* expr, token* t_of, case_node* cases,
320 		token* t_end);
321 
322     virtual void attrib(int ctx);
323     virtual void translate(int ctx);
324 };
325 
326 
327 class if_node : public stmt_node {
328   public:
329     expr_node*   expr;
330     stmt_node*   alt1;
331     stmt_node*   alt2;
332 
333     token        *t_if, *t_then, *t_else;
334 
335     if_node(token* t_if, expr_node* expr, token* t_then, stmt_node* alt1,
336 	    token* t_else = NULL, stmt_node* alt2 = NULL);
337 
338     virtual void attrib(int ctx);
339     virtual void translate(int ctx);
340 };
341 
342 class for_node : public stmt_node {
343   public:
344     token        *t_for, *t_ident, *t_asg, *t_to, *t_do;
345     expr_node    *from, *till;
346     stmt_node    *body;
347     symbol       *var;
348 
349     for_node(token* t_for, token* t_ident, token* t_asg,  expr_node* from,
350 	     token* t_to, expr_node* till, token* t_do, stmt_node* body);
351 
352     virtual void attrib(int ctx);
353     virtual void translate(int ctx);
354 };
355 
356 class while_node : public stmt_node {
357   public:
358     token        *t_while, *t_do;
359     expr_node    *expr;
360     stmt_node    *body;
361 
362     while_node(token* t_while, expr_node* expr, token* t_do, stmt_node* body);
363 
364     virtual void attrib(int ctx);
365     virtual void translate(int ctx);
366 };
367 
368 class repeat_node : public stmt_node {
369   public:
370     token        *t_repeat, *t_until;
371     stmt_node    *body;
372     expr_node    *expr;
373 
374     repeat_node(token* t_repeat, stmt_node* body, token* t_until, expr_node* expr);
375 
376     virtual void attrib(int ctx);
377     virtual void translate(int ctx);
378 };
379 
380 class return_node : public stmt_node {
381   public:
382     token        *t_return;
383 
384     return_node(token* t_return);
385 
386     virtual void attrib(int ctx);
387     virtual void translate(int ctx);
388 };
389 
390 class empty_node : public stmt_node {
391   public:
392     token* last;
393     empty_node(token* last);
394 
395     virtual void attrib(int ctx);
396     virtual void translate(int ctx);
397 };
398 
399 //=============================================================================
400 // Expression level syntax:
401 //   constant     ::= integer | real | string | set
402 //   set_elem     ::= expr | expr '..' expr
403 //   set          ::= '[' set_elem { ',' set_elem } ']'
404 //   expr_group   ::= '(' expr_list ')
405 //   expr_list    ::= expr { ',' expr } ')'
406 //   primary      ::= '(' expr ')' | literal | call_expr | ident
407 //                  | deref_expr | idx_expr | access_expr | set_constant
408 //   access_expr  ::= expr '.' ident
409 //   deref_expr   ::= expr '^'
410 //   idx_expr     ::= expr '[' expr_list ']'
411 //   call_expr    ::= expr [ expr_group ]
412 //   binary       ::= expresion op expr
413 //   unary        ::= op expr
414 //   expr         ::= primary | unary | binary
415 //   write_list   ::= '(' write_param { ',' write_param } ')'
416 //   write_param  ::= expr [ ':' constant [ ':' constant ] ]
417 //=============================================================================
418 
419 // Values for expression flags
420 enum {
421     tn_is_const         = 0x01,  // constant integer expression
422     tn_is_literal       = 0x02   // expression is literal (immediate constant)
423 };
424 
425 //-----------------------------------------------------------------------------
426 // Values for expression tags
427 
428 enum {
429     tn_add,         // addition
430     tn_sub,         // subtraction
431     tn_mod,         // modulo
432     tn_mul,         // multiplication
433     tn_div,         // division with truncation of the result
434     tn_divr,        // division with real result
435     tn_plus,        // unary plus
436     tn_minus,       // unary minus
437     tn_deref,       // dereferencing pointer
438     tn_filevar,     // dereferencing file variable
439     tn_address,     // dereference operation eaten by getting address (&*x)
440     tn_access,      // access to component of record
441     tn_ref,         // reference to variable: &v
442     tn_index,       // indiexation in array
443     tn_in,          // in set predicat
444     tn_eq,          // comparison operators
445     tn_ne,          //
446     tn_gt,          //
447     tn_ge,          //
448     tn_lt,          //
449     tn_le,          //
450     tn_let,         // =
451     tn_letadd,      // +=
452     tn_letsub,      // -=
453     tn_letmul,      // *=
454     tn_letdiv,      // /=
455     tn_letshl,      // <<=
456     tn_letshr,      // >>=
457     tn_letand,      // &=
458     tn_letor,       // |=
459     tn_and,         // logical(bit) AND
460     tn_shr,         // shift right
461     tn_shl,         // shift left
462     tn_not,         // logical(bit) NOT
463     tn_or,          // logical(bit) OR
464     tn_xor,         // bit eXclusive-OR
465     tn_atom,        // atom expresion (variable)
466     tn_self,        // atom expresion (variable)
467     tn_intnum,      // integer constant
468     tn_realnum,     // real constant
469     tn_string,      // string constant
470     tn_char,        // character constant
471     tn_set,         // set of elements
472     tn_group,       // group of expressions in braces
473     tn_fcall,       // function call
474     tn_loophole,    // type conversion
475     tn_skip,        // skipped optional positional parameter
476     tn_wrp,         // write parameter
477     tn_retarr,      // result of function is assigned to array
478     tn_case_range,  // case items range
479     tn_record_const // record constant
480 };
481 
482 class expr_node : public node {
483   public:
484     expr_node* next;
485 
486     char       tag;
487     char       parent_tag;
488     char       flags;
489     int        value; // value of constant expression
490     tpexpr*    type;
491 
is_const_literal()492     bool       is_const_literal() {
493 	return (flags & (tn_is_const|tn_is_literal)) == (tn_is_const|tn_is_literal);
494     }
495 
496     bool       is_parameter();
497 
498     expr_node(int expr_tag, int expr_flags = 0) {
499 	tag = expr_tag;
500 	parent_tag = tn_group;
501 	type = NULL;
502 	flags = expr_flags;
503 	next = NULL;
504     }
505 };
506 
507 //=============================================================================
508 
509 class atom_expr_node : public expr_node {
510   public:
511     token  *tkn;
512     symbol *var;
513 
514     symbol *with;  // variable stored result of 'with' expression
515     char   *temp;  // temporary variable for returning array
516 
517     atom_expr_node(token* tkn);
518 
519     virtual void attrib(int ctx);
520     virtual void translate(int ctx);
521 };
522 
523 class literal_node : public expr_node {
524   public:
525     token*  value_tkn;
526 
527     literal_node(token* value_tkn, int tag);
528 };
529 
530 class integer_node : public literal_node {
531   public:
532     int  radix;
533     integer_node(token* value_str);
534 
535     virtual void attrib(int ctx);
536     virtual void translate(int ctx);
537 };
538 
539 
540 class real_node : public literal_node {
541   public:
542     real_node(token* value_str);
543 
544     virtual void attrib(int ctx);
545     virtual void translate(int ctx);
546 };
547 
548 
549 class string_node : public literal_node {
550   public:
551     string_node(token* value_str);
552 
553     virtual void attrib(int ctx);
554     virtual void translate(int ctx);
555 };
556 
557 class address_node : public expr_node {
558     expr_node* var;
559     token*     t_adr;
560   public:
561     address_node(token* t_adr, expr_node* var);
562 
563     virtual void attrib(int ctx);
564     virtual void translate(int ctx);
565 };
566 
567 class case_range_node : public expr_node {
568   public:
569     expr_node* from;
570     token*     t_range;
571     expr_node* to;
572 
573     virtual void attrib(int ctx);
574     virtual void translate(int ctx);
575 
576     case_range_node(expr_node* from, token* t_range, expr_node* to);
577 };
578 
579 class set_item_node : public node {
580   public:
581     set_item_node* next;
582     tpexpr*        type;
583 
set_item_node()584     set_item_node() { next = NULL; }
585 };
586 
587 class set_elem_node : public set_item_node {
588   public:
589     expr_node*        item;
590 
591     set_elem_node(expr_node* item);
592 
593     virtual void attrib(int ctx);
594     virtual void translate(int ctx);
595 };
596 
597 class set_range_node : public set_item_node {
598   public:
599     expr_node*    low;
600     token*        dots; // '..'
601     expr_node*    high;
602 
603     set_range_node(expr_node* low, token* dots, expr_node* high);
604 
605     virtual void attrib(int ctx);
606     virtual void translate(int ctx);
607 };
608 
609 
610 class set_node : public expr_node {
611   public:
612     token*         t_lbr;    // '['
613     token*         t_rbr;    // ']'
614     set_item_node* items;
615 
616     set_node(token* t_lbr, set_item_node* items, token* t_rbr);
617 
618     virtual void attrib(int ctx);
619     virtual void translate(int ctx);
620 };
621 
622 class idx_expr_node : public expr_node {
623   public:
624     expr_node* arr;
625     token*     t_lbr; // '['
626     token*     t_rbr; // ']'
627     expr_node* indices;
628 
629     idx_expr_node(expr_node* arr, token* t_lbr, expr_node* indices,
630                   token* t_rbr);
631 
632     virtual void attrib(int ctx);
633     virtual void translate(int ctx);
634 };
635 
636 
637 class deref_expr_node : public expr_node {
638   public:
639     token*     op; // '^'
640     expr_node* ptr;
641 
642     deref_expr_node(expr_node* ptr, token* op);
643 
644     virtual void attrib(int ctx);
645     virtual void translate(int ctx);
646 };
647 
648 class access_expr_node : public expr_node {
649   public:
650     expr_node* rec;
651     token*     pnt; // '.'
652     token*     field;
653     symbol*    recfld;
654 
655     access_expr_node(expr_node* rec, token* pnt, token* field);
656 
657     virtual void attrib(int ctx);
658     virtual void translate(int ctx);
659 };
660 
661 class op_node : public expr_node {
662 public:
663     token*     op;
664     expr_node* left;
665     expr_node* right;
666     expr_node* parent;
667 
668     op_node(int tag, expr_node* left, token* op, expr_node* right = NULL);
669 
670     virtual void attrib(int ctx);
671     virtual void translate(int ctx);
672 };
673 
674 class fcall_node : public expr_node {
675   public:
676     expr_node       *fptr;
677     token           *lpar;
678     expr_node       *args;
679     token           *rpar;
680     char            *temp; // temporary variable used for returning array
681 
682     fcall_node(expr_node* fptr, token* lpar, expr_node* args,
683                token* rpar);
684 
685     virtual void attrib(int ctx);
686     virtual void translate(int ctx);
687 };
688 
689 class skipped_node : public expr_node {
690   public:
691     token* comma;
692 
693     skipped_node(token* comma);
694 
695     virtual void attrib(int ctx);
696     virtual void translate(int ctx);
697 };
698 
699 
700 class tpd_node;
701 
702 class loophole_node : public expr_node {
703   public:
704     token           *t_loophole;
705     token           *t_lpar;
706     tpd_node        *tpd;
707     token           *t_comma;
708     expr_node       *expr;
709     token           *t_rpar;
710 
711     loophole_node(token* t_loophole, token* t_lpar, tpd_node* tpd, token* t_comma,
712                   expr_node *expr, token* t_rpar);
713 
714     virtual void attrib(int ctx);
715     virtual void translate(int ctx);
716 };
717 
718 class expr_group_node : public expr_node {
719   public:
720     token          *lpar;
721     token          *rpar;
722     expr_node  	   *expr;
723     int             ctx;
724 
725     expr_group_node(token* lpar, expr_node* expr, token* rpar);
726 
727     virtual void attrib(int ctx);
728     virtual void translate(int ctx);
729 };
730 
731 class field_init_node : public node {
732   public:
733     field_init_node* next;
734     token*           t_field;
735     token*           t_coln;
736     expr_node*       value;
737 
738     field_init_node(token* t_field, token* t_coln, expr_node* value);
739 
740     void attrib(tpexpr* record_type);
741     void translate(int);
742 };
743 
744 class record_constant_node : public expr_node {
745   public:
746     token           *lpar;
747     token           *rpar;
748     field_init_node *flist;
749 
750     record_constant_node(token* lpar, field_init_node* flist, token* rpar);
751 
752     virtual void attrib(int ctx);
753     virtual void translate(int ctx);
754 };
755 
756 
757 class write_param_node : public expr_node {
758   public:
759     expr_node*      val;
760     token*          t_coln1;
761     expr_node*      width;
762     token*          t_coln2;
763     expr_node*      prec;
764 
765     write_param_node(expr_node* val,
766 		     token* t_coln1 = NULL, expr_node* width = NULL,
767 		     token* t_coln2 = NULL, expr_node* prec = NULL);
768 
769     virtual void attrib(int ctx);
770     virtual void translate(int ctx);
771 };
772 
773 
774 class write_list_node : public node {
775   public:
776     token            *lpar;
777     token            *rpar;
778     write_param_node *vals;
779 
780     write_list_node(token* lpar, write_param_node* vals, token* rpar);
781 
782     virtual void attrib(int ctx);
783     virtual void translate(int ctx);
784 };
785 
786 
787 //=============================================================================
788 // Declaration syntax:
789 //   label_decl_part  ::= [ LABEL ident { ',' ident } ';' ]
790 //   const_def_part   ::= [ CONST const_def ';' { const_def ';' } ]
791 //   type_def_part    ::= [ TYPE type_def ';' { type_def ';' } ]
792 //   var_decl_part    ::= [ VAR var_decls ';' ]
793 //   proc_fwd_decl    ::= proc_decl ';' ident ';'
794 //   proc_decl        ::= (PROCEDURE | FUNCTION) ident [ formal_params ] [ ':' type ]
795 //   proc_def         ::= proc_decl ';' body ';'
796 //   formal_params    ::= '(' formal_param { ';' formal_param } ')'
797 //   formal_param     ::= var_decl | proc_decl
798 //   const_def        ::= ident '=' expr
799 //   type_def         ::= ident '=' type
800 //   var_decls        ::= var_decl { ';' var_decl }
801 //   var_decl         ::= ident { ',' ident } ':' type
802 //   type             ::= simple_type | array_type | record_type | set_type |
803 //                        file_type | pointer_type | subrange_type | enum_type
804 //   subrange_type    ::= '[' expr '..' expr ']'
805 //   enum_type        ::= '(' expr { ',' expr } ')'
806 //   pointer_type     ::= '^' type
807 //   file_type        ::= FILE OF type_denoter
808 //   set_type         ::= SET OF type_denoter
809 //   record_type      ::= RECORD field_list END
810 //   field_list       ::= [ (fixed_part [';' variant_part] | variant_part) [;] ]
811 //   fixed_part       ::= var_decls
812 //   variant_part     ::= CASE selector OF
813 //                        variant { ';' variant }
814 //   selector         ::= [ tag_field ':' ] tag_type
815 //   variant          ::= constant { ',' constant } ':' '(' field_list ')'
816 //   simple_type      ::= ident
817 //   array_type       ::= ARRAY '[' index ']' OF type
818 //   index            ::= conformant_index | fixed_index
819 //   conformant_index ::= var_decls
820 //   fixed_index      ::= range { ',' range }
821 //   range            ::= expr '..' expr | type
822 //=============================================================================
823 
824 
825 class decl_node : public node {
826   public:
827     decl_node* next;
828 
829     enum {
830 	is_toplevel = 1, // The declaration is at the top level of module
831 	is_argument = 2, // The declaration is in the argument list of
832 			 // procedure
833 	is_static   = 4, // keyword 'static' must be prepended before decl
834 	is_extern   = 8,
835 	is_member   = 16
836     };
837     int	      attr;
838 
decl_node()839     decl_node() { next = NULL; }
840 };
841 
842 
843 
844 //
845 // labels declaration
846 //
847 
848 class label_decl_part_node : public decl_node {
849   public:
850     token*      t_label;
851     token*      t_semi;           // ';'
852     token_list* labels;
853 
854     label_decl_part_node(token* t_label, token_list* label_list,
855                          token* t_semi);
856 
857     virtual void attrib(int ctx);
858     virtual void translate(int ctx);
859 };
860 
861 //
862 // Constant definition
863 //
864 
865 
866 class const_def_node : public decl_node {
867   public:
868     token*           ident;
869     token*           equal;
870     expr_node*       constant;
871     symbol*          sym;
872     static const_def_node* enumeration;
873 
874     const_def_node(token* ident, token* equal, expr_node* value);
875 
876     virtual void attrib(int ctx);
877     virtual void translate(int ctx);
878 };
879 
880 
881 class typed_const_def_node : public const_def_node {
882   public:
883     token*           coln;
884     tpd_node*        tpd;
885 
886     typed_const_def_node(token* ident, token* coln, tpd_node* tpd,
887 			 token* equal, expr_node* constant);
888 
889     virtual void attrib(int ctx);
890     virtual void translate(int ctx);
891 };
892 
893 class const_def_part_node : public decl_node {
894   public:
895     token*           t_const;
896     const_def_node*  list;
897 
898     const_def_part_node(token* t_const, const_def_node* list);
899 
900     virtual void attrib(int ctx);
901     virtual void translate(int ctx);
902 };
903 
904 
905 //
906 // Type definition
907 //
908 
909 
910 class type_def_node : public decl_node {
911   public:
912     token*           ident;
913     token*           equal;
914     tpd_node*        tpd;
915     symbol*          sym;
916 
917     type_def_node(token* ident, token* equal, tpd_node* tpd);
918 
919     virtual void attrib(int ctx);
920     virtual void translate(int ctx);
921 };
922 
923 
924 class type_def_part_node : public decl_node {
925 public:
926     token*          t_type;
927     type_def_node*  types;
928 
929     type_def_part_node(token* t_type, type_def_node* list);
930 
931     virtual void attrib(int ctx);
932     virtual void translate(int ctx);
933 };
934 
935 //
936 // Variable declaration
937 //
938 
939 
940 class var_decl_node : public decl_node {
941   public:
942     token_list*      vars;
943     token*           coln;   // ':'
944     tpd_node*        tpd;
945     token*           scope;
946 
947     var_decl_node(token_list* vars, token* coln, tpd_node* tpd);
948 
949     virtual void attrib(int ctx);
950     virtual void translate(int ctx);
951 };
952 
953 
954 class var_decl_part_node : public decl_node {
955   public:
956     token*           t_var;
957     var_decl_node*   vars;
958 
959     var_decl_part_node(token* t_var, var_decl_node* vars);
960 
961     virtual void attrib(int ctx);
962     virtual void translate(int ctx);
963 };
964 
965 
966 class var_origin_decl_node : public decl_node {
967   public:
968     token     *t_ident;
969     token     *t_origin;
970     expr_node *addr;
971     token     *t_colon;
972     tpd_node  *tpd;
973 
974     symbol    *sym;
975     tpexpr    *type;
976 
977     var_origin_decl_node(token* t_ident, token* t_origin,
978                          expr_node *addr, token* t_colon, tpd_node *tpd);
979 
980     virtual void attrib(int ctx);
981     virtual void translate(int ctx);
982 };
983 
984 
985 
986 //
987 // Procedures and functions declarations and definition
988 //
989 
990 
991 class param_list_node : public node {
992   public:
993     token       *lpar, *rpar;
994     decl_node   *params;
995 
996     param_list_node(token* lpar, decl_node* params, token* rpar);
997 
998     virtual void attrib(int ctx);
999     virtual void translate(int ctx);
1000 };
1001 
1002 class unit_spec_node : public decl_node {
1003   public:
1004     token* t_unit;
1005     token* t_name;
1006     token* t_semi;
1007     token* t_interface;
1008 
1009     decl_node* decls;
1010 
1011     unit_spec_node(token* t_unit, token* t_name, token* t_semi,
1012 		   token* t_interface, decl_node* decls);
1013 
1014     virtual void attrib(int ctx);
1015     virtual void translate(int ctx);
1016 };
1017 
1018 class proc_decl_node : public decl_node {
1019   protected:
1020     void                insert_return_type();
1021     void                insert_params();
1022 
1023   public:
1024     token*              t_proc;
1025     token*              t_ident;
1026     param_list_node*    params;
1027     token*              t_coln;
1028     tpd_node*           ret_type;
1029     symbol*             var;
1030     proc_tp*            type;
1031 
1032     proc_decl_node(token* t_proc, token* t_ident,  param_list_node* params,
1033                    token* t_coln = NULL, tpd_node* ret_type = NULL);
1034 
1035     virtual void attrib(int ctx);
1036     virtual void translate(int ctx);
1037 };
1038 
1039 class proc_fwd_decl_node : public proc_decl_node {
1040   public:
1041     token*              t_semi1;
1042     token_list*         qualifiers;
1043     token*              t_semi2;
1044     bool                is_external;
1045     bool                is_static;
1046     bool                is_virtual;
1047 
1048     proc_fwd_decl_node(token* t_proc, token* t_ident, param_list_node* params,
1049 		       token* t_coln, tpd_node* ret_type,
1050                        token* t_semi1, token_list* qualifiers = NULL,
1051 		       token* t_semi2 = NULL);
1052 
1053     virtual void attrib(int ctx);
1054     virtual void translate(int ctx);
1055 };
1056 
1057 
1058 class proc_def_node : public proc_decl_node {
1059   public:
1060     token*              t_class;
1061     token*              t_dot;
1062     token*              t_semi1;
1063     block_node*         block;
1064     token*              t_semi2;
1065     token*              t_attrib;
1066     token*              t_semi3;
1067     bool                use_forward;
1068 
1069     static object_tp*   self;
1070     symbol*             s_self;
1071 
1072     proc_def_node(token* t_proc, token* t_class, token* t_dot, token* t_ident, param_list_node* params,
1073 		  token* t_coln, tpd_node* ret_type,
1074 		  token* t_semi1, token* t_attrib, token* t_semi2, block_node* block, token* t_semi3);
1075 
1076     virtual void attrib(int ctx);
1077     virtual void translate(int ctx);
1078 };
1079 
1080 
1081 //
1082 // Type definitions
1083 //
1084 
1085 class tpd_node : public node {
1086   public:
1087     tpexpr*     type; /* Type which is designated by this typenode */
1088 
1089     enum { tpd_simple, tpd_set, tpd_enum, tpd_range, tpd_proc,
1090            tpd_array, tpd_string, tpd_ref, tpd_object, tpd_record, tpd_file };
1091     int         tag;
1092 
tpd_node(int tpd_tag)1093     tpd_node(int tpd_tag) { type = NULL; tag = tpd_tag; }
1094 };
1095 
1096 
1097 
1098 class simple_tpd_node: public tpd_node {
1099   public:
1100     token       *tkn;
1101     symbol      *sym;
1102 
1103     simple_tpd_node(token* tkn);
1104 
1105     virtual void attrib(int ctx);
1106     virtual void translate(int ctx);
1107 };
1108 
1109 //
1110 // Function (procedure) pointer type defineition
1111 //
1112 
1113 class fptr_tpd_node : public tpd_node {
1114   public:
1115     token*           t_proc;
1116     token*           t_coln;
1117     token*           t_params;
1118     param_list_node* params;
1119     tpd_node*        ret_type;
1120 
1121     fptr_tpd_node(token* t_proc, param_list_node* params,
1122 		  token* t_coln = NULL, tpd_node* ret_type = NULL);
1123 
1124     virtual void attrib(int ctx);
1125     virtual void translate(int ctx);
1126 };
1127 
1128 //
1129 // Enumeration type defineition
1130 //
1131 
1132 class enum_tpd_node : public tpd_node {
1133   public:
1134     token*      lpar;
1135     token*      rpar;
1136     token_list* items;
1137 
1138     enum_tpd_node(token* lpar, token_list* items, token* rpar);
1139 
1140     virtual void attrib(int ctx);
1141     virtual void translate(int ctx);
1142 };
1143 
1144 //
1145 // Subrange type definition
1146 //
1147 
1148 class range_tpd_node: public tpd_node {
1149   public:
1150     expr_node*  low;
1151     expr_node*  high;
1152     token*      dots; // '..'
1153 
1154     char*         ctype;
1155 
1156     range_tpd_node(expr_node* low, token* dots, expr_node* high);
1157 
1158     virtual void attrib(int ctx);
1159     virtual void translate(int ctx);
1160 };
1161 
1162 //
1163 // Array type defineition
1164 //
1165 
1166 class idx_node : public node {
1167   public:
1168     idx_node*      next;
idx_node()1169     idx_node() { next = NULL; }
1170 };
1171 
1172 class type_index_node : public idx_node {
1173   public:
1174     tpexpr*    type;
1175     tpd_node*  tpd;
1176 
1177     type_index_node(tpd_node* tpd);
1178 
1179     virtual void attrib(int ctx);
1180     virtual void translate(int ctx);
1181 };
1182 
1183 
1184 class range_index_node : public idx_node {
1185   public:
1186     expr_node*   low;
1187     token*       dots;
1188     expr_node*   high;
1189 
1190     range_index_node(expr_node *low, token* dots, expr_node* high);
1191 
1192     virtual void attrib(int ctx);
1193     virtual void translate(int ctx);
1194 };
1195 
1196 
1197 class conformant_index_node : public idx_node {
1198   public:
1199     token    *low;
1200     token    *dots;
1201     token    *high;
1202     token    *coln;
1203     tpd_node *tpd;
1204 
1205     conformant_index_node(token *low, token *dots, token *high, token *coln,
1206                           tpd_node* tpd);
1207 
1208     virtual void attrib(int ctx);
1209     virtual void  translate(int ctx);
1210 };
1211 
1212 
1213 class array_tpd_node: public tpd_node {
1214   public:
1215     token*      t_packed;          // 'Packed' (if any)
1216     token*      t_array;	   // 'Array'
1217     token*      t_lbr;             // '['
1218     token*      t_rbr;             // ']'
1219     token*      t_of;              // 'of'
1220     idx_node*   indices;	   // indices (possibly with comma)
1221     tpd_node*   eltd;		   // Declaration of element type
1222 
1223     void set_indices_attrib(idx_node* idx);
1224 
1225     array_tpd_node(token* t_packed, token *t_array,
1226                    token* t_lbr, idx_node *indices,
1227                    token* t_rbr, token* t_of, tpd_node *eltd);
1228 
1229     virtual void attrib(int ctx);
1230     virtual void translate(int ctx);
1231 };
1232 
1233 //
1234 // Turbo Pascal string types
1235 //
1236 
1237 class varying_tpd_node: public tpd_node {
1238   public:
1239     token*      t_string;	   // 'String'
1240     token*      t_lbr;             // '['
1241     expr_node*  size; 	           // size of string
1242     token*      t_rbr;             // ']'
1243 
1244     varying_tpd_node(token *t_string, token* t_lbr, expr_node *size,
1245 		     token* t_rbr);
1246 
1247     virtual void attrib(int ctx);
1248     virtual void translate(int ctx);
1249 };
1250 
1251 class string_tpd_node: public tpd_node {
1252   public:
1253     token*      t_string;	   // 'String'
1254 
1255     string_tpd_node(token *t_string);
1256 
1257     virtual void attrib(int ctx);
1258     virtual void translate(int ctx);
1259 };
1260 
1261 //
1262 // Pointer type definition
1263 //
1264 
1265 class ptr_tpd_node: public tpd_node {
1266   public:
1267     token*      tkn_ref; // '^'
1268     tpd_node*   tpd;
1269 
1270     ptr_tpd_node(token* tkn_ref, tpd_node* tpd);
1271 
1272     virtual void attrib(int ctx);
1273     virtual void translate(int ctx);
1274 };
1275 
1276 //
1277 // Record type definition
1278 //
1279 
1280 class field_list_node;
1281 
1282 class variant_node : public node {
1283   public:
1284     static int          number;
1285 
1286     variant_node*       next;
1287 
1288     expr_node*          tag_list;
1289     token*              t_coln; // ':'
1290     token*              t_lpar; // '('
1291     token*              t_rpar; // ')'
1292     char*               struct_name;
1293     field_list_node*    fields;
1294 
1295     variant_node(expr_node* tag_list, token* t_coln,
1296                  token* t_lpar, field_list_node* fields, token* t_rpar);
1297 
1298     virtual void attrib(int ctx);
1299     virtual void translate(int ctx);
1300 };
1301 
1302 class selector_node : public node {
1303   public:
1304     token*           tag_field;
1305     token*           coln;
1306     tpd_node*        tag_type;
1307     symbol*          var;
1308 
1309     selector_node(token* tag_field, token* coln, tpd_node* tag_type);
1310 };
1311 
1312 
1313 class variant_part_node : public node {
1314   public:
1315     token*           t_case;
1316     selector_node*   selector;
1317     token*           t_of;
1318     variant_node*    variants;
1319 
1320     variant_part_node(token* t_case, selector_node* selector, token* t_of,
1321 		      variant_node* variants);
1322 
1323     virtual void attrib(int ctx);
1324     virtual void translate(int ctx);
1325 };
1326 
1327 class field_list_node : public node {
1328   public:
1329     var_decl_node*     fix_part;
1330     variant_part_node* var_part;
1331     int                ctx; // union / record
1332 
1333     bool               is_single(); // list has single declaration
1334 
1335     field_list_node(var_decl_node* fix_part,
1336                     variant_part_node* var_part = NULL);
1337 
1338     virtual void attrib(int ctx);
1339     virtual void translate(int ctx);
1340 };
1341 
1342 
1343 class record_tpd_node : public tpd_node {
1344   public:
1345     token*           t_packed;
1346     token*           t_record;
1347     token*           t_end;
1348     field_list_node* fields;
1349     record_tpd_node* outer;
1350 
1351     record_tpd_node(token* t_packed, token* t_record,
1352                     field_list_node* fields, token* t_end);
1353 
1354     void assign_name();
1355 
1356     virtual void attrib(int ctx);
1357     virtual void translate(int ctx);
1358 };
1359 
1360 //
1361 // Borland Pascal object
1362 //
1363 
1364 class object_tpd_node : public tpd_node {
1365   public:
1366     token*           t_object;
1367     token*           t_lbr;
1368     token*           t_superclass;
1369     token*           t_rbr;
1370     token*           t_end;
1371     decl_node*       fields;
1372 
1373     symbol*          super;
1374 
1375     object_tpd_node(token* t_object, token* t_lbr, token* t_superclass, token* t_rbr,
1376                     decl_node* fields, token* t_end);
1377 
1378     virtual void attrib(int ctx);
1379     virtual void translate(int ctx);
1380 };
1381 
1382 
1383 //
1384 // File type definition
1385 //
1386 
1387 
1388 class file_tpd_node : public tpd_node {
1389   public:
1390     token*           t_packed;
1391     token*           t_file;
1392     token*           t_of;
1393     tpd_node*        recordtp;
1394 
1395     file_tpd_node(token* t_packed, token* t_file, token* t_of, tpd_node* recordtp);
1396 
1397     virtual void attrib(int ctx);
1398     virtual void translate(int ctx);
1399 };
1400 
1401 //
1402 // Set type definition
1403 //
1404 
1405 class set_tpd_node : public tpd_node {
1406   public:
1407     token*           t_packed;
1408     token*           t_set;
1409     token*           t_of;
1410     tpd_node*        elemtp;
1411 
1412     set_tpd_node(token* t_packed, token* t_set, token* t_of, tpd_node* elemtp);
1413 
1414     virtual void attrib(int ctx);
1415     virtual void translate(int ctx);
1416 };
1417 
1418 
1419 #endif
1420 
1421