source: src/Parser/parser.yy@ 274ce8c

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 274ce8c was 6d49ea3, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

second attempt, add declarations into if conditional

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