source: src/Parser/parser.yy@ 96fc67b

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 96fc67b was f9941ff, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add error message for unimplemented qualified names [fixes #54]

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