source: src/Parser/parser.yy @ 7c17511

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 7c17511 was ca78437, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Updated the CatchStmt? node so that it may handle both termination and resumption.

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