source: src/Parser/parser.yy@ c9c9fd7e

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 c9c9fd7e was 641c3d0, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add "= void" syntax to delete overloaded routine

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