source: src/Parser/parser.yy@ bbe1a87

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 bbe1a87 was 4c3ee8d, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

create helper distQual, fix error for SC qualifiers

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