source: src/Parser/parser.yy@ 058f549

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

update error message printing and add column to parsing errors

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