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