source: src/Parser/parser.yy@ 2a8427c6

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

adjust meaning of var-args for C empty parameter list

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