source: src/Parser/parser.yy@ 27cc24e

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 27cc24e was 409433da, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

support coroutine, monitor, thread as kind of structure

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