source: src/Parser/parser.yy@ cd99ef1

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 cd99ef1 was dc2b4d6, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Discused updates to handler predicate parsing.

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