source: src/Parser/parser.yy.new@ fe26fbf

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 fe26fbf was c0aa336, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

third attempt at gcc attributes

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