source: src/Parser/parser.yy @ a2a8d2a6

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since a2a8d2a6 was 2acf5fc, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

Merge branch 'master' of plg2:software/cfa/cfa-cc

Conflicts:

src/Parser/parser.cc
src/Parser/parser.yy

  • Property mode set to 100644
File size: 103.8 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[974906e2]7// cfa.y --
8//
[c11e31c]9// Author           : Peter A. Buhr
10// Created On       : Sat Sep  1 20:22:55 2001
11// Last Modified By : Peter A. Buhr
[2acf5fc]12// Last Modified On : Mon Aug 22 14:30:56 2016
13// Update Count     : 1944
[974906e2]14//
[c11e31c]15
[de62360d]16// This grammar is based on the ANSI99/11 C grammar, specifically parts of EXPRESSION and STATEMENTS, and on the C
17// grammar by James A. Roskind, specifically parts of DECLARATIONS and EXTERNAL DEFINITIONS.  While parts have been
18// copied, important changes have been made in all sections; these changes are sufficient to constitute a new grammar.
19// In particular, this grammar attempts to be more syntactically precise, i.e., it parses less incorrect language syntax
20// that must be subsequently rejected by semantic checks.  Nevertheless, there are still several semantic checks
21// required and many are noted in the grammar. Finally, the grammar is extended with GCC and CFA language extensions.
[c11e31c]22
[de62360d]23// Acknowledgments to Richard Bilson, Glen Ditchfield, and Rodolfo Gabriel Esteves who all helped when I got stuck with
24// the grammar.
[c11e31c]25
26// The root language for this grammar is ANSI99/11 C. All of ANSI99/11 is parsed, except for:
27//
28// 1. designation with '=' (use ':' instead)
29//
[de62360d]30// Most of the syntactic extensions from ANSI90 to ANSI11 C are marked with the comment "C99/C11". This grammar also has
31// two levels of extensions. The first extensions cover most of the GCC C extensions, except for:
[c11e31c]32//
[e7aed49]33// 1. designation with and without '=' (use ':' instead)
34// 2. attributes not allowed in parenthesis of declarator
[c11e31c]35//
[de62360d]36// All of the syntactic extensions for GCC C are marked with the comment "GCC". The second extensions are for Cforall
37// (CFA), which fixes several of C's outstanding problems and extends C with many modern language concepts. All of the
38// syntactic extensions for CFA C are marked with the comment "CFA". As noted above, there is one unreconcileable
39// parsing problem between C99 and CFA with respect to designators; this is discussed in detail before the "designation"
40// grammar rule.
[51b7345]41
42%{
[b87a5ed]43#define YYDEBUG_LEXER_TEXT (yylval)                                             // lexer loads this up each time
44#define YYDEBUG 1                                                                               // get the pretty debugging code to compile
[51b7345]45
46#undef __GNUC_MINOR__
47
48#include <cstdio>
49#include <stack>
50#include "lex.h"
[984dce6]51#include "parser.h"
[51b7345]52#include "ParseNode.h"
[984dce6]53#include "TypedefTable.h"
[1db21619]54#include "TypeData.h"
[51b7345]55#include "LinkageSpec.h"
56
[ab57786]57union DeclQualifiers {
58        unsigned int value;                                                                     // assume 32-bits
59        struct {
60                bool Extern : 1;
61                bool Static : 1;
62                bool Auto : 1;
63                bool Register : 1;
64                bool Inline : 1;
65                bool Fortran : 1;
66                bool Noreturn : 1;
67                bool Threadlocal : 1;
68                bool Extension : 1;
69                bool Lvalue : 1;
70                bool Const : 1;
71                bool Volatile : 1;
72                bool Restrict : 1;
73                bool Atomic : 1;
74        } qual;
75}; // DeclQualifiers
76DeclQualifiers declQualifiers = { 0 };
77
78union DeclType {
79        unsigned int value;                                                                     // assume 32-bits
80        struct {
81                bool Char : 1;
82                bool Bool : 1;
83                bool Short : 1;
84                bool Int : 1;
85                bool Float : 1;
86                bool Double : 1;
87                bool Long : 1;
88                bool Signed : 1;
89                bool Unsigned : 1;
90                bool Void : 1;
91                bool Complex : 1;
92                bool Imaginary : 1;
93                bool Valist : 1;
94        } type;
95}; // DeclType
96DeclType declTypes = { 0 };
97
[cbaee0d]98extern DeclarationNode * parseTree;
[8b7ee09]99extern LinkageSpec::Spec linkage;
[0da3e2c]100extern TypedefTable typedefTable;
101
[8b7ee09]102std::stack< LinkageSpec::Spec > linkageStack;
[7bf7fb9]103
[ab57786]104void appendStr( std::string *to, std::string *from ) {
[7bf7fb9]105        // "abc" "def" "ghi" => "abcdefghi", remove new text from quotes and insert before last quote in old string.
[ab57786]106        to->insert( to->length() - 1, from->substr( 1, from->length() - 2 ) );
[7bf7fb9]107} // appendStr
[51b7345]108%}
109
[c11e31c]110//************************* TERMINAL TOKENS ********************************
[51b7345]111
[c11e31c]112// keywords
[51b7345]113%token TYPEDEF
114%token AUTO EXTERN REGISTER STATIC
[b87a5ed]115%token INLINE                                                                                   // C99
116%token FORTRAN                                                                                  // C99, extension ISO/IEC 9899:1999 Section J.5.9(1)
[51b7345]117%token CONST VOLATILE
[b87a5ed]118%token RESTRICT                                                                                 // C99
119%token FORALL LVALUE                                                                    // CFA
[51b7345]120%token VOID CHAR SHORT INT LONG FLOAT DOUBLE SIGNED UNSIGNED
[90c3b1c]121%token VALIST                                                                                   // GCC
[b87a5ed]122%token BOOL COMPLEX IMAGINARY                                                   // C99
123%token TYPEOF LABEL                                                                             // GCC
[51b7345]124%token ENUM STRUCT UNION
[e7aed49]125%token OTYPE FTYPE DTYPE TRAIT                                                  // CFA
[5721a6d]126%token SIZEOF OFFSETOF
[b87a5ed]127%token ATTRIBUTE EXTENSION                                                              // GCC
[51b7345]128%token IF ELSE SWITCH CASE DEFAULT DO WHILE FOR BREAK CONTINUE GOTO RETURN
[02e5ab6]129%token CHOOSE DISABLE ENABLE FALLTHRU TRY CATCH CATCHRESUME FINALLY THROW THROWRESUME AT        // CFA
[b87a5ed]130%token ASM                                                                                              // C99, extension ISO/IEC 9899:1999 Section J.5.10(1)
[c11e31c]131%token ALIGNAS ALIGNOF ATOMIC GENERIC NORETURN STATICASSERT THREADLOCAL // C11
[51b7345]132
[c11e31c]133// names and constants: lexer differentiates between identifier and typedef names
[b87a5ed]134%token<tok> IDENTIFIER                  QUOTED_IDENTIFIER               TYPEDEFname                             TYPEGENname
135%token<tok> ATTR_IDENTIFIER             ATTR_TYPEDEFname                ATTR_TYPEGENname
136%token<tok> INTEGERconstant             FLOATINGconstant                CHARACTERconstant               STRINGliteral
137%token<tok> ZERO                                ONE                                             // CFA
[51b7345]138
[c11e31c]139// multi-character operators
[b87a5ed]140%token ARROW                                                                                    // ->
141%token ICR DECR                                                                                 // ++   --
142%token LS RS                                                                                    // <<   >>
143%token LE GE EQ NE                                                                              // <=   >=      ==      !=
144%token ANDAND OROR                                                                              // &&   ||
145%token ELLIPSIS                                                                                 // ...
146
147%token MULTassign       DIVassign       MODassign                               // *=   /=      %=/
148%token PLUSassign       MINUSassign                                                     // +=   -=
149%token LSassign         RSassign                                                        // <<=  >>=
150%token ANDassign        ERassign        ORassign                                // &=   ^=      |=
[51b7345]151
[e7aed49]152%token ATassign                                                                                 // @=
[097e2b0]153
[c11e31c]154// Types declaration
[51b7345]155%union
156{
[b87a5ed]157        Token tok;
158        ParseNode *pn;
159        ExpressionNode *en;
160        DeclarationNode *decl;
[68cd1ce]161        DeclarationNode::Aggregate aggKey;
[b87a5ed]162        DeclarationNode::TypeClass tclass;
163        StatementNode *sn;
[d1625f8]164        ConstantExpr *constant;
[2f22cc4]165        ForCtl *fctl;
[7f5566b]166        LabelNode *label;
[b87a5ed]167        InitializerNode *in;
[d9e2280]168        OperKinds op;
[ab57786]169        std::string *str;
[7f5566b]170        bool flag;
[51b7345]171}
172
[097e2b0]173%type<tok> identifier  no_01_identifier  no_attr_identifier zero_one
[2871210]174%type<tok> identifier_or_type_name  no_attr_identifier_or_type_name  no_01_identifier_or_type_name
[ab57786]175%type<constant> string_literal
176%type<str> string_literal_list
[51b7345]177
[c11e31c]178// expressions
[d1625f8]179%type<en> constant
[b87a5ed]180%type<en> tuple                                                 tuple_expression_list
[9706554]181%type<op> ptrref_operator                               unary_operator                          assignment_operator
[b87a5ed]182%type<en> primary_expression                    postfix_expression                      unary_expression
183%type<en> cast_expression                               multiplicative_expression       additive_expression                     shift_expression
184%type<en> relational_expression                 equality_expression                     AND_expression                          exclusive_OR_expression
185%type<en> inclusive_OR_expression               logical_AND_expression          logical_OR_expression           conditional_expression
186%type<en> constant_expression                   assignment_expression           assignment_expression_opt
187%type<en> comma_expression                              comma_expression_opt
[2f22cc4]188%type<en> argument_expression_list              argument_expression                     assignment_opt
189%type<fctl> for_control_expression
[51b7345]190%type<en> subrange
[7f5566b]191%type<en> asm_operands_opt asm_operands_list asm_operand
192%type<label> label_list
[d1625f8]193%type<en> asm_clobbers_list_opt
[7f5566b]194%type<flag> asm_volatile_opt
[51b7345]195
[c11e31c]196// statements
[b87a5ed]197%type<sn> labeled_statement                             compound_statement                      expression_statement            selection_statement
[097e2b0]198%type<sn> iteration_statement                   jump_statement                          exception_statement                     asm_statement
[b87a5ed]199%type<sn> fall_through_opt                              fall_through
200%type<sn> statement                                             statement_list
201%type<sn> block_item_list                               block_item
[51b7345]202%type<sn> case_clause
[8688ce1]203%type<en> case_value
204%type<sn> case_value_list                               case_label                                      case_label_list
[b87a5ed]205%type<sn> switch_clause_list_opt                switch_clause_list                      choose_clause_list_opt          choose_clause_list
[1d4580a]206%type<sn> handler_list                                  handler_clause                          finally_clause
[51b7345]207
[c11e31c]208// declarations
[51b7345]209%type<decl> abstract_array abstract_declarator abstract_function abstract_parameter_array
210%type<decl> abstract_parameter_declaration abstract_parameter_declarator abstract_parameter_function
211%type<decl> abstract_parameter_ptr abstract_ptr
212
213%type<aggKey> aggregate_key
214%type<decl>  aggregate_name
215
216%type<decl> array_dimension array_parameter_1st_dimension array_parameter_dimension multi_array_dimension
217
218%type<decl> assertion assertion_list_opt
219
220%type<en>   bit_subrange_size_opt bit_subrange_size
221
222%type<decl> basic_declaration_specifier basic_type_name basic_type_specifier direct_type_name indirect_type_name
223
[4040425]224%type<decl> trait_declaration trait_declaration_list trait_declaring_list trait_specifier
[51b7345]225
226%type<decl> declaration declaration_list declaration_list_opt declaration_qualifier_list
227%type<decl> declaration_specifier declarator declaring_list
228
229%type<decl> elaborated_type_name
230
231%type<decl> enumerator_list enum_name
232%type<en> enumerator_value_opt
233
234%type<decl> exception_declaration external_definition external_definition_list external_definition_list_opt
235
236%type<decl> field_declaration field_declaration_list field_declarator field_declaring_list
237%type<en> field field_list
238
[4d51835]239%type<decl> external_function_definition function_definition function_array function_declarator function_no_ptr function_ptr
[51b7345]240
241%type<decl> identifier_parameter_array identifier_parameter_declarator identifier_parameter_function
242%type<decl> identifier_parameter_ptr identifier_list
243
244%type<decl> new_abstract_array new_abstract_declarator_no_tuple new_abstract_declarator_tuple
245%type<decl> new_abstract_function new_abstract_parameter_declaration new_abstract_parameter_list
246%type<decl> new_abstract_ptr new_abstract_tuple
247
248%type<decl> new_array_parameter_1st_dimension
249
[4040425]250%type<decl> new_trait_declaring_list new_declaration new_field_declaring_list
[51b7345]251%type<decl> new_function_declaration new_function_return new_function_specifier
252
253%type<decl> new_identifier_parameter_array new_identifier_parameter_declarator_no_tuple
254%type<decl> new_identifier_parameter_declarator_tuple new_identifier_parameter_ptr
255
256%type<decl> new_parameter_declaration new_parameter_list new_parameter_type_list new_parameter_type_list_opt
257
258%type<decl> new_typedef_declaration new_variable_declaration new_variable_specifier
259
260%type<decl> old_declaration old_declaration_list old_declaration_list_opt old_function_array
261%type<decl> old_function_declarator old_function_no_ptr old_function_ptr
262
263%type<decl> parameter_declaration parameter_list parameter_type_list
264%type<decl> parameter_type_list_opt
265
[2871210]266%type<decl> paren_identifier paren_type
[51b7345]267
[0da3e2c]268%type<decl> storage_class storage_class_list
[51b7345]269
270%type<decl> sue_declaration_specifier sue_type_specifier
271
272%type<tclass> type_class
273%type<decl> type_declarator type_declarator_name type_declaring_list
274
[2871210]275%type<decl> typedef type_array typedef_declaration typedef_declaration_specifier typedef_expression
276%type<decl> type_function type_parameter_array type_parameter_function type_parameter_ptr
[c6b1105]277%type<decl> type_parameter_redeclarator type_ptr variable_type_redeclarator typedef_type_specifier
[2871210]278%type<decl> typegen_declaration_specifier typegen_type_specifier typegen_name
[51b7345]279
280%type<decl> type_name type_name_no_function
281%type<decl> type_parameter type_parameter_list
282
283%type<en> type_name_list
284
285%type<decl> type_qualifier type_qualifier_name type_qualifier_list type_qualifier_list_opt type_specifier
286
287%type<decl> variable_abstract_array variable_abstract_declarator variable_abstract_function
288%type<decl> variable_abstract_ptr variable_array variable_declarator variable_function variable_ptr
289
[1db21619]290%type<decl> attribute_list_opt attribute_list attribute
291
[c11e31c]292// initializers
[51b7345]293%type<in>  initializer initializer_list initializer_opt
294
[c11e31c]295// designators
[51b7345]296%type<en>  designator designator_list designation
297
298
[b87a5ed]299// Handle single shift/reduce conflict for dangling else by shifting the ELSE token. For example, this string
300// is ambiguous:
301// .---------.                          matches IF '(' comma_expression ')' statement
[c11e31c]302// if ( C ) S1 else S2
[b87a5ed]303// `-----------------'          matches IF '(' comma_expression ')' statement ELSE statement */
[51b7345]304
[c11e31c]305%nonassoc THEN  // rule precedence for IF '(' comma_expression ')' statement
306%nonassoc ELSE  // token precedence for start of else clause in IF statement
[51b7345]307
[b87a5ed]308%start translation_unit                                                                 // parse-tree root
[51b7345]309
310%%
[c11e31c]311//************************* Namespace Management ********************************
312
[de62360d]313// The grammar in the ANSI C standard is not strictly context-free, since it relies upon the distinct terminal symbols
314// "identifier" and "TYPEDEFname" that are lexically identical.  While it is possible to write a purely context-free
315// grammar, such a grammar would obscure the relationship between syntactic and semantic constructs.  Hence, this
316// grammar uses the ANSI style.
[c11e31c]317//
[de62360d]318// Cforall compounds this problem by introducing type names local to the scope of a declaration (for instance, those
319// introduced through "forall" qualifiers), and by introducing "type generators" -- parametrized types.  This latter
320// type name creates a third class of identifiers that must be distinguished by the scanner.
[c11e31c]321//
[de62360d]322// Since the scanner cannot distinguish among the different classes of identifiers without some context information, it
323// accesses a data structure (the TypedefTable) to allow classification of an identifier that it has just read.
324// Semantic actions during the parser update this data structure when the class of identifiers change.
[c11e31c]325//
[de62360d]326// Because the Cforall language is block-scoped, there is the possibility that an identifier can change its class in a
327// local scope; it must revert to its original class at the end of the block.  Since type names can be local to a
328// particular declaration, each declaration is itself a scope.  This requires distinguishing between type names that are
329// local to the current declaration scope and those that persist past the end of the declaration (i.e., names defined in
330// "typedef" or "type" declarations).
[c11e31c]331//
[de62360d]332// The non-terminals "push" and "pop" derive the empty string; their only use is to denote the opening and closing of
333// scopes.  Every push must have a matching pop, although it is regrettable the matching pairs do not always occur
334// within the same rule.  These non-terminals may appear in more contexts than strictly necessary from a semantic point
335// of view.  Unfortunately, these extra rules are necessary to prevent parsing conflicts -- the parser may not have
336// enough context and look-ahead information to decide whether a new scope is necessary, so the effect of these extra
337// rules is to open a new scope unconditionally.  As the grammar evolves, it may be neccesary to add or move around
338// "push" and "pop" nonterminals to resolve conflicts of this sort.
[51b7345]339
340push:
[ab57786]341                { typedefTable.enterScope(); }
[4d51835]342        ;
[51b7345]343
344pop:
[ab57786]345                { typedefTable.leaveScope(); }
[4d51835]346        ;
[51b7345]347
[c11e31c]348//************************* CONSTANTS ********************************
[51b7345]349
350constant:
[de62360d]351                // ENUMERATIONconstant is not included here; it is treated as a variable with type "enumeration constant".
[ab57786]352        INTEGERconstant                                                         { $$ = new ExpressionNode( build_constantInteger( *$1 ) ); }
353        | FLOATINGconstant                                                      { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
354        | CHARACTERconstant                                                     { $$ = new ExpressionNode( build_constantChar( *$1 ) ); }
[4d51835]355        ;
[51b7345]356
357identifier:
[4d51835]358        IDENTIFIER
359        | ATTR_IDENTIFIER                                                                       // CFA
360        | zero_one                                                                                      // CFA
361        ;
[51b7345]362
363no_01_identifier:
[4d51835]364        IDENTIFIER
365        | ATTR_IDENTIFIER                                                                       // CFA
366        ;
[51b7345]367
368no_attr_identifier:
[4d51835]369        IDENTIFIER
[bd85400]370        | zero_one                                                                                      // CFA
[4d51835]371        ;
[51b7345]372
[b87a5ed]373zero_one:                                                                                               // CFA
[4d51835]374        ZERO
375        | ONE
376        ;
[51b7345]377
[ab57786]378string_literal:
379        string_literal_list                                                     { $$ = build_constantStr( *$1 ); }
380        ;
381
[b87a5ed]382string_literal_list:                                                                    // juxtaposed strings are concatenated
[ab57786]383        STRINGliteral                                                           { $$ = $1; } // conversion from tok to str
[7bf7fb9]384        | string_literal_list STRINGliteral
385                {
[ab57786]386                        appendStr( $1, $2 );                                            // append 2nd juxtaposed string to 1st
[7bf7fb9]387                        delete $2;                                                                      // allocated by lexer
[ab57786]388                        $$ = $1;                                                                        // conversion from tok to str
[7bf7fb9]389                }
[4d51835]390        ;
[51b7345]391
[c11e31c]392//************************* EXPRESSIONS ********************************
[51b7345]393
394primary_expression:
[4d51835]395        IDENTIFIER                                                                                      // typedef name cannot be used as a variable name
[d1625f8]396                { $$ = new ExpressionNode( build_varref( $1 ) ); }
[4d51835]397        | zero_one
[d1625f8]398                { $$ = new ExpressionNode( build_varref( $1 ) ); }
[4d51835]399        | '(' comma_expression ')'
400                { $$ = $2; }
401        | '(' compound_statement ')'                                            // GCC, lambda expression
[d1625f8]402        { $$ = new ExpressionNode( build_valexpr( $2 ) ); }
[4d51835]403        ;
[51b7345]404
405postfix_expression:
[4d51835]406        primary_expression
407        | postfix_expression '[' push assignment_expression pop ']'
[c6b1105]408                // CFA, comma_expression disallowed in this context because it results in a common user error: subscripting a
[de62360d]409                // matrix with x[i,j] instead of x[i][j]. While this change is not backwards compatible, there seems to be
410                // little advantage to this feature and many disadvantages. It is possible to write x[(i,j)] in CFA, which is
411                // equivalent to the old x[i,j].
[d1625f8]412                { $$ = new ExpressionNode( build_binary_val( OperKinds::Index, $1, $4 ) ); }
[4d51835]413        | postfix_expression '(' argument_expression_list ')'
[d1625f8]414                { $$ = new ExpressionNode( build_func( $1, $3 ) ); }
[ab57786]415                // ambiguity with .0 so space required after field-selection, e.g.
[bd85400]416                //   struct S { int 0, 1; } s; s. 0 = 0; s. 1 = 1;
[4d51835]417        | postfix_expression '.' no_attr_identifier
[d1625f8]418                { $$ = new ExpressionNode( build_fieldSel( $1, build_varref( $3 ) ) ); }
[4d51835]419        | postfix_expression '.' '[' push field_list pop ']' // CFA, tuple field selector
420        | postfix_expression ARROW no_attr_identifier
[d1625f8]421                { $$ = new ExpressionNode( build_pfieldSel( $1, build_varref( $3 ) ) ); }
[4d51835]422        | postfix_expression ARROW '[' push field_list pop ']' // CFA, tuple field selector
423        | postfix_expression ICR
[d1625f8]424                { $$ = new ExpressionNode( build_unary_ptr( OperKinds::IncrPost, $1 ) ); }
[4d51835]425        | postfix_expression DECR
[d1625f8]426                { $$ = new ExpressionNode( build_unary_ptr( OperKinds::DecrPost, $1 ) ); }
[4d51835]427        | '(' type_name_no_function ')' '{' initializer_list comma_opt '}' // C99
[d1625f8]428                { $$ = new ExpressionNode( build_compoundLiteral( $2, new InitializerNode( $5, true ) ) ); }
[097e2b0]429        | postfix_expression '{' argument_expression_list '}' // CFA
430                {
[9706554]431                        Token fn;
432                        fn.str = new std::string( "?{}" ); // location undefined
[1d4580a]433                        $$ = new ExpressionNode( build_func( new ExpressionNode( build_varref( fn ) ), (ExpressionNode *)( $1 )->set_last( $3 ) ) );
[097e2b0]434                }
[4d51835]435        ;
[51b7345]436
437argument_expression_list:
[4d51835]438        argument_expression
439        | argument_expression_list ',' argument_expression
[1d4580a]440                { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
[4d51835]441        ;
[51b7345]442
443argument_expression:
[4d51835]444        // empty
445                { $$ = 0; }                                                                             // use default argument
446        | assignment_expression
447        ;
[b87a5ed]448
449field_list:                                                                                             // CFA, tuple field selector
[4d51835]450        field
[1d4580a]451        | field_list ',' field                                          { $$ = (ExpressionNode *)$1->set_last( $3 ); }
[4d51835]452        ;
[b87a5ed]453
454field:                                                                                                  // CFA, tuple field selector
[4d51835]455        no_attr_identifier
[d1625f8]456                { $$ = new ExpressionNode( build_varref( $1 ) ); }
[ab57786]457                // ambiguity with .0 so space required after field-selection, e.g.
[bd85400]458                //   struct S { int 0, 1; } s; s. 0 = 0; s. 1 = 1;
[4d51835]459        | no_attr_identifier '.' field
[d1625f8]460                { $$ = new ExpressionNode( build_fieldSel( $3, build_varref( $1 ) ) ); }
[4d51835]461        | no_attr_identifier '.' '[' push field_list pop ']'
[d1625f8]462                { $$ = new ExpressionNode( build_fieldSel( $5, build_varref( $1 ) ) ); }
[4d51835]463        | no_attr_identifier ARROW field
[d1625f8]464                { $$ = new ExpressionNode( build_pfieldSel( $3, build_varref( $1 ) ) ); }
[4d51835]465        | no_attr_identifier ARROW '[' push field_list pop ']'
[d1625f8]466                { $$ = new ExpressionNode( build_pfieldSel( $5, build_varref( $1 ) ) ); }
[4d51835]467        ;
[51b7345]468
469unary_expression:
[4d51835]470        postfix_expression
[c6b1105]471                // first location where constant/string can have operator applied: sizeof 3/sizeof "abc" still requires
472                // semantics checks, e.g., ++3, 3--, *3, &&3
[51b1202]473        | constant
474                { $$ = $1; }
[ab57786]475        | string_literal
[d1625f8]476                { $$ = new ExpressionNode( $1 ); }
[4d51835]477        | EXTENSION cast_expression                                                     // GCC
[e04ef3a]478                { $$ = $2->set_extension( true ); }
[c6b1105]479                // '*' ('&') is separated from unary_operator because of shift/reduce conflict in:
480                //              { * X; }         // dereference X
481                //              { * int X; } // CFA declaration of pointer to int
[51e076e]482        | ptrref_operator cast_expression                                       // CFA
[9706554]483                {
484                        switch ( $1 ) {
[d9e2280]485                          case OperKinds::AddressOf:
[d1625f8]486                                $$ = new ExpressionNode( build_addressOf( $2 ) );
[9706554]487                                break;
[d9e2280]488                          case OperKinds::PointTo:
[d1625f8]489                                $$ = new ExpressionNode( build_unary_val( $1, $2 ) );
[9706554]490                                break;
491                          default:
492                                assert( false );
493                        }
494                }
[4d51835]495        | unary_operator cast_expression
[d1625f8]496                { $$ = new ExpressionNode( build_unary_val( $1, $2 ) ); }
[dd51906]497        | ICR unary_expression
[d1625f8]498                { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Incr, $2 ) ); }
[dd51906]499        | DECR unary_expression
[d1625f8]500                { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Decr, $2 ) ); }
[4d51835]501        | SIZEOF unary_expression
[d1625f8]502                { $$ = new ExpressionNode( build_sizeOfexpr( $2 ) ); }
[4d51835]503        | SIZEOF '(' type_name_no_function ')'
[d1625f8]504                { $$ = new ExpressionNode( build_sizeOftype( $3 ) ); }
505        | ALIGNOF unary_expression                                                      // GCC, variable alignment
506                { $$ = new ExpressionNode( build_alignOfexpr( $2 ) ); }
507        | ALIGNOF '(' type_name_no_function ')'                         // GCC, type alignment
508                { $$ = new ExpressionNode( build_alignOftype( $3 ) ); }
[5721a6d]509        | OFFSETOF '(' type_name_no_function ',' no_attr_identifier ')'
[d1625f8]510                { $$ = new ExpressionNode( build_offsetOf( $3, build_varref( $5 ) ) ); }
[4d51835]511        | ATTR_IDENTIFIER
[d1625f8]512                { $$ = new ExpressionNode( build_attrexpr( build_varref( $1 ), nullptr ) ); }
[4d51835]513        | ATTR_IDENTIFIER '(' argument_expression ')'
[d1625f8]514                { $$ = new ExpressionNode( build_attrexpr( build_varref( $1 ), $3 ) ); }
515        | ATTR_IDENTIFIER '(' type_name ')'
516                { $$ = new ExpressionNode( build_attrtype( build_varref( $1 ), $3 ) ); }
[c6b1105]517//      | ANDAND IDENTIFIER                                                                     // GCC, address of label
[d1625f8]518//              { $$ = new ExpressionNode( new OperatorNode( OperKinds::LabelAddress ), new ExpressionNode( build_varref( $2, true ) ); }
[4d51835]519        ;
[51b7345]520
[dd51906]521ptrref_operator:
[d9e2280]522        '*'                                                                                     { $$ = OperKinds::PointTo; }
523        | '&'                                                                           { $$ = OperKinds::AddressOf; }
[c6b1105]524                // GCC, address of label must be handled by semantic check for ref,ref,label
[d9e2280]525//      | ANDAND                                                                        { $$ = OperKinds::And; }
[dd51906]526        ;
527
[51b7345]528unary_operator:
[d9e2280]529        '+'                                                                                     { $$ = OperKinds::UnPlus; }
530        | '-'                                                                           { $$ = OperKinds::UnMinus; }
531        | '!'                                                                           { $$ = OperKinds::Neg; }
532        | '~'                                                                           { $$ = OperKinds::BitNeg; }
[4d51835]533        ;
[51b7345]534
535cast_expression:
[4d51835]536        unary_expression
537        | '(' type_name_no_function ')' cast_expression
[d1625f8]538                { $$ = new ExpressionNode( build_cast( $2, $4 ) ); }
[4d51835]539        | '(' type_name_no_function ')' tuple
[d1625f8]540                { $$ = new ExpressionNode( build_cast( $2, $4 ) ); }
[4d51835]541        ;
[51b7345]542
543multiplicative_expression:
[4d51835]544        cast_expression
545        | multiplicative_expression '*' cast_expression
[d1625f8]546                { $$ = new ExpressionNode( build_binary_val( OperKinds::Mul, $1, $3 ) ); }
[4d51835]547        | multiplicative_expression '/' cast_expression
[d1625f8]548                { $$ = new ExpressionNode( build_binary_val( OperKinds::Div, $1, $3 ) ); }
[4d51835]549        | multiplicative_expression '%' cast_expression
[d1625f8]550                { $$ = new ExpressionNode( build_binary_val( OperKinds::Mod, $1, $3 ) ); }
[4d51835]551        ;
[51b7345]552
553additive_expression:
[4d51835]554        multiplicative_expression
555        | additive_expression '+' multiplicative_expression
[d1625f8]556                { $$ = new ExpressionNode( build_binary_val( OperKinds::Plus, $1, $3 ) ); }
[4d51835]557        | additive_expression '-' multiplicative_expression
[d1625f8]558                { $$ = new ExpressionNode( build_binary_val( OperKinds::Minus, $1, $3 ) ); }
[4d51835]559        ;
[51b7345]560
561shift_expression:
[4d51835]562        additive_expression
563        | shift_expression LS additive_expression
[d1625f8]564                { $$ = new ExpressionNode( build_binary_val( OperKinds::LShift, $1, $3 ) ); }
[4d51835]565        | shift_expression RS additive_expression
[d1625f8]566                { $$ = new ExpressionNode( build_binary_val( OperKinds::RShift, $1, $3 ) ); }
[4d51835]567        ;
[51b7345]568
569relational_expression:
[4d51835]570        shift_expression
571        | relational_expression '<' shift_expression
[d1625f8]572                { $$ = new ExpressionNode( build_binary_val( OperKinds::LThan, $1, $3 ) ); }
[4d51835]573        | relational_expression '>' shift_expression
[d1625f8]574                { $$ = new ExpressionNode( build_binary_val( OperKinds::GThan, $1, $3 ) ); }
[4d51835]575        | relational_expression LE shift_expression
[d1625f8]576                { $$ = new ExpressionNode( build_binary_val( OperKinds::LEThan, $1, $3 ) ); }
[4d51835]577        | relational_expression GE shift_expression
[d1625f8]578                { $$ = new ExpressionNode( build_binary_val( OperKinds::GEThan, $1, $3 ) ); }
[4d51835]579        ;
[51b7345]580
581equality_expression:
[4d51835]582        relational_expression
583        | equality_expression EQ relational_expression
[d1625f8]584                { $$ = new ExpressionNode( build_binary_val( OperKinds::Eq, $1, $3 ) ); }
[4d51835]585        | equality_expression NE relational_expression
[d1625f8]586                { $$ = new ExpressionNode( build_binary_val( OperKinds::Neq, $1, $3 ) ); }
[4d51835]587        ;
[51b7345]588
589AND_expression:
[4d51835]590        equality_expression
591        | AND_expression '&' equality_expression
[d1625f8]592                { $$ = new ExpressionNode( build_binary_val( OperKinds::BitAnd, $1, $3 ) ); }
[4d51835]593        ;
[51b7345]594
595exclusive_OR_expression:
[4d51835]596        AND_expression
597        | exclusive_OR_expression '^' AND_expression
[d1625f8]598                { $$ = new ExpressionNode( build_binary_val( OperKinds::Xor, $1, $3 ) ); }
[4d51835]599        ;
[51b7345]600
601inclusive_OR_expression:
[4d51835]602        exclusive_OR_expression
603        | inclusive_OR_expression '|' exclusive_OR_expression
[d1625f8]604                { $$ = new ExpressionNode( build_binary_val( OperKinds::BitOr, $1, $3 ) ); }
[4d51835]605        ;
[51b7345]606
607logical_AND_expression:
[4d51835]608        inclusive_OR_expression
609        | logical_AND_expression ANDAND inclusive_OR_expression
[d1625f8]610                { $$ = new ExpressionNode( build_and_or( $1, $3, true ) ); }
[4d51835]611        ;
[51b7345]612
613logical_OR_expression:
[4d51835]614        logical_AND_expression
615        | logical_OR_expression OROR logical_AND_expression
[d1625f8]616                { $$ = new ExpressionNode( build_and_or( $1, $3, false ) ); }
[4d51835]617        ;
[51b7345]618
619conditional_expression:
[4d51835]620        logical_OR_expression
621        | logical_OR_expression '?' comma_expression ':' conditional_expression
[d1625f8]622                { $$ = new ExpressionNode( build_cond( $1, $3, $5 ) ); }
[d9e2280]623                // FIX ME: this hack computes $1 twice
[4d51835]624        | logical_OR_expression '?' /* empty */ ':' conditional_expression // GCC, omitted first operand
[d1625f8]625                { $$ = new ExpressionNode( build_cond( $1, $1, $4 ) ); }
[4d51835]626        | logical_OR_expression '?' comma_expression ':' tuple // CFA, tuple expression
[d1625f8]627                { $$ = new ExpressionNode( build_cond( $1, $3, $5 ) ); }
[4d51835]628        ;
[51b7345]629
630constant_expression:
[4d51835]631        conditional_expression
632        ;
[51b7345]633
634assignment_expression:
[4d51835]635                // CFA, assignment is separated from assignment_operator to ensure no assignment operations for tuples
636        conditional_expression
637        | unary_expression assignment_operator assignment_expression
[d1625f8]638                { $$ = new ExpressionNode( build_binary_ptr( $2, $1, $3 ) ); }
[4d51835]639        | tuple assignment_opt                                                          // CFA, tuple expression
[d1625f8]640                { $$ = ( $2 == 0 ) ? $1 : new ExpressionNode( build_binary_ptr( OperKinds::Assign, $1, $2 ) ); }
[4d51835]641        ;
[51b7345]642
643assignment_expression_opt:
[4d51835]644        // empty
[d1625f8]645                { $$ = nullptr; }
[4d51835]646        | assignment_expression
647        ;
[b87a5ed]648
[9706554]649assignment_operator:
[d9e2280]650        '='                                                                                     { $$ = OperKinds::Assign; }
651        | MULTassign                                                            { $$ = OperKinds::MulAssn; }
652        | DIVassign                                                                     { $$ = OperKinds::DivAssn; }
653        | MODassign                                                                     { $$ = OperKinds::ModAssn; }
654        | PLUSassign                                                            { $$ = OperKinds::PlusAssn; }
655        | MINUSassign                                                           { $$ = OperKinds::MinusAssn; }
656        | LSassign                                                                      { $$ = OperKinds::LSAssn; }
657        | RSassign                                                                      { $$ = OperKinds::RSAssn; }
658        | ANDassign                                                                     { $$ = OperKinds::AndAssn; }
659        | ERassign                                                                      { $$ = OperKinds::ERAssn; }
660        | ORassign                                                                      { $$ = OperKinds::OrAssn; }
[9706554]661        ;
662
[b87a5ed]663tuple:                                                                                                  // CFA, tuple
[de62360d]664                // CFA, one assignment_expression is factored out of comma_expression to eliminate a shift/reduce conflict with
665                // comma_expression in new_identifier_parameter_array and new_abstract_array
[2871210]666        '[' ']'
[d1625f8]667                { $$ = new ExpressionNode( build_tuple() ); }
[4d51835]668        | '[' push assignment_expression pop ']'
[d1625f8]669                { $$ = new ExpressionNode( build_tuple( $3 ) ); }
[4d51835]670        | '[' push ',' tuple_expression_list pop ']'
[1d4580a]671                { $$ = new ExpressionNode( build_tuple( (ExpressionNode *)(new ExpressionNode( nullptr ) )->set_last( $4 ) ) ); }
[4d51835]672        | '[' push assignment_expression ',' tuple_expression_list pop ']'
[1d4580a]673                { $$ = new ExpressionNode( build_tuple( (ExpressionNode *)$3->set_last( $5 ) ) ); }
[4d51835]674        ;
[51b7345]675
676tuple_expression_list:
[4d51835]677        assignment_expression_opt
678        | tuple_expression_list ',' assignment_expression_opt
[1d4580a]679                { $$ = (ExpressionNode *)$1->set_last( $3 ); }
[4d51835]680        ;
[51b7345]681
682comma_expression:
[4d51835]683        assignment_expression
[9706554]684        | comma_expression ',' assignment_expression
[d1625f8]685                { $$ = new ExpressionNode( build_comma( $1, $3 ) ); }
[4d51835]686        ;
[51b7345]687
688comma_expression_opt:
[4d51835]689        // empty
690                { $$ = 0; }
691        | comma_expression
692        ;
[51b7345]693
[c11e31c]694//*************************** STATEMENTS *******************************
[51b7345]695
696statement:
[4d51835]697        labeled_statement
698        | compound_statement
699        | expression_statement                                          { $$ = $1; }
700        | selection_statement
701        | iteration_statement
702        | jump_statement
703        | exception_statement
704        | asm_statement
[097e2b0]705        | '^' postfix_expression '{' argument_expression_list '}' ';' // CFA
706                {
[d9e2280]707                        Token fn;
708                        fn.str = new std::string( "^?{}" ); // location undefined
[e82aa9df]709                        $$ = new StatementNode( build_expr( new ExpressionNode( build_func( new ExpressionNode( build_varref( fn ) ), (ExpressionNode *)( $2 )->set_last( $4 ) ) ) ) );
[097e2b0]710                }
[4d51835]711        ;
[51b7345]712
713labeled_statement:
[c6b1105]714                // labels cannot be identifiers 0 or 1
715        IDENTIFIER ':' attribute_list_opt statement
[1db21619]716                {
717                        $$ = $4->add_label( $1 );
718                }
[4d51835]719        ;
[51b7345]720
721compound_statement:
[4d51835]722        '{' '}'
[e82aa9df]723                { $$ = new StatementNode( build_compound( (StatementNode *)0 ) ); }
[4d51835]724        | '{'
[de62360d]725                // Two scopes are necessary because the block itself has a scope, but every declaration within the block also
726                // requires its own scope
[4d51835]727          push push
[51b1202]728          local_label_declaration_opt                                           // GCC, local labels
[4d51835]729          block_item_list pop '}'                                                       // C99, intermix declarations and statements
[e82aa9df]730                { $$ = new StatementNode( build_compound( $5 ) ); }
[4d51835]731        ;
[b87a5ed]732
733block_item_list:                                                                                // C99
[4d51835]734        block_item
735        | block_item_list push block_item
[1d4580a]736                { if ( $1 != 0 ) { $1->set_last( $3 ); $$ = $1; } }
[4d51835]737        ;
[51b7345]738
739block_item:
[4d51835]740        declaration                                                                                     // CFA, new & old style declarations
[e82aa9df]741                { $$ = new StatementNode( $1 ); }
[4d51835]742        | EXTENSION declaration                                                         // GCC
[8e9cbb2]743                {       // mark all fields in list
[926af74]744                        for ( DeclarationNode *iter = $2; iter != nullptr; iter = (DeclarationNode *)iter->get_next() )
[8e9cbb2]745                                iter->set_extension( true );
[e82aa9df]746                        $$ = new StatementNode( $2 );
[8e9cbb2]747                }
[4d51835]748        | function_definition
[e82aa9df]749                { $$ = new StatementNode( $1 ); }
[4d51835]750        | statement pop
751        ;
[51b7345]752
753statement_list:
[4d51835]754        statement
755        | statement_list statement
[1d4580a]756                { if ( $1 != 0 ) { $1->set_last( $2 ); $$ = $1; } }
[4d51835]757        ;
[51b7345]758
759expression_statement:
[4d51835]760        comma_expression_opt ';'
[e82aa9df]761                { $$ = new StatementNode( build_expr( $1 ) ); }
[4d51835]762        ;
[51b7345]763
764selection_statement:
[4d51835]765        IF '(' comma_expression ')' statement                           %prec THEN
766                // explicitly deal with the shift/reduce conflict on if/else
[e82aa9df]767                { $$ = new StatementNode( build_if( $3, $5, nullptr ) ); }
[4d51835]768        | IF '(' comma_expression ')' statement ELSE statement
[e82aa9df]769                { $$ = new StatementNode( build_if( $3, $5, $7 ) ); }
[4d51835]770        | SWITCH '(' comma_expression ')' case_clause           // CFA
[e82aa9df]771                { $$ = new StatementNode( build_switch( $3, $5 ) ); }
[4d51835]772        | SWITCH '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt '}' // CFA
[4e06c1e]773                {
[e82aa9df]774                        StatementNode *sw = new StatementNode( build_switch( $3, $8 ) );
[4e06c1e]775                        // The semantics of the declaration list is changed to include associated initialization, which is performed
776                        // *before* the transfer to the appropriate case clause by hoisting the declarations into a compound
777                        // statement around the switch.  Statements after the initial declaration list can never be executed, and
[8688ce1]778                        // therefore, are removed from the grammar even though C allows it. The change also applies to choose
779                        // statement.
[e82aa9df]780                        $$ = $7 != 0 ? new StatementNode( build_compound( (StatementNode *)((new StatementNode( $7 ))->set_last( sw )) ) ) : sw;
[4e06c1e]781                }
[4d51835]782        | CHOOSE '(' comma_expression ')' case_clause           // CFA
[e82aa9df]783                { $$ = new StatementNode( build_switch( $3, $5 ) ); }
[4d51835]784        | CHOOSE '(' comma_expression ')' '{' push declaration_list_opt choose_clause_list_opt '}' // CFA
[4e06c1e]785                {
[e82aa9df]786                        StatementNode *sw = new StatementNode( build_switch( $3, $8 ) );
787                        $$ = $7 != 0 ? new StatementNode( build_compound( (StatementNode *)((new StatementNode( $7 ))->set_last( sw )) ) ) : sw;
[4e06c1e]788                }
[4d51835]789        ;
[b87a5ed]790
[de62360d]791// CASE and DEFAULT clauses are only allowed in the SWITCH statement, precluding Duff's device. In addition, a case
792// clause allows a list of values and subranges.
[b87a5ed]793
794case_value:                                                                                             // CFA
[4d51835]795        constant_expression                                                     { $$ = $1; }
796        | constant_expression ELLIPSIS constant_expression      // GCC, subrange
[d1625f8]797                { $$ = new ExpressionNode( build_range( $1, $3 ) ); }
[4d51835]798        | subrange                                                                                      // CFA, subrange
799        ;
[b87a5ed]800
801case_value_list:                                                                                // CFA
[e82aa9df]802        case_value                                                                      { $$ = new StatementNode( build_case( $1 ) ); }
[064e3ff]803                // convert case list, e.g., "case 1, 3, 5:" into "case 1: case 3: case 5"
[e82aa9df]804        | case_value_list ',' case_value                        { $$ = (StatementNode *)($1->set_last( new StatementNode( build_case( $3 ) ) ) ); }
[4d51835]805        ;
[b87a5ed]806
807case_label:                                                                                             // CFA
[8688ce1]808        CASE case_value_list ':'                                        { $$ = $2; }
[e82aa9df]809        | DEFAULT ':'                                                           { $$ = new StatementNode( build_default() ); }
[4d51835]810                // A semantic check is required to ensure only one default clause per switch/choose statement.
811        ;
[b87a5ed]812
813case_label_list:                                                                                // CFA
[4d51835]814        case_label
[1d4580a]815        | case_label_list case_label                            { $$ = (StatementNode *)( $1->set_last( $2 )); }
[4d51835]816        ;
[b87a5ed]817
818case_clause:                                                                                    // CFA
[e82aa9df]819        case_label_list statement                                       { $$ = $1->append_last_case( new StatementNode( build_compound( $2 ) ) ); }
[4d51835]820        ;
[b87a5ed]821
822switch_clause_list_opt:                                                                 // CFA
[4d51835]823        // empty
824                { $$ = 0; }
825        | switch_clause_list
826        ;
[b87a5ed]827
828switch_clause_list:                                                                             // CFA
[4d51835]829        case_label_list statement_list
[e82aa9df]830                { $$ = $1->append_last_case( new StatementNode( build_compound( $2 ) ) ); }
[4d51835]831        | switch_clause_list case_label_list statement_list
[e82aa9df]832                { $$ = (StatementNode *)( $1->set_last( $2->append_last_case( new StatementNode( build_compound( $3 ) ) ) ) ); }
[4d51835]833        ;
[b87a5ed]834
835choose_clause_list_opt:                                                                 // CFA
[4d51835]836        // empty
837                { $$ = 0; }
838        | choose_clause_list
839        ;
[b87a5ed]840
841choose_clause_list:                                                                             // CFA
[4d51835]842        case_label_list fall_through
[de62360d]843                { $$ = $1->append_last_case( $2 ); }
[4d51835]844        | case_label_list statement_list fall_through_opt
[e82aa9df]845                { $$ = $1->append_last_case( new StatementNode( build_compound( (StatementNode *)$2->set_last( $3 ) ) ) ); }
[4d51835]846        | choose_clause_list case_label_list fall_through
[1d4580a]847                { $$ = (StatementNode *)( $1->set_last( $2->append_last_case( $3 ))); }
[4d51835]848        | choose_clause_list case_label_list statement_list fall_through_opt
[e82aa9df]849                { $$ = (StatementNode *)( $1->set_last( $2->append_last_case( new StatementNode( build_compound( (StatementNode *)$3->set_last( $4 ) ) ) ) ) ); }
[4d51835]850        ;
[b87a5ed]851
852fall_through_opt:                                                                               // CFA
[4d51835]853        // empty
[ab57786]854                { $$ = new StatementNode( build_branch( BranchStmt::Break ) ); } // insert implicit break
[4d51835]855        | fall_through
856        ;
[b87a5ed]857
858fall_through:                                                                                   // CFA
[4e06c1e]859        FALLTHRU
860                { $$ = 0; }
861        | FALLTHRU ';'
862                { $$ = 0; }
[4d51835]863        ;
[51b7345]864
865iteration_statement:
[4d51835]866        WHILE '(' comma_expression ')' statement
[e82aa9df]867                { $$ = new StatementNode( build_while( $3, $5 ) ); }
[4d51835]868        | DO statement WHILE '(' comma_expression ')' ';'
[e82aa9df]869                { $$ = new StatementNode( build_while( $5, $2 ) ); }
[4d51835]870        | FOR '(' push for_control_expression ')' statement
[e82aa9df]871                { $$ = new StatementNode( build_for( $4, $6 ) ); }
[4d51835]872        ;
[51b7345]873
874for_control_expression:
[4d51835]875        comma_expression_opt pop ';' comma_expression_opt ';' comma_expression_opt
[2f22cc4]876                { $$ = new ForCtl( $1, $4, $6 ); }
[4d51835]877        | declaration comma_expression_opt ';' comma_expression_opt // C99
[2f22cc4]878                { $$ = new ForCtl( $1, $2, $4 ); }
[d1625f8]879        ;
[51b7345]880
881jump_statement:
[c6b1105]882        GOTO IDENTIFIER ';'
[ab57786]883                { $$ = new StatementNode( build_branch( $2, BranchStmt::Goto ) ); }
[4d51835]884        | GOTO '*' comma_expression ';'                                         // GCC, computed goto
[4e06c1e]885                // The syntax for the GCC computed goto violates normal expression precedence, e.g., goto *i+3; => goto *(i+3);
[de62360d]886                // whereas normal operator precedence yields goto (*i)+3;
[e82aa9df]887                { $$ = new StatementNode( build_computedgoto( $3 ) ); }
[4d51835]888        | CONTINUE ';'
[de62360d]889                // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
[ab57786]890                { $$ = new StatementNode( build_branch( BranchStmt::Continue ) ); }
[4e06c1e]891        | CONTINUE IDENTIFIER ';'                                                       // CFA, multi-level continue
[de62360d]892                // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
893                // the target of the transfer appears only at the start of an iteration statement.
[ab57786]894                { $$ = new StatementNode( build_branch( $2, BranchStmt::Continue ) ); }
[4d51835]895        | BREAK ';'
[de62360d]896                // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
[ab57786]897                { $$ = new StatementNode( build_branch( BranchStmt::Break ) ); }
[4e06c1e]898        | BREAK IDENTIFIER ';'                                                          // CFA, multi-level exit
[de62360d]899                // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
900                // the target of the transfer appears only at the start of an iteration statement.
[ab57786]901                { $$ = new StatementNode( build_branch( $2, BranchStmt::Break ) ); }
[4d51835]902        | RETURN comma_expression_opt ';'
[e82aa9df]903                { $$ = new StatementNode( build_return( $2 ) ); }
[8cc5cb0]904        | THROW assignment_expression_opt ';'                           // handles rethrow
[e82aa9df]905                { $$ = new StatementNode( build_throw( $2 ) ); }
[8cc5cb0]906        | THROWRESUME assignment_expression_opt ';'                     // handles reresume
[e82aa9df]907                { $$ = new StatementNode( build_throw( $2 ) ); }
[8cc5cb0]908        | THROWRESUME assignment_expression_opt AT assignment_expression ';' // handles reresume
[e82aa9df]909                { $$ = new StatementNode( build_throw( $2 ) ); }
[4d51835]910        ;
[51b7345]911
912exception_statement:
[4d51835]913        TRY compound_statement handler_list
[e82aa9df]914                { $$ = new StatementNode( build_try( $2, $3, 0 ) ); }
[4d51835]915        | TRY compound_statement finally_clause
[e82aa9df]916                { $$ = new StatementNode( build_try( $2, 0, $3 ) ); }
[4d51835]917        | TRY compound_statement handler_list finally_clause
[e82aa9df]918                { $$ = new StatementNode( build_try( $2, $3, $4 ) ); }
[4d51835]919        ;
[51b7345]920
921handler_list:
[4d51835]922        handler_clause
[de62360d]923                // ISO/IEC 9899:1999 Section 15.3(6 ) If present, a "..." handler shall be the last handler for its try block.
[4d51835]924        | CATCH '(' ELLIPSIS ')' compound_statement
[e82aa9df]925                { $$ = new StatementNode( build_catch( 0, $5, true ) ); }
[4d51835]926        | handler_clause CATCH '(' ELLIPSIS ')' compound_statement
[e82aa9df]927                { $$ = (StatementNode *)$1->set_last( new StatementNode( build_catch( 0, $6, true ) ) ); }
[02e5ab6]928        | CATCHRESUME '(' ELLIPSIS ')' compound_statement
[e82aa9df]929                { $$ = new StatementNode( build_catch( 0, $5, true ) ); }
[02e5ab6]930        | handler_clause CATCHRESUME '(' ELLIPSIS ')' compound_statement
[e82aa9df]931                { $$ = (StatementNode *)$1->set_last( new StatementNode( build_catch( 0, $6, true ) ) ); }
[4d51835]932        ;
[51b7345]933
934handler_clause:
[4d51835]935        CATCH '(' push push exception_declaration pop ')' compound_statement pop
[e82aa9df]936                { $$ = new StatementNode( build_catch( $5, $8 ) ); }
[4d51835]937        | handler_clause CATCH '(' push push exception_declaration pop ')' compound_statement pop
[e82aa9df]938        { $$ = (StatementNode *)$1->set_last( new StatementNode( build_catch( $6, $9 ) ) ); }
[02e5ab6]939        | CATCHRESUME '(' push push exception_declaration pop ')' compound_statement pop
[e82aa9df]940                { $$ = new StatementNode( build_catch( $5, $8 ) ); }
[02e5ab6]941        | handler_clause CATCHRESUME '(' push push exception_declaration pop ')' compound_statement pop
[e82aa9df]942                { $$ = (StatementNode *)$1->set_last( new StatementNode( build_catch( $6, $9 ) ) ); }
[4d51835]943        ;
[51b7345]944
945finally_clause:
[4d51835]946        FINALLY compound_statement
947                {
[e82aa9df]948                        $$ = new StatementNode( build_finally( $2 ) );
[4d51835]949                }
950        ;
[51b7345]951
952exception_declaration:
[4d51835]953                // A semantic check is required to ensure type_specifier does not create a new type, e.g.:
954                //
955                //              catch ( struct { int i; } x ) ...
956                //
957                // This new type cannot catch any thrown type because of name equivalence among types.
958        type_specifier
959        | type_specifier declarator
960                {
961                        typedefTable.addToEnclosingScope( TypedefTable::ID );
962                        $$ = $2->addType( $1 );
963                }
964        | type_specifier variable_abstract_declarator
965                { $$ = $2->addType( $1 ); }
966        | new_abstract_declarator_tuple no_attr_identifier      // CFA
967                {
968                        typedefTable.addToEnclosingScope( TypedefTable::ID );
969                        $$ = $1->addName( $2 );
970                }
971        | new_abstract_declarator_tuple                                         // CFA
972        ;
[51b7345]973
974asm_statement:
[ab57786]975        ASM asm_volatile_opt '(' string_literal ')' ';'
[e82aa9df]976                { $$ = new StatementNode( build_asmstmt( $2, $4, 0 ) ); }
[ab57786]977        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ')' ';' // remaining GCC
[e82aa9df]978                { $$ = new StatementNode( build_asmstmt( $2, $4, $6 ) ); }
[ab57786]979        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ')' ';'
[e82aa9df]980                { $$ = new StatementNode( build_asmstmt( $2, $4, $6, $8 ) ); }
[ab57786]981        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ':' asm_clobbers_list_opt ')' ';'
[e82aa9df]982                { $$ = new StatementNode( build_asmstmt( $2, $4, $6, $8, $10 ) ); }
[ab57786]983        | ASM asm_volatile_opt GOTO '(' string_literal ':' ':' asm_operands_opt ':' asm_clobbers_list_opt ':' label_list ')' ';'
[e82aa9df]984                { $$ = new StatementNode( build_asmstmt( $2, $5, 0, $8, $10, $12 ) ); }
[7f5566b]985        ;
986
987asm_volatile_opt:                                                                               // GCC
988        // empty
989                { $$ = false; }
990        | VOLATILE
991                { $$ = true; }
[4d51835]992        ;
[b87a5ed]993
994asm_operands_opt:                                                                               // GCC
[4d51835]995        // empty
[7f5566b]996                { $$ = 0; }                                                                             // use default argument
[4d51835]997        | asm_operands_list
998        ;
[b87a5ed]999
1000asm_operands_list:                                                                              // GCC
[4d51835]1001        asm_operand
1002        | asm_operands_list ',' asm_operand
[1d4580a]1003                { $$ = (ExpressionNode *)$1->set_last( $3 ); }
[4d51835]1004        ;
[b87a5ed]1005
1006asm_operand:                                                                                    // GCC
[ab57786]1007        string_literal '(' constant_expression ')'
[e82aa9df]1008                { $$ = new ExpressionNode( build_asmexpr( 0, $1, $3 ) ); }
[ab57786]1009        | '[' constant_expression ']' string_literal '(' constant_expression ')'
[e82aa9df]1010        { $$ = new ExpressionNode( build_asmexpr( $2, $4, $6 ) ); }
[7f5566b]1011        ;
1012
[4e06c1e]1013asm_clobbers_list_opt:                                                                  // GCC
[7f5566b]1014        // empty
1015                { $$ = 0; }                                                                             // use default argument
[ab57786]1016        | string_literal
[d1625f8]1017                { $$ = new ExpressionNode( $1 ); }
[ab57786]1018        | asm_clobbers_list_opt ',' string_literal
[e82aa9df]1019                { $$ = (ExpressionNode *)$1->set_last( new ExpressionNode( $3 ) ); }
[4d51835]1020        ;
[b87a5ed]1021
[7f5566b]1022label_list:
1023        no_attr_identifier
[ab57786]1024                {
1025                        $$ = new LabelNode(); $$->labels.push_back( *$1 );
1026                        delete $1;                                                                      // allocated by lexer
1027                }
[7f5566b]1028        | label_list ',' no_attr_identifier
[ab57786]1029                {
1030                        $$ = $1; $1->labels.push_back( *$3 );
1031                        delete $3;                                                                      // allocated by lexer
1032                }
[4d51835]1033        ;
[51b7345]1034
[c11e31c]1035//******************************* DECLARATIONS *********************************
[51b7345]1036
[b87a5ed]1037declaration_list_opt:                                                                   // used at beginning of switch statement
[4d51835]1038        pop
1039                { $$ = 0; }
1040        | declaration_list
1041        ;
[51b7345]1042
1043declaration_list:
[4d51835]1044        declaration
1045        | declaration_list push declaration
1046                { $$ = $1->appendList( $3 ); }
1047        ;
[51b7345]1048
[b87a5ed]1049old_declaration_list_opt:                                                               // used to declare parameter types in K&R style functions
[4d51835]1050        pop
1051                { $$ = 0; }
1052        | old_declaration_list
1053        ;
[51b7345]1054
1055old_declaration_list:
[4d51835]1056        old_declaration
1057        | old_declaration_list push old_declaration
1058                { $$ = $1->appendList( $3 ); }
1059        ;
[b87a5ed]1060
[51b1202]1061local_label_declaration_opt:                                                    // GCC, local label
[4d51835]1062        // empty
[51b1202]1063        | local_label_declaration_list
[4d51835]1064        ;
[b87a5ed]1065
[51b1202]1066local_label_declaration_list:                                                   // GCC, local label
1067        LABEL local_label_list ';'
1068        | local_label_declaration_list LABEL local_label_list ';'
[4d51835]1069        ;
[b87a5ed]1070
[51b1202]1071local_label_list:                                                                               // GCC, local label
[7f5566b]1072        no_attr_identifier_or_type_name                         {}
[51b1202]1073        | local_label_list ',' no_attr_identifier_or_type_name {}
[4d51835]1074        ;
[b87a5ed]1075
1076declaration:                                                                                    // CFA, new & old style declarations
[4d51835]1077        new_declaration
1078        | old_declaration
1079        ;
[b87a5ed]1080
[de62360d]1081// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
1082// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
1083// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
1084// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
1085// the declaration list (as in Pascal).  ANSI C and the new CFA declarations may appear together in the same program
1086// block, but cannot be mixed within a specific declaration.
[c11e31c]1087//
[b87a5ed]1088//                      CFA                                     C
1089//              [10] int x;                     int x[10];              // array of 10 integers
1090//              [10] * char y;          char *y[10];    // array of 10 pointers to char
1091
1092new_declaration:                                                                                // CFA
[4d51835]1093        new_variable_declaration pop ';'
1094        | new_typedef_declaration pop ';'
1095        | new_function_declaration pop ';'
1096        | type_declaring_list pop ';'
[4040425]1097        | trait_specifier pop ';'
[4d51835]1098        ;
[b87a5ed]1099
1100new_variable_declaration:                                                               // CFA
[4d51835]1101        new_variable_specifier initializer_opt
1102                {
1103                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[3cfe27f]1104                        $$ = $1->addInitializer( $2 );
[4d51835]1105                }
1106        | declaration_qualifier_list new_variable_specifier initializer_opt
[de62360d]1107                // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
1108                // them as a type_qualifier cannot appear in that context.
[4d51835]1109                {
1110                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[3cfe27f]1111                        $$ = $2->addQualifiers( $1 )->addInitializer( $3 );;
[4d51835]1112                }
[2871210]1113        | new_variable_declaration pop ',' push identifier_or_type_name initializer_opt
[4d51835]1114                {
1115                        typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
[3cfe27f]1116                        $$ = $1->appendList( $1->cloneType( $5 )->addInitializer( $6 ) );
[4d51835]1117                }
1118        ;
[b87a5ed]1119
1120new_variable_specifier:                                                                 // CFA
[de62360d]1121                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1122                // storage-class
[2871210]1123        new_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
[4d51835]1124                {
1125                        typedefTable.setNextIdentifier( *$2 );
1126                        $$ = $1->addName( $2 );
1127                }
[2871210]1128        | new_abstract_tuple identifier_or_type_name asm_name_opt
[4d51835]1129                {
1130                        typedefTable.setNextIdentifier( *$2 );
1131                        $$ = $1->addName( $2 );
1132                }
[2871210]1133        | type_qualifier_list new_abstract_tuple identifier_or_type_name asm_name_opt
[4d51835]1134                {
1135                        typedefTable.setNextIdentifier( *$3 );
1136                        $$ = $2->addQualifiers( $1 )->addName( $3 );
1137                }
1138        ;
[b87a5ed]1139
1140new_function_declaration:                                                               // CFA
[4d51835]1141        new_function_specifier
1142                {
1143                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1144                        $$ = $1;
1145                }
1146        | type_qualifier_list new_function_specifier
1147                {
1148                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1149                        $$ = $2->addQualifiers( $1 );
1150                }
1151        | declaration_qualifier_list new_function_specifier
1152                {
1153                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1154                        $$ = $2->addQualifiers( $1 );
1155                }
1156        | declaration_qualifier_list type_qualifier_list new_function_specifier
1157                {
1158                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1159                        $$ = $3->addQualifiers( $1 )->addQualifiers( $2 );
1160                }
[2871210]1161        | new_function_declaration pop ',' push identifier_or_type_name
[4d51835]1162                {
1163                        typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
1164                        $$ = $1->appendList( $1->cloneType( $5 ) );
1165                }
1166        ;
[b87a5ed]1167
1168new_function_specifier:                                                                 // CFA
[2871210]1169        '[' ']' identifier_or_type_name '(' push new_parameter_type_list_opt pop ')' // S/R conflict
[4d51835]1170                {
[2871210]1171                        $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, 0, true );
[4d51835]1172                }
[2871210]1173//      '[' ']' identifier '(' push new_parameter_type_list_opt pop ')'
1174//              {
1175//                      typedefTable.setNextIdentifier( *$5 );
1176//                      $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1177//              }
1178//      | '[' ']' TYPEDEFname '(' push new_parameter_type_list_opt pop ')'
1179//              {
1180//                      typedefTable.setNextIdentifier( *$5 );
1181//                      $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1182//              }
1183//      | '[' ']' typegen_name
1184                // identifier_or_type_name must be broken apart because of the sequence:
[4d51835]1185                //
[2871210]1186                //   '[' ']' identifier_or_type_name '(' new_parameter_type_list_opt ')'
[4d51835]1187                //   '[' ']' type_specifier
1188                //
[2871210]1189                // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1190                // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
1191        | new_abstract_tuple identifier_or_type_name '(' push new_parameter_type_list_opt pop ')'
[de62360d]1192                // To obtain LR(1 ), this rule must be factored out from function return type (see new_abstract_declarator).
[4d51835]1193                {
1194                        $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1195                }
[2871210]1196        | new_function_return identifier_or_type_name '(' push new_parameter_type_list_opt pop ')'
[4d51835]1197                {
1198                        $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1199                }
1200        ;
[b87a5ed]1201
1202new_function_return:                                                                    // CFA
[4d51835]1203        '[' push new_parameter_list pop ']'
1204                { $$ = DeclarationNode::newTuple( $3 ); }
1205        | '[' push new_parameter_list pop ',' push new_abstract_parameter_list pop ']'
[de62360d]1206                // To obtain LR(1 ), the last new_abstract_parameter_list is added into this flattened rule to lookahead to the
1207                // ']'.
[4d51835]1208                { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
1209        ;
[b87a5ed]1210
1211new_typedef_declaration:                                                                // CFA
[4d51835]1212        TYPEDEF new_variable_specifier
1213                {
[de62360d]1214                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1215                        $$ = $2->addTypedef();
1216                }
1217        | TYPEDEF new_function_specifier
1218                {
[de62360d]1219                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1220                        $$ = $2->addTypedef();
1221                }
1222        | new_typedef_declaration pop ',' push no_attr_identifier
1223                {
[de62360d]1224                        typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
[4d51835]1225                        $$ = $1->appendList( $1->cloneType( $5 ) );
1226                }
1227        ;
[b87a5ed]1228
[de62360d]1229// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1230// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
[51b7345]1231
1232typedef_declaration:
[4d51835]1233        TYPEDEF type_specifier declarator
1234                {
[de62360d]1235                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1236                        $$ = $3->addType( $2 )->addTypedef();
1237                }
1238        | typedef_declaration pop ',' push declarator
1239                {
[de62360d]1240                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1241                        $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1242                }
[de62360d]1243        | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
[4d51835]1244                {
[de62360d]1245                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1246                        $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1247                }
1248        | type_specifier TYPEDEF declarator
1249                {
[de62360d]1250                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1251                        $$ = $3->addType( $1 )->addTypedef();
1252                }
1253        | type_specifier TYPEDEF type_qualifier_list declarator
1254                {
[de62360d]1255                        typedefTable.addToEnclosingScope( TypedefTable::TD );
1256                        $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
[4d51835]1257                }
1258        ;
[b87a5ed]1259
[721f17a]1260typedef_expression:
1261                // GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
[4d51835]1262        TYPEDEF no_attr_identifier '=' assignment_expression
1263                {
[de62360d]1264                        typedefTable.addToEnclosingScope( *$2, TypedefTable::TD );
[4d51835]1265                        $$ = DeclarationNode::newName( 0 ); // XXX
1266                }
1267        | typedef_expression pop ',' push no_attr_identifier '=' assignment_expression
1268                {
[de62360d]1269                        typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
[4d51835]1270                        $$ = DeclarationNode::newName( 0 ); // XXX
1271                }
1272        ;
[51b7345]1273
1274old_declaration:
[4d51835]1275        declaring_list pop ';'
1276        | typedef_declaration pop ';'
1277        | typedef_expression pop ';'                                            // GCC, naming expression type
1278        | sue_declaration_specifier pop ';'
1279        ;
[51b7345]1280
1281declaring_list:
[de62360d]1282                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1283                // storage-class
[4d51835]1284        declaration_specifier declarator asm_name_opt initializer_opt
1285                {
1286                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[de62360d]1287                        $$ = ( $2->addType( $1 ))->addInitializer( $4 );
[4d51835]1288                }
1289        | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1290                {
1291                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[de62360d]1292                        $$ = $1->appendList( $1->cloneBaseType( $4->addInitializer( $6 ) ) );
[4d51835]1293                }
1294        ;
[b87a5ed]1295
1296declaration_specifier:                                                                  // type specifier + storage class
[4d51835]1297        basic_declaration_specifier
1298        | sue_declaration_specifier
1299        | typedef_declaration_specifier
1300        | typegen_declaration_specifier
1301        ;
[b87a5ed]1302
1303type_specifier:                                                                                 // declaration specifier - storage class
[4d51835]1304        basic_type_specifier
1305        | sue_type_specifier
1306        | typedef_type_specifier
1307        | typegen_type_specifier
1308        ;
[b87a5ed]1309
1310type_qualifier_list_opt:                                                                // GCC, used in asm_statement
[4d51835]1311        // empty
1312                { $$ = 0; }
1313        | type_qualifier_list
1314        ;
[51b7345]1315
1316type_qualifier_list:
[de62360d]1317                // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
[4d51835]1318                //
[de62360d]1319                // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1320                // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1321                // appeared only once.
[4d51835]1322        type_qualifier
1323        | type_qualifier_list type_qualifier
1324                { $$ = $1->addQualifiers( $2 ); }
1325        ;
[51b7345]1326
1327type_qualifier:
[4d51835]1328        type_qualifier_name
1329        | attribute
[1db21619]1330        //{ $$ = DeclarationNode::newQualifier( DeclarationNode::Attribute ); }
[4d51835]1331        ;
[51b7345]1332
1333type_qualifier_name:
[4d51835]1334        CONST
1335                { $$ = DeclarationNode::newQualifier( DeclarationNode::Const ); }
1336        | RESTRICT
1337                { $$ = DeclarationNode::newQualifier( DeclarationNode::Restrict ); }
1338        | VOLATILE
1339                { $$ = DeclarationNode::newQualifier( DeclarationNode::Volatile ); }
1340        | LVALUE                                                                                        // CFA
1341                { $$ = DeclarationNode::newQualifier( DeclarationNode::Lvalue ); }
1342        | ATOMIC
1343                { $$ = DeclarationNode::newQualifier( DeclarationNode::Atomic ); }
1344        | FORALL '('
1345                {
1346                        typedefTable.enterScope();
1347                }
1348          type_parameter_list ')'                                                       // CFA
1349                {
1350                        typedefTable.leaveScope();
1351                        $$ = DeclarationNode::newForall( $4 );
1352                }
1353        ;
[51b7345]1354
1355declaration_qualifier_list:
[4d51835]1356        storage_class_list
[de62360d]1357        | type_qualifier_list storage_class_list                        // remaining OBSOLESCENT (see 2 )
[4d51835]1358                { $$ = $1->addQualifiers( $2 ); }
1359        | declaration_qualifier_list type_qualifier_list storage_class_list
1360                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1361        ;
[51b7345]1362
1363storage_class_list:
[de62360d]1364                // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
1365                // only one of each is specified, except for inline, which can appear with the others.
[4d51835]1366                //
[de62360d]1367                // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
1368                // specifiers in a declaration.
[4d51835]1369        storage_class
1370        | storage_class_list storage_class
1371                { $$ = $1->addQualifiers( $2 ); }
1372        ;
[51b7345]1373
1374storage_class:
[4d51835]1375        EXTERN
1376                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Extern ); }
1377        | STATIC
1378                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Static ); }
1379        | AUTO
1380                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Auto ); }
1381        | REGISTER
1382                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Register ); }
1383        | INLINE                                                                                        // C99
1384                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Inline ); }
1385        | FORTRAN                                                                                       // C99
1386                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Fortran ); }
[68cd1ce]1387        | NORETURN                                                                                      // C11
1388                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Noreturn ); }
1389        | THREADLOCAL                                                                           // C11
1390                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Threadlocal ); }
[4d51835]1391        ;
[51b7345]1392
1393basic_type_name:
[4d51835]1394        CHAR
1395                { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
1396        | DOUBLE
1397                { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
1398        | FLOAT
1399                { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
1400        | INT
1401                { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
1402        | LONG
1403                { $$ = DeclarationNode::newModifier( DeclarationNode::Long ); }
1404        | SHORT
1405                { $$ = DeclarationNode::newModifier( DeclarationNode::Short ); }
1406        | SIGNED
1407                { $$ = DeclarationNode::newModifier( DeclarationNode::Signed ); }
1408        | UNSIGNED
1409                { $$ = DeclarationNode::newModifier( DeclarationNode::Unsigned ); }
1410        | VOID
1411                { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1412        | BOOL                                                                                          // C99
1413                { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
1414        | COMPLEX                                                                                       // C99
1415                { $$ = DeclarationNode::newBasicType( DeclarationNode::Complex ); }
1416        | IMAGINARY                                                                                     // C99
1417                { $$ = DeclarationNode::newBasicType( DeclarationNode::Imaginary ); }
[90c3b1c]1418        | VALIST                                                                                        // GCC, __builtin_va_list
1419                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Valist ); }
[4d51835]1420        ;
[51b7345]1421
1422basic_declaration_specifier:
[4d51835]1423                // A semantic check is necessary for conflicting storage classes.
1424        basic_type_specifier
1425        | declaration_qualifier_list basic_type_specifier
1426                { $$ = $2->addQualifiers( $1 ); }
1427        | basic_declaration_specifier storage_class                     // remaining OBSOLESCENT (see 2)
1428                { $$ = $1->addQualifiers( $2 ); }
1429        | basic_declaration_specifier storage_class type_qualifier_list
1430                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1431        | basic_declaration_specifier storage_class basic_type_specifier
1432                { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
1433        ;
[51b7345]1434
1435basic_type_specifier:
[4d51835]1436        direct_type_name
1437        | type_qualifier_list_opt indirect_type_name type_qualifier_list_opt
1438                { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
1439        ;
[51b7345]1440
1441direct_type_name:
[4d51835]1442                // A semantic check is necessary for conflicting type qualifiers.
1443        basic_type_name
1444        | type_qualifier_list basic_type_name
1445                { $$ = $2->addQualifiers( $1 ); }
1446        | direct_type_name type_qualifier
1447                { $$ = $1->addQualifiers( $2 ); }
1448        | direct_type_name basic_type_name
1449                { $$ = $1->addType( $2 ); }
1450        ;
[51b7345]1451
1452indirect_type_name:
[4d51835]1453        TYPEOF '(' type_name ')'                                                        // GCC: typeof(x) y;
1454                { $$ = $3; }
1455        | TYPEOF '(' comma_expression ')'                                       // GCC: typeof(a+b) y;
1456                { $$ = DeclarationNode::newTypeof( $3 ); }
1457        | ATTR_TYPEGENname '(' type_name ')'                            // CFA: e.g., @type(x) y;
1458                { $$ = DeclarationNode::newAttr( $1, $3 ); }
1459        | ATTR_TYPEGENname '(' comma_expression ')'                     // CFA: e.g., @type(a+b) y;
1460                { $$ = DeclarationNode::newAttr( $1, $3 ); }
1461        ;
[51b7345]1462
1463sue_declaration_specifier:
[4d51835]1464        sue_type_specifier
1465        | declaration_qualifier_list sue_type_specifier
1466                { $$ = $2->addQualifiers( $1 ); }
1467        | sue_declaration_specifier storage_class                       // remaining OBSOLESCENT (see 2)
1468                { $$ = $1->addQualifiers( $2 ); }
1469        | sue_declaration_specifier storage_class type_qualifier_list
1470                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1471        ;
[51b7345]1472
1473sue_type_specifier:
[4d51835]1474        elaborated_type_name                                                            // struct, union, enum
1475        | type_qualifier_list elaborated_type_name
1476                { $$ = $2->addQualifiers( $1 ); }
1477        | sue_type_specifier type_qualifier
1478                { $$ = $1->addQualifiers( $2 ); }
1479        ;
[51b7345]1480
1481typedef_declaration_specifier:
[4d51835]1482        typedef_type_specifier
1483        | declaration_qualifier_list typedef_type_specifier
1484                { $$ = $2->addQualifiers( $1 ); }
1485        | typedef_declaration_specifier storage_class           // remaining OBSOLESCENT (see 2)
1486                { $$ = $1->addQualifiers( $2 ); }
1487        | typedef_declaration_specifier storage_class type_qualifier_list
1488                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1489        ;
[b87a5ed]1490
1491typedef_type_specifier:                                                                 // typedef types
[4d51835]1492        TYPEDEFname
1493                { $$ = DeclarationNode::newFromTypedef( $1 ); }
1494        | type_qualifier_list TYPEDEFname
1495                { $$ = DeclarationNode::newFromTypedef( $2 )->addQualifiers( $1 ); }
1496        | typedef_type_specifier type_qualifier
1497                { $$ = $1->addQualifiers( $2 ); }
1498        ;
[51b7345]1499
1500elaborated_type_name:
[4d51835]1501        aggregate_name
1502        | enum_name
1503        ;
[51b7345]1504
1505aggregate_name:
[4d51835]1506        aggregate_key '{' field_declaration_list '}'
[5d125e4]1507                { $$ = DeclarationNode::newAggregate( $1, 0, 0, $3, true ); }
[2871210]1508        | aggregate_key no_attr_identifier_or_type_name
[45161b4d]1509                {
1510                        typedefTable.makeTypedef( *$2 );
[5d125e4]1511                        $$ = DeclarationNode::newAggregate( $1, $2, 0, 0, false );
[45161b4d]1512                }
1513        | aggregate_key no_attr_identifier_or_type_name
1514                { typedefTable.makeTypedef( *$2 ); }
1515                '{' field_declaration_list '}'
[5d125e4]1516                { $$ = DeclarationNode::newAggregate( $1, $2, 0, $5, true ); }
[721f17a]1517        | aggregate_key '(' type_name_list ')' '{' field_declaration_list '}' // CFA
[5d125e4]1518                { $$ = DeclarationNode::newAggregate( $1, 0, $3, $6, false ); }
[2871210]1519        | aggregate_key typegen_name                                            // CFA, S/R conflict
1520                { $$ = $2; }
[4d51835]1521        ;
[51b7345]1522
1523aggregate_key:
[4d51835]1524        STRUCT attribute_list_opt
1525                { $$ = DeclarationNode::Struct; }
1526        | UNION attribute_list_opt
1527                { $$ = DeclarationNode::Union; }
1528        ;
[51b7345]1529
1530field_declaration_list:
[5d125e4]1531        // empty
1532                { $$ = 0; }
[4d51835]1533        | field_declaration_list field_declaration
[5d125e4]1534                { $$ = $1 != 0 ? $1->appendList( $2 ) : $2; }
[4d51835]1535        ;
[51b7345]1536
1537field_declaration:
[4d51835]1538        new_field_declaring_list ';'                                            // CFA, new style field declaration
1539        | EXTENSION new_field_declaring_list ';'                        // GCC
[ca35c51]1540                { $$ = $2->set_extension( true ); }
[4d51835]1541        | field_declaring_list ';'
1542        | EXTENSION field_declaring_list ';'                            // GCC
[8e9cbb2]1543                {       // mark all fields in list
[926af74]1544                        for ( DeclarationNode *iter = $2; iter != nullptr; iter = (DeclarationNode *)iter->get_next() )
[8e9cbb2]1545                                iter->set_extension( true );
1546                        $$ = $2;
1547                }
[4d51835]1548        ;
[b87a5ed]1549
1550new_field_declaring_list:                                                               // CFA, new style field declaration
[4d51835]1551        new_abstract_declarator_tuple                                           // CFA, no field name
[2871210]1552        | new_abstract_declarator_tuple no_attr_identifier_or_type_name
[4d51835]1553                { $$ = $1->addName( $2 ); }
[2871210]1554        | new_field_declaring_list ',' no_attr_identifier_or_type_name
[4d51835]1555                { $$ = $1->appendList( $1->cloneType( $3 ) ); }
1556        | new_field_declaring_list ','                                          // CFA, no field name
1557                { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1558        ;
[51b7345]1559
1560field_declaring_list:
[4d51835]1561        type_specifier field_declarator
1562                { $$ = $2->addType( $1 ); }
1563        | field_declaring_list ',' attribute_list_opt field_declarator
1564                { $$ = $1->appendList( $1->cloneBaseType( $4 ) ); }
1565        ;
[51b7345]1566
1567field_declarator:
[4d51835]1568        // empty
1569                { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
1570        | bit_subrange_size                                                                     // no field name
1571                { $$ = DeclarationNode::newBitfield( $1 ); }
1572        | variable_declarator bit_subrange_size_opt
1573                // A semantic check is required to ensure bit_subrange only appears on base type int.
1574                { $$ = $1->addBitfield( $2 ); }
[c6b1105]1575        | variable_type_redeclarator bit_subrange_size_opt
[4d51835]1576                // A semantic check is required to ensure bit_subrange only appears on base type int.
1577                { $$ = $1->addBitfield( $2 ); }
1578        | variable_abstract_declarator                                          // CFA, no field name
1579        ;
[51b7345]1580
1581bit_subrange_size_opt:
[4d51835]1582        // empty
1583                { $$ = 0; }
1584        | bit_subrange_size
1585                { $$ = $1; }
1586        ;
[51b7345]1587
1588bit_subrange_size:
[4d51835]1589        ':' constant_expression
1590                { $$ = $2; }
1591        ;
[51b7345]1592
1593enum_key:
[4d51835]1594        ENUM attribute_list_opt
1595        ;
[51b7345]1596
1597enum_name:
[4d51835]1598        enum_key '{' enumerator_list comma_opt '}'
1599                { $$ = DeclarationNode::newEnum( 0, $3 ); }
[2871210]1600        | enum_key no_attr_identifier_or_type_name
[45161b4d]1601                {
1602                        typedefTable.makeTypedef( *$2 );
1603                        $$ = DeclarationNode::newEnum( $2, 0 );
1604                }
1605        | enum_key no_attr_identifier_or_type_name
1606                { typedefTable.makeTypedef( *$2 ); }
1607                '{' enumerator_list comma_opt '}'
1608                { $$ = DeclarationNode::newEnum( $2, $5 ); }
[4d51835]1609        ;
[51b7345]1610
1611enumerator_list:
[2871210]1612        no_attr_identifier_or_type_name enumerator_value_opt
[4d51835]1613                { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
[2871210]1614        | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
[4d51835]1615                { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1616        ;
[51b7345]1617
1618enumerator_value_opt:
[4d51835]1619        // empty
1620                { $$ = 0; }
1621        | '=' constant_expression
1622                { $$ = $2; }
1623        ;
[51b7345]1624
[c11e31c]1625// Minimum of one parameter after which ellipsis is allowed only at the end.
[51b7345]1626
[b87a5ed]1627new_parameter_type_list_opt:                                                    // CFA
[4d51835]1628        // empty
1629                { $$ = 0; }
1630        | new_parameter_type_list
1631        ;
[b87a5ed]1632
1633new_parameter_type_list:                                                                // CFA, abstract + real
[4d51835]1634        new_abstract_parameter_list
1635        | new_parameter_list
1636        | new_parameter_list pop ',' push new_abstract_parameter_list
1637                { $$ = $1->appendList( $5 ); }
1638        | new_abstract_parameter_list pop ',' push ELLIPSIS
1639                { $$ = $1->addVarArgs(); }
1640        | new_parameter_list pop ',' push ELLIPSIS
1641                { $$ = $1->addVarArgs(); }
1642        ;
[b87a5ed]1643
1644new_parameter_list:                                                                             // CFA
[de62360d]1645                // To obtain LR(1) between new_parameter_list and new_abstract_tuple, the last new_abstract_parameter_list is
1646                // factored out from new_parameter_list, flattening the rules to get lookahead to the ']'.
[4d51835]1647        new_parameter_declaration
1648        | new_abstract_parameter_list pop ',' push new_parameter_declaration
1649                { $$ = $1->appendList( $5 ); }
1650        | new_parameter_list pop ',' push new_parameter_declaration
1651                { $$ = $1->appendList( $5 ); }
1652        | new_parameter_list pop ',' push new_abstract_parameter_list pop ',' push new_parameter_declaration
1653                { $$ = $1->appendList( $5 )->appendList( $9 ); }
1654        ;
[b87a5ed]1655
1656new_abstract_parameter_list:                                                    // CFA, new & old style abstract
[4d51835]1657        new_abstract_parameter_declaration
1658        | new_abstract_parameter_list pop ',' push new_abstract_parameter_declaration
1659                { $$ = $1->appendList( $5 ); }
1660        ;
[51b7345]1661
1662parameter_type_list_opt:
[4d51835]1663        // empty
1664                { $$ = 0; }
1665        | parameter_type_list
1666        ;
[51b7345]1667
1668parameter_type_list:
[4d51835]1669        parameter_list
1670        | parameter_list pop ',' push ELLIPSIS
1671                { $$ = $1->addVarArgs(); }
1672        ;
[b87a5ed]1673
1674parameter_list:                                                                                 // abstract + real
[4d51835]1675        abstract_parameter_declaration
1676        | parameter_declaration
1677        | parameter_list pop ',' push abstract_parameter_declaration
1678                { $$ = $1->appendList( $5 ); }
1679        | parameter_list pop ',' push parameter_declaration
1680                { $$ = $1->appendList( $5 ); }
1681        ;
[51b7345]1682
[de62360d]1683// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
[2871210]1684// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
[51b7345]1685
[b87a5ed]1686new_parameter_declaration:                                                              // CFA, new & old style parameter declaration
[4d51835]1687        parameter_declaration
[2871210]1688        | new_identifier_parameter_declarator_no_tuple identifier_or_type_name assignment_opt
[4d51835]1689                { $$ = $1->addName( $2 ); }
[2871210]1690        | new_abstract_tuple identifier_or_type_name assignment_opt
[4d51835]1691                // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
1692                { $$ = $1->addName( $2 ); }
[2871210]1693        | type_qualifier_list new_abstract_tuple identifier_or_type_name assignment_opt
[4d51835]1694                { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
1695        | new_function_specifier
1696        ;
[b87a5ed]1697
1698new_abstract_parameter_declaration:                                             // CFA, new & old style parameter declaration
[4d51835]1699        abstract_parameter_declaration
1700        | new_identifier_parameter_declarator_no_tuple
1701        | new_abstract_tuple
1702                // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
1703        | type_qualifier_list new_abstract_tuple
1704                { $$ = $2->addQualifiers( $1 ); }
1705        | new_abstract_function
1706        ;
[51b7345]1707
1708parameter_declaration:
[4d51835]1709        declaration_specifier identifier_parameter_declarator assignment_opt
1710                {
1711                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[de62360d]1712                        $$ = $2->addType( $1 )->addInitializer( new InitializerNode( $3 ) );
[4d51835]1713                }
[2871210]1714        | declaration_specifier type_parameter_redeclarator assignment_opt
[4d51835]1715                {
1716                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[de62360d]1717                        $$ = $2->addType( $1 )->addInitializer( new InitializerNode( $3 ) );
[4d51835]1718                }
1719        ;
[51b7345]1720
1721abstract_parameter_declaration:
[4d51835]1722        declaration_specifier
1723        | declaration_specifier abstract_parameter_declarator
1724                { $$ = $2->addType( $1 ); }
1725        ;
[51b7345]1726
[c11e31c]1727// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
[de62360d]1728// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
1729// identifiers.  The ANSI-style parameter-list can redefine a typedef name.
[51b7345]1730
[b87a5ed]1731identifier_list:                                                                                // K&R-style parameter list => no types
[4d51835]1732        no_attr_identifier
1733                { $$ = DeclarationNode::newName( $1 ); }
1734        | identifier_list ',' no_attr_identifier
1735                { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
1736        ;
[51b7345]1737
[2871210]1738identifier_or_type_name:
[4d51835]1739        identifier
1740        | TYPEDEFname
1741        | TYPEGENname
1742        ;
[51b7345]1743
[2871210]1744no_01_identifier_or_type_name:
[4d51835]1745        no_01_identifier
1746        | TYPEDEFname
1747        | TYPEGENname
1748        ;
[51b7345]1749
[2871210]1750no_attr_identifier_or_type_name:
[4d51835]1751        no_attr_identifier
1752        | TYPEDEFname
[721f17a]1753        | TYPEGENname
[4d51835]1754        ;
[b87a5ed]1755
1756type_name_no_function:                                                                  // sizeof, alignof, cast (constructor)
[4d51835]1757        new_abstract_declarator_tuple                                           // CFA
1758        | type_specifier
1759        | type_specifier variable_abstract_declarator
1760                { $$ = $2->addType( $1 ); }
1761        ;
[b87a5ed]1762
1763type_name:                                                                                              // typeof, assertion
[4d51835]1764        new_abstract_declarator_tuple                                           // CFA
1765        | new_abstract_function                                                         // CFA
1766        | type_specifier
1767        | type_specifier abstract_declarator
1768                { $$ = $2->addType( $1 ); }
1769        ;
[51b7345]1770
1771initializer_opt:
[4d51835]1772        // empty
1773                { $$ = 0; }
[2871210]1774        | '=' initializer
1775                { $$ = $2; }
[097e2b0]1776        | ATassign initializer
[974906e2]1777                { $$ = $2->set_maybeConstructed( false ); }
[4d51835]1778        ;
[51b7345]1779
1780initializer:
[de62360d]1781        assignment_expression                                           { $$ = new InitializerNode( $1 ); }
1782        | '{' initializer_list comma_opt '}'            { $$ = new InitializerNode( $2, true ); }
[4d51835]1783        ;
[51b7345]1784
1785initializer_list:
[097e2b0]1786        // empty
1787                { $$ = 0; }
1788        | initializer
[4d51835]1789        | designation initializer                                       { $$ = $2->set_designators( $1 ); }
[1d4580a]1790        | initializer_list ',' initializer                      { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
[4d51835]1791        | initializer_list ',' designation initializer
[1d4580a]1792                { $$ = (InitializerNode *)( $1->set_last( $4->set_designators( $3 ) ) ); }
[4d51835]1793        ;
[b87a5ed]1794
[de62360d]1795// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
1796// '=' to separator the designator from the initializer value, as in:
[c11e31c]1797//
[b87a5ed]1798//              int x[10] = { [1] = 3 };
[c11e31c]1799//
[de62360d]1800// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment.  To disambiguate this case, CFA
1801// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
1802// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
1803// shift/reduce conflicts
[51b7345]1804
1805designation:
[4d51835]1806        designator_list ':'                                                                     // C99, CFA uses ":" instead of "="
[2871210]1807        | no_attr_identifier_or_type_name ':'                           // GCC, field name
[d1625f8]1808                { $$ = new ExpressionNode( build_varref( $1 ) ); }
[4d51835]1809        ;
[51b7345]1810
[b87a5ed]1811designator_list:                                                                                // C99
[4d51835]1812        designator
[2871210]1813        | designator_list designator
[1d4580a]1814                { $$ = (ExpressionNode *)( $1->set_last( $2 ) ); }
[d1625f8]1815        //| designator_list designator                                          { $$ = new ExpressionNode( $1, $2 ); }
[4d51835]1816        ;
[51b7345]1817
1818designator:
[d1625f8]1819        '.' no_attr_identifier_or_type_name                                     // C99, field name
1820                { $$ = new ExpressionNode( build_varref( $2 ) ); }
[4d51835]1821        | '[' push assignment_expression pop ']'                        // C99, single array element
[de62360d]1822                // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
[d1625f8]1823                { $$ = $3; }
[4d51835]1824        | '[' push subrange pop ']'                                                     // CFA, multiple array elements
[d1625f8]1825                { $$ = $3; }
[4d51835]1826        | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
[d1625f8]1827                { $$ = new ExpressionNode( build_range( $3, $5 ) ); }
[4d51835]1828        | '.' '[' push field_list pop ']'                                       // CFA, tuple field selector
[d1625f8]1829                { $$ = $4; }
[4d51835]1830        ;
[51b7345]1831
[de62360d]1832// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
1833// rather than an object-oriented type system. This required four groups of extensions:
[c11e31c]1834//
1835// Overloading: function, data, and operator identifiers may be overloaded.
1836//
[de62360d]1837// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
1838//     and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
1839//     definitions of new types. Type declarations with storage class "extern" provide opaque types.
[c11e31c]1840//
[de62360d]1841// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
1842//     site. A polymorphic function is not a template; it is a function, with an address and a type.
[c11e31c]1843//
1844// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
[de62360d]1845//     types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
1846//     hierarchies. Unlike classes, they can define relationships between types.  Assertions declare that a type or
1847//     types provide the operations declared by a specification.  Assertions are normally used to declare requirements
1848//     on type arguments of polymorphic functions.
[c11e31c]1849
[b87a5ed]1850typegen_declaration_specifier:                                                  // CFA
[4d51835]1851        typegen_type_specifier
1852        | declaration_qualifier_list typegen_type_specifier
1853                { $$ = $2->addQualifiers( $1 ); }
1854        | typegen_declaration_specifier storage_class           // remaining OBSOLESCENT (see 2)
1855                { $$ = $1->addQualifiers( $2 ); }
1856        | typegen_declaration_specifier storage_class type_qualifier_list
1857                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1858        ;
[b87a5ed]1859
1860typegen_type_specifier:                                                                 // CFA
[2871210]1861        typegen_name
1862        | type_qualifier_list typegen_name
1863                { $$ = $2->addQualifiers( $1 ); }
[4d51835]1864        | typegen_type_specifier type_qualifier
1865                { $$ = $1->addQualifiers( $2 ); }
1866        ;
[b87a5ed]1867
[2871210]1868typegen_name:                                                                                   // CFA
1869        TYPEGENname '(' type_name_list ')'
1870                { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
1871        ;
1872
[b87a5ed]1873type_parameter_list:                                                                    // CFA
[4d51835]1874        type_parameter assignment_opt
1875        | type_parameter_list ',' type_parameter assignment_opt
1876                { $$ = $1->appendList( $3 ); }
1877        ;
[b87a5ed]1878
1879type_parameter:                                                                                 // CFA
[2871210]1880        type_class no_attr_identifier_or_type_name
1881                { typedefTable.addToEnclosingScope( *$2, TypedefTable::TD ); }
[4d51835]1882          assertion_list_opt
1883                { $$ = DeclarationNode::newTypeParam( $1, $2 )->addAssertions( $4 ); }
1884        | type_specifier identifier_parameter_declarator
1885        ;
[b87a5ed]1886
1887type_class:                                                                                             // CFA
[4040425]1888        OTYPE
[4d51835]1889                { $$ = DeclarationNode::Type; }
1890        | DTYPE
1891                { $$ = DeclarationNode::Ftype; }
1892        | FTYPE
1893                { $$ = DeclarationNode::Dtype; }
1894        ;
[b87a5ed]1895
1896assertion_list_opt:                                                                             // CFA
[4d51835]1897        // empty
1898                { $$ = 0; }
1899        | assertion_list_opt assertion
[4e06c1e]1900                { $$ = $1 != 0 ? $1->appendList( $2 ) : $2; }
[4d51835]1901        ;
[b87a5ed]1902
1903assertion:                                                                                              // CFA
[2871210]1904        '|' no_attr_identifier_or_type_name '(' type_name_list ')'
[4d51835]1905                {
[4040425]1906                        typedefTable.openTrait( *$2 );
1907                        $$ = DeclarationNode::newTraitUse( $2, $4 );
[4d51835]1908                }
[4040425]1909        | '|' '{' push trait_declaration_list '}'
[4d51835]1910                { $$ = $4; }
[4040425]1911        | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list '}' '(' type_name_list ')'
[4d51835]1912                { $$ = 0; }
1913        ;
[b87a5ed]1914
1915type_name_list:                                                                                 // CFA
[4d51835]1916        type_name
[d1625f8]1917                { $$ = new ExpressionNode( build_typevalue( $1 ) ); }
[4d51835]1918        | assignment_expression
1919        | type_name_list ',' type_name
[1d4580a]1920                { $$ = (ExpressionNode *)( $1->set_last( new ExpressionNode( build_typevalue( $3 ) ) ) ); }
[4d51835]1921        | type_name_list ',' assignment_expression
[1d4580a]1922                { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
[4d51835]1923        ;
[b87a5ed]1924
1925type_declaring_list:                                                                    // CFA
[4040425]1926        OTYPE type_declarator
[4d51835]1927                { $$ = $2; }
[4040425]1928        | storage_class_list OTYPE type_declarator
[4d51835]1929                { $$ = $3->addQualifiers( $1 ); }
1930        | type_declaring_list ',' type_declarator
1931                { $$ = $1->appendList( $3->copyStorageClasses( $1 ) ); }
1932        ;
[b87a5ed]1933
1934type_declarator:                                                                                // CFA
[4d51835]1935        type_declarator_name assertion_list_opt
1936                { $$ = $1->addAssertions( $2 ); }
1937        | type_declarator_name assertion_list_opt '=' type_name
1938                { $$ = $1->addAssertions( $2 )->addType( $4 ); }
1939        ;
[b87a5ed]1940
1941type_declarator_name:                                                                   // CFA
[2871210]1942        no_attr_identifier_or_type_name
[4d51835]1943                {
[de62360d]1944                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TD );
[4d51835]1945                        $$ = DeclarationNode::newTypeDecl( $1, 0 );
1946                }
[2871210]1947        | no_01_identifier_or_type_name '(' push type_parameter_list pop ')'
[4d51835]1948                {
[de62360d]1949                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TG );
[4d51835]1950                        $$ = DeclarationNode::newTypeDecl( $1, $4 );
1951                }
1952        ;
[b87a5ed]1953
[4040425]1954trait_specifier:                                                                                // CFA
1955        TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{' '}'
[4d51835]1956                {
[de62360d]1957                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
[4040425]1958                        $$ = DeclarationNode::newTrait( $2, $5, 0 );
[4d51835]1959                }
[4040425]1960        | TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{'
[4d51835]1961                {
[4040425]1962                        typedefTable.enterTrait( *$2 );
[4d51835]1963                        typedefTable.enterScope();
1964                }
[4040425]1965          trait_declaration_list '}'
[4d51835]1966                {
[4040425]1967                        typedefTable.leaveTrait();
[de62360d]1968                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
[4040425]1969                        $$ = DeclarationNode::newTrait( $2, $5, $10 );
[4d51835]1970                }
1971        ;
[b87a5ed]1972
[4040425]1973trait_declaration_list:                                                         // CFA
1974        trait_declaration
1975        | trait_declaration_list push trait_declaration
[4d51835]1976                { $$ = $1->appendList( $3 ); }
1977        ;
[b87a5ed]1978
[4040425]1979trait_declaration:                                                                      // CFA
1980        new_trait_declaring_list pop ';'
1981        | trait_declaring_list pop ';'
[4d51835]1982        ;
[b87a5ed]1983
[4040425]1984new_trait_declaring_list:                                                               // CFA
[4d51835]1985        new_variable_specifier
1986                {
1987                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1988                        $$ = $1;
1989                }
1990        | new_function_specifier
1991                {
1992                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1993                        $$ = $1;
1994                }
[4040425]1995        | new_trait_declaring_list pop ',' push identifier_or_type_name
[4d51835]1996                {
[de62360d]1997                        typedefTable.addToEnclosingScope2( *$5, TypedefTable::ID );
[4d51835]1998                        $$ = $1->appendList( $1->cloneType( $5 ) );
1999                }
2000        ;
[b87a5ed]2001
[4040425]2002trait_declaring_list:                                                                   // CFA
[4d51835]2003        type_specifier declarator
2004                {
2005                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
2006                        $$ = $2->addType( $1 );
2007                }
[4040425]2008        | trait_declaring_list pop ',' push declarator
[4d51835]2009                {
2010                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
2011                        $$ = $1->appendList( $1->cloneBaseType( $5 ) );
2012                }
2013        ;
[51b7345]2014
[c11e31c]2015//***************************** EXTERNAL DEFINITIONS *****************************
[51b7345]2016
2017translation_unit:
[4d51835]2018        // empty
2019                {}                                                                                              // empty input file
2020        | external_definition_list
[926af74]2021                { parseTree = parseTree != nullptr ? parseTree->appendList( $1 ) : $1;  }
[4d51835]2022        ;
[51b7345]2023
2024external_definition_list:
[4d51835]2025        external_definition
2026        | external_definition_list push external_definition
[926af74]2027                { $$ = $1 != nullptr ? $1->appendList( $3 ) : $3; }
[4d51835]2028        ;
[51b7345]2029
2030external_definition_list_opt:
[4d51835]2031        // empty
2032                { $$ = 0; }
2033        | external_definition_list
2034        ;
[51b7345]2035
2036external_definition:
[4d51835]2037        declaration
2038        | external_function_definition
2039        | asm_statement                                                                         // GCC, global assembler statement
2040                {}
2041        | EXTERN STRINGliteral
2042                {
[3b8e52c]2043                        linkageStack.push( linkage );                           // handle nested extern "C"/"Cforall"
[ab57786]2044                        linkage = LinkageSpec::fromString( *$2 );
[4d51835]2045                }
2046          '{' external_definition_list_opt '}'                          // C++-style linkage specifier
2047                {
2048                        linkage = linkageStack.top();
2049                        linkageStack.pop();
2050                        $$ = $5;
2051                }
2052        | EXTENSION external_definition
[8e9cbb2]2053                {       // mark all fields in list
[926af74]2054                        for ( DeclarationNode *iter = $2; iter != nullptr; iter = (DeclarationNode *)iter->get_next() )
[8e9cbb2]2055                                iter->set_extension( true );
2056                        $$ = $2;
2057                }
[4d51835]2058        ;
2059
2060external_function_definition:
2061        function_definition
[de62360d]2062                // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
[c6b1105]2063                // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2064                // Parsing is possible because function_definition does not appear in the context of an expression (nested
2065                // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2066                // declaration must still have a type_specifier.  OBSOLESCENT (see 1)
[4d51835]2067        | function_declarator compound_statement
2068                {
2069                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2070                        typedefTable.leaveScope();
2071                        $$ = $1->addFunctionBody( $2 );
2072                }
2073        | old_function_declarator push old_declaration_list_opt compound_statement
2074                {
2075                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2076                        typedefTable.leaveScope();
2077                        $$ = $1->addOldDeclList( $3 )->addFunctionBody( $4 );
2078                }
2079        ;
[51b7345]2080
2081function_definition:
[4d51835]2082        new_function_declaration compound_statement                     // CFA
2083                {
2084                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2085                        typedefTable.leaveScope();
2086                        $$ = $1->addFunctionBody( $2 );
2087                }
2088        | declaration_specifier function_declarator compound_statement
2089                {
2090                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2091                        typedefTable.leaveScope();
2092                        $$ = $2->addFunctionBody( $3 )->addType( $1 );
2093                }
2094        | type_qualifier_list function_declarator compound_statement
2095                {
2096                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2097                        typedefTable.leaveScope();
2098                        $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
2099                }
2100        | declaration_qualifier_list function_declarator compound_statement
2101                {
2102                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2103                        typedefTable.leaveScope();
2104                        $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
2105                }
2106        | declaration_qualifier_list type_qualifier_list function_declarator compound_statement
2107                {
2108                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2109                        typedefTable.leaveScope();
2110                        $$ = $3->addFunctionBody( $4 )->addQualifiers( $2 )->addQualifiers( $1 );
2111                }
2112
2113                // Old-style K&R function definition, OBSOLESCENT (see 4)
2114        | declaration_specifier old_function_declarator push old_declaration_list_opt compound_statement
2115                {
2116                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2117                        typedefTable.leaveScope();
2118                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addType( $1 );
2119                }
2120        | type_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2121                {
2122                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2123                        typedefTable.leaveScope();
2124                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
2125                }
2126
2127                // Old-style K&R function definition with "implicit int" type_specifier, OBSOLESCENT (see 4)
2128        | declaration_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2129                {
2130                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2131                        typedefTable.leaveScope();
2132                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
2133                }
2134        | declaration_qualifier_list type_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2135                {
2136                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2137                        typedefTable.leaveScope();
2138                        $$ = $3->addOldDeclList( $5 )->addFunctionBody( $6 )->addQualifiers( $2 )->addQualifiers( $1 );
2139                }
2140        ;
[51b7345]2141
2142declarator:
[4d51835]2143        variable_declarator
[c6b1105]2144        | variable_type_redeclarator
[4d51835]2145        | function_declarator
2146        ;
[51b7345]2147
2148subrange:
[4d51835]2149        constant_expression '~' constant_expression                     // CFA, integer subrange
[d1625f8]2150                { $$ = new ExpressionNode( build_range( $1, $3 ) ); }
[4d51835]2151        ;
[b87a5ed]2152
2153asm_name_opt:                                                                                   // GCC
[4d51835]2154        // empty
2155        | ASM '(' string_literal_list ')' attribute_list_opt
[8d2844a]2156                { delete $3; }
[4d51835]2157        ;
[b87a5ed]2158
2159attribute_list_opt:                                                                             // GCC
[4d51835]2160        // empty
[1db21619]2161                { $$ = 0; }
[4d51835]2162        | attribute_list
2163        ;
[b87a5ed]2164
2165attribute_list:                                                                                 // GCC
[4d51835]2166        attribute
2167        | attribute_list attribute
[1db21619]2168                { $$ = $2->addQualifiers( $1 ); }
[4d51835]2169        ;
[b87a5ed]2170
2171attribute:                                                                                              // GCC
[4d51835]2172        ATTRIBUTE '(' '(' attribute_parameter_list ')' ')'
[1db21619]2173        //              { $$ = DeclarationNode::newQualifier( DeclarationNode::Attribute ); }
2174                { $$ = 0; }
[4d51835]2175        ;
[b87a5ed]2176
2177attribute_parameter_list:                                                               // GCC
[4d51835]2178        attrib
2179        | attribute_parameter_list ',' attrib
2180        ;
[b87a5ed]2181
2182attrib:                                                                                                 // GCC
[4d51835]2183        // empty
2184        | any_word
2185        | any_word '(' comma_expression_opt ')'
[8d2844a]2186                { delete $3; }
[4d51835]2187        ;
[b87a5ed]2188
2189any_word:                                                                                               // GCC
[2037f82]2190        identifier_or_type_name { delete $1; }
2191        | storage_class { delete $1; }
2192        | basic_type_name { delete $1; }
2193        | type_qualifier { delete $1; }
[4d51835]2194        ;
[51b7345]2195
[c11e31c]2196// ============================================================================
[de62360d]2197// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2198// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2199// in:
[c11e31c]2200//
[b87a5ed]2201//              int (*f())[10] { ... };
2202//              ... (*f())[3] += 1;             // definition mimics usage
[c11e31c]2203//
[de62360d]2204// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2205// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
[c11e31c]2206// ============================================================================
2207
2208// ----------------------------------------------------------------------------
[de62360d]2209// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2210// to define a variable or function prototype, e.g.:
[c11e31c]2211//
[b87a5ed]2212//              valid declaration               invalid definition
2213//              -----------------               ------------------
[4d51835]2214//              int f;                                  int f {}
2215//              int *f;                                 int *f {}
[b87a5ed]2216//              int f[10];                              int f[10] {}
[4d51835]2217//              int (*f)(int);                  int (*f)(int) {}
[c11e31c]2218//
[de62360d]2219// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2220// variable_declarator and function_declarator.
[c11e31c]2221// ----------------------------------------------------------------------------
2222
[de62360d]2223// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2224// declaring an array of functions versus a pointer to an array of functions.
[51b7345]2225
2226variable_declarator:
[4d51835]2227        paren_identifier attribute_list_opt
[1db21619]2228                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2229        | variable_ptr
2230        | variable_array attribute_list_opt
[1db21619]2231                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2232        | variable_function attribute_list_opt
[1db21619]2233                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2234        ;
[51b7345]2235
2236paren_identifier:
[4d51835]2237        identifier
2238                {
[de62360d]2239                        typedefTable.setNextIdentifier( *$1 );
[4d51835]2240                        $$ = DeclarationNode::newName( $1 );
2241                }
2242        | '(' paren_identifier ')'                                                      // redundant parenthesis
2243                { $$ = $2; }
2244        ;
[51b7345]2245
2246variable_ptr:
[dd51906]2247        ptrref_operator variable_declarator
[4d51835]2248                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2249        | ptrref_operator type_qualifier_list variable_declarator
[4d51835]2250                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2251        | '(' variable_ptr ')'
2252                { $$ = $2; }
2253        ;
[51b7345]2254
2255variable_array:
[4d51835]2256        paren_identifier array_dimension
2257                { $$ = $1->addArray( $2 ); }
2258        | '(' variable_ptr ')' array_dimension
2259                { $$ = $2->addArray( $4 ); }
2260        | '(' variable_array ')' multi_array_dimension          // redundant parenthesis
2261                { $$ = $2->addArray( $4 ); }
2262        | '(' variable_array ')'                                                        // redundant parenthesis
2263                { $$ = $2; }
2264        ;
[51b7345]2265
2266variable_function:
[4d51835]2267        '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2268                { $$ = $2->addParamList( $6 ); }
2269        | '(' variable_function ')'                                                     // redundant parenthesis
2270                { $$ = $2; }
2271        ;
[51b7345]2272
[c6b1105]2273// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2274// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2275// is the same scope.  The pattern precludes returning arrays and functions versus pointers to arrays and functions.
[51b7345]2276
2277function_declarator:
[4d51835]2278        function_no_ptr attribute_list_opt
[1db21619]2279                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2280        | function_ptr
2281        | function_array attribute_list_opt
[1db21619]2282                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2283        ;
[51b7345]2284
2285function_no_ptr:
[4d51835]2286        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2287                { $$ = $1->addParamList( $4 ); }
2288        | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2289                { $$ = $2->addParamList( $6 ); }
2290        | '(' function_no_ptr ')'                                                       // redundant parenthesis
2291                { $$ = $2; }
2292        ;
[51b7345]2293
2294function_ptr:
[dd51906]2295        ptrref_operator function_declarator
[4d51835]2296                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2297        | ptrref_operator type_qualifier_list function_declarator
[4d51835]2298                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2299        | '(' function_ptr ')'
2300                { $$ = $2; }
2301        ;
[51b7345]2302
2303function_array:
[4d51835]2304        '(' function_ptr ')' array_dimension
2305                { $$ = $2->addArray( $4 ); }
2306        | '(' function_array ')' multi_array_dimension          // redundant parenthesis
2307                { $$ = $2->addArray( $4 ); }
2308        | '(' function_array ')'                                                        // redundant parenthesis
2309                { $$ = $2; }
2310        ;
[51b7345]2311
[de62360d]2312// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4) that is not redefining a typedef name
2313// (see function_declarator for additional comments). The pattern precludes returning arrays and functions versus
2314// pointers to arrays and functions.
[51b7345]2315
2316old_function_declarator:
[4d51835]2317        old_function_no_ptr
2318        | old_function_ptr
2319        | old_function_array
2320        ;
[51b7345]2321
2322old_function_no_ptr:
[4d51835]2323        paren_identifier '(' identifier_list ')'                        // function_declarator handles empty parameter
2324                { $$ = $1->addIdList( $3 ); }
2325        | '(' old_function_ptr ')' '(' identifier_list ')'
2326                { $$ = $2->addIdList( $5 ); }
2327        | '(' old_function_no_ptr ')'                                           // redundant parenthesis
2328                { $$ = $2; }
2329        ;
[51b7345]2330
2331old_function_ptr:
[dd51906]2332        ptrref_operator old_function_declarator
[4d51835]2333                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2334        | ptrref_operator type_qualifier_list old_function_declarator
[4d51835]2335                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2336        | '(' old_function_ptr ')'
2337                { $$ = $2; }
2338        ;
[51b7345]2339
2340old_function_array:
[4d51835]2341        '(' old_function_ptr ')' array_dimension
2342                { $$ = $2->addArray( $4 ); }
2343        | '(' old_function_array ')' multi_array_dimension      // redundant parenthesis
2344                { $$ = $2->addArray( $4 ); }
2345        | '(' old_function_array ')'                                            // redundant parenthesis
2346                { $$ = $2; }
2347        ;
[51b7345]2348
[2871210]2349// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
[c11e31c]2350//
[b87a5ed]2351//              typedef int foo;
2352//              {
2353//                 int foo; // redefine typedef name in new scope
2354//              }
[c11e31c]2355//
[de62360d]2356// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2357// and functions versus pointers to arrays and functions.
[51b7345]2358
[c6b1105]2359variable_type_redeclarator:
[2871210]2360        paren_type attribute_list_opt
[1db21619]2361                { $$ = $1->addQualifiers( $2 ); }
[2871210]2362        | type_ptr
2363        | type_array attribute_list_opt
[1db21619]2364                { $$ = $1->addQualifiers( $2 ); }
[2871210]2365        | type_function attribute_list_opt
[1db21619]2366                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2367        ;
[51b7345]2368
[2871210]2369paren_type:
2370        typedef
2371        | '(' paren_type ')'
[4d51835]2372                { $$ = $2; }
2373        ;
[51b7345]2374
[2871210]2375type_ptr:
[c6b1105]2376        ptrref_operator variable_type_redeclarator
[4d51835]2377                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[c6b1105]2378        | ptrref_operator type_qualifier_list variable_type_redeclarator
[4d51835]2379                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
[2871210]2380        | '(' type_ptr ')'
[4d51835]2381                { $$ = $2; }
2382        ;
[51b7345]2383
[2871210]2384type_array:
2385        paren_type array_dimension
[4d51835]2386                { $$ = $1->addArray( $2 ); }
[2871210]2387        | '(' type_ptr ')' array_dimension
[4d51835]2388                { $$ = $2->addArray( $4 ); }
[2871210]2389        | '(' type_array ')' multi_array_dimension                      // redundant parenthesis
[4d51835]2390                { $$ = $2->addArray( $4 ); }
[2871210]2391        | '(' type_array ')'                                                            // redundant parenthesis
[4d51835]2392                { $$ = $2; }
2393        ;
[51b7345]2394
[2871210]2395type_function:
2396        paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2397                { $$ = $1->addParamList( $4 ); }
[2871210]2398        | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2399                { $$ = $2->addParamList( $6 ); }
[2871210]2400        | '(' type_function ')'                                                         // redundant parenthesis
[4d51835]2401                { $$ = $2; }
2402        ;
[51b7345]2403
[de62360d]2404// This pattern parses a declaration for a parameter variable or function prototype that is not redefining a typedef
2405// name and allows the C99 array options, which can only appear in a parameter list.  The pattern precludes declaring an
2406// array of functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to
2407// arrays and functions.
[51b7345]2408
2409identifier_parameter_declarator:
[4d51835]2410        paren_identifier attribute_list_opt
[1db21619]2411                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2412        | identifier_parameter_ptr
2413        | identifier_parameter_array attribute_list_opt
[1db21619]2414                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2415        | identifier_parameter_function attribute_list_opt
[1db21619]2416                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2417        ;
[51b7345]2418
2419identifier_parameter_ptr:
[dd51906]2420        ptrref_operator identifier_parameter_declarator
[4d51835]2421                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2422        | ptrref_operator type_qualifier_list identifier_parameter_declarator
[4d51835]2423                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2424        | '(' identifier_parameter_ptr ')'
2425                { $$ = $2; }
2426        ;
[51b7345]2427
2428identifier_parameter_array:
[4d51835]2429        paren_identifier array_parameter_dimension
2430                { $$ = $1->addArray( $2 ); }
2431        | '(' identifier_parameter_ptr ')' array_dimension
2432                { $$ = $2->addArray( $4 ); }
2433        | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2434                { $$ = $2->addArray( $4 ); }
2435        | '(' identifier_parameter_array ')'                            // redundant parenthesis
2436                { $$ = $2; }
2437        ;
[51b7345]2438
2439identifier_parameter_function:
[4d51835]2440        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2441                { $$ = $1->addParamList( $4 ); }
2442        | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2443                { $$ = $2->addParamList( $6 ); }
2444        | '(' identifier_parameter_function ')'                         // redundant parenthesis
2445                { $$ = $2; }
2446        ;
[b87a5ed]2447
[de62360d]2448// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2449// e.g.:
[c11e31c]2450//
[b87a5ed]2451//              typedef int foo;
2452//              int f( int foo ); // redefine typedef name in new scope
[c11e31c]2453//
[de62360d]2454// and allows the C99 array options, which can only appear in a parameter list.  In addition, the pattern handles the
2455// special meaning of parenthesis around a typedef name:
[c11e31c]2456//
[b87a5ed]2457//              ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2458//              parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2459//              not as redundant parentheses around the identifier."
[c11e31c]2460//
[c6b1105]2461// For example:
[c11e31c]2462//
[b87a5ed]2463//              typedef float T;
[4d51835]2464//              int f( int ( T [5] ) );                                 // see abstract_parameter_declarator
[b87a5ed]2465//              int g( int ( T ( int ) ) );                             // see abstract_parameter_declarator
2466//              int f( int f1( T a[5] ) );                              // see identifier_parameter_declarator
2467//              int g( int g1( T g2( int p ) ) );               // see identifier_parameter_declarator
[c11e31c]2468//
[de62360d]2469// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2470// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2471// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2472// functions.
[51b7345]2473
[2871210]2474type_parameter_redeclarator:
[4d51835]2475        typedef attribute_list_opt
[1db21619]2476                { $$ = $1->addQualifiers( $2 ); }
[2871210]2477        | type_parameter_ptr
2478        | type_parameter_array attribute_list_opt
[1db21619]2479                { $$ = $1->addQualifiers( $2 ); }
[2871210]2480        | type_parameter_function attribute_list_opt
[1db21619]2481                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2482        ;
[51b7345]2483
2484typedef:
[4d51835]2485        TYPEDEFname
2486                {
[de62360d]2487                        typedefTable.setNextIdentifier( *$1 );
[4d51835]2488                        $$ = DeclarationNode::newName( $1 );
2489                }
[2871210]2490        | TYPEGENname
2491                {
2492                        typedefTable.setNextIdentifier( *$1 );
2493                        $$ = DeclarationNode::newName( $1 );
2494                }
[4d51835]2495        ;
[51b7345]2496
[2871210]2497type_parameter_ptr:
[dd51906]2498        ptrref_operator type_parameter_redeclarator
[4d51835]2499                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2500        | ptrref_operator type_qualifier_list type_parameter_redeclarator
[4d51835]2501                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
[2871210]2502        | '(' type_parameter_ptr ')'
[4d51835]2503                { $$ = $2; }
2504        ;
[51b7345]2505
[2871210]2506type_parameter_array:
[4d51835]2507        typedef array_parameter_dimension
2508                { $$ = $1->addArray( $2 ); }
[2871210]2509        | '(' type_parameter_ptr ')' array_parameter_dimension
[4d51835]2510                { $$ = $2->addArray( $4 ); }
2511        ;
[51b7345]2512
[2871210]2513type_parameter_function:
[4d51835]2514        typedef '(' push parameter_type_list_opt pop ')'        // empty parameter list OBSOLESCENT (see 3)
2515                { $$ = $1->addParamList( $4 ); }
[2871210]2516        | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2517                { $$ = $2->addParamList( $6 ); }
2518        ;
[b87a5ed]2519
[de62360d]2520// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2521// which the type applies, e.g.:
[c11e31c]2522//
[b87a5ed]2523//              sizeof( int );
2524//              sizeof( int [10] );
[c11e31c]2525//
[de62360d]2526// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2527// and functions versus pointers to arrays and functions.
[51b7345]2528
2529abstract_declarator:
[4d51835]2530        abstract_ptr
2531        | abstract_array attribute_list_opt
[1db21619]2532                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2533        | abstract_function attribute_list_opt
[1db21619]2534                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2535        ;
[51b7345]2536
2537abstract_ptr:
[dd51906]2538        ptrref_operator
[4d51835]2539                { $$ = DeclarationNode::newPointer( 0 ); }
[dd51906]2540        | ptrref_operator type_qualifier_list
[4d51835]2541                { $$ = DeclarationNode::newPointer( $2 ); }
[dd51906]2542        | ptrref_operator abstract_declarator
[4d51835]2543                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2544        | ptrref_operator type_qualifier_list abstract_declarator
[4d51835]2545                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2546        | '(' abstract_ptr ')'
2547                { $$ = $2; }
2548        ;
[51b7345]2549
2550abstract_array:
[4d51835]2551        array_dimension
2552        | '(' abstract_ptr ')' array_dimension
2553                { $$ = $2->addArray( $4 ); }
2554        | '(' abstract_array ')' multi_array_dimension          // redundant parenthesis
2555                { $$ = $2->addArray( $4 ); }
2556        | '(' abstract_array ')'                                                        // redundant parenthesis
2557                { $$ = $2; }
2558        ;
[51b7345]2559
2560abstract_function:
[4d51835]2561        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2562                { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2563        | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2564                { $$ = $2->addParamList( $6 ); }
2565        | '(' abstract_function ')'                                                     // redundant parenthesis
2566                { $$ = $2; }
2567        ;
[51b7345]2568
2569array_dimension:
[4d51835]2570                // Only the first dimension can be empty.
[2871210]2571        '[' ']'
[4d51835]2572                { $$ = DeclarationNode::newArray( 0, 0, false ); }
[2871210]2573        | '[' ']' multi_array_dimension
2574                { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
[4d51835]2575        | multi_array_dimension
2576        ;
[51b7345]2577
2578multi_array_dimension:
[4d51835]2579        '[' push assignment_expression pop ']'
2580                { $$ = DeclarationNode::newArray( $3, 0, false ); }
2581        | '[' push '*' pop ']'                                                          // C99
2582                { $$ = DeclarationNode::newVarArray( 0 ); }
2583        | multi_array_dimension '[' push assignment_expression pop ']'
2584                { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2585        | multi_array_dimension '[' push '*' pop ']'            // C99
2586                { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2587        ;
[51b7345]2588
[c11e31c]2589// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2590// identifier to which the type applies, e.g.:
2591//
[b87a5ed]2592//              int f( int );                   // abstract variable parameter; no parameter name specified
2593//              int f( int (int) );             // abstract function-prototype parameter; no parameter name specified
[c11e31c]2594//
[de62360d]2595// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2596// and functions versus pointers to arrays and functions.
[51b7345]2597
2598abstract_parameter_declarator:
[4d51835]2599        abstract_parameter_ptr
2600        | abstract_parameter_array attribute_list_opt
[1db21619]2601                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2602        | abstract_parameter_function attribute_list_opt
[1db21619]2603                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2604        ;
[51b7345]2605
2606abstract_parameter_ptr:
[dd51906]2607        ptrref_operator
[4d51835]2608                { $$ = DeclarationNode::newPointer( 0 ); }
[dd51906]2609        | ptrref_operator type_qualifier_list
[4d51835]2610                { $$ = DeclarationNode::newPointer( $2 ); }
[dd51906]2611        | ptrref_operator abstract_parameter_declarator
[4d51835]2612                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2613        | ptrref_operator type_qualifier_list abstract_parameter_declarator
[4d51835]2614                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2615        | '(' abstract_parameter_ptr ')'
2616                { $$ = $2; }
2617        ;
[51b7345]2618
2619abstract_parameter_array:
[4d51835]2620        array_parameter_dimension
2621        | '(' abstract_parameter_ptr ')' array_parameter_dimension
2622                { $$ = $2->addArray( $4 ); }
2623        | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2624                { $$ = $2->addArray( $4 ); }
2625        | '(' abstract_parameter_array ')'                                      // redundant parenthesis
2626                { $$ = $2; }
2627        ;
[51b7345]2628
2629abstract_parameter_function:
[4d51835]2630        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2631                { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2632        | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2633                { $$ = $2->addParamList( $6 ); }
2634        | '(' abstract_parameter_function ')'                           // redundant parenthesis
2635                { $$ = $2; }
2636        ;
[51b7345]2637
2638array_parameter_dimension:
[4d51835]2639                // Only the first dimension can be empty or have qualifiers.
2640        array_parameter_1st_dimension
2641        | array_parameter_1st_dimension multi_array_dimension
2642                { $$ = $1->addArray( $2 ); }
2643        | multi_array_dimension
2644        ;
[51b7345]2645
[c11e31c]2646// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
2647//
[de62360d]2648//              ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
2649//              a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
[51b7345]2650
2651array_parameter_1st_dimension:
[2871210]2652        '[' ']'
[4d51835]2653                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2654        // multi_array_dimension handles the '[' '*' ']' case
2655        | '[' push type_qualifier_list '*' pop ']'                      // remaining C99
2656                { $$ = DeclarationNode::newVarArray( $3 ); }
2657        | '[' push type_qualifier_list pop ']'
2658                { $$ = DeclarationNode::newArray( 0, $3, false ); }
2659        // multi_array_dimension handles the '[' assignment_expression ']' case
2660        | '[' push type_qualifier_list assignment_expression pop ']'
2661                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2662        | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
2663                { $$ = DeclarationNode::newArray( $5, $4, true ); }
2664        | '[' push type_qualifier_list STATIC assignment_expression pop ']'
2665                { $$ = DeclarationNode::newArray( $5, $3, true ); }
2666        ;
[b87a5ed]2667
[de62360d]2668// This pattern parses a declaration of an abstract variable, i.e., there is no identifier to which the type applies,
2669// e.g.:
[c11e31c]2670//
[b87a5ed]2671//              sizeof( int ); // abstract variable; no identifier name specified
[c11e31c]2672//
[de62360d]2673// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2674// and functions versus pointers to arrays and functions.
[51b7345]2675
2676variable_abstract_declarator:
[4d51835]2677        variable_abstract_ptr
2678        | variable_abstract_array attribute_list_opt
[1db21619]2679                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2680        | variable_abstract_function attribute_list_opt
[1db21619]2681                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2682        ;
[51b7345]2683
2684variable_abstract_ptr:
[dd51906]2685        ptrref_operator
[4d51835]2686                { $$ = DeclarationNode::newPointer( 0 ); }
[dd51906]2687        | ptrref_operator type_qualifier_list
[4d51835]2688                { $$ = DeclarationNode::newPointer( $2 ); }
[dd51906]2689        | ptrref_operator variable_abstract_declarator
[4d51835]2690                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2691        | ptrref_operator type_qualifier_list variable_abstract_declarator
[4d51835]2692                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2693        | '(' variable_abstract_ptr ')'
2694                { $$ = $2; }
2695        ;
[51b7345]2696
2697variable_abstract_array:
[4d51835]2698        array_dimension
2699        | '(' variable_abstract_ptr ')' array_dimension
2700                { $$ = $2->addArray( $4 ); }
2701        | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
2702                { $$ = $2->addArray( $4 ); }
2703        | '(' variable_abstract_array ')'                                       // redundant parenthesis
2704                { $$ = $2; }
2705        ;
[51b7345]2706
2707variable_abstract_function:
[4d51835]2708        '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2709                { $$ = $2->addParamList( $6 ); }
2710        | '(' variable_abstract_function ')'                            // redundant parenthesis
2711                { $$ = $2; }
2712        ;
[b87a5ed]2713
[de62360d]2714// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
2715// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
[b87a5ed]2716
2717new_identifier_parameter_declarator_tuple:                              // CFA
[4d51835]2718        new_identifier_parameter_declarator_no_tuple
2719        | new_abstract_tuple
2720        | type_qualifier_list new_abstract_tuple
2721                { $$ = $2->addQualifiers( $1 ); }
2722        ;
[b87a5ed]2723
2724new_identifier_parameter_declarator_no_tuple:                   // CFA
[4d51835]2725        new_identifier_parameter_ptr
2726        | new_identifier_parameter_array
2727        ;
[b87a5ed]2728
2729new_identifier_parameter_ptr:                                                   // CFA
[dd51906]2730        ptrref_operator type_specifier
[4d51835]2731                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2732        | type_qualifier_list ptrref_operator type_specifier
[4d51835]2733                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
[dd51906]2734        | ptrref_operator new_abstract_function
[4d51835]2735                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2736        | type_qualifier_list ptrref_operator new_abstract_function
[4d51835]2737                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
[dd51906]2738        | ptrref_operator new_identifier_parameter_declarator_tuple
[4d51835]2739                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2740        | type_qualifier_list ptrref_operator new_identifier_parameter_declarator_tuple
[4d51835]2741                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2742        ;
[b87a5ed]2743
2744new_identifier_parameter_array:                                                 // CFA
[de62360d]2745                // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
2746                // shift/reduce conflict with new-style empty (void) function return type.
[2871210]2747        '[' ']' type_specifier
2748                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[4d51835]2749        | new_array_parameter_1st_dimension type_specifier
2750                { $$ = $2->addNewArray( $1 ); }
[2871210]2751        | '[' ']' multi_array_dimension type_specifier
2752                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[4d51835]2753        | new_array_parameter_1st_dimension multi_array_dimension type_specifier
2754                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2755        | multi_array_dimension type_specifier
2756                { $$ = $2->addNewArray( $1 ); }
[2871210]2757        | '[' ']' new_identifier_parameter_ptr
2758                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[4d51835]2759        | new_array_parameter_1st_dimension new_identifier_parameter_ptr
2760                { $$ = $2->addNewArray( $1 ); }
[2871210]2761        | '[' ']' multi_array_dimension new_identifier_parameter_ptr
2762                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[4d51835]2763        | new_array_parameter_1st_dimension multi_array_dimension new_identifier_parameter_ptr
2764                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2765        | multi_array_dimension new_identifier_parameter_ptr
2766                { $$ = $2->addNewArray( $1 ); }
2767        ;
[51b7345]2768
2769new_array_parameter_1st_dimension:
[4d51835]2770        '[' push type_qualifier_list '*' pop ']'                        // remaining C99
2771                { $$ = DeclarationNode::newVarArray( $3 ); }
2772        | '[' push type_qualifier_list assignment_expression pop ']'
2773                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2774        | '[' push declaration_qualifier_list assignment_expression pop ']'
2775                // declaration_qualifier_list must be used because of shift/reduce conflict with
2776                // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
2777                // appear in this context.
2778                { $$ = DeclarationNode::newArray( $4, $3, true ); }
2779        | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
2780                { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
2781        ;
[b87a5ed]2782
[de62360d]2783// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
2784// identifier to which the type applies, e.g.:
[c11e31c]2785//
[b87a5ed]2786//              [int] f( int );                         // abstract variable parameter; no parameter name specified
2787//              [int] f( [int] (int) );         // abstract function-prototype parameter; no parameter name specified
[c11e31c]2788//
2789// These rules need LR(3):
2790//
[2871210]2791//              new_abstract_tuple identifier_or_type_name
2792//              '[' new_parameter_list ']' identifier_or_type_name '(' new_parameter_type_list_opt ')'
[c11e31c]2793//
2794// since a function return type can be syntactically identical to a tuple type:
2795//
[b87a5ed]2796//              [int, int] t;
2797//              [int, int] f( int );
[c11e31c]2798//
[2871210]2799// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
[de62360d]2800// new_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
2801// lookahead. To accomplish this, new_abstract_declarator has an entry point without tuple, and tuple declarations are
2802// duplicated when appearing with new_function_specifier.
[b87a5ed]2803
2804new_abstract_declarator_tuple:                                                  // CFA
[4d51835]2805        new_abstract_tuple
2806        | type_qualifier_list new_abstract_tuple
2807                { $$ = $2->addQualifiers( $1 ); }
2808        | new_abstract_declarator_no_tuple
2809        ;
[b87a5ed]2810
2811new_abstract_declarator_no_tuple:                                               // CFA
[4d51835]2812        new_abstract_ptr
2813        | new_abstract_array
2814        ;
[b87a5ed]2815
2816new_abstract_ptr:                                                                               // CFA
[dd51906]2817        ptrref_operator type_specifier
[4d51835]2818                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2819        | type_qualifier_list ptrref_operator type_specifier
[4d51835]2820                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
[dd51906]2821        | ptrref_operator new_abstract_function
[4d51835]2822                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2823        | type_qualifier_list ptrref_operator new_abstract_function
[4d51835]2824                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
[dd51906]2825        | ptrref_operator new_abstract_declarator_tuple
[4d51835]2826                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2827        | type_qualifier_list ptrref_operator new_abstract_declarator_tuple
[4d51835]2828                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2829        ;
[b87a5ed]2830
2831new_abstract_array:                                                                             // CFA
[de62360d]2832                // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
2833                // empty (void) function return type.
[2871210]2834        '[' ']' type_specifier
2835                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2836        | '[' ']' multi_array_dimension type_specifier
2837                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[4d51835]2838        | multi_array_dimension type_specifier
2839                { $$ = $2->addNewArray( $1 ); }
[2871210]2840        | '[' ']' new_abstract_ptr
2841                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2842        | '[' ']' multi_array_dimension new_abstract_ptr
2843                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[4d51835]2844        | multi_array_dimension new_abstract_ptr
2845                { $$ = $2->addNewArray( $1 ); }
2846        ;
[b87a5ed]2847
2848new_abstract_tuple:                                                                             // CFA
[4d51835]2849        '[' push new_abstract_parameter_list pop ']'
2850                { $$ = DeclarationNode::newTuple( $3 ); }
2851        ;
[b87a5ed]2852
2853new_abstract_function:                                                                  // CFA
[2871210]2854        '[' ']' '(' new_parameter_type_list_opt ')'
2855                { $$ = DeclarationNode::newFunction( 0, DeclarationNode::newTuple( 0 ), $4, 0 ); }
[4d51835]2856        | new_abstract_tuple '(' push new_parameter_type_list_opt pop ')'
2857                { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2858        | new_function_return '(' push new_parameter_type_list_opt pop ')'
2859                { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2860        ;
[b87a5ed]2861
[de62360d]2862// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
2863//    each declaration, and in the specifier-qualifier list in each structure declaration and type name."
[c11e31c]2864//
[de62360d]2865// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
2866//    the declaration specifiers in a declaration is an obsolescent feature."
[c11e31c]2867//
2868// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
2869//    prototype-format parameter type declarators) is an obsolescent feature."
2870//
[de62360d]2871// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
2872//    declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
[51b7345]2873
[c11e31c]2874//************************* MISCELLANEOUS ********************************
[51b7345]2875
[b87a5ed]2876comma_opt:                                                                                              // redundant comma
[4d51835]2877        // empty
2878        | ','
2879        ;
[51b7345]2880
2881assignment_opt:
[4d51835]2882        // empty
2883                { $$ = 0; }
2884        | '=' assignment_expression
2885                { $$ = $2; }
2886        ;
[51b7345]2887
2888%%
[c11e31c]2889// ----end of grammar----
[51b7345]2890
[0da3e2c]2891extern char *yytext;
2892
[5f2f2d7]2893void yyerror( const char * ) {
2894        std::cout << "Error ";
[b87a5ed]2895        if ( yyfilename ) {
[45161b4d]2896                std::cout << "in file " << yyfilename << " ";
[5f2f2d7]2897        } // if
[b1d6dd5]2898        std::cout << "at line " << yylineno << " reading token \"" << (yytext[0] == '\0' ? "EOF" : yytext) << "\"" << std::endl;
[51b7345]2899}
2900
[c11e31c]2901// Local Variables: //
[b87a5ed]2902// mode: c++ //
[de62360d]2903// tab-width: 4 //
[c11e31c]2904// compile-command: "make install" //
2905// End: //
Note: See TracBrowser for help on using the repository browser.