source: src/Parser/parser.yy@ b87e2b60

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

add new type for ranges and refactor parser code

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