source: src/Parser/parser.yy@ 69efef81

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

simplify code for field declarations

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