source: src/Parser/parser.yy@ 3f414ef

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

change StorageClass to bitset, support _Thread_local as separate storage-class

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