source: src/Parser/parser.yy@ 4ea632e

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

formatting and bug-fix for qualifier distribution

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