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

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory 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 2acf5fc was 2acf5fc, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

Merge branch 'master' of plg2:software/cfa/cfa-cc

Conflicts:

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

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