source: src/Parser/parser.yy @ 36e6f10

Last change on this file since 36e6f10 was 36e6f10, checked in by Andrew Beach <ajbeach@…>, 20 months ago

Parser now uses constants from the new ast types.

  • Property mode set to 100644
File size: 175.1 KB
Line 
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//
7// parser.yy --
8//
9// Author           : Peter A. Buhr
10// Created On       : Sat Sep  1 20:22:55 2001
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Oct  3 17:14:12 2023
13// Update Count     : 6396
14//
15
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.
22
23// Acknowledgments to Richard Bilson, Glen Ditchfield, and Rodolfo Gabriel Esteves who all helped when I got stuck with
24// the grammar.
25
26// The root language for this grammar is ANSI99/11 C. All of ANSI99/11 is parsed, except for:
27//
28//   designation with '=' (use ':' instead)
29//
30// This incompatibility is discussed in detail before the "designation" grammar rule.  Most of the syntactic extensions
31// from ANSI90 to ANSI11 C are marked with the comment "C99/C11".
32
33// This grammar also has two levels of extensions. The first extensions cover most of the GCC C extensions. All of the
34// syntactic extensions for GCC C are marked with the comment "GCC". The second extensions are for Cforall (CFA), which
35// fixes several of C's outstanding problems and extends C with many modern language concepts. All of the syntactic
36// extensions for CFA C are marked with the comment "CFA".
37
38%{
39#define YYDEBUG_LEXER_TEXT( yylval )                                    // lexer loads this up each time
40#define YYDEBUG 1                                                                               // get the pretty debugging code to compile
41#define YYERROR_VERBOSE                                                                 // more information in syntax errors
42
43#undef __GNUC_MINOR__
44
45#include <cstdio>
46#include <sstream>
47#include <stack>
48using namespace std;
49
50#include "DeclarationNode.h"                            // for DeclarationNode, ...
51#include "ExpressionNode.h"                             // for ExpressionNode, ...
52#include "InitializerNode.h"                            // for InitializerNode, ...
53#include "ParserTypes.h"
54#include "StatementNode.h"                              // for build_...
55#include "TypedefTable.h"
56#include "TypeData.h"
57#include "Common/SemanticError.h"                                               // error_str
58#include "Common/utility.h"                                                             // for maybeMoveBuild, maybeBuild, CodeLo...
59
60#include "SynTree/Attribute.h"                                                  // for Attribute
61
62// lex uses __null in a boolean context, it's fine.
63#ifdef __clang__
64#pragma GCC diagnostic ignored "-Wparentheses-equality"
65#endif
66
67extern DeclarationNode * parseTree;
68extern ast::Linkage::Spec linkage;
69extern TypedefTable typedefTable;
70
71stack<ast::Linkage::Spec> linkageStack;
72
73bool appendStr( string & to, string & from ) {
74        // 1. Multiple strings are concatenated into a single string but not combined internally. The reason is that
75        //    "\x12" "3" is treated as 2 characters versus 1 because "escape sequences are converted into single members of
76        //    the execution character set just prior to adjacent string literal concatenation" (C11, Section 6.4.5-8). It is
77        //    easier to let the C compiler handle this case.
78        //
79        // 2. String encodings are transformed into canonical form (one encoding at start) so the encoding can be found
80        //    without searching the string, e.g.: "abc" L"def" L"ghi" => L"abc" "def" "ghi". Multiple encodings must match,
81        //    e.g., u"a" U"b" L"c" is disallowed.
82
83        if ( from[0] != '"' ) {                                                         // encoding ?
84                if ( to[0] != '"' ) {                                                   // encoding ?
85                        if ( to[0] != from[0] || to[1] != from[1] ) { // different encodings ?
86                                yyerror( "non-matching string encodings for string-literal concatenation" );
87                                return false;                                                   // parse error, must call YYERROR in action
88                        } else if ( from[1] == '8' ) {
89                                from.erase( 0, 1 );                                             // remove 2nd encoding
90                        } // if
91                } else {
92                        if ( from[1] == '8' ) {                                         // move encoding to start
93                                to = "u8" + to;
94                                from.erase( 0, 1 );                                             // remove 2nd encoding
95                        } else {
96                                to = from[0] + to;
97                        } // if
98                } // if
99                from.erase( 0, 1 );                                                             // remove 2nd encoding
100        } // if
101        to += " " + from;                                                                       // concatenated into single string
102        return true;
103} // appendStr
104
105DeclarationNode * distAttr( DeclarationNode * typeSpec, DeclarationNode * declList ) {
106        // distribute declaration_specifier across all declared variables, e.g., static, const, but not __attribute__.
107        assert( declList );
108        // printf( "distAttr1 typeSpec %p\n", typeSpec ); typeSpec->print( std::cout );
109        DeclarationNode * cl = (new DeclarationNode)->addType( typeSpec );
110        // printf( "distAttr2 cl %p\n", cl ); cl->type->print( std::cout );
111        // cl->type->aggregate.name = cl->type->aggInst.aggregate->aggregate.name;
112
113        for ( DeclarationNode * cur = dynamic_cast<DeclarationNode *>( declList->get_next() ); cur != nullptr; cur = dynamic_cast<DeclarationNode *>( cur->get_next() ) ) {
114                cl->cloneBaseType( cur );
115        } // for
116        declList->addType( cl );
117        // printf( "distAttr3 declList %p\n", declList ); declList->print( std::cout, 0 );
118        return declList;
119} // distAttr
120
121void distExt( DeclarationNode * declaration ) {
122        // distribute EXTENSION across all declarations
123        for ( DeclarationNode *iter = declaration; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
124                iter->set_extension( true );
125        } // for
126} // distExt
127
128void distInl( DeclarationNode * declaration ) {
129        // distribute INLINE across all declarations
130        for ( DeclarationNode *iter = declaration; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
131                iter->set_inLine( true );
132        } // for
133} // distInl
134
135void distQual( DeclarationNode * declaration, DeclarationNode * qualifiers ) {
136        // distribute qualifiers across all non-variable declarations in a distribution statemement
137        for ( DeclarationNode * iter = declaration; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
138                // SKULLDUGGERY: Distributions are parsed inside out, so qualifiers are added to declarations inside out. Since
139                // addQualifiers appends to the back of the list, the forall clauses are in the wrong order (right to left). To
140                // get the qualifiers in the correct order and still use addQualifiers (otherwise, 90% of addQualifiers has to
141                // be copied to add to front), the appropriate forall pointers are interchanged before calling addQualifiers.
142                DeclarationNode * clone = qualifiers->clone();
143                if ( qualifiers->type ) {                                               // forall clause ? (handles SC)
144                        if ( iter->type->kind == TypeData::Aggregate ) { // struct/union ?
145                                swap( clone->type->forall, iter->type->aggregate.params );
146                                iter->addQualifiers( clone );
147                        } else if ( iter->type->kind == TypeData::AggregateInst && iter->type->aggInst.aggregate->aggregate.body ) { // struct/union ?
148                                // Create temporary node to hold aggregate, call addQualifiers as above, then put nodes back together.
149                                DeclarationNode newnode;
150                                swap( newnode.type, iter->type->aggInst.aggregate );
151                                swap( clone->type->forall, newnode.type->aggregate.params );
152                                newnode.addQualifiers( clone );
153                                swap( newnode.type, iter->type->aggInst.aggregate );
154                        } else if ( iter->type->kind == TypeData::Function ) { // routines ?
155                                swap( clone->type->forall, iter->type->forall );
156                                iter->addQualifiers( clone );
157                        } // if
158                } else {                                                                                // just SC qualifiers
159                        iter->addQualifiers( clone );
160                } // if
161        } // for
162        delete qualifiers;
163} // distQual
164
165// There is an ambiguity for inline generic-routine return-types and generic routines.
166//   forall( otype T ) struct S { int i; } bar( T ) {}
167// Does the forall bind to the struct or the routine, and how would it be possible to explicitly specify the binding.
168//   forall( otype T ) struct S { int T; } forall( otype W ) bar( W ) {}
169// Currently, the forall is associated with the routine, and the generic type has to be separately defined:
170//   forall( otype T ) struct S { int T; };
171//   forall( otype W ) bar( W ) {}
172
173void rebindForall( DeclarationNode * declSpec, DeclarationNode * funcDecl ) {
174        if ( declSpec->type->kind == TypeData::Aggregate ) { // ignore aggregate definition
175                funcDecl->type->forall = declSpec->type->aggregate.params; // move forall from aggregate to function type
176                declSpec->type->aggregate.params = nullptr;
177        } // if
178} // rebindForall
179
180string * build_postfix_name( string * name ) {
181        *name = string("__postfix_func_") + *name;
182        return name;
183} // build_postfix_name
184
185DeclarationNode * fieldDecl( DeclarationNode * typeSpec, DeclarationNode * fieldList ) {
186        if ( ! fieldList ) {                                                            // field declarator ?
187                if ( ! ( typeSpec->type && (typeSpec->type->kind == TypeData::Aggregate || typeSpec->type->kind == TypeData::Enum) ) ) {
188                        stringstream ss;
189                        // printf( "fieldDecl1 typeSpec %p\n", typeSpec ); typeSpec->type->print( std::cout );
190                        SemanticWarning( yylloc, Warning::SuperfluousDecl, ss.str().c_str() );
191                        return nullptr;
192                } // if
193                // printf( "fieldDecl2 typeSpec %p\n", typeSpec ); typeSpec->type->print( std::cout );
194                fieldList = DeclarationNode::newName( nullptr );
195        } // if
196//      return distAttr( typeSpec, fieldList );                         // mark all fields in list
197
198        // printf( "fieldDecl3 typeSpec %p\n", typeSpec ); typeSpec->print( std::cout, 0 );
199        DeclarationNode * temp = distAttr( typeSpec, fieldList );                               // mark all fields in list
200        // printf( "fieldDecl4 temp %p\n", temp ); temp->print( std::cout, 0 );
201        return temp;
202} // fieldDecl
203
204#define NEW_ZERO new ExpressionNode( build_constantInteger( yylloc, *new string( "0" ) ) )
205#define NEW_ONE  new ExpressionNode( build_constantInteger( yylloc, *new string( "1" ) ) )
206#define UPDOWN( compop, left, right ) (compop == OperKinds::LThan || compop == OperKinds::LEThan ? left : right)
207#define MISSING_ANON_FIELD "syntax error, missing loop fields with an anonymous loop index is meaningless as loop index is unavailable in loop body."
208#define MISSING_LOW "syntax error, missing low value for up-to range so index is uninitialized."
209#define MISSING_HIGH "syntax error, missing high value for down-to range so index is uninitialized."
210
211static ForCtrl * makeForCtrl(
212                const CodeLocation & location,
213                DeclarationNode * init,
214                enum OperKinds compop,
215                ExpressionNode * comp,
216                ExpressionNode * inc ) {
217        // Wrap both comp/inc if they are non-null.
218        if ( comp ) comp = new ExpressionNode( build_binary_val( location,
219                compop,
220                new ExpressionNode( build_varref( location, new string( *init->name ) ) ),
221                comp ) );
222        if ( inc ) inc = new ExpressionNode( build_binary_val( location,
223                // choose += or -= for upto/downto
224                compop == OperKinds::LThan || compop == OperKinds::LEThan ? OperKinds::PlusAssn : OperKinds::MinusAssn,
225                new ExpressionNode( build_varref( location, new string( *init->name ) ) ),
226                inc ) );
227        // The StatementNode call frees init->name, it must happen later.
228        return new ForCtrl( new StatementNode( init ), comp, inc );
229}
230
231ForCtrl * forCtrl( const CodeLocation & location, DeclarationNode * index, ExpressionNode * start, enum OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
232        if ( index->initializer ) {
233                SemanticError( yylloc, "syntax error, direct initialization disallowed. Use instead: type var; initialization ~ comparison ~ increment." );
234        } // if
235        if ( index->next ) {
236                SemanticError( yylloc, "syntax error, multiple loop indexes disallowed in for-loop declaration." );
237        } // if
238        DeclarationNode * initDecl = index->addInitializer( new InitializerNode( start ) );
239        return makeForCtrl( location, initDecl, compop, comp, inc );
240} // forCtrl
241
242ForCtrl * forCtrl( const CodeLocation & location, ExpressionNode * type, string * index, ExpressionNode * start, enum OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
243        ast::ConstantExpr * constant = dynamic_cast<ast::ConstantExpr *>(type->expr.get());
244        if ( constant && (constant->rep == "0" || constant->rep == "1") ) {
245                type = new ExpressionNode( new ast::CastExpr( location, maybeMoveBuild(type), new ast::BasicType( ast::BasicType::SignedInt ) ) );
246        } // if
247        DeclarationNode * initDecl = distAttr(
248                DeclarationNode::newTypeof( type, true ),
249                DeclarationNode::newName( index )->addInitializer( new InitializerNode( start ) )
250        );
251        return makeForCtrl( location, initDecl, compop, comp, inc );
252} // forCtrl
253
254ForCtrl * forCtrl( const CodeLocation & location, ExpressionNode * type, ExpressionNode * index, ExpressionNode * start, enum OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
255        if ( auto identifier = dynamic_cast<ast::NameExpr *>(index->expr.get()) ) {
256                return forCtrl( location, type, new string( identifier->name ), start, compop, comp, inc );
257        } else if ( auto commaExpr = dynamic_cast<ast::CommaExpr *>( index->expr.get() ) ) {
258                if ( auto identifier = commaExpr->arg1.as<ast::NameExpr>() ) {
259                        return forCtrl( location, type, new string( identifier->name ), start, compop, comp, inc );
260                } else {
261                        SemanticError( yylloc, "syntax error, loop-index name missing. Expression disallowed." ); return nullptr;
262                } // if
263        } else {
264                SemanticError( yylloc, "syntax error, loop-index name missing. Expression disallowed. ." ); return nullptr;
265        } // if
266} // forCtrl
267
268static void IdentifierBeforeIdentifier( string & identifier1, string & identifier2, const char * kind ) {
269        SemanticError( yylloc, ::toString( "syntax error, adjacent identifiers \"", identifier1, "\" and \"", identifier2, "\" are not meaningful in a", kind, ".\n"
270                                   "Possible cause is misspelled type name or missing generic parameter." ) );
271} // IdentifierBeforeIdentifier
272
273static void IdentifierBeforeType( string & identifier, const char * kind ) {
274        SemanticError( yylloc, ::toString( "syntax error, identifier \"", identifier, "\" cannot appear before a ", kind, ".\n"
275                                   "Possible cause is misspelled storage/CV qualifier, misspelled typename, or missing generic parameter." ) );
276} // IdentifierBeforeType
277
278bool forall = false;                                                                    // aggregate have one or more forall qualifiers ?
279
280// https://www.gnu.org/software/bison/manual/bison.html#Location-Type
281#define YYLLOC_DEFAULT(Cur, Rhs, N)                                                                                             \
282if ( N ) {                                                                                                                                              \
283        (Cur).first_line   = YYRHSLOC( Rhs, 1 ).first_line;                                                     \
284        (Cur).first_column = YYRHSLOC( Rhs, 1 ).first_column;                                           \
285        (Cur).last_line    = YYRHSLOC( Rhs, N ).last_line;                                                      \
286        (Cur).last_column  = YYRHSLOC( Rhs, N ).last_column;                                            \
287        (Cur).filename     = YYRHSLOC( Rhs, 1 ).filename;                                                       \
288} else {                                                                                                                                                \
289        (Cur).first_line   = (Cur).last_line = YYRHSLOC( Rhs, 0 ).last_line;            \
290        (Cur).first_column = (Cur).last_column = YYRHSLOC( Rhs, 0 ).last_column;        \
291        (Cur).filename     = YYRHSLOC( Rhs, 0 ).filename;                                                       \
292}
293%}
294
295%define parse.error verbose
296
297// Types declaration for productions
298
299%union {
300        Token tok;
301        ExpressionNode * expr;
302        DeclarationNode * decl;
303        ast::AggregateDecl::Aggregate aggKey;
304        ast::TypeDecl::Kind tclass;
305        StatementNode * stmt;
306        ClauseNode * clause;
307        ast::WaitForStmt * wfs;
308    ast::WaitUntilStmt::ClauseNode * wucn;
309        CondCtl * ifctl;
310        ForCtrl * forctl;
311        LabelNode * labels;
312        InitializerNode * init;
313        OperKinds oper;
314        std::string * str;
315        bool is_volatile;
316        EnumHiding enum_hiding;
317        ast::ExceptionKind except_kind;
318        ast::GenericExpr * genexpr;
319}
320
321// ************************ TERMINAL TOKENS ********************************
322
323// keywords
324%token TYPEDEF
325%token EXTERN STATIC AUTO REGISTER
326%token THREADLOCALGCC THREADLOCALC11                                    // GCC, C11
327%token INLINE FORTRAN                                                                   // C99, extension ISO/IEC 9899:1999 Section J.5.9(1)
328%token NORETURN                                                                                 // C11
329%token CONST VOLATILE
330%token RESTRICT                                                                                 // C99
331%token ATOMIC                                                                                   // C11
332%token FORALL MUTEX VIRTUAL VTABLE COERCE                               // CFA
333%token VOID CHAR SHORT INT LONG FLOAT DOUBLE SIGNED UNSIGNED
334%token BOOL COMPLEX IMAGINARY                                                   // C99
335%token INT128 UINT128 uuFLOAT80 uuFLOAT128                              // GCC
336%token uFLOAT16 uFLOAT32 uFLOAT32X uFLOAT64 uFLOAT64X uFLOAT128 // GCC
337%token DECIMAL32 DECIMAL64 DECIMAL128                                   // GCC
338%token ZERO_T ONE_T                                                                             // CFA
339%token SIZEOF TYPEOF VA_LIST VA_ARG AUTO_TYPE                   // GCC
340%token OFFSETOF BASETYPEOF TYPEID                                               // CFA
341%token ENUM STRUCT UNION
342%token EXCEPTION                                                                                // CFA
343%token GENERATOR COROUTINE MONITOR THREAD                               // CFA
344%token OTYPE FTYPE DTYPE TTYPE TRAIT                                    // CFA
345// %token RESUME                                                                                        // CFA
346%token LABEL                                                                                    // GCC
347%token SUSPEND                                                                                  // CFA
348%token ATTRIBUTE EXTENSION                                                              // GCC
349%token IF ELSE SWITCH CASE DEFAULT DO WHILE FOR BREAK CONTINUE GOTO RETURN
350%token CHOOSE FALLTHRU FALLTHROUGH WITH WHEN WAITFOR WAITUNTIL // CFA
351%token CORUN COFOR
352%token DISABLE ENABLE TRY THROW THROWRESUME AT                  // CFA
353%token ASM                                                                                              // C99, extension ISO/IEC 9899:1999 Section J.5.10(1)
354%token ALIGNAS ALIGNOF GENERIC STATICASSERT                             // C11
355
356// names and constants: lexer differentiates between identifier and typedef names
357%token<tok> IDENTIFIER          TYPEDIMname             TYPEDEFname             TYPEGENname
358%token<tok> TIMEOUT                     WAND    WOR                     CATCH                   RECOVER                 CATCHRESUME             FIXUP           FINALLY         // CFA
359%token<tok> INTEGERconstant     CHARACTERconstant       STRINGliteral
360%token<tok> DIRECTIVE
361// Floating point constant is broken into three kinds of tokens because of the ambiguity with tuple indexing and
362// overloading constants 0/1, e.g., x.1 is lexed as (x)(.1), where (.1) is a factional constant, but is semantically
363// converted into the tuple index (.)(1). e.g., 3.x
364%token<tok>     FLOATING_DECIMALconstant        FLOATING_FRACTIONconstant       FLOATINGconstant
365
366// multi-character operators
367%token ARROW                                                                                    // ->
368%token ICR DECR                                                                                 // ++   --
369%token LS RS                                                                                    // <<   >>
370%token LE GE EQ NE                                                                              // <=   >=      ==      !=
371%token ANDAND OROR                                                                              // &&   ||
372%token ELLIPSIS                                                                                 // ...
373
374%token EXPassign        MULTassign      DIVassign       MODassign       // \=   *=      /=      %=
375%token PLUSassign       MINUSassign                                                     // +=   -=
376%token LSassign         RSassign                                                        // <<=  >>=
377%token ANDassign        ERassign        ORassign                                // &=   ^=      |=
378
379%token ErangeUpEq       ErangeDown      ErangeDownEq                    // ~=   -~      -~=
380%token ATassign                                                                                 // @=
381
382%type<tok> identifier                                   identifier_at                           identifier_or_type_name         attr_name
383%type<tok> quasi_keyword
384%type<expr> string_literal
385%type<str> string_literal_list
386
387%type<enum_hiding> hide_opt                             visible_hide_opt
388
389// expressions
390%type<expr> constant
391%type<expr> tuple                                               tuple_expression_list
392%type<oper> ptrref_operator                             unary_operator                          assignment_operator                     simple_assignment_operator      compound_assignment_operator
393%type<expr> primary_expression                  postfix_expression                      unary_expression
394%type<expr> cast_expression_list                cast_expression                         exponential_expression          multiplicative_expression       additive_expression
395%type<expr> shift_expression                    relational_expression           equality_expression
396%type<expr> AND_expression                              exclusive_OR_expression         inclusive_OR_expression
397%type<expr> logical_AND_expression              logical_OR_expression
398%type<expr> conditional_expression              constant_expression                     assignment_expression           assignment_expression_opt
399%type<expr> comma_expression                    comma_expression_opt
400%type<expr> argument_expression_list_opt argument_expression_list       argument_expression                     default_initializer_opt
401%type<ifctl> conditional_declaration
402%type<forctl> for_control_expression    for_control_expression_list
403%type<oper> upupeq updown updowneq downupdowneq
404%type<expr> subrange
405%type<decl> asm_name_opt
406%type<expr> asm_operands_opt                    asm_operands_list                       asm_operand
407%type<labels> label_list
408%type<expr> asm_clobbers_list_opt
409%type<is_volatile> asm_volatile_opt
410%type<expr> handler_predicate_opt
411%type<genexpr> generic_association              generic_assoc_list
412
413// statements
414%type<stmt> statement                                   labeled_statement                       compound_statement
415%type<stmt> statement_decl                              statement_decl_list                     statement_list_nodecl
416%type<stmt> selection_statement                 if_statement
417%type<clause> switch_clause_list_opt    switch_clause_list
418%type<expr> case_value
419%type<clause> case_clause                               case_value_list                         case_label      case_label_list
420%type<stmt> iteration_statement                 jump_statement
421%type<stmt> expression_statement                asm_statement
422%type<stmt> with_statement
423%type<expr> with_clause_opt
424%type<stmt> corun_statement                             cofor_statement
425%type<stmt> exception_statement
426%type<clause> handler_clause                    finally_clause
427%type<except_kind> handler_key
428%type<stmt> mutex_statement
429%type<expr> when_clause                                 when_clause_opt                         waitfor         waituntil               timeout
430%type<stmt> waitfor_statement                   waituntil_statement
431%type<wfs> wor_waitfor_clause
432%type<wucn> waituntil_clause                    wand_waituntil_clause       wor_waituntil_clause
433
434// declarations
435%type<decl> abstract_declarator abstract_ptr abstract_array abstract_function array_dimension multi_array_dimension
436%type<decl> abstract_parameter_declarator_opt abstract_parameter_declarator abstract_parameter_ptr abstract_parameter_array abstract_parameter_function array_parameter_dimension array_parameter_1st_dimension
437%type<decl> abstract_parameter_declaration
438
439%type<aggKey> aggregate_key aggregate_data aggregate_control
440%type<decl> aggregate_type aggregate_type_nobody
441
442%type<decl> assertion assertion_list assertion_list_opt
443
444%type<expr> bit_subrange_size_opt bit_subrange_size
445
446%type<decl> basic_declaration_specifier basic_type_name basic_type_specifier direct_type indirect_type
447%type<decl> vtable vtable_opt default_opt
448
449%type<decl> trait_declaration trait_declaration_list trait_declaring_list trait_specifier
450
451%type<decl> declaration declaration_list declaration_list_opt declaration_qualifier_list
452%type<decl> declaration_specifier declaration_specifier_nobody declarator declaring_list
453
454%type<decl> elaborated_type elaborated_type_nobody
455
456%type<decl> enumerator_list enum_type enum_type_nobody
457%type<init> enumerator_value_opt
458
459%type<decl> external_definition external_definition_list external_definition_list_opt
460
461%type<decl> exception_declaration
462
463%type<decl> field_declaration_list_opt field_declaration field_declaring_list_opt field_declarator field_abstract_list_opt field_abstract
464%type<expr> field field_name_list field_name fraction_constants_opt
465
466%type<decl> external_function_definition function_definition function_array function_declarator function_no_ptr function_ptr
467
468%type<decl> identifier_parameter_declarator identifier_parameter_ptr identifier_parameter_array identifier_parameter_function
469%type<decl> identifier_list
470
471%type<decl> cfa_abstract_array cfa_abstract_declarator_no_tuple cfa_abstract_declarator_tuple
472%type<decl> cfa_abstract_function cfa_abstract_parameter_declaration cfa_abstract_parameter_list
473%type<decl> cfa_abstract_ptr cfa_abstract_tuple
474
475%type<decl> cfa_array_parameter_1st_dimension
476
477%type<decl> cfa_trait_declaring_list cfa_declaration cfa_field_declaring_list cfa_field_abstract_list
478%type<decl> cfa_function_declaration cfa_function_return cfa_function_specifier
479
480%type<decl> cfa_identifier_parameter_array cfa_identifier_parameter_declarator_no_tuple
481%type<decl> cfa_identifier_parameter_declarator_tuple cfa_identifier_parameter_ptr
482
483%type<decl> cfa_parameter_declaration cfa_parameter_list cfa_parameter_ellipsis_list_opt
484
485%type<decl> cfa_typedef_declaration cfa_variable_declaration cfa_variable_specifier
486
487%type<decl> c_declaration static_assert
488%type<decl> KR_function_declarator KR_function_no_ptr KR_function_ptr KR_function_array
489%type<decl> KR_parameter_list KR_parameter_list_opt
490
491%type<decl> parameter_declaration parameter_list parameter_type_list_opt
492
493%type<decl> paren_identifier paren_type
494
495%type<decl> storage_class storage_class_list
496
497%type<decl> sue_declaration_specifier sue_declaration_specifier_nobody sue_type_specifier sue_type_specifier_nobody
498
499%type<tclass> type_class new_type_class
500%type<decl> type_declarator type_declarator_name type_declaring_list
501
502%type<decl> type_declaration_specifier type_type_specifier type_name typegen_name
503%type<decl> typedef_name typedef_declaration typedef_expression
504
505%type<decl> variable_type_redeclarator variable_type_ptr variable_type_array variable_type_function
506%type<decl> general_function_declarator function_type_redeclarator function_type_array function_type_no_ptr function_type_ptr
507
508%type<decl> type_parameter_redeclarator type_parameter_ptr type_parameter_array type_parameter_function
509
510%type<decl> type type_no_function
511%type<decl> type_parameter type_parameter_list type_initializer_opt
512
513%type<expr> type_parameters_opt type_list array_type_list
514
515%type<decl> type_qualifier type_qualifier_name forall type_qualifier_list_opt type_qualifier_list
516%type<decl> type_specifier type_specifier_nobody
517
518%type<decl> variable_declarator variable_ptr variable_array variable_function
519%type<decl> variable_abstract_declarator variable_abstract_ptr variable_abstract_array variable_abstract_function
520
521%type<decl> attribute_list_opt attribute_list attribute attribute_name_list attribute_name
522
523// initializers
524%type<init>  initializer initializer_list_opt initializer_opt
525
526// designators
527%type<expr>  designator designator_list designation
528
529
530// Handle shift/reduce conflict for dangling else by shifting the ELSE token. For example, this string is ambiguous:
531//   .---------.                                matches IF '(' comma_expression ')' statement . (reduce)
532//   if ( C ) S1 else S2
533//   `-----------------'                matches IF '(' comma_expression ')' statement . (shift) ELSE statement */
534// Similar issues exit with the waitfor statement.
535
536// Order of these lines matters (low-to-high precedence). THEN is left associative over WAND/WOR/TIMEOUT/ELSE, WAND/WOR
537// is left associative over TIMEOUT/ELSE, and TIMEOUT is left associative over ELSE.
538%precedence THEN                // rule precedence for IF/WAITFOR statement
539%precedence ANDAND              // token precedence for start of WAND in WAITFOR statement
540%precedence WAND                // token precedence for start of WAND in WAITFOR statement
541%precedence OROR                // token precedence for start of WOR in WAITFOR statement
542%precedence WOR                 // token precedence for start of WOR in WAITFOR statement
543%precedence TIMEOUT             // token precedence for start of TIMEOUT in WAITFOR statement
544%precedence CATCH               // token precedence for start of TIMEOUT in WAITFOR statement
545%precedence RECOVER             // token precedence for start of TIMEOUT in WAITFOR statement
546%precedence CATCHRESUME // token precedence for start of TIMEOUT in WAITFOR statement
547%precedence FIXUP               // token precedence for start of TIMEOUT in WAITFOR statement
548%precedence FINALLY             // token precedence for start of TIMEOUT in WAITFOR statement
549%precedence ELSE                // token precedence for start of else clause in IF/WAITFOR statement
550
551
552// Handle shift/reduce conflict for generic type by shifting the '(' token. For example, this string is ambiguous:
553//   forall( otype T ) struct Foo { T v; };
554//       .-----.                                matches pointer to function returning a generic (which is impossible without a type)
555//   Foo ( *fp )( int );
556//   `---'                                              matches start of TYPEGENname '('
557// must be:
558//   Foo( int ) ( *fp )( int );
559// The same problem occurs here:
560//   forall( otype T ) struct Foo { T v; } ( *fp )( int );
561// must be:
562//   forall( otype T ) struct Foo { T v; } ( int ) ( *fp )( int );
563
564// Order of these lines matters (low-to-high precedence).
565%precedence TYPEGENname
566%precedence '}'
567%precedence '('
568
569// %precedence RESUME
570// %precedence '{'
571// %precedence ')'
572
573%locations                                                                                              // support location tracking for error messages
574
575%start translation_unit                                                                 // parse-tree root
576
577%%
578// ************************ Namespace Management ********************************
579
580// The C grammar is not context free because it relies on the distinct terminal symbols "identifier" and "TYPEDEFname",
581// which are lexically identical.
582//
583//   typedef int foo; // identifier foo must now be scanned as TYPEDEFname
584//   foo f;           // to allow it to appear in this context
585//
586// While it may be possible to write a purely context-free grammar, such a grammar would obscure the relationship
587// between syntactic and semantic constructs.  Cforall compounds this problem by introducing type names local to the
588// scope of a declaration (for instance, those introduced through "forall" qualifiers), and by introducing "type
589// generators" -- parameterized types.  This latter type name creates a third class of identifiers, "TYPEGENname", which
590// must be distinguished by the lexical scanner.
591//
592// Since the scanner cannot distinguish among the different classes of identifiers without some context information,
593// there is a type table (typedefTable), which holds type names and identifiers that override type names, for each named
594// scope. During parsing, semantic actions update the type table by adding new identifiers in the current scope. For
595// each context that introduces a name scope, a new level is created in the type table and that level is popped on
596// exiting the scope.  Since type names can be local to a particular declaration, each declaration is itself a scope.
597// This requires distinguishing between type names that are local to the current declaration scope and those that
598// persist past the end of the declaration (i.e., names defined in "typedef" or "otype" declarations).
599//
600// The non-terminals "push" and "pop" denote the opening and closing of named scopes. Every push has a matching pop in
601// the production rule. There are multiple lists of declarations, where each declaration is a named scope, so pop/push
602// around the list separator.
603//
604//  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
605//      push               pop   push                   pop
606
607push:
608                { typedefTable.enterScope(); }
609        ;
610
611pop:
612                { typedefTable.leaveScope(); }
613        ;
614
615// ************************ CONSTANTS ********************************
616
617constant:
618                // ENUMERATIONconstant is not included here; it is treated as a variable with type "enumeration constant".
619        INTEGERconstant                                                         { $$ = new ExpressionNode( build_constantInteger( yylloc, *$1 ) ); }
620        | FLOATING_DECIMALconstant                                      { $$ = new ExpressionNode( build_constantFloat( yylloc, *$1 ) ); }
621        | FLOATING_FRACTIONconstant                                     { $$ = new ExpressionNode( build_constantFloat( yylloc, *$1 ) ); }
622        | FLOATINGconstant                                                      { $$ = new ExpressionNode( build_constantFloat( yylloc, *$1 ) ); }
623        | CHARACTERconstant                                                     { $$ = new ExpressionNode( build_constantChar( yylloc, *$1 ) ); }
624        ;
625
626quasi_keyword:                                                                                  // CFA
627        TIMEOUT
628        | WAND
629        | WOR
630        | CATCH
631        | RECOVER
632        | CATCHRESUME
633        | FIXUP
634        | FINALLY
635        ;
636
637identifier:
638        IDENTIFIER
639        | quasi_keyword
640        ;
641
642identifier_at:
643        identifier
644        | '@'                                                                                           // CFA
645                { Token tok = { new string( DeclarationNode::anonymous.newName() ), yylval.tok.loc }; $$ = tok; }
646        ;
647
648string_literal:
649        string_literal_list                                                     { $$ = new ExpressionNode( build_constantStr( yylloc, *$1 ) ); }
650        ;
651
652string_literal_list:                                                                    // juxtaposed strings are concatenated
653        STRINGliteral                                                           { $$ = $1; } // conversion from tok to str
654        | string_literal_list STRINGliteral
655                {
656                        if ( ! appendStr( *$1, *$2 ) ) YYERROR;         // append 2nd juxtaposed string to 1st
657                        delete $2;                                                                      // allocated by lexer
658                        $$ = $1;                                                                        // conversion from tok to str
659                }
660        ;
661
662// ************************ EXPRESSIONS ********************************
663
664primary_expression:
665        IDENTIFIER                                                                                      // typedef name cannot be used as a variable name
666                { $$ = new ExpressionNode( build_varref( yylloc, $1 ) ); }
667        | quasi_keyword
668                { $$ = new ExpressionNode( build_varref( yylloc, $1 ) ); }
669        | TYPEDIMname                                                                           // CFA, generic length argument
670                // { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( DeclarationNode::newFromTypedef( $1 ) ) ) ); }
671                // { $$ = new ExpressionNode( build_varref( $1 ) ); }
672                { $$ = new ExpressionNode( build_dimensionref( yylloc, $1 ) ); }
673        | tuple
674        | '(' comma_expression ')'
675                { $$ = $2; }
676        | '(' compound_statement ')'                                            // GCC, lambda expression
677                { $$ = new ExpressionNode( new ast::StmtExpr( yylloc, dynamic_cast<ast::CompoundStmt *>( maybeMoveBuild( $2 ) ) ) ); }
678        | type_name '.' identifier                                                      // CFA, nested type
679                { $$ = new ExpressionNode( build_qualified_expr( yylloc, $1, build_varref( yylloc, $3 ) ) ); }
680        | type_name '.' '[' field_name_list ']'                         // CFA, nested type / tuple field selector
681                { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
682        | GENERIC '(' assignment_expression ',' generic_assoc_list ')' // C11
683                {
684                        // add the missing control expression to the GenericExpr and return it
685                        $5->control = maybeMoveBuild( $3 );
686                        $$ = new ExpressionNode( $5 );
687                }
688        // | RESUME '(' comma_expression ')'
689        //      { SemanticError( yylloc, "Resume expression is currently unimplemented." ); $$ = nullptr; }
690        // | RESUME '(' comma_expression ')' compound_statement
691        //      { SemanticError( yylloc, "Resume expression is currently unimplemented." ); $$ = nullptr; }
692        | IDENTIFIER IDENTIFIER                                                         // invalid syntax rule
693                { IdentifierBeforeIdentifier( *$1.str, *$2.str, "n expression" ); $$ = nullptr; }
694        | IDENTIFIER type_qualifier                                                     // invalid syntax rule
695                { IdentifierBeforeType( *$1.str, "type qualifier" ); $$ = nullptr; }
696        | IDENTIFIER storage_class                                                      // invalid syntax rule
697                { IdentifierBeforeType( *$1.str, "storage class" ); $$ = nullptr; }
698        | IDENTIFIER basic_type_name                                            // invalid syntax rule
699                { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
700        | IDENTIFIER TYPEDEFname                                                        // invalid syntax rule
701                { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
702        | IDENTIFIER TYPEGENname                                                        // invalid syntax rule
703                { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
704        ;
705
706generic_assoc_list:                                                                             // C11
707        generic_association
708        | generic_assoc_list ',' generic_association
709                {
710                        // steal the association node from the singleton and delete the wrapper
711                        assert( 1 == $3->associations.size() );
712                        $1->associations.push_back( $3->associations.front() );
713                        delete $3;
714                        $$ = $1;
715                }
716        ;
717
718generic_association:                                                                    // C11
719        type_no_function ':' assignment_expression
720                {
721                        // create a GenericExpr wrapper with one association pair
722                        $$ = new ast::GenericExpr( yylloc, nullptr, { { maybeMoveBuildType( $1 ), maybeMoveBuild( $3 ) } } );
723                }
724        | DEFAULT ':' assignment_expression
725                { $$ = new ast::GenericExpr( yylloc, nullptr, { { maybeMoveBuild( $3 ) } } ); }
726        ;
727
728postfix_expression:
729        primary_expression
730        | postfix_expression '[' assignment_expression ',' tuple_expression_list ']'
731                // Historic, transitional: Disallow commas in subscripts.
732                // Switching to this behaviour may help check if a C compatibilty case uses comma-exprs in subscripts.
733                // Current: Commas in subscripts make tuples.
734                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, $1, new ExpressionNode( build_tuple( yylloc, (ExpressionNode *)($3->set_last( $5 ) ) )) ) ); }
735        | postfix_expression '[' assignment_expression ']'
736                // CFA, comma_expression disallowed in this context because it results in a common user error: subscripting a
737                // matrix with x[i,j] instead of x[i][j]. While this change is not backwards compatible, there seems to be
738                // little advantage to this feature and many disadvantages. It is possible to write x[(i,j)] in CFA, which is
739                // equivalent to the old x[i,j].
740                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, $1, $3 ) ); }
741        | constant '[' assignment_expression ']'                        // 3[a], 'a'[a], 3.5[a]
742                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, $1, $3 ) ); }
743        | string_literal '[' assignment_expression ']'          // "abc"[3], 3["abc"]
744                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, $1, $3 ) ); }
745        | postfix_expression '{' argument_expression_list_opt '}' // CFA, constructor call
746                {
747                        Token fn;
748                        fn.str = new std::string( "?{}" );                      // location undefined - use location of '{'?
749                        $$ = new ExpressionNode( new ast::ConstructorExpr( yylloc, build_func( yylloc, new ExpressionNode( build_varref( yylloc, fn ) ), (ExpressionNode *)( $1 )->set_last( $3 ) ) ) );
750                }
751        | postfix_expression '(' argument_expression_list_opt ')'
752                { $$ = new ExpressionNode( build_func( yylloc, $1, $3 ) ); }
753        | VA_ARG '(' primary_expression ',' declaration_specifier_nobody abstract_parameter_declarator_opt ')'
754                // { SemanticError( yylloc, "va_arg is currently unimplemented." ); $$ = nullptr; }
755                { $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, new string( "__builtin_va_arg") ) ),
756                                                                                           (ExpressionNode *)($3->set_last( (ExpressionNode *)($6 ? $6->addType( $5 ) : $5) )) ) ); }
757        | postfix_expression '`' identifier                                     // CFA, postfix call
758                { $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, build_postfix_name( $3 ) ) ), $1 ) ); }
759        | constant '`' identifier                                                       // CFA, postfix call
760                { $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, build_postfix_name( $3 ) ) ), $1 ) ); }
761        | string_literal '`' identifier                                         // CFA, postfix call
762                { $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, build_postfix_name( $3 ) ) ), $1 ) ); }
763        | postfix_expression '.' identifier
764                { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_varref( yylloc, $3 ) ) ); }
765        | postfix_expression '.' INTEGERconstant                        // CFA, tuple index
766                { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_constantInteger( yylloc, *$3 ) ) ); }
767        | postfix_expression FLOATING_FRACTIONconstant          // CFA, tuple index
768                { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_field_name_FLOATING_FRACTIONconstant( yylloc, *$2 ) ) ); }
769        | postfix_expression '.' '[' field_name_list ']'        // CFA, tuple field selector
770                { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_tuple( yylloc, $4 ) ) ); }
771        | postfix_expression '.' aggregate_control
772                { $$ = new ExpressionNode( build_keyword_cast( yylloc, $3, $1 ) ); }
773        | postfix_expression ARROW identifier
774                { $$ = new ExpressionNode( build_pfieldSel( yylloc, $1, build_varref( yylloc, $3 ) ) ); }
775        | postfix_expression ARROW INTEGERconstant                      // CFA, tuple index
776                { $$ = new ExpressionNode( build_pfieldSel( yylloc, $1, build_constantInteger( yylloc, *$3 ) ) ); }
777        | postfix_expression ARROW '[' field_name_list ']'      // CFA, tuple field selector
778                { $$ = new ExpressionNode( build_pfieldSel( yylloc, $1, build_tuple( yylloc, $4 ) ) ); }
779        | postfix_expression ICR
780                { $$ = new ExpressionNode( build_unary_val( yylloc, OperKinds::IncrPost, $1 ) ); }
781        | postfix_expression DECR
782                { $$ = new ExpressionNode( build_unary_val( yylloc, OperKinds::DecrPost, $1 ) ); }
783        | '(' type_no_function ')' '{' initializer_list_opt comma_opt '}' // C99, compound-literal
784                { $$ = new ExpressionNode( build_compoundLiteral( yylloc, $2, new InitializerNode( $5, true ) ) ); }
785        | '(' type_no_function ')' '@' '{' initializer_list_opt comma_opt '}' // CFA, explicit C compound-literal
786                { $$ = new ExpressionNode( build_compoundLiteral( yylloc, $2, (new InitializerNode( $6, true ))->set_maybeConstructed( false ) ) ); }
787        | '^' primary_expression '{' argument_expression_list_opt '}' // CFA, destructor call
788                {
789                        Token fn;
790                        fn.str = new string( "^?{}" );                          // location undefined
791                        $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, fn ) ), (ExpressionNode *)( $2 )->set_last( $4 ) ) );
792                }
793        ;
794
795argument_expression_list_opt:
796        // empty
797                { $$ = nullptr; }
798        | argument_expression_list
799        ;
800
801argument_expression_list:
802        argument_expression
803        | argument_expression_list_opt ',' argument_expression
804                { $$ = (ExpressionNode *)($1->set_last( $3 )); }
805        ;
806
807argument_expression:
808        '@'                                                                                                     // CFA, default parameter
809                { SemanticError( yylloc, "Default parameter for argument is currently unimplemented." ); $$ = nullptr; }
810                // { $$ = new ExpressionNode( build_constantInteger( *new string( "2" ) ) ); }
811        | assignment_expression
812        ;
813
814field_name_list:                                                                                // CFA, tuple field selector
815        field
816        | field_name_list ',' field                                     { $$ = (ExpressionNode *)($1->set_last( $3 )); }
817        ;
818
819field:                                                                                                  // CFA, tuple field selector
820        field_name
821        | FLOATING_DECIMALconstant field
822                { $$ = new ExpressionNode( build_fieldSel( yylloc, new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( yylloc, *$1 ) ), maybeMoveBuild( $2 ) ) ); }
823        | FLOATING_DECIMALconstant '[' field_name_list ']'
824                { $$ = new ExpressionNode( build_fieldSel( yylloc, new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( yylloc, *$1 ) ), build_tuple( yylloc, $3 ) ) ); }
825        | field_name '.' field
826                { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, maybeMoveBuild( $3 ) ) ); }
827        | field_name '.' '[' field_name_list ']'
828                { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_tuple( yylloc, $4 ) ) ); }
829        | field_name ARROW field
830                { $$ = new ExpressionNode( build_pfieldSel( yylloc, $1, maybeMoveBuild( $3 ) ) ); }
831        | field_name ARROW '[' field_name_list ']'
832                { $$ = new ExpressionNode( build_pfieldSel( yylloc, $1, build_tuple( yylloc, $4 ) ) ); }
833        ;
834
835field_name:
836        INTEGERconstant fraction_constants_opt
837                { $$ = new ExpressionNode( build_field_name_fraction_constants( yylloc, build_constantInteger( yylloc, *$1 ), $2 ) ); }
838        | FLOATINGconstant fraction_constants_opt
839                { $$ = new ExpressionNode( build_field_name_fraction_constants( yylloc, build_field_name_FLOATINGconstant( yylloc, *$1 ), $2 ) ); }
840        | identifier_at fraction_constants_opt                          // CFA, allow anonymous fields
841                {
842                        $$ = new ExpressionNode( build_field_name_fraction_constants( yylloc, build_varref( yylloc, $1 ), $2 ) );
843                }
844        ;
845
846fraction_constants_opt:
847        // empty
848                { $$ = nullptr; }
849        | fraction_constants_opt FLOATING_FRACTIONconstant
850                {
851                        ast::Expr * constant = build_field_name_FLOATING_FRACTIONconstant( yylloc, *$2 );
852                        $$ = $1 != nullptr ? new ExpressionNode( build_fieldSel( yylloc, $1, constant ) ) : new ExpressionNode( constant );
853                }
854        ;
855
856unary_expression:
857        postfix_expression
858                // first location where constant/string can have operator applied: sizeof 3/sizeof "abc" still requires
859                // semantics checks, e.g., ++3, 3--, *3, &&3
860        | constant
861        | string_literal
862                { $$ = $1; }
863        | EXTENSION cast_expression                                                     // GCC
864                { $$ = $2->set_extension( true ); }
865                // '*' ('&') is separated from unary_operator because of shift/reduce conflict in:
866                //              { * X; }         // dereference X
867                //              { * int X; } // CFA declaration of pointer to int
868        | ptrref_operator cast_expression                                       // CFA
869                {
870                        switch ( $1 ) {
871                        case OperKinds::AddressOf:
872                                $$ = new ExpressionNode( new ast::AddressExpr( maybeMoveBuild( $2 ) ) );
873                                break;
874                        case OperKinds::PointTo:
875                                $$ = new ExpressionNode( build_unary_val( yylloc, $1, $2 ) );
876                                break;
877                        case OperKinds::And:
878                                $$ = new ExpressionNode( new ast::AddressExpr( new ast::AddressExpr( maybeMoveBuild( $2 ) ) ) );
879                                break;
880                        default:
881                                assert( false );
882                        }
883                }
884        | unary_operator cast_expression
885                { $$ = new ExpressionNode( build_unary_val( yylloc, $1, $2 ) ); }
886        | ICR unary_expression
887                { $$ = new ExpressionNode( build_unary_val( yylloc, OperKinds::Incr, $2 ) ); }
888        | DECR unary_expression
889                { $$ = new ExpressionNode( build_unary_val( yylloc, OperKinds::Decr, $2 ) ); }
890        | SIZEOF unary_expression
891                { $$ = new ExpressionNode( new ast::SizeofExpr( yylloc, maybeMoveBuild( $2 ) ) ); }
892        | SIZEOF '(' type_no_function ')'
893                { $$ = new ExpressionNode( new ast::SizeofExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
894        | ALIGNOF unary_expression                                                      // GCC, variable alignment
895                { $$ = new ExpressionNode( new ast::AlignofExpr( yylloc, maybeMoveBuild( $2 ) ) ); }
896        | ALIGNOF '(' type_no_function ')'                                      // GCC, type alignment
897                { $$ = new ExpressionNode( new ast::AlignofExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
898        | OFFSETOF '(' type_no_function ',' identifier ')'
899                { $$ = new ExpressionNode( build_offsetOf( yylloc, $3, build_varref( yylloc, $5 ) ) ); }
900        | TYPEID '(' type_no_function ')'
901                {
902                        SemanticError( yylloc, "typeid name is currently unimplemented." ); $$ = nullptr;
903                        // $$ = new ExpressionNode( build_offsetOf( $3, build_varref( $5 ) ) );
904                }
905        ;
906
907ptrref_operator:
908        '*'                                                                                     { $$ = OperKinds::PointTo; }
909        | '&'                                                                           { $$ = OperKinds::AddressOf; }
910                // GCC, address of label must be handled by semantic check for ref,ref,label
911        | ANDAND                                                                        { $$ = OperKinds::And; }
912        ;
913
914unary_operator:
915        '+'                                                                                     { $$ = OperKinds::UnPlus; }
916        | '-'                                                                           { $$ = OperKinds::UnMinus; }
917        | '!'                                                                           { $$ = OperKinds::Neg; }
918        | '~'                                                                           { $$ = OperKinds::BitNeg; }
919        ;
920
921cast_expression:
922        unary_expression
923        | '(' type_no_function ')' cast_expression
924                { $$ = new ExpressionNode( build_cast( yylloc, $2, $4 ) ); }
925        | '(' aggregate_control '&' ')' cast_expression         // CFA
926                { $$ = new ExpressionNode( build_keyword_cast( yylloc, $2, $5 ) ); }
927        | '(' aggregate_control '*' ')' cast_expression         // CFA
928                { $$ = new ExpressionNode( build_keyword_cast( yylloc, $2, $5 ) ); }
929        | '(' VIRTUAL ')' cast_expression                                       // CFA
930                { $$ = new ExpressionNode( new ast::VirtualCastExpr( yylloc, maybeMoveBuild( $4 ), maybeMoveBuildType( nullptr ) ) ); }
931        | '(' VIRTUAL type_no_function ')' cast_expression      // CFA
932                { $$ = new ExpressionNode( new ast::VirtualCastExpr( yylloc, maybeMoveBuild( $5 ), maybeMoveBuildType( $3 ) ) ); }
933        | '(' RETURN type_no_function ')' cast_expression       // CFA
934                { $$ = new ExpressionNode( build_cast( yylloc, $3, $5, ast::CastExpr::Return ) ); }
935        | '(' COERCE type_no_function ')' cast_expression       // CFA
936                { SemanticError( yylloc, "Coerce cast is currently unimplemented." ); $$ = nullptr; }
937        | '(' qualifier_cast_list ')' cast_expression           // CFA
938                { SemanticError( yylloc, "Qualifier cast is currently unimplemented." ); $$ = nullptr; }
939//      | '(' type_no_function ')' tuple
940//              { $$ = new ast::ExpressionNode( build_cast( yylloc, $2, $4 ) ); }
941        ;
942
943qualifier_cast_list:
944        cast_modifier type_qualifier_name
945        | cast_modifier MUTEX
946        | qualifier_cast_list cast_modifier type_qualifier_name
947        | qualifier_cast_list cast_modifier MUTEX
948        ;
949
950cast_modifier:
951        '-'
952        | '+'
953        ;
954
955exponential_expression:
956        cast_expression
957        | exponential_expression '\\' cast_expression
958                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Exp, $1, $3 ) ); }
959        ;
960
961multiplicative_expression:
962        exponential_expression
963        | multiplicative_expression '*' exponential_expression
964                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Mul, $1, $3 ) ); }
965        | multiplicative_expression '/' exponential_expression
966                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Div, $1, $3 ) ); }
967        | multiplicative_expression '%' exponential_expression
968                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Mod, $1, $3 ) ); }
969        ;
970
971additive_expression:
972        multiplicative_expression
973        | additive_expression '+' multiplicative_expression
974                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Plus, $1, $3 ) ); }
975        | additive_expression '-' multiplicative_expression
976                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Minus, $1, $3 ) ); }
977        ;
978
979shift_expression:
980        additive_expression
981        | shift_expression LS additive_expression
982                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::LShift, $1, $3 ) ); }
983        | shift_expression RS additive_expression
984                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::RShift, $1, $3 ) ); }
985        ;
986
987relational_expression:
988        shift_expression
989        | relational_expression '<' shift_expression
990                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::LThan, $1, $3 ) ); }
991        | relational_expression '>' shift_expression
992                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::GThan, $1, $3 ) ); }
993        | relational_expression LE shift_expression
994                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::LEThan, $1, $3 ) ); }
995        | relational_expression GE shift_expression
996                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::GEThan, $1, $3 ) ); }
997        ;
998
999equality_expression:
1000        relational_expression
1001        | equality_expression EQ relational_expression
1002                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Eq, $1, $3 ) ); }
1003        | equality_expression NE relational_expression
1004                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Neq, $1, $3 ) ); }
1005        ;
1006
1007AND_expression:
1008        equality_expression
1009        | AND_expression '&' equality_expression
1010                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::BitAnd, $1, $3 ) ); }
1011        ;
1012
1013exclusive_OR_expression:
1014        AND_expression
1015        | exclusive_OR_expression '^' AND_expression
1016                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Xor, $1, $3 ) ); }
1017        ;
1018
1019inclusive_OR_expression:
1020        exclusive_OR_expression
1021        | inclusive_OR_expression '|' exclusive_OR_expression
1022                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::BitOr, $1, $3 ) ); }
1023        ;
1024
1025logical_AND_expression:
1026        inclusive_OR_expression
1027        | logical_AND_expression ANDAND inclusive_OR_expression
1028                { $$ = new ExpressionNode( build_and_or( yylloc, $1, $3, ast::AndExpr ) ); }
1029        ;
1030
1031logical_OR_expression:
1032        logical_AND_expression
1033        | logical_OR_expression OROR logical_AND_expression
1034                { $$ = new ExpressionNode( build_and_or( yylloc, $1, $3, ast::OrExpr ) ); }
1035        ;
1036
1037conditional_expression:
1038        logical_OR_expression
1039        | logical_OR_expression '?' comma_expression ':' conditional_expression
1040                { $$ = new ExpressionNode( build_cond( yylloc, $1, $3, $5 ) ); }
1041                // FIX ME: computes $1 twice
1042        | logical_OR_expression '?' /* empty */ ':' conditional_expression // GCC, omitted first operand
1043                { $$ = new ExpressionNode( build_cond( yylloc, $1, $1->clone(), $4 ) ); }
1044        ;
1045
1046constant_expression:
1047        conditional_expression
1048        ;
1049
1050assignment_expression:
1051                // CFA, assignment is separated from assignment_operator to ensure no assignment operations for tuples
1052        conditional_expression
1053        | unary_expression assignment_operator assignment_expression
1054                {
1055//                      if ( $2 == OperKinds::AtAssn ) {
1056//                              SemanticError( yylloc, "C @= assignment is currently unimplemented." ); $$ = nullptr;
1057//                      } else {
1058                                $$ = new ExpressionNode( build_binary_val( yylloc, $2, $1, $3 ) );
1059//                      } // if
1060                }
1061        | unary_expression '=' '{' initializer_list_opt comma_opt '}'
1062                { SemanticError( yylloc, "Initializer assignment is currently unimplemented." ); $$ = nullptr; }
1063        ;
1064
1065assignment_expression_opt:
1066        // empty
1067                { $$ = nullptr; }
1068        | assignment_expression
1069        ;
1070
1071assignment_operator:
1072        simple_assignment_operator
1073        | compound_assignment_operator
1074        ;
1075
1076simple_assignment_operator:
1077        '='                                                                                     { $$ = OperKinds::Assign; }
1078        | ATassign                                                                      { $$ = OperKinds::AtAssn; } // CFA
1079        ;
1080
1081compound_assignment_operator:
1082        EXPassign                                                                       { $$ = OperKinds::ExpAssn; }
1083        | MULTassign                                                            { $$ = OperKinds::MulAssn; }
1084        | DIVassign                                                                     { $$ = OperKinds::DivAssn; }
1085        | MODassign                                                                     { $$ = OperKinds::ModAssn; }
1086        | PLUSassign                                                            { $$ = OperKinds::PlusAssn; }
1087        | MINUSassign                                                           { $$ = OperKinds::MinusAssn; }
1088        | LSassign                                                                      { $$ = OperKinds::LSAssn; }
1089        | RSassign                                                                      { $$ = OperKinds::RSAssn; }
1090        | ANDassign                                                                     { $$ = OperKinds::AndAssn; }
1091        | ERassign                                                                      { $$ = OperKinds::ERAssn; }
1092        | ORassign                                                                      { $$ = OperKinds::OrAssn; }
1093        ;
1094
1095tuple:                                                                                                  // CFA, tuple
1096                // CFA, one assignment_expression is factored out of comma_expression to eliminate a shift/reduce conflict with
1097                // comma_expression in cfa_identifier_parameter_array and cfa_abstract_array
1098//      '[' ']'
1099//              { $$ = new ExpressionNode( build_tuple() ); }
1100//      | '[' push assignment_expression pop ']'
1101//              { $$ = new ExpressionNode( build_tuple( $3 ) ); }
1102        '[' ',' tuple_expression_list ']'
1103                { $$ = new ExpressionNode( build_tuple( yylloc, (ExpressionNode *)(new ExpressionNode( nullptr ) )->set_last( $3 ) ) ); }
1104        | '[' push assignment_expression pop ',' tuple_expression_list ']'
1105                { $$ = new ExpressionNode( build_tuple( yylloc, (ExpressionNode *)($3->set_last( $6 ) ) )); }
1106        ;
1107
1108tuple_expression_list:
1109        assignment_expression
1110        | '@'                                                                                           // CFA
1111                { SemanticError( yylloc, "Eliding tuple element with '@' is currently unimplemented." ); $$ = nullptr; }
1112        | tuple_expression_list ',' assignment_expression
1113                { $$ = (ExpressionNode *)($1->set_last( $3 )); }
1114        | tuple_expression_list ',' '@'
1115                { SemanticError( yylloc, "Eliding tuple element with '@' is currently unimplemented." ); $$ = nullptr; }
1116        ;
1117
1118comma_expression:
1119        assignment_expression
1120        | comma_expression ',' assignment_expression
1121                { $$ = new ExpressionNode( new ast::CommaExpr( yylloc, maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
1122        ;
1123
1124comma_expression_opt:
1125        // empty
1126                { $$ = nullptr; }
1127        | comma_expression
1128        ;
1129
1130// ************************** STATEMENTS *******************************
1131
1132statement:
1133        labeled_statement
1134        | compound_statement
1135        | expression_statement
1136        | selection_statement
1137        | iteration_statement
1138        | jump_statement
1139        | with_statement
1140        | mutex_statement
1141        | waitfor_statement
1142        | waituntil_statement
1143        | corun_statement
1144        | cofor_statement
1145        | exception_statement
1146        | enable_disable_statement
1147                { SemanticError( yylloc, "enable/disable statement is currently unimplemented." ); $$ = nullptr; }
1148        | asm_statement
1149        | DIRECTIVE
1150                { $$ = new StatementNode( build_directive( yylloc, $1 ) ); }
1151        ;
1152
1153labeled_statement:
1154                // labels cannot be identifiers 0 or 1
1155        identifier_or_type_name ':' attribute_list_opt statement
1156                { $$ = $4->add_label( yylloc, $1, $3 ); }
1157        | identifier_or_type_name ':' attribute_list_opt error // invalid syntax rule
1158                {
1159                        SemanticError( yylloc, ::toString( "syntx error, label \"", *$1.str, "\" must be associated with a statement, "
1160                                                                                           "where a declaration, case, or default is not a statement. "
1161                                                                                           "Move the label or terminate with a semi-colon." ) );
1162                        $$ = nullptr;
1163                }
1164        ;
1165
1166compound_statement:
1167        '{' '}'
1168                { $$ = new StatementNode( build_compound( yylloc, (StatementNode *)0 ) ); }
1169        | '{' push
1170          local_label_declaration_opt                                           // GCC, local labels appear at start of block
1171          statement_decl_list                                                           // C99, intermix declarations and statements
1172          pop '}'
1173                { $$ = new StatementNode( build_compound( yylloc, $4 ) ); }
1174        ;
1175
1176statement_decl_list:                                                                    // C99
1177        statement_decl
1178        | statement_decl_list statement_decl
1179                { assert( $1 ); $1->set_last( $2 ); $$ = $1; }
1180        ;
1181
1182statement_decl:
1183        declaration                                                                                     // CFA, new & old style declarations
1184                { $$ = new StatementNode( $1 ); }
1185        | EXTENSION declaration                                                         // GCC
1186                { distExt( $2 ); $$ = new StatementNode( $2 ); }
1187        | function_definition
1188                { $$ = new StatementNode( $1 ); }
1189        | EXTENSION function_definition                                         // GCC
1190                { distExt( $2 ); $$ = new StatementNode( $2 ); }
1191        | statement
1192        ;
1193
1194statement_list_nodecl:
1195        statement
1196        | statement_list_nodecl statement
1197                { assert( $1 ); $1->set_last( $2 ); $$ = $1; }
1198        | statement_list_nodecl error                                           // invalid syntax rule
1199                { SemanticError( yylloc, "syntax error, declarations only allowed at the start of the switch body, i.e., after the '{'." ); $$ = nullptr; }
1200        ;
1201
1202expression_statement:
1203        comma_expression_opt ';'
1204                { $$ = new StatementNode( build_expr( yylloc, $1 ) ); }
1205        ;
1206
1207selection_statement:
1208                        // pop causes a S/R conflict without separating the IF statement into a non-terminal even after resolving
1209                        // the inherent S/R conflict with THEN/ELSE.
1210        push if_statement pop
1211                { $$ = $2; }
1212        | SWITCH '(' comma_expression ')' case_clause
1213                { $$ = new StatementNode( build_switch( yylloc, true, $3, $5 ) ); }
1214        | SWITCH '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt pop '}' // CFA
1215                {
1216                        StatementNode *sw = new StatementNode( build_switch( yylloc, true, $3, $8 ) );
1217                        // The semantics of the declaration list is changed to include associated initialization, which is performed
1218                        // *before* the transfer to the appropriate case clause by hoisting the declarations into a compound
1219                        // statement around the switch.  Statements after the initial declaration list can never be executed, and
1220                        // therefore, are removed from the grammar even though C allows it. The change also applies to choose
1221                        // statement.
1222                        $$ = $7 ? new StatementNode( build_compound( yylloc, (StatementNode *)((new StatementNode( $7 ))->set_last( sw )) ) ) : sw;
1223                }
1224        | SWITCH '(' comma_expression ')' '{' error '}'         // CFA, invalid syntax rule error
1225                { SemanticError( yylloc, "synatx error, declarations can only appear before the list of case clauses." ); $$ = nullptr; }
1226        | CHOOSE '(' comma_expression ')' case_clause           // CFA
1227                { $$ = new StatementNode( build_switch( yylloc, false, $3, $5 ) ); }
1228        | CHOOSE '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt pop '}' // CFA
1229                {
1230                        StatementNode *sw = new StatementNode( build_switch( yylloc, false, $3, $8 ) );
1231                        $$ = $7 ? new StatementNode( build_compound( yylloc, (StatementNode *)((new StatementNode( $7 ))->set_last( sw )) ) ) : sw;
1232                }
1233        | CHOOSE '(' comma_expression ')' '{' error '}'         // CFA, invalid syntax rule
1234                { SemanticError( yylloc, "syntax error, declarations can only appear before the list of case clauses." ); $$ = nullptr; }
1235        ;
1236
1237if_statement:
1238        IF '(' conditional_declaration ')' statement            %prec THEN
1239                // explicitly deal with the shift/reduce conflict on if/else
1240                { $$ = new StatementNode( build_if( yylloc, $3, maybe_build_compound( yylloc, $5 ), nullptr ) ); }
1241        | IF '(' conditional_declaration ')' statement ELSE statement
1242                { $$ = new StatementNode( build_if( yylloc, $3, maybe_build_compound( yylloc, $5 ), maybe_build_compound( yylloc, $7 ) ) ); }
1243        ;
1244
1245conditional_declaration:
1246        comma_expression
1247                { $$ = new CondCtl( nullptr, $1 ); }
1248        | c_declaration                                                                         // no semi-colon
1249                { $$ = new CondCtl( $1, nullptr ); }
1250        | cfa_declaration                                                                       // no semi-colon
1251                { $$ = new CondCtl( $1, nullptr ); }
1252        | declaration comma_expression                                          // semi-colon separated
1253                { $$ = new CondCtl( $1, $2 ); }
1254        ;
1255
1256// CASE and DEFAULT clauses are only allowed in the SWITCH statement, precluding Duff's device. In addition, a case
1257// clause allows a list of values and subranges.
1258
1259case_value:                                                                                             // CFA
1260        constant_expression                                                     { $$ = $1; }
1261        | constant_expression ELLIPSIS constant_expression      // GCC, subrange
1262                { $$ = new ExpressionNode( new ast::RangeExpr( yylloc, maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
1263        | subrange                                                                                      // CFA, subrange
1264        ;
1265
1266case_value_list:                                                                                // CFA
1267        case_value                                                                      { $$ = new ClauseNode( build_case( yylloc, $1 ) ); }
1268                // convert case list, e.g., "case 1, 3, 5:" into "case 1: case 3: case 5"
1269        | case_value_list ',' case_value                        { $$ = $1->set_last( new ClauseNode( build_case( yylloc, $3 ) ) ); }
1270        ;
1271
1272case_label:                                                                                             // CFA
1273        CASE error                                                                                      // invalid syntax rule
1274                { SemanticError( yylloc, "syntax error, case list missing after case." ); $$ = nullptr; }
1275        | CASE case_value_list ':'                                      { $$ = $2; }
1276        | CASE case_value_list error                                            // invalid syntax rule
1277                { SemanticError( yylloc, "syntax error, colon missing after case list." ); $$ = nullptr; }
1278        | DEFAULT ':'                                                           { $$ = new ClauseNode( build_default( yylloc ) ); }
1279                // A semantic check is required to ensure only one default clause per switch/choose statement.
1280        | DEFAULT error                                                                         //  invalid syntax rule
1281                { SemanticError( yylloc, "syntax error, colon missing after default." ); $$ = nullptr; }
1282        ;
1283
1284case_label_list:                                                                                // CFA
1285        case_label
1286        | case_label_list case_label                            { $$ = $1->set_last( $2 ); }
1287        ;
1288
1289case_clause:                                                                                    // CFA
1290        case_label_list statement                                       { $$ = $1->append_last_case( maybe_build_compound( yylloc, $2 ) ); }
1291        ;
1292
1293switch_clause_list_opt:                                                                 // CFA
1294        // empty
1295                { $$ = nullptr; }
1296        | switch_clause_list
1297        ;
1298
1299switch_clause_list:                                                                             // CFA
1300        case_label_list statement_list_nodecl
1301                { $$ = $1->append_last_case( new StatementNode( build_compound( yylloc, $2 ) ) ); }
1302        | switch_clause_list case_label_list statement_list_nodecl
1303                { $$ = $1->set_last( $2->append_last_case( new StatementNode( build_compound( yylloc, $3 ) ) ) ); }
1304        ;
1305
1306iteration_statement:
1307        WHILE '(' ')' statement                                                         %prec THEN // CFA => while ( 1 )
1308                { $$ = new StatementNode( build_while( yylloc, new CondCtl( nullptr, NEW_ONE ), maybe_build_compound( yylloc, $4 ) ) ); }
1309        | WHILE '(' ')' statement ELSE statement                        // CFA
1310                {
1311                        $$ = new StatementNode( build_while( yylloc, new CondCtl( nullptr, NEW_ONE ), maybe_build_compound( yylloc, $4 ) ) );
1312                        SemanticWarning( yylloc, Warning::SuperfluousElse );
1313                }
1314        | WHILE '(' conditional_declaration ')' statement       %prec THEN
1315                { $$ = new StatementNode( build_while( yylloc, $3, maybe_build_compound( yylloc, $5 ) ) ); }
1316        | WHILE '(' conditional_declaration ')' statement ELSE statement // CFA
1317                { $$ = new StatementNode( build_while( yylloc, $3, maybe_build_compound( yylloc, $5 ), $7 ) ); }
1318        | DO statement WHILE '(' ')' ';'                                        // CFA => do while( 1 )
1319                { $$ = new StatementNode( build_do_while( yylloc, NEW_ONE, maybe_build_compound( yylloc, $2 ) ) ); }
1320        | DO statement WHILE '(' ')' ELSE statement                     // CFA
1321                {
1322                        $$ = new StatementNode( build_do_while( yylloc, NEW_ONE, maybe_build_compound( yylloc, $2 ) ) );
1323                        SemanticWarning( yylloc, Warning::SuperfluousElse );
1324                }
1325        | DO statement WHILE '(' comma_expression ')' ';'
1326                { $$ = new StatementNode( build_do_while( yylloc, $5, maybe_build_compound( yylloc, $2 ) ) ); }
1327        | DO statement WHILE '(' comma_expression ')' ELSE statement // CFA
1328                { $$ = new StatementNode( build_do_while( yylloc, $5, maybe_build_compound( yylloc, $2 ), $8 ) ); }
1329        | FOR '(' ')' statement                                                         %prec THEN // CFA => for ( ;; )
1330                { $$ = new StatementNode( build_for( yylloc, new ForCtrl( nullptr, nullptr, nullptr ), maybe_build_compound( yylloc, $4 ) ) ); }
1331        | FOR '(' ')' statement ELSE statement                          // CFA
1332                {
1333                        $$ = new StatementNode( build_for( yylloc, new ForCtrl( nullptr, nullptr, nullptr ), maybe_build_compound( yylloc, $4 ) ) );
1334                        SemanticWarning( yylloc, Warning::SuperfluousElse );
1335                }
1336        | FOR '(' for_control_expression_list ')' statement     %prec THEN
1337                { $$ = new StatementNode( build_for( yylloc, $3, maybe_build_compound( yylloc, $5 ) ) ); }
1338        | FOR '(' for_control_expression_list ')' statement ELSE statement // CFA
1339                { $$ = new StatementNode( build_for( yylloc, $3, maybe_build_compound( yylloc, $5 ), $7 ) ); }
1340        ;
1341
1342for_control_expression_list:
1343        for_control_expression
1344        | for_control_expression_list ':' for_control_expression
1345                // ForCtrl + ForCtrl:
1346                //    init + init => multiple declaration statements that are hoisted
1347                //    condition + condition => (expression) && (expression)
1348                //    change + change => (expression), (expression)
1349                {
1350                        $1->init->set_last( $3->init );
1351                        if ( $1->condition ) {
1352                                if ( $3->condition ) {
1353                                        $1->condition->expr.reset( new ast::LogicalExpr( yylloc, $1->condition->expr.release(), $3->condition->expr.release(), ast::AndExpr ) );
1354                                } // if
1355                        } else $1->condition = $3->condition;
1356                        if ( $1->change ) {
1357                                if ( $3->change ) {
1358                                        $1->change->expr.reset( new ast::CommaExpr( yylloc, $1->change->expr.release(), $3->change->expr.release() ) );
1359                                } // if
1360                        } else $1->change = $3->change;
1361                        $$ = $1;
1362                }
1363        ;
1364
1365for_control_expression:
1366        ';' comma_expression_opt ';' comma_expression_opt
1367                { $$ = new ForCtrl( nullptr, $2, $4 ); }
1368        | comma_expression ';' comma_expression_opt ';' comma_expression_opt
1369                {
1370                        StatementNode * init = $1 ? new StatementNode( new ast::ExprStmt( yylloc, maybeMoveBuild( $1 ) ) ) : nullptr;
1371                        $$ = new ForCtrl( init, $3, $5 );
1372                }
1373        | declaration comma_expression_opt ';' comma_expression_opt // C99, declaration has ';'
1374                { $$ = new ForCtrl( new StatementNode( $1 ), $2, $4 ); }
1375
1376        | '@' ';' comma_expression                                                      // CFA, empty loop-index
1377                { $$ = new ForCtrl( nullptr, $3, nullptr ); }
1378        | '@' ';' comma_expression ';' comma_expression         // CFA, empty loop-index
1379                { $$ = new ForCtrl( nullptr, $3, $5 ); }
1380
1381        | comma_expression                                                                      // CFA, anonymous loop-index
1382                { $$ = forCtrl( yylloc, $1, new string( DeclarationNode::anonymous.newName() ), NEW_ZERO, OperKinds::LThan, $1->clone(), NEW_ONE ); }
1383        | downupdowneq comma_expression                                         // CFA, anonymous loop-index
1384                { $$ = forCtrl( yylloc, $2, new string( DeclarationNode::anonymous.newName() ), UPDOWN( $1, NEW_ZERO, $2->clone() ), $1, UPDOWN( $1, $2->clone(), NEW_ZERO ), NEW_ONE ); }
1385
1386        | comma_expression updowneq comma_expression            // CFA, anonymous loop-index
1387                { $$ = forCtrl( yylloc, $1, new string( DeclarationNode::anonymous.newName() ), UPDOWN( $2, $1->clone(), $3 ), $2, UPDOWN( $2, $3->clone(), $1->clone() ), NEW_ONE ); }
1388        | '@' updowneq comma_expression                                         // CFA, anonymous loop-index
1389                {
1390                        if ( $2 == OperKinds::LThan || $2 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
1391                        else $$ = forCtrl( yylloc, $3, new string( DeclarationNode::anonymous.newName() ), $3->clone(), $2, nullptr, NEW_ONE );
1392                }
1393        | comma_expression updowneq '@'                                         // CFA, anonymous loop-index
1394                {
1395                        if ( $2 == OperKinds::LThan || $2 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
1396                        else { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
1397                }
1398        | comma_expression updowneq comma_expression '~' comma_expression // CFA, anonymous loop-index
1399                { $$ = forCtrl( yylloc, $1, new string( DeclarationNode::anonymous.newName() ), UPDOWN( $2, $1->clone(), $3 ), $2, UPDOWN( $2, $3->clone(), $1->clone() ), $5 ); }
1400        | '@' updowneq comma_expression '~' comma_expression // CFA, anonymous loop-index
1401                {
1402                        if ( $2 == OperKinds::LThan || $2 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
1403                        else $$ = forCtrl( yylloc, $3, new string( DeclarationNode::anonymous.newName() ), $3->clone(), $2, nullptr, $5 );
1404                }
1405        | comma_expression updowneq '@' '~' comma_expression // CFA, anonymous loop-index
1406                {
1407                        if ( $2 == OperKinds::LThan || $2 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
1408                        else { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
1409                }
1410        | comma_expression updowneq comma_expression '~' '@' // CFA, invalid syntax rule
1411                { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
1412        | '@' updowneq '@'                                                                      // CFA, invalid syntax rule
1413                { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
1414        | '@' updowneq comma_expression '~' '@'                         // CFA, invalid syntax rule
1415                { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
1416        | comma_expression updowneq '@' '~' '@'                         // CFA, invalid syntax rule
1417                { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
1418        | '@' updowneq '@' '~' '@'                                                      // CFA, invalid syntax rule
1419                { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
1420
1421        | comma_expression ';' comma_expression                         // CFA
1422                { $$ = forCtrl( yylloc, $3, $1, NEW_ZERO, OperKinds::LThan, $3->clone(), NEW_ONE ); }
1423        | comma_expression ';' downupdowneq comma_expression // CFA
1424                { $$ = forCtrl( yylloc, $4, $1, UPDOWN( $3, NEW_ZERO, $4->clone() ), $3, UPDOWN( $3, $4->clone(), NEW_ZERO ), NEW_ONE ); }
1425
1426        | comma_expression ';' comma_expression updowneq comma_expression // CFA
1427                { $$ = forCtrl( yylloc, $3, $1, UPDOWN( $4, $3->clone(), $5 ), $4, UPDOWN( $4, $5->clone(), $3->clone() ), NEW_ONE ); }
1428        | comma_expression ';' '@' updowneq comma_expression // CFA
1429                {
1430                        if ( $4 == OperKinds::LThan || $4 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
1431                        else $$ = forCtrl( yylloc, $5, $1, $5->clone(), $4, nullptr, NEW_ONE );
1432                }
1433        | comma_expression ';' comma_expression updowneq '@' // CFA
1434                {
1435                        if ( $4 == OperKinds::GThan || $4 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
1436                        else if ( $4 == OperKinds::LEThan ) { SemanticError( yylloc, "syntax error, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
1437                        else $$ = forCtrl( yylloc, $3, $1, $3->clone(), $4, nullptr, NEW_ONE );
1438                }
1439        | comma_expression ';' '@' updowneq '@'                         // CFA, invalid syntax rule
1440                { SemanticError( yylloc, "syntax error, missing low/high value for up/down-to range so index is uninitialized." ); $$ = nullptr; }
1441
1442        | comma_expression ';' comma_expression updowneq comma_expression '~' comma_expression // CFA
1443                { $$ = forCtrl( yylloc, $3, $1, UPDOWN( $4, $3->clone(), $5 ), $4, UPDOWN( $4, $5->clone(), $3->clone() ), $7 ); }
1444        | comma_expression ';' '@' updowneq comma_expression '~' comma_expression // CFA, invalid syntax rule
1445                {
1446                        if ( $4 == OperKinds::LThan || $4 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
1447                        else $$ = forCtrl( yylloc, $5, $1, $5->clone(), $4, nullptr, $7 );
1448                }
1449        | comma_expression ';' comma_expression updowneq '@' '~' comma_expression // CFA
1450                {
1451                        if ( $4 == OperKinds::GThan || $4 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
1452                        else if ( $4 == OperKinds::LEThan ) { SemanticError( yylloc, "syntax error, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
1453                        else $$ = forCtrl( yylloc, $3, $1, $3->clone(), $4, nullptr, $7 );
1454                }
1455        | comma_expression ';' comma_expression updowneq comma_expression '~' '@' // CFA
1456                { $$ = forCtrl( yylloc, $3, $1, UPDOWN( $4, $3->clone(), $5 ), $4, UPDOWN( $4, $5->clone(), $3->clone() ), nullptr ); }
1457        | comma_expression ';' '@' updowneq comma_expression '~' '@' // CFA, invalid syntax rule
1458                {
1459                        if ( $4 == OperKinds::LThan || $4 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
1460                        else $$ = forCtrl( yylloc, $5, $1, $5->clone(), $4, nullptr, nullptr );
1461                }
1462        | comma_expression ';' comma_expression updowneq '@' '~' '@' // CFA
1463                {
1464                        if ( $4 == OperKinds::GThan || $4 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
1465                        else if ( $4 == OperKinds::LEThan ) { SemanticError( yylloc, "syntax error, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
1466                        else $$ = forCtrl( yylloc, $3, $1, $3->clone(), $4, nullptr, nullptr );
1467                }
1468        | comma_expression ';' '@' updowneq '@' '~' '@' // CFA
1469                { SemanticError( yylloc, "syntax error, missing low/high value for up/down-to range so index is uninitialized." ); $$ = nullptr; }
1470
1471        | declaration comma_expression                                          // CFA
1472                { $$ = forCtrl( yylloc, $1, NEW_ZERO, OperKinds::LThan, $2, NEW_ONE ); }
1473        | declaration downupdowneq comma_expression                     // CFA
1474                { $$ = forCtrl( yylloc, $1, UPDOWN( $2, NEW_ZERO, $3 ), $2, UPDOWN( $2, $3->clone(), NEW_ZERO ), NEW_ONE ); }
1475
1476        | declaration comma_expression updowneq comma_expression // CFA
1477                { $$ = forCtrl( yylloc, $1, UPDOWN( $3, $2->clone(), $4 ), $3, UPDOWN( $3, $4->clone(), $2->clone() ), NEW_ONE ); }
1478        | declaration '@' updowneq comma_expression                     // CFA
1479                {
1480                        if ( $3 == OperKinds::LThan || $3 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
1481                        else $$ = forCtrl( yylloc, $1, $4, $3, nullptr, NEW_ONE );
1482                }
1483        | declaration comma_expression updowneq '@'                     // CFA
1484                {
1485                        if ( $3 == OperKinds::GThan || $3 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
1486                        else if ( $3 == OperKinds::LEThan ) { SemanticError( yylloc, "syntax error, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
1487                        else $$ = forCtrl( yylloc, $1, $2, $3, nullptr, NEW_ONE );
1488                }
1489
1490        | declaration comma_expression updowneq comma_expression '~' comma_expression // CFA
1491                { $$ = forCtrl( yylloc, $1, UPDOWN( $3, $2, $4 ), $3, UPDOWN( $3, $4->clone(), $2->clone() ), $6 ); }
1492        | declaration '@' updowneq comma_expression '~' comma_expression // CFA
1493                {
1494                        if ( $3 == OperKinds::LThan || $3 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
1495                        else $$ = forCtrl( yylloc, $1, $4, $3, nullptr, $6 );
1496                }
1497        | declaration comma_expression updowneq '@' '~' comma_expression // CFA
1498                {
1499                        if ( $3 == OperKinds::GThan || $3 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
1500                        else if ( $3 == OperKinds::LEThan ) { SemanticError( yylloc, "syntax error, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
1501                        else $$ = forCtrl( yylloc, $1, $2, $3, nullptr, $6 );
1502                }
1503        | declaration comma_expression updowneq comma_expression '~' '@' // CFA
1504                { $$ = forCtrl( yylloc, $1, UPDOWN( $3, $2, $4 ), $3, UPDOWN( $3, $4->clone(), $2->clone() ), nullptr ); }
1505        | declaration '@' updowneq comma_expression '~' '@' // CFA
1506                {
1507                        if ( $3 == OperKinds::LThan || $3 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
1508                        else $$ = forCtrl( yylloc, $1, $4, $3, nullptr, nullptr );
1509                }
1510        | declaration comma_expression updowneq '@' '~' '@'     // CFA
1511                {
1512                        if ( $3 == OperKinds::GThan || $3 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
1513                        else if ( $3 == OperKinds::LEThan ) { SemanticError( yylloc, "syntax error, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
1514                        else $$ = forCtrl( yylloc, $1, $2, $3, nullptr, nullptr );
1515                }
1516        | declaration '@' updowneq '@' '~' '@'                          // CFA, invalid syntax rule
1517                { SemanticError( yylloc, "syntax error, missing low/high value for up/down-to range so index is uninitialized." ); $$ = nullptr; }
1518
1519        | comma_expression ';' TYPEDEFname                                      // CFA, array type
1520                {
1521                        SemanticError( yylloc, "Type iterator is currently unimplemented." ); $$ = nullptr;
1522                        //$$ = forCtrl( new ExpressionNode( build_varref( $3 ) ), $1, nullptr, OperKinds::Range, nullptr, nullptr );
1523                }
1524        | comma_expression ';' downupdowneq TYPEDEFname         // CFA, array type
1525                {
1526                        if ( $3 == OperKinds::LEThan || $3 == OperKinds::GEThan ) {
1527                                SemanticError( yylloc, "syntax error, all enumeration ranges are equal (all values). Remove \"=~\"." ); $$ = nullptr;
1528                        }
1529                        SemanticError( yylloc, "Type iterator is currently unimplemented." ); $$ = nullptr;
1530                }
1531        ;
1532
1533downupdowneq:
1534        ErangeDown
1535                { $$ = OperKinds::GThan; }
1536        | ErangeUpEq
1537                { $$ = OperKinds::LEThan; }
1538        | ErangeDownEq
1539                { $$ = OperKinds::GEThan; }
1540        ;
1541
1542updown:
1543        '~'
1544                { $$ = OperKinds::LThan; }
1545        | ErangeDown
1546                { $$ = OperKinds::GThan; }
1547        ;
1548
1549updowneq:
1550        updown
1551        | ErangeUpEq
1552                { $$ = OperKinds::LEThan; }
1553        | ErangeDownEq
1554                { $$ = OperKinds::GEThan; }
1555        ;
1556
1557jump_statement:
1558        GOTO identifier_or_type_name ';'
1559                { $$ = new StatementNode( build_branch( yylloc, $2, ast::BranchStmt::Goto ) ); }
1560        | GOTO '*' comma_expression ';'                                         // GCC, computed goto
1561                // The syntax for the GCC computed goto violates normal expression precedence, e.g., goto *i+3; => goto *(i+3);
1562                // whereas normal operator precedence yields goto (*i)+3;
1563                { $$ = new StatementNode( build_computedgoto( $3 ) ); }
1564                // A semantic check is required to ensure fallthru appears only in the body of a choose statement.
1565        | fall_through_name ';'                                                         // CFA
1566                { $$ = new StatementNode( build_branch( yylloc, ast::BranchStmt::FallThrough ) ); }
1567        | fall_through_name identifier_or_type_name ';'         // CFA
1568                { $$ = new StatementNode( build_branch( yylloc, $2, ast::BranchStmt::FallThrough ) ); }
1569        | fall_through_name DEFAULT ';'                                         // CFA
1570                { $$ = new StatementNode( build_branch( yylloc, ast::BranchStmt::FallThroughDefault ) ); }
1571        | CONTINUE ';'
1572                // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
1573                { $$ = new StatementNode( build_branch( yylloc, ast::BranchStmt::Continue ) ); }
1574        | CONTINUE identifier_or_type_name ';'                          // CFA, multi-level continue
1575                // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1576                // the target of the transfer appears only at the start of an iteration statement.
1577                { $$ = new StatementNode( build_branch( yylloc, $2, ast::BranchStmt::Continue ) ); }
1578        | BREAK ';'
1579                // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
1580                { $$ = new StatementNode( build_branch( yylloc, ast::BranchStmt::Break ) ); }
1581        | BREAK identifier_or_type_name ';'                                     // CFA, multi-level exit
1582                // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1583                // the target of the transfer appears only at the start of an iteration statement.
1584                { $$ = new StatementNode( build_branch( yylloc, $2, ast::BranchStmt::Break ) ); }
1585        | RETURN comma_expression_opt ';'
1586                { $$ = new StatementNode( build_return( yylloc, $2 ) ); }
1587        | RETURN '{' initializer_list_opt comma_opt '}' ';'
1588                { SemanticError( yylloc, "Initializer return is currently unimplemented." ); $$ = nullptr; }
1589        | SUSPEND ';'
1590                { $$ = new StatementNode( build_suspend( yylloc, nullptr, ast::SuspendStmt::None ) ); }
1591        | SUSPEND compound_statement
1592                { $$ = new StatementNode( build_suspend( yylloc, $2, ast::SuspendStmt::None ) ); }
1593        | SUSPEND COROUTINE ';'
1594                { $$ = new StatementNode( build_suspend( yylloc, nullptr, ast::SuspendStmt::Coroutine ) ); }
1595        | SUSPEND COROUTINE compound_statement
1596                { $$ = new StatementNode( build_suspend( yylloc, $3, ast::SuspendStmt::Coroutine ) ); }
1597        | SUSPEND GENERATOR ';'
1598                { $$ = new StatementNode( build_suspend( yylloc, nullptr, ast::SuspendStmt::Generator ) ); }
1599        | SUSPEND GENERATOR compound_statement
1600                { $$ = new StatementNode( build_suspend( yylloc, $3, ast::SuspendStmt::Generator ) ); }
1601        | THROW assignment_expression_opt ';'                           // handles rethrow
1602                { $$ = new StatementNode( build_throw( yylloc, $2 ) ); }
1603        | THROWRESUME assignment_expression_opt ';'                     // handles reresume
1604                { $$ = new StatementNode( build_resume( yylloc, $2 ) ); }
1605        | THROWRESUME assignment_expression_opt AT assignment_expression ';' // handles reresume
1606                { $$ = new StatementNode( build_resume_at( $2, $4 ) ); }
1607        ;
1608
1609fall_through_name:                                                                              // CFA
1610        FALLTHRU
1611        | FALLTHROUGH
1612        ;
1613
1614with_statement:
1615        WITH '(' tuple_expression_list ')' statement
1616                { $$ = new StatementNode( build_with( yylloc, $3, $5 ) ); }
1617        ;
1618
1619// If MUTEX becomes a general qualifier, there are shift/reduce conflicts, so possibly change syntax to "with mutex".
1620mutex_statement:
1621        MUTEX '(' argument_expression_list_opt ')' statement
1622                {
1623                        if ( ! $3 ) { SemanticError( yylloc, "syntax error, mutex argument list cannot be empty." ); $$ = nullptr; }
1624                        $$ = new StatementNode( build_mutex( yylloc, $3, $5 ) );
1625                }
1626        ;
1627
1628when_clause:
1629        WHEN '(' comma_expression ')'                           { $$ = $3; }
1630        ;
1631
1632when_clause_opt:
1633        // empty
1634                { $$ = nullptr; }
1635        | when_clause
1636        ;
1637
1638cast_expression_list:
1639        cast_expression
1640        | cast_expression_list ',' cast_expression
1641                // { $$ = (ExpressionNode *)($1->set_last( $3 )); }
1642                { SemanticError( yylloc, "List of mutex member is currently unimplemented." ); $$ = nullptr; }
1643        ;
1644
1645timeout:
1646        TIMEOUT '(' comma_expression ')'                        { $$ = $3; }
1647        ;
1648
1649wor:
1650        OROR
1651        | WOR
1652
1653waitfor:
1654        WAITFOR '(' cast_expression ')'
1655                { $$ = $3; }
1656        | WAITFOR '(' cast_expression_list ':' argument_expression_list_opt ')'
1657                { $$ = (ExpressionNode *)($3->set_last( $5 )); }
1658        ;
1659
1660wor_waitfor_clause:
1661        when_clause_opt waitfor statement                                       %prec THEN
1662                // Called first: create header for WaitForStmt.
1663                { $$ = build_waitfor( yylloc, new ast::WaitForStmt( yylloc ), $1, $2, maybe_build_compound( yylloc, $3 ) ); }
1664        | wor_waitfor_clause wor when_clause_opt waitfor statement
1665                { $$ = build_waitfor( yylloc, $1, $3, $4, maybe_build_compound( yylloc, $5 ) ); }
1666        | wor_waitfor_clause wor when_clause_opt ELSE statement
1667                { $$ = build_waitfor_else( yylloc, $1, $3, maybe_build_compound( yylloc, $5 ) ); }
1668        | wor_waitfor_clause wor when_clause_opt timeout statement      %prec THEN
1669                { $$ = build_waitfor_timeout( yylloc, $1, $3, $4, maybe_build_compound( yylloc, $5 ) ); }
1670        // "else" must be conditional after timeout or timeout is never triggered (i.e., it is meaningless)
1671        | wor_waitfor_clause wor when_clause_opt timeout statement wor ELSE statement // invalid syntax rule
1672                { SemanticError( yylloc, "syntax error, else clause must be conditional after timeout or timeout never triggered." ); $$ = nullptr; }
1673        | wor_waitfor_clause wor when_clause_opt timeout statement wor when_clause ELSE statement
1674                { $$ = build_waitfor_else( yylloc, build_waitfor_timeout( yylloc, $1, $3, $4, maybe_build_compound( yylloc, $5 ) ), $7, maybe_build_compound( yylloc, $9 ) ); }
1675        ;
1676
1677waitfor_statement:
1678        wor_waitfor_clause                                                                      %prec THEN
1679                { $$ = new StatementNode( $1 ); }
1680        ;
1681
1682wand:
1683        ANDAND
1684        | WAND
1685        ;
1686
1687waituntil:
1688        WAITUNTIL '(' comma_expression ')'
1689                { $$ = $3; }
1690        ;
1691
1692waituntil_clause:
1693        when_clause_opt waituntil statement
1694                { $$ = build_waituntil_clause( yylloc, $1, $2, maybe_build_compound( yylloc, $3 ) ); }
1695        | '(' wor_waituntil_clause ')'
1696                { $$ = $2; }
1697        ;
1698
1699wand_waituntil_clause:
1700        waituntil_clause                                                                        %prec THEN
1701                { $$ = $1; }
1702        | waituntil_clause wand wand_waituntil_clause
1703                { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::AND, $1, $3 ); }
1704        ;
1705
1706wor_waituntil_clause:
1707        wand_waituntil_clause
1708                { $$ = $1; }
1709        | wor_waituntil_clause wor wand_waituntil_clause
1710                { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::OR, $1, $3 ); }
1711        | wor_waituntil_clause wor when_clause_opt ELSE statement
1712                { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::LEFT_OR, $1, build_waituntil_else( yylloc, $3, maybe_build_compound( yylloc, $5 ) ) ); }
1713        ;
1714
1715waituntil_statement:
1716        wor_waituntil_clause                                                            %prec THEN
1717                { $$ = new StatementNode( build_waituntil_stmt( yylloc, $1 ) ); }
1718        ;
1719
1720corun_statement:
1721        CORUN statement
1722                { $$ = new StatementNode( build_corun( yylloc, $2 ) ); }
1723        ;
1724
1725cofor_statement:
1726        COFOR '(' for_control_expression_list ')' statement
1727                { SemanticError( yylloc, "cofor statement is currently unimplemented." ); $$ = nullptr; }
1728        ;
1729
1730exception_statement:
1731        TRY compound_statement handler_clause                                   %prec THEN
1732                { $$ = new StatementNode( build_try( yylloc, $2, $3, nullptr ) ); }
1733        | TRY compound_statement finally_clause
1734                { $$ = new StatementNode( build_try( yylloc, $2, nullptr, $3 ) ); }
1735        | TRY compound_statement handler_clause finally_clause
1736                { $$ = new StatementNode( build_try( yylloc, $2, $3, $4 ) ); }
1737        ;
1738
1739handler_clause:
1740        handler_key '(' push exception_declaration pop handler_predicate_opt ')' compound_statement
1741                { $$ = new ClauseNode( build_catch( yylloc, $1, $4, $6, $8 ) ); }
1742        | handler_clause handler_key '(' push exception_declaration pop handler_predicate_opt ')' compound_statement
1743                { $$ = $1->set_last( new ClauseNode( build_catch( yylloc, $2, $5, $7, $9 ) ) ); }
1744        ;
1745
1746handler_predicate_opt:
1747        // empty
1748                { $$ = nullptr; }
1749        | ';' conditional_expression                            { $$ = $2; }
1750        ;
1751
1752handler_key:
1753        CATCH                                                                           { $$ = ast::Terminate; }
1754        | RECOVER                                                                       { $$ = ast::Terminate; }
1755        | CATCHRESUME                                                           { $$ = ast::Resume; }
1756        | FIXUP                                                                         { $$ = ast::Resume; }
1757        ;
1758
1759finally_clause:
1760        FINALLY compound_statement                                      { $$ = new ClauseNode( build_finally( yylloc, $2 ) ); }
1761        ;
1762
1763exception_declaration:
1764                // No SUE declaration in parameter list.
1765        type_specifier_nobody
1766        | type_specifier_nobody declarator
1767                { $$ = $2->addType( $1 ); }
1768        | type_specifier_nobody variable_abstract_declarator
1769                { $$ = $2->addType( $1 ); }
1770        | cfa_abstract_declarator_tuple identifier                      // CFA
1771                { $$ = $1->addName( $2 ); }
1772        | cfa_abstract_declarator_tuple                                         // CFA
1773        ;
1774
1775enable_disable_statement:
1776        enable_disable_key identifier_list compound_statement
1777        ;
1778
1779enable_disable_key:
1780        ENABLE
1781        | DISABLE
1782        ;
1783
1784asm_statement:
1785        ASM asm_volatile_opt '(' string_literal ')' ';'
1786                { $$ = new StatementNode( build_asm( yylloc, $2, $4, nullptr ) ); }
1787        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ')' ';' // remaining GCC
1788                { $$ = new StatementNode( build_asm( yylloc, $2, $4, $6 ) ); }
1789        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ')' ';'
1790                { $$ = new StatementNode( build_asm( yylloc, $2, $4, $6, $8 ) ); }
1791        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ':' asm_clobbers_list_opt ')' ';'
1792                { $$ = new StatementNode( build_asm( yylloc, $2, $4, $6, $8, $10 ) ); }
1793        | ASM asm_volatile_opt GOTO '(' string_literal ':' ':' asm_operands_opt ':' asm_clobbers_list_opt ':' label_list ')' ';'
1794                { $$ = new StatementNode( build_asm( yylloc, $2, $5, nullptr, $8, $10, $12 ) ); }
1795        ;
1796
1797asm_volatile_opt:                                                                               // GCC
1798        // empty
1799                { $$ = false; }
1800        | VOLATILE
1801                { $$ = true; }
1802        ;
1803
1804asm_operands_opt:                                                                               // GCC
1805        // empty
1806                { $$ = nullptr; }                                                               // use default argument
1807        | asm_operands_list
1808        ;
1809
1810asm_operands_list:                                                                              // GCC
1811        asm_operand
1812        | asm_operands_list ',' asm_operand
1813                { $$ = (ExpressionNode *)($1->set_last( $3 )); }
1814        ;
1815
1816asm_operand:                                                                                    // GCC
1817        string_literal '(' constant_expression ')'
1818                { $$ = new ExpressionNode( new ast::AsmExpr( yylloc, "", maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
1819        | '[' IDENTIFIER ']' string_literal '(' constant_expression ')'
1820                {
1821                        $$ = new ExpressionNode( new ast::AsmExpr( yylloc, *$2.str, maybeMoveBuild( $4 ), maybeMoveBuild( $6 ) ) );
1822                        delete $2.str;
1823                }
1824        ;
1825
1826asm_clobbers_list_opt:                                                                  // GCC
1827        // empty
1828                { $$ = nullptr; }                                                               // use default argument
1829        | string_literal
1830                { $$ = $1; }
1831        | asm_clobbers_list_opt ',' string_literal
1832                { $$ = (ExpressionNode *)( $1->set_last( $3 ) ); }
1833        ;
1834
1835label_list:
1836        identifier
1837                {
1838                        $$ = new LabelNode(); $$->labels.emplace_back( yylloc, *$1 );
1839                        delete $1;                                                                      // allocated by lexer
1840                }
1841        | label_list ',' identifier
1842                {
1843                        $$ = $1; $1->labels.emplace_back( yylloc, *$3 );
1844                        delete $3;                                                                      // allocated by lexer
1845                }
1846        ;
1847
1848// ****************************** DECLARATIONS *********************************
1849
1850declaration_list_opt:                                                                   // used at beginning of switch statement
1851        // empty
1852                { $$ = nullptr; }
1853        | declaration_list
1854        ;
1855
1856declaration_list:
1857        declaration
1858        | declaration_list declaration
1859                { $$ = $1->appendList( $2 ); }
1860        ;
1861
1862KR_parameter_list_opt:                                                                  // used to declare parameter types in K&R style functions
1863        // empty
1864                { $$ = nullptr; }
1865        | KR_parameter_list
1866        ;
1867
1868KR_parameter_list:
1869        c_declaration ';'
1870                { $$ = $1; }
1871        | KR_parameter_list c_declaration ';'
1872                { $$ = $1->appendList( $2 ); }
1873        ;
1874
1875local_label_declaration_opt:                                                    // GCC, local label
1876        // empty
1877        | local_label_declaration_list
1878        ;
1879
1880local_label_declaration_list:                                                   // GCC, local label
1881        LABEL local_label_list ';'
1882        | local_label_declaration_list LABEL local_label_list ';'
1883        ;
1884
1885local_label_list:                                                                               // GCC, local label
1886        identifier_or_type_name
1887        | local_label_list ',' identifier_or_type_name
1888        ;
1889
1890declaration:                                                                                    // old & new style declarations
1891        c_declaration ';'
1892                {
1893                        // printf( "C_DECLARATION1 %p %s\n", $$, $$->name ? $$->name->c_str() : "(nil)" );
1894                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
1895                        //   printf( "\tattr %s\n", attr->name.c_str() );
1896                        // } // for
1897                }
1898        | cfa_declaration ';'                                                           // CFA
1899        | static_assert                                                                         // C11
1900        ;
1901
1902static_assert:
1903        STATICASSERT '(' constant_expression ',' string_literal ')' ';' // C11
1904                { $$ = DeclarationNode::newStaticAssert( $3, maybeMoveBuild( $5 ) ); }
1905        | STATICASSERT '(' constant_expression ')' ';'          // CFA
1906                { $$ = DeclarationNode::newStaticAssert( $3, build_constantStr( yylloc, *new string( "\"\"" ) ) ); }
1907
1908// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
1909// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
1910// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
1911// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
1912// the declaration list (as in Pascal).  ANSI C and the new CFA declarations may appear together in the same program
1913// block, but cannot be mixed within a specific declaration.
1914//
1915//                      CFA                                     C
1916//              [10] int x;                     int x[10];              // array of 10 integers
1917//              [10] * char y;          char *y[10];    // array of 10 pointers to char
1918
1919cfa_declaration:                                                                                // CFA
1920        cfa_variable_declaration
1921        | cfa_typedef_declaration
1922        | cfa_function_declaration
1923        | type_declaring_list
1924                { SemanticError( yylloc, "otype declaration is currently unimplemented." ); $$ = nullptr; }
1925        | trait_specifier
1926        ;
1927
1928cfa_variable_declaration:                                                               // CFA
1929        cfa_variable_specifier initializer_opt
1930                { $$ = $1->addInitializer( $2 ); }
1931        | declaration_qualifier_list cfa_variable_specifier initializer_opt
1932                // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
1933                // them as a type_qualifier cannot appear in that context.
1934                { $$ = $2->addQualifiers( $1 )->addInitializer( $3 ); }
1935        | cfa_variable_declaration pop ',' push identifier_or_type_name initializer_opt
1936                { $$ = $1->appendList( $1->cloneType( $5 )->addInitializer( $6 ) ); }
1937        ;
1938
1939cfa_variable_specifier:                                                                 // CFA
1940                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1941                // storage-class
1942        cfa_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
1943                { $$ = $1->addName( $2 )->addAsmName( $3 ); }
1944        | cfa_abstract_tuple identifier_or_type_name asm_name_opt
1945                { $$ = $1->addName( $2 )->addAsmName( $3 ); }
1946        | type_qualifier_list cfa_abstract_tuple identifier_or_type_name asm_name_opt
1947                { $$ = $2->addQualifiers( $1 )->addName( $3 )->addAsmName( $4 ); }
1948        ;
1949
1950cfa_function_declaration:                                                               // CFA
1951        cfa_function_specifier
1952        | type_qualifier_list cfa_function_specifier
1953                { $$ = $2->addQualifiers( $1 ); }
1954        | declaration_qualifier_list cfa_function_specifier
1955                { $$ = $2->addQualifiers( $1 ); }
1956        | declaration_qualifier_list type_qualifier_list cfa_function_specifier
1957                { $$ = $3->addQualifiers( $1 )->addQualifiers( $2 ); }
1958        | cfa_function_declaration ',' identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')'
1959                {
1960                        // Append the return type at the start (left-hand-side) to each identifier in the list.
1961                        DeclarationNode * ret = new DeclarationNode;
1962                        ret->type = maybeClone( $1->type->base );
1963                        $$ = $1->appendList( DeclarationNode::newFunction( $3, ret, $6, nullptr ) );
1964                }
1965        ;
1966
1967cfa_function_specifier:                                                                 // CFA
1968//      '[' ']' identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' // S/R conflict
1969//              {
1970//                      $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, nullptr, true );
1971//              }
1972//      '[' ']' identifier '(' push cfa_parameter_ellipsis_list_opt pop ')'
1973//              {
1974//                      typedefTable.setNextIdentifier( *$5 );
1975//                      $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, nullptr, true );
1976//              }
1977//      | '[' ']' TYPEDEFname '(' push cfa_parameter_ellipsis_list_opt pop ')'
1978//              {
1979//                      typedefTable.setNextIdentifier( *$5 );
1980//                      $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, nullptr, true );
1981//              }
1982//      | '[' ']' typegen_name
1983                // identifier_or_type_name must be broken apart because of the sequence:
1984                //
1985                //   '[' ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
1986                //   '[' ']' type_specifier
1987                //
1988                // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1989                // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
1990        cfa_abstract_tuple identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' attribute_list_opt
1991                // To obtain LR(1 ), this rule must be factored out from function return type (see cfa_abstract_declarator).
1992                { $$ = DeclarationNode::newFunction( $2, $1, $5, nullptr )->addQualifiers( $8 ); }
1993        | cfa_function_return identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' attribute_list_opt
1994                { $$ = DeclarationNode::newFunction( $2, $1, $5, nullptr )->addQualifiers( $8 ); }
1995        ;
1996
1997cfa_function_return:                                                                    // CFA
1998        '[' push cfa_parameter_list pop ']'
1999                { $$ = DeclarationNode::newTuple( $3 ); }
2000        | '[' push cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ']'
2001                // To obtain LR(1 ), the last cfa_abstract_parameter_list is added into this flattened rule to lookahead to the ']'.
2002                { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
2003        ;
2004
2005cfa_typedef_declaration:                                                                // CFA
2006        TYPEDEF cfa_variable_specifier
2007                {
2008                        typedefTable.addToEnclosingScope( *$2->name, TYPEDEFname, "cfa_typedef_declaration 1" );
2009                        $$ = $2->addTypedef();
2010                }
2011        | TYPEDEF cfa_function_specifier
2012                {
2013                        typedefTable.addToEnclosingScope( *$2->name, TYPEDEFname, "cfa_typedef_declaration 2" );
2014                        $$ = $2->addTypedef();
2015                }
2016        | cfa_typedef_declaration pop ',' push identifier
2017                {
2018                        typedefTable.addToEnclosingScope( *$5, TYPEDEFname, "cfa_typedef_declaration 3" );
2019                        $$ = $1->appendList( $1->cloneType( $5 ) );
2020                }
2021        ;
2022
2023// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
2024// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
2025
2026typedef_declaration:
2027        TYPEDEF type_specifier declarator
2028                {
2029                        typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "typedef_declaration 1" );
2030                        if ( $2->type->forall || ($2->type->kind == TypeData::Aggregate && $2->type->aggregate.params) ) {
2031                                SemanticError( yylloc, "forall qualifier in typedef is currently unimplemented." ); $$ = nullptr;
2032                        } else $$ = $3->addType( $2 )->addTypedef(); // watchout frees $2 and $3
2033                }
2034        | typedef_declaration ',' declarator
2035                {
2036                        typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "typedef_declaration 2" );
2037                        $$ = $1->appendList( $1->cloneBaseType( $3 )->addTypedef() );
2038                }
2039        | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
2040                { SemanticError( yylloc, "Type qualifiers/specifiers before TYPEDEF is deprecated, move after TYPEDEF." ); $$ = nullptr; }
2041        | type_specifier TYPEDEF declarator
2042                { SemanticError( yylloc, "Type qualifiers/specifiers before TYPEDEF is deprecated, move after TYPEDEF." ); $$ = nullptr; }
2043        | type_specifier TYPEDEF type_qualifier_list declarator
2044                { SemanticError( yylloc, "Type qualifiers/specifiers before TYPEDEF is deprecated, move after TYPEDEF." ); $$ = nullptr; }
2045        ;
2046
2047typedef_expression:
2048                // deprecated GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
2049        TYPEDEF identifier '=' assignment_expression
2050                {
2051                        SemanticError( yylloc, "TYPEDEF expression is deprecated, use typeof(...) instead." ); $$ = nullptr;
2052                }
2053        | typedef_expression ',' identifier '=' assignment_expression
2054                {
2055                        SemanticError( yylloc, "TYPEDEF expression is deprecated, use typeof(...) instead." ); $$ = nullptr;
2056                }
2057        ;
2058
2059c_declaration:
2060        declaration_specifier declaring_list
2061                { $$ = distAttr( $1, $2 ); }
2062        | typedef_declaration
2063        | typedef_expression                                                            // deprecated GCC, naming expression type
2064        | sue_declaration_specifier
2065                {
2066                        assert( $1->type );
2067                        if ( $1->type->qualifiers.any() ) {                     // CV qualifiers ?
2068                                SemanticError( yylloc, "syntax error, useless type qualifier(s) in empty declaration." ); $$ = nullptr;
2069                        }
2070                        // enums are never empty declarations because there must have at least one enumeration.
2071                        if ( $1->type->kind == TypeData::AggregateInst && $1->storageClasses.any() ) { // storage class ?
2072                                SemanticError( yylloc, "syntax error, useless storage qualifier(s) in empty aggregate declaration." ); $$ = nullptr;
2073                        }
2074                }
2075        ;
2076
2077declaring_list:
2078                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
2079                // storage-class
2080        variable_declarator asm_name_opt initializer_opt
2081                { $$ = $1->addAsmName( $2 )->addInitializer( $3 ); }
2082        | variable_type_redeclarator asm_name_opt initializer_opt
2083                { $$ = $1->addAsmName( $2 )->addInitializer( $3 ); }
2084
2085        | general_function_declarator asm_name_opt
2086                { $$ = $1->addAsmName( $2 )->addInitializer( nullptr ); }
2087        | general_function_declarator asm_name_opt '=' VOID
2088                { $$ = $1->addAsmName( $2 )->addInitializer( new InitializerNode( true ) ); }
2089
2090        | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
2091                { $$ = $1->appendList( $4->addQualifiers( $3 )->addAsmName( $5 )->addInitializer( $6 ) ); }
2092        ;
2093
2094general_function_declarator:
2095        function_type_redeclarator
2096        | function_declarator
2097        ;
2098
2099declaration_specifier:                                                                  // type specifier + storage class
2100        basic_declaration_specifier
2101        | type_declaration_specifier
2102        | sue_declaration_specifier
2103        | sue_declaration_specifier invalid_types                       // invalid syntax rule
2104                {
2105                        SemanticError( yylloc, ::toString( "syntax error, expecting ';' at end of ",
2106                                $1->type->enumeration.name ? "enum" : ast::AggregateDecl::aggrString( $1->type->aggregate.kind ),
2107                                " declaration." ) );
2108                        $$ = nullptr;
2109                }
2110        ;
2111
2112invalid_types:
2113        aggregate_key
2114        | basic_type_name
2115        | indirect_type
2116        ;
2117
2118declaration_specifier_nobody:                                                   // type specifier + storage class - {...}
2119                // Preclude SUE declarations in restricted scopes:
2120                //
2121                //    int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
2122                //
2123                // because it is impossible to call f due to name equivalence.
2124        basic_declaration_specifier
2125        | sue_declaration_specifier_nobody
2126        | type_declaration_specifier
2127        ;
2128
2129type_specifier:                                                                                 // type specifier
2130        basic_type_specifier
2131        | sue_type_specifier
2132        | type_type_specifier
2133        ;
2134
2135type_specifier_nobody:                                                                  // type specifier - {...}
2136                // Preclude SUE declarations in restricted scopes:
2137                //
2138                //    int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
2139                //
2140                // because it is impossible to call f due to name equivalence.
2141        basic_type_specifier
2142        | sue_type_specifier_nobody
2143        | type_type_specifier
2144        ;
2145
2146type_qualifier_list_opt:                                                                // GCC, used in asm_statement
2147        // empty
2148                { $$ = nullptr; }
2149        | type_qualifier_list
2150        ;
2151
2152type_qualifier_list:
2153                // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
2154                //
2155                // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
2156                // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
2157                // appeared only once.
2158        type_qualifier
2159        | type_qualifier_list type_qualifier
2160                { $$ = $1->addQualifiers( $2 ); }
2161        ;
2162
2163type_qualifier:
2164        type_qualifier_name
2165        | attribute                                                                                     // trick handles most atrribute locations
2166        ;
2167
2168type_qualifier_name:
2169        CONST
2170                { $$ = DeclarationNode::newTypeQualifier( ast::CV::Const ); }
2171        | RESTRICT
2172                { $$ = DeclarationNode::newTypeQualifier( ast::CV::Restrict ); }
2173        | VOLATILE
2174                { $$ = DeclarationNode::newTypeQualifier( ast::CV::Volatile ); }
2175        | ATOMIC
2176                { $$ = DeclarationNode::newTypeQualifier( ast::CV::Atomic ); }
2177        | forall
2178                { $$ = DeclarationNode::newForall( $1 ); }
2179        ;
2180
2181forall:
2182        FORALL '(' type_parameter_list ')'                                      // CFA
2183                { $$ = $3; }
2184        ;
2185
2186declaration_qualifier_list:
2187        storage_class_list
2188        | type_qualifier_list storage_class_list                        // remaining OBSOLESCENT (see 2 )
2189                { $$ = $1->addQualifiers( $2 ); }
2190        | declaration_qualifier_list type_qualifier_list storage_class_list
2191                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2192        ;
2193
2194storage_class_list:
2195                // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
2196                // only one of each is specified, except for inline, which can appear with the others.
2197                //
2198                // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
2199                // specifiers in a declaration.
2200        storage_class
2201        | storage_class_list storage_class
2202                { $$ = $1->addQualifiers( $2 ); }
2203        ;
2204
2205storage_class:
2206        EXTERN
2207                { $$ = DeclarationNode::newStorageClass( ast::Storage::Extern ); }
2208        | STATIC
2209                { $$ = DeclarationNode::newStorageClass( ast::Storage::Static ); }
2210        | AUTO
2211                { $$ = DeclarationNode::newStorageClass( ast::Storage::Auto ); }
2212        | REGISTER
2213                { $$ = DeclarationNode::newStorageClass( ast::Storage::Register ); }
2214        | THREADLOCALGCC                                                                                // GCC
2215                { $$ = DeclarationNode::newStorageClass( ast::Storage::ThreadLocalGcc ); }
2216        | THREADLOCALC11                                                                                // C11
2217                { $$ = DeclarationNode::newStorageClass( ast::Storage::ThreadLocalC11 ); }
2218                // Put function specifiers here to simplify parsing rules, but separate them semantically.
2219        | INLINE                                                                                        // C99
2220                { $$ = DeclarationNode::newFuncSpecifier( ast::Function::Inline ); }
2221        | FORTRAN                                                                                       // C99
2222                { $$ = DeclarationNode::newFuncSpecifier( ast::Function::Fortran ); }
2223        | NORETURN                                                                                      // C11
2224                { $$ = DeclarationNode::newFuncSpecifier( ast::Function::Noreturn ); }
2225        ;
2226
2227basic_type_name:
2228        VOID
2229                { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
2230        | BOOL                                                                                          // C99
2231                { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
2232        | CHAR
2233                { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
2234        | INT
2235                { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
2236        | INT128
2237                { $$ = DeclarationNode::newBasicType( DeclarationNode::Int128 ); }
2238        | UINT128
2239                { $$ = DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ); }
2240        | FLOAT
2241                { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
2242        | DOUBLE
2243                { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
2244        | uuFLOAT80
2245                { $$ = DeclarationNode::newBasicType( DeclarationNode::uuFloat80 ); }
2246        | uuFLOAT128
2247                { $$ = DeclarationNode::newBasicType( DeclarationNode::uuFloat128 ); }
2248        | uFLOAT16
2249                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat16 ); }
2250        | uFLOAT32
2251                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat32 ); }
2252        | uFLOAT32X
2253                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat32x ); }
2254        | uFLOAT64
2255                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat64 ); }
2256        | uFLOAT64X
2257                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat64x ); }
2258        | uFLOAT128
2259                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat128 ); }
2260        | DECIMAL32
2261                { SemanticError( yylloc, "_Decimal32 is currently unimplemented." ); $$ = nullptr; }
2262        | DECIMAL64
2263                { SemanticError( yylloc, "_Decimal64 is currently unimplemented." ); $$ = nullptr; }
2264        | DECIMAL128
2265                { SemanticError( yylloc, "_Decimal128 is currently unimplemented." ); $$ = nullptr; }
2266        | COMPLEX                                                                                       // C99
2267                { $$ = DeclarationNode::newComplexType( DeclarationNode::Complex ); }
2268        | IMAGINARY                                                                                     // C99
2269                { $$ = DeclarationNode::newComplexType( DeclarationNode::Imaginary ); }
2270        | SIGNED
2271                { $$ = DeclarationNode::newSignedNess( DeclarationNode::Signed ); }
2272        | UNSIGNED
2273                { $$ = DeclarationNode::newSignedNess( DeclarationNode::Unsigned ); }
2274        | SHORT
2275                { $$ = DeclarationNode::newLength( DeclarationNode::Short ); }
2276        | LONG
2277                { $$ = DeclarationNode::newLength( DeclarationNode::Long ); }
2278        | VA_LIST                                                                                       // GCC, __builtin_va_list
2279                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Valist ); }
2280        | AUTO_TYPE
2281                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::AutoType ); }
2282        | vtable
2283        ;
2284
2285vtable_opt:
2286        // empty
2287                { $$ = nullptr; }
2288        | vtable
2289        ;
2290
2291vtable:
2292        VTABLE '(' type_name ')' default_opt
2293                { $$ = DeclarationNode::newVtableType( $3 ); }
2294                // { SemanticError( yylloc, "vtable is currently unimplemented." ); $$ = nullptr; }
2295        ;
2296
2297default_opt:
2298        // empty
2299                { $$ = nullptr; }
2300        | DEFAULT
2301                { SemanticError( yylloc, "vtable default is currently unimplemented." ); $$ = nullptr; }
2302        ;
2303
2304basic_declaration_specifier:
2305                // A semantic check is necessary for conflicting storage classes.
2306        basic_type_specifier
2307        | declaration_qualifier_list basic_type_specifier
2308                { $$ = $2->addQualifiers( $1 ); }
2309        | basic_declaration_specifier storage_class                     // remaining OBSOLESCENT (see 2)
2310                { $$ = $1->addQualifiers( $2 ); }
2311        | basic_declaration_specifier storage_class type_qualifier_list
2312                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2313        | basic_declaration_specifier storage_class basic_type_specifier
2314                { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
2315        ;
2316
2317basic_type_specifier:
2318        direct_type
2319                // Cannot have type modifiers, e.g., short, long, etc.
2320        | type_qualifier_list_opt indirect_type type_qualifier_list_opt
2321                { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
2322        ;
2323
2324direct_type:
2325        basic_type_name
2326        | type_qualifier_list basic_type_name
2327                { $$ = $2->addQualifiers( $1 ); }
2328        | direct_type type_qualifier
2329                { $$ = $1->addQualifiers( $2 ); }
2330        | direct_type basic_type_name
2331                { $$ = $1->addType( $2 ); }
2332        ;
2333
2334indirect_type:
2335        TYPEOF '(' type ')'                                                                     // GCC: typeof( x ) y;
2336                { $$ = $3; }
2337        | TYPEOF '(' comma_expression ')'                                       // GCC: typeof( a+b ) y;
2338                { $$ = DeclarationNode::newTypeof( $3 ); }
2339        | BASETYPEOF '(' type ')'                                                       // CFA: basetypeof( x ) y;
2340                { $$ = DeclarationNode::newTypeof( new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $3 ) ) ), true ); }
2341        | BASETYPEOF '(' comma_expression ')'                           // CFA: basetypeof( a+b ) y;
2342                { $$ = DeclarationNode::newTypeof( $3, true ); }
2343        | ZERO_T                                                                                        // CFA
2344                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Zero ); }
2345        | ONE_T                                                                                         // CFA
2346                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::One ); }
2347        ;
2348
2349sue_declaration_specifier:                                                              // struct, union, enum + storage class + type specifier
2350        sue_type_specifier
2351                {
2352                        // printf( "sue_declaration_specifier %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2353                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2354                        //   printf( "\tattr %s\n", attr->name.c_str() );
2355                        // } // for
2356                }
2357        | declaration_qualifier_list sue_type_specifier
2358                { $$ = $2->addQualifiers( $1 ); }
2359        | sue_declaration_specifier storage_class                       // remaining OBSOLESCENT (see 2)
2360                { $$ = $1->addQualifiers( $2 ); }
2361        | sue_declaration_specifier storage_class type_qualifier_list
2362                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2363        ;
2364
2365sue_type_specifier:                                                                             // struct, union, enum + type specifier
2366        elaborated_type
2367                {
2368                        // printf( "sue_type_specifier %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2369                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2370                        //   printf( "\tattr %s\n", attr->name.c_str() );
2371                        // } // for
2372                }
2373        | type_qualifier_list
2374                { if ( $1->type != nullptr && $1->type->forall ) forall = true; } // remember generic type
2375          elaborated_type
2376                { $$ = $3->addQualifiers( $1 ); }
2377        | sue_type_specifier type_qualifier
2378                {
2379                        if ( $2->type != nullptr && $2->type->forall ) forall = true; // remember generic type
2380                        $$ = $1->addQualifiers( $2 );
2381                }
2382        ;
2383
2384sue_declaration_specifier_nobody:                                               // struct, union, enum - {...} + storage class + type specifier
2385        sue_type_specifier_nobody
2386        | declaration_qualifier_list sue_type_specifier_nobody
2387                { $$ = $2->addQualifiers( $1 ); }
2388        | sue_declaration_specifier_nobody storage_class        // remaining OBSOLESCENT (see 2)
2389                { $$ = $1->addQualifiers( $2 ); }
2390        | sue_declaration_specifier_nobody storage_class type_qualifier_list
2391                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2392        ;
2393
2394sue_type_specifier_nobody:                                                              // struct, union, enum - {...} + type specifier
2395        elaborated_type_nobody
2396        | type_qualifier_list elaborated_type_nobody
2397                { $$ = $2->addQualifiers( $1 ); }
2398        | sue_type_specifier_nobody type_qualifier
2399                { $$ = $1->addQualifiers( $2 ); }
2400        ;
2401
2402type_declaration_specifier:
2403        type_type_specifier
2404        | declaration_qualifier_list type_type_specifier
2405                { $$ = $2->addQualifiers( $1 ); }
2406        | type_declaration_specifier storage_class                      // remaining OBSOLESCENT (see 2)
2407                { $$ = $1->addQualifiers( $2 ); }
2408        | type_declaration_specifier storage_class type_qualifier_list
2409                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2410        ;
2411
2412type_type_specifier:                                                                    // typedef types
2413        type_name
2414        | type_qualifier_list type_name
2415                { $$ = $2->addQualifiers( $1 ); }
2416        | type_type_specifier type_qualifier
2417                { $$ = $1->addQualifiers( $2 ); }
2418        ;
2419
2420type_name:
2421        TYPEDEFname
2422                { $$ = DeclarationNode::newFromTypedef( $1 ); }
2423        | '.' TYPEDEFname
2424                { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), DeclarationNode::newFromTypedef( $2 ) ); }
2425        | type_name '.' TYPEDEFname
2426                { $$ = DeclarationNode::newQualifiedType( $1, DeclarationNode::newFromTypedef( $3 ) ); }
2427        | typegen_name
2428        | '.' typegen_name
2429                { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), $2 ); }
2430        | type_name '.' typegen_name
2431                { $$ = DeclarationNode::newQualifiedType( $1, $3 ); }
2432        ;
2433
2434typegen_name:                                                                                   // CFA
2435        TYPEGENname
2436                { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
2437        | TYPEGENname '(' ')'
2438                { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
2439        | TYPEGENname '(' type_list ')'
2440                { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
2441        ;
2442
2443elaborated_type:                                                                                // struct, union, enum
2444        aggregate_type
2445                {
2446                        // printf( "elaborated_type %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2447                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2448                        //   printf( "\tattr %s\n", attr->name.c_str() );
2449                        // } // for
2450                }
2451        | enum_type
2452        ;
2453
2454elaborated_type_nobody:                                                                 // struct, union, enum - {...}
2455        aggregate_type_nobody
2456        | enum_type_nobody
2457        ;
2458
2459aggregate_type:                                                                                 // struct, union
2460        aggregate_key attribute_list_opt
2461                { forall = false; }                                                             // reset
2462          '{' field_declaration_list_opt '}' type_parameters_opt
2463                { $$ = DeclarationNode::newAggregate( $1, nullptr, $7, $5, true )->addQualifiers( $2 ); }
2464        | aggregate_key attribute_list_opt identifier
2465                {
2466                        typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname, "aggregate_type: 1" );
2467                        forall = false;                                                         // reset
2468                }
2469          '{' field_declaration_list_opt '}' type_parameters_opt
2470                {
2471                        $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
2472                }
2473        | aggregate_key attribute_list_opt TYPEDEFname          // unqualified type name
2474                {
2475                        typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname, "aggregate_type: 2" );
2476                        forall = false;                                                         // reset
2477                }
2478          '{' field_declaration_list_opt '}' type_parameters_opt
2479                {
2480                        DeclarationNode::newFromTypedef( $3 );
2481                        $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
2482                }
2483        | aggregate_key attribute_list_opt TYPEGENname          // unqualified type name
2484                {
2485                        typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname, "aggregate_type: 3" );
2486                        forall = false;                                                         // reset
2487                }
2488          '{' field_declaration_list_opt '}' type_parameters_opt
2489                {
2490                        DeclarationNode::newFromTypeGen( $3, nullptr );
2491                        $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
2492                }
2493        | aggregate_type_nobody
2494        ;
2495
2496type_parameters_opt:
2497        // empty
2498                { $$ = nullptr; }                                                               %prec '}'
2499        | '(' type_list ')'
2500                { $$ = $2; }
2501        ;
2502
2503aggregate_type_nobody:                                                                  // struct, union - {...}
2504        aggregate_key attribute_list_opt identifier
2505                {
2506                        typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname, "aggregate_type_nobody" );
2507                        forall = false;                                                         // reset
2508                        $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
2509                }
2510        | aggregate_key attribute_list_opt type_name
2511                {
2512                        forall = false;                                                         // reset
2513                        // Create new generic declaration with same name as previous forward declaration, where the IDENTIFIER is
2514                        // switched to a TYPEGENname. Link any generic arguments from typegen_name to new generic declaration and
2515                        // delete newFromTypeGen.
2516                        if ( $3->type->kind == TypeData::SymbolicInst && ! $3->type->symbolic.isTypedef ) {
2517                                $$ = $3->addQualifiers( $2 );
2518                        } else {
2519                                $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $3->type->symbolic.actuals, nullptr, false )->addQualifiers( $2 );
2520                                $3->type->symbolic.name = nullptr;                      // copied to $$
2521                                $3->type->symbolic.actuals = nullptr;
2522                                delete $3;
2523                        }
2524                }
2525        ;
2526
2527aggregate_key:
2528        aggregate_data
2529        | aggregate_control
2530        ;
2531
2532aggregate_data:
2533        STRUCT vtable_opt
2534                { $$ = ast::AggregateDecl::Struct; }
2535        | UNION
2536                { $$ = ast::AggregateDecl::Union; }
2537        | EXCEPTION                                                                                     // CFA
2538                { $$ = ast::AggregateDecl::Exception; }
2539          //            { SemanticError( yylloc, "exception aggregate is currently unimplemented." ); $$ = ast::AggregateDecl::NoAggregate; }
2540        ;
2541
2542aggregate_control:                                                                              // CFA
2543        MONITOR
2544                { $$ = ast::AggregateDecl::Monitor; }
2545        | MUTEX STRUCT
2546                { $$ = ast::AggregateDecl::Monitor; }
2547        | GENERATOR
2548                { $$ = ast::AggregateDecl::Generator; }
2549        | MUTEX GENERATOR
2550                {
2551                        SemanticError( yylloc, "monitor generator is currently unimplemented." );
2552                        $$ = ast::AggregateDecl::NoAggregate;
2553                }
2554        | COROUTINE
2555                { $$ = ast::AggregateDecl::Coroutine; }
2556        | MUTEX COROUTINE
2557                {
2558                        SemanticError( yylloc, "monitor coroutine is currently unimplemented." );
2559                        $$ = ast::AggregateDecl::NoAggregate;
2560                }
2561        | THREAD
2562                { $$ = ast::AggregateDecl::Thread; }
2563        | MUTEX THREAD
2564                {
2565                        SemanticError( yylloc, "monitor thread is currently unimplemented." );
2566                        $$ = ast::AggregateDecl::NoAggregate;
2567                }
2568        ;
2569
2570field_declaration_list_opt:
2571        // empty
2572                { $$ = nullptr; }
2573        | field_declaration_list_opt field_declaration
2574                { $$ = $1 ? $1->appendList( $2 ) : $2; }
2575        ;
2576
2577field_declaration:
2578        type_specifier field_declaring_list_opt ';'
2579                {
2580                        // printf( "type_specifier1 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2581                        $$ = fieldDecl( $1, $2 );
2582                        // printf( "type_specifier2 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2583                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2584                        //   printf( "\tattr %s\n", attr->name.c_str() );
2585                        // } // for
2586                }
2587        | type_specifier field_declaring_list_opt '}'           // invalid syntax rule
2588                {
2589                        SemanticError( yylloc, ::toString( "syntax error, expecting ';' at end of previous declaration." ) );
2590                        $$ = nullptr;
2591                }
2592        | EXTENSION type_specifier field_declaring_list_opt ';' // GCC
2593                { $$ = fieldDecl( $2, $3 ); distExt( $$ ); }
2594        | STATIC type_specifier field_declaring_list_opt ';' // CFA
2595                { SemanticError( yylloc, "STATIC aggregate field qualifier currently unimplemented." ); $$ = nullptr; }
2596        | INLINE type_specifier field_abstract_list_opt ';'     // CFA
2597                {
2598                        if ( ! $3 ) {                                                           // field declarator ?
2599                                $3 = DeclarationNode::newName( nullptr );
2600                        } // if
2601                        $3->inLine = true;
2602                        $$ = distAttr( $2, $3 );                                        // mark all fields in list
2603                        distInl( $3 );
2604                }
2605        | INLINE aggregate_control ';'                                          // CFA
2606                { SemanticError( yylloc, "INLINE aggregate control currently unimplemented." ); $$ = nullptr; }
2607        | typedef_declaration ';'                                                       // CFA
2608        | cfa_field_declaring_list ';'                                          // CFA, new style field declaration
2609        | EXTENSION cfa_field_declaring_list ';'                        // GCC
2610                { distExt( $2 ); $$ = $2; }                                             // mark all fields in list
2611        | INLINE cfa_field_abstract_list ';'                            // CFA, new style field declaration
2612                { $$ = $2; }                                                                    // mark all fields in list
2613        | cfa_typedef_declaration ';'                                           // CFA
2614        | static_assert                                                                         // C11
2615        ;
2616
2617field_declaring_list_opt:
2618        // empty
2619                { $$ = nullptr; }
2620        | field_declarator
2621        | field_declaring_list_opt ',' attribute_list_opt field_declarator
2622                { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
2623        ;
2624
2625field_declarator:
2626        bit_subrange_size                                                                       // C special case, no field name
2627                { $$ = DeclarationNode::newBitfield( $1 ); }
2628        | variable_declarator bit_subrange_size_opt
2629                // A semantic check is required to ensure bit_subrange only appears on integral types.
2630                { $$ = $1->addBitfield( $2 ); }
2631        | variable_type_redeclarator bit_subrange_size_opt
2632                // A semantic check is required to ensure bit_subrange only appears on integral types.
2633                { $$ = $1->addBitfield( $2 ); }
2634        | function_type_redeclarator bit_subrange_size_opt
2635                // A semantic check is required to ensure bit_subrange only appears on integral types.
2636                { $$ = $1->addBitfield( $2 ); }
2637        ;
2638
2639field_abstract_list_opt:
2640        // empty
2641                { $$ = nullptr; }
2642        | field_abstract
2643        | field_abstract_list_opt ',' attribute_list_opt field_abstract
2644                { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
2645        ;
2646
2647field_abstract:
2648                //      no bit fields
2649        variable_abstract_declarator
2650        ;
2651
2652cfa_field_declaring_list:                                                               // CFA, new style field declaration
2653        // bit-fields are handled by C declarations
2654        cfa_abstract_declarator_tuple identifier_or_type_name
2655                { $$ = $1->addName( $2 ); }
2656        | cfa_field_declaring_list ',' identifier_or_type_name
2657                { $$ = $1->appendList( $1->cloneType( $3 ) ); }
2658        ;
2659
2660cfa_field_abstract_list:                                                                // CFA, new style field declaration
2661        // bit-fields are handled by C declarations
2662        cfa_abstract_declarator_tuple
2663        | cfa_field_abstract_list ','
2664                { $$ = $1->appendList( $1->cloneType( 0 ) ); }
2665        ;
2666
2667bit_subrange_size_opt:
2668        // empty
2669                { $$ = nullptr; }
2670        | bit_subrange_size
2671        ;
2672
2673bit_subrange_size:
2674        ':' assignment_expression
2675                { $$ = $2; }
2676        ;
2677
2678enum_type:
2679        ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
2680                { $$ = DeclarationNode::newEnum( nullptr, $4, true, false )->addQualifiers( $2 ); }
2681        | ENUM attribute_list_opt '!' '{' enumerator_list comma_opt '}' // invalid syntax rule
2682                { SemanticError( yylloc, "syntax error, hiding '!' the enumerator names of an anonymous enumeration means the names are inaccessible." ); $$ = nullptr; }
2683        | ENUM attribute_list_opt identifier
2684                { typedefTable.makeTypedef( *$3, "enum_type 1" ); }
2685          hide_opt '{' enumerator_list comma_opt '}'
2686                { $$ = DeclarationNode::newEnum( $3, $7, true, false, nullptr, $5 )->addQualifiers( $2 ); }
2687        | ENUM attribute_list_opt typedef_name hide_opt '{' enumerator_list comma_opt '}' // unqualified type name
2688                { $$ = DeclarationNode::newEnum( $3->name, $6, true, false, nullptr, $4 )->addQualifiers( $2 ); }
2689        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt '{' enumerator_list comma_opt '}'
2690                {
2691                        if ( $3->storageClasses.val != 0 || $3->type->qualifiers.any() ) {
2692                                SemanticError( yylloc, "syntax error, storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." );
2693                        }
2694                        $$ = DeclarationNode::newEnum( nullptr, $7, true, true, $3 )->addQualifiers( $5 );
2695                }
2696        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt '!' '{' enumerator_list comma_opt '}' // unqualified type name
2697                { SemanticError( yylloc, "syntax error, hiding '!' the enumerator names of an anonymous enumeration means the names are inaccessible." ); $$ = nullptr; }
2698        | ENUM '(' ')' attribute_list_opt '{' enumerator_list comma_opt '}'
2699                {
2700                        $$ = DeclarationNode::newEnum( nullptr, $6, true, true )->addQualifiers( $4 );
2701                }
2702        | ENUM '(' ')' attribute_list_opt '!' '{' enumerator_list comma_opt '}' // invalid syntax rule
2703                { SemanticError( yylloc, "syntax error, hiding '!' the enumerator names of an anonymous enumeration means the names are inaccessible." ); $$ = nullptr; }
2704        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt
2705                {
2706                        if ( $3 && ($3->storageClasses.any() || $3->type->qualifiers.val != 0 )) {
2707                                SemanticError( yylloc, "syntax error, storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." );
2708                        }
2709                        typedefTable.makeTypedef( *$6, "enum_type 2" );
2710                }
2711          hide_opt '{' enumerator_list comma_opt '}'
2712                {
2713                        $$ = DeclarationNode::newEnum( $6, $11, true, true, $3, $9 )->addQualifiers( $5 )->addQualifiers( $7 );
2714                }
2715        | ENUM '(' ')' attribute_list_opt identifier attribute_list_opt hide_opt '{' enumerator_list comma_opt '}'
2716                {
2717                        $$ = DeclarationNode::newEnum( $5, $9, true, true, nullptr, $7 )->addQualifiers( $4 )->addQualifiers( $6 );
2718                }
2719        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt typedef_name attribute_list_opt hide_opt '{' enumerator_list comma_opt '}'
2720                {
2721                        $$ = DeclarationNode::newEnum( $6->name, $10, true, true, $3, $8 )->addQualifiers( $5 )->addQualifiers( $7 );
2722                }
2723        | ENUM '(' ')' attribute_list_opt typedef_name attribute_list_opt hide_opt '{' enumerator_list comma_opt '}'
2724                {
2725                        $$ = DeclarationNode::newEnum( $5->name, $9, true, true, nullptr, $7 )->addQualifiers( $4 )->addQualifiers( $6 );
2726                }
2727        | enum_type_nobody
2728        ;
2729
2730hide_opt:
2731        // empty
2732                { $$ = EnumHiding::Visible; }
2733        | '!'
2734                { $$ = EnumHiding::Hide; }
2735        ;
2736
2737enum_type_nobody:                                                                               // enum - {...}
2738        ENUM attribute_list_opt identifier
2739                {
2740                        typedefTable.makeTypedef( *$3, "enum_type_nobody 1" );
2741                        $$ = DeclarationNode::newEnum( $3, nullptr, false, false )->addQualifiers( $2 );
2742                }
2743        | ENUM attribute_list_opt type_name
2744                {
2745                        typedefTable.makeTypedef( *$3->type->symbolic.name, "enum_type_nobody 2" );
2746                        $$ = DeclarationNode::newEnum( $3->type->symbolic.name, nullptr, false, false )->addQualifiers( $2 );
2747                }
2748        ;
2749
2750enumerator_list:
2751        visible_hide_opt identifier_or_type_name enumerator_value_opt
2752                { $$ = DeclarationNode::newEnumValueGeneric( $2, $3 ); }
2753        | INLINE type_name
2754                { $$ = DeclarationNode::newEnumInLine( *$2->type->symbolic.name ); }
2755        | enumerator_list ',' visible_hide_opt identifier_or_type_name enumerator_value_opt
2756                { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( $4, $5 ) ); }
2757        | enumerator_list ',' INLINE type_name enumerator_value_opt
2758                { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ) ); }
2759        ;
2760
2761visible_hide_opt:
2762        hide_opt
2763        | '^'
2764                { $$ = EnumHiding::Visible; }
2765        ;
2766
2767enumerator_value_opt:
2768        // empty
2769                { $$ = nullptr; }
2770        | '=' constant_expression                                       { $$ = new InitializerNode( $2 ); }
2771        | '=' '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $3, true ); }
2772        // | simple_assignment_operator initializer
2773        //      { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
2774        ;
2775
2776cfa_parameter_ellipsis_list_opt:                                                // CFA, abstract + real
2777        // empty
2778                { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
2779        | ELLIPSIS
2780                { $$ = nullptr; }
2781        | cfa_abstract_parameter_list
2782        | cfa_parameter_list
2783        | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
2784                { $$ = $1->appendList( $5 ); }
2785        | cfa_abstract_parameter_list pop ',' push ELLIPSIS
2786                { $$ = $1->addVarArgs(); }
2787        | cfa_parameter_list pop ',' push ELLIPSIS
2788                { $$ = $1->addVarArgs(); }
2789        ;
2790
2791cfa_parameter_list:                                                                             // CFA
2792                // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
2793                // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
2794        cfa_parameter_declaration
2795        | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
2796                { $$ = $1->appendList( $5 ); }
2797        | cfa_parameter_list pop ',' push cfa_parameter_declaration
2798                { $$ = $1->appendList( $5 ); }
2799        | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
2800                { $$ = $1->appendList( $5 )->appendList( $9 ); }
2801        ;
2802
2803cfa_abstract_parameter_list:                                                    // CFA, new & old style abstract
2804        cfa_abstract_parameter_declaration
2805        | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
2806                { $$ = $1->appendList( $5 ); }
2807        ;
2808
2809parameter_type_list_opt:
2810        // empty
2811                { $$ = nullptr; }
2812        | ELLIPSIS
2813                { $$ = nullptr; }
2814        | parameter_list
2815        | parameter_list ',' ELLIPSIS
2816                { $$ = $1->addVarArgs(); }
2817        ;
2818
2819parameter_list:                                                                                 // abstract + real
2820        abstract_parameter_declaration
2821        | parameter_declaration
2822        | parameter_list ',' abstract_parameter_declaration
2823                { $$ = $1->appendList( $3 ); }
2824        | parameter_list ',' parameter_declaration
2825                { $$ = $1->appendList( $3 ); }
2826        ;
2827
2828// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
2829// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
2830
2831cfa_parameter_declaration:                                                              // CFA, new & old style parameter declaration
2832        parameter_declaration
2833        | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initializer_opt
2834                { $$ = $1->addName( $2 ); }
2835        | cfa_abstract_tuple identifier_or_type_name default_initializer_opt
2836                // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2837                { $$ = $1->addName( $2 ); }
2838        | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initializer_opt
2839                { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
2840        | cfa_function_specifier
2841        ;
2842
2843cfa_abstract_parameter_declaration:                                             // CFA, new & old style parameter declaration
2844        abstract_parameter_declaration
2845        | cfa_identifier_parameter_declarator_no_tuple
2846        | cfa_abstract_tuple
2847                // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2848        | type_qualifier_list cfa_abstract_tuple
2849                { $$ = $2->addQualifiers( $1 ); }
2850        | cfa_abstract_function
2851        ;
2852
2853parameter_declaration:
2854                // No SUE declaration in parameter list.
2855        declaration_specifier_nobody identifier_parameter_declarator default_initializer_opt
2856                { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2857        | declaration_specifier_nobody type_parameter_redeclarator default_initializer_opt
2858                { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2859        ;
2860
2861abstract_parameter_declaration:
2862        declaration_specifier_nobody default_initializer_opt
2863                { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
2864        | declaration_specifier_nobody abstract_parameter_declarator default_initializer_opt
2865                { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2866        ;
2867
2868// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
2869// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
2870// identifiers.  The ANSI-style parameter-list can redefine a typedef name.
2871
2872identifier_list:                                                                                // K&R-style parameter list => no types
2873        identifier
2874                { $$ = DeclarationNode::newName( $1 ); }
2875        | identifier_list ',' identifier
2876                { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
2877        ;
2878
2879identifier_or_type_name:
2880        identifier
2881        | TYPEDEFname
2882        | TYPEGENname
2883        ;
2884
2885type_no_function:                                                                               // sizeof, alignof, cast (constructor)
2886        cfa_abstract_declarator_tuple                                           // CFA
2887        | type_specifier                                                                        // cannot be type_specifier_nobody, e.g., (struct S {}){} is a thing
2888        | type_specifier abstract_declarator
2889                { $$ = $2->addType( $1 ); }
2890        ;
2891
2892type:                                                                                                   // typeof, assertion
2893        type_no_function
2894        | cfa_abstract_function                                                         // CFA
2895        ;
2896
2897initializer_opt:
2898        // empty
2899                { $$ = nullptr; }
2900        | simple_assignment_operator initializer        { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
2901        | '=' VOID                                                                      { $$ = new InitializerNode( true ); }
2902        | '{' initializer_list_opt comma_opt '}'        { $$ = new InitializerNode( $2, true ); }
2903        ;
2904
2905initializer:
2906        assignment_expression                                           { $$ = new InitializerNode( $1 ); }
2907        | '{' initializer_list_opt comma_opt '}'        { $$ = new InitializerNode( $2, true ); }
2908        ;
2909
2910initializer_list_opt:
2911        // empty
2912                { $$ = nullptr; }
2913        | initializer
2914        | designation initializer                                       { $$ = $2->set_designators( $1 ); }
2915        | initializer_list_opt ',' initializer          { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
2916        | initializer_list_opt ',' designation initializer { $$ = (InitializerNode *)($1->set_last( $4->set_designators( $3 ) )); }
2917        ;
2918
2919// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
2920// '=' to separator the designator from the initializer value, as in:
2921//
2922//              int x[10] = { [1] = 3 };
2923//
2924// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment.  To disambiguate this case, CFA
2925// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
2926// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
2927// shift/reduce conflicts
2928
2929designation:
2930        designator_list ':'                                                                     // C99, CFA uses ":" instead of "="
2931        | identifier_at ':'                                                                     // GCC, field name
2932                { $$ = new ExpressionNode( build_varref( yylloc, $1 ) ); }
2933        ;
2934
2935designator_list:                                                                                // C99
2936        designator
2937        | designator_list designator
2938                { $$ = (ExpressionNode *)($1->set_last( $2 )); }
2939        //| designator_list designator                                          { $$ = new ExpressionNode( $1, $2 ); }
2940        ;
2941
2942designator:
2943        '.' identifier_at                                                                       // C99, field name
2944                { $$ = new ExpressionNode( build_varref( yylloc, $2 ) ); }
2945        | '[' push assignment_expression pop ']'                        // C99, single array element
2946                // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
2947                { $$ = $3; }
2948        | '[' push subrange pop ']'                                                     // CFA, multiple array elements
2949                { $$ = $3; }
2950        | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
2951                { $$ = new ExpressionNode( new ast::RangeExpr( yylloc, maybeMoveBuild( $3 ), maybeMoveBuild( $5 ) ) ); }
2952        | '.' '[' push field_name_list pop ']'                          // CFA, tuple field selector
2953                { $$ = $4; }
2954        ;
2955
2956// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
2957// rather than an object-oriented type system. This required four groups of extensions:
2958//
2959// Overloading: function, data, and operator identifiers may be overloaded.
2960//
2961// Type declarations: "otype" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
2962//     and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
2963//     definitions of new types. Type declarations with storage class "extern" provide opaque types.
2964//
2965// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
2966//     site. A polymorphic function is not a template; it is a function, with an address and a type.
2967//
2968// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
2969//     types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
2970//     hierarchies. Unlike classes, they can define relationships between types.  Assertions declare that a type or
2971//     types provide the operations declared by a specification.  Assertions are normally used to declare requirements
2972//     on type arguments of polymorphic functions.
2973
2974type_parameter_list:                                                                    // CFA
2975        type_parameter
2976        | type_parameter_list ',' type_parameter
2977                { $$ = $1->appendList( $3 ); }
2978        ;
2979
2980type_initializer_opt:                                                                   // CFA
2981        // empty
2982                { $$ = nullptr; }
2983        | '=' type
2984                { $$ = $2; }
2985        ;
2986
2987type_parameter:                                                                                 // CFA
2988        type_class identifier_or_type_name
2989                {
2990                        typedefTable.addToScope( *$2, TYPEDEFname, "type_parameter 1" );
2991                        if ( $1 == ast::TypeDecl::Otype ) { SemanticError( yylloc, "otype keyword is deprecated, use T " ); }
2992                        if ( $1 == ast::TypeDecl::Dtype ) { SemanticError( yylloc, "dtype keyword is deprecated, use T &" ); }
2993                        if ( $1 == ast::TypeDecl::Ttype ) { SemanticError( yylloc, "ttype keyword is deprecated, use T ..." ); }
2994                }
2995          type_initializer_opt assertion_list_opt
2996                { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2997        | identifier_or_type_name new_type_class
2998                { typedefTable.addToScope( *$1, TYPEDEFname, "type_parameter 2" ); }
2999          type_initializer_opt assertion_list_opt
3000                { $$ = DeclarationNode::newTypeParam( $2, $1 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
3001        | '[' identifier_or_type_name ']'
3002                {
3003                        typedefTable.addToScope( *$2, TYPEDIMname, "type_parameter 3" );
3004                        $$ = DeclarationNode::newTypeParam( ast::TypeDecl::Dimension, $2 );
3005                }
3006        // | type_specifier identifier_parameter_declarator
3007        | assertion_list
3008                { $$ = DeclarationNode::newTypeParam( ast::TypeDecl::Dtype, new string( DeclarationNode::anonymous.newName() ) )->addAssertions( $1 ); }
3009        ;
3010
3011new_type_class:                                                                                 // CFA
3012        // empty
3013                { $$ = ast::TypeDecl::Otype; }
3014        | '&'
3015                { $$ = ast::TypeDecl::Dtype; }
3016        | '*'
3017                { $$ = ast::TypeDecl::DStype; }                                         // dtype + sized
3018        // | '(' '*' ')'
3019        //      { $$ = ast::TypeDecl::Ftype; }
3020        | ELLIPSIS
3021                { $$ = ast::TypeDecl::Ttype; }
3022        ;
3023
3024type_class:                                                                                             // CFA
3025        OTYPE
3026                { $$ = ast::TypeDecl::Otype; }
3027        | DTYPE
3028                { $$ = ast::TypeDecl::Dtype; }
3029        | FTYPE
3030                { $$ = ast::TypeDecl::Ftype; }
3031        | TTYPE
3032                { $$ = ast::TypeDecl::Ttype; }
3033        ;
3034
3035assertion_list_opt:                                                                             // CFA
3036        // empty
3037                { $$ = nullptr; }
3038        | assertion_list
3039        ;
3040
3041assertion_list:                                                                                 // CFA
3042        assertion
3043        | assertion_list assertion
3044                { $$ = $1->appendList( $2 ); }
3045        ;
3046
3047assertion:                                                                                              // CFA
3048        '|' identifier_or_type_name '(' type_list ')'
3049                { $$ = DeclarationNode::newTraitUse( $2, $4 ); }
3050        | '|' '{' push trait_declaration_list pop '}'
3051                { $$ = $4; }
3052        // | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list pop '}' '(' type_list ')'
3053        //      { SemanticError( yylloc, "Generic data-type assertion is currently unimplemented." ); $$ = nullptr; }
3054        ;
3055
3056type_list:                                                                                              // CFA
3057        type
3058                { $$ = new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $1 ) ) ); }
3059        | assignment_expression
3060        | type_list ',' type
3061                { $$ = (ExpressionNode *)($1->set_last( new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $3 ) ) ) )); }
3062        | type_list ',' assignment_expression
3063                { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
3064        ;
3065
3066type_declaring_list:                                                                    // CFA
3067        OTYPE type_declarator
3068                { $$ = $2; }
3069        | storage_class_list OTYPE type_declarator
3070                { $$ = $3->addQualifiers( $1 ); }
3071        | type_declaring_list ',' type_declarator
3072                { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
3073        ;
3074
3075type_declarator:                                                                                // CFA
3076        type_declarator_name assertion_list_opt
3077                { $$ = $1->addAssertions( $2 ); }
3078        | type_declarator_name assertion_list_opt '=' type
3079                { $$ = $1->addAssertions( $2 )->addType( $4 ); }
3080        ;
3081
3082type_declarator_name:                                                                   // CFA
3083        identifier_or_type_name
3084                {
3085                        typedefTable.addToEnclosingScope( *$1, TYPEDEFname, "type_declarator_name 1" );
3086                        $$ = DeclarationNode::newTypeDecl( $1, nullptr );
3087                }
3088        | identifier_or_type_name '(' type_parameter_list ')'
3089                {
3090                        typedefTable.addToEnclosingScope( *$1, TYPEGENname, "type_declarator_name 2" );
3091                        $$ = DeclarationNode::newTypeDecl( $1, $3 );
3092                }
3093        ;
3094
3095trait_specifier:                                                                                // CFA
3096        TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' '}'
3097                {
3098                        SemanticWarning( yylloc, Warning::DeprecTraitSyntax );
3099                        $$ = DeclarationNode::newTrait( $2, $4, nullptr );
3100                }
3101        | forall TRAIT identifier_or_type_name '{' '}'          // alternate
3102                { $$ = DeclarationNode::newTrait( $3, $1, nullptr ); }
3103        | TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' push trait_declaration_list pop '}'
3104                {
3105                        SemanticWarning( yylloc, Warning::DeprecTraitSyntax );
3106                        $$ = DeclarationNode::newTrait( $2, $4, $8 );
3107                }
3108        | forall TRAIT identifier_or_type_name '{' push trait_declaration_list pop '}' // alternate
3109                { $$ = DeclarationNode::newTrait( $3, $1, $6 ); }
3110        ;
3111
3112trait_declaration_list:                                                                 // CFA
3113        trait_declaration
3114        | trait_declaration_list pop push trait_declaration
3115                { $$ = $1->appendList( $4 ); }
3116        ;
3117
3118trait_declaration:                                                                              // CFA
3119        cfa_trait_declaring_list ';'
3120        | trait_declaring_list ';'
3121        ;
3122
3123cfa_trait_declaring_list:                                                               // CFA
3124        cfa_variable_specifier
3125        | cfa_function_specifier
3126        | cfa_trait_declaring_list pop ',' push identifier_or_type_name
3127                { $$ = $1->appendList( $1->cloneType( $5 ) ); }
3128        ;
3129
3130trait_declaring_list:                                                                   // CFA
3131        type_specifier declarator
3132                { $$ = $2->addType( $1 ); }
3133        | trait_declaring_list pop ',' push declarator
3134                { $$ = $1->appendList( $1->cloneBaseType( $5 ) ); }
3135        ;
3136
3137// **************************** EXTERNAL DEFINITIONS *****************************
3138
3139translation_unit:
3140        // empty, input file
3141        | external_definition_list
3142                { parseTree = parseTree ? parseTree->appendList( $1 ) : $1;     }
3143        ;
3144
3145external_definition_list:
3146        push external_definition pop
3147                { $$ = $2; }
3148        | external_definition_list push external_definition pop
3149                { $$ = $1 ? $1->appendList( $3 ) : $3; }
3150        ;
3151
3152external_definition_list_opt:
3153        // empty
3154                { $$ = nullptr; }
3155        | external_definition_list
3156        ;
3157
3158up:
3159                { typedefTable.up( forall ); forall = false; }
3160        ;
3161
3162down:
3163                { typedefTable.down(); }
3164        ;
3165
3166external_definition:
3167        DIRECTIVE
3168                { $$ = DeclarationNode::newDirectiveStmt( new StatementNode( build_directive( yylloc, $1 ) ) ); }
3169        | declaration
3170                {
3171                        // Variable declarations of anonymous types requires creating a unique type-name across multiple translation
3172                        // unit, which is a dubious task, especially because C uses name rather than structural typing; hence it is
3173                        // disallowed at the moment.
3174                        if ( $1->linkage == ast::Linkage::Cforall && ! $1->storageClasses.is_static && $1->type && $1->type->kind == TypeData::AggregateInst ) {
3175                                if ( $1->type->aggInst.aggregate->kind == TypeData::Enum && $1->type->aggInst.aggregate->enumeration.anon ) {
3176                                        SemanticError( yylloc, "extern anonymous enumeration is currently unimplemented." ); $$ = nullptr;
3177                                } else if ( $1->type->aggInst.aggregate->aggregate.anon ) { // handles struct or union
3178                                        SemanticError( yylloc, "extern anonymous struct/union is currently unimplemented." ); $$ = nullptr;
3179                                }
3180                        }
3181                }
3182        | IDENTIFIER IDENTIFIER
3183                { IdentifierBeforeIdentifier( *$1.str, *$2.str, " declaration" ); $$ = nullptr; }
3184        | IDENTIFIER type_qualifier                                                     // invalid syntax rule
3185                { IdentifierBeforeType( *$1.str, "type qualifier" ); $$ = nullptr; }
3186        | IDENTIFIER storage_class                                                      // invalid syntax rule
3187                { IdentifierBeforeType( *$1.str, "storage class" ); $$ = nullptr; }
3188        | IDENTIFIER basic_type_name                                            // invalid syntax rule
3189                { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
3190        | IDENTIFIER TYPEDEFname                                                        // invalid syntax rule
3191                { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
3192        | IDENTIFIER TYPEGENname                                                        // invalid syntax rule
3193                { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
3194        | external_function_definition
3195        | EXTENSION external_definition                                         // GCC, multiple __extension__ allowed, meaning unknown
3196                {
3197                        distExt( $2 );                                                          // mark all fields in list
3198                        $$ = $2;
3199                }
3200        | ASM '(' string_literal ')' ';'                                        // GCC, global assembler statement
3201                { $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asm( yylloc, false, $3, nullptr ) ) ); }
3202        | EXTERN STRINGliteral
3203                {
3204                        linkageStack.push( linkage );                           // handle nested extern "C"/"Cforall"
3205                        linkage = ast::Linkage::update( yylloc, linkage, $2 );
3206                }
3207          up external_definition down
3208                {
3209                        linkage = linkageStack.top();
3210                        linkageStack.pop();
3211                        $$ = $5;
3212                }
3213        | EXTERN STRINGliteral                                                          // C++-style linkage specifier
3214                {
3215                        linkageStack.push( linkage );                           // handle nested extern "C"/"Cforall"
3216                        linkage = ast::Linkage::update( yylloc, linkage, $2 );
3217                }
3218          '{' up external_definition_list_opt down '}'
3219                {
3220                        linkage = linkageStack.top();
3221                        linkageStack.pop();
3222                        $$ = $6;
3223                }
3224        // global distribution
3225        | type_qualifier_list
3226                {
3227                        if ( $1->type->qualifiers.any() ) {
3228                                SemanticError( yylloc, "syntax error, CV qualifiers cannot be distributed; only storage-class and forall qualifiers." );
3229                        }
3230                        if ( $1->type->forall ) forall = true;          // remember generic type
3231                }
3232          '{' up external_definition_list_opt down '}'          // CFA, namespace
3233                {
3234                        distQual( $5, $1 );
3235                        forall = false;
3236                        $$ = $5;
3237                }
3238        | declaration_qualifier_list
3239                {
3240                        if ( $1->type && $1->type->qualifiers.any() ) {
3241                                SemanticError( yylloc, "syntax error, CV qualifiers cannot be distributed; only storage-class and forall qualifiers." );
3242                        }
3243                        if ( $1->type && $1->type->forall ) forall = true; // remember generic type
3244                }
3245          '{' up external_definition_list_opt down '}'          // CFA, namespace
3246                {
3247                        distQual( $5, $1 );
3248                        forall = false;
3249                        $$ = $5;
3250                }
3251        | declaration_qualifier_list type_qualifier_list
3252                {
3253                        if ( ($1->type && $1->type->qualifiers.any()) || ($2->type && $2->type->qualifiers.any()) ) {
3254                                SemanticError( yylloc, "syntax error, CV qualifiers cannot be distributed; only storage-class and forall qualifiers." );
3255                        }
3256                        if ( ($1->type && $1->type->forall) || ($2->type && $2->type->forall) ) forall = true; // remember generic type
3257                }
3258          '{' up external_definition_list_opt down '}'          // CFA, namespace
3259                {
3260                        distQual( $6, $1->addQualifiers( $2 ) );
3261                        forall = false;
3262                        $$ = $6;
3263                }
3264        ;
3265
3266external_function_definition:
3267        function_definition
3268                // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
3269                // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
3270                // Parsing is possible because function_definition does not appear in the context of an expression (nested
3271                // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
3272                // declaration must still have a type_specifier.  OBSOLESCENT (see 1)
3273        | function_declarator compound_statement
3274                { $$ = $1->addFunctionBody( $2 ); }
3275        | KR_function_declarator KR_parameter_list_opt compound_statement
3276                { $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 ); }
3277        ;
3278
3279with_clause_opt:
3280        // empty
3281                { $$ = nullptr; forall = false; }
3282        | WITH '(' tuple_expression_list ')' attribute_list_opt
3283                {
3284                        $$ = $3; forall = false;
3285                        if ( $5 ) {
3286                                SemanticError( yylloc, "syntax error, attributes cannot be associated with function body. Move attribute(s) before \"with\" clause." );
3287                                $$ = nullptr;
3288                        } // if
3289                }
3290        ;
3291
3292function_definition:
3293        cfa_function_declaration with_clause_opt compound_statement     // CFA
3294                {
3295                        // Add the function body to the last identifier in the function definition list, i.e., foo3:
3296                        //   [const double] foo1(), foo2( int ), foo3( double ) { return 3.0; }
3297                        $1->get_last()->addFunctionBody( $3, $2 );
3298                        $$ = $1;
3299                }
3300        | declaration_specifier function_declarator with_clause_opt compound_statement
3301                {
3302                        rebindForall( $1, $2 );
3303                        $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
3304                }
3305        | declaration_specifier function_type_redeclarator with_clause_opt compound_statement
3306                {
3307                        rebindForall( $1, $2 );
3308                        $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
3309                }
3310                // handles default int return type, OBSOLESCENT (see 1)
3311        | type_qualifier_list function_declarator with_clause_opt compound_statement
3312                { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
3313                // handles default int return type, OBSOLESCENT (see 1)
3314        | declaration_qualifier_list function_declarator with_clause_opt compound_statement
3315                { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
3316                // handles default int return type, OBSOLESCENT (see 1)
3317        | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
3318                { $$ = $3->addFunctionBody( $5, $4 )->addQualifiers( $2 )->addQualifiers( $1 ); }
3319
3320                // Old-style K&R function definition, OBSOLESCENT (see 4)
3321        | declaration_specifier KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3322                {
3323                        rebindForall( $1, $2 );
3324                        $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addType( $1 );
3325                }
3326                // handles default int return type, OBSOLESCENT (see 1)
3327        | type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3328                { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
3329                // handles default int return type, OBSOLESCENT (see 1)
3330        | declaration_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3331                { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
3332                // handles default int return type, OBSOLESCENT (see 1)
3333        | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3334                { $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6, $5 )->addQualifiers( $2 )->addQualifiers( $1 ); }
3335        ;
3336
3337declarator:
3338        variable_declarator
3339        | variable_type_redeclarator
3340        | function_declarator
3341        | function_type_redeclarator
3342        ;
3343
3344subrange:
3345        constant_expression '~' constant_expression                     // CFA, integer subrange
3346                { $$ = new ExpressionNode( new ast::RangeExpr( yylloc, maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
3347        ;
3348
3349asm_name_opt:                                                                                   // GCC
3350        // empty
3351                { $$ = nullptr; }
3352        | ASM '(' string_literal ')' attribute_list_opt
3353                {
3354                        DeclarationNode * name = new DeclarationNode();
3355                        name->asmName = maybeMoveBuild( $3 );
3356                        $$ = name->addQualifiers( $5 );
3357                }
3358        ;
3359
3360attribute_list_opt:                                                                             // GCC
3361        // empty
3362                { $$ = nullptr; }
3363        | attribute_list
3364        ;
3365
3366attribute_list:                                                                                 // GCC
3367        attribute
3368        | attribute_list attribute
3369                { $$ = $2->addQualifiers( $1 ); }
3370        ;
3371
3372attribute:                                                                                              // GCC
3373        ATTRIBUTE '(' '(' attribute_name_list ')' ')'
3374                { $$ = $4; }
3375        ;
3376
3377attribute_name_list:                                                                    // GCC
3378        attribute_name
3379        | attribute_name_list ',' attribute_name
3380                { $$ = $3->addQualifiers( $1 ); }
3381        ;
3382
3383attribute_name:                                                                                 // GCC
3384        // empty
3385                { $$ = nullptr; }
3386        | attr_name
3387                { $$ = DeclarationNode::newAttribute( $1 ); }
3388        | attr_name '(' argument_expression_list_opt ')'
3389                { $$ = DeclarationNode::newAttribute( $1, $3 ); }
3390        ;
3391
3392attr_name:                                                                                              // GCC
3393        IDENTIFIER
3394        | quasi_keyword
3395        | TYPEDEFname
3396        | TYPEGENname
3397        | FALLTHROUGH
3398                { $$ = Token{ new string( "fallthrough" ), { nullptr, -1 } }; }
3399        | CONST
3400                { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
3401        ;
3402
3403// ============================================================================
3404// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
3405// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
3406// in:
3407//
3408//              int (*f())[10] { ... };
3409//              ... (*f())[3] += 1;             // definition mimics usage
3410//
3411// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
3412// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
3413// ============================================================================
3414
3415// ----------------------------------------------------------------------------
3416// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
3417// to define a variable or function prototype, e.g.:
3418//
3419//              valid declaration               invalid definition
3420//              -----------------               ------------------
3421//              int f;                                  int f {}
3422//              int *f;                                 int *f {}
3423//              int f[10];                              int f[10] {}
3424//              int (*f)(int);                  int (*f)(int) {}
3425//
3426// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
3427// variable_declarator and function_declarator.
3428// ----------------------------------------------------------------------------
3429
3430// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
3431// declaring an array of functions versus a pointer to an array of functions.
3432
3433paren_identifier:
3434        identifier_at
3435                { $$ = DeclarationNode::newName( $1 ); }
3436        | '(' paren_identifier ')'                                                      // redundant parenthesis
3437                { $$ = $2; }
3438        ;
3439
3440variable_declarator:
3441        paren_identifier attribute_list_opt
3442                { $$ = $1->addQualifiers( $2 ); }
3443        | variable_ptr
3444        | variable_array attribute_list_opt
3445                { $$ = $1->addQualifiers( $2 ); }
3446        | variable_function attribute_list_opt
3447                { $$ = $1->addQualifiers( $2 ); }
3448        ;
3449
3450variable_ptr:
3451        ptrref_operator variable_declarator
3452                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3453        | ptrref_operator type_qualifier_list variable_declarator
3454                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3455        | '(' variable_ptr ')' attribute_list_opt                       // redundant parenthesis
3456                { $$ = $2->addQualifiers( $4 ); }
3457        | '(' attribute_list variable_ptr ')' attribute_list_opt // redundant parenthesis
3458                { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3459        ;
3460
3461variable_array:
3462        paren_identifier array_dimension
3463                { $$ = $1->addArray( $2 ); }
3464        | '(' variable_ptr ')' array_dimension
3465                { $$ = $2->addArray( $4 ); }
3466        | '(' attribute_list variable_ptr ')' array_dimension
3467                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3468        | '(' variable_array ')' multi_array_dimension          // redundant parenthesis
3469                { $$ = $2->addArray( $4 ); }
3470        | '(' attribute_list variable_array ')' multi_array_dimension // redundant parenthesis
3471                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3472        | '(' variable_array ')'                                                        // redundant parenthesis
3473                { $$ = $2; }
3474        | '(' attribute_list variable_array ')'                         // redundant parenthesis
3475                { $$ = $3->addQualifiers( $2 ); }
3476        ;
3477
3478variable_function:
3479        '(' variable_ptr ')' '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3480                { $$ = $2->addParamList( $5 ); }
3481        | '(' attribute_list variable_ptr ')' '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3482                { $$ = $3->addQualifiers( $2 )->addParamList( $6 ); }
3483        | '(' variable_function ')'                                                     // redundant parenthesis
3484                { $$ = $2; }
3485        | '(' attribute_list variable_function ')'                      // redundant parenthesis
3486                { $$ = $3->addQualifiers( $2 ); }
3487        ;
3488
3489// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
3490// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
3491// is the same scope.  The pattern precludes returning arrays and functions versus pointers to arrays and functions.
3492
3493function_declarator:
3494        function_no_ptr attribute_list_opt
3495                { $$ = $1->addQualifiers( $2 ); }
3496        | function_ptr
3497        | function_array attribute_list_opt
3498                { $$ = $1->addQualifiers( $2 ); }
3499        ;
3500
3501function_no_ptr:
3502        paren_identifier '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3503                { $$ = $1->addParamList( $3 ); }
3504        | '(' function_ptr ')' '(' parameter_type_list_opt ')'
3505                { $$ = $2->addParamList( $5 ); }
3506        | '(' attribute_list function_ptr ')' '(' parameter_type_list_opt ')'
3507                { $$ = $3->addQualifiers( $2 )->addParamList( $6 ); }
3508        | '(' function_no_ptr ')'                                                       // redundant parenthesis
3509                { $$ = $2; }
3510        | '(' attribute_list function_no_ptr ')'                        // redundant parenthesis
3511                { $$ = $3->addQualifiers( $2 ); }
3512        ;
3513
3514function_ptr:
3515        ptrref_operator function_declarator
3516                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3517        | ptrref_operator type_qualifier_list function_declarator
3518                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3519        | '(' function_ptr ')' attribute_list_opt
3520                { $$ = $2->addQualifiers( $4 ); }
3521        | '(' attribute_list function_ptr ')' attribute_list_opt
3522                { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3523        ;
3524
3525function_array:
3526        '(' function_ptr ')' array_dimension
3527                { $$ = $2->addArray( $4 ); }
3528        | '(' attribute_list function_ptr ')' array_dimension
3529                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3530        | '(' function_array ')' multi_array_dimension          // redundant parenthesis
3531                { $$ = $2->addArray( $4 ); }
3532        | '(' attribute_list function_array ')' multi_array_dimension // redundant parenthesis
3533                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3534        | '(' function_array ')'                                                        // redundant parenthesis
3535                { $$ = $2; }
3536        | '(' attribute_list function_array ')'                         // redundant parenthesis
3537                { $$ = $3->addQualifiers( $2 ); }
3538        ;
3539
3540// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
3541//
3542//   f( a, b, c ) int a, *b, c[]; {}
3543//
3544// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
3545// returning arrays and functions versus pointers to arrays and functions.
3546
3547KR_function_declarator:
3548        KR_function_no_ptr
3549        | KR_function_ptr
3550        | KR_function_array
3551        ;
3552
3553KR_function_no_ptr:
3554        paren_identifier '(' identifier_list ')'                        // function_declarator handles empty parameter
3555                { $$ = $1->addIdList( $3 ); }
3556        | '(' KR_function_ptr ')' '(' parameter_type_list_opt ')'
3557                { $$ = $2->addParamList( $5 ); }
3558        | '(' attribute_list KR_function_ptr ')' '(' parameter_type_list_opt ')'
3559                { $$ = $3->addQualifiers( $2 )->addParamList( $6 ); }
3560        | '(' KR_function_no_ptr ')'                                            // redundant parenthesis
3561                { $$ = $2; }
3562        | '(' attribute_list KR_function_no_ptr ')'                     // redundant parenthesis
3563                { $$ = $3->addQualifiers( $2 ); }
3564        ;
3565
3566KR_function_ptr:
3567        ptrref_operator KR_function_declarator
3568                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3569        | ptrref_operator type_qualifier_list KR_function_declarator
3570                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3571        | '(' KR_function_ptr ')'
3572                { $$ = $2; }
3573        | '(' attribute_list KR_function_ptr ')'
3574                { $$ = $3->addQualifiers( $2 ); }
3575        ;
3576
3577KR_function_array:
3578        '(' KR_function_ptr ')' array_dimension
3579                { $$ = $2->addArray( $4 ); }
3580        | '(' attribute_list KR_function_ptr ')' array_dimension
3581                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3582        | '(' KR_function_array ')' multi_array_dimension       // redundant parenthesis
3583                { $$ = $2->addArray( $4 ); }
3584        | '(' attribute_list KR_function_array ')' multi_array_dimension // redundant parenthesis
3585                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3586        | '(' KR_function_array ')'                                                     // redundant parenthesis
3587                { $$ = $2; }
3588        | '(' attribute_list KR_function_array ')'                      // redundant parenthesis
3589                { $$ = $3->addQualifiers( $2 ); }
3590        ;
3591
3592// This pattern parses a declaration for a variable that redefines a type name, e.g.:
3593//
3594//              typedef int foo;
3595//              {
3596//                 int foo; // redefine typedef name in new scope
3597//              }
3598
3599paren_type:
3600        typedef_name
3601                {
3602                        // hide type name in enclosing scope by variable name
3603                        typedefTable.addToEnclosingScope( *$1->name, IDENTIFIER, "paren_type" );
3604                }
3605        | '(' paren_type ')'
3606                { $$ = $2; }
3607        ;
3608
3609variable_type_redeclarator:
3610        paren_type attribute_list_opt
3611                { $$ = $1->addQualifiers( $2 ); }
3612        | variable_type_ptr
3613        | variable_type_array attribute_list_opt
3614                { $$ = $1->addQualifiers( $2 ); }
3615        | variable_type_function attribute_list_opt
3616                { $$ = $1->addQualifiers( $2 ); }
3617        ;
3618
3619variable_type_ptr:
3620        ptrref_operator variable_type_redeclarator
3621                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3622        | ptrref_operator type_qualifier_list variable_type_redeclarator
3623                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3624        | '(' variable_type_ptr ')' attribute_list_opt          // redundant parenthesis
3625                { $$ = $2->addQualifiers( $4 ); }
3626        | '(' attribute_list variable_type_ptr ')' attribute_list_opt // redundant parenthesis
3627                { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3628        ;
3629
3630variable_type_array:
3631        paren_type array_dimension
3632                { $$ = $1->addArray( $2 ); }
3633        | '(' variable_type_ptr ')' array_dimension
3634                { $$ = $2->addArray( $4 ); }
3635        | '(' attribute_list variable_type_ptr ')' array_dimension
3636                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3637        | '(' variable_type_array ')' multi_array_dimension     // redundant parenthesis
3638                { $$ = $2->addArray( $4 ); }
3639        | '(' attribute_list variable_type_array ')' multi_array_dimension // redundant parenthesis
3640                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3641        | '(' variable_type_array ')'                                           // redundant parenthesis
3642                { $$ = $2; }
3643        | '(' attribute_list variable_type_array ')'            // redundant parenthesis
3644                { $$ = $3->addQualifiers( $2 ); }
3645        ;
3646
3647variable_type_function:
3648        '(' variable_type_ptr ')' '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3649                { $$ = $2->addParamList( $5 ); }
3650        | '(' attribute_list variable_type_ptr ')' '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3651                { $$ = $3->addQualifiers( $2 )->addParamList( $6 ); }
3652        | '(' variable_type_function ')'                                        // redundant parenthesis
3653                { $$ = $2; }
3654        | '(' attribute_list variable_type_function ')'         // redundant parenthesis
3655                { $$ = $3->addQualifiers( $2 ); }
3656        ;
3657
3658// This pattern parses a declaration for a function prototype that redefines a type name.  It precludes declaring an
3659// array of functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to
3660// arrays and functions.
3661
3662function_type_redeclarator:
3663        function_type_no_ptr attribute_list_opt
3664                { $$ = $1->addQualifiers( $2 ); }
3665        | function_type_ptr
3666        | function_type_array attribute_list_opt
3667                { $$ = $1->addQualifiers( $2 ); }
3668        ;
3669
3670function_type_no_ptr:
3671        paren_type '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3672                { $$ = $1->addParamList( $3 ); }
3673        | '(' function_type_ptr ')' '(' parameter_type_list_opt ')'
3674                { $$ = $2->addParamList( $5 ); }
3675        | '(' attribute_list function_type_ptr ')' '(' parameter_type_list_opt ')'
3676                { $$ = $3->addQualifiers( $2 )->addParamList( $6 ); }
3677        | '(' function_type_no_ptr ')'                                          // redundant parenthesis
3678                { $$ = $2; }
3679        | '(' attribute_list function_type_no_ptr ')'           // redundant parenthesis
3680                { $$ = $3->addQualifiers( $2 ); }
3681        ;
3682
3683function_type_ptr:
3684        ptrref_operator function_type_redeclarator
3685                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3686        | ptrref_operator type_qualifier_list function_type_redeclarator
3687                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3688        | '(' function_type_ptr ')' attribute_list_opt
3689                { $$ = $2->addQualifiers( $4 ); }
3690        | '(' attribute_list function_type_ptr ')' attribute_list_opt
3691                { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3692        ;
3693
3694function_type_array:
3695        '(' function_type_ptr ')' array_dimension
3696                { $$ = $2->addArray( $4 ); }
3697        | '(' attribute_list function_type_ptr ')' array_dimension
3698                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3699        | '(' function_type_array ')' multi_array_dimension     // redundant parenthesis
3700                { $$ = $2->addArray( $4 ); }
3701        | '(' attribute_list function_type_array ')' multi_array_dimension // redundant parenthesis
3702                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3703        | '(' function_type_array ')'                                           // redundant parenthesis
3704                { $$ = $2; }
3705        | '(' attribute_list function_type_array ')'            // redundant parenthesis
3706                { $$ = $3->addQualifiers( $2 ); }
3707        ;
3708
3709// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
3710// typedef name and allows the C99 array options, which can only appear in a parameter list.  The pattern precludes
3711// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
3712// pointers to arrays and functions.
3713
3714identifier_parameter_declarator:
3715        paren_identifier attribute_list_opt
3716                { $$ = $1->addQualifiers( $2 ); }
3717        | '&' MUTEX paren_identifier attribute_list_opt
3718                { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( ast::CV::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
3719        | identifier_parameter_ptr
3720        | identifier_parameter_array attribute_list_opt
3721                { $$ = $1->addQualifiers( $2 ); }
3722        | identifier_parameter_function attribute_list_opt
3723                { $$ = $1->addQualifiers( $2 ); }
3724        ;
3725
3726identifier_parameter_ptr:
3727        ptrref_operator identifier_parameter_declarator
3728                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3729        | ptrref_operator type_qualifier_list identifier_parameter_declarator
3730                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3731        | '(' identifier_parameter_ptr ')' attribute_list_opt // redundant parenthesis
3732                { $$ = $2->addQualifiers( $4 ); }
3733        ;
3734
3735identifier_parameter_array:
3736        paren_identifier array_parameter_dimension
3737                { $$ = $1->addArray( $2 ); }
3738        | '(' identifier_parameter_ptr ')' array_dimension
3739                { $$ = $2->addArray( $4 ); }
3740        | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
3741                { $$ = $2->addArray( $4 ); }
3742        | '(' identifier_parameter_array ')'                            // redundant parenthesis
3743                { $$ = $2; }
3744        ;
3745
3746identifier_parameter_function:
3747        paren_identifier '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3748                { $$ = $1->addParamList( $3 ); }
3749        | '(' identifier_parameter_ptr ')' '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3750                { $$ = $2->addParamList( $5 ); }
3751        | '(' identifier_parameter_function ')'                         // redundant parenthesis
3752                { $$ = $2; }
3753        ;
3754
3755// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
3756// e.g.:
3757//
3758//              typedef int foo;
3759//              forall( otype T ) struct foo;
3760//              int f( int foo ); // redefine typedef name in new scope
3761//
3762// and allows the C99 array options, which can only appear in a parameter list.
3763
3764type_parameter_redeclarator:
3765        typedef_name attribute_list_opt
3766                { $$ = $1->addQualifiers( $2 ); }
3767        | '&' MUTEX typedef_name attribute_list_opt
3768                { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( ast::CV::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
3769        | type_parameter_ptr
3770        | type_parameter_array attribute_list_opt
3771                { $$ = $1->addQualifiers( $2 ); }
3772        | type_parameter_function attribute_list_opt
3773                { $$ = $1->addQualifiers( $2 ); }
3774        ;
3775
3776typedef_name:
3777        TYPEDEFname
3778                { $$ = DeclarationNode::newName( $1 ); }
3779        | TYPEGENname
3780                { $$ = DeclarationNode::newName( $1 ); }
3781        ;
3782
3783type_parameter_ptr:
3784        ptrref_operator type_parameter_redeclarator
3785                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3786        | ptrref_operator type_qualifier_list type_parameter_redeclarator
3787                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3788        | '(' type_parameter_ptr ')' attribute_list_opt         // redundant parenthesis
3789                { $$ = $2->addQualifiers( $4 ); }
3790        ;
3791
3792type_parameter_array:
3793        typedef_name array_parameter_dimension
3794                { $$ = $1->addArray( $2 ); }
3795        | '(' type_parameter_ptr ')' array_parameter_dimension
3796                { $$ = $2->addArray( $4 ); }
3797        ;
3798
3799type_parameter_function:
3800        typedef_name '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3801                { $$ = $1->addParamList( $3 ); }
3802        | '(' type_parameter_ptr ')' '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3803                { $$ = $2->addParamList( $5 ); }
3804        ;
3805
3806// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
3807// which the type applies, e.g.:
3808//
3809//              sizeof( int );
3810//              sizeof( int * );
3811//              sizeof( int [10] );
3812//              sizeof( int (*)() );
3813//              sizeof( int () );
3814//
3815// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3816// and functions versus pointers to arrays and functions.
3817
3818abstract_declarator:
3819        abstract_ptr
3820        | abstract_array attribute_list_opt
3821                { $$ = $1->addQualifiers( $2 ); }
3822        | abstract_function attribute_list_opt
3823                { $$ = $1->addQualifiers( $2 ); }
3824        ;
3825
3826abstract_ptr:
3827        ptrref_operator
3828                { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
3829        | ptrref_operator type_qualifier_list
3830                { $$ = DeclarationNode::newPointer( $2, $1 ); }
3831        | ptrref_operator abstract_declarator
3832                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3833        | ptrref_operator type_qualifier_list abstract_declarator
3834                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3835        | '(' abstract_ptr ')' attribute_list_opt
3836                { $$ = $2->addQualifiers( $4 ); }
3837        ;
3838
3839abstract_array:
3840        array_dimension
3841        | '(' abstract_ptr ')' array_dimension
3842                { $$ = $2->addArray( $4 ); }
3843        | '(' abstract_array ')' multi_array_dimension          // redundant parenthesis
3844                { $$ = $2->addArray( $4 ); }
3845        | '(' abstract_array ')'                                                        // redundant parenthesis
3846                { $$ = $2; }
3847        ;
3848
3849abstract_function:
3850        '(' parameter_type_list_opt ')'                 // empty parameter list OBSOLESCENT (see 3)
3851                { $$ = DeclarationNode::newFunction( nullptr, nullptr, $2, nullptr ); }
3852        | '(' abstract_ptr ')' '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3853                { $$ = $2->addParamList( $5 ); }
3854        | '(' abstract_function ')'                                                     // redundant parenthesis
3855                { $$ = $2; }
3856        ;
3857
3858array_dimension:
3859                // Only the first dimension can be empty.
3860        '[' ']'
3861                { $$ = DeclarationNode::newArray( nullptr, nullptr, false ); }
3862        | '[' ']' multi_array_dimension
3863                { $$ = DeclarationNode::newArray( nullptr, nullptr, false )->addArray( $3 ); }
3864                // Cannot use constant_expression because of tuples => semantic check
3865        | '[' push assignment_expression pop ',' comma_expression ']' // CFA
3866                { $$ = DeclarationNode::newArray( $3, nullptr, false )->addArray( DeclarationNode::newArray( $6, nullptr, false ) ); }
3867                // { SemanticError( yylloc, "New array dimension is currently unimplemented." ); $$ = nullptr; }
3868        | '[' push array_type_list pop ']'                                      // CFA
3869                { $$ = DeclarationNode::newArray( $3, nullptr, false ); }
3870        | multi_array_dimension
3871        ;
3872
3873array_type_list:
3874        basic_type_name
3875                { $$ = new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $1 ) ) ); }
3876        | type_name
3877                { $$ = new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $1 ) ) ); }
3878        | assignment_expression upupeq assignment_expression
3879        | array_type_list ',' basic_type_name
3880                { $$ = (ExpressionNode *)($1->set_last( new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $3 ) ) ) )); }
3881        | array_type_list ',' type_name
3882                { $$ = (ExpressionNode *)($1->set_last( new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $3 ) ) ) )); }
3883        | array_type_list ',' assignment_expression upupeq assignment_expression
3884        ;
3885
3886upupeq:
3887        '~'
3888                { $$ = OperKinds::LThan; }
3889        | ErangeUpEq
3890                { $$ = OperKinds::LEThan; }
3891        ;
3892
3893multi_array_dimension:
3894        '[' push assignment_expression pop ']'
3895                { $$ = DeclarationNode::newArray( $3, nullptr, false ); }
3896        | '[' push '*' pop ']'                                                          // C99
3897                { $$ = DeclarationNode::newVarArray( 0 ); }
3898        | multi_array_dimension '[' push assignment_expression pop ']'
3899                { $$ = $1->addArray( DeclarationNode::newArray( $4, nullptr, false ) ); }
3900        | multi_array_dimension '[' push '*' pop ']'            // C99
3901                { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
3902        ;
3903
3904// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
3905// identifier to which the type applies, e.g.:
3906//
3907//              int f( int );                   // not handled here
3908//              int f( int * );                 // abstract function-prototype parameter; no parameter name specified
3909//              int f( int (*)() );             // abstract function-prototype parameter; no parameter name specified
3910//              int f( int (int) );             // abstract function-prototype parameter; no parameter name specified
3911//
3912// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3913// and functions versus pointers to arrays and functions. In addition, the pattern handles the
3914// special meaning of parenthesis around a typedef name:
3915//
3916//              ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
3917//              parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
3918//              not as redundant parentheses around the identifier."
3919//
3920// For example:
3921//
3922//              typedef float T;
3923//              int f( int ( T [5] ) );                                 // see abstract_parameter_declarator
3924//              int g( int ( T ( int ) ) );                             // see abstract_parameter_declarator
3925//              int f( int f1( T a[5] ) );                              // see identifier_parameter_declarator
3926//              int g( int g1( T g2( int p ) ) );               // see identifier_parameter_declarator
3927//
3928// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
3929// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
3930// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
3931// functions.
3932
3933abstract_parameter_declarator_opt:
3934        // empty
3935                { $$ = nullptr; }
3936        | abstract_parameter_declarator
3937        ;
3938
3939abstract_parameter_declarator:
3940        abstract_parameter_ptr
3941        | '&' MUTEX attribute_list_opt
3942                { $$ = DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( ast::CV::Mutex ), OperKinds::AddressOf )->addQualifiers( $3 ); }
3943        | abstract_parameter_array attribute_list_opt
3944                { $$ = $1->addQualifiers( $2 ); }
3945        | abstract_parameter_function attribute_list_opt
3946                { $$ = $1->addQualifiers( $2 ); }
3947        ;
3948
3949abstract_parameter_ptr:
3950        ptrref_operator
3951                { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
3952        | ptrref_operator type_qualifier_list
3953                { $$ = DeclarationNode::newPointer( $2, $1 ); }
3954        | ptrref_operator abstract_parameter_declarator
3955                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3956        | ptrref_operator type_qualifier_list abstract_parameter_declarator
3957                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3958        | '(' abstract_parameter_ptr ')' attribute_list_opt     // redundant parenthesis
3959                { $$ = $2->addQualifiers( $4 ); }
3960        ;
3961
3962abstract_parameter_array:
3963        array_parameter_dimension
3964        | '(' abstract_parameter_ptr ')' array_parameter_dimension
3965                { $$ = $2->addArray( $4 ); }
3966        | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
3967                { $$ = $2->addArray( $4 ); }
3968        | '(' abstract_parameter_array ')'                                      // redundant parenthesis
3969                { $$ = $2; }
3970        ;
3971
3972abstract_parameter_function:
3973        '(' parameter_type_list_opt ')'                 // empty parameter list OBSOLESCENT (see 3)
3974                { $$ = DeclarationNode::newFunction( nullptr, nullptr, $2, nullptr ); }
3975        | '(' abstract_parameter_ptr ')' '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
3976                { $$ = $2->addParamList( $5 ); }
3977        | '(' abstract_parameter_function ')'                           // redundant parenthesis
3978                { $$ = $2; }
3979        ;
3980
3981array_parameter_dimension:
3982                // Only the first dimension can be empty or have qualifiers.
3983        array_parameter_1st_dimension
3984        | array_parameter_1st_dimension multi_array_dimension
3985                { $$ = $1->addArray( $2 ); }
3986        | multi_array_dimension
3987        ;
3988
3989// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
3990//
3991//              ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
3992//              a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
3993
3994array_parameter_1st_dimension:
3995        '[' ']'
3996                { $$ = DeclarationNode::newArray( nullptr, nullptr, false ); }
3997                // multi_array_dimension handles the '[' '*' ']' case
3998        | '[' push type_qualifier_list '*' pop ']'                      // remaining C99
3999                { $$ = DeclarationNode::newVarArray( $3 ); }
4000        | '[' push type_qualifier_list pop ']'
4001                { $$ = DeclarationNode::newArray( nullptr, $3, false ); }
4002                // multi_array_dimension handles the '[' assignment_expression ']' case
4003        | '[' push type_qualifier_list assignment_expression pop ']'
4004                { $$ = DeclarationNode::newArray( $4, $3, false ); }
4005        | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
4006                { $$ = DeclarationNode::newArray( $5, $4, true ); }
4007        | '[' push type_qualifier_list STATIC assignment_expression pop ']'
4008                { $$ = DeclarationNode::newArray( $5, $3, true ); }
4009        ;
4010
4011// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
4012//
4013//   struct S {
4014//       int;
4015//       int *;
4016//       int [10];
4017//       int (*)();
4018//   };
4019
4020variable_abstract_declarator:
4021        variable_abstract_ptr
4022        | variable_abstract_array attribute_list_opt
4023                { $$ = $1->addQualifiers( $2 ); }
4024        | variable_abstract_function attribute_list_opt
4025                { $$ = $1->addQualifiers( $2 ); }
4026        ;
4027
4028variable_abstract_ptr:
4029        ptrref_operator
4030                { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
4031        | ptrref_operator type_qualifier_list
4032                { $$ = DeclarationNode::newPointer( $2, $1 ); }
4033        | ptrref_operator variable_abstract_declarator
4034                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
4035        | ptrref_operator type_qualifier_list variable_abstract_declarator
4036                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
4037        | '(' variable_abstract_ptr ')' attribute_list_opt      // redundant parenthesis
4038                { $$ = $2->addQualifiers( $4 ); }
4039        ;
4040
4041variable_abstract_array:
4042        array_dimension
4043        | '(' variable_abstract_ptr ')' array_dimension
4044                { $$ = $2->addArray( $4 ); }
4045        | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
4046                { $$ = $2->addArray( $4 ); }
4047        | '(' variable_abstract_array ')'                                       // redundant parenthesis
4048                { $$ = $2; }
4049        ;
4050
4051variable_abstract_function:
4052        '(' variable_abstract_ptr ')' '(' parameter_type_list_opt ')' // empty parameter list OBSOLESCENT (see 3)
4053                { $$ = $2->addParamList( $5 ); }
4054        | '(' variable_abstract_function ')'                            // redundant parenthesis
4055                { $$ = $2; }
4056        ;
4057
4058// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
4059// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
4060
4061cfa_identifier_parameter_declarator_tuple:                              // CFA
4062        cfa_identifier_parameter_declarator_no_tuple
4063        | cfa_abstract_tuple
4064        | type_qualifier_list cfa_abstract_tuple
4065                { $$ = $2->addQualifiers( $1 ); }
4066        ;
4067
4068cfa_identifier_parameter_declarator_no_tuple:                   // CFA
4069        cfa_identifier_parameter_ptr
4070        | cfa_identifier_parameter_array
4071        ;
4072
4073cfa_identifier_parameter_ptr:                                                   // CFA
4074                // No SUE declaration in parameter list.
4075        ptrref_operator type_specifier_nobody
4076                { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
4077        | type_qualifier_list ptrref_operator type_specifier_nobody
4078                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
4079        | ptrref_operator cfa_abstract_function
4080                { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
4081        | type_qualifier_list ptrref_operator cfa_abstract_function
4082                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
4083        | ptrref_operator cfa_identifier_parameter_declarator_tuple
4084                { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
4085        | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
4086                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
4087        ;
4088
4089cfa_identifier_parameter_array:                                                 // CFA
4090                // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
4091                // shift/reduce conflict with new-style empty (void) function return type.
4092        '[' ']' type_specifier_nobody
4093                { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
4094        | cfa_array_parameter_1st_dimension type_specifier_nobody
4095                { $$ = $2->addNewArray( $1 ); }
4096        | '[' ']' multi_array_dimension type_specifier_nobody
4097                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
4098        | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
4099                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
4100        | multi_array_dimension type_specifier_nobody
4101                { $$ = $2->addNewArray( $1 ); }
4102
4103        | '[' ']' cfa_identifier_parameter_ptr
4104                { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
4105        | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
4106                { $$ = $2->addNewArray( $1 ); }
4107        | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
4108                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
4109        | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
4110                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
4111        | multi_array_dimension cfa_identifier_parameter_ptr
4112                { $$ = $2->addNewArray( $1 ); }
4113        ;
4114
4115cfa_array_parameter_1st_dimension:
4116        '[' push type_qualifier_list '*' pop ']'                        // remaining C99
4117                { $$ = DeclarationNode::newVarArray( $3 ); }
4118        | '[' push type_qualifier_list assignment_expression pop ']'
4119                { $$ = DeclarationNode::newArray( $4, $3, false ); }
4120        | '[' push declaration_qualifier_list assignment_expression pop ']'
4121                // declaration_qualifier_list must be used because of shift/reduce conflict with
4122                // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
4123                // appear in this context.
4124                { $$ = DeclarationNode::newArray( $4, $3, true ); }
4125        | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
4126                { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
4127        ;
4128
4129// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
4130// identifier to which the type applies, e.g.:
4131//
4132//              [int] f( int );                         // abstract variable parameter; no parameter name specified
4133//              [int] f( [int] (int) );         // abstract function-prototype parameter; no parameter name specified
4134//
4135// These rules need LR(3):
4136//
4137//              cfa_abstract_tuple identifier_or_type_name
4138//              '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
4139//
4140// since a function return type can be syntactically identical to a tuple type:
4141//
4142//              [int, int] t;
4143//              [int, int] f( int );
4144//
4145// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
4146// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
4147// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
4148// duplicated when appearing with cfa_function_specifier.
4149
4150cfa_abstract_declarator_tuple:                                                  // CFA
4151        cfa_abstract_tuple
4152        | type_qualifier_list cfa_abstract_tuple
4153                { $$ = $2->addQualifiers( $1 ); }
4154        | cfa_abstract_declarator_no_tuple
4155        ;
4156
4157cfa_abstract_declarator_no_tuple:                                               // CFA
4158        cfa_abstract_ptr
4159        | cfa_abstract_array
4160        ;
4161
4162cfa_abstract_ptr:                                                                               // CFA
4163        ptrref_operator type_specifier
4164                { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
4165        | type_qualifier_list ptrref_operator type_specifier
4166                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
4167        | ptrref_operator cfa_abstract_function
4168                { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
4169        | type_qualifier_list ptrref_operator cfa_abstract_function
4170                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
4171        | ptrref_operator cfa_abstract_declarator_tuple
4172                { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
4173        | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
4174                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
4175        ;
4176
4177cfa_abstract_array:                                                                             // CFA
4178                // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
4179                // empty (void) function return type.
4180        '[' ']' type_specifier
4181                { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
4182        | '[' ']' multi_array_dimension type_specifier
4183                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
4184        | multi_array_dimension type_specifier
4185                { $$ = $2->addNewArray( $1 ); }
4186        | '[' ']' cfa_abstract_ptr
4187                { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
4188        | '[' ']' multi_array_dimension cfa_abstract_ptr
4189                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
4190        | multi_array_dimension cfa_abstract_ptr
4191                { $$ = $2->addNewArray( $1 ); }
4192        ;
4193
4194cfa_abstract_tuple:                                                                             // CFA
4195        '[' push cfa_abstract_parameter_list pop ']'
4196                { $$ = DeclarationNode::newTuple( $3 ); }
4197        | '[' push type_specifier_nobody ELLIPSIS pop ']'
4198                { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
4199        | '[' push type_specifier_nobody ELLIPSIS constant_expression pop ']'
4200                { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
4201        ;
4202
4203cfa_abstract_function:                                                                  // CFA
4204//      '[' ']' '(' cfa_parameter_ellipsis_list_opt ')'
4205//              { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
4206        cfa_abstract_tuple '(' push cfa_parameter_ellipsis_list_opt pop ')'
4207                { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
4208        | cfa_function_return '(' push cfa_parameter_ellipsis_list_opt pop ')'
4209                { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
4210        ;
4211
4212// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
4213//    each declaration, and in the specifier-qualifier list in each structure declaration and type name."
4214//
4215// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
4216//    the declaration specifiers in a declaration is an obsolescent feature."
4217//
4218// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
4219//    prototype-format parameter type declarators) is an obsolescent feature."
4220//
4221// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
4222//    declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
4223
4224// ************************ MISCELLANEOUS ********************************
4225
4226comma_opt:                                                                                              // redundant comma
4227        // empty
4228        | ','
4229        ;
4230
4231default_initializer_opt:
4232        // empty
4233                { $$ = nullptr; }
4234        | '=' assignment_expression
4235                { $$ = $2; }
4236        ;
4237
4238%%
4239
4240// ----end of grammar----
4241
4242// Local Variables: //
4243// mode: c++ //
4244// tab-width: 4 //
4245// compile-command: "make install" //
4246// End: //
Note: See TracBrowser for help on using the repository browser.