source: src/Parser/parser.yy@ b6fd751

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

Merge branch 'master' into tuples

Conflicts:

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

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