-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathparse.y
1716 lines (1478 loc) · 42.1 KB
/
parse.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
/* YACC parser for C syntax.
Copyright (C) 1987 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing. Refer to the GNU CC General Public
License for full details.
Everyone is granted permission to copy, modify and redistribute
GNU CC, but only under the conditions described in the
GNU CC General Public License. A copy of this license is
supposed to have been given to you along with GNU CC so you
can know your rights and responsibilities. It should be in a
file named COPYING. Among other things, the copyright notice
and this notice must be preserved on all copies. */
/* To whomever it may concern: I have heard that such a thing was once
written by AT&T, but I have never seen it. */
%{
#include "config.h"
#include "tree.h"
#include "parse.h"
#include "c-tree.h"
%}
%start program
%union {long itype; tree ttype; enum tree_code code}
/* all identifiers that are not reserved words
and are not declared typedefs in the current block */
%token IDENTIFIER
/* all identifiers that are declared typedefs in the current block.
In some contexts, they are treated just like IDENTIFIER,
but they can also serve as typespecs in declarations. */
%token TYPENAME
/* reserved words that specify storage class.
yylval contains an IDENTIFER_NODE which indicates which one. */
%token SCSPEC
/* reserved words that specify type.
yylval contains an IDENTIFER_NODE which indicates which one. */
%token TYPESPEC
/* reserved words that modify type: "const" or "volatile".
yylval contains an IDENTIFER_NODE which indicates which one. */
%token TYPEMOD
/* character or numeric constants.
yylval is the node for the constant. */
%token CONSTANT
/* String constants in raw form.
yylval is a STRING_CST node. */
%token STRING
/* "...", used for functions with variable arglists. */
%token ELLIPSIS
/* the reserved words */
%token SIZEOF ENUM STRUCT UNION IF ELSE WHILE DO FOR SWITCH CASE DEFAULT
%token BREAK CONTINUE RETURN GOTO ASM
/* Define the operator tokens and their precedences.
The value is an integer because, if used, it is the tree code
to use in the expression made from the operator. */
%right <code> ASSIGN '='
%right <code> '?' ':'
%left <code> OROR
%left <code> ANDAND
%left <code> '|'
%left <code> '^'
%left <code> '&'
%left <code> EQCOMPARE
%left <code> ARITHCOMPARE
%left <code> LSHIFT RSHIFT
%left <code> '+' '-'
%left <code> '*' '/' '%'
%right <code> UNARY PLUSPLUS MINUSMINUS
%left HYPERUNARY
%left <code> POINTSAT '.'
%type <code> unop
%type <ttype> identifier IDENTIFIER TYPENAME CONSTANT expr nonnull_exprlist exprlist
%type <ttype> expr_no_commas primary string STRING
%type <ttype> typed_declspecs scspecs typespecs typespec SCSPEC TYPESPEC TYPEMOD
%type <ttype> initdecls notype_initdecls initdcl notype_initdcl
%type <ttype> init initlist
%type <ttype> declarator
%type <ttype> notype_declarator after_type_declarator
%type <ttype> structsp component_decl_list component_decl components component_declarator
%type <ttype> enumlist enumerator
%type <ttype> typename absdcl absdcl1 typemods
%type <ttype> stmts
%type <ttype> pushlevel compstmt stmt xexpr parmlist parms parm identifiers
%{
/* the declaration found for the last IDENTIFIER token read in.
yylex must look this up to detect typedefs, which get token type TYPENAME,
so it is left around in case the identifier is not a typedef but is
used in a context which makes it a reference to a variable. */
static tree lastiddecl;
tree current_function_decl, current_switch_stmt, current_block;
tree current_continue_label, current_break_label;
tree continue_label_stack, break_label_stack;
static void pushbreak(), popbreak();
static tree finish_compound_stmt();
static tree make_pointer_declarator ();
static tree combine_strings ();
/* list of types and structure classes of the current declaration */
tree current_declspecs;
char *input_filename; /* file being read */
%}
%%
program:
extdefs
;
/* the reason for the strange actions in this rule
is so that notype_initdecls when reached via datadef
can find a valid list of type and sc specs in $0. */
extdefs:
{$<ttype>$ = NULL_TREE} extdef
| extdefs {$<ttype>$ = NULL_TREE} extdef
;
extdef:
fndef
| datadef
| ASM '(' STRING ')' ';'
{ assemble_asm ($3); }
;
datadef:
setspecs notype_initdecls ';'
| scspecs setspecs notype_initdecls ';'
{}
| typed_declspecs setspecs initdecls ';'
{}
| scspecs ';'
{ yyerror ("empty declaration"); }
| typed_declspecs ';'
{ shadow_tag ($1); }
| error ';'
| error '}'
| ';'
;
fndef:
typed_declspecs setspecs declarator
{ if (! start_function ($1, $3))
YYFAIL; }
xdecls
{ store_parm_decls (); }
compstmt
{ finish_function (input_filename, @7.first_line, $7); }
| typed_declspecs setspecs declarator error
{ }
| scspecs setspecs notype_declarator
{ if (! start_function ($1, $3))
YYFAIL; }
xdecls
{ store_parm_decls (); }
compstmt
{ finish_function (input_filename, @7.first_line, $7); }
| scspecs setspecs notype_declarator error
{ }
| setspecs notype_declarator
{ if (! start_function (0, $2))
YYFAIL; }
xdecls
{ store_parm_decls (); }
compstmt
{ finish_function (input_filename, @6.first_line, $6); }
| setspecs notype_declarator error
;
identifier:
IDENTIFIER
| TYPENAME
;
unop: '&'
{ $$ = ADDR_EXPR; }
| '-'
{ $$ = NEGATE_EXPR; }
| '+'
{ $$ = CONVERT_EXPR; }
| PLUSPLUS
{ $$ = PREINCREMENT_EXPR; }
| MINUSMINUS
{ $$ = PREDECREMENT_EXPR; }
| '~'
{ $$ = BIT_NOT_EXPR; }
| '!'
{ $$ = TRUTH_NOT_EXPR; }
;
expr: nonnull_exprlist
{ $$ = build_compound_expr($1); }
;
exprlist:
/* empty */
{ $$ = NULL_TREE; }
| nonnull_exprlist
;
nonnull_exprlist:
expr_no_commas
{ $$ = build_tree_list (NULL_TREE, $1); }
| nonnull_exprlist ',' expr_no_commas
{ chainon ($1, build_tree_list (NULL_TREE, $3)); }
;
expr_no_commas:
primary
| '*' expr_no_commas %prec UNARY
{ $$ = build_indirect_ref ($2); }
| unop expr_no_commas %prec UNARY
{ $$ = build_unary_op ($1, $2, 0); }
| '(' typename ')' expr_no_commas %prec UNARY
{ $$ = build_c_cast (groktypename($2), $4); }
| SIZEOF expr_no_commas %prec UNARY
{ $$ = c_sizeof (TREE_TYPE ($2)); }
| SIZEOF '(' typename ')' %prec HYPERUNARY
{ $$ = c_sizeof (groktypename($3)); }
| expr_no_commas '+' expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas '-' expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas '*' expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas '/' expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas '%' expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas LSHIFT expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas RSHIFT expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas ARITHCOMPARE expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas EQCOMPARE expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas '&' expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas '|' expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas '^' expr_no_commas
{ $$ = build_binary_op ($2, $1, $3); }
| expr_no_commas ANDAND expr_no_commas
{ $$ = build_binary_op (TRUTH_ANDIF_EXPR, $1, $3); }
| expr_no_commas OROR expr_no_commas
{ $$ = build_binary_op (TRUTH_ORIF_EXPR, $1, $3); }
| expr_no_commas '?' expr ':' expr_no_commas
{ $$ = build_conditional_expr($1, $3, $5); }
| expr_no_commas '=' expr_no_commas
{ $$ = build_modify_expr($1, $3); }
| expr_no_commas ASSIGN expr_no_commas
{ register tree tem
= duplicate_reference ($1);
$$ = build_modify_expr(tem, build_binary_op ($2, tem, $3)); }
;
primary:
IDENTIFIER
{ $$ = lastiddecl;
if (!$$)
{
if (yychar == YYEMPTY)
yychar = YYLEX;
if (yychar == '(')
$$ = implicitly_declare($1);
else
{
yyerror("variable %s used but not declared",
IDENTIFIER_POINTER ($1));
$$ = error_mark_node;
}
}
if (TREE_CODE ($$) == CONST_DECL)
$$ = DECL_INITIAL ($$);
}
| CONSTANT
| string
{ $$ = combine_strings ($1); }
| '(' expr ')'
{ $$ = $2; }
| '(' error ')'
{ $$ = error_mark_node; }
| primary '(' exprlist ')' %prec '.'
{ $$ = build_function_call ($1, $3); }
| primary '[' expr ']' %prec '.'
{ $$ = build_array_ref ($1, $3); }
| primary '.' identifier
{ $$ = build_component_ref($1, $3); }
| primary POINTSAT identifier
{ $$ = build_component_ref(build_indirect_ref ($1), $3); }
| primary PLUSPLUS
{ $$ = build_unary_op (POSTINCREMENT_EXPR, $1, 0); }
| primary MINUSMINUS
{ $$ = build_unary_op (POSTDECREMENT_EXPR, $1, 0); }
;
/* Produces a STRING_CST with perhaps more STRING_CSTs chained onto it. */
string:
STRING
| string STRING
{ $$ = chainon ($1, $2); }
;
xdecls:
/* empty */
| decls
;
decls:
decl
| errstmt
| decls decl
| decl errstmt
;
/* records the type and storage class specs to use for processing
the declarators that follow */
setspecs: /* empty */
{ current_declspecs = $<ttype>0; }
;
decl:
typed_declspecs setspecs initdecls ';'
{}
| scspecs setspecs notype_initdecls ';'
{}
| typed_declspecs ';'
{ shadow_tag ($1); }
| scspecs ';'
{ warning ("empty declaration"); }
;
/* declspecs which contain at least one type specifier.
A typedef'd name following these is taken as a name to be declared. */
typed_declspecs:
typespec
{ $$ = build_tree_list (NULL_TREE, $1); }
| scspecs typespec
{ $$ = tree_cons (NULL_TREE, $2, $1); }
| typed_declspecs TYPESPEC
{ $$ = tree_cons (NULL_TREE, $2, $1); }
| typed_declspecs TYPEMOD
{ $$ = tree_cons (NULL_TREE, $2, $1); }
| typed_declspecs structsp
{ $$ = tree_cons (NULL_TREE, $2, $1); }
| typed_declspecs SCSPEC
{ $$ = tree_cons (NULL_TREE, $2, $1); }
;
/* declspecs which contain no type specifiers.
The identifier to which they apply must not be a typedef'd name. */
scspecs: SCSPEC
{ $$ = build_tree_list (NULL_TREE, $1); }
| scspecs SCSPEC
{ $$ = tree_cons (NULL_TREE, $2, $1); }
;
/* used instead of declspecs where storage classes are not allowed
(typenames, structure components, and parameters) */
typespecs:
typespec
{ $$ = build_tree_list (NULL_TREE, $1); }
| typespecs TYPESPEC
{ $$ = tree_cons (NULL_TREE, $2, $1); }
| typespecs TYPEMOD
{ $$ = tree_cons (NULL_TREE, $2, $1); }
| typespecs structsp
{ $$ = tree_cons (NULL_TREE, $2, $1); }
;
typespec: TYPESPEC
| TYPEMOD
| structsp
| TYPENAME
;
initdecls:
initdcl
| initdecls ',' initdcl
;
notype_initdecls:
notype_initdcl
| notype_initdecls ',' initdcl
;
initdcl:
declarator '='
{ $<ttype>$ = start_decl ($1, current_declspecs, 1); }
init
/* Note how the declaration of the variable is in effect while its init is parsed! */
{ finish_decl (input_filename, @1.first_line, $<ttype>3, $4); }
| declarator
{ tree d = start_decl ($1, current_declspecs, 0);
finish_decl (input_filename, @1.first_line, d, NULL_TREE); }
;
notype_initdcl:
notype_declarator '='
{ $<ttype>$ = start_decl ($1, current_declspecs, 1); }
init
/* Note how the declaration of the variable is in effect while its init is parsed! */
{ finish_decl (input_filename, @1.first_line, $<ttype>3, $4); }
| notype_declarator
{ tree d = start_decl ($1, current_declspecs, 0);
finish_decl (input_filename, @1.first_line, d, NULL_TREE); }
;
init:
expr_no_commas
| '{' initlist '}'
{ $$ = build1 (CONSTRUCTOR, nreverse ($2)); }
| '{' initlist ',' '}'
{ $$ = build1 (CONSTRUCTOR, nreverse ($2)); }
;
/* This chain is built in reverse order,
and put in forward order where initlist is used. */
initlist:
init
{ $$ = build_tree_list (NULL_TREE, $1); }
| initlist ',' init
{ $$ = tree_cons (NULL, $3, $1); }
;
/* Any kind of declarator (thus, all declarators allowed
after an explicit typespec). */
declarator:
after_type_declarator
| notype_declarator
;
/* A declarator that is allowed only after an explicit typespec. */
after_type_declarator:
after_type_declarator '(' parmlist ')' %prec '.'
{ $$ = build2 (CALL_EXPR, $1, $3); }
| after_type_declarator '(' identifiers ')' %prec '.'
{ $$ = build2 (CALL_EXPR, $1, $3); }
| after_type_declarator '(' error ')' %prec '.'
{ $$ = build2 (CALL_EXPR, $1, NULL_TREE); }
| after_type_declarator '[' expr ']' %prec '.'
{ $$ = build2 (ARRAY_REF, $1, $3); }
| after_type_declarator '[' ']' %prec '.'
{ $$ = build2 (ARRAY_REF, $1, NULL_TREE); }
| TYPENAME
;
/* A declarator allowed whether or not there has been
an explicit typespec. These cannot redeclare a typedef-name. */
notype_declarator:
notype_declarator '(' parmlist ')' %prec '.'
{ $$ = build2 (CALL_EXPR, $1, $3); }
| notype_declarator '(' identifiers ')' %prec '.'
{ $$ = build2 (CALL_EXPR, $1, $3); }
| notype_declarator '(' error ')' %prec '.'
{ $$ = build2 (CALL_EXPR, $1, NULL_TREE); }
| '(' notype_declarator ')'
{ $$ = $2; }
| '*' typemods notype_declarator %prec UNARY
{ $$ = make_pointer_declarator ($2, $3); }
| notype_declarator '[' expr ']' %prec '.'
{ $$ = build2 (ARRAY_REF, $1, $3); }
| notype_declarator '[' ']' %prec '.'
{ $$ = build2 (ARRAY_REF, $1, NULL_TREE); }
| IDENTIFIER
;
structsp:
STRUCT identifier '{' component_decl_list '}'
{ $$ = build_struct (RECORD_TYPE, input_filename, @1.first_line, $2, $4, 0); }
| STRUCT '{' component_decl_list '}'
{ $$ = build_struct (RECORD_TYPE, input_filename, @1.first_line, NULL_TREE, $3, 0); }
| STRUCT identifier
{ $$ = build_struct (RECORD_TYPE, input_filename, @1.first_line, $2, NULL_TREE, 1); }
| UNION identifier '{' component_decl_list '}'
{ $$ = build_struct (UNION_TYPE, input_filename, @1.first_line, $2, $4, 0); }
| UNION '{' component_decl_list '}'
{ $$ = build_struct (UNION_TYPE, input_filename, @1.first_line, NULL_TREE, $3, 0); }
| UNION identifier
{ $$ = build_struct (UNION_TYPE, input_filename, @1.first_line, $2, NULL_TREE, 1); }
| ENUM identifier '{'
{ $$ = start_enum ($2); }
enumlist '}'
{ $$ = finish_enum ($<ttype>4, nreverse ($5)); }
| ENUM '{'
{ $$ = start_enum (NULL_TREE); }
enumlist '}'
{ $$ = finish_enum ($<ttype>3, nreverse ($4)); }
| ENUM identifier
{ $$ = xref_enum ($2); }
;
component_decl_list: /* empty */
{ $$ = NULL_TREE; }
| component_decl
| component_decl_list ';' component_decl
{ $$ = chainon ($1, $3); }
| component_decl_list ';'
;
component_decl:
typespecs setspecs components
{ $$ = $3; }
| error
{ $$ == NULL_TREE; }
;
components:
/* empty */
{ $$ = NULL_TREE; }
| component_declarator
| components ',' component_declarator
{ $$ = chainon ($1, $3); }
;
component_declarator:
declarator
{ $$ = grokfield (input_filename, @1.first_line, $1, current_declspecs, NULL_TREE); }
| declarator ':' expr_no_commas
{ $$ = grokfield (input_filename, @1.first_line, $1, current_declspecs, $3); }
| ':' expr_no_commas
{ $$ = grokfield (input_filename, @1.first_line, NULL_TREE, current_declspecs, $2); }
;
/* We chain the enumerators in reverse order.
They are put in forward order where enumlist is used.
(The order used to be significant, but no longer is so.
However, we still maintain the order, just to be clean.) */
enumlist:
enumerator
| enumlist ',' enumerator
{ $$ = chainon ($3, $1); }
| enumlist ','
;
enumerator:
identifier
{ $$ = build_enumerator ($1, NULL_TREE); }
| identifier '=' expr_no_commas
{ $$ = build_enumerator ($1, $3); }
;
typename:
typespecs absdcl
{ $$ = build_tree_list ($1, $2); }
;
absdcl: /* an absolute declarator */
/* empty */
{ $$ = NULL_TREE; }
| absdcl1
;
typemods:
/* empty */
{ $$ = NULL_TREE; }
| typemods TYPEMOD
{ $$ = tree_cons (NULL_TREE, $2, $1); }
;
absdcl1: /* a nonempty absolute declarator */
'(' absdcl1 ')'
{ $$ = $2; }
| '*' typemods absdcl1 %prec UNARY
{ $$ = make_pointer_declarator ($2, $3); }
| '*' typemods %prec UNARY
{ $$ = make_pointer_declarator ($2, NULL_TREE); }
| absdcl1 '(' parmlist ')' %prec '.'
{ $$ = build2 (CALL_EXPR, $1, $3); }
| absdcl1 '[' expr ']' %prec '.'
{ $$ = build2 (ARRAY_REF, $1, $3); }
| absdcl1 '[' ']' %prec '.'
{ $$ = build2 (ARRAY_REF, $1, NULL_TREE); }
| '(' parmlist ')' %prec '.'
{ $$ = build2 (CALL_EXPR, NULL_TREE, $2); }
| '[' expr ']' %prec '.'
{ $$ = build2 (ARRAY_REF, NULL_TREE, $2); }
| '[' ']' %prec '.'
{ $$ = build2 (ARRAY_REF, NULL_TREE, NULL_TREE); }
;
/* at least one statement, the first of which parses without error. */
/* stmts is used only after decls, so an invalid first statement
is actually regarded as an invalid decl and part of the decls. */
/* To speed things up, we actually chain the statements in
reverse order and return them that way.
They are put into forward order where stmts is used. */
stmts:
stmt
| stmts stmt
{ $$ = chainon ($2, $1); }
| stmts errstmt
;
errstmt: error ';'
;
/* build the LET_STMT node before parsing its contents,
so that any LET_STMTs within the context can have their display pointers
set up to point at this one. */
pushlevel: /* empty */
{ pushlevel();
$$ = current_block;
current_block
= build_let (input_filename, 0, 0, 0, $$, 0);
}
;
compstmt: '{' '}'
{ $$ = build_compound (input_filename, @1.first_line, 0); }
| '{' pushlevel decls stmts '}'
{ $$ = finish_compound_stmt (current_block, nreverse ($4),
$2, @1.first_line); }
| '{' pushlevel decls '}'
{ $$ = finish_compound_stmt (current_block, NULL_TREE,
$2, @1.first_line); }
| '{' pushlevel error '}'
{ $$ = error_mark_node;
current_block = $2;
poplevel(); }
| '{' pushlevel stmts '}'
{ $$ = finish_compound_stmt (current_block, nreverse ($3), $2,
@1.first_line); }
;
stmt: compstmt
| expr ';'
{ $$ = build_expr_stmt (input_filename, @1.first_line, $1); }
| IF '(' expr ')' stmt
{ $$ = build_if (input_filename, @1.first_line, default_conversion ($3), $5, 0); }
| IF '(' expr ')' stmt ELSE stmt
{ $$ = build_if (input_filename, @1.first_line, default_conversion ($3), $5, $7); }
| WHILE
{ pushbreak(1); }
'(' expr ')' stmt
{ $$ = build_loop (input_filename, @1.first_line,
chainon (build_exit (input_filename, @4.first_line,
default_conversion ($4)),
chainon ($6, current_continue_label)));
$$ = build_compound (input_filename, @1.first_line, chainon ($$, current_break_label));
popbreak(1); }
| DO
{ pushbreak(1); }
stmt WHILE '(' expr ')' ';'
{ $$ = build_loop (input_filename, @1.first_line,
chainon ($3, chainon(current_continue_label,
build_exit (input_filename, @6.first_line,
default_conversion ($6)))));
$$ = build_compound (input_filename, @1.first_line, chainon ($$, current_break_label));
popbreak(1); }
| FOR
{ pushbreak(1); }
'(' xexpr ';' xexpr ';' xexpr ')' stmt
{ $$ = build_compound (input_filename, @1.first_line,
chainon ($4 ? build_expr_stmt (input_filename, @4.first_line, $4) : NULL_TREE,
build_loop (input_filename, @1.first_line,
chainon ($6 ? build_exit (input_filename, @6.first_line,
default_conversion ($6))
: NULL_TREE,
chainon (chainon ($10, current_continue_label),
$8 ? build_expr_stmt (input_filename, @8.first_line, $8) : NULL_TREE)))));
$$ = build_compound (input_filename, @1.first_line, chainon ($$, current_break_label));
popbreak(1); }
| SWITCH '(' expr ')'
{ $<ttype>$ = current_switch_stmt;
pushbreak(0);
current_switch_stmt
= build_switch_stmt (input_filename, @1.first_line,
default_conversion ($3)); }
stmt
{ $$ = build_compound (input_filename, @1.first_line,
chainon(current_switch_stmt,
chainon($6, current_break_label)));
finish_switch_stmt (current_switch_stmt, current_break_label);
popbreak (0);
current_switch_stmt = $<ttype>5; }
| CASE expr ':' stmt
{ register tree value = fold($2);
tree l = build_label (input_filename, @1.first_line, NULL_TREE, current_block);
if (TREE_CODE (value) != INTEGER_CST)
{
yyerror("case label does not reduce to an integer constant");
value = error_mark_node;
}
pushcase(value, l);
$$ = build_compound (input_filename, @1.first_line, chainon(l, $4));
}
| DEFAULT ':' stmt
{
tree l = build_label (input_filename, @1.first_line, 0, current_block);
pushcase(NULL_TREE, l);
$$ = build_compound (input_filename, @1.first_line, chainon(l, $3));
}
| BREAK ';'
{ if (current_break_label)
$$ = build_goto (input_filename, @1.first_line, STMT_BODY (current_break_label));
else
{
yyerror("break statement not within a do, for, while or switch statement");
$$ = error_mark_node;
}
}
| CONTINUE ';'
{ if (current_continue_label)
$$ = build_goto (input_filename, @1.first_line, STMT_BODY (current_continue_label));
else
{
yyerror("continue statement not within a do, for or while statement");
$$ = error_mark_node;
}
}
| RETURN ';'
{ $$ = build_return (input_filename, @1.first_line, NULL_TREE); }
| RETURN expr ';'
{ $$ = build_return_stmt (input_filename, @1.first_line, $2); }
| GOTO identifier ';'
{ pushgoto($$ = build_goto (input_filename, @1.first_line, $2)); }
| ASM '(' STRING ')' ';'
{ $$ = build_asm_stmt (input_filename, @1.first_line, $3); }
| identifier ':' stmt
{ $$ = build_compound (input_filename, @1.first_line, chainon (build_label (input_filename, @1.first_line, $1, current_block), $3)); }
| ';'
{ $$ = build_compound (input_filename, @1.first_line, 0); }
;
xexpr:
/* empty */
{ $$ = NULL_TREE; }
| expr
;
/* This is what appears inside the parens in a function declarator.
Is value is represented in the format that grokdeclarator expects. */
parmlist: /* empty */
{ $$ = NULL_TREE; }
| parms
{ $$ = chainon ($1, build_tree_list (NULL_TREE,
void_type_node)); }
| parms ',' ELLIPSIS
;
/* A nonempty list of parameter declarations or type names. */
parms:
parm
{ $$ = build_tree_list (NULL_TREE, $1); }
| parms ',' parm
{ $$ = chainon ($1, build_tree_list (NULL_TREE, $3)); }
;
parm:
typespecs declarator
{ $$ = build_tree_list ($1, $2) ; }
| typespecs absdcl
{ $$ = build_tree_list ($1, $2); }
;
/* A nonempty list of identifiers. */
identifiers:
IDENTIFIER
{ $$ = build_tree_list (NULL_TREE, $1); }
| identifiers ',' IDENTIFIER
{ $$ = chainon ($1, build_tree_list (NULL_TREE, $3)); }
;
%%
static tree
finish_compound_stmt(block, stmts, outer_block, line)
tree block, stmts, outer_block;
int line;
{
register tree decls = getdecls();
register tree tags = gettags ();
if (decls || tags)
{
finish_block (block, decls, tags, stmts);
poplevel();
current_block = outer_block;
STMT_SOURCE_LINE (block) = line;
return block;
}
else
{
current_block = outer_block;
poplevel();
return build_compound (input_filename, line, stmts);
}
}
static void
pushbreak(a)
int a;
{
if (current_break_label)
TREE_CHAIN (current_break_label) = break_label_stack;
break_label_stack = current_break_label;
current_break_label = build_label (0, 0, NULL_TREE, current_block);
if (a)
{
if (current_continue_label)
TREE_CHAIN (current_continue_label) = continue_label_stack;
continue_label_stack = current_continue_label;
current_continue_label = build_label (0, 0, NULL_TREE, current_block);
}
}
static void
popbreak(a)
int a;
{
current_break_label = break_label_stack;
if (current_break_label)
break_label_stack = TREE_CHAIN (break_label_stack);
if (a)
{
current_continue_label = continue_label_stack;
if (current_continue_label)
continue_label_stack = TREE_CHAIN (continue_label_stack);
}
if (current_break_label)
TREE_CHAIN (current_break_label) = NULL;
if (current_continue_label)
TREE_CHAIN (current_continue_label) = NULL;
}
/* Return something to represent absolute declarators containing a *.
TARGET is the absolute declarator that the * contains.
TYPEMODS is a list of modifiers such as const or volatile
to apply to the pointer type, represented as identifiers.
We return an INDIRECT_REF whose "contents" are TARGET
and whose type is the modifier list. */
static tree
make_pointer_declarator (typemods, target)
tree typemods, target;
{
register tree t = build1 (INDIRECT_REF, target);
TREE_TYPE (t) = typemods;
return t;
}
/* Given a chain of STRING_CST nodes,
concatenate them into one STRING_CST
and then return an ADDR_EXPR for it. */
static tree
combine_strings (strings)
tree strings;
{
register tree value, t;
if (TREE_CHAIN (strings))
{
/* More than one in the chain, so concatenate. */
register char *p, *q;
register int length = 1;
/* Don't include the \0 at the end of each substring,
except for the last one. */
for (t = strings; t; t = TREE_CHAIN (t))
length += TREE_STRING_LENGTH (t) - 1;
p = (char *) oballoc (length);
q = p;
for (t = strings; t; t = TREE_CHAIN (t))
{
bcopy (TREE_STRING_POINTER (t), q, TREE_STRING_LENGTH (t) - 1);
q += TREE_STRING_LENGTH (t) - 1;
}
*q = 0;
value = make_node (STRING_CST);
TREE_TYPE (value) = TREE_TYPE (strings);
TREE_STRING_POINTER (value) = p;
TREE_STRING_LENGTH (value) = length;
TREE_LITERAL (value) = 1;
}
else
value = strings;
value = build1 (ADDR_EXPR, value);
TREE_TYPE (value) = string_type_node;
TREE_LITERAL (value) = 1;
return value;
}
int lineno; /* current line number in file being read */
FILE *finput; /* input file.
Normally a pipe from the preprocessor. */
/* lexical analyzer */
static int maxtoken; /* Current length of token buffer */
static char *token_buffer; /* Pointer to token buffer */
/* frw[i] is index in rw of the first word whose length is i. */
#define MAXRESERVED 9
static char frw[10] =
{ 0, 0, 0, 2, 5, 13, 19, 27, 29, 33 };
static char *rw[] =
{ "if", "do", "int", "for", "asm",
"case", "char", "auto", "goto", "else", "long", "void", "enum",
"float", "short", "union", "break", "while", "const",
"double", "static", "extern", "struct", "return", "sizeof", "switch", "signed",
"typedef", "default",
"unsigned", "continue", "register", "volatile" };
static short rtoken[] =
{ IF, DO, TYPESPEC, FOR, ASM,
CASE, TYPESPEC, SCSPEC, GOTO, ELSE, TYPESPEC, TYPESPEC, ENUM,
TYPESPEC, TYPESPEC, UNION, BREAK, WHILE, TYPEMOD,
TYPESPEC, SCSPEC, SCSPEC, STRUCT, RETURN, SIZEOF, SWITCH, TYPESPEC,
SCSPEC, DEFAULT,
TYPESPEC, CONTINUE, SCSPEC, TYPEMOD };
/* This table corresponds to rw and rtoken.
Its element is an index in ridpointers */
#define NORID (enum rid) 0
static enum rid rid[] =
{ NORID, NORID, RID_INT, NORID, NORID,
NORID, RID_CHAR, RID_AUTO, NORID, NORID, RID_LONG, RID_VOID, NORID,
RID_FLOAT, RID_SHORT, NORID, NORID, NORID, RID_CONST,
RID_DOUBLE, RID_STATIC, RID_EXTERN, NORID, NORID, NORID, NORID, RID_SIGNED,
RID_TYPEDEF, NORID,
RID_UNSIGNED, NORID, RID_REGISTER, RID_VOLATILE };
/* The elements of `ridpointers' are identifier nodes
for the reserved type names and storage classes. */
tree ridpointers[(int) RID_MAX];
static tree line_identifier; /* The identifier node named "line" */
void check_newline();
void
init_lex()
{
extern char *malloc();
/* Start it at 0, because check_newline is called atthe very beginning
and will increment it to 1. */
lineno = 0;
current_function_decl = NULL;
current_switch_stmt = NULL;
current_block = NULL;
current_break_label = NULL;
current_continue_label = NULL;
break_label_stack = NULL;
continue_label_stack = NULL;
line_identifier = get_identifier("line");
maxtoken = 40;