source: src/Parser/parser.yy@ 796cea3

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 796cea3 was a7c90d4, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

change StorageClass to bitset, support _Thread_local as separate storage-class

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