Debian Codegraph @

terps/tads/tads3/tcprsprg.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header$";
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 tcprsprg.cpp - Program-level parsing
15 Function
16 This parser module includes statement node classes that are needed only
17 for a full compiler that parses at the outer program-level structure -
18 object definitions, 'extern' statements, etc. This file also contains code
19 to read and write symbol and object files. This module should not be
20 needed for tools that need only to compile expressions or code fragments,
21 such as debuggers or interpreters equipped with "eval()" functionality.
22 Notes
23
24 Modified
25 01/09/10 MJRoberts - Creation
26 */
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "os.h"
33 #include "t3std.h"
34 #include "utf8.h"
35 #include "tcprs.h"
36 #include "tctarg.h"
37 #include "tcgen.h"
38 #include "vmhash.h"
39 #include "tcmain.h"
40 #include "vmfile.h"
41 #include "tcmake.h"
42
43
44 /* ------------------------------------------------------------------------ */
45 /*
46 * Add an entry to a global symbol table, and mark the symbol as
47 * referenced.
48 */
49 void CTcPrsSymtab::add_to_global_symtab(CTcPrsSymtab *tab, CTcSymbol *entry)
50 {
51 /* add the entry to the symbol table */
52 tab->get_hashtab()->add(entry);
53
54 /* mark the entry as referenced */
55 entry->mark_referenced();
56 }
57
58
59 /* ------------------------------------------------------------------------ */
60 /*
61 * Callback context for enumerating the global symbol table entries for
62 * writing a symbol export file
63 */
64 struct prs_wsf_ctx
65 {
66 /* number of symbols so far */
67 ulong cnt;
68
69 /* the file we're writing */
70 CVmFile *fp;
71
72 /* the parser object */
73 CTcParser *prs;
74 };
75
76 /*
77 * Signature string for symbol export file. The numerical suffix at the
78 * end of the initial string is a format version number - we simply
79 * increment this each time we make a change to the format. On loading a
80 * symbol file, we check to make sure the format matches; if it doesn't,
81 * we'll abort with an error, ensuring that we don't try to read a file
82 * that's not formatted with the current structure. Since symbol files are
83 * derived directly from source files via compilation, the worst case is
84 * that the user has to do a full recompile when upgrading to a new
85 * compiler version, so we don't bother trying to maintain upward
86 * compatibility among the symbol file formats.
87 */
88 static const char symbol_export_sig[] = "TADS3.SymbolExport.0007\n\r\032";
89
90 /*
91 * Write a symbol export file.
92 */
93 void CTcParser::write_symbol_file(CVmFile *fp, CTcMake *make_obj)
94 {
95 prs_wsf_ctx ctx;
96 long size_ofs;
97 long cnt_ofs;
98 long end_ofs;
99 int i;
100 int cnt;
101
102 /* write the file signature */
103 fp->write_bytes(symbol_export_sig, sizeof(symbol_export_sig) - 1);
104
105 /* write a placeholder for the make-config block size */
106 size_ofs = fp->get_pos();
107 fp->write_uint4(0);
108
109 /* if there's a make object, tell it to write out its config data */
110 if (make_obj != 0)
111 {
112 long end_ofs;
113
114 /* write out the data */
115 make_obj->write_build_config_to_sym_file(fp);
116
117 /* go back and fix up the size */
118 end_ofs = fp->get_pos();
119 fp->set_pos(size_ofs);
120 fp->write_uint4(end_ofs - size_ofs - 4);
121
122 /* seek back to the end for continued writing */
123 fp->set_pos(end_ofs);
124 }
125
126 /*
127 * write the number of intrinsic function sets, followed by the
128 * function set names
129 */
130 fp->write_uint4(cnt = G_cg->get_fnset_cnt());
131 for (i = 0 ; i < cnt ; ++i)
132 {
133 const char *nm;
134 size_t len;
135
136 /* get the name */
137 nm = G_cg->get_fnset_name(i);
138
139 /* write the length followed by the name */
140 fp->write_uint2(len = strlen(nm));
141 fp->write_bytes(nm, len);
142 }
143
144 /*
145 * write the number of intrinsic classes, followed by the intrinsic
146 * class names
147 */
148 fp->write_uint4(cnt = G_cg->get_meta_cnt());
149 for (i = 0 ; i < cnt ; ++i)
150 {
151 const char *nm;
152 size_t len;
153
154 /* get the name */
155 nm = G_cg->get_meta_name(i);
156
157 /* write the length followed by the name */
158 fp->write_uint2(len = strlen(nm));
159 fp->write_bytes(nm, len);
160 }
161
162 /*
163 * write a placeholder for the symbol count, first remembering where
164 * we put it so that we can come back and fix it up after we know
165 * how many symbols there actually are
166 */
167 cnt_ofs = fp->get_pos();
168 fp->write_uint4(0);
169
170 /* write each entry from the global symbol table */
171 ctx.cnt = 0;
172 ctx.fp = fp;
173 ctx.prs = this;
174 global_symtab_->enum_entries(&write_sym_cb, &ctx);
175
176 /* go back and fix up the counter */
177 end_ofs = fp->get_pos();
178 fp->set_pos(cnt_ofs);
179 fp->write_uint4(ctx.cnt);
180
181 /*
182 * seek back to the ending offset, in case the caller is embedding
183 * the symbol data stream in a larger file structure of some kind
184 * and will be writing additional data after the end of our portion
185 */
186 fp->set_pos(end_ofs);
187 }
188
189 /*
190 * Callback for symbol table enumeration for writing a symbol export
191 * file. Writes one symbol to the file.
192 */
193 void CTcParser::write_sym_cb(void *ctx0, CTcSymbol *sym)
194 {
195 prs_wsf_ctx *ctx = (prs_wsf_ctx *)ctx0;
196
197 /* ask the symbol to write itself to the file */
198 if (sym->write_to_sym_file(ctx->fp))
199 {
200 /* we wrote this symbol - count it */
201 ++(ctx->cnt);
202 }
203 }
204
205 /* ------------------------------------------------------------------------ */
206 /*
207 * Seek to the start of the build configuration information in an object
208 * file
209 */
210 ulong CTcParser::seek_sym_file_build_config_info(class CVmFile *fp)
211 {
212 char buf[128];
213 ulong siz;
214 int err = FALSE;
215
216 /* read the signature, and make sure it matches */
217 err_try
218 {
219 fp->read_bytes(buf, sizeof(symbol_export_sig) - 1);
220 if (memcmp(buf, symbol_export_sig,
221 sizeof(symbol_export_sig) - 1) != 0)
222 {
223 /* invalid signature - no config data available */
224 return 0;
225 }
226
227 /* read the size of the block */
228 siz = fp->read_uint4();
229 }
230 err_catch_disc
231 {
232 /* note the error */
233 err = TRUE;
234 }
235 err_end;
236
237 /*
238 * if an error occurred, indicate that no configuration block is
239 * available
240 */
241 if (err)
242 return 0;
243
244 /*
245 * tell the caller the size of the block; the block immediately
246 * follows the size prefix, so the file pointer is set to the right
247 * position now
248 */
249 return siz;
250 }
251
252
253 /* ------------------------------------------------------------------------ */
254 /*
255 * Read a symbol file
256 */
257 int CTcParser::read_symbol_file(CVmFile *fp)
258 {
259 char buf[128];
260 ulong cnt;
261 ulong i;
262 ulong skip_len;
263
264 /* read the signature and ensure it's valid */
265 fp->read_bytes(buf, sizeof(symbol_export_sig) - 1);
266 if (memcmp(buf, symbol_export_sig, sizeof(symbol_export_sig) - 1) != 0)
267 {
268 /* it's invalid - log an error */
269 G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_SYMEXP_INV_SIG);
270
271 /* return failure */
272 return 1;
273 }
274
275 /*
276 * read the make-config data size and skip the data block - it's here
277 * for use by the make utility, and we don't have any use for it here
278 */
279 skip_len = fp->read_uint4();
280 fp->set_pos(fp->get_pos() + skip_len);
281
282 /* read the intrinsic function set count, then read the function sets */
283 cnt = fp->read_uint4();
284 for (i = 0 ; i < cnt ; ++i)
285 {
286 char buf[256];
287
288 /* read the function set name */
289 if (CTcParser::read_len_prefix_str(fp, buf, sizeof(buf),
290 TCERR_SYMEXP_SYM_TOO_LONG))
291 return 101;
292
293 /* add it to the function set list */
294 G_cg->add_fnset(buf);
295 }
296
297 /* read the metaclass count, then read the metaclasses */
298 cnt = fp->read_uint4();
299 for (i = 0 ; i < cnt ; ++i)
300 {
301 char buf[256];
302
303 /* read the metaclass external name */
304 if (CTcParser::read_len_prefix_str(fp, buf, sizeof(buf),
305 TCERR_SYMEXP_SYM_TOO_LONG))
306 return 102;
307
308 /* add it to the metaclass list */
309 G_cg->find_or_add_meta(buf, strlen(buf), 0);
310 }
311
312 /* read the symbol count */
313 cnt = fp->read_uint4();
314
315 /* read the symbols */
316 for ( ; cnt != 0 ; --cnt)
317 {
318 /* read a symbol */
319 CTcSymbol *sym = CTcSymbol::read_from_sym_file(fp);
320
321 /* if that failed, abort */
322 if (sym == 0)
323 return 2;
324
325 /* check for an existing definition */
326 CTcSymbol *old_sym = global_symtab_->find(
327 sym->get_sym(), sym->get_sym_len());
328
329 /*
330 * if we're redefining a weak property symbol, delete the old
331 * property symbol
332 */
333 if (old_sym != 0
334 && old_sym != sym
335 && old_sym->get_type() == TC_SYM_PROP
336 && ((CTcSymProp *)old_sym)->is_weak())
337 {
338 global_symtab_->remove_entry(old_sym);
339 old_sym = 0;
340 }
341
342 /*
343 * If the symbol is already in the symbol table, log a warning
344 * and ignore the symbol. If the new symbol we loaded is the
345 * same object as the original symbol table entry, it means that
346 * this is a valid redefinition - the symbol subclass loader
347 * indicated this by returning the same entry.
348 */
349 if (old_sym == sym)
350 {
351 /*
352 * it's a harmless redefinition - there's no need to warn
353 * and no need to add the symbol to the table again
354 */
355 }
356 else if (old_sym != 0)
357 {
358 /*
359 * it's already defined - log it as a pedantic warning (it's
360 * not a standard-level warning because, if it is actually a
361 * problem, it'll cause an actual error at link time, so
362 * there's little value in complaining about it now)
363 */
364 G_tcmain->log_error(0, 0, TC_SEV_PEDANTIC, TCERR_SYMEXP_REDEF,
365 (int)sym->get_sym_len(), sym->get_sym());
366 }
367 else
368 {
369 /* add it to the global symbol table */
370 global_symtab_->add_entry(sym);
371 }
372 }
373
374 /* success */
375 return 0;
376 }
377
378 /* ------------------------------------------------------------------------ */
379 /*
380 * Write to an object file
381 */
382
383 /*
384 * Write to an object file
385 */
386 void CTcParser::write_to_object_file(CVmFile *fp)
387 {
388 prs_wsf_ctx ctx;
389 long cnt_ofs;
390 long end_ofs;
391 ulong anon_cnt;
392 ulong nonsym_cnt;
393 tcprs_nonsym_obj *nonsym;
394 CTcSymObj *anon_obj;
395 CTcGramProdEntry *prod;
396 ulong prod_cnt;
397 CTcPrsExport *exp;
398 ulong exp_cnt;
399
400 /*
401 * reset the object file symbol and dictionary index values - start
402 * with 1, since 0 is a "null pointer" index value
403 */
404 obj_file_sym_idx_ = 1;
405 obj_file_dict_idx_ = 1;
406
407 /*
408 * write a placeholder for the symbol index table count, the
409 * dictionary table count, and the actual symbol count
410 */
411 cnt_ofs = fp->get_pos();
412 fp->write_uint4(0);
413 fp->write_uint4(0);
414 fp->write_uint4(0);
415
416 /* write each entry from the global symbol table */
417 ctx.cnt = 0;
418 ctx.fp = fp;
419 ctx.prs = this;
420 global_symtab_->enum_entries(&write_obj_cb, &ctx);
421
422 /* count the anonymous objects */
423 for (anon_cnt = 0, anon_obj = anon_obj_head_ ; anon_obj != 0 ;
424 ++anon_cnt, anon_obj = (CTcSymObj *)anon_obj->nxt_) ;
425
426 /* write the number of anonymous objects */
427 fp->write_uint4(anon_cnt);
428
429 /* write the anonymous objects */
430 for (anon_obj = anon_obj_head_ ; anon_obj != 0 ;
431 anon_obj = (CTcSymObj *)anon_obj->nxt_)
432 {
433 /* write this anonymous object */
434 anon_obj->write_to_obj_file(fp);
435 }
436
437 /* count the non-symbol objects */
438 for (nonsym_cnt = 0, nonsym = nonsym_obj_head_ ; nonsym != 0 ;
439 ++nonsym_cnt, nonsym = nonsym->nxt_) ;
440
441 /* write the non-symbol object ID's */
442 fp->write_uint4(nonsym_cnt);
443 for (nonsym = nonsym_obj_head_ ; nonsym != 0 ; nonsym = nonsym->nxt_)
444 fp->write_uint4((ulong)nonsym->id_);
445
446 /* remember where we are, and seek back to the counters for fixup */
447 end_ofs = fp->get_pos();
448 fp->set_pos(cnt_ofs);
449
450 /* write the actual symbol index count */
451 fp->write_uint4(obj_file_sym_idx_);
452
453 /* write the actual dictionary index count */
454 fp->write_uint4(obj_file_dict_idx_);
455
456 /* write the total named symbol count */
457 fp->write_uint4(ctx.cnt);
458
459 /*
460 * seek back to the ending offset so we can continue on to write the
461 * remainder of the file
462 */
463 fp->set_pos(end_ofs);
464
465 /* write a placeholder for the symbol cross-reference entries */
466 cnt_ofs = fp->get_pos();
467 fp->write_uint4(0);
468
469 /*
470 * enumerate entries again, this time writing cross-object
471 * references (this must be done after we've written out the main
472 * part of each object, so that we have a full table of all of the
473 * objects loaded before we write any references)
474 */
475 ctx.cnt = 0;
476 global_symtab_->enum_entries(&write_obj_ref_cb, &ctx);
477
478 /* go back and fix up the cross-reference count */
479 end_ofs = fp->get_pos();
480 fp->set_pos(cnt_ofs);
481 fp->write_uint4(ctx.cnt);
482 fp->set_pos(end_ofs);
483
484 /* write the anonymous object cross-references */
485 fp->write_uint4(anon_cnt);
486 for (anon_obj = anon_obj_head_ ; anon_obj != 0 ;
487 anon_obj = (CTcSymObj *)anon_obj->nxt_)
488 {
489 /* write this anonymous object's cross-references */
490 anon_obj->write_refs_to_obj_file(fp);
491 }
492
493 /* count the grammar productions */
494 for (prod_cnt = 0, prod = gramprod_head_ ; prod != 0 ;
495 ++prod_cnt, prod = prod->get_next()) ;
496
497 /* write the number of productions */
498 fp->write_uint4(prod_cnt);
499
500 /*
501 * write the main list of grammar rules - this is the list of
502 * productions that are associated with anonymous match objects and
503 * hence cannot be replaced or modified
504 */
505 for (prod = gramprod_head_ ; prod != 0 ; prod = prod->get_next())
506 {
507 /* write this entry */
508 prod->write_to_obj_file(fp);
509 }
510
511 /* write a placeholder for the named grammar rule count */
512 cnt_ofs = fp->get_pos();
513 fp->write_uint4(0);
514
515 /*
516 * write the private grammar rules - this is the set of rules
517 * associated with named match objects, so we write these by
518 * enumerating the symbol table again through the grammar rule writer
519 * callback
520 */
521 ctx.cnt = 0;
522 global_symtab_->enum_entries(&write_obj_gram_cb, &ctx);
523
524 /* go back and fix up the named grammar rule count */
525 end_ofs = fp->get_pos();
526 fp->set_pos(cnt_ofs);
527 fp->write_uint4(ctx.cnt);
528 fp->set_pos(end_ofs);
529
530 /* count the exports */
531 for (exp_cnt = 0, exp = exp_head_ ; exp != 0 ;
532 ++exp_cnt, exp = exp->get_next());
533
534 /* write the number of exports */
535 fp->write_uint4(exp_cnt);
536
537 /* write the exports */
538 for (exp = exp_head_ ; exp != 0 ; exp = exp->get_next())
539 {
540 /* write this entry */
541 exp->write_to_obj_file(fp);
542 }
543 }
544
545 /*
546 * Callback for symbol table enumeration for writing an object file.
547 * Writes one symbol to the file.
548 */
549 void CTcParser::write_obj_cb(void *ctx0, CTcSymbol *sym)
550 {
551 prs_wsf_ctx *ctx = (prs_wsf_ctx *)ctx0;
552
553 /* ask the symbol to write itself to the file */
554 if (sym->write_to_obj_file(ctx->fp))
555 {
556 /* we wrote this symbol - count it */
557 ++(ctx->cnt);
558 }
559 }
560
561 /*
562 * Callback for symbol table numeration - write object references to the
563 * object file.
564 */
565 void CTcParser::write_obj_ref_cb(void *ctx0, CTcSymbol *sym)
566 {
567 prs_wsf_ctx *ctx = (prs_wsf_ctx *)ctx0;
568
569 /* ask the symbol to write its references to the file */
570 if (sym->write_refs_to_obj_file(ctx->fp))
571 ++(ctx->cnt);
572 }
573
574 /*
575 * Callback for symbol table enumeration - write named grammar rules
576 */
577 void CTcParser::write_obj_gram_cb(void *ctx0, CTcSymbol *sym)
578 {
579 prs_wsf_ctx *ctx = (prs_wsf_ctx *)ctx0;
580 CTcSymObj *obj_sym;
581
582 /* if it's not an object symbol, we can ignore it */
583 if (sym->get_type() != TC_SYM_OBJ)
584 return;
585
586 /* cast it to the proper type */
587 obj_sym = (CTcSymObj *)sym;
588
589 /* if it doesn't have a private grammar rule list, ignore it */
590 if (obj_sym->get_grammar_entry() == 0)
591 return;
592
593 /* count it */
594 ++(ctx->cnt);
595
596 /* write the defining symbol's object file index */
597 ctx->fp->write_uint4(obj_sym->get_obj_file_idx());
598
599 /* write the grammar rule */
600 obj_sym->get_grammar_entry()->write_to_obj_file(ctx->fp);
601 }
602
603 /* ------------------------------------------------------------------------ */
604 /*
605 * Turn sourceTextGroup mode on or off
606 */
607 void CTcParser::set_source_text_group_mode(int f)
608 {
609 /* set the new mode */
610 src_group_mode_ = (f != 0);
611
612 /*
613 * If we're turning this mode on for the first time, add the object
614 * definition for the sourceTextGroup object - this is an anonymous
615 * object that we automatically create, once per source module; all of
616 * the sourceTextGroup properties in the module point to this same
617 * object, which is how we can tell at run-time that they were defined
618 * in the same module.
619 */
620 if (f && src_group_id_ == TCTARG_INVALID_OBJ)
621 {
622 CTcSymObj *obj;
623 CTPNStmObject *obj_stm;
624 CTcConstVal cval;
625
626 /* create the anonymous object */
627 src_group_id_ = G_cg->new_obj_id();
628
629 /* create an anonymous symbol table entry for the object */
630 obj = new CTcSymObj(".sourceTextGroup", 16, FALSE,
631 src_group_id_, FALSE, TC_META_TADSOBJ, 0);
632 G_prs->add_anon_obj(obj);
633
634 /* create an object statement for it */
635 obj_stm = new CTPNStmObject(obj, FALSE);
636 obj->set_obj_stm(obj_stm);
637
638 /*
639 * if we know the module name and sequence number, set properties
640 * for them in the referent object
641 */
642 if (module_name_ != 0)
643 {
644 cval.set_sstr(module_name_, strlen(module_name_));
645 obj_stm->add_prop(src_group_mod_sym_, new CTPNConst(&cval),
646 FALSE, FALSE);
647 }
648 if (module_seqno_ != 0)
649 {
650 cval.set_int(module_seqno_);
651 obj_stm->add_prop(src_group_seq_sym_, new CTPNConst(&cval),
652 FALSE, FALSE);
653 }
654
655 /* add it to the nested statement list */
656 add_nested_stm(obj_stm);
657 }
658 }
659
660 /* ------------------------------------------------------------------------ */
661 /*
662 * Parse top-level definitions. Returns the head of a list of statement
663 * nodes; the statements are the top-level statements in the program.
664 */
665 class CTPNStmProg *CTcParser::parse_top()
666 {
667 /* nothing in our list yet */
668 CTPNStmTop *first_stm = 0;
669 CTPNStmTop *last_stm = 0;
670
671 /* if there are any pending nested statements, add them to the list now */
672 if (nested_stm_head_ != 0)
673 {
674 /* move the nested statement list into the top-level list */
675 first_stm = nested_stm_head_;
676 last_stm = nested_stm_tail_;
677
678 /* clear out the old list */
679 nested_stm_head_ = nested_stm_tail_ = 0;
680 }
681
682 /* do not suppress errors */
683 int suppress_error = FALSE;
684
685 /* no end-of-file error yet */
686 int err = FALSE;
687
688 /* keep going until we reach the end of the file */
689 for (int done = FALSE ; !done && !err ; )
690 {
691 /* we don't have a statement yet */
692 CTPNStmTop *cur_stm = 0;
693
694 /*
695 * presume we'll find a valid statement and hence won't need to
696 * suppress subsequent errors
697 */
698 int suppress_next_error = FALSE;
699
700 /* see what we have */
701 switch(G_tok->cur())
702 {
703 case TOKT_EOF:
704 /* we're done */
705 done = TRUE;
706 break;
707
708 case TOKT_FUNCTION:
709 case TOKT_METHOD:
710 /* parse the function definition */
711 cur_stm = parse_function(&err, FALSE, FALSE, FALSE, TRUE);
712 break;
713
714 case TOKT_EXTERN:
715 /* parse the extern definition */
716 parse_extern(&err);
717 break;
718
719 case TOKT_INTRINSIC:
720 /* parse an intrinsic function set definition */
721 cur_stm = parse_intrinsic(&err);
722 break;
723
724 case TOKT_TRANSIENT:
725 case TOKT_SYM:
726 /* it's an object or function definition */
727 cur_stm = parse_object_or_func(&err, FALSE, suppress_error,
728 &suppress_next_error);
729 break;
730
731 case TOKT_OBJECT:
732 /*
733 * it's an anonymous object with 'object' superclass, or an
734 * 'object template' definition
735 */
736 cur_stm = parse_object_stm(&err, FALSE);
737 break;
738
739 case TOKT_PLUS:
740 case TOKT_INC:
741 cur_stm = parse_plus_object(&err);
742 break;
743
744 case TOKT_CLASS:
745 /* it's a class definition */
746 cur_stm = parse_class(&err);
747 break;
748
749 case TOKT_REPLACE:
750 /* it's a 'replace' statement */
751 cur_stm = parse_replace(&err);
752 break;
753
754 case TOKT_MODIFY:
755 /* it's a 'modify' statement */
756 cur_stm = parse_modify(&err);
757 break;
758
759 case TOKT_PROPERTY:
760 /* it's a 'property' statement */
761 parse_property(&err);
762 break;
763
764 case TOKT_EXPORT:
765 /* it's an 'export' statement */
766 parse_export(&err);
767 break;
768
769 case TOKT_DICTIONARY:
770 /* it's a 'dictionary' statement */
771 cur_stm = parse_dict(&err);
772 break;
773
774 case TOKT_GRAMMAR:
775 /* it's a 'grammar' statement */
776 cur_stm = parse_grammar(&err, FALSE, FALSE);
777 break;
778
779 case TOKT_ENUM:
780 /* it's an 'enum' statement */
781 parse_enum(&err);
782 break;
783
784 case TOKT_SEM:
785 /* empty statement - skip it */
786 G_tok->next();
787 break;
788
789 default:
790 /*
791 * note the error, unless we just generated the same error
792 * for the previous token - if we did, there's no point in
793 * showing it again, since we're probably skipping lots of
794 * tokens trying to get resynchronized
795 */
796 if (!suppress_error)
797 G_tok->log_error_curtok(TCERR_REQ_FUNC_OR_OBJ);
798
799 /* suppress the next error of the same kind */
800 suppress_next_error = TRUE;
801
802 /* skip the errant token */
803 G_tok->next();
804 break;
805 }
806
807 /*
808 * set the error suppression status according to whether we just
809 * found an error - if we did, don't report another of the same
810 * kind twice in a row, since we're probably just scanning
811 * through tons of stuff trying to get re-synchronized
812 */
813 suppress_error = suppress_next_error;
814
815 /* if we parsed a statement, add it to our list */
816 if (cur_stm != 0)
817 {
818 /* link the statement at the end of our list */
819 if (last_stm != 0)
820 last_stm->set_next_stm_top(cur_stm);
821 else
822 first_stm = cur_stm;
823 last_stm = cur_stm;
824 }
825 }
826
827 /* add the list of nested top-level statements to the statement list */
828 if (nested_stm_head_ != 0)
829 {
830 if (last_stm != 0)
831 last_stm->set_next_stm_top(nested_stm_head_);
832 else
833 first_stm = nested_stm_head_;
834 }
835
836 /*
837 * we've now moved the nested top-level statement list into the normal
838 * program statement list, so we can forget this as a separate list
839 */
840 nested_stm_head_ = nested_stm_tail_ = 0;
841
842 /* construct a compound statement to hold the top-level statement list */
843 return new CTPNStmProg(first_stm);
844 }
845
846 /* ------------------------------------------------------------------------ */
847 /*
848 * Parse a 'property' top-level statement
849 */
850 void CTcParser::parse_property(int *err)
851 {
852 int done;
853
854 /* skip the 'property' token */
855 G_tok->next();
856
857 /* parse property names until we run out */
858 for (done = FALSE ; !done ; )
859 {
860 /* we should be looking at a property name */
861 if (G_tok->cur() == TOKT_SYM)
862 {
863 /*
864 * look up the property; this will add it to the symbol table
865 * if it isn't already in there, and will generate an error if
866 * it's already defined as something other than a property
867 */
868 look_up_prop(G_tok->getcur(), TRUE);
869
870 /* skip the symbol and check what follows */
871 if (G_tok->next() == TOKT_COMMA)
872 {
873 /* skip the comma and continue */
874 G_tok->next();
875 }
876 else if (G_tok->cur() == TOKT_SEM)
877 {
878 /*
879 * end of the statement - skip the semicolon and we're
880 * done
881 */
882 G_tok->next();
883 break;
884 }
885 else
886 {
887 /*
888 * if it's a brace, they probably left out the semicolon;
889 * if it's a symbol, they probably left out the comma;
890 * otherwise, it's probably a stray token
891 */
892 switch(G_tok->cur())
893 {
894 case TOKT_SYM:
895 /*
896 * probably left out a comma - flag an error, but
897 * proceed with parsing this new symbol
898 */
899 G_tok->log_error_curtok(TCERR_PROPDECL_REQ_COMMA);
900 break;
901
902 case TOKT_LBRACE:
903 case TOKT_RBRACE:
904 case TOKT_EOF:
905 /* they probably left out the semicolon */
906 G_tok->log_error_curtok(TCERR_EXPECTED_SEMI);
907 return;
908
909 default:
910 /* log an error */
911 G_tok->log_error_curtok(TCERR_PROPDECL_REQ_COMMA);
912
913 /* skip the errant symbol */
914 G_tok->next();
915
916 /*
917 * if a comma follows, something was probably just
918 * inserted - skip the comma; if a semicolon follows,
919 * assume we're at the end of the statement
920 */
921 if (G_tok->cur() == TOKT_COMMA)
922 {
923 /*
924 * we're probably okay again - skip the comma and
925 * continue
926 */
927 G_tok->next();
928 }
929 else if (G_tok->cur() == TOKT_SEM)
930 {
931 /* skip the semicolon, and we're done */
932 G_tok->next();
933 done = TRUE;
934 }
935
936 /* proceed with what follows */
937 break;
938 }
939 }
940 }
941 else
942 {
943 /* log an error */
944 G_tok->log_error_curtok(TCERR_PROPDECL_REQ_SYM);
945
946 switch(G_tok->cur())
947 {
948 case TOKT_SEM:
949 case TOKT_LBRACE:
950 case TOKT_RBRACE:
951 case TOKT_EOF:
952 /* they're probably done with the statement */
953 return;
954
955 case TOKT_COMMA:
956 /*
957 * they probably just doubled a comma - skip it and
958 * continue
959 */
960 G_tok->next();
961 break;
962
963 default:
964 /* skip this token */
965 G_tok->next();
966
967 /*
968 * if a comma follows, they probably put in a keyword or
969 * something like that - skip the comma and continue
970 */
971 if (G_tok->cur() == TOKT_COMMA)
972 G_tok->next();
973 break;
974 }
975
976 /* if a semicolon follows, we're done */
977 if (G_tok->cur() == TOKT_SEM)
978 {
979 /* skip the semicolon and we're done */
980 G_tok->next();
981 break;
982 }
983 }
984 }
985 }
986
987 /* ------------------------------------------------------------------------ */
988 /*
989 * Parse an 'export' top-level statement
990 */
991 void CTcParser::parse_export(int *err)
992 {
993 CTcPrsExport *exp;
994
995 /* skip the 'export' token and see what follows */
996 switch(G_tok->next())
997 {
998 case TOKT_SYM:
999 /* create a new export record for this symbol token */
1000 exp = add_export(G_tok->getcur()->get_text(),
1001 G_tok->getcur()->get_text_len());
1002
1003 /*
1004 * Check to see what follows. We can either have a single-quoted
1005 * string giving the external name by which this symbol should be
1006 * known, or the end of the statement. If it's the latter, the
1007 * external name is the same as the symbol name.
1008 */
1009 switch(G_tok->next())
1010 {
1011 case TOKT_SEM:
1012 /* end of the statement - skip the semicolon, and we're done */
1013 G_tok->next();
1014 break;
1015
1016 case TOKT_SSTR:
1017 /*
1018 * it's an explicit external name specification - save the
1019 * name with the export record
1020 */
1021 exp->set_extern_name(G_tok->getcur()->get_text(),
1022 G_tok->getcur()->get_text_len());
1023
1024 /* if it's too long, flag an error */
1025 if (G_tok->getcur()->get_text_len() > TOK_SYM_MAX_LEN)
1026 G_tok->log_error_curtok(TCERR_EXPORT_EXT_TOO_LONG);
1027
1028 /* skip the string and check for a semicolon */
1029 if (G_tok->next() != TOKT_SEM)
1030 {
1031 /* assume they just left out the semicolon, but log it */
1032 G_tok->log_error_curtok(TCERR_EXPECTED_SEMI);
1033 }
1034 break;
1035
1036 default:
1037 /*
1038 * anything else is an error; assume they simply omitted the
1039 * semicolon
1040 */
1041 G_tok->log_error_curtok(TCERR_EXPECTED_SEMI);
1042 }
1043
1044 /* done */
1045 break;
1046
1047 case TOKT_SEM:
1048 case TOKT_LBRACE:
1049 case TOKT_RBRACE:
1050 case TOKT_EOF:
1051 /*
1052 * they probably just left something out; consider this the end of
1053 * the statement, but log an error about it
1054 */
1055 G_tok->log_error_curtok(TCERR_EXPORT_REQ_SYM);
1056 break;
1057
1058 default:
1059 /*
1060 * something weird happened; skip the errant token, and consider
1061 * it the end of the statement
1062 */
1063 G_tok->log_error_curtok(TCERR_EXPORT_REQ_SYM);
1064 G_tok->next();
1065 break;
1066 }
1067 }
1068
1069 /*
1070 * Add an export
1071 */
1072 CTcPrsExport *CTcParser::add_export(const char *sym, size_t len)
1073 {
1074 CTcPrsExport *exp;
1075
1076 /* create a new record */
1077 exp = new CTcPrsExport(sym, len);
1078
1079 /* add it to our list */
1080 add_export_to_list(exp);
1081
1082 /* return it */
1083 return exp;
1084 }
1085
1086 /*
1087 * Add an export to our list
1088 */
1089 void CTcParser::add_export_to_list(CTcPrsExport *exp)
1090 {
1091 /* link it at the end of our list */
1092 if (exp_tail_ == 0)
1093 exp_head_ = exp;
1094 else
1095 exp_tail_->set_next(exp);
1096 exp_tail_ = exp;
1097 }
1098
1099 /*
1100 * Parse a 'dictionary' top-level statement
1101 */
1102 CTPNStmTop *CTcParser::parse_dict(int *err)
1103 {
1104 int done;
1105
1106 /* skip the 'dictionary' token and check what follows */
1107 switch(G_tok->next())
1108 {
1109 case TOKT_PROPERTY:
1110 /*
1111 * 'dictionary property' definition - what follows is a list of
1112 * properties that are going to be considered dictionary
1113 * properties.
1114 */
1115
1116 /* skip the 'property' token */
1117 G_tok->next();
1118
1119 /* parse the list of property names */
1120 for (done = FALSE ; !done ; )
1121 {
1122 /* check what we have */
1123 if (G_tok->cur() == TOKT_SYM)
1124 {
1125 CTcSymProp *sym;
1126
1127 /* look up the property */
1128 sym = look_up_prop(G_tok->getcur(), TRUE);
1129
1130 /*
1131 * if it's already marked as a dictionary property,
1132 * there's nothing we need to do now; otherwise, mark it
1133 */
1134 if (sym != 0 && !sym->is_vocab())
1135 {
1136 CTcDictPropEntry *entry;
1137
1138 /* mark it as a vocabulary property */
1139 sym->set_vocab(TRUE);
1140
1141 /*
1142 * include the symbol in our master dictionary
1143 * property list
1144 */
1145
1146 /* create a new list entry */
1147 entry = new (G_prsmem) CTcDictPropEntry(sym);
1148
1149 /* link it into our list */
1150 entry->nxt_ = dict_prop_head_;
1151 dict_prop_head_ = entry;
1152 }
1153
1154 /* skip the symbol and check what follows */
1155 if (G_tok->next() == TOKT_COMMA)
1156 {
1157 /* skip the comma and continue */
1158 G_tok->next();
1159 }
1160 else if (G_tok->cur() == TOKT_SEM)
1161 {
1162 /*
1163 * end of the statement - skip the semicolon and
1164 * we're done
1165 */
1166 G_tok->next();
1167 break;
1168 }
1169 else
1170 {
1171 /*
1172 * if it's a brace, they probably left out the
1173 * semicolon; if it's a symbol, they probably left
1174 * out the comma; otherwise, it's probably a stray
1175 * token
1176 */
1177 switch(G_tok->cur())
1178 {
1179 case TOKT_SYM:
1180 /*
1181 * probably left out a comma - flag an error,
1182 * but proceed with parsing this new symbol
1183 */
1184 G_tok->log_error_curtok(TCERR_DICT_PROP_REQ_COMMA);
1185 break;
1186
1187 case TOKT_LBRACE:
1188 case TOKT_RBRACE:
1189 case TOKT_EOF:
1190 /* they probably left out the semicolon */
1191 G_tok->log_error_curtok(TCERR_EXPECTED_SEMI);
1192 return 0;
1193
1194 default:
1195 /* log an error */
1196 G_tok->log_error_curtok(TCERR_DICT_PROP_REQ_COMMA);
1197
1198 /* skip the errant symbol */
1199 G_tok->next();
1200
1201 /*
1202 * if a comma follows, something was probably
1203 * just inserted - skip the comma; if a
1204 * semicolon follows, assume we're at the end of
1205 * the statement
1206 */
1207 if (G_tok->cur() == TOKT_COMMA)
1208 {
1209 /*
1210 * we're probably okay again - skip the
1211 * comma and continue
1212 */
1213 G_tok->next();
1214 }
1215 else if (G_tok->cur() == TOKT_SEM)
1216 {
1217 /* skip the semicolon, and we're done */
1218 G_tok->next();
1219 done = TRUE;
1220 }
1221
1222 /* proceed with what follows */
1223 break;
1224 }
1225 }
1226 }
1227 else
1228 {
1229 /* log an error */
1230 G_tok->log_error_curtok(TCERR_DICT_PROP_REQ_SYM);
1231
1232 switch(G_tok->cur())
1233 {
1234 case TOKT_SEM:
1235 case TOKT_LBRACE:
1236 case TOKT_RBRACE:
1237 case TOKT_EOF:
1238 /* they're probably done with the statement */
1239 return 0;
1240
1241 case TOKT_COMMA:
1242 /*
1243 * they probably just doubled a comma - skip it and
1244 * continue
1245 */
1246 G_tok->next();
1247 break;
1248
1249 default:
1250 /* skip this token */
1251 G_tok->next();
1252
1253 /*
1254 * if a comma follows, they probably put in a
1255 * keyword or something like that - skip the comma
1256 * and continue
1257 */
1258 if (G_tok->cur() == TOKT_COMMA)
1259 G_tok->next();
1260 break;
1261 }
1262
1263 /* if a semicolon follows, we're done */
1264 if (G_tok->cur() == TOKT_SEM)
1265 {
1266 /* skip the semicolon and we're done */
1267 G_tok->next();
1268 break;
1269 }
1270 }
1271 }
1272
1273 /*
1274 * this statement doesn't result in adding anything to the parse
1275 * tree
1276 */
1277 return 0;
1278
1279 case TOKT_SYM:
1280 /*
1281 * Dictionary object definition - what follows is the name of an
1282 * object to be created of metaclass 'dictionary'. This object
1283 * is to become the active dictionary for subsequent parsing.
1284 */
1285 {
1286 CTcDictEntry *dict;
1287 CTPNStmDict *dict_stm = 0;
1288
1289 /* find the dictionary entry */
1290 dict = declare_dict(G_tok->getcur()->get_text(),
1291 G_tok->getcur()->get_text_len());
1292
1293 /* if we have a symbol, create the statement node */
1294 if (dict != 0)
1295 {
1296 /* create the 'dictionary' statement node */
1297 dict_stm = new CTPNStmDict(dict);
1298
1299 /*
1300 * remember the active dictionary - this is the
1301 * dictionary into which subsequent vocabulary will be
1302 * inserted
1303 */
1304 dict_cur_ = dict;
1305
1306 /* mark the dictionary as non-external */
1307 dict->get_sym()->set_extern(FALSE);
1308 }
1309
1310 /* skip the symbol and parse the required closing semicolon */
1311 G_tok->next();
1312 if (parse_req_sem())
1313 {
1314 *err = TRUE;
1315 return 0;
1316 }
1317
1318 /* return the 'dictionary' statement */
1319 return dict_stm;
1320 }
1321
1322 default:
1323 /* anything else is invalid */
1324 G_tok->log_error_curtok(TCERR_DICT_SYNTAX);
1325 return 0;
1326 }
1327 }
1328
1329 /* ------------------------------------------------------------------------ */
1330 /*
1331 * Grammar tree parser compiler interface functions for the regular static
1332 * compiler
1333 */
1334 class CTcGramAltFuncsStatic: public CTcGramAltFuncs
1335 {
1336 public:
1337 /* initialize with the parser object */
1338 CTcGramAltFuncsStatic(CTcParser *parser) { this->prs = parser; }
1339
1340 /* look up a property - look up or add to the global symbols */
1341 CTcSymProp *look_up_prop(const CTcToken *tok, int show_err)
1342 { return prs->look_up_prop(tok, show_err); }
1343
1344 /* declare a grammar object - look up in or add to the global symbols */
1345 CTcGramProdEntry *declare_gramprod(const char *txt, size_t len)
1346 { return prs->declare_gramprod(txt, len); }
1347
1348 /* check the given enum for use as a production token */
1349 virtual void check_enum_tok(class CTcSymEnum *enumsym)
1350 {
1351 /*
1352 * Only allow 'enum token' values in static compilation. I'm not
1353 * actually sure at this point what the rationale ever was for this
1354 * bit of strictness; there's no separate meaning for regular enums
1355 * in this context, and as far as I can tell the compiler doesn't
1356 * use the is_token() attribute for any other purpose. The
1357 * somewhat similar 'dictionary property' attribute at least does
1358 * trigger special compiler behavior, which is to add strings to
1359 * the dictionary when 'dictionary property' properties are defined
1360 * on objects. My guess is that I originally had something along
1361 * the same lines in mind for 'enum token', but whatever it was
1362 * turned out to be superfluous or just wrong. So 'enum token'
1363 * amounts to documentation at this point.
1364 *
1365 * The reason for abstracting this test into the AltFuncs structure
1366 * is that the dynamic compiler doesn't have any information on
1367 * 'enum token' or 'dictionary property' attributes - it just knows
1368 * these symbols as 'enum' or 'property' values, since there's no
1369 * VM-level representation of the extra attributes. So for dynamic
1370 * compilation, the grammar parser doesn't have any way to accept
1371 * enum tokens in rules. One possible solution was to contrive to
1372 * store the extra attributes in the run-time symbol table. But
1373 * given the apparent pointlessness of the strict test, I'm opting
1374 * for the easier route, which is to simply relax enforcement for
1375 * dynamic compilation, and allow any enum value to be used. We
1376 * can probably drop the test for static compilation as well, but
1377 * I'm keeping it for now on the general principle of avoiding
1378 * gratuitous changes.
1379 */
1380 if (!enumsym->is_token())
1381 G_tok->log_error_curtok(TCERR_GRAMMAR_BAD_ENUM);
1382 }
1383
1384 /* EOF in the middle of an alternative - this is an error */
1385 void on_eof(int *err)
1386 {
1387 /* log the error */
1388 G_tok->log_error_curtok(TCERR_GRAMMAR_INVAL_TOK);
1389
1390 /* note the error for our caller */
1391 *err = TRUE;
1392 }
1393
1394 /* the parser object */
1395 CTcParser *prs;
1396 };
1397
1398 /* ------------------------------------------------------------------------ */
1399 /*
1400 * Parse a 'grammar' top-level statement
1401 */
1402 CTPNStmTop *CTcParser::parse_grammar(int *err, int replace, int modify)
1403 {
1404 /* no GrammarProd object yet */
1405 CTcSymObj *gram_obj = 0;
1406
1407 /* presume we're not modifying anything */
1408 CTcSymObj *mod_orig_sym = 0;
1409
1410 /* presume we won't need a private production object */
1411 int need_private_prod = FALSE;
1412
1413 /* presume we won't find a valid production name */
1414 CTcToken prod_name;
1415 prod_name.set_text("?", 1);
1416 CTcGramProdEntry *prod = 0;
1417
1418 /* presume it will be anonymous (i.e., no name tag) */
1419 int is_anon = TRUE;
1420
1421 /* skip the 'grammar' token and check for the production name */
1422 if (G_tok->next() != TOKT_SYM)
1423 {
1424 /* log an error, then proceed without a symbol name */
1425 G_tok->log_error_curtok(TCERR_GRAMMAR_REQ_SYM);
1426 }
1427 else
1428 {
1429 /* remember the production name */
1430 prod_name = *G_tok->copycur();
1431
1432 /* find or create the 'grammar production' entry */
1433 prod = declare_gramprod(prod_name.get_text(),
1434 prod_name.get_text_len());
1435 }
1436
1437 /* use the production name as the name tag */
1438 char tag_buf[TOK_SYM_MAX_LEN*2 + 32];
1439 const char *name_tag = prod_name.get_text();
1440 size_t name_tag_len = prod_name.get_text_len();
1441
1442 /* presume there'll be no sub-tag */
1443 const char *sub_tag = 0;
1444 size_t sub_tag_len = 0;
1445
1446 /* check for the optional name-tag token */
1447 if (G_tok->next() == TOKT_LPAR)
1448 {
1449 /* skip the paren - the name token follows */
1450 G_tok->next();
1451
1452 /*
1453 * limit the added copying so we don't overflow our buffer - note
1454 * that we need two bytes for parens
1455 */
1456 size_t copy_len = G_tok->getcur()->get_text_len();
1457 if (copy_len > sizeof(tag_buf) - name_tag_len - 2)
1458 copy_len = sizeof(tag_buf) - name_tag_len - 2;
1459
1460 /* remember the sub-tag */
1461 sub_tag = G_tok->getcur()->get_text();
1462 sub_tag_len = G_tok->getcur()->get_text_len();
1463
1464 /* build the name */
1465 memcpy(tag_buf, name_tag, name_tag_len);
1466 tag_buf[name_tag_len] = '(';
1467 memcpy(tag_buf + name_tag_len + 1,
1468 G_tok->getcur()->get_text(), copy_len);
1469 tag_buf[name_tag_len + 1 + copy_len] = ')';
1470
1471 /* store the tag in tokenizer memory */
1472 name_tag_len += 2 + copy_len;
1473 name_tag = G_tok->store_source(tag_buf, name_tag_len);
1474
1475 /* require the closing paren */
1476 if (G_tok->next() == TOKT_RPAR)
1477 {
1478 /* found it - skip it */
1479 G_tok->next();
1480 }
1481 else
1482 {
1483 /* flag the error, but proceed as though it was there */
1484 G_tok->log_error_curtok(TCERR_GRAMMAR_REQ_NAME_RPAR);
1485 }
1486
1487 /*
1488 * There's a tag, so the grammar match object has a symbol name
1489 * given by the full "prod(tag)" string. Note that grammar match
1490 * definitions are inherently classes.
1491 */
1492 int is_class = TRUE;
1493 int trans = FALSE;
1494 gram_obj = find_or_def_obj(name_tag, name_tag_len,
1495 replace, modify, &is_class,
1496 &mod_orig_sym, 0, &trans);
1497
1498 /* note that the object is named */
1499 is_anon = FALSE;
1500
1501 /*
1502 * Since this is a named grammar match object, we must keep the
1503 * list of grammar rules defined here in a private list. At link
1504 * time, we'll merge this private grammar rule list with the
1505 * master list for the production, but we must keep a private list
1506 * until link time in case this match object is modified or
1507 * replaced.
1508 */
1509 need_private_prod = TRUE;
1510 }
1511 else if (!replace && !modify && G_tok->cur() == TOKT_SEM)
1512 {
1513 /*
1514 * It's an empty 'grammar x;' statement, with no rule definition
1515 * and no properties. This is a simple declaration of the
1516 * production name and defines no rules for it; the only thing we
1517 * have to do is mark the production object as explicitly declared.
1518 */
1519 if (prod != 0)
1520 prod->set_declared(TRUE);
1521
1522 /* skip the semicolon */
1523 G_tok->next();
1524
1525 /*
1526 * we're done - a simple grammar production declaration generates
1527 * no parse tree object
1528 */
1529 return 0;
1530 }
1531 else
1532 {
1533 /*
1534 * There's no tag, so the grammar match object is anonymous.
1535 * Create a new anonymous object symbol for it.
1536 */
1537 gram_obj = new CTcSymObj(".anon", 5, FALSE, G_cg->new_obj_id(),
1538 FALSE, TC_META_TADSOBJ, 0);
1539
1540 /*
1541 * 'replace' and 'modify' can only be used with tagged grammar
1542 * rules, since that's the only way we can refer to a specific rule
1543 * previously defined
1544 */
1545 if (replace || modify)
1546 G_tok->log_error(TCERR_GRAMMAR_MOD_REQ_TAG);
1547 }
1548
1549 /* check for and skip the required colon */
1550 if (G_tok->cur() == TOKT_COLON)
1551 {
1552 /* it's there - skip it and proceed */
1553 G_tok->next();
1554 }
1555 else
1556 {
1557 /*
1558 * log an error, then proceed on the assumption that the colon
1559 * was simply omitted
1560 */
1561 G_tok->log_error_curtok(TCERR_GRAMMAR_REQ_COLON);
1562 }
1563
1564 /*
1565 * mark it as a class - this object exists as a factory for match tree
1566 * instances
1567 */
1568 if (gram_obj != 0)
1569 gram_obj->set_is_class(TRUE);
1570
1571 /*
1572 * If we're modifying the rule, check for an empty rule list. With
1573 * 'modify', an empty rule list has the special meaning that we retain
1574 * the original rule list of the base object being modified.
1575 */
1576 CTcGramPropArrows arrows;
1577 if (modify && G_tok->cur() == TOKT_COLON)
1578 {
1579 /*
1580 * This is a 'modify', and the rule list is empty. This means that
1581 * we're to leave the original rule list of the base object intact,
1582 * so we don't need to flatten the input syntax (since there's no
1583 * input syntax to flatten), we don't need to create a new private
1584 * list (since we want to leave the existing private list intact),
1585 * and we don't need to set up the first alternative for the rule
1586 * (since there will be no alternatives at all).
1587 */
1588 G_tok->next();
1589 }
1590 else
1591 {
1592 /*
1593 * We don't have an empty 'modify' rule list, so we're defining a
1594 * rule list for this production. If we decided that we need to
1595 * store the list in a private list, create the private list.
1596 */
1597 if (need_private_prod && gram_obj != 0)
1598 prod = gram_obj->create_grammar_entry(
1599 prod_name.get_text(), prod_name.get_text_len());
1600
1601 /* parse the token specification list */
1602 CTcGramAltFuncsStatic funcs(this);
1603 parse_gram_alts(err, gram_obj, prod, &arrows, &funcs);
1604 if (*err != 0)
1605 return 0;
1606 }
1607
1608 /* parse the object body */
1609 CTPNStmObject *stm = parse_object_body(
1610 err, gram_obj, TRUE, is_anon, TRUE, FALSE, modify, mod_orig_sym,
1611 0, 0, 0, FALSE);
1612
1613 /*
1614 * Add the grammarInfo property. This property is a method that
1615 * returns a list whose first element is the match's name tag, and
1616 * whose subsequent elements are the arrow-assigned properties.
1617 *
1618 * This is only necessary if we managed to create a statement object.
1619 */
1620 if (stm != 0)
1621 {
1622 CTPNList *lst;
1623 CTcConstVal cval;
1624 size_t i;
1625
1626 /* this is in global scope */
1627 local_symtab_ = global_symtab_;
1628
1629 /* create the list expression */
1630 lst = new CTPNList();
1631
1632 /* add the name-tag string element */
1633 cval.set_sstr(name_tag, name_tag_len);
1634 lst->add_element(new CTPNConst(&cval));
1635
1636 /* add each property to the list */
1637 for (i = 0 ; i < arrows.cnt ; ++i)
1638 {
1639 /* add a node to evaluate this property symbol */
1640 lst->add_element(create_sym_node(arrows.prop[i]->get_sym(),
1641 arrows.prop[i]->get_sym_len()));
1642 }
1643
1644 /* add a property for this */
1645 stm->add_prop(graminfo_prop_, lst, FALSE, FALSE);
1646
1647 /* add a grammarTag property with the tag name */
1648 cval.set_sstr(sub_tag, sub_tag_len);
1649 stm->add_prop(gramtag_prop_, new CTPNConst(&cval), FALSE, FALSE);
1650 }
1651
1652 /* return the object definition statement */
1653 return stm;
1654 }
1655
1656 /* ------------------------------------------------------------------------ */
1657 /*
1658 * Create a grammar rule list object
1659 */
1660 CTcGramProdEntry *CTcSymObjBase::create_grammar_entry(
1661 const char *prod_sym, size_t prod_sym_len)
1662 {
1663 /* look up the grammar production symbol */
1664 CTcSymObj *sym = G_prs->find_or_def_gramprod(prod_sym, prod_sym_len, 0);
1665
1666 /* create a new grammar list associated with the production */
1667 grammar_entry_ = new (G_prsmem) CTcGramProdEntry(sym);
1668
1669 /* return the new grammar list */
1670 return grammar_entry_;
1671 }
1672
1673
1674 /* ------------------------------------------------------------------------ */
1675 /*
1676 * Find or add a grammar production symbol. Returns the master
1677 * CTcGramProdEntry object for the grammar production.
1678 */
1679 CTcGramProdEntry *CTcParser::declare_gramprod(const char *txt, size_t len)
1680 {
1681 /* find or define the grammar production object symbol */
1682 CTcGramProdEntry *entry;
1683 (void)find_or_def_gramprod(txt, len, &entry);
1684
1685 /* return the entry */
1686 return entry;
1687 }
1688
1689 /* ------------------------------------------------------------------------ */
1690 /*
1691 * Find or add a grammar production symbol.
1692 */
1693 CTcSymObj *CTcParser::find_or_def_gramprod(const char *txt, size_t len,
1694 CTcGramProdEntry **entryp)
1695 {
1696 CTcSymObj *sym;
1697 CTcGramProdEntry *entry;
1698
1699 /* look up the symbol */
1700 sym = (CTcSymObj *)global_symtab_->find(txt, len);
1701
1702 /* if it's not defined, add it; otherwise, make sure it's correct */
1703 if (sym == 0)
1704 {
1705 /* it's not yet defined - define it as a grammar production */
1706 sym = new CTcSymObj(G_tok->getcur()->get_text(),
1707 G_tok->getcur()->get_text_len(),
1708 FALSE, G_cg->new_obj_id(), FALSE,
1709 TC_META_GRAMPROD, 0);
1710
1711 /* add it to the global symbol table */
1712 global_symtab_->add_entry(sym);
1713
1714 /* create a new production list entry */
1715 entry = create_gramprod_entry(sym);
1716 }
1717 else
1718 {
1719 /* make sure it's an object of metaclass 'grammar production' */
1720 if (sym->get_type() != TC_SYM_OBJ)
1721 {
1722 /* log an error */
1723 G_tok->log_error(TCERR_REDEF_AS_OBJ, (int)len, txt);
1724
1725 /* forget the symbol - it's not even an object */
1726 sym = 0;
1727 }
1728 else if (sym->get_metaclass() != TC_META_GRAMPROD)
1729 {
1730 /* it's an object, but not a production - log an error */
1731 G_tok->log_error(TCERR_REDEF_AS_GRAMPROD, (int)len, txt);
1732
1733 /* forget the symbol */
1734 sym = 0;
1735 }
1736
1737 /*
1738 * If we found the symbol, make sure it's not marked as external,
1739 * since we're actually defining a rule for this production. If
1740 * the production is exported from any other modules, we'll share
1741 * the production object with the other modules.
1742 */
1743 if (sym != 0)
1744 sym->set_extern(FALSE);
1745
1746 /* get the existing production object, if any */
1747 entry = get_gramprod_entry(sym);
1748
1749 /*
1750 * if we didn't find the entry, we must have loaded the symbol from
1751 * a symbol file - add the entry now
1752 */
1753 if (entry == 0)
1754 entry = create_gramprod_entry(sym);
1755 }
1756
1757 /*
1758 * if the caller is interested in knowing the associated grammar rule
1759 * list entry, return it
1760 */
1761 if (entryp != 0)
1762 *entryp = entry;
1763
1764 /* return the new symbol */
1765 return sym;
1766 }
1767
1768 /* ------------------------------------------------------------------------ */
1769 /*
1770 * Create a grammar production list entry. This creates a CTcGramProdEntry
1771 * object associated with the given grammar production symbol, and links
1772 * the new entry into the master list of grammar production entries.
1773 */
1774 CTcGramProdEntry *CTcParser::create_gramprod_entry(CTcSymObj *sym)
1775 {
1776 CTcGramProdEntry *entry;
1777
1778 /* allocate the new entry */
1779 entry = new (G_prsmem) CTcGramProdEntry(sym);
1780
1781 /* link the new entry into our list of anonymous match entries */
1782 if (gramprod_tail_ != 0)
1783 gramprod_tail_->set_next(entry);
1784 else
1785 gramprod_head_ = entry;
1786 gramprod_tail_ = entry;
1787
1788 /*
1789 * set the metaclass-specific data in the object symbol to point to the
1790 * grammar production list entry
1791 */
1792 if (sym != 0)
1793 sym->set_meta_extra(entry);
1794
1795 /* return the new entry */
1796 return entry;
1797 }
1798
1799 /* ------------------------------------------------------------------------ */
1800 /*
1801 * find a grammar entry for a given (GrammarProd) object symbol
1802 */
1803 CTcGramProdEntry *CTcParser::get_gramprod_entry(CTcSymObj *obj)
1804 {
1805 /* the grammar entry is the metaclass-specific data pointer */
1806 return (obj == 0 ? 0 : (CTcGramProdEntry *)obj->get_meta_extra());
1807 }
1808
1809 /* ------------------------------------------------------------------------ */
1810 /*
1811 * Find or add a dictionary symbol
1812 */
1813 CTcDictEntry *CTcParser::declare_dict(const char *txt, size_t len)
1814 {
1815 CTcSymObj *sym;
1816 CTcDictEntry *entry = 0;
1817
1818 /* look up the symbol */
1819 sym = (CTcSymObj *)global_symtab_->find(txt, len);
1820
1821 /* if it's not defined, add it; otherwise, make sure it's correct */
1822 if (sym == 0)
1823 {
1824 /* it's not yet defined - define it as a dictionary */
1825 sym = new CTcSymObj(G_tok->getcur()->get_text(),
1826 G_tok->getcur()->get_text_len(),
1827 FALSE, G_cg->new_obj_id(), FALSE,
1828 TC_META_DICT, 0);
1829
1830 /* add it to the global symbol table */
1831 global_symtab_->add_entry(sym);
1832
1833 /* create a new dictionary entry */
1834 entry = create_dict_entry(sym);
1835 }
1836 else
1837 {
1838 /* make sure it's an object of metaclass 'dictionary' */
1839 if (sym->get_type() != TC_SYM_OBJ)
1840 {
1841 /* log an error */
1842 G_tok->log_error_curtok(TCERR_REDEF_AS_OBJ);
1843
1844 /* forget the symbol - it's not even an object */
1845 sym = 0;
1846 }
1847 else if (sym->get_metaclass() != TC_META_DICT)
1848 {
1849 /* it's an object, but not a dictionary - log an error */
1850 G_tok->log_error_curtok(TCERR_REDEF_AS_DICT);
1851
1852 /* forget the symbol */
1853 sym = 0;
1854 }
1855
1856 /* find it in our dictionary list */
1857 entry = get_dict_entry(sym);
1858
1859 /*
1860 * if we didn't find the entry, we must have loaded the symbol from
1861 * a symbol file - add the dictionary list entry now
1862 */
1863 if (entry == 0)
1864 entry = create_dict_entry(sym);
1865 }
1866
1867 /* return the new dictionary object */
1868 return entry;
1869 }
1870
1871 /* ------------------------------------------------------------------------ */
1872 /*
1873 * Object symbols
1874 */
1875
1876 /*
1877 * Build my dictionary
1878 */
1879 void CTcSymObjBase::build_dictionary()
1880 {
1881 CTcVocabEntry *entry;
1882
1883 /* if I don't have a dictionary, there's nothing to do */
1884 if (dict_ == 0)
1885 return;
1886
1887 /*
1888 * if I'm a class, there's nothing to do, since vocabulary defined in a
1889 * class is only entered in the dictionary for the instances of the
1890 * class, not for the class itself
1891 */
1892 if (is_class_)
1893 return;
1894
1895 /* add inherited words from my superclasses to my list */
1896 inherit_vocab();
1897
1898 /* add each of my words to the dictionary */
1899 for (entry = vocab_ ; entry != 0 ; entry = entry->nxt_)
1900 {
1901 /* add this word to my dictionary */
1902 dict_->add_word(entry->txt_, entry->len_, FALSE,
1903 obj_id_, entry->prop_);
1904 }
1905 }
1906
1907 /*
1908 * Synthesize a placeholder symbol for a modified object.
1909 *
1910 * The new symbol is not for use by the source code; we add it merely as a
1911 * placeholder. Build its name starting with a space so that it can never
1912 * be reached from source code, and use the object number to ensure it's
1913 * unique within the file.
1914 */
1915 CTcSymObj *CTcSymObjBase::synthesize_modified_obj_sym(int anon)
1916 {
1917 char nm[TOK_SYM_MAX_LEN + 1];
1918 const char *stored_nm;
1919 tc_obj_id mod_id;
1920 CTcSymObj *mod_sym;
1921 size_t len;
1922
1923 /* create a new ID for the modified object */
1924 mod_id = G_cg->new_obj_id();
1925
1926 /* build the name */
1927 if (anon)
1928 {
1929 /* it's anonymous - we don't need a real name */
1930 stored_nm = ".anon";
1931 len = strlen(nm);
1932 }
1933 else
1934 {
1935 /* synthesize a name */
1936 synthesize_modified_obj_name(nm, mod_id);
1937 len = strlen(nm);
1938
1939 /* store it in tokenizer memory */
1940 stored_nm = G_tok->store_source(nm, len);
1941 }
1942
1943 /* create the object */
1944 mod_sym = new CTcSymObj(stored_nm, len, FALSE, mod_id, FALSE,
1945 TC_META_TADSOBJ, 0);
1946
1947 /* mark it as modified */
1948 mod_sym->set_modified(TRUE);
1949
1950 /* add it to the symbol table, if it has a name */
1951 if (!anon)
1952 G_prs->get_global_symtab()->add_entry(mod_sym);
1953 else
1954 G_prs->add_anon_obj(mod_sym);
1955
1956 /* return the new symbol */
1957 return mod_sym;
1958 }
1959
1960 /*
1961 * Build the name of a synthesized placeholder symbol for a given object
1962 * number. The buffer should be TOK_SYM_MAX_LEN + 1 bytes long.
1963 */
1964 void CTcSymObjBase::
1965 synthesize_modified_obj_name(char *buf, tctarg_obj_id_t obj_id)
1966 {
1967 /*
1968 * Build the fake name, based on the object ID to ensure uniqueness and
1969 * so that we can look it up based on the object ID. Start it with a
1970 * space so that no source token can ever refer to it.
1971 */
1972 sprintf(buf, " %lx", (ulong)obj_id);
1973 }
1974
1975
1976 /* ------------------------------------------------------------------------ */
1977 /*
1978 * create a new dictionary list entry
1979 */
1980 CTcDictEntry *CTcParser::create_dict_entry(CTcSymObj *sym)
1981 {
1982 CTcDictEntry *entry;
1983
1984 /* allocate the new entry */
1985 entry = new (G_prsmem) CTcDictEntry(sym);
1986
1987 /* link the new entry into our list */
1988 if (dict_tail_ != 0)
1989 dict_tail_->set_next(entry);
1990 else
1991 dict_head_ = entry;
1992 dict_tail_ = entry;
1993
1994 /*
1995 * set the metaclass-specific extra data in the object symbol to point
1996 * to the dictionary list entry
1997 */
1998 sym->set_meta_extra(entry);
1999
2000 /* return the new entry */
2001 return entry;
2002 }
2003
2004 /* ------------------------------------------------------------------------ */
2005 /*
2006 * find a dictionary list entry for a given object symbol
2007 */
2008 CTcDictEntry *CTcParser::get_dict_entry(CTcSymObj *obj)
2009 {
2010 /* the dictionary list entry is the metaclass-specific data pointer */
2011 return (obj == 0 ? 0 : (CTcDictEntry *)obj->get_meta_extra());
2012 }
2013
2014 /* ------------------------------------------------------------------------ */
2015 /*
2016 * Parse an 'enum' top-level statement
2017 */
2018 void CTcParser::parse_enum(int *err)
2019 {
2020 int done;
2021 int is_token;
2022
2023 /* presume it's not a 'token' enum */
2024 is_token = FALSE;
2025
2026 /* skip the 'enum' token */
2027 G_tok->next();
2028
2029 /*
2030 * if this is the 'token' context-sensitive keyword, note it and
2031 * skip it
2032 */
2033 if (G_tok->cur() == TOKT_SYM && G_tok->cur_tok_matches("token", 5))
2034 {
2035 /* remember that it's a 'token' enum */
2036 is_token = TRUE;
2037
2038 /* skip the keyword */
2039 G_tok->next();
2040 }
2041
2042 /* parse enum constants */
2043 for (done = FALSE ; !done ; )
2044 {
2045 CTcSymEnum *sym;
2046
2047 switch (G_tok->cur())
2048 {
2049 case TOKT_SYM:
2050 /* make sure the symbol isn't already defined */
2051 sym = (CTcSymEnum *)
2052 global_symtab_->find(G_tok->getcur()->get_text(),
2053 G_tok->getcur()->get_text_len());
2054
2055 /* if it's already defined, make sure it's an enum */
2056 if (sym != 0)
2057 {
2058 /* if it's not an enum, it's an error */
2059 if (sym->get_type() != TC_SYM_ENUM)
2060 {
2061 /* log the error */
2062 G_tok->log_error_curtok(TCERR_REDEF_AS_ENUM);
2063 }
2064 else if (is_token)
2065 {
2066 /*
2067 * we're defining a token - mark the old symbol as a
2068 * token if it wasn't already
2069 */
2070 sym->set_is_token(TRUE);
2071 }
2072 }
2073 else
2074 {
2075 /* create the new symbol */
2076 sym = new CTcSymEnum(G_tok->getcur()->get_text(),
2077 G_tok->getcur()->get_text_len(),
2078 FALSE, new_enum_id(), is_token);
2079
2080 /* add it to the symbol table */
2081 global_symtab_->add_entry(sym);
2082
2083 /* mark it as referenced, since it's defined here */
2084 sym->mark_referenced();
2085 }
2086
2087 /* skip the symbol name and see what follows */
2088 switch(G_tok->next())
2089 {
2090 case TOKT_COMMA:
2091 /* skip the comma and keep going */
2092 G_tok->next();
2093 break;
2094
2095 case TOKT_SEM:
2096 /* end of the statement */
2097 done = TRUE;
2098 G_tok->next();
2099 break;
2100
2101 case TOKT_LBRACE:
2102 case TOKT_RBRACE:
2103 case TOKT_EOF:
2104 /* they probably omitted the closing semicolon */
2105 G_tok->log_error_curtok(TCERR_EXPECTED_SEMI);
2106 done = TRUE;
2107 break;
2108
2109 default:
2110 /* a comma was required */
2111 G_tok->log_error_curtok(TCERR_ENUM_REQ_COMMA);
2112
2113 /* skip the offending symbol and proceed */
2114 G_tok->next();
2115 break;
2116 }
2117
2118 /* done with this token */
2119 break;
2120
2121 case TOKT_LBRACE:
2122 case TOKT_RBRACE:
2123 case TOKT_EOF:
2124 /*
2125 * they probably omitted the closing semicolon - log an
2126 * error, then stop scanning the statement, since we're
2127 * probably in the next statement already
2128 */
2129 G_tok->log_error_curtok(TCERR_EXPECTED_SEMI);
2130 done = TRUE;
2131 break;
2132
2133 case TOKT_SEM:
2134 /* they must have left out a symbol */
2135 G_tok->log_error_curtok(TCERR_ENUM_REQ_SYM);
2136
2137 /* accept the semicolon as the end of the statement */
2138 G_tok->next();
2139 done = TRUE;
2140 break;
2141
2142 default:
2143 /* anything else is an error */
2144 G_tok->log_error_curtok(TCERR_ENUM_REQ_SYM);
2145
2146 /* skip the offending token and keep parsing */
2147 G_tok->next();
2148 break;
2149 }
2150 }
2151 }
2152
2153 /* ------------------------------------------------------------------------ */
2154 /*
2155 * Parse an 'extern' top-level statement
2156 */
2157 void CTcParser::parse_extern(int *err)
2158 {
2159 int is_class;
2160
2161 /* skip the 'extern' token and check what follows */
2162 switch(G_tok->next())
2163 {
2164 case TOKT_FUNCTION:
2165 case TOKT_METHOD:
2166 /* parse the function definition for the prototype only */
2167 parse_function(err, TRUE, FALSE, FALSE, TRUE);
2168 break;
2169
2170 case TOKT_CLASS:
2171 /* note that it's a class, and process it otherwise like 'object' */
2172 is_class = TRUE;
2173 goto do_object;
2174
2175 case TOKT_OBJECT:
2176 /* note that it's not a class */
2177 is_class = FALSE;
2178
2179 do_object:
2180 /* external object/class - skip the keyword and get the symbol */
2181 if (G_tok->next() == TOKT_SYM)
2182 {
2183 CTcSymObj *sym;
2184
2185 /* check for a prior definition */
2186 sym = (CTcSymObj *)
2187 global_symtab_->find(G_tok->getcur()->get_text(),
2188 G_tok->getcur()->get_text_len());
2189 if (sym == 0)
2190 {
2191 /* not yet defined - create the external object symbol */
2192 sym = new CTcSymObj(G_tok->getcur()->get_text(),
2193 G_tok->getcur()->get_text_len(),
2194 FALSE, G_cg->new_obj_id(), TRUE,
2195 TC_META_TADSOBJ, 0);
2196
2197 /* add it to the symbol table */
2198 global_symtab_->add_entry(sym);
2199 }
2200 else if (sym->get_type() == TC_SYM_OBJ
2201 && sym->get_metaclass() == TC_META_TADSOBJ
2202 && ((sym->is_class() != 0) == (is_class != 0)))
2203 {
2204 /*
2205 * It's already defined as this same type, so this
2206 * definition is merely redundant - ignore it.
2207 */
2208 }
2209 else
2210 {
2211 /*
2212 * it's already defined, but not as an object - log an
2213 * error and ignore the redefinition
2214 */
2215 G_tok->log_error_curtok(TCERR_OBJ_REDEF);
2216 }
2217
2218 /* parse the required semicolon */
2219 G_tok->next();
2220 if (parse_req_sem())
2221 *err = TRUE;
2222 }
2223 else
2224 {
2225 /* invalid syntax - log an error */
2226 G_tok->log_error_curtok(TCERR_EXTERN_OBJ_REQ_SYM);
2227
2228 /* skip to the next semicolon */
2229 if (skip_to_sem())
2230 *err = TRUE;
2231 }
2232 break;
2233
2234 default:
2235 /* log an error */
2236 G_tok->log_error_curtok(TCERR_INVAL_EXTERN);
2237
2238 /* skip to the next semicolon */
2239 if (skip_to_sem())
2240 *err = TRUE;
2241 break;
2242 }
2243 }
2244
2245 /* ------------------------------------------------------------------------ */
2246 /*
2247 * Parse a 'function' top-level statement
2248 */
2249 CTPNStmTop *CTcParser::parse_function(int *err, int is_extern,
2250 int replace, int modify,
2251 int func_kw_present)
2252 {
2253 CTcToken tok;
2254 CTcSymFunc *func_sym;
2255 CTPNCodeBody *code_body;
2256 int has_proto = TRUE;
2257 int argc;
2258 int opt_argc;
2259 int varargs;
2260 int varargs_list;
2261 CTcSymLocal *varargs_list_local;
2262 int has_retval;
2263 int redef;
2264 CTcTokFileDesc *sym_file;
2265 long sym_linenum;
2266 CTcFormalTypeList *type_list = 0;
2267 int is_method = FALSE;
2268
2269 /* skip the 'function' or 'method' keyword, if present */
2270 if (func_kw_present)
2271 {
2272 /* note if it's a method definition */
2273 is_method = (G_tok->cur() == TOKT_METHOD);
2274
2275 /* skip the token */
2276 G_tok->next();
2277 }
2278
2279 /* we need a symbol */
2280 if (G_tok->cur() != TOKT_SYM)
2281 {
2282 /* log an error */
2283 G_tok->log_error_curtok(TCERR_FUNC_REQ_SYM);
2284
2285 /* proceed from this token */
2286 return 0;
2287 }
2288
2289 /* remember the symbol token */
2290 tok = *G_tok->copycur();
2291
2292 /* remember the location of the symbol (for later error reporting) */
2293 G_tok->get_last_pos(&sym_file, &sym_linenum);
2294
2295 /*
2296 * Skip past the function name. If the next token is a semicolon,
2297 * this is a forward declaration; otherwise, we need either an open
2298 * brace or an argument list
2299 */
2300 switch(G_tok->next())
2301 {
2302 case TOKT_SEM:
2303 /* it's just a forward declaration - skip the semicolon */
2304 G_tok->next();
2305
2306 /*
2307 * if this is an 'extern' declaration, then declare the function
2308 * as extern with zero arguments
2309 */
2310 if (is_extern)
2311 {
2312 /* no prototype and no arguments */
2313 has_proto = FALSE;
2314 argc = 0;
2315 opt_argc = 0;
2316 varargs = FALSE;
2317 varargs_list = FALSE;
2318
2319 /* assume it has a return value */
2320 has_retval = TRUE;
2321
2322 /* no code body */
2323 code_body = 0;
2324
2325 /* handle the declaration */
2326 goto decl_func;
2327 }
2328
2329 /*
2330 * A non-extern forward declaration has no effect. Ignore the
2331 * definition and keep going.
2332 */
2333 return 0;
2334
2335 case TOKT_LPAR:
2336 case TOKT_LBRACE:
2337 /*
2338 * check whether this is an 'extern' declaration or the actual
2339 * definition, and parse accordingly
2340 */
2341 if (is_extern)
2342 {
2343 /*
2344 * It's external - parse simply the formal parameter list.
2345 * A brace is not allowed here.
2346 */
2347 if (G_tok->cur() == TOKT_LPAR)
2348 {
2349 /* skip the paren */
2350 G_tok->next();
2351
2352 /* parse the formals */
2353 parse_formal_list(TRUE, TRUE, &argc, &opt_argc, &varargs,
2354 &varargs_list, &varargs_list_local,
2355 err, 0, FALSE, &type_list);
2356
2357 /*
2358 * if it looks as though there's a code body, log an
2359 * error and skip and ignore the code body
2360 */
2361 if (G_tok->cur() == TOKT_LBRACE
2362 || G_tok->cur() == TOKT_EQ)
2363 {
2364 /* log the error */
2365 G_tok->log_error(TCERR_EXTERN_NO_CODE_BODY);
2366
2367 /* parse and ignore the code body */
2368 parse_code_body(FALSE, FALSE, FALSE,
2369 &argc, &opt_argc, &varargs,
2370 &varargs_list, &varargs_list_local,
2371 &has_retval, err, 0, TCPRS_CB_NORMAL,
2372 0, 0, 0, 0);
2373
2374 /* return failure */
2375 return 0;
2376 }
2377
2378 /* parse the required terminating semicolon */
2379 parse_req_sem();
2380 }
2381 else
2382 {
2383 /* code body is not allowed - log an error */
2384 G_tok->log_error(TCERR_EXTERN_NO_CODE_BODY);
2385
2386 /* parse and ignore the code body */
2387 parse_code_body(FALSE, FALSE, is_method,
2388 &argc, &opt_argc, &varargs,
2389 &varargs_list, &varargs_list_local,
2390 &has_retval, err, 0, TCPRS_CB_NORMAL,
2391 0, 0, 0, 0);
2392
2393 /* done */
2394 return 0;
2395 }
2396
2397 /* there's no code body for an 'extern' definintion */
2398 code_body = 0;
2399
2400 /* assume all externals return a value */
2401 has_retval = TRUE;
2402 }
2403 else
2404 {
2405 /*
2406 * We're parsing the actual definition. Parse the formal
2407 * parameter list and the code body.
2408 */
2409 code_body = parse_code_body(FALSE, FALSE, is_method,
2410 &argc, &opt_argc, &varargs,
2411 &varargs_list, &varargs_list_local,
2412 &has_retval, err, 0, TCPRS_CB_NORMAL,
2413 0, 0, 0, &type_list);
2414 }
2415
2416 /* if that failed, do not go on */
2417 if (*err || (code_body == 0 && !is_extern))
2418 return 0;
2419
2420 decl_func:
2421 /* if we have a type list, generate the decorated name */
2422 if (type_list != 0)
2423 {
2424 /*
2425 * The function we're defining here will go by a decorated
2426 * name, not the base name, since it's a particular version of
2427 * this multi-method. So, we need to replace the function name
2428 * token with the decorated form, which incorporates the typed
2429 * parameter list to distinguish this version from other
2430 * multi-methods with the same base name and different
2431 * parameter type lists.
2432 *
2433 * Before we generate the decorated name, though, define the
2434 * base name as an external multi-method function, so that
2435 * we'll know what to do when we see calls to the base
2436 * function. We define the base name as external because the
2437 * current source code isn't actually a definition of the base
2438 * name; the base name refers to an implied function that's not
2439 * actually defined anywhere in the source code, but rather is
2440 * generated automatically by the compiler. As such, the
2441 * source we're compiling effectively declares the existence of
2442 * a function with the base name, but says that it will be
2443 * defined elsewhere - this is, of course, the exact purpose of
2444 * an external declaration.
2445 *
2446 * So, if the base name isn't yet defined, define it as an
2447 * external function, and mark it as a multi-method. Start by
2448 * looking up the base function name.
2449 */
2450 func_sym = (CTcSymFunc *)global_symtab_->find_delete_weak(
2451 tok.get_text(), tok.get_text_len());
2452
2453 /*
2454 * if it's defined, make sure the existing definition is
2455 * compatible; if it's not defined, define it now
2456 */
2457 if (func_sym != 0)
2458 {
2459 /*
2460 * it's previously defined: to be compatible, it has to be
2461 * an external, multi-method function
2462 */
2463 if (func_sym->get_type() != TC_SYM_FUNC)
2464 {
2465 /* it's already defined as a non-function */
2466 G_tcmain->log_error(
2467 sym_file, sym_linenum,
2468 TC_SEV_ERROR, TCERR_REDEF_AS_FUNC,
2469 (int)tok.get_text_len(), tok.get_text());
2470 }
2471 else if (!(func_sym->is_extern()
2472 && func_sym->is_multimethod()))
2473 {
2474 /* it's already defined incompatibly - flag the error */
2475 G_tcmain->log_error(
2476 sym_file, sym_linenum,
2477 TC_SEV_ERROR, TCERR_FUNC_REDEF_AS_MULTIMETHOD,
2478 (int)tok.get_text_len(), tok.get_text());
2479
2480 /* ignore the redefinition */
2481 return 0;
2482 }
2483 }
2484 else
2485 {
2486 /*
2487 * It's not defined yet, so add it as an external
2488 * multi-method function. This base form is always a
2489 * varargs function with no fixed arguments, since the
2490 * parameter lists of the different definitions can vary.
2491 * We'll likewise assume that there's a return value, since
2492 * some instances might have returns.
2493 */
2494 func_sym = new CTcSymFunc(tok.get_text(), tok.get_text_len(),
2495 FALSE, 0, 0, TRUE, TRUE,
2496 TRUE, TRUE, TRUE, TRUE);
2497
2498 /*
2499 * Mark the function symbol as defined in this file. This
2500 * will ensure that we export it to our symbol file, even
2501 * though it's defined here as an extern symbol. We don't
2502 * normally export externs, since we'd simply count on the
2503 * defining file to export them; but the base symbol for a
2504 * multi-method has no definer at all until link time, so
2505 * *someone* needs to export it. So export it from any
2506 * file that defines a multi-method with the base name.
2507 */
2508 func_sym->set_mm_def(TRUE);
2509
2510 /* add it to the symbol table */
2511 global_symtab_->add_entry(func_sym);
2512 }
2513
2514 /*
2515 * Now we can generate the decorated name. This name
2516 * distinguishes this type-list version of the function from
2517 * other versions with the same base name, and is the name by
2518 * which the actual function definition will be known. We'll
2519 * also operate on the decorated name for 'replace' or
2520 * 'modify', since what we're replacing or modifying is this
2521 * specific type-list version of the function.
2522 */
2523 type_list->decorate_name(&tok, &tok);
2524 }
2525
2526 /* find any existing function symbol */
2527 func_sym = (CTcSymFunc *)global_symtab_->find_delete_weak(
2528 tok.get_text(), tok.get_text_len());
2529
2530 /*
2531 * If the symbol was already defined, the previous definition must
2532 * be external, or this new definition must be external, or the new
2533 * definition must be a 'replace' definition.
2534 */
2535 redef = (func_sym != 0
2536 && (func_sym->get_type() != TC_SYM_FUNC
2537 || (!func_sym->is_extern()
2538 && !is_extern && !replace && !modify)));
2539
2540 /*
2541 * In addition, if both function definitions have prototypes, the
2542 * prototypes must match.
2543 */
2544 if (has_proto && func_sym != 0 && func_sym->has_proto())
2545 {
2546 redef |= (func_sym->get_argc() != argc
2547 || func_sym->is_varargs() != varargs
2548 || func_sym->is_multimethod() != (type_list != 0));
2549 }
2550
2551 /*
2552 * If the symbol was already defined, log an error, then ignore
2553 * the redefinition and return an empty statement. Note that we
2554 * waited until now because we still wanted to parse the syntax
2555 * of the function, but we can't really do anything with it once
2556 * we're finished with the parsing.
2557 */
2558 if (redef)
2559 {
2560 int err;
2561
2562 /*
2563 * Note the problem. Select the error message to display
2564 * depending on whether the symbol was previously defined as
2565 * a function (in which case the problem is the conflicting
2566 * prototype) or as another type.
2567 */
2568 if (func_sym->get_type() == TC_SYM_FUNC)
2569 {
2570 /*
2571 * if one or the other definition is external, or we're
2572 * replacing the function, the problem is a mismatch in
2573 * the parameter lists; otherwise, the problem is that
2574 * the function is simply defined twice
2575 */
2576 if (func_sym->is_extern() || is_extern || replace || modify)
2577 err = TCERR_INCOMPAT_FUNC_REDEF;
2578 else
2579 err = TCERR_FUNC_REDEF;
2580 }
2581 else
2582 {
2583 /* it's being redefined as a new type */
2584 err = TCERR_REDEF_AS_FUNC;
2585 }
2586
2587 /* log the error (at the symbol's position) */
2588 G_tcmain->log_error(sym_file, sym_linenum, TC_SEV_ERROR, err,
2589 (int)tok.get_text_len(), tok.get_text());
2590
2591 /* ignore the re-definition */
2592 return 0;
2593 }
2594
2595 /*
2596 * create a symbol table entry for the function if we didn't
2597 * have on already
2598 */
2599 if (func_sym == 0)
2600 {
2601 /* we didn't have a symbol previously - create one */
2602 func_sym = new CTcSymFunc(tok.get_text(), tok.get_text_len(),
2603 FALSE, argc, opt_argc, varargs,
2604 has_retval, type_list != 0,
2605 FALSE, is_extern, has_proto);
2606
2607 /* add the entry to the global symbol table */
2608 global_symtab_->add_entry(func_sym);
2609
2610 /*
2611 * We can't replace/modify a function that wasn't previously
2612 * defined - if we're replacing it, note the error. Don't
2613 * bother with this check if we're parsing only for syntax.
2614 */
2615 if ((replace || modify) && !syntax_only_)
2616 G_tok->log_error(TCERR_REPFUNC_UNDEF);
2617 }
2618 else
2619 {
2620 /* replace or modify the previous definition if appropriate */
2621 if (replace)
2622 {
2623 /*
2624 * check to see whether it's external or previously
2625 * defined in the same translation unit
2626 */
2627 if (func_sym->is_extern())
2628 {
2629 /*
2630 * The function was previously external, and we're
2631 * replacing it -- mark it as such so that we'll
2632 * know to perform the replacement at link time. We
2633 * only need to do this when replacing an external
2634 * function, because if the function is defined in
2635 * the same translation unit then the replacement
2636 * will be completed right now, and we don't need to
2637 * do any more work at link time.
2638 */
2639 func_sym->set_ext_replace(TRUE);
2640 }
2641 else
2642 {
2643 CTcSymFunc *mod_base;
2644
2645 /*
2646 * The function was previously defined in the same
2647 * translation unit. Mark the previous code body as
2648 * replaced so that we don't bother generating any
2649 * code for it.
2650 */
2651 if (func_sym->get_code_body() != 0)
2652 func_sym->get_code_body()->set_replaced(TRUE);
2653
2654 /*
2655 * If the function we're replacing modifies any base
2656 * functions, we're effectively replacing all of the
2657 * base functions as well. None of these functions
2658 * will be reachable, since a modified base function is
2659 * only reachable through its modifying function; since
2660 * the function we're replacing is becoming
2661 * unreachable, therefore, all of its base functions
2662 * are as well.
2663 */
2664 for (mod_base = func_sym->get_mod_base() ; mod_base != 0 ;
2665 mod_base = mod_base->get_mod_base())
2666 {
2667 /* mark this base function as replaced */
2668 if (mod_base->is_extern())
2669 {
2670 /* mark it as a replaced extern */
2671 mod_base->set_ext_replace(TRUE);
2672
2673 /* note that our function replaces an external */
2674 func_sym->set_ext_replace(TRUE);
2675 }
2676 else if (mod_base->get_code_body() != 0)
2677 {
2678 /* mark its code body as replaced */
2679 mod_base->get_code_body()->set_replaced(TRUE);
2680 }
2681 }
2682
2683 /*
2684 * since we're replacing the function, it no longer
2685 * has any modified base functions
2686 */
2687 func_sym->set_mod_base(0);
2688 }
2689 }
2690 else if (modify)
2691 {
2692 CTcSymFunc *mod_sym;
2693
2694 /*
2695 * Create a new dummy symbol for the original version.
2696 * This symbol doesn't go in the symbol table; instead, we
2697 * link it into our modified base function list behind the
2698 * symbol table entry.
2699 *
2700 * The name is important, though: we have to give this an
2701 * empty name so that the object file reader will know that
2702 * this isn't an actual global symbol. This will let the
2703 * object file loader's code stream reader know that it
2704 * can't fix up references to the code stream anchor by
2705 * looking at the global symbol table. (We have to write
2706 * some extra code in the function symbol loader to deal
2707 * with this, since the code stream reader can't.)
2708 *
2709 * Note that we want the new base function symbol to keep
2710 * the same 'extern' attribute as the original symbol. If
2711 * we're modifying an external function, this will allow us
2712 * to apply the modification at link time between this
2713 * module and the module where the imported original
2714 * function was defined.
2715 */
2716 mod_sym = new CTcSymFunc("", 0, FALSE,
2717 argc, opt_argc, varargs, has_retval,
2718 func_sym->is_multimethod(),
2719 func_sym->is_multimethod_base(),
2720 func_sym->is_extern(),
2721 func_sym->has_proto());
2722
2723 /*
2724 * remember the original global function in the modified
2725 * placeholder symbol
2726 */
2727 mod_sym->set_mod_global(func_sym);
2728
2729 /*
2730 * If the original has a code body, switch the code body to
2731 * use our new symbol's fixup list.
2732 *
2733 * The only way to reference the original code body is
2734 * through a 'replaced()' call in the modifying function,
2735 * so any fixups currently associated with the function
2736 * symbol should remain with the function symbol - in other
2737 * words, we don't have to move any existing fixups
2738 * anywhere.
2739 */
2740 if (func_sym->get_code_body() != 0)
2741 func_sym->get_code_body()->set_symbol_fixup_list(
2742 mod_sym, mod_sym->get_fixup_list_anchor());
2743
2744 /* transfer the old code body to the modified base symbol */
2745 mod_sym->set_code_body(func_sym->get_code_body());
2746
2747 /*
2748 * put the new modified base function at the head of the
2749 * modified base list
2750 */
2751 mod_sym->set_mod_base(func_sym->get_mod_base());
2752 func_sym->set_mod_base(mod_sym);
2753 }
2754
2755 /*
2756 * The function was already defined. If this is an actual
2757 * definition (not an 'extern' declaration), then clear the
2758 * 'extern' flag on the existing symbol.
2759 */
2760 if (!is_extern)
2761 func_sym->set_extern(FALSE);
2762 }
2763
2764 /*
2765 * If there's a code body, tell the code body about its symbol
2766 * table entry. (If the function is 'extern' it has no code
2767 * body at this point.)
2768 */
2769 if (code_body != 0)
2770 code_body->set_symbol_fixup_list(
2771 func_sym, func_sym->get_fixup_list_anchor());
2772
2773 /* tell the function about its code body */
2774 func_sym->set_code_body(code_body);
2775
2776 /* return the code body as the result */
2777 return code_body;
2778
2779 default:
2780 /* log an error and continue from here */
2781 G_tok->log_error_curtok(TCERR_REQ_CODE_BODY);
2782 return 0;
2783 }
2784 }
2785
2786 /* ------------------------------------------------------------------------ */
2787 /*
2788 * Parse an 'intrinsic' top-level statement
2789 */
2790 CTPNStmTop *CTcParser::parse_intrinsic(int *err)
2791 {
2792 int list_done;
2793 ushort fn_set_id;
2794 ushort fn_set_idx;
2795 tc_toktyp_t tok;
2796
2797 /* skip the 'intrinsic' keyword, and check for the function set name */
2798 if ((tok = G_tok->next()) == TOKT_SSTR)
2799 {
2800 /* tell the code generator to add this function set */
2801 fn_set_id = G_cg->add_fnset(G_tok->getcur()->get_text(),
2802 G_tok->getcur()->get_text_len());
2803
2804 /* skip the name */
2805 G_tok->next();
2806 }
2807 else if (tok == TOKT_CLASS)
2808 {
2809 /* it's an intrinsic class (metaclass) definition - go parse it */
2810 return parse_intrinsic_class(err);
2811 }
2812 else
2813 {
2814 /*
2815 * note the error, then keep going, assuming that the name was
2816 * missing but that the rest is correctly formed
2817 */
2818 G_tok->log_error_curtok(TCERR_REQ_INTRINS_NAME);
2819
2820 /* use an arbitrary function set ID, since we're failing */
2821 fn_set_id = 0;
2822 }
2823
2824 /* we need an open brace after that */
2825 if (G_tok->cur() == TOKT_LBRACE)
2826 {
2827 /* skip the brace */
2828 G_tok->next();
2829 }
2830 else
2831 {
2832 /* note the error, but keep going */
2833 G_tok->log_error_curtok(TCERR_REQ_INTRINS_LBRACE);
2834 }
2835
2836 /* keep going until we find the closing brace */
2837 for (list_done = FALSE, fn_set_idx = 0 ; !list_done ; )
2838 {
2839 CTcToken fn_tok;
2840 int argc;
2841 int optargc;
2842 int varargs;
2843 int varargs_list;
2844 CTcSymLocal *varargs_list_local;
2845 CTcSymBif *bif_sym;
2846
2847 /* check what we have */
2848 switch(G_tok->cur())
2849 {
2850 case TOKT_RBRACE:
2851 /* closing brace - skip it, and we're done */
2852 G_tok->next();
2853 list_done = TRUE;
2854 break;
2855
2856 case TOKT_EOF:
2857 /* end of file - log an error */
2858 G_tok->log_error(TCERR_EOF_IN_INTRINS);
2859
2860 /* return failure */
2861 *err = TRUE;
2862 return 0;
2863
2864 case TOKT_SEM:
2865 /* empty list element; skip it */
2866 G_tok->next();
2867 break;
2868
2869 case TOKT_SYM:
2870 /* remember the function name */
2871 fn_tok = *G_tok->copycur();
2872
2873 /* the next token must be an open paren */
2874 if (G_tok->next() == TOKT_LPAR)
2875 {
2876 /* skip the paren */
2877 G_tok->next();
2878 }
2879 else
2880 {
2881 /* log an error, and assume the parent was simply missing */
2882 G_tok->log_error_curtok(TCERR_REQ_INTRINS_LPAR);
2883 }
2884
2885 /*
2886 * parse the formals - igore the names (hence 'count_only' =
2887 * true), and allow optional arguments
2888 */
2889 parse_formal_list(TRUE, TRUE, &argc, &optargc, &varargs,
2890 &varargs_list, &varargs_list_local, err, 0,
2891 FALSE, 0);
2892 if (*err)
2893 return 0;
2894
2895 /* require the closing semicolon */
2896 if (parse_req_sem())
2897 {
2898 *err = TRUE;
2899 return 0;
2900 }
2901
2902 /* add a global symbol table entry for the function */
2903 bif_sym = new CTcSymBif(fn_tok.get_text(),
2904 fn_tok.get_text_len(), FALSE,
2905 fn_set_id, fn_set_idx, TRUE,
2906 argc, argc + optargc, varargs);
2907 global_symtab_->add_entry(bif_sym);
2908
2909 /* count the function set entry */
2910 ++fn_set_idx;
2911
2912 /* done with this function */
2913 break;
2914
2915 default:
2916 /* anything else is an error */
2917 G_tok->log_error_curtok(TCERR_REQ_INTRINS_SYM);
2918
2919 /* skip the errant token and proceed */
2920 G_tok->next();
2921 break;
2922 }
2923 }
2924
2925 /*
2926 * there's no node to return - a function set doesn't generate any
2927 * code, but simply adds entries to the symbol table, which we've
2928 * already done
2929 */
2930 return 0;
2931 }
2932
2933 /* ------------------------------------------------------------------------ */
2934 /*
2935 * Parse an 'intrinsic class' definition
2936 */
2937 CTPNStmTop *CTcParser::parse_intrinsic_class(int *err)
2938 {
2939 int meta_id;
2940 int list_done;
2941 int got_name_tok = FALSE;
2942 CTcToken meta_tok;
2943 CTcSymMetaclass *meta_sym = 0;
2944
2945 /* skip the 'class' keyword and check the class name symbol */
2946 if (G_tok->next() == TOKT_SYM)
2947 {
2948 /* get the token */
2949 meta_tok = *G_tok->copycur();
2950
2951 /*
2952 * See if it's defined yet. If it was previously defined as an
2953 * external metaclass,
2954 */
2955 meta_sym = (CTcSymMetaclass *)global_symtab_->find(
2956 meta_tok.get_text(), meta_tok.get_text_len());
2957 if (meta_sym != 0
2958 && (meta_sym->get_type() != TC_SYM_METACLASS
2959 || !meta_sym->is_ext()))
2960 {
2961 /* duplicate definition - log an error and forget the symbol */
2962 G_tok->log_error_curtok(TCERR_REDEF_INTRINS_NAME);
2963 meta_sym = 0;
2964 }
2965 else
2966 {
2967 /* note that we successfully parsed the class name token */
2968 got_name_tok = TRUE;
2969 }
2970
2971 /* move on to the next token */
2972 G_tok->next();
2973 }
2974 else
2975 {
2976 /*
2977 * note the error, then keep going, assuming the name was
2978 * missing but that the rest is well-formed
2979 */
2980 G_tok->log_error_curtok(TCERR_REQ_INTRINS_CLASS_NAME_SYM);
2981 }
2982
2983 /* skip the name symbol, and check for the global name string */
2984 if (G_tok->cur() == TOKT_SSTR)
2985 {
2986 /* set up the definitions if we got a valid name token */
2987 if (got_name_tok)
2988 {
2989 /* if we don't already have a symbol table entry, create one */
2990 if (meta_sym == 0)
2991 {
2992 /* no prior symbol table entry - create it and add it */
2993 meta_sym = new CTcSymMetaclass(
2994 meta_tok.get_text(), meta_tok.get_text_len(),
2995 FALSE, 0, G_cg->new_obj_id());
2996 global_symtab_->add_entry(meta_sym);
2997 }
2998 else
2999 {
3000 /*
3001 * we have a prior definition, which must have been an
3002 * external import from a symbol file; remove the external
3003 * attribute since we're actually defining it now
3004 */
3005 meta_sym->set_ext(FALSE);
3006 }
3007
3008 /* tell the code generator to add this metaclass */
3009 meta_id = G_cg->find_or_add_meta(
3010 G_tok->getcur()->get_text(), G_tok->getcur()->get_text_len(),
3011 meta_sym);
3012
3013 /*
3014 * if the metaclass was already defined for another symbol,
3015 * it's an error
3016 */
3017 CTcSymMetaclass *table_sym = G_cg->get_meta_sym(meta_id);
3018 if (table_sym != meta_sym)
3019 G_tok->log_error(TCERR_META_ALREADY_DEF,
3020 (int)table_sym->get_sym_len(),
3021 table_sym->get_sym());
3022
3023 /* set the metaclass ID in the symbol */
3024 meta_sym->set_meta_idx(meta_id);
3025 }
3026
3027 /* skip the name */
3028 G_tok->next();
3029 }
3030 else
3031 {
3032 /*
3033 * note the error, then keep going, assuming that the name was
3034 * missing but that the rest is correctly formed
3035 */
3036 G_tok->log_error_curtok(TCERR_REQ_INTRINS_CLASS_NAME);
3037 }
3038
3039 /* check for the optional intrinsic superclass */
3040 if (G_tok->cur() == TOKT_COLON)
3041 {
3042 /* skip the colon and get the symbol */
3043 if (G_tok->next() != TOKT_SYM)
3044 {
3045 /* note the error, and just continue from here */
3046 G_tok->log_error_curtok(TCERR_REQ_INTRINS_SUPERCLASS_NAME);
3047 }
3048 else
3049 {
3050 /* look up the superclass in the global symbols */
3051 CTcSymMetaclass *sc_meta_sym =
3052 (CTcSymMetaclass *)global_symtab_->find(
3053 G_tok->getcur()->get_text(),
3054 G_tok->getcur()->get_text_len());
3055
3056 /* make sure we found the symbol, and that it's a metaclass */
3057 if (sc_meta_sym == 0)
3058 {
3059 /* the intrinsic superclass is not defined */
3060 G_tok->log_error_curtok(TCERR_INTRINS_SUPERCLASS_UNDEF);
3061 }
3062 else if (sc_meta_sym->get_type() != TC_SYM_METACLASS)
3063 {
3064 /* this is not an intrinsic class */
3065 G_tok->log_error_curtok(TCERR_INTRINS_SUPERCLASS_NOT_INTRINS);
3066 }
3067 else
3068 {
3069 /* remember the supermetaclass */
3070 if (meta_sym != 0)
3071 meta_sym->set_super_meta(sc_meta_sym);
3072 }
3073
3074 /* we're done with the name token */
3075 G_tok->next();
3076 }
3077 }
3078
3079 /* we need an open brace after that */
3080 if (G_tok->cur() == TOKT_LBRACE)
3081 {
3082 /* skip the brace */
3083 G_tok->next();
3084 }
3085 else
3086 {
3087 /* note the error, but keep going */
3088 G_tok->log_error_curtok(TCERR_REQ_INTRINS_CLASS_LBRACE);
3089 }
3090
3091 /* keep going until we find the closing brace */
3092 for (list_done = FALSE ; !list_done ; )
3093 {
3094 CTcToken prop_tok;
3095
3096 /* presume it won't be a 'static' property */
3097 int is_static = FALSE;
3098
3099 /* check what we have */
3100 switch(G_tok->cur())
3101 {
3102 case TOKT_RBRACE:
3103 /* closing brace - skip it, and we're done */
3104 G_tok->next();
3105 list_done = TRUE;
3106 break;
3107
3108 case TOKT_EOF:
3109 /* end of file - log an error */
3110 G_tok->log_error(TCERR_EOF_IN_INTRINS_CLASS);
3111
3112 /* return failure */
3113 *err = TRUE;
3114 return 0;
3115
3116 case TOKT_SEM:
3117 /* empty list element; skip it */
3118 G_tok->next();
3119 break;
3120
3121 case TOKT_STATIC:
3122 /* note that we have a static property */
3123 is_static = TRUE;
3124
3125 /* a symbol must follow */
3126 if (G_tok->next() != TOKT_SYM)
3127 {
3128 G_tok->log_error(TCERR_REQ_INTRINS_CLASS_PROP);
3129 break;
3130 }
3131
3132 /* fall through to parse the rest of the property definition */
3133
3134 case TOKT_SYM:
3135 /* remember the property name */
3136 prop_tok = *G_tok->copycur();
3137
3138 /* add the property */
3139 if (meta_sym != 0)
3140 meta_sym->add_prop(prop_tok.get_text(),
3141 prop_tok.get_text_len(), 0, is_static);
3142
3143 /* skip the property name symbol */
3144 G_tok->next();
3145
3146 /* if there's a formal parameter list, parse it */
3147 if (G_tok->cur() == TOKT_LPAR)
3148 {
3149 int argc;
3150 int opt_argc;
3151 int varargs;
3152 int varargs_list;
3153 CTcSymLocal *varargs_list_local;
3154
3155 /* skip the open paren */
3156 G_tok->next();
3157
3158 /* parse the list, ignoring the symbols defined */
3159 parse_formal_list(TRUE, TRUE, &argc, &opt_argc, &varargs,
3160 &varargs_list, &varargs_list_local,
3161 err, 0, FALSE, 0);
3162
3163 /* if a fatal error occurred, return failure */
3164 if (*err != 0)
3165 return 0;
3166 }
3167
3168 /* require a semicolon after the property definition */
3169 if (parse_req_sem())
3170 {
3171 *err = TRUE;
3172 return 0;
3173 }
3174
3175 /* done with this function */
3176 break;
3177
3178 default:
3179 /* anything else is an error */
3180 G_tok->log_error_curtok(TCERR_REQ_INTRINS_CLASS_PROP);
3181
3182 /* skip the errant token and proceed */
3183 G_tok->next();
3184 break;
3185 }
3186 }
3187
3188 /*
3189 * there's no node to return - a metaclass definition doesn't
3190 * generate any code, but simply adds entries to the symbol table,
3191 * which we've already done
3192 */
3193 return 0;
3194 }
3195
3196 /* ------------------------------------------------------------------------ */
3197 /*
3198 * Parse an 'object' statement - this can be either an 'object template'
3199 * statement or an anonymous object definition for a base object.
3200 */
3201 CTPNStmTop *CTcParser::parse_object_stm(int *err, int trans)
3202 {
3203 /* it's either a template definition or an anonymous object */
3204 if (G_tok->next() == TOKT_TEMPLATE)
3205 {
3206 /*
3207 * it's an 'object template' statement - go parse the template,
3208 * with no superclass token
3209 */
3210 return parse_template_def(err, 0);
3211 }
3212 else
3213 {
3214 /*
3215 * no 'template' token, so this is an anonymous object instance
3216 * definition - put back the 'object' token
3217 */
3218 G_tok->unget();
3219
3220 /* parse the anonymous object statement */
3221 return parse_anon_object(err, 0, FALSE, 0, trans);
3222 }
3223 }
3224
3225 /* ------------------------------------------------------------------------ */
3226 /*
3227 * Parse an object template definition. If the class token is null, this
3228 * is a root object template; otherwise, the class token gives the class
3229 * symbol with which to associate the template.
3230 */
3231 CTPNStmTop *CTcParser::parse_template_def(int *err, const CTcToken *class_tok)
3232 {
3233 CTcToken prop_tok;
3234 CTcObjTemplateItem *alt_group_head;
3235 CTcObjTemplateItem *item_head;
3236 CTcObjTemplateItem *item_tail;
3237 CTcSymObj *class_sym;
3238
3239 /* no items in our list yet */
3240 item_head = item_tail = alt_group_head = 0;
3241 size_t item_cnt = 0;
3242
3243 /* presume we won't find an 'inherited' token */
3244 int found_inh = FALSE;
3245
3246 /*
3247 * If there's a class token, it must refer to an object class. If
3248 * there's no such symbol defined yet, define it as an external
3249 * object; otherwise, make sure the existing symbol is an object of
3250 * metaclass tads-object.
3251 */
3252 if (class_tok != 0)
3253 {
3254 /*
3255 * if the class token is 'string', it's a string template rather
3256 * than an object template - go parse that separately if so
3257 */
3258 if (class_tok->text_matches("string"))
3259 return parse_string_template_def(err);
3260
3261 /*
3262 * Look up the symbol. Don't mark the symbol as referenced; merely
3263 * defining a template for an object doesn't require an external
3264 * reference on the object.
3265 */
3266 class_sym = (CTcSymObj *)get_global_symtab()->find_noref(
3267 class_tok->get_text(), class_tok->get_text_len(), 0);
3268
3269 /*
3270 * if we didn't find it, define it; otherwise, ensure it's defined
3271 * as an object
3272 */
3273 if (class_sym == 0)
3274 {
3275 /*
3276 * it's undefined, so add a forward definition by creating the
3277 * symbol as an external object
3278 */
3279 class_sym = new CTcSymObj(class_tok->get_text(),
3280 class_tok->get_text_len(), FALSE,
3281 G_cg->new_obj_id(), TRUE,
3282 TC_META_TADSOBJ, 0);
3283
3284 /* add it to the master symbol table */
3285 get_global_symtab()->add_entry(class_sym);
3286 }
3287 else if (class_sym->get_type() != TC_SYM_OBJ
3288 || class_sym->get_metaclass() != TC_META_TADSOBJ)
3289 {
3290 /* it's defined incorrectly - flag the error */
3291 G_tok->log_error_curtok(TCERR_REDEF_AS_OBJ);
3292
3293 /* forget the conflicting symbol and proceed with parsing */
3294 class_sym = 0;
3295 }
3296 }
3297 else
3298 {
3299 /* this is a root object template - there's no class symbol */
3300 class_sym = 0;
3301 }
3302
3303 /* move on to the next token */
3304 G_tok->next();
3305
3306 /* keep going until we run out of template tokens */
3307 int all_ok = TRUE;
3308 for (int done = FALSE ; !done ; )
3309 {
3310 tc_toktyp_t def_tok;
3311 int ok;
3312 int is_inh;
3313 int is_alt, is_opt;
3314
3315 /* presume we will find a valid token */
3316 ok = TRUE;
3317
3318 /* presume the defining token type is the current token */
3319 def_tok = G_tok->cur();
3320
3321 /* presume this won't be an 'inherited' token */
3322 is_inh = FALSE;
3323
3324 /* see what we have next */
3325 switch(G_tok->cur())
3326 {
3327 case TOKT_SEM:
3328 /*
3329 * that's the end of the statement - skip the semicolon, and
3330 * we're done
3331 */
3332 G_tok->next();
3333 done = TRUE;
3334 break;
3335
3336 case TOKT_INHERITED:
3337 /* flag that we are to inherit superclass templates */
3338 is_inh = TRUE;
3339 found_inh = TRUE;
3340 break;
3341
3342 case TOKT_SSTR:
3343 case TOKT_DSTR:
3344 /* string property */
3345 {
3346 utf8_ptr p;
3347 size_t rem;
3348 int valid;
3349 tc_toktyp_t kwtok;
3350
3351 /* this is our property name token */
3352 prop_tok = *G_tok->copycur();
3353
3354 /*
3355 * this is also our defining token - the actual value in
3356 * object instances will use this string type
3357 */
3358 def_tok = G_tok->cur();
3359
3360 /*
3361 * make sure that the contents of the string forms a
3362 * valid symbol token
3363 */
3364
3365 /* set up at the start of the token */
3366 p.set((char *)G_tok->getcur()->get_text());
3367 rem = G_tok->getcur()->get_text_len();
3368
3369 /* make sure the first character is valid */
3370 valid = (rem != 0 && is_syminit(p.getch()));
3371
3372 /* skip the first character */
3373 if (rem != 0)
3374 p.inc(&rem);
3375
3376 /* scan the rest of the string */
3377 for ( ; rem != 0 && valid ; p.inc(&rem))
3378 {
3379 /*
3380 * if this isn't a valid symbol character, we don't
3381 * have a valid symbol
3382 */
3383 if (!is_sym(p.getch()))
3384 valid = FALSE;
3385 }
3386
3387 /*
3388 * if the string is lexically valid as a symbol, make
3389 * sure it's not a keyword - if it is, it can't be a
3390 * property name
3391 */
3392 if (valid && G_tok->look_up_keyword(G_tok->getcur(), &kwtok))
3393 valid = FALSE;
3394
3395 /* if the symbol isn't valid, it's an error */
3396 if (!valid)
3397 {
3398 /* log the error */
3399 G_tok->log_error_curtok(TCERR_OBJ_TPL_STR_REQ_PROP);
3400
3401 /* note the problem */
3402 ok = FALSE;
3403 }
3404 }
3405 break;
3406
3407 case TOKT_LBRACK:
3408 /* list property - defining instances will use lists here */
3409 def_tok = G_tok->cur();
3410
3411 /* the next token must be the property name */
3412 if (G_tok->next() != TOKT_SYM)
3413 {
3414 /* log an error */
3415 G_tok->log_error(TCERR_OBJ_TPL_OP_REQ_PROP,
3416 G_tok->get_op_text(def_tok),
3417 (int)G_tok->getcur()->get_text_len(),
3418 G_tok->getcur()->get_text());
3419
3420 /* note that we don't have a valid token */
3421 ok = FALSE;
3422
3423 /*
3424 * if this token isn't ']' or something that looks like
3425 * an end-of-statement token, skip it
3426 */
3427 switch(G_tok->cur())
3428 {
3429 case TOKT_SEM:
3430 case TOKT_EOF:
3431 case TOKT_LBRACE:
3432 case TOKT_RBRACE:
3433 /*
3434 * don't skip any of these - they might be statement
3435 * enders
3436 */
3437 break;
3438
3439 case TOKT_RBRACK:
3440 /*
3441 * they must simply have left out the property - don't
3442 * skip this, since we'll skip a token shortly anyway
3443 */
3444 break;
3445
3446 default:
3447 /* skip the errant token */
3448 G_tok->next();
3449 break;
3450 }
3451 }
3452 else
3453 {
3454 /* remember the property token */
3455 prop_tok = *G_tok->copycur();
3456
3457 /* require the ']' token */
3458 if (G_tok->next() != TOKT_RBRACK)
3459 {
3460 /* log the error */
3461 G_tok->log_error_curtok(TCERR_OBJ_TPL_REQ_RBRACK);
3462
3463 /*
3464 * proceed on the assumption that they simply left
3465 * out the ']'
3466 */
3467 }
3468 }
3469 break;
3470
3471 case TOKT_AT:
3472 case TOKT_PLUS:
3473 case TOKT_MINUS:
3474 case TOKT_TIMES:
3475 case TOKT_DIV:
3476 case TOKT_MOD:
3477 case TOKT_ARROW:
3478 case TOKT_AND:
3479 case TOKT_NOT:
3480 case TOKT_BNOT:
3481 case TOKT_COMMA:
3482 /* the operator is the defining token */
3483 def_tok = G_tok->cur();
3484
3485 /* the next token must be the property name */
3486 if (G_tok->next() != TOKT_SYM)
3487 {
3488 /* log an error */
3489 G_tok->log_error(TCERR_OBJ_TPL_OP_REQ_PROP,
3490 G_tok->get_op_text(def_tok),
3491 (int)G_tok->getcur()->get_text_len(),
3492 G_tok->getcur()->get_text());
3493
3494 /* note that we don't have a valid token */
3495 ok = FALSE;
3496 }
3497
3498 /* remember the property token */
3499 prop_tok = *G_tok->copycur();
3500 break;
3501
3502 case TOKT_LBRACE:
3503 case TOKT_RBRACE:
3504 case TOKT_EOF:
3505 case TOKT_OBJECT:
3506 /* they must have left off the ';' */
3507 G_tok->log_error_curtok(TCERR_OBJ_TPL_BAD_TOK);
3508
3509 /*
3510 * stop parsing here, assuming the statement should have
3511 * ended by now
3512 */
3513 done = TRUE;
3514 ok = FALSE;
3515 break;
3516
3517 default:
3518 /* log an error and skip the invalid token */
3519 G_tok->log_error_curtok(TCERR_OBJ_TPL_BAD_TOK);
3520 G_tok->next();
3521 ok = FALSE;
3522 break;
3523 }
3524
3525 /* presume we won't find an alternative or optionality suffix */
3526 is_opt = FALSE;
3527 is_alt = FALSE;
3528
3529 /*
3530 * if we haven't reached the end of the statement, check to see if
3531 * the item is optional or an alternative
3532 */
3533 if (!done)
3534 {
3535 /* get the next token */
3536 G_tok->next();
3537
3538 /* check to see if the item is optional */
3539 if (G_tok->cur() == TOKT_QUESTION)
3540 {
3541 /* it's optional */
3542 is_opt = TRUE;
3543
3544 /* skip the '?' */
3545 G_tok->next();
3546 }
3547
3548 /* check for an alternative */
3549 if (G_tok->cur() == TOKT_OR)
3550 {
3551 /* this item is an alternative */
3552 is_alt = TRUE;
3553
3554 /* skip the '|' */
3555 G_tok->next();
3556 }
3557 }
3558
3559 /*
3560 * If the previous item was followed by '|', we're in the same
3561 * alternative group with that item. This means we're optional if
3562 * it's marked as optional, and all of the preceding items in our
3563 * group are optional if we're marked as optional.
3564 */
3565 if (item_tail != 0 && item_tail->is_alt_)
3566 {
3567 /*
3568 * if we're optional, mark all prior items in the group as
3569 * optional
3570 */
3571 if (is_opt)
3572 {
3573 CTcObjTemplateItem *item;
3574
3575 /* mark everything in our group as optional */
3576 for (item = alt_group_head ; item != 0 ; item = item->nxt_)
3577 item->is_opt_ = TRUE;
3578 }
3579
3580 /* if the prior item was optional, we're optional */
3581 if (item_tail->is_opt_)
3582 is_opt = TRUE;
3583 }
3584
3585 /*
3586 * if we encountered any problems, note that we have an error in
3587 * the overall statement
3588 */
3589 if (!ok)
3590 all_ok = FALSE;
3591
3592 /* if we found a valid property token, add it to the template */
3593 if (!done && ok)
3594 {
3595 CTcSymProp *prop_sym;
3596 CTcObjTemplateItem *item;
3597
3598 /* presume we won't create a new item */
3599 item = 0;
3600
3601 /* check to see if it's a property or an 'inherited' token */
3602 if (is_inh)
3603 {
3604 /* create an 'inherited' template item */
3605 item = new (G_prsmem)
3606 CTcObjTemplateItem(0, TOKT_INHERITED, FALSE, FALSE);
3607 }
3608 else
3609 {
3610 /* make sure we have a valid property name */
3611 prop_sym = look_up_prop(&prop_tok, FALSE);
3612 if (prop_sym == 0)
3613 {
3614 /* couldn't find the property - log an error */
3615 G_tok->log_error(TCERR_OBJ_TPL_SYM_NOT_PROP,
3616 (int)prop_tok.get_text_len(),
3617 prop_tok.get_text());
3618 }
3619 else if (prop_sym->is_vocab())
3620 {
3621 /* dictionary properties are not valid in templates */
3622 G_tok->log_error(TCERR_OBJ_TPL_NO_VOCAB,
3623 (int)prop_sym->get_sym_len(),
3624 prop_sym->get_sym());
3625 }
3626 else
3627 {
3628 /*
3629 * Scan the list so far to ensure that this same
3630 * property isn't already part of the list. However,
3631 * do allow duplicates within the run of alternatives
3632 * of which the new item will be a part, since we'll
3633 * only end up using one of the alternatives and hence
3634 * the property will only be entered once in the
3635 * property even if it appears multiple times in the
3636 * run. The run of alternatives of which we're a part
3637 * is the final run in the list, since we're being
3638 * added at the end of the list.
3639 */
3640 for (item = item_head ; item != 0 ; item = item->nxt_)
3641 {
3642 /* check for a duplicate of this item's property */
3643 if (item->prop_ == prop_sym)
3644 {
3645 CTcObjTemplateItem *sub;
3646
3647 /*
3648 * if everything from here to the end of the
3649 * list is marked as an alternative, then we're
3650 * just adding a duplicate property to a run of
3651 * alternatives, which is fine
3652 */
3653 for (sub = item ; sub != 0 && sub->is_alt_ ;
3654 sub = sub->nxt_) ;
3655
3656 /*
3657 * if we found a non-alternative following this
3658 * item, then this is indeed a duplicate; if we
3659 * didn't find any non-alternatives, we're just
3660 * adding this to a run of alternatives, so
3661 * we're okay
3662 */
3663 if (sub != 0)
3664 {
3665 /* it's a duplicate - log the error */
3666 G_tok->log_error(TCERR_OBJ_TPL_PROP_DUP,
3667 (int)prop_sym->get_sym_len(),
3668 prop_sym->get_sym());
3669
3670 /* no need to look any further */
3671 break;
3672 }
3673 }
3674 }
3675
3676 /* create the template item */
3677 item = new (G_prsmem)
3678 CTcObjTemplateItem(prop_sym, def_tok,
3679 is_alt, is_opt);
3680 }
3681 }
3682
3683 /* if we have a valid new item, add it to our list */
3684 if (item != 0)
3685 {
3686 /*
3687 * if we're an alternative and the prior item was not,
3688 * we're the head of a new alternative group; if we're not
3689 * an alternative, then we're not in an alternative group
3690 * after this item
3691 */
3692 if (is_alt && (item_tail == 0 || !item_tail->is_alt_))
3693 {
3694 /* we're the head of a new group */
3695 alt_group_head = item;
3696 }
3697 else if (!is_alt)
3698 {
3699 /* we're no longer in an alternative group */
3700 alt_group_head = 0;
3701 }
3702
3703 /* link it into our list */
3704 if (item_tail != 0)
3705 item_tail->nxt_ = item;
3706 else
3707 item_head = item;
3708 item_tail = item;
3709
3710 /* count it */
3711 ++item_cnt;
3712 }
3713 }
3714 }
3715
3716 /* if the template is empty, warn about it */
3717 if (item_cnt == 0)
3718 {
3719 /* flag it as an error */
3720 G_tok->log_error(TCERR_TEMPLATE_EMPTY);
3721
3722 /* there's no point in saving an empty template */
3723 all_ok = FALSE;
3724 }
3725
3726 /* if we were successful, add the new template to our master list */
3727 if (all_ok)
3728 {
3729 /*
3730 * If we found an 'inherited' keyword, we must expand the template
3731 * with all inherited templates. Otherwise, simply add the
3732 * template exactly as given.
3733 */
3734 if (found_inh)
3735 {
3736 /*
3737 * Traverse all superclass templates and add each inherited
3738 * form.
3739 */
3740 add_inherited_templates(class_sym, item_head, item_cnt);
3741 }
3742 else
3743 {
3744 /* add our single template */
3745 add_template_def(class_sym, item_head, item_cnt);
3746 }
3747 }
3748
3749 /* an 'object template' statement generates no tree data */
3750 return 0;
3751 }
3752
3753 /*
3754 * Linked list structure for inherited template list
3755 */
3756 struct inh_tpl_entry
3757 {
3758 inh_tpl_entry() { }
3759
3760 /* this template */
3761 CTcObjTemplate *tpl;
3762
3763 /* next in list */
3764 inh_tpl_entry *nxt;
3765 };
3766
3767 /*
3768 * Expand a template definition for inherited superclass templates
3769 */
3770 void CTcParser::add_inherited_templates(CTcSymObj *sc_sym,
3771 CTcObjTemplateItem *item_head,
3772 size_t item_cnt)
3773 {
3774 inh_tpl_entry *head;
3775 inh_tpl_entry *tail;
3776 inh_tpl_entry *cur;
3777 CTcObjTemplate *tpl;
3778
3779 /* start with an empty list */
3780 head = tail = 0;
3781
3782 /*
3783 * traverse the superclass tree, building a list of all unique
3784 * templates inherited from the tree
3785 */
3786 build_super_template_list(&head, &tail, sc_sym);
3787
3788 /* expand our template for each item in the superclass template list */
3789 for (cur = head ; cur != 0 ; cur = cur->nxt)
3790 expand_and_add_inherited_template(sc_sym, item_head, cur->tpl);
3791
3792 /* expand our template for each item in the root template list */
3793 for (tpl = template_head_ ; tpl != 0 ; tpl = tpl->nxt_)
3794 expand_and_add_inherited_template(sc_sym, item_head, tpl);
3795
3796 /*
3797 * finally, add the trivial expansion (in other words, with the
3798 * 'inherited' keyword replaced by nothing)
3799 */
3800 expand_and_add_inherited_template(sc_sym, item_head, 0);
3801 }
3802
3803 /*
3804 * Expand a template that contains an 'inherited' keyword, substituting
3805 * the given inherited template list for the 'inherited' keyword, and add
3806 * the resulting template to the template list for the given class.
3807 */
3808 void CTcParser::expand_and_add_inherited_template(
3809 CTcSymObj *sc_sym, CTcObjTemplateItem *item_head,
3810 CTcObjTemplate *sc_tpl)
3811 {
3812 CTcObjTemplateItem *exp_head;
3813 CTcObjTemplateItem *exp_tail;
3814 CTcObjTemplateItem *cur;
3815 CTcObjTemplateItem *end_nxt;
3816 size_t cnt;
3817
3818 /* our new list is empty thus far */
3819 exp_head = exp_tail = 0;
3820
3821 /*
3822 * Build a new list. Copy items from the original list until we find
3823 * the 'inherited' item, then copy items from the inherited template,
3824 * and finally copy the remaining items from the original list.
3825 */
3826 for (end_nxt = 0, cur = item_head, cnt = 0 ; cur != 0 ; cur = cur->nxt_)
3827 {
3828 CTcObjTemplateItem *new_item;
3829
3830 /* if this is the 'inherited' entry, switch to the superclass list */
3831 if (cur->tok_type_ == TOKT_INHERITED)
3832 {
3833 /*
3834 * if we're doing a trivial expansion, indicated by a null
3835 * superclass template, simply skip the 'inherited' keyword
3836 * altogether and proceed with copying the main list
3837 */
3838 if (sc_tpl == 0)
3839 continue;
3840
3841 /* remember where to pick up in the containing list */
3842 end_nxt = cur;
3843
3844 /* switch to the inherited list */
3845 cur = sc_tpl->items_;
3846 }
3847
3848 /* make a copy of the current item */
3849 new_item = new (G_prsmem)
3850 CTcObjTemplateItem(cur->prop_, cur->tok_type_,
3851 cur->is_alt_, cur->is_opt_);
3852
3853 /* link it into our list */
3854 if (exp_tail != 0)
3855 exp_tail->nxt_ = new_item;
3856 else
3857 exp_head = new_item;
3858 exp_tail = new_item;
3859
3860 /* count the item */
3861 ++cnt;
3862
3863 /*
3864 * if we're at the end of the current list, and we have an outer
3865 * list to resume, resume the outer list
3866 */
3867 if (cur->nxt_ == 0 && end_nxt != 0)
3868 {
3869 /* resume at the outer list */
3870 cur = end_nxt;
3871
3872 /* forget the outer list now that we're resuming it */
3873 end_nxt = 0;
3874 }
3875 }
3876
3877 /* add the expanded template definition */
3878 add_template_def(sc_sym, exp_head, cnt);
3879 }
3880
3881 /*
3882 * Build a list of inherited templates given the base class. We add only
3883 * templates from superclasses of the given class.
3884 *
3885 * We add only templates that aren't already in the list, because the
3886 * class could conceivably inherit from the same base class more than
3887 * once, if it (or a superclass) inherits from multiple base classes that
3888 * share a common base class.
3889 */
3890 void CTcParser::build_super_template_list(inh_tpl_entry **list_head,
3891 inh_tpl_entry **list_tail,
3892 CTcSymObj *sc_sym)
3893 {
3894 CTPNSuperclass *sc;
3895
3896 /* if this object was defined as a subclass of 'object', we're done */
3897 if (sc_sym->sc_is_root())
3898 return;
3899
3900 /* scan all superclasses of the given superclass */
3901 for (sc = sc_sym->get_sc_name_head() ; sc != 0 ; sc = sc->nxt_)
3902 {
3903 CTcSymObj *sc_sym_cur;
3904 CTcObjTemplate *tpl;
3905
3906 /* if this item is not an object, skip it */
3907 sc_sym_cur = (CTcSymObj *)sc->get_sym();
3908 if (sc_sym_cur == 0 || sc_sym_cur->get_type() != TC_SYM_OBJ)
3909 continue;
3910
3911 /*
3912 * scan this superclass's templates and add each one that's not
3913 * already in the list
3914 */
3915 for (tpl = sc_sym_cur->get_first_template() ; tpl != 0 ;
3916 tpl = tpl->nxt_)
3917 {
3918 inh_tpl_entry *entry;
3919
3920 /* find this template in the inherited list */
3921 for (entry = *list_head ; entry != 0 ; entry = entry->nxt)
3922 {
3923 /* if it's in the list, stop looking for it */
3924 if (entry->tpl == tpl)
3925 break;
3926 }
3927
3928 /* if we didn't find this template, add it */
3929 if (entry == 0)
3930 {
3931 /* create a new list item */
3932 entry = new (G_prsmem) inh_tpl_entry();
3933 entry->tpl = tpl;
3934
3935 /*
3936 * Link it in at the end of the list. Note that we want to
3937 * link it at the end because this puts the template
3938 * inheritance list in the same order as the superclass
3939 * definitions. Since we search the template list in
3940 * order, this ensures that we find the first matching
3941 * template in superclass inheritance order in cases where
3942 * there are multiple superclasses.
3943 */
3944 if (*list_tail != 0)
3945 (*list_tail)->nxt = entry;
3946 else
3947 *list_head = entry;
3948 *list_tail = entry;
3949 entry->nxt = 0;
3950 }
3951 }
3952
3953 /* inherit from this superclass's superclasses */
3954 build_super_template_list(list_head, list_tail, sc_sym_cur);
3955 }
3956 }
3957
3958 /*
3959 * Add a template definition
3960 */
3961 void CTcParser::add_template_def(
3962 CTcSymObj *class_sym, CTcObjTemplateItem *item_head, size_t item_cnt)
3963 {
3964 /* create the new template list entry */
3965 CTcObjTemplate *tpl = new (G_prsmem) CTcObjTemplate(item_head, item_cnt);
3966
3967 /*
3968 * link it into the appropriate list - if it's associated with a
3969 * class, link it into the class symbol's list; otherwise link it into
3970 * the master list for the root object class
3971 */
3972 if (class_sym != 0)
3973 {
3974 /* link it into the class's list */
3975 class_sym->add_template(tpl);
3976 }
3977 else
3978 {
3979 /* it's for the root class - link it into the master list */
3980 if (template_tail_ != 0)
3981 template_tail_->nxt_ = tpl;
3982 else
3983 template_head_ = tpl;
3984 template_tail_ = tpl;
3985 }
3986
3987 /*
3988 * if this is the longest template so far, extend the template
3989 * instance expression array to make room for parsing this template's
3990 * instances
3991 */
3992 if (item_cnt > template_expr_max_)
3993 {
3994 /*
3995 * note the new length, rounding up to ensure that we don't have
3996 * to repeatedly reallocate if we have several more of roughly
3997 * this same length
3998 */
3999 template_expr_max_ = (item_cnt + 15) & ~15;
4000
4001 /* reallocate the list */
4002 template_expr_ = (CTcObjTemplateInst *)G_prsmem->
4003 alloc(sizeof(template_expr_[0])
4004 * template_expr_max_);
4005 }
4006 }
4007
4008
4009 /* ------------------------------------------------------------------------ */
4010 /*
4011 * String template definition
4012 */
4013
4014 void CTcStrTemplate::append_tok(const CTcToken *tok)
4015 {
4016 /* create a new list element for the token */
4017 CTcTokenEle *ele = new (G_prsmem)CTcTokenEle();
4018 G_tok->copytok(ele, tok);
4019
4020 /* add it to the tail of the list */
4021 if (tail != 0)
4022 tail->setnxt(ele);
4023 else
4024 head = ele;
4025 tail = ele;
4026
4027 /* count the token */
4028 cnt++;
4029 }
4030
4031 /* ------------------------------------------------------------------------ */
4032 /*
4033 * Parse a string template definition:
4034 *
4035 *. string template <<tok...>> funcName;
4036 *
4037 * The 'tok' entries are the literal tokens to match for the embedding
4038 * contents. We can have zero or more of these; a completely empty
4039 * template sets up a function that processes every expression not
4040 * processed by another template. Optionally, one token may be '*', which
4041 * represents an actual expression. If there's no '*', the template simply
4042 * matches the literal token list exactly as given, and doesn't accept an
4043 * expression. 'funcName' is a symbol giving the name of the function
4044 * that's to be invoked at run-time when this template is used. If there's
4045 * a '*', this function takes one argument giving the expression value for
4046 * the '*', otherwise it takes no arguments.
4047 */
4048 CTPNStmTop *CTcParser::parse_string_template_def(int *err)
4049 {
4050 /* set up an empty string template object */
4051 CTcStrTemplate *tpl = new (G_prsmem) CTcStrTemplate();
4052
4053 /* check that the template starts with << */
4054 if (G_tok->next() == TOKT_SHL)
4055 G_tok->next();
4056 else
4057 G_tok->log_error_curtok(TCERR_STRTPL_MISSING_LANGLE);
4058
4059 /* keep going until we find the >> or end of statement */
4060 for (int done = FALSE ; !done ; )
4061 {
4062 switch (G_tok->cur())
4063 {
4064 case TOKT_ASHR:
4065 /* >> - end of the token list */
4066 G_tok->next();
4067 done = TRUE;
4068 break;
4069
4070 case TOKT_SEM:
4071 case TOKT_RBRACE:
4072 /*
4073 * we must be missing the >> at the end of the list - log an
4074 * error and return, since we're assuming we've run out of
4075 * tokens in the template definition
4076 */
4077 G_tok->log_error_curtok(TCERR_STRTPL_MISSING_RANGLE);
4078 return 0;
4079
4080 default:
4081 /* add the token to the template's list */
4082 tpl->append_tok(G_tok->getcur());
4083
4084 /* if this is a '*' token, note it */
4085 if (G_tok->cur() == TOKT_TIMES)
4086 {
4087 /* if we already have a '*', it's an error */
4088 if (tpl->star)
4089 G_tok->log_error(TCERR_STRTPL_MULTI_STAR);
4090
4091 /* note it in the template */
4092 tpl->star = TRUE;
4093 }
4094
4095 /* on to the next token */
4096 G_tok->next();
4097 break;
4098 }
4099 }
4100
4101 /* we need a symbol for the function */
4102 if (G_tok->cur() != TOKT_SYM)
4103 {
4104 /* missing symbol - skip to the next semicolon or brace */
4105 if (skip_to_sem())
4106 *err = TRUE;
4107
4108 /* give up */
4109 return 0;
4110 }
4111
4112 /*
4113 * Our requirement for the processor function is that it takes one
4114 * argument if we have a '*' symbol in the token list, otherwise no
4115 * arguments.
4116 */
4117 int argc = tpl->star ? 1 : 0;
4118
4119 /* look up the symbol */
4120 const CTcToken *functok = G_tok->getcur();
4121 CTcSymFunc *funcsym = (CTcSymFunc *)global_symtab_->find(
4122 functok->get_text(), functok->get_text_len());
4123
4124 /*
4125 * if it's defined, check that it matches; otherwise add an implicit
4126 * 'extern' declaration for it
4127 */
4128 if (funcsym != 0)
4129 {
4130 /*
4131 * It's already defined. Make sure it accepts the argument count
4132 * we want to pass it. If it's not external, make sure that it
4133 * returns a value. (We'll have to give it the benefit of the
4134 * doubt if it's external, as the 'extern' declaration has no way
4135 * to say one way or the other.)
4136 */
4137 if (!funcsym->argc_ok(argc)
4138 || (!funcsym->is_extern() && !funcsym->has_retval()))
4139 G_tok->log_error(TCERR_STRTPL_FUNC_MISMATCH,
4140 (int)functok->get_text_len(),
4141 functok->get_text(), argc);
4142 }
4143 else
4144 {
4145 /*
4146 * It's not defined yet, so add an extern definition for it. The
4147 * function takes one argument if there's a '*' in the template,
4148 * otherwise no arguments.
4149 */
4150 funcsym = new CTcSymFunc(
4151 functok->get_text(), functok->get_text_len(), FALSE,
4152 tpl->star ? 1 : 0, 0, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE);
4153
4154 /* add it to the symbol table */
4155 global_symtab_->add_entry(funcsym);
4156 }
4157
4158 /* save the function symbol in the template */
4159 tpl->func = funcsym;
4160
4161 /* link the template into the parser's global list */
4162 if (str_template_tail_ != 0)
4163 str_template_tail_->nxt = tpl;
4164 else
4165 str_template_head_ = tpl;
4166 str_template_tail_ = tpl;
4167
4168 /* skip the function symbol and require a semicolon */
4169 G_tok->next();
4170 if (parse_req_sem())
4171 *err = TRUE;
4172
4173 /* the 'string template' statement generates no parse tree entry */
4174 return 0;
4175 }
4176
4177
4178 /* ------------------------------------------------------------------------ */
4179 /*
4180 * Parse an object or function definition
4181 */
4182 CTPNStmTop *CTcParser::parse_object_or_func(int *err, int replace,
4183 int suppress_error,
4184 int *suppress_next_error)
4185 {
4186 CTcToken init_tok;
4187 int trans;
4188
4189 /* if the token is 'transient', it's an object definition */
4190 if ((trans = G_tok->cur() == TOKT_TRANSIENT) != 0)
4191 {
4192 /* skip the 'transient' keyword */
4193 G_tok->next();
4194
4195 /*
4196 * if 'object' follows, treat this as an object statement;
4197 * otherwise, we'll process using our normal handling, nothing our
4198 * 'transient' status
4199 */
4200 if (G_tok->cur() == TOKT_OBJECT)
4201 return parse_object_stm(err, TRUE);
4202 }
4203
4204 /* remember the initial token */
4205 init_tok = *G_tok->copycur();
4206
4207 /* check the next token */
4208 switch(G_tok->next())
4209 {
4210 case TOKT_COLON:
4211 /*
4212 * it's an object definition - back up to the object name symbol
4213 * and go parse the object definition
4214 */
4215 G_tok->unget();
4216 return parse_object(err, replace, FALSE, FALSE, 0, trans);
4217
4218 case TOKT_LPAR:
4219 /* 'transient' isn't allowed here */
4220 if (trans)
4221 G_tok->log_error(TCERR_INVAL_TRANSIENT);
4222
4223 /*
4224 * it's a function definition - back up to the function name
4225 * symbol and go parse the function
4226 */
4227 G_tok->unget();
4228 return parse_function(err, FALSE, replace, FALSE, FALSE);
4229
4230 case TOKT_TEMPLATE:
4231 /* 'transient template' is an error - flag it, but proceed anyway */
4232 if (trans)
4233 G_tok->log_error(TCERR_INVAL_TRANSIENT);
4234
4235 /* it's a template for a specific object - parse it */
4236 return parse_template_def(err, &init_tok);
4237
4238 default:
4239 /*
4240 * if the first token was a symbol that has already been defined
4241 * as a class or object, this must be an anonymous object
4242 * definition
4243 */
4244 if (init_tok.gettyp() == TOKT_SYM)
4245 {
4246 CTcSymObj *sym;
4247
4248 /* look up the symbol in the global symbol table */
4249 sym = (CTcSymObj *)
4250 global_symtab_->find(init_tok.get_text(),
4251 init_tok.get_text_len());
4252
4253 /*
4254 * If it's a TADS Object symbol, this must be an anonymous
4255 * instance definition. If the symbol is not yet defined
4256 * and we're doing a syntax-only pass, assume the same
4257 * thing.
4258 */
4259 if ((sym !=0
4260 && sym->get_type() == TC_SYM_OBJ
4261 && sym->get_metaclass() == TC_META_TADSOBJ)
4262 || (sym == 0 && syntax_only_))
4263 {
4264 /*
4265 * If this is a 'replace' statement, we must have a class
4266 * list with the replacement object, so this is invalid.
4267 */
4268 if (replace)
4269 {
4270 /* log the error, but continue parsing despite it */
4271 G_tok->log_error(TCERR_REPLACE_OBJ_REQ_SC);
4272 }
4273
4274 /* put the initial symbol back */
4275 G_tok->unget();
4276
4277 /* parse the anonymous object definition */
4278 return parse_anon_object(err, 0, FALSE, 0, trans);
4279 }
4280 }
4281 else if (init_tok.gettyp() == TOKT_OBJECT)
4282 {
4283 /*
4284 * this is a base anonymous object - put the 'object' token
4285 * back and go parse the object definition
4286 */
4287 G_tok->unget();
4288 return parse_anon_object(err, 0, FALSE, 0, trans);
4289 }
4290
4291 /*
4292 * anything else is an error - log an error, unless we just
4293 * logged this same error on the previous token
4294 */
4295 if (!suppress_error)
4296 G_tok->log_error_curtok(TCERR_REQ_FUNC_OR_OBJ);
4297
4298 /*
4299 * suppress this same error if it occurs again on the next
4300 * token, since if it does we're probably just scanning past
4301 * tons of garbage trying to resynchronize
4302 */
4303 if (suppress_next_error != 0)
4304 *suppress_next_error = TRUE;
4305 return 0;
4306 }
4307 }
4308
4309 /*
4310 * Parse a '+' object definition, or a '+ property' statement
4311 */
4312 CTPNStmTop *CTcParser::parse_plus_object(int *err)
4313 {
4314 int cnt;
4315 int is_class;
4316 int anon;
4317 int trans;
4318
4319 /* count the '+' or '++' tokens */
4320 for (cnt = 0 ; G_tok->cur() == TOKT_PLUS || G_tok->cur() == TOKT_INC ;
4321 G_tok->next())
4322 {
4323 /* count it */
4324 ++cnt;
4325
4326 /* '++' counts twice for obvious reasons */
4327 if (G_tok->cur() == TOKT_INC)
4328 ++cnt;
4329 }
4330
4331 /*
4332 * if the count is one, and the next token is 'property', it's the
4333 * '+' property definition
4334 */
4335 if (cnt == 1 && G_tok->cur() == TOKT_PROPERTY)
4336 {
4337 /* skip the 'property' token and make sure we have a symbol */
4338 if (G_tok->next() != TOKT_SYM)
4339 {
4340 /* log the error */
4341 G_tok->log_error_curtok(TCERR_PLUSPROP_REQ_SYM);
4342
4343 /* if it's anything other than a ';', skip it */
4344 if (G_tok->cur() != TOKT_SEM)
4345 G_tok->next();
4346 }
4347 else
4348 {
4349 /* set the property */
4350 plus_prop_ = look_up_prop(G_tok->getcur(), TRUE);
4351
4352 /* skip the token */
4353 G_tok->next();
4354 }
4355
4356 /* require the closing semicolon */
4357 if (parse_req_sem())
4358 {
4359 *err = TRUE;
4360 return 0;
4361 }
4362
4363 /* done - there's no statement object for this directive */
4364 return 0;
4365 }
4366
4367 /* presume it's an ordinary object */
4368 is_class = FALSE;
4369 trans = FALSE;
4370 anon = FALSE;
4371
4372 /* check for the 'class' keyword or the 'transient' keyword */
4373 if (G_tok->cur() == TOKT_CLASS)
4374 {
4375 /* skip the 'class' keyword */
4376 G_tok->next();
4377
4378 /* note it */
4379 is_class = TRUE;
4380 }
4381 else if (G_tok->cur() == TOKT_TRANSIENT)
4382 {
4383 /* skip the 'transient' keyword */
4384 G_tok->next();
4385
4386 /* note it */
4387 trans = TRUE;
4388 }
4389
4390 /*
4391 * check to see if the object name is followed by a colon - if not,
4392 * it's an anonymous object definition
4393 */
4394 if (G_tok->cur() == TOKT_SYM)
4395 {
4396 /* check the next symbol to see if it's a colon */
4397 anon = (G_tok->next() != TOKT_COLON);
4398
4399 /* put back the token */
4400 G_tok->unget();
4401 }
4402 else if (G_tok->cur() == TOKT_OBJECT)
4403 {
4404 /* it's an anonymous base object */
4405 anon = TRUE;
4406 }
4407
4408 /* parse appropriately for anonymous or named object */
4409 if (anon)
4410 {
4411 /* parse the anonymous object definition */
4412 return parse_anon_object(err, cnt, FALSE, 0, trans);
4413 }
4414 else
4415 {
4416 /* parse the object definition */
4417 return parse_object(err, FALSE, FALSE, is_class, cnt, trans);
4418 }
4419 }
4420
4421 /* ------------------------------------------------------------------------ */
4422 /*
4423 * Find or define an object symbol.
4424 */
4425 CTcSymObj *CTcParser::find_or_def_obj(
4426 const char *tok_txt, size_t tok_len, int replace, int modify,
4427 int *is_class, CTcSymObj **mod_orig_sym, CTcSymMetaclass **meta_sym,
4428 int *trans)
4429 {
4430 /* we don't have a modify base symbol yet */
4431 *mod_orig_sym = 0;
4432
4433 /* presume we won't find a modified metaclass definition */
4434 if (meta_sym != 0)
4435 *meta_sym = 0;
4436
4437 /* look up the symbol to see if it's already defined */
4438 CTcSymbol *sym = global_symtab_->find(tok_txt, tok_len);
4439
4440 /* check for 'modify' used with an intrinsic class */
4441 CTcSymObj *obj_sym;
4442 if (meta_sym != 0
4443 && modify
4444 && sym != 0
4445 && sym->get_type() == TC_SYM_METACLASS)
4446 {
4447 /*
4448 * We're modifying an intrinsic class. In this case, the base
4449 * object is the previous modification object for the class, or no
4450 * object at all. In either case, we must create a new object and
4451 * assign it to the metaclass.
4452 */
4453
4454 /* get the metaclass symbol */
4455 *meta_sym = (CTcSymMetaclass *)sym;
4456
4457 /* get the original modification object */
4458 CTcSymObj *old_mod_obj = (*meta_sym)->get_mod_obj();
4459
4460 /*
4461 * If there is no original modification object, create one - this
4462 * is necessary so that there's always a dummy object at the root
4463 * of the modification chain in each object file. During linking,
4464 * when one object file modifies one of these chains loaded from
4465 * another object file, we'll use the pointer to the dummy root
4466 * object in the second object file to point instead to the top of
4467 * the chain in the first object file.
4468 */
4469 if (old_mod_obj == 0)
4470 {
4471 /* create an anonymous base object */
4472 old_mod_obj = CTcSymObj::synthesize_modified_obj_sym(FALSE);
4473
4474 /* the object is of metaclass IntrinsicClassModifier */
4475 old_mod_obj->set_metaclass(TC_META_ICMOD);
4476
4477 /* give the dummy base object an empty statement */
4478 CTPNStmObject *base_stm = new CTPNStmObject(old_mod_obj, FALSE);
4479 old_mod_obj->set_obj_stm(base_stm);
4480
4481 /*
4482 * add the base statement to our nested top-level statement
4483 * list, so that we generate this object's code
4484 */
4485 add_nested_stm(base_stm);
4486 }
4487
4488 /*
4489 * note the superclass of the new modification object - this is
4490 * simply the previous modification object, which we're further
4491 * modifying, thus making this previous modification object the
4492 * base of the new modification
4493 */
4494 *mod_orig_sym = old_mod_obj;
4495
4496 /* create a symbol for the new modification object */
4497 obj_sym = CTcSymObj::synthesize_modified_obj_sym(FALSE);
4498
4499 /* the object is of metaclass IntrinsicClassModifier */
4500 obj_sym->set_metaclass(TC_META_ICMOD);
4501
4502 /* set the new object's base modified object */
4503 obj_sym->set_mod_base_sym(old_mod_obj);
4504
4505 /* make this the new metaclass modifier object */
4506 (*meta_sym)->set_mod_obj(obj_sym);
4507
4508 /* return the symbol */
4509 return obj_sym;
4510 }
4511
4512 /* assume for now that the symbol is an object (we'll check shortly) */
4513 obj_sym = (CTcSymObj *)sym;
4514 if (obj_sym != 0)
4515 {
4516 /*
4517 * This symbol is already defined. The previous definition must
4518 * be an object, or this definition is incompatible. If it's an
4519 * object, then it must have been external, *or* we must be
4520 * replacing or modifying the previous definition.
4521 */
4522 if (obj_sym->get_type() != TC_SYM_OBJ)
4523 {
4524 /*
4525 * it's not an object - log an error; continue parsing the
4526 * definition for syntax, but the definition is ultimately
4527 * invalid because the object name is invalid
4528 */
4529 G_tok->log_error_curtok(TCERR_REDEF_AS_OBJ);
4530
4531 /* ignore the definition */
4532 obj_sym = 0;
4533 }
4534 else if ((modify || replace)
4535 && obj_sym->get_metaclass() != TC_META_TADSOBJ)
4536 {
4537 /*
4538 * we can't modify or replace BigNumber instances, Dictionary
4539 * instances, or anything other than ordinary TADS objects
4540 */
4541 G_tok->log_error(TCERR_CANNOT_MOD_OR_REP_TYPE);
4542
4543 /* ignore the definition */
4544 obj_sym = 0;
4545 }
4546 else if (modify)
4547 {
4548 CTPNStmObject *mod_orig_stm;
4549 CTcSymObj *mod_sym;
4550
4551 /*
4552 * remember the original pre-modified object parse tree - we
4553 * might need to delete properties in the tree (for any
4554 * properties defined with the "replace" keyword)
4555 */
4556 mod_orig_stm = obj_sym->get_obj_stm();
4557
4558 /*
4559 * We're modifying the previous definition. The object was
4560 * previously defined within this translation unit, so the
4561 * modification can be conducted entirely within the
4562 * translation unit (i.e., there's no link-time work we'll
4563 * need to do).
4564 *
4565 * Create a new fake symbol for the original object, and
4566 * assign it a new ID. We need a symbol table entry because
4567 * the object statement parse tree depends upon having a
4568 * symbol table entry, and because the symbol table entry is
4569 * the mechanism by which the linker can fix up the object ID
4570 * from object file local numbering to image file global
4571 * numbering.
4572 */
4573 mod_sym = CTcSymObj::synthesize_modified_obj_sym(FALSE);
4574
4575 if (mod_orig_stm != 0)
4576 {
4577 /*
4578 * transfer the old object definition tree to the new
4579 * symbol, since the new symbol now holds the original
4580 * object - the real symbol is going to have the new
4581 * (modified) object definition instead
4582 */
4583 mod_sym->set_obj_stm(mod_orig_stm);
4584 mod_orig_stm->set_obj_sym(mod_sym);
4585
4586 /* mark the original as modified */
4587 mod_orig_stm->set_modified(TRUE);
4588
4589 /* keep the same class attributes as the original */
4590 *is_class = mod_orig_stm->is_class();
4591
4592 /* keep the same transient status as the original */
4593 *trans = mod_orig_stm->is_transient();
4594 }
4595 else
4596 {
4597 /*
4598 * it's external - use the class and transient status of
4599 * the imported symbol
4600 */
4601 *is_class = obj_sym->is_class();
4602 *trans = obj_sym->is_transient();
4603 }
4604
4605 /*
4606 * transfer the property deletion list from the original
4607 * symbol to the new symbol
4608 */
4609 mod_sym->set_del_prop_head(obj_sym->get_first_del_prop());
4610 obj_sym->set_del_prop_head(0);
4611
4612 /* transfer the vocabulary list from the original */
4613 mod_sym->set_vocab_head(obj_sym->get_vocab_head());
4614 obj_sym->set_vocab_head(0);
4615
4616 /* copy the 'transient' status to the new symbol */
4617 if (obj_sym->is_transient())
4618 mod_sym->set_transient();
4619
4620 /*
4621 * Build a token containing the synthesized object name for
4622 * the pre-modified object, so that we can refer to it in the
4623 * superclass list of the new object.
4624 */
4625 *mod_orig_sym = mod_sym;
4626
4627 /*
4628 * transfer the base 'modify' symbol to the new symbol for the
4629 * original object
4630 */
4631 mod_sym->set_mod_base_sym(obj_sym->get_mod_base_sym());
4632
4633 /*
4634 * If we're modifying an external object, we must mark the
4635 * symbol as such - this will put the 'modify' flag with the
4636 * symbol in the object file, so that we'll apply the 'modify'
4637 * at link time.
4638 */
4639 if (obj_sym->is_extern())
4640 {
4641 /* mark this symbol as modifying an external symbol */
4642 obj_sym->set_ext_modify(TRUE);
4643
4644 /* we're no longer external */
4645 obj_sym->set_extern(FALSE);
4646
4647 /* mark the synthesized original symbol as external */
4648 mod_sym->set_extern(TRUE);
4649 }
4650
4651 /* remember the base symbol */
4652 obj_sym->set_mod_base_sym(mod_sym);
4653
4654 /*
4655 * keep the original version's dictionary with the base
4656 * symbol, and change the modified symbol to use the
4657 * dictionary now active
4658 */
4659 mod_sym->set_dict(obj_sym->get_dict());
4660 obj_sym->set_dict(dict_cur_);
4661 }
4662 else if (obj_sym->is_extern())
4663 {
4664 /*
4665 * it was previously defined as external, so this definition
4666 * is acceptable - simply remove the 'extern' flag from the
4667 * object symbol now that we have the real definition
4668 */
4669 obj_sym->set_extern(FALSE);
4670
4671 /* set the object's dictionary to the active dictionary */
4672 obj_sym->set_dict(dict_cur_);
4673
4674 /*
4675 * If we're replacing an external object, we must mark the
4676 * symbol as a replacement - this will put the 'replace' flag
4677 * with the symbol in the object file, so that we'll apply the
4678 * replacement at link time.
4679 */
4680 if (replace)
4681 obj_sym->set_ext_replace(TRUE);
4682 }
4683 else if (replace)
4684 {
4685 /*
4686 * We're replacing the previous definition. We can simply
4687 * discard the previous definition and replace its parse tree
4688 * with our own. Mark the original object parse tree as
4689 * replaced, so that we do not generate any code for it.
4690 */
4691 obj_sym->get_obj_stm()->set_replaced(TRUE);
4692
4693 /*
4694 * Note that we do NOT mark the object as a 'replace' object
4695 * in the symbol table. Because the object was previously
4696 * defined within this translation unit, the replacement is
4697 * now completed - there is no need to apply any replacement
4698 * from this object at link time, and in fact it would be
4699 * incorrect to do so.
4700 */
4701 }
4702 else
4703 {
4704 /*
4705 * the symbol is already defined in this module, and we're not
4706 * replacing or modifying it -- this is an error, because we
4707 * can only define an object once
4708 */
4709 G_tok->log_error(TCERR_OBJ_REDEF, (int)tok_len, tok_txt);
4710
4711 /* ignore the definition */
4712 obj_sym = 0;
4713 }
4714 }
4715 else
4716 {
4717 /*
4718 * the object is not already defined in the global symbol table --
4719 * create a new object symbol
4720 */
4721 obj_sym = new CTcSymObj(tok_txt, tok_len, FALSE,
4722 G_cg->new_obj_id(), FALSE,
4723 TC_META_TADSOBJ, dict_cur_);
4724
4725 /* add the object to the global symbol table */
4726 global_symtab_->add_entry(obj_sym);
4727
4728 /*
4729 * We can't replace or modify an object that hasn't been defined
4730 * yet (it has to have been defined at least as an external).
4731 * Do not make this check in syntax-only mode, because it's not
4732 * important to the syntax.
4733 */
4734 if (replace || modify)
4735 {
4736 /* only log an error in full compile mode */
4737 if (!syntax_only_)
4738 {
4739 /* full compilation - log the error */
4740 G_tok->log_error(TCERR_REPMODOBJ_UNDEF);
4741 }
4742 else
4743 {
4744 /*
4745 * We're only parsing - mark this as an external replace
4746 * or external modify on the assumption that the symbol
4747 * will be defined externally in the actual compilation.
4748 * In particular, if we're building a symbol file, we
4749 * don't want to write undefined 'modify' or 'replace'
4750 * symbols, because such symbols are not really defined
4751 * by this module.
4752 */
4753 if (replace)
4754 obj_sym->set_ext_replace(TRUE);
4755 else
4756 obj_sym->set_ext_modify(TRUE);
4757 }
4758 }
4759 }
4760
4761 /* mark the object as a class if appropriate */
4762 if (*is_class && obj_sym != 0)
4763 obj_sym->set_is_class(TRUE);
4764
4765 /* mark the object as transient */
4766 if (*trans && obj_sym != 0)
4767 obj_sym->set_transient();
4768
4769 /* return the object symbol */
4770 return obj_sym;
4771 }
4772
4773 /* ------------------------------------------------------------------------ */
4774 /*
4775 * Parse a top-level object definition statement
4776 */
4777 CTPNStmTop *CTcParser::parse_object(
4778 int *err, int replace, int modify, int is_class, int plus_cnt, int trans)
4779 {
4780 /* find or define the symbol */
4781 CTcSymObj *mod_orig_sym;
4782 CTcSymMetaclass *meta_sym;
4783 CTcSymObj *obj_sym = find_or_def_obj(
4784 G_tok->getcur()->get_text(), G_tok->getcur()->get_text_len(),
4785 replace, modify, &is_class, &mod_orig_sym, &meta_sym, &trans);
4786
4787 /* skip the object name */
4788 G_tok->next();
4789
4790 /* parse the body */
4791 return parse_object_body(
4792 err, obj_sym, is_class, FALSE, FALSE, FALSE,
4793 modify, mod_orig_sym, plus_cnt, meta_sym, 0, trans);
4794 }
4795
4796 /*
4797 * Parse an anonymous object. This can be either a top-level object
4798 * definition with no name, or a nested object defined as the value side of
4799 * a property setting within an object definition's property list.
4800 */
4801 CTPNStmObject *CTcParser::parse_anon_object(
4802 int *err, int plus_cnt, int is_nested,
4803 tcprs_term_info *term_info, int trans)
4804 {
4805 /* create an anonymous object symbol */
4806 CTcSymObj *obj_sym = new CTcSymObj(
4807 ".anon", 5, FALSE, G_cg->new_obj_id(), FALSE, TC_META_TADSOBJ, 0);
4808
4809 /* set the dictionary for the new object */
4810 obj_sym->set_dict(dict_cur_);
4811
4812 /* parse the object body */
4813 return parse_object_body(
4814 err, obj_sym, FALSE, TRUE, FALSE, is_nested,
4815 FALSE, 0, plus_cnt, 0, term_info, trans);
4816 }
4817
4818 /* ------------------------------------------------------------------------ */
4819 /*
4820 * Object statement
4821 */
4822
4823 /*
4824 * Parse a property definition using the nested object syntax. This is for
4825 * property definitions of the form "prop: Thing { ... }" that define a new
4826 * static object instance and use a reference as the value of the property.
4827 */
4828 int CTPNStmObjectBase::parse_nested_obj_prop(
4829 CTPNObjProp* &new_prop, int *err, tcprs_term_info *term_info,
4830 const CTcToken *prop_tok, int replace)
4831 {
4832 /* parse the body of the nested object */
4833 CTPNStmObject *nested_obj = G_prs->parse_anon_object(
4834 err, 0, TRUE, term_info, FALSE);
4835
4836 /*
4837 * If we encountered a termination error, assume the error is actually
4838 * in the enclosing object, and that this wasn't meant to be a nested
4839 * object after all. Do not define the symbol we took as a property to
4840 * be a property, since it might well have been meant as an object name
4841 * instead. If a termination error did occur, simply return, so that
4842 * the caller can handle the assumed termination.
4843 */
4844 if (term_info->unterm_)
4845 return FALSE;
4846
4847 /* make sure we didn't encounter an error */
4848 if (*err != 0 )
4849 return FALSE;
4850
4851 /* make sure we created a valid parse tree for the object */
4852 if (nested_obj == 0)
4853 {
4854 *err = TRUE;
4855 return FALSE;
4856 }
4857
4858 /* the value we wish to assign is a reference to this object */
4859 CTcConstVal cval;
4860 cval.set_obj(nested_obj->get_obj_sym()->get_obj_id(),
4861 nested_obj->get_obj_sym()->get_metaclass());
4862 CTcPrsNode *expr = new CTPNConst(&cval);
4863
4864 /*
4865 * look up the property symbol - we can finally look it up with
4866 * reasonable confidence, since it appears we do indeed have a valid
4867 * nested object definition, hence we can stop waiting to see if
4868 * something goes wrong
4869 */
4870 CTcSymProp *prop_sym = G_prs->look_up_prop(prop_tok, TRUE);
4871
4872 /* if it's a vocabulary property, this type isn't allowed */
4873 if (prop_sym != 0 && prop_sym->is_vocab())
4874 G_tok->log_error(TCERR_VOCAB_REQ_SSTR, 1, ":");
4875
4876 /* if we have a valid property and expression, add it */
4877 if (prop_sym != 0)
4878 new_prop = add_prop(prop_sym, expr, replace, FALSE);
4879
4880 /*
4881 * Define the lexicalParent property in the nested object with a
4882 * reference back to the parent object.
4883 */
4884 cval.set_obj(get_obj_sym() != 0
4885 ? get_obj_sym()->get_obj_id() : TCTARG_INVALID_OBJ,
4886 get_obj_sym() != 0
4887 ? get_obj_sym()->get_metaclass() : TC_META_UNKNOWN);
4888 expr = new CTPNConst(&cval);
4889 nested_obj->add_prop(G_prs->get_lexical_parent_sym(), expr, FALSE, FALSE);
4890
4891 /*
4892 * add the nested object's parse tree to the queue of nested top-level
4893 * statements - since an object is a top-level statement, but we're not
4894 * parsing it at the top level, we need to add it to the pending queue
4895 * explicitly
4896 */
4897 G_prs->add_nested_stm(nested_obj);
4898
4899 /* success */
4900 return TRUE;
4901 }
4902
4903 /* ------------------------------------------------------------------------ */
4904 /*
4905 * Parse an object body
4906 */
4907 CTPNStmObject *CTcParser::parse_object_body(
4908 int *err, CTcSymObj *obj_sym,
4909 int is_class, int is_anon, int is_grammar, int is_nested,
4910 int modify, CTcSymObj *mod_orig_sym, int plus_cnt,
4911 CTcSymMetaclass *meta_sym, tcprs_term_info *term_info, int trans)
4912 {
4913 /*
4914 * If we don't have an enclosing term_info, use my own; if a valid
4915 * term_info was passed in, continue using the enclosing one. We
4916 * always want to use the outermost term_info, because our heuristic
4917 * is to assume that any lack of termination applies to the first
4918 * object it possibly could apply to.
4919 */
4920 tcprs_term_info my_term_info;
4921 if (term_info == 0)
4922 term_info = &my_term_info;
4923
4924 /* presume the object won't use braces around its property list */
4925 int braces = FALSE;
4926
4927 /* create the object statement */
4928 CTPNStmObject *obj_stm = new CTPNStmObject(obj_sym, is_class);
4929 if (obj_sym != 0)
4930 obj_sym->set_obj_stm(obj_stm);
4931
4932 /* set the 'transient' status if appropriate */
4933 if (trans)
4934 obj_stm->set_transient();
4935
4936 /* remember whether 'self' was valid in the enclosing context */
4937 int old_self_valid = self_valid_;
4938
4939 /* 'self' is valid within object definitions */
4940 self_valid_ = TRUE;
4941
4942 /* if it's anonymous, add it to the anonymous object list */
4943 if (is_anon && obj_sym != 0)
4944 add_anon_obj(obj_sym);
4945
4946 /*
4947 * set the object's line location (we must do this explicitly
4948 * because we're in a top-level statement and hence do not have a
4949 * current source position saved at the moment)
4950 */
4951 obj_stm->set_source_pos(G_tok->get_last_desc(),
4952 G_tok->get_last_linenum());
4953
4954 /*
4955 * If we're not modifying, parse the class list. If this is a
4956 * 'modify' statement, there's no class list.
4957 */
4958 if (!modify)
4959 {
4960 /*
4961 * Parse and skip the colon, if required. All objects except
4962 * anonymous objects and grammar rule definitions have the colon;
4963 * for anonymous objects and grammar definitions, the caller is
4964 * required to advance us to the class list before parsing the
4965 * object body.
4966 */
4967 if (is_anon || is_grammar)
4968 {
4969 /*
4970 * it's anonymous, or it's a grammar definition - the caller
4971 * will have already advanced us to the class list, so there's
4972 * no need to look for a colon
4973 */
4974 }
4975 else if (G_tok->cur() == TOKT_COLON)
4976 {
4977 /* skip the colon */
4978 G_tok->next();
4979 }
4980 else
4981 {
4982 /* it's an error, but assume they simply left off the colon */
4983 G_tok->log_error_curtok(TCERR_OBJDEF_REQ_COLON);
4984 }
4985
4986 /*
4987 * Drop any existing list of superclass names from the symbol. We
4988 * might have a list of names from loading the symbol file; if so,
4989 * forget that, and use the list defined here instead.
4990 */
4991 if (obj_sym != 0)
4992 obj_sym->clear_sc_names();
4993
4994 /* parse the superclass list */
4995 parse_superclass_list(obj_sym, obj_stm->get_superclass_list());
4996 }
4997 else
4998 {
4999 /*
5000 * This is a 'modify' statement. The original pre-modified
5001 * object is our single superclass.
5002 *
5003 * Note that we do not do anything with the superclass name list,
5004 * because we're reassigning the symbol representing the global
5005 * name of the object (obj_sym) to refer to the new modifier
5006 * object. As far as the name structure of the program is
5007 * concerned, this object's superclass list is the same as the
5008 * original object's superclass list.
5009 */
5010 if (mod_orig_sym != 0)
5011 obj_stm->add_superclass(mod_orig_sym);
5012 }
5013
5014 /*
5015 * clear the list of dictionary properties, so we can keep track of
5016 * which dictionary properties this object explicitly defines
5017 */
5018 for (CTcDictPropEntry *dict_prop = dict_prop_head_ ; dict_prop != 0 ;
5019 dict_prop = dict_prop->nxt_)
5020 {
5021 /* mark this dictionary property as not defined for this object */
5022 dict_prop->defined_ = FALSE;
5023 }
5024
5025 /*
5026 * If we have a '+' list, figure the object's location by finding
5027 * the item in the location stack at the desired depth.
5028 */
5029 if (plus_cnt != 0)
5030 {
5031 /*
5032 * if no '+' property has been defined, or they're asking for a
5033 * nesting level that doesn't exist, note the error and ignore the
5034 * '+' setting
5035 */
5036 if (plus_prop_ == 0
5037 || (size_t)plus_cnt > plus_stack_alloc_
5038 || plus_stack_[plus_cnt - 1] == 0)
5039 {
5040 /* log the error */
5041 G_tok->log_error(TCERR_PLUSOBJ_TOO_MANY);
5042 }
5043 else
5044 {
5045 /*
5046 * find the object - the location of an object with N '+'
5047 * signs is the object most recently defined with N-1 '+'
5048 * signs
5049 */
5050 CTPNStmObject *loc = plus_stack_[plus_cnt - 1];
5051
5052 /* add the '+' property value, if it's a valid object symbol */
5053 if (loc->get_obj_sym() != 0)
5054 {
5055 /* set up a constant expression for the object reference */
5056 CTcConstVal cval;
5057 cval.set_obj(loc->get_obj_sym()->get_obj_id(),
5058 loc->get_obj_sym()->get_metaclass());
5059 CTcPrsNode *expr = new CTPNConst(&cval);
5060
5061 /* add the location property */
5062 CTPNObjProp *prop = obj_stm->add_prop(
5063 plus_prop_, expr, FALSE, FALSE);
5064
5065 /*
5066 * mark it as overwritable, since it was added via the '+'
5067 * notation
5068 */
5069 prop->set_overwritable();
5070 }
5071 }
5072 }
5073
5074 /*
5075 * Remember this object as the most recent object defined with its
5076 * number of '+' signs. Only pay attention to '+' signs for top-level
5077 * objects - if this is a nested object, it doesn't participate in the
5078 * '+' mechanism at all. Also, ignore classes defined with no '+'
5079 * signs - the '+' mechanism is generally only useful for instances,
5080 * so ignore it for classes that don't use the notation explicitly.
5081 */
5082 if (!is_nested && (!is_class || plus_cnt != 0))
5083 {
5084 /* first, allocate more space in the '+' stack if necessary */
5085 if ((size_t)plus_cnt >= plus_stack_alloc_)
5086 {
5087 size_t new_alloc;
5088 size_t i;
5089
5090 /*
5091 * allocate more space - go a bit above what we need to avoid
5092 * having to reallocate again immediately if they a few levels
5093 * deeper
5094 */
5095 new_alloc = plus_cnt + 16;
5096 plus_stack_ = (CTPNStmObject **)
5097 t3realloc(plus_stack_,
5098 new_alloc * sizeof(*plus_stack_));
5099
5100 /* clear out the newly-allocated entries */
5101 for (i = plus_stack_alloc_ ; i < new_alloc ; ++i)
5102 plus_stack_[i] = 0;
5103
5104 /* remember the new allocation size */
5105 plus_stack_alloc_ = new_alloc;
5106 }
5107
5108 /* remember this object as the last object at its depth */
5109 plus_stack_[plus_cnt] = obj_stm;
5110 }
5111
5112 /*
5113 * Check for an open brace. If the object uses braces around its
5114 * property list, template properties can be either before the open
5115 * brace or just inside it.
5116 */
5117 if (G_tok->cur() == TOKT_LBRACE)
5118 {
5119 /* skip the open brace */
5120 G_tok->next();
5121
5122 /* note that we're using braces */
5123 braces = TRUE;
5124 }
5125
5126 /* check for template syntax */
5127 parse_obj_template(err, obj_stm, FALSE);
5128 if (*err)
5129 {
5130 self_valid_ = old_self_valid;
5131 return 0;
5132 }
5133
5134 /*
5135 * If we didn't already find an open brace, try again now that we've
5136 * scanned the template properties, since template properties can
5137 * either immediately precede or immediately follow the open brace, if
5138 * present.
5139 */
5140 if (!braces && G_tok->cur() == TOKT_LBRACE)
5141 {
5142 /* note that we're using braces */
5143 braces = TRUE;
5144
5145 /* skip the open brace */
5146 G_tok->next();
5147 }
5148
5149 /*
5150 * If this is a nested object, the property list must be enclosed in
5151 * braces. If we haven't found a brace by this point, it's an error.
5152 */
5153 if (is_nested && !braces)
5154 {
5155 /*
5156 * No braces - this is illegal, because a nested object requires
5157 * braces. The most likely error is that the previous object was
5158 * not properly terminated, and this is simply a new object
5159 * definition. Flag the error as a missing terminator on the
5160 * enclosing object.
5161 */
5162 G_tcmain->log_error(term_info->desc_, term_info->linenum_,
5163 TC_SEV_ERROR, TCERR_UNTERM_OBJ_DEF);
5164
5165 /* set the termination error flag for the caller */
5166 term_info->unterm_ = TRUE;
5167 }
5168
5169 /*
5170 * If we're not a class, and we're not a 'modify', add the automatic
5171 * 'sourceTextOrder' property, add the 'sourceTextGroup' if desired.
5172 * sourceTextOrder provides a source file sequence number that can be
5173 * used to order a set of objects into the same sequence in which they
5174 * appeared in the original source file, which is often useful for
5175 * assembling pre-initialized structures. sourceTextGroup identifies
5176 * the file itself.
5177 */
5178 if (!is_class && !modify)
5179 {
5180 /* set up the constant expression for the source sequence number */
5181 CTcConstVal cval;
5182 cval.set_int(src_order_idx_++);
5183 CTcPrsNode *expr = new CTPNConst(&cval);
5184
5185 /* create the new sourceTextOrder property */
5186 CTPNObjProp *prop = obj_stm->add_prop(
5187 src_order_sym_, expr, FALSE, FALSE);
5188
5189 /* mark it as overwritable with an explicit property definition */
5190 prop->set_overwritable();
5191
5192 /* if we're generating sourceTextGroup properties, add it */
5193 if (src_group_mode_)
5194 {
5195 /* set up the expression for the source group object */
5196 cval.set_obj(src_group_id_, TC_META_TADSOBJ);
5197 expr = new CTPNConst(&cval);
5198
5199 /* create the new sourceTextGroup property */
5200 prop = obj_stm->add_prop(src_group_sym_, expr, FALSE, FALSE);
5201
5202 /* mark it as overwritable */
5203 prop->set_overwritable();
5204 }
5205 }
5206
5207 /* parse the property list */
5208 if (!parse_obj_prop_list(
5209 err, obj_stm, meta_sym, modify, is_nested, braces, FALSE,
5210 &my_term_info, term_info))
5211 {
5212 self_valid_ = old_self_valid;
5213 return 0;
5214 }
5215
5216 /*
5217 * Finish the object by adding dictionary property placeholders. Don't
5218 * bother if this is an intrinsic class modifier, since intrinsic class
5219 * objects can't have dictionary associations.
5220 */
5221 if (obj_stm != 0 && meta_sym == 0)
5222 {
5223 /*
5224 * Add a placeholder for each dictionary property we didn't
5225 * explicitly define for the object. This will ensure that we have
5226 * enough property slots pre-allocated at link time if the object
5227 * inherits vocabulary from its superclasses. The linker will
5228 * remove any slots that end up being unused, so this won't affect
5229 * run-time efficiency or the ultimate size of the image file.
5230 */
5231 for (CTcDictPropEntry *dict_prop = dict_prop_head_ ; dict_prop != 0 ;
5232 dict_prop = dict_prop->nxt_)
5233 {
5234 /* if this one isn't defined, add a placeholder */
5235 if (!dict_prop->defined_)
5236 {
5237 /* set up a VOCAB_LIST value */
5238 CTcConstVal cval;
5239 cval.set_vocab_list();
5240 CTcPrsNode *expr = new CTPNConst(&cval);
5241
5242 /* add it */
5243 obj_stm->add_prop(dict_prop->prop_, expr, FALSE, FALSE);
5244 }
5245 }
5246 }
5247
5248 /* restore the enclosing 'self' validity */
5249 self_valid_ = old_self_valid;
5250
5251 /* return the object statement node */
5252 return obj_stm;
5253 }
5254
5255 /* ------------------------------------------------------------------------ */
5256 /*
5257 * Parse a class definition
5258 */
5259 CTPNStmTop *CTcParser::parse_class(int *err)
5260 {
5261 /* skip the 'class' keyword */
5262 G_tok->next();
5263
5264 /* parse the object definition */
5265 return parse_object(err, FALSE, FALSE, TRUE, 0, FALSE);
5266 }
5267
5268 /* ------------------------------------------------------------------------ */
5269 /*
5270 * Parse a 'modify' statement
5271 */
5272 CTPNStmTop *CTcParser::parse_modify(int *err)
5273 {
5274 /* skip the 'modify' keyword */
5275 G_tok->next();
5276
5277 /* if 'grammar' follows, parse the grammar definition */
5278 if (G_tok->cur() == TOKT_GRAMMAR)
5279 return parse_grammar(err, FALSE, TRUE);
5280
5281 /* if 'function' follows, parse the function definition */
5282 if (G_tok->cur() == TOKT_FUNCTION || G_tok->cur() == TOKT_METHOD)
5283 return parse_function(err, FALSE, FALSE, TRUE, TRUE);
5284
5285 /* if a symbol and an open paren follow, parse the function definition */
5286 if (G_tok->cur() == TOKT_SYM)
5287 {
5288 /* we have a symbol - check the next token, but put it right back */
5289 int is_lpar = (G_tok->next() == TOKT_LPAR);
5290 G_tok->unget();
5291
5292 /* if a paren follows the symbol, it's a function */
5293 if (is_lpar)
5294 return parse_function(err, FALSE, FALSE, TRUE, FALSE);
5295 }
5296
5297 /* parse the object definition */
5298 return parse_object(err, FALSE, TRUE, FALSE, 0, FALSE);
5299 }
5300
5301 /*
5302 * Parse a 'replace' statement
5303 */
5304 CTPNStmTop *CTcParser::parse_replace(int *err)
5305 {
5306 /* skip the 'replace' keyword and see what follows */
5307 switch (G_tok->next())
5308 {
5309 case TOKT_FUNCTION:
5310 case TOKT_METHOD:
5311 /* replace the function */
5312 return parse_function(err, FALSE, TRUE, FALSE, TRUE);
5313
5314 case TOKT_CLASS:
5315 /* skip the 'class' keyword */
5316 G_tok->next();
5317
5318 /* parse the object definition */
5319 return parse_object(err, TRUE, FALSE, TRUE, 0, FALSE);
5320
5321 case TOKT_GRAMMAR:
5322 /* parse the 'grammar' definition */
5323 return parse_grammar(err, TRUE, FALSE);
5324
5325 case TOKT_SYM:
5326 case TOKT_TRANSIENT:
5327 /* replace the object or function definition */
5328 return parse_object_or_func(err, TRUE, 0, 0);
5329
5330 default:
5331 /* it's an error */
5332 G_tok->log_error_curtok(TCERR_REPLACE_REQ_OBJ_OR_FUNC);
5333
5334 /* skip the invalid token */
5335 G_tok->next();
5336 return 0;
5337 }
5338 }
5339
5340 /* ------------------------------------------------------------------------ */
5341 /*
5342 * Generic statement node
5343 */
5344
5345 /*
5346 * Add a debugging line record for the current statement. Call this just
5347 * before generating instructions for the statement; we'll create a line
5348 * record associating the current byte-code location with our source file
5349 * and line number.
5350 */
5351 void CTPNStmBase::add_debug_line_rec()
5352 {
5353 /* add a line record to the code generator's list */
5354 if (file_ != 0)
5355 G_cg->add_line_rec(file_, linenum_);
5356 }
5357
5358 /*
5359 * Add a debugging line record for a given position
5360 */
5361 void CTPNStmBase::add_debug_line_rec(CTcTokFileDesc *desc, long linenum)
5362 {
5363 /* add a line record to the code generator's list */
5364 G_cg->add_line_rec(desc, linenum);
5365 }
5366
5367 /* ------------------------------------------------------------------------ */
5368 /*
5369 * Export symbol
5370 */
5371
5372 /*
5373 * write to object file
5374 */
5375 void CTcPrsExport::write_to_obj_file(CVmFile *fp)
5376 {
5377 /* write our internal symbol name, with length prefix */
5378 fp->write_uint2((int)get_sym_len());
5379 fp->write_bytes(get_sym(), get_sym_len());
5380
5381 /* write our external name, with length prefix */
5382 fp->write_uint2((int)get_ext_len());
5383 fp->write_bytes(get_ext_name(), get_ext_len());
5384 }
5385
5386 /*
5387 * read from object file
5388 */
5389 CTcPrsExport *CTcPrsExport::read_from_obj_file(CVmFile *fp)
5390 {
5391 char buf[TOK_SYM_MAX_LEN + 1];
5392 size_t sym_len;
5393 size_t ext_len;
5394 const char *sym;
5395 const char *ext;
5396 CTcPrsExport *exp;
5397
5398 /* read the symbol */
5399 sym = CTcParser::read_len_prefix_str(fp, buf, sizeof(buf), &sym_len,
5400 TCERR_EXPORT_SYM_TOO_LONG);
5401 if (sym == 0)
5402 return 0;
5403
5404 /* read the external name */
5405 ext = CTcParser::read_len_prefix_str(fp, buf, sizeof(buf), &ext_len,
5406 TCERR_EXPORT_SYM_TOO_LONG);
5407 if (ext == 0)
5408 return 0;
5409
5410 /* create a new export record for the symbol */
5411 exp = new CTcPrsExport(sym, sym_len);
5412
5413 /* set the external name */
5414 exp->set_extern_name(ext, ext_len);
5415
5416 /* return the new record */
5417 return exp;
5418 }
5419
5420
5421 /* ------------------------------------------------------------------------ */
5422 /*
5423 * Add a generated object - static compilation mode only.
5424 */
5425 CTcSymObj *CTcParser::add_gen_obj_stat(CTcSymObj *cls)
5426 {
5427 /* allocate an object ID */
5428 tctarg_obj_id_t id = G_cg->new_obj_id();
5429
5430 /* set up an anonymous symbol for it */
5431 CTcSymObj *sym = new CTcSymObj(
5432 ".anon", 5, FALSE, id, FALSE, TC_META_TADSOBJ, 0);
5433
5434 /* create its object statement, and plug it into the symbol */
5435 CTPNStmObject *stm = new CTPNStmObject(sym, FALSE);
5436 sym->set_obj_stm(stm);
5437
5438 /* add the superclass, if we have one */
5439 if (cls != 0)
5440 stm->add_superclass(cls);
5441
5442 /* add the object to the top-level statement list */
5443 add_anon_obj(sym);
5444 add_nested_stm(stm);
5445
5446 /* return the object symbol */
5447 return sym;
5448 }
5449
5450 /*
5451 * Add a property to a generated object - static compilation mode only.
5452 */
5453 void CTcParser::add_gen_obj_prop_stat(
5454 CTcSymObj *obj, CTcSymProp *prop, const CTcConstVal *val)
5455 {
5456 /* add the property to the object statement */
5457 obj->get_obj_stm()->add_prop(prop, new CTPNConst(val), FALSE, FALSE);
5458 }
5459
5460
5461 /* ------------------------------------------------------------------------ */
5462 /*
5463 * Program Statement node
5464 */
5465
5466 /*
5467 * fold constants - we simply fold constants in each of our statements
5468 */
5469 CTcPrsNode *CTPNStmProg::fold_constants(class CTcPrsSymtab *symtab)
5470 {
5471 CTPNStmTop *cur;
5472
5473 /* iterate through our statements and fold constants in each one */
5474 for (cur = first_stm_ ; cur != 0 ; cur = cur->get_next_stm_top())
5475 cur->fold_constants(symtab);
5476
5477 /*
5478 * although nodes within our subtree might have been changed, this
5479 * compound statement itself is unchanged by constant folding
5480 */
5481 return this;
5482 }
5483
5484 /*
5485 * Generate code
5486 */
5487 void CTPNStmProg::gen_code(int, int)
5488 {
5489 CTPNStmTop *cur;
5490
5491 /*
5492 * iterate through our statements and generate code for each in
5493 * sequence
5494 */
5495 for (cur = first_stm_ ; cur != 0 ; cur = cur->get_next_stm_top())
5496 cur->gen_code(TRUE, TRUE);
5497
5498 /*
5499 * iterate again, this time checking local symbol tables - we must wait
5500 * to do this until after we've generated all of the code, because
5501 * local symbol tables can be cross-referenced in some cases (in
5502 * particular, when locals are shared through contexts, such as with
5503 * anonymous functions)
5504 */
5505 for (cur = first_stm_ ; cur != 0 ; cur = cur->get_next_stm_top())
5506 cur->check_locals();
5507 }
5508
5509 /* ------------------------------------------------------------------------ */
5510 /*
5511 * Dictionary entry - each dictionary object gets one of these objects to
5512 * track it
5513 */
5514
5515 /*
5516 * construction
5517 */
5518 CTcDictEntry::CTcDictEntry(CTcSymObj *sym)
5519 {
5520 const size_t hash_table_size = 128;
5521
5522 /* remember my object symbol and word truncation length */
5523 sym_ = sym;
5524
5525 /* no object file index yet */
5526 obj_idx_ = 0;
5527
5528 /* not in a list yet */
5529 nxt_ = 0;
5530
5531 /* create my hash table */
5532 hashtab_ = new (G_prsmem)
5533 CVmHashTable(hash_table_size,
5534 new (G_prsmem) CVmHashFuncCI(), TRUE,
5535 new (G_prsmem) CVmHashEntry *[hash_table_size]);
5536 }
5537
5538 /*
5539 * Add a word to the table
5540 */
5541 void CTcDictEntry::add_word(const char *txt, size_t len, int copy,
5542 tc_obj_id obj, tc_prop_id prop)
5543 {
5544 CVmHashEntryPrsDict *entry;
5545
5546 /* search for an existing entry */
5547 entry = (CVmHashEntryPrsDict *)hashtab_->find(txt, len);
5548
5549 /* if there's no entry, create a new one */
5550 if (entry == 0)
5551 {
5552 /* create a new item */
5553 entry = new (G_prsmem) CVmHashEntryPrsDict(txt, len, copy);
5554
5555 /* add it to the table */
5556 hashtab_->add(entry);
5557 }
5558
5559 /* add this object/property association to the word's hash table entry */
5560 entry->add_item(obj, prop);
5561 }
5562
5563 /*
5564 * Write my symbol to an object file
5565 */
5566 void CTcDictEntry::write_sym_to_obj_file(CVmFile *fp)
5567 {
5568 /* if I already have a non-zero index value, I've already been written */
5569 if (obj_idx_ != 0)
5570 return;
5571
5572 /* assign myself an object file dictionary index */
5573 obj_idx_ = G_prs->get_next_obj_file_dict_idx();
5574
5575 /* write my symbol to the object file */
5576 sym_->write_to_obj_file(fp);
5577 }
5578
5579 /* ------------------------------------------------------------------------ */
5580 /*
5581 * Grammar production entry
5582 */
5583
5584 /*
5585 * Write to an object file
5586 */
5587 void CTcGramProdEntry::write_to_obj_file(CVmFile *fp)
5588 {
5589 ulong cnt;
5590 CTcGramProdAlt *alt;
5591 ulong flags;
5592
5593 /* write the object file index of my production object symbol */
5594 fp->write_uint4(prod_sym_ == 0 ? 0 : prod_sym_->get_obj_file_idx());
5595
5596 /* set up the flags */
5597 flags = 0;
5598 if (is_declared_)
5599 flags |= 1;
5600
5601 /* write the flags */
5602 fp->write_uint4(flags);
5603
5604 /* count my alternatives */
5605 for (cnt = 0, alt = alt_head_ ; alt != 0 ;
5606 ++cnt, alt = alt->get_next()) ;
5607
5608 /* write the count */
5609 fp->write_uint4(cnt);
5610
5611 /* write each alternative */
5612 for (alt = alt_head_ ; alt != 0 ; alt = alt->get_next())
5613 alt->write_to_obj_file(fp);
5614 }
5615
5616
5617 /* ------------------------------------------------------------------------ */
5618 /*
5619 * Grammar production rule alternative object
5620 */
5621
5622 /*
5623 * Write to an object file
5624 */
5625 void CTcGramProdAlt::write_to_obj_file(CVmFile *fp)
5626 {
5627 ulong cnt;
5628 CTcGramProdTok *tok;
5629
5630 /* write my score and badness */
5631 fp->write_int2(score_);
5632 fp->write_int2(badness_);
5633
5634 /* write the index of my processor object symbol */
5635 fp->write_uint4(obj_sym_ == 0 ? 0 : obj_sym_->get_obj_file_idx());
5636
5637 /* write the dictionary index */
5638 fp->write_uint4(dict_ == 0 ? 0 : dict_->get_obj_idx());
5639
5640 /* count my tokens */
5641 for (cnt = 0, tok = tok_head_ ; tok != 0 ;
5642 ++cnt, tok = tok->get_next()) ;
5643
5644 /* write my token count */
5645 fp->write_uint4(cnt);
5646
5647 /* write the tokens */
5648 for (tok = tok_head_ ; tok != 0 ; tok = tok->get_next())
5649 tok->write_to_obj_file(fp);
5650 }
5651
5652 /* ------------------------------------------------------------------------ */
5653 /*
5654 * Grammar production token object
5655 */
5656
5657 /*
5658 * write to an object file
5659 */
5660 void CTcGramProdTok::write_to_obj_file(CVmFile *fp)
5661 {
5662 size_t i;
5663
5664 /* write my type */
5665 fp->write_uint2((int)typ_);
5666
5667 /* write my data */
5668 switch(typ_)
5669 {
5670 case TCGRAM_PROD:
5671 /* write my object's object file index */
5672 fp->write_uint4(val_.obj_ != 0
5673 ? val_.obj_->get_obj_file_idx() : 0);
5674 break;
5675
5676 case TCGRAM_TOKEN_TYPE:
5677 /* write my enum token ID */
5678 fp->write_uint4(val_.enum_id_);
5679 break;
5680
5681 case TCGRAM_PART_OF_SPEECH:
5682 /* write my property ID */
5683 fp->write_uint2(val_.prop_);
5684 break;
5685
5686 case TCGRAM_LITERAL:
5687 /* write my string */
5688 fp->write_uint2(val_.str_.len_);
5689 fp->write_bytes(val_.str_.txt_, val_.str_.len_);
5690 break;
5691
5692 case TCGRAM_STAR:
5693 /* no additional value data */
5694 break;
5695
5696 case TCGRAM_PART_OF_SPEECH_LIST:
5697 /* write the length */
5698 fp->write_uint2(val_.prop_list_.len_);
5699
5700 /* write each element */
5701 for (i = 0 ; i < val_.prop_list_.len_ ; ++i)
5702 fp->write_uint2(val_.prop_list_.arr_[i]);
5703
5704 /* done */
5705 break;
5706
5707 case TCGRAM_UNKNOWN:
5708 /* no value - there's nothing extra to write */
5709 break;
5710 }
5711
5712 /* write my property association */
5713 fp->write_uint2(prop_assoc_);
5714 }
5715