Debian Codegraph @

terps/tads/tads3/tcpndrv.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/TCPNDRV.H,v 1.4 1999/07/11 00:46:53 MJRoberts Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 tcpndrv.h - parse node base classes derived from CTcPrsNode
12 Function
13 This file contains the intermediate classes derived from CTcPrsNode.
14
15 CTcPrsNode is defined separately for each target code generator.
16 These classes are separated into this header file so that they can
17 be defined after the target-specific CTcPrsNode header has been
18 included, which in turn must be included after the base parse node
19 file, tcpnbase.h. Finally, after this file, the target-specific
20 derived classes header can be included.
21
22 The sequence is thus:
23
24 tcpnbase.h - target-independent CTcPrsNodeBase
25 ???.h - target-specific CTcPrsNode definition (file varies by target)
26 tcpnint.h - target-independent intermediate classes
27 ???.h - target-specific intermediate classes
28 tcpndrv.h - target-independent derived CTPNXxxBase classes
29 ???.h - target-specific derived CTPNXxx final classes
30 Notes
31
32 Modified
33 05/10/99 MJRoberts - Creation
34 */
35
36 #ifndef TCPNDRV_H
37 #define TCPNDRV_H
38
39 /* ------------------------------------------------------------------------ */
40 /*
41 * "argcount" node
42 */
43 class CTPNArgcBase: public CTcPrsNode
44 {
45 public:
46 /* fold constants */
47 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
48 {
49 /* simply return myself unchanged */
50 return this;
51 }
52
53 /* adjust for dynamic (run-time) compilation */
54 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
55 { return this; }
56 };
57
58 /* ------------------------------------------------------------------------ */
59 /*
60 * "self" node
61 */
62 class CTPNSelfBase: public CTcPrsNode
63 {
64 public:
65 /* fold constants */
66 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
67 {
68 /* simply return myself unchanged */
69 return this;
70 }
71
72 /* adjust for dynamic (run-time) compilation */
73 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
74 { return this; }
75 };
76
77 /* ------------------------------------------------------------------------ */
78 /*
79 * "targetprop" node
80 */
81 class CTPNTargetpropBase: public CTcPrsNode
82 {
83 public:
84 /* fold constants */
85 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
86 {
87 /* simply return myself unchanged */
88 return this;
89 }
90
91 /* adjust for dynamic (run-time) compilation */
92 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
93 { return this; }
94 };
95
96
97 /* ------------------------------------------------------------------------ */
98 /*
99 * "targetobj" node
100 */
101 class CTPNTargetobjBase: public CTcPrsNode
102 {
103 public:
104 /* fold constants */
105 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
106 {
107 /* simply return myself unchanged */
108 return this;
109 }
110
111 /* adjust for dynamic (run-time) compilation */
112 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
113 { return this; }
114 };
115
116
117 /* ------------------------------------------------------------------------ */
118 /*
119 * "definingobj" node
120 */
121 class CTPNDefiningobjBase: public CTcPrsNode
122 {
123 public:
124 /* fold constants */
125 class CTcPrsNode *fold_constants(class CTcPrsSymtab *)
126 {
127 /* simply return myself unchanged */
128 return this;
129 }
130
131 /* adjust for dynamic (run-time) compilation */
132 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
133 { return this; }
134 };
135
136 /* ------------------------------------------------------------------------ */
137 /*
138 * "invokee" node
139 */
140 class CTPNInvokeeBase: public CTcPrsNode
141 {
142 public:
143 /* fold constants */
144 class CTcPrsNode *fold_constants(class CTcPrsSymtab *) { return this; }
145
146 /* adjust for dynamic (run-time) compilation */
147 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
148 { return this; }
149 };
150
151
152 /* ------------------------------------------------------------------------ */
153 /*
154 * "inherited" node
155 */
156 class CTPNInhBase: public CTcPrsNode
157 {
158 public:
159 CTPNInhBase()
160 {
161 typelist_ = 0;
162 }
163
164 /* fold constants */
165 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
166 {
167 /* simply return myself unchanged */
168 return this;
169 }
170
171 /* adjust for dynamic (run-time) compilation */
172 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
173 { return this; }
174
175 /* get/set the formal type list */
176 class CTcFormalTypeList *get_typelist() const { return typelist_; }
177 void set_typelist(class CTcFormalTypeList *l) { typelist_ = l; }
178
179 protected:
180 class CTcFormalTypeList *typelist_;
181 };
182
183 /*
184 * "inherited class" node - for (inherited superclass.method) construct
185 */
186 class CTPNInhClassBase: public CTcPrsNode
187 {
188 public:
189 CTPNInhClassBase(const char *sym, size_t len)
190 {
191 sym_ = sym;
192 len_ = len;
193 }
194
195 /* fold constants */
196 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
197 {
198 /* simply return myself unchanged */
199 return this;
200 }
201
202 /* adjust for dynamic (run-time) compilation */
203 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
204 { return this; }
205
206 protected:
207 /* the superclass name symbol and its length */
208 const char *sym_;
209 size_t len_;
210 };
211
212
213 /*
214 * "delegated" node - for (delegated expr.method) construct
215 */
216 class CTPNDelegatedBase: public CTcPrsNode
217 {
218 public:
219 CTPNDelegatedBase(CTcPrsNode *delegatee)
220 {
221 delegatee_ = delegatee;
222 }
223
224 /* fold constants */
225 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
226 {
227 /* fold constants in the delegatee */
228 delegatee_ = delegatee_->fold_constants(symtab);
229
230 /* return myself otherwise unchanged */
231 return this;
232 }
233
234 /* adjust for dynamic (run-time) compilation */
235 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
236 {
237 /* adjust the delegatee subexpression */
238 delegatee_ = delegatee_->adjust_for_dyn(info);
239
240 /* return myself otherwise unchanged */
241 return this;
242 }
243
244 protected:
245 /* the delegatee (the object to which we delegate the method) */
246 CTcPrsNode *delegatee_;
247 };
248
249 /*
250 * "replaced" node
251 */
252 class CTPNReplacedBase: public CTcPrsNode
253 {
254 public:
255 /* fold constants */
256 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
257 {
258 /* simply return myself unchanged */
259 return this;
260 }
261
262 /* adjust for dynamic (run-time) compilation */
263 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
264 { return this; }
265 };
266
267
268 /* ------------------------------------------------------------------------ */
269 /*
270 * Parse node for constant values (numbers, strings)
271 */
272 class CTPNConstBase: public CTcPrsNode
273 {
274 public:
275 CTPNConstBase(const CTcConstVal *val) { val_ = *val; }
276
277 /* get the constant value of the node */
278 virtual class CTcConstVal *get_const_val() { return &val_; }
279
280 /* fold constants */
281 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
282 {
283 /* simply return myself unchanged */
284 return this;
285 }
286
287 /* adjust for dynamic (run-time) compilation */
288 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
289
290 /* check to see if this is a boolean value */
291 virtual int is_bool() const { return val_.is_bool(); }
292
293 protected:
294 CTcConstVal val_;
295 };
296
297 /*
298 * The target must define a subclass, CTPNDebugConst, for use when
299 * evaluating debugger expressions.
300 */
301 /* class CTPNDebugConst: public CTPNConst ... */
302
303 /* ------------------------------------------------------------------------ */
304 /*
305 * Address-of Operator
306 */
307 class CTPNAddrBase: public CTPNUnary
308 {
309 public:
310 CTPNAddrBase(CTcPrsNode *sub)
311 : CTPNUnary(sub) { }
312
313 /* this is an address expression */
314 virtual int is_addr() const { return TRUE; }
315
316 /*
317 * Determine if this address expression is the same as a given
318 * address expression. Returns true if so, false if not. Sets
319 * (*comparable) to true if the comparison is meaningful, false if
320 * not; if (*comparable) is set to false, the caller should not
321 * attempt to perform a compile-time comparison of the addresses,
322 * and must defer the comparison to run-time.
323 */
324 int is_addr_eq(const class CTPNAddr *, int *comparable) const;
325
326 /* adjust for dynamic (run-time) compilation */
327 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
328 { return this; }
329
330 /* fold constants */
331 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
332 };
333
334 /* ------------------------------------------------------------------------ */
335 /*
336 * Symbol node. This node handles symbols that are not resolved at
337 * parse time, and whose resolution must be deferred to code generation.
338 * Any symbol that doesn't resolve at parse time to something in local
339 * scope uses this type of node.
340 */
341 class CTPNSymBase: public CTcPrsNode
342 {
343 public:
344 CTPNSymBase(const char *sym, size_t len)
345 {
346 /* remember my symbol name */
347 sym_ = sym;
348 len_ = len;
349 }
350
351 /*
352 * Assume that a simple symbol primary can be used as an lvalue.
353 * Some symbols are not suitable as lvalues, but we can't know this
354 * at parse time, so assume that an lvalue is okay for now.
355 */
356 virtual int check_lvalue() const { return TRUE; }
357
358 /* check to see if this is an lvalue, resolving in the given scope */
359 virtual int check_lvalue_resolved(class CTcPrsSymtab *symtab) const;
360
361 /*
362 * At parse time, assume that we can take the address of a simple
363 * symbol primary.
364 */
365 virtual int has_addr() const { return TRUE; }
366
367 /* determine if I have a return value when called */
368 virtual int has_return_value_on_call() const;
369
370 /* get my symbol information */
371 const char *get_sym_text() const { return sym_; }
372 size_t get_sym_text_len() const { return len_; }
373 int sym_text_matches(const char *sym, size_t len) const
374 { return (len_ == len && memcmp(sym, sym_, len) == 0); }
375
376 /* fold constants */
377 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
378
379 /* generate a constant node for my address value */
380 class CTcPrsNode *fold_addr_const(class CTcPrsSymtab *symtab);
381
382 /* adjust for dynamic (run-time) compilation */
383 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *);
384
385 protected:
386 /* my symbol */
387 const char *sym_;
388 size_t len_;
389 };
390
391 /* ------------------------------------------------------------------------ */
392 /*
393 * Resolved symbol node. This node handles symbols that are resolve
394 * during parsing. All local scope symbols, by the nature of the
395 * language, are resolvable during parsing, because local scope symbols
396 * must be declared before their first use.
397 */
398 class CTPNSymResolvedBase: public CTcPrsNode
399 {
400 public:
401 CTPNSymResolvedBase(CTcSymbol *sym)
402 {
403 /* remember my symbol table entry */
404 sym_ = sym;
405 }
406
407 /* get my symbol information */
408 const char *get_sym_text() const { return sym_->get_sym(); }
409 size_t get_sym_text_len() const { return sym_->get_sym_len(); }
410 int sym_text_matches(const char *sym, size_t len) const
411 {
412 return (sym_->get_sym_len() == len
413 && memcmp(sym, sym_->get_sym(), len) == 0);
414 }
415
416 /* determine if I can be an lvalue */
417 virtual int check_lvalue() const { return sym_->check_lvalue(); }
418
419 /*
420 * check to see if this is an lvalue, resolving in the given scope -
421 * this type of symbol is pre-resolved, so we can ignore the symbol
422 * table and just ask our resoloved symbol directly
423 */
424 virtual int check_lvalue_resolved(class CTcPrsSymtab *) const
425 { return sym_->check_lvalue(); }
426
427 /* determine if I have an address */
428 virtual int has_addr() const { return sym_->has_addr(); }
429
430 /* determine if I have a return value when called as a function */
431 virtual int has_return_value_on_call() const
432 { return sym_->has_return_value_on_call(); }
433
434 /* fold constants */
435 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
436
437 /* generate a constant node for my address value */
438 class CTcPrsNode *fold_addr_const(class CTcPrsSymtab *symtab);
439
440 /* adjust for dynamic (run-time) compilation */
441 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
442 { return this; }
443
444 protected:
445 /* the symbol table entry for this symbol */
446 class CTcSymbol *sym_;
447 };
448
449 /* ------------------------------------------------------------------------ */
450 /*
451 * Debugger local variable resolved symbol
452 */
453 class CTPNSymDebugLocalBase: public CTcPrsNode
454 {
455 public:
456 CTPNSymDebugLocalBase(const struct tcprsdbg_sym_info *info);
457
458 /* locals can always be lvalues */
459 virtual int check_lvalue() const { return TRUE; }
460 virtual int check_lvalue_resolved(class CTcPrsSymtab *) const
461 { return TRUE; }
462
463 /* determine if I have an address */
464 virtual int has_addr() const { return FALSE; }
465
466 /*
467 * Determine if I have a return value when called as a function. We
468 * have to assume that we do, since we can't know in advance what the
469 * variable will hold at run-time. If it holds a function with a
470 * return value, we'll need to return the value; if it doesn't, the
471 * function will effectively return nil, so returning the non-result
472 * will do no harm.
473 */
474 virtual int has_return_value_on_call() const { return TRUE; }
475
476 /* fold constants */
477 class CTcPrsNode *fold_constants(class CTcPrsSymtab *) { return this; }
478
479 /* adjust for dynamic (run-time) compilation */
480 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
481 { return this; }
482
483 protected:
484 /* local variable ID */
485 uint var_id_;
486
487 /* context array index for this variable, if I'm in a context */
488 int ctx_arr_idx_;
489
490 /* stack frame index */
491 uint frame_idx_;
492
493 /* is this a parameter? (if not, it's a local) */
494 uint is_param_ : 1;
495
496 };
497
498 /* ------------------------------------------------------------------------ */
499 /*
500 * Argument List
501 */
502 class CTPNArglistBase: public CTcPrsNode
503 {
504 public:
505 CTPNArglistBase(int argc, class CTPNArg *list_head)
506 {
507 /* remember the argument count and list head */
508 argc_ = argc;
509 list_ = list_head;
510 }
511
512 /* get/set the argument count */
513 int get_argc() const { return argc_; }
514 void set_argc(int argc) { argc_ = argc; }
515
516 /*
517 * Get the head argument of the list. The other arguments can be
518 * obtained by following the linked list from this one. Note that
519 * we build some argument lists in reverse order, with the last
520 * argument at the head of the list.
521 */
522 class CTPNArg *get_arg_list_head() const { return list_; }
523
524 /* set the argument list head */
525 void set_arg_list_head(class CTPNArg *head) { list_ = head; }
526
527 /* fold constants */
528 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
529
530 /* adjust for dynamic (run-time) compilation */
531 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
532
533 protected:
534 /*
535 * argument count (this is simply the number of entries in the list,
536 * but we keep it separately for easy access)
537 */
538 int argc_;
539
540 /* head of the argument list */
541 class CTPNArg *list_;
542 };
543
544
545 /* ------------------------------------------------------------------------ */
546 /*
547 * Argument list entry. Each list entry has the expression for the
548 * argument and a pointer to the next element of the argument list.
549 */
550 class CTPNArgBase: public CTcPrsNode
551 {
552 public:
553 CTPNArgBase(CTcPrsNode *arg_expr)
554 {
555 /* remember the expression for this argument */
556 arg_expr_ = arg_expr;
557
558 /* there's nothing else in our list yet */
559 next_arg_ = 0;
560
561 /* assume it's not a list-to-varargs conversion */
562 is_varargs_ = FALSE;
563
564 /* presume no argument name */
565 name_.settyp(TOKT_INVALID);
566 }
567
568 /* set the parameter name, for a "name: value" argument */
569 void set_name(const class CTcToken *tok);
570
571 /* get the name and its length */
572 const char *get_name() const { return name_.get_text(); }
573 const size_t get_name_len() const { return name_.get_text_len(); }
574
575 /* is this a named parameter? */
576 int is_named_param() { return name_.gettyp() != TOKT_INVALID; }
577
578 /* get the argument expression */
579 CTcPrsNode *get_arg_expr() const { return arg_expr_; }
580
581 /* get the next argument in my list */
582 class CTPNArg *get_next_arg() const { return next_arg_; }
583
584 /* set the next element in my list */
585 void set_next_arg(class CTPNArg *prv) { next_arg_ = prv; }
586
587 /* fold constants */
588 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
589
590 /* adjust for dynamic (run-time) compilation */
591 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
592 {
593 /* adjust the expression */
594 arg_expr_ = arg_expr_->adjust_for_dyn(info);
595
596 /* return myself otherwise unchanged */
597 return this;
598 }
599
600 /* get/set list-to-varargs conversion flag */
601 int is_varargs() const { return is_varargs_; }
602 void set_varargs(int f) { is_varargs_ = (f != 0); }
603
604 protected:
605 /* argument expression */
606 CTcPrsNode *arg_expr_;
607
608 /* next argument in the list */
609 class CTPNArg *next_arg_;
610
611 /* the parameter name, for a "name: expression" argument */
612 CTcToken name_;
613
614 /* flag: this is a list-to-varargs parameter */
615 unsigned int is_varargs_ : 1;
616 };
617
618
619
620 /* ------------------------------------------------------------------------ */
621 /*
622 * Function/Method Call node
623 */
624 class CTPNCallBase: public CTcPrsNode
625 {
626 public:
627 CTPNCallBase(CTcPrsNode *func, class CTPNArglist *arglist)
628 {
629 /* remember the expression giving the function or method to call */
630 func_ = func;
631
632 /* remember the argument list */
633 arglist_ = arglist;
634 }
635
636 /* get the function to call */
637 CTcPrsNode *get_func() const { return func_; }
638
639 /* get the argument list */
640 class CTPNArglist *get_arg_list() const { return arglist_; }
641
642 /* fold constants */
643 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
644
645 /* we have a return value if our function has a return value */
646 virtual int has_return_value() const
647 { return func_->has_return_value_on_call(); }
648
649 /* adjust for dynamic (run-time) compilation */
650 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
651
652 protected:
653 /* expression giving function or method to call */
654 CTcPrsNode *func_;
655
656 /* argument list */
657 class CTPNArglist *arglist_;
658 };
659
660
661 /* ------------------------------------------------------------------------ */
662 /*
663 * double-quoted string node
664 */
665 class CTPNDstrBase: public CTcPrsNode
666 {
667 public:
668 CTPNDstrBase(const char *str, size_t len);
669
670 /* get my string information */
671 const char *get_str() const { return str_; }
672 size_t get_str_len() const { return len_; }
673
674 /* I'm a double-quoted string node */
675 virtual int is_dstring() const { return TRUE; }
676 virtual int is_dstring_expr() const { return TRUE; }
677
678 /* fold constants - there's nothing extra to do here */
679 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
680 { return this; }
681
682 /* double-quoted strings have no value */
683 virtual int has_return_value() const { return FALSE; }
684
685 /* adjust for dynamic (run-time) compilation */
686 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
687
688 protected:
689 /* my string */
690 const char *str_;
691 size_t len_;
692 };
693
694 /*
695 * The target must also define CTPNDebugDstr, a subclass of CTPNDstr.
696 * This for use in debugger expressions.
697 */
698 /* class CTPNDebugDstr: public CTPNDstr ... */
699
700 /*
701 * Double-quoted string embedding. This node contains an expression
702 * embedded with the "<< >>" notation inside a double-quoted string.
703 * This type of expression requires an implicit say() call.
704 */
705 class CTPNDstrEmbedBase: public CTPNUnary
706 {
707 public:
708 CTPNDstrEmbedBase(CTcPrsNode *sub)
709 : CTPNUnary(sub) { }
710
711 /* this is part of a dstring expression */
712 virtual int is_dstring_expr() const { return TRUE; }
713
714 /* we have no return value */
715 virtual int has_return_value() const { return FALSE; }
716
717 /* adjust for dynamic (run-time) compilation */
718 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
719 {
720 /* don't allow in speculative mode due to side effects */
721 if (info->speculative)
722 err_throw(VMERR_BAD_SPEC_EVAL);
723
724 /* inherit default unary operator handling */
725 return CTPNUnary::adjust_for_dyn(info);
726 }
727 };
728
729 /* ------------------------------------------------------------------------ */
730 /*
731 * An embedded <<one of>> list in a string.
732 */
733 class CTPNStrOneOfBase: public CTcPrsNode
734 {
735 public:
736 /*
737 * Create from a list of values, and the attribute string for selecting
738 * a value on each invocation. The attributes string must be static
739 * data, since we don't make a copy. Currently we only allow built-in
740 * attribute selections, which we define in a static structure.
741 */
742 CTPNStrOneOfBase(int dstr, class CTPNList *lst, class CTcSymObj *state_obj)
743 {
744 dstr_ = dstr;
745 lst_ = lst;
746 state_obj_ = state_obj;
747 }
748
749 /* this is part of a dstring expression */
750 virtual int is_dstring_expr() const { return TRUE; }
751
752 /* adjust for dynamic compilation */
753 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
754
755 /* folder constants */
756 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
757
758 /*
759 * The overall "one of" list has a return value if it's in a
760 * single-quoted string, since the individual elements will themselves
761 * be represented as single-quoted strings. If we're in a
762 * double-quoted string, the individual elements are double-quoted
763 * strings.
764 */
765 int has_return_value() const { return !dstr_; }
766
767 protected:
768 /* type of string - double quoted or single quoted */
769 int dstr_;
770
771 /* the underlying list of substrings to choose from */
772 class CTPNList *lst_;
773
774 /* the state object (an anonymous OneOfIndexGen instance) */
775 class CTcSymObj *state_obj_;
776 };
777
778
779 /* ------------------------------------------------------------------------ */
780 /*
781 * List node. A list has an underlying linked list of elements.
782 */
783 class CTPNListBase: public CTcPrsNode
784 {
785 public:
786 CTPNListBase()
787 {
788 /* there are initially no elements */
789 cnt_ = 0;
790 head_ = tail_ = 0;
791
792 /*
793 * initially, the list is constant, because it's just an empty
794 * list; as we add elements, we'll check for constants, and
795 * we'll clear the constant flag the first time we add a
796 * non-constant element
797 */
798 is_const_ = TRUE;
799
800 /* presume it's not a lookup table list */
801 is_lookup_table_ = FALSE;
802
803 /* set my constant value to point to myself */
804 const_val_.set_list((class CTPNList *)this);
805 }
806
807 /* add an element to the list with a given expression */
808 void add_element(CTcPrsNode *expr);
809
810 /* remove each occurrence of a constant element from the list */
811 void remove_element(const class CTcConstVal *val);
812
813 /* get the constant value of the list */
814 CTcConstVal *get_const_val()
815 {
816 /*
817 * return our constant value only if we have one AND we're not a
818 * lookup table - a lookup table requires a NEW operation at
819 * run-time, so even if the underlying list of source values is
820 * constant, the overall list value isn't
821 */
822 return (is_const_ && !is_lookup_table_ ? &const_val_ : 0);
823 }
824
825 /*
826 * get the first/last element - other elements can be reached by
827 * traversing the linked list of elements starting with the head or
828 * tail
829 */
830 class CTPNListEle *get_head() const { return head_; }
831 class CTPNListEle *get_tail() const { return tail_; }
832
833 /* get the constant element at the given index */
834 CTcPrsNode *get_const_ele(int index);
835
836 /* get the number of elements in the list */
837 int get_count() const { return cnt_; }
838
839 /* fold constants */
840 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
841
842 /* adjust for dynamic (run-time) compilation */
843 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
844
845 /* mark it as a lookup table list */
846 void set_lookup_table() { is_lookup_table_ = TRUE; }
847
848 protected:
849 /* number of elements in the list */
850 int cnt_;
851
852 /* head and tail of list of elements */
853 class CTPNListEle *head_;
854 class CTPNListEle *tail_;
855
856 /*
857 * our constant value - this is valid only when all elements of the
858 * list are constant, as indicated by the is_const_ paremeter
859 */
860 CTcConstVal const_val_;
861
862 /* flag: all elements of the list are constant */
863 unsigned int is_const_ : 1;
864
865 /* flag: this is a LookupTable list */
866 unsigned int is_lookup_table_ : 1;
867 };
868
869 /*
870 * List Element. Each list element is represented by one of these
871 * objects; these are in turn linked together into a list.
872 */
873 class CTPNListEleBase: public CTcPrsNode
874 {
875 public:
876 CTPNListEleBase(CTcPrsNode *expr)
877 {
878 /* remember the underlying expression */
879 expr_ = expr;
880
881 /* this element isn't in a list yet */
882 next_ = 0;
883 prev_ = 0;
884 }
885
886 /* get/set the next element */
887 class CTPNListEle *get_next() const { return next_; }
888 void set_next(class CTPNListEle *nxt) { next_ = nxt; }
889
890 /* get/set the previous element */
891 class CTPNListEle *get_prev() const { return prev_; }
892 void set_prev(class CTPNListEle *prv) { prev_ = prv; }
893
894 /* get the underlying expression */
895 CTcPrsNode *get_expr() const { return expr_; }
896
897 /* fold constants */
898 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
899 {
900 /* fold constants in my expression */
901 expr_ = expr_->fold_constants(symtab);
902
903 /* return myself unchanged (although my contents may be changed) */
904 return this;
905 }
906
907 /* adjust for dynamic (run-time) compilation */
908 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
909 {
910 /* adjust my expression */
911 expr_ = expr_->adjust_for_dyn(info);
912
913 /* return myself otherwise unchanged */
914 return this;
915 }
916
917 protected:
918 /* the underlying expression */
919 CTcPrsNode *expr_;
920
921 /* the next element of the list */
922 class CTPNListEle *next_;
923
924 /* previous element of the list */
925 class CTPNListEle *prev_;
926 };
927
928 /* ------------------------------------------------------------------------ */
929 /*
930 * Member with no arguments
931 */
932 class CTPNMemberBase: public CTcPrsNode
933 {
934 public:
935 CTPNMemberBase(CTcPrsNode *obj_expr, CTcPrsNode *prop_expr,
936 int prop_is_expr)
937 {
938 /* remember the subexpressions */
939 obj_expr_ = obj_expr;
940 prop_expr_ = prop_expr;
941
942 /* remember whether or not the property is an expression */
943 prop_is_expr_ = prop_is_expr;
944 }
945
946 /* a member with no arguments can always be used as an lvalue */
947 virtual int check_lvalue() const { return TRUE; }
948 virtual int check_lvalue_resolved(class CTcPrsSymtab *) const
949 { return TRUE; }
950
951 /* get the subexpressions */
952 CTcPrsNode *get_obj_expr() const { return obj_expr_; }
953 CTcPrsNode *get_prop_expr() const { return prop_expr_; }
954
955 /* fold constants */
956 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
957 {
958 /* fold constants in the object and property expressions */
959 obj_expr_ = obj_expr_->fold_constants(symtab);
960 prop_expr_ = prop_expr_->fold_constants(symtab);
961
962 /* return myself with no further evaluation */
963 return this;
964 }
965
966 /* adjust for dynamic (run-time) compilation */
967 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
968
969 protected:
970 /* object expression (left of '.') */
971 CTcPrsNode *obj_expr_;
972
973 /* property expression (right of '.') */
974 CTcPrsNode *prop_expr_;
975
976 /* flag: the property is an expression, not a simple property name */
977 unsigned int prop_is_expr_ : 1;
978 };
979
980 /*
981 * The target must also define another class, CTPNSpecDebugMember, a
982 * subclass of CTPNMember. This new class must ensure at run-time that
983 * the property evaluation has no side effects.
984 */
985 /* class CTPNSpecDebugMember: public CTPNMember ... */
986
987 /*
988 * Member with arguments - this is for a property evaluation with an
989 * argument list. (Simple property evaluation without arguments is a
990 * normal binary node subclass.)
991 */
992 class CTPNMemArgBase: public CTcPrsNode
993 {
994 public:
995 CTPNMemArgBase(CTcPrsNode *obj_expr, CTcPrsNode *prop_expr,
996 int prop_is_expr, class CTPNArglist *arglist)
997 {
998 /* save the subexpressions */
999 obj_expr_ = obj_expr;
1000 prop_expr_ = prop_expr;
1001 arglist_ = arglist;
1002
1003 /* remember whether or not the property is an expression */
1004 prop_is_expr_ = prop_is_expr;
1005 }
1006
1007 /* get the subexpressions */
1008 CTcPrsNode *get_obj_expr() const { return obj_expr_; }
1009 CTcPrsNode *get_prop_expr() const { return prop_expr_; }
1010 class CTPNArglist *get_arg_list() const { return arglist_; }
1011
1012 /* fold constants */
1013 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
1014
1015 /* adjust for dynamic (run-time) compilation */
1016 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
1017
1018 protected:
1019 /* object expression (left of '.') */
1020 CTcPrsNode *obj_expr_;
1021
1022 /* property expression (right of '.') */
1023 CTcPrsNode *prop_expr_;
1024
1025 /* argument list */
1026 class CTPNArglist *arglist_;
1027
1028 /* flag: the property is an expression, not a simple property name */
1029 unsigned int prop_is_expr_ : 1;
1030 };
1031
1032 /* ------------------------------------------------------------------------ */
1033 /*
1034 * Subscript
1035 */
1036 class CTPNSubscriptBase: public CTPNBin
1037 {
1038 public:
1039 CTPNSubscriptBase(CTcPrsNode *lhs, CTcPrsNode *rhs)
1040 : CTPNBin(lhs, rhs) { }
1041
1042 /* a subscripted array element can be assigned into */
1043 virtual int check_lvalue() const { return TRUE; }
1044 virtual int check_lvalue_resolved(class CTcPrsSymtab *) const
1045 { return TRUE; }
1046
1047 /* fold constants */
1048 class CTcPrsNode *fold_binop();
1049 };
1050
1051
1052 /* ------------------------------------------------------------------------ */
1053 /*
1054 * Comma operator
1055 */
1056 class CTPNCommaBase: public CTPNBin
1057 {
1058 public:
1059 CTPNCommaBase(CTcPrsNode *lhs, CTcPrsNode *rhs)
1060 : CTPNBin(lhs, rhs) { }
1061
1062 /* fold constants */
1063 class CTcPrsNode *fold_binop();
1064
1065 /*
1066 * a comma expression's value is the value of the right operand, so the
1067 * type is boolean if the right operand's type is boolean
1068 */
1069 virtual int is_bool() const { return right_->is_bool(); }
1070
1071 /*
1072 * determine if I have a value - I have a value if my right
1073 * subexpression has a value
1074 */
1075 virtual int has_return_value() const
1076 { return right_->has_return_value(); }
1077
1078 /* this is part of a dstring expression if either side is */
1079 virtual int is_dstring_expr() const
1080 { return left_->is_dstring_expr() || right_->is_dstring_expr(); }
1081 };
1082
1083 /* ------------------------------------------------------------------------ */
1084 /*
1085 * Add
1086 */
1087 class CTPNAddBase: public CTPNBin
1088 {
1089 public:
1090 CTPNAddBase(CTcPrsNode *lhs, CTcPrsNode *rhs)
1091 : CTPNBin(lhs, rhs) { }
1092
1093 /* fold constants */
1094 class CTcPrsNode *fold_binop();
1095 };
1096
1097 /* ------------------------------------------------------------------------ */
1098 /*
1099 * Subtract
1100 */
1101 class CTPNSubBase: public CTPNBin
1102 {
1103 public:
1104 CTPNSubBase(CTcPrsNode *lhs, CTcPrsNode *rhs)
1105 : CTPNBin(lhs, rhs) { }
1106
1107 /* fold constants */
1108 class CTcPrsNode *fold_binop();
1109 };
1110
1111 /* ------------------------------------------------------------------------ */
1112 /*
1113 * Equality Comparison
1114 */
1115 class CTPNEqBase: public CTPNBin
1116 {
1117 public:
1118 CTPNEqBase(CTcPrsNode *lhs, CTcPrsNode *rhs)
1119 : CTPNBin(lhs, rhs) { }
1120
1121 /* fold constants */
1122 class CTcPrsNode *fold_binop();
1123
1124 /* comparison operators yield boolean results */
1125 virtual int is_bool() const { return TRUE; }
1126 };
1127
1128 /* ------------------------------------------------------------------------ */
1129 /*
1130 * Inequality Comparison
1131 */
1132 class CTPNNeBase: public CTPNBin
1133 {
1134 public:
1135 CTPNNeBase(CTcPrsNode *lhs, CTcPrsNode *rhs)
1136 : CTPNBin(lhs, rhs) { }
1137
1138 /* fold constants */
1139 class CTcPrsNode *fold_binop();
1140
1141 /* comparison operators yield boolean results */
1142 virtual int is_bool() const { return TRUE; }
1143 };
1144
1145 /* ------------------------------------------------------------------------ */
1146 /*
1147 * 'is in'
1148 */
1149 class CTPNIsInBase: public CTPNBin
1150 {
1151 public:
1152 CTPNIsInBase(CTcPrsNode *lhs, class CTPNArglist *rhs);
1153
1154 /* fold constants */
1155 class CTcPrsNode *fold_binop();
1156
1157 /* comparison operators yield boolean results */
1158 virtual int is_bool() const { return TRUE; }
1159
1160 protected:
1161 /*
1162 * flag: if true, we have a constant true value (because the left
1163 * side is a constant, and the right side list contains the same
1164 * constant)
1165 */
1166 unsigned int const_true_ : 1;
1167 };
1168
1169 /* ------------------------------------------------------------------------ */
1170 /*
1171 * 'not in'
1172 */
1173 class CTPNNotInBase: public CTPNBin
1174 {
1175 public:
1176 CTPNNotInBase(CTcPrsNode *lhs, class CTPNArglist *rhs);
1177
1178 /* fold constants */
1179 class CTcPrsNode *fold_binop();
1180
1181 /* comparison operators yield boolean results */
1182 virtual int is_bool() const { return TRUE; }
1183
1184 /*
1185 * flag: if true, we have a constant true value (because the left
1186 * side is a constant, and the right side list is entirely constants
1187 * and doesn't contain the left side value)
1188 */
1189 unsigned int const_false_ : 1;
1190 };
1191
1192 /* ------------------------------------------------------------------------ */
1193 /*
1194 * Logical AND operator node
1195 */
1196 class CTPNAndBase: public CTPNBin
1197 {
1198 public:
1199 CTPNAndBase(CTcPrsNode *lhs, CTcPrsNode *rhs)
1200 : CTPNBin(lhs, rhs) { }
1201
1202 /* a logical AND always yields a boolean */
1203 virtual int is_bool() const { return TRUE; }
1204
1205 /* fold constants */
1206 class CTcPrsNode *fold_binop();
1207 };
1208
1209 /* ------------------------------------------------------------------------ */
1210 /*
1211 * Logical OR operator node
1212 */
1213 class CTPNOrBase: public CTPNBin
1214 {
1215 public:
1216 CTPNOrBase(CTcPrsNode *lhs, CTcPrsNode *rhs)
1217 : CTPNBin(lhs, rhs) { }
1218
1219 /* a logical OR always yields a boolean value */
1220 virtual int is_bool() const { return TRUE; }
1221
1222 /* fold constants */
1223 class CTcPrsNode *fold_binop();
1224 };
1225
1226 /* ------------------------------------------------------------------------ */
1227 /*
1228 * Simple assignment operator
1229 */
1230 class CTPNAsiBase: public CTPNBin
1231 {
1232 public:
1233 CTPNAsiBase(CTcPrsNode *lhs, CTcPrsNode *rhs)
1234 : CTPNBin(lhs, rhs) { }
1235
1236 /* we're a simple assignment operator */
1237 virtual int is_simple_asi() const { return TRUE; }
1238
1239 /* adjust for dynamic (run-time) compilation */
1240 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
1241 {
1242 /* if it's speculative, don't allow it, because of side effects */
1243 if (info->speculative)
1244 err_throw(VMERR_BAD_SPEC_EVAL);
1245
1246 /* inherit default processing */
1247 return CTPNBin::adjust_for_dyn(info);
1248 }
1249 };
1250
1251 /* ------------------------------------------------------------------------ */
1252 /*
1253 * NOT operator node
1254 */
1255 class CTPNNotBase: public CTPNUnary
1256 {
1257 public:
1258 CTPNNotBase(CTcPrsNode *sub) : CTPNUnary(sub) { }
1259
1260 /* NOT always yields a boolean value */
1261 virtual int is_bool() const { return TRUE; }
1262
1263 /* fold constants */
1264 class CTcPrsNode *fold_unop();
1265 };
1266
1267 /* ------------------------------------------------------------------------ */
1268 /*
1269 * If-nil node - operator ??
1270 */
1271 class CTPNIfnilBase: public CTcPrsNode
1272 {
1273 public:
1274 CTPNIfnilBase(class CTcPrsNode *first, class CTcPrsNode *second)
1275 {
1276 first_ = first;
1277 second_ = second;
1278 }
1279
1280 /* fold constants */
1281 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
1282
1283 /* I have a return value if one or the other of my subnodes has a value */
1284 virtual int has_return_value() const
1285 { return first_->has_return_value() || second_->has_return_value(); }
1286
1287 /* adjust for dynamic (run-time) compilation */
1288 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
1289 {
1290 /* adjust my subexpressions */
1291 first_ = first_->adjust_for_dyn(info);
1292 second_ = second_->adjust_for_dyn(info);
1293 return this;
1294 }
1295
1296 protected:
1297 class CTcPrsNode *first_;
1298 class CTcPrsNode *second_;
1299 };
1300
1301 /* ------------------------------------------------------------------------ */
1302 /*
1303 * Conditional operator parse node
1304 */
1305 class CTPNIfBase: public CTcPrsNode
1306 {
1307 public:
1308 CTPNIfBase(class CTcPrsNode *first, class CTcPrsNode *second,
1309 class CTcPrsNode *third)
1310 {
1311 first_ = first;
1312 second_ = second;
1313 third_ = third;
1314 }
1315
1316 /* fold constants */
1317 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
1318
1319 /* add the branches */
1320 void set_cond(CTcPrsNode *e) { first_ = e; }
1321 void set_then(CTcPrsNode *e) { second_ = e; }
1322 void set_else(CTcPrsNode *e) { third_ = e; }
1323
1324 /* I have a return value if one or the other of my subnodes has a value */
1325 virtual int has_return_value() const
1326 { return second_->has_return_value() || third_->has_return_value(); }
1327
1328 /* adjust for dynamic (run-time) compilation */
1329 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
1330 {
1331 /* adjust my subexpressions */
1332 first_ = first_->adjust_for_dyn(info);
1333 second_ = second_->adjust_for_dyn(info);
1334 third_ = third_->adjust_for_dyn(info);
1335
1336 /* return myself otherwise unchanged */
1337 return this;
1338 }
1339
1340 protected:
1341 class CTcPrsNode *first_;
1342 class CTcPrsNode *second_;
1343 class CTcPrsNode *third_;
1344 };
1345
1346 /* ------------------------------------------------------------------------ */
1347 /*
1348 * Derived target-independent symbol classes
1349 */
1350
1351 /*
1352 * Undefined symbol. During symbol resolution (after all parsing is
1353 * completed), when we encounter a symbol that isn't defined, we can use
1354 * this symbol type to provide a placeholder. Creating an unknown
1355 * symbol is an error, so the program cannot be successfully compiled
1356 * once an undefined symbol is encountered, but this at least allows
1357 * compilation to proceed through the rest of the program to check for
1358 * other errors.
1359 */
1360 class CTcSymUndefBase: public CTcSymbol
1361 {
1362 public:
1363 CTcSymUndefBase(const char *str, size_t len, int copy)
1364 : CTcSymbol(str, len, copy, TC_SYM_UNKNOWN) { }
1365
1366 /* do not write this to a symbol file */
1367 virtual int write_to_sym_file(class CVmFile *) { return FALSE; }
1368
1369 /* do not write this to an object file */
1370 virtual int write_to_obj_file(class CVmFile *) { return FALSE; }
1371
1372 /* this can't be an lvalue */
1373 virtual int check_lvalue() const { return FALSE; }
1374 };
1375
1376 /*
1377 * Function. At construction time, a function's actual code offset is
1378 * never known; we cannot know a function's code offset until after
1379 * we've finished code generation.
1380 */
1381 class CTcSymFuncBase: public CTcSymbol
1382 {
1383 public:
1384 CTcSymFuncBase(const char *str, size_t len, int copy,
1385 int argc, int opt_argc, int varargs, int has_retval,
1386 int is_multimethod, int is_multimethod_base,
1387 int is_extern, int has_proto)
1388 : CTcSymbol(str, len, copy, TC_SYM_FUNC)
1389 {
1390 /* remember the interface information */
1391 argc_ = argc;
1392 opt_argc_ = opt_argc;
1393 varargs_ = varargs;
1394 has_retval_ = has_retval;
1395 is_multimethod_ = is_multimethod;
1396 is_multimethod_base_ = is_multimethod_base;
1397
1398 /* note whether it's external */
1399 is_extern_ = is_extern;
1400
1401 /* note whether it has a prototype */
1402 has_proto_ = has_proto;
1403
1404 /* no code stream anchor yet */
1405 anchor_ = 0;
1406
1407 /* no fixups yet */
1408 fixups_ = 0;
1409
1410 /* this isn't replacing an external function */
1411 ext_replace_ = FALSE;
1412
1413 /* we don't know our code body yet */
1414 code_body_ = 0;
1415
1416 /* there's no base modified symbol */
1417 mod_base_ = 0;
1418
1419 /* we haven't been modified yet */
1420 mod_global_ = 0;
1421
1422 /* assume it's not defined in this file */
1423 mm_def_ = FALSE;
1424 }
1425
1426 /* determine if I'm an unresolved external */
1427 virtual int is_unresolved_extern() const { return is_extern_; }
1428
1429 /* this can't be an lvalue */
1430 virtual int check_lvalue() const { return FALSE; }
1431
1432 /*
1433 * Get the argument count. If is_varargs() returns true, this is a
1434 * minimum argument count; otherwise, this is the exact number of
1435 * arguments required.
1436 */
1437 int get_argc() const { return argc_; }
1438 int get_opt_argc() const { return opt_argc_; }
1439 int is_varargs() const { return varargs_; }
1440
1441 /* is the argument list within the correct range? */
1442 int argc_ok(int n) const
1443 {
1444 /*
1445 * the argument list is correct if it has at least the fixed
1446 * arguments, AND either we have varargs (in which case there's no
1447 * upper limit) or the argument count is less than the maximum
1448 * (which is the fixed plus optionals)
1449 */
1450 return (n >= argc_
1451 && (varargs_ || n <= argc_ + opt_argc_));
1452 }
1453
1454 /* build a descriptive message about the argument list, for errors */
1455 const char *get_argc_desc(char *buf)
1456 {
1457 if (varargs_)
1458 sprintf(buf, "%d+", argc_);
1459 else if (opt_argc_ != 0)
1460 sprintf(buf, "%d-%d", argc_, argc_ + opt_argc_);
1461 else
1462 sprintf(buf, "%d", argc_);
1463
1464 return buf;
1465 }
1466
1467 /* get/set the 'extern' flag */
1468 int is_extern() const { return is_extern_; }
1469 void set_extern(int f) { is_extern_ = f; }
1470
1471 /* get/set the 'has prototype' flag */
1472 int has_proto() const { return has_proto_; }
1473 void set_has_proto(int f) { has_proto_ = f; }
1474
1475 /* get/set the multi-method flag */
1476 int is_multimethod() const { return is_multimethod_; }
1477 void set_multimethod(int f) { is_multimethod_ = f; }
1478
1479 /* get/set the multi-method base function flag */
1480 int is_multimethod_base() const { return is_multimethod_base_; }
1481 void set_multimethod_base(int f) { is_multimethod_base_ = f; }
1482
1483 /* is this multi-method defined in this file? */
1484 int is_mm_def() const { return mm_def_; }
1485 void set_mm_def(int f) { mm_def_ = f; }
1486
1487 /* get/set the external 'replace' flag */
1488 int get_ext_replace() const { return ext_replace_; }
1489 void set_ext_replace(int f) { ext_replace_ = f; }
1490
1491 /* get/set the modified base symbol */
1492 class CTcSymFunc *get_mod_base() const { return mod_base_; }
1493 void set_mod_base(class CTcSymFunc *f) { mod_base_ = f; }
1494
1495 /*
1496 * Get/set the global symbol. For a function replaced with 'modify',
1497 * this is the original symbol for the function, which is still in the
1498 * global symbol table. Otherwise, this is simply null.
1499 */
1500 class CTcSymFunc *get_mod_global() const { return mod_global_; }
1501 void set_mod_global(class CTcSymFunc *f) { mod_global_ = f; }
1502
1503 /* get/set my code body */
1504 class CTPNCodeBody *get_code_body() const { return code_body_; }
1505 void set_code_body(class CTPNCodeBody *cb) { code_body_ = cb; }
1506
1507 /* determine if the function has a return value */
1508 int has_retval() const { return has_retval_; }
1509
1510 /* the name of a function used alone yields its address */
1511 class CTcPrsNode *fold_constant();
1512
1513 /* write some additional data to the symbol file */
1514 virtual int write_to_sym_file(class CVmFile *fp);
1515
1516 /* write some additional data to the object file */
1517 virtual int write_to_obj_file(class CVmFile *fp);
1518
1519 /* read from a symbol file */
1520 static class CTcSymbol *read_from_sym_file(class CVmFile *fp);
1521
1522 /* load from an object file */
1523 static int load_from_obj_file(class CVmFile *fp, const textchar_t *fname);
1524
1525 /* get my anchor */
1526 struct CTcStreamAnchor *get_anchor() const { return anchor_; }
1527
1528 /*
1529 * Set my anchor. This must be invoked during code generation when
1530 * our code body is generated.
1531 */
1532 virtual void set_anchor(struct CTcStreamAnchor *anchor)
1533 { anchor_ = anchor; }
1534
1535 /* get a pointer to my fixup list anchor */
1536 virtual struct CTcAbsFixup **get_fixup_list_anchor() { return &fixups_; }
1537
1538 /* add an absolute fixup for the symbol at the given stream offset */
1539 void add_abs_fixup(class CTcDataStream *ds, ulong ofs);
1540
1541 /* add an absolute fixup for the symbol at the current stream offset */
1542 void add_abs_fixup(class CTcDataStream *ds);
1543
1544 /* determine if I have a return value when called as a function */
1545 virtual int has_return_value_on_call() const
1546 { return has_retval_; }
1547
1548 /* get the number of allocated modified base function offsets */
1549 int get_mod_base_offset_count() const
1550 { return mod_base_ofs_list_.get_count(); }
1551
1552 /* get a modified base function offset by index */
1553 ulong get_mod_base_offset(int idx)
1554 { return (ulong)mod_base_ofs_list_.get_ele_long(idx); }
1555
1556 /* add a modified base function offset */
1557 void add_mod_base_offset(ulong ofs)
1558 { mod_base_ofs_list_.add_ele((long)ofs); }
1559
1560 /* clear the modified base offset list */
1561 void clear_mod_base_offsets() { mod_base_ofs_list_.clear(); }
1562
1563 /*
1564 * Set the absolute address. For debugger expressions, this sets the
1565 * code pool address specified in the debugger source expression.
1566 */
1567 virtual void set_abs_addr(uint32_t addr) = 0;
1568
1569 protected:
1570 /* head of the fixup list for our function's code */
1571 struct CTcAbsFixup *fixups_;
1572
1573 /*
1574 * my stream anchor - this records my byte stream data size, code
1575 * stream offset, and (after linking) final pool address
1576 */
1577 struct CTcStreamAnchor *anchor_;
1578
1579 /* the code body of the function */
1580 class CTPNCodeBody *code_body_;
1581
1582 /* argument count */
1583 int argc_;
1584
1585 /* number of additional optional arguments */
1586 int opt_argc_;
1587
1588 /* the original symbol we modified, if any */
1589 class CTcSymFunc *mod_base_;
1590
1591 /* global symbol, for a modified function */
1592 class CTcSymFunc *mod_global_;
1593
1594 /* array of modified base function code stream offsets */
1595 CPrsArrayList mod_base_ofs_list_;
1596
1597 /* flag: variable arguments (in which case argc_ is only a minimum) */
1598 unsigned int varargs_ : 1;
1599
1600 /* flag: the function has a return value (false -> void function) */
1601 unsigned int has_retval_ : 1;
1602
1603 /* flag: this function is external to us */
1604 unsigned int is_extern_ : 1;
1605
1606 /* flag: this function replaces an external function */
1607 unsigned int ext_replace_ : 1;
1608
1609 /* flag: this is a multi-method */
1610 unsigned int is_multimethod_ : 1;
1611
1612 /* flag: this is a multi-method base function */
1613 unsigned int is_multimethod_base_ : 1;
1614
1615 /* flag: this multi-method is defined in this file */
1616 unsigned int mm_def_ : 1;
1617
1618 /*
1619 * Flag: this function has a known prototype. An 'extern' function
1620 * declaration is allowed without a prototype, in which case the
1621 * function can be defined elsewhere with any prototype.
1622 */
1623 unsigned int has_proto_ : 1;
1624 };
1625
1626 /*
1627 * metaclass symbol - property list entry
1628 */
1629 class CTcSymMetaProp
1630 {
1631 public:
1632 CTcSymMetaProp(class CTcSymProp *prop, int is_static)
1633 {
1634 /* remember the property symbol */
1635 prop_ = prop;
1636
1637 /* remember whether it's static */
1638 is_static_ = is_static;
1639
1640 /* not in a list yet */
1641 nxt_ = 0;
1642 }
1643
1644 /* property symbol for this entry */
1645 class CTcSymProp *prop_;
1646
1647 /* next entry in the list */
1648 CTcSymMetaProp *nxt_;
1649
1650 /* flag: the property is declared as 'static' */
1651 unsigned int is_static_ : 1;
1652 };
1653
1654 /*
1655 * Metaclass.
1656 *
1657 * Metaclasses must be declared per module. However, we store "soft"
1658 * references in symbol files, purely so that the defined() operator knows
1659 * whether a metaclass is included in the build. The soft references don't
1660 * contain any property table information, so we still need a full
1661 * 'intrinsic class' definition in each module.
1662 */
1663 class CTcSymMetaclassBase: public CTcSymbol
1664 {
1665 public:
1666 CTcSymMetaclassBase(const char *str, size_t len, int copy,
1667 int meta_idx, tctarg_obj_id_t class_obj)
1668 : CTcSymbol(str, len, copy, TC_SYM_METACLASS)
1669 {
1670 /* remember the metaclass dependency list index */
1671 meta_idx_ = meta_idx;
1672
1673 /* we have no properties in our list yet */
1674 prop_head_ = 0;
1675 prop_tail_ = 0;
1676 prop_cnt_ = 0;
1677
1678 /* remember my class object */
1679 class_obj_ = class_obj;
1680
1681 /* we have no modification object yet */
1682 mod_obj_ = 0;
1683
1684 /* we don't have a superclass yet */
1685 super_meta_ = 0;
1686
1687 /* assume it's not an external symbol */
1688 ext_ = FALSE;
1689
1690 /* not yet referenced */
1691 ref_ = FALSE;
1692 }
1693
1694 /* this can't be an lvalue */
1695 virtual int check_lvalue() const { return FALSE; }
1696
1697 /* add a property to my list */
1698 void add_prop(const char *txt, size_t len, const char *obj_fname,
1699 int is_static);
1700 void add_prop(class CTcSymProp *prop, int is_static);
1701
1702 /* get the head of the property symbol list */
1703 CTcSymMetaProp *get_prop_head() const { return prop_head_; }
1704
1705 /* get the nth property ID from the metaclass method table */
1706 CTcSymMetaProp *get_nth_prop(int n) const;
1707
1708 /* get/set the metaclass dependency table index */
1709 int get_meta_idx() const { return meta_idx_; }
1710 void set_meta_idx(int idx) { meta_idx_ = idx; }
1711
1712 /* write to a symbol file */
1713 virtual int write_to_sym_file(class CVmFile *fp);
1714
1715 /* read from a symbol file */
1716 static class CTcSymbol *read_from_sym_file(class CVmFile *fp);
1717
1718 /* write some additional data to the object file */
1719 virtual int write_to_obj_file(class CVmFile *fp);
1720
1721 /* load from an object file */
1722 static int load_from_obj_file(class CVmFile *fp,
1723 const textchar_t *fname,
1724 tctarg_obj_id_t *obj_xlat);
1725
1726 /* get my class object */
1727 tctarg_obj_id_t get_class_obj() const { return class_obj_; }
1728
1729 /* get/set my modification object */
1730 class CTcSymObj *get_mod_obj() const { return mod_obj_; }
1731 void set_mod_obj(CTcSymObj *obj) { mod_obj_ = obj; }
1732
1733 /* get/set my intrinsic superclass */
1734 class CTcSymMetaclass *get_super_meta() const { return super_meta_; }
1735 void set_super_meta(class CTcSymMetaclass *sc) { super_meta_ = sc; }
1736
1737 /*
1738 * Get/set the external reference status. An external metaclass is one
1739 * that's defined in another module; we have visibility to it via a
1740 * reference in a symbol file. This definition isn't sufficient to
1741 * actually use the metaclass; the metaclass must be declared per
1742 * module via an explicitly included 'intrinsic class' statment. The
1743 * external reference is sufficient for the defined() operator, though,
1744 * so we can test whether a metaclass is part of the build within a
1745 * module that doesn't declare the metaclass.
1746 */
1747 int is_ext() const { return ext_; }
1748 void set_ext(int f) { ext_ = f; }
1749
1750 protected:
1751 /* our intrinsic superclass */
1752 class CTcSymMetaclass *super_meta_;
1753
1754 /* metaclass dependency table index */
1755 int meta_idx_;
1756
1757 /* list of properties */
1758 CTcSymMetaProp *prop_head_;
1759 CTcSymMetaProp *prop_tail_;
1760
1761 /*
1762 * modification object - this is the object that contains the user
1763 * methods added to the intrinsic class via 'modify'
1764 */
1765 class CTcSymObj *mod_obj_;
1766
1767 /* number of properties in our list */
1768 int prop_cnt_;
1769
1770 /* the object that represents the class */
1771 tctarg_obj_id_t class_obj_;
1772
1773 /* is this an external metaclass symbol? */
1774 uint ext_ : 1;
1775
1776 /* has our object been referenced in code generation? */
1777 uint ref_ : 1;
1778 };
1779
1780 /*
1781 * Object. An object's ID number can be assigned as soon as the object
1782 * is defined during parsing.
1783 */
1784 class CTcSymObjBase: public CTcSymbol
1785 {
1786 public:
1787 CTcSymObjBase(const char *str, size_t len, int copy, ulong obj_id,
1788 int is_extern, tc_metaclass_t meta,
1789 class CTcDictEntry *dict)
1790 : CTcSymbol(str, len, copy, TC_SYM_OBJ)
1791 {
1792 /* store the object ID */
1793 obj_id_ = obj_id;
1794
1795 /* note whether it's external */
1796 is_extern_ = is_extern;
1797
1798 /* we're not yet referenced */
1799 ref_ = FALSE;
1800
1801 /* we don't have a stream offset yet */
1802 stream_ofs_ = 0xffffffff;
1803
1804 /* we're don't 'modify' or 'replace' an external object */
1805 ext_modify_ = ext_replace_ = FALSE;
1806
1807 /* I haven't been modified yet */
1808 modified_ = FALSE;
1809
1810 /* presume we won't modify another object */
1811 mod_base_sym_ = 0;
1812
1813 /* presume we won't be modified by another object */
1814 modifying_obj_.sym_ = 0;
1815
1816 /* no parse tree yet */
1817 obj_stm_ = 0;
1818
1819 /* no self-reference fixups yet */
1820 fixups_ = 0;
1821
1822 /* no deleted properties yet */
1823 first_del_prop_ = 0;
1824
1825 /* set the metaclass */
1826 metaclass_ = meta;
1827
1828 /* set the dictionary */
1829 dict_ = dict;
1830
1831 /* not yet written to object file */
1832 written_to_obj_ = FALSE;
1833 counted_in_obj_ = FALSE;
1834
1835 /* no object file index yet */
1836 obj_file_idx_ = 0;
1837
1838 /* no vocabulary yet */
1839 vocab_ = 0;
1840
1841 /* no superclasses in our list yet */
1842 sc_ = 0;
1843
1844 /* no superclass names in our list yet */
1845 sc_name_head_ = 0;
1846 sc_name_tail_ = 0;
1847
1848 /* presume we're not a class */
1849 is_class_ = FALSE;
1850
1851 /* we haven't built our inherited vocabulary list yet */
1852 vocab_inherited_ = FALSE;
1853
1854 /* presume we're fully nonymous */
1855 anon_ = FALSE;
1856
1857 /* assume we will have no metaclass-specific extra data object */
1858 meta_extra_ = 0;
1859
1860 /* we're not yet explicitly based on the root object class */
1861 sc_is_root_ = FALSE;
1862
1863 /* no templates for this class yet */
1864 template_head_ = template_tail_ = 0;
1865
1866 /* we don't have a private grammar rule list yet */
1867 grammar_entry_ = 0;
1868
1869 /* presume it's not transient */
1870 transient_ = FALSE;
1871 }
1872
1873 /* this can't be an lvalue */
1874 virtual int check_lvalue() const { return FALSE; }
1875
1876 /* get/set my metaclass */
1877 tc_metaclass_t get_metaclass() const { return metaclass_; }
1878 void set_metaclass(tc_metaclass_t meta) { metaclass_ = meta; }
1879
1880 /* determine if I'm an unresolved external */
1881 virtual int is_unresolved_extern() const { return is_extern_; }
1882
1883 /* get/set the object ID */
1884 ulong get_obj_id() const { return obj_id_; }
1885 void set_obj_id(ulong obj_id) { obj_id_ = obj_id; }
1886
1887 /*
1888 * Get/set the 'extern' flag. If an object symbol is defined as
1889 * extern, it means that the object is referenced but not defined in
1890 * the current file. This is used for separate compilation; extern
1891 * objects must be resolved during linking.
1892 */
1893 int is_extern() const { return is_extern_; }
1894 void set_extern(int f) { is_extern_ = f; }
1895
1896 /* get the appropriate object data stream for this object */
1897 class CTcDataStream *get_stream()
1898 { return get_stream_from_meta(metaclass_); }
1899
1900 /* get/set my object stream offset */
1901 ulong get_stream_ofs() const { return stream_ofs_; }
1902 void set_stream_ofs(ulong ofs) { stream_ofs_ = ofs; }
1903
1904 /* get/set my object statement's parse tree */
1905 class CTPNStmObject *get_obj_stm() const { return obj_stm_; }
1906 void set_obj_stm(class CTPNStmObject *obj_stm) { obj_stm_ = obj_stm; }
1907
1908 /* get/set the external 'modify' flag */
1909 int get_ext_modify() const { return ext_modify_; }
1910 void set_ext_modify(int f) { ext_modify_ = f; }
1911
1912 /* get/set the external 'replace' flag */
1913 int get_ext_replace() const { return ext_replace_; }
1914 void set_ext_replace(int f) { ext_replace_ = f; }
1915
1916 /*
1917 * Get/Set the 'modify' base object. For a 'modify' object, this sets
1918 * the symbol (which is always a synthesized symbol) of the object
1919 * being modified.
1920 */
1921 class CTcSymObj *get_mod_base_sym() const { return mod_base_sym_; }
1922 void set_mod_base_sym(class CTcSymObj *sym);
1923
1924 /*
1925 * Get/set the modifiying object - this is the 'modify' object
1926 * that's modifying us. When we set this value, we'll also tell the
1927 * modified object that we are its modifier.
1928 */
1929 class CTcSymObj *get_modifying_sym() const
1930 { return modifying_obj_.sym_; }
1931 void set_modifying_sym(CTcSymObj *sym)
1932 { modifying_obj_.sym_ = sym; }
1933
1934 /*
1935 * Get/set the modifying object ID - the ID can be used instead of
1936 * the symbol, if desired. Note that only one or the other may be
1937 * stored, since they share the same data space.
1938 */
1939 tctarg_obj_id_t get_modifying_obj_id() const
1940 { return modifying_obj_.id_; }
1941 void set_modifying_obj_id(tctarg_obj_id_t id)
1942 { modifying_obj_.id_ = id; }
1943
1944 /*
1945 * get/set the 'modified' flag - this flag indicates that this is a
1946 * placeholder symbol for a modified object
1947 */
1948 int get_modified() const { return modified_; }
1949 void set_modified(int f) { modified_ = f; }
1950
1951 /*
1952 * Synthesize a placeholder symbol to use for modified objects.
1953 * This symbol keeps track of the original object after
1954 * modification.
1955 */
1956 static class CTcSymObj *synthesize_modified_obj_sym(int anon);
1957
1958 /*
1959 * synthesize the name of a modified object base class symbol given
1960 * the base class's object ID
1961 */
1962 static void synthesize_modified_obj_name(char *buf,
1963 tctarg_obj_id_t obj_id);
1964
1965 /* add a self-reference fixup */
1966 void add_self_ref_fixup(class CTcDataStream *stream, ulong ofs);
1967
1968 /* an object's value is a compile-time constant */
1969 class CTcPrsNode *fold_constant();
1970
1971 /*
1972 * we can't take the address of an object - an object symbol yields a
1973 * reference to begin with, so taking its address is meaningless
1974 */
1975 virtual int has_addr() const { return FALSE; }
1976
1977 /* write some additional data to the symbol file */
1978 virtual int write_to_sym_file(class CVmFile *fp);
1979
1980 /* write some additional data to the object file */
1981 virtual int write_to_obj_file(class CVmFile *fp);
1982
1983 /* write myself as a modified base object to an object file */
1984 int write_to_obj_file_as_modified(class CVmFile *fp);
1985
1986 /* write references to the object file */
1987 virtual int write_refs_to_obj_file(class CVmFile *fp);
1988
1989 /* load references from the object file */
1990 virtual void load_refs_from_obj_file(class CVmFile *fp,
1991 const textchar_t *fname,
1992 tctarg_obj_id_t *obj_xlat,
1993 tctarg_prop_id_t *prop_xlat);
1994
1995 /* mark the object as referenced */
1996 void mark_referenced() { ref_ = TRUE; }
1997
1998 /* read from a symbol file */
1999 static class CTcSymbol *read_from_sym_file(class CVmFile *fp);
2000
2001 /* load from an object file */
2002 static int load_from_obj_file(class CVmFile *fp, const textchar_t *fname,
2003 tctarg_obj_id_t *obj_xlat, int anon);
2004
2005 /* load a modified base object from an object file */
2006 static class CTcSymObj *
2007 load_from_obj_file_modbase(class CVmFile *fp, const textchar_t *fname,
2008 tctarg_obj_id_t *obj_xlat,
2009 const textchar_t *mod_name,
2010 size_t mod_name_len, int anon);
2011
2012 /* get/set the head of my fixup list */
2013 struct CTcIdFixup *get_fixups() const { return fixups_; }
2014 void set_fixups(struct CTcIdFixup *fixups) { fixups_ = fixups; }
2015
2016 /* apply our internal fixups */
2017 virtual void apply_internal_fixups();
2018
2019 /* add a deleted property entry */
2020 void add_del_prop_entry(class CTcSymProp *prop)
2021 { add_del_prop_to_list(&first_del_prop_, prop); }
2022
2023 /* get the head of my deleted property list */
2024 class CTcObjPropDel *get_first_del_prop() const
2025 { return first_del_prop_; }
2026
2027 /* set the head of my deleted property list, replacing any old list */
2028 void set_del_prop_head(class CTcObjPropDel *list_head)
2029 { first_del_prop_ = list_head; }
2030
2031 /*
2032 * Delete a property from 'modify' base objects. This must be
2033 * defined by the target-specific code subclass, because the action
2034 * of this routine is specific to the code generator.
2035 *
2036 * This carries out a link-time 'replace' on a property in a
2037 * 'modify' object. This object is the 'modify' object; we must
2038 * delete the property in all of the base classes of this object
2039 * that were modified by this 'modify' or by a 'modify' to which
2040 * this 'modify' indirectly applies. Do not delete the property
2041 * from this object - only from its modified base classes. Stop
2042 * upon reaching a non-modified base class, because this is the base
2043 * class of the original.
2044 */
2045 virtual void delete_prop_from_mod_base(tctarg_prop_id_t prop_id) = 0;
2046
2047 /*
2048 * Mark the compiled data for this object as a class object. This
2049 * must be defined by the target-specific code subclass, because we
2050 * need to modify the generated code for the object.
2051 *
2052 * This routine is called when an object loaded from an object file
2053 * is modified by an object in another object file, and hence
2054 * becomes a base class at link-time. We call this on the original,
2055 * which doesn't know it's being modified until link time.
2056 */
2057 virtual void mark_compiled_as_class() = 0;
2058
2059 /* get/set the symbol's containing dictionary */
2060 class CTcDictEntry *get_dict() const { return dict_; }
2061 void set_dict(class CTcDictEntry *dict) { dict_ = dict; }
2062
2063 /* get/set my object file index */
2064 uint get_obj_file_idx() const { return obj_file_idx_; }
2065 void set_obj_file_idx(uint idx) { obj_file_idx_ = idx; }
2066
2067 /* add a word to my vocabulary list */
2068 void add_vocab_word(const char *txt, size_t len, tctarg_prop_id_t prop);
2069
2070 /* delete a vocabulary property from my list (for 'replace') */
2071 void delete_vocab_prop(tctarg_prop_id_t prop);
2072
2073 /* get/set the vocabulary word list head */
2074 class CTcVocabEntry *get_vocab_head() const { return vocab_; }
2075 void set_vocab_head(class CTcVocabEntry *entry) { vocab_ = entry; }
2076
2077 /* build my dictionary */
2078 virtual void build_dictionary();
2079
2080 /* inherit my vocabulary from superclasses (used during linking) */
2081 void inherit_vocab();
2082
2083 /* add my vocabulary words to a subclass's vocabulary (for linking) */
2084 void add_vocab_to_subclass(class CTcSymObj *sub);
2085
2086 /* get/set my class flag (used and valid only during linking) */
2087 int is_class() const { return is_class_ != 0; }
2088 void set_is_class(int is_class) { is_class_ = (is_class != 0); }
2089
2090 /* get/set the resolved superclass list head (link-time only) */
2091 class CTcObjScEntry *get_sc_head() const { return sc_; }
2092 void set_sc_head(class CTcObjScEntry *sc) { sc_ = sc; }
2093
2094 /* get the named superclass list head (compile-time only) */
2095 class CTPNSuperclass *get_sc_name_head() const { return sc_name_head_; }
2096
2097 /* clear the named superclass list */
2098 void clear_sc_names() { sc_name_head_ = sc_name_tail_ = 0; }
2099
2100 /* copy the name list from another object symbol */
2101 void copy_sc_names(CTcSymObjBase *other_obj)
2102 {
2103 /* point our entries directly to the other symbol's */
2104 sc_name_head_ = other_obj->sc_name_head_;
2105 sc_name_tail_ = other_obj->sc_name_tail_;
2106 }
2107
2108 /*
2109 * Add a named superclass.
2110 *
2111 * We keep a superclass list here, separately from the superclass list
2112 * that the associated object statement itself maintains, because we
2113 * want hierarchy information for external objects as well as objects
2114 * defined in the current module. An external object has only a
2115 * symbol table entry, so there's no object statement in which to
2116 * stash the object list for such objects.
2117 *
2118 * In addition, this symbolic superclass list isn't exactly the same
2119 * as the true superclass list, because the symbolic list doesn't
2120 * include the special unnamed objects created when 'modify' is used.
2121 * The list we keep here mirrors the apparent structure as represented
2122 * in the source code.
2123 */
2124 void add_sc_name_entry(const char *txt, size_t len);
2125
2126 /*
2127 * determine if I have the given superclass - returns true if the
2128 * given superclass is among my direct superclasses, or if any of my
2129 * direct superclasses has the given superclass
2130 */
2131 int has_superclass(class CTcSymObj *sc_sym) const;
2132
2133 /* get/set the anonymous flag */
2134 int is_anon() const { return anon_ != 0; }
2135 void set_anon(int f) { anon_ = (f != 0); }
2136
2137 /* get/set the metaclass extra data for this object */
2138 void *get_meta_extra() const { return meta_extra_; }
2139 void set_meta_extra(void *ptr) { meta_extra_ = ptr; }
2140
2141 /*
2142 * get/set the root-object-superclass flag - if this is set, it means
2143 * that the object was explicitly defined with 'object' (the root
2144 * object type) as its superclass
2145 */
2146 int sc_is_root() const { return sc_is_root_; }
2147 void set_sc_is_root(int f) { sc_is_root_ = (f != 0); }
2148
2149 /* add a class-specific template */
2150 void add_template(class CTcObjTemplate *tpl);
2151
2152 /* get the head/tail of my template list */
2153 class CTcObjTemplate *get_first_template() const
2154 { return template_head_; }
2155 class CTcObjTemplate *get_last_template() const
2156 { return template_tail_; }
2157
2158 /*
2159 * Create our grammar rule entry object. If we already have one,
2160 * we'll discard it in favor of the new one; this allows a 'replace'
2161 * or 'modify' to override any previously-defined grammar rules.
2162 */
2163 class CTcGramProdEntry *create_grammar_entry(
2164 const char *prod_sym, size_t prod_sym_len);
2165
2166 /* get my grammar rule list */
2167 class CTcGramProdEntry *get_grammar_entry() const
2168 { return grammar_entry_; }
2169
2170 /*
2171 * merge my grammar rule list (if any) into the global rule list for
2172 * the associated production object
2173 */
2174 void merge_grammar_entry();
2175
2176 /* get/set transient status */
2177 int is_transient() const { return transient_; }
2178 void set_transient() { transient_ = TRUE; }
2179
2180 protected:
2181 /* add a deleted property entry to a list */
2182 static void add_del_prop_to_list(class CTcObjPropDel **list_head,
2183 class CTcSymProp *prop);
2184
2185 /* main object file writer routine */
2186 int write_to_obj_file_main(CVmFile *fp);
2187
2188 /* main object file loader routine */
2189 static class CTcSymObj *
2190 load_from_obj_file_main(class CVmFile *fp, const textchar_t *fname,
2191 tctarg_obj_id_t *obj_xlat,
2192 const textchar_t *mod_name,
2193 size_t mod_name_len, int anon);
2194
2195 /* get the stream for a given metaclass */
2196 static class CTcDataStream *get_stream_from_meta(tc_metaclass_t meta);
2197
2198 /* the object's ID */
2199 ulong obj_id_;
2200
2201 /*
2202 * the object stream (G_os, or other stream as appropriate) offset of
2203 * our data - this is valid after code generation if we're not
2204 * external
2205 */
2206 ulong stream_ofs_;
2207
2208 /* the parse tree for our object definition */
2209 class CTPNStmObject *obj_stm_;
2210
2211 /* head of our self-reference object ID fixup list */
2212 struct CTcIdFixup *fixups_;
2213
2214 /*
2215 * Head of list of deleted properties. This list is used only in
2216 * the top object in a stack of modified objects (i.e., this list is
2217 * attached to the latest 'modify' object), and only when the bottom
2218 * object in the stack is external to the current translation unit.
2219 * This is the list of objects that must be deleted at link time,
2220 * because they're external to the current translation unit.
2221 */
2222 class CTcObjPropDel *first_del_prop_;
2223
2224 /*
2225 * Base symbol for a 'modify' object. This is the synthesized
2226 * symbol for the object that this object modifies.
2227 */
2228 class CTcSymObj *mod_base_sym_;
2229
2230 /*
2231 * Modifying symbol for an object modified by a 'modify' object.
2232 * This points to the object that is modifying us - it's simply the
2233 * reverse link of mod_base_sym_.
2234 */
2235 union
2236 {
2237 class CTcSymObj *sym_;
2238 tctarg_obj_id_t id_;
2239 } modifying_obj_;
2240
2241 /* the dictionary holding my vocabulary words */
2242 class CTcDictEntry *dict_;
2243
2244 /* head of vocabulary list for this object */
2245 class CTcVocabEntry *vocab_;
2246
2247 /*
2248 * Head of superclass list - this list is used and valid ONLY during
2249 * linking; it is null during compilation. Because a class or object
2250 * can be defined before its superclasses, and can be defined in
2251 * separate a separate module from its superclass, we can't resolve
2252 * superclasses until link time.
2253 */
2254 class CTcObjScEntry *sc_;
2255
2256 /*
2257 * Superclass name list. This list is used and valid ONLY during
2258 * normal compilation, and keeps track of the symbolic name structure
2259 * of the class hiearchy. We don't resolve these names; we simply
2260 * keep track of the names as they appear in the source code.
2261 *
2262 * Note that the entries in this list must all have valid txt_ entries
2263 * - no entries with sym_ values are allowed in this list.
2264 */
2265 class CTPNSuperclass *sc_name_head_;
2266 class CTPNSuperclass *sc_name_tail_;
2267
2268 /* pointer to metaclass-specific extra data */
2269 void *meta_extra_;
2270
2271 /* the object's metaclass */
2272 tc_metaclass_t metaclass_;
2273
2274 /* list of templates associated with this class */
2275 class CTcObjTemplate *template_head_;
2276 class CTcObjTemplate *template_tail_;
2277
2278 /* object file index - used for serializing to the object file */
2279 uint obj_file_idx_;
2280
2281 /* grammar rule list, if we have one */
2282 class CTcGramProdEntry *grammar_entry_;
2283
2284 /* flag: this object is external to us */
2285 uint is_extern_ : 1;
2286
2287 /* flag: the object has been referenced */
2288 uint ref_ : 1;
2289
2290 /*
2291 * 'modify' and 'replace' flags - these indicate that this object
2292 * modifies/replaces an *external* object, hence the modify/replace
2293 * action must be performed at link time
2294 */
2295 uint ext_modify_ : 1;
2296 uint ext_replace_ : 1;
2297
2298 /* flag: I've been modified by another object */
2299 uint modified_ : 1;
2300
2301 /* flag: I'm a 'class' object (used and valid only during linking) */
2302 uint is_class_ : 1;
2303
2304 /* flag: this object has been written to the object file */
2305 uint written_to_obj_ : 1;
2306
2307 /* flag: this object has been counted as written to the object file */
2308 uint counted_in_obj_ : 1;
2309
2310 /*
2311 * flag: we've built our full vocabulary by explicitly inheriting
2312 * our superclass's words into our own list
2313 */
2314 uint vocab_inherited_ : 1;
2315
2316 /* flag: this object is anonymous */
2317 uint anon_ : 1;
2318
2319 /*
2320 * flag: we are explicitly defined with the root object ('object') as
2321 * our superclass
2322 */
2323 uint sc_is_root_ : 1;
2324
2325 /* flag: the object is transient */
2326 uint transient_ : 1;
2327 };
2328
2329 /*
2330 * Object vocabulary word entry
2331 */
2332 class CTcVocabEntry
2333 {
2334 public:
2335 CTcVocabEntry(const char *txt, size_t len, tctarg_prop_id_t prop)
2336 {
2337 /*
2338 * keep a reference to the text - assume it's in parser memory,
2339 * so it'll stay around for the duration of the compilation
2340 */
2341 txt_ = txt;
2342 len_ = len;
2343
2344 /* remember; the property */
2345 prop_ = prop;
2346
2347 /* not in a list yet */
2348 nxt_ = 0;
2349 }
2350
2351 /* text of word */
2352 const char *txt_;
2353 size_t len_;
2354
2355 /* property of the word association */
2356 tctarg_prop_id_t prop_;
2357
2358 /* next entry in list */
2359 CTcVocabEntry *nxt_;
2360 };
2361
2362 /*
2363 * Object superclass list entry
2364 */
2365 class CTcObjScEntry
2366 {
2367 public:
2368 CTcObjScEntry(class CTcSymObj *sym)
2369 {
2370 /* remember the object symbol */
2371 sym_ = sym;
2372
2373 /* not in a list yet */
2374 nxt_ = 0;
2375 }
2376
2377 /* object symbol */
2378 class CTcSymObj *sym_;
2379
2380 /* next superclass list entry */
2381 CTcObjScEntry *nxt_;
2382 };
2383
2384
2385 /*
2386 * Property. A property's ID can be assigned as soon as the property is
2387 * first used in an object definition.
2388 */
2389 class CTcSymPropBase: public CTcSymbol
2390 {
2391 public:
2392 CTcSymPropBase(const char *str, size_t len, int copy,
2393 tctarg_prop_id_t prop)
2394 : CTcSymbol(str, len, copy, TC_SYM_PROP)
2395 {
2396 /* remember our property ID */
2397 prop_ = prop;
2398
2399 /* we're not referenced yet */
2400 ref_ = FALSE;
2401
2402 /* presume it's not a vocabulary property */
2403 vocab_ = FALSE;
2404
2405 /* presume it's not a weak property assumption */
2406 weak_ = FALSE;
2407 }
2408
2409 /* get the property ID */
2410 tctarg_prop_id_t get_prop() const { return prop_; }
2411
2412 /* we can get the address of a property */
2413 class CTcPrsNode *fold_addr_const();
2414
2415 /* we can assign to a property */
2416 virtual int check_lvalue() const { return TRUE; }
2417
2418 /* we can take the address of a property */
2419 virtual int has_addr() const { return TRUE; }
2420
2421 /* mark the property as referenced */
2422 void mark_referenced() { ref_ = TRUE; }
2423
2424 /* get/set the vocabulary-property flag */
2425 int is_vocab() const { return vocab_; }
2426 void set_vocab(int flag) { vocab_ = (flag != 0); }
2427
2428 /* get/set the "weak" flag */
2429 int is_weak() const { return weak_; }
2430 void set_weak(int flag) { weak_ = (flag != 0); }
2431
2432 /* read/write symbol file */
2433 static class CTcSymbol *read_from_sym_file(class CVmFile *fp);
2434 virtual int write_to_sym_file(class CVmFile *fp);
2435
2436 /* write to an object file */
2437 int write_to_obj_file(class CVmFile *fp);
2438
2439 /* load from an object file */
2440 static int load_from_obj_file(class CVmFile *fp, const textchar_t *fname,
2441 tctarg_prop_id_t *prop_xlat);
2442
2443 protected:
2444 /* property ID */
2445 tctarg_prop_id_t prop_;
2446
2447 /* flag: the property is referenced */
2448 unsigned int ref_ : 1;
2449
2450 /* flag: this is a vocabulary property */
2451 unsigned int vocab_ : 1;
2452
2453 /*
2454 * Flag: this is a "weak" property definition. A weak definition is
2455 * one that can be overridden by a conflicting definition for the same
2456 * symbol using another type. Symbols used with "&" are assumed to be
2457 * properties, but only if we don't later find a conflicting
2458 * definition; the weak flag tells us that we can drop the property
2459 * assumption without complaint if we do find a different definition
2460 * for the symbol later.
2461 */
2462 unsigned int weak_: 1;
2463 };
2464
2465 /*
2466 * Enumerator
2467 */
2468 class CTcSymEnumBase: public CTcSymbol
2469 {
2470 public:
2471 CTcSymEnumBase(const char *str, size_t len, int copy, ulong id,
2472 int is_token)
2473 : CTcSymbol(str, len, copy, TC_SYM_ENUM)
2474 {
2475 /* remember my ID */
2476 enum_id_ = id;
2477
2478 /* remember if it's a 'token' enum */
2479 is_token_ = is_token;
2480
2481 /* not yet referenced */
2482 ref_ = FALSE;
2483 }
2484
2485 /* this can't be an lvalue */
2486 virtual int check_lvalue() const { return FALSE; }
2487
2488 /* mark the symbol as referenced */
2489 void mark_referenced() { ref_ = TRUE; }
2490
2491 /* an object's value is a compile-time constant */
2492 class CTcPrsNode *fold_constant();
2493
2494 /* get my enumerator ID */
2495 ulong get_enum_id() const { return enum_id_; }
2496
2497 /* get/set the 'token' flag */
2498 int is_token() const { return is_token_ != 0; }
2499 void set_is_token(int flag) { is_token_ = (flag != 0); }
2500
2501 /* write to a symbol file */
2502 virtual int write_to_sym_file(class CVmFile *fp);
2503
2504 /* read from a symbol file */
2505 static class CTcSymbol *read_from_sym_file(class CVmFile *fp);
2506
2507 /* write to an object file */
2508 int write_to_obj_file(class CVmFile *fp);
2509
2510 /* load from an object file */
2511 static int load_from_obj_file(class CVmFile *fp, const textchar_t *fname,
2512 ulong *enum_xlat);
2513
2514 protected:
2515 /* my enumerator ID */
2516 ulong enum_id_;
2517
2518 /* flag: this is a 'token' enum */
2519 unsigned int is_token_ : 1;
2520
2521 /* referenced? */
2522 unsigned int ref_ : 1;
2523 };
2524
2525
2526 /*
2527 * local variable/parameter
2528 */
2529 class CTcSymLocalBase: public CTcSymbol
2530 {
2531 public:
2532 CTcSymLocalBase(const char *str, size_t len, int copy,
2533 int is_param, int var_num);
2534
2535 /* get the variable number */
2536 int get_var_num() const { return var_num_; }
2537
2538 /* true -> parameter, false -> local variable */
2539 int is_param() const { return is_param_; }
2540
2541 /* get/set the parameter position (0 is the leftmost argument) */
2542 int get_param_index() const { return param_index_; }
2543 void set_param_index(int n) { param_index_ = n; }
2544
2545 /* true -> named parameter */
2546 int is_named_param() const { return is_named_param_; }
2547 void set_named_param(int f) { is_named_param_ = f; }
2548
2549 /* true -> optional parameter */
2550 int is_opt_param() const { return is_opt_param_; }
2551 void set_opt_param(int f) { is_opt_param_ = f; }
2552
2553 /* get/set the default value expression */
2554 CTcPrsNode *get_defval_expr() const { return defval_expr_; }
2555 int get_defval_seqno() const { return defval_seqno_; }
2556 void set_defval_expr(CTcPrsNode *expr, int seqno)
2557 {
2558 defval_expr_ = expr;
2559 defval_seqno_ = seqno;
2560 }
2561
2562 /*
2563 * get/set "list parameter" flag - a list parameter is still a local,
2564 * but from the programmer's perspective acts like a parameter because
2565 * its role is to contain a list of varargs parameters beyond a certain
2566 * point in the argument list
2567 */
2568 int is_list_param() const { return is_list_param_; }
2569 void set_list_param(int f) { is_list_param_ = f; }
2570
2571 /*
2572 * true -> "context local" - A context local is a local variable
2573 * contained in a scope enclosing an anonymous function. Because
2574 * the lifetime of an anonymous function can exceed that of the
2575 * activation record that was active when the anonymous function
2576 * object was created, every local variable in a scope that is
2577 * referenced from within an anonymous function defined within that
2578 * scope must be stored in a separate context object, rather than in
2579 * the activation record. The context object is created along with
2580 * the activation record, but it is not destroyed with the
2581 * activation record; instead, a reference to the context object is
2582 * stored in the anonymous function, which ensures that the context
2583 * object stays reachable as long as the anonymous function object
2584 * itself is reachable.
2585 */
2586 int is_ctx_local() const { return (is_ctx_local_ != 0); }
2587
2588 /*
2589 * Establish the variable as a context local, assigning the property
2590 * ID of the context object that stores the variable's value, and
2591 * the local variable that contains the context object itself.
2592 */
2593 void set_ctx_level(int ctx_level)
2594 {
2595 /* it's a context local now */
2596 is_ctx_local_ = TRUE;
2597
2598 /* remember the context level */
2599 ctx_level_ = ctx_level;
2600
2601 /* we don't know our context array index yet */
2602 ctx_arr_idx_ = 0;
2603 }
2604
2605 /*
2606 * get/set the original local in the enclosing scope for an
2607 * anonymous function's inherited local
2608 */
2609 class CTcSymLocal *get_ctx_orig() const { return ctx_orig_; }
2610 void set_ctx_orig(class CTcSymLocal *orig) { ctx_orig_ = orig; }
2611
2612 /* get my context array index */
2613 int get_ctx_arr_idx() const;
2614
2615 /* we can always assign to a local */
2616 virtual int check_lvalue() const { return TRUE; }
2617
2618 /*
2619 * get/set the 'used' flag - this tracks whether or not the
2620 * variable's value was referenced anywhere in the scope in which it
2621 * was defined
2622 */
2623 int get_val_used() const { return val_used_; }
2624 void set_val_used(int f);
2625
2626 /*
2627 * get/set the 'assigned' flag - this tracks whether or not the
2628 * variable has been assigned a value
2629 */
2630 int get_val_assigned() const { return val_assigned_; }
2631 void set_val_assigned(int f);
2632
2633 /* get the source location where the symbol was defined */
2634 class CTcTokFileDesc *get_src_desc() const { return src_desc_; }
2635 long get_src_linenum() const { return src_linenum_; }
2636
2637 /* get/set reference flag */
2638 int is_referenced() const { return referenced_ != 0; }
2639 void mark_referenced() { referenced_ = TRUE; }
2640
2641 /* check for references in local scope */
2642 virtual void check_local_references();
2643
2644 /* create a new context copy */
2645 virtual class CTcSymbol *new_ctx_var() const;
2646
2647 /* apply context variable conversion */
2648 virtual int apply_ctx_var_conv(class CTcPrsSymtab *symtab,
2649 class CTPNCodeBody *code_body);
2650
2651 /* finish context variable conversion */
2652 virtual void finish_ctx_var_conv();
2653
2654 /* convert this variable to a context variable */
2655 void convert_to_ctx_var(int val_used, int val_assigned);
2656
2657 protected:
2658 /* source location where the symbol was defined */
2659 class CTcTokFileDesc *src_desc_;
2660 long src_linenum_;
2661
2662 /* variable number - stack frame offset for local or parameter */
2663 int var_num_;
2664
2665 /*
2666 * Parameter index. This is the index within the formal parameter list
2667 * of a positional parameter variable. For regular positional
2668 * parameters, this is the same as var_num_. However, it's different
2669 * for *optional* parameters, because optional parameters are actually
2670 * stored in local variables, meaning that var_num_ is a *local* number
2671 * rather than an argument number. In order to generate the code that
2672 * loads the actual argument value into the local variable, we have to
2673 * keep track of both stack locations. This is where we keep track of
2674 * the parameter index.
2675 */
2676 int param_index_;
2677
2678 /*
2679 * context variable number - if this is a context variable, this is
2680 * the stack frame index of the variable containing the object
2681 * containing our value
2682 */
2683 int ctx_var_num_;
2684
2685 /*
2686 * Context level - this is the nesting level of the anonymous
2687 * function's context. A level of 0 indicates a local variable in
2688 * the current function; a level of 1 indicates a variable in the
2689 * enclosing scope; 2 is the next enclosing scope; and so on.
2690 */
2691 int ctx_level_;
2692
2693 /*
2694 * for a context variable, the index of this variable in the local
2695 * context array (zero means that it's not assigned)
2696 */
2697 int ctx_arr_idx_;
2698
2699 /*
2700 * for a variable within an anonymous function, the original local
2701 * variable symbol from the enclosing scope to which this variable
2702 * refers
2703 */
2704 class CTcSymLocal *ctx_orig_;
2705
2706 /*
2707 * For a formal parameter with a default value, the default value
2708 * expression, and the sequence number among the default expressions.
2709 * We process these expressions at function entry in left-to-right
2710 * order for the argument list, to ensure a predicatable order for
2711 * dependency resolution and side effects.
2712 */
2713 class CTcPrsNode *defval_expr_;
2714 int defval_seqno_;
2715
2716 /* true -> parameter, false -> local variable */
2717 unsigned int is_param_ : 1;
2718
2719 /* true -> named parameter */
2720 unsigned int is_named_param_ : 1;
2721
2722 /* true -> optional parametre */
2723 unsigned int is_opt_param_ : 1;
2724
2725 /*
2726 * true -> not a real parameter, but a "parameter list" local - this is
2727 * a local variable into which we deposit a list of the parameters
2728 * beyond a certain point, defined via the "[varname]" syntax in a
2729 * formal parameter list
2730 */
2731 unsigned int is_list_param_ : 1;
2732
2733 /* true -> this is a context local variable */
2734 unsigned int is_ctx_local_ : 1;
2735
2736 /* flag: the context variable number has been assigned */
2737 unsigned int ctx_var_num_set_ : 1;
2738
2739 /* true -> the value was referenced somewhere in its scope */
2740 unsigned int val_used_ : 1;
2741
2742 /* true -> the value was assigned */
2743 unsigned int val_assigned_ : 1;
2744
2745 /* true -> symbol has been referenced */
2746 unsigned int referenced_ : 1;
2747 };
2748
2749 /*
2750 * Dynamic local. This is a symbol entry for a local variable accessed
2751 * through a StackFrameRef object, for dynamic compilation.
2752 */
2753 class CTcSymDynLocalBase: public CTcSymbol
2754 {
2755 public:
2756 CTcSymDynLocalBase(const char *str, size_t len, int copy,
2757 tctarg_obj_id_t fref, int varnum, int ctxidx)
2758 : CTcSymbol(str, len, copy, TC_SYM_DYNLOCAL)
2759 {
2760 fref_ = fref;
2761 varnum_ = varnum;
2762 ctxidx_ = ctxidx;
2763 }
2764
2765 protected:
2766 /* the StackFrameRef object giving the frame containing the variable */
2767 tctarg_obj_id_t fref_;
2768
2769 /* the variable number in the frame */
2770 int varnum_;
2771
2772 /* the context index (0 if it's an ordinary frame variable) */
2773 int ctxidx_;
2774 };
2775
2776 /*
2777 * Built-in function
2778 */
2779 class CTcSymBifBase: public CTcSymbol
2780 {
2781 public:
2782 CTcSymBifBase(const char *str, size_t len, int copy,
2783 ushort func_set_id, ushort func_idx, int has_retval,
2784 int min_argc, int max_argc, int varargs)
2785 : CTcSymbol(str, len, copy, TC_SYM_BIF)
2786 {
2787 func_set_id_ = func_set_id;
2788 func_idx_ = func_idx;
2789 has_retval_ = has_retval;
2790 min_argc_ = min_argc;
2791 max_argc_ = max_argc;
2792 varargs_ = varargs;
2793 }
2794
2795 /* this can't be an lvalue */
2796 virtual int check_lvalue() const { return FALSE; }
2797
2798 /* get the function set ID and index in the function set */
2799 ushort get_func_set_id() const { return func_set_id_; }
2800 ushort get_func_idx() const { return func_idx_; }
2801
2802 /*
2803 * Get the minimum and maximum argument counts. If is_varargs()
2804 * returns true, there is no maximum, in which case get_max_argc()
2805 * is not meaningful.
2806 */
2807 int get_min_argc() const { return min_argc_; }
2808 int get_max_argc() const { return max_argc_; }
2809 int is_varargs() const { return varargs_; }
2810
2811 /* determine if the function has a return value */
2812 int has_retval() const { return has_retval_; }
2813
2814 /*
2815 * Do not write this to a symbol export file - this symbol is
2816 * external to us, hence we have no need to define it for other
2817 * modules to import.
2818 */
2819 virtual int write_to_sym_file(class CVmFile *) { return FALSE; }
2820
2821 /* write to an object file */
2822 virtual int write_to_obj_file(class CVmFile *fp);
2823
2824 /* load from an object file */
2825 static int load_from_obj_file(class CVmFile *fp, const textchar_t *fname);
2826
2827 protected:
2828 /* function set ID */
2829 ushort func_set_id_;
2830
2831 /* index within the function set */
2832 ushort func_idx_;
2833
2834 /* minimum and maximum argument count, and varargs flag */
2835 int min_argc_;
2836 int max_argc_;
2837 unsigned int varargs_ : 1;
2838
2839 /* true -> function has a return value, false -> void return */
2840 unsigned int has_retval_ : 1;
2841 };
2842
2843 /*
2844 * External function
2845 */
2846 class CTcSymExtfnBase: public CTcSymbol
2847 {
2848 public:
2849 CTcSymExtfnBase(const char *str, size_t len, int copy,
2850 int argc, int varargs, int has_retval)
2851 : CTcSymbol(str, len, copy, TC_SYM_EXTFN)
2852 {
2853 /* offset is not yet known */
2854 argc_ = argc;
2855 varargs_ = varargs;
2856 has_retval_ = has_retval;
2857 }
2858
2859 /* this can't be an lvalue */
2860 virtual int check_lvalue() const { return FALSE; }
2861
2862 /*
2863 * Get the argument count. If is_varargs() returns true, this is a
2864 * minimum argument count; otherwise, this is the exact number of
2865 * arguments required.
2866 */
2867 int get_argc() const { return argc_; }
2868 int is_varargs() const { return varargs_; }
2869
2870 /* determine if the function has a return value */
2871 int has_retval() const { return has_retval_; }
2872
2873 protected:
2874 /* argument count */
2875 int argc_;
2876
2877 /* flag: variable arguments (in which case argc_ is only a minimum) */
2878 unsigned int varargs_ : 1;
2879
2880 /* flag: function has a return value (false -> void function) */
2881 unsigned int has_retval_ : 1;
2882 };
2883
2884 /*
2885 * Code ("goto") label.
2886 */
2887 class CTcSymLabelBase: public CTcSymbol
2888 {
2889 public:
2890 CTcSymLabelBase(const char *str, size_t len, int copy)
2891 : CTcSymbol(str, len, copy, TC_SYM_LABEL)
2892 {
2893 /* the symbol is not yet referenced */
2894 referenced_ = FALSE;
2895
2896 /* we don't know the defining statement node yet */
2897 stm_ = 0;
2898 }
2899
2900 /* this can't be an lvalue */
2901 virtual int check_lvalue() const { return FALSE; }
2902
2903 /* get/set the defining statement node */
2904 class CTPNStmLabel *get_stm() const { return stm_; }
2905 void set_stm(class CTPNStmLabel *stm) { stm_ = stm; }
2906
2907 /* add control flow flags to our statement */
2908 void add_control_flow_flags(ulong flags);
2909
2910 protected:
2911 /* the defining statement's parse node */
2912 class CTPNStmLabel *stm_;
2913
2914 /*
2915 * flag: the symbol is referenced by at least one statement (we keep
2916 * track of this so that we warn when a label is defined but never
2917 * used as a target)
2918 */
2919 unsigned int referenced_ : 1;
2920 };
2921
2922
2923 /* ------------------------------------------------------------------------ */
2924 /*
2925 * Program Node. This is the top-level node in the tree; this node
2926 * contains the entire program as a sublist.
2927 */
2928 class CTPNStmProg: public CTPNStm
2929 {
2930 public:
2931 CTPNStmProg(class CTPNStmTop *first_stm)
2932 {
2933 /* remember the head of our statement list */
2934 first_stm_ = first_stm;
2935 }
2936
2937 /* fold constants */
2938 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
2939
2940 /*
2941 * Generate the program code and write to the image file. After
2942 * parsing, if parsing is successful, the compiler can invoke this
2943 * routine to construct the image file; there is no need to invoke
2944 * gen_code() directly.
2945 */
2946 void build_image(class CVmFile *image_fp, uchar xor_mask,
2947 const char tool_data[4]);
2948
2949 /*
2950 * Generate the program code and write to the object file. After
2951 * parsing, if parsing is successful, the compiler can invoke this
2952 * routine to construct the object file.
2953 */
2954 void build_object_file(class CVmFile *object_fp, class CTcMake *make_obj);
2955
2956 /* generate code for the program */
2957 virtual void gen_code(int discard, int for_condition);
2958
2959 protected:
2960 /*
2961 * Generate code. Returns zero on success, non-zero on error. This
2962 * is a common routine used by build_image() and build_object_file()
2963 * to complete code generation after parsing.
2964 */
2965 int gen_code_for_build();
2966
2967 /* head of our list */
2968 class CTPNStmTop *first_stm_;
2969 };
2970
2971
2972 /* ------------------------------------------------------------------------ */
2973 /*
2974 * Top-level object - this is a base class for statements which can
2975 * appear as top-level definitions, such as functions and objects.
2976 *
2977 * This class should not need to be derived per target, so further
2978 * target-generic base classes are derived from this directly.
2979 */
2980 class CTPNStmTop: public CTPNStm
2981 {
2982 public:
2983 CTPNStmTop()
2984 {
2985 /* we're not in a list yet */
2986 next_stm_top_ = 0;
2987 }
2988
2989 /* get/set the next top-level statement in the top-level list */
2990 CTPNStmTop *get_next_stm_top() const { return next_stm_top_; }
2991 void set_next_stm_top(CTPNStmTop *nxt) { next_stm_top_ = nxt; }
2992
2993 /*
2994 * Check the local symbol table. This is called after all of the
2995 * code has been generated for the entire program, which ensures
2996 * that all of the code that could affect any local symbols (even
2997 * those shared through context variables) has been finished. By
2998 * default we do nothing.
2999 */
3000 virtual void check_locals() { }
3001
3002 /* get/set the dynamic compilation run-time object ID */
3003 tctarg_obj_id_t get_dyn_obj_id() const { return dyn_obj_id_; }
3004 void set_dyn_obj_id(tctarg_obj_id_t id) { dyn_obj_id_ = id; }
3005
3006 protected:
3007 /*
3008 * Dynamic code compilation object ID. When we compile new source code
3009 * at run-time, the bytecode is stored in a DynamicFunc instance rather
3010 * than in the constant code pool, because the code pool is limited to
3011 * code loaded from the image file. Before generating code, the
3012 * dynamic compiler assigns a new DynamicFunc ID to each top-level
3013 * statement in the nested statement list. The DynamicFunc instances
3014 * aren't actually populated until the code is generated, but the
3015 * object numbers are assigned in advance to allow cross-references in
3016 * generated code.
3017 */
3018 tctarg_obj_id_t dyn_obj_id_;
3019
3020 /* next top-level statement in the top-level list */
3021 CTPNStmTop *next_stm_top_;
3022 };
3023
3024
3025 /* ------------------------------------------------------------------------ */
3026 /*
3027 * Anonymous function
3028 */
3029 class CTPNAnonFuncBase: public CTcPrsNode
3030 {
3031 public:
3032 CTPNAnonFuncBase(class CTPNCodeBody *code_body, int has_retval,
3033 int is_method)
3034 {
3035 /* remember my code body and return value status */
3036 code_body_ = code_body;
3037 has_retval_ = has_retval;
3038 is_method_ = is_method;
3039 }
3040
3041 /* no constant value */
3042 virtual class CTcConstVal *get_const_val() { return 0; }
3043
3044 /* I have a return value when called if the underlying code body does */
3045 virtual int has_return_value_on_call() const { return has_retval_; }
3046
3047 /*
3048 * fold constants - we don't have to fold constants in the
3049 * underlying code body, because its constants will be folded along
3050 * with other top-level statements
3051 */
3052 virtual class CTcPrsNode *fold_constants(class CTcPrsSymtab *)
3053 { return this; }
3054
3055 /* adjust for dynamic (run-time) compilation */
3056 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
3057 { return this; }
3058
3059 /* mark as replaced/obsolete */
3060 void set_replaced(int flag);
3061
3062 protected:
3063 /* code body of the function */
3064 class CTPNCodeBody *code_body_;
3065
3066 /* true -> the underlying code body has a return value */
3067 unsigned int has_retval_ : 1;
3068
3069 /* true -> this is a method; false -> it's a function */
3070 unsigned int is_method_ : 1;
3071 };
3072
3073 /* ------------------------------------------------------------------------ */
3074 /*
3075 * Code Body. A code body has local variables, goto labels, and a
3076 * compound statement.
3077 */
3078 class CTPNCodeBodyBase: public CTPNStmTop
3079 {
3080 public:
3081 CTPNCodeBodyBase(class CTcPrsSymtab *lcltab, class CTcPrsSymtab *gototab,
3082 class CTPNStm *stm, int argc, int opt_argc, int varargs,
3083 int varargs_list, class CTcSymLocal *varargs_list_local,
3084 int local_cnt, int self_valid,
3085 struct CTcCodeBodyRef *enclosing_code_body);
3086
3087 /*
3088 * Mark the code as having been replaced - this is used when this is
3089 * the code body for a function, and the function is replaced by
3090 * another implementation in the same translation unit. When we've
3091 * been replaced, we can't be referenced, hence we don't need to
3092 * generate any code.
3093 */
3094 void set_replaced(int f) { replaced_ = f; }
3095 int is_replaced() const { return replaced_; }
3096
3097 /*
3098 * Set our fixup list anchor - our creator must invoke this if we
3099 * are to use a fixup list associated with a symbol table entry. A
3100 * code body that can be reached from a symbol table entry (such as
3101 * a function name) must have its fixup list anchor associated with
3102 * the symbol table entry so that we can create the necessary
3103 * associations in an object file.
3104 */
3105 void set_symbol_fixup_list(class CTcSymFunc *sym,
3106 struct CTcAbsFixup **fixup_list_anchor)
3107 {
3108 /* remember the information */
3109 fixup_owner_sym_ = sym;
3110 fixup_list_anchor_ = fixup_list_anchor;
3111 }
3112
3113 /* use our internal fixup list */
3114 void use_internal_fixup_list()
3115 {
3116 /* we have no function symbol */
3117 fixup_owner_sym_ = 0;
3118
3119 /* use our internal list anchor */
3120 fixup_list_anchor_ = &internal_fixups_;
3121 }
3122
3123 /* get my associated function symbol, if there is one */
3124 class CTcSymFunc *get_func_sym() const { return fixup_owner_sym_; }
3125
3126 /* fold constants */
3127 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
3128
3129 /*
3130 * Check for unreferenced labels. Scans the 'goto' symbol table and
3131 * ensures that each labeled statement has been referenced; logs
3132 * a warning for each label that isn't referenced.
3133 */
3134 void check_unreferenced_labels();
3135
3136 /* get a pointer to my fixup list head */
3137 struct CTcAbsFixup **get_fixup_list_head() const
3138 { return fixup_list_anchor_; }
3139
3140 /* set the location of the opening brace */
3141 void set_start_location(class CTcTokFileDesc *desc, long linenum)
3142 {
3143 start_desc_ = desc;
3144 start_linenum_ = linenum;
3145 }
3146
3147 /* set the location of the closing brace */
3148 void set_end_location(class CTcTokFileDesc *desc, long linenum)
3149 {
3150 end_desc_ = desc;
3151 end_linenum_ = linenum;
3152 }
3153
3154 /* add an absolute fixup for the symbol at the given stream offset */
3155 void add_abs_fixup(class CTcDataStream *ds, ulong ofs);
3156
3157 /* add an absolute fixup for the symbol at the current stream offset */
3158 void add_abs_fixup(class CTcDataStream *ds);
3159
3160 /* mark the code body as requiring a local variable context object */
3161 void set_local_ctx(int var_num, int max_local_arr_idx)
3162 {
3163 /* note that we have a local context */
3164 has_local_ctx_ = TRUE;
3165
3166 /*
3167 * remember our the local variable number that contains our
3168 * context object
3169 */
3170 local_ctx_var_ = var_num;
3171
3172 /* remember our maximum context local array index */
3173 local_ctx_arr_size_ = max_local_arr_idx;
3174 }
3175
3176 /* does this function have a local context? */
3177 int has_local_ctx() const { return has_local_ctx_; }
3178
3179 /*
3180 * Get the context variable for a given recursion level, creating
3181 * the new variable if necessary. Level 1 is the first enclosing
3182 * scope, 2 is the next enclosing scope, and so on.
3183 */
3184 int get_or_add_ctx_var_for_level(int level);
3185
3186 /*
3187 * find a context variable given a requested level; returns true if
3188 * successful, false if not
3189 */
3190 int get_ctx_var_for_level(int level, int *varnum);
3191
3192 /* get the head of the local context list */
3193 struct CTcCodeBodyCtx *get_ctx_head() const { return ctx_head_; }
3194 struct CTcCodeBodyCtx *get_ctx_tail() const { return ctx_tail_; }
3195
3196 /* mark this as an anonymous method */
3197 int is_anon_method() const { return is_anon_method_; }
3198 void set_anon_method(int f) { is_anon_method_ = f; }
3199
3200 /* mark this as a dynamic function or method */
3201 int is_dyn_func() const { return is_dyn_func_; }
3202 int is_dyn_method() const { return is_dyn_method_; }
3203 void set_dyn_func(int f) { is_dyn_func_ = f; }
3204 void set_dyn_method(int f) { is_dyn_method_ = f; }
3205
3206 /* get/set 'self' reference status */
3207 int self_referenced() const { return self_referenced_; }
3208 void set_self_referenced(int f) { self_referenced_ = f; }
3209
3210 /* get/set full method context reference status */
3211 int full_method_ctx_referenced() const
3212 { return full_method_ctx_referenced_; }
3213 void set_full_method_ctx_referenced(int f)
3214 { full_method_ctx_referenced_ = f; }
3215
3216 /* get/set the local context need for 'self' */
3217 int local_ctx_needs_self() const { return local_ctx_needs_self_; }
3218 void set_local_ctx_needs_self(int f) { local_ctx_needs_self_ = f; }
3219
3220 /* get/set the local context need for the full method context */
3221 int local_ctx_needs_full_method_ctx() const
3222 { return local_ctx_needs_full_method_ctx_; }
3223 void set_local_ctx_needs_full_method_ctx(int f)
3224 { local_ctx_needs_full_method_ctx_ = f; }
3225
3226 /* get/set the operator overload flag */
3227 int is_operator_overload() const { return op_overload_; }
3228 void set_operator_overload(int f) { op_overload_ = f; }
3229
3230 /*
3231 * Get the base function symbol for a code body defining a modified
3232 * function (i.e., 'modify <funcname>...'). This is the function to
3233 * which 'replaced' refers within this code body and within nested code
3234 * bodies.
3235 */
3236 class CTcSymFunc *get_replaced_func() const;
3237
3238 /* get the immediately enclosing code body */
3239 class CTPNCodeBody *get_enclosing() const;
3240
3241 /* get my argument count */
3242 int get_argc() const { return argc_; }
3243 int get_opt_argc() const { return opt_argc_; }
3244
3245 /* adjust for dynamic (run-time) compilation */
3246 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
3247 {
3248 stm_ = (CTPNStm *)stm_->adjust_for_dyn(info);
3249 return this;
3250 }
3251
3252 protected:
3253 /* enumerator callback for unreferenced label check */
3254 static void unref_label_cb(void *ctx, class CTcSymbol *sym);
3255
3256 /* get the outermost enclosing code cody */
3257 class CTPNCodeBody *get_outermost_enclosing() const;
3258
3259 /* source location of opening brace */
3260 class CTcTokFileDesc *start_desc_;
3261 long start_linenum_;
3262
3263 /* source location of the end of the code body (the closing '}') */
3264 class CTcTokFileDesc *end_desc_;
3265 long end_linenum_;
3266
3267 /* local symbol table for the code body */
3268 class CTcPrsSymtab *lcltab_;
3269
3270 /* goto symbol table for the code body */
3271 class CTcPrsSymtab *gototab_;
3272
3273 /* statement making up the code body */
3274 class CTPNStm *stm_;
3275
3276 /*
3277 * fixup list head pointer - this will be set to &internal_fixups_
3278 * if we don't have a symbol table fixup list, or will point to our
3279 * symbol table fixup list if we have an external symbol providing
3280 * the fixup list
3281 */
3282 struct CTcAbsFixup **fixup_list_anchor_;
3283
3284 /* our symbol table entry, if our fixup list is defined there */
3285 class CTcSymFunc *fixup_owner_sym_;
3286
3287 /*
3288 * Our internal list head for our fixup list for references to this
3289 * code block. This is used only if we don't have an external fixup
3290 * list defined in a symbol table entry.
3291 */
3292 struct CTcAbsFixup *internal_fixups_;
3293
3294 /* enclosing code body pointer */
3295 struct CTcCodeBodyRef *enclosing_code_body_;
3296
3297 /* head/tail of anonymous function context list */
3298 struct CTcCodeBodyCtx *ctx_head_;
3299 struct CTcCodeBodyCtx *ctx_tail_;
3300
3301 /* number of local variable slots required */
3302 int local_cnt_;
3303
3304 /* number of arguments */
3305 int argc_;
3306
3307 /* number of additional optional arguments */
3308 int opt_argc_;
3309
3310 /* if function has a varargs-list local, this is the local ID */
3311 class CTcSymLocal *varargs_list_local_;
3312
3313 /* if function has a local context, this is its local ID */
3314 int local_ctx_var_;
3315
3316 /*
3317 * number of slots in the local context array, if we have a local
3318 * context at all
3319 */
3320 int local_ctx_arr_size_;
3321
3322 /*
3323 * flag: we have a varargs-list local (varargs_list_local_ has the
3324 * local variable symbol object)
3325 */
3326 unsigned int varargs_list_ : 1;
3327
3328 /* flag: function takes variable number of arguments */
3329 unsigned int varargs_ : 1;
3330
3331 /* flag: I've been replaced (and hence don't need codegen) */
3332 unsigned int replaced_ : 1;
3333
3334 /*
3335 * flag: the code body requires a local context - this is set if we
3336 * have any local variables that are stored in context objects for
3337 * use in anonymous functions
3338 */
3339 unsigned int has_local_ctx_ : 1;
3340
3341 /* flag: the local context requires 'self' */
3342 unsigned int local_ctx_needs_self_ : 1;
3343
3344 /*
3345 * flag: the local context requires the full method context (self,
3346 * targetprop, targetobj, definingobj)
3347 */
3348 unsigned int local_ctx_needs_full_method_ctx_ : 1;
3349
3350 /* flag: 'self' is valid in this code body */
3351 unsigned int self_valid_ : 1;
3352
3353 /* flags: 'self' and full method context referenced in this code body */
3354 unsigned int self_referenced_ : 1;
3355 unsigned int full_method_ctx_referenced_ : 1;
3356
3357 /* flag: this is an operator overload method */
3358 unsigned int op_overload_ : 1;
3359
3360 /* flag: this is an anonymous method */
3361 unsigned int is_anon_method_ : 1;
3362
3363 /* flags: this is a dynamic function/method */
3364 unsigned int is_dyn_func_ : 1;
3365 unsigned int is_dyn_method_ : 1;
3366 };
3367
3368 /*
3369 * Context level information
3370 */
3371 struct CTcCodeBodyCtx
3372 {
3373 CTcCodeBodyCtx() { }
3374
3375 /* level number */
3376 int level_;
3377
3378 /* local variable containing the context object for this level */
3379 int var_num_;
3380
3381 /* next/previous level structure in list */
3382 CTcCodeBodyCtx *nxt_;
3383 CTcCodeBodyCtx *prv_;
3384 };
3385
3386 /*
3387 * Code body reference.
3388 *
3389 * We don't create a CTPNCodeBody object for a code body we're parsing
3390 * until after we finish parsing it, so we can't establish a pointer to an
3391 * enclosing code body while we're parsing a nested code body within the
3392 * enclosing one. However, we want nested code body objects to be able to
3393 * refer to their enclosing code bodies. To deal with this, we create a
3394 * "code body reference" object for each code body at the start of parsing
3395 * the code body, and then fill it in with the code body object once we
3396 * finish parsing; nested code bodies can refer to the reference object
3397 * during parsing, so that after parsing they can get to the code body
3398 * itself.
3399 */
3400 struct CTcCodeBodyRef
3401 {
3402 CTcCodeBodyRef() { ptr = 0; }
3403
3404 /* pointer to enclosing code body */
3405 class CTPNCodeBody *ptr;
3406 };
3407
3408 /* ------------------------------------------------------------------------ */
3409 /*
3410 * Compound statement. A compound statement simply contains a list of
3411 * statements to be executed in sequence; this corresponds to a set of
3412 * statements enclosed in braces. A compound statement can have its own
3413 * set of local variables, since the enclosing braces constitute a local
3414 * scope.
3415 */
3416 class CTPNStmCompBase: public CTPNStm
3417 {
3418 public:
3419 CTPNStmCompBase(class CTPNStm *first_stm, class CTcPrsSymtab *symtab);
3420
3421 /* get my first statement */
3422 class CTPNStm *get_first_stm() const { return first_stm_; }
3423
3424 /* get the local scope's symbol table */
3425 class CTcPrsSymtab *get_symtab() const { return symtab_; }
3426
3427 /* set our own-scope flag */
3428 void set_has_own_scope(int f) { has_own_scope_ = f; }
3429
3430 /* fold constants */
3431 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
3432
3433 /* generate code for the compound statement */
3434 virtual void gen_code(int discard, int for_condition);
3435
3436 /* evaluate control flow for the statement list */
3437 virtual unsigned long get_control_flow(int warn) const;
3438
3439 /*
3440 * Determine if this statement has a code label. We have a code
3441 * label if our first statement has a code label.
3442 */
3443 virtual int has_code_label() const
3444 {
3445 return (first_stm_ == 0 ? FALSE : first_stm_->has_code_label());
3446 }
3447
3448 /* get the location of the closing brace */
3449 class CTcTokFileDesc *get_end_desc() const { return end_desc_; }
3450 long get_end_linenum() const { return end_linenum_; }
3451
3452 /* adjust for dynamic (run-time) compilation */
3453 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *);
3454
3455 protected:
3456 /* source location of closing brace */
3457 class CTcTokFileDesc *end_desc_;
3458 long end_linenum_;
3459
3460 /* first statement in our list of statements */
3461 class CTPNStm *first_stm_;
3462
3463 /*
3464 * our local symbol table; this is a private symbol table with the
3465 * symbols for our local scope if we have any, or is simply the
3466 * enclosing scope's symbol table if there are no variables in the
3467 * local scope
3468 */
3469 class CTcPrsSymtab *symtab_;
3470
3471 /*
3472 * flag: we have our own private symbol table (it's not the
3473 * enclosing scope's symbol table)
3474 */
3475 unsigned int has_own_scope_ : 1;
3476 };
3477
3478 /* ------------------------------------------------------------------------ */
3479 /*
3480 * Null statement - this is the statement that results from an empty
3481 * pair of braces, or simply a semicolon.
3482 */
3483 class CTPNStmNullBase: public CTPNStm
3484 {
3485 public:
3486 CTPNStmNullBase() { }
3487
3488 /* fold constants */
3489 class CTcPrsNode *fold_constants(class CTcPrsSymtab *) { return this; }
3490
3491 /* generate code for the compound statement */
3492 virtual void gen_code(int, int) { }
3493
3494 /* adjust for dynamic (run-time) compilation */
3495 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *)
3496 { return this; }
3497 };
3498
3499 /* ------------------------------------------------------------------------ */
3500 /*
3501 * Expression statement.
3502 */
3503 class CTPNStmExprBase: public CTPNStm
3504 {
3505 public:
3506 CTPNStmExprBase(class CTcPrsNode *expr)
3507 {
3508 /* remember my expression */
3509 expr_ = expr;
3510 }
3511
3512 /* fold constants */
3513 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
3514
3515 /* generate code for the compound statement */
3516 virtual void gen_code(int discard, int for_condition);
3517
3518 /* adjust for dynamic (run-time) compilation */
3519 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
3520 {
3521 expr_ = expr_->adjust_for_dyn(info);
3522 return this;
3523 }
3524
3525 protected:
3526 /* our expression */
3527 class CTcPrsNode *expr_;
3528 };
3529
3530 /* ------------------------------------------------------------------------ */
3531 /*
3532 * Static property initializer statement. This is used to hold the
3533 * expression of a static initializer, just under the CTPNObjProp
3534 * element in the tree.
3535 */
3536 class CTPNStmStaticPropInitBase: public CTPNStm
3537 {
3538 public:
3539 CTPNStmStaticPropInitBase(class CTcPrsNode *expr,
3540 tctarg_prop_id_t prop)
3541 {
3542 /* remember my expression and the property to be initialized */
3543 expr_ = expr;
3544 prop_ = prop;
3545 }
3546
3547 /*
3548 * fold constants - this is never used, because we always fold the
3549 * constants in the underlying expression before we create one of
3550 * these nodes, as this type of node is only created during constant
3551 * folding of the containing property initializer node
3552 */
3553 class CTcPrsNode *fold_constants(class CTcPrsSymtab *) { return this; }
3554
3555 protected:
3556 /* our expression */
3557 class CTcPrsNode *expr_;
3558
3559 /* property we initialize */
3560 tctarg_prop_id_t prop_;
3561 };
3562
3563 /* ------------------------------------------------------------------------ */
3564 /*
3565 * 'if' statement
3566 */
3567 class CTPNStmIfBase: public CTPNStm
3568 {
3569 public:
3570 CTPNStmIfBase(class CTcPrsNode *cond_expr,
3571 class CTPNStm *then_part, class CTPNStm *else_part)
3572 {
3573 /* remember the condition and the two statements */
3574 cond_expr_ = cond_expr;
3575 then_part_ = then_part;
3576 else_part_ = else_part;
3577 }
3578
3579 /* fold constants */
3580 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
3581
3582 /* evaluate control flow for the conditional */
3583 virtual unsigned long get_control_flow(int warn) const;
3584
3585 /* adjust for dynamic (run-time) compilation */
3586 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
3587 {
3588 if (then_part_ != 0)
3589 then_part_ = (CTPNStm *)then_part_->adjust_for_dyn(info);
3590 if (else_part_ != 0)
3591 else_part_ = (CTPNStm *)else_part_->adjust_for_dyn(info);
3592 return this;
3593 }
3594
3595 protected:
3596 /* condition expression */
3597 class CTcPrsNode *cond_expr_;
3598
3599 /* 'then' and 'else' substatements */
3600 class CTPNStm *then_part_;
3601 class CTPNStm *else_part_;
3602 };
3603
3604 /* ------------------------------------------------------------------------ */
3605 /*
3606 * 'for' statement
3607 */
3608 class CTPNStmForBase: public CTPNStmEnclosing
3609 {
3610 public:
3611 CTPNStmForBase(class CTcPrsNode *init_expr,
3612 class CTcPrsNode *cond_expr,
3613 class CTcPrsNode *reinit_expr,
3614 class CTPNForIn *in_exprs,
3615 class CTcPrsSymtab *symtab,
3616 class CTPNStmEnclosing *enclosing_stm)
3617 : CTPNStmEnclosing(enclosing_stm)
3618 {
3619 /* remember the statement parts */
3620 init_expr_ = init_expr;
3621 cond_expr_ = cond_expr;
3622 reinit_expr_ = reinit_expr;
3623 in_exprs_ = in_exprs;
3624 symtab_ = symtab;
3625
3626 /* no body yet */
3627 body_stm_ = 0;
3628
3629 /*
3630 * presume we use our parent's symbol table, not our own private
3631 * one
3632 */
3633 has_own_scope_ = FALSE;
3634 }
3635
3636 /* set the body */
3637 void set_body(class CTPNStm *body) { body_stm_ = body; }
3638
3639 /* fold constants */
3640 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
3641
3642 /* evaluate control flow for the loop */
3643 virtual unsigned long get_control_flow(int warn) const;
3644
3645 /* set our own-scope flag */
3646 void set_has_own_scope(int f) { has_own_scope_ = f; }
3647
3648 /* adjust for dynamic (run-time) compilation */
3649 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
3650 {
3651 if (init_expr_ != 0)
3652 init_expr_ = init_expr_->adjust_for_dyn(info);
3653 if (cond_expr_ != 0)
3654 cond_expr_ = cond_expr_->adjust_for_dyn(info);
3655 if (reinit_expr_ != 0)
3656 reinit_expr_ = reinit_expr_->adjust_for_dyn(info);
3657 if (body_stm_ != 0)
3658 body_stm_ = (CTPNStm *)body_stm_->adjust_for_dyn(info);
3659
3660 return this;
3661 }
3662
3663 protected:
3664 /* initialization expression */
3665 class CTcPrsNode *init_expr_;
3666
3667 /* loop condition expression */
3668 class CTcPrsNode *cond_expr_;
3669
3670 /* reinitialization expression */
3671 class CTcPrsNode *reinit_expr_;
3672
3673 /*
3674 * Head of list of "in" expressions (lval in collection, lval in
3675 * from..to). These appear as ordinary expressions within the
3676 * init_expr_ comma list, but we track them separately here, since they
3677 * implicitly generate code in the condition and reinit phases as well.
3678 */
3679 class CTPNForIn *in_exprs_;
3680
3681 /* body of the loop */
3682 class CTPNStm *body_stm_;
3683
3684 /* our local scope symbol table */
3685 class CTcPrsSymtab *symtab_;
3686
3687 /* flag: we have our own private symbol table (not our parent's) */
3688 unsigned int has_own_scope_ : 1;
3689 };
3690
3691 /* ------------------------------------------------------------------------ */
3692 /*
3693 * '<variable> in <expression>' node, for 'for' statements. Since 3.1,
3694 * regular 'for' statements can include 'in' clauses mixed in with other
3695 * initializer expressions.
3696 */
3697 class CTPNVarInBase: public CTPNForIn
3698 {
3699 public:
3700 CTPNVarInBase(class CTcPrsNode *lval, class CTcPrsNode *expr,
3701 int iter_local_id)
3702 {
3703 lval_ = lval;
3704 expr_ = expr;
3705 iter_local_id_ = iter_local_id;
3706 }
3707
3708 /* fold constants */
3709 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
3710
3711 /* adjust for dynamic (run-time) compilation */
3712 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
3713 {
3714 lval_ = lval_->adjust_for_dyn(info);
3715 expr_ = expr_->adjust_for_dyn(info);
3716
3717 return this;
3718 }
3719
3720 protected:
3721 /* the lvalue for the control variable */
3722 class CTcPrsNode *lval_;
3723
3724 /* the collection expression */
3725 class CTcPrsNode *expr_;
3726
3727 /* private local variable for the iterator */
3728 int iter_local_id_;
3729 };
3730
3731 /*
3732 * '<variable> in <from> .. <to>' node, for 'for' statements. Since 3.1,
3733 * 'for' statements can include 'in' clauses with range expressions using
3734 * '..'.
3735 */
3736 class CTPNVarInRangeBase: public CTPNForIn
3737 {
3738 public:
3739 CTPNVarInRangeBase(class CTcPrsNode *lval,
3740 class CTcPrsNode *from_expr,
3741 class CTcPrsNode *to_expr,
3742 class CTcPrsNode *step_expr,
3743 int to_local_id, int step_local_id)
3744 {
3745 lval_ = lval;
3746 from_expr_ = from_expr;
3747 to_expr_ = to_expr;
3748 step_expr_ = step_expr;
3749 to_local_id_ = to_local_id;
3750 step_local_id_ = step_local_id;
3751 }
3752
3753 /* adjust for dynamic (run-time) compilation */
3754 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
3755 {
3756 lval_ = lval_->adjust_for_dyn(info);
3757 from_expr_ = from_expr_->adjust_for_dyn(info);
3758 to_expr_ = to_expr_->adjust_for_dyn(info);
3759 if (step_expr_ != 0)
3760 step_expr_ = step_expr_->adjust_for_dyn(info);
3761
3762 return this;
3763 }
3764
3765 /* fold constants */
3766 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
3767
3768 protected:
3769 /* the lvalue for the control variable */
3770 class CTcPrsNode *lval_;
3771
3772 /* the 'from' and 'to' expressions */
3773 class CTcPrsNode *from_expr_;
3774 class CTcPrsNode *to_expr_;
3775
3776 /* the optional 'step' expression */
3777 class CTcPrsNode *step_expr_;
3778
3779 /* local variable number for the evaluated "to" expression */
3780 int to_local_id_;
3781
3782 /*
3783 * local variable number for the evaluated "step" expression - this is
3784 * used only if we have a non-constant step expression
3785 */
3786 int step_local_id_;
3787 };
3788
3789 /* ------------------------------------------------------------------------ */
3790 /*
3791 * 'foreach' statement
3792 */
3793 class CTPNStmForeachBase: public CTPNStmEnclosing
3794 {
3795 public:
3796 CTPNStmForeachBase(class CTcPrsNode *iter_expr,
3797 class CTcPrsNode *coll_expr,
3798 class CTcPrsSymtab *symtab,
3799 class CTPNStmEnclosing *enclosing_stm,
3800 int iter_local_id)
3801 : CTPNStmEnclosing(enclosing_stm)
3802 {
3803 /* remember the statement parts */
3804 iter_expr_ = iter_expr;
3805 coll_expr_ = coll_expr;
3806 symtab_ = symtab;
3807
3808 /* no body yet */
3809 body_stm_ = 0;
3810
3811 /* remember my private iterator local */
3812 iter_local_id_ = iter_local_id;
3813
3814 /*
3815 * presume we use our parent's symbol table, not our own private
3816 * one
3817 */
3818 has_own_scope_ = FALSE;
3819 }
3820
3821 /* set the body */
3822 void set_body(class CTPNStm *body) { body_stm_ = body; }
3823
3824 /* fold constants */
3825 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
3826
3827 /* evaluate control flow for the loop */
3828 virtual unsigned long get_control_flow(int warn) const;
3829
3830 /* set our own-scope flag */
3831 void set_has_own_scope(int f) { has_own_scope_ = f; }
3832
3833 /* adjust for dynamic (run-time) compilation */
3834 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
3835 {
3836 iter_expr_ = iter_expr_->adjust_for_dyn(info);
3837 coll_expr_ = coll_expr_->adjust_for_dyn(info);
3838 body_stm_ = (CTPNStm *)body_stm_->adjust_for_dyn(info);
3839 return this;
3840 }
3841
3842 protected:
3843 /* iteration lvalue expression */
3844 class CTcPrsNode *iter_expr_;
3845
3846 /* collection expression */
3847 class CTcPrsNode *coll_expr_;
3848
3849 /* body of the loop */
3850 class CTPNStm *body_stm_;
3851
3852 /* our local scope symbol table */
3853 class CTcPrsSymtab *symtab_;
3854
3855 /* ID of secret local variable containing the iterator object */
3856 int iter_local_id_;
3857
3858 /* flag: we have our own private symbol table (not our parent's) */
3859 unsigned int has_own_scope_ : 1;
3860 };
3861
3862 /* ------------------------------------------------------------------------ */
3863 /*
3864 * 'while' statement - this is an enclosing statement, because it can
3865 * interact with 'try' blocks with 'break' and 'continue' statements
3866 * used within the body of the loop
3867 */
3868 class CTPNStmWhileBase: public CTPNStmEnclosing
3869 {
3870 public:
3871 CTPNStmWhileBase(class CTcPrsNode *cond_expr,
3872 class CTPNStmEnclosing *enclosing_stm)
3873 : CTPNStmEnclosing(enclosing_stm)
3874 {
3875 /* remember the statement parts */
3876 cond_expr_ = cond_expr;
3877
3878 /* no body yet */
3879 body_stm_ = 0;
3880 }
3881
3882 /* set the body */
3883 void set_body(class CTPNStm *body) { body_stm_ = body; }
3884
3885 /* fold constants */
3886 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
3887
3888 /* evaluate control flow for the loop */
3889 virtual unsigned long get_control_flow(int warn) const;
3890
3891 /* adjust for dynamic (run-time) compilation */
3892 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
3893 {
3894 cond_expr_ = cond_expr_->adjust_for_dyn(info);
3895 body_stm_ = (CTPNStm *)body_stm_->adjust_for_dyn(info);
3896 return this;
3897 }
3898
3899 protected:
3900 /* loop condition expression */
3901 class CTcPrsNode *cond_expr_;
3902
3903 /* body of the loop */
3904 class CTPNStm *body_stm_;
3905 };
3906
3907 /* ------------------------------------------------------------------------ */
3908 /*
3909 * 'do-while' statement
3910 */
3911 class CTPNStmDoWhileBase: public CTPNStmEnclosing
3912 {
3913 public:
3914 CTPNStmDoWhileBase(class CTPNStmEnclosing *enclosing_stm)
3915 : CTPNStmEnclosing(enclosing_stm)
3916 {
3917 /* no condition or body yet */
3918 cond_expr_ = 0;
3919 body_stm_ = 0;
3920
3921 /*
3922 * we don't know the location of the 'while' part yet, so assume
3923 * it's at the same location as the 'do' for now
3924 */
3925 while_desc_ = file_;
3926 while_linenum_ = linenum_;
3927 }
3928
3929 /* set the condition */
3930 void set_cond(class CTcPrsNode *cond) { cond_expr_ = cond; }
3931
3932 /* set the body */
3933 void set_body(class CTPNStm *body) { body_stm_ = body; }
3934
3935 /* fold constants */
3936 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
3937
3938 /* evaluate control flow for the loop */
3939 virtual unsigned long get_control_flow(int warn) const;
3940
3941 /* set the source location of the 'while' statement */
3942 void set_while_pos(class CTcTokFileDesc *desc, long linenum)
3943 {
3944 while_desc_ = desc;
3945 while_linenum_ = linenum;
3946 }
3947
3948 /* adjust for dynamic (run-time) compilation */
3949 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
3950 {
3951 cond_expr_ = cond_expr_->adjust_for_dyn(info);
3952 body_stm_ = (CTPNStm *)body_stm_->adjust_for_dyn(info);
3953 return this;
3954 }
3955
3956 protected:
3957 /* source location of 'while' */
3958 class CTcTokFileDesc *while_desc_;
3959 long while_linenum_;
3960
3961 /* loop condition expression */
3962 class CTcPrsNode *cond_expr_;
3963
3964 /* body of the loop */
3965 class CTPNStm *body_stm_;
3966 };
3967
3968 /* ------------------------------------------------------------------------ */
3969 /*
3970 * 'break' statement
3971 */
3972 class CTPNStmBreakBase: public CTPNStm
3973 {
3974 public:
3975 CTPNStmBreakBase()
3976 {
3977 /* no label yet */
3978 lbl_ = 0;
3979 lbl_len_ = 0;
3980 }
3981
3982 /* fold constants */
3983 class CTcPrsNode *fold_constants(class CTcPrsSymtab *)
3984 {
3985 /* we have nothing to fold - return unchangdd */
3986 return this;
3987 }
3988
3989 /* evaluate control flow for the statement */
3990 virtual unsigned long get_control_flow(int warn) const;
3991
3992 /* set my label */
3993 void set_label(const class CTcToken *tok);
3994
3995 /* adjust for dynamic (run-time) compilation */
3996 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
3997 { return this; }
3998
3999 protected:
4000 /* my break-to label, if specified */
4001 const textchar_t *lbl_;
4002 size_t lbl_len_;
4003 };
4004
4005 /* ------------------------------------------------------------------------ */
4006 /*
4007 * 'continue' statement
4008 */
4009 class CTPNStmContinueBase: public CTPNStm
4010 {
4011 public:
4012 CTPNStmContinueBase()
4013 {
4014 /* no label yet */
4015 lbl_ = 0;
4016 lbl_len_ = 0;
4017 }
4018
4019 /* fold constants */
4020 class CTcPrsNode *fold_constants(class CTcPrsSymtab *)
4021 {
4022 /* we have nothing to fold - return unchangdd */
4023 return this;
4024 }
4025
4026 /* evaluate control flow for the statement */
4027 virtual unsigned long get_control_flow(int warn) const;
4028
4029 /* set my label */
4030 void set_label(const class CTcToken *tok);
4031
4032 /* adjust for dynamic (run-time) compilation */
4033 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
4034 { return this; }
4035
4036 protected:
4037 /* my break-to label, if specified */
4038 const textchar_t *lbl_;
4039 size_t lbl_len_;
4040 };
4041
4042 /* ------------------------------------------------------------------------ */
4043 /*
4044 * 'return' statement; if the expression node is null, this is a simple
4045 * void return
4046 */
4047 class CTPNStmReturnBase: public CTPNStm
4048 {
4049 public:
4050 CTPNStmReturnBase(class CTcPrsNode *expr)
4051 {
4052 /* remember the expression */
4053 expr_ = expr;
4054 }
4055
4056 /* fold constants */
4057 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
4058
4059 /* evaluate control flow for the statement */
4060 virtual unsigned long get_control_flow(int /*warn*/) const
4061 {
4062 /*
4063 * we return a value if we have an expression, otherwise we're
4064 * just a void return
4065 */
4066 return (expr_ == 0 ? TCPRS_FLOW_RET_VOID : TCPRS_FLOW_RET_VAL);
4067 }
4068
4069 /* adjust for dynamic (run-time) compilation */
4070 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
4071 {
4072 expr_ = expr_->adjust_for_dyn(info);
4073 return this;
4074 }
4075
4076 protected:
4077 /* the expression to return, or null for a void return */
4078 class CTcPrsNode *expr_;
4079 };
4080
4081 /* ------------------------------------------------------------------------ */
4082 /*
4083 * 'switch' statement. This is an 'enclosing' statement type because
4084 * 'break' can refer to the statement from within the code body.
4085 */
4086 class CTPNStmSwitchBase: public CTPNStmEnclosing
4087 {
4088 public:
4089 CTPNStmSwitchBase(class CTPNStmEnclosing *enclosing_stm)
4090 : CTPNStmEnclosing(enclosing_stm)
4091 {
4092 /* we have no body or expression yet */
4093 expr_ = 0;
4094 body_ = 0;
4095
4096 /* we have no case or default labels yet */
4097 case_cnt_ = 0;
4098 has_default_ = FALSE;
4099 }
4100
4101 /* set my expression */
4102 void set_expr(class CTcPrsNode *expr) { expr_ = expr; }
4103
4104 /* set my body */
4105 void set_body(class CTPNStm *body) { body_ = body; }
4106
4107 /* fold constants */
4108 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
4109
4110 /* evaluate control flow for the statement */
4111 virtual unsigned long get_control_flow(int warn) const;
4112
4113 /* get the number of 'case' labels */
4114 int get_case_cnt() const { return case_cnt_; }
4115
4116 /* determine if there's a 'default' label */
4117 int get_has_default() const { return has_default_ != 0; }
4118
4119 /* increment the number of case labels */
4120 void inc_case_cnt() { ++case_cnt_; }
4121
4122 /* mark that the body has a 'default' label */
4123 void set_has_default() { has_default_ = TRUE; }
4124
4125 /* adjust for dynamic (run-time) compilation */
4126 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
4127 {
4128 expr_ = expr_->adjust_for_dyn(info);
4129 body_ = (CTPNStm *)body_->adjust_for_dyn(info);
4130 return this;
4131 }
4132
4133 protected:
4134 /* the controlling expression */
4135 class CTcPrsNode *expr_;
4136
4137 /* body of the switch */
4138 class CTPNStm *body_;
4139
4140 /* number of case labels */
4141 int case_cnt_;
4142
4143 /* flag: we have a 'default' label */
4144 unsigned int has_default_ : 1;
4145 };
4146
4147
4148 /* ------------------------------------------------------------------------ */
4149 /*
4150 * code label statement - a code label contains the statement that it
4151 * labels. This is an enclosing statement type because targeting a
4152 * label with a 'goto', 'break', or 'continue' requires that we be able
4153 * to find intermediate 'try' blocks.
4154 */
4155 class CTPNStmLabelBase: public CTPNStmEnclosing
4156 {
4157 public:
4158 CTPNStmLabelBase(class CTcSymLabel *lbl, CTPNStmEnclosing *enclosing)
4159 : CTPNStmEnclosing(enclosing)
4160 {
4161 /* remember my label */
4162 lbl_ = lbl;
4163
4164 /* we have no enclosed statement yet */
4165 stm_ = 0;
4166
4167 /* we're not targeted with any 'break' or 'continue' statements yet */
4168 control_flow_flags_ = 0;
4169 }
4170
4171 /* set my enclosed statement */
4172 void set_stm(class CTPNStm *stm) { stm_ = stm; }
4173
4174 /* fold constants */
4175 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
4176
4177 /* evaluate control flow for the statement */
4178 virtual unsigned long get_control_flow(int warn) const;
4179
4180 /* I am a code label, thus I have a code label */
4181 virtual int has_code_label() const { return TRUE; }
4182
4183 /*
4184 * add explicit targeting flags (break, continue, goto) to my
4185 * statement
4186 */
4187 void add_control_flow_flags(ulong flags);
4188
4189 /* get my explicit control flow flag */
4190 ulong get_explicit_control_flow_flags() const
4191 { return control_flow_flags_; }
4192
4193 /* adjust for dynamic (run-time) compilation */
4194 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
4195 {
4196 stm_ = (CTPNStm *)adjust_for_dyn(info);
4197 return this;
4198 }
4199
4200 protected:
4201 /* my label */
4202 class CTcSymLabel *lbl_;
4203
4204 /* my enclosed statement */
4205 class CTPNStm *stm_;
4206
4207 /*
4208 * My explicit control flow flags. Any time this label appears in a
4209 * 'break' or 'continue' statement enclosed within the labeled
4210 * statement, we'll set the appropriate flags here. When charting
4211 * the control flow through the statement, we will take these into
4212 * account after charting the flow through our body.
4213 */
4214 ulong control_flow_flags_;
4215 };
4216
4217 /* ------------------------------------------------------------------------ */
4218 /*
4219 * 'goto' statement
4220 */
4221 class CTPNStmGotoBase: public CTPNStm
4222 {
4223 public:
4224 CTPNStmGotoBase(const textchar_t *lbl, size_t lbl_len)
4225 {
4226 /*
4227 * remember the label - we can store a reference to it without
4228 * copying, because the tokenizer keeps symbol token text around
4229 * throughout compilation
4230 */
4231 lbl_ = lbl;
4232 lbl_len_ = lbl_len;
4233 }
4234
4235 /* fold constants */
4236 class CTcPrsNode *fold_constants(class CTcPrsSymtab *)
4237 {
4238 /* 'goto' statements are unaffected by constant folding */
4239 return this;
4240 }
4241
4242 /* evaluate control flow for the statement */
4243 virtual unsigned long get_control_flow(int warn) const;
4244
4245 /* adjust for dynamic (run-time) compilation */
4246 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
4247 { return this; }
4248
4249 protected:
4250 /* our code label text */
4251 const textchar_t *lbl_;
4252 size_t lbl_len_;
4253 };
4254
4255
4256 /* ------------------------------------------------------------------------ */
4257 /*
4258 * 'case' label statement
4259 */
4260 class CTPNStmCaseBase: public CTPNStm
4261 {
4262 public:
4263 CTPNStmCaseBase()
4264 {
4265 /* we have no statement or expression yet */
4266 expr_ = 0;
4267 stm_ = 0;
4268 }
4269
4270 /* set my expression */
4271 void set_expr(class CTcPrsNode *expr) { expr_ = expr; }
4272
4273 /* set my enclosed statement */
4274 void set_stm(class CTPNStm *stm) { stm_ = stm; }
4275
4276 /* fold constants */
4277 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
4278
4279 /* evaluate control flow for the statement */
4280 virtual unsigned long get_control_flow(int warn) const;
4281
4282 /* I am a code label, thus I have a code label */
4283 virtual int has_code_label() const { return TRUE; }
4284
4285 /* adjust for dynamic (run-time) compilation */
4286 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
4287 {
4288 expr_ = expr_->adjust_for_dyn(info);
4289 stm_ = (CTPNStm *)stm_->adjust_for_dyn(info);
4290 return this;
4291 }
4292
4293 protected:
4294 /* my expression */
4295 class CTcPrsNode *expr_;
4296
4297 /* my enclosed statement */
4298 class CTPNStm *stm_;
4299 };
4300
4301 /* ------------------------------------------------------------------------ */
4302 /*
4303 * 'default' label statement
4304 */
4305 class CTPNStmDefaultBase: public CTPNStm
4306 {
4307 public:
4308 CTPNStmDefaultBase()
4309 {
4310 /* we have no statement yet */
4311 stm_ = 0;
4312 }
4313
4314 /* set my enclosed statement */
4315 void set_stm(class CTPNStm *stm) { stm_ = stm; }
4316
4317 /* fold constants */
4318 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
4319
4320 /* evaluate control flow for the statement */
4321 virtual unsigned long get_control_flow(int warn) const;
4322
4323 /* I am a code label, thus I have a code label */
4324 virtual int has_code_label() const { return TRUE; }
4325
4326 /* adjust for dynamic (run-time) compilation */
4327 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
4328 { return this; }
4329
4330 protected:
4331 /* my enclosed statement */
4332 class CTPNStm *stm_;
4333 };
4334
4335 /* ------------------------------------------------------------------------ */
4336 /*
4337 * 'try' statement
4338 */
4339 class CTPNStmTryBase: public CTPNStmEnclosing
4340 {
4341 public:
4342 CTPNStmTryBase(CTPNStmEnclosing *enclosing)
4343 : CTPNStmEnclosing(enclosing)
4344 {
4345 /* we have no protected body yet */
4346 body_stm_ = 0;
4347
4348 /* we have no 'catch' or 'finally' clause yet */
4349 first_catch_stm_ = last_catch_stm_ = 0;
4350 finally_stm_ = 0;
4351 }
4352
4353 /* set my protected statement */
4354 void set_body_stm(class CTPNStm *body_stm) { body_stm_ = body_stm; }
4355
4356 /* add a catch to my list */
4357 void add_catch(class CTPNStmCatch *catch_stm);
4358
4359 /* set the 'finally' clause */
4360 void set_finally(class CTPNStmFinally *finally_stm)
4361 { finally_stm_ = finally_stm; }
4362
4363 /* determine if I have any 'catch' or 'finally' clauses */
4364 int has_catch_or_finally() const
4365 {
4366 return (first_catch_stm_ != 0 || finally_stm_ != 0);
4367 }
4368
4369 /* fold constants */
4370 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
4371
4372 /* evaluate control flow for the statement list */
4373 virtual unsigned long get_control_flow(int warn) const;
4374
4375 /* adjust for dynamic (run-time) compilation */
4376 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
4377
4378 protected:
4379 /* my enclosed statement */
4380 class CTPNStm *body_stm_;
4381
4382 /* head and tail of my 'catch' clause list */
4383 class CTPNStmCatch *first_catch_stm_;
4384 class CTPNStmCatch *last_catch_stm_;
4385
4386 /* my 'finally' clause, if any */
4387 class CTPNStmFinally *finally_stm_;
4388 };
4389
4390 /* ------------------------------------------------------------------------ */
4391 /*
4392 * 'catch' clause
4393 */
4394 class CTPNStmCatchBase: public CTPNStm
4395 {
4396 public:
4397 CTPNStmCatchBase()
4398 {
4399 /* we have no statement yet */
4400 body_ = 0;
4401
4402 /* we don't know our exception class yet */
4403 exc_class_ = 0;
4404 exc_class_len_ = 0;
4405
4406 /* we don't know our exception variable yet */
4407 exc_var_ = 0;
4408
4409 /* we're not in a list yet */
4410 next_catch_ = 0;
4411
4412 /* we don't know our local symbol table yet */
4413 symtab_ = 0;
4414 }
4415
4416 /* set my enclosed statement */
4417 void set_body(class CTPNStm *body) { body_ = body; }
4418
4419 /* set my exception class name */
4420 void set_exc_class(const class CTcToken *tok);
4421
4422 /* set my exception variable */
4423 void set_exc_var(class CTcSymLocal *var) { exc_var_ = var; }
4424
4425 /* fold constants */
4426 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
4427
4428 /* evaluate control flow for the clause */
4429 virtual unsigned long get_control_flow(int warn) const;
4430
4431 /* get/set the next 'catch' clause in our list */
4432 CTPNStmCatch *get_next_catch() const { return next_catch_; }
4433 void set_next_catch(CTPNStmCatch *nxt) { next_catch_ = nxt; }
4434
4435 /* set my local-scope symbol table */
4436 void set_symtab(class CTcPrsSymtab *symtab) { symtab_ = symtab; }
4437
4438 /* adjust for dynamic (run-time) compilation */
4439 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
4440 {
4441 body_ = (CTPNStm *)body_->adjust_for_dyn(info);
4442 return this;
4443 }
4444
4445 protected:
4446 /* the body of the 'catch' block */
4447 class CTPNStm *body_;
4448
4449 /* exception name */
4450 const textchar_t *exc_class_;
4451 size_t exc_class_len_;
4452
4453 /* exception variable (formal parameter to 'catch' block) */
4454 class CTcSymLocal *exc_var_;
4455
4456 /* next 'catch' clause in our list */
4457 CTPNStmCatch *next_catch_;
4458
4459 /* my local-scope symbol table (for my formal parameter) */
4460 class CTcPrsSymtab *symtab_;
4461 };
4462
4463 /* ------------------------------------------------------------------------ */
4464 /*
4465 * 'finally' clause
4466 */
4467 class CTPNStmFinallyBase: public CTPNStmEnclosing
4468 {
4469 public:
4470 CTPNStmFinallyBase(CTPNStmEnclosing *enclosing,
4471 int exc_local_id, int jsr_local_id)
4472 : CTPNStmEnclosing(enclosing)
4473 {
4474 /* we have no body yet */
4475 body_ = 0;
4476
4477 /* remember our local variable ID's */
4478 exc_local_id_ = exc_local_id;
4479 jsr_local_id_ = jsr_local_id;
4480
4481 /* use my starting position as the ending position for now */
4482 end_desc_ = file_;
4483 end_linenum_ = linenum_;
4484 }
4485
4486 /* set my body */
4487 void set_body(class CTPNStm *stm) { body_ = stm; }
4488
4489 /*
4490 * set my ending source position (i.e., the position of the closing
4491 * brace of the block of code contained within the 'finally' block
4492 */
4493 void set_end_pos(class CTcTokFileDesc *desc, long linenum)
4494 {
4495 end_desc_ = desc;
4496 end_linenum_ = linenum;
4497 }
4498
4499 /* get the ending source position information */
4500 class CTcTokFileDesc *get_end_desc() const { return end_desc_; }
4501 long get_end_linenum() const { return end_linenum_; }
4502
4503 /* fold constants */
4504 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
4505
4506 /* evaluate control flow for the clause */
4507 virtual unsigned long get_control_flow(int warn) const;
4508
4509 /* adjust for dynamic (run-time) compilation */
4510 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
4511 {
4512 body_ = (CTPNStm *)body_->adjust_for_dyn(info);
4513 return this;
4514 }
4515
4516 protected:
4517 /* my body */
4518 class CTPNStm *body_;
4519
4520 /*
4521 * our exception local ID - this is an unnamed local variable that
4522 * we use to store our exception parameter temporarily while we run
4523 * the 'finally' block
4524 */
4525 int exc_local_id_;
4526
4527 /*
4528 * our subroutine return address local ID - this is another unnamed
4529 * local that we use to store our subroutine return address
4530 */
4531 int jsr_local_id_;
4532
4533 /*
4534 * source position of end of the 'finally' clause - we keep track of
4535 * this location because code generated after the return from
4536 * calling the 'finally' clause is most sensibly thought of as being
4537 * at this location
4538 */
4539 class CTcTokFileDesc *end_desc_;
4540 long end_linenum_;
4541 };
4542
4543 /* ------------------------------------------------------------------------ */
4544 /*
4545 * 'throw' statement
4546 */
4547 class CTPNStmThrowBase: public CTPNStm
4548 {
4549 public:
4550 CTPNStmThrowBase(class CTcPrsNode *expr)
4551 {
4552 /* remember the expression to be thrown */
4553 expr_ = expr;
4554 }
4555
4556 /* fold constants */
4557 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
4558
4559 /* evaluate control flow for the statement */
4560 virtual unsigned long get_control_flow(int /*warn*/) const
4561 {
4562 /* this statement always exits by 'throw' (obviously) */
4563 return TCPRS_FLOW_THROW;
4564 }
4565
4566 /* adjust for dynamic (run-time) compilation */
4567 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info)
4568 {
4569 expr_ = expr_->adjust_for_dyn(info);
4570 return this;
4571 }
4572
4573 protected:
4574 /* expression to throw */
4575 class CTcPrsNode *expr_;
4576 };
4577
4578 /* ------------------------------------------------------------------------ */
4579 /*
4580 * 'dictionary' statement
4581 */
4582 class CTPNStmDictBase: public CTPNStmTop
4583 {
4584 public:
4585 CTPNStmDictBase(class CTcDictEntry *dict)
4586 {
4587 /* remember the dictionary entry */
4588 dict_ = dict;
4589 }
4590
4591 /* fold constants - this has no effect on us */
4592 class CTcPrsNode *fold_constants(class CTcPrsSymtab *) { return this; }
4593
4594 protected:
4595 /* the underlying dictionary entry */
4596 class CTcDictEntry *dict_;
4597 };
4598
4599
4600 /* ------------------------------------------------------------------------ */
4601 /*
4602 * Parse node for object superclass list entries
4603 */
4604 class CTPNSuperclass: public CTcPrsNode
4605 {
4606 public:
4607 CTPNSuperclass(const textchar_t *sym, size_t len)
4608 {
4609 /* remember my name */
4610 sym_txt_ = sym;
4611 sym_len_ = len;
4612
4613 /* we have a name instead of a symbol object */
4614 sym_ = 0;
4615
4616 /* I'm not in a list yet */
4617 nxt_ = prv_ = 0;
4618 }
4619
4620 CTPNSuperclass(class CTcSymbol *sym)
4621 {
4622 /* remember my symbol */
4623 sym_ = sym;
4624
4625 /* we have the symbol, so we don't need to store the name */
4626 sym_txt_ = 0;
4627 sym_len_ = 0;
4628
4629 /* I'm not in a list yet */
4630 nxt_ = 0;
4631 }
4632
4633 /* fold constants */
4634 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
4635 { return this; }
4636
4637 /* generate code for the compound statement */
4638 virtual void gen_code(int discard, int for_condition) { }
4639
4640 /* get my symbol */
4641 class CTcSymbol *get_sym() const;
4642
4643 /* get my text - valid only if we are storing text, not a symbol */
4644 const char *get_sym_txt() const { return sym_txt_; }
4645 size_t get_sym_len() const { return sym_len_; }
4646
4647 /* am I a subclass of the given class? */
4648 int is_subclass_of(const CTPNSuperclass *sc) const;
4649
4650 /* next/previous entry in my list */
4651 CTPNSuperclass *nxt_, *prv_;
4652
4653 protected:
4654 /* my symbol - if this isn't set, we'll use sym_txt_ instead */
4655 class CTcSymbol *sym_;
4656
4657 /* my name - we use this if sym_ is not set */
4658 const textchar_t *sym_txt_;
4659 size_t sym_len_;
4660 };
4661
4662 /*
4663 * Superclass list
4664 */
4665 class CTPNSuperclassList
4666 {
4667 public:
4668 CTPNSuperclassList() { head_ = tail_ = 0; dst_ = &head_; }
4669
4670 void append(class CTPNSuperclass *sc)
4671 {
4672 /* set the 'next' pointer for the current tail */
4673 *dst_ = sc;
4674 *(dst_ = &sc->nxt_) = 0;
4675
4676 /* set the 'previous' pointer for the new entry */
4677 sc->prv_ = tail_;
4678 tail_ = sc;
4679 }
4680
4681 /* head and tail of our list */
4682 class CTPNSuperclass *head_;
4683 class CTPNSuperclass *tail_;
4684
4685 private:
4686 class CTPNSuperclass **dst_;
4687 };
4688
4689
4690 /*
4691 * Property list
4692 */
4693 class CTPNPropList
4694 {
4695 public:
4696 CTPNPropList() { first_ = last_ = 0; dst_ = &first_; cnt_ = 0; }
4697
4698 /* append a property to our list */
4699 void append(class CTPNObjProp *prop);
4700
4701 /* delete a property from our list; returns true if found, false if not */
4702 int del(class CTcSymProp *sym);
4703
4704 /* head/tail of our property list */
4705 class CTPNObjProp *first_;
4706 class CTPNObjProp *last_;
4707
4708 /* number of properties in the list */
4709 int cnt_;
4710
4711 private:
4712 /* next destination for append() */
4713 class CTPNObjProp **dst_;
4714 };
4715
4716
4717 /* ------------------------------------------------------------------------ */
4718 /*
4719 * Mix-in base for an object definer node. This is instantiated in the
4720 * top-level object statement class (CTPNStmObject) and the in-line object
4721 * definition expression node (CTPNInlineObject).
4722 */
4723 class CTPNObjDef
4724 {
4725 public:
4726 CTPNObjDef()
4727 {
4728 /* presume there's no object symbol */
4729 obj_sym_ = 0;
4730
4731 /* presume we won't have an undescribed superclass */
4732 undesc_sc_ = FALSE;
4733
4734 /* presume we won't have a template usage error */
4735 bad_template_ = FALSE;
4736 }
4737
4738 /* is this a regular object or an inline object? */
4739 virtual int is_inline_object() const = 0;
4740
4741 /* set the 'undescribed class' flag */
4742 int has_undesc_sc() const { return undesc_sc_; }
4743 void set_undesc_sc(int f) { undesc_sc_ = f; }
4744
4745 /* get/set the 'bad template' flag */
4746 int has_bad_template() const { return bad_template_; }
4747 void note_bad_template(int f) { bad_template_ = f; }
4748
4749 /* add a property value */
4750 class CTPNObjProp *add_prop(
4751 class CTcSymProp *prop_sym, class CTcPrsNode *expr,
4752 int replace, int is_static);
4753
4754 /*
4755 * Add a method. 'expr' is the original expression for cases where the
4756 * code body is derived from a simple expression in the source code
4757 * rather than an explicit code block. Keeping the original expression
4758 * allows us to use a simple constant value if the expression ends up
4759 * folding to a constant.
4760 */
4761 class CTPNObjProp *add_method(
4762 class CTcSymProp *prop_sym, class CTPNCodeBody *code_body,
4763 class CTcPrsNode *expr, int replace);
4764
4765 /*
4766 * Add a method to an inline object. The method in this case is
4767 * represented as an anonymous method object. 'expr' is the original
4768 * expression for cases where the anonymous function is derived from a
4769 * simple expression in the source code rather than an explicit code
4770 * block.
4771 */
4772 class CTPNObjProp *add_inline_method(
4773 class CTcSymProp *prop_sym, class CTPNAnonFunc *inline_method,
4774 class CTcPrsNode *expr, int replace);
4775
4776 /* add a property as a nested object value */
4777 virtual int parse_nested_obj_prop(
4778 class CTPNObjProp* &new_prop, int *err,
4779 struct tcprs_term_info *term_info,
4780 const class CTcToken *prop_tok, int replace) = 0;
4781
4782 /* delete a property */
4783 virtual void delete_property(class CTcSymProp *prop_sym)
4784 { proplist_.del(prop_sym); }
4785
4786 /* get my superclass list */
4787 CTPNSuperclassList &get_superclass_list() { return sclist_; }
4788
4789 /* get my first superclass */
4790 class CTPNSuperclass *get_first_sc() const { return sclist_.head_; }
4791
4792 /* get my first property */
4793 class CTPNObjProp *get_first_prop() const { return proplist_.first_; }
4794
4795 /* get the object symbol */
4796 class CTcSymObj *get_obj_sym() const { return obj_sym_; }
4797
4798 /*
4799 * set my object symbol - when an object is modified (via the 'modify'
4800 * statement), the object tree can be moved to a new symbol
4801 */
4802 void set_obj_sym(class CTcSymObj *obj_sym) { obj_sym_ = obj_sym; }
4803
4804 /* fold constants in the property list */
4805 void fold_proplist(class CTcPrsSymtab *symtab);
4806
4807 protected:
4808 /* add an entry to my property list */
4809 virtual void add_prop_entry(class CTPNObjProp *prop, int /*replace*/)
4810 { proplist_.append(prop); }
4811
4812 /* object name symbol */
4813 class CTcSymObj *obj_sym_;
4814
4815 /* property list */
4816 CTPNPropList proplist_;
4817
4818 /* superclass list */
4819 CTPNSuperclassList sclist_;
4820
4821 /*
4822 * Flag: this object definition includes an undescribed superclass.
4823 * This indicates that we're based on a class that was explicitly
4824 * defined as 'extern', in which case it can't be used as the source of
4825 * a template, since we know nothing about the class other than that it
4826 * is indeed a class.
4827 */
4828 unsigned int undesc_sc_ : 1;
4829
4830 /* flag: this object definition used a template that wasn't matched */
4831 unsigned int bad_template_ : 1;
4832 };
4833
4834
4835 /* ------------------------------------------------------------------------ */
4836 /*
4837 * object definition statement base class
4838 */
4839 class CTPNStmObjectBase: public CTPNStmTop, public CTPNObjDef
4840 {
4841 public:
4842 CTPNStmObjectBase(class CTcSymObj *obj_sym, int is_class)
4843 {
4844 /* remember our defining global symbol */
4845 obj_sym_ = obj_sym;
4846
4847 /* we're not yet replaced by another object */
4848 replaced_ = FALSE;
4849
4850 /* we're not yet modified */
4851 modified_ = FALSE;
4852
4853 /* note whether I'm a class or an ordinary object instance */
4854 is_class_ = is_class;
4855
4856 /* presume it's not transient */
4857 transient_ = FALSE;
4858 }
4859
4860 /* this is a regular top-level object definition */
4861 virtual int is_inline_object() const { return FALSE; }
4862
4863 /*
4864 * mark the object as replaced - this indicates that another object
4865 * in the same translation unit has replaced this object, hence we
4866 * should not generate any code for this object
4867 */
4868 void set_replaced(int f) { replaced_ = f; }
4869
4870 /*
4871 * Mark the object as modified - this indicates that another object
4872 * in the same translation unit has modified this object. We'll
4873 * store this information in the object stream header data for use
4874 * at link time.
4875 */
4876 void set_modified(int f) { modified_ = f; }
4877
4878 /* add a superclass with the given name or symbol */
4879 void add_superclass(const class CTcToken *tok);
4880 void add_superclass(class CTcSymbol *sym);
4881
4882 /* determine if I'm a class object */
4883 int is_class() const { return (is_class_ != 0); }
4884
4885 /*
4886 * Delete a property value. This is used when a 'modify' object
4887 * defines a property with 'replace', so that the property defined in
4888 * the modified original object is removed entirely rather than left in
4889 * as an inherited property.
4890 */
4891 virtual void delete_property(class CTcSymProp *prop_sym);
4892
4893 /*
4894 * Add an implicit constructor. This should be called just before
4895 * code generation; we'll check to see if the object requires an
4896 * implicit constructor, and add one to the object if so. An object
4897 * provides an implicit constructor if it has multiple superclasses
4898 * and no explicit constructor.
4899 */
4900 void add_implicit_constructor();
4901
4902 /* get/set the 'transient' status */
4903 int is_transient() const { return transient_; }
4904 void set_transient() { transient_ = TRUE; }
4905
4906 /* add a property as a nested object value */
4907 virtual int parse_nested_obj_prop(
4908 class CTPNObjProp* &new_prop, int *err,
4909 struct tcprs_term_info *term_info,
4910 const class CTcToken *prop_tok, int replace);
4911
4912 /* fold constants */
4913 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
4914 {
4915 CTPNObjDef::fold_proplist(symtab);
4916 return this;
4917 }
4918
4919 protected:
4920 /* add an entry to my property list */
4921 virtual void add_prop_entry(class CTPNObjProp *prop, int replace);
4922
4923 /* flag: I'm a class */
4924 unsigned int is_class_ : 1;
4925
4926 /* flag: I've been replaced by another object */
4927 unsigned int replaced_ : 1;
4928
4929 /* flag: I've been modified by another object */
4930 unsigned int modified_ : 1;
4931
4932 /* flag: the object is transient */
4933 unsigned int transient_ : 1;
4934 };
4935
4936 /* ------------------------------------------------------------------------ */
4937 /*
4938 * In-line object definition. This represents an object defined in-line as
4939 * part of an expression. This type of object definition creates a new
4940 * instance of the specified object when the expression is evaluated, using
4941 * anonymous methods for code properties. (Anonymous methods are the same
4942 * as anonymous functions, aka closures, except that they bind only to the
4943 * local variables in the lexically enclosing frame but not to the 'self'
4944 * context.) Using anonymous methods allows methods defined in the object
4945 * to read and write the lexically enclosing frame's local variables, so
4946 * that the entire object acts like a closure.
4947 */
4948 class CTPNInlineObjectBase: public CTcPrsNode, public CTPNObjDef
4949 {
4950 public:
4951 /* fold constants */
4952 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab)
4953 {
4954 CTPNObjDef::fold_proplist(symtab);
4955 return this;
4956 }
4957
4958 /* this is an inline object definition */
4959 virtual int is_inline_object() const { return TRUE; }
4960
4961 /* parse a nested object property */
4962 virtual int parse_nested_obj_prop(
4963 class CTPNObjProp* &new_prop, int *err,
4964 struct tcprs_term_info *term_info,
4965 const class CTcToken *prop_tok, int replace);
4966
4967 /* adjust for dynamic execution */
4968 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
4969 };
4970
4971 /* ------------------------------------------------------------------------ */
4972 /*
4973 * Parse node entry for a property value or method entry in an object
4974 * definition statement's property list. We can have either an
4975 * expression entry or a code body entry; we keep open the possibility
4976 * of either one in this single class (rather than subclassing for the
4977 * two possibilities) because we might have to transform from an
4978 * expression to a code body if we find a non-constant expression, which
4979 * we can't determine until constant-folding time.
4980 */
4981 class CTPNObjPropBase: public CTPNStm
4982 {
4983 friend class CTPNObjDef;
4984 friend class CTPNStmObjectBase;
4985 friend class CTPNStmObject;
4986 friend class CTPNPropList;
4987
4988 public:
4989 CTPNObjPropBase(class CTPNObjDef *objdef, class CTcSymProp *prop_sym,
4990 class CTcPrsNode *expr, class CTPNCodeBody *code_body,
4991 class CTPNAnonFunc *inline_method, int is_static)
4992 {
4993 /* remember the object and property information */
4994 objdef_ = objdef;
4995 prop_sym_ = prop_sym;
4996
4997 /* not in a property list yet */
4998 nxt_ = 0;
4999
5000 /* remember our expression and code body values */
5001 expr_ = expr;
5002 code_body_ = code_body;
5003 inline_method_ = inline_method;
5004
5005 /* remember if it's static */
5006 is_static_ = is_static;
5007
5008 /* by default, properties cannot be overwritten with redefinitions */
5009 is_overwritable_ = FALSE;
5010 }
5011
5012 /* get my property symbol */
5013 class CTcSymProp *get_prop_sym() const { return prop_sym_; }
5014
5015 /* fold constants */
5016 class CTcPrsNode *fold_constants(class CTcPrsSymtab *symtab);
5017
5018 /* adjust for dynamic execution */
5019 class CTcPrsNode *adjust_for_dyn(const tcpn_dyncomp_info *info);
5020
5021 /* get the next property of this object */
5022 class CTPNObjProp *get_next_prop() const { return nxt_; }
5023
5024 /* am I a constructor? */
5025 int is_constructor() const;
5026
5027 /*
5028 * Set/get flag indicating that we're overwritable. An overwritable
5029 * property can be superseded with a duplicate definition of the same
5030 * property in the same object. By default, it's an error to define
5031 * the same property more than once in the same object, but properties
5032 * defined with certain implicit notation can be overwritten with an
5033 * explicit redefinition. Specifically, a 'location' property added
5034 * with the '+' notation can be overwritten, and the automatic
5035 * 'sourceTextOrder' and 'sourceTextGroup' properties can be
5036 * overwritten.
5037 */
5038 int is_overwritable() const { return is_overwritable_; }
5039 void set_overwritable() { is_overwritable_ = TRUE; }
5040
5041 protected:
5042 /* my object statement */
5043 class CTPNObjDef *objdef_;
5044
5045 /* my property symbol */
5046 class CTcSymProp *prop_sym_;
5047
5048 /* next property in my list */
5049 class CTPNObjProp *nxt_;
5050
5051 /* my value expression */
5052 class CTcPrsNode *expr_;
5053
5054 /* symbol table in effect for this expression */
5055 class CTcPrsSymtab *symtab_;
5056
5057 /* my code body node */
5058 class CTPNCodeBody *code_body_;
5059
5060 /* for in-line objects, code is represented as an anonymous method */
5061 class CTPNAnonFunc *inline_method_;
5062
5063 /* am I static? */
5064 unsigned int is_static_ : 1;
5065
5066 /* am I overwritable? */
5067 unsigned int is_overwritable_ : 1;
5068 };
5069
5070 /*
5071 * Deleted property object. This is used for an entry in a deleted
5072 * property list for a 'modify' object.
5073 */
5074 class CTcObjPropDel: public CTcPrsAllocObj
5075 {
5076 public:
5077 CTcObjPropDel(class CTcSymProp *prop_sym)
5078 {
5079 /* save the property information */
5080 prop_sym_ = prop_sym;
5081
5082 /* not in a list yet */
5083 nxt_ = 0;
5084 }
5085
5086 /* the property to delete */
5087 class CTcSymProp *prop_sym_;
5088
5089 /* next in list */
5090 CTcObjPropDel *nxt_;
5091 };
5092
5093 /*
5094 * Implicit constructor statement.
5095 */
5096 class CTPNStmImplicitCtorBase: public CTPNStm
5097 {
5098 public:
5099 CTPNStmImplicitCtorBase(class CTPNStmObject *obj_stm)
5100 {
5101 /* remember my object statement */
5102 obj_stm_ = obj_stm;
5103 }
5104
5105 /* fold constants */
5106 class CTcPrsNode *fold_constants(class CTcPrsSymtab *) { return this; }
5107
5108 protected:
5109 /* my object statement */
5110 class CTPNStmObject *obj_stm_;
5111 };
5112
5113 #endif /* TCPNDRV_H */
5114