source: src/Parser/parser.yy@ 5bdeb35

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

allow distribution into nested extern "C" block

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