source: src/Parser/parser.yy@ 46fa473

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 stuck-waitfor-destruct with_gc
Last change on this file since 46fa473 was 46fa473, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add typedef to aggregate

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