source: src/Parser/parser.yy@ 4e2b9710

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 4e2b9710 was c1c1112, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

fix segment fault when printing syntax error, more refactoring of parser code

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