source: src/Parser/parser.yy@ 3ed994e

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

Push deleted decls through the system

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