source: src/Parser/parser.yy@ 9d32bc8

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 9d32bc8 was 6d539f83, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

rename functions

  • 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 : Mon Apr 30 09:20:47 2018
13// Update Count : 3207
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 if ( $1->type->forall ) xxx = forall = true; // remember generic type
2454 }
2455 push '{' external_definition_list '}' // CFA, namespace
2456 {
2457 for ( DeclarationNode * iter = $6; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
2458 iter->addQualifiers( $1->clone() );
2459 iter->addQualifiers( $2->clone() );
2460 } // for
2461 xxx = false;
2462 delete $1;
2463 delete $2;
2464 $$ = $6;
2465 }
2466 ;
2467
2468external_function_definition:
2469 function_definition
2470 // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
2471 // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2472 // Parsing is possible because function_definition does not appear in the context of an expression (nested
2473 // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2474 // declaration must still have a type_specifier. OBSOLESCENT (see 1)
2475 | function_declarator compound_statement
2476 {
2477 typedefTable.addToEnclosingScope( TypedefTable::ID );
2478 typedefTable.leaveScope();
2479 $$ = $1->addFunctionBody( $2 );
2480 }
2481 | KR_function_declarator KR_declaration_list_opt compound_statement
2482 {
2483 typedefTable.addToEnclosingScope( TypedefTable::ID );
2484 typedefTable.leaveScope();
2485 $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 );
2486 }
2487 ;
2488
2489with_clause_opt:
2490 // empty
2491 { $$ = nullptr; forall = false; }
2492 | WITH '(' tuple_expression_list ')'
2493 { $$ = $3; forall = false; }
2494 ;
2495
2496function_definition:
2497 cfa_function_declaration with_clause_opt compound_statement // CFA
2498 {
2499 typedefTable.addToEnclosingScope( TypedefTable::ID );
2500 typedefTable.leaveScope();
2501 // Add the function body to the last identifier in the function definition list, i.e., foo3:
2502 // [const double] foo1(), foo2( int ), foo3( double ) { return 3.0; }
2503 $1->get_last()->addFunctionBody( $3, $2 );
2504 $$ = $1;
2505 }
2506 | declaration_specifier function_declarator with_clause_opt compound_statement
2507 {
2508 rebindForall( $1, $2 );
2509 typedefTable.addToEnclosingScope( TypedefTable::ID );
2510 typedefTable.leaveScope();
2511 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
2512 }
2513 // handles default int return type, OBSOLESCENT (see 1)
2514 | type_qualifier_list function_declarator with_clause_opt compound_statement
2515 {
2516 typedefTable.addToEnclosingScope( TypedefTable::ID );
2517 typedefTable.leaveScope();
2518 $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 );
2519 }
2520 // handles default int return type, OBSOLESCENT (see 1)
2521 | declaration_qualifier_list function_declarator with_clause_opt compound_statement
2522 {
2523 typedefTable.addToEnclosingScope( TypedefTable::ID );
2524 typedefTable.leaveScope();
2525 $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 );
2526 }
2527 // handles default int return type, OBSOLESCENT (see 1)
2528 | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
2529 {
2530 typedefTable.addToEnclosingScope( TypedefTable::ID );
2531 typedefTable.leaveScope();
2532 $$ = $3->addFunctionBody( $5, $4 )->addQualifiers( $2 )->addQualifiers( $1 );
2533 }
2534
2535 // Old-style K&R function definition, OBSOLESCENT (see 4)
2536 | declaration_specifier KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
2537 {
2538 rebindForall( $1, $2 );
2539 typedefTable.addToEnclosingScope( TypedefTable::ID );
2540 typedefTable.leaveScope();
2541 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addType( $1 );
2542 }
2543 // handles default int return type, OBSOLESCENT (see 1)
2544 | type_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
2545 {
2546 typedefTable.addToEnclosingScope( TypedefTable::ID );
2547 typedefTable.leaveScope();
2548 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 );
2549 }
2550 // handles default int return type, OBSOLESCENT (see 1)
2551 | declaration_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
2552 {
2553 typedefTable.addToEnclosingScope( TypedefTable::ID );
2554 typedefTable.leaveScope();
2555 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 );
2556 }
2557 // handles default int return type, OBSOLESCENT (see 1)
2558 | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
2559 {
2560 typedefTable.addToEnclosingScope( TypedefTable::ID );
2561 typedefTable.leaveScope();
2562 $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6, $5 )->addQualifiers( $2 )->addQualifiers( $1 );
2563 }
2564 ;
2565
2566declarator:
2567 variable_declarator
2568 | variable_type_redeclarator
2569 | function_declarator
2570 ;
2571
2572subrange:
2573 constant_expression '~' constant_expression // CFA, integer subrange
2574 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
2575 ;
2576
2577asm_name_opt: // GCC
2578 // empty
2579 { $$ = nullptr; }
2580 | ASM '(' string_literal ')' attribute_list_opt
2581 {
2582 DeclarationNode * name = new DeclarationNode();
2583 name->asmName = $3;
2584 $$ = name->addQualifiers( $5 );
2585 }
2586 ;
2587
2588attribute_list_opt: // GCC
2589 // empty
2590 { $$ = nullptr; }
2591 | attribute_list
2592 ;
2593
2594attribute_list: // GCC
2595 attribute
2596 | attribute_list attribute
2597 { $$ = $2->addQualifiers( $1 ); }
2598 ;
2599
2600attribute: // GCC
2601 ATTRIBUTE '(' '(' attribute_name_list ')' ')'
2602 { $$ = $4; }
2603 ;
2604
2605attribute_name_list: // GCC
2606 attribute_name
2607 | attribute_name_list ',' attribute_name
2608 { $$ = $3->addQualifiers( $1 ); }
2609 ;
2610
2611attribute_name: // GCC
2612 // empty
2613 { $$ = nullptr; }
2614 | attr_name
2615 { $$ = DeclarationNode::newAttribute( $1 ); }
2616 | attr_name '(' argument_expression_list ')'
2617 { $$ = DeclarationNode::newAttribute( $1, $3 ); }
2618 ;
2619
2620attr_name: // GCC
2621 IDENTIFIER
2622 | quasi_keyword
2623 | TYPEDEFname
2624 | TYPEGENname
2625 | FALLTHROUGH
2626 { $$ = Token{ new string( "fallthrough" ), { nullptr, -1 } }; }
2627 | CONST
2628 { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
2629 ;
2630
2631// ============================================================================
2632// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2633// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2634// in:
2635//
2636// int (*f())[10] { ... };
2637// ... (*f())[3] += 1; // definition mimics usage
2638//
2639// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2640// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
2641// ============================================================================
2642
2643// ----------------------------------------------------------------------------
2644// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2645// to define a variable or function prototype, e.g.:
2646//
2647// valid declaration invalid definition
2648// ----------------- ------------------
2649// int f; int f {}
2650// int *f; int *f {}
2651// int f[10]; int f[10] {}
2652// int (*f)(int); int (*f)(int) {}
2653//
2654// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2655// variable_declarator and function_declarator.
2656// ----------------------------------------------------------------------------
2657
2658// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2659// declaring an array of functions versus a pointer to an array of functions.
2660
2661variable_declarator:
2662 paren_identifier attribute_list_opt
2663 { $$ = $1->addQualifiers( $2 ); }
2664 | variable_ptr
2665 | variable_array attribute_list_opt
2666 { $$ = $1->addQualifiers( $2 ); }
2667 | variable_function attribute_list_opt
2668 { $$ = $1->addQualifiers( $2 ); }
2669 ;
2670
2671paren_identifier:
2672 identifier
2673 {
2674 typedefTable.setNextIdentifier( *$1 );
2675 $$ = DeclarationNode::newName( $1 );
2676 }
2677 | '(' paren_identifier ')' // redundant parenthesis
2678 { $$ = $2; }
2679 ;
2680
2681variable_ptr:
2682 ptrref_operator variable_declarator
2683 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2684 | ptrref_operator type_qualifier_list variable_declarator
2685 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2686 | '(' variable_ptr ')' attribute_list_opt
2687 { $$ = $2->addQualifiers( $4 ); } // redundant parenthesis
2688 ;
2689
2690variable_array:
2691 paren_identifier array_dimension
2692 { $$ = $1->addArray( $2 ); }
2693 | '(' variable_ptr ')' array_dimension
2694 { $$ = $2->addArray( $4 ); }
2695 | '(' variable_array ')' multi_array_dimension // redundant parenthesis
2696 { $$ = $2->addArray( $4 ); }
2697 | '(' variable_array ')' // redundant parenthesis
2698 { $$ = $2; }
2699 ;
2700
2701variable_function:
2702 '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2703 { $$ = $2->addParamList( $6 ); }
2704 | '(' variable_function ')' // redundant parenthesis
2705 { $$ = $2; }
2706 ;
2707
2708// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2709// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2710// is the same scope. The pattern precludes returning arrays and functions versus pointers to arrays and functions.
2711
2712function_declarator:
2713 function_no_ptr attribute_list_opt
2714 { $$ = $1->addQualifiers( $2 ); }
2715 | function_ptr
2716 | function_array attribute_list_opt
2717 { $$ = $1->addQualifiers( $2 ); }
2718 ;
2719
2720function_no_ptr:
2721 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2722 { $$ = $1->addParamList( $4 ); }
2723 | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2724 { $$ = $2->addParamList( $6 ); }
2725 | '(' function_no_ptr ')' // redundant parenthesis
2726 { $$ = $2; }
2727 ;
2728
2729function_ptr:
2730 ptrref_operator function_declarator
2731 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2732 | ptrref_operator type_qualifier_list function_declarator
2733 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2734 | '(' function_ptr ')'
2735 { $$ = $2; }
2736 ;
2737
2738function_array:
2739 '(' function_ptr ')' array_dimension
2740 { $$ = $2->addArray( $4 ); }
2741 | '(' function_array ')' multi_array_dimension // redundant parenthesis
2742 { $$ = $2->addArray( $4 ); }
2743 | '(' function_array ')' // redundant parenthesis
2744 { $$ = $2; }
2745 ;
2746
2747// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
2748//
2749// f( a, b, c ) int a, *b, c[]; {}
2750//
2751// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
2752// returning arrays and functions versus pointers to arrays and functions.
2753
2754KR_function_declarator:
2755 KR_function_no_ptr
2756 | KR_function_ptr
2757 | KR_function_array
2758 ;
2759
2760KR_function_no_ptr:
2761 paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
2762 { $$ = $1->addIdList( $3 ); }
2763 | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
2764 { $$ = $2->addParamList( $6 ); }
2765 | '(' KR_function_no_ptr ')' // redundant parenthesis
2766 { $$ = $2; }
2767 ;
2768
2769KR_function_ptr:
2770 ptrref_operator KR_function_declarator
2771 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2772 | ptrref_operator type_qualifier_list KR_function_declarator
2773 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2774 | '(' KR_function_ptr ')'
2775 { $$ = $2; }
2776 ;
2777
2778KR_function_array:
2779 '(' KR_function_ptr ')' array_dimension
2780 { $$ = $2->addArray( $4 ); }
2781 | '(' KR_function_array ')' multi_array_dimension // redundant parenthesis
2782 { $$ = $2->addArray( $4 ); }
2783 | '(' KR_function_array ')' // redundant parenthesis
2784 { $$ = $2; }
2785 ;
2786
2787// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
2788//
2789// typedef int foo;
2790// {
2791// int foo; // redefine typedef name in new scope
2792// }
2793//
2794// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2795// and functions versus pointers to arrays and functions.
2796
2797variable_type_redeclarator:
2798 paren_type attribute_list_opt
2799 { $$ = $1->addQualifiers( $2 ); }
2800 | type_ptr
2801 | type_array attribute_list_opt
2802 { $$ = $1->addQualifiers( $2 ); }
2803 | type_function attribute_list_opt
2804 { $$ = $1->addQualifiers( $2 ); }
2805 ;
2806
2807paren_type:
2808 typedef
2809 | '(' paren_type ')'
2810 { $$ = $2; }
2811 ;
2812
2813type_ptr:
2814 ptrref_operator variable_type_redeclarator
2815 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2816 | ptrref_operator type_qualifier_list variable_type_redeclarator
2817 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2818 | '(' type_ptr ')' attribute_list_opt
2819 { $$ = $2->addQualifiers( $4 ); }
2820 ;
2821
2822type_array:
2823 paren_type array_dimension
2824 { $$ = $1->addArray( $2 ); }
2825 | '(' type_ptr ')' array_dimension
2826 { $$ = $2->addArray( $4 ); }
2827 | '(' type_array ')' multi_array_dimension // redundant parenthesis
2828 { $$ = $2->addArray( $4 ); }
2829 | '(' type_array ')' // redundant parenthesis
2830 { $$ = $2; }
2831 ;
2832
2833type_function:
2834 paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2835 { $$ = $1->addParamList( $4 ); }
2836 | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2837 { $$ = $2->addParamList( $6 ); }
2838 | '(' type_function ')' // redundant parenthesis
2839 { $$ = $2; }
2840 ;
2841
2842// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
2843// typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes
2844// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
2845// pointers to arrays and functions.
2846
2847identifier_parameter_declarator:
2848 paren_identifier attribute_list_opt
2849 { $$ = $1->addQualifiers( $2 ); }
2850 | '&' MUTEX paren_identifier attribute_list_opt
2851 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
2852 | identifier_parameter_ptr
2853 | identifier_parameter_array attribute_list_opt
2854 { $$ = $1->addQualifiers( $2 ); }
2855 | identifier_parameter_function attribute_list_opt
2856 { $$ = $1->addQualifiers( $2 ); }
2857 ;
2858
2859identifier_parameter_ptr:
2860 ptrref_operator identifier_parameter_declarator
2861 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2862 | ptrref_operator type_qualifier_list identifier_parameter_declarator
2863 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2864 | '(' identifier_parameter_ptr ')' attribute_list_opt
2865 { $$ = $2->addQualifiers( $4 ); }
2866 ;
2867
2868identifier_parameter_array:
2869 paren_identifier array_parameter_dimension
2870 { $$ = $1->addArray( $2 ); }
2871 | '(' identifier_parameter_ptr ')' array_dimension
2872 { $$ = $2->addArray( $4 ); }
2873 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2874 { $$ = $2->addArray( $4 ); }
2875 | '(' identifier_parameter_array ')' // redundant parenthesis
2876 { $$ = $2; }
2877 ;
2878
2879identifier_parameter_function:
2880 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2881 { $$ = $1->addParamList( $4 ); }
2882 | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2883 { $$ = $2->addParamList( $6 ); }
2884 | '(' identifier_parameter_function ')' // redundant parenthesis
2885 { $$ = $2; }
2886 ;
2887
2888// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2889// e.g.:
2890//
2891// typedef int foo;
2892// forall( otype T ) struct foo;
2893// int f( int foo ); // redefine typedef name in new scope
2894//
2895// and allows the C99 array options, which can only appear in a parameter list.
2896
2897type_parameter_redeclarator:
2898 typedef attribute_list_opt
2899 { $$ = $1->addQualifiers( $2 ); }
2900 | '&' MUTEX typedef attribute_list_opt
2901 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
2902 | type_parameter_ptr
2903 | type_parameter_array attribute_list_opt
2904 { $$ = $1->addQualifiers( $2 ); }
2905 | type_parameter_function attribute_list_opt
2906 { $$ = $1->addQualifiers( $2 ); }
2907 ;
2908
2909typedef:
2910 TYPEDEFname
2911 {
2912 typedefTable.setNextIdentifier( *$1 );
2913 $$ = DeclarationNode::newName( $1 );
2914 }
2915 | TYPEGENname
2916 {
2917 typedefTable.setNextIdentifier( *$1 );
2918 $$ = DeclarationNode::newName( $1 );
2919 }
2920 ;
2921
2922type_parameter_ptr:
2923 ptrref_operator type_parameter_redeclarator
2924 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2925 | ptrref_operator type_qualifier_list type_parameter_redeclarator
2926 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2927 | '(' type_parameter_ptr ')' attribute_list_opt
2928 { $$ = $2->addQualifiers( $4 ); }
2929 ;
2930
2931type_parameter_array:
2932 typedef array_parameter_dimension
2933 { $$ = $1->addArray( $2 ); }
2934 | '(' type_parameter_ptr ')' array_parameter_dimension
2935 { $$ = $2->addArray( $4 ); }
2936 ;
2937
2938type_parameter_function:
2939 typedef '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2940 { $$ = $1->addParamList( $4 ); }
2941 | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2942 { $$ = $2->addParamList( $6 ); }
2943 ;
2944
2945// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2946// which the type applies, e.g.:
2947//
2948// sizeof( int );
2949// sizeof( int * );
2950// sizeof( int [10] );
2951// sizeof( int (*)() );
2952// sizeof( int () );
2953//
2954// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2955// and functions versus pointers to arrays and functions.
2956
2957abstract_declarator:
2958 abstract_ptr
2959 | abstract_array attribute_list_opt
2960 { $$ = $1->addQualifiers( $2 ); }
2961 | abstract_function attribute_list_opt
2962 { $$ = $1->addQualifiers( $2 ); }
2963 ;
2964
2965abstract_ptr:
2966 ptrref_operator
2967 { $$ = DeclarationNode::newPointer( 0, $1 ); }
2968 | ptrref_operator type_qualifier_list
2969 { $$ = DeclarationNode::newPointer( $2, $1 ); }
2970 | ptrref_operator abstract_declarator
2971 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2972 | ptrref_operator type_qualifier_list abstract_declarator
2973 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2974 | '(' abstract_ptr ')' attribute_list_opt
2975 { $$ = $2->addQualifiers( $4 ); }
2976 ;
2977
2978abstract_array:
2979 array_dimension
2980 | '(' abstract_ptr ')' array_dimension
2981 { $$ = $2->addArray( $4 ); }
2982 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
2983 { $$ = $2->addArray( $4 ); }
2984 | '(' abstract_array ')' // redundant parenthesis
2985 { $$ = $2; }
2986 ;
2987
2988abstract_function:
2989 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2990 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
2991 | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2992 { $$ = $2->addParamList( $6 ); }
2993 | '(' abstract_function ')' // redundant parenthesis
2994 { $$ = $2; }
2995 ;
2996
2997array_dimension:
2998 // Only the first dimension can be empty.
2999 '[' ']'
3000 { $$ = DeclarationNode::newArray( 0, 0, false ); }
3001 | '[' ']' multi_array_dimension
3002 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
3003 | multi_array_dimension
3004 ;
3005
3006multi_array_dimension:
3007 '[' push assignment_expression pop ']'
3008 { $$ = DeclarationNode::newArray( $3, 0, false ); }
3009 | '[' push '*' pop ']' // C99
3010 { $$ = DeclarationNode::newVarArray( 0 ); }
3011 | multi_array_dimension '[' push assignment_expression pop ']'
3012 { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
3013 | multi_array_dimension '[' push '*' pop ']' // C99
3014 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
3015 ;
3016
3017// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
3018// identifier to which the type applies, e.g.:
3019//
3020// int f( int ); // not handled here
3021// int f( int * ); // abstract function-prototype parameter; no parameter name specified
3022// int f( int (*)() ); // abstract function-prototype parameter; no parameter name specified
3023// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
3024//
3025// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3026// and functions versus pointers to arrays and functions. In addition, the pattern handles the
3027// special meaning of parenthesis around a typedef name:
3028//
3029// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
3030// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
3031// not as redundant parentheses around the identifier."
3032//
3033// For example:
3034//
3035// typedef float T;
3036// int f( int ( T [5] ) ); // see abstract_parameter_declarator
3037// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
3038// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
3039// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
3040//
3041// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
3042// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
3043// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
3044// functions.
3045
3046abstract_parameter_declarator:
3047 abstract_parameter_ptr
3048 | '&' MUTEX attribute_list_opt
3049 { $$ = DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf )->addQualifiers( $3 ); }
3050 | abstract_parameter_array attribute_list_opt
3051 { $$ = $1->addQualifiers( $2 ); }
3052 | abstract_parameter_function attribute_list_opt
3053 { $$ = $1->addQualifiers( $2 ); }
3054 ;
3055
3056abstract_parameter_ptr:
3057 ptrref_operator
3058 { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
3059 | ptrref_operator type_qualifier_list
3060 { $$ = DeclarationNode::newPointer( $2, $1 ); }
3061 | ptrref_operator abstract_parameter_declarator
3062 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3063 | ptrref_operator type_qualifier_list abstract_parameter_declarator
3064 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3065 | '(' abstract_parameter_ptr ')' attribute_list_opt
3066 { $$ = $2->addQualifiers( $4 ); }
3067 ;
3068
3069abstract_parameter_array:
3070 array_parameter_dimension
3071 | '(' abstract_parameter_ptr ')' array_parameter_dimension
3072 { $$ = $2->addArray( $4 ); }
3073 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
3074 { $$ = $2->addArray( $4 ); }
3075 | '(' abstract_parameter_array ')' // redundant parenthesis
3076 { $$ = $2; }
3077 ;
3078
3079abstract_parameter_function:
3080 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3081 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
3082 | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3083 { $$ = $2->addParamList( $6 ); }
3084 | '(' abstract_parameter_function ')' // redundant parenthesis
3085 { $$ = $2; }
3086 ;
3087
3088array_parameter_dimension:
3089 // Only the first dimension can be empty or have qualifiers.
3090 array_parameter_1st_dimension
3091 | array_parameter_1st_dimension multi_array_dimension
3092 { $$ = $1->addArray( $2 ); }
3093 | multi_array_dimension
3094 ;
3095
3096// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
3097//
3098// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
3099// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
3100
3101array_parameter_1st_dimension:
3102 '[' ']'
3103 { $$ = DeclarationNode::newArray( 0, 0, false ); }
3104 // multi_array_dimension handles the '[' '*' ']' case
3105 | '[' push type_qualifier_list '*' pop ']' // remaining C99
3106 { $$ = DeclarationNode::newVarArray( $3 ); }
3107 | '[' push type_qualifier_list pop ']'
3108 { $$ = DeclarationNode::newArray( 0, $3, false ); }
3109 // multi_array_dimension handles the '[' assignment_expression ']' case
3110 | '[' push type_qualifier_list assignment_expression pop ']'
3111 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3112 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
3113 { $$ = DeclarationNode::newArray( $5, $4, true ); }
3114 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
3115 { $$ = DeclarationNode::newArray( $5, $3, true ); }
3116 ;
3117
3118// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
3119//
3120// struct S {
3121// int;
3122// int *;
3123// int [10];
3124// int (*)();
3125// };
3126
3127variable_abstract_declarator:
3128 variable_abstract_ptr
3129 | variable_abstract_array attribute_list_opt
3130 { $$ = $1->addQualifiers( $2 ); }
3131 | variable_abstract_function attribute_list_opt
3132 { $$ = $1->addQualifiers( $2 ); }
3133 ;
3134
3135variable_abstract_ptr:
3136 ptrref_operator
3137 { $$ = DeclarationNode::newPointer( 0, $1 ); }
3138 | ptrref_operator type_qualifier_list
3139 { $$ = DeclarationNode::newPointer( $2, $1 ); }
3140 | ptrref_operator variable_abstract_declarator
3141 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3142 | ptrref_operator type_qualifier_list variable_abstract_declarator
3143 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3144 | '(' variable_abstract_ptr ')' attribute_list_opt
3145 { $$ = $2->addQualifiers( $4 ); }
3146 ;
3147
3148variable_abstract_array:
3149 array_dimension
3150 | '(' variable_abstract_ptr ')' array_dimension
3151 { $$ = $2->addArray( $4 ); }
3152 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
3153 { $$ = $2->addArray( $4 ); }
3154 | '(' variable_abstract_array ')' // redundant parenthesis
3155 { $$ = $2; }
3156 ;
3157
3158variable_abstract_function:
3159 '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3160 { $$ = $2->addParamList( $6 ); }
3161 | '(' variable_abstract_function ')' // redundant parenthesis
3162 { $$ = $2; }
3163 ;
3164
3165// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
3166// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
3167
3168cfa_identifier_parameter_declarator_tuple: // CFA
3169 cfa_identifier_parameter_declarator_no_tuple
3170 | cfa_abstract_tuple
3171 | type_qualifier_list cfa_abstract_tuple
3172 { $$ = $2->addQualifiers( $1 ); }
3173 ;
3174
3175cfa_identifier_parameter_declarator_no_tuple: // CFA
3176 cfa_identifier_parameter_ptr
3177 | cfa_identifier_parameter_array
3178 ;
3179
3180cfa_identifier_parameter_ptr: // CFA
3181 // No SUE declaration in parameter list.
3182 ptrref_operator type_specifier_nobody
3183 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3184 | type_qualifier_list ptrref_operator type_specifier_nobody
3185 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3186 | ptrref_operator cfa_abstract_function
3187 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3188 | type_qualifier_list ptrref_operator cfa_abstract_function
3189 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3190 | ptrref_operator cfa_identifier_parameter_declarator_tuple
3191 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3192 | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
3193 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3194 ;
3195
3196cfa_identifier_parameter_array: // CFA
3197 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
3198 // shift/reduce conflict with new-style empty (void) function return type.
3199 '[' ']' type_specifier_nobody
3200 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3201 | cfa_array_parameter_1st_dimension type_specifier_nobody
3202 { $$ = $2->addNewArray( $1 ); }
3203 | '[' ']' multi_array_dimension type_specifier_nobody
3204 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3205 | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
3206 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3207 | multi_array_dimension type_specifier_nobody
3208 { $$ = $2->addNewArray( $1 ); }
3209
3210 | '[' ']' cfa_identifier_parameter_ptr
3211 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3212 | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
3213 { $$ = $2->addNewArray( $1 ); }
3214 | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
3215 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3216 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
3217 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3218 | multi_array_dimension cfa_identifier_parameter_ptr
3219 { $$ = $2->addNewArray( $1 ); }
3220 ;
3221
3222cfa_array_parameter_1st_dimension:
3223 '[' push type_qualifier_list '*' pop ']' // remaining C99
3224 { $$ = DeclarationNode::newVarArray( $3 ); }
3225 | '[' push type_qualifier_list assignment_expression pop ']'
3226 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3227 | '[' push declaration_qualifier_list assignment_expression pop ']'
3228 // declaration_qualifier_list must be used because of shift/reduce conflict with
3229 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
3230 // appear in this context.
3231 { $$ = DeclarationNode::newArray( $4, $3, true ); }
3232 | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
3233 { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
3234 ;
3235
3236// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
3237// identifier to which the type applies, e.g.:
3238//
3239// [int] f( int ); // abstract variable parameter; no parameter name specified
3240// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
3241//
3242// These rules need LR(3):
3243//
3244// cfa_abstract_tuple identifier_or_type_name
3245// '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_type_list_opt ')'
3246//
3247// since a function return type can be syntactically identical to a tuple type:
3248//
3249// [int, int] t;
3250// [int, int] f( int );
3251//
3252// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
3253// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
3254// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
3255// duplicated when appearing with cfa_function_specifier.
3256
3257cfa_abstract_declarator_tuple: // CFA
3258 cfa_abstract_tuple
3259 | type_qualifier_list cfa_abstract_tuple
3260 { $$ = $2->addQualifiers( $1 ); }
3261 | cfa_abstract_declarator_no_tuple
3262 ;
3263
3264cfa_abstract_declarator_no_tuple: // CFA
3265 cfa_abstract_ptr
3266 | cfa_abstract_array
3267 ;
3268
3269cfa_abstract_ptr: // CFA
3270 ptrref_operator type_specifier
3271 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3272 | type_qualifier_list ptrref_operator type_specifier
3273 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3274 | ptrref_operator cfa_abstract_function
3275 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3276 | type_qualifier_list ptrref_operator cfa_abstract_function
3277 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3278 | ptrref_operator cfa_abstract_declarator_tuple
3279 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3280 | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
3281 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3282 ;
3283
3284cfa_abstract_array: // CFA
3285 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
3286 // empty (void) function return type.
3287 '[' ']' type_specifier
3288 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3289 | '[' ']' multi_array_dimension type_specifier
3290 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3291 | multi_array_dimension type_specifier
3292 { $$ = $2->addNewArray( $1 ); }
3293 | '[' ']' cfa_abstract_ptr
3294 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3295 | '[' ']' multi_array_dimension cfa_abstract_ptr
3296 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3297 | multi_array_dimension cfa_abstract_ptr
3298 { $$ = $2->addNewArray( $1 ); }
3299 ;
3300
3301cfa_abstract_tuple: // CFA
3302 '[' push cfa_abstract_parameter_list pop ']'
3303 { $$ = DeclarationNode::newTuple( $3 ); }
3304 ;
3305
3306cfa_abstract_function: // CFA
3307// '[' ']' '(' cfa_parameter_type_list_opt ')'
3308// { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
3309 cfa_abstract_tuple '(' push cfa_parameter_type_list_opt pop ')'
3310 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3311 | cfa_function_return '(' push cfa_parameter_type_list_opt pop ')'
3312 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3313 ;
3314
3315// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3316// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
3317//
3318// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3319// the declaration specifiers in a declaration is an obsolescent feature."
3320//
3321// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3322// prototype-format parameter type declarators) is an obsolescent feature."
3323//
3324// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3325// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
3326
3327//************************* MISCELLANEOUS ********************************
3328
3329comma_opt: // redundant comma
3330 // empty
3331 | ','
3332 ;
3333
3334default_initialize_opt:
3335 // empty
3336 { $$ = nullptr; }
3337 | '=' assignment_expression
3338 { $$ = $2; }
3339 ;
3340
3341%%
3342// ----end of grammar----
3343
3344// Local Variables: //
3345// mode: c++ //
3346// tab-width: 4 //
3347// compile-command: "make install" //
3348// End: //
Note: See TracBrowser for help on using the repository browser.