source: src/Parser/parser.yy@ 3f5a28b

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 3f5a28b was 8b47e50, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add syntax for "with" clause/statement

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