source: src/Parser/parser.yy @ 9b4343e

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 9b4343e was 9c23f31, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

fix conflicts

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