source: src/Parser/parser.yy @ a3a17ba

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since a3a17ba was 51b1202, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

move constant/string to unary_expression, temporarily separate asm and local label, introduce DesignatorNode?

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