forked from seyko2/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gram.y
3186 lines (2982 loc) · 75.1 KB
/
gram.y
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/gram.y 1.29" */
/*******************************************************************************
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.
gram.y:
This is the C++ syntax analyser.
Syntax extensions for error handling:
nested functions
any expression can be empty
any expression can be a constant_expression
A call to error() does not change the parser's state
***************************************************************************/
%{
#include "cfront.h"
#include "size.h"
#include "template.h"
#include <string.h>
#ifdef SVR42
#include <unistd.h>
#endif
// include tqueue.h after YYSTYPE is defined ...
int in_friend = 0;
int must_be_friend = 0;
int dont_instantiate = 0;
static int explicit_template_definition = 0;
Pname righttname=0;
static struct parstate {
Ptype intypedef;
int infriend;
int defercheck;
Pname intag;
int cid;
} pstate[BLMAX];
static int px;
extern int classid;
static void
SAVE_STATE()
{
//error('d',"save_state: in_typedef%t in_tag%n defer_check %d",in_typedef,in_tag,defer_check);
if ( px >= BLMAX ) error('i',"parsing state stack overflow -- current table %s",Ctbl->whereami());
pstate[px].intypedef = in_typedef;
pstate[px].infriend = in_friend;
in_typedef = 0;
in_friend = 0;
pstate[px].defercheck = defer_check;
defer_check = 0;
pstate[px].intag = in_tag;
in_tag = 0;
pstate[px].cid = classid;
classid = 0;
++px;
}
static void
RESTORE_STATE()
{
//error('d',"restore_state: in_typedef%t in_tag%n defer_check %d",in_typedef,in_tag,defer_check);
if ( --px < 0 ) error('i',"parsing state stack underflow -- current table %s",Ctbl->whereami());
in_typedef = pstate[px].intypedef;
in_friend = pstate[px].infriend;
defer_check = pstate[px].defercheck;
in_tag = pstate[px].intag;
classid = pstate[px].cid;
//error('d'," -> in_typedef%t in_tag%n defer_check %d",in_typedef,in_tag,defer_check);
}
//SYM parsing symbol table management
inline void
PUSH_ARG_SCOPE()
{
DB(if(Kdebug>=1)error('d',"push arg table; %ctbl %s",'C',Ctbl->whereami()););
Ctbl = new ktable( 0, Ctbl, 0 );
Ctbl->k_id = ARG;
}
inline void
PUSH_CLASS_SCOPE( Pname n )
{
DB(if(Kdebug>=1)error('d',"pushC table%n; %ctbl %s",n,'C',Ctbl->whereami()););
// table allocated in name::tname() to avoid problems with forward
// refs to class templates
Pclass cl = n->tp->classtype();
cl->k_tbl->k_next = Ctbl;
Ctbl = cl->k_tbl;
Ctbl->expand(CTBLSIZE);
}
inline void
PUSH_BLOCK_SCOPE()
{
DB(if(Kdebug>=1)error('d',"push block tbl; %ctbl %s in %s",'C',Ctbl->whereami(),Ctbl->k_next->whereami()););
if ( Ctbl->k_id == ARG )
Ctbl->expand(TBLSIZE);
else {
Ctbl = new ktable(TBLSIZE, Ctbl, Ctbl->k_name );
}
Ctbl->k_id = BLOCK;
if ( Ctbl->k_next == Gtbl
|| Ctbl->k_next->k_id == BLOCK
|| Ctbl->k_next->k_id == CLASS )
Ctbl->k_t->next = Ctbl->k_next->k_t;
}
inline void
PUSH_TEMPLATE_SCOPE()
{
DB(if(Kdebug>=1)error('d',"push template tbl; %ctbl %s",'C',Ctbl->whereami()););
Ctbl = new ktable( 0, Ctbl, 0 );
Ctbl->k_id = TEMPLATE;
}
inline void
POP_SCOPE( int deallocate = 0 )
{
DB(if(Kdebug>=1)error('d',"pop tbl %s",Ctbl->whereami()););
if ( Ctbl == Gtbl ) error('i', "bad parsing table");
Pktab b = Ctbl;
Ctbl = Ctbl->k_next;
if ( b->k_id == ARG || deallocate ) delete b;
}
/*SYM -- replaces set_scope()/curr_scope for switching between scopes
* in member defs
* Temporarily reset scope in member defs:
* int X::f( T t = a ) { ... }
* int X::s[10] = ... , i = 3;
* |----scope of X----|
* ??? What about exprs "p->operator T()", etc ???
*/
// Although local member defs are illegal outside a local class, scopes
// are stacked for error recovery / extensibility.
struct pcontext {
Pktab ktbl;
int saved_template;
};
static pcontext scopestack[BLMAX];
static int scopex = 0;
Pname
SET_SCOPE( Pname n )
{
Pktab ntb;
if ( n == 0 ) ntb = 0;
else if ( n == sta_name ) ntb = Gtbl;
else if ( n->n_template_arg == template_type_formal ) {
DB(if(Kdebug>=1)error('d',"set scope%n -- template formal -- currently in %s next %s",n,Ctbl->whereami(),Ctbl->k_next->whereami()););
return n;
}
else if ( n->tp ) {
Ptype t = n->tp->skiptypedefs();
if ( t->base==COBJ ) ntb = t->classtype()->k_tbl;
else ntb = 0;
} else ntb = 0;
DB(if(Kdebug>=1)error('d',"set scope%n ntb %s next %s, currently in %s",n,ntb->whereami(),ntb?ntb->k_next->whereami():"<nil>",Ctbl->whereami()););
if (ntb == 0) return 0;//error('i',"scope set to null table(n==%n)!",n);
if ( scopex >= BLMAX ) error('i',"set scope %s: parsing scope stack overflow -- current table %s",ntb->whereami(),Ctbl->whereami());
Pktab tt = Ctbl;
if ( Ctbl->k_id == TEMPLATE ) {
// parsing template member function
// template<...> PT<...>::f() {}
// be sure template params are in scope when parsing f
// -- extract template scope from current scope and put it
// in new scope
Ctbl = Ctbl->k_next;
tt->k_next = ntb->k_next;
ntb->k_next = tt;
scopestack[scopex].saved_template = 1;
}
else if ( Ctbl->k_id == ARG && Ctbl->k_next->k_id == TEMPLATE ) {
// parsing static data member template with declarator ()'s.
// template<...> T (PT<...>::d) = v;
// interpose PT's table between ARG table and its parent
Ctbl = Ctbl->k_next->k_next;
tt->k_next->k_next = ntb->k_next;
ntb->k_next = tt->k_next;
tt->k_next = ntb;
scopestack[scopex].saved_template = 1;
ntb = tt;
}
scopestack[scopex++].ktbl = Ctbl;
Ctbl = ntb;
return n;
}
void
UNSET_SCOPE()
{
// restore surrounding template scope, if appropriate
Pktab tt = 0;
if ( --scopex < 0 ) error('i',"parsing scope stack underflow -- current scope %s",Ctbl->whereami());
if ( scopestack[scopex].saved_template ) {
scopestack[scopex].saved_template = 0;
if ( Ctbl->k_next==0 || Ctbl->k_next->k_id != TEMPLATE )
error('i',"set scope failed restoring template table -- current table %s",Ctbl->whereami());
tt = Ctbl->k_next;
Ctbl->k_next = tt->k_next;
}
DB(if(Kdebug>=1)error('d',"unset scope %s -> %s tt %s",Ctbl->whereami(),scopestack[scopex].ktbl->whereami(),tt?tt->whereami():"<nil>"););
Ctbl = scopestack[scopex].ktbl;
if ( tt ) { tt->k_next = Ctbl; Ctbl = tt; }
}
inline Pktab
GET_XSCOPE()
{
if ( scopex-1 < 0 ) error('i',"parsing scope stack underflow -- current table %s",Ctbl->whereami());
return scopestack[scopex-1].ktbl;
}
inline void
SET_XSCOPE( Pktab tb )
{
if ( scopex-1 < 0 ) error('i',"parsing scope stack underflow -- current table %s",Ctbl->whereami());
scopestack[scopex-1].ktbl = tb;
}
// macros
#define copy_if_need_be(s) ((templp->in_progress || templp->parameters_in_progress) ? strdup(s) : s)
#define YYMAXDEPTH 600
#if 0
#define YYCLEAN {free(yys); free(yyv);}
#else
#define YYCLEAN
#endif
#ifdef DBG
#ifndef YYDEBUG
#define YYDEBUG 1
#endif
#endif
static int init_seen = 0;
static int cdi = 0;
static Pnlist cd = 0, cd_vec[BLMAX];
static char stmt_seen = 0, stmt_vec[BLMAX];
static Pnlist scd[BLMAX]; // keep track of cd list outside of switch
static int scdp = -1;
static Pname memptr_pn;
static TOK memptr_tok;
static Pname err_name = 0;
// support for template friend declarations within a class
static Pcons templ_friends;
// fcts put into norm2.c just to get them out of gram.y
void sig_name(Pname);
Ptype tok_to_type(TOK);
void memptrdcl(Pname, Pname, Ptype, Pname);
static bit decl_with_init(Pnlist cd)
/* do the declarations have an initializer
or a class object with a constructor */
{
for (Pname n=cd->head;n;n=n->n_list) {
if (n->n_initializer) return 1;
Pname cln=n->tp->is_cl_obj();
if (cln && Pclass(cln->tp)->c_ctor)
return 1;
}
return 0;
}
static char*
get_classname(const char* s)
{
// error('d',"get_classname(%s)",s);
char* r = new char[strlen(s)+1];
sprintf(r,"%s", s);
char *s2 = r;
const char* s1 = s;
while (*s) {
for ( ; s[0] && (s[0] != '_' || (s[1] && s[1] != '_')); s++) s1++;;
if (*s) {
if (strncmp(s,"___pt__",7)==0) {
*s2 = 0;
return r;
}
if (strncmp(s,"__pt__",6)==0) { // parameterized class
*s2 = '\0';
return r;
}
}
return r;
}
return r;
}
static Pptr doptr(TOK p, TOK t)
{
Pptr r = new ptr(p,0);
switch (t) {
case CONST:
r->b_const = 1;
// if (p == RPTR) error('w',"redundant `const' after &");
break;
case VOLATILE:
error('w',"\"volatile\" not implemented (ignored)");
break;
default:
error("syntax error: *%k",t);
}
return r;
}
static Pbcl dobase(TOK pr, Pname n, TOK v = 0)
{
// error('d',"dobase(%k %n %k)", pr,n,v);
Pbcl b = new basecl(0,0);
b->ppp = pr; // save protection indicator
if (n) {
if (n->base != TNAME) {
Pname nn = k_find_name(n->string,Ctbl,HIDDEN);
if ( nn == 0 ) {
error("BN%n not aTN",n);
return 0;
} else
n = nn;
}
Pbase bt = Pbase(n->tp);
while (bt->base == TYPE) bt = Pbase(bt->b_name->tp);
if (bt->base != COBJ) {
// template <class B> class D : public B {};
if (templp->in_progress == true && bt->base == ANY)
error('s',"formalTZ%n used asBC ofY",n);
else error("BN%n not aCN",n);
return 0;
}
if (v) {
if (v != VIRTUAL) error("syntax error:%k inBCD",v);
b->base = VIRTUAL;
}
else
b->base = NAME;
b->bclass = Pclass(bt->b_name->tp);
}
return b;
}
#define Ndata(a,b) b->normalize(Pbase(a),0,0)
#define Ncast(a,b) b->normalize(Pbase(a),0,1)
#define Nfct(a,b,c) b->normalize(Pbase(a),Pblock(c),0)
//#define Ncopy(n) (n->base==TNAME)?new name(n->string):n
inline Pname Ncopy(Pname n)
{
Pname nn;
if (n->base!=TNAME) {
nn = n;
} else {
nn = new name(n->string);
nn->n_template_arg = n->n_template_arg;
}
return nn;
}
#define Finit(p) Pfct(p)->f_init
#define Fargdcl(p,q,r) Pfct(p)->argdcl(q,r)
#define Freturns(p) Pfct(p)->returns
#define Fbody(p) Pfct(p)->body /*SYM*/
#define Vtype(v) Pvec(v)->typ
#define Ptyp(p) Pptr(p)->typ
/* avoid redefinitions */
#undef EOFTOK
#undef ASM
#undef BREAK
#undef CASE
#undef CONTINUE
#undef DEFAULT
#undef DELETE
#undef VEC_DELETE
#undef DO
#undef ELSE
#undef ENUM
#undef FOR
#undef FORTRAN
#undef FRIEND
#undef GOTO
#undef IF
#undef NEW
#undef VEC_NEW
#undef OPERATOR
#undef RETURN
#undef SIZEOF
#undef SWITCH
#undef THIS
#undef WHILE
#undef LP
#undef RP
#undef LB
#undef RB
#undef REF
#undef DOT
#undef NOT
#undef COMPL
#undef MUL
#undef AND
#undef PLUS
#undef MINUS
#undef ER
#undef OR
#undef ANDAND
#undef OROR
#undef QUEST
#undef COLON
#undef ASSIGN
#undef CM
#undef SM
#undef LC
#undef RC
#undef ID
#undef STRING
#undef ICON
#undef FCON
#undef CCON
#undef ZERO
#undef ASOP
#undef RELOP
#undef EQUOP
#undef DIVOP
#undef SHIFTOP
#undef ICOP
#undef TYPE
#undef CATCH
#undef THROW
#undef TRY
#undef TNAME
#undef EMPTY
#undef NO_ID
#undef NO_EXPR
#undef FDEF
#undef ELLIPSIS
#undef AGGR
#undef MEM
#undef MEMPTR
#undef PR
#undef MEMQ
#undef TSCOPE
#undef DECL_MARKER
#undef REFMUL
#undef LDOUBLE
#undef LINKAGE
#undef TEMPLATE
#undef XVIRT
#undef XNLIST
#undef XILINE
#undef XIA
#undef SM_PARAM
#undef PTNAME
#undef NEW_INIT_KLUDGE
/*Forward declarations*/
static void arg_redec( Pname fn );
static void hoist_al();
static Pname enumcheck( Pname n );
static void check_tag();
static bit check_if_base( Pclass c1, Pclass c2 );
static Pname dummy_dtor( TOK q, TOK d );
static Pname dummy_dtor();
%}
%union {
const char* s;
TOK t;
int i;
loc l;
Pname pn;
Ptype pt;
Pexpr pe;
Pstmt ps;
Pbase pb;
Pnlist nl;
Pslist sl;
Pelist el;
Pbcl pbc;
Pptr pp;
PP p; // fudge: pointer to all class node objects
Plist pl;
toknode* q; // token queue
}
%{
#include "tqueue.h"
extern YYSTYPE yylval, yyval;
extern int yyparse();
// in_typedef should allow for nested in_typedef
extern int declTag; // !1: inline, virtual mod permitted
int in_sizeof = 0;
int in_new = 0;
Ptype in_typedef = 0; // catch redefinition of TNAME
Pname in_tag = 0; // handle complex typedefs: int (*)()
extern int defer_check; // redefinition typedef check delay
extern int must_be_id; // !0, TNAME => ID, i.e., int X
int DECL_TYPE = 0; // lalex() wants this set for global x(*fp)()
int in_arg_list=0; // !0 when parsing argument list, 1: in (), 2: in <>
static int in_binit_list=0;
int in_class_decl=0; // !0 when processing class definition
int parsing_class_members=0; // !0 when parsing class def but not member function body
int in_mem_fct=0; // !0 when parsing member function definition
Ptempl_inst pti = 0; // explicit template class: class X<int> {};
#define yylex lalex
#define NEXTTOK() ( (yychar==-1) ? (yychar=yylex(),yychar) : yychar )
#define EXPECT_ID() must_be_id = 1
#define NOT_EXPECT_ID() must_be_id = 0
Pname syn()
{
ll:
switch (yyparse()) {
case 0: return 0; // EOF
case 1: goto ll; // no action needed
default: return yyval.pn;
}
}
%}
/*
the token definitions are copied from token.h,
and all %token replaced by %token
*/
/* keywords in alphabetical order */
%token EOFTOK 0
%token ASM 1
%token AUTO 2
%token BREAK 3
%token CASE 4
%token CONTINUE 7
%token DEFAULT 8
%token DELETE 9
%token DO 10
%token ELSE 12
%token ENUM 13
%token FOR 16
%token FORTRAN 17
%token FRIEND 18
%token GOTO 19
%token IF 20
%token NEW 23
%token OPERATOR 24
%token RETURN 28
%token SIZEOF 30
%token SWITCH 33
%token THIS 34
%token WHILE 39
/* operators in priority order (sort of) */
%token LP 40
%token RP 41
%token LB 42
%token RB 43
%token REF 44
%token DOT 45
%token NOT 46
%token COMPL 47
%token MUL 50
%token AND 52
%token PLUS 54
%token MINUS 55
%token LT 58
%token GT 60
%token ER 64
%token OR 65
%token ANDAND 66
%token OROR 67
%token QUEST 68
%token COLON 69
%token ASSIGN 70
%token CM 71
%token SM 72
%token LC 73
%token RC 74
/* = constants etc. */
%token ID 80
%token STRING 81
%token ICON 82
%token FCON 83
%token CCON 84
%token NAME 85
%token ZERO 86
/* groups of tokens */
%token ASOP 90 /* op= */
%token RELOP 91 /* LE GE LT GT */
%token EQUOP 92 /* EQ NE */
%token DIVOP 93 /* DIV MOD */
%token SHIFTOP 94 /* LS RS */
%token ICOP 95 /* INCR DECR */
%token TYPE 97
/* TYPE = INT FLOAT CHAR DOUBLE REGISTER STATIC EXTERN AUTO
LONG SHORT UNSIGNED INLINE FRIEND VIRTUAL */
%token CATCH 98
%token THROW 99
%token TRY 100
%token TNAME 123
%token EMPTY 124
%token NO_ID 125
%token NO_EXPR 126
%token FDEF 127
%token ELLIPSIS 155
%token AGGR 156
%token MEM 160
%token MEMPTR 173
%token PR 175 /* PUBLIC PRIVATE PROTECTED */
%token MEMQ 176 /* TNAME :: */
%token TSCOPE 178 /* TNAME :: */
%token DECL_MARKER 179
%token REFMUL 180 /* ->*, .* */
%token LDOUBLE 181
%token LINKAGE 182 /* extern "asdf" */
%token TEMPLATE 185 /* local class */
/* "tokens" for aux data structures */
%token XVIRT 200 /* class virt */
%token XNLIST 201 /* struct name_list */
%token XILINE 202
%token XIA 203
%token SM_PARAM 207
%token PTNAME 209
%token NEW_INIT_KLUDGE 210
%token XDELETED_NODE 211
%token VEC_DELETE 212
%token VEC_NEW 213
%token DUMMY_LAST_NODE 214
%type <p> external_def fct_dcl fct_def att_fct_def arg_dcl_list
base_init init_list binit
data_dcl ext_def vec ptr
type tp enum_dcl moe_list moe
tag ttag enumtag class_head class_dcl cl_mem_list
cl_mem dl decl_list
fname decl initializer stmt_list
caselab_stmt caselablist
block statement simple ex_list elist e ee term prim
term_elist
cast_decl cast_type c_decl c_type c_tp
arg_decl formal_decl at arg_type arg_list arg_type_list
new_decl new_type
new_type2 new_decl2 cm
condition
TSCOPE tscope MEMQ TNAME tn_list MEMPTR dtorspec
scope_qualifiers /*SYM*/
qualified_tname
PTNAME tname ptname template_def
%type <sl> handler_list
%type <ps> handler
%type <l> LC RC SWITCH CASE DEFAULT FOR IF DO WHILE GOTO RETURN DELETE
BREAK CONTINUE
%type <t> oper ellipsis_opt
EQUOP DIVOP SHIFTOP ICOP RELOP GT LT ASOP
ANDAND OROR PLUS MINUS MUL ASSIGN OR ER AND
LP LB NOT COMPL AGGR
TYPE PR REFMUL
%type <s> CCON ZERO ICON FCON STRING LINKAGE
%type <pn> ID FDEF inline_fct_def identifier exception_type
%type <pbc> base_list base_unit_list base_unit
%type <q> EMPTY
%type <i> fct_attributes
%type <pl> arg_lp type_list
%type <pe> temp_inst_parm
%type <el> temp_inst_parms
%left EMPTY
%left NO_ID
%left RC LC ID BREAK CONTINUE RETURN GOTO DELETE DO IF WHILE FOR CASE DEFAULT
AGGR ENUM TYPE TNAME TSCOPE
%left NO_EXPR
%left CM
%right ASOP ASSIGN
%right QUEST COLON
%left OROR
%left ANDAND
%left OR
%left ER
%left AND
%left EQUOP
%left RELOP GT LT
%left SHIFTOP
%left PLUS MINUS
%left MUL DIVOP MEMPTR
%left REFMUL
%right NOT COMPL NEW
%right ICOP SIZEOF
%left LB LP DOT REF MEM
%start ext_def
%%
/*
this parser handles declarations one by one,
NOT a complete .c file
*/
/************** DECLARATIONS in the outermost scope: returns Pname (in yylval) ***/
ext_def : external_def { YYCLEAN;return 2; }
| SM { YYCLEAN;return 1; }
| EOFTOK { YYCLEAN;return 0; }
| LINKAGE LC
{
set_linkage($<s>1);
bl_level--;
YYCLEAN;return 1;
}
| RC
{
set_linkage(0);
bl_level++;
YYCLEAN;return 1;
}
| template { YYCLEAN;return 1; }
;
template : TEMPLATE
{
PUSH_TEMPLATE_SCOPE();//SYM
// error('d',"template seen: in_class_decl %d", in_class_decl);
if (in_class_decl) {
must_be_friend = 1;
if (templp->in_progress == true) // inside template
templp->save_templ = new templ_state;
}
else must_be_friend = 0;
templp->start() ;
templp->formals_in_progress = true;
}
LT { in_arg_list = 2; } template_parm_list GT
{
templp->enter_parameters();
templp->formals_in_progress = false;
in_arg_list = 0;
}
template_def
{
templp->end($<pn>8);
POP_SCOPE(); //SYM
if (in_class_decl && templp->save_templ) {
delete templp->save_templ;
templp->save_templ = 0;
}
else {
templp->in_progress = false;
bound_expr_tbl->reinit();
}
//SYM -- goto mod removed
}
;
template_def : data_dcl
| att_fct_def { goto mod; }
| fct_def { goto mod; }
| fct_dcl
| class_dcl SM
{
Pname pn = $<pb>1->aggr();
/* basetype:aggr() does not return the name for a forward
* declaration, so extract it directly */
$$ = (pn ? pn : $<pb>1->b_name);
DECL_TYPE = 0;
}
;
identifier : ID
| qualified_tname
{ $<pn>$ = Ncopy($<pn>1) ;}
;
external_def : data_dcl
{
//SYM -- tn stuff removed
if ($<pn>1==0) $<i>$ = 1;
}
| att_fct_def { goto mod; }
| fct_def
{ mod: //SYM -- tn stuff removed
Pname n = $<pn>1;
if ( n && n->n_qualifier ) {//SYM
if ( n->n_qualifier->n_template_arg != template_type_formal )
UNSET_SCOPE();
if ( n->n_qualifier == sta_name )
n->n_qualifier = 0;
}
}
| fct_dcl
| ASM LP STRING RP SM
{ Pname n = new name(make_name('A'));
n->tp = new basetype(ASM,0);
Pbase(n->tp)->b_name = Pname($<s>3);
$$ = n;
}
;
fct_dcl : decl ASSIGN initializer SM
{
err_name = $<pn>1;
if(err_name) err_name->n_initializer = $<pe>3;
goto fix;
}
| decl SM
{
Ptype t;
err_name = $<pn>1;
fix:
if (err_name == 0) {
error("syntax error:TX");
$$ = Ndata(defa_type,err_name);
}
else if ((t=err_name->tp) == 0) {
error("TX for%n",err_name);
$$ = Ndata(defa_type,err_name);
}
else if (t->base==FCT) {
if (Pfct(t)->returns==0)
$$ = Nfct(defa_type,err_name,0);
else
$$ = Ndata(0,err_name);
}
else {
error("syntax error:TX for%k%n",t->base,err_name);
$$ = Ndata(defa_type,err_name);
}
if ( err_name && err_name->n_qualifier ) {
if ( err_name->n_qualifier->n_template_arg != template_type_formal )
UNSET_SCOPE();
if ( err_name->n_qualifier == sta_name )
err_name->n_qualifier = 0;
}
}
;
att_fct_def : type decl arg_dcl_list check_inline
{ if ( yychar == LC ) --bl_level;
Pname n = Nfct($1,$<pn>2,dummy);
Fargdcl(n->tp,name_unlist($<nl>3),n);
arg_redec( $<pn>2 );
$<pn>$ = n;
if ( yychar == LC ) ++bl_level;
Ctbl->k_name = n;
}/*SYM*/
base_init block
{ Pname n = $<pn>5;//SYM
if ( !in_typedef ) {
Fbody(n->tp) = Pblock($7);//SYM
Finit(n->tp) = $<pn>6;
}
$$ = n;
NOT_EXPECT_ID();
}
| type decl arg_dcl_list check_inline EMPTY
{
Pname n = Nfct($1,$<pn>2,dummy);
Fargdcl(n->tp,name_unlist($<nl>3),n);
$<q>5->retval.pn = n;
$$ = n;
NOT_EXPECT_ID();
}
| type decl arg_dcl_list check_inline NO_ID /*syntax error*/
{
if (!templp->in_progress)
error(&$<pn>2->where,"syntax error -- did you forget a ';'?");
Pname n = Nfct($1,$<pn>2,0);
$$ = n;
NOT_EXPECT_ID();
}
;
fct_def : decl arg_dcl_list check_inline
{ if ( yychar == LC ) --bl_level;
Pname n = Nfct(defa_type,$<pn>1,dummy);
Fargdcl(n->tp,name_unlist($<nl>2),n);
arg_redec( $<pn>1 );
$<pn>$ = n;
if ( yychar == LC ) ++bl_level;
Ctbl->k_name = n;
}/*SYM*/
base_init block
{ Pname n = $<pn>4;//SYM
Fbody(n->tp) = Pblock($6);//SYM
if ( $<pn>5 && $<pn>5->n_list &&
ccl && ccl->csu == UNION )
error( "multiple initializers in unionK %s::%n", $<pn>1->string, $<pn>1 );
Finit(n->tp) = $<pn>5;
$$ = n;
NOT_EXPECT_ID();
}
| decl arg_dcl_list check_inline EMPTY
{
Pname n = Nfct(defa_type,$<pn>1,dummy);
Fargdcl(n->tp,name_unlist($<nl>2),n);
$<q>4->retval.pn = n;
$$ = n;
NOT_EXPECT_ID();
}
| decl arg_dcl_list check_inline NO_ID /*syntax error*/
{
if (explicit_template_definition == 0 )
error(&$<pn>1->where,"badD of%n -- did you forget a ';'?",$<pn>1);
else {
Pname n = pti->get_tname();
if ($<pn>1->n_oper == DTOR)
error('s',"explicitYZL for destructor of specializedYC%n -- please drop the parameter list",n);
else
error('i',"specialializedYC%n: declaration problem: %s",n,$<pn>1->string);
error('i', "cannot recover from previous error" );
}
Pname n = Nfct(defa_type,$<pn>1,0);
$$ = n;
NOT_EXPECT_ID();
}
;
inline_fct_def : FDEF
{ PUSH_ARG_SCOPE();//SYM
arg_redec($<pn>1);
Ctbl->k_name = $<pn>1;
}
base_init block
{
Finit($1->tp) = $<pn>3;
Pfct($1->tp)->body = Pblock($4);
$$ = $1;
NOT_EXPECT_ID();
}
;
check_inline : /* empty */
{
// if parsing implicit inline def, save body
// of function for parsing after class def
if ( Ctbl->k_id != ARG )
error('i',"expectingA table in check_inline!");
switch ( NEXTTOK() ) {
case LC: case COLON:
if ( in_class_decl ) {
// mem or friend inline def
// save text of mem_init & ftn
la_backup(yychar,yylval);
// yylval used as dummy...
la_backup(FDEF, yylval);
if ( (yylval.q = save_text()) ) {
yychar = EMPTY;
POP_SCOPE();//SYM
} else { // syntax error
// just parse in place
yylex(); // FDEF
yylex();
yychar = NO_ID;
hoist_al();//SYM
}
} // if in_class_decl
//SYM -- else non-nested ftn def
//SYM -- arg table will become block table
break;
default:
la_backup(yychar,yylval);
yychar = NO_ID; // 'graceful' recovery
hoist_al();//SYM
break;
}
}
;
base_init : COLON { ++in_binit_list; } init_list