source: src/Parser/parser.yy@ db82596

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

redo automake third attempt

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