source: src/Parser/parser.yy @ 0e34a14

ADTast-experimentalpthread-emulation
Last change on this file since 0e34a14 was dbedd71, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

update for-control with corrected @ usage for negative range

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