forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlalex.c
1877 lines (1763 loc) · 64.3 KB
/
lalex.c
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*ident "@(#)cls4:src/lalex.c 1.21" */
/*******************************************************************************
C++ source for the C++ Language System, Release 3.0. This product
is a new release of the original cfront developed in the computer
science research center of AT&T Bell Laboratories.
Copyright (c) 1993 UNIX System Laboratories, Inc.
Copyright (c) 1991, 1992 AT&T and UNIX System Laboratories, Inc.
Copyright (c) 1984, 1989, 1990 AT&T. All Rights Reserved.
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE of AT&T and UNIX System
Laboratories, Inc. The copyright notice above does not evidence
any actual or intended publication of such source code.
lalex.c:
lookahead
*****************************************************************************/
#include <stdio.h>
#include "cfront.h"
#include "yystype.h"
#include "tqueue.h"
#include "template.h"
#ifdef DBG
#define LDB(val, a) \
{ \
if (Ldebug >= val) { \
a; \
} \
}
#else
#define LDB(val, a) /**/
#endif
// static data members definition
int templ_compilation::parameters_in_progress = 0;
toknode *toknode::free_toks = 0;
#ifdef DBG
extern "C" char *image(int t) {
if (keys[t])
return keys[t];
else {
static char b[20];
sprintf(b, "token(%d)", t);
return b;
}
}
extern "C" void printok2(TOK tk, YYSTYPE rv, loc wh) {
switch (tk) {
default:
fprintf(stderr, "\t%s", image(tk));
break;
case ID:
case ICON:
case CCON:
case FCON:
case STRING:
fprintf(stderr, "\tID '%s'", rv.s);
break;
case TNAME:
fprintf(stderr, "\tTNAME '%s'", rv.pn->string);
break;
case NAME:
fprintf(stderr, "\tID '%s'", rv.pn->string);
break;
case PTNAME:
fprintf(stderr, "\tPTNAME '%s'", rv.pn->string);
break;
case AGGR:
fprintf(stderr, "\tAGGR '%s'", image(rv.t));
break;
case TYPE:
fprintf(stderr, "\tTYPE '%s'", image(rv.t));
break;
case TSCOPE:
fprintf(stderr, "\tTSCOPE '%s'::", rv.pn->string);
break;
case MEMPTR:
fprintf(stderr, "\tMEMPTR '%s'::*", rv.pn->string);
break;
}
putc(' ', stderr);
wh.put(stderr);
putc('\n', stderr);
fflush(stderr);
}
extern "C" void printok(toknode *t, char *id = 0) {
if (id)
fprintf(stderr, "%s:", id);
if (t == 0) {
fprintf(stderr, " NULL TOKNODE!\n");
fflush(stderr);
} else
printok2(t->tok, t->retval, t->place);
}
extern "C" void showQ(char *where)
/*
display token Q
*/
{
fprintf(stderr, "TOKEN Q (%s):\n", where);
for (register toknode *t = front; t; t = t->next)
printok(t);
putc('\n', stderr);
fflush(stderr);
}
#endif
int bl_level;
static int laexpr(TOK);
static int latype(TOK);
static int la_decl(int);
static TOK lookahead();
/* make this a toknode! */
static int lasttk = 0; // one token history
static YYSTYPE lastval; // yylval lasttk value
static int must_be_expr = 0; // handle redundant parentheses
int must_be_id = 0; // !0, TNAME => ID, i.e., int X
loc curloc;
int curr_file;
static toknode *latok; // current lookahead token
toknode *front = 0;
static toknode *rear = 0;
const int TQCHUNK = 16;
void *toknode::operator new(size_t) {
register toknode *p;
if ((p = free_toks) == 0) {
register toknode *q;
free_toks = q = (toknode *) new char[TQCHUNK * sizeof(toknode)];
p = free_toks;
for (; q != &p[TQCHUNK - 1]; q->next = q + 1, ++q)
;
q->next = 0;
}
free_toks = p->next;
return p;
}
toknode::toknode(TOK t, YYSTYPE r, loc tloc) {
tok = t;
used = 0;
retval = r;
place = tloc;
next = last = 0;
}
void toknode::operator delete(void *vp, size_t) {
register toknode *p = (toknode *) vp;
p->next = free_toks;
free_toks = p;
vp = 0;
}
static void add_tokens()
/*
extend lookahead token queue when depleted
*/
{
TOK tk = tlex();
if (tk != ID)
return;
while (tk == ID || tk == MEM || tk == DOT)
tk = tlex();
}
#define USE_TOKEN(T, W) \
LDB(2, error('d', &(T)->place, "use_token('%k','%s')", (T)->tok, W);); \
if (!(T)->used) \
use_token(T);
// SYM -- removed Ptype return_nstd_local_type(Pname,TOK&)
// SYM -- removed Pname local_nested_kludge( Pname n, Pname tn )
enum { one_back, two_back };
static TOK last_tokens[2]; // TSCOPE not reduced at this point
static Pname last_tname; // tname :: id, where id is nested class
static void use_token(toknode *T)
/*
lookup TNAMEs here instead of in tlex()
maintain block level
*/
{
T->used = 1;
static bit aggr = 0;
if (T->tok == AGGR || T->tok == ENUM)
aggr = 1;
else if ((T->tok != MEM) && (T->tok != ID))
aggr = 0;
DB(if (Ldebug >= 1) {
error('d', &T->place, "\n*** use_token(%k )", T->tok);
printok(T);
error('D', &T->place, " lasttk%k last_tname%n last tokens%k%k", lasttk, last_tname,
last_tokens[one_back], last_tokens[two_back]);
});
switch (T->tok) {
case REF:
case DOT: {
toknode *t = T;
Pname q = 0, r = 0;
for (;;) {
if (t->next == 0)
add_tokens();
t = t->next;
if (t->tok == ID && t->next->tok == MEM) {
Pname n = new name(t->retval.s);
n->base = MEMQ;
if (q == 0)
q = r = n;
else {
r->n_list = n;
r = n;
}
t = t->next;
} else if (t->tok == MEM) {
Pname n = new name();
n->base = MEMQ;
if (q == 0)
q = r = n;
else {
r->n_list = n;
r = n;
}
} else
break;
}
if (q) {
toknode *x = T->next, *xx = x->next;
x->tok = MEMQ;
x->retval.pn = q;
x->used = 1;
x->next = t;
t->last->next = 0;
t->last = x;
for (; xx; xx = x) {
x = xx->next;
delete xx;
}
}
break;
}
case ID:
if (last_tokens[one_back] == MEMQ)
break;
{
Pname n = 0;
TOK sc = T->next && T->next->tok == MEM || aggr ? HIDDEN : 0;
// error('d', &T->place, "use_token: %s", T->retval.s );
// look up in correct table
if (last_tokens[one_back] == MEM
|| (last_tokens[one_back] == TSCOPE && !templp->in_progress)) {
if (last_tokens[two_back] == TNAME
|| (last_tokens[two_back] == GT && last_tokens[one_back] != MEM)) {
if (last_tokens[two_back] == GT && last_tokens[one_back] == TSCOPE) {
extern YYSTYPE yyval;
last_tname = yyval.pn;
}
// TNAME :: ID
Pname tn = last_tname;
if (tn == 0)
error('i', &T->place, "last_tname not set for tname::%s", T->retval.s);
while (tn->tp && tn->tp->base == TYPE)
tn = Pbase(tn->tp)->b_name;
if (strcmp(T->retval.s, tn->string) == 0) {
// X::X or X::~X -- leave as TNAME here
n = tn;
} else if (tn->tp && tn->tp->base == COBJ) {
Pclass cl = Pclass(Pbase(tn->tp)->b_name->tp);
// X::X or X::~X -- leave as TNAME here
if (cl->is_templ_instance()
&& strcmp(T->retval.s, Ptclass(cl)->unparametrized_tname()->string)
== 0) {
n = tn;
} else if ((cl->defined & (DEF_SEEN | DEFINED)) == 0)
error(&T->place, "%n:: %s -- %tU", last_tname, T->retval.s, cl);
else {
n = k_find_member(T->retval.s, cl, sc);
if (n && n->n_ktable == Gtbl)
n = 0;
}
} else {
if (tn->tp->base != ANY) // don't flag Template formal
error(&T->place, "%n:: %s --%n not aCN", tn, T->retval.s, tn);
n = k_find_name(T->retval.s, Ctbl, sc);
}
} else if (last_tokens[two_back] != ID) {
// :: ID
n = k_find_name(T->retval.s, Gtbl, sc);
}
} else // look in current scope
n = k_find_name(T->retval.s, Ctbl, sc);
T->idname = n;
if (n && n->base == TNAME) {
T->tok = TNAME;
T->retval.pn = n;
DB(if (Ldebug >= 1) error('d', &T->place, "use_token: tname%n", T->retval.pn););
}
// SYM -- remove nn = local_nested_kludge(nn,ntd==NESTED?n:0);
#ifdef DBG
else if (Ldebug >= 1)
error('d', &T->place, "use_token: id %s", T->retval.s);
#endif
break;
}
case LC:
++bl_level;
break;
case RC:
--bl_level;
break;
}
if (T->tok != COMPL || last_tokens[one_back] != MEM) {
last_tokens[two_back] = last_tokens[one_back];
last_tokens[one_back] = T->tok;
if (T->tok == TNAME)
last_tname = T->retval.pn;
}
}
static void la_reset(toknode *T, Pname qual) {
// just failed a lookahead for fptr declarator after qualified TNAME
// TNAME::TNAME ( ... )
// probably a member ftn declaration
// "unuse" tokens after T so names will be searched in correct scope
// T should == '('
last_tokens[one_back] = MEM;
last_tokens[two_back] = TNAME;
last_tname = qual;
while (T && T->used) {
T->used = 0;
switch (T->tok) {
case TNAME:
T->tok = ID;
T->retval.s = T->retval.pn->string;
break;
case LC:
--bl_level;
break; // backtrack
case RC:
++bl_level;
break; // backtrack
}
T = T->next;
}
}
void addtok(TOK t, YYSTYPE r, loc tloc) {
toknode *T = new toknode(t, r, tloc);
if (front == 0)
front = rear = T;
else {
rear->next = T;
T->last = rear;
rear = T;
}
// error('d',&tloc,"addtok: %k '%s'",t,t==ID?r.s:"");
// showQ("addtok");
}
static Pname idname;
extern TOK deltok(int noset = 0) {
register toknode *T = front;
USE_TOKEN(T, "deltok");
register TOK tk = T->tok;
if (!noset) {
yylval = T->retval;
curloc = T->place;
}
curr_file = curloc.file;
if (front = front->next)
front->last = 0;
else
latok = rear = 0;
idname = T->idname;
delete T;
return tk;
}
static void del_tokens(toknode *marker)
/*
delete tokens from marker to latok, not inclusive
*/
{
if (marker == 0 || marker == latok || marker->next == 0)
error('i', "bad token queue");
LDB(2, fprintf(stderr, "del_tokens: %s..%s\n", image(marker->tok), image(latok->tok)));
register toknode *tt = marker->next;
if (tt == latok)
return;
marker->next = latok;
latok->last->next = 0;
latok->last = marker;
register toknode *tx = tt;
do {
LDB(3, fprintf(stderr, " deleting %s\n", image(tt->tok)));
tx = tx->next;
delete tt;
tt = tx;
} while (tx);
}
static void del_1(toknode *t)
// delete t from the queue
{
if (t->last)
t->last->next = t->next;
else
front = t->next;
if (latok == t)
latok = t->last ? t->last : front;
if (t->next)
t->next->last = t->last;
else
rear = t->last;
delete t;
}
extern TOK la_look()
/*
peek at head of token queue
*/
{
LDB(1, fprintf(stderr, "\n*** la_look()\n"));
if (front == 0)
add_tokens();
latok = front;
USE_TOKEN(latok, "la_look");
LDB(1, fprintf(stderr, " -- %s\n", image(latok->tok)));
return latok->tok;
}
#define NEXTTOK() ((yychar == -1) ? (yychar = lalex(), yychar) : yychar)
void check_decl()
/*
Lookahead to direct parsing of local/arg type declarations
la_decl() returns 1 if lookahead sees a declaration.
*/
{
TOK tk2;
switch (NEXTTOK()) {
default:
break;
case TSCOPE: // XXX
DB(if (Ldebug >= 1) error('d', "check_decl() tscope%n...", yylval.pn););
tk2 = la_look();
while (tk2 == TSCOPE)
tk2 = lookahead();
if (tk2 == TNAME) {
toknode *t = latok;
if (t->tok != TNAME)
error('i', &t->place, "check_decl() token scan");
tk2 = lookahead();
if (tk2 == LP && la_decl(in_arg_list)) {
t->tok = DECL_MARKER; // TNAME
}
}
DB(if (Ldebug >= 1) error('d', "%k", DECL_MARKER););
break;
case DECL_MARKER:
if (in_arg_list == 0)
break;
// gag! -- re-scan for declaration in case previous call
// to la_decl() came before reduction of arg_lp
// (occurs if la_decl() were called from lalex())
yychar = DECL_TYPE;
DECL_TYPE = 0;
if (yychar != TYPE && yychar != TNAME)
error('i', "check_decl() failed rescanning arg decl; yychar==%d", yychar);
// no break;
case TYPE:
case TNAME:
DB(if (Ldebug >= 1) error('d', "check_decl() %s", yychar == TYPE ? "TYPE" : "TNAME"););
if (la_look() == LP && la_decl(in_arg_list)) {
must_be_id = 0;
DECL_TYPE = yychar;
yychar = DECL_MARKER;
DB(if (Ldebug >= 1) error('d', "%k", DECL_MARKER););
}
}
}
void check_cast()
/*
Lookahead to direct parsing of cast
la_cast() returns 1 if lookahead sees an ambiguous old-style C cast.
*/
{
TOK tk2;
switch (NEXTTOK()) {
case TSCOPE: // XXX
tk2 = la_look();
while (tk2 == TSCOPE)
tk2 = lookahead();
if (tk2 == TNAME) {
toknode *t = latok;
if (t->tok != TNAME)
error('i', &t->place, "check_cast() token scan");
tk2 = lookahead();
if (tk2 == LP && la_decl(in_arg_list)) {
t->tok = DECL_MARKER; // TNAME
}
}
break;
case TYPE:
case TNAME:
if (la_look() == LP && la_cast()) {
must_be_id = 0;
DECL_TYPE = yychar;
yychar = DECL_MARKER;
}
}
}
static int latype(TOK t) {
switch (t) {
default: // includes friend, typedef, storage classes, etc.
return 0;
case CHAR:
case SHORT:
case INT:
case LONG:
case FLOAT:
case DOUBLE:
case UNSIGNED:
return 1;
}
}
static int laexpr(TOK t) {
switch (t) {
default:
return 0;
case RETURN:
case NEW:
case AND:
case ANDAND:
case OR:
case OROR:
case SIZEOF:
case NOT:
case COMPL:
case MUL:
case PLUS:
case MINUS:
case ER:
case ASSIGN:
case ASOP:
case RELOP:
case EQUOP:
case DIVOP:
case SHIFTOP:
case ICOP:
return 1;
}
}
static toknode *get_next_token(toknode *t) {
if (!t->next)
add_tokens();
return t->next;
}
static int template_tscope(Pname tn, toknode *lt)
/* provide the looakhead for determining TSCOPE tokens when the name is a
* parametrized type name; the lookahead here is non-trivial, because it
* involves stepping over the template arguments.
*/
{
int nest = 0; // the LT has been fetched
if (lt->tok != LT)
error('i', "a `<' token was expected");
// assume the worst, ensure that name strings are consed in the heap
templp->parameters_in_progress++;
for (toknode *t = lt;; t = get_next_token(t))
switch (t->tok) {
case LT:
++nest;
continue;
case GT:
// ***************
// need to fold in awareness of x::y::z
if (--nest == 0) {
t = get_next_token(t);
if (t->tok == MEM || t->tok == TSCOPE) {
// determine whether it is a memptr
if (t->next == 0)
add_tokens();
if (t->next->tok == MUL) {
t->tok = MEMPTR;
del_1(t->next);
} else {
t->tok = TSCOPE;
// handle X<T>::Y ...
if (t->next->tok == ID && templp->in_progress) {
Pname cn = tn->tp->is_cl_obj();
if (cn) {
int hh = t->next->next->tok == MEM;
Pname tn2 = k_find_member(t->next->retval.s, Pclass(cn->tp), hh);
if (tn2 && tn2->base == TNAME) {
t->next->tok = TNAME;
t->next->retval.pn = tn2;
}
}
}
}
t->retval.pn = tn;
--templp->parameters_in_progress;
return 1;
} else {
--templp->parameters_in_progress;
return 0;
}
}
continue;
case SM:
case LC:
case RC: // a quick exit in case of error
case EOFTOK:
--templp->parameters_in_progress;
return 0;
default:
continue;
}
}
static TOK lookahead()
/*
advance lookahead pointer, lexing at end of Q
handle occurrences of TNAME and TSCOPE
(should be kept up to date with lalex())
*/
{
TOK tk;
TOK tk2;
TOK prev_tk = 0;
YYSTYPE lastval;
if (latok == rear) {
add_tokens();
if (latok)
latok = latok->next;
else
latok = front;
} else
latok = latok->next;
if (latok->last) {
prev_tk = latok->last->tok;
lastval = latok->last->retval;
}
nexttok:
USE_TOKEN(latok, "lookahead1");
tk = latok->tok;
if (tk == ID || tk == TNAME) {
if (latok->next == 0)
add_tokens();
USE_TOKEN(latok->next, "lookahead2");
/* TOK */ tk2 = latok->next->tok;
if (tk == TNAME) {
if (tk2 == LT) {
// a parametrized type name -- differentiate from TNAME
// so that it can be dealt with in the grammar.
if (template_tscope(latok->retval.pn, latok->next))
tk = PTNAME;
} else if (tk2 == MEM || tk2 == DOT) {
tscope:
tk = TSCOPE;
// error('d',"lookahead: tk: %k tk2: %k", tk, tk2 );
// XXX -- should be modified to loop and do lookup
latok = latok->next;
if (latok->next == 0)
add_tokens();
USE_TOKEN(latok->next, "lookahead3");
tk2 = latok->next->tok;
if (tk2 == MUL) {
tk = MEMPTR;
latok = latok->next;
}
} else if ((prev_tk == MUL && tk2 != RP) || prev_tk == AND) {
tk = ID;
latok->retval.pn->hide();
latok->tok = ID;
latok->retval.s = latok->retval.pn->string;
}
} else if (tk2 == MEM) {
// ID ::
// XXX latok = latok->next->next;
// XXX goto nexttok;
goto tscope; // treat as tscope
}
if (tk == ID && (tk2 == ID || (prev_tk == ID && (tk2 == COLON || tk2 == LC)))) {
// ID ID
latok = latok->next;
goto nexttok;
}
}
//??? check_for_nested()
return tk;
}
extern int in_sizeof;
extern int in_friend;
extern int in_new;
static int type_is_redefined(Pname n) {
Pktab tb = Ctbl;
while (tb->k_id == ARG)
tb = tb->k_next;
return n->n_ktable == tb;
}
static Pname do_nl_type(Pname n, int lex_level, TOK tecsu)
/*
* replaces do_local_class() and do_nested_type()
* define a new type
* context is either "AGGR ID" or "ENUM ID" at local or nested scope
* NOTE: typedefs now processed in name::tdef()
*/
{
Pname nn = n;
if (ccl && in_mem_fct == 0 && strcmp(ccl->string, n->string) == 0) { // class x { typedef T x;
error("nested%k%n redefines immediately enclosing class", tecsu == TYPE ? TPDEF : tecsu, n);
error('i', "cannot recover from previous errors");
}
switch (tecsu) {
case CLASS:
case STRUCT:
case UNION:
case ENUM:
// check for redef at local scope...
if (n->base == TNAME // previous def exists
&& n->lex_level == lex_level // same block level
&& type_is_redefined(n)) {
if (n->tp == 0 || (n->tp->base != COBJ && n->tp->base != EOBJ)) {
// error("two definitions of%n",n);
// error('i',"cannot recover from earlier errors");
// typedef T ... class T{}, etc.
// error caught later
return n;
}
// catch some redefs here to avoid internal errors later
if (n->tp->base == EOBJ && tecsu != ENUM || n->tp->base == COBJ && tecsu == ENUM) {
error("%n defined asC and enum", n);
error('i', "cannot recover");
}
// class C{}; ... class C{};
// enum E{}; ... enum E{};
// etc. -- also an error,but requires name to be placed on
// local_class so error can be detected later during dcl phase
if (n->tp->base == COBJ && (n->tp->classtype()->defined & (DEFINED | DEF_SEEN)) == 0)
// class X; class X{}; ...
return n;
}
nn = new name(n->string);
nn->lex_level = lex_level;
nn = nn->tname(tecsu);
if (nn->n_ktable == 0)
error('i', "TN insert failed for%n", nn);
// if local, put on list for use in del.c
if (tecsu != ENUM && nn->n_ktable->k_id == BLOCK)
local_class = new name_list(nn, local_class);
break;
default:
error('i', &n->where, "bad tecsu%k in do_nl_type()", tecsu);
}
return nn;
}
// SYM -- removed int is_empty( Pclass cl, bit const_chk )
/* for nested class check, empty means *no* members
* for const object check, means no *data* members
*/
// SYM -- Since size isn't calculated until dcl() and since
// SYM local/nested classes aren't type checked until the
// SYM end of a function/class, is_empty should not be used by the parser.
// SYM -- removed int is_empty( Penum en )
// SYM -- Pname check_nested_type(Pname) removed
// SYM -- removed int in_local_class( Pclass cl )
// SYM ??? static Pname dtor_seen;
// SYM ??? static int in_expr;
extern TOK lalex()
/* return next token to grammar */
{
register TOK tk;
if (front == 0)
add_tokens(); // extend lookahead queue
LDB(1, fprintf(stderr, "\n*** lalex()\n"); showQ("before"));
gettok:
tk = deltok();
Pname n = idname;
// error('d',&curloc,"lalex: just got %k '%s' in_typedef: %d ccl:
// %t",tk,tk==ID||tk==TNAME?n->string:"", in_typedef,ccl);
if (tk == ID || tk == TNAME) {
TOK tk2 = la_look();
int lex_level = bl_level - in_class_decl - (tk2 == LC);
if (tk == TNAME) {
// error('d', "lalex tname %n; lasttk: %k tk2: %k", yylval.pn, lasttk, tk2);
// error('d', " must_be_id: %d must_be_expr %d decl_type
// %d",must_be_id,must_be_expr,DECL_TYPE); error('d', " bl_level: %d parsing_members
// %d",bl_level,parsing_class_members);
if (tk2 == LP && lasttk != TSCOPE && lasttk != MEM
&& (lasttk == TYPE || bl_level == 0 || parsing_class_members) && (laexpr(lasttk) == 0)
&& must_be_expr == 0 && DECL_TYPE == 0) {
if (la_decl(in_arg_list)) {
must_be_id = 0;
DECL_TYPE = tk;
tk = DECL_MARKER;
goto ret;
}
}
// SYM -- change
if (lasttk == AGGR || lasttk == ENUM) {
if (tk2 == LC || tk2 == COLON || (tk2 == SM && !in_new && !in_friend)) {
// tag def
// XXXXX currently enter all unqualified
// XXXXX friends in Gtbl
// XXXXX Should eventually enter in enclosing
// XXXXX scope, however back end currently
// XXXXX doesn't support this, due to lack
// XXXXX of context info.
// XXXXX Commented code below will enter name
// XXXXX correctly when back end is brought
// XXXXX up to date.
// Pktab otbl = Ctbl;
// Pclass occl = ccl;
if (in_friend) {
if (Ctbl->k_id != CLASS)
error("friend %s not inC", yylval.pn->string);
// else {
// --in_class_decl;
// ccl = ccl->in_class;
// }
// if ( Ctbl->k_id == CLASS || Ctbl->k_id == TEMPLATE ) {
// Ctbl = Ctbl->k_next;
// if (Ctbl->k_id == TEMPLATE) Ctbl = Ctbl->k_next;
// }
} else // remove this line when friend name entry is fixed
if (tk2 != SM || type_is_redefined(yylval.pn) == 0) {
if (lex_level && (in_class_decl == 0 || in_mem_fct))
yylval.pn = do_nl_type(yylval.pn, lex_level, lastval.t);
else if (in_class_decl && ccl)
yylval.pn = do_nl_type(yylval.pn, lex_level, lastval.t);
}
// Ctbl = otbl;
// if ( ccl != occl ) {
// ccl = occl;
// ++in_class_decl;
//}
}
}
if (tk2 == LT && template_tscope(yylval.pn, latok)) {
tk = PTNAME; // a parameterized type name
} else if (tk2 == MEM
|| (tk2 == DOT && lasttk != REF && lasttk != DOT && lasttk != REFMUL)) {
if (tk2 == DOT)
error("``.'' used for qualification; please use ``::''");
if (yylval.pn->tp->base == COBJ) {
Pclass cl = yylval.pn->tp->classtype();
Pclass cl2 = ccl;
while (cl2 && cl2->in_class)
cl2 = cl2->in_class;
if (cl && cl->class_base
&& (cl2 == 0 || cl2->templ_base == CL_TEMPLATE || same_class(cl, cl2) == 0))
error("YC%n must be qualifiedWZL of instantiations", yylval.pn);
}
tk = TSCOPE;
{ // XXX -- should be modified to do lookup and del at each ::
int n = 0;
while ((tk2 = lookahead()) == TSCOPE)
n += 2;
if (tk2 == TNAME) {
Pname cn = latok->retval.pn;
toknode *t = latok;
tk2 = lookahead();
if (tk2 == LP && (bl_level == 0 || parsing_class_members)
&& (laexpr(lasttk) == 0) && must_be_expr == 0 && DECL_TYPE == 0) {
if (la_decl(in_arg_list)) {
must_be_id = 0;
// t->tok = DECL_MARKER;
DECL_TYPE = TNAME;
// tk2 = deltok(1); // ::
n++; // ::
n++; // TNAME
tk = DECL_MARKER;
while (n-- > 0)
deltok();
goto ret;
}
la_reset(t->next, cn);
}
}
}
tk2 = deltok(1);
tk2 = la_look();
if (tk2 == MUL) {
tk = MEMPTR;
tk2 = deltok(1);
}
} // if tk2==MEM
// Have a TNAME. Check to be sure.
else if (must_be_id) {
DB(if (Ldebug >= 2) error('d', "lalex: must_be_id: <tname %n> <%k>", yylval.pn, tk2););
if (in_class_decl && lasttk == TYPE && tk2 == LP
&& strcmp(yylval.pn->string, ccl->string) == 0)
error("%nK with returnT", yylval.pn);
else if (lasttk == TYPE && lastval.t == OVERLOAD && (tk2 == SM || tk2 == LP)) {
tk = ID;
yylval.pn->hide();
yylval.pn = new name(yylval.pn->string);
yylval.pn->n_oper = TNAME;
} else if (lasttk == OPERATOR)
// SYM -- remove || in_typedef && yylval.pn->n_key == NESTED)
must_be_id = 0;
else if (lasttk != TSCOPE // watch out for X::X
|| lastval.pn != yylval.pn
|| (in_typedef && in_typedef->check(yylval.pn->tp, 0) == 0)) {
DB(if (Ldebug >= 2) error('d', "lalex: -- tname -> id; lasttk%k", lasttk));
tk = ID;
if (in_typedef && (lasttk == MUL || lasttk == REF)) {
defer_check = 1;
in_tag = yylval.pn;
}
if (lasttk == MEM && yylval.pn->lex_level) {
// SYM -- change
// SYM ??? this code looks suspicious
Pname nn = k_find_name(yylval.pn->string, Gtbl, 0);
if (nn == 0 || nn->base == NAME)
error("%k%s undeclared", lasttk, yylval.pn->string);
else
yylval.pn = nn;
} else {
if (lasttk != DOT && lasttk != REF && lasttk != TSCOPE && lasttk != GOTO) {
// handle typedefs in basetype::check
// when type is available
if (!in_typedef || in_arg_list) {
DB(if (Ldebug >= 2) error('d', "\"%s\" line %d: hiding%n", __FILE__,
__LINE__, yylval.pn));
yylval.pn->hide();
yylval.pn = new name(yylval.pn->string);
} else if (yylval.pn->base == TNAME) {
yylval.pn = new name(yylval.pn->string);
}
// else name already copied in do_nl_type()
// NOTE: copying name should preserve tpdef
yylval.pn->n_oper = TNAME;
} else {
// error('d',"tname%n -> id lasttk%k tk2%k",yylval.pn,lasttk,tk2);
yylval.pn = new name(yylval.pn->string);
}
DB(if (Ldebug >= 2) error('d', " -- %n%k", yylval.pn, yylval.pn->n_oper));
}
if (defer_check)
defer_check = 0;
}
} // must_be_id