source: src/Parser/parser.yy @ 9feb34b

ADTast-experimental
Last change on this file since 9feb34b was 9feb34b, checked in by Andrew Beach <ajbeach@…>, 13 months ago

Moved toString and toCString to a new header. Updated includes. cassert was somehow getting instances of toString before but that stopped working so I embedded the new smaller include.

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