source: src/Parser/parser.yy @ 6b8643d

new-envwith_gc
Last change on this file since 6b8643d was b048dc3, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

additional intermediate changes

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