source: src/Parser/parser.yy@ 55a68c3

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 55a68c3 was 307a732, checked in by Andrew Beach <ajbeach@…>, 8 years ago

The exception handling code compilers and translates, but the translation crashes.

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