source: src/Parser/parser.yy@ 552f5cb

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 552f5cb was 0a73148, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

fix conflict

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