source: src/Parser/parser.yy@ 6e8bd43

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 6e8bd43 was 738e304, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

merge qualifier types and use the one in Type

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