source: src/Parser/parser.yy@ 70529dc

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

add mutex statement, and restrict mutex qualifier to only one occurrence in parameter list

  • Property mode set to 100644
File size: 117.2 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// parser.yy --
8//
9// Author : Peter A. Buhr
10// Created On : Sat Sep 1 20:22:55 2001
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Oct 16 11:07:29 2017
13// Update Count : 2892
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 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
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 { $$ = nullptr; } // FIX ME
461 | type_name '.' '[' push field_list pop ']' // CFA, nested type / tuple field selector
462 { $$ = 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: // CFA
977 FALLTHRU
978 { $$ = nullptr; }
979 | FALLTHRU ';'
980 { $$ = nullptr; }
981 ;
982
983iteration_statement:
984 WHILE '(' comma_expression ')' statement
985 { $$ = new StatementNode( build_while( $3, $5 ) ); }
986 | DO statement WHILE '(' comma_expression ')' ';'
987 { $$ = new StatementNode( build_while( $5, $2, true ) ); }
988 | FOR '(' push for_control_expression ')' statement
989 { $$ = new StatementNode( build_for( $4, $6 ) ); }
990 ;
991
992for_control_expression:
993 comma_expression_opt pop ';' comma_expression_opt ';' comma_expression_opt
994 { $$ = new ForCtl( $1, $4, $6 ); }
995 | declaration comma_expression_opt ';' comma_expression_opt // C99
996 { $$ = new ForCtl( $1, $2, $4 ); }
997 ;
998
999jump_statement:
1000 GOTO identifier_or_type_name ';'
1001 { $$ = new StatementNode( build_branch( $2, BranchStmt::Goto ) ); }
1002 | GOTO '*' comma_expression ';' // GCC, computed goto
1003 // The syntax for the GCC computed goto violates normal expression precedence, e.g., goto *i+3; => goto *(i+3);
1004 // whereas normal operator precedence yields goto (*i)+3;
1005 { $$ = new StatementNode( build_computedgoto( $3 ) ); }
1006 | CONTINUE ';'
1007 // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
1008 { $$ = new StatementNode( build_branch( BranchStmt::Continue ) ); }
1009 | CONTINUE identifier_or_type_name ';' // CFA, multi-level continue
1010 // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1011 // the target of the transfer appears only at the start of an iteration statement.
1012 { $$ = new StatementNode( build_branch( $2, BranchStmt::Continue ) ); }
1013 | BREAK ';'
1014 // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
1015 { $$ = new StatementNode( build_branch( BranchStmt::Break ) ); }
1016 | BREAK identifier_or_type_name ';' // CFA, multi-level exit
1017 // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1018 // the target of the transfer appears only at the start of an iteration statement.
1019 { $$ = new StatementNode( build_branch( $2, BranchStmt::Break ) ); }
1020 | RETURN comma_expression_opt ';'
1021 { $$ = new StatementNode( build_return( $2 ) ); }
1022 | THROW assignment_expression_opt ';' // handles rethrow
1023 { $$ = new StatementNode( build_throw( $2 ) ); }
1024 | THROWRESUME assignment_expression_opt ';' // handles reresume
1025 { $$ = new StatementNode( build_resume( $2 ) ); }
1026 | THROWRESUME assignment_expression_opt AT assignment_expression ';' // handles reresume
1027 { $$ = new StatementNode( build_resume_at( $2, $4 ) ); }
1028 ;
1029
1030with_statement:
1031 WITH '(' tuple_expression_list ')' statement
1032 { $$ = nullptr; } // FIX ME
1033 ;
1034
1035// If MUTEX becomes a general qualifier, there are shift/reduce conflicts, so change syntax to "with mutex".
1036mutex_statement:
1037 MUTEX '(' argument_expression_list ')' statement
1038 { $$ = nullptr; } // FIX ME
1039 ;
1040
1041when_clause:
1042 WHEN '(' comma_expression ')'
1043 { $$ = $3; }
1044 ;
1045
1046when_clause_opt:
1047 // empty
1048 { $$ = nullptr; }
1049 | when_clause
1050 ;
1051
1052waitfor:
1053 WAITFOR '(' identifier ')'
1054 {
1055 $$ = new ExpressionNode( new NameExpr( *$3 ) );
1056 delete $3;
1057 }
1058 | WAITFOR '(' identifier ',' argument_expression_list ')'
1059 {
1060 $$ = new ExpressionNode( new NameExpr( *$3 ) );
1061 $$->set_last( $5 );
1062 delete $3;
1063 }
1064 ;
1065
1066timeout:
1067 TIMEOUT '(' comma_expression ')'
1068 { $$ = $3; }
1069 ;
1070
1071waitfor_clause:
1072 when_clause_opt waitfor statement %prec THEN
1073 { $$ = build_waitfor( $2, $3, $1 ); }
1074 | when_clause_opt waitfor statement WOR waitfor_clause
1075 { $$ = build_waitfor( $2, $3, $1, $5 ); }
1076 | when_clause_opt timeout statement %prec THEN
1077 { $$ = build_waitfor_timeout( $2, $3, $1 ); }
1078 | when_clause_opt ELSE statement
1079 { $$ = build_waitfor_timeout( nullptr, $3, $1 ); }
1080 // "else" must be conditional after timeout or timeout is never triggered (i.e., it is meaningless)
1081 | when_clause_opt timeout statement WOR when_clause ELSE statement
1082 { $$ = build_waitfor_timeout( $2, $3, $1, $7, $5 ); }
1083 ;
1084
1085waitfor_statement:
1086 when_clause_opt waitfor statement %prec THEN
1087 { $$ = new StatementNode( build_waitfor( $2, $3, $1 ) ); }
1088 | when_clause_opt waitfor statement WOR waitfor_clause
1089 { $$ = new StatementNode( build_waitfor( $2, $3, $1, $5 ) ); }
1090 ;
1091
1092exception_statement:
1093 TRY compound_statement handler_clause
1094 { $$ = new StatementNode( build_try( $2, $3, 0 ) ); }
1095 | TRY compound_statement finally_clause
1096 { $$ = new StatementNode( build_try( $2, 0, $3 ) ); }
1097 | TRY compound_statement handler_clause finally_clause
1098 { $$ = new StatementNode( build_try( $2, $3, $4 ) ); }
1099 ;
1100
1101handler_clause:
1102 handler_key '(' push push exception_declaration pop handler_predicate_opt ')' compound_statement pop
1103 { $$ = new StatementNode( build_catch( $1, $5, $7, $9 ) ); }
1104 | handler_clause handler_key '(' push push exception_declaration pop handler_predicate_opt ')' compound_statement pop
1105 { $$ = (StatementNode *)$1->set_last( new StatementNode( build_catch( $2, $6, $8, $10 ) ) ); }
1106 ;
1107
1108handler_predicate_opt:
1109 //empty
1110 { $$ = nullptr; }
1111 | ';' conditional_expression
1112 { $$ = $2; }
1113 ;
1114
1115handler_key:
1116 CATCH
1117 { $$ = CatchStmt::Terminate; }
1118 | CATCHRESUME
1119 { $$ = CatchStmt::Resume; }
1120 ;
1121
1122finally_clause:
1123 FINALLY compound_statement
1124 {
1125 $$ = new StatementNode( build_finally( $2 ) );
1126 }
1127 ;
1128
1129exception_declaration:
1130 // No SUE declaration in parameter list.
1131 type_specifier_nobody
1132 | type_specifier_nobody declarator
1133 {
1134 typedefTable.addToEnclosingScope( TypedefTable::ID );
1135 $$ = $2->addType( $1 );
1136 }
1137 | type_specifier_nobody variable_abstract_declarator
1138 { $$ = $2->addType( $1 ); }
1139 | cfa_abstract_declarator_tuple no_attr_identifier // CFA
1140 {
1141 typedefTable.addToEnclosingScope( TypedefTable::ID );
1142 $$ = $1->addName( $2 );
1143 }
1144 | cfa_abstract_declarator_tuple // CFA
1145 ;
1146
1147asm_statement:
1148 ASM asm_volatile_opt '(' string_literal ')' ';'
1149 { $$ = new StatementNode( build_asmstmt( $2, $4, 0 ) ); }
1150 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ')' ';' // remaining GCC
1151 { $$ = new StatementNode( build_asmstmt( $2, $4, $6 ) ); }
1152 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ')' ';'
1153 { $$ = new StatementNode( build_asmstmt( $2, $4, $6, $8 ) ); }
1154 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ':' asm_clobbers_list_opt ')' ';'
1155 { $$ = new StatementNode( build_asmstmt( $2, $4, $6, $8, $10 ) ); }
1156 | ASM asm_volatile_opt GOTO '(' string_literal ':' ':' asm_operands_opt ':' asm_clobbers_list_opt ':' label_list ')' ';'
1157 { $$ = new StatementNode( build_asmstmt( $2, $5, 0, $8, $10, $12 ) ); }
1158 ;
1159
1160asm_volatile_opt: // GCC
1161 // empty
1162 { $$ = false; }
1163 | VOLATILE
1164 { $$ = true; }
1165 ;
1166
1167asm_operands_opt: // GCC
1168 // empty
1169 { $$ = nullptr; } // use default argument
1170 | asm_operands_list
1171 ;
1172
1173asm_operands_list: // GCC
1174 asm_operand
1175 | asm_operands_list ',' asm_operand
1176 { $$ = (ExpressionNode *)$1->set_last( $3 ); }
1177 ;
1178
1179asm_operand: // GCC
1180 string_literal '(' constant_expression ')'
1181 { $$ = new ExpressionNode( new AsmExpr( maybeMoveBuild< Expression >( (ExpressionNode *)nullptr ), $1, maybeMoveBuild< Expression >( $3 ) ) ); }
1182 | '[' constant_expression ']' string_literal '(' constant_expression ')'
1183 { $$ = new ExpressionNode( new AsmExpr( maybeMoveBuild< Expression >( $2 ), $4, maybeMoveBuild< Expression >( $6 ) ) ); }
1184 ;
1185
1186asm_clobbers_list_opt: // GCC
1187 // empty
1188 { $$ = nullptr; } // use default argument
1189 | string_literal
1190 { $$ = new ExpressionNode( $1 ); }
1191 | asm_clobbers_list_opt ',' string_literal
1192 // set_last returns ParseNode *
1193 { $$ = (ExpressionNode *)$1->set_last( new ExpressionNode( $3 ) ); }
1194 ;
1195
1196label_list:
1197 no_attr_identifier
1198 {
1199 $$ = new LabelNode(); $$->labels.push_back( *$1 );
1200 delete $1; // allocated by lexer
1201 }
1202 | label_list ',' no_attr_identifier
1203 {
1204 $$ = $1; $1->labels.push_back( *$3 );
1205 delete $3; // allocated by lexer
1206 }
1207 ;
1208
1209//******************************* DECLARATIONS *********************************
1210
1211declaration_list_opt: // used at beginning of switch statement
1212 pop
1213 { $$ = nullptr; }
1214 | declaration_list
1215 ;
1216
1217declaration_list:
1218 declaration
1219 | declaration_list push declaration
1220 { $$ = $1->appendList( $3 ); }
1221 ;
1222
1223KR_declaration_list_opt: // used to declare parameter types in K&R style functions
1224 // empty
1225 { $$ = nullptr; }
1226 | KR_declaration_list
1227 ;
1228
1229KR_declaration_list:
1230 push c_declaration pop ';'
1231 { $$ = $2; }
1232 | KR_declaration_list push c_declaration pop ';'
1233 { $$ = $1->appendList( $3 ); }
1234 ;
1235
1236local_label_declaration_opt: // GCC, local label
1237 // empty
1238 | local_label_declaration_list
1239 ;
1240
1241local_label_declaration_list: // GCC, local label
1242 LABEL local_label_list ';'
1243 | local_label_declaration_list LABEL local_label_list ';'
1244 ;
1245
1246local_label_list: // GCC, local label
1247 no_attr_identifier_or_type_name {}
1248 | local_label_list ',' no_attr_identifier_or_type_name {}
1249 ;
1250
1251declaration: // old & new style declarations
1252 c_declaration pop ';'
1253 | cfa_declaration pop ';' // CFA
1254 ;
1255
1256// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
1257// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
1258// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
1259// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
1260// the declaration list (as in Pascal). ANSI C and the new CFA declarations may appear together in the same program
1261// block, but cannot be mixed within a specific declaration.
1262//
1263// CFA C
1264// [10] int x; int x[10]; // array of 10 integers
1265// [10] * char y; char *y[10]; // array of 10 pointers to char
1266
1267cfa_declaration: // CFA
1268 cfa_variable_declaration
1269 | cfa_typedef_declaration
1270 | cfa_function_declaration
1271 | type_declaring_list
1272 | trait_specifier
1273 ;
1274
1275cfa_variable_declaration: // CFA
1276 cfa_variable_specifier initializer_opt
1277 {
1278 typedefTable.addToEnclosingScope( TypedefTable::ID );
1279 $$ = $1->addInitializer( $2 );
1280 }
1281 | declaration_qualifier_list cfa_variable_specifier initializer_opt
1282 // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
1283 // them as a type_qualifier cannot appear in that context.
1284 {
1285 typedefTable.addToEnclosingScope( TypedefTable::ID );
1286 $$ = $2->addQualifiers( $1 )->addInitializer( $3 );;
1287 }
1288 | cfa_variable_declaration pop ',' push identifier_or_type_name initializer_opt
1289 {
1290 typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
1291 $$ = $1->appendList( $1->cloneType( $5 )->addInitializer( $6 ) );
1292 }
1293 ;
1294
1295cfa_variable_specifier: // CFA
1296 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1297 // storage-class
1298 cfa_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
1299 {
1300 typedefTable.setNextIdentifier( *$2 );
1301 $$ = $1->addName( $2 )->addAsmName( $3 );
1302 }
1303 | cfa_abstract_tuple identifier_or_type_name asm_name_opt
1304 {
1305 typedefTable.setNextIdentifier( *$2 );
1306 $$ = $1->addName( $2 )->addAsmName( $3 );
1307 }
1308 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name asm_name_opt
1309 {
1310 typedefTable.setNextIdentifier( *$3 );
1311 $$ = $2->addQualifiers( $1 )->addName( $3 )->addAsmName( $4 );
1312 }
1313 ;
1314
1315cfa_function_declaration: // CFA
1316 cfa_function_specifier
1317 {
1318 typedefTable.addToEnclosingScope( TypedefTable::ID );
1319 $$ = $1;
1320 }
1321 | type_qualifier_list cfa_function_specifier
1322 {
1323 typedefTable.addToEnclosingScope( TypedefTable::ID );
1324 $$ = $2->addQualifiers( $1 );
1325 }
1326 | declaration_qualifier_list cfa_function_specifier
1327 {
1328 typedefTable.addToEnclosingScope( TypedefTable::ID );
1329 $$ = $2->addQualifiers( $1 );
1330 }
1331 | declaration_qualifier_list type_qualifier_list cfa_function_specifier
1332 {
1333 typedefTable.addToEnclosingScope( TypedefTable::ID );
1334 $$ = $3->addQualifiers( $1 )->addQualifiers( $2 );
1335 }
1336 | cfa_function_declaration pop ',' push identifier_or_type_name
1337 {
1338 typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
1339 $$ = $1->appendList( $1->cloneType( $5 ) );
1340 }
1341 ;
1342
1343cfa_function_specifier: // CFA
1344// '[' ']' identifier_or_type_name '(' push cfa_parameter_type_list_opt pop ')' // S/R conflict
1345// {
1346// $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, 0, true );
1347// }
1348// '[' ']' identifier '(' push cfa_parameter_type_list_opt pop ')'
1349// {
1350// typedefTable.setNextIdentifier( *$5 );
1351// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1352// }
1353// | '[' ']' TYPEDEFname '(' push cfa_parameter_type_list_opt pop ')'
1354// {
1355// typedefTable.setNextIdentifier( *$5 );
1356// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1357// }
1358// | '[' ']' typegen_name
1359 // identifier_or_type_name must be broken apart because of the sequence:
1360 //
1361 // '[' ']' identifier_or_type_name '(' cfa_parameter_type_list_opt ')'
1362 // '[' ']' type_specifier
1363 //
1364 // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1365 // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
1366 cfa_abstract_tuple identifier_or_type_name '(' push cfa_parameter_type_list_opt pop ')'
1367 // To obtain LR(1 ), this rule must be factored out from function return type (see cfa_abstract_declarator).
1368 {
1369 $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1370 }
1371 | cfa_function_return identifier_or_type_name '(' push cfa_parameter_type_list_opt pop ')'
1372 {
1373 $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1374 }
1375 ;
1376
1377cfa_function_return: // CFA
1378 '[' push cfa_parameter_list pop ']'
1379 { $$ = DeclarationNode::newTuple( $3 ); }
1380 | '[' push cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ']'
1381 // To obtain LR(1 ), the last cfa_abstract_parameter_list is added into this flattened rule to lookahead to the
1382 // ']'.
1383 { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
1384 ;
1385
1386cfa_typedef_declaration: // CFA
1387 TYPEDEF cfa_variable_specifier
1388 {
1389 typedefTable.addToEnclosingScope( TypedefTable::TD );
1390 $$ = $2->addTypedef();
1391 }
1392 | TYPEDEF cfa_function_specifier
1393 {
1394 typedefTable.addToEnclosingScope( TypedefTable::TD );
1395 $$ = $2->addTypedef();
1396 }
1397 | cfa_typedef_declaration pop ',' push no_attr_identifier
1398 {
1399 typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
1400 $$ = $1->appendList( $1->cloneType( $5 ) );
1401 }
1402 ;
1403
1404// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1405// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
1406
1407typedef_declaration:
1408 TYPEDEF type_specifier declarator
1409 {
1410 typedefTable.addToEnclosingScope( TypedefTable::TD );
1411 $$ = $3->addType( $2 )->addTypedef();
1412 }
1413 | typedef_declaration pop ',' push declarator
1414 {
1415 typedefTable.addToEnclosingScope( TypedefTable::TD );
1416 $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1417 }
1418 | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
1419 {
1420 typedefTable.addToEnclosingScope( TypedefTable::TD );
1421 $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1422 }
1423 | type_specifier TYPEDEF declarator
1424 {
1425 typedefTable.addToEnclosingScope( TypedefTable::TD );
1426 $$ = $3->addType( $1 )->addTypedef();
1427 }
1428 | type_specifier TYPEDEF type_qualifier_list declarator
1429 {
1430 typedefTable.addToEnclosingScope( TypedefTable::TD );
1431 $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
1432 }
1433 ;
1434
1435typedef_expression:
1436 // GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
1437 TYPEDEF no_attr_identifier '=' assignment_expression
1438 {
1439 typedefTable.addToEnclosingScope( *$2, TypedefTable::TD );
1440 $$ = DeclarationNode::newName( 0 ); // unimplemented
1441 }
1442 | typedef_expression pop ',' push no_attr_identifier '=' assignment_expression
1443 {
1444 typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
1445 $$ = DeclarationNode::newName( 0 ); // unimplemented
1446 }
1447 ;
1448
1449//c_declaration:
1450// declaring_list pop ';'
1451// | typedef_declaration pop ';'
1452// | typedef_expression pop ';' // GCC, naming expression type
1453// | sue_declaration_specifier pop ';'
1454// ;
1455//
1456//declaring_list:
1457// // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1458// // storage-class
1459// declarator asm_name_opt initializer_opt
1460// {
1461// typedefTable.addToEnclosingScope( TypedefTable::ID );
1462// $$ = ( $2->addType( $1 ))->addAsmName( $3 )->addInitializer( $4 );
1463// }
1464// | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1465// {
1466// typedefTable.addToEnclosingScope( TypedefTable::ID );
1467// $$ = $1->appendList( $1->cloneBaseType( $4->addAsmName( $5 )->addInitializer( $6 ) ) );
1468// }
1469// ;
1470
1471c_declaration:
1472 declaration_specifier declaring_list
1473 {
1474 $$ = distAttr( $1, $2 );
1475 }
1476 | typedef_declaration
1477 | typedef_expression // GCC, naming expression type
1478 | sue_declaration_specifier
1479 ;
1480
1481declaring_list:
1482 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1483 // storage-class
1484 declarator asm_name_opt initializer_opt
1485 {
1486 typedefTable.addToEnclosingScope( TypedefTable::ID );
1487 $$ = $1->addAsmName( $2 )->addInitializer( $3 );
1488 }
1489 | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1490 {
1491 typedefTable.addToEnclosingScope( TypedefTable::ID );
1492 $$ = $1->appendList( $4->addQualifiers( $3 )->addAsmName( $5 )->addInitializer( $6 ) );
1493 }
1494 ;
1495
1496declaration_specifier: // type specifier + storage class
1497 basic_declaration_specifier
1498 | sue_declaration_specifier
1499 | type_declaration_specifier
1500 ;
1501
1502declaration_specifier_nobody: // type specifier + storage class - {...}
1503 // Preclude SUE declarations in restricted scopes:
1504 //
1505 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1506 //
1507 // because it is impossible to call f due to name equivalence.
1508 basic_declaration_specifier
1509 | sue_declaration_specifier_nobody
1510 | type_declaration_specifier
1511 ;
1512
1513type_specifier: // type specifier
1514 basic_type_specifier
1515 | sue_type_specifier
1516 | type_type_specifier
1517 ;
1518
1519type_specifier_nobody: // type specifier - {...}
1520 // Preclude SUE declarations in restricted scopes:
1521 //
1522 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1523 //
1524 // because it is impossible to call f due to name equivalence.
1525 basic_type_specifier
1526 | sue_type_specifier_nobody
1527 | type_type_specifier
1528 ;
1529
1530type_qualifier_list_opt: // GCC, used in asm_statement
1531 // empty
1532 { $$ = nullptr; }
1533 | type_qualifier_list
1534 ;
1535
1536type_qualifier_list:
1537 // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
1538 //
1539 // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1540 // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1541 // appeared only once.
1542 type_qualifier
1543 | type_qualifier_list type_qualifier
1544 { $$ = $1->addQualifiers( $2 ); }
1545 ;
1546
1547type_qualifier:
1548 type_qualifier_name
1549 | attribute
1550 ;
1551
1552type_qualifier_name:
1553 CONST
1554 { $$ = DeclarationNode::newTypeQualifier( Type::Const ); }
1555 | RESTRICT
1556 { $$ = DeclarationNode::newTypeQualifier( Type::Restrict ); }
1557 | VOLATILE
1558 { $$ = DeclarationNode::newTypeQualifier( Type::Volatile ); }
1559 | ATOMIC
1560 { $$ = DeclarationNode::newTypeQualifier( Type::Atomic ); }
1561 | FORALL '('
1562 {
1563 typedefTable.enterScope();
1564 }
1565 type_parameter_list ')' // CFA
1566 {
1567 typedefTable.leaveScope();
1568 $$ = DeclarationNode::newForall( $4 );
1569 }
1570 ;
1571
1572declaration_qualifier_list:
1573 storage_class_list
1574 | type_qualifier_list storage_class_list // remaining OBSOLESCENT (see 2 )
1575 { $$ = $1->addQualifiers( $2 ); }
1576 | declaration_qualifier_list type_qualifier_list storage_class_list
1577 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1578 ;
1579
1580storage_class_list:
1581 // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
1582 // only one of each is specified, except for inline, which can appear with the others.
1583 //
1584 // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
1585 // specifiers in a declaration.
1586 storage_class
1587 | storage_class_list storage_class
1588 { $$ = $1->addQualifiers( $2 ); }
1589 ;
1590
1591storage_class:
1592 EXTERN
1593 { $$ = DeclarationNode::newStorageClass( Type::Extern ); }
1594 | STATIC
1595 { $$ = DeclarationNode::newStorageClass( Type::Static ); }
1596 | AUTO
1597 { $$ = DeclarationNode::newStorageClass( Type::Auto ); }
1598 | REGISTER
1599 { $$ = DeclarationNode::newStorageClass( Type::Register ); }
1600 | THREADLOCAL // C11
1601 { $$ = DeclarationNode::newStorageClass( Type::Threadlocal ); }
1602 // Put function specifiers here to simplify parsing rules, but separate them semantically.
1603 | INLINE // C99
1604 { $$ = DeclarationNode::newFuncSpecifier( Type::Inline ); }
1605 | FORTRAN // C99
1606 { $$ = DeclarationNode::newFuncSpecifier( Type::Fortran ); }
1607 | NORETURN // C11
1608 { $$ = DeclarationNode::newFuncSpecifier( Type::Noreturn ); }
1609 ;
1610
1611basic_type_name:
1612 VOID
1613 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1614 | BOOL // C99
1615 { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
1616 | CHAR
1617 { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
1618 | INT
1619 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
1620 | INT128
1621 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int128 ); }
1622 | FLOAT
1623 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
1624 | FLOAT80
1625 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float80 ); }
1626 | FLOAT128
1627 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float128 ); }
1628 | DOUBLE
1629 { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
1630 | COMPLEX // C99
1631 { $$ = DeclarationNode::newComplexType( DeclarationNode::Complex ); }
1632 | IMAGINARY // C99
1633 { $$ = DeclarationNode::newComplexType( DeclarationNode::Imaginary ); }
1634 | SIGNED
1635 { $$ = DeclarationNode::newSignedNess( DeclarationNode::Signed ); }
1636 | UNSIGNED
1637 { $$ = DeclarationNode::newSignedNess( DeclarationNode::Unsigned ); }
1638 | SHORT
1639 { $$ = DeclarationNode::newLength( DeclarationNode::Short ); }
1640 | LONG
1641 { $$ = DeclarationNode::newLength( DeclarationNode::Long ); }
1642 | ZERO_T
1643 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Zero ); }
1644 | ONE_T
1645 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::One ); }
1646 | VALIST // GCC, __builtin_va_list
1647 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Valist ); }
1648 ;
1649
1650basic_declaration_specifier:
1651 // A semantic check is necessary for conflicting storage classes.
1652 basic_type_specifier
1653 | declaration_qualifier_list basic_type_specifier
1654 { $$ = $2->addQualifiers( $1 ); }
1655 | basic_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1656 { $$ = $1->addQualifiers( $2 ); }
1657 | basic_declaration_specifier storage_class type_qualifier_list
1658 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1659 | basic_declaration_specifier storage_class basic_type_specifier
1660 { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
1661 ;
1662
1663basic_type_specifier:
1664 direct_type
1665 | type_qualifier_list_opt indirect_type type_qualifier_list_opt
1666 { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
1667 ;
1668
1669direct_type:
1670 // A semantic check is necessary for conflicting type qualifiers.
1671 basic_type_name
1672 | type_qualifier_list basic_type_name
1673 { $$ = $2->addQualifiers( $1 ); }
1674 | direct_type type_qualifier
1675 { $$ = $1->addQualifiers( $2 ); }
1676 | direct_type basic_type_name
1677 { $$ = $1->addType( $2 ); }
1678 ;
1679
1680indirect_type:
1681 TYPEOF '(' type ')' // GCC: typeof(x) y;
1682 { $$ = $3; }
1683 | TYPEOF '(' comma_expression ')' // GCC: typeof(a+b) y;
1684 { $$ = DeclarationNode::newTypeof( $3 ); }
1685 | ATTR_TYPEGENname '(' type ')' // CFA: e.g., @type(x) y;
1686 { $$ = DeclarationNode::newAttr( $1, $3 ); }
1687 | ATTR_TYPEGENname '(' comma_expression ')' // CFA: e.g., @type(a+b) y;
1688 { $$ = DeclarationNode::newAttr( $1, $3 ); }
1689 ;
1690
1691sue_declaration_specifier: // struct, union, enum + storage class + type specifier
1692 sue_type_specifier
1693 | declaration_qualifier_list sue_type_specifier
1694 { $$ = $2->addQualifiers( $1 ); }
1695 | sue_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1696 { $$ = $1->addQualifiers( $2 ); }
1697 | sue_declaration_specifier storage_class type_qualifier_list
1698 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1699 ;
1700
1701sue_type_specifier: // struct, union, enum + type specifier
1702 elaborated_type
1703 | type_qualifier_list
1704 { if ( $1->type != nullptr && $1->type->forall ) forall = true; } // remember generic type
1705 elaborated_type
1706 { $$ = $3->addQualifiers( $1 ); }
1707 | sue_type_specifier type_qualifier
1708 { $$ = $1->addQualifiers( $2 ); }
1709 ;
1710
1711sue_declaration_specifier_nobody: // struct, union, enum - {...} + storage class + type specifier
1712 sue_type_specifier_nobody
1713 | declaration_qualifier_list sue_type_specifier_nobody
1714 { $$ = $2->addQualifiers( $1 ); }
1715 | sue_declaration_specifier_nobody storage_class // remaining OBSOLESCENT (see 2)
1716 { $$ = $1->addQualifiers( $2 ); }
1717 | sue_declaration_specifier_nobody storage_class type_qualifier_list
1718 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1719 ;
1720
1721sue_type_specifier_nobody: // struct, union, enum - {...} + type specifier
1722 elaborated_type_nobody
1723 | type_qualifier_list elaborated_type_nobody
1724 { $$ = $2->addQualifiers( $1 ); }
1725 | sue_type_specifier_nobody type_qualifier
1726 { $$ = $1->addQualifiers( $2 ); }
1727 ;
1728
1729type_declaration_specifier:
1730 type_type_specifier
1731 | declaration_qualifier_list type_type_specifier
1732 { $$ = $2->addQualifiers( $1 ); }
1733 | type_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1734 { $$ = $1->addQualifiers( $2 ); }
1735 | type_declaration_specifier storage_class type_qualifier_list
1736 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1737 ;
1738
1739type_type_specifier: // typedef types
1740 type_name
1741 | type_qualifier_list type_name
1742 { $$ = $2->addQualifiers( $1 ); }
1743 | type_type_specifier type_qualifier
1744 { $$ = $1->addQualifiers( $2 ); }
1745 ;
1746
1747type_name:
1748 TYPEDEFname
1749 { $$ = DeclarationNode::newFromTypedef( $1 ); }
1750 | '.' TYPEDEFname
1751 { $$ = DeclarationNode::newFromTypedef( $2 ); } // FIX ME
1752 | type_name '.' TYPEDEFname
1753 { $$ = DeclarationNode::newFromTypedef( $3 ); } // FIX ME
1754 | typegen_name
1755 | '.' typegen_name
1756 { $$ = $2; } // FIX ME
1757 | type_name '.' typegen_name
1758 { $$ = $3; } // FIX ME
1759 ;
1760
1761typegen_name: // CFA
1762 TYPEGENname '(' ')'
1763 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
1764 | TYPEGENname '(' type_list ')'
1765 { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
1766 ;
1767
1768elaborated_type: // struct, union, enum
1769 aggregate_type
1770 | enum_type
1771 ;
1772
1773elaborated_type_nobody: // struct, union, enum - {...}
1774 aggregate_type_nobody
1775 | enum_type_nobody
1776 ;
1777
1778aggregate_type: // struct, union
1779 aggregate_key attribute_list_opt '{' field_declaration_list '}'
1780 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), nullptr, $4, true )->addQualifiers( $2 ); }
1781 | aggregate_key attribute_list_opt no_attr_identifier_or_type_name
1782 {
1783 typedefTable.makeTypedef( *$3 ); // create typedef
1784 if ( forall ) typedefTable.changeKind( *$3, TypedefTable::TG ); // possibly update
1785 forall = false; // reset
1786 }
1787 '{' field_declaration_list '}'
1788 { $$ = DeclarationNode::newAggregate( $1, $3, nullptr, $6, true )->addQualifiers( $2 ); }
1789 | aggregate_key attribute_list_opt '(' type_list ')' '{' field_declaration_list '}' // CFA
1790 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), $4, $7, false )->addQualifiers( $2 ); }
1791 | aggregate_type_nobody
1792 ;
1793
1794aggregate_type_nobody: // struct, union - {...}
1795 aggregate_key attribute_list_opt no_attr_identifier
1796 {
1797 typedefTable.makeTypedef( *$3 );
1798 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1799 }
1800 | aggregate_key attribute_list_opt TYPEDEFname
1801 {
1802 typedefTable.makeTypedef( *$3 );
1803 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1804 }
1805 | aggregate_key attribute_list_opt typegen_name // CFA
1806 { $$ = $3->addQualifiers( $2 ); }
1807 ;
1808
1809aggregate_key:
1810 STRUCT
1811 { $$ = DeclarationNode::Struct; }
1812 | UNION
1813 { $$ = DeclarationNode::Union; }
1814 | COROUTINE
1815 { $$ = DeclarationNode::Coroutine; }
1816 | MONITOR
1817 { $$ = DeclarationNode::Monitor; }
1818 | THREAD
1819 { $$ = DeclarationNode::Thread; }
1820 ;
1821
1822field_declaration_list:
1823 // empty
1824 { $$ = nullptr; }
1825 | field_declaration_list field_declaration
1826 { $$ = $1 ? $1->appendList( $2 ) : $2; }
1827 ;
1828
1829field_declaration:
1830 cfa_field_declaring_list ';' // CFA, new style field declaration
1831 | EXTENSION cfa_field_declaring_list ';' // GCC
1832 {
1833 distExt( $2 ); // mark all fields in list
1834 $$ = $2;
1835 }
1836 | type_specifier field_declaring_list ';'
1837 {
1838 $$ = distAttr( $1, $2 ); }
1839 | EXTENSION type_specifier field_declaring_list ';' // GCC
1840 {
1841 distExt( $3 ); // mark all fields in list
1842 $$ = distAttr( $2, $3 );
1843 }
1844 ;
1845
1846cfa_field_declaring_list: // CFA, new style field declaration
1847 cfa_abstract_declarator_tuple // CFA, no field name
1848 | cfa_abstract_declarator_tuple no_attr_identifier_or_type_name
1849 { $$ = $1->addName( $2 ); }
1850 | cfa_field_declaring_list ',' no_attr_identifier_or_type_name
1851 { $$ = $1->appendList( $1->cloneType( $3 ) ); }
1852 | cfa_field_declaring_list ',' // CFA, no field name
1853 { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1854 ;
1855
1856field_declaring_list:
1857 field_declarator
1858 | field_declaring_list ',' attribute_list_opt field_declarator
1859 { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
1860 ;
1861
1862field_declarator:
1863 // empty
1864 { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
1865 // '@'
1866 // { $$ = DeclarationNode::newName( new string( DeclarationNode::anonymous.newName() ) ); } // CFA, no field name
1867 | bit_subrange_size // no field name
1868 { $$ = DeclarationNode::newBitfield( $1 ); }
1869 | variable_declarator bit_subrange_size_opt
1870 // A semantic check is required to ensure bit_subrange only appears on base type int.
1871 { $$ = $1->addBitfield( $2 ); }
1872 | variable_type_redeclarator bit_subrange_size_opt
1873 // A semantic check is required to ensure bit_subrange only appears on base type int.
1874 { $$ = $1->addBitfield( $2 ); }
1875 | variable_abstract_declarator // CFA, no field name
1876 ;
1877
1878bit_subrange_size_opt:
1879 // empty
1880 { $$ = nullptr; }
1881 | bit_subrange_size
1882 { $$ = $1; }
1883 ;
1884
1885bit_subrange_size:
1886 ':' constant_expression
1887 { $$ = $2; }
1888 ;
1889
1890enum_type: // enum
1891 ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
1892 { $$ = DeclarationNode::newEnum( new string( DeclarationNode::anonymous.newName() ), $4, true )->addQualifiers( $2 ); }
1893 | ENUM attribute_list_opt no_attr_identifier_or_type_name
1894 { typedefTable.makeTypedef( *$3 ); }
1895 '{' enumerator_list comma_opt '}'
1896 { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
1897 | enum_type_nobody
1898 ;
1899
1900enum_type_nobody: // enum - {...}
1901 ENUM attribute_list_opt no_attr_identifier_or_type_name
1902 {
1903 typedefTable.makeTypedef( *$3 );
1904 $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 );
1905 }
1906 ;
1907
1908enumerator_list:
1909 no_attr_identifier_or_type_name enumerator_value_opt
1910 { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
1911 | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
1912 { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1913 ;
1914
1915enumerator_value_opt:
1916 // empty
1917 { $$ = nullptr; }
1918 | '=' constant_expression
1919 { $$ = $2; }
1920 ;
1921
1922// Minimum of one parameter after which ellipsis is allowed only at the end.
1923
1924cfa_parameter_type_list_opt: // CFA
1925 // empty
1926 { $$ = nullptr; }
1927 | cfa_parameter_type_list
1928 ;
1929
1930cfa_parameter_type_list: // CFA, abstract + real
1931 cfa_abstract_parameter_list
1932 | cfa_parameter_list
1933 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
1934 { $$ = $1->appendList( $5 ); }
1935 | cfa_abstract_parameter_list pop ',' push ELLIPSIS
1936 { $$ = $1->addVarArgs(); }
1937 | cfa_parameter_list pop ',' push ELLIPSIS
1938 { $$ = $1->addVarArgs(); }
1939 ;
1940
1941cfa_parameter_list: // CFA
1942 // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
1943 // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
1944 cfa_parameter_declaration
1945 | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
1946 { $$ = $1->appendList( $5 ); }
1947 | cfa_parameter_list pop ',' push cfa_parameter_declaration
1948 { $$ = $1->appendList( $5 ); }
1949 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
1950 { $$ = $1->appendList( $5 )->appendList( $9 ); }
1951 ;
1952
1953cfa_abstract_parameter_list: // CFA, new & old style abstract
1954 cfa_abstract_parameter_declaration
1955 | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
1956 { $$ = $1->appendList( $5 ); }
1957 ;
1958
1959parameter_type_list_opt:
1960 // empty
1961 { $$ = nullptr; }
1962 | parameter_type_list
1963 ;
1964
1965parameter_type_list:
1966 parameter_list
1967 | parameter_list pop ',' push ELLIPSIS
1968 { $$ = $1->addVarArgs(); }
1969 ;
1970
1971parameter_list: // abstract + real
1972 abstract_parameter_declaration
1973 | parameter_declaration
1974 | parameter_list pop ',' push abstract_parameter_declaration
1975 { $$ = $1->appendList( $5 ); }
1976 | parameter_list pop ',' push parameter_declaration
1977 { $$ = $1->appendList( $5 ); }
1978 ;
1979
1980// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
1981// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
1982
1983cfa_parameter_declaration: // CFA, new & old style parameter declaration
1984 parameter_declaration
1985 | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initialize_opt
1986 { $$ = $1->addName( $2 ); }
1987 | cfa_abstract_tuple identifier_or_type_name default_initialize_opt
1988 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
1989 { $$ = $1->addName( $2 ); }
1990 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initialize_opt
1991 { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
1992 | cfa_function_specifier
1993 ;
1994
1995cfa_abstract_parameter_declaration: // CFA, new & old style parameter declaration
1996 abstract_parameter_declaration
1997 | cfa_identifier_parameter_declarator_no_tuple
1998 | cfa_abstract_tuple
1999 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2000 | type_qualifier_list cfa_abstract_tuple
2001 { $$ = $2->addQualifiers( $1 ); }
2002 | cfa_abstract_function
2003 ;
2004
2005parameter_declaration:
2006 // No SUE declaration in parameter list.
2007 declaration_specifier_nobody identifier_parameter_declarator default_initialize_opt
2008 {
2009 typedefTable.addToEnclosingScope( TypedefTable::ID );
2010 $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr );
2011 }
2012 | declaration_specifier_nobody type_parameter_redeclarator default_initialize_opt
2013 {
2014 typedefTable.addToEnclosingScope( TypedefTable::ID );
2015 $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr );
2016 }
2017 ;
2018
2019abstract_parameter_declaration:
2020 declaration_specifier_nobody default_initialize_opt
2021 { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
2022 | declaration_specifier_nobody abstract_parameter_declarator default_initialize_opt
2023 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2024 ;
2025
2026// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
2027// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
2028// identifiers. The ANSI-style parameter-list can redefine a typedef name.
2029
2030identifier_list: // K&R-style parameter list => no types
2031 no_attr_identifier
2032 { $$ = DeclarationNode::newName( $1 ); }
2033 | identifier_list ',' no_attr_identifier
2034 { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
2035 ;
2036
2037identifier_or_type_name:
2038 identifier
2039 | TYPEDEFname
2040 | TYPEGENname
2041 ;
2042
2043no_attr_identifier_or_type_name:
2044 no_attr_identifier
2045 | TYPEDEFname
2046 | TYPEGENname
2047 ;
2048
2049type_no_function: // sizeof, alignof, cast (constructor)
2050 cfa_abstract_declarator_tuple // CFA
2051 | type_specifier
2052 | type_specifier abstract_declarator
2053 { $$ = $2->addType( $1 ); }
2054 ;
2055
2056type: // typeof, assertion
2057 type_no_function
2058 | cfa_abstract_function // CFA
2059 ;
2060
2061initializer_opt:
2062 // empty
2063 { $$ = nullptr; }
2064 | '=' initializer
2065 { $$ = $2; }
2066 | '=' VOID
2067 { $$ = nullptr; }
2068 | ATassign initializer
2069 { $$ = $2->set_maybeConstructed( false ); }
2070 ;
2071
2072initializer:
2073 assignment_expression { $$ = new InitializerNode( $1 ); }
2074 | '{' initializer_list comma_opt '}' { $$ = new InitializerNode( $2, true ); }
2075 ;
2076
2077initializer_list:
2078 // empty
2079 { $$ = nullptr; }
2080 | initializer
2081 | designation initializer { $$ = $2->set_designators( $1 ); }
2082 | initializer_list ',' initializer { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
2083 | initializer_list ',' designation initializer
2084 { $$ = (InitializerNode *)( $1->set_last( $4->set_designators( $3 ) ) ); }
2085 ;
2086
2087// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
2088// '=' to separator the designator from the initializer value, as in:
2089//
2090// int x[10] = { [1] = 3 };
2091//
2092// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this case, CFA
2093// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
2094// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
2095// shift/reduce conflicts
2096
2097designation:
2098 designator_list ':' // C99, CFA uses ":" instead of "="
2099 | no_attr_identifier ':' // GCC, field name
2100 { $$ = new ExpressionNode( build_varref( $1 ) ); }
2101 ;
2102
2103designator_list: // C99
2104 designator
2105 | designator_list designator
2106 { $$ = (ExpressionNode *)( $1->set_last( $2 ) ); }
2107 //| designator_list designator { $$ = new ExpressionNode( $1, $2 ); }
2108 ;
2109
2110designator:
2111 '.' no_attr_identifier // C99, field name
2112 { $$ = new ExpressionNode( build_varref( $2 ) ); }
2113 | '[' push assignment_expression pop ']' // C99, single array element
2114 // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
2115 { $$ = $3; }
2116 | '[' push subrange pop ']' // CFA, multiple array elements
2117 { $$ = $3; }
2118 | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
2119 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $3 ), maybeMoveBuild< Expression >( $5 ) ) ); }
2120 | '.' '[' push field_list pop ']' // CFA, tuple field selector
2121 { $$ = $4; }
2122 ;
2123
2124// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
2125// rather than an object-oriented type system. This required four groups of extensions:
2126//
2127// Overloading: function, data, and operator identifiers may be overloaded.
2128//
2129// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
2130// and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
2131// definitions of new types. Type declarations with storage class "extern" provide opaque types.
2132//
2133// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
2134// site. A polymorphic function is not a template; it is a function, with an address and a type.
2135//
2136// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
2137// types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
2138// hierarchies. Unlike classes, they can define relationships between types. Assertions declare that a type or
2139// types provide the operations declared by a specification. Assertions are normally used to declare requirements
2140// on type arguments of polymorphic functions.
2141
2142type_parameter_list: // CFA
2143 type_parameter
2144 { $$ = $1; }
2145 | type_parameter_list ',' type_parameter
2146 { $$ = $1->appendList( $3 ); }
2147 ;
2148
2149type_initializer_opt: // CFA
2150 // empty
2151 { $$ = nullptr; }
2152 | '=' type
2153 { $$ = $2; }
2154 ;
2155
2156type_parameter: // CFA
2157 type_class no_attr_identifier_or_type_name
2158 { typedefTable.addToEnclosingScope( *$2, TypedefTable::TD ); }
2159 type_initializer_opt assertion_list_opt
2160 { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2161 | type_specifier identifier_parameter_declarator
2162 ;
2163
2164type_class: // CFA
2165 OTYPE
2166 { $$ = DeclarationNode::Otype; }
2167 | DTYPE
2168 { $$ = DeclarationNode::Dtype; }
2169 | FTYPE
2170 { $$ = DeclarationNode::Ftype; }
2171 | TTYPE
2172 { $$ = DeclarationNode::Ttype; }
2173 ;
2174
2175assertion_list_opt: // CFA
2176 // empty
2177 { $$ = nullptr; }
2178 | assertion_list_opt assertion
2179 { $$ = $1 ? $1->appendList( $2 ) : $2; }
2180 ;
2181
2182assertion: // CFA
2183 '|' no_attr_identifier_or_type_name '(' type_list ')'
2184 {
2185 typedefTable.openTrait( *$2 );
2186 $$ = DeclarationNode::newTraitUse( $2, $4 );
2187 }
2188 | '|' '{' push trait_declaration_list '}'
2189 { $$ = $4; }
2190 | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list '}' '(' type_list ')'
2191 { $$ = nullptr; }
2192 ;
2193
2194type_list: // CFA
2195 type
2196 { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( $1 ) ) ); }
2197 | assignment_expression
2198 | type_list ',' type
2199 { $$ = (ExpressionNode *)( $1->set_last( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ) ) ); }
2200 | type_list ',' assignment_expression
2201 { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
2202 ;
2203
2204type_declaring_list: // CFA
2205 OTYPE type_declarator
2206 { $$ = $2; }
2207 | storage_class_list OTYPE type_declarator
2208 { $$ = $3->addQualifiers( $1 ); }
2209 | type_declaring_list ',' type_declarator
2210 { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
2211 ;
2212
2213type_declarator: // CFA
2214 type_declarator_name assertion_list_opt
2215 { $$ = $1->addAssertions( $2 ); }
2216 | type_declarator_name assertion_list_opt '=' type
2217 { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2218 ;
2219
2220type_declarator_name: // CFA
2221 no_attr_identifier_or_type_name
2222 {
2223 typedefTable.addToEnclosingScope( *$1, TypedefTable::TD );
2224 $$ = DeclarationNode::newTypeDecl( $1, 0 );
2225 }
2226 | no_attr_identifier_or_type_name '(' push type_parameter_list pop ')'
2227 {
2228 typedefTable.addToEnclosingScope( *$1, TypedefTable::TG );
2229 $$ = DeclarationNode::newTypeDecl( $1, $4 );
2230 }
2231 ;
2232
2233trait_specifier: // CFA
2234 TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{' '}'
2235 {
2236 typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
2237 $$ = DeclarationNode::newTrait( $2, $5, 0 );
2238 }
2239 | TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{'
2240 {
2241 typedefTable.enterTrait( *$2 );
2242 typedefTable.enterScope();
2243 }
2244 trait_declaration_list '}'
2245 {
2246 typedefTable.leaveTrait();
2247 typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
2248 $$ = DeclarationNode::newTrait( $2, $5, $10 );
2249 }
2250 ;
2251
2252trait_declaration_list: // CFA
2253 trait_declaration
2254 | trait_declaration_list push trait_declaration
2255 { $$ = $1->appendList( $3 ); }
2256 ;
2257
2258trait_declaration: // CFA
2259 cfa_trait_declaring_list pop ';'
2260 | trait_declaring_list pop ';'
2261 ;
2262
2263cfa_trait_declaring_list: // CFA
2264 cfa_variable_specifier
2265 {
2266 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2267 $$ = $1;
2268 }
2269 | cfa_function_specifier
2270 {
2271 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2272 $$ = $1;
2273 }
2274 | cfa_trait_declaring_list pop ',' push identifier_or_type_name
2275 {
2276 typedefTable.addToEnclosingScope2( *$5, TypedefTable::ID );
2277 $$ = $1->appendList( $1->cloneType( $5 ) );
2278 }
2279 ;
2280
2281trait_declaring_list: // CFA
2282 type_specifier declarator
2283 {
2284 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2285 $$ = $2->addType( $1 );
2286 }
2287 | trait_declaring_list pop ',' push declarator
2288 {
2289 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2290 $$ = $1->appendList( $1->cloneBaseType( $5 ) );
2291 }
2292 ;
2293
2294//***************************** EXTERNAL DEFINITIONS *****************************
2295
2296translation_unit:
2297 // empty
2298 {} // empty input file
2299 | external_definition_list
2300 { parseTree = parseTree ? parseTree->appendList( $1 ) : $1; }
2301 ;
2302
2303external_definition_list:
2304 external_definition
2305 | external_definition_list push external_definition
2306 { $$ = $1 ? $1->appendList( $3 ) : $3; }
2307 ;
2308
2309external_definition_list_opt:
2310 // empty
2311 { $$ = nullptr; }
2312 | external_definition_list
2313 ;
2314
2315external_definition:
2316 declaration
2317 | external_function_definition
2318 | ASM '(' string_literal ')' ';' // GCC, global assembler statement
2319 {
2320 $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asmstmt( false, $3, 0 ) ) );
2321 }
2322 | EXTERN STRINGliteral // C++-style linkage specifier
2323 {
2324 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
2325 linkage = LinkageSpec::linkageUpdate( linkage, $2 );
2326 }
2327 '{' external_definition_list_opt '}'
2328 {
2329 linkage = linkageStack.top();
2330 linkageStack.pop();
2331 $$ = $5;
2332 }
2333 | EXTENSION external_definition // GCC, multiple __extension__ allowed, meaning unknown
2334 {
2335 distExt( $2 ); // mark all fields in list
2336 $$ = $2;
2337 }
2338 ;
2339
2340external_function_definition:
2341 function_definition
2342 // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
2343 // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2344 // Parsing is possible because function_definition does not appear in the context of an expression (nested
2345 // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2346 // declaration must still have a type_specifier. OBSOLESCENT (see 1)
2347 | function_declarator compound_statement
2348 {
2349 typedefTable.addToEnclosingScope( TypedefTable::ID );
2350 typedefTable.leaveScope();
2351 $$ = $1->addFunctionBody( $2 );
2352 }
2353 | KR_function_declarator KR_declaration_list_opt compound_statement
2354 {
2355 typedefTable.addToEnclosingScope( TypedefTable::ID );
2356 typedefTable.leaveScope();
2357 $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 );
2358 }
2359 ;
2360
2361with_clause_opt:
2362 // empty
2363 { $$ = nullptr; } // FIX ME
2364 | WITH '(' tuple_expression_list ')'
2365 { $$ = nullptr; } // FIX ME
2366 ;
2367
2368function_definition:
2369 cfa_function_declaration with_clause_opt compound_statement // CFA
2370 {
2371 typedefTable.addToEnclosingScope( TypedefTable::ID );
2372 typedefTable.leaveScope();
2373 $$ = $1->addFunctionBody( $3 );
2374 }
2375 | declaration_specifier function_declarator with_clause_opt compound_statement
2376 {
2377 typedefTable.addToEnclosingScope( TypedefTable::ID );
2378 typedefTable.leaveScope();
2379 $$ = $2->addFunctionBody( $4 )->addType( $1 );
2380 }
2381 | type_qualifier_list function_declarator with_clause_opt compound_statement
2382 {
2383 typedefTable.addToEnclosingScope( TypedefTable::ID );
2384 typedefTable.leaveScope();
2385 $$ = $2->addFunctionBody( $4 )->addQualifiers( $1 );
2386 }
2387 | declaration_qualifier_list function_declarator with_clause_opt compound_statement
2388 {
2389 typedefTable.addToEnclosingScope( TypedefTable::ID );
2390 typedefTable.leaveScope();
2391 $$ = $2->addFunctionBody( $4 )->addQualifiers( $1 );
2392 }
2393 | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
2394 {
2395 typedefTable.addToEnclosingScope( TypedefTable::ID );
2396 typedefTable.leaveScope();
2397 $$ = $3->addFunctionBody( $5 )->addQualifiers( $2 )->addQualifiers( $1 );
2398 }
2399
2400 // Old-style K&R function definition, OBSOLESCENT (see 4)
2401 | declaration_specifier KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
2402 {
2403 typedefTable.addToEnclosingScope( TypedefTable::ID );
2404 typedefTable.leaveScope();
2405 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5 )->addType( $1 );
2406 }
2407 | type_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
2408 {
2409 typedefTable.addToEnclosingScope( TypedefTable::ID );
2410 typedefTable.leaveScope();
2411 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5 )->addQualifiers( $1 );
2412 }
2413
2414 // Old-style K&R function definition with "implicit int" type_specifier, OBSOLESCENT (see 4)
2415 | declaration_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
2416 {
2417 typedefTable.addToEnclosingScope( TypedefTable::ID );
2418 typedefTable.leaveScope();
2419 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5 )->addQualifiers( $1 );
2420 }
2421 | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
2422 {
2423 typedefTable.addToEnclosingScope( TypedefTable::ID );
2424 typedefTable.leaveScope();
2425 $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6 )->addQualifiers( $2 )->addQualifiers( $1 );
2426 }
2427 ;
2428
2429declarator:
2430 variable_declarator
2431 | variable_type_redeclarator
2432 | function_declarator
2433 ;
2434
2435subrange:
2436 constant_expression '~' constant_expression // CFA, integer subrange
2437 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
2438 ;
2439
2440asm_name_opt: // GCC
2441 // empty
2442 { $$ = nullptr; }
2443 | ASM '(' string_literal ')' attribute_list_opt
2444 {
2445 DeclarationNode * name = new DeclarationNode();
2446 name->asmName = $3;
2447 $$ = name->addQualifiers( $5 );
2448 }
2449 ;
2450
2451attribute_list_opt: // GCC
2452 // empty
2453 { $$ = nullptr; }
2454 | attribute_list
2455 ;
2456
2457attribute_list: // GCC
2458 attribute
2459 | attribute_list attribute
2460 { $$ = $2->addQualifiers( $1 ); }
2461 ;
2462
2463attribute: // GCC
2464 ATTRIBUTE '(' '(' attribute_name_list ')' ')'
2465 { $$ = $4; }
2466 ;
2467
2468attribute_name_list: // GCC
2469 attribute_name
2470 | attribute_name_list ',' attribute_name
2471 { $$ = $3->addQualifiers( $1 ); }
2472 ;
2473
2474attribute_name: // GCC
2475 // empty
2476 { $$ = nullptr; }
2477 | attr_name
2478 { $$ = DeclarationNode::newAttribute( $1 ); }
2479 | attr_name '(' argument_expression_list ')'
2480 { $$ = DeclarationNode::newAttribute( $1, $3 ); }
2481 ;
2482
2483attr_name: // GCC
2484 IDENTIFIER
2485 | quasi_keyword
2486 | TYPEDEFname
2487 | TYPEGENname
2488 | CONST
2489 { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
2490 ;
2491
2492// ============================================================================
2493// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2494// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2495// in:
2496//
2497// int (*f())[10] { ... };
2498// ... (*f())[3] += 1; // definition mimics usage
2499//
2500// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2501// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
2502// ============================================================================
2503
2504// ----------------------------------------------------------------------------
2505// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2506// to define a variable or function prototype, e.g.:
2507//
2508// valid declaration invalid definition
2509// ----------------- ------------------
2510// int f; int f {}
2511// int *f; int *f {}
2512// int f[10]; int f[10] {}
2513// int (*f)(int); int (*f)(int) {}
2514//
2515// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2516// variable_declarator and function_declarator.
2517// ----------------------------------------------------------------------------
2518
2519// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2520// declaring an array of functions versus a pointer to an array of functions.
2521
2522variable_declarator:
2523 paren_identifier attribute_list_opt
2524 { $$ = $1->addQualifiers( $2 ); }
2525 | variable_ptr
2526 | variable_array attribute_list_opt
2527 { $$ = $1->addQualifiers( $2 ); }
2528 | variable_function attribute_list_opt
2529 { $$ = $1->addQualifiers( $2 ); }
2530 ;
2531
2532paren_identifier:
2533 identifier
2534 {
2535 typedefTable.setNextIdentifier( *$1 );
2536 $$ = DeclarationNode::newName( $1 );
2537 }
2538 | '(' paren_identifier ')' // redundant parenthesis
2539 { $$ = $2; }
2540 ;
2541
2542variable_ptr:
2543 ptrref_operator variable_declarator
2544 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2545 | ptrref_operator type_qualifier_list variable_declarator
2546 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2547 | '(' variable_ptr ')' attribute_list_opt
2548 { $$ = $2->addQualifiers( $4 ); } // redundant parenthesis
2549 ;
2550
2551variable_array:
2552 paren_identifier array_dimension
2553 { $$ = $1->addArray( $2 ); }
2554 | '(' variable_ptr ')' array_dimension
2555 { $$ = $2->addArray( $4 ); }
2556 | '(' variable_array ')' multi_array_dimension // redundant parenthesis
2557 { $$ = $2->addArray( $4 ); }
2558 | '(' variable_array ')' // redundant parenthesis
2559 { $$ = $2; }
2560 ;
2561
2562variable_function:
2563 '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2564 { $$ = $2->addParamList( $6 ); }
2565 | '(' variable_function ')' // redundant parenthesis
2566 { $$ = $2; }
2567 ;
2568
2569// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2570// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2571// is the same scope. The pattern precludes returning arrays and functions versus pointers to arrays and functions.
2572
2573function_declarator:
2574 function_no_ptr attribute_list_opt
2575 { $$ = $1->addQualifiers( $2 ); }
2576 | function_ptr
2577 | function_array attribute_list_opt
2578 { $$ = $1->addQualifiers( $2 ); }
2579 ;
2580
2581function_no_ptr:
2582 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2583 { $$ = $1->addParamList( $4 ); }
2584 | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2585 { $$ = $2->addParamList( $6 ); }
2586 | '(' function_no_ptr ')' // redundant parenthesis
2587 { $$ = $2; }
2588 ;
2589
2590function_ptr:
2591 ptrref_operator function_declarator
2592 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2593 | ptrref_operator type_qualifier_list function_declarator
2594 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2595 | '(' function_ptr ')'
2596 { $$ = $2; }
2597 ;
2598
2599function_array:
2600 '(' function_ptr ')' array_dimension
2601 { $$ = $2->addArray( $4 ); }
2602 | '(' function_array ')' multi_array_dimension // redundant parenthesis
2603 { $$ = $2->addArray( $4 ); }
2604 | '(' function_array ')' // redundant parenthesis
2605 { $$ = $2; }
2606 ;
2607
2608// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
2609//
2610// f( a, b, c ) int a, *b, c[]; {}
2611//
2612// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
2613// returning arrays and functions versus pointers to arrays and functions.
2614
2615KR_function_declarator:
2616 KR_function_no_ptr
2617 | KR_function_ptr
2618 | KR_function_array
2619 ;
2620
2621KR_function_no_ptr:
2622 paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
2623 { $$ = $1->addIdList( $3 ); }
2624 | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
2625 { $$ = $2->addParamList( $6 ); }
2626 | '(' KR_function_no_ptr ')' // redundant parenthesis
2627 { $$ = $2; }
2628 ;
2629
2630KR_function_ptr:
2631 ptrref_operator KR_function_declarator
2632 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2633 | ptrref_operator type_qualifier_list KR_function_declarator
2634 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2635 | '(' KR_function_ptr ')'
2636 { $$ = $2; }
2637 ;
2638
2639KR_function_array:
2640 '(' KR_function_ptr ')' array_dimension
2641 { $$ = $2->addArray( $4 ); }
2642 | '(' KR_function_array ')' multi_array_dimension // redundant parenthesis
2643 { $$ = $2->addArray( $4 ); }
2644 | '(' KR_function_array ')' // redundant parenthesis
2645 { $$ = $2; }
2646 ;
2647
2648// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
2649//
2650// typedef int foo;
2651// {
2652// int foo; // redefine typedef name in new scope
2653// }
2654//
2655// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2656// and functions versus pointers to arrays and functions.
2657
2658variable_type_redeclarator:
2659 paren_type attribute_list_opt
2660 { $$ = $1->addQualifiers( $2 ); }
2661 | type_ptr
2662 | type_array attribute_list_opt
2663 { $$ = $1->addQualifiers( $2 ); }
2664 | type_function attribute_list_opt
2665 { $$ = $1->addQualifiers( $2 ); }
2666 ;
2667
2668paren_type:
2669 typedef
2670 | '(' paren_type ')'
2671 { $$ = $2; }
2672 ;
2673
2674type_ptr:
2675 ptrref_operator variable_type_redeclarator
2676 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2677 | ptrref_operator type_qualifier_list variable_type_redeclarator
2678 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2679 | '(' type_ptr ')' attribute_list_opt
2680 { $$ = $2->addQualifiers( $4 ); }
2681 ;
2682
2683type_array:
2684 paren_type array_dimension
2685 { $$ = $1->addArray( $2 ); }
2686 | '(' type_ptr ')' array_dimension
2687 { $$ = $2->addArray( $4 ); }
2688 | '(' type_array ')' multi_array_dimension // redundant parenthesis
2689 { $$ = $2->addArray( $4 ); }
2690 | '(' type_array ')' // redundant parenthesis
2691 { $$ = $2; }
2692 ;
2693
2694type_function:
2695 paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2696 { $$ = $1->addParamList( $4 ); }
2697 | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2698 { $$ = $2->addParamList( $6 ); }
2699 | '(' type_function ')' // redundant parenthesis
2700 { $$ = $2; }
2701 ;
2702
2703// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
2704// typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes
2705// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
2706// pointers to arrays and functions.
2707
2708identifier_parameter_declarator:
2709 paren_identifier attribute_list_opt
2710 { $$ = $1->addQualifiers( $2 ); }
2711 | '&' MUTEX paren_identifier attribute_list_opt
2712 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
2713 | identifier_parameter_ptr
2714 | identifier_parameter_array attribute_list_opt
2715 { $$ = $1->addQualifiers( $2 ); }
2716 | identifier_parameter_function attribute_list_opt
2717 { $$ = $1->addQualifiers( $2 ); }
2718 ;
2719
2720identifier_parameter_ptr:
2721 ptrref_operator identifier_parameter_declarator
2722 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2723 | ptrref_operator type_qualifier_list identifier_parameter_declarator
2724 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2725 | '(' identifier_parameter_ptr ')' attribute_list_opt
2726 { $$ = $2->addQualifiers( $4 ); }
2727 ;
2728
2729identifier_parameter_array:
2730 paren_identifier array_parameter_dimension
2731 { $$ = $1->addArray( $2 ); }
2732 | '(' identifier_parameter_ptr ')' array_dimension
2733 { $$ = $2->addArray( $4 ); }
2734 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2735 { $$ = $2->addArray( $4 ); }
2736 | '(' identifier_parameter_array ')' // redundant parenthesis
2737 { $$ = $2; }
2738 ;
2739
2740identifier_parameter_function:
2741 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2742 { $$ = $1->addParamList( $4 ); }
2743 | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2744 { $$ = $2->addParamList( $6 ); }
2745 | '(' identifier_parameter_function ')' // redundant parenthesis
2746 { $$ = $2; }
2747 ;
2748
2749// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2750// e.g.:
2751//
2752// typedef int foo;
2753// forall( otype T ) foo( T );
2754// int f( int foo ); // redefine typedef name in new scope
2755//
2756// and allows the C99 array options, which can only appear in a parameter list.
2757
2758type_parameter_redeclarator:
2759 typedef attribute_list_opt
2760 { $$ = $1->addQualifiers( $2 ); }
2761 | '&' MUTEX typedef attribute_list_opt
2762 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
2763 | type_parameter_ptr
2764 | type_parameter_array attribute_list_opt
2765 { $$ = $1->addQualifiers( $2 ); }
2766 | type_parameter_function attribute_list_opt
2767 { $$ = $1->addQualifiers( $2 ); }
2768 ;
2769
2770typedef:
2771 TYPEDEFname
2772 {
2773 typedefTable.setNextIdentifier( *$1 );
2774 $$ = DeclarationNode::newName( $1 );
2775 }
2776 | TYPEGENname
2777 {
2778 typedefTable.setNextIdentifier( *$1 );
2779 $$ = DeclarationNode::newName( $1 );
2780 }
2781 ;
2782
2783type_parameter_ptr:
2784 ptrref_operator type_parameter_redeclarator
2785 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2786 | ptrref_operator type_qualifier_list type_parameter_redeclarator
2787 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2788 | '(' type_parameter_ptr ')' attribute_list_opt
2789 { $$ = $2->addQualifiers( $4 ); }
2790 ;
2791
2792type_parameter_array:
2793 typedef array_parameter_dimension
2794 { $$ = $1->addArray( $2 ); }
2795 | '(' type_parameter_ptr ')' array_parameter_dimension
2796 { $$ = $2->addArray( $4 ); }
2797 ;
2798
2799type_parameter_function:
2800 typedef '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2801 { $$ = $1->addParamList( $4 ); }
2802 | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2803 { $$ = $2->addParamList( $6 ); }
2804 ;
2805
2806// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2807// which the type applies, e.g.:
2808//
2809// sizeof( int );
2810// sizeof( int * );
2811// sizeof( int [10] );
2812// sizeof( int (*)() );
2813// sizeof( int () );
2814//
2815// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2816// and functions versus pointers to arrays and functions.
2817
2818abstract_declarator:
2819 abstract_ptr
2820 | abstract_array attribute_list_opt
2821 { $$ = $1->addQualifiers( $2 ); }
2822 | abstract_function attribute_list_opt
2823 { $$ = $1->addQualifiers( $2 ); }
2824 ;
2825
2826abstract_ptr:
2827 ptrref_operator
2828 { $$ = DeclarationNode::newPointer( 0, $1 ); }
2829 | ptrref_operator type_qualifier_list
2830 { $$ = DeclarationNode::newPointer( $2, $1 ); }
2831 | ptrref_operator abstract_declarator
2832 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
2833 | ptrref_operator type_qualifier_list abstract_declarator
2834 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2835 | '(' abstract_ptr ')' attribute_list_opt
2836 { $$ = $2->addQualifiers( $4 ); }
2837 ;
2838
2839abstract_array:
2840 array_dimension
2841 | '(' abstract_ptr ')' array_dimension
2842 { $$ = $2->addArray( $4 ); }
2843 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
2844 { $$ = $2->addArray( $4 ); }
2845 | '(' abstract_array ')' // redundant parenthesis
2846 { $$ = $2; }
2847 ;
2848
2849abstract_function:
2850 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2851 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
2852 | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2853 { $$ = $2->addParamList( $6 ); }
2854 | '(' abstract_function ')' // redundant parenthesis
2855 { $$ = $2; }
2856 ;
2857
2858array_dimension:
2859 // Only the first dimension can be empty.
2860 '[' ']'
2861 { $$ = DeclarationNode::newArray( 0, 0, false ); }
2862 | '[' ']' multi_array_dimension
2863 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
2864 | multi_array_dimension
2865 ;
2866
2867multi_array_dimension:
2868 '[' push assignment_expression pop ']'
2869 { $$ = DeclarationNode::newArray( $3, 0, false ); }
2870 | '[' push '*' pop ']' // C99
2871 { $$ = DeclarationNode::newVarArray( 0 ); }
2872 | multi_array_dimension '[' push assignment_expression pop ']'
2873 { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2874 | multi_array_dimension '[' push '*' pop ']' // C99
2875 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2876 ;
2877
2878// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2879// identifier to which the type applies, e.g.:
2880//
2881// int f( int ); // not handled here
2882// int f( int * ); // abstract function-prototype parameter; no parameter name specified
2883// int f( int (*)() ); // abstract function-prototype parameter; no parameter name specified
2884// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
2885//
2886// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2887// and functions versus pointers to arrays and functions. In addition, the pattern handles the
2888// special meaning of parenthesis around a typedef name:
2889//
2890// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2891// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2892// not as redundant parentheses around the identifier."
2893//
2894// For example:
2895//
2896// typedef float T;
2897// int f( int ( T [5] ) ); // see abstract_parameter_declarator
2898// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
2899// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
2900// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
2901//
2902// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2903// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2904// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2905// functions.
2906
2907abstract_parameter_declarator:
2908 abstract_parameter_ptr
2909 | '&' MUTEX attribute_list_opt
2910 { $$ = DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf )->addQualifiers( $3 ); }
2911 | abstract_parameter_array attribute_list_opt
2912 { $$ = $1->addQualifiers( $2 ); }
2913 | abstract_parameter_function attribute_list_opt
2914 { $$ = $1->addQualifiers( $2 ); }
2915 ;
2916
2917abstract_parameter_ptr:
2918 ptrref_operator
2919 { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
2920 | ptrref_operator type_qualifier_list
2921 { $$ = DeclarationNode::newPointer( $2, $1 ); }
2922 | ptrref_operator abstract_parameter_declarator
2923 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
2924 | ptrref_operator type_qualifier_list abstract_parameter_declarator
2925 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
2926 | '(' abstract_parameter_ptr ')' attribute_list_opt
2927 { $$ = $2->addQualifiers( $4 ); }
2928 ;
2929
2930abstract_parameter_array:
2931 array_parameter_dimension
2932 | '(' abstract_parameter_ptr ')' array_parameter_dimension
2933 { $$ = $2->addArray( $4 ); }
2934 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2935 { $$ = $2->addArray( $4 ); }
2936 | '(' abstract_parameter_array ')' // redundant parenthesis
2937 { $$ = $2; }
2938 ;
2939
2940abstract_parameter_function:
2941 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2942 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
2943 | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2944 { $$ = $2->addParamList( $6 ); }
2945 | '(' abstract_parameter_function ')' // redundant parenthesis
2946 { $$ = $2; }
2947 ;
2948
2949array_parameter_dimension:
2950 // Only the first dimension can be empty or have qualifiers.
2951 array_parameter_1st_dimension
2952 | array_parameter_1st_dimension multi_array_dimension
2953 { $$ = $1->addArray( $2 ); }
2954 | multi_array_dimension
2955 ;
2956
2957// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
2958//
2959// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
2960// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
2961
2962array_parameter_1st_dimension:
2963 '[' ']'
2964 { $$ = DeclarationNode::newArray( 0, 0, false ); }
2965 // multi_array_dimension handles the '[' '*' ']' case
2966 | '[' push type_qualifier_list '*' pop ']' // remaining C99
2967 { $$ = DeclarationNode::newVarArray( $3 ); }
2968 | '[' push type_qualifier_list pop ']'
2969 { $$ = DeclarationNode::newArray( 0, $3, false ); }
2970 // multi_array_dimension handles the '[' assignment_expression ']' case
2971 | '[' push type_qualifier_list assignment_expression pop ']'
2972 { $$ = DeclarationNode::newArray( $4, $3, false ); }
2973 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
2974 { $$ = DeclarationNode::newArray( $5, $4, true ); }
2975 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
2976 { $$ = DeclarationNode::newArray( $5, $3, true ); }
2977 ;
2978
2979// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
2980//
2981// struct S {
2982// int;
2983// int *;
2984// int [10];
2985// int (*)();
2986// };
2987
2988variable_abstract_declarator:
2989 variable_abstract_ptr
2990 | variable_abstract_array attribute_list_opt
2991 { $$ = $1->addQualifiers( $2 ); }
2992 | variable_abstract_function attribute_list_opt
2993 { $$ = $1->addQualifiers( $2 ); }
2994 ;
2995
2996variable_abstract_ptr:
2997 ptrref_operator
2998 { $$ = DeclarationNode::newPointer( 0, $1 ); }
2999 | ptrref_operator type_qualifier_list
3000 { $$ = DeclarationNode::newPointer( $2, $1 ); }
3001 | ptrref_operator variable_abstract_declarator
3002 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3003 | ptrref_operator type_qualifier_list variable_abstract_declarator
3004 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3005 | '(' variable_abstract_ptr ')' attribute_list_opt
3006 { $$ = $2->addQualifiers( $4 ); }
3007 ;
3008
3009variable_abstract_array:
3010 array_dimension
3011 | '(' variable_abstract_ptr ')' array_dimension
3012 { $$ = $2->addArray( $4 ); }
3013 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
3014 { $$ = $2->addArray( $4 ); }
3015 | '(' variable_abstract_array ')' // redundant parenthesis
3016 { $$ = $2; }
3017 ;
3018
3019variable_abstract_function:
3020 '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3021 { $$ = $2->addParamList( $6 ); }
3022 | '(' variable_abstract_function ')' // redundant parenthesis
3023 { $$ = $2; }
3024 ;
3025
3026// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
3027// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
3028
3029cfa_identifier_parameter_declarator_tuple: // CFA
3030 cfa_identifier_parameter_declarator_no_tuple
3031 | cfa_abstract_tuple
3032 | type_qualifier_list cfa_abstract_tuple
3033 { $$ = $2->addQualifiers( $1 ); }
3034 ;
3035
3036cfa_identifier_parameter_declarator_no_tuple: // CFA
3037 cfa_identifier_parameter_ptr
3038 | cfa_identifier_parameter_array
3039 ;
3040
3041cfa_identifier_parameter_ptr: // CFA
3042 // No SUE declaration in parameter list.
3043 ptrref_operator type_specifier_nobody
3044 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3045 | type_qualifier_list ptrref_operator type_specifier_nobody
3046 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3047 | ptrref_operator cfa_abstract_function
3048 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3049 | type_qualifier_list ptrref_operator cfa_abstract_function
3050 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3051 | ptrref_operator cfa_identifier_parameter_declarator_tuple
3052 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3053 | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
3054 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3055 ;
3056
3057cfa_identifier_parameter_array: // CFA
3058 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
3059 // shift/reduce conflict with new-style empty (void) function return type.
3060 '[' ']' type_specifier_nobody
3061 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3062 | cfa_array_parameter_1st_dimension type_specifier_nobody
3063 { $$ = $2->addNewArray( $1 ); }
3064 | '[' ']' multi_array_dimension type_specifier_nobody
3065 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3066 | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
3067 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3068 | multi_array_dimension type_specifier_nobody
3069 { $$ = $2->addNewArray( $1 ); }
3070
3071 | '[' ']' cfa_identifier_parameter_ptr
3072 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3073 | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
3074 { $$ = $2->addNewArray( $1 ); }
3075 | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
3076 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3077 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
3078 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3079 | multi_array_dimension cfa_identifier_parameter_ptr
3080 { $$ = $2->addNewArray( $1 ); }
3081 ;
3082
3083cfa_array_parameter_1st_dimension:
3084 '[' push type_qualifier_list '*' pop ']' // remaining C99
3085 { $$ = DeclarationNode::newVarArray( $3 ); }
3086 | '[' push type_qualifier_list assignment_expression pop ']'
3087 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3088 | '[' push declaration_qualifier_list assignment_expression pop ']'
3089 // declaration_qualifier_list must be used because of shift/reduce conflict with
3090 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
3091 // appear in this context.
3092 { $$ = DeclarationNode::newArray( $4, $3, true ); }
3093 | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
3094 { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
3095 ;
3096
3097// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
3098// identifier to which the type applies, e.g.:
3099//
3100// [int] f( int ); // abstract variable parameter; no parameter name specified
3101// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
3102//
3103// These rules need LR(3):
3104//
3105// cfa_abstract_tuple identifier_or_type_name
3106// '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_type_list_opt ')'
3107//
3108// since a function return type can be syntactically identical to a tuple type:
3109//
3110// [int, int] t;
3111// [int, int] f( int );
3112//
3113// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
3114// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
3115// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
3116// duplicated when appearing with cfa_function_specifier.
3117
3118cfa_abstract_declarator_tuple: // CFA
3119 cfa_abstract_tuple
3120 | type_qualifier_list cfa_abstract_tuple
3121 { $$ = $2->addQualifiers( $1 ); }
3122 | cfa_abstract_declarator_no_tuple
3123 ;
3124
3125cfa_abstract_declarator_no_tuple: // CFA
3126 cfa_abstract_ptr
3127 | cfa_abstract_array
3128 ;
3129
3130cfa_abstract_ptr: // CFA
3131 ptrref_operator type_specifier
3132 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3133 | type_qualifier_list ptrref_operator type_specifier
3134 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3135 | ptrref_operator cfa_abstract_function
3136 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3137 | type_qualifier_list ptrref_operator cfa_abstract_function
3138 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3139 | ptrref_operator cfa_abstract_declarator_tuple
3140 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3141 | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
3142 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3143 ;
3144
3145cfa_abstract_array: // CFA
3146 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
3147 // empty (void) function return type.
3148 '[' ']' type_specifier
3149 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3150 | '[' ']' multi_array_dimension type_specifier
3151 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3152 | multi_array_dimension type_specifier
3153 { $$ = $2->addNewArray( $1 ); }
3154 | '[' ']' cfa_abstract_ptr
3155 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3156 | '[' ']' multi_array_dimension cfa_abstract_ptr
3157 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3158 | multi_array_dimension cfa_abstract_ptr
3159 { $$ = $2->addNewArray( $1 ); }
3160 ;
3161
3162cfa_abstract_tuple: // CFA
3163 '[' push cfa_abstract_parameter_list pop ']'
3164 { $$ = DeclarationNode::newTuple( $3 ); }
3165 ;
3166
3167cfa_abstract_function: // CFA
3168// '[' ']' '(' cfa_parameter_type_list_opt ')'
3169// { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
3170 cfa_abstract_tuple '(' push cfa_parameter_type_list_opt pop ')'
3171 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3172 | cfa_function_return '(' push cfa_parameter_type_list_opt pop ')'
3173 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3174 ;
3175
3176// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3177// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
3178//
3179// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3180// the declaration specifiers in a declaration is an obsolescent feature."
3181//
3182// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3183// prototype-format parameter type declarators) is an obsolescent feature."
3184//
3185// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3186// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
3187
3188//************************* MISCELLANEOUS ********************************
3189
3190comma_opt: // redundant comma
3191 // empty
3192 | ','
3193 ;
3194
3195default_initialize_opt:
3196 // empty
3197 { $$ = nullptr; }
3198 | '=' assignment_expression
3199 { $$ = $2; }
3200 ;
3201
3202%%
3203// ----end of grammar----
3204
3205// Local Variables: //
3206// mode: c++ //
3207// tab-width: 4 //
3208// compile-command: "make install" //
3209// End: //
Note: See TracBrowser for help on using the repository browser.