source: src/Parser/parser.yy@ afcb0a3

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 no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since afcb0a3 was 3fd8b0e, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Build typedefs inside aggregates

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