source: src/Parser/parser.yy@ cd14861

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors 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 cd14861 was 3b58d91, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

add AST nodes TupleIndexExpr, MemberTupleExpr, MassAssignExpr, and MultipleAssignExpr, modify parser to produce nodes for field tuples, modify UntypedMemberExpr to contain a list of members

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