source: src/Parser/parser.yy@ 5189888

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 5189888 was 097e2b0, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

constructor/destructor, more example programs

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