source: src/Parser/parser.yy @ e72fc60

Last change on this file since e72fc60 was 6cef439, checked in by Andrew Beach <ajbeach@…>, 7 months ago

Return 'TypeData? *' from some parse rules. Moved TypeData? construction over to that file.

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