source: src/Parser/parser.yy@ 3fe98b7

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 3fe98b7 was fbcde64, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

remove duplication in compound literal, support aggregate-type compound literals

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