source: src/Parser/parser.yy @ eb24cec0

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

make keywords catch, catchResume, finally quasi-keywords, add quasi-keyword recover as synonym for catch and fixup for catchResume

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