source: src/Parser/parser.yy@ 759f05f

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 with_gc
Last change on this file since 759f05f was 6a276a0, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Update parser for new fallthrough semantics

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