source: src/Parser/parser.yy @ bd3d9e4

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 bd3d9e4 was bd3d9e4, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

move constructor call from primary to postfix expression

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