-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgnuc_parser.yy
2547 lines (2318 loc) · 75.6 KB
/
gnuc_parser.yy
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
/*
* Copyright (C) 2019 SUSE Software Solutions Germany GmbH
*
* This file is part of klp-ccp.
*
* klp-ccp is free software: you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* klp-ccp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with klp-ccp. If not, see <https://www.gnu.org/licenses/>.
*/
%language "C++"
%skeleton "lalr1.cc"
%defines
%define api.namespace {klp::ccp::yy}
%define parser_class_name {gnuc_parser}
%name-prefix "pd."
%define api.location.type {pp_tokens_range}
%locations
%code requires {
#ifdef DEBUG_PARSER
#define YYDEBUG 1
#endif
#include "config.h"
#include "types.hh"
#include "ast.hh"
namespace klp {
namespace ccp
{
namespace yy
{
// Bison doesn't accept namespace specifications at
// %define api.location.type. Pull the type in.
typedef klp::ccp::pp_tokens_range pp_tokens_range;
class gnuc_parser_driver;
}
}
}
}
%code {
using namespace klp::ccp;
using namespace klp::ccp::ast;
#define YYLLOC_DEFAULT(Cur, Rhs, N) \
do { \
if (N) { \
Cur = pp_tokens_range{YYRHSLOC(Rhs, 1).begin, \
YYRHSLOC(Rhs, N).end}; \
} else { \
Cur = YYRHSLOC(Rhs, 0); \
} \
} while (0)
#include "gnuc_parser_driver.hh"
template<typename T>
static T* mv_p(T* &&p)
{
T *_p = nullptr;
std::swap(p, _p);
return _p;
}
#define MV_P(p) mv_p(std::move(p))
template <typename T>
static void empty(T* &value, klp::ccp::pp_tokens_range &loc)
{
value = nullptr;
loc.begin = loc.end;
}
static void empty(klp::ccp::pp_tokens_range &loc)
{
loc.begin = loc.end;
}
}
%parse-param {klp::ccp::yy::gnuc_parser_driver &pd}
%union {
klp::ccp::pp_token_index token_index;
klp::ccp::ast::assign_op assign_op;
klp::ccp::ast::binary_op binary_op;
klp::ccp::ast::unary_op_pre unary_op_pre;
klp::ccp::types::struct_or_union_kind struct_or_union_kind;
klp::ccp::types::ext_int_type::kind ext_int_kind;
klp::ccp::types::ext_float_type::kind ext_float_kind;
klp::ccp::ast::expr *expr;
klp::ccp::ast::offset_member_designator *offset_member_designator;
klp::ccp::ast::string_literal *string_literal;
klp::ccp::ast::generic_association_list *generic_association_list;
klp::ccp::ast::generic_association *generic_association;
klp::ccp::ast::expr_list *expr_list;
klp::ccp::ast::attribute *attribute;
klp::ccp::ast::attribute_list *attribute_list;
klp::ccp::ast::attribute_specifier *attribute_specifier;
klp::ccp::ast::attribute_specifier_list *attribute_specifier_list;
klp::ccp::ast::pointer *pointer;
klp::ccp::ast::direct_abstract_declarator *direct_abstract_declarator;
klp::ccp::ast::abstract_declarator *abstract_declarator;
klp::ccp::ast::type_name *type_name;
klp::ccp::ast::direct_declarator *direct_declarator;
klp::ccp::ast::declarator *declarator;
klp::ccp::ast::storage_class_specifier *storage_class_specifier;
klp::ccp::ast::type_qualifier *type_qualifier;
klp::ccp::ast::type_qualifier_list *type_qualifier_list;
klp::ccp::ast::type_specifier *type_specifier;
klp::ccp::ast::struct_declaration *struct_declaration;
klp::ccp::ast::struct_declarator *struct_declarator;
klp::ccp::ast::struct_declarator_list *struct_declarator_list;
klp::ccp::ast::unnamed_struct_or_union *unnamed_struct_or_union;
klp::ccp::ast::struct_declaration_list *struct_declaration_list;
klp::ccp::ast::enumerator *enumerator;
klp::ccp::ast::enumerator_list *enumerator_list;
klp::ccp::ast::function_specifier *function_specifier;
klp::ccp::ast::specifier_qualifier_list *specifier_qualifier_list;
klp::ccp::ast::declaration_specifiers *declaration_specifiers;
klp::ccp::ast::initializer *initializer;
klp::ccp::ast::initializer_list *initializer_list;
klp::ccp::ast::designator *designator;
klp::ccp::ast::designator_list *designator_list;
klp::ccp::ast::designation *designation;
klp::ccp::ast::asm_label *asm_label;
klp::ccp::ast::init_declarator *init_declarator;
klp::ccp::ast::init_declarator_list *init_declarator_list;
klp::ccp::ast::declaration *declaration;
klp::ccp::ast::static_assertion *static_assertion;
klp::ccp::ast::parameter_declaration *parameter_declaration;
klp::ccp::ast::parameter_declaration_list *parameter_declaration_list;
klp::ccp::ast::identifier_list *identifier_list;
klp::ccp::ast::declaration_list *declaration_list;
klp::ccp::ast::stmt *stmt;
klp::ccp::ast::stmt_compound *stmt_compound;
klp::ccp::ast::local_label_declaration *local_label_declaration;
klp::ccp::ast::local_label_declaration_list *local_label_declaration_list;
klp::ccp::ast::block_item *block_item;
klp::ccp::ast::block_item_list *block_item_list;
klp::ccp::ast::asm_directive *asm_directive;
klp::ccp::ast::asm_qualifier_list *asm_qualifier_list;
klp::ccp::ast::asm_operand_name *asm_operand_name;
klp::ccp::ast::asm_operand *asm_operand;
klp::ccp::ast::asm_operand_list *asm_operand_list;
klp::ccp::ast::asm_clobber_list *asm_clobber_list;
klp::ccp::ast::asm_jump_to_label_list *asm_jump_to_label_list;
klp::ccp::ast::function_definition *function_definition;
klp::ccp::ast::external_declaration *external_declaration;
klp::ccp::ast::translation_unit *translation_unit;
}
%destructor {} <token_index>
%destructor {} <assign_op>
%destructor {} <binary_op>
%destructor {} <unary_op_pre>
%destructor {} <struct_or_union_kind>
%destructor { $$.~kind(); } <ext_int_kind>
%destructor { $$.~kind(); } <ext_float_kind>
%destructor { delete $$; } <*>
/* keyword tokens */
%token TOK_KW_ASM
%token TOK_KW_TYPEDEF
%token TOK_KW_EXTERN
%token TOK_KW_STATIC
%token TOK_KW_AUTO
%token TOK_KW_REGISTER
%token TOK_KW_VOID
%token TOK_KW_CHAR
%token TOK_KW_SHORT
%token TOK_KW_INT
%token TOK_KW_LONG
%token TOK_KW_FLOAT
%token TOK_KW_DOUBLE
%token TOK_KW_SIGNED
%token TOK_KW_UNSIGNED
%token TOK_KW_BOOL
%token TOK_KW_COMPLEX
%token<ext_int_kind> TOK_KW_EXT_INT
%token<ext_float_kind> TOK_KW_EXT_FLOAT
%token TOK_KW_AUTO_TYPE
%token TOK_KW_STRUCT
%token TOK_KW_UNION
%token TOK_KW_ENUM
%token TOK_KW_CONST
%token TOK_KW_RESTRICT
%token TOK_KW_VOLATILE
%token TOK_KW_INLINE
%token TOK_KW_TYPEOF
%token TOK_KW_SIZEOF
%token TOK_KW_ALIGNOF
%token TOK_KW_GENERIC
%token TOK_KW_BUILTIN_CHOOSE_EXPR
%token TOK_KW_BUILTIN_OFFSETOF
%token TOK_KW_BUILTIN_TYPES_COMPATIBLE_P
%token TOK_KW_BUILTIN_VA_ARG
%token TOK_KW_ATTRIBUTE
%token TOK_KW_CASE
%token TOK_KW_DEFAULT
%token TOK_KW_LABEL
%token TOK_KW_IF
%token TOK_KW_ELSE
%token TOK_KW_SWITCH
%token TOK_KW_WHILE
%token TOK_KW_DO
%token TOK_KW_FOR
%token TOK_KW_GOTO
%token TOK_KW_CONTINUE
%token TOK_KW_BREAK
%token TOK_KW_RETURN
%token TOK_KW_STATIC_ASSERT
/* Preprocessor tokens. */
%token <token_index> TOK_STRING_LITERAL
%token <token_index> TOK_IDENTIFIER
%token <token_index> TOK_CONSTANT
/* Artificial token for an indentifier resolved to a typedef name. */
%token <token_index> TOK_TYPEDEF_IDENTIFIER
/* Punctuators which aren't necessarily operators. */
%token TOK_LPAREN
%token TOK_RPAREN
%token TOK_LBRACE
%token TOK_RBRACE
%token TOK_LBRACKET
%token TOK_RBRACKET
%token TOK_TRIPLE_DOT
%token TOK_DOT
%token TOK_COLON
%token TOK_SEMICOLON
%token TOK_COMMA
%token TOK_ASTERISK
%token TOK_EQ
/* Punctuators which are always operators. */
%token TOK_OP_ASSIGN_MUL
%token TOK_OP_ASSIGN_DIV
%token TOK_OP_ASSIGN_MOD
%token TOK_OP_ASSIGN_ADD
%token TOK_OP_ASSIGN_SUB
%token TOK_OP_ASSIGN_LSHIFT
%token TOK_OP_ASSIGN_RSHIFT
%token TOK_OP_ASSIGN_BIN_AND
%token TOK_OP_ASSIGN_BIN_XOR
%token TOK_OP_ASSIGN_BIN_OR
%token TOK_OP_LOGICAL_OR
%token TOK_OP_DOUBLE_AMPERSAND
%token TOK_OP_QUESTION_MARK
%token TOK_OP_DEREF_MEMBER
%token TOK_OP_INC
%token TOK_OP_DEC
%token TOK_OP_BIN_OR
%token TOK_OP_BIN_XOR
%token TOK_OP_REL_LT
%token TOK_OP_REL_GT
%token TOK_OP_REL_LEQ
%token TOK_OP_REL_GEQ
%token TOK_OP_EQ
%token TOK_OP_NEQ
%token TOK_OP_PLUS
%token TOK_OP_MINUS
%token TOK_OP_AMPERSAND
%token TOK_OP_DIV
%token TOK_OP_MODULO
%token TOK_OP_BIN_NEG
%token TOK_OP_LOGICAL_NOT
%token TOK_OP_LSHIFT
%token TOK_OP_RSHIFT
/* Define precedence in order to solve the dangling else problem */
%nonassoc TOK_KW_IF
%nonassoc TOK_KW_ELSE
/*
* Define precedence for resolving a shift/reduce conflict where an
* attribute follows the closing brace of a struct, union or enum
* declaration: this clashes with the declaration_specifiers
* production also allowing for attributes.
*/
%nonassoc TOK_DUMMY_PRIO_GREEDY_ATTR_LIST
%nonassoc TOK_KW_ATTRIBUTE
%type <translation_unit> translation_unit
%type <external_declaration> external_declaration
%type <function_definition> function_definition
%type <function_definition> function_definition_ext
%type <stmt_compound> function_definition_body
%type <declaration> declaration
%type <declaration> declaration_ext
%type <declaration_specifiers> declaration_specifiers_no_ts_opt
%type <declaration_specifiers> declaration_specifiers_no_ts
%type <declaration_specifiers> declaration_specifiers_w_non_att_no_ts
%type <declaration_specifiers> declaration_specifiers_ts_no_tdid
%type <declaration_specifiers> declaration_specifiers_ts
%type <declaration_specifiers> auto_type_declaration_specifiers
%type <storage_class_specifier> storage_class_specifier
%type <type_specifier> type_specifier_no_tdid
%type <type_qualifier> type_qualifier
%type <function_specifier> function_specifier
%type <attribute_specifier_list> attribute_specifier_list_opt
%type <attribute_specifier_list> attribute_specifier_list
%type <attribute_specifier> attribute_specifier
%type <attribute_list> attribute_list
%type <attribute> attribute_opt
%type <attribute> attribute
%type <token_index> attribute_name
%type <expr_list> attribute_parameters
%type <expr_list> expression_list_opt
%type <expr_list> expression_list
%type <type_specifier> struct_or_union_specifier
%type <struct_or_union_kind> struct_or_union
%type <token_index> id_or_tdid
%type <struct_declaration_list> struct_declaration_list_opt
%type <struct_declaration_list> struct_declaration_list
%type <struct_declaration> struct_declaration
%type <specifier_qualifier_list> specifier_qualifier_list_no_ts_opt
%type <specifier_qualifier_list> specifier_qualifier_list_no_ts
%type <specifier_qualifier_list> specifier_qualifier_list_ts_no_tdid
%type <specifier_qualifier_list> specifier_qualifier_list_ts
%type <type_qualifier_list> type_qualifier_list_opt
%type <type_qualifier_list> type_qualifier_list
%type <struct_declarator_list> struct_declarator_list_no_tdid
%type <struct_declarator> struct_declarator_no_tdid
%type <struct_declarator_list> struct_declarator_list_tdid
%type <struct_declarator> struct_declarator_tdid
%type <unnamed_struct_or_union> unnamed_struct_or_union
%type <type_specifier> enum_specifier
%type <enumerator_list> enumerator_list
%type <enumerator> enumerator
%type <type_specifier> typedef_name_no_tdid
%type <type_name> type_name
%type <abstract_declarator> abstract_declarator_opt
%type <abstract_declarator> abstract_declarator
%type <abstract_declarator> abstract_declarator_ptr
%type <abstract_declarator> abstract_declarator_non_ptr
%type <pointer> pointer_opt
%type <pointer> pointer
%type <direct_abstract_declarator> direct_abstract_declarator_opt
%type <direct_abstract_declarator> direct_abstract_declarator
%type <parameter_declaration_list> parameter_type_list_opt
%type <parameter_declaration_list> parameter_type_list
%type <parameter_declaration_list> parameter_list
%type <parameter_declaration> parameter_declaration_first
%type <parameter_declaration> parameter_declaration
%type <declarator> param_declarator_tdid
%type <declarator> param_declarator_tdid_in_parens
%type <direct_declarator> direct_param_declarator_tdid
%type <direct_declarator> direct_param_declarator_tdid_in_parens_tdid
%type <direct_declarator> direct_param_declarator_tdid_in_parens_no_tdid
%type <declarator> declarator_no_tdid_opt
%type <declarator> declarator_no_tdid
%type <direct_declarator> direct_declarator_no_tdid
%type <declarator> declarator_tdid_opt
%type <declarator> declarator_tdid
%type <direct_declarator> direct_declarator_tdid
%type <identifier_list> identifier_list_opt
%type <identifier_list> identifier_list
%type <declaration_list> declaration_list_opt
%type <declaration_list> declaration_list
%type <declaration> declaration_not_starting_with_att
%type <static_assertion> static_assertion
%type <declaration_specifiers> declaration_specifiers_not_starting_with_att_no_ts_opt
%type <declaration_specifiers> declaration_specifiers_not_starting_with_att_no_ts
%type <declaration_specifiers> declaration_specifiers_not_starting_with_att_ts_no_tdid
%type <declaration_specifiers> declaration_specifiers_not_starting_with_att_ts
%type <init_declarator_list> init_declarator_list_no_tdid_opt
%type <init_declarator_list> init_declarator_list_no_tdid
%type <init_declarator> init_declarator_no_tdid
%type <init_declarator_list> init_declarator_list_tdid_opt
%type <init_declarator_list> init_declarator_list_tdid
%type <init_declarator> init_declarator_tdid
%type <init_declarator_list> auto_type_init_declarator_list
%type <init_declarator> auto_type_init_declarator
%type <asm_label> asm_label
%type <initializer> initializer
%type <initializer_list> initializer_list
%type <designation> designation_opt
%type <designation> designation
%type <designator_list> designator_list
%type <designator> designator
%type <stmt> statement
%type <stmt> labeled_statement
%type <stmt_compound> compound_statement
%type <local_label_declaration_list> local_label_declaration_list_opt
%type <local_label_declaration_list> local_label_declaration_list
%type <local_label_declaration> local_label_declaration
%type <block_item_list> block_item_list_opt
%type <block_item_list> block_item_list
%type <block_item> block_item
%type <stmt> expression_statement
%type <stmt> selection_statement
%type <stmt> iteration_statement
%type <stmt> jump_statement
%type <stmt> asm_statement
%type <asm_directive> asm_directive
%type <asm_qualifier_list> asm_qualifier_list_opt
%type <asm_qualifier_list> asm_qualifier_list
%type <asm_operand_list> asm_operand_list_opt
%type <asm_operand_list> asm_operand_list
%type <asm_operand> asm_operand
%type <asm_operand_name> asm_operand_name_opt
%type <asm_operand_name> asm_operand_name
%type <asm_clobber_list> asm_clobber_list_opt
%type <asm_clobber_list> asm_clobber_list
%type <asm_jump_to_label_list> asm_jump_to_label_list_opt
%type <asm_jump_to_label_list> asm_jump_to_label_list
%type <expr> constant_expression
%type <expr> expression_opt
%type <expr> expression
%type <expr> assignment_expression_opt
%type <expr> assignment_expression
%type <assign_op> assignment_operator
%type <expr> conditional_expression
%type <expr> logical_or_expression
%type <expr> logical_and_expression
%type <expr> inclusive_or_expression
%type <expr> exclusive_or_expression
%type <expr> and_expression
%type <expr> equality_expression
%type <binary_op> equality_operator
%type <expr> relational_expression
%type <binary_op> relational_operator
%type <expr> shift_expression
%type <binary_op> shift_operator
%type <expr> additive_expression
%type <expr> multiplicative_expression
%type <expr> cast_expression
%type <expr> unary_expression
%type <unary_op_pre> unary_operator
%type <offset_member_designator> offset_member_designator
%type <expr> postfix_expression
%type <expr> statement_expression
%type <expr> primary_expression
%type <string_literal> string_literal
%type <generic_association_list> generic_association_list;
%type <generic_association> generic_association;
%%
start:
/* empty */
{ pd.leave_td_scope(); }
| translation_unit
{
pd.leave_td_scope();
pd._result = MV_P($1);
}
;
translation_unit:
external_declaration
{ $$ = new translation_unit(std::move($1)); }
| translation_unit external_declaration
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| TOK_SEMICOLON
{ $$ = new translation_unit(@1); }
| translation_unit TOK_SEMICOLON
{ $$ = MV_P($1);}
;
external_declaration:
function_definition_ext
{ $$ = new external_declaration_func(std::move($1)); }
| declaration_ext
{ $$ = new external_declaration_decl(std::move($1)); }
| static_assertion
{ $$ = new external_declaration_static_assert(std::move($1)); }
| asm_directive
{ $$ = new external_declaration_asm(std::move($1)); }
;
function_definition_ext:
function_definition
{ $$ = MV_P($1); }
| declarator_no_tdid
attribute_specifier_list_opt { pd.handle_decl_id($1->get_id_tok()); }
declaration_list_opt function_definition_body
{
declaration_specifiers *ds =
new declaration_specifiers(pp_tokens_range(@1.begin, @1.begin));
try {
$$ = new function_definition(@$, std::move(ds), std::move($1),
std::move($2), std::move($4),
std::move($5));
} catch (...) {
delete ds;
throw;
}
}
;
function_definition:
declaration_specifiers_no_ts declarator_no_tdid
attribute_specifier_list_opt { pd.handle_decl_id($2->get_id_tok()); }
declaration_list_opt function_definition_body
{
$$ = new function_definition(@$, std::move($1), std::move($2),
std::move($3), std::move($5),
std::move($6));
}
| declaration_specifiers_ts declarator_tdid
attribute_specifier_list_opt { pd.handle_decl_id($2->get_id_tok()); }
declaration_list_opt function_definition_body
{
$$ = new function_definition(@$, std::move($1), std::move($2),
std::move($3), std::move($5),
std::move($6));
}
;
function_definition_body:
TOK_LBRACE {
pd.clear_in_typedef();
pd.restore_stashed_td_scope();
}
local_label_declaration_list_opt block_item_list_opt TOK_RBRACE
{
pd.leave_td_scope();
$$ = new stmt_compound(@$, std::move($3), std::move($4));
}
;
static_assertion:
TOK_KW_STATIC_ASSERT TOK_LPAREN expression TOK_COMMA string_literal TOK_RPAREN TOK_SEMICOLON
{ $$ = new static_assertion(@$, std::move($3), std::move($5)); }
;
declaration_ext:
init_declarator_list_no_tdid TOK_SEMICOLON
{
declaration_specifiers *ds =
new declaration_specifiers(pp_tokens_range(@1.begin, @1.begin));
try {
$$ = new declaration(@$, std::move(ds), std::move($1));
} catch (...) {
delete ds;
throw;
}
}
| declaration
{
$$ = MV_P($1);
}
;
declaration:
declaration_specifiers_no_ts init_declarator_list_no_tdid_opt TOK_SEMICOLON
{
pd.clear_in_typedef();
$$ = new declaration(@$, std::move($1), std::move($2));
}
| declaration_specifiers_ts init_declarator_list_tdid_opt TOK_SEMICOLON
{
pd.clear_in_typedef();
$$ = new declaration(@$, std::move($1), std::move($2));
}
| auto_type_declaration_specifiers auto_type_init_declarator_list TOK_SEMICOLON
{
$$ = new declaration(@$, std::move($1), std::move($2));
}
;
declaration_specifiers_no_ts_opt:
/* empty */
{ empty($$, @$); }
| declaration_specifiers_no_ts
{ $$ = MV_P($1); }
;
declaration_specifiers_no_ts:
declaration_specifiers_w_non_att_no_ts
{ $$ = MV_P($1); }
| attribute_specifier_list
{ $$ = new declaration_specifiers(std::move($1)); }
;
declaration_specifiers_w_non_att_no_ts:
storage_class_specifier
{ $$ = new declaration_specifiers(std::move($1)); }
| attribute_specifier_list storage_class_specifier
{
$$ = new declaration_specifiers(std::move($1));
$$->extend(std::move($2));
}
| declaration_specifiers_w_non_att_no_ts storage_class_specifier
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| type_qualifier
{ $$ = new declaration_specifiers(std::move($1)); }
| attribute_specifier_list type_qualifier
{
$$ = new declaration_specifiers(std::move($1));
$$->extend(std::move($2));
}
| declaration_specifiers_w_non_att_no_ts type_qualifier
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| function_specifier
{ $$ = new declaration_specifiers(std::move($1)); }
| attribute_specifier_list function_specifier
{
$$ = new declaration_specifiers(std::move($1));
$$->extend(std::move($2));
}
| declaration_specifiers_w_non_att_no_ts function_specifier
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| declaration_specifiers_w_non_att_no_ts attribute_specifier_list %prec TOK_DUMMY_PRIO_GREEDY_ATTR_LIST
{ $$ = MV_P($1); $$->extend(std::move($2)); }
;
declaration_specifiers_ts_no_tdid:
declaration_specifiers_no_ts_opt type_specifier_no_tdid
{
if (!$1) {
$$ = new declaration_specifiers(std::move($2));
@$.begin = @2.begin;
} else {
$$ = MV_P($1);
$$->extend(std::move($2));
}
}
| declaration_specifiers_ts_no_tdid type_specifier_no_tdid
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| declaration_specifiers_ts_no_tdid storage_class_specifier
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| declaration_specifiers_ts_no_tdid type_qualifier
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| declaration_specifiers_ts_no_tdid function_specifier
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| declaration_specifiers_ts_no_tdid attribute_specifier_list %prec TOK_DUMMY_PRIO_GREEDY_ATTR_LIST
{ $$ = MV_P($1); $$->extend(std::move($2)); }
;
declaration_specifiers_ts:
declaration_specifiers_ts_no_tdid
{ $$ = MV_P($1); }
| declaration_specifiers_no_ts TOK_TYPEDEF_IDENTIFIER declaration_specifiers_no_ts_opt
{
type_specifier_tdid *ts_tdid = new type_specifier_tdid($2);
$$ = MV_P($1);
$$->extend(std::move(ts_tdid));
$$->extend(std::move($3));
}
| TOK_TYPEDEF_IDENTIFIER declaration_specifiers_no_ts_opt
{
type_specifier_tdid *ts_tdid = new type_specifier_tdid($1);
try {
$$ = new declaration_specifiers(std::move(ts_tdid));
} catch (...) {
delete ts_tdid;
}
$$->extend(std::move($2));
}
;
auto_type_declaration_specifiers:
declaration_specifiers_no_ts_opt TOK_KW_AUTO_TYPE declaration_specifiers_no_ts_opt
{
type_specifier_auto_type *ts_auto_type = new type_specifier_auto_type(@2.begin);
if (!$1) {
try {
$$ = new declaration_specifiers(std::move(ts_auto_type));
} catch (...) {
delete ts_auto_type;
}
@$.begin = @2.begin;
} else {
$$ = MV_P($1);
$$->extend(std::move(ts_auto_type));
}
if ($3)
$$->extend(std::move($3));
}
;
storage_class_specifier:
TOK_KW_TYPEDEF
{
pd.set_in_typedef();
$$ = new storage_class_specifier(@$, storage_class::sc_typedef);
}
| TOK_KW_EXTERN
{ $$ = new storage_class_specifier(@$, storage_class::sc_extern); }
| TOK_KW_STATIC
{ $$ = new storage_class_specifier(@$, storage_class::sc_static); }
| TOK_KW_AUTO
{ $$ = new storage_class_specifier(@$, storage_class::sc_auto); }
| TOK_KW_REGISTER
{ $$ = new storage_class_specifier(@$, storage_class::sc_register); }
;
type_specifier_no_tdid:
TOK_KW_VOID
{ $$ = new type_specifier_pod(@$, pod_spec::ps_void); }
| TOK_KW_CHAR
{ $$ = new type_specifier_pod(@$, pod_spec::ps_char); }
| TOK_KW_SHORT
{ $$ = new type_specifier_pod(@$, pod_spec::ps_short); }
| TOK_KW_INT
{ $$ = new type_specifier_pod(@$, pod_spec::ps_int); }
| TOK_KW_LONG
{ $$ = new type_specifier_pod(@$, pod_spec::ps_long); }
| TOK_KW_FLOAT
{ $$ = new type_specifier_pod(@$, pod_spec::ps_float); }
| TOK_KW_DOUBLE
{ $$ = new type_specifier_pod(@$, pod_spec::ps_double); }
| TOK_KW_SIGNED
{ $$ = new type_specifier_pod(@$, pod_spec::ps_signed); }
| TOK_KW_UNSIGNED
{ $$ = new type_specifier_pod(@$, pod_spec::ps_unsigned); }
| TOK_KW_BOOL
{ $$ = new type_specifier_pod(@$, pod_spec::ps_bool); }
| TOK_KW_COMPLEX
{ $$ = new type_specifier_pod(@$, pod_spec::ps_complex); }
| TOK_KW_EXT_INT
{ $$ = new type_specifier_ext_int(@$, $1); }
| TOK_KW_EXT_FLOAT
{ $$ = new type_specifier_ext_float(@$, $1); }
| struct_or_union_specifier
{ $$ =MV_P($1); }
| enum_specifier
{ $$ =MV_P($1); }
| typedef_name_no_tdid
{ $$ =MV_P($1); }
;
type_qualifier:
TOK_KW_CONST
{ $$ = new type_qualifier(@$, types::qualifier::q_const); }
| TOK_KW_RESTRICT
{ $$ = new type_qualifier(@$, types::qualifier::q_restrict); }
| TOK_KW_VOLATILE
{ $$ = new type_qualifier(@$, types::qualifier::q_volatile); }
;
function_specifier:
TOK_KW_INLINE
{ $$ = new function_specifier(@1.begin); }
;
attribute_specifier_list_opt:
/* empty */ %prec TOK_DUMMY_PRIO_GREEDY_ATTR_LIST
{ empty($$, @$); };
| attribute_specifier_list %prec TOK_DUMMY_PRIO_GREEDY_ATTR_LIST
{ $$ = MV_P($1); }
;
attribute_specifier_list:
attribute_specifier
{ $$ = new attribute_specifier_list(std::move($1)); }
| attribute_specifier_list attribute_specifier
{ $$ = MV_P($1); $$->extend(std::move($2)); }
;
attribute_specifier:
TOK_KW_ATTRIBUTE TOK_LPAREN TOK_LPAREN attribute_list TOK_RPAREN TOK_RPAREN
{ $$ = new attribute_specifier(@$, std::move($4)); }
;
attribute_list:
attribute_opt
{ $$ = new attribute_list(@$, std::move($1)); }
| attribute_list TOK_COMMA attribute_opt
{ $$ = MV_P($1); $$->extend(@3, std::move($3)); }
;
attribute_opt:
/* empty */
{ empty($$, @$); }
| attribute
{ $$ = MV_P($1); }
;
attribute:
attribute_name
{ $$ = new attribute($1); }
| attribute_name TOK_LPAREN attribute_parameters TOK_RPAREN
{ $$ = new attribute(@$, $1, std::move($3)); }
;
attribute_name:
id_or_tdid
{ $$ = $1; }
| TOK_KW_CONST
{ $$ = @$.begin; }
;
attribute_parameters:
expression_list_opt
{ $$ = MV_P($1); }
;
expression_list_opt:
/* empty */
{ empty($$, @$); }
| expression_list
{ $$ = MV_P($1); }
;
expression_list:
assignment_expression
{ $$ = new expr_list(std::move($1)); }
| expression_list TOK_COMMA assignment_expression
{ $$ = MV_P($1); $$->extend(std::move($3)); }
;
struct_or_union_specifier:
struct_or_union attribute_specifier_list_opt id_or_tdid TOK_LBRACE struct_declaration_list_opt TOK_RBRACE attribute_specifier_list_opt
{
$$ = new struct_or_union_def(@$, $1, $3, std::move($5),
std::move($2), std::move($7));
}
| struct_or_union attribute_specifier_list_opt TOK_LBRACE struct_declaration_list_opt TOK_RBRACE attribute_specifier_list_opt
{
$$ = new struct_or_union_def(@$, $1, std::move($4),
std::move($2), std::move($6));
}
| struct_or_union attribute_specifier_list_opt id_or_tdid
{ $$ = new struct_or_union_ref(@$, $1, $3, std::move($2)); }
;
struct_or_union:
TOK_KW_STRUCT
{ $$ = types::struct_or_union_kind::souk_struct; }
| TOK_KW_UNION
{ $$ = types::struct_or_union_kind::souk_union; }
;
id_or_tdid:
TOK_IDENTIFIER
{ $$ = $1; }
| TOK_TYPEDEF_IDENTIFIER
{ $$ = $1; }
;
struct_declaration_list_opt:
/* empty */
{ empty($$, @$); }
| semicolons
{ $$ = nullptr; }
| struct_declaration_list
{ $$ = MV_P($1); }
;
struct_declaration_list:
semicolons_opt struct_declaration semicolons_opt
{ $$ = new struct_declaration_list(std::move($2)); }
| struct_declaration_list struct_declaration semicolons_opt
{ $$ = MV_P($1); $$->extend(std::move($2)); }
;
semicolons_opt:
/* empty */
{ empty(@$); }
| semicolons
;
semicolons:
TOK_SEMICOLON
| semicolons TOK_SEMICOLON
;
/*
* The specifier_qualifier_list in front of the unnamed_struct_or_union
* below should really be a type_qualifier_list. However, this would result
* in a reduce/reduce conflict. Hence, allow for the superset when parsing.
*/
struct_declaration:
specifier_qualifier_list_no_ts struct_declarator_list_no_tdid TOK_SEMICOLON
{ $$ = new struct_declaration_c99(@$, std::move($1), std::move($2)); }
| specifier_qualifier_list_ts struct_declarator_list_tdid TOK_SEMICOLON
{ $$ = new struct_declaration_c99(@$, std::move($1), std::move($2)); }
| specifier_qualifier_list_no_ts unnamed_struct_or_union TOK_SEMICOLON
{
$$ = new struct_declaration_unnamed_sou(@$, std::move($1),
std::move($2));
}
| unnamed_struct_or_union TOK_SEMICOLON
{
$$ = new struct_declaration_unnamed_sou(@$, nullptr,
std::move($1));
}
| static_assertion
{
$$ = new struct_declaration_static_assert(std::move($1));
}
;
specifier_qualifier_list_no_ts_opt:
/* empty */
{ empty($$, @$); }
| specifier_qualifier_list_no_ts
{ $$ = MV_P($1); }
;
specifier_qualifier_list_no_ts:
type_qualifier
{ $$ = new specifier_qualifier_list(std::move($1)); }
| specifier_qualifier_list_no_ts type_qualifier
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| attribute_specifier_list %prec TOK_DUMMY_PRIO_GREEDY_ATTR_LIST
{ $$ = new specifier_qualifier_list(std::move($1)); }
| specifier_qualifier_list_no_ts attribute_specifier_list %prec TOK_DUMMY_PRIO_GREEDY_ATTR_LIST
{ $$ = MV_P($1); $$->extend(std::move($2)); }
;
specifier_qualifier_list_ts_no_tdid:
type_specifier_no_tdid
{ $$ = new specifier_qualifier_list(std::move($1)); }
| specifier_qualifier_list_no_ts type_specifier_no_tdid
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| specifier_qualifier_list_ts_no_tdid type_specifier_no_tdid
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| specifier_qualifier_list_ts_no_tdid type_qualifier
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| specifier_qualifier_list_ts_no_tdid attribute_specifier_list %prec TOK_DUMMY_PRIO_GREEDY_ATTR_LIST
{ $$ = MV_P($1); $$->extend(std::move($2)); }
;
specifier_qualifier_list_ts:
specifier_qualifier_list_ts_no_tdid
{ $$ = MV_P($1); }
| specifier_qualifier_list_no_ts_opt TOK_TYPEDEF_IDENTIFIER specifier_qualifier_list_no_ts_opt
{
type_specifier_tdid *ts_tdid = new type_specifier_tdid($2);
if (!$1) {
try {
$$ = new specifier_qualifier_list(std::move(ts_tdid));
} catch (...) {
delete ts_tdid;
}
@$.begin = @2.begin;
} else {
$$ = MV_P($1);
$$->extend(std::move(ts_tdid));
}
$$->extend(std::move($3));
}
;
type_qualifier_list_opt:
/* empty */
{ empty($$, @$); }
| type_qualifier_list
{ $$ = MV_P(std::move($1)); }
;
type_qualifier_list:
type_qualifier
{ $$ = new type_qualifier_list(std::move($1)); }
| type_qualifier_list type_qualifier
{ $$ = MV_P($1); $$->extend(std::move($2)); }
| attribute_specifier_list %prec TOK_DUMMY_PRIO_GREEDY_ATTR_LIST
{ $$ = new type_qualifier_list(std::move($1)); }