source: src/Parser/parser.yy @ 8e5724e

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 8e5724e was 1f44196, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

Merge branch 'master' of plg2:software/cfa/cfa-cc

Conflicts:

src/Parser/parser.cc

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