source: src/Parser/parser.yy@ 32b018e

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 32b018e was 1b29996, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

lex/parse new tuples

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