source: src/Parser/parser.yy @ 52be5948

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

update for-control with more error messages

  • Property mode set to 100644
File size: 158.3 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// parser.yy --
8//
9// Author           : Peter A. Buhr
10// Created On       : Sat Sep  1 20:22:55 2001
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Aug  9 17:21:46 2022
13// Update Count     : 5629
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, error
1332                { SemanticError( yylloc, "Missing loop fields ('@') with an anonymous loop index is not useful." ); $$ = nullptr; }
1333        | comma_expression updowneq '@'                                         // CFA
1334                { SemanticError( yylloc, "Missing loop fields ('@') with an anonymous loop index is not useful." ); $$ = nullptr; }
1335
1336        | comma_expression updowneq comma_expression '~' comma_expression // CFA
1337                { $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), UPDOWN( $2, $1->clone(), $3 ), $2, UPDOWN( $2, $3->clone(), $1->clone() ), $5 ); }
1338        | '@' updowneq comma_expression '~' comma_expression // CFA, error
1339                { SemanticError( yylloc, "Missing loop fields ('@') with an anonymous loop index is not useful." ); $$ = nullptr; }
1340        | comma_expression updowneq '@' '~' comma_expression // CFA, error
1341                { SemanticError( yylloc, "Missing loop fields ('@') with an anonymous loop index is not useful." ); $$ = nullptr; }
1342        | comma_expression updowneq comma_expression '~' '@' // CFA, error
1343                { SemanticError( yylloc, "Missing loop fields ('@') with an anonymous loop index is not useful." ); $$ = nullptr; }
1344        | '@' updowneq comma_expression '~' '@'                         // CFA, error
1345                { SemanticError( yylloc, "Missing loop fields ('@') with an anonymous loop index is not useful." ); $$ = nullptr; }
1346        | comma_expression updowneq '@' '~' '@'                         // CFA, error
1347                { SemanticError( yylloc, "Missing loop fields ('@') with an anonymous loop index is not useful." ); $$ = nullptr; }
1348
1349        | comma_expression ';' comma_expression                         // CFA
1350                { $$ = forCtrl( $3, $1, NEW_ZERO, OperKinds::LThan, $3->clone(), NEW_ONE ); }
1351        | comma_expression ';' downupdowneq comma_expression // CFA
1352                { $$ = forCtrl( $4, $1, UPDOWN( $3, NEW_ZERO, $4->clone() ), $3, UPDOWN( $3, $4->clone(), NEW_ZERO ), NEW_ONE ); }
1353
1354        | comma_expression ';' comma_expression updowneq comma_expression // CFA
1355                { $$ = forCtrl( $3, $1, UPDOWN( $4, $3->clone(), $5 ), $4, UPDOWN( $4, $5->clone(), $3->clone() ), NEW_ONE ); }
1356        | comma_expression ';' '@' updowneq comma_expression // CFA, error
1357                { SemanticError( yylloc, "Missing start value so cannot compare." ); $$ = nullptr; }
1358        | comma_expression ';' comma_expression updowneq '@' // CFA
1359                {
1360                        if ( $4 == OperKinds::LEThan || $4 == OperKinds::GEThan ) { SemanticError( yylloc, "Decrement with missing comparison is meaningless. Use \"~\"." ); $$ = nullptr; }
1361                        else $$ = forCtrl( $3, $1, $3->clone(), $4, nullptr, NEW_ONE );
1362                }
1363
1364        | comma_expression ';' comma_expression updowneq comma_expression '~' comma_expression // CFA
1365                { $$ = forCtrl( $3, $1, UPDOWN( $4, $3->clone(), $5 ), $4, UPDOWN( $4, $5->clone(), $3->clone() ), $7 ); }
1366        | comma_expression ';' '@' updowneq comma_expression '~' comma_expression // CFA, error
1367                { SemanticError( yylloc, "Missing start declaration so cannot compare." ); $$ = nullptr; }
1368        | comma_expression ';' comma_expression updowneq '@' '~' comma_expression // CFA
1369                {
1370                        if ( $4 == OperKinds::LEThan || $4 == OperKinds::GEThan ) { SemanticError( yylloc, "Equal for comparison is meaningless with missing comparison. Use \"~\"." ); $$ = nullptr; }
1371                        else $$ = forCtrl( $3, $1, $3->clone(), $4, nullptr, $7 );
1372                }
1373        | comma_expression ';' comma_expression updowneq comma_expression '~' '@' // CFA
1374                { $$ = forCtrl( $3, $1, UPDOWN( $4, $3->clone(), $5 ), $4, UPDOWN( $4, $5->clone(), $3->clone() ), nullptr ); }
1375        | comma_expression ';' '@' updowneq comma_expression '~' '@' // CFA, error
1376                { SemanticError( yylloc, "Missing start declaration so cannot compare." ); $$ = nullptr; }
1377        | comma_expression ';' comma_expression updowneq '@' '~' '@' // CFA
1378                {
1379                        if ( $4 == OperKinds::GThan ) { SemanticError( yylloc, "Negative range \"-~\" is meaningless when comparison and iterator are empty. Use \"~\"." ); $$ = nullptr; }
1380                        else if ( $4 == OperKinds::LEThan || $4 == OperKinds::GEThan ) { SemanticError( yylloc, "Equal for comparison is meaningless with missing comparison. Use \"~\"." ); $$ = nullptr; }
1381                        else $$ = forCtrl( $3, $1, $3->clone(), $4, nullptr, nullptr );
1382                }
1383
1384        | declaration comma_expression                                          // CFA
1385                { $$ = forCtrl( $1, NEW_ZERO, OperKinds::LThan, $2, NEW_ONE ); }
1386        | declaration downupdowneq comma_expression                     // CFA
1387                { $$ = forCtrl( $1, UPDOWN( $2, NEW_ZERO, $3 ), $2, UPDOWN( $2, $3->clone(), NEW_ZERO ), NEW_ONE ); }
1388
1389        | declaration comma_expression updowneq comma_expression // CFA
1390                { $$ = forCtrl( $1, UPDOWN( $3, $2->clone(), $4 ), $3, UPDOWN( $3, $4->clone(), $2->clone() ), NEW_ONE ); }
1391        | declaration '@' updowneq comma_expression                     // CFA, error
1392                { SemanticError( yylloc, "Missing start value so cannot compare." ); $$ = nullptr; }
1393        | declaration comma_expression updowneq '@'                     // CFA
1394                {
1395                        if ( $3 == OperKinds::LEThan || $3 == OperKinds::GEThan ) { SemanticError( yylloc, "Decrement with missing comparison is meaningless. Use \"~\"." ); $$ = nullptr; }
1396                        else $$ = forCtrl( $1, $2, $3, nullptr, NEW_ONE );
1397                }
1398
1399        | declaration comma_expression updowneq comma_expression '~' comma_expression // CFA
1400                { $$ = forCtrl( $1, UPDOWN( $3, $2, $4 ), $3, UPDOWN( $3, $4->clone(), $2->clone() ), $6 ); }
1401        | declaration '@' updowneq comma_expression '~' comma_expression // CFA, error
1402                { SemanticError( yylloc, "Missing start declaration so cannot compare." ); $$ = nullptr; }
1403        | declaration comma_expression updowneq '@' '~' comma_expression // CFA
1404                {
1405                        if ( $3 == OperKinds::LEThan || $3 == OperKinds::GEThan ) { SemanticError( yylloc, "Equal for comparison is meaningless with missing comparison. Use \"~\"." ); $$ = nullptr; }
1406                        else $$ = forCtrl( $1, $2, $3, nullptr, $6 );
1407                }
1408        | declaration comma_expression updowneq comma_expression '~' '@' // CFA
1409                { $$ = forCtrl( $1, UPDOWN( $3, $2, $4 ), $3, UPDOWN( $3, $4->clone(), $2->clone() ), nullptr ); }
1410        | declaration '@' updowneq comma_expression '~' '@' // CFA, error
1411                { SemanticError( yylloc, "Missing start declaration so cannot compare." ); $$ = nullptr; }
1412        | declaration comma_expression updowneq '@' '~' '@'     // CFA
1413                {
1414                        if ( $3 == OperKinds::GThan ) { SemanticError( yylloc, "Negative range \"-~\" is meaningless when comparison and iterator are empty. Use \"~\"." ); $$ = nullptr; }
1415                        else if ( $3 == OperKinds::LEThan || $3 == OperKinds::GEThan ) { SemanticError( yylloc, "Equal for comparison is meaningless with missing comparison. Use \"~\"." ); $$ = nullptr; }
1416                        else $$ = forCtrl( $1, $2, $3, nullptr, nullptr );
1417                }
1418
1419        | comma_expression ';' TYPEDEFname                                      // CFA, array type
1420                {
1421                        SemanticError( yylloc, "Type iterator is currently unimplemented." ); $$ = nullptr;
1422                        //$$ = forCtrl( new ExpressionNode( build_varref( $3 ) ), $1, nullptr, OperKinds::Range, nullptr, nullptr );
1423                }
1424        | comma_expression ';' downupdowneq TYPEDEFname         // CFA, array type
1425                {
1426                        if ( $3 == OperKinds::LEThan || $3 == OperKinds::GEThan ) { SemanticError( yylloc, "All enumation ranges are equal (all values). Remove \"=~\"." ); $$ = nullptr; }
1427                        SemanticError( yylloc, "Type iterator is currently unimplemented." ); $$ = nullptr;
1428                }
1429        ;
1430
1431downupdowneq:
1432        ErangeDown
1433                { $$ = OperKinds::GThan; }
1434        | ErangeUpEq
1435                { $$ = OperKinds::LEThan; }
1436        | ErangeDownEq
1437                { $$ = OperKinds::GEThan; }
1438        ;
1439
1440updown:
1441        '~'
1442                { $$ = OperKinds::LThan; }
1443        | ErangeDown
1444                { $$ = OperKinds::GThan; }
1445        ;
1446
1447updowneq:
1448        updown
1449        | ErangeUpEq
1450                { $$ = OperKinds::LEThan; }
1451        | ErangeDownEq
1452                { $$ = OperKinds::GEThan; }
1453        ;
1454
1455jump_statement:
1456        GOTO identifier_or_type_name ';'
1457                { $$ = new StatementNode( build_branch( $2, BranchStmt::Goto ) ); }
1458        | GOTO '*' comma_expression ';'                                         // GCC, computed goto
1459                // The syntax for the GCC computed goto violates normal expression precedence, e.g., goto *i+3; => goto *(i+3);
1460                // whereas normal operator precedence yields goto (*i)+3;
1461                { $$ = new StatementNode( build_computedgoto( $3 ) ); }
1462                // A semantic check is required to ensure fallthru appears only in the body of a choose statement.
1463        | fall_through_name ';'                                                         // CFA
1464                { $$ = new StatementNode( build_branch( BranchStmt::FallThrough ) ); }
1465        | fall_through_name identifier_or_type_name ';'         // CFA
1466                { $$ = new StatementNode( build_branch( $2, BranchStmt::FallThrough ) ); }
1467        | fall_through_name DEFAULT ';'                                         // CFA
1468                { $$ = new StatementNode( build_branch( BranchStmt::FallThroughDefault ) ); }
1469        | CONTINUE ';'
1470                // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
1471                { $$ = new StatementNode( build_branch( BranchStmt::Continue ) ); }
1472        | CONTINUE identifier_or_type_name ';'                          // CFA, multi-level continue
1473                // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1474                // the target of the transfer appears only at the start of an iteration statement.
1475                { $$ = new StatementNode( build_branch( $2, BranchStmt::Continue ) ); }
1476        | BREAK ';'
1477                // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
1478                { $$ = new StatementNode( build_branch( BranchStmt::Break ) ); }
1479        | BREAK identifier_or_type_name ';'                                     // CFA, multi-level exit
1480                // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1481                // the target of the transfer appears only at the start of an iteration statement.
1482                { $$ = new StatementNode( build_branch( $2, BranchStmt::Break ) ); }
1483        | RETURN comma_expression_opt ';'
1484                { $$ = new StatementNode( build_return( $2 ) ); }
1485        | RETURN '{' initializer_list_opt comma_opt '}' ';'
1486                { SemanticError( yylloc, "Initializer return is currently unimplemented." ); $$ = nullptr; }
1487        | SUSPEND ';'
1488                { $$ = new StatementNode( build_suspend( nullptr ) ); }
1489        | SUSPEND compound_statement
1490                { $$ = new StatementNode( build_suspend( $2 ) ); }
1491        | SUSPEND COROUTINE ';'
1492                { $$ = new StatementNode( build_suspend( nullptr, SuspendStmt::Coroutine ) ); }
1493        | SUSPEND COROUTINE compound_statement
1494                { $$ = new StatementNode( build_suspend( $3, SuspendStmt::Coroutine ) ); }
1495        | SUSPEND GENERATOR ';'
1496                { $$ = new StatementNode( build_suspend( nullptr, SuspendStmt::Generator ) ); }
1497        | SUSPEND GENERATOR compound_statement
1498                { $$ = new StatementNode( build_suspend( $3, SuspendStmt::Generator ) ); }
1499        | THROW assignment_expression_opt ';'                           // handles rethrow
1500                { $$ = new StatementNode( build_throw( $2 ) ); }
1501        | THROWRESUME assignment_expression_opt ';'                     // handles reresume
1502                { $$ = new StatementNode( build_resume( $2 ) ); }
1503        | THROWRESUME assignment_expression_opt AT assignment_expression ';' // handles reresume
1504                { $$ = new StatementNode( build_resume_at( $2, $4 ) ); }
1505        ;
1506
1507fall_through_name:                                                                              // CFA
1508        FALLTHRU
1509        | FALLTHROUGH
1510        ;
1511
1512with_statement:
1513        WITH '(' tuple_expression_list ')' statement
1514                { $$ = new StatementNode( build_with( $3, $5 ) ); }
1515        ;
1516
1517// If MUTEX becomes a general qualifier, there are shift/reduce conflicts, so change syntax to "with mutex".
1518mutex_statement:
1519        MUTEX '(' argument_expression_list ')' statement
1520                { $$ = new StatementNode( build_mutex( $3, $5 ) ); }
1521        ;
1522
1523when_clause:
1524        WHEN '(' comma_expression ')'                           { $$ = $3; }
1525        ;
1526
1527when_clause_opt:
1528        // empty
1529                { $$ = nullptr; }
1530        | when_clause
1531        ;
1532
1533waitfor:
1534        WAITFOR '(' cast_expression ')'
1535                { $$ = $3; }
1536//      | WAITFOR '(' cast_expression ',' argument_expression_list_opt ')'
1537//              { $$ = (ExpressionNode *)$3->set_last( $5 ); }
1538        | WAITFOR '(' cast_expression_list ':' argument_expression_list_opt ')'
1539                { $$ = (ExpressionNode *)($3->set_last( $5 )); }
1540        ;
1541
1542cast_expression_list:
1543        cast_expression
1544        | cast_expression_list ',' cast_expression
1545                // { $$ = (ExpressionNode *)($1->set_last( $3 )); }
1546                { SemanticError( yylloc, "List of mutex member is currently unimplemented." ); $$ = nullptr; }
1547        ;
1548
1549timeout:
1550        TIMEOUT '(' comma_expression ')'                        { $$ = $3; }
1551        ;
1552
1553waitfor_clause:
1554        when_clause_opt waitfor statement                                       %prec THEN
1555                { $$ = build_waitfor( $2, maybe_build_compound( $3 ), $1 ); }
1556        | when_clause_opt waitfor statement WOR waitfor_clause
1557                { $$ = build_waitfor( $2, maybe_build_compound( $3 ), $1, $5 ); }
1558        | when_clause_opt timeout statement                                     %prec THEN
1559                { $$ = build_waitfor_timeout( $2, maybe_build_compound( $3 ), $1 ); }
1560        | when_clause_opt ELSE statement
1561                { $$ = build_waitfor_timeout( nullptr, maybe_build_compound( $3 ), $1 ); }
1562        // "else" must be conditional after timeout or timeout is never triggered (i.e., it is meaningless)
1563        | when_clause_opt timeout statement WOR ELSE statement // syntax error
1564                { SemanticError( yylloc, "else clause must be conditional after timeout or timeout never triggered." ); $$ = nullptr; }
1565        | when_clause_opt timeout statement WOR when_clause ELSE statement
1566                { $$ = build_waitfor_timeout( $2, maybe_build_compound( $3 ), $1, maybe_build_compound( $7 ), $5 ); }
1567        ;
1568
1569waitfor_statement:
1570        when_clause_opt waitfor statement                                       %prec THEN
1571                { $$ = new StatementNode( build_waitfor( $2, $3, $1 ) ); }
1572        | when_clause_opt waitfor statement WOR waitfor_clause
1573                { $$ = new StatementNode( build_waitfor( $2, $3, $1, $5 ) ); }
1574        ;
1575
1576exception_statement:
1577        TRY compound_statement handler_clause                                   %prec THEN
1578                { $$ = new StatementNode( build_try( $2, $3, 0 ) ); }
1579        | TRY compound_statement finally_clause
1580                { $$ = new StatementNode( build_try( $2, 0, $3 ) ); }
1581        | TRY compound_statement handler_clause finally_clause
1582                { $$ = new StatementNode( build_try( $2, $3, $4 ) ); }
1583        ;
1584
1585handler_clause:
1586        handler_key '(' push exception_declaration pop handler_predicate_opt ')' compound_statement
1587                { $$ = new StatementNode( build_catch( $1, $4, $6, $8 ) ); }
1588        | handler_clause handler_key '(' push exception_declaration pop handler_predicate_opt ')' compound_statement
1589                { $$ = (StatementNode *)$1->set_last( new StatementNode( build_catch( $2, $5, $7, $9 ) ) ); }
1590        ;
1591
1592handler_predicate_opt:
1593        // empty
1594                { $$ = nullptr; }
1595        | ';' conditional_expression                            { $$ = $2; }
1596        ;
1597
1598handler_key:
1599        CATCH                                                                           { $$ = CatchStmt::Terminate; }
1600        | RECOVER                                                                       { $$ = CatchStmt::Terminate; }
1601        | CATCHRESUME                                                           { $$ = CatchStmt::Resume; }
1602        | FIXUP                                                                         { $$ = CatchStmt::Resume; }
1603        ;
1604
1605finally_clause:
1606        FINALLY compound_statement                                      { $$ = new StatementNode( build_finally( $2 ) ); }
1607        ;
1608
1609exception_declaration:
1610                // No SUE declaration in parameter list.
1611        type_specifier_nobody
1612        | type_specifier_nobody declarator
1613                { $$ = $2->addType( $1 ); }
1614        | type_specifier_nobody variable_abstract_declarator
1615                { $$ = $2->addType( $1 ); }
1616        | cfa_abstract_declarator_tuple identifier                      // CFA
1617                { $$ = $1->addName( $2 ); }
1618        | cfa_abstract_declarator_tuple                                         // CFA
1619        ;
1620
1621enable_disable_statement:
1622        enable_disable_key identifier_list compound_statement
1623        ;
1624
1625enable_disable_key:
1626        ENABLE
1627        | DISABLE
1628        ;
1629
1630asm_statement:
1631        ASM asm_volatile_opt '(' string_literal ')' ';'
1632                { $$ = new StatementNode( build_asm( $2, $4, 0 ) ); }
1633        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ')' ';' // remaining GCC
1634                { $$ = new StatementNode( build_asm( $2, $4, $6 ) ); }
1635        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ')' ';'
1636                { $$ = new StatementNode( build_asm( $2, $4, $6, $8 ) ); }
1637        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ':' asm_clobbers_list_opt ')' ';'
1638                { $$ = new StatementNode( build_asm( $2, $4, $6, $8, $10 ) ); }
1639        | ASM asm_volatile_opt GOTO '(' string_literal ':' ':' asm_operands_opt ':' asm_clobbers_list_opt ':' label_list ')' ';'
1640                { $$ = new StatementNode( build_asm( $2, $5, 0, $8, $10, $12 ) ); }
1641        ;
1642
1643asm_volatile_opt:                                                                               // GCC
1644        // empty
1645                { $$ = false; }
1646        | VOLATILE
1647                { $$ = true; }
1648        ;
1649
1650asm_operands_opt:                                                                               // GCC
1651        // empty
1652                { $$ = nullptr; }                                                               // use default argument
1653        | asm_operands_list
1654        ;
1655
1656asm_operands_list:                                                                              // GCC
1657        asm_operand
1658        | asm_operands_list ',' asm_operand
1659                { $$ = (ExpressionNode *)($1->set_last( $3 )); }
1660        ;
1661
1662asm_operand:                                                                                    // GCC
1663        string_literal '(' constant_expression ')'
1664                { $$ = new ExpressionNode( new AsmExpr( nullptr, $1, maybeMoveBuild<Expression>( $3 ) ) ); }
1665        | '[' IDENTIFIER ']' string_literal '(' constant_expression ')'
1666                { $$ = new ExpressionNode( new AsmExpr( $2, $4, maybeMoveBuild<Expression>( $6 ) ) ); }
1667        ;
1668
1669asm_clobbers_list_opt:                                                                  // GCC
1670        // empty
1671                { $$ = nullptr; }                                                               // use default argument
1672        | string_literal
1673                { $$ = new ExpressionNode( $1 ); }
1674        | asm_clobbers_list_opt ',' string_literal
1675                { $$ = (ExpressionNode *)($1->set_last( new ExpressionNode( $3 ) )); }
1676        ;
1677
1678label_list:
1679        identifier
1680                {
1681                        $$ = new LabelNode(); $$->labels.push_back( *$1 );
1682                        delete $1;                                                                      // allocated by lexer
1683                }
1684        | label_list ',' identifier
1685                {
1686                        $$ = $1; $1->labels.push_back( *$3 );
1687                        delete $3;                                                                      // allocated by lexer
1688                }
1689        ;
1690
1691//******************************* DECLARATIONS *********************************
1692
1693declaration_list_opt:                                                                   // used at beginning of switch statement
1694        // empty
1695                { $$ = nullptr; }
1696        | declaration_list
1697        ;
1698
1699declaration_list:
1700        declaration
1701        | declaration_list declaration
1702                { $$ = $1->appendList( $2 ); }
1703        ;
1704
1705KR_parameter_list_opt:                                                                  // used to declare parameter types in K&R style functions
1706        // empty
1707                { $$ = nullptr; }
1708        | KR_parameter_list
1709        ;
1710
1711KR_parameter_list:
1712        push c_declaration pop ';'
1713                { $$ = $2; }
1714        | KR_parameter_list push c_declaration pop ';'
1715                { $$ = $1->appendList( $3 ); }
1716        ;
1717
1718local_label_declaration_opt:                                                    // GCC, local label
1719        // empty
1720        | local_label_declaration_list
1721        ;
1722
1723local_label_declaration_list:                                                   // GCC, local label
1724        LABEL local_label_list ';'
1725        | local_label_declaration_list LABEL local_label_list ';'
1726        ;
1727
1728local_label_list:                                                                               // GCC, local label
1729        identifier_or_type_name
1730        | local_label_list ',' identifier_or_type_name
1731        ;
1732
1733declaration:                                                                                    // old & new style declarations
1734        c_declaration ';'
1735                {
1736                        // printf( "C_DECLARATION1 %p %s\n", $$, $$->name ? $$->name->c_str() : "(nil)" );
1737                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
1738                        //   printf( "\tattr %s\n", attr->name.c_str() );
1739                        // } // for
1740                }
1741        | cfa_declaration ';'                                                           // CFA
1742        | static_assert                                                                         // C11
1743        ;
1744
1745static_assert:
1746        STATICASSERT '(' constant_expression ',' string_literal ')' ';' // C11
1747                { $$ = DeclarationNode::newStaticAssert( $3, $5 ); }
1748        | STATICASSERT '(' constant_expression ')' ';'          // CFA
1749                { $$ = DeclarationNode::newStaticAssert( $3, build_constantStr( *new string( "\"\"" ) ) ); }
1750
1751// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
1752// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
1753// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
1754// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
1755// the declaration list (as in Pascal).  ANSI C and the new CFA declarations may appear together in the same program
1756// block, but cannot be mixed within a specific declaration.
1757//
1758//                      CFA                                     C
1759//              [10] int x;                     int x[10];              // array of 10 integers
1760//              [10] * char y;          char *y[10];    // array of 10 pointers to char
1761
1762cfa_declaration:                                                                                // CFA
1763        cfa_variable_declaration
1764        | cfa_typedef_declaration
1765        | cfa_function_declaration
1766        | type_declaring_list
1767                { SemanticError( yylloc, "otype declaration is currently unimplemented." ); $$ = nullptr; }
1768        | trait_specifier
1769        ;
1770
1771cfa_variable_declaration:                                                               // CFA
1772        cfa_variable_specifier initializer_opt
1773                { $$ = $1->addInitializer( $2 ); }
1774        | declaration_qualifier_list cfa_variable_specifier initializer_opt
1775                // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
1776                // them as a type_qualifier cannot appear in that context.
1777                { $$ = $2->addQualifiers( $1 )->addInitializer( $3 ); }
1778        | cfa_variable_declaration pop ',' push identifier_or_type_name initializer_opt
1779                { $$ = $1->appendList( $1->cloneType( $5 )->addInitializer( $6 ) ); }
1780        ;
1781
1782cfa_variable_specifier:                                                                 // CFA
1783                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1784                // storage-class
1785        cfa_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
1786                { $$ = $1->addName( $2 )->addAsmName( $3 ); }
1787        | cfa_abstract_tuple identifier_or_type_name asm_name_opt
1788                { $$ = $1->addName( $2 )->addAsmName( $3 ); }
1789        | type_qualifier_list cfa_abstract_tuple identifier_or_type_name asm_name_opt
1790                { $$ = $2->addQualifiers( $1 )->addName( $3 )->addAsmName( $4 ); }
1791        ;
1792
1793cfa_function_declaration:                                                               // CFA
1794        cfa_function_specifier
1795        | type_qualifier_list cfa_function_specifier
1796                { $$ = $2->addQualifiers( $1 ); }
1797        | declaration_qualifier_list cfa_function_specifier
1798                { $$ = $2->addQualifiers( $1 ); }
1799        | declaration_qualifier_list type_qualifier_list cfa_function_specifier
1800                { $$ = $3->addQualifiers( $1 )->addQualifiers( $2 ); }
1801        | cfa_function_declaration ',' identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')'
1802                {
1803                        // Append the return type at the start (left-hand-side) to each identifier in the list.
1804                        DeclarationNode * ret = new DeclarationNode;
1805                        ret->type = maybeClone( $1->type->base );
1806                        $$ = $1->appendList( DeclarationNode::newFunction( $3, ret, $6, nullptr ) );
1807                }
1808        ;
1809
1810cfa_function_specifier:                                                                 // CFA
1811//      '[' ']' identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' // S/R conflict
1812//              {
1813//                      $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, 0, true );
1814//              }
1815//      '[' ']' identifier '(' push cfa_parameter_ellipsis_list_opt pop ')'
1816//              {
1817//                      typedefTable.setNextIdentifier( *$5 );
1818//                      $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1819//              }
1820//      | '[' ']' TYPEDEFname '(' push cfa_parameter_ellipsis_list_opt pop ')'
1821//              {
1822//                      typedefTable.setNextIdentifier( *$5 );
1823//                      $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1824//              }
1825//      | '[' ']' typegen_name
1826                // identifier_or_type_name must be broken apart because of the sequence:
1827                //
1828                //   '[' ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
1829                //   '[' ']' type_specifier
1830                //
1831                // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1832                // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
1833        cfa_abstract_tuple identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' attribute_list_opt
1834                // To obtain LR(1 ), this rule must be factored out from function return type (see cfa_abstract_declarator).
1835                { $$ = DeclarationNode::newFunction( $2, $1, $5, 0 )->addQualifiers( $8 ); }
1836        | cfa_function_return identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' attribute_list_opt
1837                { $$ = DeclarationNode::newFunction( $2, $1, $5, 0 )->addQualifiers( $8 ); }
1838        ;
1839
1840cfa_function_return:                                                                    // CFA
1841        '[' push cfa_parameter_list pop ']'
1842                { $$ = DeclarationNode::newTuple( $3 ); }
1843        | '[' push cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ']'
1844                // To obtain LR(1 ), the last cfa_abstract_parameter_list is added into this flattened rule to lookahead to the ']'.
1845                { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
1846        ;
1847
1848cfa_typedef_declaration:                                                                // CFA
1849        TYPEDEF cfa_variable_specifier
1850                {
1851                        typedefTable.addToEnclosingScope( *$2->name, TYPEDEFname, "1" );
1852                        $$ = $2->addTypedef();
1853                }
1854        | TYPEDEF cfa_function_specifier
1855                {
1856                        typedefTable.addToEnclosingScope( *$2->name, TYPEDEFname, "2" );
1857                        $$ = $2->addTypedef();
1858                }
1859        | cfa_typedef_declaration pop ',' push identifier
1860                {
1861                        typedefTable.addToEnclosingScope( *$5, TYPEDEFname, "3" );
1862                        $$ = $1->appendList( $1->cloneType( $5 ) );
1863                }
1864        ;
1865
1866// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1867// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
1868
1869typedef_declaration:
1870        TYPEDEF type_specifier declarator
1871                {
1872                        typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "4" );
1873                        $$ = $3->addType( $2 )->addTypedef();
1874                }
1875        | typedef_declaration pop ',' push declarator
1876                {
1877                        typedefTable.addToEnclosingScope( *$5->name, TYPEDEFname, "5" );
1878                        $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1879                }
1880        | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
1881                {
1882                        typedefTable.addToEnclosingScope( *$4->name, TYPEDEFname, "6" );
1883                        $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1884                }
1885        | type_specifier TYPEDEF declarator
1886                {
1887                        typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "7" );
1888                        $$ = $3->addType( $1 )->addTypedef();
1889                }
1890        | type_specifier TYPEDEF type_qualifier_list declarator
1891                {
1892                        typedefTable.addToEnclosingScope( *$4->name, TYPEDEFname, "8" );
1893                        $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
1894                }
1895        ;
1896
1897typedef_expression:
1898                // deprecated GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
1899        TYPEDEF identifier '=' assignment_expression
1900                {
1901                        SemanticError( yylloc, "Typedef expression is deprecated, use typeof(...) instead." ); $$ = nullptr;
1902                }
1903        | typedef_expression pop ',' push identifier '=' assignment_expression
1904                {
1905                        SemanticError( yylloc, "Typedef expression is deprecated, use typeof(...) instead." ); $$ = nullptr;
1906                }
1907        ;
1908
1909c_declaration:
1910        declaration_specifier declaring_list
1911                { $$ = distAttr( $1, $2 ); }
1912        | typedef_declaration
1913        | typedef_expression                                                            // deprecated GCC, naming expression type
1914        | sue_declaration_specifier
1915        ;
1916
1917declaring_list:
1918                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1919                // storage-class
1920        declarator asm_name_opt initializer_opt
1921                { $$ = $1->addAsmName( $2 )->addInitializer( $3 ); }
1922        | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1923                { $$ = $1->appendList( $4->addQualifiers( $3 )->addAsmName( $5 )->addInitializer( $6 ) ); }
1924        ;
1925
1926declaration_specifier:                                                                  // type specifier + storage class
1927        basic_declaration_specifier
1928        | sue_declaration_specifier
1929        | type_declaration_specifier
1930        ;
1931
1932declaration_specifier_nobody:                                                   // type specifier + storage class - {...}
1933                // Preclude SUE declarations in restricted scopes:
1934                //
1935                //    int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1936                //
1937                // because it is impossible to call f due to name equivalence.
1938        basic_declaration_specifier
1939        | sue_declaration_specifier_nobody
1940        | type_declaration_specifier
1941        ;
1942
1943type_specifier:                                                                                 // type specifier
1944        basic_type_specifier
1945        | sue_type_specifier
1946                {
1947                        // printf( "sue_type_specifier2 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
1948                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
1949                        //   printf( "\tattr %s\n", attr->name.c_str() );
1950                        // } // for
1951                }
1952        | type_type_specifier
1953        ;
1954
1955type_specifier_nobody:                                                                  // type specifier - {...}
1956                // Preclude SUE declarations in restricted scopes:
1957                //
1958                //    int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1959                //
1960                // because it is impossible to call f due to name equivalence.
1961        basic_type_specifier
1962        | sue_type_specifier_nobody
1963        | type_type_specifier
1964        ;
1965
1966type_qualifier_list_opt:                                                                // GCC, used in asm_statement
1967        // empty
1968                { $$ = nullptr; }
1969        | type_qualifier_list
1970        ;
1971
1972type_qualifier_list:
1973                // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
1974                //
1975                // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1976                // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1977                // appeared only once.
1978        type_qualifier
1979        | type_qualifier_list type_qualifier
1980                { $$ = $1->addQualifiers( $2 ); }
1981        ;
1982
1983type_qualifier:
1984        type_qualifier_name
1985        | attribute                                                                                     // trick handles most atrribute locations
1986        ;
1987
1988type_qualifier_name:
1989        CONST
1990                { $$ = DeclarationNode::newTypeQualifier( Type::Const ); }
1991        | RESTRICT
1992                { $$ = DeclarationNode::newTypeQualifier( Type::Restrict ); }
1993        | VOLATILE
1994                { $$ = DeclarationNode::newTypeQualifier( Type::Volatile ); }
1995        | ATOMIC
1996                { $$ = DeclarationNode::newTypeQualifier( Type::Atomic ); }
1997        | forall
1998        ;
1999
2000forall:
2001        FORALL '(' type_parameter_list ')'                                      // CFA
2002                { $$ = DeclarationNode::newForall( $3 ); }
2003        ;
2004
2005declaration_qualifier_list:
2006        storage_class_list
2007        | type_qualifier_list storage_class_list                        // remaining OBSOLESCENT (see 2 )
2008                { $$ = $1->addQualifiers( $2 ); }
2009        | declaration_qualifier_list type_qualifier_list storage_class_list
2010                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2011        ;
2012
2013storage_class_list:
2014                // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
2015                // only one of each is specified, except for inline, which can appear with the others.
2016                //
2017                // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
2018                // specifiers in a declaration.
2019        storage_class
2020        | storage_class_list storage_class
2021                { $$ = $1->addQualifiers( $2 ); }
2022        ;
2023
2024storage_class:
2025        EXTERN
2026                { $$ = DeclarationNode::newStorageClass( Type::Extern ); }
2027        | STATIC
2028                { $$ = DeclarationNode::newStorageClass( Type::Static ); }
2029        | AUTO
2030                { $$ = DeclarationNode::newStorageClass( Type::Auto ); }
2031        | REGISTER
2032                { $$ = DeclarationNode::newStorageClass( Type::Register ); }
2033        | THREADLOCAL                                                                           // C11
2034                { $$ = DeclarationNode::newStorageClass( Type::Threadlocal ); }
2035                // Put function specifiers here to simplify parsing rules, but separate them semantically.
2036        | INLINE                                                                                        // C99
2037                { $$ = DeclarationNode::newFuncSpecifier( Type::Inline ); }
2038        | FORTRAN                                                                                       // C99
2039                { $$ = DeclarationNode::newFuncSpecifier( Type::Fortran ); }
2040        | NORETURN                                                                                      // C11
2041                { $$ = DeclarationNode::newFuncSpecifier( Type::Noreturn ); }
2042        ;
2043
2044basic_type_name:
2045        VOID
2046                { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
2047        | BOOL                                                                                          // C99
2048                { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
2049        | CHAR
2050                { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
2051        | INT
2052                { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
2053        | INT128
2054                { $$ = DeclarationNode::newBasicType( DeclarationNode::Int128 ); }
2055        | UINT128
2056                { $$ = DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ); }
2057        | FLOAT
2058                { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
2059        | DOUBLE
2060                { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
2061        | uuFLOAT80
2062                { $$ = DeclarationNode::newBasicType( DeclarationNode::uuFloat80 ); }
2063        | uuFLOAT128
2064                { $$ = DeclarationNode::newBasicType( DeclarationNode::uuFloat128 ); }
2065        | uFLOAT16
2066                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat16 ); }
2067        | uFLOAT32
2068                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat32 ); }
2069        | uFLOAT32X
2070                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat32x ); }
2071        | uFLOAT64
2072                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat64 ); }
2073        | uFLOAT64X
2074                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat64x ); }
2075        | uFLOAT128
2076                { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat128 ); }
2077        | DECIMAL32
2078                { SemanticError( yylloc, "_Decimal32 is currently unimplemented." ); $$ = nullptr; }
2079        | DECIMAL64
2080                { SemanticError( yylloc, "_Decimal64 is currently unimplemented." ); $$ = nullptr; }
2081        | DECIMAL128
2082                { SemanticError( yylloc, "_Decimal128 is currently unimplemented." ); $$ = nullptr; }
2083        | COMPLEX                                                                                       // C99
2084                { $$ = DeclarationNode::newComplexType( DeclarationNode::Complex ); }
2085        | IMAGINARY                                                                                     // C99
2086                { $$ = DeclarationNode::newComplexType( DeclarationNode::Imaginary ); }
2087        | SIGNED
2088                { $$ = DeclarationNode::newSignedNess( DeclarationNode::Signed ); }
2089        | UNSIGNED
2090                { $$ = DeclarationNode::newSignedNess( DeclarationNode::Unsigned ); }
2091        | SHORT
2092                { $$ = DeclarationNode::newLength( DeclarationNode::Short ); }
2093        | LONG
2094                { $$ = DeclarationNode::newLength( DeclarationNode::Long ); }
2095        | VALIST                                                                                        // GCC, __builtin_va_list
2096                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Valist ); }
2097        | AUTO_TYPE
2098                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::AutoType ); }
2099        | vtable
2100        ;
2101
2102vtable_opt:
2103        // empty
2104                { $$ = nullptr; }
2105        | vtable
2106        ;
2107
2108vtable:
2109        VTABLE '(' type_name ')' default_opt
2110                { $$ = DeclarationNode::newVtableType( $3 ); }
2111                // { SemanticError( yylloc, "vtable is currently unimplemented." ); $$ = nullptr; }
2112        ;
2113
2114default_opt:
2115        // empty
2116                { $$ = nullptr; }
2117        | DEFAULT
2118                { SemanticError( yylloc, "vtable default is currently unimplemented." ); $$ = nullptr; }
2119        ;
2120
2121basic_declaration_specifier:
2122                // A semantic check is necessary for conflicting storage classes.
2123        basic_type_specifier
2124        | declaration_qualifier_list basic_type_specifier
2125                { $$ = $2->addQualifiers( $1 ); }
2126        | basic_declaration_specifier storage_class                     // remaining OBSOLESCENT (see 2)
2127                { $$ = $1->addQualifiers( $2 ); }
2128        | basic_declaration_specifier storage_class type_qualifier_list
2129                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2130        | basic_declaration_specifier storage_class basic_type_specifier
2131                { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
2132        ;
2133
2134basic_type_specifier:
2135        direct_type
2136                // Cannot have type modifiers, e.g., short, long, etc.
2137        | type_qualifier_list_opt indirect_type type_qualifier_list_opt
2138                { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
2139        ;
2140
2141direct_type:
2142        basic_type_name
2143        | type_qualifier_list basic_type_name
2144                { $$ = $2->addQualifiers( $1 ); }
2145        | direct_type type_qualifier
2146                { $$ = $1->addQualifiers( $2 ); }
2147        | direct_type basic_type_name
2148                { $$ = $1->addType( $2 ); }
2149        ;
2150
2151indirect_type:
2152        TYPEOF '(' type ')'                                                                     // GCC: typeof( x ) y;
2153                { $$ = $3; }
2154        | TYPEOF '(' comma_expression ')'                                       // GCC: typeof( a+b ) y;
2155                { $$ = DeclarationNode::newTypeof( $3 ); }
2156        | BASETYPEOF '(' type ')'                                                       // CFA: basetypeof( x ) y;
2157                { $$ = DeclarationNode::newTypeof( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ), true ); }
2158        | BASETYPEOF '(' comma_expression ')'                           // CFA: basetypeof( a+b ) y;
2159                { $$ = DeclarationNode::newTypeof( $3, true ); }
2160        | ZERO_T                                                                                        // CFA
2161                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Zero ); }
2162        | ONE_T                                                                                         // CFA
2163                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::One ); }
2164        ;
2165
2166sue_declaration_specifier:                                                              // struct, union, enum + storage class + type specifier
2167        sue_type_specifier
2168                {
2169                        // printf( "sue_declaration_specifier %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2170                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2171                        //   printf( "\tattr %s\n", attr->name.c_str() );
2172                        // } // for
2173                }
2174        | declaration_qualifier_list sue_type_specifier
2175                { $$ = $2->addQualifiers( $1 ); }
2176        | sue_declaration_specifier storage_class                       // remaining OBSOLESCENT (see 2)
2177                { $$ = $1->addQualifiers( $2 ); }
2178        | sue_declaration_specifier storage_class type_qualifier_list
2179                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2180        ;
2181
2182sue_type_specifier:                                                                             // struct, union, enum + type specifier
2183        elaborated_type
2184                {
2185                        // printf( "sue_type_specifier %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2186                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2187                        //   printf( "\tattr %s\n", attr->name.c_str() );
2188                        // } // for
2189                }
2190        | type_qualifier_list
2191                { if ( $1->type != nullptr && $1->type->forall ) forall = true; } // remember generic type
2192          elaborated_type
2193                { $$ = $3->addQualifiers( $1 ); }
2194        | sue_type_specifier type_qualifier
2195                {
2196                        if ( $2->type != nullptr && $2->type->forall ) forall = true; // remember generic type
2197                        $$ = $1->addQualifiers( $2 );
2198                }
2199        ;
2200
2201sue_declaration_specifier_nobody:                                               // struct, union, enum - {...} + storage class + type specifier
2202        sue_type_specifier_nobody
2203        | declaration_qualifier_list sue_type_specifier_nobody
2204                { $$ = $2->addQualifiers( $1 ); }
2205        | sue_declaration_specifier_nobody storage_class        // remaining OBSOLESCENT (see 2)
2206                { $$ = $1->addQualifiers( $2 ); }
2207        | sue_declaration_specifier_nobody storage_class type_qualifier_list
2208                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2209        ;
2210
2211sue_type_specifier_nobody:                                                              // struct, union, enum - {...} + type specifier
2212        elaborated_type_nobody
2213        | type_qualifier_list elaborated_type_nobody
2214                { $$ = $2->addQualifiers( $1 ); }
2215        | sue_type_specifier_nobody type_qualifier
2216                { $$ = $1->addQualifiers( $2 ); }
2217        ;
2218
2219type_declaration_specifier:
2220        type_type_specifier
2221        | declaration_qualifier_list type_type_specifier
2222                { $$ = $2->addQualifiers( $1 ); }
2223        | type_declaration_specifier storage_class                      // remaining OBSOLESCENT (see 2)
2224                { $$ = $1->addQualifiers( $2 ); }
2225        | type_declaration_specifier storage_class type_qualifier_list
2226                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2227        ;
2228
2229type_type_specifier:                                                                    // typedef types
2230        type_name
2231        | type_qualifier_list type_name
2232                { $$ = $2->addQualifiers( $1 ); }
2233        | type_type_specifier type_qualifier
2234                { $$ = $1->addQualifiers( $2 ); }
2235        ;
2236
2237type_name:
2238        TYPEDEFname
2239                { $$ = DeclarationNode::newFromTypedef( $1 ); }
2240        | '.' TYPEDEFname
2241                { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), DeclarationNode::newFromTypedef( $2 ) ); }
2242        | type_name '.' TYPEDEFname
2243                { $$ = DeclarationNode::newQualifiedType( $1, DeclarationNode::newFromTypedef( $3 ) ); }
2244        | typegen_name
2245        | '.' typegen_name
2246                { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), $2 ); }
2247        | type_name '.' typegen_name
2248                { $$ = DeclarationNode::newQualifiedType( $1, $3 ); }
2249        ;
2250
2251typegen_name:                                                                                   // CFA
2252        TYPEGENname
2253                { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
2254        | TYPEGENname '(' ')'
2255                { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
2256        | TYPEGENname '(' type_list ')'
2257                { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
2258        ;
2259
2260elaborated_type:                                                                                // struct, union, enum
2261        aggregate_type
2262                {
2263                        // printf( "elaborated_type %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2264                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2265                        //   printf( "\tattr %s\n", attr->name.c_str() );
2266                        // } // for
2267                }
2268        | enum_type
2269        ;
2270
2271elaborated_type_nobody:                                                                 // struct, union, enum - {...}
2272        aggregate_type_nobody
2273        | enum_type_nobody
2274        ;
2275
2276aggregate_type:                                                                                 // struct, union
2277        aggregate_key attribute_list_opt
2278                { forall = false; }                                                             // reset
2279          '{' field_declaration_list_opt '}' type_parameters_opt
2280                { $$ = DeclarationNode::newAggregate( $1, nullptr, $7, $5, true )->addQualifiers( $2 ); }
2281        | aggregate_key attribute_list_opt identifier
2282                {
2283                        typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef
2284                        forall = false;                                                         // reset
2285                }
2286          '{' field_declaration_list_opt '}' type_parameters_opt
2287                {
2288                        // printf( "aggregate_type1 %s\n", $3.str->c_str() );
2289                        // if ( $2 )
2290                        //      for ( Attribute * attr: reverseIterate( $2->attributes ) ) {
2291                        //              printf( "copySpecifiers12 %s\n", attr->name.c_str() );
2292                        //      } // for
2293                        $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
2294                        // printf( "aggregate_type2 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2295                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2296                        //      printf( "aggregate_type3 %s\n", attr->name.c_str() );
2297                        // } // for
2298                }
2299        | aggregate_key attribute_list_opt TYPEDEFname          // unqualified type name
2300                {
2301                        typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef
2302                        forall = false;                                                         // reset
2303                }
2304          '{' field_declaration_list_opt '}' type_parameters_opt
2305                {
2306                        // printf( "AGG3\n" );
2307                        DeclarationNode::newFromTypedef( $3 );
2308                        $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
2309                }
2310        | aggregate_key attribute_list_opt TYPEGENname          // unqualified type name
2311                {
2312                        typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef
2313                        forall = false;                                                         // reset
2314                }
2315          '{' field_declaration_list_opt '}' type_parameters_opt
2316                {
2317                        // printf( "AGG4\n" );
2318                        DeclarationNode::newFromTypeGen( $3, nullptr );
2319                        $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
2320                }
2321        | aggregate_type_nobody
2322        ;
2323
2324type_parameters_opt:
2325        // empty
2326                { $$ = nullptr; }                                                               %prec '}'
2327        | '(' type_list ')'
2328                { $$ = $2; }
2329        ;
2330
2331aggregate_type_nobody:                                                                  // struct, union - {...}
2332        aggregate_key attribute_list_opt identifier
2333                {
2334                        typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname );
2335                        forall = false;                                                         // reset
2336                        $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
2337                }
2338        | aggregate_key attribute_list_opt type_name
2339                {
2340                        forall = false;                                                         // reset
2341                        // Create new generic declaration with same name as previous forward declaration, where the IDENTIFIER is
2342                        // switched to a TYPEGENname. Link any generic arguments from typegen_name to new generic declaration and
2343                        // delete newFromTypeGen.
2344                        $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $3->type->symbolic.actuals, nullptr, false )->addQualifiers( $2 );
2345                        $3->type->symbolic.name = nullptr;
2346                        $3->type->symbolic.actuals = nullptr;
2347                        delete $3;
2348                }
2349        ;
2350
2351aggregate_key:
2352        aggregate_data
2353        | aggregate_control
2354        ;
2355
2356aggregate_data:
2357        STRUCT vtable_opt
2358                { $$ = AggregateDecl::Struct; }
2359        | UNION
2360                { $$ = AggregateDecl::Union; }
2361        | EXCEPTION                                                                                     // CFA
2362                { $$ = AggregateDecl::Exception; }
2363          //            { SemanticError( yylloc, "exception aggregate is currently unimplemented." ); $$ = AggregateDecl::NoAggregate; }
2364        ;
2365
2366aggregate_control:                                                                              // CFA
2367        MONITOR
2368                { $$ = AggregateDecl::Monitor; }
2369        | MUTEX STRUCT
2370                { $$ = AggregateDecl::Monitor; }
2371        | GENERATOR
2372                { $$ = AggregateDecl::Generator; }
2373        | MUTEX GENERATOR
2374                { SemanticError( yylloc, "monitor generator is currently unimplemented." ); $$ = AggregateDecl::NoAggregate; }
2375        | COROUTINE
2376                { $$ = AggregateDecl::Coroutine; }
2377        | MUTEX COROUTINE
2378                { SemanticError( yylloc, "monitor coroutine is currently unimplemented." ); $$ = AggregateDecl::NoAggregate; }
2379        | THREAD
2380                { $$ = AggregateDecl::Thread; }
2381        | MUTEX THREAD
2382                { SemanticError( yylloc, "monitor thread is currently unimplemented." ); $$ = AggregateDecl::NoAggregate; }
2383        ;
2384
2385field_declaration_list_opt:
2386        // empty
2387                { $$ = nullptr; }
2388        | field_declaration_list_opt field_declaration
2389                { $$ = $1 ? $1->appendList( $2 ) : $2; }
2390        ;
2391
2392field_declaration:
2393        type_specifier field_declaring_list_opt ';'
2394                {
2395                        // printf( "type_specifier1 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2396                        $$ = fieldDecl( $1, $2 );
2397                        // printf( "type_specifier2 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2398                        // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2399                        //   printf( "\tattr %s\n", attr->name.c_str() );
2400                        // } // for
2401                }
2402        | EXTENSION type_specifier field_declaring_list_opt ';' // GCC
2403                { $$ = fieldDecl( $2, $3 ); distExt( $$ ); }
2404        | INLINE type_specifier field_abstract_list_opt ';'     // CFA
2405                {
2406                        if ( ! $3 ) {                                                           // field declarator ?
2407                                $3 = DeclarationNode::newName( nullptr );
2408                        } // if
2409                        $3->inLine = true;
2410                        $$ = distAttr( $2, $3 );                                        // mark all fields in list
2411                        distInl( $3 );
2412                }
2413        | INLINE aggregate_control ';'                                          // CFA
2414                { SemanticError( yylloc, "INLINE aggregate control currently unimplemented." ); $$ = nullptr; }
2415        | typedef_declaration ';'                                                       // CFA
2416        | cfa_field_declaring_list ';'                                          // CFA, new style field declaration
2417        | EXTENSION cfa_field_declaring_list ';'                        // GCC
2418                { distExt( $2 ); $$ = $2; }                                             // mark all fields in list
2419        | INLINE cfa_field_abstract_list ';'                            // CFA, new style field declaration
2420                { $$ = $2; }                                                                    // mark all fields in list
2421        | cfa_typedef_declaration ';'                                           // CFA
2422        | static_assert                                                                         // C11
2423        ;
2424
2425field_declaring_list_opt:
2426        // empty
2427                { $$ = nullptr; }
2428        | field_declarator
2429        | field_declaring_list_opt ',' attribute_list_opt field_declarator
2430                { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
2431        ;
2432
2433field_declarator:
2434        bit_subrange_size                                                                       // C special case, no field name
2435                { $$ = DeclarationNode::newBitfield( $1 ); }
2436        | variable_declarator bit_subrange_size_opt
2437                // A semantic check is required to ensure bit_subrange only appears on integral types.
2438                { $$ = $1->addBitfield( $2 ); }
2439        | variable_type_redeclarator bit_subrange_size_opt
2440                // A semantic check is required to ensure bit_subrange only appears on integral types.
2441                { $$ = $1->addBitfield( $2 ); }
2442        ;
2443
2444field_abstract_list_opt:
2445        // empty
2446                { $$ = nullptr; }
2447        | field_abstract
2448        | field_abstract_list_opt ',' attribute_list_opt field_abstract
2449                { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
2450        ;
2451
2452field_abstract:
2453                //      no bit fields
2454        variable_abstract_declarator
2455        ;
2456
2457cfa_field_declaring_list:                                                               // CFA, new style field declaration
2458        // bit-fields are handled by C declarations
2459        cfa_abstract_declarator_tuple identifier_or_type_name
2460                { $$ = $1->addName( $2 ); }
2461        | cfa_field_declaring_list ',' identifier_or_type_name
2462                { $$ = $1->appendList( $1->cloneType( $3 ) ); }
2463        ;
2464
2465cfa_field_abstract_list:                                                                // CFA, new style field declaration
2466        // bit-fields are handled by C declarations
2467        cfa_abstract_declarator_tuple
2468        | cfa_field_abstract_list ','
2469                { $$ = $1->appendList( $1->cloneType( 0 ) ); }
2470        ;
2471
2472bit_subrange_size_opt:
2473        // empty
2474                { $$ = nullptr; }
2475        | bit_subrange_size
2476        ;
2477
2478bit_subrange_size:
2479        ':' assignment_expression
2480                { $$ = $2; }
2481        ;
2482
2483enum_type:
2484        ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
2485                { $$ = DeclarationNode::newEnum( nullptr, $4, true )->addQualifiers( $2 ); }
2486        | ENUM attribute_list_opt identifier
2487                { typedefTable.makeTypedef( *$3 ); }
2488          '{' enumerator_list comma_opt '}'
2489                { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
2490        | ENUM attribute_list_opt typedef_name                          // unqualified type name
2491          '{' enumerator_list comma_opt '}'
2492                { $$ = DeclarationNode::newEnum( $3->name, $5, true )->addQualifiers( $2 ); }
2493        | ENUM '(' ')' attribute_list_opt '{' enumerator_list comma_opt '}'
2494                { SemanticError( yylloc, "Unvalued enumerated type is currently unimplemented." ); $$ = nullptr; }
2495        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt '{' enumerator_list comma_opt '}'
2496                {
2497                        if ( $3->storageClasses.val != 0 || $3->type->qualifiers.val != 0 )
2498                        { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
2499
2500                        $$ = DeclarationNode::newEnum( nullptr, $7, true, $3 )->addQualifiers( $5 );
2501                }
2502        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt
2503                {
2504                        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." ); }
2505                        typedefTable.makeTypedef( *$6 );
2506                }
2507          '{' enumerator_list comma_opt '}'
2508                {
2509                        $$ = DeclarationNode::newEnum( $6, $10, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
2510                }
2511        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
2512                {
2513                        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." ); }
2514                        typedefTable.makeTypedef( *$6->name );
2515                        $$ = DeclarationNode::newEnum( $6->name, $9, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
2516                }
2517        | enum_type_nobody
2518        ;
2519
2520enum_type_nobody:                                                                               // enum - {...}
2521        ENUM attribute_list_opt identifier
2522                { typedefTable.makeTypedef( *$3 ); $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 ); }
2523        | ENUM attribute_list_opt type_name                                     // qualified type name
2524                { typedefTable.makeTypedef( *$3->type->symbolic.name ); $$ = DeclarationNode::newEnum( $3->type->symbolic.name, 0, false )->addQualifiers( $2 ); }
2525        ;
2526
2527enumerator_list:
2528        identifier_or_type_name enumerator_value_opt
2529                { $$ = DeclarationNode::newEnumValueGeneric( $1, $2 ); }
2530        | INLINE type_name
2531                { $$ = DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ); }
2532        | enumerator_list ',' identifier_or_type_name enumerator_value_opt
2533                { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( $3, $4 ) ); }
2534        | enumerator_list ',' INLINE type_name enumerator_value_opt
2535                { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ) ); }
2536        ;
2537
2538enumerator_value_opt:
2539        // empty
2540                { $$ = nullptr; }
2541        | '=' constant_expression                                       { $$ = new InitializerNode( $2 ); }
2542        | '=' '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $3, true ); }
2543        // | simple_assignment_operator initializer
2544        //      { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
2545        ;
2546
2547cfa_parameter_ellipsis_list_opt:                                                // CFA, abstract + real
2548        // empty
2549                { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
2550        | ELLIPSIS
2551                { $$ = nullptr; }
2552        | cfa_abstract_parameter_list
2553        | cfa_parameter_list
2554        | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
2555                { $$ = $1->appendList( $5 ); }
2556        | cfa_abstract_parameter_list pop ',' push ELLIPSIS
2557                { $$ = $1->addVarArgs(); }
2558        | cfa_parameter_list pop ',' push ELLIPSIS
2559                { $$ = $1->addVarArgs(); }
2560        ;
2561
2562cfa_parameter_list:                                                                             // CFA
2563                // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
2564                // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
2565        cfa_parameter_declaration
2566        | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
2567                { $$ = $1->appendList( $5 ); }
2568        | cfa_parameter_list pop ',' push cfa_parameter_declaration
2569                { $$ = $1->appendList( $5 ); }
2570        | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
2571                { $$ = $1->appendList( $5 )->appendList( $9 ); }
2572        ;
2573
2574cfa_abstract_parameter_list:                                                    // CFA, new & old style abstract
2575        cfa_abstract_parameter_declaration
2576        | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
2577                { $$ = $1->appendList( $5 ); }
2578        ;
2579
2580parameter_type_list_opt:
2581        // empty
2582                { $$ = nullptr; }
2583        | ELLIPSIS
2584                { $$ = nullptr; }
2585        | parameter_list
2586        | parameter_list pop ',' push ELLIPSIS
2587                { $$ = $1->addVarArgs(); }
2588        ;
2589
2590parameter_list:                                                                                 // abstract + real
2591        abstract_parameter_declaration
2592        | parameter_declaration
2593        | parameter_list pop ',' push abstract_parameter_declaration
2594                { $$ = $1->appendList( $5 ); }
2595        | parameter_list pop ',' push parameter_declaration
2596                { $$ = $1->appendList( $5 ); }
2597        ;
2598
2599// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
2600// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
2601
2602cfa_parameter_declaration:                                                              // CFA, new & old style parameter declaration
2603        parameter_declaration
2604        | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initializer_opt
2605                { $$ = $1->addName( $2 ); }
2606        | cfa_abstract_tuple identifier_or_type_name default_initializer_opt
2607                // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2608                { $$ = $1->addName( $2 ); }
2609        | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initializer_opt
2610                { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
2611        | cfa_function_specifier
2612        ;
2613
2614cfa_abstract_parameter_declaration:                                             // CFA, new & old style parameter declaration
2615        abstract_parameter_declaration
2616        | cfa_identifier_parameter_declarator_no_tuple
2617        | cfa_abstract_tuple
2618                // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2619        | type_qualifier_list cfa_abstract_tuple
2620                { $$ = $2->addQualifiers( $1 ); }
2621        | cfa_abstract_function
2622        ;
2623
2624parameter_declaration:
2625                // No SUE declaration in parameter list.
2626        declaration_specifier_nobody identifier_parameter_declarator default_initializer_opt
2627                { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2628        | declaration_specifier_nobody type_parameter_redeclarator default_initializer_opt
2629                { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2630        ;
2631
2632abstract_parameter_declaration:
2633        declaration_specifier_nobody default_initializer_opt
2634                { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
2635        | declaration_specifier_nobody abstract_parameter_declarator default_initializer_opt
2636                { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2637        ;
2638
2639// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
2640// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
2641// identifiers.  The ANSI-style parameter-list can redefine a typedef name.
2642
2643identifier_list:                                                                                // K&R-style parameter list => no types
2644        identifier
2645                { $$ = DeclarationNode::newName( $1 ); }
2646        | identifier_list ',' identifier
2647                { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
2648        ;
2649
2650identifier_or_type_name:
2651        identifier
2652        | TYPEDEFname
2653        | TYPEGENname
2654        ;
2655
2656type_no_function:                                                                               // sizeof, alignof, cast (constructor)
2657        cfa_abstract_declarator_tuple                                           // CFA
2658        | type_specifier
2659        | type_specifier abstract_declarator
2660                { $$ = $2->addType( $1 ); }
2661        ;
2662
2663type:                                                                                                   // typeof, assertion
2664        type_no_function
2665        | cfa_abstract_function                                                         // CFA
2666        ;
2667
2668initializer_opt:
2669        // empty
2670                { $$ = nullptr; }
2671        | simple_assignment_operator initializer        { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
2672        | '=' VOID                                                                      { $$ = new InitializerNode( true ); }
2673        | '{' initializer_list_opt comma_opt '}'        { $$ = new InitializerNode( $2, true ); }
2674        ;
2675
2676initializer:
2677        assignment_expression                                           { $$ = new InitializerNode( $1 ); }
2678        | '{' initializer_list_opt comma_opt '}'        { $$ = new InitializerNode( $2, true ); }
2679        ;
2680
2681initializer_list_opt:
2682        // empty
2683                { $$ = nullptr; }
2684        | initializer
2685        | designation initializer                                       { $$ = $2->set_designators( $1 ); }
2686        | initializer_list_opt ',' initializer          { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
2687        | initializer_list_opt ',' designation initializer { $$ = (InitializerNode *)($1->set_last( $4->set_designators( $3 ) )); }
2688        ;
2689
2690// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
2691// '=' to separator the designator from the initializer value, as in:
2692//
2693//              int x[10] = { [1] = 3 };
2694//
2695// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment.  To disambiguate this case, CFA
2696// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
2697// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
2698// shift/reduce conflicts
2699
2700designation:
2701        designator_list ':'                                                                     // C99, CFA uses ":" instead of "="
2702        | identifier_at ':'                                                                     // GCC, field name
2703                { $$ = new ExpressionNode( build_varref( $1 ) ); }
2704        ;
2705
2706designator_list:                                                                                // C99
2707        designator
2708        | designator_list designator
2709                { $$ = (ExpressionNode *)($1->set_last( $2 )); }
2710        //| designator_list designator                                          { $$ = new ExpressionNode( $1, $2 ); }
2711        ;
2712
2713designator:
2714        '.' identifier_at                                                                       // C99, field name
2715                { $$ = new ExpressionNode( build_varref( $2 ) ); }
2716        | '[' push assignment_expression pop ']'                        // C99, single array element
2717                // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
2718                { $$ = $3; }
2719        | '[' push subrange pop ']'                                                     // CFA, multiple array elements
2720                { $$ = $3; }
2721        | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
2722                { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $3 ), maybeMoveBuild<Expression>( $5 ) ) ); }
2723        | '.' '[' push field_name_list pop ']'                          // CFA, tuple field selector
2724                { $$ = $4; }
2725        ;
2726
2727// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
2728// rather than an object-oriented type system. This required four groups of extensions:
2729//
2730// Overloading: function, data, and operator identifiers may be overloaded.
2731//
2732// Type declarations: "otype" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
2733//     and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
2734//     definitions of new types. Type declarations with storage class "extern" provide opaque types.
2735//
2736// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
2737//     site. A polymorphic function is not a template; it is a function, with an address and a type.
2738//
2739// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
2740//     types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
2741//     hierarchies. Unlike classes, they can define relationships between types.  Assertions declare that a type or
2742//     types provide the operations declared by a specification.  Assertions are normally used to declare requirements
2743//     on type arguments of polymorphic functions.
2744
2745type_parameter_list:                                                                    // CFA
2746        type_parameter
2747        | type_parameter_list ',' type_parameter
2748                { $$ = $1->appendList( $3 ); }
2749        ;
2750
2751type_initializer_opt:                                                                   // CFA
2752        // empty
2753                { $$ = nullptr; }
2754        | '=' type
2755                { $$ = $2; }
2756        ;
2757
2758type_parameter:                                                                                 // CFA
2759        type_class identifier_or_type_name
2760                {
2761                        typedefTable.addToScope( *$2, TYPEDEFname, "9" );
2762                        if ( $1 == TypeDecl::Otype ) { SemanticError( yylloc, "otype keyword is deprecated, use T " ); }
2763                        if ( $1 == TypeDecl::Dtype ) { SemanticError( yylloc, "dtype keyword is deprecated, use T &" ); }
2764                        if ( $1 == TypeDecl::Ttype ) { SemanticError( yylloc, "ttype keyword is deprecated, use T ..." ); }
2765                }
2766          type_initializer_opt assertion_list_opt
2767                { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2768        | identifier_or_type_name new_type_class
2769                { typedefTable.addToScope( *$1, TYPEDEFname, "9" ); }
2770          type_initializer_opt assertion_list_opt
2771                { $$ = DeclarationNode::newTypeParam( $2, $1 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2772        | '[' identifier_or_type_name ']'
2773                {
2774                        typedefTable.addToScope( *$2, TYPEDIMname, "9" );
2775                        $$ = DeclarationNode::newTypeParam( TypeDecl::Dimension, $2 );
2776                }
2777        // | type_specifier identifier_parameter_declarator
2778        | assertion_list
2779                { $$ = DeclarationNode::newTypeParam( TypeDecl::Dtype, new string( DeclarationNode::anonymous.newName() ) )->addAssertions( $1 ); }
2780        ;
2781
2782new_type_class:                                                                                 // CFA
2783        // empty
2784                { $$ = TypeDecl::Otype; }
2785        | '&'
2786                { $$ = TypeDecl::Dtype; }
2787        | '*'
2788                { $$ = TypeDecl::DStype; }                                              // dtype + sized
2789        // | '(' '*' ')'
2790        //      { $$ = TypeDecl::Ftype; }
2791        | ELLIPSIS
2792                { $$ = TypeDecl::Ttype; }
2793        ;
2794
2795type_class:                                                                                             // CFA
2796        OTYPE
2797                { $$ = TypeDecl::Otype; }
2798        | DTYPE
2799                { $$ = TypeDecl::Dtype; }
2800        | FTYPE
2801                { $$ = TypeDecl::Ftype; }
2802        | TTYPE
2803                { $$ = TypeDecl::Ttype; }
2804        ;
2805
2806assertion_list_opt:                                                                             // CFA
2807        // empty
2808                { $$ = nullptr; }
2809        | assertion_list
2810        ;
2811
2812assertion_list:                                                                                 // CFA
2813        assertion
2814        | assertion_list assertion
2815                { $$ = $1->appendList( $2 ); }
2816        ;
2817
2818assertion:                                                                                              // CFA
2819        '|' identifier_or_type_name '(' type_list ')'
2820                { $$ = DeclarationNode::newTraitUse( $2, $4 ); }
2821        | '|' '{' push trait_declaration_list pop '}'
2822                { $$ = $4; }
2823        // | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list pop '}' '(' type_list ')'
2824        //      { SemanticError( yylloc, "Generic data-type assertion is currently unimplemented." ); $$ = nullptr; }
2825        ;
2826
2827type_list:                                                                                              // CFA
2828        type
2829                { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( $1 ) ) ); }
2830        | assignment_expression
2831        | type_list ',' type
2832                { $$ = (ExpressionNode *)($1->set_last( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ) )); }
2833        | type_list ',' assignment_expression
2834                { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
2835        ;
2836
2837type_declaring_list:                                                                    // CFA
2838        OTYPE type_declarator
2839                { $$ = $2; }
2840        | storage_class_list OTYPE type_declarator
2841                { $$ = $3->addQualifiers( $1 ); }
2842        | type_declaring_list ',' type_declarator
2843                { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
2844        ;
2845
2846type_declarator:                                                                                // CFA
2847        type_declarator_name assertion_list_opt
2848                { $$ = $1->addAssertions( $2 ); }
2849        | type_declarator_name assertion_list_opt '=' type
2850                { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2851        ;
2852
2853type_declarator_name:                                                                   // CFA
2854        identifier_or_type_name
2855                {
2856                        typedefTable.addToEnclosingScope( *$1, TYPEDEFname, "10" );
2857                        $$ = DeclarationNode::newTypeDecl( $1, 0 );
2858                }
2859        | identifier_or_type_name '(' type_parameter_list ')'
2860                {
2861                        typedefTable.addToEnclosingScope( *$1, TYPEGENname, "11" );
2862                        $$ = DeclarationNode::newTypeDecl( $1, $3 );
2863                }
2864        ;
2865
2866trait_specifier:                                                                                // CFA
2867        TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' '}'
2868                { $$ = DeclarationNode::newTrait( $2, $4, 0 ); }
2869        | TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' push trait_declaration_list pop '}'
2870                { $$ = DeclarationNode::newTrait( $2, $4, $8 ); }
2871        ;
2872
2873trait_declaration_list:                                                                 // CFA
2874        trait_declaration
2875        | trait_declaration_list pop push trait_declaration
2876                { $$ = $1->appendList( $4 ); }
2877        ;
2878
2879trait_declaration:                                                                              // CFA
2880        cfa_trait_declaring_list ';'
2881        | trait_declaring_list ';'
2882        ;
2883
2884cfa_trait_declaring_list:                                                               // CFA
2885        cfa_variable_specifier
2886        | cfa_function_specifier
2887        | cfa_trait_declaring_list pop ',' push identifier_or_type_name
2888                { $$ = $1->appendList( $1->cloneType( $5 ) ); }
2889        ;
2890
2891trait_declaring_list:                                                                   // CFA
2892        type_specifier declarator
2893                { $$ = $2->addType( $1 ); }
2894        | trait_declaring_list pop ',' push declarator
2895                { $$ = $1->appendList( $1->cloneBaseType( $5 ) ); }
2896        ;
2897
2898//***************************** EXTERNAL DEFINITIONS *****************************
2899
2900translation_unit:
2901        // empty, input file
2902        | external_definition_list
2903                { parseTree = parseTree ? parseTree->appendList( $1 ) : $1;     }
2904        ;
2905
2906external_definition_list:
2907        push external_definition pop
2908                { $$ = $2; }
2909        | external_definition_list push external_definition pop
2910                { $$ = $1 ? $1->appendList( $3 ) : $3; }
2911        ;
2912
2913external_definition_list_opt:
2914        // empty
2915                { $$ = nullptr; }
2916        | external_definition_list
2917        ;
2918
2919up:
2920                { typedefTable.up( forall ); forall = false; }
2921        ;
2922
2923down:
2924                { typedefTable.down(); }
2925        ;
2926
2927external_definition:
2928        DIRECTIVE
2929                { $$ = DeclarationNode::newDirectiveStmt( new StatementNode( build_directive( $1 ) ) ); }
2930        | declaration
2931        | external_function_definition
2932        | EXTENSION external_definition                                         // GCC, multiple __extension__ allowed, meaning unknown
2933                {
2934                        distExt( $2 );                                                          // mark all fields in list
2935                        $$ = $2;
2936                }
2937        | ASM '(' string_literal ')' ';'                                        // GCC, global assembler statement
2938                { $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asm( false, $3, 0 ) ) ); }
2939        | EXTERN STRINGliteral
2940                {
2941                        linkageStack.push( linkage );                           // handle nested extern "C"/"Cforall"
2942                        linkage = LinkageSpec::update( yylloc, linkage, $2 );
2943                }
2944          up external_definition down
2945                {
2946                        linkage = linkageStack.top();
2947                        linkageStack.pop();
2948                        $$ = $5;
2949                }
2950        | EXTERN STRINGliteral                                                          // C++-style linkage specifier
2951                {
2952                        linkageStack.push( linkage );                           // handle nested extern "C"/"Cforall"
2953                        linkage = LinkageSpec::update( yylloc, linkage, $2 );
2954                }
2955          '{' up external_definition_list_opt down '}'
2956                {
2957                        linkage = linkageStack.top();
2958                        linkageStack.pop();
2959                        $$ = $6;
2960                }
2961        | type_qualifier_list
2962                {
2963                        if ( $1->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
2964                        if ( $1->type->forall ) forall = true;          // remember generic type
2965                }
2966          '{' up external_definition_list_opt down '}'          // CFA, namespace
2967                {
2968                        distQual( $5, $1 );
2969                        forall = false;
2970                        $$ = $5;
2971                }
2972        | declaration_qualifier_list
2973                {
2974                        if ( $1->type && $1->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
2975                        if ( $1->type && $1->type->forall ) forall = true; // remember generic type
2976                }
2977          '{' up external_definition_list_opt down '}'          // CFA, namespace
2978                {
2979                        distQual( $5, $1 );
2980                        forall = false;
2981                        $$ = $5;
2982                }
2983        | declaration_qualifier_list type_qualifier_list
2984                {
2985                        if ( ($1->type && $1->type->qualifiers.val) || $2->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
2986                        if ( ($1->type && $1->type->forall) || $2->type->forall ) forall = true; // remember generic type
2987                }
2988          '{' up external_definition_list_opt down '}'          // CFA, namespace
2989                {
2990                        distQual( $6, $1->addQualifiers( $2 ) );
2991                        forall = false;
2992                        $$ = $6;
2993                }
2994        ;
2995
2996external_function_definition:
2997        function_definition
2998                // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
2999                // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
3000                // Parsing is possible because function_definition does not appear in the context of an expression (nested
3001                // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
3002                // declaration must still have a type_specifier.  OBSOLESCENT (see 1)
3003        | function_declarator compound_statement
3004                { $$ = $1->addFunctionBody( $2 ); }
3005        | KR_function_declarator KR_parameter_list_opt compound_statement
3006                { $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 ); }
3007        ;
3008
3009with_clause_opt:
3010        // empty
3011                { $$ = nullptr; forall = false; }
3012        | WITH '(' tuple_expression_list ')' attribute_list_opt
3013                {
3014                        $$ = $3; forall = false;
3015                        if ( $5 ) {
3016                                SemanticError( yylloc, "Attributes cannot be associated with function body. Move attribute(s) before \"with\" clause." );
3017                                $$ = nullptr;
3018                        } // if
3019                }
3020        ;
3021
3022function_definition:
3023        cfa_function_declaration with_clause_opt compound_statement     // CFA
3024                {
3025                        // Add the function body to the last identifier in the function definition list, i.e., foo3:
3026                        //   [const double] foo1(), foo2( int ), foo3( double ) { return 3.0; }
3027                        $1->get_last()->addFunctionBody( $3, $2 );
3028                        $$ = $1;
3029                }
3030        | declaration_specifier function_declarator with_clause_opt compound_statement
3031                {
3032                        rebindForall( $1, $2 );
3033                        $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
3034                }
3035        | declaration_specifier variable_type_redeclarator with_clause_opt compound_statement
3036                {
3037                        rebindForall( $1, $2 );
3038                        $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
3039                }
3040                // handles default int return type, OBSOLESCENT (see 1)
3041        | type_qualifier_list function_declarator with_clause_opt compound_statement
3042                { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
3043                // handles default int return type, OBSOLESCENT (see 1)
3044        | declaration_qualifier_list function_declarator with_clause_opt compound_statement
3045                { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
3046                // handles default int return type, OBSOLESCENT (see 1)
3047        | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
3048                { $$ = $3->addFunctionBody( $5, $4 )->addQualifiers( $2 )->addQualifiers( $1 ); }
3049
3050                // Old-style K&R function definition, OBSOLESCENT (see 4)
3051        | declaration_specifier KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3052                {
3053                        rebindForall( $1, $2 );
3054                        $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addType( $1 );
3055                }
3056                // handles default int return type, OBSOLESCENT (see 1)
3057        | type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3058                { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
3059                // handles default int return type, OBSOLESCENT (see 1)
3060        | declaration_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3061                { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
3062                // handles default int return type, OBSOLESCENT (see 1)
3063        | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3064                { $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6, $5 )->addQualifiers( $2 )->addQualifiers( $1 ); }
3065        ;
3066
3067declarator:
3068        variable_declarator
3069        | variable_type_redeclarator
3070        | function_declarator
3071        ;
3072
3073subrange:
3074        constant_expression '~' constant_expression                     // CFA, integer subrange
3075                { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
3076        ;
3077
3078asm_name_opt:                                                                                   // GCC
3079        // empty
3080                { $$ = nullptr; }
3081        | ASM '(' string_literal ')' attribute_list_opt
3082                {
3083                        DeclarationNode * name = new DeclarationNode();
3084                        name->asmName = $3;
3085                        $$ = name->addQualifiers( $5 );
3086                }
3087        ;
3088
3089attribute_list_opt:                                                                             // GCC
3090        // empty
3091                { $$ = nullptr; }
3092        | attribute_list
3093        ;
3094
3095attribute_list:                                                                                 // GCC
3096        attribute
3097        | attribute_list attribute
3098                { $$ = $2->addQualifiers( $1 ); }
3099        ;
3100
3101attribute:                                                                                              // GCC
3102        ATTRIBUTE '(' '(' attribute_name_list ')' ')'
3103                { $$ = $4; }
3104        ;
3105
3106attribute_name_list:                                                                    // GCC
3107        attribute_name
3108        | attribute_name_list ',' attribute_name
3109                { $$ = $3->addQualifiers( $1 ); }
3110        ;
3111
3112attribute_name:                                                                                 // GCC
3113        // empty
3114                { $$ = nullptr; }
3115        | attr_name
3116                { $$ = DeclarationNode::newAttribute( $1 ); }
3117        | attr_name '(' argument_expression_list_opt ')'
3118                { $$ = DeclarationNode::newAttribute( $1, $3 ); }
3119        ;
3120
3121attr_name:                                                                                              // GCC
3122        IDENTIFIER
3123        | quasi_keyword
3124        | TYPEDEFname
3125        | TYPEGENname
3126        | FALLTHROUGH
3127                { $$ = Token{ new string( "fallthrough" ), { nullptr, -1 } }; }
3128        | CONST
3129                { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
3130        ;
3131
3132// ============================================================================
3133// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
3134// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
3135// in:
3136//
3137//              int (*f())[10] { ... };
3138//              ... (*f())[3] += 1;             // definition mimics usage
3139//
3140// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
3141// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
3142// ============================================================================
3143
3144// ----------------------------------------------------------------------------
3145// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
3146// to define a variable or function prototype, e.g.:
3147//
3148//              valid declaration               invalid definition
3149//              -----------------               ------------------
3150//              int f;                                  int f {}
3151//              int *f;                                 int *f {}
3152//              int f[10];                              int f[10] {}
3153//              int (*f)(int);                  int (*f)(int) {}
3154//
3155// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
3156// variable_declarator and function_declarator.
3157// ----------------------------------------------------------------------------
3158
3159// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
3160// declaring an array of functions versus a pointer to an array of functions.
3161
3162paren_identifier:
3163        identifier_at
3164                { $$ = DeclarationNode::newName( $1 ); }
3165        | '(' paren_identifier ')'                                                      // redundant parenthesis
3166                { $$ = $2; }
3167        ;
3168
3169variable_declarator:
3170        paren_identifier attribute_list_opt
3171                { $$ = $1->addQualifiers( $2 ); }
3172        | variable_ptr
3173        | variable_array attribute_list_opt
3174                { $$ = $1->addQualifiers( $2 ); }
3175        | variable_function attribute_list_opt
3176                { $$ = $1->addQualifiers( $2 ); }
3177        ;
3178
3179variable_ptr:
3180        ptrref_operator variable_declarator
3181                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3182        | ptrref_operator type_qualifier_list variable_declarator
3183                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3184        | '(' variable_ptr ')' attribute_list_opt                       // redundant parenthesis
3185                { $$ = $2->addQualifiers( $4 ); }
3186        | '(' attribute_list variable_ptr ')' attribute_list_opt // redundant parenthesis
3187                { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3188        ;
3189
3190variable_array:
3191        paren_identifier array_dimension
3192                { $$ = $1->addArray( $2 ); }
3193        | '(' variable_ptr ')' array_dimension
3194                { $$ = $2->addArray( $4 ); }
3195        | '(' attribute_list variable_ptr ')' array_dimension
3196                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3197        | '(' variable_array ')' multi_array_dimension          // redundant parenthesis
3198                { $$ = $2->addArray( $4 ); }
3199        | '(' attribute_list variable_array ')' multi_array_dimension // redundant parenthesis
3200                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3201        | '(' variable_array ')'                                                        // redundant parenthesis
3202                { $$ = $2; }
3203        | '(' attribute_list variable_array ')'                         // redundant parenthesis
3204                { $$ = $3->addQualifiers( $2 ); }
3205        ;
3206
3207variable_function:
3208        '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3209                { $$ = $2->addParamList( $6 ); }
3210        | '(' attribute_list variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3211                { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3212        | '(' variable_function ')'                                                     // redundant parenthesis
3213                { $$ = $2; }
3214        | '(' attribute_list variable_function ')'                      // redundant parenthesis
3215                { $$ = $3->addQualifiers( $2 ); }
3216        ;
3217
3218// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
3219// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
3220// is the same scope.  The pattern precludes returning arrays and functions versus pointers to arrays and functions.
3221
3222function_declarator:
3223        function_no_ptr attribute_list_opt
3224                { $$ = $1->addQualifiers( $2 ); }
3225        | function_ptr
3226        | function_array attribute_list_opt
3227                { $$ = $1->addQualifiers( $2 ); }
3228        ;
3229
3230function_no_ptr:
3231        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3232                { $$ = $1->addParamList( $4 ); }
3233        | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
3234                { $$ = $2->addParamList( $6 ); }
3235        | '(' attribute_list function_ptr ')' '(' push parameter_type_list_opt pop ')'
3236                { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3237        | '(' function_no_ptr ')'                                                       // redundant parenthesis
3238                { $$ = $2; }
3239        | '(' attribute_list function_no_ptr ')'                        // redundant parenthesis
3240                { $$ = $3->addQualifiers( $2 ); }
3241        ;
3242
3243function_ptr:
3244        ptrref_operator function_declarator
3245                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3246        | ptrref_operator type_qualifier_list function_declarator
3247                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3248        | '(' function_ptr ')' attribute_list_opt
3249                { $$ = $2->addQualifiers( $4 ); }
3250        | '(' attribute_list function_ptr ')' attribute_list_opt
3251                { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3252        ;
3253
3254function_array:
3255        '(' function_ptr ')' array_dimension
3256                { $$ = $2->addArray( $4 ); }
3257        | '(' attribute_list function_ptr ')' array_dimension
3258                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3259        | '(' function_array ')' multi_array_dimension          // redundant parenthesis
3260                { $$ = $2->addArray( $4 ); }
3261        | '(' attribute_list function_array ')' multi_array_dimension // redundant parenthesis
3262                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3263        | '(' function_array ')'                                                        // redundant parenthesis
3264                { $$ = $2; }
3265        | '(' attribute_list function_array ')'                         // redundant parenthesis
3266                { $$ = $3->addQualifiers( $2 ); }
3267        ;
3268
3269// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
3270//
3271//   f( a, b, c ) int a, *b, c[]; {}
3272//
3273// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
3274// returning arrays and functions versus pointers to arrays and functions.
3275
3276KR_function_declarator:
3277        KR_function_no_ptr
3278        | KR_function_ptr
3279        | KR_function_array
3280        ;
3281
3282KR_function_no_ptr:
3283        paren_identifier '(' identifier_list ')'                        // function_declarator handles empty parameter
3284                { $$ = $1->addIdList( $3 ); }
3285        | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
3286                { $$ = $2->addParamList( $6 ); }
3287        | '(' attribute_list KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
3288                { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3289        | '(' KR_function_no_ptr ')'                                            // redundant parenthesis
3290                { $$ = $2; }
3291        | '(' attribute_list KR_function_no_ptr ')'                     // redundant parenthesis
3292                { $$ = $3->addQualifiers( $2 ); }
3293        ;
3294
3295KR_function_ptr:
3296        ptrref_operator KR_function_declarator
3297                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3298        | ptrref_operator type_qualifier_list KR_function_declarator
3299                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3300        | '(' KR_function_ptr ')'
3301                { $$ = $2; }
3302        | '(' attribute_list KR_function_ptr ')'
3303                { $$ = $3->addQualifiers( $2 ); }
3304        ;
3305
3306KR_function_array:
3307        '(' KR_function_ptr ')' array_dimension
3308                { $$ = $2->addArray( $4 ); }
3309        | '(' attribute_list KR_function_ptr ')' array_dimension
3310                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3311        | '(' KR_function_array ')' multi_array_dimension       // redundant parenthesis
3312                { $$ = $2->addArray( $4 ); }
3313        | '(' attribute_list KR_function_array ')' multi_array_dimension // redundant parenthesis
3314                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3315        | '(' KR_function_array ')'                                                     // redundant parenthesis
3316                { $$ = $2; }
3317        | '(' attribute_list KR_function_array ')'                      // redundant parenthesis
3318                { $$ = $3->addQualifiers( $2 ); }
3319        ;
3320
3321// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
3322//
3323//              typedef int foo;
3324//              {
3325//                 int foo; // redefine typedef name in new scope
3326//              }
3327//
3328// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3329// and functions versus pointers to arrays and functions.
3330
3331paren_type:
3332        typedef_name
3333                {
3334                        // hide type name in enclosing scope by variable name
3335                        typedefTable.addToEnclosingScope( *$1->name, IDENTIFIER, "ID" );
3336                }
3337        | '(' paren_type ')'
3338                { $$ = $2; }
3339        ;
3340
3341variable_type_redeclarator:
3342        paren_type attribute_list_opt
3343                { $$ = $1->addQualifiers( $2 ); }
3344        | type_ptr
3345        | type_array attribute_list_opt
3346                { $$ = $1->addQualifiers( $2 ); }
3347        | type_function attribute_list_opt
3348                { $$ = $1->addQualifiers( $2 ); }
3349        ;
3350
3351type_ptr:
3352        ptrref_operator variable_type_redeclarator
3353                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3354        | ptrref_operator type_qualifier_list variable_type_redeclarator
3355                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3356        | '(' type_ptr ')' attribute_list_opt                           // redundant parenthesis
3357                { $$ = $2->addQualifiers( $4 ); }
3358        | '(' attribute_list type_ptr ')' attribute_list_opt // redundant parenthesis
3359                { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3360        ;
3361
3362type_array:
3363        paren_type array_dimension
3364                { $$ = $1->addArray( $2 ); }
3365        | '(' type_ptr ')' array_dimension
3366                { $$ = $2->addArray( $4 ); }
3367        | '(' attribute_list type_ptr ')' array_dimension
3368                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3369        | '(' type_array ')' multi_array_dimension                      // redundant parenthesis
3370                { $$ = $2->addArray( $4 ); }
3371        | '(' attribute_list type_array ')' multi_array_dimension // redundant parenthesis
3372                { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3373        | '(' type_array ')'                                                            // redundant parenthesis
3374                { $$ = $2; }
3375        | '(' attribute_list type_array ')'                                     // redundant parenthesis
3376                { $$ = $3->addQualifiers( $2 ); }
3377        ;
3378
3379type_function:
3380        paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3381                { $$ = $1->addParamList( $4 ); }
3382        | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3383                { $$ = $2->addParamList( $6 ); }
3384        | '(' attribute_list type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3385                { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3386        | '(' type_function ')'                                                         // redundant parenthesis
3387                { $$ = $2; }
3388        | '(' attribute_list type_function ')'                          // redundant parenthesis
3389                { $$ = $3->addQualifiers( $2 ); }
3390        ;
3391
3392// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
3393// typedef name and allows the C99 array options, which can only appear in a parameter list.  The pattern precludes
3394// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
3395// pointers to arrays and functions.
3396
3397identifier_parameter_declarator:
3398        paren_identifier attribute_list_opt
3399                { $$ = $1->addQualifiers( $2 ); }
3400        | '&' MUTEX paren_identifier attribute_list_opt
3401                { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
3402        | identifier_parameter_ptr
3403        | identifier_parameter_array attribute_list_opt
3404                { $$ = $1->addQualifiers( $2 ); }
3405        | identifier_parameter_function attribute_list_opt
3406                { $$ = $1->addQualifiers( $2 ); }
3407        ;
3408
3409identifier_parameter_ptr:
3410        ptrref_operator identifier_parameter_declarator
3411                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3412        | ptrref_operator type_qualifier_list identifier_parameter_declarator
3413                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3414        | '(' identifier_parameter_ptr ')' attribute_list_opt // redundant parenthesis
3415                { $$ = $2->addQualifiers( $4 ); }
3416        ;
3417
3418identifier_parameter_array:
3419        paren_identifier array_parameter_dimension
3420                { $$ = $1->addArray( $2 ); }
3421        | '(' identifier_parameter_ptr ')' array_dimension
3422                { $$ = $2->addArray( $4 ); }
3423        | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
3424                { $$ = $2->addArray( $4 ); }
3425        | '(' identifier_parameter_array ')'                            // redundant parenthesis
3426                { $$ = $2; }
3427        ;
3428
3429identifier_parameter_function:
3430        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3431                { $$ = $1->addParamList( $4 ); }
3432        | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3433                { $$ = $2->addParamList( $6 ); }
3434        | '(' identifier_parameter_function ')'                         // redundant parenthesis
3435                { $$ = $2; }
3436        ;
3437
3438// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
3439// e.g.:
3440//
3441//              typedef int foo;
3442//              forall( otype T ) struct foo;
3443//              int f( int foo ); // redefine typedef name in new scope
3444//
3445// and allows the C99 array options, which can only appear in a parameter list.
3446
3447type_parameter_redeclarator:
3448        typedef_name attribute_list_opt
3449                { $$ = $1->addQualifiers( $2 ); }
3450        | '&' MUTEX typedef_name attribute_list_opt
3451                { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
3452        | type_parameter_ptr
3453        | type_parameter_array attribute_list_opt
3454                { $$ = $1->addQualifiers( $2 ); }
3455        | type_parameter_function attribute_list_opt
3456                { $$ = $1->addQualifiers( $2 ); }
3457        ;
3458
3459typedef_name:
3460        TYPEDEFname
3461                { $$ = DeclarationNode::newName( $1 ); }
3462        | TYPEGENname
3463                { $$ = DeclarationNode::newName( $1 ); }
3464        ;
3465
3466type_parameter_ptr:
3467        ptrref_operator type_parameter_redeclarator
3468                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3469        | ptrref_operator type_qualifier_list type_parameter_redeclarator
3470                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3471        | '(' type_parameter_ptr ')' attribute_list_opt         // redundant parenthesis
3472                { $$ = $2->addQualifiers( $4 ); }
3473        ;
3474
3475type_parameter_array:
3476        typedef_name array_parameter_dimension
3477                { $$ = $1->addArray( $2 ); }
3478        | '(' type_parameter_ptr ')' array_parameter_dimension
3479                { $$ = $2->addArray( $4 ); }
3480        ;
3481
3482type_parameter_function:
3483        typedef_name '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3484                { $$ = $1->addParamList( $4 ); }
3485        | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3486                { $$ = $2->addParamList( $6 ); }
3487        ;
3488
3489// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
3490// which the type applies, e.g.:
3491//
3492//              sizeof( int );
3493//              sizeof( int * );
3494//              sizeof( int [10] );
3495//              sizeof( int (*)() );
3496//              sizeof( int () );
3497//
3498// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3499// and functions versus pointers to arrays and functions.
3500
3501abstract_declarator:
3502        abstract_ptr
3503        | abstract_array attribute_list_opt
3504                { $$ = $1->addQualifiers( $2 ); }
3505        | abstract_function attribute_list_opt
3506                { $$ = $1->addQualifiers( $2 ); }
3507        ;
3508
3509abstract_ptr:
3510        ptrref_operator
3511                { $$ = DeclarationNode::newPointer( 0, $1 ); }
3512        | ptrref_operator type_qualifier_list
3513                { $$ = DeclarationNode::newPointer( $2, $1 ); }
3514        | ptrref_operator abstract_declarator
3515                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3516        | ptrref_operator type_qualifier_list abstract_declarator
3517                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3518        | '(' abstract_ptr ')' attribute_list_opt
3519                { $$ = $2->addQualifiers( $4 ); }
3520        ;
3521
3522abstract_array:
3523        array_dimension
3524        | '(' abstract_ptr ')' array_dimension
3525                { $$ = $2->addArray( $4 ); }
3526        | '(' abstract_array ')' multi_array_dimension          // redundant parenthesis
3527                { $$ = $2->addArray( $4 ); }
3528        | '(' abstract_array ')'                                                        // redundant parenthesis
3529                { $$ = $2; }
3530        ;
3531
3532abstract_function:
3533        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
3534                { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
3535        | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3536                { $$ = $2->addParamList( $6 ); }
3537        | '(' abstract_function ')'                                                     // redundant parenthesis
3538                { $$ = $2; }
3539        ;
3540
3541array_dimension:
3542                // Only the first dimension can be empty.
3543        '[' ']'
3544                { $$ = DeclarationNode::newArray( 0, 0, false ); }
3545        | '[' ']' multi_array_dimension
3546                { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
3547        | '[' push assignment_expression pop ',' comma_expression ']'
3548                { $$ = DeclarationNode::newArray( $3, 0, false )->addArray( DeclarationNode::newArray( $6, 0, false ) ); }
3549                // { SemanticError( yylloc, "New array dimension is currently unimplemented." ); $$ = nullptr; }
3550        | multi_array_dimension
3551        ;
3552
3553multi_array_dimension:
3554        '[' push assignment_expression pop ']'
3555                { $$ = DeclarationNode::newArray( $3, 0, false ); }
3556        | '[' push '*' pop ']'                                                          // C99
3557                { $$ = DeclarationNode::newVarArray( 0 ); }
3558        | multi_array_dimension '[' push assignment_expression pop ']'
3559                { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
3560        | multi_array_dimension '[' push '*' pop ']'            // C99
3561                { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
3562        ;
3563
3564// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
3565// identifier to which the type applies, e.g.:
3566//
3567//              int f( int );                   // not handled here
3568//              int f( int * );                 // abstract function-prototype parameter; no parameter name specified
3569//              int f( int (*)() );             // abstract function-prototype parameter; no parameter name specified
3570//              int f( int (int) );             // abstract function-prototype parameter; no parameter name specified
3571//
3572// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3573// and functions versus pointers to arrays and functions. In addition, the pattern handles the
3574// special meaning of parenthesis around a typedef name:
3575//
3576//              ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
3577//              parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
3578//              not as redundant parentheses around the identifier."
3579//
3580// For example:
3581//
3582//              typedef float T;
3583//              int f( int ( T [5] ) );                                 // see abstract_parameter_declarator
3584//              int g( int ( T ( int ) ) );                             // see abstract_parameter_declarator
3585//              int f( int f1( T a[5] ) );                              // see identifier_parameter_declarator
3586//              int g( int g1( T g2( int p ) ) );               // see identifier_parameter_declarator
3587//
3588// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
3589// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
3590// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
3591// functions.
3592
3593abstract_parameter_declarator:
3594        abstract_parameter_ptr
3595        | '&' MUTEX attribute_list_opt
3596                { $$ = DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf )->addQualifiers( $3 ); }
3597        | abstract_parameter_array attribute_list_opt
3598                { $$ = $1->addQualifiers( $2 ); }
3599        | abstract_parameter_function attribute_list_opt
3600                { $$ = $1->addQualifiers( $2 ); }
3601        ;
3602
3603abstract_parameter_ptr:
3604        ptrref_operator
3605                { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
3606        | ptrref_operator type_qualifier_list
3607                { $$ = DeclarationNode::newPointer( $2, $1 ); }
3608        | ptrref_operator abstract_parameter_declarator
3609                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3610        | ptrref_operator type_qualifier_list abstract_parameter_declarator
3611                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3612        | '(' abstract_parameter_ptr ')' attribute_list_opt     // redundant parenthesis
3613                { $$ = $2->addQualifiers( $4 ); }
3614        ;
3615
3616abstract_parameter_array:
3617        array_parameter_dimension
3618        | '(' abstract_parameter_ptr ')' array_parameter_dimension
3619                { $$ = $2->addArray( $4 ); }
3620        | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
3621                { $$ = $2->addArray( $4 ); }
3622        | '(' abstract_parameter_array ')'                                      // redundant parenthesis
3623                { $$ = $2; }
3624        ;
3625
3626abstract_parameter_function:
3627        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
3628                { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
3629        | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3630                { $$ = $2->addParamList( $6 ); }
3631        | '(' abstract_parameter_function ')'                           // redundant parenthesis
3632                { $$ = $2; }
3633        ;
3634
3635array_parameter_dimension:
3636                // Only the first dimension can be empty or have qualifiers.
3637        array_parameter_1st_dimension
3638        | array_parameter_1st_dimension multi_array_dimension
3639                { $$ = $1->addArray( $2 ); }
3640        | multi_array_dimension
3641        ;
3642
3643// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
3644//
3645//              ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
3646//              a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
3647
3648array_parameter_1st_dimension:
3649        '[' ']'
3650                { $$ = DeclarationNode::newArray( 0, 0, false ); }
3651                // multi_array_dimension handles the '[' '*' ']' case
3652        | '[' push type_qualifier_list '*' pop ']'                      // remaining C99
3653                { $$ = DeclarationNode::newVarArray( $3 ); }
3654        | '[' push type_qualifier_list pop ']'
3655                { $$ = DeclarationNode::newArray( 0, $3, false ); }
3656                // multi_array_dimension handles the '[' assignment_expression ']' case
3657        | '[' push type_qualifier_list assignment_expression pop ']'
3658                { $$ = DeclarationNode::newArray( $4, $3, false ); }
3659        | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
3660                { $$ = DeclarationNode::newArray( $5, $4, true ); }
3661        | '[' push type_qualifier_list STATIC assignment_expression pop ']'
3662                { $$ = DeclarationNode::newArray( $5, $3, true ); }
3663        ;
3664
3665// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
3666//
3667//              struct S {
3668//          int;
3669//          int *;
3670//          int [10];
3671//          int (*)();
3672//      };
3673
3674variable_abstract_declarator:
3675        variable_abstract_ptr
3676        | variable_abstract_array attribute_list_opt
3677                { $$ = $1->addQualifiers( $2 ); }
3678        | variable_abstract_function attribute_list_opt
3679                { $$ = $1->addQualifiers( $2 ); }
3680        ;
3681
3682variable_abstract_ptr:
3683        ptrref_operator
3684                { $$ = DeclarationNode::newPointer( 0, $1 ); }
3685        | ptrref_operator type_qualifier_list
3686                { $$ = DeclarationNode::newPointer( $2, $1 ); }
3687        | ptrref_operator variable_abstract_declarator
3688                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3689        | ptrref_operator type_qualifier_list variable_abstract_declarator
3690                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3691        | '(' variable_abstract_ptr ')' attribute_list_opt      // redundant parenthesis
3692                { $$ = $2->addQualifiers( $4 ); }
3693        ;
3694
3695variable_abstract_array:
3696        array_dimension
3697        | '(' variable_abstract_ptr ')' array_dimension
3698                { $$ = $2->addArray( $4 ); }
3699        | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
3700                { $$ = $2->addArray( $4 ); }
3701        | '(' variable_abstract_array ')'                                       // redundant parenthesis
3702                { $$ = $2; }
3703        ;
3704
3705variable_abstract_function:
3706        '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3707                { $$ = $2->addParamList( $6 ); }
3708        | '(' variable_abstract_function ')'                            // redundant parenthesis
3709                { $$ = $2; }
3710        ;
3711
3712// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
3713// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
3714
3715cfa_identifier_parameter_declarator_tuple:                              // CFA
3716        cfa_identifier_parameter_declarator_no_tuple
3717        | cfa_abstract_tuple
3718        | type_qualifier_list cfa_abstract_tuple
3719                { $$ = $2->addQualifiers( $1 ); }
3720        ;
3721
3722cfa_identifier_parameter_declarator_no_tuple:                   // CFA
3723        cfa_identifier_parameter_ptr
3724        | cfa_identifier_parameter_array
3725        ;
3726
3727cfa_identifier_parameter_ptr:                                                   // CFA
3728                // No SUE declaration in parameter list.
3729        ptrref_operator type_specifier_nobody
3730                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3731        | type_qualifier_list ptrref_operator type_specifier_nobody
3732                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3733        | ptrref_operator cfa_abstract_function
3734                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3735        | type_qualifier_list ptrref_operator cfa_abstract_function
3736                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3737        | ptrref_operator cfa_identifier_parameter_declarator_tuple
3738                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3739        | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
3740                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3741        ;
3742
3743cfa_identifier_parameter_array:                                                 // CFA
3744                // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
3745                // shift/reduce conflict with new-style empty (void) function return type.
3746        '[' ']' type_specifier_nobody
3747                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3748        | cfa_array_parameter_1st_dimension type_specifier_nobody
3749                { $$ = $2->addNewArray( $1 ); }
3750        | '[' ']' multi_array_dimension type_specifier_nobody
3751                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3752        | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
3753                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3754        | multi_array_dimension type_specifier_nobody
3755                { $$ = $2->addNewArray( $1 ); }
3756
3757        | '[' ']' cfa_identifier_parameter_ptr
3758                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3759        | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
3760                { $$ = $2->addNewArray( $1 ); }
3761        | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
3762                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3763        | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
3764                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3765        | multi_array_dimension cfa_identifier_parameter_ptr
3766                { $$ = $2->addNewArray( $1 ); }
3767        ;
3768
3769cfa_array_parameter_1st_dimension:
3770        '[' push type_qualifier_list '*' pop ']'                        // remaining C99
3771                { $$ = DeclarationNode::newVarArray( $3 ); }
3772        | '[' push type_qualifier_list assignment_expression pop ']'
3773                { $$ = DeclarationNode::newArray( $4, $3, false ); }
3774        | '[' push declaration_qualifier_list assignment_expression pop ']'
3775                // declaration_qualifier_list must be used because of shift/reduce conflict with
3776                // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
3777                // appear in this context.
3778                { $$ = DeclarationNode::newArray( $4, $3, true ); }
3779        | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
3780                { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
3781        ;
3782
3783// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
3784// identifier to which the type applies, e.g.:
3785//
3786//              [int] f( int );                         // abstract variable parameter; no parameter name specified
3787//              [int] f( [int] (int) );         // abstract function-prototype parameter; no parameter name specified
3788//
3789// These rules need LR(3):
3790//
3791//              cfa_abstract_tuple identifier_or_type_name
3792//              '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
3793//
3794// since a function return type can be syntactically identical to a tuple type:
3795//
3796//              [int, int] t;
3797//              [int, int] f( int );
3798//
3799// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
3800// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
3801// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
3802// duplicated when appearing with cfa_function_specifier.
3803
3804cfa_abstract_declarator_tuple:                                                  // CFA
3805        cfa_abstract_tuple
3806        | type_qualifier_list cfa_abstract_tuple
3807                { $$ = $2->addQualifiers( $1 ); }
3808        | cfa_abstract_declarator_no_tuple
3809        ;
3810
3811cfa_abstract_declarator_no_tuple:                                               // CFA
3812        cfa_abstract_ptr
3813        | cfa_abstract_array
3814        ;
3815
3816cfa_abstract_ptr:                                                                               // CFA
3817        ptrref_operator type_specifier
3818                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3819        | type_qualifier_list ptrref_operator type_specifier
3820                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3821        | ptrref_operator cfa_abstract_function
3822                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3823        | type_qualifier_list ptrref_operator cfa_abstract_function
3824                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3825        | ptrref_operator cfa_abstract_declarator_tuple
3826                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3827        | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
3828                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3829        ;
3830
3831cfa_abstract_array:                                                                             // CFA
3832                // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
3833                // empty (void) function return type.
3834        '[' ']' type_specifier
3835                { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3836        | '[' ']' multi_array_dimension type_specifier
3837                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3838        | multi_array_dimension type_specifier
3839                { $$ = $2->addNewArray( $1 ); }
3840        | '[' ']' cfa_abstract_ptr
3841                { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3842        | '[' ']' multi_array_dimension cfa_abstract_ptr
3843                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3844        | multi_array_dimension cfa_abstract_ptr
3845                { $$ = $2->addNewArray( $1 ); }
3846        ;
3847
3848cfa_abstract_tuple:                                                                             // CFA
3849        '[' push cfa_abstract_parameter_list pop ']'
3850                { $$ = DeclarationNode::newTuple( $3 ); }
3851        | '[' push type_specifier_nobody ELLIPSIS pop ']'
3852                { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
3853        | '[' push type_specifier_nobody ELLIPSIS constant_expression pop ']'
3854                { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
3855        ;
3856
3857cfa_abstract_function:                                                                  // CFA
3858//      '[' ']' '(' cfa_parameter_ellipsis_list_opt ')'
3859//              { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
3860        cfa_abstract_tuple '(' push cfa_parameter_ellipsis_list_opt pop ')'
3861                { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3862        | cfa_function_return '(' push cfa_parameter_ellipsis_list_opt pop ')'
3863                { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3864        ;
3865
3866// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3867//    each declaration, and in the specifier-qualifier list in each structure declaration and type name."
3868//
3869// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3870//    the declaration specifiers in a declaration is an obsolescent feature."
3871//
3872// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3873//    prototype-format parameter type declarators) is an obsolescent feature."
3874//
3875// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3876//    declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
3877
3878//************************* MISCELLANEOUS ********************************
3879
3880comma_opt:                                                                                              // redundant comma
3881        // empty
3882        | ','
3883        ;
3884
3885default_initializer_opt:
3886        // empty
3887                { $$ = nullptr; }
3888        | '=' assignment_expression
3889                { $$ = $2; }
3890        ;
3891
3892%%
3893
3894// ----end of grammar----
3895
3896// Local Variables: //
3897// mode: c++ //
3898// tab-width: 4 //
3899// compile-command: "make install" //
3900// End: //
Note: See TracBrowser for help on using the repository browser.