Debian Codegraph @

terps/tads/tads3/tcprsstm.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/tads3/TCPRSSTM.CPP,v 1.1 1999/07/11 00:46:54 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1999, 2010 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 tcprs.cpp - TADS 3 Compiler Parser - statement node classes
15 Function
16 This parser module includes statement node classes that are needed for
17 parsing executable code bodies - the insides of functions and methods.
18 This doesn't include the outer program-level structures, such as object
19 definitions, or the inner expression structures.
20
21 This code is needed for interpreters that include "eval()" functionality,
22 for dynamic construction of new executable code.
23 Notes
24
25 Modified
26 04/30/99 MJRoberts - Creation
27 */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 #include "os.h"
34 #include "t3std.h"
35 #include "utf8.h"
36 #include "tcprs.h"
37 #include "tctarg.h"
38 #include "tcgen.h"
39 #include "vmhash.h"
40 #include "tcmain.h"
41 #include "vmfile.h"
42 #include "tcmake.h"
43
44
45
46 /* ------------------------------------------------------------------------ */
47 /*
48 * Clear the local anonymous function shared context information
49 */
50 void CTcParser::clear_local_ctx()
51 {
52 /* we don't have a local context */
53 has_local_ctx_ = FALSE;
54
55 /* there is no local context local yet */
56 local_ctx_var_num_ = 0;
57
58 /* no variable properties are used */
59 ctx_var_props_used_ = 0;
60
61 /*
62 * start the context array index at the next entry after the slot we
63 * always use to store the method context of the enclosing lexical
64 * scope
65 */
66 next_ctx_arr_idx_ = TCPRS_LOCAL_CTX_METHODCTX + 1;
67
68 /* we haven't yet referenced 'self' or any other method context yet */
69 self_referenced_ = FALSE;
70 full_method_ctx_referenced_ = FALSE;
71
72 /* we don't yet need the method context in the local context */
73 local_ctx_needs_self_ = FALSE;
74 local_ctx_needs_full_method_ctx_ = FALSE;
75 }
76
77 /*
78 * Set up the current code body with a local variable context
79 */
80 void CTcParser::init_local_ctx()
81 {
82 /* if I already have a local variable context, there's nothing to do */
83 if (has_local_ctx_)
84 return;
85
86 /* note that we now require a local variable context object */
87 has_local_ctx_ = TRUE;
88
89 /* create the local context variable */
90 local_ctx_var_num_ = alloc_local();
91 }
92
93 /*
94 * Allocate a context variable index
95 */
96 int CTcParser::alloc_ctx_arr_idx()
97 {
98 return next_ctx_arr_idx_++;
99 }
100
101 /*
102 * enumeration callback context
103 */
104 struct enum_locals_ctx
105 {
106 /* symbol table containing context locals */
107 CTcPrsSymtab *symtab;
108
109 /* code body */
110 CTPNCodeBody *code_body;
111 };
112
113 /*
114 * Enumeration callback - find local variables inherited from enclosing
115 * scopes (for anonymous functions)
116 */
117 void CTcParser::enum_for_ctx_locals(void *ctx0, CTcSymbol *sym)
118 {
119 /* cast the context */
120 enum_locals_ctx *ctx = (enum_locals_ctx *)ctx0;
121
122 /* tell this symbol to apply its local variable conversion */
123 sym->apply_ctx_var_conv(ctx->symtab, ctx->code_body);
124 }
125
126 /*
127 * Set up the local context for access to enclosing scope locals. Call
128 * this after parsing a code body to set up the hooks between the new code
129 * body's proxy symbol table for enclosing scopes and the actual enclosing
130 * local scopes.
131 */
132 void CTcParser::finish_local_ctx(CTPNCodeBody *cb, CTcPrsSymtab *local_symtab)
133 {
134 /* if we have a local context, mark the code body accordingly */
135 if (has_local_ctx_)
136 cb->set_local_ctx(local_ctx_var_num_, next_ctx_arr_idx_ - 1);
137
138 /*
139 * If the caller passed in a local symbol table, check the table for
140 * context variables from enclosing scopes, and assign the local holder
141 * for each such variable.
142 */
143 if (local_symtab != 0)
144 {
145 /*
146 * consider only the outermost local table, since that's where the
147 * shared locals reside
148 */
149 CTcPrsSymtab *tab, *par;
150 for (tab = local_symtab_, par = tab->get_parent() ;
151 par != 0 && par != global_symtab_ ;
152 tab = par, par = par->get_parent()) ;
153
154 /* enumerate the variables */
155 enum_locals_ctx ctx;
156 ctx.symtab = tab;
157 ctx.code_body = cb;
158 tab->enum_entries(&enum_for_ctx_locals, &ctx);
159 }
160 }
161
162 /* ------------------------------------------------------------------------ */
163 /*
164 * Parse a formal parameter list. If 'count_only' is true, we're only
165 * interested in counting the formals, and we don't bother adding them
166 * to any symbol table. If 'opt_allowed' is true, a parameter name can
167 * be followed by a question mark token to mark the parameter as
168 * optional.
169 *
170 * '*argc' returns with the number of parameters. 'opt_argc' can be
171 * null; if it's not null, '*opt_argc' returns with the number of
172 * parameters marked as optional.
173 *
174 * 'base_formal_num' is the starting formal parameter number to use in
175 * creating symbol table entries. This is meaningful only if
176 * 'count_only' is false.
177 *
178 * The caller must already have checked for an open paren and skipped
179 * it.
180 */
181 void CTcParser::parse_formal_list(
182 int count_only, int opt_allowed,
183 int *argc, int *opt_argc, int *varargs,
184 int *varargs_list, CTcSymLocal **varargs_list_local,
185 int *err, int base_formal_num, int for_short_anon_func,
186 CTcFormalTypeList **type_list)
187 {
188 CTcSymLocal *lcl = 0;
189 CTcToken varname;
190 int named_param = FALSE, opt_param = FALSE;
191 int is_typed = FALSE;
192 int defval_cnt = 0;
193
194 /*
195 * choose the end token - if this is a normal argument list, the
196 * ending token is a right parenthesis; for a short-form anonymous
197 * function, it's a colon
198 */
199 tc_toktyp_t end_tok = (for_short_anon_func ? TOKT_COLON : TOKT_RPAR);
200 int missing_end_tok_err = (for_short_anon_func
201 ? TCERR_MISSING_COLON_FORMAL
202 : TCERR_MISSING_RPAR_FORMAL);
203
204 /* no arguments yet */
205 *argc = 0;
206 if (opt_argc != 0)
207 *opt_argc = 0;
208 *varargs = FALSE;
209 *varargs_list = FALSE;
210
211 /* no error yet */
212 *err = FALSE;
213
214 /* we've only just begun */
215 int done = FALSE;
216
217 /* check for an empty list */
218 if (G_tok->cur() == end_tok)
219 {
220 /* the list is empty - we're already done */
221 done = TRUE;
222
223 /* skip the closing token */
224 G_tok->next();
225 }
226
227 /* keep going until done */
228 while (!done)
229 {
230 tc_toktyp_t suffix_tok = TOKT_INVALID;
231 CTcPrsNode *defval_expr = 0;
232
233 /* see what comes next */
234 switch(G_tok->cur())
235 {
236 case TOKT_ELLIPSIS:
237 /* it's an ellipsis - note that we have varargs */
238 *varargs = TRUE;
239
240 parse_ellipsis:
241 /* add the varargs indicator to the formal type list */
242 if (type_list != 0 && *type_list != 0)
243 (*type_list)->add_ellipsis();
244
245 /* the next token must be the close paren */
246 if (G_tok->next() == end_tok)
247 {
248 /* we've reached the end of the list */
249 goto handle_end_tok;
250 }
251 else
252 {
253 /* this is an error - guess about the problem */
254 switch(G_tok->cur())
255 {
256 case TOKT_COMMA:
257 /*
258 * we seem to have more in the list - log an error,
259 * but continue parsing the list
260 */
261 G_tok->next();
262 G_tok->log_error(TCERR_ELLIPSIS_NOT_LAST);
263 break;
264
265 default:
266 /*
267 * anything else is probably a missing right paren -
268 * provide it and exit the formal list
269 */
270 done = TRUE;
271 G_tok->log_error_curtok(missing_end_tok_err);
272 break;
273 }
274 }
275 break;
276
277 case TOKT_LBRACK:
278 /*
279 * varargs with named list variable for last parameter -
280 * this generates setup code that stores the arguments from
281 * this one forward in a list to be stored in this variable
282 */
283
284 /* note that we have varargs */
285 *varargs = TRUE;
286
287 /* note it in the type list as well */
288 if (type_list != 0 && *type_list != 0)
289 (*type_list)->add_ellipsis();
290
291 /* skip the bracket and check that a symbol follows */
292 switch(G_tok->next())
293 {
294 case TOKT_RBRACK:
295 /*
296 * empty brackets - treat this as identical to an
297 * ellipsis; since they didn't name the varargs list
298 * parameter, it's just a varargs indication
299 */
300 goto parse_ellipsis;
301
302 case TOKT_SYM:
303 /* if we're creating a symbol table, add the symbol */
304 if (!count_only)
305 {
306 int local_id;
307
308 /* create a local scope if we haven't already */
309 create_scope_local_symtab();
310
311 /* note that we have a varargs list parameter */
312 *varargs_list = TRUE;
313
314 /*
315 * insert the new variable as a local - it's not a
316 * formal, since we're going to take the actuals,
317 * make a list, and store them in this local; the
318 * formal in this position will not be named, since
319 * this is a varargs function
320 */
321 local_id = alloc_local();
322 *varargs_list_local = local_symtab_->add_local(
323 G_tok->getcur()->get_text(),
324 G_tok->getcur()->get_text_len(),
325 local_id, FALSE, TRUE, FALSE);
326
327 /* mark it as a list parameter */
328 (*varargs_list_local)->set_list_param(TRUE);
329 }
330
331 /* a close bracket must follow */
332 switch (G_tok->next())
333 {
334 case TOKT_RBRACK:
335 /* skip the close bracket, and check for the end token */
336 if (G_tok->next() == end_tok)
337 {
338 /* that's it - the list is done */
339 goto handle_end_tok;
340 }
341
342 /* we didn't find the end token - guess about the error */
343 switch (G_tok->cur())
344 {
345 case TOKT_COMMA:
346 /*
347 * they seem to want another parameter - this is
348 * an error, but keep parsing anyway
349 */
350 G_tok->log_error(TCERR_LISTPAR_NOT_LAST);
351 break;
352
353 case TOKT_LBRACE:
354 case TOKT_RBRACE:
355 case TOKT_LPAR:
356 case TOKT_EOF:
357 /* the ending token was probably just forgotten */
358 G_tok->log_error_curtok(missing_end_tok_err);
359
360 /* presume that the argument list is done */
361 done = TRUE;
362 break;
363
364 default:
365 /* end token is missing - log an error */
366 G_tok->log_error_curtok(missing_end_tok_err);
367
368 /*
369 * skip the errant token; if the ending token
370 * follows this one, skip it as well, since
371 * there's probably just a stray extra token
372 * before the ending token
373 */
374 if (G_tok->next() == end_tok)
375 G_tok->next();
376
377 /* presume they simply left out the paren */
378 done = TRUE;
379 break;
380 }
381 break;
382
383 case TOKT_LBRACE:
384 case TOKT_RBRACE:
385 case TOKT_EOF:
386 /* they must have left out the bracket and paren */
387 G_tok->log_error_curtok(TCERR_LISTPAR_REQ_RBRACK);
388 done = TRUE;
389 break;
390
391 default:
392 /* note the missing bracket */
393 G_tok->log_error_curtok(TCERR_LISTPAR_REQ_RBRACK);
394
395 /* skip the errant token and assume we're done */
396 G_tok->next();
397 done = TRUE;
398 break;
399 }
400 break;
401
402 default:
403 /* log an error */
404 G_tok->log_error_curtok(TCERR_LISTPAR_REQ_SYM);
405
406 /* if this is the ending token, we're done */
407 if (G_tok->cur() == end_tok)
408 goto handle_end_tok;
409
410 /*
411 * if we're not on something that looks like the end of
412 * the parameter list, skip the errant token
413 */
414 switch(G_tok->cur())
415 {
416 case TOKT_RBRACE:
417 case TOKT_LBRACE:
418 case TOKT_EOF:
419 /* looks like they left out the closing markers */
420 done = TRUE;
421 break;
422
423 default:
424 /*
425 * skip the errant token and continue - they
426 * probably have more argument list following
427 */
428 G_tok->next();
429 break;
430 }
431 break;
432 }
433 break;
434
435 case TOKT_SYM:
436 /*
437 * If a formal type list is allowed, check to see if the next
438 * token is a symbol - if it is, the current token is the type,
439 * and the next token is the variable name.
440 */
441 is_typed = FALSE;
442 if (type_list != 0)
443 {
444 /* save the initial token */
445 CTcToken init_tok = *G_tok->copycur();
446
447 /* check the next one */
448 if (G_tok->next() == TOKT_SYM)
449 {
450 /*
451 * We have another symbol, so the first token is the
452 * type name, and the current token is the param name.
453 * Add the type to the formal type list.
454 */
455
456 /* first, make sure we've created the type list */
457 if (*type_list == 0)
458 {
459 /*
460 * We haven't created one yet - create it now.
461 * Since we're just getting around to creating the
462 * list, add untyped elements for the parameters
463 * we've seen so far - they all must have been
464 * untyped, since we create the list on
465 * encountering the first typed parameter.
466 */
467 *type_list = new (G_prsmem) CTcFormalTypeList();
468 (*type_list)->add_untyped_params(*argc);
469 }
470
471 /* add this typed parameter */
472 (*type_list)->add_typed_param(&init_tok);
473
474 /* flag it as typed */
475 is_typed = TRUE;
476
477 /*
478 * make sure the type symbol is defined as a class or
479 * object
480 */
481 CTcSymbol *clsym = global_symtab_->find(
482 init_tok.get_text(), init_tok.get_text_len());
483 if (clsym == 0)
484 {
485 /* it's not defined - add it as an external object */
486 clsym = new CTcSymObj(
487 init_tok.get_text(), init_tok.get_text_len(),
488 FALSE, G_cg->new_obj_id(), TRUE,
489 TC_META_TADSOBJ, 0);
490
491 /* mark it as referenced */
492 clsym->mark_referenced();
493
494 /* add it to the table */
495 global_symtab_->add_entry(clsym);
496 }
497 else if (clsym->get_type() != TC_SYM_OBJ
498 && clsym->get_type() != TC_SYM_METACLASS)
499 {
500 /* it's something other than an object */
501 G_tok->log_error(TCERR_MMPARAM_NOT_OBJECT,
502 (int)init_tok.get_text_len(),
503 init_tok.get_text());
504 }
505 }
506 else
507 {
508 /* there's no type name - back up to the param name */
509 G_tok->unget();
510
511 /*
512 * We need to add an untyped element to the type list,
513 * but don't do so quite yet - we need to make sure
514 * that this isn't a named argument parameter. Only
515 * positional elements go in the type list. For now,
516 * simply flag it as non-typed.
517 */
518 is_typed = FALSE;
519 }
520 }
521
522 /* remember and skip the symbol name */
523 varname = *G_tok->getcur();
524 G_tok->next();
525
526 /* check for a colon, which might indicate a named parameter */
527 named_param = FALSE;
528 if (G_tok->cur() == TOKT_COLON)
529 {
530 /* it looks like a named param so far */
531 named_param = TRUE;
532
533 /*
534 * There's one case where a colon *doesn't* indicate a
535 * named parameter, which is when it's the closing colon of
536 * a short-form anonymous function parameter list. For
537 * those, the colon is a name marker only if it's followed
538 * by a continuation of the formal list: specifically,
539 * another parameter (so, a comma), or ':' to indicate the
540 * actual end of the list. If anything else follows, the
541 * colon is the terminator rather than part of the formal
542 * list.
543 */
544 if (for_short_anon_func)
545 {
546 /* check the next token */
547 switch (G_tok->next())
548 {
549 case TOKT_COMMA:
550 case TOKT_COLON:
551 /*
552 * More list follows, so this is indeed a named
553 * parameter marker.
554 */
555 break;
556
557 default:
558 /*
559 * We don't have more list, so this colon is the
560 * end marker for the formal list. Put it back and
561 * continue.
562 */
563 named_param = FALSE;
564 G_tok->unget();
565 break;
566 }
567 }
568 else
569 {
570 /*
571 * for regular argument lists, this can only be a named
572 * parameter indicator; skip it
573 */
574 G_tok->next();
575 }
576
577 /* named parameters can't be typed */
578 if (named_param && is_typed)
579 {
580 G_tok->log_error(TCERR_NAMED_ARG_NO_TYPE,
581 (int)varname.get_text_len(),
582 varname.get_text());
583 }
584 }
585
586 /*
587 * check for an optionality flag (a '?' suffix) or a default
588 * value ('= <expression>')
589 */
590 opt_param = FALSE;
591 suffix_tok = G_tok->cur();
592 defval_expr = 0;
593 if (opt_allowed
594 && (suffix_tok == TOKT_QUESTION || suffix_tok == TOKT_EQ))
595 {
596 /* it's optional */
597 opt_param = TRUE;
598
599 /* count the optional argument */
600 if (opt_argc != 0 && !named_param)
601 ++(*opt_argc);
602
603 /* skip the suffix token */
604 G_tok->next();
605
606 /* if it was an '=', parse the default value expression */
607 if (suffix_tok == TOKT_EQ)
608 {
609 /*
610 * parse the expression - comma has precedence in this
611 * context as an argument separator, so we need to
612 * parse this as an assignment expression (this is for
613 * precedence reasons, NOT because it's syntactically
614 * like an assignment - the '=' postfix to the argument
615 * name isn't part of the expression we're parsing)
616 */
617 defval_expr = parse_asi_expr();
618 }
619 }
620
621 /*
622 * if any previous arguments are optional, all subsequent
623 * arguments must be optional as well
624 */
625 if (opt_allowed
626 && opt_argc != 0 && *opt_argc != 0
627 && !opt_param && !named_param)
628 {
629 /* flag it as an error */
630 G_tok->log_error(TCERR_ARG_MUST_BE_OPT,
631 (int)varname.get_text_len(),
632 varname.get_text());
633 }
634
635 /* if we're creating symbol table entries, add this symbol */
636 lcl = 0;
637 if (!count_only)
638 {
639 /*
640 * create a new local symbol table if we don't already have
641 * one
642 */
643 create_scope_local_symtab();
644
645 /*
646 * Insert the new variable for the formal. If it's a named
647 * parameter or an optional parameter, it's not a real
648 * formal, since it doesn't point directly to a parameter
649 * slot in the stack frame - it points instead to a regular
650 * local variable slot that we have to set up with
651 * generated code in the function prolog.
652 */
653 if (named_param || opt_param)
654 {
655 /* it's a pseudo-formal - create a local for it */
656 lcl = local_symtab_->add_local(
657 varname.get_text(), varname.get_text_len(),
658 alloc_local(), FALSE, FALSE, FALSE);
659
660 /* set the special flags */
661 lcl->set_named_param(named_param);
662 lcl->set_opt_param(opt_param);
663
664 /* remember the default value expression, if any */
665 if (defval_expr != 0)
666 lcl->set_defval_expr(defval_expr, defval_cnt++);
667 }
668 else
669 {
670 /* insert the new local variable for the formal */
671 lcl = local_symtab_->add_formal(
672 varname.get_text(), varname.get_text_len(),
673 base_formal_num + *argc, FALSE);
674 }
675
676 /*
677 * If it's a position parameter, set its parameter index.
678 * Since optional parameters are actually stored as local
679 * variables, we need to separately track the parameter
680 * list index so that we can load the value into the local
681 * on function entry.
682 */
683 if (!named_param && lcl != 0)
684 lcl->set_param_index(*argc);
685 }
686
687 /*
688 * If it's not a named parameter, count it. Named parameters
689 * don't count as formals since they're not part of the
690 * positional list and aren't in the parameter area of the
691 * stack frame.
692 */
693 if (!named_param)
694 {
695 /* count it in the positional list */
696 ++(*argc);
697
698 /* if it was untyped, add it to the type list */
699 if (type_list != 0 && *type_list != 0 && !is_typed)
700 (*type_list)->add_untyped_param();
701 }
702
703 /* check for the closing token */
704 if (G_tok->cur() == end_tok)
705 {
706 /* it's the closing token - skip it and stop scanning */
707 goto handle_end_tok;
708 }
709
710 /* check what follows */
711 switch (G_tok->cur())
712 {
713 case TOKT_COMMA:
714 /* skip the comma and continue */
715 G_tok->next();
716 break;
717
718 case TOKT_SEM:
719 case TOKT_RBRACE:
720 case TOKT_LBRACE:
721 case TOKT_EQ:
722 case TOKT_EOF:
723 /*
724 * We've obviously left the list - the problem is
725 * probably that we're missing the right paren. Catch
726 * it in the main loop.
727 */
728 break;
729
730 case TOKT_SYM:
731 /*
732 * they seem to have left out a comma - keep parsing
733 * from the symbol token
734 */
735 G_tok->log_error_curtok(TCERR_REQ_COMMA_FORMAL);
736 break;
737
738 default:
739 /* anything else is an error */
740 G_tok->log_error_curtok(TCERR_REQ_COMMA_FORMAL);
741
742 /* skip the errant token and continue */
743 G_tok->next();
744 break;
745 }
746
747 /* done with the formal */
748 break;
749
750 case TOKT_SEM:
751 case TOKT_RBRACE:
752 case TOKT_LBRACE:
753 case TOKT_EOF:
754 /*
755 * We've obviously left the list - the problem is probably
756 * that we're missing the right paren. Log an error and
757 * stop scanning.
758 */
759 G_tok->log_error_curtok(missing_end_tok_err);
760 done = TRUE;
761 break;
762
763 default:
764 /* check to see if it's the ending token */
765 if (G_tok->cur() == end_tok)
766 {
767 /*
768 * they seem to have put in a comma followed by the
769 * ending token - it's probably just a stray extra comma
770 */
771 G_tok->log_error(TCERR_MISSING_LAST_FORMAL);
772
773 /* skip the paren and stop scanning */
774 G_tok->next();
775 done = TRUE;
776 break;
777 }
778
779 /*
780 * If this is a short-form anonymous function's parameter
781 * list, they probably forgot the colon - generate a more
782 * specific error for this case, and assume the list ends
783 * here.
784 */
785 if (for_short_anon_func)
786 {
787 /* tell them they left out the colon */
788 G_tok->log_error_curtok(TCERR_MISSING_COLON_FORMAL);
789
790 /* presume the argument list was meant to end here */
791 done = TRUE;
792 break;
793 }
794
795 /*
796 * anything else is probably just an extraneous token; skip
797 * it and go on
798 */
799 G_tok->log_error_curtok(TCERR_REQ_SYM_FORMAL);
800 G_tok->next();
801 break;
802
803 handle_end_tok:
804 /* we've reached the end token - skip it, and we're done */
805 G_tok->next();
806 done = TRUE;
807 break;
808 }
809 }
810
811 /*
812 * Check for the "multimethod" modifier, if allowed. It's allowed if a
813 * formal type list is allowed and this is a normal full parameter list
814 * (i.e., specified in parentheses rather than as an anonymous function
815 * formal list).
816 */
817 if (G_tok->cur() == TOKT_SYM
818 && G_tok->cur_tok_matches("multimethod", 11))
819 {
820 /* make sure it's allowed */
821 if (type_list != 0 && end_tok == TOKT_RPAR)
822 {
823 /* it's allowed - create the type list if we haven't already */
824 if (*type_list == 0)
825 {
826 /* create the type list */
827 *type_list = new (G_prsmem) CTcFormalTypeList();
828
829 /* add untyped parameters for the ones we've defined */
830 (*type_list)->add_untyped_params(*argc);
831
832 /* mark it as varargs if applicable */
833 if (*varargs)
834 (*type_list)->add_ellipsis();
835 }
836 }
837 else
838 {
839 /* it's not allowed - flag it as an error */
840 G_tok->log_error(TCERR_MULTIMETHOD_NOT_ALLOWED);
841 }
842
843 /* skip the token */
844 G_tok->next();
845 }
846
847 /* deduct the optional arguments from the fixed arguments */
848 if (opt_argc != 0)
849 *argc -= *opt_argc;
850 }
851
852
853 /* ------------------------------------------------------------------------ */
854 /*
855 * Parse a nested code body (such as an anonymous function code body)
856 */
857 CTPNCodeBody *CTcParser::parse_nested_code_body(
858 int eq_before_brace, int self_valid,
859 int *p_argc, int *p_opt_argc, int *p_varargs,
860 int *p_varargs_list, CTcSymLocal **p_varargs_list_local,
861 int *p_has_retval, int *err, CTcPrsSymtab *local_symtab,
862 tcprs_codebodytype cb_type)
863 {
864 /* remember the original parser state */
865 CTcPrsSymtab *old_local_symtab = local_symtab_;
866 CTcPrsSymtab *old_enclosing_local_symtab = enclosing_local_symtab_;
867 CTPNStmEnclosing *old_enclosing_stm = enclosing_stm_;
868 CTcPrsSymtab *old_goto_symtab = goto_symtab_;
869 int old_local_cnt = local_cnt_;
870 int old_max_local_cnt = max_local_cnt_;
871 int old_has_local_ctx = has_local_ctx_;
872 int old_local_ctx_var_num = local_ctx_var_num_;
873 int old_ctx_var_props_used = ctx_var_props_used_;
874 int old_next_ctx_arr_idx = next_ctx_arr_idx_;
875 int old_self_valid = self_valid_;
876 int old_self_referenced = self_referenced_;
877 int old_full_method_ctx_referenced = full_method_ctx_referenced_;
878 int old_local_ctx_needs_self = local_ctx_needs_self_;
879 int old_local_ctx_needs_full_method_ctx = local_ctx_needs_full_method_ctx_;
880 CTcCodeBodyRef *old_cur_code_body = cur_code_body_;
881
882 /* parse the code body */
883 CTPNCodeBody *code_body = parse_code_body(
884 eq_before_brace, FALSE, self_valid,
885 p_argc, p_opt_argc, p_varargs, p_varargs_list, p_varargs_list_local,
886 p_has_retval, err, local_symtab, cb_type, 0, 0, cur_code_body_, 0);
887
888 /* restore the parser state */
889 cur_code_body_ = old_cur_code_body;
890 local_symtab_ = old_local_symtab;
891 enclosing_local_symtab_ = old_enclosing_local_symtab;
892 enclosing_stm_ = old_enclosing_stm;
893 goto_symtab_ = old_goto_symtab;
894 local_cnt_ = old_local_cnt;
895 max_local_cnt_ = old_max_local_cnt;
896 has_local_ctx_ = old_has_local_ctx;
897 local_ctx_var_num_ = old_local_ctx_var_num;
898 ctx_var_props_used_ = old_ctx_var_props_used;
899 next_ctx_arr_idx_ = old_next_ctx_arr_idx;
900 self_valid_ = old_self_valid;
901 self_referenced_ = old_self_referenced;
902 full_method_ctx_referenced_= old_full_method_ctx_referenced;
903 local_ctx_needs_self_ = old_local_ctx_needs_self;
904 local_ctx_needs_full_method_ctx_ = old_local_ctx_needs_full_method_ctx;
905
906 /* return the code body we parsed */
907 return code_body;
908 }
909
910 /*
911 * Set the outer local symbol table. If the caller has provided us
912 * with an explicit pre-constructed local symbol table, use that;
913 * otherwise, use the global symbol table, since we have no locals
914 * of our own yet.
915 */
916 void CTcParser::set_local_symtab(CTcPrsSymtab *local_symtab) {
917 local_symtab_ = (local_symtab == 0 ? global_symtab_ : local_symtab);
918 enclosing_local_symtab_ = (local_symtab_->get_parent() == 0
919 ? global_symtab_
920 : local_symtab_->get_parent());
921 }
922
923 /* ------------------------------------------------------------------------ */
924 /*
925 * Parse a function or method body
926 *
927 * op_args is non-zero if this is an operator overload property. This
928 * encodes the possible usages of the operator: bit 0x01 is set if this can
929 * be a unary operator, bit 0x02 if it can be binary, and bit 0x4 is set if
930 * it can be trinary. Most operators have only one bit set, but some have
931 * multiple uses, such as "-" which can be unary (-3) or binary (5-2). The
932 * number of arguments is always one less than the number of operands,
933 * because 'self' is always the primary operand (the sole operand of a
934 * unary, the left operand of a binary, or the leftmost operand of a
935 * trinary).
936 */
937 CTPNCodeBody *CTcParser::parse_code_body(
938 int eq_before_brace, int is_obj_prop, int self_valid,
939 int *p_argc, int *p_opt_argc, int *p_varargs, int *p_varargs_list,
940 CTcSymLocal **p_varargs_list_local, int *p_has_retval,
941 int *err, CTcPrsSymtab *local_symtab, tcprs_codebodytype cb_type,
942 struct propset_def *propset_stack, int propset_depth,
943 CTcCodeBodyRef *enclosing_code_body, CTcFormalTypeList **type_list)
944 {
945 /*
946 * create a new code body reference - this will let nested code bodies
947 * refer back to the code body object we're about to parse, even though
948 * we won't create the actual code body object until we're done parsing
949 * the entire code body
950 */
951 cur_code_body_ = new (G_prsmem) CTcCodeBodyRef();
952
953 /* note if we're parsing some kind of anonymous function */
954 int parsing_anon_fn = (cb_type == TCPRS_CB_ANON_FN
955 || cb_type == TCPRS_CB_SHORT_ANON_FN);
956
957 /* remember the 'self' validity */
958 self_valid_ = self_valid;
959
960 /* presume we will not need a local variable context object */
961 clear_local_ctx();
962
963 set_local_symtab(local_symtab);
964
965 /* there's no enclosing statement yet */
966 enclosing_stm_ = 0;
967
968 /*
969 * defer creating a 'goto' symbol table until we encounter a label
970 * or a 'goto'
971 */
972 goto_symtab_ = 0;
973
974 /* no locals yet */
975 local_cnt_ = 0;
976 max_local_cnt_ = 0;
977
978 /* no formals yet */
979 int formal_num = 0;
980 int opt_formal_num = 0;
981 int varargs = FALSE;
982 int varargs_list = FALSE;
983 CTcSymLocal *varargs_list_local = 0;
984
985 /* we haven't built our statement yet */
986 CTPNStmComp *stm = 0;
987 long start_line = 0;
988 CTcTokFileDesc *start_desc = 0;
989
990 /* check for a short anonymous function, which uses unusual syntax */
991 if (cb_type == TCPRS_CB_SHORT_ANON_FN)
992 {
993 /* we're at the opening brace now */
994 G_tok->get_last_pos(&start_desc, &start_line);
995
996 /*
997 * a short-form anonymous function always has an argument list,
998 * but it uses special notation: the argument list is simply the
999 * first thing after the function's open brace, and ends with a
1000 * colon
1001 */
1002 parse_formal_list(FALSE, TRUE, &formal_num, &opt_formal_num,
1003 &varargs, &varargs_list, &varargs_list_local,
1004 err, 0, TRUE, 0);
1005 if (*err)
1006 return 0;
1007
1008 /*
1009 * The contents of a short-form anonymous function are simply an
1010 * expression, whose value is implicitly returned by the function.
1011 * Alternatively, it can start with a list of "local" clauses, to
1012 * define local variables.
1013 */
1014 CTcPrsNode *expr = 0;
1015 while (G_tok->cur() == TOKT_LOCAL)
1016 {
1017 /* we need a variable name symbol */
1018 if (G_tok->next() == TOKT_SYM)
1019 {
1020 /* add the symbol */
1021 CTcSymLocal *lcl = local_symtab_->add_local(alloc_local());
1022
1023 /* check for an initializer */
1024 CTcPrsNode *subexpr = 0;
1025 if (G_tok->next() == TOKT_ASI || G_tok->cur() == TOKT_EQ)
1026 {
1027 /* parse the initializer */
1028 subexpr = parse_local_initializer(lcl, err);
1029
1030 /* combine it with the expression we had so far */
1031 expr = (expr == 0 ? subexpr :
1032 new CTPNComma(expr, subexpr));
1033 }
1034
1035 /* if there's a comma, skip it */
1036 if (G_tok->cur() == TOKT_COMMA)
1037 G_tok->next();
1038 }
1039 else
1040 {
1041 /* invalid 'local' syntax */
1042 G_tok->log_error_curtok(TCERR_LOCAL_REQ_SYM);
1043 }
1044 }
1045
1046 /* if there's anything left, parse the simple expression */
1047 if (expr == 0 || G_tok->cur() != TOKT_RBRACE)
1048 {
1049 /* parse the rest of the expression */
1050 CTcPrsNode *subexpr = parse_expr_or_dstr(TRUE);
1051
1052 /* combine it with the local initializers, if any */
1053 expr = (expr == 0 ? subexpr : new CTPNComma(expr, subexpr));
1054 }
1055
1056 /*
1057 * The next token must be the closing brace ('}') of the function.
1058 * If the next token is a semicolon, it's an error, but it's
1059 * probably just a superfluous semicolon that we can ignore.
1060 */
1061 if (G_tok->cur() == TOKT_SEM)
1062 {
1063 /* log an error explaining the problem */
1064 G_tok->log_error(TCERR_SEM_IN_SHORT_ANON_FN);
1065
1066 /* skip the semicolon */
1067 G_tok->next();
1068 }
1069
1070 /* check for the brace */
1071 switch (G_tok->cur())
1072 {
1073 case TOKT_RBRACE:
1074 /* this is what we want - skip it and continue */
1075 G_tok->next();
1076 break;
1077
1078 case TOKT_EOF:
1079 /* log an error and give up */
1080 G_tok->log_error_curtok(TCERR_SHORT_ANON_FN_REQ_RBRACE);
1081 *err = 1;
1082 return 0;
1083
1084 default:
1085 /* log an error, assuming they simply forgot the '}' */
1086 G_tok->log_error_curtok(TCERR_SHORT_ANON_FN_REQ_RBRACE);
1087 break;
1088 }
1089
1090 /*
1091 * This anonymous function syntax implicitly returns the value of
1092 * the expression, so generate a 'return' statement node that
1093 * returns the expression. If the expression has no return value,
1094 * we're simply evaluating it for side-effects, so wrap it in a
1095 * simple 'expression' statement.
1096 */
1097 CTPNStm *ret_stm;
1098 if (expr->has_return_value())
1099 ret_stm = new CTPNStmReturn(expr);
1100 else
1101 ret_stm = new CTPNStmExpr(expr);
1102
1103 /* put the 'return' statement inside a compound statement */
1104 stm = new CTPNStmComp(ret_stm, local_symtab_);
1105 }
1106 else
1107 {
1108 /*
1109 * If we have a propertyset stack, set up an inserted token stream
1110 * with the expanded token list for the formals, combining the
1111 * formals from the enclosing propertyset definitions with the
1112 * formals defined here.
1113 */
1114 if (propset_depth != 0)
1115 insert_propset_expansion(propset_stack, propset_depth);
1116
1117 /*
1118 * if we have an explicit left parenthesis, or an implied formal
1119 * list from an enclosing propertyset, parse the list
1120 */
1121 if (G_tok->cur() == TOKT_LPAR)
1122 {
1123 /* skip the open paren */
1124 G_tok->next();
1125
1126 /*
1127 * Parse the formal list. Add the symbols to the local
1128 * table (hence 'count_only' = false), and don't allow
1129 * optional arguments.
1130 */
1131 parse_formal_list(FALSE, TRUE, &formal_num, &opt_formal_num,
1132 &varargs, &varargs_list, &varargs_list_local,
1133 err, 0, FALSE, type_list);
1134 if (*err)
1135 return 0;
1136 }
1137
1138 /* parse an equals sign, if present */
1139 if (G_tok->cur() == TOKT_EQ)
1140 {
1141 /*
1142 * An equals sign after a formal parameter list can be used if
1143 * the 'eq_before_brace' flag is set. Otherwise, if we're
1144 * defining an object property, this is an error, since it's
1145 * obsolete TADS 2 syntax that we no longer allow - because
1146 * this is a change in syntax, we want to catch it
1147 * specifically so we can provide good diagnostic information
1148 * for it.
1149 */
1150 if (eq_before_brace && G_tok->cur() == TOKT_EQ)
1151 {
1152 /* it's allowed - skip the '=' */
1153 G_tok->next();
1154 }
1155 else if (is_obj_prop)
1156 {
1157 /* obsolete tads 2 syntax - flag the error */
1158 G_tok->log_error(TCERR_EQ_WITH_METHOD_OBSOLETE);
1159
1160 /*
1161 * skip the '=' so we can continue parsing the rest of the
1162 * code body without cascading errors
1163 */
1164 G_tok->next();
1165 }
1166 else
1167 {
1168 /*
1169 * it's not a situation where we allow '=' specifically,
1170 * or where we know why it might be present erroneously -
1171 * let it go for now, as we'll flag the error in the
1172 * normal compound statement parsing
1173 */
1174 }
1175 }
1176
1177 /* check for '(' syntax */
1178 //$$$
1179
1180 /* require the '{' */
1181 switch (G_tok->cur())
1182 {
1183 case TOKT_LBRACE:
1184 parse_body:
1185 /* note the location of the opening brace */
1186 G_tok->get_last_pos(&start_desc, &start_line);
1187
1188 /* parse the compound statement */
1189 stm = parse_compound(err, TRUE, TRUE, 0, TRUE);
1190 break;
1191
1192 case TOKT_SEM:
1193 case TOKT_RBRACE:
1194 /*
1195 * we seem to have found the end of the object definition, or
1196 * the end of a code body - treat it as an empty code body
1197 */
1198 G_tok->log_error_curtok(TCERR_REQ_LBRACE_CODE);
1199 stm = new CTPNStmComp(0, 0);
1200 break;
1201
1202 default:
1203 /*
1204 * the '{' was missing - log an error, but proceed from the
1205 * current token on the assumption that they merely left out
1206 * the open brace
1207 */
1208 G_tok->log_error_curtok(TCERR_REQ_LBRACE_CODE);
1209 goto parse_body;
1210 }
1211 }
1212
1213 /* if that failed, return the error */
1214 if (*err || stm == 0)
1215 return 0;
1216
1217 /*
1218 * determine how the statement exits, and generate any internal flow
1219 * warnings within the body code
1220 */
1221 unsigned long flow_flags = stm->get_control_flow(TRUE);
1222
1223 /*
1224 * Warn if the function has both explicit void and value returns.
1225 * If not, check to see if it continues; if so, it implicitly
1226 * returns a void value by falling off the end, so warn if it both
1227 * falls off the end and returns a value somewhere else. Suppress
1228 * this warning if this is a syntax check only.
1229 */
1230 if (!G_prs->get_syntax_only())
1231 {
1232 if ((flow_flags & TCPRS_FLOW_RET_VAL) != 0
1233 && (flow_flags & TCPRS_FLOW_RET_VOID) != 0)
1234 {
1235 /* it has explicit void and value returns */
1236 stm->log_warning(TCERR_RET_VAL_AND_VOID);
1237 }
1238 else if ((flow_flags & TCPRS_FLOW_RET_VAL) != 0
1239 && (flow_flags & TCPRS_FLOW_NEXT) != 0)
1240 {
1241 /* it has explicit value returns, and implicit void return */
1242 stm->log_warning(TCERR_RET_VAL_AND_IMP_VOID);
1243 }
1244 }
1245
1246 /* if the caller is interested, return the interface details */
1247 if (p_argc != 0)
1248 *p_argc = formal_num;
1249 if (p_opt_argc != 0)
1250 *p_opt_argc = opt_formal_num;
1251 if (p_varargs != 0)
1252 *p_varargs = varargs;
1253 if (p_varargs_list != 0)
1254 *p_varargs_list = varargs_list;
1255 if (p_varargs_list_local != 0)
1256 *p_varargs_list_local = varargs_list_local;
1257 if (p_has_retval)
1258 *p_has_retval = ((flow_flags & TCPRS_FLOW_RET_VAL) != 0);
1259
1260 /* create a code body node for the result */
1261 CTPNCodeBody *body_stm = new CTPNCodeBody(
1262 local_symtab_, goto_symtab_, stm,
1263 formal_num, opt_formal_num, varargs, varargs_list, varargs_list_local,
1264 max_local_cnt_, self_valid, enclosing_code_body);
1265
1266 /* store this new statement in the current code body reference object */
1267 cur_code_body_->ptr = body_stm;
1268
1269 /* save the starting location */
1270 body_stm->set_start_location(start_desc, start_line);
1271
1272 /*
1273 * set the end location in the new code body to the end location in
1274 * the underlying compound statement
1275 */
1276 body_stm->set_end_location(stm->get_end_desc(), stm->get_end_linenum());
1277
1278 /* set up the local context for access to enclosing scope locals */
1279 finish_local_ctx(body_stm, local_symtab);
1280
1281 /*
1282 * if 'self' is valid, and we're parsing an anonymous function, and we
1283 * have any references in this code body to any method context
1284 * variables (self, targetprop, targetobj, definingobj), make certain
1285 * that the code body has a context at level 1, so that it can pick up
1286 * our method context
1287 */
1288 if (self_valid && parsing_anon_fn
1289 && (self_referenced_ || full_method_ctx_referenced_))
1290 body_stm->get_or_add_ctx_var_for_level(1);
1291
1292 /* mark the code body for references to the method context */
1293 body_stm->set_self_referenced(self_referenced_);
1294 body_stm->set_full_method_ctx_referenced(full_method_ctx_referenced_);
1295
1296 /*
1297 * mark the code body for inclusion in any local context of the method
1298 * context
1299 */
1300 body_stm->set_local_ctx_needs_self(local_ctx_needs_self_);
1301 body_stm->set_local_ctx_needs_full_method_ctx(
1302 local_ctx_needs_full_method_ctx_);
1303
1304 /* return the new body statement */
1305 return body_stm;
1306 }
1307
1308 /*
1309 * Parse a compound statement
1310 */
1311 CTPNStmComp *CTcParser::parse_compound(
1312 int *err, int skip_lbrace, int need_rbrace,
1313 CTPNStmSwitch *enclosing_switch, int use_enclosing_scope)
1314 {
1315 /* save the current line information for later */
1316 CTcTokFileDesc *file;
1317 long linenum;
1318 G_tok->get_last_pos(&file, &linenum);
1319
1320 /* skip the '{' if we're on one and the caller wants us to */
1321 if (skip_lbrace && G_tok->cur() == TOKT_LBRACE)
1322 G_tok->next();
1323
1324 /* enter a scope */
1325 tcprs_scope_t scope_data;
1326 if (!use_enclosing_scope)
1327 enter_scope(&scope_data);
1328
1329 /* we don't have any statements in our sublist yet */
1330 CTPNStm *first_stm = 0;
1331 CTPNStm *last_stm = 0;
1332 CTPNStm *cur_stm = 0;
1333
1334 /* presume we won't find the closing brace */
1335 int skip_rbrace = FALSE;
1336
1337 /* keep going until we reach the closing '}' */
1338 for (int done = FALSE ; !done ; )
1339 {
1340 /* check what we've found */
1341 switch (G_tok->cur())
1342 {
1343 case TOKT_RBRACE:
1344 /* it's our closing brace - we're done */
1345 done = TRUE;
1346 cur_stm = 0;
1347
1348 /* note that we must still skip the closing brace */
1349 skip_rbrace = TRUE;
1350
1351 /* stop scanning statements */
1352 break;
1353
1354 case TOKT_EOF:
1355 /*
1356 * if we're at end of file, and we don't need a right brace to
1357 * end the block, consider this the end of the block
1358 */
1359 if (!need_rbrace)
1360 {
1361 done = TRUE;
1362 cur_stm = 0;
1363 skip_rbrace = FALSE;
1364 break;
1365 }
1366 else
1367 {
1368 /* it's an error */
1369 G_tok->log_error(TCERR_EOF_IN_CODE);
1370 cur_stm = 0;
1371 done = TRUE;
1372 break;
1373 }
1374 break;
1375
1376 default:
1377 /* parse a statement */
1378 cur_stm = parse_stm(err, enclosing_switch, FALSE);
1379
1380 /* if an error occurred, stop parsing */
1381 if (*err)
1382 done = TRUE;
1383 break;
1384 }
1385
1386 /* if we parsed a statement, add it to our list */
1387 if (cur_stm != 0)
1388 {
1389 /* link the statement at the end of our list */
1390 if (last_stm != 0)
1391 last_stm->set_next_stm(cur_stm);
1392 else
1393 first_stm = cur_stm;
1394 last_stm = cur_stm;
1395 }
1396 }
1397
1398 /* if there's no statement, make the body a null statement */
1399 if (first_stm == 0)
1400 first_stm = new CTPNStmNull();
1401
1402 /* build the compound statement node */
1403 CTPNStmComp *comp_stm = new CTPNStmComp(first_stm, local_symtab_);
1404
1405 /* set some additional information if we created a statement */
1406 if (comp_stm != 0)
1407 {
1408 /* set the statement's line to the start of the compound */
1409 comp_stm->set_source_pos(file, linenum);
1410
1411 /* note whether or not we have our own private scope */
1412 comp_stm->set_has_own_scope(!use_enclosing_scope
1413 && (local_symtab_
1414 != scope_data.local_symtab));
1415 }
1416
1417 /* if necessary, skip the closing brace */
1418 if (skip_rbrace)
1419 G_tok->next();
1420
1421 /* leave the local scope */
1422 if (!use_enclosing_scope)
1423 leave_scope(&scope_data);
1424
1425 /* return the compound statement object */
1426 return comp_stm;
1427 }
1428
1429 /*
1430 * Create a local symbol table for the current scope, if necessary
1431 */
1432 void CTcParser::create_scope_local_symtab()
1433 {
1434 /*
1435 * if our symbol table is the same as the enclosing symbol table, we
1436 * must create our own table
1437 */
1438 if (local_symtab_ == enclosing_local_symtab_)
1439 {
1440 /*
1441 * Create our own local symbol table, replacing the current one
1442 * - we saved the enclosing one already when we entered the
1443 * scope, so we'll restore it on our way out. The new local
1444 * symbol table has the enclosing symbol table as its parent
1445 * scope.
1446 */
1447 local_symtab_ = new CTcPrsSymtab(local_symtab_);
1448 }
1449 }
1450
1451 /*
1452 * Parse a local variable definition
1453 */
1454 CTPNStm *CTcParser::parse_local(int *err)
1455 {
1456 int done;
1457 CTPNStm *first_stm;
1458 CTPNStm *last_stm;
1459
1460 /* we have no initializer statements yet */
1461 first_stm = last_stm = 0;
1462
1463 /* skip the 'local' keyword */
1464 G_tok->next();
1465
1466 /* keep going until we reach the closing semicolon */
1467 for (done = FALSE ; !done ; )
1468 {
1469 /* we need a symbol name */
1470 if (G_tok->cur() == TOKT_SYM)
1471 {
1472 const char *sym;
1473 size_t symlen;
1474 CTcSymLocal *lcl;
1475 CTPNStm *stm;
1476 CTcPrsNode *expr;
1477
1478 /* get the symbol string from the token */
1479 sym = G_tok->getcur()->get_text();
1480 symlen = G_tok->getcur()->get_text_len();
1481
1482 /* add the new local variable to our symbol table */
1483 lcl = local_symtab_->add_local(sym, symlen, alloc_local(),
1484 FALSE, FALSE, FALSE);
1485
1486 /* skip the symbol and check for an initial value assignment */
1487 switch (G_tok->next())
1488 {
1489 case TOKT_EQ:
1490 case TOKT_ASI:
1491 /* parse the initializer */
1492 expr = parse_local_initializer(lcl, err);
1493
1494 /* if we didn't get a statement, we can't proceed */
1495 if (expr == 0)
1496 {
1497 done = TRUE;
1498 break;
1499 }
1500
1501 /* create a statement for the assignment */
1502 stm = new CTPNStmExpr(expr);
1503
1504 /*
1505 * set the statement's source location according to the
1506 * current source location - if we have multiple
1507 * initializers over several lines, this will allow the
1508 * debugger to step through the individual
1509 * initializations
1510 */
1511 stm->set_source_pos(G_tok->get_last_desc(),
1512 G_tok->get_last_linenum());
1513
1514 /* add the statement to our list */
1515 if (last_stm != 0)
1516 last_stm->set_next_stm(stm);
1517 else
1518 first_stm = stm;
1519 last_stm = stm;
1520
1521 /* done */
1522 break;
1523
1524 default:
1525 /* there's nothing more to do with this variable */
1526 break;
1527 }
1528
1529 /*
1530 * check what follows - we can have a comma to introduce
1531 * another local variable, or a semicolon to end the
1532 * statement
1533 */
1534 switch(G_tok->cur())
1535 {
1536 case TOKT_COMMA:
1537 /* skip the comma and go on to the next variable */
1538 G_tok->next();
1539 break;
1540
1541 case TOKT_SEM:
1542 /* skip the semicolon, and stop scanning */
1543 G_tok->next();
1544 done = TRUE;
1545 break;
1546
1547 case TOKT_SYM:
1548 /*
1549 * they probably just left out a comma - assume the
1550 * comma is there and keep going
1551 */
1552 G_tok->log_error_curtok(TCERR_LOCAL_REQ_COMMA);
1553 break;
1554
1555 default:
1556 /*
1557 * these almost certainly indicate that they left out a
1558 * semicolon - report the error and continue from here
1559 */
1560 G_tok->log_error_curtok(TCERR_LOCAL_REQ_COMMA);
1561 done = TRUE;
1562 break;
1563 }
1564 }
1565 else
1566 {
1567 /* symbol required - log the error */
1568 G_tok->log_error_curtok(TCERR_LOCAL_REQ_SYM);
1569
1570 /* determine how to proceed based on what we have */
1571 switch(G_tok->cur())
1572 {
1573 case TOKT_COMMA:
1574 /*
1575 * they probably just put in an extra comma - skip it
1576 * and keep trying to parse the local list
1577 */
1578 G_tok->next();
1579 break;
1580
1581 case TOKT_SEM:
1582 /* that's the end of the statement */
1583 G_tok->next();
1584 done = TRUE;
1585 break;
1586
1587 case TOKT_EOF:
1588 /* set the error flag and stop scanning */
1589 *err = TRUE;
1590 done = TRUE;
1591 break;
1592
1593 default:
1594 /* try skipping this token and trying again */
1595 G_tok->next();
1596 break;
1597 }
1598 }
1599 }
1600
1601 /*
1602 * if we have one statement, return it; if we have more than one,
1603 * return a compound statement to contain the list; if we have
1604 * nothing, return nothing
1605 */
1606 if (first_stm == 0)
1607 return 0;
1608 else if (first_stm == last_stm)
1609 return first_stm;
1610 else
1611 return new CTPNStmComp(first_stm, local_symtab_);
1612 }
1613
1614 /*
1615 * Parse a local variable initializer
1616 */
1617 CTcPrsNode *CTcParser::parse_local_initializer(CTcSymLocal *lcl, int *err)
1618 {
1619 /*
1620 * skip the assignment operator and parse the expression (which
1621 * cannot use the comma operator)
1622 */
1623 G_tok->next();
1624 CTcPrsNode *expr = parse_asi_expr();
1625
1626 /* if that failed, return failure */
1627 if (expr == 0)
1628 return 0;
1629
1630 /*
1631 * if we have a valid local, return a new expression node for the
1632 * assignment; otherwise just return the expression, since we have
1633 * nothing to assign to
1634 */
1635 return (lcl != 0 ? new CTPNAsi(new CTPNSymResolved(lcl), expr) : expr);
1636 }
1637
1638 /*
1639 * Parse a statement
1640 */
1641 CTPNStm *CTcParser::parse_stm(int *err, CTPNStmSwitch *enclosing_switch,
1642 int compound_use_enclosing_scope)
1643 {
1644 CTcToken tok;
1645
1646 /*
1647 * remember where the statement starts - when we create the
1648 * statement object, it will refer to these values to set its
1649 * internal memory of the statement's source file location
1650 */
1651 cur_desc_ = G_tok->get_last_desc();
1652 cur_linenum_ = G_tok->get_last_linenum();
1653
1654 /* see what we have */
1655 try_again:
1656 switch(G_tok->cur())
1657 {
1658 case TOKT_EOF:
1659 /* unexpected end of file - log an error */
1660 G_tok->log_error(TCERR_EOF_IN_CODE);
1661
1662 /* set the caller's error flag */
1663 *err = TRUE;
1664
1665 /* there's no statement to return, obviously */
1666 return 0;
1667
1668 case TOKT_DSTR_MID:
1669 case TOKT_DSTR_END:
1670 case TOKT_RBRACE:
1671 /*
1672 * we shouldn't be looking at any of these at the start of a
1673 * statement
1674 */
1675 G_tok->log_error_curtok(TCERR_EXPECTED_STMT_START);
1676 G_tok->next();
1677 return 0;
1678
1679 case TOKT_SEM:
1680 /*
1681 * null statement - this doesn't generate any code; simply skip
1682 * the semicolon and keep going
1683 */
1684 G_tok->next();
1685
1686 /* this doesn't generate any code */
1687 return 0;
1688
1689 case TOKT_LOCAL:
1690 /* if we don't have our own local symbol table, create one */
1691 create_scope_local_symtab();
1692
1693 /* parse the local variable definition and return the result */
1694 return parse_local(err);
1695
1696 case TOKT_LBRACE:
1697 /* it's a compound statement */
1698 return parse_compound(err, TRUE, TRUE,
1699 0, compound_use_enclosing_scope);
1700
1701 case TOKT_IF:
1702 /* parse an if statement */
1703 return parse_if(err);
1704
1705 case TOKT_RETURN:
1706 /* parse a return statement */
1707 return parse_return(err);
1708
1709 case TOKT_FOR:
1710 /* parse a for statement */
1711 return parse_for(err);
1712
1713 case TOKT_FOREACH:
1714 /* parse a foreach statement */
1715 return parse_foreach(err);
1716
1717 case TOKT_WHILE:
1718 /* parse a while statement */
1719 return parse_while(err);
1720
1721 case TOKT_DO:
1722 /* parse a do-while */
1723 return parse_do_while(err);
1724
1725 case TOKT_SWITCH:
1726 /* parse a switch */
1727 return parse_switch(err);
1728
1729 case TOKT_GOTO:
1730 /* parse a 'goto' */
1731 return parse_goto(err);
1732
1733 case TOKT_BREAK:
1734 return parse_break(err);
1735
1736 case TOKT_CONTINUE:
1737 return parse_continue(err);
1738
1739 case TOKT_TRY:
1740 return parse_try(err);
1741
1742 case TOKT_THROW:
1743 return parse_throw(err);
1744
1745 case TOKT_CATCH:
1746 /* misplaced 'catch' clause - log an error */
1747 G_tok->log_error(TCERR_MISPLACED_CATCH);
1748
1749 /*
1750 * skip the following open paren, class name, variable name, and
1751 * closing paren, as long as we find all of these
1752 */
1753 if (G_tok->next() == TOKT_LPAR
1754 && G_tok->next() == TOKT_SYM
1755 && G_tok->next() == TOKT_SYM
1756 && G_tok->next() == TOKT_RPAR)
1757 G_tok->next();
1758
1759 /* there's no valid statement to return */
1760 return 0;
1761
1762 case TOKT_FINALLY:
1763 /* misplaced 'finally' clause - log an error */
1764 G_tok->log_error(TCERR_MISPLACED_FINALLY);
1765
1766 /* skip the 'finally' keyword, and return failure */
1767 G_tok->next();
1768 return 0;
1769
1770 case TOKT_ELSE:
1771 /*
1772 * misplaced 'else' clause - log an error, skip the 'else'
1773 * keyword, and proceed with what follows
1774 */
1775 G_tok->log_error(TCERR_MISPLACED_ELSE);
1776 G_tok->next();
1777 return 0;
1778
1779 case TOKT_CASE:
1780 /*
1781 * if we're in a 'switch', it's a valid 'case' label; otherwise,
1782 * it's misplaced
1783 */
1784 if (enclosing_switch != 0)
1785 {
1786 /* parse the 'case' label */
1787 return parse_case(err, enclosing_switch);
1788 }
1789 else
1790 {
1791 /*
1792 * not directly within a 'switch', so this is a misplaced
1793 * 'case' keyword - log an error
1794 */
1795 G_tok->log_error(TCERR_MISPLACED_CASE);
1796
1797 /* skip the 'case' keyword */
1798 G_tok->next();
1799
1800 /* assume there's an expression here, and skip that as well */
1801 parse_expr();
1802
1803 /* if there's a colon, skip it, too */
1804 if (G_tok->cur() == TOKT_COLON)
1805 G_tok->next();
1806
1807 /* proceed from here */
1808 return 0;
1809 }
1810
1811 case TOKT_DEFAULT:
1812 /* allow this only if we're directly in a 'switch' body */
1813 if (enclosing_switch != 0)
1814 {
1815 /* parse the 'default' label */
1816 return parse_default(err, enclosing_switch);
1817 }
1818 else
1819 {
1820 /* misplaced 'default' keyword - log an error */
1821 G_tok->log_error(TCERR_MISPLACED_DEFAULT);
1822
1823 /* skip the 'default' keyword; if there's a colon, skip it, too */
1824 if (G_tok->next() == TOKT_COLON)
1825 G_tok->next();
1826
1827 /* proceed from here */
1828 return 0;
1829 }
1830
1831 case TOKT_SYM:
1832 /*
1833 * It's a symbol. First, check for a label. This requires that
1834 * we look ahead one token, because we have to look at the next
1835 * token to see if it's a colon; if it's not, we have to back up
1836 * and parse the symbol as the start of an expression. So,
1837 * remember the current symbol token, then look at what follows.
1838 */
1839 tok = *G_tok->copycur();
1840 if (G_tok->next() == TOKT_COLON)
1841 {
1842 CTPNStmEnclosing *old_enclosing;
1843 CTPNStmLabel *label_stm;
1844 CTcSymLabel *lbl;
1845 CTPNStm *stm;
1846
1847 /* it's a label - create a symbol table entry for it */
1848 lbl = add_code_label(&tok);
1849
1850 /* create the labeled statement node */
1851 label_stm = new CTPNStmLabel(lbl, enclosing_stm_);
1852
1853 /* skip the colon */
1854 G_tok->next();
1855
1856 /*
1857 * set our new label to be the enclosing label for
1858 * everything contained within its statement
1859 */
1860 old_enclosing = set_enclosing_stm(label_stm);
1861
1862 /* parse the labeled statement */
1863 stm = parse_stm(err, enclosing_switch, FALSE);
1864
1865 /* restore our enclosing statement */
1866 set_enclosing_stm(old_enclosing);
1867
1868 /* if parsing the labeled statement failed, give up */
1869 if (*err)
1870 return 0;
1871
1872 /* connect to the label to the statement it labels */
1873 label_stm->set_stm(stm);
1874
1875 /* point the label symbol to its statement node */
1876 if (lbl != 0)
1877 lbl->set_stm(label_stm);
1878
1879 /* return the labeled statement node */
1880 return label_stm;
1881 }
1882
1883 /*
1884 * it's not a label - push the colon back into the input stream
1885 * so that we read it again, then parse this as an ordinary
1886 * expression
1887 */
1888 G_tok->unget();
1889 goto do_parse_expr;
1890
1891 case TOKT_RPAR:
1892 /*
1893 * they probably had too many close parens in something like a
1894 * 'for' or 'if' statement - flag the error
1895 */
1896 G_tok->log_error(TCERR_EXTRA_RPAR);
1897
1898 /* skip the extra paren and go back for another try */
1899 G_tok->next();
1900 goto try_again;
1901
1902 default:
1903 do_parse_expr:
1904 /* anything else must be the start of an expression */
1905 {
1906 /* parse the expression */
1907 CTcPrsNode *expr = parse_expr_or_dstr(TRUE);
1908
1909 /* the statement must be terminated with a semicolon */
1910 if (parse_req_sem())
1911 {
1912 /* set the error flag */
1913 *err = TRUE;
1914
1915 /* there's no statement to return */
1916 return 0;
1917 }
1918
1919 /*
1920 * if we successfully parsed an expression, create a
1921 * statement node for the expression; if expr is null, the
1922 * expression parser will already have issued an error, so
1923 * we can simply ignore the failed expression and continue
1924 * to the next statement
1925 */
1926 if (expr != 0)
1927 return new CTPNStmExpr(expr);
1928 else
1929 return 0;
1930 }
1931 }
1932 }
1933
1934
1935 /*
1936 * Add a 'goto' label symbol to the current code body
1937 */
1938 CTcSymLabel *CTcParser::add_code_label(const CTcToken *tok)
1939 {
1940 /* if there's no 'goto' symbol table, create one */
1941 if (goto_symtab_ == 0)
1942 goto_symtab_ = new CTcPrsSymtab(0);
1943
1944 /* create the label and return it */
1945 return goto_symtab_->add_code_label(tok->get_text(),
1946 tok->get_text_len(), FALSE);
1947 }
1948
1949
1950 /*
1951 * Parse an 'if' statement
1952 */
1953 CTPNStm *CTcParser::parse_if(int *err)
1954 {
1955 CTcPrsNode *cond_expr;
1956 CTPNStm *if_stm;
1957 CTPNStm *then_stm;
1958 CTPNStm *else_stm;
1959 CTcTokFileDesc *file;
1960 long linenum;
1961
1962 /* save the starting line information for later */
1963 G_tok->get_last_pos(&file, &linenum);
1964
1965 /* skip the 'if' keyword, and require the open paren */
1966 if (G_tok->next() == TOKT_LPAR)
1967 {
1968 /* skip the left paren */
1969 G_tok->next();
1970 }
1971 else
1972 {
1973 /*
1974 * log an error, but proceed on the assumption that they simply
1975 * left out the paren
1976 */
1977 G_tok->log_error_curtok(TCERR_REQ_LPAR_IF);
1978 }
1979
1980 /* parse the expression */
1981 cond_expr = parse_cond_expr();
1982
1983 /* if that failed, return failure */
1984 if (cond_expr == 0)
1985 {
1986 *err = TRUE;
1987 return 0;
1988 }
1989
1990 /* require the close paren */
1991 if (G_tok->cur() == TOKT_RPAR)
1992 {
1993 /* skip it */
1994 G_tok->next();
1995 }
1996 else
1997 {
1998 /*
1999 * log an error, then proceed assuming that the paren was merely
2000 * omitted
2001 */
2002 G_tok->log_error_curtok(TCERR_REQ_RPAR_IF);
2003 }
2004
2005 /* parse the true-part statement */
2006 then_stm = parse_stm(err, 0, FALSE);
2007
2008 /* if an error occurred, return failure */
2009 if (*err)
2010 return 0;
2011
2012 /* check for 'else' */
2013 if (G_tok->cur() == TOKT_ELSE)
2014 {
2015 /* skip the 'else' keyword */
2016 G_tok->next();
2017
2018 /* parse the false-part statement */
2019 else_stm = parse_stm(err, 0, FALSE);
2020
2021 /* if an error occurred, return failure */
2022 if (*err)
2023 return 0;
2024 }
2025 else
2026 {
2027 /* there's no 'else' part */
2028 else_stm = 0;
2029 }
2030
2031 /* create and return the 'if' statement node */
2032 if_stm = new CTPNStmIf(cond_expr, then_stm, else_stm);
2033
2034 /* set the original statement position in the node */
2035 if_stm->set_source_pos(file, linenum);
2036
2037 /* return the 'if' statement node */
2038 return if_stm;
2039 }
2040
2041 /*
2042 * Parse a 'return' statement
2043 */
2044 CTPNStm *CTcParser::parse_return(int *err)
2045 {
2046 CTPNStm *stm;
2047
2048 /* skip the 'return' keyword and see what we have */
2049 switch(G_tok->next())
2050 {
2051 case TOKT_SEM:
2052 /*
2053 * end of the statement - this is a void return; skip the
2054 * semicolon, and return a void return statement node
2055 */
2056 G_tok->next();
2057 stm = new CTPNStmReturn(0);
2058 break;
2059
2060 case TOKT_LBRACE:
2061 case TOKT_RBRACE:
2062 /*
2063 * they probably just left out a semicolon - flag the error, and
2064 * continue parsing from this token
2065 */
2066 G_tok->log_error_curtok(TCERR_RET_REQ_EXPR);
2067 return 0;
2068
2069 default:
2070 /* it's a return with an expression - parse the expression */
2071 stm = new CTPNStmReturn(parse_expr());
2072
2073 /* make sure we're on a semicolon */
2074 if (parse_req_sem())
2075 {
2076 *err = TRUE;
2077 return 0;
2078 }
2079
2080 /* done */
2081 break;
2082 }
2083
2084 /* return the statement node we created */
2085 return stm;
2086 }
2087
2088 /*
2089 * Parse a 'for' statement
2090 */
2091 CTPNStm *CTcParser::parse_for(int *err)
2092 {
2093 /* save the current line information for later */
2094 CTcTokFileDesc *file;
2095 long linenum;
2096 G_tok->get_last_pos(&file, &linenum);
2097
2098 /*
2099 * enter a scope, in case we create a local symbol table for local
2100 * variables defined within the 'for' statement
2101 */
2102 tcprs_scope_t scope_data;
2103 enter_scope(&scope_data);
2104
2105 /* parse the open paren */
2106 if (G_tok->next() == TOKT_LPAR)
2107 {
2108 /* skip it */
2109 G_tok->next();
2110 }
2111 else
2112 {
2113 /* log an error, and proceed, assuming it was simply left out */
2114 G_tok->log_error_curtok(TCERR_REQ_FOR_LPAR);
2115 }
2116
2117 /* we don't have any of the expressions yet */
2118 CTcPrsNode *init_expr = 0;
2119 CTcPrsNode *cond_expr = 0;
2120 CTcPrsNode *reinit_expr = 0;
2121 int found_in = FALSE;
2122
2123 /* "in" expression list */
2124 CTPNForIn *in_head = 0, *in_tail = 0;
2125
2126 /* parse the initializer list */
2127 for (int done = FALSE ; !done ; )
2128 {
2129 /* presume we won't find an expression on this round */
2130 CTcPrsNode *expr = 0;
2131
2132 /* check what we have */
2133 switch(G_tok->cur())
2134 {
2135 case TOKT_LOCAL:
2136 /*
2137 * if we haven't created our own symbol table local to the
2138 * 'for' loop, do so now
2139 */
2140 create_scope_local_symtab();
2141
2142 /* skip the 'local' keyword and get the local name */
2143 if (G_tok->next() == TOKT_SYM)
2144 {
2145 /* add the local symbol */
2146 CTcSymLocal *lcl = local_symtab_->add_local(alloc_local());
2147
2148 /* check for the required initializer */
2149 switch(G_tok->next())
2150 {
2151 case TOKT_ASI:
2152 case TOKT_EQ:
2153 /* parse the initializer */
2154 expr = parse_local_initializer(lcl, err);
2155 break;
2156
2157 case TOKT_SYM:
2158 /* check for an 'in' expression */
2159 if (G_tok->getcur()->text_matches("in", 2))
2160 {
2161 /* parse the 'var in expr' or 'var in from..to' */
2162 G_tok->next();
2163 expr = parse_for_in_clause(
2164 new CTPNSymResolved(lcl), in_head, in_tail);
2165
2166 /* note the "in" */
2167 found_in = TRUE;
2168
2169 /* if that failed, stop scanning the initializer */
2170 if (expr == 0)
2171 done = TRUE;
2172 }
2173 else
2174 {
2175 /* anything else is an error */
2176 goto req_asi;
2177 }
2178 break;
2179
2180 default:
2181 req_asi:
2182 /* log an error - an initializer is required */
2183 G_tok->log_error_curtok(TCERR_REQ_FOR_LOCAL_INIT);
2184 break;
2185 }
2186 }
2187 else
2188 {
2189 /*
2190 * the 'local' statement isn't constructed properly -
2191 * this is difficult to recover from intelligently, so
2192 * just log an error and keep going from here
2193 */
2194 G_tok->log_error_curtok(TCERR_LOCAL_REQ_SYM);
2195 break;
2196 }
2197 break;
2198
2199 case TOKT_SEM:
2200 /* it's a semicolon - we're done with the initializer list */
2201 done = TRUE;
2202
2203 /*
2204 * if we have an expression already, it means that the
2205 * previous token was a comma - this is an error, since we
2206 * have a missing expression; log the error but continue
2207 * anyway
2208 */
2209 if (init_expr != 0)
2210 G_tok->log_error(TCERR_MISSING_FOR_INIT_EXPR);
2211 break;
2212
2213 case TOKT_RPAR:
2214 /* if we found an "in", we can end early */
2215 if (found_in)
2216 {
2217 done = TRUE;
2218 break;
2219 }
2220
2221 /* otherwise, fall through to the missing part error... */
2222 /* FALL THROUGH */
2223
2224 case TOKT_LBRACE:
2225 case TOKT_RBRACE:
2226 /* premature end of the list - log an error and stop */
2227 G_tok->log_error_curtok(TCERR_MISSING_FOR_PART);
2228 done = TRUE;
2229 break;
2230
2231 default:
2232 /*
2233 * This must be an expression - parse it. Parse an
2234 * assignment expression, not a comma expression, because we
2235 * must check for a "local" clause after each comma.
2236 */
2237 expr = parse_asi_expr();
2238
2239 /* if that failed, stop scanning the "for" */
2240 if (expr == 0)
2241 done = TRUE;
2242
2243 /* check for an 'in' expression */
2244 if (G_tok->cur() == TOKT_SYM
2245 && G_tok->getcur()->text_matches("in", 2))
2246 {
2247 /* skip the "in" and parse the collection/range */
2248 G_tok->next();
2249 expr = parse_for_in_clause(expr, in_head, in_tail);
2250
2251 /* note the "in" */
2252 found_in = TRUE;
2253
2254 /* if that failed, stop parsing the initializer */
2255 if (expr == 0)
2256 done = TRUE;
2257 }
2258
2259 /* done with this clause */
2260 break;
2261 }
2262
2263 /*
2264 * if we got an expression, add it into the initializer
2265 * expression under construction by adding it under a "comma"
2266 * node
2267 */
2268 if (expr != 0)
2269 {
2270 /*
2271 * if there's an expression, build a comma expression for
2272 * the expression so far plus the new expression; otherwise,
2273 * the new expression becomes the entire expression so far
2274 */
2275 if (init_expr != 0)
2276 init_expr = new CTPNComma(init_expr, expr);
2277 else
2278 init_expr = expr;
2279 }
2280
2281 /* if we're done, we can stop now */
2282 if (done)
2283 break;
2284
2285 /*
2286 * we must have a semicolon or comma after each initializer
2287 * expression
2288 */
2289 switch(G_tok->cur())
2290 {
2291 case TOKT_SEM:
2292 /* that's the end of the statement - stop now */
2293 done = TRUE;
2294 break;
2295
2296 case TOKT_COMMA:
2297 /* skip the comma and parse the next initializer */
2298 G_tok->next();
2299 break;
2300
2301 case TOKT_RPAR:
2302 /* if we found an "in" clause, we only need an initializer */
2303 if (found_in)
2304 {
2305 done = TRUE;
2306 break;
2307 }
2308
2309 /* otherwise, fall through to the error case... */
2310 /* FALL THROUGH */
2311
2312 case TOKT_LBRACE:
2313 case TOKT_RBRACE:
2314 /* log an error, and stop parsing the expression list */
2315 G_tok->log_error_curtok(TCERR_MISSING_FOR_PART);
2316 done = TRUE;
2317 break;
2318
2319 default:
2320 /* log an error */
2321 G_tok->log_error_curtok(TCERR_REQ_FOR_INIT_COMMA);
2322
2323 /* skip the errant token and keep going */
2324 G_tok->next();
2325 break;
2326 }
2327 }
2328
2329 /*
2330 * if we successfully found the ';' at the end of the initializer
2331 * list, parse the condition expression
2332 */
2333 if (G_tok->cur() == TOKT_SEM)
2334 {
2335 int cont_to_reinit;
2336
2337 /* presume we'll want to continue to the reinit expression */
2338 cont_to_reinit = TRUE;
2339
2340 /* skip the ';' */
2341 G_tok->next();
2342
2343 /* if the condition isn't empty, parse it */
2344 if (G_tok->cur() != TOKT_SEM)
2345 cond_expr = parse_cond_expr();
2346
2347 /* require the ';' after the expression */
2348 switch(G_tok->cur())
2349 {
2350 case TOKT_SEM:
2351 /* it's fine - keep going from here */
2352 G_tok->next();
2353 break;
2354
2355 case TOKT_RPAR:
2356 case TOKT_LBRACE:
2357 case TOKT_RBRACE:
2358 /* missing part */
2359 G_tok->log_error_curtok(TCERR_MISSING_FOR_PART);
2360
2361 /* don't bother trying to find a reinitialization expression */
2362 cont_to_reinit = FALSE;
2363 break;
2364
2365 default:
2366 /*
2367 * we seem to be missing the semicolon; keep going from
2368 * here, assuming that they simply left out the semicolon
2369 * between the condition and reinitializer expressions
2370 */
2371 G_tok->log_error_curtok(TCERR_REQ_FOR_COND_SEM);
2372 break;
2373 }
2374
2375 /*
2376 * if we're to continue to the reinitializer, parse it; there is
2377 * no reinitialization expression if the next token is a right
2378 * paren
2379 */
2380 if (cont_to_reinit && G_tok->cur() != TOKT_RPAR)
2381 {
2382 /* parse the expression */
2383 reinit_expr = parse_expr();
2384 }
2385
2386 /* make sure we have the right paren */
2387 if (G_tok->cur() == TOKT_RPAR)
2388 {
2389 /* skip the paren */
2390 G_tok->next();
2391 }
2392 else
2393 {
2394 /*
2395 * log an error, and try parsing the body from here, on the
2396 * assumption that they simply forgot about the right paren
2397 * and jumped right into the body
2398 */
2399 G_tok->log_error_curtok(TCERR_REQ_FOR_RPAR);
2400 }
2401 }
2402 else if (G_tok->cur() == TOKT_RPAR)
2403 {
2404 /*
2405 * We already found the right paren - early, so we logged an error
2406 * if necessary (it's an error in most cases, but *not* if there's
2407 * an 'in' clause in the initializer list). Simply skip it now so
2408 * that we can proceed to the body of the 'for'.
2409 */
2410 G_tok->next();
2411 }
2412
2413 /* create the "for" node */
2414 CTPNStmFor *for_stm = new CTPNStmFor(
2415 init_expr, cond_expr, reinit_expr, in_head,
2416 local_symtab_, enclosing_stm_);
2417
2418 /* set the 'for' to enclose its body */
2419 CTPNStmEnclosing *old_enclosing = set_enclosing_stm(for_stm);
2420
2421 /* parse the body of the "for" loop */
2422 CTPNStm *body_stm = parse_stm(err, 0, FALSE);
2423
2424 /* restore the old enclosing statement */
2425 set_enclosing_stm(old_enclosing);
2426
2427 /* if that failed, return failure */
2428 if (*err)
2429 return 0;
2430
2431 /* set the body of the 'for' */
2432 for_stm->set_body(body_stm);
2433
2434 /* set the original statement position in the node */
2435 for_stm->set_source_pos(file, linenum);
2436
2437 /* set the own-scope flag */
2438 for_stm->set_has_own_scope(local_symtab_ != scope_data.local_symtab);
2439
2440 /* exit any local scope we created */
2441 leave_scope(&scope_data);
2442
2443 /* return the "for" node */
2444 return for_stm;
2445 }
2446
2447 /*
2448 * Parse a for..in clause. This parses the part after the "in"; we accept
2449 * either a simple expression, or a range (expr .. expr). We return the
2450 * expression node representing the clause. It's up to the caller to look
2451 * up the local variable before the "in" and pass it to us.
2452 */
2453 CTcPrsNode *CTcParser::parse_for_in_clause(
2454 CTcPrsNode *lval, CTPNForIn *&head, CTPNForIn *&tail)
2455 {
2456 /*
2457 * Parse an assignment expression. We parse from this point in the
2458 * grammar because the entire 'in' clause can be part of a comma
2459 * expression, so we want to stop if we reach a comma rather than
2460 * treating the comma as part of our own expression.
2461 */
2462 CTcPrsNode *expr = parse_asi_expr();
2463
2464 /* if that failed, return failure */
2465 if (expr == 0)
2466 return 0;
2467
2468 /* if the next token is "..", we have a "for var in from..to" range */
2469 CTPNForIn *in_expr;
2470 if (G_tok->cur() == TOKT_DOTDOT)
2471 {
2472 /* skip the ".." and parse the "to" expression */
2473 G_tok->next();
2474 CTcPrsNode *toExpr = parse_asi_expr();
2475
2476 /* if that failed, return failure */
2477 if (toExpr == 0)
2478 return 0;
2479
2480 /* check for the optional "step" expression */
2481 CTcPrsNode *stepExpr = 0;
2482 if (G_tok->cur() == TOKT_SYM
2483 && G_tok->getcur()->text_matches("step", 4))
2484 {
2485 /* skip the "step" and parse the expression */
2486 G_tok->next();
2487 stepExpr = parse_asi_expr();
2488 }
2489
2490 /*
2491 * allocate variables for the "to" and "step" expressions, but only
2492 * if they're non-constant
2493 */
2494 int to_local = (toExpr->is_const() ? -1 : alloc_local());
2495 int step_local = (stepExpr == 0 || stepExpr->is_const() ? -1 :
2496 alloc_local());
2497
2498 /* build the "for x in range" node */
2499 in_expr = new CTPNVarInRange(lval, expr, toExpr, stepExpr,
2500 to_local, step_local);
2501 }
2502 else
2503 {
2504 /* it's a plain old "for x in collection" */
2505 in_expr = new CTPNVarIn(lval, expr, alloc_local());
2506 }
2507
2508 /* link it into the list */
2509 if (tail != 0)
2510 tail->setnxt(in_expr);
2511 else
2512 head = in_expr;
2513 tail = in_expr;
2514
2515 /* return the new expression */
2516 return in_expr;
2517 }
2518
2519 /*
2520 * 'for var in expr' - fold constants
2521 */
2522 CTcPrsNode *CTPNVarInBase::fold_constants(CTcPrsSymtab *symtab)
2523 {
2524 /* fold each element */
2525 lval_ = lval_->fold_constants(symtab);
2526 expr_ = expr_->fold_constants(symtab);
2527
2528 /* we can't fold the overall 'in' expression itself */
2529 return this;
2530 }
2531
2532 /*
2533 * 'for var in from .. to' - fold constants
2534 */
2535 CTcPrsNode *CTPNVarInRangeBase::fold_constants(CTcPrsSymtab *symtab)
2536 {
2537 /* fold each element */
2538 lval_ = lval_->fold_constants(symtab);
2539 from_expr_ = from_expr_->fold_constants(symtab);
2540 to_expr_ = to_expr_->fold_constants(symtab);
2541 if (step_expr_ != 0)
2542 step_expr_ = step_expr_->fold_constants(symtab);
2543
2544 /* we can't fold the overall 'in' expression itself */
2545 return this;
2546 }
2547
2548
2549 /*
2550 * Parse a 'foreach' statement
2551 */
2552 CTPNStm *CTcParser::parse_foreach(int *err)
2553 {
2554 tcprs_scope_t scope_data;
2555 CTcPrsNode *iter_expr;
2556 CTcPrsNode *coll_expr;
2557 CTPNStm *body_stm;
2558 CTPNStmForeach *foreach_stm;
2559 CTcTokFileDesc *file;
2560 long linenum;
2561 CTPNStmEnclosing *old_enclosing;
2562
2563 /* save the current line information for later */
2564 G_tok->get_last_pos(&file, &linenum);
2565
2566 /*
2567 * enter a scope, in case we create a local symbol table for local
2568 * variables defined within the 'for' statement
2569 */
2570 enter_scope(&scope_data);
2571
2572 /* parse the open paren */
2573 if (G_tok->next() == TOKT_LPAR)
2574 {
2575 /* skip it */
2576 G_tok->next();
2577 }
2578 else
2579 {
2580 /* log an error, and proceed, assuming it was simply left out */
2581 G_tok->log_error_curtok(TCERR_REQ_FOREACH_LPAR);
2582 }
2583
2584 /* we don't have the iterator lvalue or collection expression yet */
2585 iter_expr = 0;
2586 coll_expr = 0;
2587
2588 /* check for 'local' before the iteration variable */
2589 switch (G_tok->cur())
2590 {
2591 case TOKT_LOCAL:
2592 /*
2593 * if we haven't created our own symbol table local to the 'for'
2594 * loop, do so now
2595 */
2596 create_scope_local_symtab();
2597
2598 /* skip the 'local' keyword and get the local name */
2599 if (G_tok->next() == TOKT_SYM)
2600 {
2601 /* add the local symbol */
2602 local_symtab_->add_local(G_tok->getcur()->get_text(),
2603 G_tok->getcur()->get_text_len(),
2604 alloc_local(), FALSE, FALSE, FALSE);
2605 }
2606 else
2607 {
2608 /* log the error */
2609 G_tok->log_error_curtok(TCERR_LOCAL_REQ_SYM);
2610 }
2611
2612 /* go handle the local as the iteration expression */
2613 goto do_expr;
2614
2615 case TOKT_LPAR:
2616 case TOKT_SYM:
2617 do_expr:
2618 /* parse the iterator lvalue expression */
2619 iter_expr = parse_expr();
2620 if (iter_expr == 0)
2621 {
2622 *err = TRUE;
2623 return 0;
2624 }
2625 break;
2626
2627 default:
2628 /* premature end of the list - log an error and stop */
2629 G_tok->log_error_curtok(TCERR_MISSING_FOREACH_EXPR);
2630 return 0;
2631 }
2632
2633 /* require the 'in' keyword */
2634 if (G_tok->cur() != TOKT_SYM || !G_tok->getcur()->text_matches("in", 2))
2635 {
2636 /* log an error */
2637 G_tok->log_error_curtok(TCERR_FOREACH_REQ_IN);
2638
2639 /* see what we have */
2640 switch(G_tok->cur())
2641 {
2642 case TOKT_LBRACE:
2643 case TOKT_RBRACE:
2644 case TOKT_SEM:
2645 case TOKT_EOF:
2646 /* probably end of statement */
2647 return 0;
2648
2649 case TOKT_RPAR:
2650 /*
2651 * probably an extra paren in the variable expression - skip
2652 * the paren and continue
2653 */
2654 G_tok->next();
2655 break;
2656
2657 default:
2658 /* probably just left out 'in' - continue from here */
2659 break;
2660 }
2661 }
2662 else
2663 {
2664 /* skip the 'in' */
2665 G_tok->next();
2666 }
2667
2668 /* parse the collection expression */
2669 coll_expr = parse_expr();
2670 if (coll_expr == 0)
2671 {
2672 *err = TRUE;
2673 return 0;
2674 }
2675
2676 /* make sure we have the close paren */
2677 if (G_tok->cur() != TOKT_RPAR)
2678 {
2679 /*
2680 * log the error, but continue from here on the assumption that
2681 * they simply left out the paren
2682 */
2683 G_tok->log_error_curtok(TCERR_REQ_FOREACH_RPAR);
2684 }
2685 else
2686 {
2687 /* skip the paren */
2688 G_tok->next();
2689 }
2690
2691 /*
2692 * create the "foreach" node, allocating a private local variable
2693 * for holding the iterator object
2694 */
2695 foreach_stm = new CTPNStmForeach(iter_expr, coll_expr,
2696 local_symtab_, enclosing_stm_,
2697 alloc_local());
2698
2699 /* set the "foreach" node to enclose its body */
2700 old_enclosing = set_enclosing_stm(foreach_stm);
2701
2702 /* parse the body of the loop */
2703 body_stm = parse_stm(err, 0, FALSE);
2704
2705 /* restore the old enclosing statement */
2706 set_enclosing_stm(old_enclosing);
2707
2708 /* if that failed, return failure */
2709 if (*err)
2710 return 0;
2711
2712 /* set the body of the 'for' */
2713 foreach_stm->set_body(body_stm);
2714
2715 /* set the original statement position in the node */
2716 foreach_stm->set_source_pos(file, linenum);
2717
2718 /* set the own-scope flag */
2719 foreach_stm->set_has_own_scope(local_symtab_ != scope_data.local_symtab);
2720
2721 /* exit any local scope we created */
2722 leave_scope(&scope_data);
2723
2724 /* return the new statement node */
2725 return foreach_stm;
2726 }
2727
2728 /*
2729 * Parse a 'break' statement
2730 */
2731 CTPNStm *CTcParser::parse_break(int *err)
2732 {
2733 CTPNStmBreak *brk_stm;
2734
2735 /* create the 'break' statement */
2736 brk_stm = new CTPNStmBreak();
2737
2738 /* skip the 'break' keyword and check what follows */
2739 switch(G_tok->next())
2740 {
2741 case TOKT_SYM:
2742 /* set the label in the statement */
2743 brk_stm->set_label(G_tok->getcur());
2744
2745 /* skip the label token */
2746 G_tok->next();
2747 break;
2748
2749 case TOKT_SEM:
2750 /* keep going - we'll skip it in a moment */
2751 break;
2752
2753 case TOKT_LBRACE:
2754 case TOKT_RBRACE:
2755 /*
2756 * they almost certainly simply left off the semicolon - don't
2757 * bother with a "label expected" error, since the real error is
2758 * most likely just "missing semicolon"
2759 */
2760 break;
2761
2762 default:
2763 /* log the error */
2764 G_tok->log_error_curtok(TCERR_BREAK_REQ_LABEL);
2765 break;
2766 }
2767
2768 /* parse the required terminating semicolon */
2769 if (parse_req_sem())
2770 {
2771 *err = TRUE;
2772 return 0;
2773 }
2774
2775 /* return the 'break' node */
2776 return brk_stm;
2777 }
2778
2779 /*
2780 * Parse a 'continue' statement
2781 */
2782 CTPNStm *CTcParser::parse_continue(int *err)
2783 {
2784 CTPNStmContinue *cont_stm;
2785
2786 /* create the 'break' statement */
2787 cont_stm = new CTPNStmContinue();
2788
2789 /* skip the 'continue' keyword and check what follows */
2790 switch(G_tok->next())
2791 {
2792 case TOKT_SYM:
2793 /* set the label in the statement */
2794 cont_stm->set_label(G_tok->getcur());
2795
2796 /* skip the label token */
2797 G_tok->next();
2798 break;
2799
2800 case TOKT_SEM:
2801 /* keep going - we'll skip it in a moment */
2802 break;
2803
2804 case TOKT_LBRACE:
2805 case TOKT_RBRACE:
2806 /*
2807 * they almost certainly simply left off the semicolon - don't
2808 * bother with a "label expected" error, since the real error is
2809 * most likely just "missing semicolon"
2810 */
2811 break;
2812
2813 default:
2814 /* log the error */
2815 G_tok->log_error_curtok(TCERR_CONT_REQ_LABEL);
2816 break;
2817 }
2818
2819 /* parse the required terminating semicolon */
2820 if (parse_req_sem())
2821 {
2822 *err = TRUE;
2823 return 0;
2824 }
2825
2826 /* return the 'continue' node */
2827 return cont_stm;
2828 }
2829
2830 /*
2831 * Parse a 'while' statement
2832 */
2833 CTPNStm *CTcParser::parse_while(int *err)
2834 {
2835 CTcPrsNode *expr;
2836 CTPNStm *body_stm;
2837 CTPNStmWhile *while_stm;
2838 CTPNStmEnclosing *old_enclosing;
2839
2840 /* skip the 'while' and check for the open paren */
2841 if (G_tok->next() == TOKT_LPAR)
2842 {
2843 /* skip the paren */
2844 G_tok->next();
2845 }
2846 else
2847 {
2848 /*
2849 * log an error, and proceed on the assumption that the paren
2850 * was simply left out and the statement is otherwise
2851 * well-formed
2852 */
2853 G_tok->log_error_curtok(TCERR_REQ_WHILE_LPAR);
2854 }
2855
2856 /* parse the condition expression */
2857 expr = parse_cond_expr();
2858 if (expr == 0)
2859 {
2860 *err = TRUE;
2861 return 0;
2862 }
2863
2864 /* create the 'while' statement node */
2865 while_stm = new CTPNStmWhile(expr, enclosing_stm_);
2866
2867 /* check for the close paren */
2868 if (G_tok->cur() == TOKT_RPAR)
2869 {
2870 /* skip the paren */
2871 G_tok->next();
2872 }
2873 else
2874 {
2875 /* log an error, and continue from here */
2876 G_tok->log_error_curtok(TCERR_REQ_WHILE_RPAR);
2877 }
2878
2879 /* set the 'while' to enclose its body */
2880 old_enclosing = set_enclosing_stm(while_stm);
2881
2882 /* parse the loop body */
2883 body_stm = parse_stm(err, 0, FALSE);
2884
2885 /* restore the old enclosing statement */
2886 set_enclosing_stm(old_enclosing);
2887
2888 /* give up on error */
2889 if (*err)
2890 return 0;
2891
2892 /* set the body */
2893 while_stm->set_body(body_stm);
2894
2895 /* that's it - build and return the 'while' node */
2896 return while_stm;
2897 }
2898
2899 /*
2900 * Parse a 'do-while' statement
2901 */
2902 CTPNStm *CTcParser::parse_do_while(int *err)
2903 {
2904 CTPNStm *body_stm;
2905 CTcPrsNode *expr;
2906 CTPNStmDoWhile *do_stm;
2907 CTPNStmEnclosing *old_enclosing;
2908
2909 /* create the statement object */
2910 do_stm = new CTPNStmDoWhile(enclosing_stm_);
2911
2912 /* skip the 'do' keyword */
2913 G_tok->next();
2914
2915 /* set the 'do' to be the enclosing statement */
2916 old_enclosing = set_enclosing_stm(do_stm);
2917
2918 /* parse the loop body */
2919 body_stm = parse_stm(err, 0, FALSE);
2920
2921 /* restore the enclosing statement */
2922 set_enclosing_stm(old_enclosing);
2923
2924 /* return on failure */
2925 if (*err)
2926 return 0;
2927
2928 /* require the 'while' keyword */
2929 if (G_tok->cur() == TOKT_WHILE)
2930 {
2931 /* skip the 'while' */
2932 G_tok->next();
2933 }
2934 else
2935 {
2936 /*
2937 * no 'while' keyword - there's no obvious way to correct this,
2938 * so simply ignore the 'do' statement and keep going from here,
2939 * on the assumption that they inadvertantly started a new
2940 * statement without finishing the 'do'
2941 */
2942 G_tok->log_error_curtok(TCERR_REQ_DO_WHILE);
2943 }
2944
2945 /* require the open paren */
2946 if (G_tok->cur() == TOKT_LPAR)
2947 G_tok->next();
2948 else
2949 G_tok->log_error_curtok(TCERR_REQ_WHILE_LPAR);
2950
2951 /* parse the expression */
2952 expr = parse_cond_expr();
2953 if (expr == 0)
2954 {
2955 *err = TRUE;
2956 return 0;
2957 }
2958
2959 /* require the close paren */
2960 if (G_tok->cur() == TOKT_RPAR)
2961 G_tok->next();
2962 else
2963 G_tok->log_error_curtok(TCERR_REQ_WHILE_RPAR);
2964
2965 /* set the condition expression and body in the 'do' node */
2966 do_stm->set_cond(expr);
2967 do_stm->set_body(body_stm);
2968
2969 /*
2970 * remember the location of the 'while' part, since this part
2971 * generates code
2972 */
2973 do_stm->set_while_pos(G_tok->get_last_desc(), G_tok->get_last_linenum());
2974
2975 /* parse the required closing semicolon */
2976 if (parse_req_sem())
2977 {
2978 *err = TRUE;
2979 return 0;
2980 }
2981
2982 /* return the new 'do-while' node */
2983 return do_stm;
2984 }
2985
2986 /*
2987 * Parse a 'switch' statement
2988 */
2989 CTPNStm *CTcParser::parse_switch(int *err)
2990 {
2991 CTcPrsNode *expr;
2992 CTPNStmSwitch *switch_stm;
2993 CTPNStm *body_stm;
2994 int skip;
2995 int unreachable_error_shown;
2996 CTPNStmEnclosing *old_enclosing;
2997
2998 /* create the switch statement object */
2999 switch_stm = new CTPNStmSwitch(enclosing_stm_);
3000
3001 /* skip the 'switch' and check for the left paren */
3002 if (G_tok->next() == TOKT_LPAR)
3003 {
3004 /* skip the left paren */
3005 G_tok->next();
3006 }
3007 else
3008 {
3009 /* log an error, and assume the paren is simply missing */
3010 G_tok->log_error_curtok(TCERR_REQ_SWITCH_LPAR);
3011 }
3012
3013 /* parse the controlling expression */
3014 expr = parse_expr();
3015 if (expr == 0)
3016 {
3017 *err = TRUE;
3018 return 0;
3019 }
3020
3021 /* set expression in the switch statement node */
3022 switch_stm->set_expr(expr);
3023
3024 /* check for and skip the close paren */
3025 if (G_tok->cur() == TOKT_RPAR)
3026 {
3027 /* the right paren is present - skip it */
3028 G_tok->next();
3029 }
3030 else
3031 {
3032 /* log an error, and keep going from here */
3033 G_tok->log_error_curtok(TCERR_REQ_SWITCH_RPAR);
3034 }
3035
3036 /* check for and skip the brace */
3037 if (G_tok->cur() == TOKT_LBRACE)
3038 {
3039 /* it's there - skip it */
3040 G_tok->next();
3041 }
3042 else
3043 {
3044 /*
3045 * log an error, and keep going on the assumption that the brace
3046 * is simply missing but the switch body is otherwise correct
3047 */
3048 G_tok->log_error_curtok(TCERR_REQ_SWITCH_LBRACE);
3049 }
3050
3051 /*
3052 * The first thing in the switch body must be a 'case', 'default',
3053 * or closing brace. Other statements preceding the first 'case' or
3054 * 'default' label within the switch body are not allowed, because
3055 * they would be unreachable. Keep skipping statements until we get
3056 * to one of these.
3057 */
3058 for (skip = TRUE, unreachable_error_shown = FALSE ; skip ; )
3059 {
3060 /* see what we have */
3061 switch(G_tok->cur())
3062 {
3063 case TOKT_CASE:
3064 case TOKT_DEFAULT:
3065 case TOKT_RBRACE:
3066 /* this is what we're looking for */
3067 skip = FALSE;
3068 break;
3069
3070 case TOKT_EOF:
3071 /* end of file within the switch - log an error */
3072 G_tok->log_error(TCERR_EOF_IN_SWITCH);
3073
3074 /* return failure */
3075 *err = TRUE;
3076 return 0;
3077
3078 default:
3079 /*
3080 * for anything else, log an error explaining that the code
3081 * is unreachable - do this only once, no matter how many
3082 * unreachable statements precede the first case label
3083 */
3084 if (!unreachable_error_shown && !G_prs->get_syntax_only())
3085 {
3086 /* show the error */
3087 G_tok->log_error(TCERR_UNREACHABLE_CODE_IN_SWITCH);
3088
3089 /*
3090 * note that we've shown the error, so we don't show it
3091 * again if more unreachable statements follow
3092 */
3093 unreachable_error_shown = TRUE;
3094 }
3095
3096 /* parse (and ignore) this statement */
3097 parse_stm(err, switch_stm, FALSE);
3098 if (*err != 0)
3099 return 0;
3100
3101 /* keep looking for the first label */
3102 break;
3103 }
3104 }
3105
3106 /* the 'switch' is the enclosing statement for children */
3107 old_enclosing = set_enclosing_stm(switch_stm);
3108
3109 /* parse the switch body */
3110 body_stm = parse_compound(err, FALSE, TRUE, switch_stm, FALSE);
3111
3112 /* restore the enclosing statement */
3113 set_enclosing_stm(old_enclosing);
3114
3115 /* if we failed to parse the compound statement, give up */
3116 if (*err)
3117 return 0;
3118
3119 /* connect the switch to its body */
3120 switch_stm->set_body(body_stm);
3121
3122 /* return the switch statement node */
3123 return switch_stm;
3124 }
3125
3126 /*
3127 * Parse a 'case' label
3128 */
3129 CTPNStm *CTcParser::parse_case(int *err, CTPNStmSwitch *enclosing_switch)
3130 {
3131 CTcPrsNode *expr;
3132 CTPNStm *stm;
3133 CTPNStmCase *case_stm;
3134
3135 /* skip the 'case' keyword */
3136 G_tok->next();
3137
3138 /* create the 'case' statement node */
3139 case_stm = new CTPNStmCase();
3140
3141 /* parse the expression */
3142 expr = parse_expr();
3143 if (expr == 0)
3144 {
3145 *err = TRUE;
3146 return 0;
3147 }
3148
3149 /* store the expression in the case statement node */
3150 case_stm->set_expr(expr);
3151
3152 /* require the colon */
3153 if (G_tok->cur() == TOKT_COLON)
3154 {
3155 /* skip the colon */
3156 G_tok->next();
3157 }
3158 else
3159 {
3160 /* log the error */
3161 G_tok->log_error_curtok(TCERR_REQ_CASE_COLON);
3162 }
3163
3164 /*
3165 * parse the labeled statement - it's directly within this same
3166 * enclosing switch, because a case label doesn't create a new
3167 * expression nesting level (hence another 'case' label immediately
3168 * following without an intervening statement is perfectly valid)
3169 */
3170 stm = parse_stm(err, enclosing_switch, FALSE);
3171
3172 /* set the statement in the case node */
3173 case_stm->set_stm(stm);
3174
3175 /* count the 'case' label in the 'switch' node */
3176 enclosing_switch->inc_case_cnt();
3177
3178 /* return the case node */
3179 return case_stm;
3180 }
3181
3182 /*
3183 * Parse a 'default' label
3184 */
3185 CTPNStm *CTcParser::parse_default(int *err, CTPNStmSwitch *enclosing_switch)
3186 {
3187 CTPNStm *stm;
3188 CTPNStmDefault *default_stm;
3189
3190 /* create the 'default' statement node */
3191 default_stm = new CTPNStmDefault();
3192
3193 /*
3194 * if the enclosing 'switch' already has a 'default' label, it's an
3195 * error; continue anyway, since we still want to finish parsing the
3196 * syntax
3197 */
3198 if (enclosing_switch->get_has_default())
3199 G_tok->log_error(TCERR_DEFAULT_REDEF);
3200
3201 /* mark the switch as having a 'default' case */
3202 enclosing_switch->set_has_default();
3203
3204 /* skip the 'default' node, and require the colon */
3205 if (G_tok->next() == TOKT_COLON)
3206 {
3207 /* skip the colon */
3208 G_tok->next();
3209 }
3210 else
3211 {
3212 /*
3213 * log an error, and keep going, assuming that the token was
3214 * accidentally omitted
3215 */
3216 G_tok->log_error_curtok(TCERR_REQ_DEFAULT_COLON);
3217 }
3218
3219 /*
3220 * parse the labeled statement - it's directly within this same
3221 * enclosing switch, because a 'default' label doesn't create a new
3222 * expression nesting level (hence another 'case' label immediately
3223 * following without an intervening statement is perfectly valid)
3224 */
3225 stm = parse_stm(err, enclosing_switch, FALSE);
3226
3227 /* set the statement in the 'default' node */
3228 default_stm->set_stm(stm);
3229
3230 /* return the 'default' node */
3231 return default_stm;
3232 }
3233
3234 /*
3235 * Parse a 'goto' statement
3236 */
3237 CTPNStm *CTcParser::parse_goto(int *err)
3238 {
3239 CTPNStmGoto *goto_stm;
3240
3241 /* skip the 'goto' keyword, and demand that a symbol follows */
3242 if (G_tok->next() == TOKT_SYM)
3243 {
3244 /* create the parse node for the 'goto' statement */
3245 goto_stm = new CTPNStmGoto(G_tok->getcur()->get_text(),
3246 G_tok->getcur()->get_text_len());
3247
3248 /* skip the symbol */
3249 G_tok->next();
3250 }
3251 else
3252 {
3253 /* log the error */
3254 G_tok->log_error(TCERR_GOTO_REQ_LABEL);
3255
3256 /* no statement */
3257 goto_stm = 0;
3258 }
3259
3260 /* parse the required semicolon */
3261 if (parse_req_sem())
3262 {
3263 *err = TRUE;
3264 return 0;
3265 }
3266
3267 /* return the statement node */
3268 return goto_stm;
3269 }
3270
3271 /*
3272 * Parse a 'try-catch-finally' statement
3273 */
3274 CTPNStm *CTcParser::parse_try(int *err)
3275 {
3276 CTPNStmTry *try_stm;
3277 CTPNStmEnclosing *old_enclosing;
3278 CTPNStm *body_stm;
3279
3280 /* create the 'try' statement node */
3281 try_stm = new CTPNStmTry(enclosing_stm_);
3282
3283 /*
3284 * the 'try' is the enclosing statement for the duration of its
3285 * protected code block
3286 */
3287 old_enclosing = set_enclosing_stm(try_stm);
3288
3289 /* skip the 'try' */
3290 G_tok->next();
3291
3292 /* parse the body of the 'try' block */
3293 body_stm = parse_stm(err, 0, FALSE);
3294
3295 /* restore the previous enclosing statement */
3296 set_enclosing_stm(old_enclosing);
3297
3298 /* if parsing the body failed, stop now */
3299 if (*err)
3300 return 0;
3301
3302 /* add the body to the 'try' */
3303 try_stm->set_body_stm(body_stm);
3304
3305 /*
3306 * check for 'catch' clauses - there could be several, so keep going
3307 * until we stop seeing 'catch' keywords
3308 */
3309 while (G_tok->cur() == TOKT_CATCH)
3310 {
3311 int catch_has_err;
3312 CTPNStm *catch_body;
3313 CTPNStmCatch *catch_stm;
3314 tcprs_scope_t scope_data;
3315
3316 /* create a local scope for the 'catch' clause */
3317 enter_scope(&scope_data);
3318 create_scope_local_symtab();
3319
3320 /* create the 'catch' statement node */
3321 catch_stm = new CTPNStmCatch();
3322
3323 /*
3324 * set the 'catch' clause's source position independently of the
3325 * overall 'try' statement, so that the debugger can track entry
3326 * into this clause
3327 */
3328 catch_stm->set_source_pos(G_tok->get_last_desc(),
3329 G_tok->get_last_linenum());
3330
3331 /* presume we'll parse this successfully */
3332 catch_has_err = FALSE;
3333
3334 /* skip the 'catch' keyword and check for the left paren */
3335 if (G_tok->next() == TOKT_LPAR)
3336 {
3337 /* skip the paren */
3338 G_tok->next();
3339 }
3340 else
3341 {
3342 /* log the error */
3343 G_tok->log_error_curtok(TCERR_REQ_CATCH_LPAR);
3344 }
3345
3346 /* get the exception class token */
3347 if (G_tok->cur() == TOKT_SYM)
3348 {
3349 /* set the class name in the 'catch' clause */
3350 catch_stm->set_exc_class(G_tok->getcur());
3351
3352 /* move on */
3353 G_tok->next();
3354 }
3355 else
3356 {
3357 /* flag the problem */
3358 G_tok->log_error_curtok(TCERR_REQ_CATCH_CLASS);
3359
3360 /* unless this is a close paren, skip the errant token */
3361 if (G_tok->cur() != TOKT_RPAR)
3362 G_tok->next();
3363
3364 /* note the error */
3365 catch_has_err = TRUE;
3366 }
3367
3368 /* get the variable name token */
3369 if (G_tok->cur() == TOKT_SYM)
3370 {
3371 CTcSymLocal *var;
3372
3373 /*
3374 * create the local variable - note that this variable is
3375 * implicitly assigned when the 'catch' clause is entered, so
3376 * mark it as initially assigned; we don't care if the
3377 * variable is ever used, so also mark it as used
3378 */
3379 var = local_symtab_->add_local(G_tok->getcur()->get_text(),
3380 G_tok->getcur()->get_text_len(),
3381 alloc_local(),
3382 FALSE, TRUE, TRUE);
3383
3384 /* set the variable in the 'catch' clause */
3385 if (!catch_has_err)
3386 catch_stm->set_exc_var(var);
3387
3388 /* move on */
3389 G_tok->next();
3390 }
3391 else
3392 {
3393 /* flag the problem */
3394 G_tok->log_error_curtok(TCERR_REQ_CATCH_VAR);
3395
3396 /* unless this is a close paren, skip the errant token */
3397 if (G_tok->cur() != TOKT_RPAR)
3398 G_tok->next();
3399
3400 /* note the error */
3401 catch_has_err = TRUE;
3402 }
3403
3404 /* check for the close paren */
3405 if (G_tok->cur() == TOKT_RPAR)
3406 {
3407 /* skip the paren */
3408 G_tok->next();
3409 }
3410 else
3411 {
3412 /*
3413 * log the error and continue, assuming that the paren is
3414 * simply missing and things are otherwise okay
3415 */
3416 G_tok->log_error_curtok(TCERR_REQ_CATCH_RPAR);
3417 }
3418
3419 /*
3420 * parse the 'catch' statement block - we've already established
3421 * a special scope for the 'catch' block, so don't start a new
3422 * scope if the block contains a compound statement
3423 */
3424 catch_body = parse_stm(err, 0, TRUE);
3425
3426 /* leave the special 'catch' scope */
3427 leave_scope(&scope_data);
3428
3429 /* if the statement block failed, give up now */
3430 if (*err)
3431 return 0;
3432
3433 /* add the 'catch' clause to the 'try' if we were successful */
3434 if (!catch_has_err)
3435 {
3436 /* set the 'catch' node's body */
3437 catch_stm->set_body(catch_body);
3438
3439 /* set the local scope in the 'catch' */
3440 catch_stm->set_symtab(local_symtab_);
3441
3442 /* add the 'catch' to the 'try' */
3443 try_stm->add_catch(catch_stm);
3444 }
3445 }
3446
3447 /* check for a 'finally' clause */
3448 if (G_tok->cur() == TOKT_FINALLY)
3449 {
3450 CTPNStmFinally *fin_stm;
3451 CTPNStm *fin_body;
3452 tcprs_scope_t scope_data;
3453 CTPNStmEnclosing *old_enclosing;
3454
3455 /*
3456 * the locals we allocate for the 'finally' are in the finally's
3457 * own scope - the slots can be reused later
3458 */
3459 enter_scope(&scope_data);
3460
3461 /* create the 'finally' node */
3462 fin_stm = new CTPNStmFinally(enclosing_stm_,
3463 alloc_local(), alloc_local());
3464
3465 /*
3466 * set the 'finally' clause's source position - we want this
3467 * clause to have its own source position independent of the
3468 * 'try' statement of which it is a part, so that the debugger
3469 * can keep track of entry into this clause's generated code
3470 */
3471 fin_stm->set_source_pos(G_tok->get_last_desc(),
3472 G_tok->get_last_linenum());
3473
3474 /* skip the 'finally' keyword */
3475 G_tok->next();
3476
3477 /* set the 'finally' to enclose its body */
3478 old_enclosing = set_enclosing_stm(fin_stm);
3479
3480 /* parse the 'finally' statement block */
3481 fin_body = parse_stm(err, 0, FALSE);
3482
3483 /* set the 'finally' block's closing position, if present */
3484 if (fin_body != 0)
3485 {
3486 fin_stm->set_end_pos(
3487 fin_body->get_end_desc(), fin_body->get_end_linenum());
3488 }
3489
3490 /* restore the enclosing statement */
3491 set_enclosing_stm(old_enclosing);
3492
3493 /* we're done with the special scope */
3494 leave_scope(&scope_data);
3495
3496 /* if that failed, give up now */
3497 if (*err)
3498 return 0;
3499
3500 /* set the 'finally' node's body */
3501 fin_stm->set_body(fin_body);
3502
3503 /* add the 'finally' to the 'try' */
3504 try_stm->set_finally(fin_stm);
3505 }
3506
3507 /* make sure we have at least one 'catch' or 'finally' clause */
3508 if (!try_stm->has_catch_or_finally())
3509 try_stm->log_error(TCERR_TRY_WITHOUT_CATCH);
3510
3511 /* return the 'try' statement node */
3512 return try_stm;
3513 }
3514
3515 /*
3516 * Parse a 'throw' statement
3517 */
3518 CTPNStm *CTcParser::parse_throw(int *err)
3519 {
3520 CTcPrsNode *expr;
3521 CTPNStmThrow *throw_stm;
3522
3523 /* skip the 'throw' keyword */
3524 G_tok->next();
3525
3526 /* parse the expression to be thrown */
3527 expr = parse_expr();
3528 if (expr == 0)
3529 {
3530 *err = TRUE;
3531 return 0;
3532 }
3533
3534 /* create the statement node */
3535 throw_stm = new CTPNStmThrow(expr);
3536
3537 /* require a terminating semicolon */
3538 if (parse_req_sem())
3539 {
3540 *err = TRUE;
3541 return 0;
3542 }
3543
3544 /* return the statement node */
3545 return throw_stm;
3546 }
3547
3548
3549 /* ------------------------------------------------------------------------ */
3550 /*
3551 * Unary operator parsing
3552 */
3553
3554 /*
3555 * Anonymous function symbol table preparer. This class creates the lcoal
3556 * symbol table for an anonymous function or method.
3557 */
3558 class CAnonFuncSymtabPrep
3559 {
3560 public:
3561 /*
3562 * Create a local symbol table for an anonymous function or method.
3563 */
3564 CTcPrsSymtab *create_symtab()
3565 {
3566 /*
3567 * Create a new local symbol table to represent the enclosing scope
3568 * of the new nested scope. Unlike most nested scopes, we can't
3569 * simply plug in the current scope's symbol table - instead, we
3570 * have to build a special representation of the enclosing scope to
3571 * handle the "closure" behavior. The enclosing scope's local
3572 * variable set effectively becomes an object rather than a simple
3573 * stack frame. The enclosing lexical scope and the anonymous
3574 * function then both reference the shared locals object.
3575 *
3576 * This synthesized enclosing scope will directly represent all of
3577 * the nested scopes up but not including to the root global scope.
3578 * The global scope doesn't need the special closure
3579 * representation, since it's already shared among all lexical
3580 * scopes anyway. Since we're not linking in to the enclosing
3581 * scope list the way we normally would, we need to explicitly link
3582 * in the global scope. To do this, we can simply make the global
3583 * scope the enclosing scope of our synthesized outer scope table.
3584 *
3585 */
3586 enc_symtab = new CTcPrsSymtab(G_prs->get_global_symtab());
3587
3588 /*
3589 * fill the new local symbol table with the inherited local symbols
3590 * from the current local scope and any enclosing scopes - but stop
3591 * when we reach the global scope, since this is already shared by
3592 * all scopes and doesn't need any special closure representation
3593 */
3594 for (CTcPrsSymtab *tab = G_prs->get_local_symtab() ;
3595 tab != 0 && tab != G_prs->get_global_symtab() ;
3596 tab = tab->get_parent())
3597 {
3598 /* enumerate entries in this table */
3599 tab->enum_entries(&enum_for_anon, this);
3600 }
3601
3602 /*
3603 * Create the local symbol table for *within* the new anonymous
3604 * fucntion scope. This is another local symbol table, this time
3605 * nested within the table containing the locals shared from the
3606 * enclosing scope. This one will contain any formals defined for
3607 * the anonymous function, which hide inherited locals from the
3608 * enclosing scope.
3609 */
3610 return new CTcPrsSymtab(enc_symtab);
3611 }
3612
3613 /*
3614 * Finish the symbol table. Call this after parsing the code body, to
3615 * convert referenced locals in enclosing scopes to context locals.
3616 */
3617 void finish()
3618 {
3619 /*
3620 * Enumerate all of the entries in our scope once again - this
3621 * time, we want to determine if there are any variables that were
3622 * not previously referenced from anonymous functions but have been
3623 * now; we need to convert all such variables to context locals.
3624 */
3625 for (CTcPrsSymtab *tab = G_prs->get_local_symtab() ;
3626 tab != 0 && tab != G_prs->get_global_symtab() ;
3627 tab = tab->get_parent())
3628 {
3629 /* enumerate entries in this table */
3630 tab->enum_entries(&enum_for_anon2, this);
3631 }
3632 }
3633
3634 private:
3635 /*
3636 * enumeration callback, phase 1: add a proxy context local to our
3637 * "enclosing" symbol table for each actual local defined in a parent
3638 * scope
3639 */
3640 static void enum_for_anon(void *ctx, CTcSymbol *sym)
3641 {
3642 /* get 'this' from the context */
3643 CAnonFuncSymtabPrep *self = (CAnonFuncSymtabPrep *)ctx;
3644
3645 /*
3646 * If this symbol is already in our table, another symbol from an
3647 * enclosed scope hides it, so ignore this one. Note that we're
3648 * only interested in the symbols defined directly in our table -
3649 * we hide symbols defined in the enclosing global scope, so we
3650 * don't care if they're already defined there.
3651 */
3652 if (self->enc_symtab->find_direct(
3653 sym->get_sym(), sym->get_sym_len()) != 0)
3654 return;
3655
3656 /* create a context-variable copy */
3657 CTcSymbol *new_sym = sym->new_ctx_var();
3658
3659 /* if we got a new symbol, add it to the new symbol table */
3660 if (new_sym != 0)
3661 self->enc_symtab->add_entry(new_sym);
3662 }
3663
3664 /*
3665 * enumeration callback, phase 2: convert locals in enclosing scopes
3666 * that are actually referenced in the anonymous function to context
3667 * locals
3668 */
3669 static void enum_for_anon2(void *ctx, CTcSymbol *sym)
3670 {
3671 /* get 'this' from the context */
3672 CAnonFuncSymtabPrep *self = (CAnonFuncSymtabPrep *)ctx;
3673
3674 /*
3675 * If this symbol isn't in the anonymous function's local symbol
3676 * table, the anonymous function didn't end up using it. The fact
3677 * that it's in the enclosing table in this case simply means that
3678 * it's a context variable in the enclosing context.
3679 */
3680 if (self->enc_symtab->find_direct(
3681 sym->get_sym(), sym->get_sym_len()) == 0)
3682 return;
3683
3684 /* ask the symbol to apply the necessary conversion */
3685 sym->finish_ctx_var_conv();
3686 }
3687
3688 /* enclosing symbol table */
3689 CTcPrsSymtab *enc_symtab;
3690 };
3691
3692 /*
3693 * Parse an anonymous function
3694 */
3695 CTPNAnonFunc *CTcPrsOpUnary::parse_anon_func(int short_form, int is_method)
3696 {
3697 /*
3698 * our code body type can be an anonymous function, a short anonymous
3699 * function, or an anonymous method
3700 */
3701 tcprs_codebodytype cb_type =
3702 (short_form ? TCPRS_CB_SHORT_ANON_FN :
3703 G_tok->cur() == TOKT_METHOD ? TCPRS_CB_ANON_METHOD :
3704 TCPRS_CB_ANON_FN);
3705
3706 /* skip the initial token */
3707 G_tok->next();
3708
3709 /* prepare the enclosing local symbol table for the new nested scope */
3710 CAnonFuncSymtabPrep symprep;
3711 CTcPrsSymtab *lcltab = symprep.create_symtab();
3712
3713 /*
3714 * Parse the code body.
3715 *
3716 * If it's a function (not a method), it shares the method context from
3717 * the lexically enclosing scope, so it has access to 'self' and other
3718 * method context variables if and only if the enclosing scope does.
3719 * If it's a method, it has access to the live method context, so the
3720 * enclosing scope's status is irrelevant.
3721 */
3722 int err = 0;
3723 int has_retval;
3724 CTPNCodeBody *code_body = G_prs->parse_nested_code_body(
3725 FALSE, is_method || G_prs->is_self_valid(),
3726 0, 0, 0, 0, 0, &has_retval, &err, lcltab, cb_type);
3727
3728 /* if that failed, return failure */
3729 if (code_body == 0 || err != 0)
3730 return 0;
3731
3732 /*
3733 * If this is an anonymous function (not an anonymous method), and the
3734 * nested code body references 'self' or the full method context, then
3735 * so does the enclosing code body, and we need these variables in the
3736 * local context for the topmost enclosing code body.
3737 *
3738 * If this is an anonymous method, references to the method context are
3739 * to the live frame at the time of invocation, not to the saved
3740 * lexical frame at the time of creation. So references from within
3741 * the method's code body don't create references on the enclosing
3742 * scope, and they don't require any shared context information.
3743 */
3744 if (!is_method)
3745 {
3746 if (code_body->self_referenced())
3747 {
3748 G_prs->set_self_referenced(TRUE);
3749 G_prs->set_local_ctx_needs_self(TRUE);
3750 }
3751 if (code_body->full_method_ctx_referenced())
3752 {
3753 G_prs->set_full_method_ctx_referenced(TRUE);
3754 G_prs->set_local_ctx_needs_full_method_ctx(TRUE);
3755 }
3756 }
3757 else
3758 {
3759 /* mark it as a method */
3760 code_body->set_anon_method(TRUE);
3761 }
3762
3763 /* finish the symbol table */
3764 symprep.finish();
3765
3766 /*
3767 * if this is a function (not a method), and there's a 'self' object in
3768 * the enclosing context, and we referenced 'self' or the full method
3769 * context in the nested code body, we'll definitely need a local
3770 * context, so make sure we have one initialized even if we don't have
3771 * any local variables shared
3772 */
3773 if (!is_method
3774 && G_prs->is_self_valid()
3775 && (code_body->self_referenced()
3776 || code_body->full_method_ctx_referenced()))
3777 {
3778 /* initialize a local context here, in the enclosing level */
3779 G_prs->init_local_ctx();
3780 }
3781
3782 /*
3783 * add the code body to the parser's master list of nested top-level
3784 * statements for the current program
3785 */
3786 G_prs->add_nested_stm(code_body);
3787
3788 /* return a new anonymous function node */
3789 return new CTPNAnonFunc(code_body, has_retval, is_method);
3790 }
3791
3792 /*
3793 * Parse an in-line object definition.
3794 */
3795 class CTPNInlineObject *CTcPrsOpUnary::parse_inline_object(int has_colon)
3796 {
3797 /* create the inline object node */
3798 CTPNInlineObject *obj = new CTPNInlineObject();
3799
3800 /* if the caller didn't already check for a colon, do so now */
3801 if (!has_colon && G_tok->next() == TOKT_COLON)
3802 {
3803 /* note and skip the colon */
3804 has_colon = TRUE;
3805 G_tok->next();
3806 }
3807
3808 /* if there's a colon, parse the superclass list */
3809 if (has_colon)
3810 G_prs->parse_superclass_list(0, obj->get_superclass_list());
3811
3812 /* next we need a left brace '{' */
3813 if (G_tok->cur() != TOKT_LBRACE)
3814 {
3815 G_tok->log_error_curtok(TCERR_INLINE_OBJ_REQ_LBRACE);
3816 return 0;
3817 }
3818
3819 /* skip the brace */
3820 G_tok->next();
3821
3822 /* parse the property list */
3823 int err = FALSE;
3824 tcprs_term_info outer_term;
3825 if (!G_prs->parse_obj_prop_list(
3826 &err, obj, 0, FALSE, FALSE, TRUE, TRUE, &outer_term, &outer_term))
3827 return 0;
3828
3829 /* return the object node */
3830 return obj;
3831 }
3832
3833
3834 /* ------------------------------------------------------------------------ */
3835 /*
3836 * State save structure for parsing property expressions
3837 */
3838 class CTcPrsPropExprSave
3839 {
3840 public:
3841 unsigned int has_local_ctx_ : 1;
3842 int local_ctx_var_num_;
3843 size_t ctx_var_props_used_;
3844 int next_ctx_arr_idx_;
3845 int local_cnt_;
3846 int max_local_cnt_;
3847 int self_referenced_;
3848 int self_valid_;
3849 int full_method_ctx_referenced_;
3850 int local_ctx_needs_self_;
3851 int local_ctx_needs_full_method_ctx_;
3852 struct CTcCodeBodyRef *cur_code_body_;
3853 CTPNStmEnclosing *enclosing_stm_;
3854 class CTcPrsSymtab *local_symtab_;
3855 class CTcPrsSymtab *enclosing_local_symtab_;
3856 class CTcPrsSymtab *goto_symtab_;
3857 CAnonFuncSymtabPrep inline_prep_;
3858 };
3859
3860
3861 /* ------------------------------------------------------------------------ */
3862 /*
3863 * Parse an object superclass list
3864 */
3865 void CTcParser::parse_superclass_list(
3866 CTcSymObj *obj_sym, CTPNSuperclassList &sclist)
3867 {
3868 /* scan the list */
3869 for (int done = FALSE ; !done ; )
3870 {
3871 /* we need a symbol */
3872 switch(G_tok->cur())
3873 {
3874 case TOKT_SYM:
3875 /* a symbol must be a superclass name - look it up */
3876 {
3877 const CTcToken *tok = G_tok->getcur();
3878 CTcSymObj *sc_sym = (CTcSymObj *)get_global_symtab()->find(
3879 tok->get_text(), tok->get_text_len());
3880
3881 /*
3882 * If this symbol is defined, and it's an object, check to
3883 * make sure this won't set up a circular class definition
3884 * - so, make sure the base class isn't the same as the
3885 * object being defined, and that it doesn't inherit from
3886 * the object being defined.
3887 */
3888 if (sc_sym != 0
3889 && obj_sym != 0
3890 && sc_sym->get_type() == TC_SYM_OBJ
3891 && (sc_sym == obj_sym || sc_sym->has_superclass(obj_sym)))
3892 {
3893 /*
3894 * this is a circular class definition - complain about
3895 * it and don't add it to my superclass list
3896 */
3897 G_tok->log_error(TCERR_CIRCULAR_CLASS_DEF,
3898 (int)sc_sym->get_sym_len(),
3899 sc_sym->get_sym(),
3900 (int)obj_sym->get_sym_len(),
3901 obj_sym->get_sym());
3902 }
3903 else
3904 {
3905 /* it's good - add the new superclass to our list */
3906 sclist.append(new CTPNSuperclass(
3907 tok->get_text(), tok->get_text_len()));
3908
3909 /*
3910 * add it to the symbol's superclass name list as well
3911 * - we use this for keeping track of the hierarchy in
3912 * the symbol file for compile-time access
3913 */
3914 if (obj_sym != 0)
3915 obj_sym->add_sc_name_entry(
3916 tok->get_text(), tok->get_text_len());
3917 }
3918
3919 /* skip the symbol and see what follows */
3920 switch (G_tok->next())
3921 {
3922 case TOKT_COMMA:
3923 /* we have another superclass following */
3924 G_tok->next();
3925 break;
3926
3927 default:
3928 /* no more superclasses */
3929 done = TRUE;
3930 break;
3931 }
3932 }
3933 break;
3934
3935 case TOKT_OBJECT:
3936 /*
3937 * it's a basic object definition - make sure other
3938 * superclasses weren't specified
3939 */
3940 if (sclist.head_ != 0)
3941 G_tok->log_error(TCERR_OBJDEF_OBJ_NO_SC);
3942
3943 /*
3944 * mark the object as having an explicit superclass of the root
3945 * object class
3946 */
3947 if (obj_sym != 0)
3948 obj_sym->set_sc_is_root(TRUE);
3949
3950 /*
3951 * skip the 'object' keyword and we're done - there's no
3952 * superclass list
3953 */
3954 G_tok->next();
3955 done = TRUE;
3956 break;
3957
3958 default:
3959 /* premature end of the object list */
3960 G_tok->log_error_curtok(TCERR_OBJDEF_REQ_SC);
3961
3962 /* stop here */
3963 done = TRUE;
3964 break;
3965 }
3966 }
3967 }
3968
3969 /*
3970 * Parse an object template instance at the beginning of an object body
3971 */
3972 void CTcParser::parse_obj_template(int *err, CTPNObjDef *objdef, int is_inline)
3973 {
3974 /* check the current token for a template use */
3975 switch(G_tok->cur())
3976 {
3977 case TOKT_SSTR:
3978 case TOKT_SSTR_START:
3979 case TOKT_DSTR:
3980 case TOKT_DSTR_START:
3981 case TOKT_LBRACK:
3982 case TOKT_AT:
3983 case TOKT_PLUS:
3984 case TOKT_MINUS:
3985 case TOKT_TIMES:
3986 case TOKT_DIV:
3987 case TOKT_MOD:
3988 case TOKT_ARROW:
3989 case TOKT_AND:
3990 case TOKT_NOT:
3991 case TOKT_BNOT:
3992 case TOKT_COMMA:
3993 /* we have an object template */
3994 break;
3995
3996 default:
3997 /* it's not a template - simply return without parsing anything */
3998 return;
3999 }
4000
4001 /* parse the expressions until we reach the end of the template */
4002 size_t cnt;
4003 CTcObjTemplateInst *p;
4004 int done;
4005 CTcPrsPropExprSave save_info;
4006 for (cnt = 0, p = template_expr_, done = FALSE ; !done ; ++cnt)
4007 {
4008 /*
4009 * remember the statment start location, in case we have a
4010 * template element that generates code (such as a double-quoted
4011 * string with an embedded expression)
4012 */
4013 cur_desc_ = G_tok->get_last_desc();
4014 cur_linenum_ = G_tok->get_last_linenum();
4015
4016 /*
4017 * note the token, so that we can figure out which template we
4018 * are using
4019 */
4020 p->def_tok_ = G_tok->cur();
4021
4022 /* assume this will also be the first token of the value expression */
4023 p->expr_tok_ = *G_tok->copycur();
4024
4025 /* we don't have any expression yet */
4026 p->expr_ = 0;
4027 p->code_body_ = 0;
4028 p->inline_method_ = 0;
4029
4030 /* prepare to parse a property value expression */
4031 begin_prop_expr(&save_info, FALSE, is_inline);
4032
4033 /* check to see if this is another template item */
4034 switch(G_tok->cur())
4035 {
4036 case TOKT_SSTR:
4037 /* single-quoted string - parse just the string */
4038 p->expr_ = CTcPrsOpUnary::parse_primary();
4039 break;
4040
4041 case TOKT_SSTR_START:
4042 /* start of single-quoted embedded expression string - parse it */
4043 p->expr_ = CTcPrsOpUnary::parse_primary();
4044
4045 /* treat it as a regular string for template matching */
4046 p->def_tok_ = TOKT_SSTR;
4047 break;
4048
4049 case TOKT_DSTR:
4050 /* string - parse it */
4051 p->expr_ = parse_expr_or_dstr(TRUE);
4052 break;
4053
4054 case TOKT_DSTR_START:
4055 /* start of a double-quoted embedded expression string */
4056 p->expr_ = parse_expr_or_dstr(TRUE);
4057
4058 /*
4059 * treat it as a regular double-quoted string for the
4060 * purposes of matching the template
4061 */
4062 p->def_tok_ = TOKT_DSTR;
4063 break;
4064
4065 case TOKT_LBRACK:
4066 /* it's a list */
4067 p->expr_ = CTcPrsOpUnary::parse_list();
4068 break;
4069
4070 case TOKT_AT:
4071 case TOKT_PLUS:
4072 case TOKT_MINUS:
4073 case TOKT_TIMES:
4074 case TOKT_DIV:
4075 case TOKT_MOD:
4076 case TOKT_ARROW:
4077 case TOKT_AND:
4078 case TOKT_NOT:
4079 case TOKT_BNOT:
4080 case TOKT_COMMA:
4081 /* skip the operator token */
4082 G_tok->next();
4083
4084 /* the value expression starts with this token */
4085 p->expr_tok_ = *G_tok->copycur();
4086
4087 /* a primary expression must follow */
4088 p->expr_ = CTcPrsOpUnary::parse_primary();
4089 break;
4090
4091 case TOKT_EOF:
4092 /* end of file - return and let the caller deal with it */
4093 return;
4094
4095 default:
4096 /* anything else ends the template list */
4097 done = TRUE;
4098
4099 /* don't count this item after all */
4100 --cnt;
4101 break;
4102 }
4103
4104 /*
4105 * check for embedded anonymous functions, and wrap the expression
4106 * in a code body if necessary
4107 */
4108 finish_prop_expr(
4109 &save_info, p->expr_, p->code_body_, p->inline_method_,
4110 FALSE, is_inline, 0);
4111
4112 /*
4113 * move on to the next expression slot if we have room (if we
4114 * don't, we won't match anything anyway; just keep writing over
4115 * the last slot so that we can at least keep parsing entries)
4116 */
4117 if (cnt + 1 < template_expr_max_)
4118 ++p;
4119 }
4120
4121 /* we have no matching template yet */
4122 const CTcObjTemplate *tpl = 0;
4123 const CTPNSuperclass *def_sc = 0;
4124
4125 /* presume we don't have any undescribed classes in our hierarchy */
4126 int undesc_class = FALSE;
4127
4128 /*
4129 * Search for the template, using the normal inheritance rules that we
4130 * use at run-time: start with the first superclass and look for a
4131 * match; if we find a match, look at subsequent superclasses to look
4132 * for one that overrides the match.
4133 */
4134 if (objdef != 0)
4135 {
4136 /* search our superclasses for a match */
4137 tpl = find_class_template(
4138 objdef->get_first_sc(), template_expr_, cnt,
4139 &def_sc, &undesc_class);
4140
4141 /* remember the 'undescribed class' status */
4142 objdef->set_undesc_sc(undesc_class);
4143 }
4144
4145 /* if we didn't find a match, look for a root object match */
4146 if (tpl == 0 && !undesc_class)
4147 tpl = find_template_match(template_head_, template_expr_, cnt);
4148
4149 /* if we didn't find a match, it's an error */
4150 if (tpl == 0)
4151 {
4152 /*
4153 * Note the error, but don't report it yet. It might be that we
4154 * failed to find a template match simply because one of our
4155 * superclass names was misspelled. If that's the case, then the
4156 * missing template is the least of our problems, and it's not
4157 * worth reporting since it's probably just a side effect of the
4158 * missing superclass (that is, once the superclass misspelling is
4159 * corrected and the code is re-compiled, we might find that the
4160 * template is correct after all, since we'll know which class to
4161 * scan for the needed template.) At code generation time, we'll
4162 * be able to resolve the superclasses and find out what's really
4163 * going on, so that we can flag the appropriate error.
4164 */
4165 if (objdef != 0)
4166 objdef->note_bad_template(TRUE);
4167
4168 /* ignore the template instance */
4169 return;
4170 }
4171
4172 /* if there's no object statement, there's nothing left to do */
4173 if (objdef == 0)
4174 return;
4175
4176 /*
4177 * we know we have a matching template, so populate our actual
4178 * parameter list with the property identifiers for the matching
4179 * template
4180 */
4181 match_template(tpl->items_, template_expr_, cnt);
4182
4183 /* define the property values according to the template */
4184 for (p = template_expr_ ; cnt != 0 ; ++p, --cnt)
4185 {
4186 /* add this property */
4187 if (p->code_body_ != 0)
4188 objdef->add_method(p->prop_, p->code_body_, p->expr_, FALSE);
4189 else if (p->inline_method_ != 0)
4190 objdef->add_inline_method(
4191 p->prop_, p->inline_method_, p->expr_, FALSE);
4192 else if (p->expr_ != 0)
4193 objdef->add_prop(p->prop_, p->expr_, FALSE, FALSE);
4194 }
4195 }
4196
4197 /* ------------------------------------------------------------------------ */
4198 /*
4199 * search a class for a template match
4200 */
4201 const CTcObjTemplate *CTcParser::
4202 find_class_template(const CTPNSuperclass *first_sc,
4203 CTcObjTemplateInst *src, size_t src_cnt,
4204 const CTPNSuperclass **def_sc,
4205 int *undesc_class)
4206 {
4207 /* scan each superclass in the list for a match */
4208 const CTPNSuperclass *sc;
4209 const CTcObjTemplate *tpl;
4210 for (tpl = 0, sc = first_sc ; sc != 0 ; sc = sc->nxt_)
4211 {
4212 /* find the symbol for this superclass */
4213 CTcSymObj *sc_sym = (CTcSymObj *)get_global_symtab()->find(
4214 sc->get_sym_txt(), sc->get_sym_len());
4215
4216 /* if there's no symbol, or it's not a tads-object, give up */
4217 if (sc_sym == 0
4218 || sc_sym->get_type() != TC_SYM_OBJ
4219 || sc_sym->get_metaclass() != TC_META_TADSOBJ)
4220 {
4221 /*
4222 * this class has an invalid superclass - just give up without
4223 * issuing any errors now, since we'll have plenty to say
4224 * about this when building the object file data
4225 */
4226 return 0;
4227 }
4228
4229 /* find a match in this superclass hierarchy */
4230 const CTcObjTemplate *cur_tpl = find_template_match(
4231 sc_sym->get_first_template(), src, src_cnt);
4232
4233 /* see what we found */
4234 const CTPNSuperclass *cur_def_sc;
4235 if (cur_tpl != 0)
4236 {
4237 /* we found it - note the current defining superclass */
4238 cur_def_sc = sc;
4239 }
4240 else
4241 {
4242 /*
4243 * If this one has no superclass list, and it's not explicitly
4244 * a subclass of the root class, then this is an undescribed
4245 * class and cannot be used with templates at all. A class is
4246 * undescribed when it is explicitly declared as 'extern', and
4247 * does not have a definition in any imported symbol file in
4248 * the current compilation. If this is the case, flag it so
4249 * the caller will know we have an undescribed class.
4250 *
4251 * Note that we only set this flag if we failed to find a
4252 * template. A template can still be used if a matching
4253 * template is explicitly defined on the class in this
4254 * compilation unit, since in that case we don't need to look
4255 * up the inheritance hierarchy for the class. That's why we
4256 * set the flag here, only after we have failed to find a
4257 * template for the object.
4258 */
4259 if (sc_sym->get_sc_name_head() == 0 && !sc_sym->sc_is_root())
4260 {
4261 /* tell the caller we have an undescribed class */
4262 *undesc_class = TRUE;
4263
4264 /*
4265 * there's no need to look any further, since any matches
4266 * we might find among our other superclasses would be in
4267 * doubt because of the lack of information about this
4268 * earlier class, which might override later superclasses
4269 * if we knew more about it
4270 */
4271 return 0;
4272 }
4273
4274 /* we didn't find it - search superclasses of this class */
4275 cur_tpl = find_class_template(sc_sym->get_sc_name_head(),
4276 src, src_cnt, &cur_def_sc,
4277 undesc_class);
4278
4279 /*
4280 * if we have an undescribed class among our superclasses,
4281 * we're implicitly undescribed as well - if that's the case,
4282 * there's no need to look any further, so return failure
4283 */
4284 if (*undesc_class)
4285 return 0;
4286 }
4287
4288 /* if we found a match, see if we want to keep it */
4289 if (cur_tpl != 0)
4290 {
4291 /*
4292 * if this is our first match, note it; if it's not, see if it
4293 * overrides the previous match
4294 */
4295 if (tpl == 0)
4296 {
4297 /* this is the first match - definitely keep it */
4298 tpl = cur_tpl;
4299 *def_sc = cur_def_sc;
4300 }
4301 else
4302 {
4303 /*
4304 * if the current source object descends from the previous
4305 * source object, this definition overrides the previous
4306 * definition, so keep it rather than the last one
4307 */
4308 if (cur_def_sc->is_subclass_of(*def_sc))
4309 {
4310 /* it overrides the previous one - keep the new one */
4311 tpl = cur_tpl;
4312 *def_sc = cur_def_sc;
4313 }
4314 }
4315 }
4316 }
4317
4318 /* return the best match we found */
4319 return tpl;
4320 }
4321
4322 /* ------------------------------------------------------------------------ */
4323 /*
4324 * Find a matching template in the given template list
4325 */
4326 const CTcObjTemplate *CTcParser::
4327 find_template_match(const CTcObjTemplate *first_tpl,
4328 CTcObjTemplateInst *src, size_t src_cnt)
4329 {
4330
4331 /* find the matching template */
4332 for (const CTcObjTemplate *tpl = first_tpl ; tpl != 0 ; tpl = tpl->nxt_)
4333 {
4334 /* check for a match */
4335 if (match_template(tpl->items_, src, src_cnt))
4336 {
4337 /* it's a match - return this template */
4338 return tpl;
4339 }
4340 }
4341
4342 /* we didn't find a match */
4343 return 0;
4344 }
4345
4346 /* ------------------------------------------------------------------------ */
4347 /*
4348 * Match a template to an actual template parameter list.
4349 */
4350 int CTcParser::match_template(const CTcObjTemplateItem *tpl_head,
4351 CTcObjTemplateInst *src, size_t src_cnt)
4352 {
4353 /* check each element of the list */
4354 CTcObjTemplateInst *p;
4355 const CTcObjTemplateItem *item;
4356 size_t rem;
4357 for (p = src, rem = src_cnt, item = tpl_head ; item != 0 && rem != 0 ;
4358 item = item->nxt_)
4359 {
4360 /*
4361 * Note whether or not this item is optional. Every element of an
4362 * alternative group must have the same optional status, so we need
4363 * only note the status of the first item if this is a group.
4364 */
4365 int is_opt = item->is_opt_;
4366
4367 /*
4368 * Scan each alternative in the current group. Note that if we're
4369 * not in an alternative group, the logic is the same: we won't
4370 * have any 'alt' flags, so we'll just scan a single item.
4371 */
4372 int match;
4373 for (match = FALSE ; ; item = item->nxt_)
4374 {
4375 /* if this one matches, note the match */
4376 if (item->tok_type_ == p->def_tok_)
4377 {
4378 /* note the match */
4379 match = TRUE;
4380
4381 /* this is the property to assign for the actual */
4382 p->prop_ = item->prop_;
4383 }
4384
4385 /*
4386 * If this one is not marked as an alternative, we're done.
4387 * The last item of an alternative group is identified by
4388 * having its 'alt' flag cleared. Also, if we somehow have an
4389 * ill-formed list, where we don't have a terminating
4390 * non-flagged item, we can stop now as well.
4391 */
4392 if (!item->is_alt_ || item->nxt_ == 0)
4393 break;
4394 }
4395
4396 /* check to see if the current item is optional */
4397 if (is_opt)
4398 {
4399 /*
4400 * The item is optional. If it matches, try it both ways:
4401 * first try matching the item, then try skipping it. If we
4402 * can match this item and still match the rest of the string,
4403 * take that interpretation; otherwise, if we can skip this
4404 * item and match the rest of the string, take *that*
4405 * interpretation. If we can't match it either way, we don't
4406 * have a match.
4407 *
4408 * First, check to see if we can match the item and still match
4409 * the rest of the string.
4410 */
4411 if (match && match_template(item->nxt_, p + 1, rem - 1))
4412 {
4413 /* we have a match */
4414 return TRUE;
4415 }
4416
4417 /*
4418 * Matching this optional item doesn't let us match the rest of
4419 * the string, so try it with this optional item omitted - in
4420 * other words, just match the rest of the string, including
4421 * the current source item, to the rest of the template,
4422 * *excluding* the current optional item.
4423 *
4424 * There's no need to recurse to do this; simply continue
4425 * iterating, but do NOT skip the current source item.
4426 */
4427 }
4428 else
4429 {
4430 /*
4431 * It's not optional, so if it doesn't match, the whole
4432 * template fails to match; if it does match, simply proceed
4433 * through the rest of the template.
4434 */
4435 if (!match)
4436 return FALSE;
4437
4438 /* we matched, so consume this source item */
4439 ++p;
4440 --rem;
4441 }
4442 }
4443
4444 /* skip any trailing optional items in the template list */
4445 while (item != 0 && item->is_opt_)
4446 item = item->nxt_;
4447
4448 /*
4449 * it's a match if and only if we reached the end of both lists at the
4450 * same time
4451 */
4452 return (item == 0 && rem == 0);
4453 }
4454
4455 /* ------------------------------------------------------------------------ */
4456 /*
4457 * Begin a property expression
4458 */
4459 void CTcParser::begin_prop_expr(
4460 CTcPrsPropExprSave *save_info, int is_static, int is_inline)
4461 {
4462 /* save the current parser state */
4463 save_info->has_local_ctx_ = has_local_ctx_;
4464 save_info->local_ctx_var_num_ = local_ctx_var_num_;
4465 save_info->ctx_var_props_used_ = ctx_var_props_used_;
4466 save_info->next_ctx_arr_idx_ = next_ctx_arr_idx_;
4467 save_info->local_cnt_ = local_cnt_;
4468 save_info->max_local_cnt_ = max_local_cnt_;
4469 save_info->self_valid_ = self_valid_;
4470 save_info->self_referenced_ = self_referenced_;
4471 save_info->cur_code_body_ = cur_code_body_;
4472 save_info->full_method_ctx_referenced_ = full_method_ctx_referenced_;
4473 save_info->local_ctx_needs_self_ = local_ctx_needs_self_;
4474 save_info->local_ctx_needs_full_method_ctx_ =
4475 local_ctx_needs_full_method_ctx_;
4476 save_info->enclosing_stm_ = enclosing_stm_;
4477 save_info->enclosing_local_symtab_ = enclosing_local_symtab_;
4478 save_info->local_symtab_ = local_symtab_;
4479 save_info->goto_symtab_ = goto_symtab_;
4480
4481 /*
4482 * we've saved the local context information, so clear it out for the
4483 * next parse job
4484 */
4485 clear_local_ctx();
4486
4487 /* set up the local symbol table for the property expression */
4488 if (is_inline)
4489 {
4490 /*
4491 * It's an inline object definition. For a normal (non-static)
4492 * property, the expression is treated as though it were wrapped in
4493 * an anonymous method, so that it can access the enclosing scope's
4494 * locals. We need to set up the usual anonymous function proxy
4495 * symbol table to allow access to the enclosing scope. For a
4496 * static property, the property expression is evaluated
4497 * immediately when we evaluate the overall inline object
4498 * expression. Static evaluation is also done in the context of
4499 * the enclosing scope, but since it happens during the enclosing
4500 * scope's lifetime, we don't have to worry about setting up a
4501 * proxy symbol table; we can simply access the locals directly
4502 * while they're still alive, so for the static case we just leave
4503 * the stack context exactly like it already is.
4504 */
4505 if (!is_static)
4506 {
4507 /*
4508 * ordinary non-static expression - we need an anonymous
4509 * function context so that the expression can access the
4510 * enclosing scope's locals even after the enclosing scope
4511 * returns to its caller
4512 */
4513 local_symtab_ = save_info->inline_prep_.create_symtab();
4514 if ((enclosing_local_symtab_ = local_symtab_->get_parent()) == 0)
4515 enclosing_local_symtab_ = global_symtab_;
4516 self_valid_ = TRUE;
4517 }
4518 }
4519 else
4520 {
4521 /*
4522 * This is a regular top-level object definition, so there's no
4523 * enclosing scope. Clear out the code body and the local symbol
4524 * table so that we parse in global context.
4525 */
4526 cur_code_body_ = 0;
4527 local_symtab_ = global_symtab_;
4528 enclosing_local_symtab_ = global_symtab_;
4529 }
4530
4531 /* start a new parsing context */
4532 enclosing_stm_ = 0;
4533 goto_symtab_ = 0;
4534
4535 /* no locals yet */
4536 local_cnt_ = 0;
4537 max_local_cnt_ = 0;
4538 }
4539
4540 /*
4541 * Finish a property expression, checking for anonymous functions and
4542 * wrapping the expression in a code body if necesssary. If the expression
4543 * contains an anonymous function which needs to share context with its
4544 * enclosing scope, we need to build the code body wrapper immediately so
4545 * that we can capture the context information, which is stored in the
4546 * parser object itself (i.e,. 'this').
4547 */
4548 void CTcParser::finish_prop_expr(
4549 CTcPrsPropExprSave *save_info,
4550 CTcPrsNode* &expr, CTPNCodeBody* &cb, CTPNAnonFunc* &inline_method,
4551 int is_static, int is_inline, CTcSymProp *prop_sym)
4552 {
4553 /* presume we won't need to create a code body or inline method */
4554 cb = 0;
4555 inline_method = 0;
4556
4557 /*
4558 * If we have a local context, we have to set up a code body in order
4559 * to initialize the local context at run-time. If this is an inline
4560 * object definition, and it's not a static property, we need to turn
4561 * the expression into an anonymous method, so we also need to wrap it
4562 * in a code body.
4563 */
4564 if (expr != 0
4565 && (has_local_ctx_
4566 || (is_inline && !is_static)))
4567 {
4568 /*
4569 * We need to wrap the expression in a code body to make the
4570 * property into a method. First, wrap the expression in an
4571 * appropriate statement so that we can put it into a code body.
4572 */
4573 CTPNStm *stm;
4574 if (is_static)
4575 {
4576 /*
4577 * it's a static initializer - wrap it in a static initializer
4578 * statement node
4579 */
4580 stm = new CTPNStmStaticPropInit(expr, prop_sym->get_prop());
4581 }
4582 else if (expr->has_return_value())
4583 {
4584 /* normal property value - wrap it in a 'return' */
4585 stm = new CTPNStmReturn(expr);
4586 }
4587 else
4588 {
4589 /*
4590 * it's an expression that yields no value, such as a
4591 * double-quoted string expression or a call to a void
4592 * function; just use the expression itself
4593 */
4594 stm = new CTPNStmExpr(expr);
4595 }
4596
4597 /* wrap the expression statement in a code body */
4598 cb = new CTPNCodeBody(
4599 local_symtab_, goto_symtab_, stm, 0, 0, FALSE, FALSE, 0,
4600 local_cnt_, self_valid_, save_info->cur_code_body_);
4601
4602 /* set up the local context for access to enclosing scope locals */
4603 finish_local_ctx(cb, local_symtab_);
4604
4605 /* mark the code body for references to the method context */
4606 cb->set_self_referenced(self_referenced_);
4607 cb->set_full_method_ctx_referenced(full_method_ctx_referenced_);
4608
4609 /* mark the code body for inclusion in any local context */
4610 cb->set_local_ctx_needs_self(local_ctx_needs_self_);
4611 cb->set_local_ctx_needs_full_method_ctx(
4612 local_ctx_needs_full_method_ctx_);
4613
4614 /*
4615 * if this is a non-static property in an inline object
4616 * definition, turn the expression into an anonymous method
4617 */
4618 if (is_inline && !is_static)
4619 {
4620 /* mark it as an anonymous method */
4621 cb->set_anon_method(TRUE);
4622
4623 /* add the code body to the master list of nested functions */
4624 add_nested_stm(cb);
4625
4626 /* wrap the code body in an anonymous method */
4627 inline_method = new CTPNAnonFunc(
4628 cb, expr->has_return_value(), TRUE);
4629
4630 /* the anonymous method supersedes the code body */
4631 cb = 0;
4632 }
4633 }
4634
4635 /* restore the saved parser state */
4636 has_local_ctx_ = save_info->has_local_ctx_;
4637 local_ctx_var_num_ = save_info->local_ctx_var_num_;
4638 ctx_var_props_used_ = save_info->ctx_var_props_used_;
4639 next_ctx_arr_idx_ = save_info->next_ctx_arr_idx_;
4640 local_cnt_ = save_info->local_cnt_;
4641 max_local_cnt_ = save_info->max_local_cnt_;
4642 cur_code_body_ = save_info->cur_code_body_;
4643 self_valid_ = save_info->self_valid_;
4644 self_referenced_ = save_info->self_referenced_;
4645 full_method_ctx_referenced_ = save_info->full_method_ctx_referenced_;
4646 local_ctx_needs_self_ = save_info->local_ctx_needs_self_;
4647 local_ctx_needs_full_method_ctx_ =
4648 save_info->local_ctx_needs_full_method_ctx_;
4649 enclosing_stm_ = save_info->enclosing_stm_;
4650 enclosing_local_symtab_ = save_info->enclosing_local_symtab_;
4651 local_symtab_ = save_info->local_symtab_;
4652 goto_symtab_ = save_info->goto_symtab_;
4653
4654 /*
4655 * if this is a non-static property of an inline object, finish the
4656 * anonymous method proxy symbol table for enclosing scopes
4657 */
4658 if (is_inline && !is_static)
4659 save_info->inline_prep_.finish();
4660 }
4661
4662 /* ------------------------------------------------------------------------ */
4663 /*
4664 * property set token source
4665 */
4666
4667 void propset_token_source::insert_token(const CTcToken *tok)
4668 {
4669 /* create a new link entry and initialize it */
4670 propset_tok *cur = new (G_prsmem) propset_tok(tok);
4671
4672 /* link it into our list */
4673 if (last_tok != 0)
4674 last_tok->nxt = cur;
4675 else
4676 nxt_tok = cur;
4677 last_tok = cur;
4678 }
4679
4680 void propset_token_source::insert_token(
4681 tc_toktyp_t typ, const char *txt, size_t len)
4682 {
4683 /* set up the token object */
4684 CTcToken tok;
4685 tok.settyp(typ);
4686 tok.set_text(txt, len);
4687
4688 /* insert it */
4689 insert_token(&tok);
4690 }
4691
4692
4693
4694 /* ------------------------------------------------------------------------ */
4695 /*
4696 * Set up the inserted token stream for a propertyset invocation
4697 */
4698 void CTcParser::insert_propset_expansion(struct propset_def *propset_stack,
4699 int propset_depth)
4700 {
4701 /*
4702 * First, determine if we have any added formals from propertyset
4703 * definitions.
4704 */
4705 int i;
4706 int formals_found;
4707 for (formals_found = FALSE, i = 0 ; i < propset_depth ; ++i)
4708 {
4709 /* if this one has formals, so note */
4710 if (propset_stack[i].param_tok_head != 0)
4711 {
4712 /* note it, and we need not look further */
4713 formals_found = TRUE;
4714 break;
4715 }
4716 }
4717
4718 /*
4719 * If we found formals from property sets, we must expand them into the
4720 * token stream.
4721 */
4722 if (formals_found)
4723 {
4724 /* insert an open paren at the start of the expansion list */
4725 propset_token_source tok_src;
4726 tok_src.insert_token(TOKT_LPAR, "(", 1);
4727
4728 /* we don't yet need a leading comma */
4729 int need_comma = FALSE;
4730
4731 /*
4732 * Add the tokens from each propertyset in the stack, from the
4733 * outside in, until we reach an asterisk in each stack.
4734 */
4735 for (i = 0 ; i < propset_depth ; ++i)
4736 {
4737 propset_tok *cur;
4738
4739 /* add the tokens from the stack element */
4740 for (cur = propset_stack[i].param_tok_head ; cur != 0 ;
4741 cur = cur->nxt)
4742 {
4743 /*
4744 * If we need a comma before the next real element, add it
4745 * now.
4746 */
4747 if (need_comma)
4748 {
4749 tok_src.insert_token(TOKT_COMMA, ",", 1);
4750 need_comma = FALSE;
4751 }
4752
4753 /*
4754 * If this is a comma and the next item is the '*', omit
4755 * the comma - if we have nothing more following, we want
4756 * to suppress the comma.
4757 */
4758 if (cur->tok.gettyp() == TOKT_COMMA
4759 && cur->nxt != 0
4760 && cur->nxt->tok.gettyp() == TOKT_TIMES)
4761 {
4762 /*
4763 * it's the comma before the star - simply stop here
4764 * for this list, but note that we need a comma before
4765 * any additional formals that we add in the future
4766 */
4767 need_comma = TRUE;
4768 break;
4769 }
4770
4771 /*
4772 * if it's the '*' for this list, stop here, since we want
4773 * to insert the next level in before we add these tokens
4774 */
4775 if (cur->tok.gettyp() == TOKT_TIMES)
4776 break;
4777
4778 /* insert it into our expansion list */
4779 tok_src.insert_token(&cur->tok);
4780 }
4781 }
4782
4783 /*
4784 * If we have explicit formals in the true input stream, add them,
4785 * up to but not including the close paren.
4786 */
4787 if (G_tok->cur() == TOKT_LPAR)
4788 {
4789 /* skip the open paren */
4790 G_tok->next();
4791
4792 /* check for a non-empty list */
4793 if (G_tok->cur() != TOKT_RPAR)
4794 {
4795 /*
4796 * the list is non-empty - if we need a comma, add it now
4797 */
4798 if (need_comma)
4799 tok_src.insert_token(TOKT_COMMA, ",", 1);
4800
4801 /* we will need a comma at the end of this list */
4802 need_comma = TRUE;
4803 }
4804
4805 /*
4806 * copy everything up to but not including the close paren to
4807 * the expansion list
4808 */
4809 while (G_tok->cur() != TOKT_RPAR
4810 && G_tok->cur() != TOKT_EOF)
4811 {
4812 /* insert this token into our expansion list */
4813 tok_src.insert_token(G_tok->getcur());
4814
4815 /* skip it */
4816 G_tok->next();
4817 }
4818
4819 /* skip the closing paren */
4820 if (G_tok->cur() == TOKT_RPAR)
4821 G_tok->next();
4822 }
4823
4824 /*
4825 * Finish the expansion by adding the parts of each propertyset
4826 * list after the '*' to the expansion list. Copy from the inside
4827 * out, since we want to unwind the nesting from outside in that we
4828 * did to start with.
4829 */
4830 for (i = propset_depth ; i != 0 ; )
4831 {
4832 /* move down to the next level */
4833 --i;
4834
4835 /* find the '*' in this list */
4836 propset_tok *cur;
4837 for (cur = propset_stack[i].param_tok_head ;
4838 cur != 0 && cur->tok.gettyp() != TOKT_TIMES ;
4839 cur = cur->nxt) ;
4840
4841 /* if we found the '*', skip it */
4842 if (cur != 0)
4843 cur = cur->nxt;
4844
4845 /*
4846 * also skip the comma following the '*', if present - we'll
4847 * explicitly insert the needed extra comma if we actually find
4848 * we need one
4849 */
4850 if (cur != 0 && cur->tok.gettyp() == TOKT_COMMA)
4851 cur = cur->nxt;
4852
4853 /* insert the remainder of the list into the expansion list */
4854 for ( ; cur != 0 ; cur = cur->nxt)
4855 {
4856 /* if we need a comma, add it now */
4857 if (need_comma)
4858 {
4859 tok_src.insert_token(TOKT_COMMA, ",", 1);
4860 need_comma = FALSE;
4861 }
4862
4863 /* insert this token */
4864 tok_src.insert_token(&cur->tok);
4865 }
4866 }
4867
4868 /* add the closing paren at the end of the expansion list */
4869 tok_src.insert_token(TOKT_RPAR, ")", 1);
4870
4871 /*
4872 * We've fully expanded the formal list. Now all that remains is
4873 * to insert the expanded token list into the token input stream,
4874 * so that we read from the expanded list instead of from the
4875 * original token stream.
4876 */
4877 G_tok->set_external_source(&tok_src);
4878 }
4879 }
4880
4881
4882 /* ------------------------------------------------------------------------ */
4883 /*
4884 * Parse an object definition's property list
4885 */
4886 int CTcParser::parse_obj_prop_list(
4887 int *err, CTPNObjDef *objdef, CTcSymMetaclass *meta_sym,
4888 int modify, int is_nested, int braces, int is_inline,
4889 tcprs_term_info *outer_term_info, tcprs_term_info *term_info)
4890 {
4891 /* we aren't in a propertyset definition yet */
4892 propset_def propset_stack[MAX_PROPSET_DEPTH];
4893 int propset_depth = 0;
4894
4895 /*
4896 * Parse the property list. Keep going until we get to the closing
4897 * semicolon.
4898 */
4899 for (int done = FALSE ; !done ; )
4900 {
4901 /*
4902 * Remember the statement start location. Some types of
4903 * property definitions result in implicit statements being
4904 * created, so we must record the statement start position for
4905 * those cases.
4906 */
4907 cur_desc_ = G_tok->get_last_desc();
4908 cur_linenum_ = G_tok->get_last_linenum();
4909
4910 /* presume no 'replace' on this property */
4911 int replace = FALSE;
4912
4913 /* parse the property definition */
4914 switch(G_tok->cur())
4915 {
4916 case TOKT_PROPERTYSET:
4917 {
4918 propset_def *cur_def;
4919 propset_def dummy_def;
4920
4921 /*
4922 * It's a property set definition. These definitions
4923 * nest; make sure we have space left in our stack.
4924 */
4925 if (propset_depth >= MAX_PROPSET_DEPTH)
4926 {
4927 /*
4928 * nested too deeply - flag the error if this is the
4929 * one that pushes us over the edge
4930 */
4931 if (propset_depth == MAX_PROPSET_DEPTH)
4932 G_tok->log_error(TCERR_PROPSET_TOO_DEEP);
4933
4934 /*
4935 * keep going with a syntactic parse despite the
4936 * problems - increment the current depth, but use a
4937 * dummy definition object, since we have no more
4938 * stack entries to store the real data
4939 */
4940 ++propset_depth;
4941 cur_def = &dummy_def;
4942 }
4943 else
4944 {
4945 /* set up to use the next available stack entry */
4946 cur_def = &propset_stack[propset_depth];
4947
4948 /* consume this stack entry */
4949 ++propset_depth;
4950 }
4951
4952 /* skip the keyword and get the property name pattern */
4953 if (G_tok->next() == TOKT_SSTR)
4954 {
4955
4956 /* note the pattern string */
4957 cur_def->prop_pattern = G_tok->getcur()->get_text();
4958 cur_def->prop_pattern_len =
4959 G_tok->getcur()->get_text_len();
4960
4961 /*
4962 * Validate the pattern. It must consist of valid
4963 * token characters, except for a single asterisk,
4964 * which it must have.
4965 */
4966 int star_cnt = 0, inval_cnt = 0;
4967 utf8_ptr p((char *)cur_def->prop_pattern);
4968 size_t rem = cur_def->prop_pattern_len;
4969 for ( ; rem != 0 ; p.inc(&rem))
4970 {
4971 /* check this character */
4972 if (p.getch() == '*')
4973 ++star_cnt;
4974 else if (!is_sym(p.getch()))
4975 ++inval_cnt;
4976 }
4977
4978 /*
4979 * if the first character isn't an asterisk, it must
4980 * be a valid initial symbol character
4981 */
4982 if (cur_def->prop_pattern_len != 0)
4983 {
4984 /* get the first character */
4985 wchar_t firstch = utf8_ptr::s_getch(
4986 cur_def->prop_pattern);
4987
4988 /*
4989 * if it's not a '*', it must be a valid initial
4990 * token character
4991 */
4992 if (firstch != '*' && !is_syminit(firstch))
4993 ++inval_cnt;
4994 }
4995
4996 /*
4997 * if it has no '*' or more than one '*', or it has
4998 * invalid characters, flag it as invalid
4999 */
5000 if (star_cnt != 1 || inval_cnt != 0)
5001 G_tok->log_error_curtok(TCERR_PROPSET_INVAL_PAT);
5002 }
5003 else
5004 {
5005 /* not a string; flag an error, but try to keep going */
5006 G_tok->log_error_curtok(TCERR_PROPSET_REQ_STR);
5007
5008 /* we have no pattern string */
5009 cur_def->prop_pattern = "";
5010 cur_def->prop_pattern_len = 0;
5011 }
5012
5013 /* we have no parameter token list yet */
5014 cur_def->param_tok_head = 0;
5015
5016 /*
5017 * skip the pattern string, and see if we have an argument
5018 * list
5019 */
5020 if (G_tok->next() == TOKT_LPAR)
5021 {
5022 /*
5023 * Current parsing state: 0=start, 1=after symbol or
5024 * '*', 2=after comma, 3=done
5025 */
5026 int state = 0;
5027
5028 /* number of '*' tokens */
5029 int star_cnt = 0;
5030
5031 /* start with an empty token list */
5032 propset_tok *last = 0;
5033
5034 /*
5035 * We have a formal parameter list that is to be
5036 * applied to each property in the set. Parse tokens
5037 * up to the closing paren, stashing them in our list.
5038 */
5039 for (G_tok->next() ; state != 3 ; )
5040 {
5041 /* check the token */
5042 switch (G_tok->cur())
5043 {
5044 case TOKT_LBRACE:
5045 case TOKT_RBRACE:
5046 case TOKT_SEM:
5047 /*
5048 * A brace or semicolon - assume that the right
5049 * paren was accidentally omitted. Flag the
5050 * error and consider the list to be finished.
5051 */
5052 G_tok->log_error_curtok(
5053 TCERR_MISSING_RPAR_FORMAL);
5054
5055 /* switch to 'done' state */
5056 state = 3;
5057 break;
5058
5059 case TOKT_RPAR:
5060 /*
5061 * Right paren. This ends the list. If we're
5062 * in state 2 ('after comma'), the paren is out
5063 * of place, so flag it as an error - but still
5064 * count it as ending the list.
5065 */
5066 if (state == 2)
5067 G_tok->log_error(TCERR_MISSING_LAST_FORMAL);
5068
5069 /* skip the paren */
5070 G_tok->next();
5071
5072 /* switch to state 'done' */
5073 state = 3;
5074 break;
5075
5076 case TOKT_SYM:
5077 case TOKT_TIMES:
5078 /*
5079 * Formal parameter name or '*'. We can accept
5080 * these in states 'start' and 'after comma' -
5081 * in those states, just go add the token to
5082 * the list we're gathering. In state 'after
5083 * formal', this is an error - a comma should
5084 * have come between the two parameters.
5085 */
5086 if (state == 1)
5087 {
5088 /*
5089 * 'after formal' - a comma must be
5090 * missing. Flag the error, but then
5091 * proceed as though the comma had been
5092 * there.
5093 */
5094 G_tok->log_error_curtok(
5095 TCERR_REQ_COMMA_FORMAL);
5096 }
5097
5098 /* switch to state 'after formal' */
5099 state = 1;
5100
5101 add_to_list:
5102 {
5103 /* create a list entry for the current token */
5104 propset_tok *cur = new (G_prsmem) propset_tok(
5105 G_tok->getcur());
5106
5107 /* add it to the token list */
5108 if (last != 0)
5109 last->nxt = cur;
5110 else
5111 cur_def->param_tok_head = cur;
5112 last = cur;
5113
5114 /* if it's a '*', count it */
5115 if (G_tok->cur() == TOKT_TIMES)
5116 ++star_cnt;
5117
5118 /* skip it */
5119 G_tok->next();
5120 }
5121 break;
5122
5123 case TOKT_COMMA:
5124 /*
5125 * In state 'after formal', a comma just goes
5126 * into our token list. A comma is invalid in
5127 * any other state.
5128 */
5129 if (state == 1)
5130 {
5131 /* switch to state 'after comma' */
5132 state = 2;
5133
5134 /* go add the token to our list */
5135 goto add_to_list;
5136 }
5137 else
5138 {
5139 /* otherwise, go handle as a bad token */
5140 goto bad_token;
5141 }
5142
5143 default:
5144 bad_token:
5145 /* generate an error according to our state */
5146 switch (state)
5147 {
5148 case 0:
5149 case 2:
5150 /*
5151 * 'start' or 'after comma' - we expected a
5152 * formal name
5153 */
5154 G_tok->log_error_curtok(TCERR_REQ_SYM_FORMAL);
5155 break;
5156
5157 case 1:
5158 /* 'after formal' - expected a comma */
5159 G_tok->log_error_curtok(
5160 TCERR_REQ_COMMA_FORMAL);
5161 break;
5162 }
5163
5164 /* skip the token and keep going */
5165 G_tok->next();
5166 break;
5167 }
5168 }
5169
5170 /* make sure we have exactly one star */
5171 if (star_cnt != 1)
5172 G_tok->log_error(TCERR_PROPSET_INVAL_FORMALS);
5173 }
5174
5175 /* require the open brace for the set */
5176 if (G_tok->cur() == TOKT_LBRACE)
5177 G_tok->next();
5178 else
5179 G_tok->log_error_curtok(TCERR_PROPSET_REQ_LBRACE);
5180 }
5181
5182 /* proceed to parse the properties within */
5183 break;
5184
5185 case TOKT_SEM:
5186 /*
5187 * If we're using brace notation (i.e., the property list is
5188 * surrounded by braces), a semicolon inside the property list
5189 * is ignored. If we're using regular notation, a semicolon
5190 * ends the property list.
5191 */
5192 if (braces || propset_depth != 0)
5193 {
5194 /*
5195 * we're using brace notation, or we're inside the braces
5196 * of a propertyset, so semicolons are ignored
5197 */
5198 }
5199 else
5200 {
5201 /*
5202 * we're using regular notation, so this semicolon
5203 * terminates the property list and the object body - note
5204 * that we're done parsing the object body
5205 */
5206 done = TRUE;
5207 }
5208
5209 /* in any case, skip the semicolon and carry on */
5210 G_tok->next();
5211 break;
5212
5213 case TOKT_CLASS:
5214 case TOKT_EXTERN:
5215 case TOKT_MODIFY:
5216 case TOKT_DICTIONARY:
5217 case TOKT_PROPERTY:
5218 case TOKT_PLUS:
5219 case TOKT_INC:
5220 case TOKT_INTRINSIC:
5221 case TOKT_OBJECT:
5222 case TOKT_GRAMMAR:
5223 case TOKT_ENUM:
5224 /*
5225 * All of these probably indicate the start of a new
5226 * statement - they probably just left off the closing
5227 * semicolon of the current object. Flag the error and
5228 * return, on the assumption that we'll find a new statement
5229 * starting here.
5230 */
5231 G_tok->log_error_curtok(braces || propset_depth != 0
5232 ? TCERR_OBJ_DEF_REQ_RBRACE
5233 : TCERR_OBJ_DEF_REQ_SEM);
5234 done = TRUE;
5235 break;
5236
5237 case TOKT_SYM:
5238 /*
5239 * Property definition. For better recovery from certain
5240 * common errors, check a couple of scenarios.
5241 *
5242 * Look ahead one token. If the next token is a colon, then
5243 * we have a nested object definition (the property is
5244 * assigned to a reference to an anonymous object about to be
5245 * defined in-line). This could also indicate a missing
5246 * semicolon (or close brace) immediately before the symbol
5247 * we're taking as a property name - the symbol we just parsed
5248 * could actually be intended as the next object's name, and
5249 * the colon introduces its superclass list. If the new
5250 * object's property list isn't enclosed in braces, this is
5251 * probably what happened.
5252 *
5253 * If the next token is '(', '{', or '=', it's almost
5254 * certainly a property definition, so proceed on that
5255 * assumption.
5256 *
5257 * If the next token is a symbol, we have one of two probable
5258 * errors. Either we have a missing '=' in a property
5259 * definition, or this is a new anonymous object definition,
5260 * and the current object definition is missing its
5261 * terminating semicolon. If the current symbol is a class
5262 * name, it's almost certainly a new object definition;
5263 * otherwise, assume it's really a property definition, and
5264 * we're just missing the '='.
5265 */
5266
5267 /* look ahead at the next token */
5268 switch(G_tok->next())
5269 {
5270 case TOKT_LPAR:
5271 case TOKT_LBRACE:
5272 case TOKT_EQ:
5273 /*
5274 * It's almost certainly a property definition, no
5275 * matter what the current symbol looks like. Put back
5276 * the token and proceed.
5277 */
5278 G_tok->unget();
5279 break;
5280
5281 case TOKT_COLON:
5282 /*
5283 * "symbol: " - a nested object definition, or what's
5284 * meant to be a new object definition, with the
5285 * terminating semicolon or brace of the current object
5286 * missing. Assume for now it's correct, in which case
5287 * it's a nested object definition; put back the token and
5288 * proceed.
5289 */
5290 G_tok->unget();
5291 break;
5292
5293 case TOKT_SYM:
5294 case TOKT_AT:
5295 case TOKT_PLUS:
5296 case TOKT_MINUS:
5297 case TOKT_TIMES:
5298 case TOKT_DIV:
5299 case TOKT_MOD:
5300 case TOKT_ARROW:
5301 case TOKT_AND:
5302 case TOKT_NOT:
5303 case TOKT_BNOT:
5304 case TOKT_COMMA:
5305 {
5306 /*
5307 * Two symbols in a row, or a symbol followed by
5308 * what might be a template operator - either an
5309 * equals sign was left out, or this is a new object
5310 * definition. Put back the token and check what
5311 * kind of symbol we had in the first place.
5312 */
5313 G_tok->unget();
5314 CTcSymbol *sym = global_symtab_->find(
5315 G_tok->getcur()->get_text(),
5316 G_tok->getcur()->get_text_len());
5317
5318 /*
5319 * if it's an object symbol, we almost certainly
5320 * have a new object definition
5321 */
5322 if (sym != 0 && sym->get_type() == TC_SYM_OBJ)
5323 {
5324 /* log the error */
5325 G_tok->log_error_curtok(
5326 braces || propset_depth != 0
5327 ? TCERR_OBJ_DEF_REQ_RBRACE
5328 : TCERR_OBJ_DEF_REQ_SEM);
5329
5330 /* assume the object is finished */
5331 done = TRUE;
5332 }
5333 }
5334 break;
5335
5336 default:
5337 /*
5338 * anything else is probably an error, but it's hard to
5339 * guess what the error is; proceed on the assumption
5340 * that it's going to be a property definition, and let
5341 * the property definition parser take care of analyzing
5342 * the problem
5343 */
5344 G_tok->unget();
5345 break;
5346 }
5347
5348 /*
5349 * if we decided that the object is finished, cancel the
5350 * property definition parsing
5351 */
5352 if (done)
5353 break;
5354
5355 parse_normal_prop:
5356 /*
5357 * initialize my termination information object with the
5358 * current location (note that we don't necessarily initialize
5359 * the current termination info, since only the outermost one
5360 * matters; if ours happens to be active, it's because it's
5361 * the outermost)
5362 */
5363 outer_term_info->init(G_tok->get_last_desc(),
5364 G_tok->get_last_linenum());
5365
5366 /* go parse the property definition */
5367 parse_obj_prop(
5368 err, objdef, replace, meta_sym, term_info,
5369 propset_stack, propset_depth, is_nested, is_inline);
5370
5371 /* if we ran into an unrecoverable error, give up */
5372 if (*err)
5373 return FALSE;
5374
5375 /*
5376 * if we encountered a termination error, assume the current
5377 * object was intended to be terminated, so stop here
5378 */
5379 if (term_info->unterm_)
5380 {
5381 /*
5382 * we found what looks like the end of the object within
5383 * the property parser - assume we're done with this
5384 * object
5385 */
5386 done = TRUE;
5387 }
5388
5389 /* done */
5390 break;
5391
5392 case TOKT_OPERATOR:
5393 /*
5394 * Operator overload property - parse this as a normal property
5395 * (the property parser takes care of the 'operator' syntax
5396 * specialization).
5397 */
5398 goto parse_normal_prop;
5399
5400 case TOKT_REPLACE:
5401 /* 'replace' is only allowed with 'modify' objects */
5402 if (!modify)
5403 G_tok->log_error(TCERR_REPLACE_PROP_REQ_MOD_OBJ);
5404
5405 /* skip the 'replace' keyword */
5406 G_tok->next();
5407
5408 /* set the 'replace' flag for the property parser */
5409 replace = TRUE;
5410
5411 /* go parse the normal property definition */
5412 goto parse_normal_prop;
5413
5414 case TOKT_RBRACE:
5415 /*
5416 * If we're in a 'propertyset' definition, this ends the
5417 * current propertyset. If the property list is enclosed in
5418 * braces, this brace terminates the property list and the
5419 * object body. Otherwise, it's an error.
5420 */
5421 if (propset_depth != 0)
5422 {
5423 /* pop a propertyset level */
5424 --propset_depth;
5425
5426 /*
5427 * skip the brace, and proceed with parsing the rest of
5428 * the object
5429 */
5430 G_tok->next();
5431 }
5432 else if (braces)
5433 {
5434 /* we're done with the object body */
5435 done = TRUE;
5436
5437 /* skip the brace */
5438 G_tok->next();
5439 }
5440 else
5441 {
5442 /*
5443 * this property list isn't enclosed in braces, so this is
5444 * completely unexpected - treat it the same as any other
5445 * invalid token
5446 */
5447 goto inval_token;
5448 }
5449 break;
5450
5451 case TOKT_EOF:
5452 default:
5453 inval_token:
5454 /* anything else is invalid */
5455 G_tok->log_error(TCERR_OBJDEF_REQ_PROP,
5456 (int)G_tok->getcur()->get_text_len(),
5457 G_tok->getcur()->get_text());
5458
5459 /* skip the errant token and continue */
5460 G_tok->next();
5461
5462 /* if we're at EOF, abort */
5463 if (G_tok->cur() == TOKT_EOF)
5464 {
5465 *err = TRUE;
5466 return FALSE;
5467 }
5468 break;
5469 }
5470 }
5471
5472 /* success */
5473 return TRUE;
5474 }
5475
5476
5477 /* ------------------------------------------------------------------------ */
5478 /*
5479 * Parse a property definition within an object definition.
5480 *
5481 * 'obj_is_nested' indicates that the enclosing object is a nested object.
5482 * This tells us that we can't allow the lexicalParent property to be
5483 * manually defined, since we will automatically define it explicitly for
5484 * the enclosing object by virtue of its being nested.
5485 */
5486 void CTcParser::parse_obj_prop(
5487 int *err, CTPNObjDef *objdef, int replace, CTcSymMetaclass *meta_sym,
5488 tcprs_term_info *term_info, propset_def *propset_stack, int propset_depth,
5489 int obj_is_nested, int is_inline)
5490 {
5491 /* we haven't created the new property yet */
5492 CTPNObjProp *new_prop = 0;
5493
5494 /* presume it's not a static property definition */
5495 int is_static = FALSE;
5496
5497 /* remember the property token */
5498 CTcToken prop_tok = *G_tok->copycur();
5499
5500 /*
5501 * if we're in a property set, apply the property set pattern to the
5502 * property name
5503 */
5504 if (propset_depth != 0)
5505 {
5506 /* we can't define operator overloads here */
5507 if (prop_tok.gettyp() == TOKT_OPERATOR)
5508 G_tok->log_error(TCERR_OPER_IN_PROPSET);
5509
5510 /*
5511 * Build the real property name based on all enclosing propertyset
5512 * pattern strings. Start with the current (innermost) pattern,
5513 * then apply the next pattern out, and so on.
5514 *
5515 * Note that if the current nesting depth is greater than the
5516 * maximum nesting depth, ignore the illegally deep levels and
5517 * just start with the deepest legal level.
5518 */
5519 int i = propset_depth;
5520 if (i > MAX_PROPSET_DEPTH)
5521 i = MAX_PROPSET_DEPTH;
5522
5523 /* start with the current token */
5524 size_t explen = G_tok->getcur()->get_text_len();
5525 char expbuf[TOK_SYM_MAX_LEN];
5526 memcpy(expbuf, G_tok->getcur()->get_text(), explen);
5527
5528 /* iterate through the propertyset stack */
5529 for (propset_def *cur = &propset_stack[i-1] ; i > 0 ; --i, --cur)
5530 {
5531 char tmpbuf[TOK_SYM_MAX_LEN];
5532 utf8_ptr src;
5533 utf8_ptr dst;
5534 size_t rem;
5535
5536 /* if we'd exceed the maximum token length, stop here */
5537 if (explen + cur->prop_pattern_len > TOK_SYM_MAX_LEN)
5538 {
5539 /* complain about it */
5540 G_tok->log_error(TCERR_PROPSET_TOK_TOO_LONG);
5541
5542 /* stop the expansion */
5543 break;
5544 }
5545
5546 /* copy the pattern up to the '*' */
5547 for (src.set((char *)cur->prop_pattern),
5548 dst.set(tmpbuf), rem = cur->prop_pattern_len ;
5549 rem != 0 && src.getch() != '*' ; src.inc(&rem))
5550 {
5551 /* copy this character */
5552 dst.setch(src.getch());
5553 }
5554
5555 /* if we found a '*', skip it */
5556 if (rem != 0 && src.getch() == '*')
5557 src.inc(&rem);
5558
5559 /* insert the expansion from the last round here */
5560 memcpy(dst.getptr(), expbuf, explen);
5561
5562 /* advance our output pointer */
5563 dst.set(dst.getptr() + explen);
5564
5565 /* copy the remainder of the pattern string */
5566 if (rem != 0)
5567 {
5568 /* copy the remaining bytes */
5569 memcpy(dst.getptr(), src.getptr(), rem);
5570
5571 /* advance the output pointer past the copied bytes */
5572 dst.set(dst.getptr() + rem);
5573 }
5574
5575 /* copy the result back to the expansion buffer */
5576 explen = dst.getptr() - tmpbuf;
5577 memcpy(expbuf, tmpbuf, explen);
5578 }
5579
5580 /* store the new token in tokenizer memory */
5581 prop_tok.set_text(G_tok->store_source(expbuf, explen), explen);
5582 }
5583
5584 /* presume we won't find a valid symbol for the property token */
5585 CTcSymProp *prop_sym = 0;
5586
5587 /* presume it's not an operator overload property */
5588 int op_args = 0;
5589
5590 /* if this is an 'operator' property, the syntax is special */
5591 if (prop_tok.gettyp() == TOKT_OPERATOR)
5592 parse_op_name(&prop_tok, &op_args);
5593
5594 /*
5595 * look up the property token as a symbol, to see if it's already
5596 * defined - we don't want to presume it's a property quite yet, but
5597 * we can at least look to see if it's defined as something else
5598 */
5599 CTcSymbol *sym = global_symtab_->find(
5600 prop_tok.get_text(), prop_tok.get_text_len());
5601
5602 /* skip it and see what comes next */
5603 switch (G_tok->next())
5604 {
5605 case TOKT_LPAR:
5606 case TOKT_LBRACE:
5607 parse_method:
5608 /* look up the symbol */
5609 prop_sym = look_up_prop(&prop_tok, TRUE);
5610
5611 /* vocabulary properties can't be code */
5612 if (prop_sym != 0 && prop_sym->is_vocab())
5613 G_tok->log_error_curtok(TCERR_VOCAB_REQ_SSTR);
5614
5615 /*
5616 * A left paren starts a formal parameter list, a left brace starts
5617 * a code body - in either case, we have a method, so parse the
5618 * code body. If we're parsing an inline object definition, parse
5619 * it as an anonymous method definition instead.
5620 */
5621 if (is_inline)
5622 {
5623 /* inline object - parse an anonymous method */
5624 G_tok->unget();
5625 CTPNAnonFunc *m = CTcPrsOpUnary::parse_anon_func(FALSE, TRUE);
5626
5627 /* add this as a property expression */
5628 if (m != 0)
5629 new_prop = objdef->add_inline_method(prop_sym, m, 0, replace);
5630 }
5631 else
5632 {
5633 /* top-level object - parse a code body */
5634 int argc;
5635 int opt_argc;
5636 int varargs;
5637 int varargs_list;
5638 CTcSymLocal *varargs_list_local;
5639 int has_retval;
5640 CTPNCodeBody *code_body = parse_code_body(
5641 FALSE, TRUE, TRUE, &argc, &opt_argc, &varargs,
5642 &varargs_list, &varargs_list_local, &has_retval, err, 0,
5643 TCPRS_CB_NORMAL, propset_stack, propset_depth, 0, 0);
5644
5645 /* check for errors in the code body */
5646 if (*err)
5647 return;
5648
5649 /*
5650 * if this is an operator, check arguments, and pick the
5651 * correct operator for the number of operands for operators
5652 * with multiple operand numbers
5653 */
5654 if (op_args != 0)
5655 {
5656 /* mark the code body as an operator overload method */
5657 if (code_body != 0)
5658 code_body->set_operator_overload(TRUE);
5659
5660 /* varargs aren't allowed */
5661 if (varargs)
5662 {
5663 G_tok->log_error(
5664 TCERR_OP_OVERLOAD_WRONG_FORMALS, op_args - 1);
5665 }
5666 else if (argc != op_args - 1)
5667 {
5668 /* not a valid combination of operator and arguments */
5669 G_tok->log_error(
5670 TCERR_OP_OVERLOAD_WRONG_FORMALS, op_args - 1);
5671 }
5672 }
5673
5674 /* add the method definition */
5675 if (prop_sym != 0)
5676 new_prop = objdef->add_method(prop_sym, code_body, 0, replace);
5677 }
5678 break;
5679
5680 case TOKT_SEM:
5681 case TOKT_RBRACE:
5682 case TOKT_RPAR:
5683 case TOKT_RBRACK:
5684 case TOKT_COMMA:
5685 /* log the error */
5686 G_tok->log_error(TCERR_OBJDEF_REQ_PROPVAL,
5687 (int)prop_tok.get_text_len(), prop_tok.get_text(),
5688 (int)G_tok->getcur()->get_text_len(),
5689 G_tok->getcur()->get_text());
5690
5691 /* if it's anything other than a semicolon, skip it */
5692 if (G_tok->cur() != TOKT_SEM)
5693 G_tok->next();
5694 break;
5695
5696 case TOKT_COLON:
5697 /*
5698 * It's a nested object definition. The value of the property is a
5699 * reference to an anonymous object defined in-line after the
5700 * colon. The in-line object definition uses the "braces" version
5701 * of the standard syntax: after the colon we have the class list,
5702 * an open brace, the property list, and a close brace.
5703 *
5704 * Note that we hold off defining the property symbol for as long
5705 * as possible, since we want to make sure that we don't have a
5706 * syntax error where this was intended to be a whole new top-level
5707 * object definition, but the terminator was accidentally left off
5708 * the previous object. In this case, the new object definition
5709 * would look like a nested object rather than a new top-level
5710 * object. This is probably the case if the symbol before the
5711 * colon is already known as an object name.
5712 */
5713 {
5714 /*
5715 * If the symbol before the colon is already known as an object
5716 * name, it must be intended as a new object rather than a
5717 * nested object, meaning that the terminator is missing from
5718 * the previous object definition. If this is the case, flag
5719 * the termination error and proceed to parse the new object
5720 * definition.
5721 */
5722 if (sym != 0 && sym->get_type() == TC_SYM_OBJ)
5723 {
5724 /*
5725 * flag the unterminated object error - note that the
5726 * location where termination should have occurred is
5727 * where the caller tells us
5728 */
5729 G_tcmain->log_error(term_info->desc_, term_info->linenum_,
5730 TC_SEV_ERROR, TCERR_UNTERM_OBJ_DEF);
5731
5732 /* unget the colon so we can parse the new object def */
5733 G_tok->unget();
5734
5735 /* flag the termination error for the caller */
5736 term_info->unterm_ = TRUE;
5737
5738 /* we're done */
5739 return;
5740 }
5741
5742 /* skip the colon */
5743 G_tok->next();
5744
5745 /* parse the nested object property */
5746 if (!objdef->parse_nested_obj_prop(
5747 new_prop, err, term_info, &prop_tok, replace))
5748 return;
5749 }
5750
5751 /* done */
5752 break;
5753
5754 case TOKT_EQ:
5755 /* skip the '=' */
5756 G_tok->next();
5757
5758 /*
5759 * if a brace follows, this must be using the obsolete TADS 2
5760 * notation, in which an equals sign followed a property name even
5761 * if a method was being defined; if this is the case, generate an
5762 * error, but then proceed to treat what follows as a method
5763 */
5764 if (G_tok->cur() == TOKT_LBRACE)
5765 {
5766 /* log the error... */
5767 G_tok->log_error(TCERR_EQ_WITH_METHOD_OBSOLETE);
5768
5769 /* ...but then go ahead and parse it as a method anyway */
5770 goto parse_method;
5771 }
5772
5773 /* go parse the value */
5774 goto parse_value;
5775
5776 default:
5777 /*
5778 * an '=' is required after a value property name; log the error
5779 * and proceed, assuming that the '=' was left out but that the
5780 * property value is otherwise well-formed
5781 */
5782 G_tok->log_error_curtok(TCERR_PROP_REQ_EQ);
5783
5784 parse_value:
5785 {
5786 /* look up the property symbol */
5787 prop_sym = look_up_prop(&prop_tok, TRUE);
5788
5789 /* note if it's a vocabulary property */
5790 int is_vocab_prop = (prop_sym != 0 && prop_sym->is_vocab());
5791
5792 /* we don't know whether we have a constant or method yet */
5793 CTPNCodeBody *code_body = 0;
5794 CTcPrsNode *expr = 0;
5795 CTPNAnonFunc *inline_method = 0;
5796
5797 /*
5798 * if this is a vocabulary property, perform special vocabulary
5799 * list parsing - a vocabulary property can have one or more
5800 * single-quoted strings that we store in a list, but the list
5801 * does not have to be enclosed in brackets
5802 */
5803 if (is_vocab_prop && G_tok->cur() == TOKT_SSTR)
5804 {
5805 /*
5806 * set the property to a vocab list placeholder for now in
5807 * the object - we'll replace it during linking with the
5808 * actual vocabulary list
5809 */
5810 CTcConstVal cval;
5811 cval.set_vocab_list();
5812 expr = new CTPNConst(&cval);
5813
5814 /* if I have no dictionary, it's an error */
5815 if (dict_cur_ == 0)
5816 G_tok->log_error(TCERR_VOCAB_NO_DICT);
5817
5818 /* get the object symbol */
5819 CTcSymObj *obj_sym = (objdef != 0 ? objdef->get_obj_sym() : 0);
5820
5821 /* parse the list entries */
5822 for (;;)
5823 {
5824 /* add the word to our vocabulary list */
5825 if (obj_sym != 0)
5826 obj_sym->add_vocab_word(
5827 G_tok->getcur()->get_text(),
5828 G_tok->getcur()->get_text_len(),
5829 prop_sym->get_prop());
5830
5831 /*
5832 * skip the string; if another string doesn't
5833 * immediately follow, we're done with the implied list
5834 *
5835 */
5836 if (G_tok->next() != TOKT_SSTR)
5837 break;
5838 }
5839 }
5840 else
5841 {
5842 /* if it's a vocabulary property, other types aren't allowed */
5843 if (is_vocab_prop)
5844 G_tok->log_error_curtok(TCERR_VOCAB_REQ_SSTR);
5845
5846 /* check for the 'static' keyword */
5847 if (G_tok->cur() == TOKT_STATIC)
5848 {
5849 /* note that it's static */
5850 is_static = TRUE;
5851
5852 /* skip the 'static' keyword */
5853 G_tok->next();
5854 }
5855
5856 /* prepare to parse the property value expression */
5857 CTcPrsPropExprSave save_info;
5858 begin_prop_expr(&save_info, is_static, is_inline);
5859
5860 /*
5861 * parse the expression (which can be a double-quoted
5862 * string instead of a value expression)
5863 */
5864 expr = parse_expr_or_dstr(TRUE);
5865 if (expr == 0)
5866 {
5867 *err = TRUE;
5868 return;
5869 }
5870
5871 /*
5872 * finish the expression, and check to see if it needs
5873 * dynamic evaluation (if so, it will yield a code body)
5874 */
5875 finish_prop_expr(
5876 &save_info, expr, code_body, inline_method,
5877 is_static, is_inline, prop_sym);
5878 }
5879
5880 /* if we have a valid property and expression, add it */
5881 if (prop_sym != 0)
5882 {
5883 /* add the expression or code body, as appropriate */
5884 if (code_body != 0)
5885 {
5886 /* method */
5887 new_prop = objdef->add_method(
5888 prop_sym, code_body, expr, replace);
5889 }
5890 else if (inline_method != 0)
5891 {
5892 /* inline method */
5893 new_prop = objdef->add_inline_method(
5894 prop_sym, inline_method, expr, replace);
5895 }
5896 else if (expr != 0)
5897 {
5898 /* constant expression */
5899 new_prop = objdef->add_prop(
5900 prop_sym, expr, replace, is_static);
5901 }
5902 }
5903 }
5904
5905 /* done with the property */
5906 break;
5907 }
5908
5909 /* make sure that the object doesn't already define this propery */
5910 if (sym != 0)
5911 {
5912 /* presume it's not a duplicate */
5913 int is_dup = FALSE;
5914
5915 /*
5916 * if the enclosing object is nested, and this is the lexicalParent
5917 * property, then it's defined automatically by the compiler and
5918 * thus can't be redefined explicitly
5919 */
5920 if (obj_is_nested && sym == lexical_parent_sym_)
5921 is_dup = TRUE;
5922
5923 /* if we didn't already find a duplicate, scan the existing list */
5924 if (!is_dup)
5925 {
5926 /* scan the property list for this object */
5927 for (CTPNObjProp *obj_prop = objdef->get_first_prop() ;
5928 obj_prop != 0 ; obj_prop = obj_prop->get_next_prop())
5929 {
5930 /*
5931 * if it matches (and it's not the one we just added), log
5932 * an error
5933 */
5934 if (obj_prop != new_prop && obj_prop->get_prop_sym() == sym)
5935 {
5936 /*
5937 * if this property is overwritable, we're allowed to
5938 * replace it with a new value; otherwise, it's an
5939 * illegal duplicate
5940 */
5941 if (obj_prop->is_overwritable())
5942 {
5943 /*
5944 * we're allowed to overwrite it with an explicit
5945 * redefinition; simply remove the old property
5946 * from the list so we can add the new one
5947 */
5948 objdef->delete_property(obj_prop->get_prop_sym());
5949 }
5950 else
5951 {
5952 /* note that we found a duplicate property */
5953 is_dup = TRUE;
5954 }
5955
5956 /* no need to continue looking */
5957 break;
5958 }
5959 }
5960 }
5961
5962 /* if we found a duplicate, log an error */
5963 if (is_dup)
5964 G_tok->log_error(TCERR_PROP_REDEF_IN_OBJ,
5965 (int)sym->get_sym_len(), sym->get_sym());
5966 }
5967
5968 /*
5969 * if we're modifying an intrinsic class, make sure the property isn't
5970 * defined in the class's native interface
5971 */
5972 if (meta_sym != 0 && prop_sym != 0)
5973 {
5974 /* check this intrinsic class and all of its superclasses */
5975 for (CTcSymMetaclass *cur_meta = meta_sym ; cur_meta != 0 ;
5976 cur_meta = cur_meta->get_super_meta())
5977 {
5978 /* scan the list of native methods */
5979 for (CTcSymMetaProp *mprop = cur_meta->get_prop_head() ;
5980 mprop != 0 ; mprop = mprop->nxt_)
5981 {
5982 /* if it matches, flag an error */
5983 if (mprop->prop_ == prop_sym)
5984 {
5985 /* log the error */
5986 G_tok->log_error(TCERR_CANNOT_MOD_META_PROP,
5987 prop_sym->get_sym_len(),
5988 prop_sym->get_sym());
5989
5990 /* no need to look any further */
5991 break;
5992 }
5993 }
5994 }
5995 }
5996
5997 /*
5998 * if this is a dictionary property, note that it's been defined for
5999 * this object
6000 */
6001 if (prop_sym != 0 && prop_sym->is_vocab())
6002 {
6003 /* scan the list of dictionary properties for this one */
6004 for (CTcDictPropEntry *entry = dict_prop_head_ ; entry != 0 ;
6005 entry = entry->nxt_)
6006 {
6007 /* check this one for a match */
6008 if (entry->prop_ == prop_sym)
6009 {
6010 /* mark this entry as defined for this object */
6011 entry->defined_ = TRUE;
6012
6013 /* no need to look any further */
6014 break;
6015 }
6016 }
6017 }
6018 }
6019
6020 /* ------------------------------------------------------------------------ */
6021 /*
6022 * Look up a property symbol. If the symbol isn't defined, add it. If
6023 * it's defined as some other kind of symbol, we'll log an error and return
6024 * null.
6025 */
6026 CTcSymProp *CTcParser::look_up_prop(const CTcToken *tok, int show_err)
6027 {
6028 /* look up the symbol */
6029 CTcSymProp *prop = (CTcSymProp *)global_symtab_->find(
6030 tok->get_text(), tok->get_text_len());
6031
6032 /* if it's already defined, make sure it's a property */
6033 if (prop != 0)
6034 {
6035 /* make sure it's a property */
6036 if (prop->get_type() != TC_SYM_PROP)
6037 {
6038 /* log an error if appropriate */
6039 if (show_err)
6040 G_tok->log_error(TCERR_REDEF_AS_PROP,
6041 (int)tok->get_text_len(), tok->get_text());
6042
6043 /* we can't use the symbol, so forget it */
6044 prop = 0;
6045 }
6046 }
6047 else
6048 {
6049 /* create the new property symbol */
6050 prop = new CTcSymProp(tok->get_text(), tok->get_text_len(),
6051 FALSE, G_cg->new_prop_id());
6052
6053 /* add it to the global symbol table */
6054 global_symtab_->add_entry(prop);
6055
6056 /* mark it as referenced */
6057 prop->mark_referenced();
6058 }
6059
6060 /* return the property symbol */
6061 return prop;
6062 }
6063
6064 /* ------------------------------------------------------------------------ */
6065 /*
6066 * Mix-in base class for object definitions (statement type or in-line
6067 * expression type)
6068 */
6069
6070 /*
6071 * Add a property value
6072 */
6073 CTPNObjProp *CTPNObjDef::add_prop(
6074 CTcSymProp *prop_sym, CTcPrsNode *expr, int replace, int is_static)
6075 {
6076 /* create the new property item */
6077 CTPNObjProp *prop = new CTPNObjProp(this, prop_sym, expr, 0, 0, is_static);
6078
6079 /* link it into my list */
6080 add_prop_entry(prop, replace);
6081
6082 /* return the new property to our caller */
6083 return prop;
6084 }
6085
6086 /*
6087 * Add a method
6088 */
6089 CTPNObjProp *CTPNObjDef::add_method(
6090 CTcSymProp *prop, CTPNCodeBody *code_body, CTcPrsNode *expr, int replace)
6091 {
6092 /* create a new property */
6093 CTPNObjProp *objprop = new CTPNObjProp(this, prop, 0, code_body, 0, FALSE);
6094
6095 /* link it into my list */
6096 add_prop_entry(objprop, replace);
6097
6098 /* return it */
6099 return objprop;
6100 }
6101
6102 /*
6103 * Add a method to an in-line object
6104 */
6105 CTPNObjProp *CTPNObjDef::add_inline_method(
6106 CTcSymProp *prop, CTPNAnonFunc *inline_method,
6107 CTcPrsNode *expr, int replace)
6108 {
6109 /* create a new property */
6110 CTPNObjProp *objprop = new CTPNObjProp(
6111 this, prop, expr, 0, inline_method, FALSE);
6112
6113 /* link it into my list */
6114 add_prop_entry(objprop, replace);
6115
6116 /* return it */
6117 return objprop;
6118 }
6119
6120 /*
6121 * fold constants
6122 */
6123 void CTPNObjDef::fold_proplist(CTcPrsSymtab *symtab)
6124 {
6125 /* fold constants in each of our property list entries */
6126 for (CTPNObjProp *prop = proplist_.first_ ; prop != 0 ; prop = prop->nxt_)
6127 prop->fold_constants(symtab);
6128 }
6129
6130
6131 /* ------------------------------------------------------------------------ */
6132 /*
6133 * Object Definition Statement
6134 */
6135
6136 /*
6137 * add a superclass given the name
6138 */
6139 void CTPNStmObjectBase::add_superclass(const CTcToken *tok)
6140 {
6141 /* add a new superclass list entry to my list */
6142 sclist_.append(new CTPNSuperclass(tok->get_text(), tok->get_text_len()));
6143 }
6144
6145 /*
6146 * add a superclass given the object symbol
6147 */
6148 void CTPNStmObjectBase::add_superclass(CTcSymbol *sym)
6149 {
6150 /* add a new superclass entry to my list */
6151 sclist_.append(new CTPNSuperclass(sym));
6152 }
6153
6154 /*
6155 * Add an implicit constructor, if one is required.
6156 */
6157 void CTPNStmObjectBase::add_implicit_constructor()
6158 {
6159 /* count the superclasses */
6160 int sc_cnt;
6161 CTPNSuperclass *sc;
6162 for (sc_cnt = 0, sc = get_first_sc() ; sc != 0 ; sc = sc->nxt_, ++sc_cnt) ;
6163
6164 /*
6165 * If the object has multiple superclasses and doesn't have an explicit
6166 * constructor, add an implicit constructor that simply invokes each
6167 * superclass constructor using the same argument list sent to our
6168 * constructor.
6169 */
6170 if (sc_cnt > 1)
6171 {
6172 /* scan the property list for a constructor property */
6173 CTPNObjProp *prop;
6174 int has_constructor;
6175 for (prop = proplist_.first_, has_constructor = FALSE ; prop != 0 ;
6176 prop = prop->nxt_)
6177 {
6178 /*
6179 * if this is a constructor, note that the object has an
6180 * explicit constructor
6181 */
6182 if (prop->is_constructor())
6183 {
6184 /* we have a consructor */
6185 has_constructor = TRUE;
6186
6187 /* that's all we needed to know - no need to keep looking */
6188 break;
6189 }
6190 }
6191
6192 /* if we don't have a constructor, add one */
6193 if (!has_constructor)
6194 {
6195 /*
6196 * Create a placeholder symbol for the varargs list variable -
6197 * we don't really need a symbol for this, but we do need the
6198 * symbol object so we can generate code for it properly. Note
6199 * that a varargs list variable is a local, not a formal.
6200 */
6201 CTcSymLocal *varargs_local = new CTcSymLocal(
6202 "*", 1, FALSE, FALSE, 0);
6203
6204 /* create the pseudo-statement for the implicit constructor */
6205 CTPNStmImplicitCtor *stm = new CTPNStmImplicitCtor(
6206 (CTPNStmObject *)this);
6207
6208 /*
6209 * Create a code body to hold the statement. The code body has
6210 * one local symbol (the vararg list formal parameter), and is
6211 * defined by the 'constructor' property.
6212 */
6213 CTPNCodeBody *cb = new CTPNCodeBody(
6214 G_prs->get_global_symtab(), G_prs->get_goto_symtab(),
6215 stm, 0, 0, TRUE, TRUE, varargs_local, 1, TRUE, 0);
6216
6217 /*
6218 * set the implicit constructor's source file location to the
6219 * source file location of the start of the object definition
6220 */
6221 cb->set_source_pos(file_, linenum_);
6222
6223 /* add it to the object's property list */
6224 add_method(G_prs->get_constructor_sym(), cb, 0, FALSE);
6225 }
6226 }
6227 }
6228
6229 /*
6230 * Add an entry to my property list
6231 */
6232 void CTPNStmObjectBase::add_prop_entry(CTPNObjProp *prop, int replace)
6233 {
6234 /* inherit the default handling */
6235 CTPNObjDef::add_prop_entry(prop, replace);
6236
6237 /* check for replacement */
6238 if (replace && !G_prs->get_syntax_only())
6239 {
6240 /* start at the current object */
6241 CTPNStmObject *stm = (CTPNStmObject *)this;
6242
6243 /* iterate through all previous versions of the object */
6244 for (;;)
6245 {
6246 /*
6247 * Get this object's superclass - since this is a 'modify'
6248 * object, its only superclass is object it directly modifies.
6249 * If there's no superclass, the modified base object must have
6250 * been undefined, so we have errors to begin with - ignore the
6251 * replacement in this case.
6252 */
6253 CTPNSuperclass *sc = stm->get_first_sc();
6254 if (sc == 0)
6255 break;
6256
6257 /* get the symbol for the superclass */
6258 CTcSymObj *sym = (CTcSymObj *)sc->get_sym();
6259
6260 /*
6261 * if this object is external, we must mark the property for
6262 * replacement so that we replace it at link time, after we've
6263 * loaded and resolved the external original object
6264 */
6265 if (sym->is_extern())
6266 {
6267 /*
6268 * Add an entry to my list of properties to delete in my
6269 * base objects at link time.
6270 */
6271 obj_sym_->add_del_prop_entry(prop->get_prop_sym());
6272
6273 /*
6274 * no need to look any further for modified originals -- we
6275 * won't find any more of them in this translation unit,
6276 * since they must all be external from here on up the
6277 * chain
6278 */
6279 break;
6280 }
6281
6282 /*
6283 * Get this symbol's object parse tree - since the symbol isn't
6284 * external, and because we can only modify objects that we've
6285 * already seen, the parse tree must be present. If the parse
6286 * tree isn't here, stop now; we might be parsing for syntax
6287 * only in this case.
6288 */
6289 stm = sym->get_obj_stm();
6290 if (stm == 0)
6291 break;
6292
6293 /* if this object isn't modified, we're done */
6294 if (!sym->get_modified())
6295 break;
6296
6297 /* delete the property in the original object */
6298 stm->delete_property(prop->get_prop_sym());
6299 }
6300 }
6301 }
6302
6303 /*
6304 * Delete a property
6305 */
6306 void CTPNStmObjectBase::delete_property(CTcSymProp *prop_sym)
6307 {
6308 /* delete the symbol from the property list */
6309 if (proplist_.del(prop_sym))
6310 {
6311 /* found it - delete the property from our vocabulary list as well */
6312 if (obj_sym_ != 0)
6313 obj_sym_->delete_vocab_prop(prop_sym->get_prop());
6314 }
6315 }
6316
6317 /* ------------------------------------------------------------------------ */
6318 /*
6319 * Inline object
6320 */
6321
6322 /*
6323 * Parse a property defining a nested object. In an inline object, a
6324 * nested object is treated as an expression containing an inline object:
6325 * we evaluate the property expression at the time of instantiating the
6326 * enclosing inline object, so this has the effect of creating the nested
6327 * object at the same time we create the enclosing inline object.
6328 */
6329 int CTPNInlineObjectBase::parse_nested_obj_prop(
6330 class CTPNObjProp* &new_prop, int *err, tcprs_term_info *term_info,
6331 const class CTcToken *prop_tok, int replace)
6332 {
6333 /*
6334 * Parse the inline object. The caller has already parsed and skipped
6335 * a colon, so tell the inline object parser that we're already past
6336 * the colon.
6337 */
6338 CTPNInlineObject *objdef = CTcPrsOpUnary::parse_inline_object(TRUE);
6339 if (objdef == 0)
6340 {
6341 *err = TRUE;
6342 return FALSE;
6343 }
6344
6345 /* look up the property symbol */
6346 CTcSymProp *prop_sym = G_prs->look_up_prop(prop_tok, TRUE);
6347 if (prop_sym != 0 && prop_sym->is_vocab())
6348 G_tok->log_error(TCERR_VOCAB_REQ_SSTR, 1, ":");
6349
6350 /* if we have a valid property, add it */
6351 if (prop_sym != 0)
6352 new_prop = add_prop(prop_sym, objdef, replace, FALSE);
6353
6354 /*
6355 * Define the lexicalParent property in the nested object with a
6356 * reference back to the parent object.
6357 */
6358 CTcConstVal cval;
6359 cval.set_obj(get_obj_sym() != 0
6360 ? get_obj_sym()->get_obj_id() : TCTARG_INVALID_OBJ,
6361 get_obj_sym() != 0
6362 ? get_obj_sym()->get_metaclass() : TC_META_UNKNOWN);
6363 CTcPrsNode *cexpr = new CTPNConst(&cval);
6364 objdef->add_prop(G_prs->get_lexical_parent_sym(), cexpr, FALSE, FALSE);
6365
6366 /* success */
6367 return TRUE;
6368 }
6369
6370 /*
6371 * adjust for dynamic compilation
6372 */
6373 CTcPrsNode *CTPNInlineObjectBase::adjust_for_dyn(const tcpn_dyncomp_info *info)
6374 {
6375 /* adjust each property expression */
6376 for (CTPNObjProp *prop = proplist_.first_ ; prop != 0 ;
6377 prop = prop->get_next_prop())
6378 prop->adjust_for_dyn(info);
6379
6380 /* we did our updates in-place */
6381 return this;
6382 }
6383
6384 /* ------------------------------------------------------------------------ */
6385 /*
6386 * Object property list
6387 */
6388
6389 /*
6390 * append a property
6391 */
6392 void CTPNPropList::append(CTPNObjProp *prop)
6393 {
6394 *dst_ = prop;
6395 *(dst_ = &prop->nxt_) = 0;
6396 last_ = prop;
6397 ++cnt_;
6398 }
6399
6400 /*
6401 * delete a property
6402 */
6403 int CTPNPropList::del(CTcSymProp *prop_sym)
6404 {
6405 /* scan our and look for a property matching the given symbol */
6406 for (CTPNObjProp **prvp = &first_, *cur = *prvp ; cur != 0 ;
6407 prvp = &cur->nxt_, cur = *prvp)
6408 {
6409 /* check for a match */
6410 if (cur->get_prop_sym() == prop_sym)
6411 {
6412 /* this is the one to delete - unlink it from the list */
6413 *prvp = cur->nxt_;
6414
6415 /* if that left the list empty, clear the tail pointer */
6416 if (first_ == 0)
6417 last_ = 0;
6418
6419 /* make sure the append pointer points to the tail's nxt_ */
6420 dst_ = (last_ != 0 ? &last_->nxt_ : &first_);
6421
6422 /* decrement the property count */
6423 --cnt_;
6424
6425 /* indicate that we found it */
6426 return TRUE;
6427 }
6428 }
6429
6430 /* didn't find it */
6431 return FALSE;
6432 }
6433
6434 /* ------------------------------------------------------------------------ */
6435 /*
6436 * Object Property node
6437 */
6438
6439 /*
6440 * fold constants
6441 */
6442 CTcPrsNode *CTPNObjPropBase::fold_constants(CTcPrsSymtab *symtab)
6443 {
6444 /*
6445 * if we have a code body or inline method, check for an underlying
6446 * expression that the method wraps
6447 */
6448 if ((code_body_ != 0 || inline_method_ != 0) && expr_ != 0)
6449 {
6450 /* fold the expression */
6451 expr_ = expr_->fold_constants(symtab);
6452
6453 /* if it's now a constant, drop the code body/method */
6454 if (expr_->is_const() || expr_->is_dstring())
6455 {
6456 if (code_body_ != 0)
6457 {
6458 code_body_->set_replaced(TRUE);
6459 code_body_ = 0;
6460 }
6461 if (inline_method_ != 0)
6462 {
6463 inline_method_->set_replaced(TRUE);
6464 inline_method_ = 0;
6465 }
6466 }
6467 else
6468 {
6469 /*
6470 * the expression is non-constant, so we need to generate the
6471 * code body for run-time evaluation; we can now drop the
6472 * expression, since the code wrapper supersedes it
6473 */
6474 expr_ = 0;
6475 }
6476 }
6477
6478 /* if we have an inline method or regular method, fold its constants */
6479 if (inline_method_ != 0)
6480 inline_method_->fold_constants(symtab);
6481 else if (code_body_ != 0)
6482 code_body_->fold_constants(symtab);
6483
6484 /* if we have an expression, fold it */
6485 if (expr_ != 0)
6486 {
6487 /*
6488 * set this statement's source line location to be the current
6489 * location for error reporting
6490 */
6491 G_tok->set_line_info(get_source_desc(), get_source_linenum());
6492
6493 /* fold constants in our expression */
6494 expr_ = expr_->fold_constants(symtab);
6495
6496 /*
6497 * If the resulting expression isn't a constant value, we need to
6498 * convert the expression to a method, since we need to evaluate
6499 * the value at run-time with code. Create an expression statement
6500 * to hold the expression, then create a code body to hold the
6501 * expression statement. Don't do this for inline objects, though;
6502 * expression properties in inline objects are evaluated at the
6503 * time the object is created, so they don't get made into methods.
6504 */
6505 if (!objdef_->is_inline_object()
6506 && !expr_->is_const() && !expr_->is_dstring())
6507 {
6508 /*
6509 * Generate an implicit 'return' for a regular expression, or
6510 * an implicit static initializer for a static property.
6511 */
6512 CTPNStm *stm;
6513 if (is_static_)
6514 {
6515 /*
6516 * it's a static initializer - generate a static
6517 * initializer expression
6518 */
6519 stm = new CTPNStmStaticPropInit(expr_, prop_sym_->get_prop());
6520 }
6521 else if (expr_->has_return_value())
6522 {
6523 /*
6524 * create a 'return' statement to return the value of the
6525 * expression
6526 */
6527 stm = new CTPNStmReturn(expr_);
6528 }
6529 else
6530 {
6531 /*
6532 * It has no return value, so it must be something like a
6533 * double-quoted string or a call to a void function. Wrap
6534 * this in an 'expression statement' so that we evaluate
6535 * the expression for its side effects but discard any
6536 * return value.
6537 */
6538 stm = new CTPNStmExpr(expr_);
6539 }
6540
6541 /* create the code body */
6542 code_body_ = new CTPNCodeBody(
6543 G_prs->get_global_symtab(), 0, stm, 0, 0,
6544 FALSE, FALSE, 0, 0, TRUE, 0);
6545
6546 /*
6547 * forget about our expression, since it's now incorporated
6548 * into the code body
6549 */
6550 expr_ = 0;
6551
6552 /*
6553 * set the source file location in both the code body and the
6554 * expression statement nodes to use our own source file
6555 * location
6556 */
6557 stm->set_source_pos(file_, linenum_);
6558 code_body_->set_source_pos(file_, linenum_);
6559 }
6560 }
6561
6562 /* we're not changed directly by this */
6563 return this;
6564 }
6565
6566 /*
6567 * adjust for dynamic execution
6568 */
6569 CTcPrsNode *CTPNObjPropBase::adjust_for_dyn(const tcpn_dyncomp_info *info)
6570 {
6571 /* adjust my various elements */
6572 if (code_body_ != 0)
6573 code_body_->adjust_for_dyn(info);
6574 if (inline_method_ != 0)
6575 inline_method_->adjust_for_dyn(info);
6576 if (expr_ != 0)
6577 expr_ = expr_->adjust_for_dyn(info);
6578
6579 /* we did our adjustments in-place */
6580 return this;
6581 }
6582
6583
6584 /*
6585 * Am I a constructor?
6586 */
6587 int CTPNObjPropBase::is_constructor() const
6588 {
6589 /*
6590 * I'm a constructor if I have a code body, and my property is the
6591 * constructor property.
6592 */
6593 return (code_body_ != 0
6594 && prop_sym_ != 0
6595 && prop_sym_->get_prop() == G_prs->get_constructor_prop());
6596 }
6597
6598
6599 /* ------------------------------------------------------------------------ */
6600 /*
6601 * compound statement base class
6602 */
6603
6604 /*
6605 * instantiate
6606 */
6607 CTPNStmCompBase::CTPNStmCompBase(CTPNStm *first_stm, CTcPrsSymtab *symtab)
6608 {
6609 /* remember the first statement in our statement list */
6610 first_stm_ = first_stm;
6611
6612 /* remember our local symbol table */
6613 symtab_ = symtab;
6614
6615 /*
6616 * presume we don't have our own private symbol table, but will
6617 * simply use our parent's
6618 */
6619 has_own_scope_ = FALSE;
6620
6621 /*
6622 * remember the source location of the closing brace, which should
6623 * be the current location when we're instantiated
6624 */
6625 end_desc_ = G_tok->get_last_desc();
6626 end_linenum_ = G_tok->get_last_linenum();
6627 }
6628
6629 /*
6630 * fold constants - we simply fold constants in each of our statements
6631 */
6632 CTcPrsNode *CTPNStmCompBase::fold_constants(class CTcPrsSymtab *symtab)
6633 {
6634 /* iterate through our statements and fold constants in each one */
6635 for (CTPNStm *cur = first_stm_ ; cur != 0 ; cur = cur->get_next_stm())
6636 {
6637 /*
6638 * set this statement's source line location to be the current
6639 * location for error reporting
6640 */
6641 G_tok->set_line_info(cur->get_source_desc(),
6642 cur->get_source_linenum());
6643
6644 /*
6645 * Fold constants, using the enclosing symbol table (not the
6646 * local symbol table - during code generation we can resolve
6647 * only to the global scope, since local scope symbols must
6648 * always be resolved during parsing). We assume that statement
6649 * nodes are never replaced during constant folding, so we
6650 * ignore the result of this call and keep our original
6651 * statement list entry.
6652 */
6653 cur->fold_constants(symtab);
6654 }
6655
6656 /*
6657 * although nodes within our subtree might have been changed, this
6658 * compound statement itself is unchanged by constant folding
6659 */
6660 return this;
6661 }
6662
6663 /*
6664 * adjust for dynamic (run-time) compilation
6665 */
6666 CTcPrsNode *CTPNStmCompBase::adjust_for_dyn(const tcpn_dyncomp_info *info)
6667 {
6668 /* adjust each statement in our list */
6669 for (CTPNStm *cur = first_stm_ ; cur != 0 ; cur = cur->get_next_stm())
6670 {
6671 /*
6672 * adjust this statement - we assume that statements are never
6673 * replaced by this operation, so we just keep our existing list
6674 */
6675 cur->adjust_for_dyn(info);
6676 }
6677
6678 /* return myself */
6679 return this;
6680 }
6681
6682
6683 /*
6684 * Generate code
6685 */
6686 void CTPNStmCompBase::gen_code(int, int)
6687 {
6688 CTPNStm *cur;
6689 CTcPrsSymtab *old_frame;
6690
6691 /* set my local scope symbol frame, if necessary */
6692 old_frame = G_cs->set_local_frame(symtab_);
6693
6694 /* set the code location for the start of the group */
6695 add_debug_line_rec();
6696
6697 /*
6698 * iterate through our statements and generate code for each in
6699 * sequence
6700 */
6701 for (cur = first_stm_ ; cur != 0 ; cur = cur->get_next_stm())
6702 {
6703 /*
6704 * set this statement's source line location to be the current
6705 * location for error reporting
6706 */
6707 G_tok->set_line_info(cur->get_source_desc(),
6708 cur->get_source_linenum());
6709
6710 /*
6711 * Generate code for the statement. Note that we generate in
6712 * the scope of the enclosing symbol table, because we never
6713 * resolve symbols to local scope during code generation - local
6714 * scope symbols must be resolved during parsing, because these
6715 * symbols must always be declared before first being used
6716 *
6717 * We have no use for the results of any expressions, so discard
6718 * = true; we don't care about the form of any logical operator
6719 * results, so use the looser "for condition" rules
6720 */
6721 cur->gen_code(TRUE, TRUE);
6722 }
6723
6724 /* set the source location for the end of the group */
6725 add_debug_line_rec(end_desc_, end_linenum_);
6726
6727 /* check for unreferenced local variables */
6728 if (symtab_ != 0 && has_own_scope_)
6729 {
6730 /*
6731 * set our line information to be current again, so that any
6732 * unreferenced local errors are reported at the start of the
6733 * compound statement
6734 */
6735 G_tok->set_line_info(get_source_desc(),
6736 get_source_linenum());
6737 }
6738
6739 /* restore the enclosing local frame */
6740 old_frame = G_cs->set_local_frame(old_frame);
6741 }
6742
6743 /*
6744 * Chart the control flow through the statement list
6745 */
6746 unsigned long CTPNStmCompBase::get_control_flow(int warn) const
6747 {
6748 /*
6749 * presume we will not find a 'throw' or 'return', and that we'll be
6750 * able to reach the next statement after the list
6751 */
6752 unsigned long flags = TCPRS_FLOW_NEXT;
6753
6754 /*
6755 * iterate through the statements in our list, since they'll be
6756 * executed in the same sequence at run-time
6757 */
6758 for (CTPNStm *cur = first_stm_ ; cur != 0 ; cur = cur->get_next_stm())
6759 {
6760 /* get this statement's control flow characteristics */
6761 unsigned long flags1 = cur->get_control_flow(warn);
6762
6763 /*
6764 * OR each of 'throws', 'returns void', and 'returns value',
6765 * since anything a reachable statement does to return or throw,
6766 * the overall list can do to leave. Likewise, the 'break',
6767 * 'goto', and 'continue' flags push up into the enclosing
6768 * statement.
6769 */
6770 flags |= (flags1 & (TCPRS_FLOW_THROW
6771 | TCPRS_FLOW_RET_VOID
6772 | TCPRS_FLOW_RET_VAL
6773 | TCPRS_FLOW_GOTO
6774 | TCPRS_FLOW_BREAK
6775 | TCPRS_FLOW_CONT));
6776
6777 /*
6778 * if this statement can't continue to the next statement, the
6779 * remainder of this compound statement is unreachable, unless
6780 * it has a code label
6781 */
6782 if (!(flags1 & TCPRS_FLOW_NEXT))
6783 {
6784 /*
6785 * we can't reach the next statement; unless we find a label
6786 * at some point after this, we won't be able to reach any
6787 * of the remaining statements, hence we won't be able to
6788 * reach the statement after our list
6789 */
6790 flags &= ~TCPRS_FLOW_NEXT;
6791
6792 /*
6793 * If another statement follows, check to see if it's
6794 * reachable by label, because it's not reachable by any
6795 * other means
6796 */
6797 CTPNStm *nxt = cur->get_next_stm();
6798 if (nxt != 0)
6799 {
6800 /*
6801 * check to see if this statement has a label; if not,
6802 * warn that it is unreachable
6803 */
6804 if (warn
6805 && !nxt->has_code_label()
6806 && !G_prs->get_syntax_only())
6807 nxt->log_warning(TCERR_UNREACHABLE_CODE);
6808
6809 /* skip ahead until we find something with a label */
6810 while (nxt != 0 && !nxt->has_code_label())
6811 {
6812 /* remember the previous statement */
6813 cur = nxt;
6814
6815 /* move on to the next statement */
6816 nxt = nxt->get_next_stm();
6817 }
6818
6819 /*
6820 * if we found a reachable statement again, we might be
6821 * able to continue from the end of the list after all
6822 */
6823 if (nxt != 0)
6824 flags |= TCPRS_FLOW_NEXT;
6825 }
6826 }
6827 }
6828
6829 /* return the result */
6830 return flags;
6831 }
6832
6833 /* ------------------------------------------------------------------------ */
6834 /*
6835 * expression statement base class
6836 */
6837
6838 /*
6839 * fold constants
6840 */
6841 CTcPrsNode *CTPNStmExprBase::fold_constants(class CTcPrsSymtab *symtab)
6842 {
6843 /* set our location for any errors that occur */
6844 G_tok->set_line_info(get_source_desc(), get_source_linenum());
6845
6846 /*
6847 * fold constants in our expression, replacing our expression with
6848 * the folded version
6849 */
6850 expr_ = expr_->fold_constants(symtab);
6851
6852 /*
6853 * although our expression subtree might have been changed, this
6854 * compound statement itself is unchanged by constant folding
6855 */
6856 return this;
6857 }
6858
6859 /*
6860 * Generate code
6861 */
6862 void CTPNStmExprBase::gen_code(int, int)
6863 {
6864 /* set our location for any errors that occur */
6865 G_tok->set_line_info(get_source_desc(), get_source_linenum());
6866
6867 /* add a debug line record for the statement */
6868 add_debug_line_rec();
6869
6870 /*
6871 * generate code in our expression - the result will not be used in any
6872 * further calculation, so discard it; and since we don't care about
6873 * the return value, use the looser "for condition" rules for any
6874 * top-level logical operators in the expression so that we don't
6875 * bother applying any extra conversions to a value we're just
6876 * discarding anyway
6877 */
6878 expr_->gen_code(TRUE, TRUE);
6879 }
6880
6881 /* ------------------------------------------------------------------------ */
6882 /*
6883 * 'if' statement
6884 */
6885
6886 /*
6887 * fold constants
6888 */
6889 CTcPrsNode *CTPNStmIfBase::fold_constants(CTcPrsSymtab *symtab)
6890 {
6891 /* set our location for any errors that occur */
6892 G_tok->set_line_info(get_source_desc(), get_source_linenum());
6893
6894 /*
6895 * fold constants in the condition, replacing the expression with
6896 * the folded expression
6897 */
6898 cond_expr_ = cond_expr_->fold_constants(symtab);
6899
6900 /* fold constants in the substatements */
6901 if (then_part_ != 0)
6902 then_part_ = (CTPNStm *)then_part_->fold_constants(symtab);
6903 if (else_part_ != 0)
6904 else_part_ = (CTPNStm *)else_part_->fold_constants(symtab);
6905
6906 /* we are not directly changed by this operation */
6907 return this;
6908 }
6909
6910 /*
6911 * Chart the control flow through the conditional
6912 */
6913 unsigned long CTPNStmIfBase::get_control_flow(int warn) const
6914 {
6915 unsigned long flags1;
6916 unsigned long flags2;
6917
6918 /*
6919 * if my condition is constant, our control flow is determined
6920 * entirely by the branch we'll take
6921 */
6922 if (cond_expr_ != 0 && cond_expr_->is_const())
6923 {
6924 CTPNStm *part;
6925
6926 /*
6927 * We have a constant expression - if it's true, our control
6928 * flow is determined by the 'then' part, otherwise it's
6929 * determines by the 'else' part.
6930 */
6931 part = (cond_expr_->get_const_val()->get_val_bool()
6932 ? then_part_ : else_part_);
6933
6934 /*
6935 * if the part determining the control flow is a null statement,
6936 * we'll simply continue to the next statement; otherwise, the
6937 * result is the control flow of the determining statement
6938 */
6939 if (part == 0)
6940 {
6941 /* it's a null statement, which merely continues */
6942 return TCPRS_FLOW_NEXT;
6943 }
6944 else
6945 {
6946 /* our control flow is this statement's control flow */
6947 return part->get_control_flow(warn);
6948 }
6949 }
6950
6951 /*
6952 * The control flow through the 'if' is a combination of the control
6953 * flow through the two parts. If one of the parts is a void
6954 * statement, it continues to the next statement after the 'if'.
6955 */
6956
6957 /* check the 'then' part if present */
6958 flags1 = (then_part_ != 0
6959 ? then_part_->get_control_flow(warn) : TCPRS_FLOW_NEXT);
6960
6961 /* check the 'else' part if present */
6962 flags2 = (else_part_ != 0
6963 ? else_part_->get_control_flow(warn) : TCPRS_FLOW_NEXT);
6964
6965 /* combine the parts to get the possible control flows */
6966 return flags1 | flags2;
6967 }
6968
6969 /* ------------------------------------------------------------------------ */
6970 /*
6971 * "for" statement node
6972 */
6973
6974 /*
6975 * fold constants
6976 */
6977 CTcPrsNode *CTPNStmForBase::fold_constants(CTcPrsSymtab *symtab)
6978 {
6979 /* set our location for any errors that occur */
6980 G_tok->set_line_info(get_source_desc(), get_source_linenum());
6981
6982 /*
6983 * fold constants in each of our expressions, replacing the
6984 * expressions with the folded versions
6985 */
6986 if (init_expr_ != 0)
6987 init_expr_ = init_expr_->fold_constants(symtab);
6988 if (cond_expr_ != 0)
6989 cond_expr_ = cond_expr_->fold_constants(symtab);
6990 if (reinit_expr_ != 0)
6991 reinit_expr_ = reinit_expr_->fold_constants(symtab);
6992
6993 /* fold constants in the body statement */
6994 if (body_stm_ != 0)
6995 body_stm_ = (CTPNStm *)body_stm_->fold_constants(symtab);
6996
6997 /* we are not directly changed by this operation */
6998 return this;
6999 }
7000
7001 /*
7002 * Chart the control flow through the loop
7003 */
7004 unsigned long CTPNStmForBase::get_control_flow(int warn) const
7005 {
7006 unsigned long flags;
7007
7008 /*
7009 * If we have a condition with a constant false value, and there's
7010 * either a reinitialization expression or a loop body, warn about the
7011 * unreachable code. If the body is directly reachable via a code
7012 * label, we don't need this warning.
7013 */
7014 if (cond_expr_ != 0
7015 && cond_expr_->is_const()
7016 && !cond_expr_->get_const_val()->get_val_bool()
7017 && (reinit_expr_ != 0 || body_stm_ != 0)
7018 && (body_stm_ != 0 && !body_stm_->has_code_label())
7019 && !G_prs->get_syntax_only())
7020 {
7021 /* log a warning if desired */
7022 if (warn && !cond_expr_->get_const_val()->is_ctc())
7023 log_warning(TCERR_FOR_COND_FALSE);
7024
7025 /* this will simply continue to the next statement */
7026 return TCPRS_FLOW_NEXT;
7027 }
7028
7029 /* check for a body */
7030 if (body_stm_ != 0)
7031 {
7032 /* determine how our body can exit */
7033 flags = body_stm_->get_control_flow(warn);
7034 }
7035 else
7036 {
7037 /*
7038 * We have no body, so it's entirely up to the loop - we'll
7039 * evaluate those parameters below, but for now clear all flags
7040 */
7041 flags = 0;
7042 }
7043
7044 /*
7045 * ignore any 'next' flag that comes out of the body, because 'next'
7046 * from the last statement in the body actually takes us back to the
7047 * top of the loop
7048 */
7049 flags &= ~TCPRS_FLOW_NEXT;
7050
7051 /*
7052 * if there's a 'break' in the body, we can continue to the next
7053 * statement, since a break in the body takes us to the statement
7054 * after the 'for'
7055 */
7056 if ((flags & TCPRS_FLOW_BREAK) != 0)
7057 flags |= TCPRS_FLOW_NEXT;
7058
7059 /*
7060 * If we have a condition, and either the condition is a constant
7061 * false value or has a non-constant value, assume that the
7062 * condition can possibly become false and hence that the loop can
7063 * exit, and hence that the next statement is reachable.
7064 */
7065 if (cond_expr_ != 0
7066 && (!cond_expr_->is_const()
7067 || (cond_expr_->is_const()
7068 && !cond_expr_->get_const_val()->get_val_bool())))
7069 flags |= TCPRS_FLOW_NEXT;
7070
7071 /*
7072 * If we have any "in" clauses, we implicitly have a condition that
7073 * allows the loop to terminate.
7074 */
7075 if (in_exprs_ != 0)
7076 flags |= TCPRS_FLOW_NEXT;
7077
7078 /*
7079 * clear the 'break' and 'continue' flags, since we capture them in
7080 * our own scope - if our child breaks or continues, it won't affect
7081 * the "for" loop's container
7082 */
7083 flags &= ~(TCPRS_FLOW_BREAK | TCPRS_FLOW_CONT);
7084
7085 /* return the flags */
7086 return flags;
7087 }
7088
7089
7090 /* ------------------------------------------------------------------------ */
7091 /*
7092 * "foreach" statement node
7093 */
7094
7095 /*
7096 * fold constants
7097 */
7098 CTcPrsNode *CTPNStmForeachBase::fold_constants(CTcPrsSymtab *symtab)
7099 {
7100 /* set our location for any errors that occur */
7101 G_tok->set_line_info(get_source_desc(), get_source_linenum());
7102
7103 /*
7104 * fold constants in each of our expressions, replacing the
7105 * expressions with the folded versions
7106 */
7107 if (iter_expr_ != 0)
7108 iter_expr_ = iter_expr_->fold_constants(symtab);
7109 if (coll_expr_ != 0)
7110 coll_expr_ = coll_expr_->fold_constants(symtab);
7111
7112 /* fold constants in the body statement */
7113 if (body_stm_ != 0)
7114 body_stm_ = (CTPNStm *)body_stm_->fold_constants(symtab);
7115
7116 /* we are not directly changed by this operation */
7117 return this;
7118 }
7119
7120 /*
7121 * Chart the control flow through the loop
7122 */
7123 unsigned long CTPNStmForeachBase::get_control_flow(int warn) const
7124 {
7125 unsigned long flags;
7126
7127 /* check for a body */
7128 if (body_stm_ != 0)
7129 {
7130 /* determine how our body can exit */
7131 flags = body_stm_->get_control_flow(warn);
7132
7133 /*
7134 * add in the 'next' flag, because collection iterations always
7135 * terminate
7136 */
7137 flags |= TCPRS_FLOW_NEXT;
7138
7139 /*
7140 * clear the 'break' and 'continue' flags, since we capture them
7141 * in our own scope - if our child breaks or continues, it won't
7142 * affect the container of the 'foreach' itself
7143 */
7144 flags &= ~(TCPRS_FLOW_BREAK | TCPRS_FLOW_CONT);
7145 }
7146 else
7147 {
7148 /*
7149 * we have no body, so it's entirely up to the loop, which
7150 * simply iterates over the list and goes to the next statement
7151 */
7152 flags = TCPRS_FLOW_NEXT;
7153 }
7154
7155 /* return the flags */
7156 return flags;
7157 }
7158
7159 /* ------------------------------------------------------------------------ */
7160 /*
7161 * "while" statement node
7162 */
7163
7164 /*
7165 * fold constants
7166 */
7167 CTcPrsNode *CTPNStmWhileBase::fold_constants(CTcPrsSymtab *symtab)
7168 {
7169 /* set our location for any errors that occur */
7170 G_tok->set_line_info(get_source_desc(), get_source_linenum());
7171
7172 /*
7173 * fold constants in our condition expression, replacing the
7174 * expression with the folded version
7175 */
7176 if (cond_expr_ != 0)
7177 cond_expr_ = cond_expr_->fold_constants(symtab);
7178
7179 /* fold constants in the body statement */
7180 if (body_stm_ != 0)
7181 body_stm_ = (CTPNStm *)body_stm_->fold_constants(symtab);
7182
7183 /* we are not directly changed by this operation */
7184 return this;
7185 }
7186
7187 /*
7188 * Chart the control flow through the statement
7189 */
7190 unsigned long CTPNStmWhileBase::get_control_flow(int warn) const
7191 {
7192 unsigned long flags;
7193
7194 /*
7195 * If we have a condition with a constant false value, and we have a
7196 * non-empty body, and the body isn't reachable via a label, warn
7197 * about the unreachable code
7198 */
7199 if (cond_expr_->is_const()
7200 && !cond_expr_->get_const_val()->get_val_bool()
7201 && body_stm_ != 0
7202 && !body_stm_->has_code_label()
7203 && G_prs->get_syntax_only())
7204 {
7205 /* log a warning if desired */
7206 if (warn && !cond_expr_->get_const_val()->is_ctc())
7207 log_warning(TCERR_WHILE_COND_FALSE);
7208
7209 /* this will simply continue to the next statement */
7210 return TCPRS_FLOW_NEXT;
7211 }
7212
7213 /* check for a body */
7214 if (body_stm_ != 0)
7215 {
7216 /* determine how our body can exit */
7217 flags = body_stm_->get_control_flow(warn);
7218 }
7219 else
7220 {
7221 /*
7222 * We have no body, so it's entirely up to the loop - we'll
7223 * evaluate those parameters below, but for now clear all flags
7224 */
7225 flags = 0;
7226 }
7227
7228 /*
7229 * ignore any 'next' flag that comes out of the body, because 'next'
7230 * from the last statement in the body actually takes us back to the
7231 * top of the loop
7232 */
7233 flags &= ~TCPRS_FLOW_NEXT;
7234
7235 /*
7236 * if there's a 'break' in the body, we can continue to the next
7237 * statement, since a break in the body takes us to the statement
7238 * after the 'for'
7239 */
7240 if ((flags & TCPRS_FLOW_BREAK) != 0)
7241 flags |= TCPRS_FLOW_NEXT;
7242
7243 /*
7244 * if the condition is a constant false value, or has a non-constant
7245 * value, assume that the condition can possibly become false and
7246 * hence that the loop can exit, and hence that the next statement
7247 * is reachable
7248 */
7249 if (!cond_expr_->is_const()
7250 || (cond_expr_->is_const()
7251 && !cond_expr_->get_const_val()->get_val_bool()))
7252 flags |= TCPRS_FLOW_NEXT;
7253
7254 /*
7255 * clear the 'break' and 'continue' flags, since we capture them in
7256 * our own scope - if our child breaks or continues, it won't affect
7257 * the loop's container
7258 */
7259 flags &= ~(TCPRS_FLOW_BREAK | TCPRS_FLOW_CONT);
7260
7261 /* return the flags */
7262 return flags;
7263 }
7264
7265 /* ------------------------------------------------------------------------ */
7266 /*
7267 * "do-while" statement node
7268 */
7269
7270 /*
7271 * fold constants
7272 */
7273 CTcPrsNode *CTPNStmDoWhileBase::fold_constants(CTcPrsSymtab *symtab)
7274 {
7275 /* set our location for any errors that occur */
7276 G_tok->set_line_info(get_source_desc(), get_source_linenum());
7277
7278 /*
7279 * fold constants in our condition expression, replacing the
7280 * expression with the folded version
7281 */
7282 if (cond_expr_ != 0)
7283 cond_expr_ = cond_expr_->fold_constants(symtab);
7284
7285 /* fold constants in the body statement */
7286 if (body_stm_ != 0)
7287 body_stm_ = (CTPNStm *)body_stm_->fold_constants(symtab);
7288
7289 /* we are not directly changed by this operation */
7290 return this;
7291 }
7292
7293 /*
7294 * Chart the control flow through the statement
7295 */
7296 unsigned long CTPNStmDoWhileBase::get_control_flow(int warn) const
7297 {
7298 unsigned long flags;
7299
7300 /* check for a body */
7301 if (body_stm_ != 0)
7302 {
7303 /* determine how our body can exit */
7304 flags = body_stm_->get_control_flow(warn);
7305 }
7306 else
7307 {
7308 /*
7309 * We have no body, so it's entirely up to the loop - we'll
7310 * evaluate those parameters below, but for now clear all flags
7311 */
7312 flags = 0;
7313 }
7314
7315 /*
7316 * If we have a 'next' flag, it means that control can flow out from
7317 * the end of the last statement in the loop. That in turn means that
7318 * we can reach the code where we evaluate the condition. So, in order
7319 * for flow to proceed out of the loop, we have to be able to reach the
7320 * condition, and then the condition has to be capable of being false
7321 * (because if it's always true, we'll simply go back to the start of
7322 * the loop every time we hit the condition).
7323 */
7324 if ((flags & TCPRS_FLOW_NEXT) != 0)
7325 {
7326 /*
7327 * The 'next' flag up to this point only means that control can
7328 * reach the condition at the bottom of the loop. Since we want to
7329 * tell our caller whether or not control can flow *out* of the
7330 * loop, this is now irrelevant, so clear the 'next' flag for now.
7331 */
7332 flags &= ~TCPRS_FLOW_NEXT;
7333
7334 /*
7335 * Okay, we now know we can reach the condition at the bottom of
7336 * the loop. If the condition is a constant false value, we know
7337 * that we can flow out of the loop, since we know we'll hit the
7338 * condition, find that it's false, and exit the loop. Likewise,
7339 * if the condition hasa non-constant value, assume that it can
7340 * possibly become false and hence that the loop can exit, and
7341 * hence that the next statement is reachable.
7342 */
7343 if (!cond_expr_->is_const()
7344 || (cond_expr_->is_const()
7345 && !cond_expr_->get_const_val()->get_val_bool()))
7346 flags |= TCPRS_FLOW_NEXT;
7347 }
7348
7349 /*
7350 * if there's a 'break' in the body, we can continue to the next
7351 * statement, since a break in the body takes us to the statement
7352 * after the 'while'
7353 */
7354 if ((flags & TCPRS_FLOW_BREAK) != 0)
7355 flags |= TCPRS_FLOW_NEXT;
7356
7357 /*
7358 * clear the 'break' and 'continue' flags, since we capture them in
7359 * our own scope - if our child breaks or continues, it won't affect
7360 * the loop's container
7361 */
7362 flags &= ~(TCPRS_FLOW_BREAK | TCPRS_FLOW_CONT);
7363
7364 /* return the flags */
7365 return flags;
7366 }
7367
7368
7369 /* ------------------------------------------------------------------------ */
7370 /*
7371 * "switch" statement node
7372 */
7373
7374 /*
7375 * fold constants
7376 */
7377 CTcPrsNode *CTPNStmSwitchBase::fold_constants(CTcPrsSymtab *symtab)
7378 {
7379 /* set our location for any errors that occur */
7380 G_tok->set_line_info(get_source_desc(), get_source_linenum());
7381
7382 /* fold constants in our expression and body */
7383 expr_ = expr_->fold_constants(symtab);
7384 if (body_ != 0)
7385 body_ = (CTPNStm *)body_->fold_constants(symtab);
7386
7387 /* we're unchanged by constant folding */
7388 return this;
7389 }
7390
7391 /*
7392 * Chart the control flow through the statement
7393 */
7394 unsigned long CTPNStmSwitchBase::get_control_flow(int warn) const
7395 {
7396 unsigned long flags;
7397
7398 /*
7399 * Control flow through a switch is controlled by control flow
7400 * through the compound statement. If the compound statement can
7401 * break, then we flow to the next statement. If the compound
7402 * statement can reach the next statement, then so can we.
7403 */
7404 flags = (body_ != 0 ? body_->get_control_flow(warn) : TCPRS_FLOW_NEXT);
7405
7406 /*
7407 * check for a reachable break in the body - a break means the next
7408 * statement is reachable
7409 */
7410 if ((flags & TCPRS_FLOW_BREAK) != 0)
7411 {
7412 /*
7413 * break takes us to the next statement, and doesn't propagate
7414 * up to a break in any enclosing statement
7415 */
7416 flags |= TCPRS_FLOW_NEXT;
7417 flags &= ~TCPRS_FLOW_BREAK;
7418 }
7419
7420 /*
7421 * If the switch has no 'default' case, it is almost certain that we
7422 * can reach the next statement after the switch, because we can
7423 * probably assume that not every case is accounted for and hence we
7424 * have the equivalent of a null 'default' case.
7425 *
7426 * Note that we can't know for sure that every case hasn't been
7427 * accounted for, because we do not have datatype information for the
7428 * controlling expression; even if we did know the controlling
7429 * expression's datatype, it is impossible to enumerate every possible
7430 * value for expressions of certain datatypes (such as lists and
7431 * strings, which have effectively infinite ranges). However, it is a
7432 * reasonable assumption that not every possible case has been
7433 * accounted for,
7434 */
7435 if (!get_has_default())
7436 flags |= TCPRS_FLOW_NEXT;
7437
7438 /* return the flags */
7439 return flags;
7440 }
7441
7442 /* ------------------------------------------------------------------------ */
7443 /*
7444 * code label statement node
7445 */
7446
7447 /*
7448 * fold constants
7449 */
7450 CTcPrsNode *CTPNStmLabelBase::fold_constants(CTcPrsSymtab *symtab)
7451 {
7452 /* set our location for any errors that occur */
7453 G_tok->set_line_info(get_source_desc(), get_source_linenum());
7454
7455 /* fold constants in our labeled statement */
7456 if (stm_ != 0)
7457 stm_ = (CTPNStm *)stm_->fold_constants(symtab);
7458
7459 /* we're unchanged by constant folding */
7460 return this;
7461 }
7462
7463 /*
7464 * Chart the control flow through the statement
7465 */
7466 unsigned long CTPNStmLabelBase::get_control_flow(int warn) const
7467 {
7468 ulong flags;
7469
7470 /*
7471 * if we have an underlying statement, control flow is the same as
7472 * the statement's control flow; if we don't have a statement (i.e.,
7473 * our labeled statement is an empty statement), we flow to the next
7474 * statement
7475 */
7476 flags = (stm_ != 0 ? stm_->get_control_flow(warn) : TCPRS_FLOW_NEXT);
7477
7478 /*
7479 * if we have an explicit 'break' flag in our control flow flags, it
7480 * means that we were targeted specifically with a 'break' statement
7481 * contained within our body - this transfers control to the next
7482 * statement after me, therefore my next statement is reachable in
7483 * this case
7484 */
7485 if ((control_flow_flags_ & TCPRS_FLOW_BREAK) != 0)
7486 flags |= TCPRS_FLOW_NEXT;
7487
7488 /* return the result */
7489 return flags;
7490 }
7491
7492 /*
7493 * add control flow flags
7494 */
7495 void CTPNStmLabelBase::add_control_flow_flags(ulong flags)
7496 {
7497 /* add the flags to any we already have */
7498 control_flow_flags_ |= flags;
7499 }
7500
7501 /* ------------------------------------------------------------------------ */
7502 /*
7503 * 'goto' statement node
7504 */
7505
7506 /*
7507 * Chart the control flow through the statement
7508 */
7509 unsigned long CTPNStmGotoBase::get_control_flow(int warn) const
7510 {
7511 CTcSymbol *sym;
7512
7513 /* find our target statement */
7514 if (G_prs->get_goto_symtab() != 0
7515 && (sym = G_prs->get_goto_symtab()->find(lbl_, lbl_len_)) != 0
7516 && sym->get_type() == TC_SYM_LABEL)
7517 {
7518 /*
7519 * we found our label - mark its statement as having a 'goto'
7520 * targeted at it
7521 */
7522 ((CTcSymLabel *)sym)->add_control_flow_flags(TCPRS_FLOW_GOTO);
7523 }
7524
7525 /* indicate that we jump somewhere */
7526 return TCPRS_FLOW_GOTO;
7527 }
7528
7529 /* ------------------------------------------------------------------------ */
7530 /*
7531 * 'case' label statement node
7532 */
7533
7534 /*
7535 * fold constants
7536 */
7537 CTcPrsNode *CTPNStmCaseBase::fold_constants(CTcPrsSymtab *symtab)
7538 {
7539 /* set our location for any errors that occur */
7540 G_tok->set_line_info(get_source_desc(), get_source_linenum());
7541
7542 /* fold constants in our expression */
7543 expr_ = expr_->fold_constants(symtab);
7544
7545 /* if the expression isn't a constant, it's an error */
7546 if (!expr_->is_const())
7547 log_error(TCERR_CASE_NOT_CONSTANT);
7548
7549 /* fold constants in our labeled statement */
7550 if (stm_ != 0)
7551 stm_ = (CTPNStm *)stm_->fold_constants(symtab);
7552
7553 /* we're unchanged by constant folding */
7554 return this;
7555 }
7556
7557 /*
7558 * Chart the control flow through the statement
7559 */
7560 unsigned long CTPNStmCaseBase::get_control_flow(int warn) const
7561 {
7562 /*
7563 * if we have an underlying statement, control flow is the same as
7564 * the statement's control flow; if we don't have a statement (i.e.,
7565 * our labeled statement is an empty statement), we flow to the next
7566 * statement
7567 */
7568 return (stm_ != 0 ? stm_->get_control_flow(warn) : TCPRS_FLOW_NEXT);
7569 }
7570
7571 /* ------------------------------------------------------------------------ */
7572 /*
7573 * 'default' label statement node
7574 */
7575
7576 /*
7577 * fold constants
7578 */
7579 CTcPrsNode *CTPNStmDefaultBase::fold_constants(CTcPrsSymtab *symtab)
7580 {
7581 /* set our location for any errors that occur */
7582 G_tok->set_line_info(get_source_desc(), get_source_linenum());
7583
7584 /* fold constants in our labeled statement */
7585 if (stm_ != 0)
7586 stm_ = (CTPNStm *)stm_->fold_constants(symtab);
7587
7588 /* we're unchanged by constant folding */
7589 return this;
7590 }
7591
7592 /*
7593 * Chart the control flow through the statement
7594 */
7595 unsigned long CTPNStmDefaultBase::get_control_flow(int warn) const
7596 {
7597 /*
7598 * if we have an underlying statement, control flow is the same as
7599 * the statement's control flow; if we don't have a statement (i.e.,
7600 * our labeled statement is an empty statement), we flow to the next
7601 * statement
7602 */
7603 return (stm_ != 0 ? stm_->get_control_flow(warn) : TCPRS_FLOW_NEXT);
7604 }
7605
7606 /* ------------------------------------------------------------------------ */
7607 /*
7608 * 'try' statement
7609 */
7610
7611 /*
7612 * add a 'catch' clause
7613 */
7614 void CTPNStmTryBase::add_catch(CTPNStmCatch *catch_stm)
7615 {
7616 /* link it in at the end of our list */
7617 if (last_catch_stm_ != 0)
7618 last_catch_stm_->set_next_catch(catch_stm);
7619 else
7620 first_catch_stm_ = catch_stm;
7621
7622 /* it's now the last catch statement */
7623 last_catch_stm_ = catch_stm;
7624 catch_stm->set_next_catch(0);
7625
7626 /* it's the last statement in its group */
7627 catch_stm->set_next_stm(0);
7628 }
7629
7630 /*
7631 * fold constants
7632 */
7633 CTcPrsNode *CTPNStmTryBase::fold_constants(CTcPrsSymtab *symtab)
7634 {
7635 CTPNStmCatch *cur_catch;
7636
7637 /* fold constants in our protected code */
7638 if (body_stm_ != 0)
7639 body_stm_->fold_constants(symtab);
7640
7641 /* fold constants in our catch list */
7642 for (cur_catch = first_catch_stm_ ; cur_catch != 0 ;
7643 cur_catch = cur_catch->get_next_catch())
7644 {
7645 /* fold constants; assume this will not change the statement */
7646 cur_catch->fold_constants(symtab);
7647 }
7648
7649 /* fold constants in our 'finally' clause */
7650 if (finally_stm_ != 0)
7651 finally_stm_->fold_constants(symtab);
7652
7653 /* we're unchanged by constant folding */
7654 return this;
7655 }
7656
7657 /*
7658 * adjust for dynamic (run-time) compilation
7659 */
7660 CTcPrsNode *CTPNStmTryBase::adjust_for_dyn(const tcpn_dyncomp_info *info)
7661 {
7662 /* adjust our body statement */
7663 if (body_stm_ != 0)
7664 body_stm_ = (CTPNStm *)body_stm_->adjust_for_dyn(info);
7665
7666 /* adjust each item in our catch list */
7667 for (CTPNStmCatch *cur_catch = first_catch_stm_ ; cur_catch != 0 ;
7668 cur_catch = cur_catch->get_next_catch())
7669 {
7670 /* adjust this item; assume it won't replace the node */
7671 cur_catch->adjust_for_dyn(info);
7672 }
7673
7674 /* adjust our 'finally' clause */
7675 if (finally_stm_ != 0)
7676 finally_stm_ = (CTPNStmFinally *)finally_stm_->adjust_for_dyn(info);
7677
7678 /* we're unchanged by constant folding */
7679 return this;
7680 }
7681
7682
7683 /*
7684 * Chart the control flow through the statement
7685 */
7686 unsigned long CTPNStmTryBase::get_control_flow(int warn) const
7687 {
7688 unsigned long flags;
7689 unsigned long fin_flags;
7690 CTPNStmCatch *cur_catch;
7691
7692 /*
7693 * First, determine the control flow through the protected code. If
7694 * it exits, we either exit or proceed to the 'finally' clause.
7695 */
7696 flags = (body_stm_ != 0
7697 ? body_stm_->get_control_flow(warn) : TCPRS_FLOW_NEXT);
7698
7699 /*
7700 * Next, figure out what the various 'catch' clauses can do. We
7701 * assume that each 'catch' clause is enterable, since any exception
7702 * could occur within the protected code (whether it explicitly
7703 * throws or not - it could call a function or method that throws).
7704 * So, we can simply OR together with the main block's flags all of
7705 * the ways the different 'catch' clauses can exit.
7706 */
7707 for (cur_catch = first_catch_stm_ ; cur_catch != 0 ;
7708 cur_catch = cur_catch->get_next_catch())
7709 {
7710 /* OR in the flags for this 'catch' block */
7711 flags |= cur_catch->get_control_flow(warn);
7712 }
7713
7714 /*
7715 * If we have a 'finally' clause, it is definitely enterable, since
7716 * every other way of leaving the protected code goes through it.
7717 * Find how it can exit.
7718 */
7719 fin_flags = (finally_stm_ != 0
7720 ? finally_stm_->get_control_flow(warn)
7721 : TCPRS_FLOW_NEXT);
7722
7723 /*
7724 * If the 'finally' block can't flow to next, then neither can the
7725 * main/catch blocks. In addition, the main/catch blocks can't
7726 * 'break', 'continue', 'goto', or 'return' either, because the
7727 * 'finally' block will not allow the other blocks to proceed with
7728 * whatever transfers they attempt. So, if 'next' isn't set in the
7729 * 'finally' flags, clear out all of these flags from the main/catch
7730 * flags.
7731 */
7732 if (!(fin_flags & TCPRS_FLOW_NEXT))
7733 flags &= ~(TCPRS_FLOW_NEXT
7734 | TCPRS_FLOW_BREAK
7735 | TCPRS_FLOW_CONT
7736 | TCPRS_FLOW_RET_VAL
7737 | TCPRS_FLOW_RET_VOID
7738 | TCPRS_FLOW_GOTO);
7739
7740 /*
7741 * Add in the remaining flags from the 'finally' block, since
7742 * whatever it does, the overall statement does. However, ignore
7743 * 'next' flags in the 'finally' block, because if the 'finally'
7744 * code goes to next, it just means that we keep doing whatever we
7745 * were doing in the main block.
7746 */
7747 fin_flags &= ~TCPRS_FLOW_NEXT;
7748 flags |= fin_flags;
7749
7750 /* return the result */
7751 return flags;
7752 }
7753
7754
7755 /* ------------------------------------------------------------------------ */
7756 /*
7757 * 'catch' clause
7758 */
7759
7760 /*
7761 * set our exception class name
7762 */
7763 void CTPNStmCatchBase::set_exc_class(const CTcToken *tok)
7764 {
7765 /*
7766 * remember the token's contents - the tokenizer keeps token symbol
7767 * strings in memory throughout the compilation, so we can simply
7768 * store a direct reference to the string
7769 */
7770 exc_class_ = tok->get_text();
7771 exc_class_len_ = tok->get_text_len();
7772 }
7773
7774 /*
7775 * fold constants
7776 */
7777 CTcPrsNode *CTPNStmCatchBase::fold_constants(CTcPrsSymtab *symtab)
7778 {
7779 /* fold constants in our body */
7780 if (body_ != 0)
7781 body_ = (CTPNStm *)body_->fold_constants(symtab);
7782
7783 /* we're unchanged by constant folding */
7784 return this;
7785 }
7786
7787 /*
7788 * Chart the control flow through the statement
7789 */
7790 unsigned long CTPNStmCatchBase::get_control_flow(int warn) const
7791 {
7792 /*
7793 * our control flow is that of the body
7794 */
7795 return (body_ != 0 ? body_->get_control_flow(warn) : TCPRS_FLOW_NEXT);
7796 }
7797
7798
7799
7800 /* ------------------------------------------------------------------------ */
7801 /*
7802 * 'finally' clause
7803 */
7804
7805 /*
7806 * fold constants
7807 */
7808 CTcPrsNode *CTPNStmFinallyBase::fold_constants(CTcPrsSymtab *symtab)
7809 {
7810 /* fold constants in our body */
7811 if (body_ != 0)
7812 body_ = (CTPNStm *)body_->fold_constants(symtab);
7813
7814 /* we're unchanged by constant folding */
7815 return this;
7816 }
7817
7818 /*
7819 * Chart the control flow through the statement
7820 */
7821 unsigned long CTPNStmFinallyBase::get_control_flow(int warn) const
7822 {
7823 /* our control flow is that of the body */
7824 return (body_ != 0 ? body_->get_control_flow(warn) : TCPRS_FLOW_NEXT);
7825 }
7826
7827
7828
7829 /* ------------------------------------------------------------------------ */
7830 /*
7831 * 'throw' statement
7832 */
7833
7834 /*
7835 * fold constants
7836 */
7837 CTcPrsNode *CTPNStmThrowBase::fold_constants(CTcPrsSymtab *symtab)
7838 {
7839 /* set our location for any errors that occur */
7840 G_tok->set_line_info(get_source_desc(), get_source_linenum());
7841
7842 /* fold constants in our expression */
7843 expr_ = expr_->fold_constants(symtab);
7844
7845 /* we're unchanged by constant folding */
7846 return this;
7847 }
7848
7849 /* ------------------------------------------------------------------------ */
7850 /*
7851 * 'break' statement
7852 */
7853
7854 /*
7855 * set the label
7856 */
7857 void CTPNStmBreakBase::set_label(const CTcToken *tok)
7858 {
7859 /*
7860 * token text is kept in memory throughout compilation, so we can
7861 * simply store a reference to the text without copying it
7862 */
7863 lbl_ = tok->get_text();
7864 lbl_len_ = tok->get_text_len();
7865 }
7866
7867 /*
7868 * chart control flow
7869 */
7870 ulong CTPNStmBreakBase::get_control_flow(int warn) const
7871 {
7872 /*
7873 * If we don't have a label, we break out of our enclosing control
7874 * structure. If we have a label, we act like a 'goto' statement.
7875 */
7876 if (lbl_ == 0)
7877 {
7878 /* no label - we simply break out of the enclosing loop or switch */
7879 return TCPRS_FLOW_BREAK;
7880 }
7881 else
7882 {
7883 CTcSymbol *sym;
7884
7885 /*
7886 * We have a label - we act like a 'goto' statement. Find our
7887 * target statement and mark it as having a targeted 'break'.
7888 */
7889 if (G_prs->get_goto_symtab() != 0
7890 && (sym = G_prs->get_goto_symtab()->find(lbl_, lbl_len_)) != 0
7891 && sym->get_type() == TC_SYM_LABEL)
7892 {
7893 /*
7894 * we found our label - mark its statement as having a
7895 * 'break' targeted at it
7896 */
7897 ((CTcSymLabel *)sym)->add_control_flow_flags(TCPRS_FLOW_BREAK);
7898 }
7899
7900 /* indicate that we jump somewhere else */
7901 return TCPRS_FLOW_GOTO;
7902 }
7903 }
7904
7905
7906 /* ------------------------------------------------------------------------ */
7907 /*
7908 * 'continue' statement
7909 */
7910
7911 /*
7912 * set the label
7913 */
7914 void CTPNStmContinueBase::set_label(const CTcToken *tok)
7915 {
7916 /*
7917 * token text is kept in memory throughout compilation, so we can
7918 * simply store a reference to the text without copying it
7919 */
7920 lbl_ = tok->get_text();
7921 lbl_len_ = tok->get_text_len();
7922 }
7923
7924 /*
7925 * chart control flow
7926 */
7927 ulong CTPNStmContinueBase::get_control_flow(int warn) const
7928 {
7929 /*
7930 * If we don't have a label, we continue in our enclosing control
7931 * structure. If we have a label, we act like a 'goto' statement.
7932 */
7933 if (lbl_ == 0)
7934 {
7935 /* no label - we simply continue with the enclosing loop */
7936 return TCPRS_FLOW_CONT;
7937 }
7938 else
7939 {
7940 CTcSymbol *sym;
7941
7942 /*
7943 * We have a label - we act like a 'goto' statement. Find our
7944 * target statement and mark it as having a targeted 'continue'.
7945 */
7946 if (G_prs->get_goto_symtab() != 0
7947 && (sym = G_prs->get_goto_symtab()->find(lbl_, lbl_len_)) != 0
7948 && sym->get_type() == TC_SYM_LABEL)
7949 {
7950 /*
7951 * we found our label - mark its statement as having a
7952 * 'continue' targeted at it
7953 */
7954 ((CTcSymLabel *)sym)->add_control_flow_flags(TCPRS_FLOW_CONT);
7955 }
7956
7957 /* indicate that we jump somewhere else */
7958 return TCPRS_FLOW_GOTO;
7959 }
7960 }
7961
7962 /* ------------------------------------------------------------------------ */
7963 /*
7964 * code ('goto') label base
7965 */
7966
7967 /*
7968 * Add control flow flags to my statement
7969 */
7970 void CTcSymLabelBase::add_control_flow_flags(ulong flags)
7971 {
7972 if (stm_ != 0)
7973 stm_->add_control_flow_flags(flags);
7974 }
7975
7976
7977 /* ------------------------------------------------------------------------ */
7978 /*
7979 * Grammar tree node subclasses
7980 */
7981
7982 /*
7983 * Grammar tree node - base class for nodes with children (OR, cat)
7984 */
7985 class CTcPrsGramNodeWithChildren: public CTcPrsGramNode
7986 {
7987 public:
7988 CTcPrsGramNodeWithChildren()
7989 {
7990 /* no subitems yet */
7991 sub_head_ = sub_tail_ = 0;
7992 }
7993
7994 /* add a sub-item */
7995 void add_sub_item(CTcPrsGramNode *sub)
7996 {
7997 /* add it to the end of the list */
7998 if (sub_tail_ != 0)
7999 sub_tail_->next_ = sub;
8000 else
8001 sub_head_ = sub;
8002
8003 /* it's now the last item */
8004 sub_tail_ = sub;
8005 sub->next_ = 0;
8006 }
8007
8008 virtual void flatten_cat()
8009 {
8010 CTcPrsGramNode *cur;
8011
8012 /* flatten CAT nodes in our subnodes */
8013 for (cur = sub_head_ ; cur != 0 ; cur = cur->next_)
8014 cur->flatten_cat();
8015 }
8016
8017 /* initialize expansion */
8018 virtual void init_expansion()
8019 {
8020 CTcPrsGramNode *cur;
8021
8022 /* initialize subnodes */
8023 for (cur = sub_head_ ; cur != 0 ; cur = cur->next_)
8024 cur->init_expansion();
8025 }
8026
8027 /* head and tail of list of subnodes */
8028 CTcPrsGramNode *sub_head_;
8029 CTcPrsGramNode *sub_tail_;
8030 };
8031
8032 /*
8033 * Grammar tree node - CAT node. This represents a string of
8034 * concatenated tokens.
8035 */
8036 class CTcPrsGramNodeCat: public CTcPrsGramNodeWithChildren
8037 {
8038 public:
8039 /* I'm a CAT node */
8040 virtual int is_cat() { return TRUE; }
8041
8042 /*
8043 * Consolidate all of the OR's at the top of the tree. If we have any
8044 * OR subnodes, we'll rewrite ourselves as an OR of the concatenations
8045 * of all of the sub-OR's in all combinations.
8046 */
8047 virtual CTcPrsGramNode *consolidate_or();
8048
8049 /*
8050 * Flatten CAT nodes together. If we have any CAT nodes below us,
8051 * move their contents into our own list directly.
8052 */
8053 virtual void flatten_cat()
8054 {
8055 CTcPrsGramNode *prv;
8056 CTcPrsGramNode *cur;
8057 CTcPrsGramNode *nxt;
8058
8059 /*
8060 * Check each child. For each child that's a CAT node, move its
8061 * contents directly into our list, removing the redundant
8062 * intervening CAT node.
8063 */
8064 for (prv = 0, cur = sub_head_ ; cur != 0 ; cur = nxt)
8065 {
8066 /* note the next item, in case we fiddle with the list */
8067 nxt = cur->next_;
8068
8069 /* flatten the CAT items below this one */
8070 cur->flatten_cat();
8071
8072 /* if this item is a CAT, move its contents directly into us */
8073 if (cur->is_cat())
8074 {
8075 CTcPrsGramNodeCat *cur_cat;
8076
8077 /* cast it to a CAT node */
8078 cur_cat = (CTcPrsGramNodeCat *)cur;
8079
8080 /* if it's empty, just remove it; otherwise, link it in */
8081 if (cur_cat->sub_head_ != 0)
8082 {
8083 /* link from the previous item to the sublist head */
8084 if (prv == 0)
8085 {
8086 /*
8087 * this is the first item - put it at the start of
8088 * our list
8089 */
8090 sub_head_ = cur_cat->sub_head_;
8091 }
8092 else
8093 {
8094 /* link it after the previous item */
8095 prv->next_ = cur_cat->sub_head_;
8096 }
8097
8098 /* link from the tail of the sublist to the next item */
8099 cur_cat->sub_tail_->next_ = nxt;
8100
8101 /* if it's the last item, set our tail pointer */
8102 if (sub_tail_ == cur)
8103 sub_tail_ = cur_cat->sub_tail_;
8104
8105 /*
8106 * the tail of the sublist is now the previous item in
8107 * the list for the purposes of the next iteration
8108 */
8109 prv = cur_cat->sub_tail_;
8110 }
8111 else
8112 {
8113 /* unlink it from our list */
8114 if (prv != 0)
8115 prv->next_ = nxt;
8116 else
8117 sub_head_ = nxt;
8118
8119 /* update the tail pointer if removing the last item */
8120 if (sub_tail_ == cur_cat)
8121 sub_tail_ = prv;
8122
8123 /* note that the previous item is unchanged */
8124 }
8125 }
8126 else
8127 {
8128 /*
8129 * it's not a CAT, so we're not changing it; this is the
8130 * previous item for the next iteration
8131 */
8132 prv = cur;
8133 }
8134 }
8135 }
8136
8137 /* clone the current expansion subtree */
8138 virtual CTcPrsGramNode *clone_expansion() const
8139 {
8140 /* create a new 'cat' node */
8141 CTcPrsGramNodeCat *new_cat = new (G_prsmem) CTcPrsGramNodeCat();
8142
8143 /* add a clone of each of my children and add it to my replica */
8144 for (CTcPrsGramNode *cur = sub_head_ ; cur != 0 ; cur = cur->next_)
8145 new_cat->add_sub_item(cur->clone_expansion());
8146
8147 /* return my replica */
8148 return new_cat;
8149 }
8150
8151 /* clone this node (without any children) */
8152 virtual CTcPrsGramNodeWithChildren *clone_this_node()
8153 {
8154 return new (G_prsmem) CTcPrsGramNodeCat();
8155 }
8156
8157 /* initialize expansion */
8158 virtual void init_expansion()
8159 {
8160 /* set to expand from our first child */
8161 cur_sub_ = sub_head_;
8162
8163 /* inherit default to initialize subnodes */
8164 CTcPrsGramNodeWithChildren::init_expansion();
8165 }
8166
8167 /* get the next token in a token expansion */
8168 const CTcToken *get_next_token()
8169 {
8170 /* advance to the next subitem */
8171 cur_sub_ = cur_sub_->next_;
8172
8173 /*
8174 * If there's anything left, return it; otherwise, return null.
8175 * During final token expansion, a 'cat' node never has anything
8176 * but tokens beneath it, since we move all the 'or' nodes to a
8177 * single 'or' at the top of the tree and flatten out the 'cat'
8178 * nodes to a single level under that; so, we can simply return
8179 * the token directly from the next underlying node.
8180 */
8181 return (cur_sub_ != 0 ? cur_sub_->get_tok() : 0);
8182 }
8183
8184 /*
8185 * get my current token - we'll just return the token from the current
8186 * subitem
8187 */
8188 const CTcToken *get_tok() const
8189 {
8190 return (cur_sub_ != 0 ? cur_sub_->get_tok() : 0);
8191 }
8192
8193 /* current subitem in expansion */
8194 CTcPrsGramNode *cur_sub_;
8195 };
8196
8197 /*
8198 * Grammar tree node - OR node. This represents a set of alternatives.
8199 */
8200 class CTcPrsGramNodeOr: public CTcPrsGramNodeWithChildren
8201 {
8202 public:
8203 /* I'm an "or" node */
8204 virtual int is_or() { return TRUE; }
8205
8206 /*
8207 * Consolidate all of the OR's at the top of the tree.
8208 */
8209 virtual CTcPrsGramNode *consolidate_or()
8210 {
8211 CTcPrsGramNode *cur;
8212 CTcPrsGramNode *nxt;
8213 CTcPrsGramNode *old_head;
8214
8215 /*
8216 * Consolidate OR's in all subtrees. If any of our immediate
8217 * children turn into OR's, simply pull their children into our OR
8218 * list directly - OR(a, OR(b, c)) is equivalent to OR(a, b, c).
8219 *
8220 * Before we build the new list, stash away our entire subtree,
8221 * since we'll rebuild the tree from the updated versions.
8222 */
8223 old_head = sub_head_;
8224 sub_head_ = sub_tail_ = 0;
8225
8226 /* run through our list and build the new, consolidated list */
8227 for (cur = old_head ; cur != 0 ; cur = nxt)
8228 {
8229 CTcPrsGramNode *new_sub;
8230
8231 /* remember the next item in the old list */
8232 nxt = cur->next_;
8233
8234 /* consolidate OR's in the subtree */
8235 new_sub = cur->consolidate_or();
8236
8237 /*
8238 * if this is an OR node, add its children directly to us;
8239 * otherwise, add the node itself
8240 */
8241 if (new_sub->is_or())
8242 {
8243 CTcPrsGramNodeOr *new_or;
8244 CTcPrsGramNode *chi;
8245 CTcPrsGramNode *next_chi;
8246
8247 /* cast it */
8248 new_or = (CTcPrsGramNodeOr *)new_sub;
8249
8250 /* add each of the sub-OR's children as our direct children */
8251 for (chi = new_or->sub_head_ ; chi != 0 ; chi = next_chi)
8252 {
8253 /* remember the next child in the old sub-list */
8254 next_chi = chi->next_;
8255
8256 /* move it directly to our list */
8257 add_sub_item(chi);
8258 }
8259
8260 /* the old OR item is now empty of children */
8261 new_or->sub_head_ = new_or->sub_tail_ = 0;
8262 }
8263 else
8264 {
8265 /* it's not an OR - add it directly to our list */
8266 add_sub_item(new_sub);
8267 }
8268 }
8269
8270 /* return myself with no further changes */
8271 return this;
8272 }
8273
8274 /* initialize expansion - set up at the first alternative */
8275 virtual void init_expansion()
8276 {
8277 /* start at the first alternative */
8278 cur_alt_ = sub_head_;
8279
8280 /* initialize the first alternative for expansion */
8281 cur_alt_->init_expansion();
8282
8283 /* we didn't just do an 'or' */
8284 just_did_or_ = FALSE;
8285
8286 /* we haven't yet returned the first token */
8287 before_first_ = TRUE;
8288 }
8289
8290 /* advance to the next alternative for expansion */
8291 virtual int advance_expansion()
8292 {
8293 /* advance to the next state */
8294 cur_alt_ = cur_alt_->next_;
8295
8296 /*
8297 * if that was the last state, wrap back to the first state and
8298 * indicate a 'carry'; otherwise, indicate no carry
8299 */
8300 if (cur_alt_ == 0)
8301 {
8302 /* we ran out of states - wrap back to the first one */
8303 cur_alt_ = sub_head_;
8304
8305 /* initialize expansion in the new subitem */
8306 cur_alt_->init_expansion();
8307
8308 /* indicate that we've wrapped and should carry forward */
8309 return TRUE;
8310 }
8311 else
8312 {
8313 /* initialize expansion in the new subitem */
8314 cur_alt_->init_expansion();
8315
8316 /* this is another valid state - no carry */
8317 return FALSE;
8318 }
8319 }
8320
8321 /* clone the current expansion subtree */
8322 virtual CTcPrsGramNode *clone_expansion() const
8323 {
8324 /* return a replica of the current alternative being expanded */
8325 return cur_alt_->clone_expansion();
8326 }
8327
8328 /* get the next token in a token expansion */
8329 const CTcToken *get_next_token()
8330 {
8331 const CTcToken *tok;
8332
8333 /*
8334 * If we returned the synthesized '|' token between alternatives
8335 * last time, advance to the next alternative and return its first
8336 * token.
8337 */
8338 if (just_did_or_)
8339 {
8340 /* we no longer just did an '|' */
8341 just_did_or_ = FALSE;
8342
8343 /* advance to the next alternative */
8344 cur_alt_ = cur_alt_->next_;
8345
8346 /* if there is no next alternative, there's nothing to return */
8347 if (cur_alt_ == 0)
8348 return 0;
8349
8350 /* initialize the new alternative */
8351 cur_alt_->init_expansion();
8352
8353 /* if this alternative has no subitems, return another '|' */
8354 if (cur_alt_->get_tok() == 0)
8355 {
8356 /* flag the '|' */
8357 just_did_or_ = TRUE;
8358
8359 /* if this was the last alternative, we're done */
8360 if (cur_alt_->next_ == 0)
8361 return 0;
8362
8363 /*
8364 * return the '|' again (we know it's already set up,
8365 * because we only get here if we did an '|' previously)
8366 */
8367 return &or_tok_;
8368 }
8369
8370 /* return its current token */
8371 return cur_alt_->get_tok();
8372 }
8373
8374 /*
8375 * Get the first or next token in the current alternative,
8376 * depending on where we are.
8377 */
8378 if (before_first_)
8379 {
8380 /* we're before the first token - get the current token */
8381 tok = cur_alt_->get_tok();
8382
8383 /* we're now past the first token */
8384 before_first_ = FALSE;
8385 }
8386 else
8387 {
8388 /* we're past the first token, so get the next one */
8389 tok = cur_alt_->get_next_token();
8390 }
8391
8392 /* if we got a token from the current alternative, return it */
8393 if (tok != 0)
8394 return tok;
8395
8396 /*
8397 * We've run out of tokens in this alternative - synthesize an OR
8398 * token ('|') and return it, so the caller will know a new
8399 * top-level alternative follows.
8400 *
8401 * Do NOT synthesize an OR token after our last alternative.
8402 */
8403 if (cur_alt_->next_ == 0)
8404 {
8405 /* this was our last alternative - we're done */
8406 return 0;
8407 }
8408
8409 /* we have another alternative - synthesize an OR */
8410 or_tok_.settyp(TOKT_OR);
8411 or_tok_.set_text("|", 1);
8412
8413 /* flag that we just did an '|' */
8414 just_did_or_ = TRUE;
8415
8416 /* return the synthesized '|' token */
8417 return &or_tok_;
8418 }
8419
8420 /* current alternative being expanded */
8421 CTcPrsGramNode *cur_alt_;
8422
8423 /*
8424 * my synthesized token for returning the '|' token between
8425 * alternatives during expansion
8426 */
8427 CTcToken or_tok_;
8428
8429 /* flag: we just returned the 'or' between two alternatives */
8430 unsigned int just_did_or_ : 1;
8431
8432 /* flag: we haven't yet returned the first token */
8433 unsigned int before_first_ : 1;
8434 };
8435
8436 /*
8437 * Consolidate all of the OR's at the top of the tree. If we have any OR
8438 * subnodes, we'll rewrite ourselves as an OR of the concatenations of all
8439 * of the sub-OR's in all combinations.
8440 */
8441 CTcPrsGramNode *CTcPrsGramNodeCat::consolidate_or()
8442 {
8443 CTcPrsGramNode *old_head;
8444 CTcPrsGramNodeOr *new_or;
8445 CTcPrsGramNode *cur;
8446 CTcPrsGramNode *nxt;
8447 int or_cnt;
8448
8449 /*
8450 * Consolidate OR's in all subtrees. Before we do, stash away our
8451 * entire subtree, since we'll rebuild the tree from the updated
8452 * versions.
8453 */
8454 old_head = sub_head_;
8455 sub_head_ = sub_tail_ = 0;
8456
8457 /* run through our old list and build the new, consolidated list */
8458 for (cur = old_head ; cur != 0 ; cur = nxt)
8459 {
8460 /* remember the next item in the old list */
8461 nxt = cur->next_;
8462
8463 /* consolidate the subtree and add it to our new list */
8464 add_sub_item(cur->consolidate_or());
8465 }
8466
8467 /*
8468 * Count of OR nodes - if we don't have any, we don't have to make any
8469 * changes. Since we've already consolidated all OR's out of
8470 * sub-nodes into a single node at the top of each subtree, we don't
8471 * have to worry about OR's below our immediate children.
8472 */
8473 for (or_cnt = 0, cur = sub_head_ ; cur != 0 ; cur = cur->next_)
8474 {
8475 /* if it's an OR node, count it */
8476 if (cur->is_or())
8477 ++or_cnt;
8478 }
8479
8480 /* if we have no OR nodes, we need no changes */
8481 if (or_cnt == 0)
8482 return this;
8483
8484 /* create a new OR node to contain the list of expansions */
8485 new_or = new (G_prsmem) CTcPrsGramNodeOr();
8486
8487 /* set up each OR node with the next alternative */
8488 for (cur = sub_head_ ; cur != 0 ; cur = cur->next_)
8489 cur->init_expansion();
8490
8491 /*
8492 * Iterate through all of the possibilities. For each iteration,
8493 * we'll build the currently selected alternative, then we'll advance
8494 * by one alternative. We'll keep doing this until we've advanced
8495 * through all of the alternatives.
8496 */
8497 for (;;)
8498 {
8499 CTcPrsGramNodeCat *new_cat;
8500
8501 /* create a new CAT node for the current selected alternative */
8502 new_cat = new (G_prsmem) CTcPrsGramNodeCat();
8503
8504 /* clone and add each current alternative to the new CAT node */
8505 for (cur = sub_head_ ; cur != 0 ; cur = cur->next_)
8506 new_cat->add_sub_item(cur->clone_expansion());
8507
8508 /*
8509 * we've finished this entire expansion, so add it to the new main
8510 * OR we're building
8511 */
8512 new_or->add_sub_item(new_cat);
8513
8514 /*
8515 * Go through the list of subitems and advance to the next OR
8516 * state. Stop when we reach the first item that advances without
8517 * a 'carry'. If we're still carrying when we reach the last
8518 * item, we know we've wrapped around back to the first
8519 * alternative and hence are done.
8520 */
8521 for (cur = sub_head_ ; cur != 0 ; cur = cur->next_)
8522 {
8523 /* advance this one; if it doesn't carry, we're done */
8524 if (!cur->advance_expansion())
8525 break;
8526 }
8527
8528 /*
8529 * if we carried past the last item, we've wrapped around back to
8530 * the first alternative, so we're done
8531 */
8532 if (cur == 0)
8533 break;
8534 }
8535
8536 /* return the new main OR we built */
8537 return new_or;
8538 }
8539
8540 /*
8541 * Grammar tree node - token node. This is a terminal node representing a
8542 * single token.
8543 */
8544 class CTcPrsGramNodeTok: public CTcPrsGramNode
8545 {
8546 public:
8547 CTcPrsGramNodeTok(const CTcToken *tok)
8548 {
8549 /* remember the token */
8550 G_tok->copytok(&tok_, tok);
8551 }
8552
8553 /* consolidate OR's */
8554 virtual CTcPrsGramNode *consolidate_or()
8555 {
8556 /* since I'm a terminal node, there's nothing to do here */
8557 return this;
8558 }
8559
8560 /* clone the current expansion subtree */
8561 virtual CTcPrsGramNode *clone_expansion() const
8562 {
8563 /* return a new token node with my same token value */
8564 return new (G_prsmem) CTcPrsGramNodeTok(&tok_);
8565 }
8566
8567 /* get my token */
8568 virtual const CTcToken *get_tok() const { return &tok_; }
8569
8570 /* my token */
8571 CTcToken tok_;
8572 };
8573
8574
8575 /*
8576 * Parse a 'grammar' statement's alternative list.
8577 */
8578 void CTcParser::parse_gram_alts(int *err, CTcSymObj *gram_obj,
8579 CTcGramProdEntry *prod,
8580 CTcGramPropArrows *arrows,
8581 CTcGramAltFuncs *funcs)
8582 {
8583 /* set up the first alternative and add it to the production */
8584 CTcGramProdAlt *alt = new (G_prsmem) CTcGramProdAlt(gram_obj, dict_cur_);
8585 if (prod != 0)
8586 prod->add_alt(alt);
8587
8588 /* flatten the grammar rules */
8589 CTcPrsGramNode *tree = flatten_gram_rule(err);
8590 if (tree == 0 || *err != 0)
8591 return;
8592
8593 /*
8594 * install the tree as the nested token source, so that we read tokens
8595 * from our expanded set of grammar rules rather than from the original
8596 * input stream
8597 */
8598 G_tok->set_external_source(tree);
8599
8600 /* parse the token specification list */
8601 for (int done = FALSE ; !done ; )
8602 {
8603 CTcGramProdTok *tok;
8604
8605 /* see what we have */
8606 switch(G_tok->cur())
8607 {
8608 case TOKT_LBRACK:
8609 {
8610 int skip_to_close;
8611
8612 /* presume we won't need to skip anything */
8613 skip_to_close = FALSE;
8614
8615 /* make sure we're at the start of the alternative */
8616 if (alt->get_tok_head() != 0)
8617 G_tok->log_warning(TCERR_GRAMMAR_QUAL_NOT_FIRST);
8618
8619 /* skip the open bracket and make sure a symbol follows */
8620 if (G_tok->next() == TOKT_SYM)
8621 {
8622 /* check what qualifier we have */
8623 if (G_tok->cur_tok_matches("badness", 7))
8624 {
8625 int val;
8626
8627 /* parse the integer-valued qualifier */
8628 val = parse_gram_qual_int(err, "badness", &done);
8629 if (*err != 0)
8630 return;
8631
8632 /* set the badness for the alternative */
8633 alt->set_badness(val);
8634 }
8635 else
8636 {
8637 /* not a recognized qualifier */
8638 G_tok->log_error_curtok(TCERR_BAD_GRAMMAR_QUAL);
8639
8640 /* skip remaining tokens to the ']' */
8641 skip_to_close = TRUE;
8642 }
8643
8644 /* check for proper close if we're is okay so far */
8645 if (!skip_to_close)
8646 {
8647 /* make sure we're at the close bracket */
8648 if (G_tok->cur() != TOKT_RBRACK)
8649 {
8650 /* log an error */
8651 G_tok->log_error_curtok(
8652 TCERR_GRAMMAR_QUAL_REQ_RBRACK);
8653
8654 /* skip to the bracket */
8655 skip_to_close = TRUE;
8656 }
8657 else
8658 {
8659 /* skip the bracket */
8660 G_tok->next();
8661 }
8662 }
8663 }
8664 else
8665 {
8666 /* log the error */
8667 G_tok->log_error_curtok(TCERR_GRAMMAR_QUAL_REQ_SYM);
8668
8669 /* skip to the end */
8670 skip_to_close = TRUE;
8671 }
8672
8673 /*
8674 * If we encountered an error, skip ahead until we find
8675 * a ']' or something that looks like the end of the
8676 * statement.
8677 */
8678 if (skip_to_close)
8679 {
8680 /* skip to the end of the qualifier */
8681 parse_gram_qual_skip(err, &done);
8682 if (*err)
8683 return;
8684 }
8685 }
8686
8687 /* done */
8688 break;
8689
8690 case TOKT_COLON:
8691 /* this is the end of the list - we're done */
8692 G_tok->next();
8693 done = TRUE;
8694 break;
8695
8696 case TOKT_OR:
8697 /*
8698 * alternator - create a new alternative and add it to the
8699 * production's list
8700 */
8701 alt = new (G_prsmem) CTcGramProdAlt(gram_obj, dict_cur_);
8702 if (prod != 0)
8703 prod->add_alt(alt);
8704
8705 /* skip the '|' token and proceed to the next token list */
8706 G_tok->next();
8707 break;
8708
8709 case TOKT_SSTR:
8710 case TOKT_DSTR:
8711 /*
8712 * quoted string (single or double) - this gives a literal
8713 * string to match
8714 */
8715 tok = new (G_prsmem) CTcGramProdTok();
8716 tok->set_match_literal(G_tok->getcur()->get_text(),
8717 G_tok->getcur()->get_text_len());
8718
8719 /* add it to the current alternative's list */
8720 alt->add_tok(tok);
8721
8722 /*
8723 * go check for an arrow (to assign the matching token to a
8724 * property)
8725 */
8726 goto check_for_arrow;
8727
8728 case TOKT_LT:
8729 /*
8730 * part-of-speech list - this gives a list, enclosed in angle
8731 * brackets, of part-of-speech properties that can be matched
8732 * by the token
8733 */
8734
8735 /* skip the open angle bracket */
8736 G_tok->next();
8737
8738 /* set up a new token with a part-of-speech list */
8739 tok = new (G_prsmem) CTcGramProdTok();
8740 tok->set_match_part_list();
8741
8742 /* add it to the current alternative's list */
8743 alt->add_tok(tok);
8744
8745 /* keep going until we reach the closing bracket */
8746 for (;;)
8747 {
8748 CTcSymbol *sym;
8749
8750 /* see what we have */
8751 switch(G_tok->cur())
8752 {
8753 case TOKT_GT:
8754 /*
8755 * closing angle bracket - we're done, so go check for
8756 * an arrow to assign the match to a property
8757 */
8758 goto check_for_arrow;
8759
8760 case TOKT_EOF:
8761 /* log the error */
8762 G_tok->log_error_curtok(TCERR_GRAMMAR_INVAL_TOK);
8763
8764 /* give up */
8765 *err = TRUE;
8766 return;
8767
8768 case TOKT_SYM:
8769 /*
8770 * symbol - look it up, defining it as a property if
8771 * it's not already defined as a property
8772 */
8773 sym = global_symtab_->find_or_def_prop_explicit(
8774 G_tok->getcur()->get_text(),
8775 G_tok->getcur()->get_text_len(), FALSE);
8776
8777 /* make sure it's a property */
8778 if (sym != 0 && sym->get_type() == TC_SYM_PROP)
8779 {
8780 CTcSymProp *propsym = (CTcSymProp *)sym;
8781
8782 /* add the property to the token's list */
8783 tok->add_match_part_ele(propsym->get_prop());
8784 }
8785 else
8786 {
8787 /* flag an error */
8788 G_tok->log_error_curtok(TCERR_GRAMMAR_LIST_REQ_PROP);
8789 }
8790
8791 /* we're done with the symbol, so skip it */
8792 G_tok->next();
8793
8794 /* back for more */
8795 break;
8796
8797 default:
8798 /*
8799 * anything else is an error; assume the '>' was
8800 * missing, so just flag the error and then act as
8801 * though we reached the end of the list
8802 */
8803 G_tok->log_error_curtok(TCERR_GRAMMAR_LIST_UNCLOSED);
8804 goto check_for_arrow;
8805 }
8806 }
8807 /* NOT REACHED */
8808
8809 case TOKT_SYM:
8810 /*
8811 * symbol token - this gives a part-of-speech or
8812 * sub-production match, depending on whether the symbol
8813 * refers to a property (in which case we have a
8814 * part-of-speech match) or an object (in which case we have a
8815 * sub-production match).
8816 */
8817 {
8818 CTcSymbol *sym;
8819
8820 /* look it up in the global symbol table */
8821 sym = global_symtab_->find(G_tok->getcur()->get_text(),
8822 G_tok->getcur()->get_text_len());
8823
8824 /*
8825 * if it's not defined, and we're not in syntax-only mode,
8826 * provide a default definition of the symbol as a
8827 * production object
8828 */
8829 if (sym == 0 && !syntax_only_)
8830 {
8831 /* it's undefined - presume it's a production */
8832 CTcGramProdEntry *sub_prod = funcs->declare_gramprod(
8833 G_tok->getcur()->get_text(),
8834 G_tok->getcur()->get_text_len());
8835
8836 /* if the symbol if still undefined it's an error */
8837 if (sub_prod == 0)
8838 return;
8839
8840 /* get the production's symbol */
8841 sym = sub_prod->get_prod_sym();
8842 }
8843
8844 /* check what kind of symbol we have */
8845 if (sym == 0)
8846 {
8847 /*
8848 * we're in parse-only mode and we have no symbol -
8849 * we can just ignore this until we're really
8850 * compiling
8851 */
8852 tok = 0;
8853 }
8854 else if (sym->get_type() == TC_SYM_OBJ)
8855 {
8856 /* we know it's an object symbol - cast it */
8857 CTcSymObj *objsym = (CTcSymObj *)sym;
8858
8859 /* make sure it's a production */
8860 if (objsym->get_metaclass() != TC_META_GRAMPROD)
8861 G_tok->log_error_curtok(TCERR_GRAMMAR_REQ_PROD);
8862
8863 /* create the production token */
8864 tok = new (G_prsmem) CTcGramProdTok();
8865 tok->set_match_prod(objsym);
8866 }
8867 else if (sym->get_type() == TC_SYM_PROP)
8868 {
8869 CTcSymProp *propsym = (CTcSymProp *)sym;
8870
8871 /* create the part-of-speech token */
8872 tok = new (G_prsmem) CTcGramProdTok();
8873 tok->set_match_part_of_speech(propsym->get_prop());
8874 }
8875 else if (sym->get_type() == TC_SYM_ENUM)
8876 {
8877 /* get the enum symbol properly cast */
8878 CTcSymEnum *enumsym = (CTcSymEnum *)sym;
8879
8880 /* enforce the 'enum token' rule if applicable */
8881 funcs->check_enum_tok(enumsym);
8882
8883 /* create the token-type token */
8884 tok = new (G_prsmem) CTcGramProdTok();
8885 tok->set_match_token_type(enumsym->get_enum_id());
8886 }
8887 else
8888 {
8889 /* it's an error */
8890 G_tok->log_error_curtok(TCERR_GRAMMAR_REQ_OBJ_OR_PROP);
8891
8892 /* no token */
8893 tok = 0;
8894 }
8895 }
8896
8897 /* if we have a token, add it to the alternative's list */
8898 if (tok != 0)
8899 alt->add_tok(tok);
8900
8901 check_for_arrow:
8902 /* skip the symbol and check for a '->' specification */
8903 if (G_tok->next() == TOKT_ARROW)
8904 {
8905 /* skip the arrow and make sure a symbol follows */
8906 if (G_tok->next() == TOKT_SYM)
8907 {
8908 CTcSymbol *sym;
8909
8910 /* look it up */
8911 sym = funcs->look_up_prop(G_tok->getcur(), FALSE);
8912
8913 /* set the association if we got a symbol */
8914 if (sym != 0)
8915 {
8916 CTcSymProp *propsym = (CTcSymProp *)sym;
8917 size_t i;
8918
8919 /* set the property association for the token */
8920 if (tok != 0)
8921 tok->set_prop_assoc(propsym->get_prop());
8922
8923 /*
8924 * Add the property to our list of assigned
8925 * properties - we'll use this to build the
8926 * grammar info list for the match object. Only
8927 * add the property if it's not already in the
8928 * list.
8929 */
8930 for (i = 0 ; i < arrows->cnt ; ++i)
8931 {
8932 /* if we found it, stop looking */
8933 if (arrows->prop[i] == propsym)
8934 break;
8935 }
8936
8937 /*
8938 * if we didn't find it, and we have room for
8939 * another, add it
8940 */
8941 if (i == arrows->cnt
8942 && arrows->cnt < arrows->max_arrows)
8943 {
8944 /* it's not there - add it */
8945 arrows->prop[arrows->cnt++] = propsym;
8946 }
8947 }
8948 else
8949 {
8950 /* log an error */
8951 G_tok->log_error_curtok(TCERR_GRAMMAR_ARROW_REQ_PROP);
8952 }
8953
8954 /* skip the symbol */
8955 G_tok->next();
8956 }
8957 else
8958 {
8959 /* it's an error */
8960 G_tok->log_error_curtok(TCERR_GRAMMAR_ARROW_REQ_PROP);
8961 }
8962 }
8963 break;
8964
8965 case TOKT_TIMES:
8966 /* free-floating end token */
8967 tok = new (G_prsmem) CTcGramProdTok();
8968 tok->set_match_star();
8969
8970 /* add it to the current alternative's list */
8971 alt->add_tok(tok);
8972
8973 /* skip the star */
8974 G_tok->next();
8975
8976 /*
8977 * this must be the last token in the alternative, so we
8978 * must have a ':' or '|' following
8979 */
8980 if (G_tok->cur() != TOKT_OR && G_tok->cur() != TOKT_COLON)
8981 G_tok->log_error_curtok(TCERR_GRAMMAR_STAR_NOT_END);
8982 break;
8983
8984 case TOKT_EOF:
8985 /* let the compiler interface decide whether it's an error */
8986 funcs->on_eof(err);
8987 if (*err)
8988 return;
8989
8990 /* in any case, it's the end of the object */
8991 done = TRUE;
8992 break;
8993
8994 case TOKT_LBRACE:
8995 case TOKT_RBRACE:
8996 case TOKT_SEM:
8997 /*
8998 * they probably meant to end the statement, even though
8999 * this isn't the time or place to do so - log an error and
9000 * stop scanning
9001 */
9002 G_tok->log_error_curtok(TCERR_GRAMMAR_INVAL_TOK);
9003 done = TRUE;
9004 break;
9005
9006 default:
9007 /* log an error */
9008 G_tok->log_error_curtok(TCERR_GRAMMAR_INVAL_TOK);
9009
9010 /*
9011 * skip the offending token, and hope that they merely
9012 * inserted something invalid
9013 */
9014 G_tok->next();
9015 break;
9016 }
9017 }
9018 }
9019
9020 /*
9021 * Flatten a set of grammar rules. Each time we find a parenthesized
9022 * alternator, we'll expand the alternatives, until we have no
9023 * parenthesized alternators.
9024 */
9025 CTcPrsGramNode *CTcParser::flatten_gram_rule(int *err)
9026 {
9027 /* first, build the top-level 'or' tree */
9028 CTcPrsGramNode *tree = parse_gram_or(err, 0);
9029 if (tree == 0 || *err != 0)
9030 return 0;
9031
9032 /* move all of the OR's to a single OR at the top of the tree */
9033 tree = tree->consolidate_or();
9034
9035 /* flatten CAT nodes in the resulting tree */
9036 tree->flatten_cat();
9037
9038 /*
9039 * if the top-level node isn't an OR, insert an OR at the top - this
9040 * makes the tree always follow the same shape, which makes it easier
9041 * to step through the tokens in the expansion
9042 */
9043 if (!tree->is_or())
9044 {
9045 CTcPrsGramNodeOr *new_or;
9046
9047 /* create an OR node for the root of the tree */
9048 new_or = new (G_prsmem) CTcPrsGramNodeOr();
9049
9050 /* insert our old root as the single alternative under the OR */
9051 new_or->add_sub_item(tree);
9052
9053 /* the OR is now the root of the tree */
9054 tree = new_or;
9055 }
9056
9057 /* start reading from the first token in the tree */
9058 tree->init_expansion();
9059
9060 /* return the tree */
9061 return tree;
9062 }
9063
9064 /*
9065 * Parse an OR expression in a grammar rule. Returns an OR node
9066 * representing the expression.
9067 */
9068 CTcPrsGramNode *CTcParser::parse_gram_or(int *err, int level)
9069 {
9070 CTcPrsGramNodeOr *or_node;
9071 CTcPrsGramNode *sub;
9072
9073 /* build our 'or' node */
9074 or_node = new (G_prsmem) CTcPrsGramNodeOr();
9075
9076 /* if the rule is completely empty, warn about it */
9077 if (level == 0 && G_tok->cur() == TOKT_COLON && !syntax_only_)
9078 G_tok->log_warning(TCERR_GRAMMAR_EMPTY);
9079
9080 /* parse our initial 'cat' subnode */
9081 sub = parse_gram_cat(err, level + 1);
9082
9083 /* abort on error */
9084 if (sub == 0 || *err != 0)
9085 return 0;
9086
9087 /* add the subnode to our 'or' list */
9088 or_node->add_sub_item(sub);
9089
9090 /* keep going as long as we have more rules appended with '|' */
9091 while (G_tok->cur() == TOKT_OR)
9092 {
9093 /* skip the '|' */
9094 G_tok->next();
9095
9096 /*
9097 * if we're at the top level, and the next token is the closing
9098 * ':', warn about the empty last rule
9099 */
9100 if (level == 0 && G_tok->cur() == TOKT_COLON && !syntax_only_)
9101 G_tok->log_warning(TCERR_GRAMMAR_ENDS_WITH_OR);
9102
9103 /* parse the 'cat' subnode */
9104 sub = parse_gram_cat(err, level + 1);
9105
9106 /* abort on error */
9107 if (sub == 0 || *err != 0)
9108 return 0;
9109
9110 /* add the subnode to our 'or' list */
9111 or_node->add_sub_item(sub);
9112 }
9113
9114 /* return the 'or' list */
9115 return or_node;
9116 }
9117
9118 /*
9119 * Parse a concatenation expression in a grammar rule. Returns a CAT node
9120 * representing the expression.
9121 */
9122 CTcPrsGramNode *CTcParser::parse_gram_cat(int *err, int level)
9123 {
9124 CTcPrsGramNodeCat *cat_node;
9125 CTcPrsGramNode *sub;
9126
9127 /* build our concatenation node */
9128 cat_node = new (G_prsmem) CTcPrsGramNodeCat();
9129
9130 /* add tokens to the cat list until we find the end of the list */
9131 for (;;)
9132 {
9133 /* see what we have */
9134 switch(G_tok->cur())
9135 {
9136 case TOKT_LPAR:
9137 /* skip the paren */
9138 G_tok->next();
9139
9140 /* parse the OR expression */
9141 sub = parse_gram_or(err, level + 1);
9142
9143 /* if that failed, abort */
9144 if (sub == 0 || *err != 0)
9145 return 0;
9146
9147 /* add the 'OR' to our concatenation expression */
9148 cat_node->add_sub_item(sub);
9149
9150 /* make sure we're at the close paren */
9151 if (G_tok->cur() != TOKT_RPAR)
9152 G_tok->log_error_curtok(TCERR_GRAMMAR_REQ_RPAR_AFTER_GROUP);
9153 else
9154 G_tok->next();
9155
9156 /*
9157 * make sure we don't have an arrow immediately following; an
9158 * arrow isn't allowed after a close paren, because a group
9159 * isn't a true subproduction
9160 */
9161 if (G_tok->cur() == TOKT_ARROW)
9162 G_tok->log_error(TCERR_GRAMMAR_GROUP_ARROW_NOT_ALLOWED);
9163
9164 /* done with the 'or' expression */
9165 break;
9166
9167 case TOKT_OR:
9168 /*
9169 * the 'or' has lower precedence than concatenation, so we've
9170 * reached the end of the concatenation expression - simply
9171 * return what we have so far and let the caller figure out
9172 * where to go next
9173 */
9174 return cat_node;
9175
9176 case TOKT_COLON:
9177 case TOKT_SEM:
9178 case TOKT_LBRACE:
9179 case TOKT_RBRACE:
9180 case TOKT_EOF:
9181 case TOKT_RPAR:
9182 /* we've reached the end of the rule */
9183 return cat_node;
9184
9185 default:
9186 /* anything else is simply concatenated to the list so far */
9187 cat_node->add_sub_item(
9188 new (G_prsmem) CTcPrsGramNodeTok(G_tok->getcur()));
9189
9190 /* skip the token and keep scanning */
9191 G_tok->next();
9192 break;
9193 }
9194 }
9195 }
9196
9197 /*
9198 * Parse a grammar qualifier integer value
9199 */
9200 int CTcParser::parse_gram_qual_int(int *err, const char *qual_name,
9201 int *stm_end)
9202 {
9203 CTcPrsNode *expr;
9204 CTcConstVal *cval;
9205
9206 /* skip the qualifier name */
9207 G_tok->next();
9208
9209 /* check for a missing expression */
9210 if (G_tok->cur() == TOKT_RBRACK)
9211 {
9212 /* don't bother looking for an expression - it's not there */
9213 expr = 0;
9214 }
9215 else
9216 {
9217 /* parse an expression */
9218 expr = parse_expr();
9219 }
9220
9221 /*
9222 * if we didn't get an expression, or it doesn't have a constant
9223 * value, or the constant value is something other than an integer,
9224 * it's an error
9225 */
9226 if (expr == 0
9227 || (cval = expr->get_const_val()) == 0
9228 || cval->get_type() != TC_CVT_INT)
9229 {
9230 /* log an error */
9231 G_tok->log_error(TCERR_GRAMMAR_QUAL_REQ_INT, qual_name);
9232
9233 /* skip to the closing bracket */
9234 parse_gram_qual_skip(err, stm_end);
9235
9236 /* we don't have a value to return; use zero by default */
9237 return 0;
9238 }
9239 else
9240 {
9241 /* return the constant expression value */
9242 return cval->get_val_int();
9243 }
9244 }
9245
9246 /*
9247 * Skip to the end of a grammar qualifier. This is used when a syntax
9248 * error occurs and we abandon parsing the rest of the qualifier.
9249 */
9250 void CTcParser::parse_gram_qual_skip(int *err, int *stm_end)
9251 {
9252 /* scan until we find the end */
9253 for (;;)
9254 {
9255 switch(G_tok->next())
9256 {
9257 case TOKT_RBRACK:
9258 /*
9259 * that's the end of the mal-formed qualifier; skip the
9260 * bracket and keep going from here
9261 */
9262 G_tok->next();
9263 break;
9264
9265 case TOKT_EOF:
9266 case TOKT_RBRACE:
9267 case TOKT_LBRACE:
9268 case TOKT_SEM:
9269 /*
9270 * probably the end of the statement - stop scanning, and
9271 * set the 'stm_end' flag to tell the caller that we're done
9272 * parsing the entire statement
9273 */
9274 *stm_end = TRUE;
9275 return;
9276
9277 default:
9278 /* skip everything else and just keep going */
9279 break;
9280 }
9281 }
9282 }
9283
9284 /* ------------------------------------------------------------------------ */
9285 /*
9286 * Grammar production list entry
9287 */
9288 CTcGramProdEntry::CTcGramProdEntry(CTcSymObj *prod_sym)
9289 {
9290 /* remember my object symbol */
9291 prod_sym_ = prod_sym;
9292
9293 /* not in a list yet */
9294 nxt_ = 0;
9295
9296 /* no alternatives yet */
9297 alt_head_ = alt_tail_ = 0;
9298
9299 /* not explicitly declared yet */
9300 is_declared_ = FALSE;
9301 }
9302
9303 /*
9304 * Add an alternative
9305 */
9306 void CTcGramProdEntry::add_alt(CTcGramProdAlt *alt)
9307 {
9308 /* link it at the end of my list */
9309 if (alt_tail_ != 0)
9310 alt_tail_->set_next(alt);
9311 else
9312 alt_head_ = alt;
9313 alt_tail_ = alt;
9314
9315 /* this is now the last element in our list */
9316 alt->set_next(0);
9317 }
9318
9319 /*
9320 * Move my alternatives to a new owner
9321 */
9322 void CTcGramProdEntry::move_alts_to(CTcGramProdEntry *new_entry)
9323 {
9324 CTcGramProdAlt *alt;
9325 CTcGramProdAlt *nxt;
9326
9327 /* move each of my alternatives */
9328 for (alt = alt_head_ ; alt != 0 ; alt = nxt)
9329 {
9330 /* remember the next alternative, since we're unlinking this one */
9331 nxt = alt->get_next();
9332
9333 /* unlink this one from the list */
9334 alt->set_next(0);
9335
9336 /* link this one into the new owner's list */
9337 new_entry->add_alt(alt);
9338 }
9339
9340 /* there's nothing left in our list */
9341 alt_head_ = alt_tail_ = 0;
9342 }
9343
9344 /* ------------------------------------------------------------------------ */
9345 /*
9346 * Grammar production alternative
9347 */
9348 CTcGramProdAlt::CTcGramProdAlt(CTcSymObj *obj_sym, CTcDictEntry *dict)
9349 {
9350 /* remember the associated processor object */
9351 obj_sym_ = obj_sym;
9352
9353 /* remember the default dictionary currently in effect */
9354 dict_ = dict;
9355
9356 /* nothing in our token list yet */
9357 tok_head_ = tok_tail_ = 0;
9358
9359 /* we don't have a score or badness yet */
9360 score_ = 0;
9361 badness_ = 0;
9362
9363 /* we're not in a list yet */
9364 nxt_ = 0;
9365 }
9366
9367 void CTcGramProdAlt::add_tok(CTcGramProdTok *tok)
9368 {
9369 /* link the token at the end of my list */
9370 if (tok_tail_ != 0)
9371 tok_tail_->set_next(tok);
9372 else
9373 tok_head_ = tok;
9374 tok_tail_ = tok;
9375
9376 /* there's nothing after this token */
9377 tok->set_next(0);
9378 }
9379
9380 /* ------------------------------------------------------------------------ */
9381 /*
9382 * Grammar production token object
9383 */
9384
9385 /*
9386 * Initialize with a part-of-speech list
9387 */
9388 void CTcGramProdTok::set_match_part_list()
9389 {
9390 const size_t init_alo = 10;
9391
9392 /* remember the type */
9393 typ_ = TCGRAM_PART_OF_SPEECH_LIST;
9394
9395 /* we have nothing in the list yet */
9396 val_.prop_list_.len_ = 0;
9397
9398 /* set the initial allocation size */
9399 val_.prop_list_.alo_ = init_alo;
9400
9401 /* allocate the initial list */
9402 val_.prop_list_.arr_ = (tctarg_prop_id_t *)G_prsmem->alloc(
9403 init_alo * sizeof(val_.prop_list_.arr_[0]));
9404 }
9405
9406 /*
9407 * Add a property to our part-of-speech match list
9408 */
9409 void CTcGramProdTok::add_match_part_ele(tctarg_prop_id_t prop)
9410 {
9411 /* if necessary, re-allocate the array at a larger size */
9412 if (val_.prop_list_.len_ == val_.prop_list_.alo_)
9413 {
9414 tctarg_prop_id_t *oldp;
9415
9416 /* bump up the size a bit */
9417 val_.prop_list_.alo_ += 10;
9418
9419 /* remember the current list long enough to copy it */
9420 oldp = val_.prop_list_.arr_;
9421
9422 /* reallocate it */
9423 val_.prop_list_.arr_ = (tctarg_prop_id_t *)G_prsmem->alloc(
9424 val_.prop_list_.alo_ * sizeof(val_.prop_list_.arr_[0]));
9425
9426 /* copy the old list into the new one */
9427 memcpy(val_.prop_list_.arr_, oldp,
9428 val_.prop_list_.len_ * sizeof(val_.prop_list_.arr_[0]));
9429 }
9430
9431 /*
9432 * we now know we have space for the new element, so add it, bumping up
9433 * the length counter to account for the addition
9434 */
9435 val_.prop_list_.arr_[val_.prop_list_.len_++] = prop;
9436 }
9437