source: src/Parser/parser.yy @ fdca7c6

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

mark aggregate as generic before parsing aggregate body so generic-type name available in body

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