source: src/Parser/parser.yy @ 98a249fb

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 98a249fb was 65d6de4, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add TYPEGENname to typegen_name, and fix shift/reduce in favour of shift

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