source: src/Parser/parser.yy @ 9939dc3

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 9939dc3 was d8454b9, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

add better error message for attributes after "with" clause, commented out print statements that will be removed shortly

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