source: src/Parser/parser.yy @ b680198

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

add _DecimalXX to lexer/parser, but mark as unimplemented

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