source: src/Parser/parser.yy@ 478b1d0

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 478b1d0 was bac5158, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

fix conflict

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