source: src/Parser/parser.yy@ 2fa5bd2

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 2fa5bd2 was e307e12, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

generalize aggregate data and control in grammar, add aggregate-control field-name, add inline aggregate-control

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