source: src/Parser/parser.yy@ db0fa7c

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 db0fa7c was ecb27a7, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

dtor operator syntax is now a void-typed expression

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