source: src/Parser/parser.yy@ 41e16b1

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 41e16b1 was 3d26610, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

more push/pop updates

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