source: src/Parser/parser.yy@ b63e376

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

change keyword type to otype and context to trait

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