source: src/Parser/parser.yy@ 60401b76

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 60401b76 was d0ffed1, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

generate syntax error for SUE declarations in restricted scopes

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