source: src/Parser/parser.yy@ 77971f6

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new stuck-waitfor-destruct with_gc
Last change on this file since 77971f6 was a1e67dd, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Merge branch 'master' into tuples

Conflicts:

src/Parser/parser.cc
src/Parser/parser.yy

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