source: src/Parser/parser.yy @ 4a9ccc3

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 4a9ccc3 was 44a81853, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

first attempt at gcc attributes

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