source: src/Parser/parser.yy @ 9059213

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

small changes to bring me up to date

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