source: src/Parser/parser.yy@ 4a58895

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 4a58895 was 67cf18c, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

implement default type arguments for generic types [closes #13]

  • Property mode set to 100644
File size: 110.6 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
[67cf18c]1608 TYPEGENname '(' ')'
1609 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
1610 | TYPEGENname '(' type_list ')'
[84d58c5]1611 { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
[4d51835]1612 ;
[51b73452]1613
[d0ffed1]1614elaborated_type: // struct, union, enum
[c0aa336]1615 aggregate_type
1616 | enum_type
[4d51835]1617 ;
[51b73452]1618
[d0ffed1]1619elaborated_type_nobody: // struct, union, enum - {...}
1620 aggregate_type_nobody
1621 | enum_type_nobody
1622 ;
1623
1624aggregate_type: // struct, union
[c0aa336]1625 aggregate_key attribute_list_opt '{' field_declaration_list '}'
[6f95000]1626 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), nullptr, $4, true )->addQualifiers( $2 ); }
[c0aa336]1627 | aggregate_key attribute_list_opt no_attr_identifier_or_type_name
[fdca7c6]1628 {
1629 typedefTable.makeTypedef( *$3 ); // create typedef
1630 if ( forall ) typedefTable.changeKind( *$3, TypedefTable::TG ); // possibly update
1631 forall = false; // reset
1632 }
[c0aa336]1633 '{' field_declaration_list '}'
1634 { $$ = DeclarationNode::newAggregate( $1, $3, nullptr, $6, true )->addQualifiers( $2 ); }
[84d58c5]1635 | aggregate_key attribute_list_opt '(' type_list ')' '{' field_declaration_list '}' // CFA
[6f95000]1636 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), $4, $7, false )->addQualifiers( $2 ); }
[d0ffed1]1637 | aggregate_type_nobody
1638 ;
1639
1640aggregate_type_nobody: // struct, union - {...}
[84d58c5]1641 aggregate_key attribute_list_opt no_attr_identifier
1642 {
1643 typedefTable.makeTypedef( *$3 );
1644 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1645 }
1646 | aggregate_key attribute_list_opt TYPEDEFname
[d0ffed1]1647 {
1648 typedefTable.makeTypedef( *$3 );
1649 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1650 }
[84d58c5]1651 | aggregate_key attribute_list_opt typegen_name // CFA
[c0aa336]1652 { $$ = $3->addQualifiers( $2 ); }
[4d51835]1653 ;
[51b73452]1654
1655aggregate_key:
[c0aa336]1656 STRUCT
[4d51835]1657 { $$ = DeclarationNode::Struct; }
[c0aa336]1658 | UNION
[4d51835]1659 { $$ = DeclarationNode::Union; }
[d3bc0ad]1660 | COROUTINE
[409433da]1661 { $$ = DeclarationNode::Coroutine; }
[d3bc0ad]1662 | MONITOR
[409433da]1663 { $$ = DeclarationNode::Monitor; }
[d3bc0ad]1664 | THREAD
[409433da]1665 { $$ = DeclarationNode::Thread; }
[4d51835]1666 ;
[51b73452]1667
1668field_declaration_list:
[5d125e4]1669 // empty
[58dd019]1670 { $$ = nullptr; }
[4d51835]1671 | field_declaration_list field_declaration
[a7741435]1672 { $$ = $1 ? $1->appendList( $2 ) : $2; }
[4d51835]1673 ;
[51b73452]1674
1675field_declaration:
[c0aa336]1676 cfa_field_declaring_list ';' // CFA, new style field declaration
1677 | EXTENSION cfa_field_declaring_list ';' // GCC
1678 {
1679 distExt( $2 ); // mark all fields in list
[8e9cbb2]1680 $$ = $2;
1681 }
[c0aa336]1682 | type_specifier field_declaring_list ';'
1683 {
1684 $$ = distAttr( $1, $2 ); }
1685 | EXTENSION type_specifier field_declaring_list ';' // GCC
1686 {
1687 distExt( $3 ); // mark all fields in list
1688 $$ = distAttr( $2, $3 );
1689 }
[4d51835]1690 ;
[b87a5ed]1691
[c0aa336]1692cfa_field_declaring_list: // CFA, new style field declaration
1693 cfa_abstract_declarator_tuple // CFA, no field name
1694 | cfa_abstract_declarator_tuple no_attr_identifier_or_type_name
[4d51835]1695 { $$ = $1->addName( $2 ); }
[c0aa336]1696 | cfa_field_declaring_list ',' no_attr_identifier_or_type_name
[4d51835]1697 { $$ = $1->appendList( $1->cloneType( $3 ) ); }
[c0aa336]1698 | cfa_field_declaring_list ',' // CFA, no field name
[4d51835]1699 { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1700 ;
[51b73452]1701
1702field_declaring_list:
[c0aa336]1703 field_declarator
[4d51835]1704 | field_declaring_list ',' attribute_list_opt field_declarator
[c0aa336]1705 { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
[4d51835]1706 ;
[51b73452]1707
1708field_declarator:
[4d51835]1709 // empty
1710 { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
[6f95000]1711 // '@'
1712 // { $$ = DeclarationNode::newName( new string( DeclarationNode::anonymous.newName() ) ); } // CFA, no field name
[4d51835]1713 | bit_subrange_size // no field name
1714 { $$ = DeclarationNode::newBitfield( $1 ); }
1715 | variable_declarator bit_subrange_size_opt
1716 // A semantic check is required to ensure bit_subrange only appears on base type int.
1717 { $$ = $1->addBitfield( $2 ); }
[c6b1105]1718 | variable_type_redeclarator bit_subrange_size_opt
[4d51835]1719 // A semantic check is required to ensure bit_subrange only appears on base type int.
1720 { $$ = $1->addBitfield( $2 ); }
1721 | variable_abstract_declarator // CFA, no field name
1722 ;
[51b73452]1723
1724bit_subrange_size_opt:
[4d51835]1725 // empty
[58dd019]1726 { $$ = nullptr; }
[4d51835]1727 | bit_subrange_size
1728 { $$ = $1; }
1729 ;
[51b73452]1730
1731bit_subrange_size:
[4d51835]1732 ':' constant_expression
1733 { $$ = $2; }
1734 ;
[51b73452]1735
[d0ffed1]1736enum_type: // enum
[c0aa336]1737 ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
[6f95000]1738 { $$ = DeclarationNode::newEnum( new string( DeclarationNode::anonymous.newName() ), $4, true )->addQualifiers( $2 ); }
[c0aa336]1739 | ENUM attribute_list_opt no_attr_identifier_or_type_name
[d0ffed1]1740 { typedefTable.makeTypedef( *$3 ); }
1741 '{' enumerator_list comma_opt '}'
1742 { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
1743 | enum_type_nobody
1744 ;
1745
1746enum_type_nobody: // enum - {...}
1747 ENUM attribute_list_opt no_attr_identifier_or_type_name
[45161b4d]1748 {
[c0aa336]1749 typedefTable.makeTypedef( *$3 );
[ca1a547]1750 $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 );
[45161b4d]1751 }
[4d51835]1752 ;
[51b73452]1753
1754enumerator_list:
[2871210]1755 no_attr_identifier_or_type_name enumerator_value_opt
[4d51835]1756 { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
[2871210]1757 | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
[4d51835]1758 { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1759 ;
[51b73452]1760
1761enumerator_value_opt:
[4d51835]1762 // empty
[58dd019]1763 { $$ = nullptr; }
[4d51835]1764 | '=' constant_expression
1765 { $$ = $2; }
1766 ;
[51b73452]1767
[c11e31c]1768// Minimum of one parameter after which ellipsis is allowed only at the end.
[51b73452]1769
[c0aa336]1770cfa_parameter_type_list_opt: // CFA
[4d51835]1771 // empty
[58dd019]1772 { $$ = nullptr; }
[c0aa336]1773 | cfa_parameter_type_list
[4d51835]1774 ;
[b87a5ed]1775
[c0aa336]1776cfa_parameter_type_list: // CFA, abstract + real
1777 cfa_abstract_parameter_list
1778 | cfa_parameter_list
1779 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
[4d51835]1780 { $$ = $1->appendList( $5 ); }
[c0aa336]1781 | cfa_abstract_parameter_list pop ',' push ELLIPSIS
[4d51835]1782 { $$ = $1->addVarArgs(); }
[c0aa336]1783 | cfa_parameter_list pop ',' push ELLIPSIS
[4d51835]1784 { $$ = $1->addVarArgs(); }
1785 ;
[b87a5ed]1786
[c0aa336]1787cfa_parameter_list: // CFA
1788 // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
1789 // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
1790 cfa_parameter_declaration
1791 | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
[4d51835]1792 { $$ = $1->appendList( $5 ); }
[c0aa336]1793 | cfa_parameter_list pop ',' push cfa_parameter_declaration
[4d51835]1794 { $$ = $1->appendList( $5 ); }
[c0aa336]1795 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
[4d51835]1796 { $$ = $1->appendList( $5 )->appendList( $9 ); }
1797 ;
[b87a5ed]1798
[c0aa336]1799cfa_abstract_parameter_list: // CFA, new & old style abstract
1800 cfa_abstract_parameter_declaration
1801 | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
[4d51835]1802 { $$ = $1->appendList( $5 ); }
1803 ;
[51b73452]1804
1805parameter_type_list_opt:
[4d51835]1806 // empty
[58dd019]1807 { $$ = nullptr; }
[4d51835]1808 | parameter_type_list
1809 ;
[51b73452]1810
1811parameter_type_list:
[4d51835]1812 parameter_list
1813 | parameter_list pop ',' push ELLIPSIS
1814 { $$ = $1->addVarArgs(); }
1815 ;
[b87a5ed]1816
1817parameter_list: // abstract + real
[4d51835]1818 abstract_parameter_declaration
1819 | parameter_declaration
1820 | parameter_list pop ',' push abstract_parameter_declaration
1821 { $$ = $1->appendList( $5 ); }
1822 | parameter_list pop ',' push parameter_declaration
1823 { $$ = $1->appendList( $5 ); }
1824 ;
[51b73452]1825
[de62360d]1826// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
[2871210]1827// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
[51b73452]1828
[c0aa336]1829cfa_parameter_declaration: // CFA, new & old style parameter declaration
[4d51835]1830 parameter_declaration
[c0aa336]1831 | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name assignment_opt
[4d51835]1832 { $$ = $1->addName( $2 ); }
[c0aa336]1833 | cfa_abstract_tuple identifier_or_type_name assignment_opt
1834 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
[4d51835]1835 { $$ = $1->addName( $2 ); }
[c0aa336]1836 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name assignment_opt
[4d51835]1837 { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
[c0aa336]1838 | cfa_function_specifier
[4d51835]1839 ;
[b87a5ed]1840
[c0aa336]1841cfa_abstract_parameter_declaration: // CFA, new & old style parameter declaration
[4d51835]1842 abstract_parameter_declaration
[c0aa336]1843 | cfa_identifier_parameter_declarator_no_tuple
1844 | cfa_abstract_tuple
1845 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
1846 | type_qualifier_list cfa_abstract_tuple
[4d51835]1847 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]1848 | cfa_abstract_function
[4d51835]1849 ;
[51b73452]1850
1851parameter_declaration:
[d0ffed1]1852 // No SUE declaration in parameter list.
1853 declaration_specifier_nobody identifier_parameter_declarator assignment_opt
[4d51835]1854 {
1855 typedefTable.addToEnclosingScope( TypedefTable::ID );
[a7741435]1856 $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr );
[4d51835]1857 }
[d0ffed1]1858 | declaration_specifier_nobody type_parameter_redeclarator assignment_opt
[4d51835]1859 {
1860 typedefTable.addToEnclosingScope( TypedefTable::ID );
[a7741435]1861 $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr );
[4d51835]1862 }
1863 ;
[51b73452]1864
1865abstract_parameter_declaration:
[d0ffed1]1866 declaration_specifier_nobody assignment_opt
[e7cc8cb]1867 { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
[d0ffed1]1868 | declaration_specifier_nobody abstract_parameter_declarator assignment_opt
[e7cc8cb]1869 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
[4d51835]1870 ;
[51b73452]1871
[c11e31c]1872// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
[de62360d]1873// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
1874// identifiers. The ANSI-style parameter-list can redefine a typedef name.
[51b73452]1875
[b87a5ed]1876identifier_list: // K&R-style parameter list => no types
[4d51835]1877 no_attr_identifier
1878 { $$ = DeclarationNode::newName( $1 ); }
1879 | identifier_list ',' no_attr_identifier
1880 { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
1881 ;
[51b73452]1882
[2871210]1883identifier_or_type_name:
[4d51835]1884 identifier
1885 | TYPEDEFname
1886 | TYPEGENname
1887 ;
[51b73452]1888
[2871210]1889no_attr_identifier_or_type_name:
[4d51835]1890 no_attr_identifier
1891 | TYPEDEFname
[721f17a]1892 | TYPEGENname
[4d51835]1893 ;
[b87a5ed]1894
[84d58c5]1895type_no_function: // sizeof, alignof, cast (constructor)
[c0aa336]1896 cfa_abstract_declarator_tuple // CFA
[4d51835]1897 | type_specifier
[c0aa336]1898 | type_specifier abstract_declarator
[4d51835]1899 { $$ = $2->addType( $1 ); }
1900 ;
[b87a5ed]1901
[84d58c5]1902type: // typeof, assertion
1903 type_no_function
[c0aa336]1904 | cfa_abstract_function // CFA
[4d51835]1905 ;
[51b73452]1906
1907initializer_opt:
[4d51835]1908 // empty
[58dd019]1909 { $$ = nullptr; }
[2871210]1910 | '=' initializer
1911 { $$ = $2; }
[097e2b0]1912 | ATassign initializer
[974906e2]1913 { $$ = $2->set_maybeConstructed( false ); }
[4d51835]1914 ;
[51b73452]1915
1916initializer:
[de62360d]1917 assignment_expression { $$ = new InitializerNode( $1 ); }
1918 | '{' initializer_list comma_opt '}' { $$ = new InitializerNode( $2, true ); }
[4d51835]1919 ;
[51b73452]1920
1921initializer_list:
[097e2b0]1922 // empty
[58dd019]1923 { $$ = nullptr; }
[097e2b0]1924 | initializer
[4d51835]1925 | designation initializer { $$ = $2->set_designators( $1 ); }
[1d4580a]1926 | initializer_list ',' initializer { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
[4d51835]1927 | initializer_list ',' designation initializer
[1d4580a]1928 { $$ = (InitializerNode *)( $1->set_last( $4->set_designators( $3 ) ) ); }
[4d51835]1929 ;
[b87a5ed]1930
[de62360d]1931// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
1932// '=' to separator the designator from the initializer value, as in:
[c11e31c]1933//
[b87a5ed]1934// int x[10] = { [1] = 3 };
[c11e31c]1935//
[de62360d]1936// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this case, CFA
1937// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
1938// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
1939// shift/reduce conflicts
[51b73452]1940
1941designation:
[4d51835]1942 designator_list ':' // C99, CFA uses ":" instead of "="
[84d58c5]1943 | no_attr_identifier ':' // GCC, field name
[d1625f8]1944 { $$ = new ExpressionNode( build_varref( $1 ) ); }
[4d51835]1945 ;
[51b73452]1946
[b87a5ed]1947designator_list: // C99
[4d51835]1948 designator
[2871210]1949 | designator_list designator
[1d4580a]1950 { $$ = (ExpressionNode *)( $1->set_last( $2 ) ); }
[d1625f8]1951 //| designator_list designator { $$ = new ExpressionNode( $1, $2 ); }
[4d51835]1952 ;
[51b73452]1953
1954designator:
[84d58c5]1955 '.' no_attr_identifier // C99, field name
[d1625f8]1956 { $$ = new ExpressionNode( build_varref( $2 ) ); }
[4d51835]1957 | '[' push assignment_expression pop ']' // C99, single array element
[de62360d]1958 // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
[d1625f8]1959 { $$ = $3; }
[4d51835]1960 | '[' push subrange pop ']' // CFA, multiple array elements
[d1625f8]1961 { $$ = $3; }
[4d51835]1962 | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
[d1625f8]1963 { $$ = new ExpressionNode( build_range( $3, $5 ) ); }
[4d51835]1964 | '.' '[' push field_list pop ']' // CFA, tuple field selector
[d1625f8]1965 { $$ = $4; }
[4d51835]1966 ;
[51b73452]1967
[de62360d]1968// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
1969// rather than an object-oriented type system. This required four groups of extensions:
[c11e31c]1970//
1971// Overloading: function, data, and operator identifiers may be overloaded.
1972//
[de62360d]1973// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
1974// and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
1975// definitions of new types. Type declarations with storage class "extern" provide opaque types.
[c11e31c]1976//
[de62360d]1977// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
1978// site. A polymorphic function is not a template; it is a function, with an address and a type.
[c11e31c]1979//
1980// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
[de62360d]1981// types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
1982// hierarchies. Unlike classes, they can define relationships between types. Assertions declare that a type or
1983// types provide the operations declared by a specification. Assertions are normally used to declare requirements
1984// on type arguments of polymorphic functions.
[c11e31c]1985
[b87a5ed]1986type_parameter_list: // CFA
[67cf18c]1987 type_parameter
1988 { $$ = $1; }
1989 | type_parameter_list ',' type_parameter
[4d51835]1990 { $$ = $1->appendList( $3 ); }
1991 ;
[b87a5ed]1992
[84d58c5]1993type_initializer_opt: // CFA
1994 // empty
1995 { $$ = nullptr; }
1996 | '=' type
1997 { $$ = $2; }
1998 ;
1999
[b87a5ed]2000type_parameter: // CFA
[2871210]2001 type_class no_attr_identifier_or_type_name
2002 { typedefTable.addToEnclosingScope( *$2, TypedefTable::TD ); }
[67cf18c]2003 type_initializer_opt assertion_list_opt
2004 { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
[4d51835]2005 | type_specifier identifier_parameter_declarator
2006 ;
[b87a5ed]2007
2008type_class: // CFA
[4040425]2009 OTYPE
[5b639ee]2010 { $$ = DeclarationNode::Otype; }
[4d51835]2011 | DTYPE
2012 { $$ = DeclarationNode::Dtype; }
[8f60f0b]2013 | FTYPE
2014 { $$ = DeclarationNode::Ftype; }
2015 | TTYPE
2016 { $$ = DeclarationNode::Ttype; }
[4d51835]2017 ;
[b87a5ed]2018
2019assertion_list_opt: // CFA
[4d51835]2020 // empty
[58dd019]2021 { $$ = nullptr; }
[4d51835]2022 | assertion_list_opt assertion
[a7741435]2023 { $$ = $1 ? $1->appendList( $2 ) : $2; }
[4d51835]2024 ;
[b87a5ed]2025
2026assertion: // CFA
[84d58c5]2027 '|' no_attr_identifier_or_type_name '(' type_list ')'
[4d51835]2028 {
[4040425]2029 typedefTable.openTrait( *$2 );
2030 $$ = DeclarationNode::newTraitUse( $2, $4 );
[4d51835]2031 }
[4040425]2032 | '|' '{' push trait_declaration_list '}'
[4d51835]2033 { $$ = $4; }
[84d58c5]2034 | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list '}' '(' type_list ')'
[58dd019]2035 { $$ = nullptr; }
[4d51835]2036 ;
[b87a5ed]2037
[84d58c5]2038type_list: // CFA
2039 type
[d1625f8]2040 { $$ = new ExpressionNode( build_typevalue( $1 ) ); }
[4d51835]2041 | assignment_expression
[84d58c5]2042 | type_list ',' type
[1d4580a]2043 { $$ = (ExpressionNode *)( $1->set_last( new ExpressionNode( build_typevalue( $3 ) ) ) ); }
[84d58c5]2044 | type_list ',' assignment_expression
[1d4580a]2045 { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
[4d51835]2046 ;
[b87a5ed]2047
2048type_declaring_list: // CFA
[4040425]2049 OTYPE type_declarator
[4d51835]2050 { $$ = $2; }
[4040425]2051 | storage_class_list OTYPE type_declarator
[4d51835]2052 { $$ = $3->addQualifiers( $1 ); }
2053 | type_declaring_list ',' type_declarator
[a7c90d4]2054 { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
[4d51835]2055 ;
[b87a5ed]2056
2057type_declarator: // CFA
[4d51835]2058 type_declarator_name assertion_list_opt
2059 { $$ = $1->addAssertions( $2 ); }
[84d58c5]2060 | type_declarator_name assertion_list_opt '=' type
[4d51835]2061 { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2062 ;
[b87a5ed]2063
2064type_declarator_name: // CFA
[2871210]2065 no_attr_identifier_or_type_name
[4d51835]2066 {
[de62360d]2067 typedefTable.addToEnclosingScope( *$1, TypedefTable::TD );
[4d51835]2068 $$ = DeclarationNode::newTypeDecl( $1, 0 );
2069 }
[84d58c5]2070 | no_attr_identifier_or_type_name '(' push type_parameter_list pop ')'
[4d51835]2071 {
[de62360d]2072 typedefTable.addToEnclosingScope( *$1, TypedefTable::TG );
[4d51835]2073 $$ = DeclarationNode::newTypeDecl( $1, $4 );
2074 }
2075 ;
[b87a5ed]2076
[4040425]2077trait_specifier: // CFA
2078 TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{' '}'
[4d51835]2079 {
[de62360d]2080 typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
[4040425]2081 $$ = DeclarationNode::newTrait( $2, $5, 0 );
[4d51835]2082 }
[4040425]2083 | TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{'
[4d51835]2084 {
[4040425]2085 typedefTable.enterTrait( *$2 );
[4d51835]2086 typedefTable.enterScope();
2087 }
[4040425]2088 trait_declaration_list '}'
[4d51835]2089 {
[4040425]2090 typedefTable.leaveTrait();
[de62360d]2091 typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
[4040425]2092 $$ = DeclarationNode::newTrait( $2, $5, $10 );
[4d51835]2093 }
2094 ;
[b87a5ed]2095
[84d58c5]2096trait_declaration_list: // CFA
[4040425]2097 trait_declaration
2098 | trait_declaration_list push trait_declaration
[4d51835]2099 { $$ = $1->appendList( $3 ); }
2100 ;
[b87a5ed]2101
[84d58c5]2102trait_declaration: // CFA
[c0aa336]2103 cfa_trait_declaring_list pop ';'
[4040425]2104 | trait_declaring_list pop ';'
[4d51835]2105 ;
[b87a5ed]2106
[c0aa336]2107cfa_trait_declaring_list: // CFA
2108 cfa_variable_specifier
[4d51835]2109 {
2110 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2111 $$ = $1;
2112 }
[c0aa336]2113 | cfa_function_specifier
[4d51835]2114 {
2115 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2116 $$ = $1;
2117 }
[c0aa336]2118 | cfa_trait_declaring_list pop ',' push identifier_or_type_name
[4d51835]2119 {
[de62360d]2120 typedefTable.addToEnclosingScope2( *$5, TypedefTable::ID );
[4d51835]2121 $$ = $1->appendList( $1->cloneType( $5 ) );
2122 }
2123 ;
[b87a5ed]2124
[4040425]2125trait_declaring_list: // CFA
[4d51835]2126 type_specifier declarator
2127 {
2128 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2129 $$ = $2->addType( $1 );
2130 }
[4040425]2131 | trait_declaring_list pop ',' push declarator
[4d51835]2132 {
2133 typedefTable.addToEnclosingScope2( TypedefTable::ID );
2134 $$ = $1->appendList( $1->cloneBaseType( $5 ) );
2135 }
2136 ;
[51b73452]2137
[c11e31c]2138//***************************** EXTERNAL DEFINITIONS *****************************
[51b73452]2139
2140translation_unit:
[4d51835]2141 // empty
2142 {} // empty input file
2143 | external_definition_list
[a7741435]2144 { parseTree = parseTree ? parseTree->appendList( $1 ) : $1; }
[4d51835]2145 ;
[51b73452]2146
2147external_definition_list:
[4d51835]2148 external_definition
2149 | external_definition_list push external_definition
[a7741435]2150 { $$ = $1 ? $1->appendList( $3 ) : $3; }
[4d51835]2151 ;
[51b73452]2152
2153external_definition_list_opt:
[4d51835]2154 // empty
[58dd019]2155 { $$ = nullptr; }
[4d51835]2156 | external_definition_list
2157 ;
[51b73452]2158
2159external_definition:
[4d51835]2160 declaration
2161 | external_function_definition
[e994912]2162 | ASM '(' string_literal ')' ';' // GCC, global assembler statement
2163 {
2164 $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asmstmt( false, $3, 0 ) ) );
2165 }
[c0aa336]2166 | EXTERN STRINGliteral // C++-style linkage specifier
[4d51835]2167 {
[3b8e52c]2168 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
[faddbd8]2169 linkage = LinkageSpec::linkageCheck( $2 );
[4d51835]2170 }
[c0aa336]2171 '{' external_definition_list_opt '}'
[4d51835]2172 {
2173 linkage = linkageStack.top();
2174 linkageStack.pop();
2175 $$ = $5;
2176 }
[c0aa336]2177 | EXTENSION external_definition // GCC, multiple __extension__ allowed, meaning unknown
2178 {
2179 distExt( $2 ); // mark all fields in list
[8e9cbb2]2180 $$ = $2;
2181 }
[4d51835]2182 ;
2183
2184external_function_definition:
2185 function_definition
[de62360d]2186 // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
[c6b1105]2187 // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2188 // Parsing is possible because function_definition does not appear in the context of an expression (nested
2189 // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2190 // declaration must still have a type_specifier. OBSOLESCENT (see 1)
[4d51835]2191 | function_declarator compound_statement
2192 {
2193 typedefTable.addToEnclosingScope( TypedefTable::ID );
2194 typedefTable.leaveScope();
2195 $$ = $1->addFunctionBody( $2 );
2196 }
[c0aa336]2197 | KR_function_declarator push KR_declaration_list_opt compound_statement
[4d51835]2198 {
2199 typedefTable.addToEnclosingScope( TypedefTable::ID );
2200 typedefTable.leaveScope();
2201 $$ = $1->addOldDeclList( $3 )->addFunctionBody( $4 );
2202 }
2203 ;
[51b73452]2204
2205function_definition:
[c0aa336]2206 cfa_function_declaration compound_statement // CFA
[4d51835]2207 {
2208 typedefTable.addToEnclosingScope( TypedefTable::ID );
2209 typedefTable.leaveScope();
2210 $$ = $1->addFunctionBody( $2 );
2211 }
2212 | declaration_specifier function_declarator compound_statement
2213 {
2214 typedefTable.addToEnclosingScope( TypedefTable::ID );
2215 typedefTable.leaveScope();
2216 $$ = $2->addFunctionBody( $3 )->addType( $1 );
2217 }
2218 | type_qualifier_list function_declarator compound_statement
2219 {
2220 typedefTable.addToEnclosingScope( TypedefTable::ID );
2221 typedefTable.leaveScope();
2222 $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
2223 }
2224 | declaration_qualifier_list function_declarator compound_statement
2225 {
2226 typedefTable.addToEnclosingScope( TypedefTable::ID );
2227 typedefTable.leaveScope();
2228 $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
2229 }
2230 | declaration_qualifier_list type_qualifier_list function_declarator compound_statement
2231 {
2232 typedefTable.addToEnclosingScope( TypedefTable::ID );
2233 typedefTable.leaveScope();
2234 $$ = $3->addFunctionBody( $4 )->addQualifiers( $2 )->addQualifiers( $1 );
2235 }
2236
2237 // Old-style K&R function definition, OBSOLESCENT (see 4)
[c0aa336]2238 | declaration_specifier KR_function_declarator push KR_declaration_list_opt compound_statement
[4d51835]2239 {
2240 typedefTable.addToEnclosingScope( TypedefTable::ID );
2241 typedefTable.leaveScope();
2242 $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addType( $1 );
2243 }
[c0aa336]2244 | type_qualifier_list KR_function_declarator push KR_declaration_list_opt compound_statement
[4d51835]2245 {
2246 typedefTable.addToEnclosingScope( TypedefTable::ID );
2247 typedefTable.leaveScope();
2248 $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
2249 }
2250
2251 // Old-style K&R function definition with "implicit int" type_specifier, OBSOLESCENT (see 4)
[c0aa336]2252 | declaration_qualifier_list KR_function_declarator push KR_declaration_list_opt compound_statement
[4d51835]2253 {
2254 typedefTable.addToEnclosingScope( TypedefTable::ID );
2255 typedefTable.leaveScope();
2256 $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
2257 }
[c0aa336]2258 | declaration_qualifier_list type_qualifier_list KR_function_declarator push KR_declaration_list_opt compound_statement
[4d51835]2259 {
2260 typedefTable.addToEnclosingScope( TypedefTable::ID );
2261 typedefTable.leaveScope();
2262 $$ = $3->addOldDeclList( $5 )->addFunctionBody( $6 )->addQualifiers( $2 )->addQualifiers( $1 );
2263 }
2264 ;
[51b73452]2265
2266declarator:
[4d51835]2267 variable_declarator
[c6b1105]2268 | variable_type_redeclarator
[4d51835]2269 | function_declarator
2270 ;
[51b73452]2271
2272subrange:
[4d51835]2273 constant_expression '~' constant_expression // CFA, integer subrange
[d1625f8]2274 { $$ = new ExpressionNode( build_range( $1, $3 ) ); }
[4d51835]2275 ;
[b87a5ed]2276
2277asm_name_opt: // GCC
[4d51835]2278 // empty
[58dd019]2279 { $$ = nullptr; }
2280 | ASM '(' string_literal ')' attribute_list_opt
[c0aa336]2281 {
2282 DeclarationNode * name = new DeclarationNode();
2283 name->asmName = $3;
2284 $$ = name->addQualifiers( $5 );
2285 }
[4d51835]2286 ;
[b87a5ed]2287
2288attribute_list_opt: // GCC
[4d51835]2289 // empty
[58dd019]2290 { $$ = nullptr; }
[4d51835]2291 | attribute_list
2292 ;
[b87a5ed]2293
2294attribute_list: // GCC
[4d51835]2295 attribute
2296 | attribute_list attribute
[1db21619]2297 { $$ = $2->addQualifiers( $1 ); }
[4d51835]2298 ;
[b87a5ed]2299
2300attribute: // GCC
[44a81853]2301 ATTRIBUTE '(' '(' attribute_name_list ')' ')'
2302 { $$ = $4; }
[4d51835]2303 ;
[b87a5ed]2304
[44a81853]2305attribute_name_list: // GCC
2306 attribute_name
2307 | attribute_name_list ',' attribute_name
[c0aa336]2308 { $$ = $3->addQualifiers( $1 ); }
[4d51835]2309 ;
[b87a5ed]2310
[44a81853]2311attribute_name: // GCC
[4d51835]2312 // empty
[44a81853]2313 { $$ = nullptr; }
2314 | attr_name
2315 { $$ = DeclarationNode::newAttribute( $1 ); }
2316 | attr_name '(' argument_expression_list ')'
2317 { $$ = DeclarationNode::newAttribute( $1, $3 ); }
[4d51835]2318 ;
[b87a5ed]2319
[44a81853]2320attr_name: // GCC
2321 IDENTIFIER
2322 | TYPEDEFname
2323 | TYPEGENname
2324 | CONST
2325 { $$ = Token{ new string( "__const__" ) }; }
[4d51835]2326 ;
[51b73452]2327
[c11e31c]2328// ============================================================================
[de62360d]2329// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2330// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2331// in:
[c11e31c]2332//
[b87a5ed]2333// int (*f())[10] { ... };
2334// ... (*f())[3] += 1; // definition mimics usage
[c11e31c]2335//
[de62360d]2336// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2337// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
[c11e31c]2338// ============================================================================
2339
2340// ----------------------------------------------------------------------------
[de62360d]2341// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2342// to define a variable or function prototype, e.g.:
[c11e31c]2343//
[b87a5ed]2344// valid declaration invalid definition
2345// ----------------- ------------------
[4d51835]2346// int f; int f {}
2347// int *f; int *f {}
[b87a5ed]2348// int f[10]; int f[10] {}
[4d51835]2349// int (*f)(int); int (*f)(int) {}
[c11e31c]2350//
[de62360d]2351// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2352// variable_declarator and function_declarator.
[c11e31c]2353// ----------------------------------------------------------------------------
2354
[de62360d]2355// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2356// declaring an array of functions versus a pointer to an array of functions.
[51b73452]2357
2358variable_declarator:
[4d51835]2359 paren_identifier attribute_list_opt
[1db21619]2360 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2361 | variable_ptr
2362 | variable_array attribute_list_opt
[1db21619]2363 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2364 | variable_function attribute_list_opt
[1db21619]2365 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2366 ;
[51b73452]2367
2368paren_identifier:
[4d51835]2369 identifier
2370 {
[de62360d]2371 typedefTable.setNextIdentifier( *$1 );
[4d51835]2372 $$ = DeclarationNode::newName( $1 );
2373 }
2374 | '(' paren_identifier ')' // redundant parenthesis
2375 { $$ = $2; }
2376 ;
[51b73452]2377
2378variable_ptr:
[dd51906]2379 ptrref_operator variable_declarator
[4d51835]2380 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2381 | ptrref_operator type_qualifier_list variable_declarator
[4d51835]2382 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
[c0aa336]2383 | '(' variable_ptr ')' attribute_list_opt
2384 { $$ = $2->addQualifiers( $4 ); } // redundant parenthesis
[4d51835]2385 ;
[51b73452]2386
2387variable_array:
[4d51835]2388 paren_identifier array_dimension
2389 { $$ = $1->addArray( $2 ); }
2390 | '(' variable_ptr ')' array_dimension
2391 { $$ = $2->addArray( $4 ); }
2392 | '(' variable_array ')' multi_array_dimension // redundant parenthesis
2393 { $$ = $2->addArray( $4 ); }
2394 | '(' variable_array ')' // redundant parenthesis
2395 { $$ = $2; }
2396 ;
[51b73452]2397
2398variable_function:
[4d51835]2399 '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2400 { $$ = $2->addParamList( $6 ); }
2401 | '(' variable_function ')' // redundant parenthesis
2402 { $$ = $2; }
2403 ;
[51b73452]2404
[c6b1105]2405// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2406// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2407// is the same scope. The pattern precludes returning arrays and functions versus pointers to arrays and functions.
[51b73452]2408
2409function_declarator:
[4d51835]2410 function_no_ptr attribute_list_opt
[1db21619]2411 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2412 | function_ptr
2413 | function_array attribute_list_opt
[1db21619]2414 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2415 ;
[51b73452]2416
2417function_no_ptr:
[4d51835]2418 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2419 { $$ = $1->addParamList( $4 ); }
2420 | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2421 { $$ = $2->addParamList( $6 ); }
2422 | '(' function_no_ptr ')' // redundant parenthesis
2423 { $$ = $2; }
2424 ;
[51b73452]2425
2426function_ptr:
[dd51906]2427 ptrref_operator function_declarator
[4d51835]2428 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2429 | ptrref_operator type_qualifier_list function_declarator
[4d51835]2430 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2431 | '(' function_ptr ')'
2432 { $$ = $2; }
2433 ;
[51b73452]2434
2435function_array:
[4d51835]2436 '(' function_ptr ')' array_dimension
2437 { $$ = $2->addArray( $4 ); }
2438 | '(' function_array ')' multi_array_dimension // redundant parenthesis
2439 { $$ = $2->addArray( $4 ); }
2440 | '(' function_array ')' // redundant parenthesis
2441 { $$ = $2; }
2442 ;
[51b73452]2443
[c0aa336]2444// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
2445//
2446// f( a, b, c ) int a, *b, c[]; {}
2447//
2448// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
2449// returning arrays and functions versus pointers to arrays and functions.
[51b73452]2450
[c0aa336]2451KR_function_declarator:
2452 KR_function_no_ptr
2453 | KR_function_ptr
2454 | KR_function_array
[4d51835]2455 ;
[51b73452]2456
[c0aa336]2457KR_function_no_ptr:
[4d51835]2458 paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
2459 { $$ = $1->addIdList( $3 ); }
[c0aa336]2460 | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
2461 { $$ = $2->addParamList( $6 ); }
2462 | '(' KR_function_no_ptr ')' // redundant parenthesis
[4d51835]2463 { $$ = $2; }
2464 ;
[51b73452]2465
[c0aa336]2466KR_function_ptr:
2467 ptrref_operator KR_function_declarator
[4d51835]2468 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[c0aa336]2469 | ptrref_operator type_qualifier_list KR_function_declarator
[4d51835]2470 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
[c0aa336]2471 | '(' KR_function_ptr ')'
[4d51835]2472 { $$ = $2; }
2473 ;
[51b73452]2474
[c0aa336]2475KR_function_array:
2476 '(' KR_function_ptr ')' array_dimension
[4d51835]2477 { $$ = $2->addArray( $4 ); }
[c0aa336]2478 | '(' KR_function_array ')' multi_array_dimension // redundant parenthesis
[4d51835]2479 { $$ = $2->addArray( $4 ); }
[c0aa336]2480 | '(' KR_function_array ')' // redundant parenthesis
[4d51835]2481 { $$ = $2; }
2482 ;
[51b73452]2483
[2871210]2484// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
[c11e31c]2485//
[b87a5ed]2486// typedef int foo;
2487// {
2488// int foo; // redefine typedef name in new scope
2489// }
[c11e31c]2490//
[de62360d]2491// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2492// and functions versus pointers to arrays and functions.
[51b73452]2493
[c6b1105]2494variable_type_redeclarator:
[2871210]2495 paren_type attribute_list_opt
[1db21619]2496 { $$ = $1->addQualifiers( $2 ); }
[2871210]2497 | type_ptr
2498 | type_array attribute_list_opt
[1db21619]2499 { $$ = $1->addQualifiers( $2 ); }
[2871210]2500 | type_function attribute_list_opt
[1db21619]2501 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2502 ;
[51b73452]2503
[2871210]2504paren_type:
2505 typedef
2506 | '(' paren_type ')'
[4d51835]2507 { $$ = $2; }
2508 ;
[51b73452]2509
[2871210]2510type_ptr:
[c6b1105]2511 ptrref_operator variable_type_redeclarator
[4d51835]2512 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[c6b1105]2513 | ptrref_operator type_qualifier_list variable_type_redeclarator
[4d51835]2514 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
[c0aa336]2515 | '(' type_ptr ')' attribute_list_opt
2516 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2517 ;
[51b73452]2518
[2871210]2519type_array:
2520 paren_type array_dimension
[4d51835]2521 { $$ = $1->addArray( $2 ); }
[2871210]2522 | '(' type_ptr ')' array_dimension
[4d51835]2523 { $$ = $2->addArray( $4 ); }
[2871210]2524 | '(' type_array ')' multi_array_dimension // redundant parenthesis
[4d51835]2525 { $$ = $2->addArray( $4 ); }
[2871210]2526 | '(' type_array ')' // redundant parenthesis
[4d51835]2527 { $$ = $2; }
2528 ;
[51b73452]2529
[2871210]2530type_function:
2531 paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2532 { $$ = $1->addParamList( $4 ); }
[2871210]2533 | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2534 { $$ = $2->addParamList( $6 ); }
[2871210]2535 | '(' type_function ')' // redundant parenthesis
[4d51835]2536 { $$ = $2; }
2537 ;
[51b73452]2538
[c0aa336]2539// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
2540// typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes
2541// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
2542// pointers to arrays and functions.
[51b73452]2543
2544identifier_parameter_declarator:
[4d51835]2545 paren_identifier attribute_list_opt
[1db21619]2546 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2547 | identifier_parameter_ptr
2548 | identifier_parameter_array attribute_list_opt
[1db21619]2549 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2550 | identifier_parameter_function attribute_list_opt
[1db21619]2551 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2552 ;
[51b73452]2553
2554identifier_parameter_ptr:
[dd51906]2555 ptrref_operator identifier_parameter_declarator
[4d51835]2556 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2557 | ptrref_operator type_qualifier_list identifier_parameter_declarator
[4d51835]2558 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
[c0aa336]2559 | '(' identifier_parameter_ptr ')' attribute_list_opt
2560 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2561 ;
[51b73452]2562
2563identifier_parameter_array:
[4d51835]2564 paren_identifier array_parameter_dimension
2565 { $$ = $1->addArray( $2 ); }
2566 | '(' identifier_parameter_ptr ')' array_dimension
2567 { $$ = $2->addArray( $4 ); }
2568 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2569 { $$ = $2->addArray( $4 ); }
2570 | '(' identifier_parameter_array ')' // redundant parenthesis
2571 { $$ = $2; }
2572 ;
[51b73452]2573
2574identifier_parameter_function:
[4d51835]2575 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2576 { $$ = $1->addParamList( $4 ); }
2577 | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2578 { $$ = $2->addParamList( $6 ); }
2579 | '(' identifier_parameter_function ')' // redundant parenthesis
2580 { $$ = $2; }
2581 ;
[b87a5ed]2582
[de62360d]2583// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2584// e.g.:
[c11e31c]2585//
[b87a5ed]2586// typedef int foo;
2587// int f( int foo ); // redefine typedef name in new scope
[c11e31c]2588//
[c0aa336]2589// and allows the C99 array options, which can only appear in a parameter list.
[51b73452]2590
[2871210]2591type_parameter_redeclarator:
[4d51835]2592 typedef attribute_list_opt
[1db21619]2593 { $$ = $1->addQualifiers( $2 ); }
[2871210]2594 | type_parameter_ptr
2595 | type_parameter_array attribute_list_opt
[1db21619]2596 { $$ = $1->addQualifiers( $2 ); }
[2871210]2597 | type_parameter_function attribute_list_opt
[1db21619]2598 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2599 ;
[51b73452]2600
2601typedef:
[4d51835]2602 TYPEDEFname
2603 {
[de62360d]2604 typedefTable.setNextIdentifier( *$1 );
[4d51835]2605 $$ = DeclarationNode::newName( $1 );
2606 }
[2871210]2607 | TYPEGENname
2608 {
2609 typedefTable.setNextIdentifier( *$1 );
2610 $$ = DeclarationNode::newName( $1 );
2611 }
[4d51835]2612 ;
[51b73452]2613
[2871210]2614type_parameter_ptr:
[dd51906]2615 ptrref_operator type_parameter_redeclarator
[4d51835]2616 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2617 | ptrref_operator type_qualifier_list type_parameter_redeclarator
[4d51835]2618 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
[c0aa336]2619 | '(' type_parameter_ptr ')' attribute_list_opt
2620 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2621 ;
[51b73452]2622
[2871210]2623type_parameter_array:
[4d51835]2624 typedef array_parameter_dimension
2625 { $$ = $1->addArray( $2 ); }
[2871210]2626 | '(' type_parameter_ptr ')' array_parameter_dimension
[4d51835]2627 { $$ = $2->addArray( $4 ); }
2628 ;
[51b73452]2629
[2871210]2630type_parameter_function:
[4d51835]2631 typedef '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2632 { $$ = $1->addParamList( $4 ); }
[2871210]2633 | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2634 { $$ = $2->addParamList( $6 ); }
2635 ;
[b87a5ed]2636
[de62360d]2637// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2638// which the type applies, e.g.:
[c11e31c]2639//
[b87a5ed]2640// sizeof( int );
[c0aa336]2641// sizeof( int * );
[b87a5ed]2642// sizeof( int [10] );
[c0aa336]2643// sizeof( int (*)() );
2644// sizeof( int () );
[c11e31c]2645//
[de62360d]2646// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2647// and functions versus pointers to arrays and functions.
[51b73452]2648
2649abstract_declarator:
[4d51835]2650 abstract_ptr
2651 | abstract_array attribute_list_opt
[1db21619]2652 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2653 | abstract_function attribute_list_opt
[1db21619]2654 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2655 ;
[51b73452]2656
2657abstract_ptr:
[dd51906]2658 ptrref_operator
[4d51835]2659 { $$ = DeclarationNode::newPointer( 0 ); }
[dd51906]2660 | ptrref_operator type_qualifier_list
[4d51835]2661 { $$ = DeclarationNode::newPointer( $2 ); }
[dd51906]2662 | ptrref_operator abstract_declarator
[4d51835]2663 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2664 | ptrref_operator type_qualifier_list abstract_declarator
[4d51835]2665 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
[c0aa336]2666 | '(' abstract_ptr ')' attribute_list_opt
2667 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2668 ;
[51b73452]2669
2670abstract_array:
[4d51835]2671 array_dimension
2672 | '(' abstract_ptr ')' array_dimension
2673 { $$ = $2->addArray( $4 ); }
2674 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
2675 { $$ = $2->addArray( $4 ); }
2676 | '(' abstract_array ')' // redundant parenthesis
2677 { $$ = $2; }
2678 ;
[51b73452]2679
2680abstract_function:
[4d51835]2681 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[2298f728]2682 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
[4d51835]2683 | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2684 { $$ = $2->addParamList( $6 ); }
2685 | '(' abstract_function ')' // redundant parenthesis
2686 { $$ = $2; }
2687 ;
[51b73452]2688
2689array_dimension:
[4d51835]2690 // Only the first dimension can be empty.
[2871210]2691 '[' ']'
[4d51835]2692 { $$ = DeclarationNode::newArray( 0, 0, false ); }
[2871210]2693 | '[' ']' multi_array_dimension
2694 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
[4d51835]2695 | multi_array_dimension
2696 ;
[51b73452]2697
2698multi_array_dimension:
[4d51835]2699 '[' push assignment_expression pop ']'
2700 { $$ = DeclarationNode::newArray( $3, 0, false ); }
2701 | '[' push '*' pop ']' // C99
2702 { $$ = DeclarationNode::newVarArray( 0 ); }
2703 | multi_array_dimension '[' push assignment_expression pop ']'
2704 { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2705 | multi_array_dimension '[' push '*' pop ']' // C99
2706 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2707 ;
[51b73452]2708
[c11e31c]2709// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2710// identifier to which the type applies, e.g.:
2711//
[c0aa336]2712// int f( int ); // not handled here
2713// int f( int * ); // abstract function-prototype parameter; no parameter name specified
2714// int f( int (*)() ); // abstract function-prototype parameter; no parameter name specified
[b87a5ed]2715// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
[c11e31c]2716//
[de62360d]2717// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
[c0aa336]2718// and functions versus pointers to arrays and functions. In addition, the pattern handles the
2719// special meaning of parenthesis around a typedef name:
2720//
2721// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2722// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2723// not as redundant parentheses around the identifier."
2724//
2725// For example:
2726//
2727// typedef float T;
2728// int f( int ( T [5] ) ); // see abstract_parameter_declarator
2729// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
2730// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
2731// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
2732//
2733// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2734// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2735// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2736// functions.
[51b73452]2737
2738abstract_parameter_declarator:
[4d51835]2739 abstract_parameter_ptr
2740 | abstract_parameter_array attribute_list_opt
[1db21619]2741 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2742 | abstract_parameter_function attribute_list_opt
[1db21619]2743 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2744 ;
[51b73452]2745
2746abstract_parameter_ptr:
[dd51906]2747 ptrref_operator
[c0aa336]2748 { $$ = DeclarationNode::newPointer( nullptr ); }
[dd51906]2749 | ptrref_operator type_qualifier_list
[4d51835]2750 { $$ = DeclarationNode::newPointer( $2 ); }
[dd51906]2751 | ptrref_operator abstract_parameter_declarator
[c0aa336]2752 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr ) ); }
[dd51906]2753 | ptrref_operator type_qualifier_list abstract_parameter_declarator
[4d51835]2754 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
[c0aa336]2755 | '(' abstract_parameter_ptr ')' attribute_list_opt
2756 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2757 ;
[51b73452]2758
2759abstract_parameter_array:
[4d51835]2760 array_parameter_dimension
2761 | '(' abstract_parameter_ptr ')' array_parameter_dimension
2762 { $$ = $2->addArray( $4 ); }
2763 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2764 { $$ = $2->addArray( $4 ); }
2765 | '(' abstract_parameter_array ')' // redundant parenthesis
2766 { $$ = $2; }
2767 ;
[51b73452]2768
2769abstract_parameter_function:
[4d51835]2770 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[2298f728]2771 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
[4d51835]2772 | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2773 { $$ = $2->addParamList( $6 ); }
2774 | '(' abstract_parameter_function ')' // redundant parenthesis
2775 { $$ = $2; }
2776 ;
[51b73452]2777
2778array_parameter_dimension:
[4d51835]2779 // Only the first dimension can be empty or have qualifiers.
2780 array_parameter_1st_dimension
2781 | array_parameter_1st_dimension multi_array_dimension
2782 { $$ = $1->addArray( $2 ); }
2783 | multi_array_dimension
2784 ;
[51b73452]2785
[c11e31c]2786// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
2787//
[de62360d]2788// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
2789// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
[51b73452]2790
2791array_parameter_1st_dimension:
[2871210]2792 '[' ']'
[4d51835]2793 { $$ = DeclarationNode::newArray( 0, 0, false ); }
2794 // multi_array_dimension handles the '[' '*' ']' case
2795 | '[' push type_qualifier_list '*' pop ']' // remaining C99
2796 { $$ = DeclarationNode::newVarArray( $3 ); }
2797 | '[' push type_qualifier_list pop ']'
2798 { $$ = DeclarationNode::newArray( 0, $3, false ); }
2799 // multi_array_dimension handles the '[' assignment_expression ']' case
2800 | '[' push type_qualifier_list assignment_expression pop ']'
2801 { $$ = DeclarationNode::newArray( $4, $3, false ); }
2802 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
2803 { $$ = DeclarationNode::newArray( $5, $4, true ); }
2804 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
2805 { $$ = DeclarationNode::newArray( $5, $3, true ); }
2806 ;
[b87a5ed]2807
[c0aa336]2808// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
[c11e31c]2809//
[c0aa336]2810// struct S {
2811// int;
2812// int *;
2813// int [10];
2814// int (*)();
2815// };
[51b73452]2816
2817variable_abstract_declarator:
[4d51835]2818 variable_abstract_ptr
2819 | variable_abstract_array attribute_list_opt
[1db21619]2820 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2821 | variable_abstract_function attribute_list_opt
[1db21619]2822 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2823 ;
[51b73452]2824
2825variable_abstract_ptr:
[dd51906]2826 ptrref_operator
[4d51835]2827 { $$ = DeclarationNode::newPointer( 0 ); }
[dd51906]2828 | ptrref_operator type_qualifier_list
[4d51835]2829 { $$ = DeclarationNode::newPointer( $2 ); }
[dd51906]2830 | ptrref_operator variable_abstract_declarator
[4d51835]2831 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2832 | ptrref_operator type_qualifier_list variable_abstract_declarator
[4d51835]2833 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
[c0aa336]2834 | '(' variable_abstract_ptr ')' attribute_list_opt
2835 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2836 ;
[51b73452]2837
2838variable_abstract_array:
[4d51835]2839 array_dimension
2840 | '(' variable_abstract_ptr ')' array_dimension
2841 { $$ = $2->addArray( $4 ); }
2842 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
2843 { $$ = $2->addArray( $4 ); }
2844 | '(' variable_abstract_array ')' // redundant parenthesis
2845 { $$ = $2; }
2846 ;
[51b73452]2847
2848variable_abstract_function:
[4d51835]2849 '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2850 { $$ = $2->addParamList( $6 ); }
2851 | '(' variable_abstract_function ')' // redundant parenthesis
2852 { $$ = $2; }
2853 ;
[b87a5ed]2854
[de62360d]2855// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
2856// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
[b87a5ed]2857
[c0aa336]2858cfa_identifier_parameter_declarator_tuple: // CFA
2859 cfa_identifier_parameter_declarator_no_tuple
2860 | cfa_abstract_tuple
2861 | type_qualifier_list cfa_abstract_tuple
[4d51835]2862 { $$ = $2->addQualifiers( $1 ); }
2863 ;
[b87a5ed]2864
[c0aa336]2865cfa_identifier_parameter_declarator_no_tuple: // CFA
2866 cfa_identifier_parameter_ptr
2867 | cfa_identifier_parameter_array
[4d51835]2868 ;
[b87a5ed]2869
[c0aa336]2870cfa_identifier_parameter_ptr: // CFA
[d0ffed1]2871 // No SUE declaration in parameter list.
2872 ptrref_operator type_specifier_nobody
[4d51835]2873 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[d0ffed1]2874 | type_qualifier_list ptrref_operator type_specifier_nobody
[4d51835]2875 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
[c0aa336]2876 | ptrref_operator cfa_abstract_function
[4d51835]2877 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[c0aa336]2878 | type_qualifier_list ptrref_operator cfa_abstract_function
[4d51835]2879 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
[c0aa336]2880 | ptrref_operator cfa_identifier_parameter_declarator_tuple
[4d51835]2881 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[c0aa336]2882 | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
[4d51835]2883 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2884 ;
[b87a5ed]2885
[c0aa336]2886cfa_identifier_parameter_array: // CFA
[de62360d]2887 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
2888 // shift/reduce conflict with new-style empty (void) function return type.
[d0ffed1]2889 '[' ']' type_specifier_nobody
[2871210]2890 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[d0ffed1]2891 | cfa_array_parameter_1st_dimension type_specifier_nobody
[4d51835]2892 { $$ = $2->addNewArray( $1 ); }
[d0ffed1]2893 | '[' ']' multi_array_dimension type_specifier_nobody
[2871210]2894 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[d0ffed1]2895 | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
[4d51835]2896 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[d0ffed1]2897 | multi_array_dimension type_specifier_nobody
[4d51835]2898 { $$ = $2->addNewArray( $1 ); }
[9059213]2899
[c0aa336]2900 | '[' ']' cfa_identifier_parameter_ptr
[2871210]2901 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[c0aa336]2902 | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
[4d51835]2903 { $$ = $2->addNewArray( $1 ); }
[c0aa336]2904 | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
[2871210]2905 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[c0aa336]2906 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]2907 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[c0aa336]2908 | multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]2909 { $$ = $2->addNewArray( $1 ); }
2910 ;
[51b73452]2911
[c0aa336]2912cfa_array_parameter_1st_dimension:
[4d51835]2913 '[' push type_qualifier_list '*' pop ']' // remaining C99
2914 { $$ = DeclarationNode::newVarArray( $3 ); }
2915 | '[' push type_qualifier_list assignment_expression pop ']'
2916 { $$ = DeclarationNode::newArray( $4, $3, false ); }
2917 | '[' push declaration_qualifier_list assignment_expression pop ']'
2918 // declaration_qualifier_list must be used because of shift/reduce conflict with
2919 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
2920 // appear in this context.
2921 { $$ = DeclarationNode::newArray( $4, $3, true ); }
2922 | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
2923 { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
2924 ;
[b87a5ed]2925
[de62360d]2926// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
2927// identifier to which the type applies, e.g.:
[c11e31c]2928//
[b87a5ed]2929// [int] f( int ); // abstract variable parameter; no parameter name specified
2930// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
[c11e31c]2931//
2932// These rules need LR(3):
2933//
[c0aa336]2934// cfa_abstract_tuple identifier_or_type_name
2935// '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_type_list_opt ')'
[c11e31c]2936//
2937// since a function return type can be syntactically identical to a tuple type:
2938//
[b87a5ed]2939// [int, int] t;
2940// [int, int] f( int );
[c11e31c]2941//
[2871210]2942// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
[c0aa336]2943// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
2944// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
2945// duplicated when appearing with cfa_function_specifier.
[b87a5ed]2946
[c0aa336]2947cfa_abstract_declarator_tuple: // CFA
2948 cfa_abstract_tuple
2949 | type_qualifier_list cfa_abstract_tuple
[4d51835]2950 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]2951 | cfa_abstract_declarator_no_tuple
[4d51835]2952 ;
[b87a5ed]2953
[c0aa336]2954cfa_abstract_declarator_no_tuple: // CFA
2955 cfa_abstract_ptr
2956 | cfa_abstract_array
[4d51835]2957 ;
[b87a5ed]2958
[c0aa336]2959cfa_abstract_ptr: // CFA
[dd51906]2960 ptrref_operator type_specifier
[4d51835]2961 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[dd51906]2962 | type_qualifier_list ptrref_operator type_specifier
[4d51835]2963 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
[c0aa336]2964 | ptrref_operator cfa_abstract_function
[4d51835]2965 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[c0aa336]2966 | type_qualifier_list ptrref_operator cfa_abstract_function
[4d51835]2967 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
[c0aa336]2968 | ptrref_operator cfa_abstract_declarator_tuple
[4d51835]2969 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
[c0aa336]2970 | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
[4d51835]2971 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2972 ;
[b87a5ed]2973
[c0aa336]2974cfa_abstract_array: // CFA
[de62360d]2975 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
2976 // empty (void) function return type.
[2871210]2977 '[' ']' type_specifier
[2298f728]2978 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[2871210]2979 | '[' ']' multi_array_dimension type_specifier
[2298f728]2980 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[4d51835]2981 | multi_array_dimension type_specifier
2982 { $$ = $2->addNewArray( $1 ); }
[c0aa336]2983 | '[' ']' cfa_abstract_ptr
[2298f728]2984 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]2985 | '[' ']' multi_array_dimension cfa_abstract_ptr
[2298f728]2986 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]2987 | multi_array_dimension cfa_abstract_ptr
[4d51835]2988 { $$ = $2->addNewArray( $1 ); }
2989 ;
[b87a5ed]2990
[c0aa336]2991cfa_abstract_tuple: // CFA
2992 '[' push cfa_abstract_parameter_list pop ']'
[4d51835]2993 { $$ = DeclarationNode::newTuple( $3 ); }
2994 ;
[b87a5ed]2995
[c0aa336]2996cfa_abstract_function: // CFA
2997// '[' ']' '(' cfa_parameter_type_list_opt ')'
[1b29996]2998// { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
[c0aa336]2999 cfa_abstract_tuple '(' push cfa_parameter_type_list_opt pop ')'
[2298f728]3000 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[c0aa336]3001 | cfa_function_return '(' push cfa_parameter_type_list_opt pop ')'
[2298f728]3002 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[4d51835]3003 ;
[b87a5ed]3004
[de62360d]3005// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3006// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
[c11e31c]3007//
[de62360d]3008// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3009// the declaration specifiers in a declaration is an obsolescent feature."
[c11e31c]3010//
3011// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3012// prototype-format parameter type declarators) is an obsolescent feature."
3013//
[de62360d]3014// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3015// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
[51b73452]3016
[c11e31c]3017//************************* MISCELLANEOUS ********************************
[51b73452]3018
[b87a5ed]3019comma_opt: // redundant comma
[4d51835]3020 // empty
3021 | ','
3022 ;
[51b73452]3023
3024assignment_opt:
[4d51835]3025 // empty
[58dd019]3026 { $$ = nullptr; }
[4d51835]3027 | '=' assignment_expression
3028 { $$ = $2; }
3029 ;
[51b73452]3030
3031%%
[c11e31c]3032// ----end of grammar----
[51b73452]3033
[0da3e2c]3034extern char *yytext;
3035
[5f2f2d7]3036void yyerror( const char * ) {
[2298f728]3037 cout << "Error ";
[b87a5ed]3038 if ( yyfilename ) {
[2298f728]3039 cout << "in file " << yyfilename << " ";
[5f2f2d7]3040 } // if
[2298f728]3041 cout << "at line " << yylineno << " reading token \"" << (yytext[0] == '\0' ? "EOF" : yytext) << "\"" << endl;
[51b73452]3042}
3043
[c11e31c]3044// Local Variables: //
[b87a5ed]3045// mode: c++ //
[de62360d]3046// tab-width: 4 //
[c11e31c]3047// compile-command: "make install" //
3048// End: //
Note: See TracBrowser for help on using the repository browser.