source: src/Parser/parser.yy @ 2ac218d

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 2ac218d was 2ac218d, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

add commented out parse rules for new ftype syntax

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