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

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 3c0ec68 was fdca7c6, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

mark aggregate as generic before parsing aggregate body so generic-type name available in body

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