source: src/Parser/parser.yy @ 58dd019

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 58dd019 was 58dd019, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add asm_name clause to declarations

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