source: src/Parser/parser.yy@ 753bf60

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 753bf60 was 2f0a0678, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

simplify TypedefTable

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