source: src/Parser/parser.yy @ bf32bb8

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 bf32bb8 was bf32bb8, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

implement transformation for MemberTupleExprs?

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