source: src/Parser/parser.yy@ 65f9dec

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 65f9dec was 84d58c5, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

parse default values for generic type parameters and nested type names

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