source: src/Parser/parser.yy@ 8ef9c5e7

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

add _Coroutine, _Monitor, _Thread keywords

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