source: src/Parser/parser.yy @ b6fe7e6

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

make constructor expressions work, fix bug with using the wrong TypeEnvironment? on member exprs, remove many unnecessary ctor/dtors from the prelude

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