source: src/Parser/parser.yy@ 2b93c5a

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 with_gc
Last change on this file since 2b93c5a was faddbd8, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code, new tuple syntax

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