source: src/Parser/parser.yy@ 6e3eaa57

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since 6e3eaa57 was 6e3eaa57, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

fix single parameter _Static_assert

  • Property mode set to 100644
File size: 124.0 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 : Mon Jun 4 21:24:46 2018
13// Update Count : 3488
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 // C11
1334 ;
1335
1336static_assert:
1337 STATICASSERT '(' constant_expression ',' string_literal ')' ';' // C11
1338 { $$ = DeclarationNode::newStaticAssert( $3, $5 ); }
1339 | STATICASSERT '(' constant_expression ')' ';' // CFA
1340 { $$ = DeclarationNode::newStaticAssert( $3, build_constantStr( *new string( "\"\"" ) ) ); }
1341
1342// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
1343// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
1344// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
1345// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
1346// the declaration list (as in Pascal). ANSI C and the new CFA declarations may appear together in the same program
1347// block, but cannot be mixed within a specific declaration.
1348//
1349// CFA C
1350// [10] int x; int x[10]; // array of 10 integers
1351// [10] * char y; char *y[10]; // array of 10 pointers to char
1352
1353cfa_declaration: // CFA
1354 cfa_variable_declaration
1355 | cfa_typedef_declaration
1356 | cfa_function_declaration
1357 | type_declaring_list
1358 | trait_specifier
1359 ;
1360
1361cfa_variable_declaration: // CFA
1362 cfa_variable_specifier initializer_opt
1363 { $$ = $1->addInitializer( $2 ); }
1364 | declaration_qualifier_list cfa_variable_specifier initializer_opt
1365 // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
1366 // them as a type_qualifier cannot appear in that context.
1367 { $$ = $2->addQualifiers( $1 )->addInitializer( $3 ); }
1368 | cfa_variable_declaration pop ',' push identifier_or_type_name initializer_opt
1369 { $$ = $1->appendList( $1->cloneType( $5 )->addInitializer( $6 ) ); }
1370 ;
1371
1372cfa_variable_specifier: // CFA
1373 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1374 // storage-class
1375 cfa_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
1376 { $$ = $1->addName( $2 )->addAsmName( $3 ); }
1377 | cfa_abstract_tuple identifier_or_type_name asm_name_opt
1378 { $$ = $1->addName( $2 )->addAsmName( $3 ); }
1379 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name asm_name_opt
1380 { $$ = $2->addQualifiers( $1 )->addName( $3 )->addAsmName( $4 ); }
1381 ;
1382
1383cfa_function_declaration: // CFA
1384 cfa_function_specifier
1385 | type_qualifier_list cfa_function_specifier
1386 { $$ = $2->addQualifiers( $1 ); }
1387 | declaration_qualifier_list cfa_function_specifier
1388 { $$ = $2->addQualifiers( $1 ); }
1389 | declaration_qualifier_list type_qualifier_list cfa_function_specifier
1390 { $$ = $3->addQualifiers( $1 )->addQualifiers( $2 ); }
1391 | cfa_function_declaration ',' identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')'
1392 {
1393 // Append the return type at the start (left-hand-side) to each identifier in the list.
1394 DeclarationNode * ret = new DeclarationNode;
1395 ret->type = maybeClone( $1->type->base );
1396 $$ = $1->appendList( DeclarationNode::newFunction( $3, ret, $6, nullptr ) );
1397 }
1398 ;
1399
1400cfa_function_specifier: // CFA
1401// '[' ']' identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' // S/R conflict
1402// {
1403// $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, 0, true );
1404// }
1405// '[' ']' identifier '(' push cfa_parameter_ellipsis_list_opt pop ')'
1406// {
1407// typedefTable.setNextIdentifier( *$5 );
1408// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1409// }
1410// | '[' ']' TYPEDEFname '(' push cfa_parameter_ellipsis_list_opt pop ')'
1411// {
1412// typedefTable.setNextIdentifier( *$5 );
1413// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1414// }
1415// | '[' ']' typegen_name
1416 // identifier_or_type_name must be broken apart because of the sequence:
1417 //
1418 // '[' ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
1419 // '[' ']' type_specifier
1420 //
1421 // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1422 // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
1423 cfa_abstract_tuple identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')'
1424 // To obtain LR(1 ), this rule must be factored out from function return type (see cfa_abstract_declarator).
1425 { $$ = DeclarationNode::newFunction( $2, $1, $5, 0 ); }
1426 | cfa_function_return identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')'
1427 { $$ = DeclarationNode::newFunction( $2, $1, $5, 0 ); }
1428 ;
1429
1430cfa_function_return: // CFA
1431 '[' push cfa_parameter_list pop ']'
1432 { $$ = DeclarationNode::newTuple( $3 ); }
1433 | '[' push cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ']'
1434 // To obtain LR(1 ), the last cfa_abstract_parameter_list is added into this flattened rule to lookahead to the ']'.
1435 { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
1436 ;
1437
1438cfa_typedef_declaration: // CFA
1439 TYPEDEF cfa_variable_specifier
1440 {
1441 typedefTable.addToEnclosingScope( *$2->name, TYPEDEFname, "1" );
1442 $$ = $2->addTypedef();
1443 }
1444 | TYPEDEF cfa_function_specifier
1445 {
1446 typedefTable.addToEnclosingScope( *$2->name, TYPEDEFname, "2" );
1447 $$ = $2->addTypedef();
1448 }
1449 | cfa_typedef_declaration pop ',' push no_attr_identifier
1450 {
1451 typedefTable.addToEnclosingScope( *$5, TYPEDEFname, "3" );
1452 $$ = $1->appendList( $1->cloneType( $5 ) );
1453 }
1454 ;
1455
1456// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1457// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
1458
1459typedef_declaration:
1460 TYPEDEF type_specifier declarator
1461 {
1462 typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "4" );
1463 $$ = $3->addType( $2 )->addTypedef();
1464 }
1465 | typedef_declaration pop ',' push declarator
1466 {
1467 typedefTable.addToEnclosingScope( *$5->name, TYPEDEFname, "5" );
1468 $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1469 }
1470 | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
1471 {
1472 typedefTable.addToEnclosingScope( *$4->name, TYPEDEFname, "6" );
1473 $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1474 }
1475 | type_specifier TYPEDEF declarator
1476 {
1477 typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "7" );
1478 $$ = $3->addType( $1 )->addTypedef();
1479 }
1480 | type_specifier TYPEDEF type_qualifier_list declarator
1481 {
1482 typedefTable.addToEnclosingScope( *$4->name, TYPEDEFname, "8" );
1483 $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
1484 }
1485 ;
1486
1487typedef_expression:
1488 // GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
1489 TYPEDEF no_attr_identifier '=' assignment_expression
1490 {
1491 // $$ = DeclarationNode::newName( 0 ); // unimplemented
1492 SemanticError( yylloc, "Typedef expression is currently unimplemented." ); $$ = nullptr;
1493 }
1494 | typedef_expression pop ',' push no_attr_identifier '=' assignment_expression
1495 {
1496 // $$ = DeclarationNode::newName( 0 ); // unimplemented
1497 SemanticError( yylloc, "Typedef expression is currently unimplemented." ); $$ = nullptr;
1498 }
1499 ;
1500
1501//c_declaration:
1502// declaring_list pop ';'
1503// | typedef_declaration pop ';'
1504// | typedef_expression pop ';' // GCC, naming expression type
1505// | sue_declaration_specifier pop ';'
1506// ;
1507//
1508//declaring_list:
1509// // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1510// // storage-class
1511// declarator asm_name_opt initializer_opt
1512// {
1513// typedefTable.addToEnclosingScope( IDENTIFIER );
1514// $$ = ( $2->addType( $1 ))->addAsmName( $3 )->addInitializer( $4 );
1515// }
1516// | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1517// {
1518// typedefTable.addToEnclosingScope( IDENTIFIER );
1519// $$ = $1->appendList( $1->cloneBaseType( $4->addAsmName( $5 )->addInitializer( $6 ) ) );
1520// }
1521// ;
1522
1523c_declaration:
1524 declaration_specifier declaring_list
1525 { $$ = distAttr( $1, $2 ); }
1526 | typedef_declaration
1527 | typedef_expression // GCC, naming expression type
1528 | sue_declaration_specifier
1529 ;
1530
1531declaring_list:
1532 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1533 // storage-class
1534 declarator asm_name_opt initializer_opt
1535 { $$ = $1->addAsmName( $2 )->addInitializer( $3 ); }
1536 | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1537 { $$ = $1->appendList( $4->addQualifiers( $3 )->addAsmName( $5 )->addInitializer( $6 ) ); }
1538 ;
1539
1540declaration_specifier: // type specifier + storage class
1541 basic_declaration_specifier
1542 | sue_declaration_specifier
1543 | type_declaration_specifier
1544 ;
1545
1546declaration_specifier_nobody: // type specifier + storage class - {...}
1547 // Preclude SUE declarations in restricted scopes:
1548 //
1549 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1550 //
1551 // because it is impossible to call f due to name equivalence.
1552 basic_declaration_specifier
1553 | sue_declaration_specifier_nobody
1554 | type_declaration_specifier
1555 ;
1556
1557type_specifier: // type specifier
1558 basic_type_specifier
1559 | sue_type_specifier
1560 | type_type_specifier
1561 ;
1562
1563type_specifier_nobody: // type specifier - {...}
1564 // Preclude SUE declarations in restricted scopes:
1565 //
1566 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1567 //
1568 // because it is impossible to call f due to name equivalence.
1569 basic_type_specifier
1570 | sue_type_specifier_nobody
1571 | type_type_specifier
1572 ;
1573
1574type_qualifier_list_opt: // GCC, used in asm_statement
1575 // empty
1576 { $$ = nullptr; }
1577 | type_qualifier_list
1578 ;
1579
1580type_qualifier_list:
1581 // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
1582 //
1583 // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1584 // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1585 // appeared only once.
1586 type_qualifier
1587 | type_qualifier_list type_qualifier
1588 { $$ = $1->addQualifiers( $2 ); }
1589 ;
1590
1591type_qualifier:
1592 type_qualifier_name
1593 | attribute
1594 ;
1595
1596type_qualifier_name:
1597 CONST
1598 { $$ = DeclarationNode::newTypeQualifier( Type::Const ); }
1599 | RESTRICT
1600 { $$ = DeclarationNode::newTypeQualifier( Type::Restrict ); }
1601 | VOLATILE
1602 { $$ = DeclarationNode::newTypeQualifier( Type::Volatile ); }
1603 | ATOMIC
1604 { $$ = DeclarationNode::newTypeQualifier( Type::Atomic ); }
1605 | forall
1606 ;
1607
1608forall:
1609 FORALL '(' type_parameter_list ')' // CFA
1610 { $$ = DeclarationNode::newForall( $3 ); }
1611 ;
1612
1613declaration_qualifier_list:
1614 storage_class_list
1615 | type_qualifier_list storage_class_list // remaining OBSOLESCENT (see 2 )
1616 { $$ = $1->addQualifiers( $2 ); }
1617 | declaration_qualifier_list type_qualifier_list storage_class_list
1618 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1619 ;
1620
1621storage_class_list:
1622 // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
1623 // only one of each is specified, except for inline, which can appear with the others.
1624 //
1625 // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
1626 // specifiers in a declaration.
1627 storage_class
1628 | storage_class_list storage_class
1629 { $$ = $1->addQualifiers( $2 ); }
1630 ;
1631
1632storage_class:
1633 EXTERN
1634 { $$ = DeclarationNode::newStorageClass( Type::Extern ); }
1635 | STATIC
1636 { $$ = DeclarationNode::newStorageClass( Type::Static ); }
1637 | AUTO
1638 { $$ = DeclarationNode::newStorageClass( Type::Auto ); }
1639 | REGISTER
1640 { $$ = DeclarationNode::newStorageClass( Type::Register ); }
1641 | THREADLOCAL // C11
1642 { $$ = DeclarationNode::newStorageClass( Type::Threadlocal ); }
1643 // Put function specifiers here to simplify parsing rules, but separate them semantically.
1644 | INLINE // C99
1645 { $$ = DeclarationNode::newFuncSpecifier( Type::Inline ); }
1646 | FORTRAN // C99
1647 { $$ = DeclarationNode::newFuncSpecifier( Type::Fortran ); }
1648 | NORETURN // C11
1649 { $$ = DeclarationNode::newFuncSpecifier( Type::Noreturn ); }
1650 ;
1651
1652basic_type_name:
1653 VOID
1654 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1655 | BOOL // C99
1656 { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
1657 | CHAR
1658 { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
1659 | INT
1660 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
1661 | INT128
1662 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int128 ); }
1663 | FLOAT
1664 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
1665 | FLOAT80
1666 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float80 ); }
1667 | FLOAT128
1668 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float128 ); }
1669 | DOUBLE
1670 { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
1671 | COMPLEX // C99
1672 { $$ = DeclarationNode::newComplexType( DeclarationNode::Complex ); }
1673 | IMAGINARY // C99
1674 { $$ = DeclarationNode::newComplexType( DeclarationNode::Imaginary ); }
1675 | SIGNED
1676 { $$ = DeclarationNode::newSignedNess( DeclarationNode::Signed ); }
1677 | UNSIGNED
1678 { $$ = DeclarationNode::newSignedNess( DeclarationNode::Unsigned ); }
1679 | SHORT
1680 { $$ = DeclarationNode::newLength( DeclarationNode::Short ); }
1681 | LONG
1682 { $$ = DeclarationNode::newLength( DeclarationNode::Long ); }
1683 | VALIST // GCC, __builtin_va_list
1684 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Valist ); }
1685 ;
1686
1687basic_declaration_specifier:
1688 // A semantic check is necessary for conflicting storage classes.
1689 basic_type_specifier
1690 | declaration_qualifier_list basic_type_specifier
1691 { $$ = $2->addQualifiers( $1 ); }
1692 | basic_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1693 { $$ = $1->addQualifiers( $2 ); }
1694 | basic_declaration_specifier storage_class type_qualifier_list
1695 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1696 | basic_declaration_specifier storage_class basic_type_specifier
1697 { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
1698 ;
1699
1700basic_type_specifier:
1701 direct_type
1702 // Cannot have type modifiers, e.g., short, long, etc.
1703 | type_qualifier_list_opt indirect_type type_qualifier_list_opt
1704 { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
1705 ;
1706
1707direct_type:
1708 basic_type_name
1709 | type_qualifier_list basic_type_name
1710 { $$ = $2->addQualifiers( $1 ); }
1711 | direct_type type_qualifier
1712 { $$ = $1->addQualifiers( $2 ); }
1713 | direct_type basic_type_name
1714 { $$ = $1->addType( $2 ); }
1715 ;
1716
1717indirect_type:
1718 TYPEOF '(' type ')' // GCC: typeof(x) y;
1719 { $$ = $3; }
1720 | TYPEOF '(' comma_expression ')' // GCC: typeof(a+b) y;
1721 { $$ = DeclarationNode::newTypeof( $3 ); }
1722 | ATTR_TYPEGENname '(' type ')' // CFA: e.g., @type(x) y;
1723 { $$ = DeclarationNode::newAttr( $1, $3 ); }
1724 | ATTR_TYPEGENname '(' comma_expression ')' // CFA: e.g., @type(a+b) y;
1725 { $$ = DeclarationNode::newAttr( $1, $3 ); }
1726 | ZERO_T // CFA
1727 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Zero ); }
1728 | ONE_T // CFA
1729 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::One ); }
1730 ;
1731
1732sue_declaration_specifier: // struct, union, enum + storage class + type specifier
1733 sue_type_specifier
1734 | declaration_qualifier_list sue_type_specifier
1735 { $$ = $2->addQualifiers( $1 ); }
1736 | sue_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1737 { $$ = $1->addQualifiers( $2 ); }
1738 | sue_declaration_specifier storage_class type_qualifier_list
1739 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1740 ;
1741
1742sue_type_specifier: // struct, union, enum + type specifier
1743 elaborated_type
1744 | type_qualifier_list
1745 { if ( $1->type != nullptr && $1->type->forall ) forall = true; } // remember generic type
1746 elaborated_type
1747 { $$ = $3->addQualifiers( $1 ); }
1748 | sue_type_specifier type_qualifier
1749 { $$ = $1->addQualifiers( $2 ); }
1750 ;
1751
1752sue_declaration_specifier_nobody: // struct, union, enum - {...} + storage class + type specifier
1753 sue_type_specifier_nobody
1754 | declaration_qualifier_list sue_type_specifier_nobody
1755 { $$ = $2->addQualifiers( $1 ); }
1756 | sue_declaration_specifier_nobody storage_class // remaining OBSOLESCENT (see 2)
1757 { $$ = $1->addQualifiers( $2 ); }
1758 | sue_declaration_specifier_nobody storage_class type_qualifier_list
1759 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1760 ;
1761
1762sue_type_specifier_nobody: // struct, union, enum - {...} + type specifier
1763 elaborated_type_nobody
1764 | type_qualifier_list elaborated_type_nobody
1765 { $$ = $2->addQualifiers( $1 ); }
1766 | sue_type_specifier_nobody type_qualifier
1767 { $$ = $1->addQualifiers( $2 ); }
1768 ;
1769
1770type_declaration_specifier:
1771 type_type_specifier
1772 | declaration_qualifier_list type_type_specifier
1773 { $$ = $2->addQualifiers( $1 ); }
1774 | type_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1775 { $$ = $1->addQualifiers( $2 ); }
1776 | type_declaration_specifier storage_class type_qualifier_list
1777 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1778 ;
1779
1780type_type_specifier: // typedef types
1781 type_name
1782 | type_qualifier_list type_name
1783 { $$ = $2->addQualifiers( $1 ); }
1784 | type_type_specifier type_qualifier
1785 { $$ = $1->addQualifiers( $2 ); }
1786 ;
1787
1788type_name:
1789 TYPEDEFname
1790 { $$ = DeclarationNode::newFromTypedef( $1 ); }
1791 | '.' TYPEDEFname
1792 { $$ = DeclarationNode::newFromTypedef( $2 ); } // FIX ME
1793 | type_name '.' TYPEDEFname
1794 { $$ = DeclarationNode::newFromTypedef( $3 ); } // FIX ME
1795 | typegen_name
1796 | '.' typegen_name
1797 { $$ = $2; } // FIX ME
1798 | type_name '.' typegen_name
1799 { $$ = $3; } // FIX ME
1800 ;
1801
1802typegen_name: // CFA
1803 TYPEGENname
1804 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
1805 | TYPEGENname '(' ')'
1806 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
1807 | TYPEGENname '(' type_list ')'
1808 { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
1809 ;
1810
1811elaborated_type: // struct, union, enum
1812 aggregate_type
1813 | enum_type
1814 ;
1815
1816elaborated_type_nobody: // struct, union, enum - {...}
1817 aggregate_type_nobody
1818 | enum_type_nobody
1819 ;
1820
1821aggregate_type: // struct, union
1822 aggregate_key attribute_list_opt '{' field_declaration_list_opt '}'
1823 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), nullptr, $4, true )->addQualifiers( $2 ); }
1824 | aggregate_key attribute_list_opt no_attr_identifier_or_type_name
1825 {
1826 typedefTable.makeTypedef( *$3 ); // create typedef
1827 if ( forall ) typedefTable.changeKind( *$3, TYPEGENname ); // possibly update
1828 forall = false; // reset
1829 }
1830 '{' field_declaration_list_opt '}'
1831 { $$ = DeclarationNode::newAggregate( $1, $3, nullptr, $6, true )->addQualifiers( $2 ); }
1832 | aggregate_key attribute_list_opt '(' type_list ')' '{' field_declaration_list_opt '}' // CFA
1833 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), $4, $7, false )->addQualifiers( $2 ); }
1834 | aggregate_type_nobody
1835 ;
1836
1837aggregate_type_nobody: // struct, union - {...}
1838 aggregate_key attribute_list_opt no_attr_identifier
1839 {
1840 typedefTable.makeTypedef( *$3 );
1841 if ( forall ) typedefTable.changeKind( *$3, TYPEGENname ); // possibly update
1842 forall = false; // reset
1843 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1844 }
1845 | aggregate_key attribute_list_opt TYPEDEFname
1846 {
1847 typedefTable.makeTypedef( *$3 );
1848 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1849 }
1850 | aggregate_key attribute_list_opt typegen_name // CFA
1851 {
1852 // Create new generic declaration with same name as previous forward declaration, where the IDENTIFIER is
1853 // switched to a TYPEGENname. Link any generic arguments from typegen_name to new generic declaration and
1854 // delete newFromTypeGen.
1855 $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $3->type->symbolic.actuals, nullptr, false )->addQualifiers( $2 );
1856 $3->type->symbolic.name = nullptr;
1857 $3->type->symbolic.actuals = nullptr;
1858 delete $3;
1859 }
1860 ;
1861
1862aggregate_key:
1863 STRUCT
1864 { $$ = DeclarationNode::Struct; }
1865 | UNION
1866 { $$ = DeclarationNode::Union; }
1867 | EXCEPTION
1868 { $$ = DeclarationNode::Exception; }
1869 | COROUTINE
1870 { $$ = DeclarationNode::Coroutine; }
1871 | MONITOR
1872 { $$ = DeclarationNode::Monitor; }
1873 | THREAD
1874 { $$ = DeclarationNode::Thread; }
1875 ;
1876
1877field_declaration_list_opt:
1878 // empty
1879 { $$ = nullptr; }
1880 | field_declaration_list_opt field_declaration
1881 { $$ = $1 ? $1->appendList( $2 ) : $2; }
1882 ;
1883
1884field_declaration:
1885 type_specifier field_declaring_list ';'
1886 { $$ = distAttr( $1, $2 ); }
1887 | EXTENSION type_specifier field_declaring_list ';' // GCC
1888 { distExt( $3 ); $$ = distAttr( $2, $3 ); } // mark all fields in list
1889 | typedef_declaration ';' // CFA
1890 { SemanticError( yylloc, "Typedef in aggregate is currently unimplemented." ); $$ = nullptr; }
1891 | cfa_field_declaring_list ';' // CFA, new style field declaration
1892 | EXTENSION cfa_field_declaring_list ';' // GCC
1893 { distExt( $2 ); $$ = $2; } // mark all fields in list
1894 | cfa_typedef_declaration ';' // CFA
1895 { SemanticError( yylloc, "Typedef in aggregate is currently unimplemented." ); $$ = nullptr; }
1896 | static_assert // C11
1897 ;
1898
1899cfa_field_declaring_list: // CFA, new style field declaration
1900 cfa_abstract_declarator_tuple // CFA, no field name
1901 | cfa_abstract_declarator_tuple no_attr_identifier_or_type_name
1902 { $$ = $1->addName( $2 ); }
1903 | cfa_field_declaring_list ',' no_attr_identifier_or_type_name
1904 { $$ = $1->appendList( $1->cloneType( $3 ) ); }
1905 | cfa_field_declaring_list ',' // CFA, no field name
1906 { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1907 ;
1908
1909field_declaring_list:
1910 field_declarator_opt
1911 | field_declaring_list ',' attribute_list_opt field_declarator_opt
1912 { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
1913 ;
1914
1915field_declarator_opt:
1916 // empty
1917 { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
1918 // '@'
1919 // { $$ = DeclarationNode::newName( new string( DeclarationNode::anonymous.newName() ) ); } // CFA, no field name
1920 | bit_subrange_size // no field name
1921 { $$ = DeclarationNode::newBitfield( $1 ); }
1922 | variable_declarator bit_subrange_size_opt
1923 // A semantic check is required to ensure bit_subrange only appears on base type int.
1924 { $$ = $1->addBitfield( $2 ); }
1925 | variable_type_redeclarator bit_subrange_size_opt
1926 // A semantic check is required to ensure bit_subrange only appears on base type int.
1927 { $$ = $1->addBitfield( $2 ); }
1928 | variable_abstract_declarator // CFA, no field name
1929 ;
1930
1931bit_subrange_size_opt:
1932 // empty
1933 { $$ = nullptr; }
1934 | bit_subrange_size
1935 ;
1936
1937bit_subrange_size:
1938 ':' constant_expression
1939 { $$ = $2; }
1940 ;
1941
1942enum_type: // enum
1943 ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
1944 { $$ = DeclarationNode::newEnum( new string( DeclarationNode::anonymous.newName() ), $4, true )->addQualifiers( $2 ); }
1945 | ENUM attribute_list_opt no_attr_identifier_or_type_name
1946 { typedefTable.makeTypedef( *$3 ); }
1947 '{' enumerator_list comma_opt '}'
1948 { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
1949 | enum_type_nobody
1950 ;
1951
1952enum_type_nobody: // enum - {...}
1953 ENUM attribute_list_opt no_attr_identifier_or_type_name
1954 {
1955 typedefTable.makeTypedef( *$3 );
1956 $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 );
1957 }
1958 ;
1959
1960enumerator_list:
1961 no_attr_identifier_or_type_name enumerator_value_opt
1962 { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
1963 | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
1964 { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1965 ;
1966
1967enumerator_value_opt:
1968 // empty
1969 { $$ = nullptr; }
1970 | '=' constant_expression
1971 { $$ = $2; }
1972 ;
1973
1974cfa_parameter_ellipsis_list_opt: // CFA, abstract + real
1975 // empty
1976 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1977 | ELLIPSIS
1978 { $$ = nullptr; }
1979 | cfa_abstract_parameter_list
1980 | cfa_parameter_list
1981 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
1982 { $$ = $1->appendList( $5 ); }
1983 | cfa_abstract_parameter_list pop ',' push ELLIPSIS
1984 { $$ = $1->addVarArgs(); }
1985 | cfa_parameter_list pop ',' push ELLIPSIS
1986 { $$ = $1->addVarArgs(); }
1987 ;
1988
1989cfa_parameter_list: // CFA
1990 // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
1991 // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
1992 cfa_parameter_declaration
1993 | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
1994 { $$ = $1->appendList( $5 ); }
1995 | cfa_parameter_list pop ',' push cfa_parameter_declaration
1996 { $$ = $1->appendList( $5 ); }
1997 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
1998 { $$ = $1->appendList( $5 )->appendList( $9 ); }
1999 ;
2000
2001cfa_abstract_parameter_list: // CFA, new & old style abstract
2002 cfa_abstract_parameter_declaration
2003 | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
2004 { $$ = $1->appendList( $5 ); }
2005 ;
2006
2007parameter_type_list_opt:
2008 // empty
2009 { $$ = nullptr; }
2010 | ELLIPSIS
2011 { $$ = nullptr; }
2012 | parameter_list
2013 | parameter_list pop ',' push ELLIPSIS
2014 { $$ = $1->addVarArgs(); }
2015 ;
2016
2017parameter_list: // abstract + real
2018 abstract_parameter_declaration
2019 | parameter_declaration
2020 | parameter_list pop ',' push abstract_parameter_declaration
2021 { $$ = $1->appendList( $5 ); }
2022 | parameter_list pop ',' push parameter_declaration
2023 { $$ = $1->appendList( $5 ); }
2024 ;
2025
2026// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
2027// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
2028
2029cfa_parameter_declaration: // CFA, new & old style parameter declaration
2030 parameter_declaration
2031 | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initialize_opt
2032 { $$ = $1->addName( $2 ); }
2033 | cfa_abstract_tuple identifier_or_type_name default_initialize_opt
2034 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2035 { $$ = $1->addName( $2 ); }
2036 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initialize_opt
2037 { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
2038 | cfa_function_specifier
2039 ;
2040
2041cfa_abstract_parameter_declaration: // CFA, new & old style parameter declaration
2042 abstract_parameter_declaration
2043 | cfa_identifier_parameter_declarator_no_tuple
2044 | cfa_abstract_tuple
2045 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2046 | type_qualifier_list cfa_abstract_tuple
2047 { $$ = $2->addQualifiers( $1 ); }
2048 | cfa_abstract_function
2049 ;
2050
2051parameter_declaration:
2052 // No SUE declaration in parameter list.
2053 declaration_specifier_nobody identifier_parameter_declarator default_initialize_opt
2054 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2055 | declaration_specifier_nobody type_parameter_redeclarator default_initialize_opt
2056 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2057 ;
2058
2059abstract_parameter_declaration:
2060 declaration_specifier_nobody default_initialize_opt
2061 { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
2062 | declaration_specifier_nobody abstract_parameter_declarator default_initialize_opt
2063 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2064 ;
2065
2066// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
2067// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
2068// identifiers. The ANSI-style parameter-list can redefine a typedef name.
2069
2070identifier_list: // K&R-style parameter list => no types
2071 no_attr_identifier
2072 { $$ = DeclarationNode::newName( $1 ); }
2073 | identifier_list ',' no_attr_identifier
2074 { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
2075 ;
2076
2077identifier_or_type_name:
2078 identifier
2079 | TYPEDEFname
2080 | TYPEGENname
2081 ;
2082
2083no_attr_identifier_or_type_name:
2084 no_attr_identifier
2085 | TYPEDEFname
2086 | TYPEGENname
2087 ;
2088
2089type_no_function: // sizeof, alignof, cast (constructor)
2090 cfa_abstract_declarator_tuple // CFA
2091 | type_specifier
2092 | type_specifier abstract_declarator
2093 { $$ = $2->addType( $1 ); }
2094 ;
2095
2096type: // typeof, assertion
2097 type_no_function
2098 | cfa_abstract_function // CFA
2099 ;
2100
2101initializer_opt:
2102 // empty
2103 { $$ = nullptr; }
2104 | '=' initializer
2105 { $$ = $2; }
2106 | '=' VOID
2107 { $$ = new InitializerNode( true ); }
2108 | ATassign initializer
2109 { $$ = $2->set_maybeConstructed( false ); }
2110 ;
2111
2112initializer:
2113 assignment_expression { $$ = new InitializerNode( $1 ); }
2114 | '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $2, true ); }
2115 ;
2116
2117initializer_list_opt:
2118 // empty
2119 { $$ = nullptr; }
2120 | initializer
2121 | designation initializer { $$ = $2->set_designators( $1 ); }
2122 | initializer_list_opt ',' initializer { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
2123 | initializer_list_opt ',' designation initializer
2124 { $$ = (InitializerNode *)( $1->set_last( $4->set_designators( $3 ) ) ); }
2125 ;
2126
2127// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
2128// '=' to separator the designator from the initializer value, as in:
2129//
2130// int x[10] = { [1] = 3 };
2131//
2132// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this case, CFA
2133// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
2134// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
2135// shift/reduce conflicts
2136
2137designation:
2138 designator_list ':' // C99, CFA uses ":" instead of "="
2139 | no_attr_identifier ':' // GCC, field name
2140 { $$ = new ExpressionNode( build_varref( $1 ) ); }
2141 ;
2142
2143designator_list: // C99
2144 designator
2145 | designator_list designator
2146 { $$ = (ExpressionNode *)( $1->set_last( $2 ) ); }
2147 //| designator_list designator { $$ = new ExpressionNode( $1, $2 ); }
2148 ;
2149
2150designator:
2151 '.' no_attr_identifier // C99, field name
2152 { $$ = new ExpressionNode( build_varref( $2 ) ); }
2153 | '[' push assignment_expression pop ']' // C99, single array element
2154 // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
2155 { $$ = $3; }
2156 | '[' push subrange pop ']' // CFA, multiple array elements
2157 { $$ = $3; }
2158 | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
2159 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $3 ), maybeMoveBuild< Expression >( $5 ) ) ); }
2160 | '.' '[' push field_list pop ']' // CFA, tuple field selector
2161 { $$ = $4; }
2162 ;
2163
2164// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
2165// rather than an object-oriented type system. This required four groups of extensions:
2166//
2167// Overloading: function, data, and operator identifiers may be overloaded.
2168//
2169// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
2170// and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
2171// definitions of new types. Type declarations with storage class "extern" provide opaque types.
2172//
2173// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
2174// site. A polymorphic function is not a template; it is a function, with an address and a type.
2175//
2176// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
2177// types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
2178// hierarchies. Unlike classes, they can define relationships between types. Assertions declare that a type or
2179// types provide the operations declared by a specification. Assertions are normally used to declare requirements
2180// on type arguments of polymorphic functions.
2181
2182type_parameter_list: // CFA
2183 type_parameter
2184 | type_parameter_list ',' type_parameter
2185 { $$ = $1->appendList( $3 ); }
2186 ;
2187
2188type_initializer_opt: // CFA
2189 // empty
2190 { $$ = nullptr; }
2191 | '=' type
2192 { $$ = $2; }
2193 ;
2194
2195type_parameter: // CFA
2196 type_class no_attr_identifier_or_type_name
2197 { typedefTable.addToScope( *$2, TYPEDEFname, "9" ); }
2198 type_initializer_opt assertion_list_opt
2199 { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2200 | type_specifier identifier_parameter_declarator
2201 | assertion_list
2202 { $$ = DeclarationNode::newTypeParam( DeclarationNode::Dtype, new string( DeclarationNode::anonymous.newName() ) )->addAssertions( $1 ); }
2203 ;
2204
2205type_class: // CFA
2206 OTYPE
2207 { $$ = DeclarationNode::Otype; }
2208 | DTYPE
2209 { $$ = DeclarationNode::Dtype; }
2210 | FTYPE
2211 { $$ = DeclarationNode::Ftype; }
2212 | TTYPE
2213 { $$ = DeclarationNode::Ttype; }
2214 ;
2215
2216assertion_list_opt: // CFA
2217 // empty
2218 { $$ = nullptr; }
2219 | assertion_list
2220 ;
2221
2222assertion_list: // CFA
2223 assertion
2224 | assertion_list assertion
2225 { $$ = $1 ? $1->appendList( $2 ) : $2; }
2226 ;
2227
2228assertion: // CFA
2229 '|' no_attr_identifier_or_type_name '(' type_list ')'
2230 { $$ = DeclarationNode::newTraitUse( $2, $4 ); }
2231 | '|' '{' push trait_declaration_list pop '}'
2232 { $$ = $4; }
2233 // | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list pop '}' '(' type_list ')'
2234 // { SemanticError( yylloc, "Generic data-type assertion is currently unimplemented." ); $$ = nullptr; }
2235 ;
2236
2237type_list: // CFA
2238 type
2239 { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( $1 ) ) ); }
2240 | assignment_expression
2241 | type_list ',' type
2242 { $$ = (ExpressionNode *)( $1->set_last( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ) ) ); }
2243 | type_list ',' assignment_expression
2244 { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
2245 ;
2246
2247type_declaring_list: // CFA
2248 OTYPE type_declarator
2249 { $$ = $2; }
2250 | storage_class_list OTYPE type_declarator
2251 { $$ = $3->addQualifiers( $1 ); }
2252 | type_declaring_list ',' type_declarator
2253 { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
2254 ;
2255
2256type_declarator: // CFA
2257 type_declarator_name assertion_list_opt
2258 { $$ = $1->addAssertions( $2 ); }
2259 | type_declarator_name assertion_list_opt '=' type
2260 { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2261 ;
2262
2263type_declarator_name: // CFA
2264 no_attr_identifier_or_type_name
2265 {
2266 typedefTable.addToEnclosingScope( *$1, TYPEDEFname, "10" );
2267 $$ = DeclarationNode::newTypeDecl( $1, 0 );
2268 }
2269 | no_attr_identifier_or_type_name '(' type_parameter_list ')'
2270 {
2271 typedefTable.addToEnclosingScope( *$1, TYPEGENname, "11" );
2272 $$ = DeclarationNode::newTypeDecl( $1, $3 );
2273 }
2274 ;
2275
2276trait_specifier: // CFA
2277 TRAIT no_attr_identifier_or_type_name '(' type_parameter_list ')' '{' '}'
2278 { $$ = DeclarationNode::newTrait( $2, $4, 0 ); }
2279 | TRAIT no_attr_identifier_or_type_name '(' type_parameter_list ')' '{' push trait_declaration_list pop '}'
2280 { $$ = DeclarationNode::newTrait( $2, $4, $8 ); }
2281 ;
2282
2283trait_declaration_list: // CFA
2284 trait_declaration
2285 | trait_declaration_list pop push trait_declaration
2286 { $$ = $1->appendList( $4 ); }
2287 ;
2288
2289trait_declaration: // CFA
2290 cfa_trait_declaring_list ';'
2291 | trait_declaring_list ';'
2292 ;
2293
2294cfa_trait_declaring_list: // CFA
2295 cfa_variable_specifier
2296 | cfa_function_specifier
2297 | cfa_trait_declaring_list pop ',' push identifier_or_type_name
2298 { $$ = $1->appendList( $1->cloneType( $5 ) ); }
2299 ;
2300
2301trait_declaring_list: // CFA
2302 type_specifier declarator
2303 { $$ = $2->addType( $1 ); }
2304 | trait_declaring_list pop ',' push declarator
2305 { $$ = $1->appendList( $1->cloneBaseType( $5 ) ); }
2306 ;
2307
2308//***************************** EXTERNAL DEFINITIONS *****************************
2309
2310translation_unit:
2311 // empty
2312 {} // empty input file
2313 | external_definition_list
2314 { parseTree = parseTree ? parseTree->appendList( $1 ) : $1; }
2315 ;
2316
2317external_definition_list:
2318 push external_definition pop
2319 { $$ = $2; }
2320 | external_definition_list
2321 { forall = xxx; }
2322 push external_definition pop
2323 { $$ = $1 ? $1->appendList( $4 ) : $4; }
2324 ;
2325
2326 // SKULLDUGGERY: Declarations in extern "X" and distribution need to be added to the current lexical scope.
2327 // However, external_definition_list creates a new scope around each external_definition, but the pop loses all the
2328 // types in the extern "X" and distribution at the end of the block. This version of external_definition_list does
2329
2330 // not do push/pop for declarations at the level of the extern "X" and distribution block. Any recursive uses of
2331 // external_definition_list within the extern "X" and distribution block correctly pushes/pops for that scope level.
2332external_definition_list_no_pop_push:
2333 external_definition
2334 | external_definition_list_no_pop_push
2335 { forall = xxx; }
2336 external_definition
2337 { $$ = $1 ? $1->appendList( $3 ) : $3; }
2338 ;
2339
2340external_definition_list_opt:
2341 // empty
2342 { $$ = nullptr; }
2343 | external_definition_list_no_pop_push
2344 ;
2345
2346external_definition:
2347 declaration
2348 | external_function_definition
2349 | EXTENSION external_definition // GCC, multiple __extension__ allowed, meaning unknown
2350 {
2351 distExt( $2 ); // mark all fields in list
2352 $$ = $2;
2353 }
2354 | ASM '(' string_literal ')' ';' // GCC, global assembler statement
2355 {
2356 $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asm( false, $3, 0 ) ) );
2357 }
2358 | EXTERN STRINGliteral // C++-style linkage specifier
2359 {
2360 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
2361 linkage = LinkageSpec::linkageUpdate( yylloc, linkage, $2 );
2362 }
2363 '{' external_definition_list_opt '}'
2364 {
2365 linkage = linkageStack.top();
2366 linkageStack.pop();
2367 $$ = $5;
2368 }
2369 | type_qualifier_list
2370 { if ( $1->type->forall ) xxx = forall = true; } // remember generic type
2371 '{' external_definition_list_opt '}' // CFA, namespace
2372 {
2373 for ( DeclarationNode * iter = $4; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
2374 if ( isMangled( iter->linkage ) ) { // ignore extern "C"
2375 iter->addQualifiers( $1->clone() );
2376 } // if
2377 } // for
2378 xxx = false;
2379 delete $1;
2380 $$ = $4;
2381 }
2382 | declaration_qualifier_list
2383 { if ( $1->type->forall ) xxx = forall = true; } // remember generic type
2384 '{' external_definition_list_opt '}' // CFA, namespace
2385 {
2386 for ( DeclarationNode * iter = $4; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
2387 if ( isMangled( iter->linkage ) ) { // ignore extern "C"
2388 iter->addQualifiers( $1->clone() );
2389 } // if
2390 } // for
2391 xxx = false;
2392 delete $1;
2393 $$ = $4;
2394 }
2395 | declaration_qualifier_list type_qualifier_list
2396 {
2397 // forall must be in the type_qualifier_list
2398 if ( $2->type->forall ) xxx = forall = true; // remember generic type
2399 }
2400 '{' external_definition_list_opt '}' // CFA, namespace
2401 {
2402 for ( DeclarationNode * iter = $5; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
2403 if ( isMangled( iter->linkage ) && isMangled( $2->linkage ) ) { // ignore extern "C"
2404 iter->addQualifiers( $1->clone() );
2405 iter->addQualifiers( $2->clone() );
2406 } // if
2407 } // for
2408 xxx = false;
2409 delete $1;
2410 delete $2;
2411 $$ = $5;
2412 }
2413 ;
2414
2415external_function_definition:
2416 function_definition
2417 // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
2418 // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2419 // Parsing is possible because function_definition does not appear in the context of an expression (nested
2420 // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2421 // declaration must still have a type_specifier. OBSOLESCENT (see 1)
2422 | function_declarator compound_statement
2423 { $$ = $1->addFunctionBody( $2 ); }
2424 | KR_function_declarator KR_parameter_list_opt compound_statement
2425 { $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 ); }
2426 ;
2427
2428with_clause_opt:
2429 // empty
2430 { $$ = nullptr; forall = false; }
2431 | WITH '(' tuple_expression_list ')'
2432 { $$ = $3; forall = false; }
2433 ;
2434
2435function_definition:
2436 cfa_function_declaration with_clause_opt compound_statement // CFA
2437 {
2438 // Add the function body to the last identifier in the function definition list, i.e., foo3:
2439 // [const double] foo1(), foo2( int ), foo3( double ) { return 3.0; }
2440 $1->get_last()->addFunctionBody( $3, $2 );
2441 $$ = $1;
2442 }
2443 | declaration_specifier function_declarator with_clause_opt compound_statement
2444 {
2445 rebindForall( $1, $2 );
2446 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
2447 }
2448 | declaration_specifier variable_type_redeclarator with_clause_opt compound_statement
2449 {
2450 rebindForall( $1, $2 );
2451 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
2452 }
2453 // handles default int return type, OBSOLESCENT (see 1)
2454 | type_qualifier_list function_declarator with_clause_opt compound_statement
2455 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
2456 // handles default int return type, OBSOLESCENT (see 1)
2457 | declaration_qualifier_list function_declarator with_clause_opt compound_statement
2458 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
2459 // handles default int return type, OBSOLESCENT (see 1)
2460 | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
2461 { $$ = $3->addFunctionBody( $5, $4 )->addQualifiers( $2 )->addQualifiers( $1 ); }
2462
2463 // Old-style K&R function definition, OBSOLESCENT (see 4)
2464 | declaration_specifier KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
2465 {
2466 rebindForall( $1, $2 );
2467 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addType( $1 );
2468 }
2469 // handles default int return type, OBSOLESCENT (see 1)
2470 | type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
2471 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
2472 // handles default int return type, OBSOLESCENT (see 1)
2473 | declaration_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
2474 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
2475 // handles default int return type, OBSOLESCENT (see 1)
2476 | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
2477 { $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6, $5 )->addQualifiers( $2 )->addQualifiers( $1 ); }
2478 ;
2479
2480declarator:
2481 variable_declarator
2482 | variable_type_redeclarator
2483 | function_declarator
2484 ;
2485
2486subrange:
2487 constant_expression '~' constant_expression // CFA, integer subrange
2488 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
2489 ;
2490
2491asm_name_opt: // GCC
2492 // empty
2493 { $$ = nullptr; }
2494 | ASM '(' string_literal ')' attribute_list_opt
2495 {
2496 DeclarationNode * name = new DeclarationNode();
2497 name->asmName = $3;
2498 $$ = name->addQualifiers( $5 );
2499 }
2500 ;
2501
2502attribute_list_opt: // GCC
2503 // empty
2504 { $$ = nullptr; }
2505 | attribute_list
2506 ;
2507
2508attribute_list: // GCC
2509 attribute
2510 | attribute_list attribute
2511 { $$ = $2->addQualifiers( $1 ); }
2512 ;
2513
2514attribute: // GCC
2515 ATTRIBUTE '(' '(' attribute_name_list ')' ')'
2516 { $$ = $4; }
2517 ;
2518
2519attribute_name_list: // GCC
2520 attribute_name
2521 | attribute_name_list ',' attribute_name
2522 { $$ = $3->addQualifiers( $1 ); }
2523 ;
2524
2525attribute_name: // GCC
2526 // empty
2527 { $$ = nullptr; }
2528 | attr_name
2529 { $$ = DeclarationNode::newAttribute( $1 ); }
2530 | attr_name '(' argument_expression_list ')'
2531 { $$ = DeclarationNode::newAttribute( $1, $3 ); }
2532 ;
2533
2534attr_name: // GCC
2535 IDENTIFIER
2536 | quasi_keyword
2537 | TYPEDEFname
2538 | TYPEGENname
2539 | FALLTHROUGH
2540 { $$ = Token{ new string( "fallthrough" ), { nullptr, -1 } }; }
2541 | CONST
2542 { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
2543 ;
2544
2545// ============================================================================
2546// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2547// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2548// in:
2549//
2550// int (*f())[10] { ... };
2551// ... (*f())[3] += 1; // definition mimics usage
2552//
2553// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2554// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
2555// ============================================================================
2556
2557// ----------------------------------------------------------------------------
2558// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2559// to define a variable or function prototype, e.g.:
2560//
2561// valid declaration invalid definition
2562// ----------------- ------------------
2563// int f; int f {}
2564// int *f; int *f {}
2565// int f[10]; int f[10] {}
2566// int (*f)(int); int (*f)(int) {}
2567//
2568// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2569// variable_declarator and function_declarator.
2570// ----------------------------------------------------------------------------
2571
2572// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2573// declaring an array of functions versus a pointer to an array of functions.
2574
2575variable_declarator:
2576 paren_identifier attribute_list_opt
2577 { $$ = $1->addQualifiers( $2 ); }
2578 | variable_ptr
2579 | variable_array attribute_list_opt
2580 { $$ = $1->addQualifiers( $2 ); }
2581 | variable_function attribute_list_opt
2582 { $$ = $1->addQualifiers( $2 ); }
2583 ;
2584
2585paren_identifier:
2586 identifier
2587 { $$ = DeclarationNode::newName( $1 ); }
2588 | '(' paren_identifier ')' // redundant parenthesis
2589 { $$ = $2; }
2590 ;
2591
2592variable_ptr:
2593 ptrref_operator variable_declarator
2594 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2595 | ptrref_operator type_qualifier_list variable_declarator
2596 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2597 | '(' variable_ptr ')' attribute_list_opt
2598 { $$ = $2->addQualifiers( $4 ); } // redundant parenthesis
2599 ;
2600
2601variable_array:
2602 paren_identifier array_dimension
2603 { $$ = $1->addArray( $2 ); }
2604 | '(' variable_ptr ')' array_dimension
2605 { $$ = $2->addArray( $4 ); }
2606 | '(' variable_array ')' multi_array_dimension // redundant parenthesis
2607 { $$ = $2->addArray( $4 ); }
2608 | '(' variable_array ')' // redundant parenthesis
2609 { $$ = $2; }
2610 ;
2611
2612variable_function:
2613 '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2614 { $$ = $2->addParamList( $6 ); }
2615 | '(' variable_function ')' // redundant parenthesis
2616 { $$ = $2; }
2617 ;
2618
2619// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2620// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2621// is the same scope. The pattern precludes returning arrays and functions versus pointers to arrays and functions.
2622
2623function_declarator:
2624 function_no_ptr attribute_list_opt
2625 { $$ = $1->addQualifiers( $2 ); }
2626 | function_ptr
2627 | function_array attribute_list_opt
2628 { $$ = $1->addQualifiers( $2 ); }
2629 ;
2630
2631function_no_ptr:
2632 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2633 { $$ = $1->addParamList( $4 ); }
2634 | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2635 { $$ = $2->addParamList( $6 ); }
2636 | '(' function_no_ptr ')' // redundant parenthesis
2637 { $$ = $2; }
2638 ;
2639
2640function_ptr:
2641 ptrref_operator function_declarator
2642 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2643 | ptrref_operator type_qualifier_list function_declarator
2644 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2645 | '(' function_ptr ')'
2646 { $$ = $2; }
2647 ;
2648
2649function_array:
2650 '(' function_ptr ')' array_dimension
2651 { $$ = $2->addArray( $4 ); }
2652 | '(' function_array ')' multi_array_dimension // redundant parenthesis
2653 { $$ = $2->addArray( $4 ); }
2654 | '(' function_array ')' // redundant parenthesis
2655 { $$ = $2; }
2656 ;
2657
2658// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
2659//
2660// f( a, b, c ) int a, *b, c[]; {}
2661//
2662// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
2663// returning arrays and functions versus pointers to arrays and functions.
2664
2665KR_function_declarator:
2666 KR_function_no_ptr
2667 | KR_function_ptr
2668 | KR_function_array
2669 ;
2670
2671KR_function_no_ptr:
2672 paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
2673 { $$ = $1->addIdList( $3 ); }
2674 | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
2675 { $$ = $2->addParamList( $6 ); }
2676 | '(' KR_function_no_ptr ')' // redundant parenthesis
2677 { $$ = $2; }
2678 ;
2679
2680KR_function_ptr:
2681 ptrref_operator KR_function_declarator
2682 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2683 | ptrref_operator type_qualifier_list KR_function_declarator
2684 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2685 | '(' KR_function_ptr ')'
2686 { $$ = $2; }
2687 ;
2688
2689KR_function_array:
2690 '(' KR_function_ptr ')' array_dimension
2691 { $$ = $2->addArray( $4 ); }
2692 | '(' KR_function_array ')' multi_array_dimension // redundant parenthesis
2693 { $$ = $2->addArray( $4 ); }
2694 | '(' KR_function_array ')' // redundant parenthesis
2695 { $$ = $2; }
2696 ;
2697
2698// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
2699//
2700// typedef int foo;
2701// {
2702// int foo; // redefine typedef name in new scope
2703// }
2704//
2705// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2706// and functions versus pointers to arrays and functions.
2707
2708variable_type_redeclarator:
2709 paren_type attribute_list_opt
2710 { $$ = $1->addQualifiers( $2 ); }
2711 | type_ptr
2712 | type_array attribute_list_opt
2713 { $$ = $1->addQualifiers( $2 ); }
2714 | type_function attribute_list_opt
2715 { $$ = $1->addQualifiers( $2 ); }
2716 ;
2717
2718paren_type:
2719 typedef
2720 // hide type name in enclosing scope by variable name
2721 { typedefTable.addToEnclosingScope( *$1->name, IDENTIFIER, "ID" ); }
2722 | '(' paren_type ')'
2723 { $$ = $2; }
2724 ;
2725
2726type_ptr:
2727 ptrref_operator variable_type_redeclarator
2728 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2729 | ptrref_operator type_qualifier_list variable_type_redeclarator
2730 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2731 | '(' type_ptr ')' attribute_list_opt
2732 { $$ = $2->addQualifiers( $4 ); }
2733 ;
2734
2735type_array:
2736 paren_type array_dimension
2737 { $$ = $1->addArray( $2 ); }
2738 | '(' type_ptr ')' array_dimension
2739 { $$ = $2->addArray( $4 ); }
2740 | '(' type_array ')' multi_array_dimension // redundant parenthesis
2741 { $$ = $2->addArray( $4 ); }
2742 | '(' type_array ')' // redundant parenthesis
2743 { $$ = $2; }
2744 ;
2745
2746type_function:
2747 paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2748 { $$ = $1->addParamList( $4 ); }
2749 | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2750 { $$ = $2->addParamList( $6 ); }
2751 | '(' type_function ')' // redundant parenthesis
2752 { $$ = $2; }
2753 ;
2754
2755// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
2756// typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes
2757// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
2758// pointers to arrays and functions.
2759
2760identifier_parameter_declarator:
2761 paren_identifier attribute_list_opt
2762 { $$ = $1->addQualifiers( $2 ); }
2763 | '&' MUTEX paren_identifier attribute_list_opt
2764 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
2765 | identifier_parameter_ptr
2766 | identifier_parameter_array attribute_list_opt
2767 { $$ = $1->addQualifiers( $2 ); }
2768 | identifier_parameter_function attribute_list_opt
2769 { $$ = $1->addQualifiers( $2 ); }
2770 ;
2771
2772identifier_parameter_ptr:
2773 ptrref_operator identifier_parameter_declarator
2774 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2775 | ptrref_operator type_qualifier_list identifier_parameter_declarator
2776 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2777 | '(' identifier_parameter_ptr ')' attribute_list_opt
2778 { $$ = $2->addQualifiers( $4 ); }
2779 ;
2780
2781identifier_parameter_array:
2782 paren_identifier array_parameter_dimension
2783 { $$ = $1->addArray( $2 ); }
2784 | '(' identifier_parameter_ptr ')' array_dimension
2785 { $$ = $2->addArray( $4 ); }
2786 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2787 { $$ = $2->addArray( $4 ); }
2788 | '(' identifier_parameter_array ')' // redundant parenthesis
2789 { $$ = $2; }
2790 ;
2791
2792identifier_parameter_function:
2793 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2794 { $$ = $1->addParamList( $4 ); }
2795 | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2796 { $$ = $2->addParamList( $6 ); }
2797 | '(' identifier_parameter_function ')' // redundant parenthesis
2798 { $$ = $2; }
2799 ;
2800
2801// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2802// e.g.:
2803//
2804// typedef int foo;
2805// forall( otype T ) struct foo;
2806// int f( int foo ); // redefine typedef name in new scope
2807//
2808// and allows the C99 array options, which can only appear in a parameter list.
2809
2810type_parameter_redeclarator:
2811 typedef attribute_list_opt
2812 { $$ = $1->addQualifiers( $2 ); }
2813 | '&' MUTEX typedef attribute_list_opt
2814 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
2815 | type_parameter_ptr
2816 | type_parameter_array attribute_list_opt
2817 { $$ = $1->addQualifiers( $2 ); }
2818 | type_parameter_function attribute_list_opt
2819 { $$ = $1->addQualifiers( $2 ); }
2820 ;
2821
2822typedef:
2823 TYPEDEFname
2824 { $$ = DeclarationNode::newName( $1 ); }
2825 | TYPEGENname
2826 { $$ = DeclarationNode::newName( $1 ); }
2827 ;
2828
2829type_parameter_ptr:
2830 ptrref_operator type_parameter_redeclarator
2831 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2832 | ptrref_operator type_qualifier_list type_parameter_redeclarator
2833 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2834 | '(' type_parameter_ptr ')' attribute_list_opt
2835 { $$ = $2->addQualifiers( $4 ); }
2836 ;
2837
2838type_parameter_array:
2839 typedef array_parameter_dimension
2840 { $$ = $1->addArray( $2 ); }
2841 | '(' type_parameter_ptr ')' array_parameter_dimension
2842 { $$ = $2->addArray( $4 ); }
2843 ;
2844
2845type_parameter_function:
2846 typedef '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2847 { $$ = $1->addParamList( $4 ); }
2848 | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2849 { $$ = $2->addParamList( $6 ); }
2850 ;
2851
2852// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2853// which the type applies, e.g.:
2854//
2855// sizeof( int );
2856// sizeof( int * );
2857// sizeof( int [10] );
2858// sizeof( int (*)() );
2859// sizeof( int () );
2860//
2861// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2862// and functions versus pointers to arrays and functions.
2863
2864abstract_declarator:
2865 abstract_ptr
2866 | abstract_array attribute_list_opt
2867 { $$ = $1->addQualifiers( $2 ); }
2868 | abstract_function attribute_list_opt
2869 { $$ = $1->addQualifiers( $2 ); }
2870 ;
2871
2872abstract_ptr:
2873 ptrref_operator
2874 { $$ = DeclarationNode::newPointer( 0, $1 ); }
2875 | ptrref_operator type_qualifier_list
2876 { $$ = DeclarationNode::newPointer( $2, $1 ); }
2877 | ptrref_operator abstract_declarator
2878 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2879 | ptrref_operator type_qualifier_list abstract_declarator
2880 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2881 | '(' abstract_ptr ')' attribute_list_opt
2882 { $$ = $2->addQualifiers( $4 ); }
2883 ;
2884
2885abstract_array:
2886 array_dimension
2887 | '(' abstract_ptr ')' array_dimension
2888 { $$ = $2->addArray( $4 ); }
2889 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
2890 { $$ = $2->addArray( $4 ); }
2891 | '(' abstract_array ')' // redundant parenthesis
2892 { $$ = $2; }
2893 ;
2894
2895abstract_function:
2896 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2897 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
2898 | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2899 { $$ = $2->addParamList( $6 ); }
2900 | '(' abstract_function ')' // redundant parenthesis
2901 { $$ = $2; }
2902 ;
2903
2904array_dimension:
2905 // Only the first dimension can be empty.
2906 '[' ']'
2907 { $$ = DeclarationNode::newArray( 0, 0, false ); }
2908 | '[' ']' multi_array_dimension
2909 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
2910 | multi_array_dimension
2911 ;
2912
2913multi_array_dimension:
2914 '[' push assignment_expression pop ']'
2915 { $$ = DeclarationNode::newArray( $3, 0, false ); }
2916 | '[' push '*' pop ']' // C99
2917 { $$ = DeclarationNode::newVarArray( 0 ); }
2918 | multi_array_dimension '[' push assignment_expression pop ']'
2919 { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2920 | multi_array_dimension '[' push '*' pop ']' // C99
2921 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2922 ;
2923
2924// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2925// identifier to which the type applies, e.g.:
2926//
2927// int f( int ); // not handled here
2928// int f( int * ); // abstract function-prototype parameter; no parameter name specified
2929// int f( int (*)() ); // abstract function-prototype parameter; no parameter name specified
2930// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
2931//
2932// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2933// and functions versus pointers to arrays and functions. In addition, the pattern handles the
2934// special meaning of parenthesis around a typedef name:
2935//
2936// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2937// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2938// not as redundant parentheses around the identifier."
2939//
2940// For example:
2941//
2942// typedef float T;
2943// int f( int ( T [5] ) ); // see abstract_parameter_declarator
2944// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
2945// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
2946// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
2947//
2948// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2949// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2950// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2951// functions.
2952
2953abstract_parameter_declarator:
2954 abstract_parameter_ptr
2955 | '&' MUTEX attribute_list_opt
2956 { $$ = DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf )->addQualifiers( $3 ); }
2957 | abstract_parameter_array attribute_list_opt
2958 { $$ = $1->addQualifiers( $2 ); }
2959 | abstract_parameter_function attribute_list_opt
2960 { $$ = $1->addQualifiers( $2 ); }
2961 ;
2962
2963abstract_parameter_ptr:
2964 ptrref_operator
2965 { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
2966 | ptrref_operator type_qualifier_list
2967 { $$ = DeclarationNode::newPointer( $2, $1 ); }
2968 | ptrref_operator abstract_parameter_declarator
2969 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
2970 | ptrref_operator type_qualifier_list abstract_parameter_declarator
2971 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2972 | '(' abstract_parameter_ptr ')' attribute_list_opt
2973 { $$ = $2->addQualifiers( $4 ); }
2974 ;
2975
2976abstract_parameter_array:
2977 array_parameter_dimension
2978 | '(' abstract_parameter_ptr ')' array_parameter_dimension
2979 { $$ = $2->addArray( $4 ); }
2980 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2981 { $$ = $2->addArray( $4 ); }
2982 | '(' abstract_parameter_array ')' // redundant parenthesis
2983 { $$ = $2; }
2984 ;
2985
2986abstract_parameter_function:
2987 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2988 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
2989 | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2990 { $$ = $2->addParamList( $6 ); }
2991 | '(' abstract_parameter_function ')' // redundant parenthesis
2992 { $$ = $2; }
2993 ;
2994
2995array_parameter_dimension:
2996 // Only the first dimension can be empty or have qualifiers.
2997 array_parameter_1st_dimension
2998 | array_parameter_1st_dimension multi_array_dimension
2999 { $$ = $1->addArray( $2 ); }
3000 | multi_array_dimension
3001 ;
3002
3003// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
3004//
3005// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
3006// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
3007
3008array_parameter_1st_dimension:
3009 '[' ']'
3010 { $$ = DeclarationNode::newArray( 0, 0, false ); }
3011 // multi_array_dimension handles the '[' '*' ']' case
3012 | '[' push type_qualifier_list '*' pop ']' // remaining C99
3013 { $$ = DeclarationNode::newVarArray( $3 ); }
3014 | '[' push type_qualifier_list pop ']'
3015 { $$ = DeclarationNode::newArray( 0, $3, false ); }
3016 // multi_array_dimension handles the '[' assignment_expression ']' case
3017 | '[' push type_qualifier_list assignment_expression pop ']'
3018 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3019 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
3020 { $$ = DeclarationNode::newArray( $5, $4, true ); }
3021 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
3022 { $$ = DeclarationNode::newArray( $5, $3, true ); }
3023 ;
3024
3025// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
3026//
3027// struct S {
3028// int;
3029// int *;
3030// int [10];
3031// int (*)();
3032// };
3033
3034variable_abstract_declarator:
3035 variable_abstract_ptr
3036 | variable_abstract_array attribute_list_opt
3037 { $$ = $1->addQualifiers( $2 ); }
3038 | variable_abstract_function attribute_list_opt
3039 { $$ = $1->addQualifiers( $2 ); }
3040 ;
3041
3042variable_abstract_ptr:
3043 ptrref_operator
3044 { $$ = DeclarationNode::newPointer( 0, $1 ); }
3045 | ptrref_operator type_qualifier_list
3046 { $$ = DeclarationNode::newPointer( $2, $1 ); }
3047 | ptrref_operator variable_abstract_declarator
3048 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3049 | ptrref_operator type_qualifier_list variable_abstract_declarator
3050 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3051 | '(' variable_abstract_ptr ')' attribute_list_opt
3052 { $$ = $2->addQualifiers( $4 ); }
3053 ;
3054
3055variable_abstract_array:
3056 array_dimension
3057 | '(' variable_abstract_ptr ')' array_dimension
3058 { $$ = $2->addArray( $4 ); }
3059 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
3060 { $$ = $2->addArray( $4 ); }
3061 | '(' variable_abstract_array ')' // redundant parenthesis
3062 { $$ = $2; }
3063 ;
3064
3065variable_abstract_function:
3066 '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3067 { $$ = $2->addParamList( $6 ); }
3068 | '(' variable_abstract_function ')' // redundant parenthesis
3069 { $$ = $2; }
3070 ;
3071
3072// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
3073// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
3074
3075cfa_identifier_parameter_declarator_tuple: // CFA
3076 cfa_identifier_parameter_declarator_no_tuple
3077 | cfa_abstract_tuple
3078 | type_qualifier_list cfa_abstract_tuple
3079 { $$ = $2->addQualifiers( $1 ); }
3080 ;
3081
3082cfa_identifier_parameter_declarator_no_tuple: // CFA
3083 cfa_identifier_parameter_ptr
3084 | cfa_identifier_parameter_array
3085 ;
3086
3087cfa_identifier_parameter_ptr: // CFA
3088 // No SUE declaration in parameter list.
3089 ptrref_operator type_specifier_nobody
3090 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3091 | type_qualifier_list ptrref_operator type_specifier_nobody
3092 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3093 | ptrref_operator cfa_abstract_function
3094 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3095 | type_qualifier_list ptrref_operator cfa_abstract_function
3096 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3097 | ptrref_operator cfa_identifier_parameter_declarator_tuple
3098 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3099 | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
3100 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3101 ;
3102
3103cfa_identifier_parameter_array: // CFA
3104 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
3105 // shift/reduce conflict with new-style empty (void) function return type.
3106 '[' ']' type_specifier_nobody
3107 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3108 | cfa_array_parameter_1st_dimension type_specifier_nobody
3109 { $$ = $2->addNewArray( $1 ); }
3110 | '[' ']' multi_array_dimension type_specifier_nobody
3111 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3112 | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
3113 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3114 | multi_array_dimension type_specifier_nobody
3115 { $$ = $2->addNewArray( $1 ); }
3116
3117 | '[' ']' cfa_identifier_parameter_ptr
3118 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3119 | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
3120 { $$ = $2->addNewArray( $1 ); }
3121 | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
3122 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3123 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
3124 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3125 | multi_array_dimension cfa_identifier_parameter_ptr
3126 { $$ = $2->addNewArray( $1 ); }
3127 ;
3128
3129cfa_array_parameter_1st_dimension:
3130 '[' push type_qualifier_list '*' pop ']' // remaining C99
3131 { $$ = DeclarationNode::newVarArray( $3 ); }
3132 | '[' push type_qualifier_list assignment_expression pop ']'
3133 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3134 | '[' push declaration_qualifier_list assignment_expression pop ']'
3135 // declaration_qualifier_list must be used because of shift/reduce conflict with
3136 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
3137 // appear in this context.
3138 { $$ = DeclarationNode::newArray( $4, $3, true ); }
3139 | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
3140 { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
3141 ;
3142
3143// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
3144// identifier to which the type applies, e.g.:
3145//
3146// [int] f( int ); // abstract variable parameter; no parameter name specified
3147// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
3148//
3149// These rules need LR(3):
3150//
3151// cfa_abstract_tuple identifier_or_type_name
3152// '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
3153//
3154// since a function return type can be syntactically identical to a tuple type:
3155//
3156// [int, int] t;
3157// [int, int] f( int );
3158//
3159// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
3160// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
3161// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
3162// duplicated when appearing with cfa_function_specifier.
3163
3164cfa_abstract_declarator_tuple: // CFA
3165 cfa_abstract_tuple
3166 | type_qualifier_list cfa_abstract_tuple
3167 { $$ = $2->addQualifiers( $1 ); }
3168 | cfa_abstract_declarator_no_tuple
3169 ;
3170
3171cfa_abstract_declarator_no_tuple: // CFA
3172 cfa_abstract_ptr
3173 | cfa_abstract_array
3174 ;
3175
3176cfa_abstract_ptr: // CFA
3177 ptrref_operator type_specifier
3178 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3179 | type_qualifier_list ptrref_operator type_specifier
3180 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3181 | ptrref_operator cfa_abstract_function
3182 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3183 | type_qualifier_list ptrref_operator cfa_abstract_function
3184 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3185 | ptrref_operator cfa_abstract_declarator_tuple
3186 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3187 | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
3188 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3189 ;
3190
3191cfa_abstract_array: // CFA
3192 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
3193 // empty (void) function return type.
3194 '[' ']' type_specifier
3195 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3196 | '[' ']' multi_array_dimension type_specifier
3197 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3198 | multi_array_dimension type_specifier
3199 { $$ = $2->addNewArray( $1 ); }
3200 | '[' ']' cfa_abstract_ptr
3201 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3202 | '[' ']' multi_array_dimension cfa_abstract_ptr
3203 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3204 | multi_array_dimension cfa_abstract_ptr
3205 { $$ = $2->addNewArray( $1 ); }
3206 ;
3207
3208cfa_abstract_tuple: // CFA
3209 '[' push cfa_abstract_parameter_list pop ']'
3210 { $$ = DeclarationNode::newTuple( $3 ); }
3211 | '[' push type_specifier_nobody ELLIPSIS pop ']'
3212 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
3213 | '[' push type_specifier_nobody ELLIPSIS constant_expression pop ']'
3214 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
3215 ;
3216
3217cfa_abstract_function: // CFA
3218// '[' ']' '(' cfa_parameter_ellipsis_list_opt ')'
3219// { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
3220 cfa_abstract_tuple '(' push cfa_parameter_ellipsis_list_opt pop ')'
3221 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3222 | cfa_function_return '(' push cfa_parameter_ellipsis_list_opt pop ')'
3223 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3224 ;
3225
3226// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3227// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
3228//
3229// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3230// the declaration specifiers in a declaration is an obsolescent feature."
3231//
3232// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3233// prototype-format parameter type declarators) is an obsolescent feature."
3234//
3235// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3236// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
3237
3238//************************* MISCELLANEOUS ********************************
3239
3240comma_opt: // redundant comma
3241 // empty
3242 | ','
3243 ;
3244
3245default_initialize_opt:
3246 // empty
3247 { $$ = nullptr; }
3248 | '=' assignment_expression
3249 { $$ = $2; }
3250 ;
3251
3252%%
3253// ----end of grammar----
3254
3255// Local Variables: //
3256// mode: c++ //
3257// tab-width: 4 //
3258// compile-command: "make install" //
3259// End: //
Note: See TracBrowser for help on using the repository browser.