Debian Codegraph @

terps/tads/tads3/tcmain.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/tads3/TCMAIN.CPP,v 1.4 1999/07/11 00:46:53 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1999, 2002 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 tcmain.cpp - TADS 3 Compiler - main compiler driver
15 Function
16
17 Notes
18
19 Modified
20 04/22/99 MJRoberts - Creation
21 */
22
23 #include "t3std.h"
24 #include "vmerr.h"
25 #include "tcglob.h"
26 #include "tcmain.h"
27 #include "tcerr.h"
28 #include "tctok.h"
29 #include "utf8.h"
30 #include "charmap.h"
31 #include "tchost.h"
32 #include "tcprs.h"
33 #include "tcgen.h"
34 #include "tctarg.h"
35 #include "charmap.h"
36 #include "resload.h"
37 #include "tcunas.h"
38
39
40 /* ------------------------------------------------------------------------ */
41 /*
42 * statics
43 */
44
45 /* references to the error subsystem */
46 int CTcMain::err_refs_ = 0;
47
48 /* flag: we have failed to load external messages */
49 int CTcMain::err_no_extern_messages_ = FALSE;
50
51 /* console output character mapper */
52 CCharmapToLocal *CTcMain::console_mapper_ = 0;
53
54
55 /* ------------------------------------------------------------------------ */
56 /*
57 * Initialize the error subsystem for the compiler
58 */
59 void CTcMain::tc_err_init(size_t param_stack_size,
60 CResLoader *res_loader)
61 {
62 /* initialize the error stack */
63 err_init(1024);
64
65 /* if this is the first initializer, set things up */
66 if (err_refs_ == 0)
67 {
68 /* if we haven't loaded external compiler messages, load them */
69 if (!err_no_extern_messages_
70 && tc_messages == &tc_messages_english[0])
71 {
72 osfildef *fp;
73
74 /* try finding a message file */
75 fp = res_loader->open_res_file("t3make.msg", 0, "XMSG");
76 if (fp != 0)
77 {
78 /* try loading it */
79 err_load_message_file(fp, &tc_messages, &tc_message_count,
80 &tc_messages_english[0],
81 tc_message_count_english);
82
83 /* done with the file */
84 osfcls(fp);
85 }
86 else
87 {
88 /* note the failure, so we don't try again */
89 err_no_extern_messages_ = FALSE;
90 }
91 }
92 }
93
94 /* count the reference depth */
95 ++err_refs_;
96 }
97
98 /*
99 * terminate the error subsystem for the compiler
100 */
101 void CTcMain::tc_err_term()
102 {
103 /* reduce the reference count */
104 --err_refs_;
105
106 /* delete the error stack */
107 err_terminate();
108
109 /* if this is the last reference, clean things up */
110 if (err_refs_ == 0)
111 {
112 /* if we loaded an external message file, unload it */
113 err_delete_message_array(&tc_messages, &tc_message_count,
114 &tc_messages_english[0],
115 tc_message_count_english);
116 }
117 }
118
119 /* ------------------------------------------------------------------------ */
120 /*
121 * Initialize the compiler
122 */
123 void CTcMain::init(CTcHostIfc *hostifc, CResLoader *res_loader,
124 const char *default_charset)
125 {
126 /* initialize the error subsystem */
127 tc_err_init(1024, res_loader);
128
129 /* remember the host interface */
130 G_hostifc = hostifc;
131
132 /* perform static initializations on the parser symbol table class */
133 CTcPrsSymtab::s_init();
134
135 /*
136 * Set the image file structure sizes for the current T3 VM spec we're
137 * targeting. When we're doing dynamic compilation, the DynamicFunc
138 * object will overwrite these with the actual settings for the loaded
139 * image file before it invokes the code generator.
140 */
141 G_sizes.mhdr = TCT3_METHOD_HDR_SIZE;
142 G_sizes.exc_entry = TCT3_EXC_ENTRY_SIZE;
143 G_sizes.dbg_hdr = TCT3_DBG_HDR_SIZE;
144 G_sizes.dbg_line = TCT3_LINE_ENTRY_SIZE;
145 G_sizes.lcl_hdr = TCT3_DBG_LCLSYM_HDR_SIZE;
146 G_sizes.dbg_fmt_vsn = TCT3_DBG_FMT_VSN;
147 G_sizes.dbg_frame = TCT3_DBG_FRAME_SIZE;
148
149 /* assume we won't have a loaded image file metaclass table */
150 G_metaclass_tab = 0;
151
152 /* we don't have a dynamic compiler interface yet */
153 G_vmifc = 0;
154
155 /* create the compiler main object */
156 G_tcmain = new CTcMain(res_loader, default_charset);
157 }
158
159 /*
160 * Terminate the compiler
161 */
162 void CTcMain::terminate()
163 {
164 /* delete the tokenizer */
165 delete G_tok;
166 G_tok = 0;
167
168 /* delete the compiler main object */
169 delete G_tcmain;
170 G_tcmain = 0;
171
172 /* forget any object and property fixups */
173 G_objfixup = 0;
174 G_propfixup = 0;
175 G_enumfixup = 0;
176
177 /*
178 * make sure we explicitly turn the fixup flags on again if we want
179 * them in a future run
180 */
181 G_keep_objfixups = FALSE;
182 G_keep_propfixups = FALSE;
183 G_keep_enumfixups = FALSE;
184
185 /* perform static termination on the parser symbol table class */
186 CTcPrsSymtab::s_terminate();
187
188 /* forget the host interface */
189 G_hostifc = 0;
190
191 /* terminate the error subsystem */
192 tc_err_term();
193 }
194
195 /* ------------------------------------------------------------------------ */
196 /*
197 * set up the compiler
198 */
199 CTcMain::CTcMain(CResLoader *res_loader, const char *default_charset)
200 {
201 char csbuf[OSFNMAX];
202
203 /*
204 * if the caller didn't provide a default character set, ask the OS
205 * what we should use
206 */
207 if (default_charset == 0)
208 {
209 /*
210 * ask the OS what to use for file contents, since we use this
211 * character set to translate the text we read from source files
212 */
213 os_get_charmap(csbuf, OS_CHARMAP_FILECONTENTS);
214
215 /* use our OS-provided character set */
216 default_charset = csbuf;
217 }
218
219 /* if there's no static console output character map, create one */
220 if (console_mapper_ == 0)
221 {
222 char mapname[32];
223
224 /* get the console character set name */
225 os_get_charmap(mapname, OS_CHARMAP_DISPLAY);
226
227 /* create a resource loader for the console character map */
228 console_mapper_ = CCharmapToLocal::load(res_loader, mapname);
229
230 /* if that failed, create an ASCII mapper */
231 if (console_mapper_ == 0)
232 console_mapper_ = CCharmapToLocal::load(res_loader, "us-ascii");
233 }
234
235 /* remember our resource loader */
236 res_loader_ = res_loader;
237
238 /*
239 * set default options - minimum verbosity, no numeric error codes,
240 * show standard warnings but not pedantic warnings, not test mode
241 */
242 err_options_ = TCMAIN_ERR_WARNINGS;
243
244 /* we have no warning suppression list yet */
245 suppress_list_ = 0;
246 suppress_cnt_ = 0;
247
248 /* remember our default character set */
249 default_charset_ = lib_copy_str(default_charset);
250
251 /* create the tokenizer */
252 G_tok = new CTcTokenizer(res_loader_, default_charset_);
253
254 /*
255 * Create the parser and node memory pool. Create the memory pool
256 * first, because the parser allocates objects out of the pool.
257 */
258 G_prsmem = new CTcPrsMem();
259 G_prs = new CTcParser();
260
261 /* create the generator data stream (for constant data) */
262 G_ds = new CTcDataStream(TCGEN_DATA_STREAM);
263
264 /* create the primary generator code stream */
265 G_cs_main = new CTcCodeStream(TCGEN_CODE_STREAM);
266
267 /* create the static initializer code stream */
268 G_cs_static = new CTcCodeStream(TCGEN_STATIC_CODE_STREAM);
269
270 /* make the primary code stream active */
271 G_cs = G_cs_main;
272
273 /* create the generator object data stream */
274 G_os = new CTcDataStream(TCGEN_OBJECT_STREAM);
275
276 /* create the intrinsic class modifier object data stream */
277 G_icmod_stream = new CTcDataStream(TCGEN_ICMOD_STREAM);
278
279 /* create the dictionary object data stream */
280 G_dict_stream = new CTcDataStream(TCGEN_DICT_STREAM);
281
282 /* create the grammar-production object data stream */
283 G_gramprod_stream = new CTcDataStream(TCGEN_GRAMPROD_STREAM);
284
285 /* create the BigNumber object data stream */
286 G_bignum_stream = new CTcDataStream(TCGEN_BIGNUM_STREAM);
287
288 /* create the RexPattern object data stream */
289 G_rexpat_stream = new CTcDataStream(TCGEN_REXPAT_STREAM);
290
291 /* create the IntrinsicClass object data stream */
292 G_int_class_stream = new CTcDataStream(TCGEN_INTCLASS_STREAM);
293
294 /* create the static initializer identifier stream */
295 G_static_init_id_stream = new CTcDataStream(TCGEN_STATIC_INIT_ID_STREAM);
296
297 /* create the local variable name stream */
298 G_lcl_stream = new CTcDataStream(TCGEN_LCL_VAR_STREAM);
299
300 /* create the target-specific code generator */
301 G_cg = new CTcGenTarg();
302
303 /* initialize the parser */
304 G_prs->init();
305
306 /* no errors or warnings yet */
307 error_count_ = 0;
308 warning_count_ = 0;
309 first_error_ = 0;
310 first_warning_ = 0;
311
312 /* set a fairly liberal maximum error limit */
313 max_error_count_ = 100;
314
315 /* there's no disassembly output stream yet */
316 G_disasm_out = 0;
317 }
318
319
320 /* ------------------------------------------------------------------------ */
321 /*
322 * delete the compiler driver
323 */
324 CTcMain::~CTcMain()
325 {
326 /* if there's a disassembly stream, delete it */
327 if (G_disasm_out != 0)
328 delete G_disasm_out;
329
330 /* delete the various data streams */
331 delete G_cs_main;
332 delete G_cs_static;
333 delete G_ds;
334 delete G_os;
335 delete G_icmod_stream;
336 delete G_dict_stream;
337 delete G_gramprod_stream;
338 delete G_bignum_stream;
339 delete G_rexpat_stream;
340 delete G_int_class_stream;
341 delete G_static_init_id_stream;
342 delete G_lcl_stream;
343
344 /* delete the console output character map, if there is one */
345 if (console_mapper_ != 0)
346 {
347 /* release our reference on it */
348 console_mapper_->release_ref();
349
350 /* forget it (since it's static) */
351 console_mapper_ = 0;
352 }
353
354 /* delete the target-specific code generator */
355 delete G_cg;
356
357 /* delete the parser and node memory pool */
358 delete G_prs;
359 delete G_prsmem;
360
361 /* delete the parser */
362 delete G_tok;
363
364 /* delete our default character set name string */
365 lib_free_str(default_charset_);
366 }
367
368
369 /* ------------------------------------------------------------------------ */
370 /*
371 * Log an error, part 1: show the message prefix. This should be called
372 * before formatting the text of the message, and part 2 should be called
373 * after formatting the message text. Returns a pointer to the message
374 * template text.
375 */
376 static const char *log_msg_internal_1(
377 CTcTokFileDesc *linedesc, long linenum,
378 int *err_counter, int *warn_counter, int *first_error, int *first_warning,
379 unsigned long options, const int *suppress_list, size_t suppress_cnt,
380 tc_severity_t severity, int err)
381 {
382 const char *msg;
383 const char *prefix;
384
385 /*
386 * If this is a warning or a pedantic warning, and it's in the list of
387 * suppressed messages, ignore it entirely.
388 */
389 if (severity == TC_SEV_PEDANTIC || severity == TC_SEV_WARNING)
390 {
391 size_t rem;
392 const int *p;
393
394 /* scan the suppress list */
395 for (p = suppress_list, rem = suppress_cnt ; rem != 0 ; ++p, --rem)
396 {
397 /* check for a match */
398 if (*p == err)
399 {
400 /* it's in the suppress list - ignore the error */
401 return 0;
402 }
403 }
404 }
405
406 /* increment the appropriate counter */
407 switch(severity)
408 {
409 case TC_SEV_INFO:
410 /*
411 * we don't need to count informational messages, and no prefix is
412 * required
413 */
414 prefix = "";
415 break;
416
417 case TC_SEV_PEDANTIC:
418 /*
419 * if we're not in "pedantic" mode, or we're not even showing
420 * regular errors, ignore it
421 */
422 if (!(options & TCMAIN_ERR_PEDANTIC)
423 || !(options & TCMAIN_ERR_WARNINGS))
424 return 0;
425
426 /* if this is the first warning, remember the code */
427 if (*warn_counter == 0 && first_warning != 0)
428 *first_warning = err;
429
430 /* count it */
431 ++(*warn_counter);
432
433 /* set the prefix */
434 prefix = "warning";
435 break;
436
437 case TC_SEV_WARNING:
438 /* if we're suppressing warnings, ignore it */
439 if (!(options & TCMAIN_ERR_WARNINGS))
440 return 0;
441
442 /* if this is the first warning, remember the code */
443 if (*warn_counter == 0 && first_warning != 0)
444 *first_warning = err;
445
446 /* count the warning */
447 ++(*warn_counter);
448
449 /* use an appropriate prefix */
450 prefix = "warning";
451 break;
452
453 case TC_SEV_ERROR:
454 /* if this is the first error, remember the code */
455 if (*err_counter == 0 && first_error != 0)
456 *first_error = err;
457
458 /* count the error */
459 ++(*err_counter);
460
461 /* use an appropriate prefix */
462 prefix = "error";
463 break;
464
465 case TC_SEV_FATAL:
466 /* if this is the first error, remember the code */
467 if (*err_counter == 0 && first_error != 0)
468 *first_error = err;
469
470 /* count this as an error */
471 ++(*err_counter);
472
473 /* use an appropriate prefix */
474 prefix = "fatal error";
475 break;
476
477 case TC_SEV_INTERNAL:
478 /* if this is the first error, remember the code */
479 if (*err_counter == 0 && first_error != 0)
480 *first_error = err;
481
482 /* count this as an error */
483 ++(*err_counter);
484
485 /* use an appropriate prefix */
486 prefix = "internal error";
487 break;
488 }
489
490 /* display the current parsing position, if available */
491 if (linedesc != 0)
492 {
493 const char *fname;
494 char qu_buf[OSFNMAX*2 + 2];
495
496 /* get the filename from the source descriptor */
497 fname = linedesc->get_fname();
498
499 /*
500 * if we're in test reporting mode, show only the root part of the
501 * filename
502 */
503 if ((options & TCMAIN_ERR_TESTMODE) != 0)
504 fname = os_get_root_name((char *)fname);
505
506 /* if they want quoted filenames, quote the filename */
507 if ((options & TCMAIN_ERR_FNAME_QU) != 0)
508 {
509 const char *src;
510 char *dst;
511
512 /* quote each character of the filename */
513 for (src = fname, qu_buf[0] = '"', dst = qu_buf + 1 ;
514 *src != '\0' ; )
515 {
516 /* if this is a quote character, stutter it */
517 if (*src == '"')
518 *dst++ = '"';
519
520 /* add this character */
521 *dst++ = *src++;
522 }
523
524 /* add a closing quote and trailing null */
525 *dst++ = '"';
526 *dst = '\0';
527
528 /* use the quoted version of the filename */
529 fname = qu_buf;
530 }
531
532 /* show the filename and line number prefix */
533 G_hostifc->print_err("%s(%ld): ", fname, linenum);
534 }
535
536 /* display the error type prefix */
537 G_hostifc->print_err("%s", prefix);
538
539 /* add the error number, if we're showing error numbers */
540 if ((options & TCMAIN_ERR_NUMBERS) != 0 && severity != TC_SEV_INFO)
541 G_hostifc->print_err(" %u", (unsigned int)err);
542
543 /* add a colon and a space for separation */
544 G_hostifc->print_err(": ");
545
546 /* get the error message */
547 msg = tcerr_get_msg(err, (options & TCMAIN_ERR_VERBOSE) != 0);
548 if (msg == 0)
549 msg = "[Unable to find message text for this error code. "
550 "This might indicate an internal problem with the compiler, "
551 "or it might be caused by an installation problem that is "
552 "preventing the compiler from finding an external message "
553 "file that it requires.]";
554
555 /* return the message text, so the caller can format it with arguments */
556 return msg;
557 }
558
559 /*
560 * Log an error, part 2: show the message suffix.
561 */
562 static void log_msg_internal_2(unsigned long options, tc_severity_t severity)
563 {
564 /*
565 * if we're in verbose mode, and this is an internal error, add the
566 * internal error explanation text
567 */
568 if (severity == TC_SEV_INTERNAL && (options & TCMAIN_ERR_VERBOSE) != 0)
569 {
570 const char *msg;
571
572 /* get the internal error explanation text and display it */
573 msg = tcerr_get_msg(TCERR_INTERNAL_EXPLAN, TRUE);
574 if (msg != 0)
575 G_hostifc->print_err("\n%s", msg);
576 }
577
578 /* end the line */
579 G_hostifc->print_err((options & TCMAIN_ERR_VERBOSE) != 0 ? "\n\n" : "\n");
580 }
581
582 /* ------------------------------------------------------------------------ */
583 /*
584 * Print an error message. Word-wrap the message if we're in verbose mode.
585 */
586 static void format_message(const char *msg, unsigned long options)
587 {
588 /*
589 * if we're in verbose mode, word-wrap to 80 columns; otherwise just
590 * print it as-is
591 */
592 if ((options & TCMAIN_ERR_VERBOSE) != 0)
593 {
594 const char *p;
595 const int line_wid = 79;
596
597 /* start on a new line, to skip any prefix text */
598 G_hostifc->print_err("\n");
599
600 /* word-wrap to 80 columns */
601 for (p = msg ; *p != '\0' ; )
602 {
603 const char *start;
604 const char *sp;
605
606 /* find the next word break */
607 for (sp = 0, start = p ; ; )
608 {
609 /* if this is a space, note it */
610 if (*p == ' ')
611 sp = p;
612
613 /*
614 * if we're at the end of the line, or we're over the line
615 * width and we found a space, break here
616 */
617 if (*p == '\0' || (p - start >= line_wid && sp != 0))
618 {
619 /* if we've reached the end, print the rest */
620 if (*p == '\0')
621 sp = p;
622
623 /* trim off trailing spaces */
624 for ( ; sp > start && *(sp-1) == ' ' ; --sp) ;
625
626 /* show this part */
627 G_hostifc->print_err("%.*s\n", sp - start, start);
628
629 /* skip leading spaces */
630 for ( ; *sp == ' ' ; ++sp) ;
631
632 /* if this is the end of the line, we're done */
633 if (*p == '\0')
634 break;
635
636 /* start over here */
637 start = p = sp;
638 }
639 else
640 {
641 /* this one fits - skip it */
642 ++p;
643 }
644 }
645 }
646 }
647 else
648 {
649 /* display it */
650 G_hostifc->print_err("%s", msg);
651 }
652 }
653
654 /* ------------------------------------------------------------------------ */
655 /*
656 * log an error from an exception object
657 */
658 void CTcMain::S_log_error(CTcTokFileDesc *linedesc, long linenum,
659 int *err_counter, int *warn_counter,
660 unsigned long options,
661 const int *suppress_list, size_t suppress_cnt,
662 tc_severity_t severity, CVmException *exc)
663 {
664 const char *msg;
665 char msgbuf[1024];
666
667 /* show the prefix */
668 msg = log_msg_internal_1(linedesc, linenum, err_counter, warn_counter,
669 0, 0, options, suppress_list, suppress_cnt,
670 severity, exc->get_error_code());
671
672 /* if the message is suppressed, we're done */
673 if (msg == 0)
674 return;
675
676 /* format the message using arguments stored in the exception */
677 err_format_msg(msgbuf, sizeof(msgbuf), msg, exc);
678
679 /* print the error */
680 format_message(msgbuf, options);
681
682 /* show the suffix */
683 log_msg_internal_2(options, severity);
684 }
685
686 /* ------------------------------------------------------------------------ */
687 /*
688 * Log an error using va_list arguments
689 */
690 void CTcMain::S_v_log_error(CTcTokFileDesc *linedesc, long linenum,
691 int *err_counter, int *warn_counter,
692 int *first_error, int *first_warning,
693 unsigned long options,
694 const int *suppress_list, size_t suppress_cnt,
695 tc_severity_t severity, int err, va_list args)
696 {
697 const char *msg;
698 char msgbuf[2048];
699
700 /* show the prefix */
701 msg = log_msg_internal_1(linedesc, linenum, err_counter, warn_counter,
702 first_error, first_warning, options,
703 suppress_list, suppress_cnt, severity, err);
704
705 /* if the message is suppressed, we're done */
706 if (msg == 0)
707 return;
708
709 /* format the message using the va_list argument */
710 t3vsprintf(msgbuf, sizeof(msgbuf), msg, args);
711
712 /* display it */
713 format_message(msgbuf, options);
714
715 /* show the suffix */
716 log_msg_internal_2(options, severity);
717 }
718
719 /* ------------------------------------------------------------------------ */
720 /*
721 * Check the current error count against the maximum error limit, and throw
722 * a fatal error if we've reached the limit.
723 */
724 void CTcMain::check_error_limit()
725 {
726 /* check the error count against the limit */
727 if (error_count_ > max_error_count_)
728 {
729 /*
730 * raise the maximum error count a bit so that we don't encounter
731 * another maximum error situation and loop on flagging the
732 * too-many-errors error while trying to display a too-many-errors
733 * error
734 */
735 max_error_count_ = error_count_ + 100;
736
737 /* display a message explaining the problem */
738 log_error(G_tok->get_last_desc(), G_tok->get_last_linenum(),
739 TC_SEV_FATAL, TCERR_TOO_MANY_ERRORS);
740
741 /* throw the generic fatal error, since we've logged this */
742 err_throw(TCERR_FATAL_ERROR);
743 }
744 }
745