source: src/Parser/parser.yy @ f8b69da7

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since f8b69da7 was 098f7ff, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Remove unmatched pop for handler_clause in parser

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