source: src/Parser/parser.yy@ 1ae39f6e

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 1ae39f6e was 1efa1e1, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

update comments on waitfor precedence

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