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

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 2b79a70 was f271bdd, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

first attempt at extended for-crtl, name changes

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