source: src/Parser/parser.yy @ fc39193

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

add support for default initializer in abstract parameter-declaration

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