source: src/Parser/parser.yy@ ecfa58be

Last change on this file since ecfa58be was e86735ba, checked in by Peter A. Buhr <pabuhr@…>, 6 months ago

clean up current for control, first attempt at adding ==/!= to for control

  • Property mode set to 100644
File size: 184.7 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
[04c78215]11// Last Modified By : Peter A. Buhr
[e86735ba]12// Last Modified On : Fri Apr 18 15:23:42 2025
13// Update Count : 7283
[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
[dd54139]26// The root language for this grammar is ANSI99/11 C. All of ANSI99/11 is parsed. Most of the syntactic extensions from
27// ANSI90 to ANSI11 C are marked with the comment "C99/C11".
[f9c3100]28
[e16eb460]29// This grammar also has two levels of extensions. The first extensions cover most of the GCC C extensions. All of the
[9380add]30// syntactic extensions for GCC C are marked with the comment "GCC". The second extensions are for Cforall (CFA), which
31// fixes several of C's outstanding problems and extends C with many modern language concepts. All of the syntactic
32// extensions for CFA C are marked with the comment "CFA".
[51b73452]33
34%{
[ec3f9c8]35#define YYDEBUG_LEXER_TEXT( yylval ) // lexer loads this up each time
[b87a5ed]36#define YYDEBUG 1 // get the pretty debugging code to compile
[201aeb9]37#define YYERROR_VERBOSE // more information in syntax errors
[51b73452]38
39#undef __GNUC_MINOR__
40
41#include <cstdio>
[9feb34b]42#include <sstream>
[51b73452]43#include <stack>
[9ed4f94]44using namespace std;
45
[c92bdcc]46#include "DeclarationNode.hpp" // for DeclarationNode, ...
47#include "ExpressionNode.hpp" // for ExpressionNode, ...
48#include "InitializerNode.hpp" // for InitializerNode, ...
49#include "ParserTypes.hpp"
50#include "StatementNode.hpp" // for build_...
51#include "TypedefTable.hpp"
52#include "TypeData.hpp"
[7a780ad]53#include "AST/Type.hpp" // for BasicType, BasicKind
[c92bdcc]54#include "Common/SemanticError.hpp" // error_str
55#include "Common/Utility.hpp" // for maybeMoveBuild, maybeBuild, CodeLo...
[cd28605]56#include "AST/Attribute.hpp" // for Attribute
57#include "AST/Print.hpp" // for print
58#include "Common/Iterate.hpp" // for reverseIterate
[51b73452]59
[ae2f2ae]60// lex uses __null in a boolean context, it's fine.
[0bd46fd]61#ifdef __clang__
[09f34a84]62#pragma GCC diagnostic ignored "-Wparentheses-equality"
[0bd46fd]63#endif
[ae2f2ae]64
[cbaee0d]65extern DeclarationNode * parseTree;
[bb7422a]66extern ast::Linkage::Spec linkage;
[0da3e2c]67extern TypedefTable typedefTable;
68
[bb7422a]69stack<ast::Linkage::Spec> linkageStack;
[7bf7fb9]70
[15697ff]71bool appendStr( string & to, string & from ) {
72 // 1. Multiple strings are concatenated into a single string but not combined internally. The reason is that
[ea0c5e3]73 // "\x12" "3" is treated as 2 characters versus 1 because "escape sequences are converted into single members of
[15697ff]74 // the execution character set just prior to adjacent string literal concatenation" (C11, Section 6.4.5-8). It is
75 // easier to let the C compiler handle this case.
76 //
77 // 2. String encodings are transformed into canonical form (one encoding at start) so the encoding can be found
78 // without searching the string, e.g.: "abc" L"def" L"ghi" => L"abc" "def" "ghi". Multiple encodings must match,
[efc8f3e]79 // e.g., u"a" U"b" L"c" is disallowed.
[15697ff]80
81 if ( from[0] != '"' ) { // encoding ?
82 if ( to[0] != '"' ) { // encoding ?
[ea0c5e3]83 if ( to[0] != from[0] || to[1] != from[1] ) { // different encodings ?
[15697ff]84 yyerror( "non-matching string encodings for string-literal concatenation" );
85 return false; // parse error, must call YYERROR in action
[ea0c5e3]86 } else if ( from[1] == '8' ) {
87 from.erase( 0, 1 ); // remove 2nd encoding
[15697ff]88 } // if
89 } else {
[ea0c5e3]90 if ( from[1] == '8' ) { // move encoding to start
91 to = "u8" + to;
92 from.erase( 0, 1 ); // remove 2nd encoding
93 } else {
94 to = from[0] + to;
95 } // if
[15697ff]96 } // if
97 from.erase( 0, 1 ); // remove 2nd encoding
98 } // if
99 to += " " + from; // concatenated into single string
100 return true;
[7bf7fb9]101} // appendStr
[c0aa336]102
[cd28605]103DeclarationNode * distTypeSpec( DeclarationNode * typeSpec, DeclarationNode * declList ) {
[4eb3a7c5]104 // Distribute type specifier across all declared variables, e.g., static, const, __attribute__.
[d8454b9]105 assert( declList );
106
[4eb3a7c5]107 // Do not distribute attributes for aggregates because the attributes surrounding the aggregate belong it not the
108 // variables in the declaration list, e.g.,
109 //
110 // struct __attribute__(( aligned(128) )) S { ...
111 // } v1 __attribute__(( aligned(64) )), v2 __attribute__(( aligned(32) )), v3;
112 // struct S v4;
113 //
114 // v1 => 64, v2 =>32, v3 => 128, v2 => 128
115 //
116 // Anonymous aggregates are a special case because there is no aggregate to bind the attribute to; hence it floats
117 // to the declaration list.
118 //
119 // struct __attribute__(( aligned(128) )) /*anonymous */ { ... } v1;
120 //
121 // v1 => 128
122
123 bool copyattr = ! (typeSpec->type && typeSpec->type->kind == TypeData::Aggregate && ! typeSpec->type->aggregate.anon );
124
125 // addType copies the type information for the aggregate instances from typeSpec into cl's aggInst.aggregate.
126 DeclarationNode * cl = (new DeclarationNode)->addType( typeSpec ); // typeSpec IS DELETED!!!
127
[647e2ea]128 // Start at second variable in declaration list and clone the type specifiers for each variable.
[44adf1b]129 for ( DeclarationNode * cur = declList->next ; cur != nullptr; cur = cur->next ) {
[4eb3a7c5]130 cl->cloneBaseType( cur, copyattr ); // cur is modified
[c0aa336]131 } // for
[4eb3a7c5]132
133 // Add first variable in declaration list with hidden type information in aggInst.aggregate, which is used by
134 // extractType to recover the type for the aggregate instances.
135 declList->addType( cl, copyattr ); // cl IS DELETED!!!
[c0aa336]136 return declList;
[cd28605]137} // distTypeSpec
138
139void distAttr( DeclarationNode * attributes, DeclarationNode * declaration ) {
140 // distribute attributes across all declaring list
141 for ( DeclarationNode * attr = attributes; attr != nullptr ; attr = attr->next ) {
142 for ( DeclarationNode * decl = declaration ; decl != nullptr ; decl = decl->next ) {
143 decl->attributes.insert( decl->attributes.begin(), attr->attributes.begin(), attr->attributes.end() );
144 } // for
145 } // for
[c0aa336]146} // distAttr
147
148void distExt( DeclarationNode * declaration ) {
149 // distribute EXTENSION across all declarations
[cd28605]150 for ( DeclarationNode * decl = declaration ; decl != nullptr ; decl = decl->next ) {
151 decl->set_extension( true );
[c0aa336]152 } // for
153} // distExt
[fdca7c6]154
[e07caa2]155void distInl( DeclarationNode * declaration ) {
[5695645]156 // distribute INLINE across all declarations
[cd28605]157 for ( DeclarationNode *decl = declaration ; decl != nullptr ; decl = decl->next ) {
158 decl->set_inLine( true );
[e07caa2]159 } // for
160} // distInl
161
[4c3ee8d]162void distQual( DeclarationNode * declaration, DeclarationNode * qualifiers ) {
[284da8c]163 // distribute qualifiers across all non-variable declarations in a distribution statemement
[cd28605]164 for ( DeclarationNode * decl = declaration ; decl != nullptr ; decl = decl->next ) {
[284da8c]165 // SKULLDUGGERY: Distributions are parsed inside out, so qualifiers are added to declarations inside out. Since
166 // addQualifiers appends to the back of the list, the forall clauses are in the wrong order (right to left). To
167 // get the qualifiers in the correct order and still use addQualifiers (otherwise, 90% of addQualifiers has to
168 // be copied to add to front), the appropriate forall pointers are interchanged before calling addQualifiers.
169 DeclarationNode * clone = qualifiers->clone();
170 if ( qualifiers->type ) { // forall clause ? (handles SC)
[cd28605]171 if ( decl->type->kind == TypeData::Aggregate ) { // struct/union ?
172 swap( clone->type->forall, decl->type->aggregate.params );
173 decl->addQualifiers( clone );
174 } else if ( decl->type->kind == TypeData::AggregateInst && decl->type->aggInst.aggregate->aggregate.body ) { // struct/union ?
[284da8c]175 // Create temporary node to hold aggregate, call addQualifiers as above, then put nodes back together.
176 DeclarationNode newnode;
[cd28605]177 swap( newnode.type, decl->type->aggInst.aggregate );
[284da8c]178 swap( clone->type->forall, newnode.type->aggregate.params );
179 newnode.addQualifiers( clone );
[cd28605]180 swap( newnode.type, decl->type->aggInst.aggregate );
181 } else if ( decl->type->kind == TypeData::Function ) { // routines ?
182 swap( clone->type->forall, decl->type->forall );
183 decl->addQualifiers( clone );
[284da8c]184 } // if
185 } else { // just SC qualifiers
[cd28605]186 decl->addQualifiers( clone );
[284da8c]187 } // if
[4c3ee8d]188 } // for
[284da8c]189 delete qualifiers;
190} // distQual
[4c3ee8d]191
[c38ae92]192// There is an ambiguity for inline generic-routine return-types and generic routines.
193// forall( otype T ) struct S { int i; } bar( T ) {}
194// Does the forall bind to the struct or the routine, and how would it be possible to explicitly specify the binding.
195// forall( otype T ) struct S { int T; } forall( otype W ) bar( W ) {}
[7fdb94e1]196// Currently, the forall is associated with the routine, and the generic type has to be separately defined:
197// forall( otype T ) struct S { int T; };
198// forall( otype W ) bar( W ) {}
[c38ae92]199
200void rebindForall( DeclarationNode * declSpec, DeclarationNode * funcDecl ) {
[7fdb94e1]201 if ( declSpec->type->kind == TypeData::Aggregate ) { // ignore aggregate definition
[c38ae92]202 funcDecl->type->forall = declSpec->type->aggregate.params; // move forall from aggregate to function type
203 declSpec->type->aggregate.params = nullptr;
204 } // if
205} // rebindForall
206
[60a8062]207string * build_postfix_name( string * name ) {
208 *name = string("__postfix_func_") + *name;
209 return name;
[dc7db63]210} // build_postfix_name
211
[f7e4db27]212DeclarationNode * fieldDecl( DeclarationNode * typeSpec, DeclarationNode * fieldList ) {
[a16f2b6]213 if ( fieldList == nullptr ) {
214 if ( !( typeSpec->type && typeSpec->type->kind == TypeData::Aggregate ) ) { // int; no fieldList
[d8454b9]215 // printf( "fieldDecl1 typeSpec %p\n", typeSpec ); typeSpec->type->print( std::cout );
[a16f2b6]216 SemanticWarning( yylloc, Warning::SuperfluousDecl );
[f7e4db27]217 return nullptr;
218 } // if
[d8454b9]219 // printf( "fieldDecl2 typeSpec %p\n", typeSpec ); typeSpec->type->print( std::cout );
[a16f2b6]220 fieldList = DeclarationNode::newName( nullptr ); // struct S { ... } no fieldList
[f7e4db27]221 } // if
[d8454b9]222
223 // printf( "fieldDecl3 typeSpec %p\n", typeSpec ); typeSpec->print( std::cout, 0 );
[cd28605]224 DeclarationNode * temp = distTypeSpec( typeSpec, fieldList ); // mark all fields in list
[d8454b9]225 // printf( "fieldDecl4 temp %p\n", temp ); temp->print( std::cout, 0 );
226 return temp;
[f7e4db27]227} // fieldDecl
228
[bb7422a]229#define NEW_ZERO new ExpressionNode( build_constantInteger( yylloc, *new string( "0" ) ) )
230#define NEW_ONE new ExpressionNode( build_constantInteger( yylloc, *new string( "1" ) ) )
[e86735ba]231#define UPDOWN( compop, left, right ) (compop == OperKinds::LThan || compop == OperKinds::LEThan || compop == OperKinds::Neq ? left : right)
[253d0b4]232#define MISSING_ANON_FIELD "illegal syntax, missing loop fields with an anonymous loop index is meaningless as loop index is unavailable in loop body."
[01afd8d]233#define MISSING_LOW "illegal syntax, missing low value for ascanding range so index is uninitialized."
234#define MISSING_HIGH "illegal syntax, missing high value for descending range so index is uninitialized."
[253d0b4]235
[089b39e1]236static ForCtrl * makeForCtrl( const CodeLocation & location, DeclarationNode * init, OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
[1cdc052]237 // Wrap both comp/inc if they are non-null.
[bb7422a]238 if ( comp ) comp = new ExpressionNode( build_binary_val( location,
[1cdc052]239 compop,
[bb7422a]240 new ExpressionNode( build_varref( location, new string( *init->name ) ) ),
[1cdc052]241 comp ) );
[bb7422a]242 if ( inc ) inc = new ExpressionNode( build_binary_val( location,
[1cdc052]243 // choose += or -= for upto/downto
[e86735ba]244 UPDOWN( compop, OperKinds::PlusAssn, OperKinds::MinusAssn ),
[bb7422a]245 new ExpressionNode( build_varref( location, new string( *init->name ) ) ),
[1cdc052]246 inc ) );
247 // The StatementNode call frees init->name, it must happen later.
248 return new ForCtrl( new StatementNode( init ), comp, inc );
249}
250
[089b39e1]251ForCtrl * forCtrl( const CodeLocation & location, DeclarationNode * index, ExpressionNode * start, OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
[d78c238]252 if ( index->initializer ) {
[253d0b4]253 SemanticError( yylloc, "illegal syntax, direct initialization disallowed. Use instead: type var; initialization ~ comparison ~ increment." );
[d78c238]254 } // if
255 if ( index->next ) {
[253d0b4]256 SemanticError( yylloc, "illegal syntax, multiple loop indexes disallowed in for-loop declaration." );
[d78c238]257 } // if
[1cdc052]258 DeclarationNode * initDecl = index->addInitializer( new InitializerNode( start ) );
[bb7422a]259 return makeForCtrl( location, initDecl, compop, comp, inc );
[d78c238]260} // forCtrl
261
[089b39e1]262ForCtrl * forCtrl( const CodeLocation & location, ExpressionNode * type, string * index, ExpressionNode * start, OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
[bb7422a]263 ast::ConstantExpr * constant = dynamic_cast<ast::ConstantExpr *>(type->expr.get());
264 if ( constant && (constant->rep == "0" || constant->rep == "1") ) {
[7a780ad]265 type = new ExpressionNode( new ast::CastExpr( location, maybeMoveBuild(type), new ast::BasicType( ast::BasicKind::SignedInt ) ) );
[0982a05]266 } // if
[cd28605]267 DeclarationNode * initDecl = distTypeSpec(
[1cdc052]268 DeclarationNode::newTypeof( type, true ),
269 DeclarationNode::newName( index )->addInitializer( new InitializerNode( start ) )
270 );
[bb7422a]271 return makeForCtrl( location, initDecl, compop, comp, inc );
[f271bdd]272} // forCtrl
273
[253d0b4]274#define MISSING_LOOP_INDEX "illegal syntax, only a single identifier or declaration allowed in initialization, e.g., for ( i; ... ) or for ( int i; ... ). Expression disallowed."
275
[089b39e1]276ForCtrl * forCtrl( const CodeLocation & location, ExpressionNode * type, ExpressionNode * index, ExpressionNode * start, OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
[bb7422a]277 if ( auto identifier = dynamic_cast<ast::NameExpr *>(index->expr.get()) ) {
278 return forCtrl( location, type, new string( identifier->name ), start, compop, comp, inc );
[f1aeede]279 } else {
[253d0b4]280 SemanticError( yylloc, MISSING_LOOP_INDEX ); return nullptr;
[f1aeede]281 } // if
[f271bdd]282} // forCtrl
283
[6d2b3dc]284ForCtrl * enumRangeCtrl( ExpressionNode * index_expr, OperKinds compop, ExpressionNode * range_over_expr, DeclarationNode * type ) {
285 assert( compop == OperKinds::LEThan || compop == OperKinds::GEThan );
[525f7ad]286 if ( auto identifier = dynamic_cast<ast::NameExpr *>(index_expr->expr.get()) ) {
[6d2b3dc]287 DeclarationNode * indexDecl =
288 DeclarationNode::newName( new std::string(identifier->name) )->addType( type );
[d3aa55e9]289 return new ForCtrl( new StatementNode( indexDecl ), range_over_expr, compop );
[525f7ad]290 } else {
[253d0b4]291 SemanticError( yylloc, MISSING_LOOP_INDEX ); return nullptr;
[525f7ad]292 } // if
293} // enumRangeCtrl
294
[996c8ed]295static void IdentifierBeforeIdentifier( string & identifier1, string & identifier2, const char * kind ) {
[253d0b4]296 SemanticError( yylloc, "illegal syntax, adjacent identifiers \"%s\" and \"%s\" are not meaningful in an %s.\n"
[b1f2007d]297 "Possible cause is misspelled type name or missing generic parameter.",
298 identifier1.c_str(), identifier2.c_str(), kind );
[996c8ed]299} // IdentifierBeforeIdentifier
300
301static void IdentifierBeforeType( string & identifier, const char * kind ) {
[253d0b4]302 SemanticError( yylloc, "illegal syntax, identifier \"%s\" cannot appear before a %s.\n"
[b1f2007d]303 "Possible cause is misspelled storage/CV qualifier, misspelled typename, or missing generic parameter.",
304 identifier.c_str(), kind );
[996c8ed]305} // IdentifierBeforeType
306
[24711a3]307bool forall = false; // aggregate have one or more forall qualifiers ?
[d48e529]308
[201aeb9]309// https://www.gnu.org/software/bison/manual/bison.html#Location-Type
310#define YYLLOC_DEFAULT(Cur, Rhs, N) \
311if ( N ) { \
312 (Cur).first_line = YYRHSLOC( Rhs, 1 ).first_line; \
313 (Cur).first_column = YYRHSLOC( Rhs, 1 ).first_column; \
314 (Cur).last_line = YYRHSLOC( Rhs, N ).last_line; \
315 (Cur).last_column = YYRHSLOC( Rhs, N ).last_column; \
316 (Cur).filename = YYRHSLOC( Rhs, 1 ).filename; \
317} else { \
318 (Cur).first_line = (Cur).last_line = YYRHSLOC( Rhs, 0 ).last_line; \
319 (Cur).first_column = (Cur).last_column = YYRHSLOC( Rhs, 0 ).last_column; \
320 (Cur).filename = YYRHSLOC( Rhs, 0 ).filename; \
321}
[51b73452]322%}
323
[15697ff]324%define parse.error verbose
325
[201aeb9]326// Types declaration for productions
[7cf8006]327
[0982a05]328%union {
[6cef439]329 // A raw token can be used.
[a67b60e]330 Token tok;
[6cef439]331
332 // The general node types hold some generic node or list of nodes.
[a67b60e]333 DeclarationNode * decl;
[6cef439]334 InitializerNode * init;
335 ExpressionNode * expr;
[32d6fdc]336 StatementNode * stmt;
[6611177]337 ClauseNode * clause;
[6cef439]338 TypeData * type;
339
340 // Special "nodes" containing compound information.
[569b118]341 CondCtrl * ifctrl;
342 ForCtrl * forctrl;
[32d6fdc]343 LabelNode * labels;
[6cef439]344
345 // Various flags and single values that become fields later.
346 ast::AggregateDecl::Aggregate aggKey;
347 ast::TypeDecl::Kind tclass;
[32d6fdc]348 OperKinds oper;
349 bool is_volatile;
350 EnumHiding enum_hiding;
351 ast::ExceptionKind except_kind;
[6cef439]352 // String passes ownership with it.
353 std::string * str;
354
355 // Narrower node types are used to avoid constant unwrapping.
356 ast::WaitForStmt * wfs;
357 ast::WaitUntilStmt::ClauseNode * wucn;
[bb7422a]358 ast::GenericExpr * genexpr;
[a67b60e]359}
360
[bb7422a]361// ************************ TERMINAL TOKENS ********************************
[51b73452]362
[c11e31c]363// keywords
[51b73452]364%token TYPEDEF
[a7c90d4]365%token EXTERN STATIC AUTO REGISTER
[59c7e3e]366%token THREADLOCALGCC THREADLOCALC11 // GCC, C11
[a7c90d4]367%token INLINE FORTRAN // C99, extension ISO/IEC 9899:1999 Section J.5.9(1)
368%token NORETURN // C11
[51b73452]369%token CONST VOLATILE
[b87a5ed]370%token RESTRICT // C99
[a7c90d4]371%token ATOMIC // C11
[1f652a7]372%token FORALL MUTEX VIRTUAL VTABLE COERCE // CFA
[72457b6]373%token VOID CHAR SHORT INT LONG FLOAT DOUBLE SIGNED UNSIGNED
[b87a5ed]374%token BOOL COMPLEX IMAGINARY // C99
[c5f69fd]375%token INT128 UINT128 FLOAT80 uuFLOAT128 // GCC
376%token FLOAT16 FLOAT32 FLOAT32X FLOAT64 FLOAT64X FLOAT128 FLOAT128X // GCC
377%token FLOAT32X4 FLOAT64X2 SVFLOAT32 SVFLOAT64 SVBOOL // GCC (ARM)
[15f769c]378%token DECIMAL32 DECIMAL64 DECIMAL128 // GCC
[72457b6]379%token ZERO_T ONE_T // CFA
[a8ced63]380%token SIZEOF TYPEOF VA_LIST VA_ARG AUTO_TYPE COUNTOF // GCC
[1f652a7]381%token OFFSETOF BASETYPEOF TYPEID // CFA
[51b73452]382%token ENUM STRUCT UNION
[c27fb59]383%token EXCEPTION // CFA
[553772b]384%token GENERATOR COROUTINE MONITOR THREAD // CFA
[a7c90d4]385%token OTYPE FTYPE DTYPE TTYPE TRAIT // CFA
[25744d2]386// %token RESUME // CFA
[1f652a7]387%token LABEL // GCC
[25744d2]388%token SUSPEND // CFA
[b87a5ed]389%token ATTRIBUTE EXTENSION // GCC
[51b73452]390%token IF ELSE SWITCH CASE DEFAULT DO WHILE FOR BREAK CONTINUE GOTO RETURN
[d96f7c4]391%token CHOOSE FALLTHROUGH WITH WHEN WAITFOR WAITUNTIL // CFA
[11ab0b4a]392%token CORUN COFOR
[4744074]393%token DISABLE ENABLE TRY THROW THROWRESUME AT // CFA
[b87a5ed]394%token ASM // C99, extension ISO/IEC 9899:1999 Section J.5.10(1)
[a7c90d4]395%token ALIGNAS ALIGNOF GENERIC STATICASSERT // C11
[51b73452]396
[c11e31c]397// names and constants: lexer differentiates between identifier and typedef names
[9fd9d015]398%token<tok> IDENTIFIER TYPEDIMname TYPEDEFname TYPEGENname
[12f1156]399%token<tok> TIMEOUT WAND WOR CATCH RECOVER CATCHRESUME FIXUP FINALLY // CFA
[9fb1367]400%token<tok> INTEGERconstant CHARACTERconstant STRINGliteral
[1a8b17a]401%token<tok> DIRECTIVE C23_ATTRIBUTE
[1b29996]402// Floating point constant is broken into three kinds of tokens because of the ambiguity with tuple indexing and
403// overloading constants 0/1, e.g., x.1 is lexed as (x)(.1), where (.1) is a factional constant, but is semantically
404// converted into the tuple index (.)(1). e.g., 3.x
[930f69e]405%token<tok> FLOATING_DECIMALconstant FLOATING_FRACTIONconstant FLOATINGconstant
[51b73452]406
[c11e31c]407// multi-character operators
[b87a5ed]408%token ARROW // ->
409%token ICR DECR // ++ --
410%token LS RS // << >>
411%token LE GE EQ NE // <= >= == !=
412%token ANDAND OROR // && ||
[647e2ea]413%token ATTR ELLIPSIS // @@ ...
[b87a5ed]414
[e5f2a67]415%token EXPassign MULTassign DIVassign MODassign // \= *= /= %=
[b87a5ed]416%token PLUSassign MINUSassign // += -=
417%token LSassign RSassign // <<= >>=
418%token ANDassign ERassign ORassign // &= ^= |=
[51b73452]419
[e86735ba]420%token ErangeUpLt ErangeUpLe ErangeEq ErangeNe // +~ +~=/~= ~== ~!=
421%token ErangeDownGt ErangeDownGe ErangeDownEq ErangeDownNe // -~ -~= -~== -~!=
[e7aed49]422%token ATassign // @=
[097e2b0]423
[e16eb460]424%type<tok> identifier identifier_at identifier_or_type_name attr_name
[5b2edbc]425%type<tok> quasi_keyword
[32d6fdc]426%type<expr> string_literal
[ab57786]427%type<str> string_literal_list
[51b73452]428
[71a422a]429%type<enum_hiding> hide_opt visible_hide_opt
[7cf8006]430
[c11e31c]431// expressions
[32d6fdc]432%type<expr> constant
[71a422a]433%type<expr> tuple tuple_expression_list
[32d6fdc]434%type<oper> ptrref_operator unary_operator assignment_operator simple_assignment_operator compound_assignment_operator
435%type<expr> primary_expression postfix_expression unary_expression
[71a422a]436%type<expr> cast_expression_list cast_expression exponential_expression multiplicative_expression additive_expression
437%type<expr> shift_expression relational_expression equality_expression
[32d6fdc]438%type<expr> AND_expression exclusive_OR_expression inclusive_OR_expression
439%type<expr> logical_AND_expression logical_OR_expression
440%type<expr> conditional_expression constant_expression assignment_expression assignment_expression_opt
[71a422a]441%type<expr> comma_expression comma_expression_opt
442%type<expr> argument_expression_list_opt argument_expression_list argument_expression default_initializer_opt
[569b118]443%type<ifctrl> conditional_declaration
444%type<forctrl> for_control_expression for_control_expression_list
[e86735ba]445%type<oper> upupeq updown updownS updownEq
[32d6fdc]446%type<expr> subrange
[c0aa336]447%type<decl> asm_name_opt
[71a422a]448%type<expr> asm_operands_opt asm_operands_list asm_operand
[cd28605]449%type<labels> asm_label_list
[32d6fdc]450%type<expr> asm_clobbers_list_opt
451%type<is_volatile> asm_volatile_opt
452%type<expr> handler_predicate_opt
[67d4e37]453%type<genexpr> generic_association generic_assoc_list
[51b73452]454
[c11e31c]455// statements
[cd28605]456%type<stmt> statement labelled_statement compound_statement
[32d6fdc]457%type<stmt> statement_decl statement_decl_list statement_list_nodecl
[647e2ea]458%type<stmt> selection_statement
[71a422a]459%type<clause> switch_clause_list_opt switch_clause_list
[32d6fdc]460%type<expr> case_value
[71a422a]461%type<clause> case_clause case_value_list case_label case_label_list
[32d6fdc]462%type<stmt> iteration_statement jump_statement
[71a422a]463%type<stmt> expression_statement asm_statement
[32d6fdc]464%type<stmt> with_statement
465%type<expr> with_clause_opt
[11ab0b4a]466%type<stmt> corun_statement cofor_statement
[32d6fdc]467%type<stmt> exception_statement
[6611177]468%type<clause> handler_clause finally_clause
[32d6fdc]469%type<except_kind> handler_key
470%type<stmt> mutex_statement
471%type<expr> when_clause when_clause_opt waitfor waituntil timeout
[71a422a]472%type<stmt> waitfor_statement waituntil_statement
[c86b08d]473%type<wfs> wor_waitfor_clause
[6e1e2d0]474%type<wucn> waituntil_clause wand_waituntil_clause wor_waituntil_clause
[51b73452]475
[c11e31c]476// declarations
[c0aa336]477%type<decl> abstract_declarator abstract_ptr abstract_array abstract_function array_dimension multi_array_dimension
[a16f2b6]478%type<decl> abstract_parameter_declarator_opt abstract_parameter_declarator abstract_parameter_ptr abstract_parameter_array
479 abstract_parameter_function array_parameter_dimension array_parameter_1st_dimension abstract_parameter_declaration
[51b73452]480
[e307e12]481%type<aggKey> aggregate_key aggregate_data aggregate_control
[d0ffed1]482%type<decl> aggregate_type aggregate_type_nobody
[51b73452]483
[9997fee]484%type<decl> assertion assertion_list assertion_list_opt
[51b73452]485
[32d6fdc]486%type<expr> bit_subrange_size_opt bit_subrange_size
[51b73452]487
[84d58c5]488%type<decl> basic_declaration_specifier basic_type_name basic_type_specifier direct_type indirect_type
[6cef439]489%type<type> basic_type_name_type
490%type<type> vtable vtable_opt default_opt
[51b73452]491
[4040425]492%type<decl> trait_declaration trait_declaration_list trait_declaring_list trait_specifier
[51b73452]493
494%type<decl> declaration declaration_list declaration_list_opt declaration_qualifier_list
[d0ffed1]495%type<decl> declaration_specifier declaration_specifier_nobody declarator declaring_list
[51b73452]496
[d0ffed1]497%type<decl> elaborated_type elaborated_type_nobody
[51b73452]498
[a8ced63]499%type<decl> enumerator_list enum_type enum_type_nobody enum_key enumerator_type
[32d6fdc]500%type<init> enumerator_value_opt
[51b73452]501
[3d56d15b]502%type<decl> external_definition external_definition_list external_definition_list_opt
503
504%type<decl> exception_declaration
[51b73452]505
[a16f2b6]506%type<decl> field_declaration_list_opt field_declaration field_declaring_list field_declaring_list_opt field_declarator field_abstract_list_opt field_abstract
[32d6fdc]507%type<expr> field field_name_list field_name fraction_constants_opt
[51b73452]508
[4d51835]509%type<decl> external_function_definition function_definition function_array function_declarator function_no_ptr function_ptr
[51b73452]510
[d3bc0ad]511%type<decl> identifier_parameter_declarator identifier_parameter_ptr identifier_parameter_array identifier_parameter_function
512%type<decl> identifier_list
[51b73452]513
[c0aa336]514%type<decl> cfa_abstract_array cfa_abstract_declarator_no_tuple cfa_abstract_declarator_tuple
515%type<decl> cfa_abstract_function cfa_abstract_parameter_declaration cfa_abstract_parameter_list
516%type<decl> cfa_abstract_ptr cfa_abstract_tuple
[51b73452]517
[c0aa336]518%type<decl> cfa_array_parameter_1st_dimension
[51b73452]519
[679e644]520%type<decl> cfa_trait_declaring_list cfa_declaration cfa_field_declaring_list cfa_field_abstract_list
[c0aa336]521%type<decl> cfa_function_declaration cfa_function_return cfa_function_specifier
[51b73452]522
[c0aa336]523%type<decl> cfa_identifier_parameter_array cfa_identifier_parameter_declarator_no_tuple
524%type<decl> cfa_identifier_parameter_declarator_tuple cfa_identifier_parameter_ptr
[51b73452]525
[647e2ea]526%type<decl> cfa_parameter_declaration cfa_parameter_list cfa_parameter_list_ellipsis_opt
[51b73452]527
[c0aa336]528%type<decl> cfa_typedef_declaration cfa_variable_declaration cfa_variable_specifier
[51b73452]529
[b9be000b]530%type<decl> c_declaration static_assert
[c0aa336]531%type<decl> KR_function_declarator KR_function_no_ptr KR_function_ptr KR_function_array
[35718a9]532%type<decl> KR_parameter_list KR_parameter_list_opt
[51b73452]533
[647e2ea]534%type<decl> parameter_declaration parameter_list parameter_list_ellipsis_opt
[51b73452]535
[2871210]536%type<decl> paren_identifier paren_type
[51b73452]537
[0da3e2c]538%type<decl> storage_class storage_class_list
[51b73452]539
[d0ffed1]540%type<decl> sue_declaration_specifier sue_declaration_specifier_nobody sue_type_specifier sue_type_specifier_nobody
[51b73452]541
[5a51798]542%type<tclass> type_class new_type_class
[51b73452]543%type<decl> type_declarator type_declarator_name type_declaring_list
544
[6cef439]545%type<decl> type_declaration_specifier type_type_specifier
546%type<type> type_name typegen_name
[f9c3100]547%type<decl> typedef_name typedef_declaration typedef_expression
[c0aa336]548
[1f771fc]549%type<decl> variable_type_redeclarator variable_type_ptr variable_type_array variable_type_function
550%type<decl> general_function_declarator function_type_redeclarator function_type_array function_type_no_ptr function_type_ptr
[c0aa336]551
552%type<decl> type_parameter_redeclarator type_parameter_ptr type_parameter_array type_parameter_function
[51b73452]553
[84d58c5]554%type<decl> type type_no_function
555%type<decl> type_parameter type_parameter_list type_initializer_opt
[51b73452]556
[647e2ea]557%type<expr> type_parameters_opt type_list array_type_list // array_dimension_list
[51b73452]558
[6cef439]559%type<decl> type_qualifier forall type_qualifier_list_opt type_qualifier_list
560%type<type> type_qualifier_name
[f9c3100]561%type<decl> type_specifier type_specifier_nobody
[51b73452]562
[c0aa336]563%type<decl> variable_declarator variable_ptr variable_array variable_function
564%type<decl> variable_abstract_declarator variable_abstract_ptr variable_abstract_array variable_abstract_function
[51b73452]565
[f9c3100]566%type<decl> attribute_list_opt attribute_list attribute attribute_name_list attribute_name
[1db21619]567
[c11e31c]568// initializers
[32d6fdc]569%type<init> initializer initializer_list_opt initializer_opt
[51b73452]570
[c11e31c]571// designators
[32d6fdc]572%type<expr> designator designator_list designation
[51b73452]573
574
[65d6de4]575// Handle shift/reduce conflict for dangling else by shifting the ELSE token. For example, this string is ambiguous:
576// .---------. matches IF '(' comma_expression ')' statement . (reduce)
577// if ( C ) S1 else S2
578// `-----------------' matches IF '(' comma_expression ')' statement . (shift) ELSE statement */
[5b2edbc]579// Similar issues exit with the waitfor statement.
[51b73452]580
[9fd9d015]581// Order of these lines matters (low-to-high precedence). THEN is left associative over WAND/WOR/TIMEOUT/ELSE, WAND/WOR
582// is left associative over TIMEOUT/ELSE, and TIMEOUT is left associative over ELSE.
[9fb1367]583%precedence THEN // rule precedence for IF/WAITFOR statement
[9fd9d015]584%precedence ANDAND // token precedence for start of WAND in WAITFOR statement
585%precedence WAND // token precedence for start of WAND in WAITFOR statement
586%precedence OROR // token precedence for start of WOR in WAITFOR statement
[9fb1367]587%precedence WOR // token precedence for start of WOR in WAITFOR statement
588%precedence TIMEOUT // token precedence for start of TIMEOUT in WAITFOR statement
589%precedence CATCH // token precedence for start of TIMEOUT in WAITFOR statement
590%precedence RECOVER // token precedence for start of TIMEOUT in WAITFOR statement
591%precedence CATCHRESUME // token precedence for start of TIMEOUT in WAITFOR statement
592%precedence FIXUP // token precedence for start of TIMEOUT in WAITFOR statement
593%precedence FINALLY // token precedence for start of TIMEOUT in WAITFOR statement
594%precedence ELSE // token precedence for start of else clause in IF/WAITFOR statement
595
[51b73452]596
[65d6de4]597// Handle shift/reduce conflict for generic type by shifting the '(' token. For example, this string is ambiguous:
598// forall( otype T ) struct Foo { T v; };
599// .-----. matches pointer to function returning a generic (which is impossible without a type)
600// Foo ( *fp )( int );
601// `---' matches start of TYPEGENname '('
[fc20514]602// must be:
[a16a7ec]603// Foo( int ) ( *fp )( int );
[fc20514]604// The same problem occurs here:
605// forall( otype T ) struct Foo { T v; } ( *fp )( int );
606// must be:
607// forall( otype T ) struct Foo { T v; } ( int ) ( *fp )( int );
[65d6de4]608
609// Order of these lines matters (low-to-high precedence).
610%precedence TYPEGENname
[284da8c]611%precedence '}'
[65d6de4]612%precedence '('
613
[c786e1d]614// %precedence RESUME
615// %precedence '{'
616// %precedence ')'
617
[f38e7d7]618%locations // support location tracking for error messages
[930f69e]619
[b87a5ed]620%start translation_unit // parse-tree root
[51b73452]621
622%%
[e1d66c84]623// ************************ Namespace Management ********************************
[c11e31c]624
[3d26610]625// The C grammar is not context free because it relies on the distinct terminal symbols "identifier" and "TYPEDEFname",
626// which are lexically identical.
[c11e31c]627//
[3d26610]628// typedef int foo; // identifier foo must now be scanned as TYPEDEFname
629// foo f; // to allow it to appear in this context
[c11e31c]630//
[3d26610]631// While it may be possible to write a purely context-free grammar, such a grammar would obscure the relationship
632// between syntactic and semantic constructs. Cforall compounds this problem by introducing type names local to the
633// scope of a declaration (for instance, those introduced through "forall" qualifiers), and by introducing "type
634// generators" -- parameterized types. This latter type name creates a third class of identifiers, "TYPEGENname", which
635// must be distinguished by the lexical scanner.
[c11e31c]636//
[3d26610]637// Since the scanner cannot distinguish among the different classes of identifiers without some context information,
638// there is a type table (typedefTable), which holds type names and identifiers that override type names, for each named
639// scope. During parsing, semantic actions update the type table by adding new identifiers in the current scope. For
640// each context that introduces a name scope, a new level is created in the type table and that level is popped on
641// exiting the scope. Since type names can be local to a particular declaration, each declaration is itself a scope.
642// This requires distinguishing between type names that are local to the current declaration scope and those that
643// persist past the end of the declaration (i.e., names defined in "typedef" or "otype" declarations).
[c11e31c]644//
[3d26610]645// The non-terminals "push" and "pop" denote the opening and closing of named scopes. Every push has a matching pop in
646// the production rule. There are multiple lists of declarations, where each declaration is a named scope, so pop/push
647// around the list separator.
648//
[71a422a]649// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[3d26610]650// push pop push pop
[51b73452]651
652push:
[ab57786]653 { typedefTable.enterScope(); }
[4d51835]654 ;
[51b73452]655
656pop:
[ab57786]657 { typedefTable.leaveScope(); }
[4d51835]658 ;
[51b73452]659
[e1d66c84]660// ************************ CONSTANTS ********************************
[51b73452]661
662constant:
[de62360d]663 // ENUMERATIONconstant is not included here; it is treated as a variable with type "enumeration constant".
[bb7422a]664 INTEGERconstant { $$ = new ExpressionNode( build_constantInteger( yylloc, *$1 ) ); }
665 | FLOATING_DECIMALconstant { $$ = new ExpressionNode( build_constantFloat( yylloc, *$1 ) ); }
666 | FLOATING_FRACTIONconstant { $$ = new ExpressionNode( build_constantFloat( yylloc, *$1 ) ); }
667 | FLOATINGconstant { $$ = new ExpressionNode( build_constantFloat( yylloc, *$1 ) ); }
668 | CHARACTERconstant { $$ = new ExpressionNode( build_constantChar( yylloc, *$1 ) ); }
[4d51835]669 ;
[51b73452]670
[5b2edbc]671quasi_keyword: // CFA
672 TIMEOUT
[9fd9d015]673 | WAND
[5b2edbc]674 | WOR
[9fb1367]675 | CATCH
676 | RECOVER
677 | CATCHRESUME
678 | FIXUP
679 | FINALLY
[5b2edbc]680 ;
681
[033ff37]682identifier:
[4d51835]683 IDENTIFIER
[5b2edbc]684 | quasi_keyword
[e16eb460]685 ;
686
687identifier_at:
688 identifier
[679e644]689 | '@' // CFA
690 { Token tok = { new string( DeclarationNode::anonymous.newName() ), yylval.tok.loc }; $$ = tok; }
[4d51835]691 ;
[51b73452]692
[12f1156]693identifier_or_type_name:
694 identifier
695 | TYPEDEFname
696 | TYPEGENname
697 ;
698
[ab57786]699string_literal:
[32d6fdc]700 string_literal_list { $$ = new ExpressionNode( build_constantStr( yylloc, *$1 ) ); }
[ab57786]701 ;
702
[b87a5ed]703string_literal_list: // juxtaposed strings are concatenated
[ab57786]704 STRINGliteral { $$ = $1; } // conversion from tok to str
[7bf7fb9]705 | string_literal_list STRINGliteral
706 {
[15697ff]707 if ( ! appendStr( *$1, *$2 ) ) YYERROR; // append 2nd juxtaposed string to 1st
[7bf7fb9]708 delete $2; // allocated by lexer
[ab57786]709 $$ = $1; // conversion from tok to str
[7bf7fb9]710 }
[4d51835]711 ;
[51b73452]712
[e1d66c84]713// ************************ EXPRESSIONS ********************************
[51b73452]714
715primary_expression:
[4d51835]716 IDENTIFIER // typedef name cannot be used as a variable name
[bb7422a]717 { $$ = new ExpressionNode( build_varref( yylloc, $1 ) ); }
[5b2edbc]718 | quasi_keyword
[bb7422a]719 { $$ = new ExpressionNode( build_varref( yylloc, $1 ) ); }
[6e50a6b]720 | TYPEDIMname // CFA, generic length argument
[bb7422a]721 { $$ = new ExpressionNode( build_dimensionref( yylloc, $1 ) ); }
[1b29996]722 | tuple
[4d51835]723 | '(' comma_expression ')'
724 { $$ = $2; }
725 | '(' compound_statement ')' // GCC, lambda expression
[bb7422a]726 { $$ = new ExpressionNode( new ast::StmtExpr( yylloc, dynamic_cast<ast::CompoundStmt *>( maybeMoveBuild( $2 ) ) ) ); }
[033ff37]727 | type_name '.' identifier // CFA, nested type
[6cef439]728 { $$ = new ExpressionNode( build_qualified_expr( yylloc, DeclarationNode::newFromTypeData( $1 ), build_varref( yylloc, $3 ) ) ); }
[679e644]729 | type_name '.' '[' field_name_list ']' // CFA, nested type / tuple field selector
[203c667]730 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
[24c3b67]731 | GENERIC '(' assignment_expression ',' generic_assoc_list ')' // C11
[d807ca28]732 {
733 // add the missing control expression to the GenericExpr and return it
[702e826]734 $5->control = maybeMoveBuild( $3 );
[d807ca28]735 $$ = new ExpressionNode( $5 );
736 }
[c786e1d]737 // | RESUME '(' comma_expression ')'
738 // { SemanticError( yylloc, "Resume expression is currently unimplemented." ); $$ = nullptr; }
739 // | RESUME '(' comma_expression ')' compound_statement
740 // { SemanticError( yylloc, "Resume expression is currently unimplemented." ); $$ = nullptr; }
[65ef0cd]741 | IDENTIFIER IDENTIFIER // invalid syntax rule
[b1f2007d]742 { IdentifierBeforeIdentifier( *$1.str, *$2.str, "expression" ); $$ = nullptr; }
[65ef0cd]743 | IDENTIFIER type_qualifier // invalid syntax rule
[996c8ed]744 { IdentifierBeforeType( *$1.str, "type qualifier" ); $$ = nullptr; }
[65ef0cd]745 | IDENTIFIER storage_class // invalid syntax rule
[996c8ed]746 { IdentifierBeforeType( *$1.str, "storage class" ); $$ = nullptr; }
[65ef0cd]747 | IDENTIFIER basic_type_name // invalid syntax rule
[996c8ed]748 { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
[65ef0cd]749 | IDENTIFIER TYPEDEFname // invalid syntax rule
[996c8ed]750 { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
[65ef0cd]751 | IDENTIFIER TYPEGENname // invalid syntax rule
[996c8ed]752 { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
[24c3b67]753 ;
754
755generic_assoc_list: // C11
[d807ca28]756 generic_association
[24c3b67]757 | generic_assoc_list ',' generic_association
[d807ca28]758 {
759 // steal the association node from the singleton and delete the wrapper
[bb7422a]760 assert( 1 == $3->associations.size() );
761 $1->associations.push_back( $3->associations.front() );
[d807ca28]762 delete $3;
763 $$ = $1;
764 }
[24c3b67]765 ;
766
767generic_association: // C11
768 type_no_function ':' assignment_expression
[d807ca28]769 {
770 // create a GenericExpr wrapper with one association pair
[bb7422a]771 $$ = new ast::GenericExpr( yylloc, nullptr, { { maybeMoveBuildType( $1 ), maybeMoveBuild( $3 ) } } );
[d807ca28]772 }
[24c3b67]773 | DEFAULT ':' assignment_expression
[bb7422a]774 { $$ = new ast::GenericExpr( yylloc, nullptr, { { maybeMoveBuild( $3 ) } } ); }
[4d51835]775 ;
[51b73452]776
777postfix_expression:
[4d51835]778 primary_expression
[1d71208]779 | postfix_expression '[' assignment_expression ',' tuple_expression_list ']'
[59c7e3e]780 // Historic, transitional: Disallow commas in subscripts.
781 // Switching to this behaviour may help check if a C compatibilty case uses comma-exprs in subscripts.
782 // Current: Commas in subscripts make tuples.
[b93c544]783 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, $1, new ExpressionNode( build_tuple( yylloc, $3->set_last( $5 ) ) ) ) ); }
[7fdb94e1]784 | postfix_expression '[' assignment_expression ']'
[c6b1105]785 // CFA, comma_expression disallowed in this context because it results in a common user error: subscripting a
[de62360d]786 // matrix with x[i,j] instead of x[i][j]. While this change is not backwards compatible, there seems to be
787 // little advantage to this feature and many disadvantages. It is possible to write x[(i,j)] in CFA, which is
788 // equivalent to the old x[i,j].
[bb7422a]789 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, $1, $3 ) ); }
[d824715]790 | constant '[' assignment_expression ']' // 3[a], 'a'[a], 3.5[a]
[bb7422a]791 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, $1, $3 ) ); }
[d824715]792 | string_literal '[' assignment_expression ']' // "abc"[3], 3["abc"]
[32d6fdc]793 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, $1, $3 ) ); }
[cbbd8fd7]794 | postfix_expression '{' argument_expression_list_opt '}' // CFA, constructor call
[bd3d9e4]795 {
796 Token fn;
797 fn.str = new std::string( "?{}" ); // location undefined - use location of '{'?
[b93c544]798 $$ = new ExpressionNode( new ast::ConstructorExpr( yylloc, build_func( yylloc, new ExpressionNode( build_varref( yylloc, fn ) ), $1->set_last( $3 ) ) ) );
[bd3d9e4]799 }
[cbbd8fd7]800 | postfix_expression '(' argument_expression_list_opt ')'
[bb7422a]801 { $$ = new ExpressionNode( build_func( yylloc, $1, $3 ) ); }
[59c7e3e]802 | VA_ARG '(' primary_expression ',' declaration_specifier_nobody abstract_parameter_declarator_opt ')'
[45ee172]803 { $$ = new ExpressionNode( build_va_arg( yylloc, $3, ( $6 ? $6->addType( $5 ) : $5 ) ) ); }
[948fdef]804 | postfix_expression '`' identifier // CFA, postfix call
[bb7422a]805 { $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, build_postfix_name( $3 ) ) ), $1 ) ); }
[948fdef]806 | constant '`' identifier // CFA, postfix call
[bb7422a]807 { $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, build_postfix_name( $3 ) ) ), $1 ) ); }
[948fdef]808 | string_literal '`' identifier // CFA, postfix call
[32d6fdc]809 { $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, build_postfix_name( $3 ) ) ), $1 ) ); }
[4eb3a7c5]810
811 // SKULLDUGGERY: The typedef table used for parsing does not store fields in structures. To parse a qualified
812 // name, it is assumed all name-tokens after the first are identifiers, regardless of how the lexer identifies
813 // them. For example:
814 //
815 // struct S;
816 // forall(T) struct T;
817 // union U;
818 // enum E { S, T, E };
819 // struct Z { int S, T, Z, E, U; };
820 // void fred () {
821 // Z z;
822 // z.S; // lexer returns S is TYPEDEFname
823 // z.T; // lexer returns T is TYPEGENname
824 // z.Z; // lexer returns Z is TYPEDEFname
825 // z.U; // lexer returns U is TYPEDEFname
826 // z.E; // lexer returns E is TYPEDEFname
827 // }
[12f1156]828 | postfix_expression '.' identifier_or_type_name
[4eb3a7c5]829 { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_varref( yylloc, $3 ) ) ); }
830
[df22130]831 | postfix_expression '.' INTEGERconstant // CFA, tuple index
[bb7422a]832 { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_constantInteger( yylloc, *$3 ) ) ); }
[930f69e]833 | postfix_expression FLOATING_FRACTIONconstant // CFA, tuple index
[bb7422a]834 { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_field_name_FLOATING_FRACTIONconstant( yylloc, *$2 ) ) ); }
[679e644]835 | postfix_expression '.' '[' field_name_list ']' // CFA, tuple field selector
[bb7422a]836 { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_tuple( yylloc, $4 ) ) ); }
[e307e12]837 | postfix_expression '.' aggregate_control
[bb7422a]838 { $$ = new ExpressionNode( build_keyword_cast( yylloc, $3, $1 ) ); }
[033ff37]839 | postfix_expression ARROW identifier
[bb7422a]840 { $$ = new ExpressionNode( build_pfieldSel( yylloc, $1, build_varref( yylloc, $3 ) ) ); }
[861799c7]841 | postfix_expression ARROW INTEGERconstant // CFA, tuple index
[bb7422a]842 { $$ = new ExpressionNode( build_pfieldSel( yylloc, $1, build_constantInteger( yylloc, *$3 ) ) ); }
[679e644]843 | postfix_expression ARROW '[' field_name_list ']' // CFA, tuple field selector
[bb7422a]844 { $$ = new ExpressionNode( build_pfieldSel( yylloc, $1, build_tuple( yylloc, $4 ) ) ); }
[4d51835]845 | postfix_expression ICR
[bb7422a]846 { $$ = new ExpressionNode( build_unary_val( yylloc, OperKinds::IncrPost, $1 ) ); }
[4d51835]847 | postfix_expression DECR
[bb7422a]848 { $$ = new ExpressionNode( build_unary_val( yylloc, OperKinds::DecrPost, $1 ) ); }
[7fdb94e1]849 | '(' type_no_function ')' '{' initializer_list_opt comma_opt '}' // C99, compound-literal
[bb7422a]850 { $$ = new ExpressionNode( build_compoundLiteral( yylloc, $2, new InitializerNode( $5, true ) ) ); }
[7fdb94e1]851 | '(' type_no_function ')' '@' '{' initializer_list_opt comma_opt '}' // CFA, explicit C compound-literal
[bb7422a]852 { $$ = new ExpressionNode( build_compoundLiteral( yylloc, $2, (new InitializerNode( $6, true ))->set_maybeConstructed( false ) ) ); }
[cbbd8fd7]853 | '^' primary_expression '{' argument_expression_list_opt '}' // CFA, destructor call
[097e2b0]854 {
[9706554]855 Token fn;
[ecb27a7]856 fn.str = new string( "^?{}" ); // location undefined
[b93c544]857 $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, fn ) ), $2->set_last( $4 ) ) );
[097e2b0]858 }
[4d51835]859 ;
[51b73452]860
[679e644]861field_name_list: // CFA, tuple field selector
[4d51835]862 field
[b93c544]863 | field_name_list ',' field { $$ = $1->set_last( $3 ); }
[4d51835]864 ;
[b87a5ed]865
866field: // CFA, tuple field selector
[faddbd8]867 field_name
[930f69e]868 | FLOATING_DECIMALconstant field
[bb7422a]869 { $$ = new ExpressionNode( build_fieldSel( yylloc, new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( yylloc, *$1 ) ), maybeMoveBuild( $2 ) ) ); }
[679e644]870 | FLOATING_DECIMALconstant '[' field_name_list ']'
[bb7422a]871 { $$ = new ExpressionNode( build_fieldSel( yylloc, new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( yylloc, *$1 ) ), build_tuple( yylloc, $3 ) ) ); }
[faddbd8]872 | field_name '.' field
[bb7422a]873 { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, maybeMoveBuild( $3 ) ) ); }
[679e644]874 | field_name '.' '[' field_name_list ']'
[bb7422a]875 { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_tuple( yylloc, $4 ) ) ); }
[faddbd8]876 | field_name ARROW field
[bb7422a]877 { $$ = new ExpressionNode( build_pfieldSel( yylloc, $1, maybeMoveBuild( $3 ) ) ); }
[679e644]878 | field_name ARROW '[' field_name_list ']'
[bb7422a]879 { $$ = new ExpressionNode( build_pfieldSel( yylloc, $1, build_tuple( yylloc, $4 ) ) ); }
[4d51835]880 ;
[51b73452]881
[faddbd8]882field_name:
[df22130]883 INTEGERconstant fraction_constants_opt
[bb7422a]884 { $$ = new ExpressionNode( build_field_name_fraction_constants( yylloc, build_constantInteger( yylloc, *$1 ), $2 ) ); }
[df22130]885 | FLOATINGconstant fraction_constants_opt
[bb7422a]886 { $$ = new ExpressionNode( build_field_name_fraction_constants( yylloc, build_field_name_FLOATINGconstant( yylloc, *$1 ), $2 ) ); }
[e16eb460]887 | identifier_at fraction_constants_opt // CFA, allow anonymous fields
[4cb935e]888 {
[bb7422a]889 $$ = new ExpressionNode( build_field_name_fraction_constants( yylloc, build_varref( yylloc, $1 ), $2 ) );
[84d58c5]890 }
[1b29996]891 ;
892
[df22130]893fraction_constants_opt:
[1b29996]894 // empty
[8780e30]895 { $$ = nullptr; }
[df22130]896 | fraction_constants_opt FLOATING_FRACTIONconstant
[8780e30]897 {
[bb7422a]898 ast::Expr * constant = build_field_name_FLOATING_FRACTIONconstant( yylloc, *$2 );
899 $$ = $1 != nullptr ? new ExpressionNode( build_fieldSel( yylloc, $1, constant ) ) : new ExpressionNode( constant );
[8780e30]900 }
[faddbd8]901 ;
902
[51b73452]903unary_expression:
[4d51835]904 postfix_expression
[c6b1105]905 // first location where constant/string can have operator applied: sizeof 3/sizeof "abc" still requires
906 // semantics checks, e.g., ++3, 3--, *3, &&3
[51b1202]907 | constant
[ab57786]908 | string_literal
[32d6fdc]909 { $$ = $1; }
[4d51835]910 | EXTENSION cast_expression // GCC
[e04ef3a]911 { $$ = $2->set_extension( true ); }
[c6b1105]912 // '*' ('&') is separated from unary_operator because of shift/reduce conflict in:
913 // { * X; } // dereference X
914 // { * int X; } // CFA declaration of pointer to int
[51e076e]915 | ptrref_operator cast_expression // CFA
[9706554]916 {
917 switch ( $1 ) {
[bb7422a]918 case OperKinds::AddressOf:
919 $$ = new ExpressionNode( new ast::AddressExpr( maybeMoveBuild( $2 ) ) );
[9706554]920 break;
[bb7422a]921 case OperKinds::PointTo:
922 $$ = new ExpressionNode( build_unary_val( yylloc, $1, $2 ) );
[9706554]923 break;
[bb7422a]924 case OperKinds::And:
925 $$ = new ExpressionNode( new ast::AddressExpr( new ast::AddressExpr( maybeMoveBuild( $2 ) ) ) );
[5809461]926 break;
[bb7422a]927 default:
[9706554]928 assert( false );
929 }
930 }
[4d51835]931 | unary_operator cast_expression
[bb7422a]932 { $$ = new ExpressionNode( build_unary_val( yylloc, $1, $2 ) ); }
[dd51906]933 | ICR unary_expression
[bb7422a]934 { $$ = new ExpressionNode( build_unary_val( yylloc, OperKinds::Incr, $2 ) ); }
[dd51906]935 | DECR unary_expression
[bb7422a]936 { $$ = new ExpressionNode( build_unary_val( yylloc, OperKinds::Decr, $2 ) ); }
[4d51835]937 | SIZEOF unary_expression
[b6f2e7ab]938 { $$ = new ExpressionNode( new ast::SizeofExpr( yylloc, new ast::TypeofType( maybeMoveBuild( $2 ) ) ) ); }
[84d58c5]939 | SIZEOF '(' type_no_function ')'
[bb7422a]940 { $$ = new ExpressionNode( new ast::SizeofExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
[cd28605]941 | SIZEOF '(' attribute_list type_no_function ')'
942 { $$ = new ExpressionNode( new ast::SizeofExpr( yylloc, maybeMoveBuildType( $4->addQualifiers( $3 ) ) ) ); }
[d1625f8]943 | ALIGNOF unary_expression // GCC, variable alignment
[b6f2e7ab]944 { $$ = new ExpressionNode( new ast::AlignofExpr( yylloc, new ast::TypeofType( maybeMoveBuild( $2 ) ) ) ); }
[a2e0687]945 | ALIGNOF '(' type_no_function ')' // GCC, type alignment
[bb7422a]946 { $$ = new ExpressionNode( new ast::AlignofExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
[7e13b11]947
948 // Cannot use rule "type", which includes cfa_abstract_function, for sizeof/alignof, because of S/R problems on
949 // look ahead, so the cfa_abstract_function is factored out.
950 | SIZEOF '(' cfa_abstract_function ')'
951 { $$ = new ExpressionNode( new ast::SizeofExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
952 | ALIGNOF '(' cfa_abstract_function ')' // GCC, type alignment
953 { $$ = new ExpressionNode( new ast::AlignofExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
954
[033ff37]955 | OFFSETOF '(' type_no_function ',' identifier ')'
[bb7422a]956 { $$ = new ExpressionNode( build_offsetOf( yylloc, $3, build_varref( yylloc, $5 ) ) ); }
[7e13b11]957 | TYPEID '(' type ')'
[1f652a7]958 {
959 SemanticError( yylloc, "typeid name is currently unimplemented." ); $$ = nullptr;
960 // $$ = new ExpressionNode( build_offsetOf( $3, build_varref( $5 ) ) );
961 }
[0c327ce]962 | COUNTOF unary_expression
[857b5f9]963 { $$ = new ExpressionNode( new ast::CountofExpr( yylloc, new ast::TypeofType( maybeMoveBuild( $2 ) ) ) ); }
[a8ced63]964 | COUNTOF '(' type_no_function ')'
[857b5f9]965 { $$ = new ExpressionNode( new ast::CountofExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
[4d51835]966 ;
[51b73452]967
[dd51906]968ptrref_operator:
[d9e2280]969 '*' { $$ = OperKinds::PointTo; }
970 | '&' { $$ = OperKinds::AddressOf; }
[c6b1105]971 // GCC, address of label must be handled by semantic check for ref,ref,label
[9f07232]972 | ANDAND { $$ = OperKinds::And; }
[dd51906]973 ;
974
[51b73452]975unary_operator:
[d9e2280]976 '+' { $$ = OperKinds::UnPlus; }
977 | '-' { $$ = OperKinds::UnMinus; }
978 | '!' { $$ = OperKinds::Neg; }
979 | '~' { $$ = OperKinds::BitNeg; }
[4d51835]980 ;
[51b73452]981
982cast_expression:
[4d51835]983 unary_expression
[84d58c5]984 | '(' type_no_function ')' cast_expression
[bb7422a]985 { $$ = new ExpressionNode( build_cast( yylloc, $2, $4 ) ); }
[e307e12]986 | '(' aggregate_control '&' ')' cast_expression // CFA
[bb7422a]987 { $$ = new ExpressionNode( build_keyword_cast( yylloc, $2, $5 ) ); }
[24711a3]988 | '(' aggregate_control '*' ')' cast_expression // CFA
[bb7422a]989 { $$ = new ExpressionNode( build_keyword_cast( yylloc, $2, $5 ) ); }
[fae90d5f]990 | '(' VIRTUAL ')' cast_expression // CFA
[6cef439]991 { $$ = new ExpressionNode( new ast::VirtualCastExpr( yylloc, maybeMoveBuild( $4 ), nullptr ) ); }
[fae90d5f]992 | '(' VIRTUAL type_no_function ')' cast_expression // CFA
[bb7422a]993 { $$ = new ExpressionNode( new ast::VirtualCastExpr( yylloc, maybeMoveBuild( $5 ), maybeMoveBuildType( $3 ) ) ); }
[a16f2b6]994 | '(' RETURN type_no_function ')' cast_expression // CFA (ASCRIPTION)
[ecf3812]995 { $$ = new ExpressionNode( build_cast( yylloc, $3, $5, ast::ReturnCast ) ); }
[a16f2b6]996 | '(' COERCE type_no_function ')' cast_expression // CFA (COERCION)
[1528a2c]997 { SemanticError( yylloc, "Coerce cast is currently unimplemented." ); $$ = nullptr; }
[a16f2b6]998 | '(' qualifier_cast_list ')' cast_expression // CFA, (modify CVs of cast_expression)
[1528a2c]999 { SemanticError( yylloc, "Qualifier cast is currently unimplemented." ); $$ = nullptr; }
[84d58c5]1000// | '(' type_no_function ')' tuple
[bb7422a]1001// { $$ = new ast::ExpressionNode( build_cast( yylloc, $2, $4 ) ); }
[4d51835]1002 ;
[51b73452]1003
[1528a2c]1004qualifier_cast_list:
1005 cast_modifier type_qualifier_name
1006 | cast_modifier MUTEX
1007 | qualifier_cast_list cast_modifier type_qualifier_name
1008 | qualifier_cast_list cast_modifier MUTEX
1009 ;
1010
1011cast_modifier:
1012 '-'
1013 | '+'
1014 ;
1015
[994d080]1016exponential_expression:
[4d51835]1017 cast_expression
[994d080]1018 | exponential_expression '\\' cast_expression
[bb7422a]1019 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Exp, $1, $3 ) ); }
[994d080]1020 ;
1021
1022multiplicative_expression:
1023 exponential_expression
1024 | multiplicative_expression '*' exponential_expression
[bb7422a]1025 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Mul, $1, $3 ) ); }
[994d080]1026 | multiplicative_expression '/' exponential_expression
[bb7422a]1027 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Div, $1, $3 ) ); }
[994d080]1028 | multiplicative_expression '%' exponential_expression
[bb7422a]1029 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Mod, $1, $3 ) ); }
[4d51835]1030 ;
[51b73452]1031
1032additive_expression:
[4d51835]1033 multiplicative_expression
1034 | additive_expression '+' multiplicative_expression
[bb7422a]1035 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Plus, $1, $3 ) ); }
[4d51835]1036 | additive_expression '-' multiplicative_expression
[bb7422a]1037 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Minus, $1, $3 ) ); }
[4d51835]1038 ;
[51b73452]1039
1040shift_expression:
[4d51835]1041 additive_expression
1042 | shift_expression LS additive_expression
[bb7422a]1043 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::LShift, $1, $3 ) ); }
[4d51835]1044 | shift_expression RS additive_expression
[bb7422a]1045 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::RShift, $1, $3 ) ); }
[4d51835]1046 ;
[51b73452]1047
1048relational_expression:
[4d51835]1049 shift_expression
1050 | relational_expression '<' shift_expression
[bb7422a]1051 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::LThan, $1, $3 ) ); }
[4d51835]1052 | relational_expression '>' shift_expression
[bb7422a]1053 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::GThan, $1, $3 ) ); }
[4d51835]1054 | relational_expression LE shift_expression
[bb7422a]1055 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::LEThan, $1, $3 ) ); }
[4d51835]1056 | relational_expression GE shift_expression
[bb7422a]1057 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::GEThan, $1, $3 ) ); }
[4d51835]1058 ;
[51b73452]1059
1060equality_expression:
[4d51835]1061 relational_expression
1062 | equality_expression EQ relational_expression
[bb7422a]1063 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Eq, $1, $3 ) ); }
[4d51835]1064 | equality_expression NE relational_expression
[bb7422a]1065 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Neq, $1, $3 ) ); }
[4d51835]1066 ;
[51b73452]1067
1068AND_expression:
[4d51835]1069 equality_expression
1070 | AND_expression '&' equality_expression
[bb7422a]1071 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::BitAnd, $1, $3 ) ); }
[4d51835]1072 ;
[51b73452]1073
1074exclusive_OR_expression:
[4d51835]1075 AND_expression
1076 | exclusive_OR_expression '^' AND_expression
[bb7422a]1077 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Xor, $1, $3 ) ); }
[4d51835]1078 ;
[51b73452]1079
1080inclusive_OR_expression:
[4d51835]1081 exclusive_OR_expression
1082 | inclusive_OR_expression '|' exclusive_OR_expression
[bb7422a]1083 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::BitOr, $1, $3 ) ); }
[4d51835]1084 ;
[51b73452]1085
1086logical_AND_expression:
[4d51835]1087 inclusive_OR_expression
1088 | logical_AND_expression ANDAND inclusive_OR_expression
[bb7422a]1089 { $$ = new ExpressionNode( build_and_or( yylloc, $1, $3, ast::AndExpr ) ); }
[4d51835]1090 ;
[51b73452]1091
1092logical_OR_expression:
[4d51835]1093 logical_AND_expression
1094 | logical_OR_expression OROR logical_AND_expression
[bb7422a]1095 { $$ = new ExpressionNode( build_and_or( yylloc, $1, $3, ast::OrExpr ) ); }
[4d51835]1096 ;
[51b73452]1097
1098conditional_expression:
[4d51835]1099 logical_OR_expression
1100 | logical_OR_expression '?' comma_expression ':' conditional_expression
[bb7422a]1101 { $$ = new ExpressionNode( build_cond( yylloc, $1, $3, $5 ) ); }
[4d51835]1102 | logical_OR_expression '?' /* empty */ ':' conditional_expression // GCC, omitted first operand
[2810700]1103 { $$ = new ExpressionNode( build_cond( yylloc, $1, nullptr, $4 ) ); }
[4d51835]1104 ;
[51b73452]1105
1106constant_expression:
[4d51835]1107 conditional_expression
1108 ;
[51b73452]1109
[c1b0b8f]1110argument_expression_list_opt:
1111 // empty
1112 { $$ = nullptr; }
1113 | argument_expression_list
1114 ;
1115
1116argument_expression_list:
1117 argument_expression
1118 // | argument_expression_list_opt ',' argument_expression // CFA, allow empty argument
1119 | argument_expression_list ',' argument_expression // no empty argument
1120 { $$ = $1->set_last( $3 ); }
1121 ;
1122
1123argument_expression:
1124 '?' // CFA, default parameter
[daa4cc1]1125 // { SemanticError( yylloc, "Argument to default parameter is currently unimplemented." ); $$ = nullptr; }
1126 { $$ = new ExpressionNode( build_constantInteger( yylloc, *new string( "2" ) ) ); }
[c1b0b8f]1127 | '?' identifier '=' assignment_expression // CFA, keyword argument
[daa4cc1]1128 // { SemanticError( yylloc, "keyword argument is currently unimplemented." ); $$ = nullptr; }
1129 { $$ = $4; }
[c1b0b8f]1130 | assignment_expression
1131 ;
1132
[51b73452]1133assignment_expression:
[4d51835]1134 // CFA, assignment is separated from assignment_operator to ensure no assignment operations for tuples
1135 conditional_expression
1136 | unary_expression assignment_operator assignment_expression
[9867cdb]1137 {
[25744d2]1138// if ( $2 == OperKinds::AtAssn ) {
1139// SemanticError( yylloc, "C @= assignment is currently unimplemented." ); $$ = nullptr;
1140// } else {
[bb7422a]1141 $$ = new ExpressionNode( build_binary_val( yylloc, $2, $1, $3 ) );
[25744d2]1142// } // if
[9867cdb]1143 }
[7fdb94e1]1144 | unary_expression '=' '{' initializer_list_opt comma_opt '}'
[fae90d5f]1145 { SemanticError( yylloc, "Initializer assignment is currently unimplemented." ); $$ = nullptr; }
[4d51835]1146 ;
[51b73452]1147
1148assignment_expression_opt:
[4d51835]1149 // empty
[d1625f8]1150 { $$ = nullptr; }
[4d51835]1151 | assignment_expression
1152 ;
[b87a5ed]1153
[9706554]1154assignment_operator:
[f9c3100]1155 simple_assignment_operator
1156 | compound_assignment_operator
1157 ;
1158
1159simple_assignment_operator:
[d9e2280]1160 '=' { $$ = OperKinds::Assign; }
[f9c3100]1161 | ATassign { $$ = OperKinds::AtAssn; } // CFA
1162 ;
1163
1164compound_assignment_operator:
1165 EXPassign { $$ = OperKinds::ExpAssn; }
[d9e2280]1166 | MULTassign { $$ = OperKinds::MulAssn; }
1167 | DIVassign { $$ = OperKinds::DivAssn; }
1168 | MODassign { $$ = OperKinds::ModAssn; }
1169 | PLUSassign { $$ = OperKinds::PlusAssn; }
1170 | MINUSassign { $$ = OperKinds::MinusAssn; }
1171 | LSassign { $$ = OperKinds::LSAssn; }
1172 | RSassign { $$ = OperKinds::RSAssn; }
1173 | ANDassign { $$ = OperKinds::AndAssn; }
1174 | ERassign { $$ = OperKinds::ERAssn; }
1175 | ORassign { $$ = OperKinds::OrAssn; }
[413ad05]1176 ;
[9706554]1177
[b87a5ed]1178tuple: // CFA, tuple
[de62360d]1179 // CFA, one assignment_expression is factored out of comma_expression to eliminate a shift/reduce conflict with
[c0aa336]1180 // comma_expression in cfa_identifier_parameter_array and cfa_abstract_array
[1a8b17a]1181 '[' ',' ']'
[dd54139]1182 // { $$ = new ExpressionNode( build_tuple( yylloc, nullptr ) ); }
1183 { SemanticError( yylloc, "Empty tuple is meaningless." ); $$ = nullptr; }
[1a8b17a]1184 | '[' assignment_expression ',' ']'
1185 { $$ = new ExpressionNode( build_tuple( yylloc, $2 ) ); }
1186 | '[' '@' comma_opt ']'
1187 { SemanticError( yylloc, "Eliding tuple element with '@' is currently unimplemented." ); $$ = nullptr; }
1188 | '[' assignment_expression ',' tuple_expression_list comma_opt ']'
[917f67dd]1189 { $$ = new ExpressionNode( build_tuple( yylloc, $2->set_last( $4 ) ) ); }
[1a8b17a]1190 | '[' '@' ',' tuple_expression_list comma_opt ']'
1191 { SemanticError( yylloc, "Eliding tuple element with '@' is currently unimplemented." ); $$ = nullptr; }
[4d51835]1192 ;
[51b73452]1193
1194tuple_expression_list:
[0a6d8204]1195 assignment_expression
1196 | '@' // CFA
1197 { SemanticError( yylloc, "Eliding tuple element with '@' is currently unimplemented." ); $$ = nullptr; }
1198 | tuple_expression_list ',' assignment_expression
[b93c544]1199 { $$ = $1->set_last( $3 ); }
[0a6d8204]1200 | tuple_expression_list ',' '@'
1201 { SemanticError( yylloc, "Eliding tuple element with '@' is currently unimplemented." ); $$ = nullptr; }
[4d51835]1202 ;
[51b73452]1203
1204comma_expression:
[4d51835]1205 assignment_expression
[9706554]1206 | comma_expression ',' assignment_expression
[bb7422a]1207 { $$ = new ExpressionNode( new ast::CommaExpr( yylloc, maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
[4d51835]1208 ;
[51b73452]1209
1210comma_expression_opt:
[4d51835]1211 // empty
[58dd019]1212 { $$ = nullptr; }
[4d51835]1213 | comma_expression
1214 ;
[51b73452]1215
[e1d66c84]1216// ************************** STATEMENTS *******************************
[51b73452]1217
1218statement:
[cd28605]1219 labelled_statement
[4d51835]1220 | compound_statement
[c0a33d2]1221 | expression_statement
[4d51835]1222 | selection_statement
1223 | iteration_statement
1224 | jump_statement
[8b47e50]1225 | with_statement
[b6b3c42]1226 | mutex_statement
[5b2edbc]1227 | waitfor_statement
[9fd9d015]1228 | waituntil_statement
[11ab0b4a]1229 | corun_statement
1230 | cofor_statement
[4d51835]1231 | exception_statement
[2a8427c6]1232 | enable_disable_statement
[fae90d5f]1233 { SemanticError( yylloc, "enable/disable statement is currently unimplemented." ); $$ = nullptr; }
[4d51835]1234 | asm_statement
[61fc4f6]1235 | DIRECTIVE
[bb7422a]1236 { $$ = new StatementNode( build_directive( yylloc, $1 ) ); }
[b9be000b]1237 ;
[51b73452]1238
[cd28605]1239labelled_statement:
[033ff37]1240 // labels cannot be identifiers 0 or 1
[44a81853]1241 identifier_or_type_name ':' attribute_list_opt statement
[bb7422a]1242 { $$ = $4->add_label( yylloc, $1, $3 ); }
[0442f93f]1243 | identifier_or_type_name ':' attribute_list_opt error // invalid syntax rule
[afe9e45]1244 {
[b1f2007d]1245 SemanticError( yylloc, "syntx error, label \"%s\" must be associated with a statement, "
1246 "where a declaration, case, or default is not a statement.\n"
1247 "Move the label or terminate with a semicolon.", $1.str->c_str() );
[afe9e45]1248 $$ = nullptr;
1249 }
[4d51835]1250 ;
[51b73452]1251
1252compound_statement:
[4d51835]1253 '{' '}'
[bb7422a]1254 { $$ = new StatementNode( build_compound( yylloc, (StatementNode *)0 ) ); }
[35718a9]1255 | '{' push
[5e25953]1256 local_label_declaration_opt // GCC, local labels appear at start of block
[9bd6105]1257 statement_decl_list // C99, intermix declarations and statements
[c0aa336]1258 pop '}'
[bb7422a]1259 { $$ = new StatementNode( build_compound( yylloc, $4 ) ); }
[4d51835]1260 ;
[b87a5ed]1261
[9bd6105]1262statement_decl_list: // C99
1263 statement_decl
[35718a9]1264 | statement_decl_list statement_decl
[6d01d89]1265 { assert( $1 ); $1->set_last( $2 ); $$ = $1; }
[4d51835]1266 ;
[51b73452]1267
[9bd6105]1268statement_decl:
[cd28605]1269 attribute_list_opt declaration // CFA, new & old style declarations
1270 { distAttr( $1, $2 ); $$ = new StatementNode( $2 ); }
1271 | attribute_list_opt EXTENSION declaration // GCC
1272 { distAttr( $1, $3 ); distExt( $3 ); $$ = new StatementNode( $3 ); }
1273 | attribute_list_opt function_definition
1274 { distAttr( $1, $2 ); $$ = new StatementNode( $2 ); }
1275 | attribute_list_opt EXTENSION function_definition // GCC
1276 { distAttr( $1, $3 ); distExt( $3 ); $$ = new StatementNode( $3 ); }
1277 | attribute_list_opt statement // FIX ME!
1278 { $$ = $2->addQualifiers( $1 ); }
[4d51835]1279 ;
[51b73452]1280
[9bd6105]1281statement_list_nodecl:
[cd28605]1282 attribute_list_opt statement
1283 { $$ = $2->addQualifiers( $1 ); } // FIX ME!
1284 | statement_list_nodecl attribute_list_opt statement
1285 { assert( $1 ); $1->set_last( $3->addQualifiers( $2 ) ); $$ = $1; } // FIX ME!
[0442f93f]1286 | statement_list_nodecl error // invalid syntax rule
[253d0b4]1287 { SemanticError( yylloc, "illegal syntax, declarations only allowed at the start of the switch body,"
[7e13b11]1288 " i.e., after the '{'." ); $$ = nullptr; }
[4d51835]1289 ;
[51b73452]1290
[cd28605]1291expression_statement: // expression or null statement
[4d51835]1292 comma_expression_opt ';'
[bb7422a]1293 { $$ = new StatementNode( build_expr( yylloc, $1 ) ); }
[4d51835]1294 ;
[51b73452]1295
[7e13b11]1296// "if", "switch", and "choose" require parenthesis around the conditional. See the following ambiguities without
1297// parenthesis:
1298//
[446740a]1299// if x + y + z; => if ( x ) + y + z or if ( x + y ) + z
[647e2ea]1300//
[7e13b11]1301// switch O { }
1302//
1303// O{} => object-constructor for conditional, switch body ???
1304// O{} => O for conditional followed by switch body
1305//
1306// C++ has this problem, as it has the same constructor syntax.
1307//
1308// switch sizeof ( T ) { }
1309//
1310// sizeof ( T ) => sizeof of T for conditional followed by switch body
1311// sizeof ( T ) => sizeof of compound literal (T){ }, closing parenthesis ???
1312//
1313// Note the two grammar rules for sizeof (alignof)
1314//
1315// | SIZEOF unary_expression
1316// | SIZEOF '(' type_no_function ')'
1317//
1318// where the first DOES NOT require parenthesis! And C++ inherits this problem from C.
[647e2ea]1319
[51b73452]1320selection_statement:
[647e2ea]1321 IF '(' conditional_declaration ')' statement %prec THEN
1322 // explicitly deal with the shift/reduce conflict on if/else
1323 { $$ = new StatementNode( build_if( yylloc, $3, maybe_build_compound( yylloc, $5 ), nullptr ) ); }
1324 | IF '(' conditional_declaration ')' statement ELSE statement
1325 { $$ = new StatementNode( build_if( yylloc, $3, maybe_build_compound( yylloc, $5 ), maybe_build_compound( yylloc, $7 ) ) ); }
[4cc585b]1326 | SWITCH '(' comma_expression ')' case_clause
[bb7422a]1327 { $$ = new StatementNode( build_switch( yylloc, true, $3, $5 ) ); }
[35718a9]1328 | SWITCH '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt pop '}' // CFA
[4e06c1e]1329 {
[bb7422a]1330 StatementNode *sw = new StatementNode( build_switch( yylloc, true, $3, $8 ) );
[4e06c1e]1331 // The semantics of the declaration list is changed to include associated initialization, which is performed
1332 // *before* the transfer to the appropriate case clause by hoisting the declarations into a compound
1333 // statement around the switch. Statements after the initial declaration list can never be executed, and
[8688ce1]1334 // therefore, are removed from the grammar even though C allows it. The change also applies to choose
1335 // statement.
[b93c544]1336 $$ = $7 ? new StatementNode( build_compound( yylloc, (new StatementNode( $7 ))->set_last( sw ) ) ) : sw;
[4e06c1e]1337 }
[0442f93f]1338 | SWITCH '(' comma_expression ')' '{' error '}' // CFA, invalid syntax rule error
[55266c7]1339 { SemanticError( yylloc, "synatx error, declarations can only appear before the list of case clauses." ); $$ = nullptr; }
[4d51835]1340 | CHOOSE '(' comma_expression ')' case_clause // CFA
[bb7422a]1341 { $$ = new StatementNode( build_switch( yylloc, false, $3, $5 ) ); }
[35718a9]1342 | CHOOSE '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt pop '}' // CFA
[4e06c1e]1343 {
[bb7422a]1344 StatementNode *sw = new StatementNode( build_switch( yylloc, false, $3, $8 ) );
[b93c544]1345 $$ = $7 ? new StatementNode( build_compound( yylloc, (new StatementNode( $7 ))->set_last( sw ) ) ) : sw;
[4e06c1e]1346 }
[0442f93f]1347 | CHOOSE '(' comma_expression ')' '{' error '}' // CFA, invalid syntax rule
[253d0b4]1348 { SemanticError( yylloc, "illegal syntax, declarations can only appear before the list of case clauses." ); $$ = nullptr; }
[4d51835]1349 ;
[b87a5ed]1350
[473d1da0]1351conditional_declaration:
[35718a9]1352 comma_expression
[569b118]1353 { $$ = new CondCtrl( nullptr, $1 ); }
[35718a9]1354 | c_declaration // no semi-colon
[569b118]1355 { $$ = new CondCtrl( $1, nullptr ); }
[35718a9]1356 | cfa_declaration // no semi-colon
[569b118]1357 { $$ = new CondCtrl( $1, nullptr ); }
[6d49ea3]1358 | declaration comma_expression // semi-colon separated
[569b118]1359 { $$ = new CondCtrl( $1, $2 ); }
[9fd9d015]1360 ;
[936e9f4]1361
[de62360d]1362// CASE and DEFAULT clauses are only allowed in the SWITCH statement, precluding Duff's device. In addition, a case
1363// clause allows a list of values and subranges.
[b87a5ed]1364
1365case_value: // CFA
[4d51835]1366 constant_expression { $$ = $1; }
1367 | constant_expression ELLIPSIS constant_expression // GCC, subrange
[bb7422a]1368 { $$ = new ExpressionNode( new ast::RangeExpr( yylloc, maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
[4d51835]1369 | subrange // CFA, subrange
1370 ;
[b87a5ed]1371
1372case_value_list: // CFA
[6611177]1373 case_value { $$ = new ClauseNode( build_case( yylloc, $1 ) ); }
[064e3ff]1374 // convert case list, e.g., "case 1, 3, 5:" into "case 1: case 3: case 5"
[6611177]1375 | case_value_list ',' case_value { $$ = $1->set_last( new ClauseNode( build_case( yylloc, $3 ) ) ); }
[4d51835]1376 ;
[b87a5ed]1377
1378case_label: // CFA
[0442f93f]1379 CASE error // invalid syntax rule
[253d0b4]1380 { SemanticError( yylloc, "illegal syntax, case list missing after case." ); $$ = nullptr; }
[5c216b4]1381 | CASE case_value_list ':' { $$ = $2; }
[0442f93f]1382 | CASE case_value_list error // invalid syntax rule
[253d0b4]1383 { SemanticError( yylloc, "illegal syntax, colon missing after case list." ); $$ = nullptr; }
[6611177]1384 | DEFAULT ':' { $$ = new ClauseNode( build_default( yylloc ) ); }
[4d51835]1385 // A semantic check is required to ensure only one default clause per switch/choose statement.
[65ef0cd]1386 | DEFAULT error // invalid syntax rule
[253d0b4]1387 { SemanticError( yylloc, "illegal syntax, colon missing after default." ); $$ = nullptr; }
[4d51835]1388 ;
[b87a5ed]1389
1390case_label_list: // CFA
[4d51835]1391 case_label
[6611177]1392 | case_label_list case_label { $$ = $1->set_last( $2 ); }
[4d51835]1393 ;
[b87a5ed]1394
1395case_clause: // CFA
[bb7422a]1396 case_label_list statement { $$ = $1->append_last_case( maybe_build_compound( yylloc, $2 ) ); }
[4d51835]1397 ;
[b87a5ed]1398
1399switch_clause_list_opt: // CFA
[4d51835]1400 // empty
[58dd019]1401 { $$ = nullptr; }
[4d51835]1402 | switch_clause_list
1403 ;
[b87a5ed]1404
1405switch_clause_list: // CFA
[9bd6105]1406 case_label_list statement_list_nodecl
[bb7422a]1407 { $$ = $1->append_last_case( new StatementNode( build_compound( yylloc, $2 ) ) ); }
[9bd6105]1408 | switch_clause_list case_label_list statement_list_nodecl
[6611177]1409 { $$ = $1->set_last( $2->append_last_case( new StatementNode( build_compound( yylloc, $3 ) ) ) ); }
[4d51835]1410 ;
[b87a5ed]1411
[51b73452]1412iteration_statement:
[5695645]1413 WHILE '(' ')' statement %prec THEN // CFA => while ( 1 )
[569b118]1414 { $$ = new StatementNode( build_while( yylloc, new CondCtrl( nullptr, NEW_ONE ), maybe_build_compound( yylloc, $4 ) ) ); }
[5695645]1415 | WHILE '(' ')' statement ELSE statement // CFA
[86b8d16]1416 {
[569b118]1417 $$ = new StatementNode( build_while( yylloc, new CondCtrl( nullptr, NEW_ONE ), maybe_build_compound( yylloc, $4 ) ) );
[3d937e2]1418 SemanticWarning( yylloc, Warning::SuperfluousElse );
[86b8d16]1419 }
[473d1da0]1420 | WHILE '(' conditional_declaration ')' statement %prec THEN
[bb7422a]1421 { $$ = new StatementNode( build_while( yylloc, $3, maybe_build_compound( yylloc, $5 ) ) ); }
[473d1da0]1422 | WHILE '(' conditional_declaration ')' statement ELSE statement // CFA
[bb7422a]1423 { $$ = new StatementNode( build_while( yylloc, $3, maybe_build_compound( yylloc, $5 ), $7 ) ); }
[f271bdd]1424 | DO statement WHILE '(' ')' ';' // CFA => do while( 1 )
[bb7422a]1425 { $$ = new StatementNode( build_do_while( yylloc, NEW_ONE, maybe_build_compound( yylloc, $2 ) ) ); }
[5695645]1426 | DO statement WHILE '(' ')' ELSE statement // CFA
[86b8d16]1427 {
[bb7422a]1428 $$ = new StatementNode( build_do_while( yylloc, NEW_ONE, maybe_build_compound( yylloc, $2 ) ) );
[3d937e2]1429 SemanticWarning( yylloc, Warning::SuperfluousElse );
[86b8d16]1430 }
1431 | DO statement WHILE '(' comma_expression ')' ';'
[bb7422a]1432 { $$ = new StatementNode( build_do_while( yylloc, $5, maybe_build_compound( yylloc, $2 ) ) ); }
[efc8f3e]1433 | DO statement WHILE '(' comma_expression ')' ELSE statement // CFA
[bb7422a]1434 { $$ = new StatementNode( build_do_while( yylloc, $5, maybe_build_compound( yylloc, $2 ), $8 ) ); }
[86b8d16]1435 | FOR '(' ')' statement %prec THEN // CFA => for ( ;; )
[bb7422a]1436 { $$ = new StatementNode( build_for( yylloc, new ForCtrl( nullptr, nullptr, nullptr ), maybe_build_compound( yylloc, $4 ) ) ); }
[86b8d16]1437 | FOR '(' ')' statement ELSE statement // CFA
1438 {
[bb7422a]1439 $$ = new StatementNode( build_for( yylloc, new ForCtrl( nullptr, nullptr, nullptr ), maybe_build_compound( yylloc, $4 ) ) );
[3d937e2]1440 SemanticWarning( yylloc, Warning::SuperfluousElse );
[86b8d16]1441 }
[efc8f3e]1442 | FOR '(' for_control_expression_list ')' statement %prec THEN
[bb7422a]1443 { $$ = new StatementNode( build_for( yylloc, $3, maybe_build_compound( yylloc, $5 ) ) ); }
[efc8f3e]1444 | FOR '(' for_control_expression_list ')' statement ELSE statement // CFA
[bb7422a]1445 { $$ = new StatementNode( build_for( yylloc, $3, maybe_build_compound( yylloc, $5 ), $7 ) ); }
[a73c16e]1446 ;
1447
[6d01d89]1448for_control_expression_list:
1449 for_control_expression
1450 | for_control_expression_list ':' for_control_expression
[67d4e37]1451 // ForCtrl + ForCtrl:
1452 // init + init => multiple declaration statements that are hoisted
1453 // condition + condition => (expression) && (expression)
1454 // change + change => (expression), (expression)
1455 {
1456 $1->init->set_last( $3->init );
1457 if ( $1->condition ) {
1458 if ( $3->condition ) {
[bb7422a]1459 $1->condition->expr.reset( new ast::LogicalExpr( yylloc, $1->condition->expr.release(), $3->condition->expr.release(), ast::AndExpr ) );
[67d4e37]1460 } // if
1461 } else $1->condition = $3->condition;
1462 if ( $1->change ) {
1463 if ( $3->change ) {
[bb7422a]1464 $1->change->expr.reset( new ast::CommaExpr( yylloc, $1->change->expr.release(), $3->change->expr.release() ) );
[67d4e37]1465 } // if
1466 } else $1->change = $3->change;
1467 $$ = $1;
1468 }
[6d01d89]1469 ;
1470
[51b73452]1471for_control_expression:
[6d01d89]1472 ';' comma_expression_opt ';' comma_expression_opt
[1cdc052]1473 { $$ = new ForCtrl( nullptr, $2, $4 ); }
[6d01d89]1474 | comma_expression ';' comma_expression_opt ';' comma_expression_opt
[1cdc052]1475 {
[253d0b4]1476 $$ = new ForCtrl( $1 ? new StatementNode( new ast::ExprStmt( yylloc, maybeMoveBuild( $1 ) ) ) : nullptr, $3, $5 );
[1cdc052]1477 }
[6d01d89]1478 | declaration comma_expression_opt ';' comma_expression_opt // C99, declaration has ';'
[1cdc052]1479 { $$ = new ForCtrl( new StatementNode( $1 ), $2, $4 ); }
[67d4e37]1480
[aa122e9]1481 | '@' ';' comma_expression // CFA, empty loop-index
[1cdc052]1482 { $$ = new ForCtrl( nullptr, $3, nullptr ); }
[aa122e9]1483 | '@' ';' comma_expression ';' comma_expression // CFA, empty loop-index
[1cdc052]1484 { $$ = new ForCtrl( nullptr, $3, $5 ); }
[51fbba5]1485
[aa122e9]1486 | comma_expression // CFA, anonymous loop-index
[bb7422a]1487 { $$ = forCtrl( yylloc, $1, new string( DeclarationNode::anonymous.newName() ), NEW_ZERO, OperKinds::LThan, $1->clone(), NEW_ONE ); }
[e86735ba]1488 | updown comma_expression // CFA, anonymous loop-index
[bb7422a]1489 { $$ = forCtrl( yylloc, $2, new string( DeclarationNode::anonymous.newName() ), UPDOWN( $1, NEW_ZERO, $2->clone() ), $1, UPDOWN( $1, $2->clone(), NEW_ZERO ), NEW_ONE ); }
[52be5948]1490
[e86735ba]1491 | comma_expression updownS comma_expression // CFA, anonymous loop-index
[bb7422a]1492 { $$ = forCtrl( yylloc, $1, new string( DeclarationNode::anonymous.newName() ), UPDOWN( $2, $1->clone(), $3 ), $2, UPDOWN( $2, $3->clone(), $1->clone() ), NEW_ONE ); }
[e86735ba]1493 | '@' updownS comma_expression // CFA, anonymous loop-index
[dbedd71]1494 {
[4fee301]1495 if ( $2 == OperKinds::LThan || $2 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
[bb7422a]1496 else $$ = forCtrl( yylloc, $3, new string( DeclarationNode::anonymous.newName() ), $3->clone(), $2, nullptr, NEW_ONE );
[dbedd71]1497 }
[e86735ba]1498 | comma_expression updownS '@' // CFA, anonymous loop-index
[dbedd71]1499 {
[4fee301]1500 if ( $2 == OperKinds::LThan || $2 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
[ed9a1ae]1501 else { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
[dbedd71]1502 }
[e86735ba]1503
1504 | comma_expression updownS comma_expression '~' comma_expression // CFA, anonymous loop-index
[bb7422a]1505 { $$ = forCtrl( yylloc, $1, new string( DeclarationNode::anonymous.newName() ), UPDOWN( $2, $1->clone(), $3 ), $2, UPDOWN( $2, $3->clone(), $1->clone() ), $5 ); }
[e86735ba]1506 | '@' updownS comma_expression '~' comma_expression // CFA, anonymous loop-index
[dbedd71]1507 {
[4fee301]1508 if ( $2 == OperKinds::LThan || $2 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
[bb7422a]1509 else $$ = forCtrl( yylloc, $3, new string( DeclarationNode::anonymous.newName() ), $3->clone(), $2, nullptr, $5 );
[dbedd71]1510 }
[e86735ba]1511 | comma_expression updownS '@' '~' comma_expression // CFA, anonymous loop-index
[dbedd71]1512 {
[4fee301]1513 if ( $2 == OperKinds::LThan || $2 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
[ed9a1ae]1514 else { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
[dbedd71]1515 }
[e86735ba]1516 | comma_expression updownS comma_expression '~' '@' // CFA, invalid syntax rule
[4fee301]1517 { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
[e86735ba]1518 | '@' updownS '@' // CFA, invalid syntax rule
[4fee301]1519 { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
[e86735ba]1520 | '@' updownS comma_expression '~' '@' // CFA, invalid syntax rule
[4fee301]1521 { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
[e86735ba]1522 | comma_expression updownS '@' '~' '@' // CFA, invalid syntax rule
[4fee301]1523 { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
[e86735ba]1524 | '@' updownS '@' '~' '@' // CFA, invalid syntax rule
[4fee301]1525 { SemanticError( yylloc, MISSING_ANON_FIELD ); $$ = nullptr; }
[52be5948]1526
[089b39e1]1527 // These rules accept a comma_expression for the initialization, when only an identifier is correct. Being
1528 // permissive allows for a better error message from forCtrl.
[f1aeede]1529 | comma_expression ';' comma_expression // CFA
[bb7422a]1530 { $$ = forCtrl( yylloc, $3, $1, NEW_ZERO, OperKinds::LThan, $3->clone(), NEW_ONE ); }
[e86735ba]1531 | comma_expression ';' updown comma_expression // CFA
[bb7422a]1532 { $$ = forCtrl( yylloc, $4, $1, UPDOWN( $3, NEW_ZERO, $4->clone() ), $3, UPDOWN( $3, $4->clone(), NEW_ZERO ), NEW_ONE ); }
[52be5948]1533
[e86735ba]1534 | comma_expression ';' comma_expression updownS comma_expression // CFA
[bb7422a]1535 { $$ = forCtrl( yylloc, $3, $1, UPDOWN( $4, $3->clone(), $5 ), $4, UPDOWN( $4, $5->clone(), $3->clone() ), NEW_ONE ); }
[e86735ba]1536 | comma_expression ';' '@' updownS comma_expression // CFA
[dbedd71]1537 {
[4fee301]1538 if ( $4 == OperKinds::LThan || $4 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
[bb7422a]1539 else $$ = forCtrl( yylloc, $5, $1, $5->clone(), $4, nullptr, NEW_ONE );
[dbedd71]1540 }
[e86735ba]1541 | comma_expression ';' comma_expression updownS '@' // CFA
[52be5948]1542 {
[4fee301]1543 if ( $4 == OperKinds::GThan || $4 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
[253d0b4]1544 else if ( $4 == OperKinds::LEThan ) { SemanticError( yylloc, "illegal syntax, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
[bb7422a]1545 else $$ = forCtrl( yylloc, $3, $1, $3->clone(), $4, nullptr, NEW_ONE );
[52be5948]1546 }
[e86735ba]1547 | comma_expression ';' '@' updownS '@' // CFA, invalid syntax rule
[01afd8d]1548 { SemanticError( yylloc, "illegal syntax, missing low/high value for ascending/descending range so index is uninitialized." ); $$ = nullptr; }
[d78c238]1549
[e86735ba]1550 | comma_expression ';' comma_expression updownEq comma_expression // CFA
1551 { $$ = forCtrl( yylloc, $3, $1, UPDOWN( $4, $3->clone(), $5 ), $4, UPDOWN( $4, $5->clone(), $3->clone() ), NEW_ONE ); }
1552
1553 | comma_expression ';' comma_expression updownS comma_expression '~' comma_expression // CFA
[bb7422a]1554 { $$ = forCtrl( yylloc, $3, $1, UPDOWN( $4, $3->clone(), $5 ), $4, UPDOWN( $4, $5->clone(), $3->clone() ), $7 ); }
[e86735ba]1555 | comma_expression ';' '@' updownS comma_expression '~' comma_expression // CFA, invalid syntax rule
[dbedd71]1556 {
[4fee301]1557 if ( $4 == OperKinds::LThan || $4 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
[bb7422a]1558 else $$ = forCtrl( yylloc, $5, $1, $5->clone(), $4, nullptr, $7 );
[dbedd71]1559 }
[e86735ba]1560 | comma_expression ';' comma_expression updownS '@' '~' comma_expression // CFA
[52be5948]1561 {
[4fee301]1562 if ( $4 == OperKinds::GThan || $4 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
[253d0b4]1563 else if ( $4 == OperKinds::LEThan ) { SemanticError( yylloc, "illegal syntax, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
[bb7422a]1564 else $$ = forCtrl( yylloc, $3, $1, $3->clone(), $4, nullptr, $7 );
[52be5948]1565 }
[e86735ba]1566 | comma_expression ';' comma_expression updownS comma_expression '~' '@' // CFA
[bb7422a]1567 { $$ = forCtrl( yylloc, $3, $1, UPDOWN( $4, $3->clone(), $5 ), $4, UPDOWN( $4, $5->clone(), $3->clone() ), nullptr ); }
[e86735ba]1568 | comma_expression ';' '@' updownS comma_expression '~' '@' // CFA, invalid syntax rule
[dbedd71]1569 {
[4fee301]1570 if ( $4 == OperKinds::LThan || $4 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
[bb7422a]1571 else $$ = forCtrl( yylloc, $5, $1, $5->clone(), $4, nullptr, nullptr );
[dbedd71]1572 }
[e86735ba]1573 | comma_expression ';' comma_expression updownS '@' '~' '@' // CFA
[d78c238]1574 {
[4fee301]1575 if ( $4 == OperKinds::GThan || $4 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
[253d0b4]1576 else if ( $4 == OperKinds::LEThan ) { SemanticError( yylloc, "illegal syntax, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
[bb7422a]1577 else $$ = forCtrl( yylloc, $3, $1, $3->clone(), $4, nullptr, nullptr );
[d78c238]1578 }
[e86735ba]1579 | comma_expression ';' '@' updownS '@' '~' '@' // CFA
[01afd8d]1580 { SemanticError( yylloc, "illegal syntax, missing low/high value for ascending/descending range so index is uninitialized." ); $$ = nullptr; }
[d78c238]1581
1582 | declaration comma_expression // CFA
[bb7422a]1583 { $$ = forCtrl( yylloc, $1, NEW_ZERO, OperKinds::LThan, $2, NEW_ONE ); }
[e86735ba]1584 | declaration updown comma_expression // CFA
[bb7422a]1585 { $$ = forCtrl( yylloc, $1, UPDOWN( $2, NEW_ZERO, $3 ), $2, UPDOWN( $2, $3->clone(), NEW_ZERO ), NEW_ONE ); }
[52be5948]1586
[e86735ba]1587 | declaration comma_expression updownS comma_expression // CFA
[bb7422a]1588 { $$ = forCtrl( yylloc, $1, UPDOWN( $3, $2->clone(), $4 ), $3, UPDOWN( $3, $4->clone(), $2->clone() ), NEW_ONE ); }
[e86735ba]1589 | declaration '@' updownS comma_expression // CFA
[dbedd71]1590 {
[4fee301]1591 if ( $3 == OperKinds::LThan || $3 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
[bb7422a]1592 else $$ = forCtrl( yylloc, $1, $4, $3, nullptr, NEW_ONE );
[dbedd71]1593 }
[e86735ba]1594 | declaration comma_expression updownS '@' // CFA
[52be5948]1595 {
[4fee301]1596 if ( $3 == OperKinds::GThan || $3 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
[253d0b4]1597 else if ( $3 == OperKinds::LEThan ) { SemanticError( yylloc, "illegal syntax, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
[bb7422a]1598 else $$ = forCtrl( yylloc, $1, $2, $3, nullptr, NEW_ONE );
[52be5948]1599 }
[d78c238]1600
[e86735ba]1601 | declaration comma_expression updownEq comma_expression // CFA
1602 { $$ = forCtrl( yylloc, $1, UPDOWN( $3, $2->clone(), $4 ), $3, UPDOWN( $3, $4->clone(), $2->clone() ), NEW_ONE ); }
1603
1604 | declaration comma_expression updownS comma_expression '~' comma_expression // CFA
[bb7422a]1605 { $$ = forCtrl( yylloc, $1, UPDOWN( $3, $2, $4 ), $3, UPDOWN( $3, $4->clone(), $2->clone() ), $6 ); }
[e86735ba]1606 | declaration '@' updownS comma_expression '~' comma_expression // CFA
[dbedd71]1607 {
[4fee301]1608 if ( $3 == OperKinds::LThan || $3 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
[bb7422a]1609 else $$ = forCtrl( yylloc, $1, $4, $3, nullptr, $6 );
[dbedd71]1610 }
[e86735ba]1611 | declaration comma_expression updownS '@' '~' comma_expression // CFA
[52be5948]1612 {
[4fee301]1613 if ( $3 == OperKinds::GThan || $3 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
[253d0b4]1614 else if ( $3 == OperKinds::LEThan ) { SemanticError( yylloc, "illegal syntax, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
[bb7422a]1615 else $$ = forCtrl( yylloc, $1, $2, $3, nullptr, $6 );
[52be5948]1616 }
[e86735ba]1617 | declaration comma_expression updownS comma_expression '~' '@' // CFA
[bb7422a]1618 { $$ = forCtrl( yylloc, $1, UPDOWN( $3, $2, $4 ), $3, UPDOWN( $3, $4->clone(), $2->clone() ), nullptr ); }
[e86735ba]1619 | declaration '@' updownS comma_expression '~' '@' // CFA
[dbedd71]1620 {
[4fee301]1621 if ( $3 == OperKinds::LThan || $3 == OperKinds::LEThan ) { SemanticError( yylloc, MISSING_LOW ); $$ = nullptr; }
[bb7422a]1622 else $$ = forCtrl( yylloc, $1, $4, $3, nullptr, nullptr );
[dbedd71]1623 }
[e86735ba]1624 | declaration comma_expression updownS '@' '~' '@' // CFA
[d78c238]1625 {
[4fee301]1626 if ( $3 == OperKinds::GThan || $3 == OperKinds::GEThan ) { SemanticError( yylloc, MISSING_HIGH ); $$ = nullptr; }
[253d0b4]1627 else if ( $3 == OperKinds::LEThan ) { SemanticError( yylloc, "illegal syntax, equality with missing high value is meaningless. Use \"~\"." ); $$ = nullptr; }
[bb7422a]1628 else $$ = forCtrl( yylloc, $1, $2, $3, nullptr, nullptr );
[d78c238]1629 }
[e86735ba]1630 | declaration '@' updownS '@' '~' '@' // CFA, invalid syntax rule
[01afd8d]1631 { SemanticError( yylloc, "illegal syntax, missing low/high value for ascending/descending range so index is uninitialized." ); $$ = nullptr; }
[67d4e37]1632
[3bd9508]1633 | comma_expression ';' type_type_specifier // CFA, enum type
[ca33b15]1634 {
[6d2b3dc]1635 $$ = enumRangeCtrl( $1, OperKinds::LEThan, new ExpressionNode( new ast::TypeExpr( yylloc, $3->clone()->buildType() ) ), $3 );
[ca33b15]1636 }
[e86735ba]1637 | comma_expression ';' updown enum_key // CFA, enum type, reverse direction
[d78c238]1638 {
[55ba259e]1639 if ( $3 == OperKinds::GThan ) {
1640 SemanticError( yylloc, "all enumeration ranges are equal (all values). Add an equal, e.g., ~=, -~=." ); $$ = nullptr;
1641 $3 = OperKinds::GEThan;
1642 } // if
[6d2b3dc]1643 $$ = enumRangeCtrl( $1, $3, new ExpressionNode( new ast::TypeExpr( yylloc, $4->clone()->buildType() ) ), $4 );
[d78c238]1644 }
[9fd9d015]1645 ;
[98337569]1646
[a8ced63]1647enum_key:
1648 type_name
[4941716]1649 {
1650 typedefTable.makeTypedef( *$1->symbolic.name, "enum_type_nobody 1" );
1651 $$ = DeclarationNode::newEnum( $1->symbolic.name, nullptr, false, false );
1652 }
[a8ced63]1653 | ENUM identifier
[4941716]1654 {
1655 typedefTable.makeTypedef( *$2, "enum_type_nobody 2" );
1656 $$ = DeclarationNode::newEnum( $2, nullptr, false, false );
1657 }
[a8ced63]1658 | ENUM type_name
[4941716]1659 {
1660 typedefTable.makeTypedef( *$2->symbolic.name, "enum_type_nobody 3" );
1661 $$ = DeclarationNode::newEnum( $2->symbolic.name, nullptr, false, false );
1662 }
[a8ced63]1663 ;
1664
[e86735ba]1665// This rule exists to handle the ambiguity with unary operator '~'. The rule is the same as updownS minus the '~'.
[55ba259e]1666// Specifically, "for ( ~5 )" means the complement of 5, not loop 0..4. Hence, in this case "for ( ~= 5 )", i.e., 0..5,
1667// it is not possible to just remove the '='. The entire '~=' must be removed.
[e86735ba]1668updown:
1669 ErangeUpLt
[01afd8d]1670 { $$ = OperKinds::LThan; }
[e86735ba]1671 | ErangeDownGt
[d78c238]1672 { $$ = OperKinds::GThan; }
[e86735ba]1673 | ErangeUpLe
[d78c238]1674 { $$ = OperKinds::LEThan; }
[e86735ba]1675 | ErangeDownGe
[d78c238]1676 { $$ = OperKinds::GEThan; }
[9fd9d015]1677 ;
[51b73452]1678
[e86735ba]1679updownS:
[01afd8d]1680 '~' // shorthand 0 ~ 10 => 0 +~ 10
1681 { $$ = OperKinds::LThan; }
[e86735ba]1682 | updown
[9fd9d015]1683 ;
[d78c238]1684
[e86735ba]1685updownEq:
1686 ErangeEq
1687 { $$ = OperKinds::Eq; }
1688 | ErangeNe
1689 { $$ = OperKinds::Neq; }
[d69f4bb4]1690 | ErangeDownEq
[e86735ba]1691 { $$ = OperKinds::Eq; }
1692 | ErangeDownNe
1693 { $$ = OperKinds::Neq; }
[9fd9d015]1694 ;
[cc22003]1695
[51b73452]1696jump_statement:
[44a81853]1697 GOTO identifier_or_type_name ';'
[bb7422a]1698 { $$ = new StatementNode( build_branch( yylloc, $2, ast::BranchStmt::Goto ) ); }
[4d51835]1699 | GOTO '*' comma_expression ';' // GCC, computed goto
[4e06c1e]1700 // The syntax for the GCC computed goto violates normal expression precedence, e.g., goto *i+3; => goto *(i+3);
[de62360d]1701 // whereas normal operator precedence yields goto (*i)+3;
[e82aa9df]1702 { $$ = new StatementNode( build_computedgoto( $3 ) ); }
[d96f7c4]1703 // A semantic check is required to ensure fallthrough appears only in the body of a choose statement.
1704 | FALLTHROUGH ';' // CFA
[bb7422a]1705 { $$ = new StatementNode( build_branch( yylloc, ast::BranchStmt::FallThrough ) ); }
[d96f7c4]1706 | FALLTHROUGH identifier_or_type_name ';' // CFA
[bb7422a]1707 { $$ = new StatementNode( build_branch( yylloc, $2, ast::BranchStmt::FallThrough ) ); }
[d96f7c4]1708 | FALLTHROUGH DEFAULT ';' // CFA
[bb7422a]1709 { $$ = new StatementNode( build_branch( yylloc, ast::BranchStmt::FallThroughDefault ) ); }
[4d51835]1710 | CONTINUE ';'
[de62360d]1711 // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
[bb7422a]1712 { $$ = new StatementNode( build_branch( yylloc, ast::BranchStmt::Continue ) ); }
[44a81853]1713 | CONTINUE identifier_or_type_name ';' // CFA, multi-level continue
[de62360d]1714 // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1715 // the target of the transfer appears only at the start of an iteration statement.
[bb7422a]1716 { $$ = new StatementNode( build_branch( yylloc, $2, ast::BranchStmt::Continue ) ); }
[4d51835]1717 | BREAK ';'
[de62360d]1718 // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
[bb7422a]1719 { $$ = new StatementNode( build_branch( yylloc, ast::BranchStmt::Break ) ); }
[44a81853]1720 | BREAK identifier_or_type_name ';' // CFA, multi-level exit
[de62360d]1721 // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1722 // the target of the transfer appears only at the start of an iteration statement.
[bb7422a]1723 { $$ = new StatementNode( build_branch( yylloc, $2, ast::BranchStmt::Break ) ); }
[4d51835]1724 | RETURN comma_expression_opt ';'
[bb7422a]1725 { $$ = new StatementNode( build_return( yylloc, $2 ) ); }
[c786e1d]1726 | RETURN '{' initializer_list_opt comma_opt '}' ';'
[fae90d5f]1727 { SemanticError( yylloc, "Initializer return is currently unimplemented." ); $$ = nullptr; }
[37cdd97]1728 | SUSPEND ';'
[bb7422a]1729 { $$ = new StatementNode( build_suspend( yylloc, nullptr, ast::SuspendStmt::None ) ); }
[9306559f]1730 | SUSPEND compound_statement
[bb7422a]1731 { $$ = new StatementNode( build_suspend( yylloc, $2, ast::SuspendStmt::None ) ); }
[37cdd97]1732 | SUSPEND COROUTINE ';'
[bb7422a]1733 { $$ = new StatementNode( build_suspend( yylloc, nullptr, ast::SuspendStmt::Coroutine ) ); }
[37cdd97]1734 | SUSPEND COROUTINE compound_statement
[bb7422a]1735 { $$ = new StatementNode( build_suspend( yylloc, $3, ast::SuspendStmt::Coroutine ) ); }
[37cdd97]1736 | SUSPEND GENERATOR ';'
[bb7422a]1737 { $$ = new StatementNode( build_suspend( yylloc, nullptr, ast::SuspendStmt::Generator ) ); }
[37cdd97]1738 | SUSPEND GENERATOR compound_statement
[bb7422a]1739 { $$ = new StatementNode( build_suspend( yylloc, $3, ast::SuspendStmt::Generator ) ); }
[8cc5cb0]1740 | THROW assignment_expression_opt ';' // handles rethrow
[bb7422a]1741 { $$ = new StatementNode( build_throw( yylloc, $2 ) ); }
[8cc5cb0]1742 | THROWRESUME assignment_expression_opt ';' // handles reresume
[bb7422a]1743 { $$ = new StatementNode( build_resume( yylloc, $2 ) ); }
[8cc5cb0]1744 | THROWRESUME assignment_expression_opt AT assignment_expression ';' // handles reresume
[daf1af8]1745 { $$ = new StatementNode( build_resume_at( $2, $4 ) ); }
[4d51835]1746 ;
[51b73452]1747
[8b47e50]1748with_statement:
[e78966e]1749 WITH '(' type_list ')' statement // support scoped enumeration
[bb7422a]1750 { $$ = new StatementNode( build_with( yylloc, $3, $5 ) ); }
[5b2edbc]1751 ;
1752
[bf20567]1753// If MUTEX becomes a general qualifier, there are shift/reduce conflicts, so possibly change syntax to "with mutex".
[b6b3c42]1754mutex_statement:
[bf20567]1755 MUTEX '(' argument_expression_list_opt ')' statement
1756 {
[253d0b4]1757 if ( ! $3 ) { SemanticError( yylloc, "illegal syntax, mutex argument list cannot be empty." ); $$ = nullptr; }
[bb7422a]1758 $$ = new StatementNode( build_mutex( yylloc, $3, $5 ) );
[bf20567]1759 }
[b6b3c42]1760 ;
1761
[51d6d6a]1762when_clause:
[6a276a0]1763 WHEN '(' comma_expression ')' { $$ = $3; }
[51d6d6a]1764 ;
1765
[5b2edbc]1766when_clause_opt:
1767 // empty
[135b431]1768 { $$ = nullptr; }
[51d6d6a]1769 | when_clause
[5b2edbc]1770 ;
1771
[4a063df]1772cast_expression_list:
1773 cast_expression
1774 | cast_expression_list ',' cast_expression
[a491a3c]1775 { SemanticError( yylloc, "List of mutex member is currently unimplemented." ); $$ = nullptr; }
[5b2edbc]1776 ;
1777
1778timeout:
[9fd9d015]1779 TIMEOUT '(' comma_expression ')' { $$ = $3; }
[5b2edbc]1780 ;
1781
[9fd9d015]1782wor:
1783 OROR
1784 | WOR
1785
1786waitfor:
1787 WAITFOR '(' cast_expression ')'
1788 { $$ = $3; }
1789 | WAITFOR '(' cast_expression_list ':' argument_expression_list_opt ')'
[b93c544]1790 { $$ = $3->set_last( $5 ); }
[5b2edbc]1791 ;
1792
[9fd9d015]1793wor_waitfor_clause:
[51d6d6a]1794 when_clause_opt waitfor statement %prec THEN
[9fd9d015]1795 // Called first: create header for WaitForStmt.
[bb7422a]1796 { $$ = build_waitfor( yylloc, new ast::WaitForStmt( yylloc ), $1, $2, maybe_build_compound( yylloc, $3 ) ); }
[70056ed]1797 | wor_waitfor_clause wor when_clause_opt waitfor statement
[bb7422a]1798 { $$ = build_waitfor( yylloc, $1, $3, $4, maybe_build_compound( yylloc, $5 ) ); }
[9fd9d015]1799 | wor_waitfor_clause wor when_clause_opt ELSE statement
[bb7422a]1800 { $$ = build_waitfor_else( yylloc, $1, $3, maybe_build_compound( yylloc, $5 ) ); }
[9fd9d015]1801 | wor_waitfor_clause wor when_clause_opt timeout statement %prec THEN
[bb7422a]1802 { $$ = build_waitfor_timeout( yylloc, $1, $3, $4, maybe_build_compound( yylloc, $5 ) ); }
[afe9e45]1803 // "else" must be conditional after timeout or timeout is never triggered (i.e., it is meaningless)
[65ef0cd]1804 | wor_waitfor_clause wor when_clause_opt timeout statement wor ELSE statement // invalid syntax rule
[253d0b4]1805 { SemanticError( yylloc, "illegal syntax, else clause must be conditional after timeout or timeout never triggered." ); $$ = nullptr; }
[9fd9d015]1806 | wor_waitfor_clause wor when_clause_opt timeout statement wor when_clause ELSE statement
[bb7422a]1807 { $$ = build_waitfor_else( yylloc, build_waitfor_timeout( yylloc, $1, $3, $4, maybe_build_compound( yylloc, $5 ) ), $7, maybe_build_compound( yylloc, $9 ) ); }
[5b2edbc]1808 ;
1809
1810waitfor_statement:
[9fd9d015]1811 wor_waitfor_clause %prec THEN
1812 { $$ = new StatementNode( $1 ); }
1813 ;
1814
1815wand:
1816 ANDAND
1817 | WAND
1818 ;
1819
1820waituntil:
[04c78215]1821 WAITUNTIL '(' comma_expression ')'
[9fd9d015]1822 { $$ = $3; }
1823 ;
1824
1825waituntil_clause:
1826 when_clause_opt waituntil statement
[c86b08d]1827 { $$ = build_waituntil_clause( yylloc, $1, $2, maybe_build_compound( yylloc, $3 ) ); }
[9fd9d015]1828 | '(' wor_waituntil_clause ')'
[c86b08d]1829 { $$ = $2; }
[9fd9d015]1830 ;
1831
1832wand_waituntil_clause:
1833 waituntil_clause %prec THEN
[c86b08d]1834 { $$ = $1; }
[9fd9d015]1835 | waituntil_clause wand wand_waituntil_clause
[c86b08d]1836 { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::AND, $1, $3 ); }
[9fd9d015]1837 ;
1838
1839wor_waituntil_clause:
1840 wand_waituntil_clause
[c86b08d]1841 { $$ = $1; }
[70056ed]1842 | wor_waituntil_clause wor wand_waituntil_clause
[c86b08d]1843 { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::OR, $1, $3 ); }
[9fd9d015]1844 | wor_waituntil_clause wor when_clause_opt ELSE statement
[c86b08d]1845 { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::LEFT_OR, $1, build_waituntil_else( yylloc, $3, maybe_build_compound( yylloc, $5 ) ) ); }
[9fd9d015]1846 ;
1847
1848waituntil_statement:
1849 wor_waituntil_clause %prec THEN
[f259682]1850 { $$ = new StatementNode( build_waituntil_stmt( yylloc, $1 ) ); }
[8b47e50]1851 ;
1852
[11ab0b4a]1853corun_statement:
1854 CORUN statement
[eb779d5]1855 { $$ = new StatementNode( build_corun( yylloc, $2 ) ); }
[11ab0b4a]1856 ;
1857
1858cofor_statement:
1859 COFOR '(' for_control_expression_list ')' statement
[3d9d017]1860 { $$ = new StatementNode( build_cofor( yylloc, $3, maybe_build_compound( yylloc, $5 ) ) ); }
[11ab0b4a]1861 ;
1862
[51b73452]1863exception_statement:
[bb7422a]1864 TRY compound_statement handler_clause %prec THEN
1865 { $$ = new StatementNode( build_try( yylloc, $2, $3, nullptr ) ); }
[4d51835]1866 | TRY compound_statement finally_clause
[bb7422a]1867 { $$ = new StatementNode( build_try( yylloc, $2, nullptr, $3 ) ); }
[cfaabe2c]1868 | TRY compound_statement handler_clause finally_clause
[bb7422a]1869 { $$ = new StatementNode( build_try( yylloc, $2, $3, $4 ) ); }
[4d51835]1870 ;
[51b73452]1871
1872handler_clause:
[206ecae]1873 handler_key '(' exception_declaration handler_predicate_opt ')' compound_statement
1874 { $$ = new ClauseNode( build_catch( yylloc, $1, $3, $4, $6 ) ); }
1875 | handler_clause handler_key '(' exception_declaration handler_predicate_opt ')' compound_statement
1876 { $$ = $1->set_last( new ClauseNode( build_catch( yylloc, $2, $4, $5, $7 ) ) ); }
[994d080]1877 ;
1878
1879handler_predicate_opt:
[7fdb94e1]1880 // empty
[cbce272]1881 { $$ = nullptr; }
[6a276a0]1882 | ';' conditional_expression { $$ = $2; }
[307a732]1883 ;
1884
1885handler_key:
[bb7422a]1886 CATCH { $$ = ast::Terminate; }
1887 | RECOVER { $$ = ast::Terminate; }
1888 | CATCHRESUME { $$ = ast::Resume; }
1889 | FIXUP { $$ = ast::Resume; }
[4d51835]1890 ;
[51b73452]1891
1892finally_clause:
[6611177]1893 FINALLY compound_statement { $$ = new ClauseNode( build_finally( yylloc, $2 ) ); }
[4d51835]1894 ;
[51b73452]1895
1896exception_declaration:
[d0ffed1]1897 // No SUE declaration in parameter list.
1898 type_specifier_nobody
1899 | type_specifier_nobody declarator
[c0a33d2]1900 { $$ = $2->addType( $1 ); }
[d0ffed1]1901 | type_specifier_nobody variable_abstract_declarator
[4d51835]1902 { $$ = $2->addType( $1 ); }
[033ff37]1903 | cfa_abstract_declarator_tuple identifier // CFA
[c0a33d2]1904 { $$ = $1->addName( $2 ); }
[c0aa336]1905 | cfa_abstract_declarator_tuple // CFA
[4d51835]1906 ;
[51b73452]1907
[2a8427c6]1908enable_disable_statement:
1909 enable_disable_key identifier_list compound_statement
1910 ;
1911
1912enable_disable_key:
1913 ENABLE
1914 | DISABLE
1915 ;
1916
[51b73452]1917asm_statement:
[ab57786]1918 ASM asm_volatile_opt '(' string_literal ')' ';'
[bb7422a]1919 { $$ = new StatementNode( build_asm( yylloc, $2, $4, nullptr ) ); }
[ab57786]1920 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ')' ';' // remaining GCC
[bb7422a]1921 { $$ = new StatementNode( build_asm( yylloc, $2, $4, $6 ) ); }
[ab57786]1922 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ')' ';'
[bb7422a]1923 { $$ = new StatementNode( build_asm( yylloc, $2, $4, $6, $8 ) ); }
[ab57786]1924 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ':' asm_clobbers_list_opt ')' ';'
[bb7422a]1925 { $$ = new StatementNode( build_asm( yylloc, $2, $4, $6, $8, $10 ) ); }
[cd28605]1926 | ASM asm_volatile_opt GOTO '(' string_literal ':' ':' asm_operands_opt ':' asm_clobbers_list_opt ':' asm_label_list ')' ';'
[bb7422a]1927 { $$ = new StatementNode( build_asm( yylloc, $2, $5, nullptr, $8, $10, $12 ) ); }
[7f5566b]1928 ;
1929
1930asm_volatile_opt: // GCC
1931 // empty
1932 { $$ = false; }
1933 | VOLATILE
1934 { $$ = true; }
[4d51835]1935 ;
[b87a5ed]1936
1937asm_operands_opt: // GCC
[4d51835]1938 // empty
[58dd019]1939 { $$ = nullptr; } // use default argument
[4d51835]1940 | asm_operands_list
1941 ;
[b87a5ed]1942
1943asm_operands_list: // GCC
[4d51835]1944 asm_operand
1945 | asm_operands_list ',' asm_operand
[b93c544]1946 { $$ = $1->set_last( $3 ); }
[4d51835]1947 ;
[b87a5ed]1948
1949asm_operand: // GCC
[ab57786]1950 string_literal '(' constant_expression ')'
[32d6fdc]1951 { $$ = new ExpressionNode( new ast::AsmExpr( yylloc, "", maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
[665f432]1952 | '[' IDENTIFIER ']' string_literal '(' constant_expression ')'
[bb7422a]1953 {
[32d6fdc]1954 $$ = new ExpressionNode( new ast::AsmExpr( yylloc, *$2.str, maybeMoveBuild( $4 ), maybeMoveBuild( $6 ) ) );
[bb7422a]1955 delete $2.str;
1956 }
[7f5566b]1957 ;
1958
[4e06c1e]1959asm_clobbers_list_opt: // GCC
[7f5566b]1960 // empty
[58dd019]1961 { $$ = nullptr; } // use default argument
[ab57786]1962 | string_literal
[32d6fdc]1963 { $$ = $1; }
[ab57786]1964 | asm_clobbers_list_opt ',' string_literal
[b93c544]1965 { $$ = $1->set_last( $3 ); }
[4d51835]1966 ;
[b87a5ed]1967
[cd28605]1968asm_label_list:
1969 identifier_or_type_name
1970 { $$ = new LabelNode(); $$->labels.emplace_back( yylloc, *$1 ); delete $1; } // allocated by lexer
1971 | asm_label_list ',' identifier_or_type_name
1972 { $$ = $1; $1->labels.emplace_back( yylloc, *$3 ); delete $3; } // allocated by lexer
[4d51835]1973 ;
[51b73452]1974
[e1d66c84]1975// ****************************** DECLARATIONS *********************************
[51b73452]1976
[b87a5ed]1977declaration_list_opt: // used at beginning of switch statement
[35718a9]1978 // empty
[58dd019]1979 { $$ = nullptr; }
[4d51835]1980 | declaration_list
1981 ;
[51b73452]1982
1983declaration_list:
[cd28605]1984 attribute_list_opt declaration
1985 { $$ = $2->addQualifiers( $1 ); }
[647e2ea]1986 | declaration_list declaration
1987 { $$ = $1->set_last( $2 ); }
[4d51835]1988 ;
[51b73452]1989
[407bde5]1990KR_parameter_list_opt: // used to declare parameter types in K&R style functions
[4cc585b]1991 // empty
[58dd019]1992 { $$ = nullptr; }
[35718a9]1993 | KR_parameter_list
[4d51835]1994 ;
[51b73452]1995
[35718a9]1996KR_parameter_list:
[c25f16b]1997 c_declaration ';'
1998 { $$ = $1; }
1999 | KR_parameter_list c_declaration ';'
[dc3fbe5]2000 { $$ = $1->set_last( $2 ); }
[4d51835]2001 ;
[b87a5ed]2002
[51b1202]2003local_label_declaration_opt: // GCC, local label
[4d51835]2004 // empty
[51b1202]2005 | local_label_declaration_list
[4d51835]2006 ;
[b87a5ed]2007
[51b1202]2008local_label_declaration_list: // GCC, local label
2009 LABEL local_label_list ';'
2010 | local_label_declaration_list LABEL local_label_list ';'
[4d51835]2011 ;
[b87a5ed]2012
[51b1202]2013local_label_list: // GCC, local label
[033ff37]2014 identifier_or_type_name
2015 | local_label_list ',' identifier_or_type_name
[4d51835]2016 ;
[b87a5ed]2017
[936e9f4]2018declaration: // old & new style declarations
[35718a9]2019 c_declaration ';'
2020 | cfa_declaration ';' // CFA
[b9f6791f]2021 | static_assert ';' // C11
[4d51835]2022 ;
[b87a5ed]2023
[b9be000b]2024static_assert:
[b9f6791f]2025 STATICASSERT '(' constant_expression ',' string_literal ')' // C11
[32d6fdc]2026 { $$ = DeclarationNode::newStaticAssert( $3, maybeMoveBuild( $5 ) ); }
[b9f6791f]2027 | STATICASSERT '(' constant_expression ')' // CFA
[bb7422a]2028 { $$ = DeclarationNode::newStaticAssert( $3, build_constantStr( yylloc, *new string( "\"\"" ) ) ); }
[b9be000b]2029
[de62360d]2030// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
2031// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
2032// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
2033// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
2034// the declaration list (as in Pascal). ANSI C and the new CFA declarations may appear together in the same program
2035// block, but cannot be mixed within a specific declaration.
[c11e31c]2036//
[b87a5ed]2037// CFA C
2038// [10] int x; int x[10]; // array of 10 integers
2039// [10] * char y; char *y[10]; // array of 10 pointers to char
2040
[c0aa336]2041cfa_declaration: // CFA
[4cc585b]2042 cfa_variable_declaration
2043 | cfa_typedef_declaration
2044 | cfa_function_declaration
2045 | type_declaring_list
[2501ae5]2046 { SemanticError( yylloc, "otype declaration is currently unimplemented." ); $$ = nullptr; }
[4cc585b]2047 | trait_specifier
[4d51835]2048 ;
[b87a5ed]2049
[c0aa336]2050cfa_variable_declaration: // CFA
2051 cfa_variable_specifier initializer_opt
[7fdb94e1]2052 { $$ = $1->addInitializer( $2 ); }
[c0aa336]2053 | declaration_qualifier_list cfa_variable_specifier initializer_opt
[de62360d]2054 // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
2055 // them as a type_qualifier cannot appear in that context.
[7fdb94e1]2056 { $$ = $2->addQualifiers( $1 )->addInitializer( $3 ); }
[c0aa336]2057 | cfa_variable_declaration pop ',' push identifier_or_type_name initializer_opt
[dc3fbe5]2058 { $$ = $1->set_last( $1->cloneType( $5 )->addInitializer( $6 ) ); }
[4d51835]2059 ;
[b87a5ed]2060
[c0aa336]2061cfa_variable_specifier: // CFA
[de62360d]2062 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
2063 // storage-class
[c0aa336]2064 cfa_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
[7fdb94e1]2065 { $$ = $1->addName( $2 )->addAsmName( $3 ); }
[c0aa336]2066 | cfa_abstract_tuple identifier_or_type_name asm_name_opt
[7fdb94e1]2067 { $$ = $1->addName( $2 )->addAsmName( $3 ); }
[dd54139]2068 | multi_array_dimension cfa_abstract_tuple identifier_or_type_name asm_name_opt
2069 { $$ = $2->addNewArray( $1 )->addName( $3 )->addAsmName( $4 ); }
2070 | multi_array_dimension type_qualifier_list cfa_abstract_tuple identifier_or_type_name asm_name_opt
2071 { $$ = $3->addNewArray( $1 )->addQualifiers( $2 )->addName( $4 )->addAsmName( $5 ); }
[2ab31fd]2072
2073 // [ int s, int t ]; // declare s and t
2074 // [ int, int ] f();
2075 // [] g( int );
2076 // [ int x, int y ] = f(); // declare x and y, initialize each from f
2077 // g( x + y );
2078 | cfa_function_return asm_name_opt
2079 { SemanticError( yylloc, "tuple-element declarations is currently unimplemented." ); $$ = nullptr; }
2080 | type_qualifier_list cfa_function_return asm_name_opt
2081 { SemanticError( yylloc, "tuple variable declaration is currently unimplemented." ); $$ = nullptr; }
[4d51835]2082 ;
[b87a5ed]2083
[c0aa336]2084cfa_function_declaration: // CFA
2085 cfa_function_specifier
2086 | type_qualifier_list cfa_function_specifier
[7fdb94e1]2087 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]2088 | declaration_qualifier_list cfa_function_specifier
[7fdb94e1]2089 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]2090 | declaration_qualifier_list type_qualifier_list cfa_function_specifier
[7fdb94e1]2091 { $$ = $3->addQualifiers( $1 )->addQualifiers( $2 ); }
[647e2ea]2092 | cfa_function_declaration ',' identifier_or_type_name '(' push cfa_parameter_list_ellipsis_opt pop ')'
[4d51835]2093 {
[481115f]2094 // Append the return type at the start (left-hand-side) to each identifier in the list.
2095 DeclarationNode * ret = new DeclarationNode;
[5bf685f]2096 ret->type = maybeCopy( $1->type->base );
[dc3fbe5]2097 $$ = $1->set_last( DeclarationNode::newFunction( $3, ret, $6, nullptr ) );
[4d51835]2098 }
2099 ;
[b87a5ed]2100
[c0aa336]2101cfa_function_specifier: // CFA
[647e2ea]2102 '[' ']' identifier '(' push cfa_parameter_list_ellipsis_opt pop ')' attribute_list_opt
2103 { $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( nullptr ), $6, nullptr )->addQualifiers( $9 ); }
2104 | '[' ']' TYPEDEFname '(' push cfa_parameter_list_ellipsis_opt pop ')' attribute_list_opt
2105 { $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( nullptr ), $6, nullptr )->addQualifiers( $9 ); }
2106 // | '[' ']' TYPEGENname '(' push cfa_parameter_list_ellipsis_opt pop ')' attribute_list_opt
2107 // { $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( nullptr ), $6, nullptr )->addQualifiers( $9 ); }
2108
[2871210]2109 // identifier_or_type_name must be broken apart because of the sequence:
[4d51835]2110 //
[647e2ea]2111 // '[' ']' identifier_or_type_name '(' cfa_parameter_list_ellipsis_opt ')'
[4d51835]2112 // '[' ']' type_specifier
2113 //
[2871210]2114 // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
2115 // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
[647e2ea]2116 | cfa_abstract_tuple identifier_or_type_name '(' push cfa_parameter_list_ellipsis_opt pop ')' attribute_list_opt
[c0aa336]2117 // To obtain LR(1 ), this rule must be factored out from function return type (see cfa_abstract_declarator).
[a5f9444]2118 { $$ = DeclarationNode::newFunction( $2, $1, $5, nullptr )->addQualifiers( $8 ); }
[647e2ea]2119 | cfa_function_return identifier_or_type_name '(' push cfa_parameter_list_ellipsis_opt pop ')' attribute_list_opt
[a5f9444]2120 { $$ = DeclarationNode::newFunction( $2, $1, $5, nullptr )->addQualifiers( $8 ); }
[4d51835]2121 ;
[b87a5ed]2122
[c0aa336]2123cfa_function_return: // CFA
[917f67dd]2124 '[' cfa_parameter_list ']'
2125 { $$ = DeclarationNode::newTuple( $2 ); }
2126 | '[' cfa_parameter_list ',' cfa_abstract_parameter_list ']'
[b048dc3]2127 // To obtain LR(1 ), the last cfa_abstract_parameter_list is added into this flattened rule to lookahead to the ']'.
[917f67dd]2128 { $$ = DeclarationNode::newTuple( $2->set_last( $4 ) ); }
[4d51835]2129 ;
[b87a5ed]2130
[c0aa336]2131cfa_typedef_declaration: // CFA
[3e5e32cf]2132 TYPEDEF attribute_list_opt cfa_variable_specifier
[4d51835]2133 {
[3e5e32cf]2134 typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "cfa_typedef_declaration 1" );
2135 $$ = $3->addTypedef()->addQualifiers( $2 );
[4d51835]2136 }
[3e5e32cf]2137 | TYPEDEF attribute_list_opt cfa_function_specifier
[4d51835]2138 {
[3e5e32cf]2139 typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "cfa_typedef_declaration 2" );
2140 $$ = $3->addTypedef()->addQualifiers( $2 );
[4d51835]2141 }
[3e5e32cf]2142 | cfa_typedef_declaration ',' attribute_list_opt identifier
[4d51835]2143 {
[3e5e32cf]2144 typedefTable.addToEnclosingScope( *$4, TYPEDEFname, "cfa_typedef_declaration 3" );
2145 $$ = $1->set_last( $1->cloneType( $4 )->addQualifiers( $3 ) );
[4d51835]2146 }
2147 ;
[b87a5ed]2148
[de62360d]2149// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
2150// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
[51b73452]2151
2152typedef_declaration:
[3e5e32cf]2153 TYPEDEF attribute_list_opt type_specifier declarator
[cd28605]2154 {
2155 typedefTable.addToEnclosingScope( *$4->name, TYPEDEFname, "typedef_declaration 1" );
2156 if ( $3->type->forall || ($3->type->kind == TypeData::Aggregate && $3->type->aggregate.params) ) {
2157 SemanticError( yylloc, "forall qualifier in typedef is currently unimplemented." ); $$ = nullptr;
2158 } else $$ = $4->addType( $3 )->addTypedef()->addQualifiers( $2 ); // watchout frees $3 and $4
2159 }
[3e5e32cf]2160 | typedef_declaration ',' attribute_list_opt declarator
[4d51835]2161 {
[3e5e32cf]2162 typedefTable.addToEnclosingScope( *$4->name, TYPEDEFname, "typedef_declaration 2" );
2163 $$ = $1->set_last( $1->cloneBaseType( $4 )->addTypedef()->addQualifiers( $3 ) );
[4d51835]2164 }
[de62360d]2165 | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
[ac235a8]2166 { SemanticError( yylloc, "Type qualifiers/specifiers before TYPEDEF is deprecated, move after TYPEDEF." ); $$ = nullptr; }
[4d51835]2167 | type_specifier TYPEDEF declarator
[ac235a8]2168 { SemanticError( yylloc, "Type qualifiers/specifiers before TYPEDEF is deprecated, move after TYPEDEF." ); $$ = nullptr; }
[4d51835]2169 | type_specifier TYPEDEF type_qualifier_list declarator
[ac235a8]2170 { SemanticError( yylloc, "Type qualifiers/specifiers before TYPEDEF is deprecated, move after TYPEDEF." ); $$ = nullptr; }
[4d51835]2171 ;
[b87a5ed]2172
[721f17a]2173typedef_expression:
[25744d2]2174 // deprecated GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
[033ff37]2175 TYPEDEF identifier '=' assignment_expression
[3e5e32cf]2176 { SemanticError( yylloc, "TYPEDEF expression is deprecated, use typeof(...) instead." ); $$ = nullptr; }
[c25f16b]2177 | typedef_expression ',' identifier '=' assignment_expression
[3e5e32cf]2178 { SemanticError( yylloc, "TYPEDEF expression is deprecated, use typeof(...) instead." ); $$ = nullptr; }
[4d51835]2179 ;
[51b73452]2180
[c0aa336]2181c_declaration:
[4cc585b]2182 declaration_specifier declaring_list
[cd28605]2183 { $$ = distTypeSpec( $1, $2 ); }
[4cc585b]2184 | typedef_declaration
[25744d2]2185 | typedef_expression // deprecated GCC, naming expression type
[4cc585b]2186 | sue_declaration_specifier
[0bcd707]2187 {
2188 assert( $1->type );
[1a73dbb]2189 if ( $1->type->qualifiers.any() ) { // CV qualifiers ?
[253d0b4]2190 SemanticError( yylloc, "illegal syntax, useless type qualifier(s) in empty declaration." ); $$ = nullptr;
[1a73dbb]2191 }
2192 // enums are never empty declarations because there must have at least one enumeration.
2193 if ( $1->type->kind == TypeData::AggregateInst && $1->storageClasses.any() ) { // storage class ?
[253d0b4]2194 SemanticError( yylloc, "illegal syntax, useless storage qualifier(s) in empty aggregate declaration." ); $$ = nullptr;
[0bcd707]2195 }
2196 }
[4d51835]2197 ;
[51b73452]2198
2199declaring_list:
[de62360d]2200 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
2201 // storage-class
[1f771fc]2202 variable_declarator asm_name_opt initializer_opt
[7fdb94e1]2203 { $$ = $1->addAsmName( $2 )->addInitializer( $3 ); }
[1f771fc]2204 | variable_type_redeclarator asm_name_opt initializer_opt
[7fdb94e1]2205 { $$ = $1->addAsmName( $2 )->addInitializer( $3 ); }
[1f771fc]2206
2207 | general_function_declarator asm_name_opt
2208 { $$ = $1->addAsmName( $2 )->addInitializer( nullptr ); }
2209 | general_function_declarator asm_name_opt '=' VOID
2210 { $$ = $1->addAsmName( $2 )->addInitializer( new InitializerNode( true ) ); }
2211
[4d51835]2212 | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
[dc3fbe5]2213 { $$ = $1->set_last( $4->addQualifiers( $3 )->addAsmName( $5 )->addInitializer( $6 ) ); }
[4d51835]2214 ;
[b87a5ed]2215
[1f771fc]2216general_function_declarator:
2217 function_type_redeclarator
2218 | function_declarator
2219 ;
2220
[b87a5ed]2221declaration_specifier: // type specifier + storage class
[4d51835]2222 basic_declaration_specifier
[84d58c5]2223 | type_declaration_specifier
[79a6b17]2224 | sue_declaration_specifier
[0442f93f]2225 | sue_declaration_specifier invalid_types // invalid syntax rule
[79a6b17]2226 {
[253d0b4]2227 SemanticError( yylloc, "illegal syntax, expecting ';' at end of \"%s\" declaration.",
[67467a3]2228 ast::AggregateDecl::aggrString( $1->type->aggregate.kind ) );
[79a6b17]2229 $$ = nullptr;
2230 }
2231 ;
2232
2233invalid_types:
2234 aggregate_key
2235 | basic_type_name
2236 | indirect_type
[4d51835]2237 ;
[b87a5ed]2238
[d0ffed1]2239declaration_specifier_nobody: // type specifier + storage class - {...}
2240 // Preclude SUE declarations in restricted scopes:
2241 //
2242 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
2243 //
2244 // because it is impossible to call f due to name equivalence.
2245 basic_declaration_specifier
2246 | sue_declaration_specifier_nobody
[84d58c5]2247 | type_declaration_specifier
[d0ffed1]2248 ;
2249
2250type_specifier: // type specifier
[4d51835]2251 basic_type_specifier
2252 | sue_type_specifier
[31df72b]2253 | type_type_specifier attribute_list_opt
[4d51835]2254 ;
[b87a5ed]2255
[d0ffed1]2256type_specifier_nobody: // type specifier - {...}
2257 // Preclude SUE declarations in restricted scopes:
2258 //
2259 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
2260 //
2261 // because it is impossible to call f due to name equivalence.
2262 basic_type_specifier
2263 | sue_type_specifier_nobody
[84d58c5]2264 | type_type_specifier
[d0ffed1]2265 ;
2266
[b87a5ed]2267type_qualifier_list_opt: // GCC, used in asm_statement
[4d51835]2268 // empty
[58dd019]2269 { $$ = nullptr; }
[4d51835]2270 | type_qualifier_list
2271 ;
[51b73452]2272
2273type_qualifier_list:
[de62360d]2274 // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
[4d51835]2275 //
[de62360d]2276 // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
2277 // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
2278 // appeared only once.
[cd28605]2279 type_qualifier attribute_list_opt
[4d51835]2280 { $$ = $1->addQualifiers( $2 ); }
[cd28605]2281 | type_qualifier_list type_qualifier attribute_list_opt
2282 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
[4d51835]2283 ;
[51b73452]2284
2285type_qualifier:
[4d51835]2286 type_qualifier_name
[6cef439]2287 { $$ = DeclarationNode::newFromTypeData( $1 ); }
[4d51835]2288 ;
[51b73452]2289
2290type_qualifier_name:
[4d51835]2291 CONST
[6cef439]2292 { $$ = build_type_qualifier( ast::CV::Const ); }
[4d51835]2293 | RESTRICT
[6cef439]2294 { $$ = build_type_qualifier( ast::CV::Restrict ); }
[4d51835]2295 | VOLATILE
[6cef439]2296 { $$ = build_type_qualifier( ast::CV::Volatile ); }
[4d51835]2297 | ATOMIC
[6cef439]2298 { $$ = build_type_qualifier( ast::CV::Atomic ); }
[7e13b11]2299
[446740a]2300 // forall is a CV qualifier because it can appear in places where SC qualifiers are disallowed.
[7e13b11]2301 //
2302 // void foo( forall( T ) T (*)( T ) ); // forward declaration
2303 // void bar( static int ); // static disallowed (gcc/CFA)
[a16a7ec]2304 | forall
[6cef439]2305 { $$ = build_forall( $1 ); }
[a16a7ec]2306 ;
2307
2308forall:
[35718a9]2309 FORALL '(' type_parameter_list ')' // CFA
[7a24d76]2310 { $$ = $3; }
[4d51835]2311 ;
[51b73452]2312
2313declaration_qualifier_list:
[4d51835]2314 storage_class_list
[cd28605]2315 | type_qualifier_list storage_class_list
[4d51835]2316 { $$ = $1->addQualifiers( $2 ); }
2317 | declaration_qualifier_list type_qualifier_list storage_class_list
2318 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2319 ;
[51b73452]2320
2321storage_class_list:
[de62360d]2322 // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
2323 // only one of each is specified, except for inline, which can appear with the others.
[4d51835]2324 //
[de62360d]2325 // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
2326 // specifiers in a declaration.
[cd28605]2327 storage_class attribute_list_opt
[4d51835]2328 { $$ = $1->addQualifiers( $2 ); }
[cd28605]2329 | storage_class_list storage_class attribute_list_opt
2330 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
[4d51835]2331 ;
[51b73452]2332
2333storage_class:
[4d51835]2334 EXTERN
[36e6f10]2335 { $$ = DeclarationNode::newStorageClass( ast::Storage::Extern ); }
[4d51835]2336 | STATIC
[36e6f10]2337 { $$ = DeclarationNode::newStorageClass( ast::Storage::Static ); }
[4d51835]2338 | AUTO
[36e6f10]2339 { $$ = DeclarationNode::newStorageClass( ast::Storage::Auto ); }
[4d51835]2340 | REGISTER
[36e6f10]2341 { $$ = DeclarationNode::newStorageClass( ast::Storage::Register ); }
[ed9a1ae]2342 | THREADLOCALGCC // GCC
[36e6f10]2343 { $$ = DeclarationNode::newStorageClass( ast::Storage::ThreadLocalGcc ); }
[ed9a1ae]2344 | THREADLOCALC11 // C11
[36e6f10]2345 { $$ = DeclarationNode::newStorageClass( ast::Storage::ThreadLocalC11 ); }
[dd020c0]2346 // Put function specifiers here to simplify parsing rules, but separate them semantically.
[4d51835]2347 | INLINE // C99
[36e6f10]2348 { $$ = DeclarationNode::newFuncSpecifier( ast::Function::Inline ); }
[4d51835]2349 | FORTRAN // C99
[36e6f10]2350 { $$ = DeclarationNode::newFuncSpecifier( ast::Function::Fortran ); }
[68cd1ce]2351 | NORETURN // C11
[36e6f10]2352 { $$ = DeclarationNode::newFuncSpecifier( ast::Function::Noreturn ); }
[4d51835]2353 ;
[51b73452]2354
2355basic_type_name:
[6cef439]2356 basic_type_name_type
2357 { $$ = DeclarationNode::newFromTypeData( $1 ); }
2358 ;
2359
2360// Just an intermediate value for conversion.
2361basic_type_name_type:
[201aeb9]2362 VOID
[e048ece]2363 { $$ = build_basic_type( TypeData::Void ); }
[4d51835]2364 | BOOL // C99
[e048ece]2365 { $$ = build_basic_type( TypeData::Bool ); }
[201aeb9]2366 | CHAR
[e048ece]2367 { $$ = build_basic_type( TypeData::Char ); }
[201aeb9]2368 | INT
[e048ece]2369 { $$ = build_basic_type( TypeData::Int ); }
[201aeb9]2370 | INT128
[e048ece]2371 { $$ = build_basic_type( TypeData::Int128 ); }
[f1da02c]2372 | UINT128
[e048ece]2373 { $$ = addType( build_basic_type( TypeData::Int128 ), build_signedness( TypeData::Unsigned ) ); }
[201aeb9]2374 | FLOAT
[e048ece]2375 { $$ = build_basic_type( TypeData::Float ); }
[201aeb9]2376 | DOUBLE
[e048ece]2377 { $$ = build_basic_type( TypeData::Double ); }
[c5f69fd]2378 | FLOAT80
2379 { $$ = build_basic_type( TypeData::Float80 ); }
[e15853c]2380 | uuFLOAT128
[e048ece]2381 { $$ = build_basic_type( TypeData::uuFloat128 ); }
[c5f69fd]2382 | FLOAT16
2383 { $$ = build_basic_type( TypeData::Float16 ); }
2384 | FLOAT32
2385 { $$ = build_basic_type( TypeData::Float32 ); }
2386 | FLOAT32X
2387 { $$ = build_basic_type( TypeData::Float32x ); }
2388 | FLOAT64
2389 { $$ = build_basic_type( TypeData::Float64 ); }
2390 | FLOAT64X
2391 { $$ = build_basic_type( TypeData::Float64x ); }
2392 | FLOAT128
2393 { $$ = build_basic_type( TypeData::Float128 ); }
[569b118]2394 // https://developer.arm.com/documentation/den0018/a/NEON-Intrinsics/Accessing-vector-types-from-C
[c5f69fd]2395 | FLOAT128X
2396 { $$ = build_basic_type( TypeData::Float128x ); }
2397 | FLOAT32X4
2398 { $$ = build_basic_type( TypeData::Float32x4 ); }
2399 | FLOAT64X2
2400 { $$ = build_basic_type( TypeData::Float64x2 ); }
2401 | SVFLOAT32
2402 { $$ = build_basic_type( TypeData::Svfloat32 ); }
2403 | SVFLOAT64
2404 { $$ = build_basic_type( TypeData::Svfloat64 ); }
2405 | SVBOOL
2406 { $$ = build_basic_type( TypeData::Svbool ); }
[15f769c]2407 | DECIMAL32
2408 { SemanticError( yylloc, "_Decimal32 is currently unimplemented." ); $$ = nullptr; }
2409 | DECIMAL64
2410 { SemanticError( yylloc, "_Decimal64 is currently unimplemented." ); $$ = nullptr; }
2411 | DECIMAL128
2412 { SemanticError( yylloc, "_Decimal128 is currently unimplemented." ); $$ = nullptr; }
[4d51835]2413 | COMPLEX // C99
[e048ece]2414 { $$ = build_complex_type( TypeData::Complex ); }
[4d51835]2415 | IMAGINARY // C99
[e048ece]2416 { $$ = build_complex_type( TypeData::Imaginary ); }
[201aeb9]2417 | SIGNED
[e048ece]2418 { $$ = build_signedness( TypeData::Signed ); }
[201aeb9]2419 | UNSIGNED
[e048ece]2420 { $$ = build_signedness( TypeData::Unsigned ); }
[201aeb9]2421 | SHORT
[e048ece]2422 { $$ = build_length( TypeData::Short ); }
[201aeb9]2423 | LONG
[e048ece]2424 { $$ = build_length( TypeData::Long ); }
[59c7e3e]2425 | VA_LIST // GCC, __builtin_va_list
[e048ece]2426 { $$ = build_builtin_type( TypeData::Valist ); }
[f673c13c]2427 | AUTO_TYPE
[e048ece]2428 { $$ = build_builtin_type( TypeData::AutoType ); }
[1f652a7]2429 | vtable
2430 ;
2431
2432vtable_opt:
2433 // empty
2434 { $$ = nullptr; }
[9380add]2435 | vtable
[1f652a7]2436 ;
2437
2438vtable:
[8f6f3729]2439 VTABLE '(' type_name ')' default_opt
[6cef439]2440 { $$ = build_vtable_type( $3 ); }
[1f652a7]2441 ;
2442
2443default_opt:
2444 // empty
2445 { $$ = nullptr; }
2446 | DEFAULT
2447 { SemanticError( yylloc, "vtable default is currently unimplemented." ); $$ = nullptr; }
[4d51835]2448 ;
[51b73452]2449
2450basic_declaration_specifier:
[4d51835]2451 // A semantic check is necessary for conflicting storage classes.
2452 basic_type_specifier
2453 | declaration_qualifier_list basic_type_specifier
2454 { $$ = $2->addQualifiers( $1 ); }
[cd28605]2455 | basic_declaration_specifier storage_class attribute_list_opt // remaining OBSOLESCENT (see 2)
2456 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
[4d51835]2457 | basic_declaration_specifier storage_class type_qualifier_list
2458 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2459 | basic_declaration_specifier storage_class basic_type_specifier
2460 { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
2461 ;
[51b73452]2462
2463basic_type_specifier:
[cd28605]2464 direct_type attribute_list_opt
2465 { $$ = $1->addQualifiers( $2 ); }
[f38e7d7]2466 // Cannot have type modifiers, e.g., short, long, etc.
[cd28605]2467 | type_qualifier_list_opt indirect_type attribute_list
2468 { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
[84d58c5]2469 | type_qualifier_list_opt indirect_type type_qualifier_list_opt
[4d51835]2470 { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
2471 ;
[51b73452]2472
[84d58c5]2473direct_type:
[4d51835]2474 basic_type_name
2475 | type_qualifier_list basic_type_name
2476 { $$ = $2->addQualifiers( $1 ); }
[84d58c5]2477 | direct_type type_qualifier
[4d51835]2478 { $$ = $1->addQualifiers( $2 ); }
[84d58c5]2479 | direct_type basic_type_name
[4d51835]2480 { $$ = $1->addType( $2 ); }
2481 ;
[51b73452]2482
[84d58c5]2483indirect_type:
[b6ad601]2484 TYPEOF '(' type ')' // GCC: typeof( x ) y;
[4d51835]2485 { $$ = $3; }
[b6ad601]2486 | TYPEOF '(' comma_expression ')' // GCC: typeof( a+b ) y;
[4d51835]2487 { $$ = DeclarationNode::newTypeof( $3 ); }
[b6ad601]2488 | BASETYPEOF '(' type ')' // CFA: basetypeof( x ) y;
[bb7422a]2489 { $$ = DeclarationNode::newTypeof( new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $3 ) ) ), true ); }
[b6ad601]2490 | BASETYPEOF '(' comma_expression ')' // CFA: basetypeof( a+b ) y;
2491 { $$ = DeclarationNode::newTypeof( $3, true ); }
[f38e7d7]2492 | ZERO_T // CFA
[e048ece]2493 { $$ = DeclarationNode::newFromTypeData( build_builtin_type( TypeData::Zero ) ); }
[f38e7d7]2494 | ONE_T // CFA
[e048ece]2495 { $$ = DeclarationNode::newFromTypeData( build_builtin_type( TypeData::One ) ); }
[4d51835]2496 ;
[51b73452]2497
[d0ffed1]2498sue_declaration_specifier: // struct, union, enum + storage class + type specifier
[4d51835]2499 sue_type_specifier
2500 | declaration_qualifier_list sue_type_specifier
2501 { $$ = $2->addQualifiers( $1 ); }
2502 | sue_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
2503 { $$ = $1->addQualifiers( $2 ); }
2504 | sue_declaration_specifier storage_class type_qualifier_list
2505 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2506 ;
[51b73452]2507
[d0ffed1]2508sue_type_specifier: // struct, union, enum + type specifier
2509 elaborated_type
[fdca7c6]2510 | type_qualifier_list
2511 { if ( $1->type != nullptr && $1->type->forall ) forall = true; } // remember generic type
2512 elaborated_type
2513 { $$ = $3->addQualifiers( $1 ); }
[4d51835]2514 | sue_type_specifier type_qualifier
[284da8c]2515 {
2516 if ( $2->type != nullptr && $2->type->forall ) forall = true; // remember generic type
2517 $$ = $1->addQualifiers( $2 );
2518 }
[4d51835]2519 ;
[51b73452]2520
[d0ffed1]2521sue_declaration_specifier_nobody: // struct, union, enum - {...} + storage class + type specifier
2522 sue_type_specifier_nobody
2523 | declaration_qualifier_list sue_type_specifier_nobody
2524 { $$ = $2->addQualifiers( $1 ); }
2525 | sue_declaration_specifier_nobody storage_class // remaining OBSOLESCENT (see 2)
2526 { $$ = $1->addQualifiers( $2 ); }
2527 | sue_declaration_specifier_nobody storage_class type_qualifier_list
2528 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2529 ;
2530
2531sue_type_specifier_nobody: // struct, union, enum - {...} + type specifier
2532 elaborated_type_nobody
2533 | type_qualifier_list elaborated_type_nobody
2534 { $$ = $2->addQualifiers( $1 ); }
2535 | sue_type_specifier_nobody type_qualifier
2536 { $$ = $1->addQualifiers( $2 ); }
2537 ;
2538
[84d58c5]2539type_declaration_specifier:
[cd28605]2540 type_type_specifier attribute_list_opt
[4d51835]2541 { $$ = $1->addQualifiers( $2 ); }
[cd28605]2542 | declaration_qualifier_list type_type_specifier attribute_list_opt
2543 { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
2544 | type_declaration_specifier storage_class attribute_list_opt // remaining OBSOLESCENT (see 2)
2545 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
[84d58c5]2546 | type_declaration_specifier storage_class type_qualifier_list
[4d51835]2547 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2548 ;
[b87a5ed]2549
[84d58c5]2550type_type_specifier: // typedef types
2551 type_name
[6cef439]2552 { $$ = DeclarationNode::newFromTypeData( $1 ); }
[84d58c5]2553 | type_qualifier_list type_name
[6cef439]2554 { $$ = DeclarationNode::newFromTypeData( $2 )->addQualifiers( $1 ); }
[84d58c5]2555 | type_type_specifier type_qualifier
2556 { $$ = $1->addQualifiers( $2 ); }
2557 ;
2558
2559type_name:
[4d51835]2560 TYPEDEFname
[6cef439]2561 { $$ = build_typedef( $1 ); }
[84d58c5]2562 | '.' TYPEDEFname
[6cef439]2563 { $$ = build_qualified_type( build_global_scope(), build_typedef( $2 ) ); }
[84d58c5]2564 | type_name '.' TYPEDEFname
[6cef439]2565 { $$ = build_qualified_type( $1, build_typedef( $3 ) ); }
[84d58c5]2566 | typegen_name
2567 | '.' typegen_name
[6cef439]2568 { $$ = build_qualified_type( build_global_scope(), $2 ); }
[84d58c5]2569 | type_name '.' typegen_name
[6cef439]2570 { $$ = build_qualified_type( $1, $3 ); }
[84d58c5]2571 ;
2572
2573typegen_name: // CFA
[65d6de4]2574 TYPEGENname
[6cef439]2575 { $$ = build_type_gen( $1, nullptr ); }
[65d6de4]2576 | TYPEGENname '(' ')'
[6cef439]2577 { $$ = build_type_gen( $1, nullptr ); }
[67cf18c]2578 | TYPEGENname '(' type_list ')'
[6cef439]2579 { $$ = build_type_gen( $1, $3 ); }
[4d51835]2580 ;
[51b73452]2581
[d0ffed1]2582elaborated_type: // struct, union, enum
[c0aa336]2583 aggregate_type
2584 | enum_type
[4d51835]2585 ;
[51b73452]2586
[d0ffed1]2587elaborated_type_nobody: // struct, union, enum - {...}
2588 aggregate_type_nobody
2589 | enum_type_nobody
2590 ;
2591
[7e13b11]2592// ************************** AGGREGATE *******************************
2593
[d0ffed1]2594aggregate_type: // struct, union
[fc20514]2595 aggregate_key attribute_list_opt
2596 { forall = false; } // reset
[cd28605]2597 '{' field_declaration_list_opt '}' type_parameters_opt attribute_list_opt
2598 { $$ = DeclarationNode::newAggregate( $1, nullptr, $7, $5, true )->addQualifiers( $2 )->addQualifiers( $8 ); }
2599 | aggregate_key attribute_list_opt identifier attribute_list_opt
[fdca7c6]2600 {
[71a422a]2601 typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname, "aggregate_type: 1" );
[fdca7c6]2602 forall = false; // reset
2603 }
[cd28605]2604 '{' field_declaration_list_opt '}' type_parameters_opt attribute_list_opt
[d8454b9]2605 {
[cd28605]2606 $$ = DeclarationNode::newAggregate( $1, $3, $9, $7, true )->addQualifiers( $2 )->addQualifiers( $4 )->addQualifiers( $10 );
[d8454b9]2607 }
[cd28605]2608 | aggregate_key attribute_list_opt TYPEDEFname attribute_list_opt // unqualified type name
[407bde5]2609 {
[71a422a]2610 typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname, "aggregate_type: 2" );
[407bde5]2611 forall = false; // reset
2612 }
[cd28605]2613 '{' field_declaration_list_opt '}' type_parameters_opt attribute_list_opt
[f9c3100]2614 {
[6cef439]2615 DeclarationNode::newFromTypeData( build_typedef( $3 ) );
[cd28605]2616 $$ = DeclarationNode::newAggregate( $1, $3, $9, $7, true )->addQualifiers( $2 )->addQualifiers( $4 )->addQualifiers( $10 );
[f9c3100]2617 }
[cd28605]2618 | aggregate_key attribute_list_opt TYPEGENname attribute_list_opt // unqualified type name
[f9c3100]2619 {
[71a422a]2620 typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname, "aggregate_type: 3" );
[f9c3100]2621 forall = false; // reset
2622 }
[cd28605]2623 '{' field_declaration_list_opt '}' type_parameters_opt attribute_list_opt
[f9c3100]2624 {
[6cef439]2625 DeclarationNode::newFromTypeData( build_type_gen( $3, nullptr ) );
[cd28605]2626 $$ = DeclarationNode::newAggregate( $1, $3, $9, $7, true )->addQualifiers( $2 )->addQualifiers( $10 );
[f9c3100]2627 }
[d0ffed1]2628 | aggregate_type_nobody
2629 ;
2630
[284da8c]2631type_parameters_opt:
2632 // empty
2633 { $$ = nullptr; } %prec '}'
2634 | '(' type_list ')'
2635 { $$ = $2; }
2636 ;
2637
[d0ffed1]2638aggregate_type_nobody: // struct, union - {...}
[73f04fd]2639 aggregate_key attribute_list_opt identifier
[84d58c5]2640 {
[71a422a]2641 typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname, "aggregate_type_nobody" );
[9997fee]2642 forall = false; // reset
[84d58c5]2643 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
2644 }
[73f04fd]2645 | aggregate_key attribute_list_opt type_name
[65d6de4]2646 {
[fc20514]2647 forall = false; // reset
[65d6de4]2648 // Create new generic declaration with same name as previous forward declaration, where the IDENTIFIER is
2649 // switched to a TYPEGENname. Link any generic arguments from typegen_name to new generic declaration and
[446740a]2650 // delete newFromTypeGen.
[6cef439]2651 if ( $3->kind == TypeData::SymbolicInst && ! $3->symbolic.isTypedef ) {
2652 $$ = DeclarationNode::newFromTypeData( $3 )->addQualifiers( $2 );
[9fa61f5]2653 } else {
[6cef439]2654 $$ = DeclarationNode::newAggregate( $1, $3->symbolic.name, $3->symbolic.actuals, nullptr, false )->addQualifiers( $2 );
2655 $3->symbolic.name = nullptr; // copied to $$
2656 $3->symbolic.actuals = nullptr;
[9fa61f5]2657 delete $3;
2658 }
[65d6de4]2659 }
[4d51835]2660 ;
[51b73452]2661
2662aggregate_key:
[e307e12]2663 aggregate_data
2664 | aggregate_control
2665 ;
2666
2667aggregate_data:
[1f652a7]2668 STRUCT vtable_opt
[bb7422a]2669 { $$ = ast::AggregateDecl::Struct; }
[c0aa336]2670 | UNION
[bb7422a]2671 { $$ = ast::AggregateDecl::Union; }
[e307e12]2672 | EXCEPTION // CFA
[bb7422a]2673 { $$ = ast::AggregateDecl::Exception; }
[e307e12]2674 ;
2675
2676aggregate_control: // CFA
[cbbd8fd7]2677 MONITOR
[bb7422a]2678 { $$ = ast::AggregateDecl::Monitor; }
[cbbd8fd7]2679 | MUTEX STRUCT
[bb7422a]2680 { $$ = ast::AggregateDecl::Monitor; }
[cbbd8fd7]2681 | GENERATOR
[bb7422a]2682 { $$ = ast::AggregateDecl::Generator; }
[cbbd8fd7]2683 | MUTEX GENERATOR
[bb7422a]2684 {
2685 SemanticError( yylloc, "monitor generator is currently unimplemented." );
2686 $$ = ast::AggregateDecl::NoAggregate;
2687 }
[d3bc0ad]2688 | COROUTINE
[bb7422a]2689 { $$ = ast::AggregateDecl::Coroutine; }
[cbbd8fd7]2690 | MUTEX COROUTINE
[bb7422a]2691 {
2692 SemanticError( yylloc, "monitor coroutine is currently unimplemented." );
2693 $$ = ast::AggregateDecl::NoAggregate;
2694 }
[d3bc0ad]2695 | THREAD
[bb7422a]2696 { $$ = ast::AggregateDecl::Thread; }
[cbbd8fd7]2697 | MUTEX THREAD
[bb7422a]2698 {
2699 SemanticError( yylloc, "monitor thread is currently unimplemented." );
2700 $$ = ast::AggregateDecl::NoAggregate;
2701 }
[4d51835]2702 ;
[51b73452]2703
[7fdb94e1]2704field_declaration_list_opt:
[cd28605]2705 // empty => struct S { /* no fields */ };
[58dd019]2706 { $$ = nullptr; }
[cd28605]2707 | field_declaration_list_opt attribute_list_opt field_declaration
2708 { distAttr( $2, $3 ); $$ = $1 ? $1->set_last( $3 ) : $3; }
[4d51835]2709 ;
[51b73452]2710
2711field_declaration:
[679e644]2712 type_specifier field_declaring_list_opt ';'
[cd28605]2713 { $$ = fieldDecl( $1, $2 ); }
[0442f93f]2714 | type_specifier field_declaring_list_opt '}' // invalid syntax rule
[55266c7]2715 {
[253d0b4]2716 SemanticError( yylloc, "illegal syntax, expecting ';' at end of previous declaration." );
[55266c7]2717 $$ = nullptr;
2718 }
[679e644]2719 | EXTENSION type_specifier field_declaring_list_opt ';' // GCC
[f7e4db27]2720 { $$ = fieldDecl( $2, $3 ); distExt( $$ ); }
[466787a]2721 | STATIC type_specifier field_declaring_list_opt ';' // CFA
[9fd9d015]2722 { SemanticError( yylloc, "STATIC aggregate field qualifier currently unimplemented." ); $$ = nullptr; }
[cd28605]2723 | INLINE attribute_list_opt type_specifier field_abstract_list_opt ';' // CFA
[679e644]2724 {
[cd28605]2725 if ( ! $4 ) { // field declarator ?
2726 $4 = DeclarationNode::newName( nullptr );
[dea36ee]2727 } // if
[cd28605]2728 $4->inLine = true;
2729 $$ = distTypeSpec( $3, $4 ); // mark all fields in list
2730 distInl( $4 );
[8f91c9ae]2731 }
[cd28605]2732 | INLINE attribute_list_opt aggregate_control ';' // CFA
[9fd9d015]2733 { SemanticError( yylloc, "INLINE aggregate control currently unimplemented." ); $$ = nullptr; }
[46fa473]2734 | typedef_declaration ';' // CFA
2735 | cfa_field_declaring_list ';' // CFA, new style field declaration
2736 | EXTENSION cfa_field_declaring_list ';' // GCC
2737 { distExt( $2 ); $$ = $2; } // mark all fields in list
[cd28605]2738 | INLINE attribute_list_opt cfa_field_abstract_list ';' // CFA, new style field declaration
2739 { $$ = $3->addQualifiers( $2 ); } // mark all fields in list
[46fa473]2740 | cfa_typedef_declaration ';' // CFA
[b9f6791f]2741 | static_assert ';' // C11
[4d51835]2742 ;
[b87a5ed]2743
[679e644]2744field_declaring_list_opt:
2745 // empty
2746 { $$ = nullptr; }
[a16f2b6]2747 | field_declaring_list
2748 ;
2749
2750field_declaring_list:
2751 field_declarator
[12f1156]2752 | field_declaring_list_opt ',' attribute_list_opt field_declarator
[dc3fbe5]2753 { $$ = $1->set_last( $4->addQualifiers( $3 ) ); }
[4d51835]2754 ;
[51b73452]2755
[679e644]2756field_declarator:
[e07caa2]2757 bit_subrange_size // C special case, no field name
[4d51835]2758 { $$ = DeclarationNode::newBitfield( $1 ); }
2759 | variable_declarator bit_subrange_size_opt
[679e644]2760 // A semantic check is required to ensure bit_subrange only appears on integral types.
[4d51835]2761 { $$ = $1->addBitfield( $2 ); }
[c6b1105]2762 | variable_type_redeclarator bit_subrange_size_opt
[679e644]2763 // A semantic check is required to ensure bit_subrange only appears on integral types.
[4d51835]2764 { $$ = $1->addBitfield( $2 ); }
[1f771fc]2765 | function_type_redeclarator bit_subrange_size_opt
2766 // A semantic check is required to ensure bit_subrange only appears on integral types.
2767 { $$ = $1->addBitfield( $2 ); }
[679e644]2768 ;
2769
[e07caa2]2770field_abstract_list_opt:
2771 // empty
[dea36ee]2772 { $$ = nullptr; }
[e07caa2]2773 | field_abstract
2774 | field_abstract_list_opt ',' attribute_list_opt field_abstract
[dc3fbe5]2775 { $$ = $1->set_last( $4->addQualifiers( $3 ) ); }
[679e644]2776 ;
2777
[e07caa2]2778field_abstract:
[cd28605]2779 variable_abstract_declarator // no bit fields
[679e644]2780 ;
2781
2782cfa_field_declaring_list: // CFA, new style field declaration
[f7e4db27]2783 // bit-fields are handled by C declarations
[033ff37]2784 cfa_abstract_declarator_tuple identifier_or_type_name
[679e644]2785 { $$ = $1->addName( $2 ); }
[033ff37]2786 | cfa_field_declaring_list ',' identifier_or_type_name
[dc3fbe5]2787 { $$ = $1->set_last( $1->cloneType( $3 ) ); }
[679e644]2788 ;
2789
2790cfa_field_abstract_list: // CFA, new style field declaration
[f7e4db27]2791 // bit-fields are handled by C declarations
[679e644]2792 cfa_abstract_declarator_tuple
2793 | cfa_field_abstract_list ','
[dc3fbe5]2794 { $$ = $1->set_last( $1->cloneType( 0 ) ); }
[4d51835]2795 ;
[51b73452]2796
2797bit_subrange_size_opt:
[4d51835]2798 // empty
[58dd019]2799 { $$ = nullptr; }
[4d51835]2800 | bit_subrange_size
2801 ;
[51b73452]2802
2803bit_subrange_size:
[c786e1d]2804 ':' assignment_expression
[4d51835]2805 { $$ = $2; }
2806 ;
[51b73452]2807
[7e13b11]2808// ************************** ENUMERATION *******************************
[647e2ea]2809
[9e7236f4]2810enum_type:
[30aab55]2811 // anonymous, no type name
[cd28605]2812 ENUM attribute_list_opt hide_opt '{' enumerator_list comma_opt '}' attribute_list_opt
[30aab55]2813 {
2814 if ( $3 == EnumHiding::Hide ) {
[253d0b4]2815 SemanticError( yylloc, "illegal syntax, hiding ('!') the enumerator names of an anonymous enumeration means the names are inaccessible." ); $$ = nullptr;
[30aab55]2816 } // if
[cd28605]2817 $$ = DeclarationNode::newEnum( nullptr, $5, true, false )->addQualifiers( $2 )->addQualifiers( $8 );
[30aab55]2818 }
[cd28605]2819 | ENUM enumerator_type attribute_list_opt hide_opt '{' enumerator_list comma_opt '}' attribute_list_opt
[30aab55]2820 {
[42422fb]2821 if ( $2 && ($2->storageClasses.val != 0 || $2->type->qualifiers.any()) ) {
[253d0b4]2822 SemanticError( yylloc, "illegal syntax, storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." );
[55266c7]2823 }
[42422fb]2824 if ( $4 == EnumHiding::Hide ) {
[253d0b4]2825 SemanticError( yylloc, "illegal syntax, hiding ('!') the enumerator names of an anonymous enumeration means the names are inaccessible." ); $$ = nullptr;
[42422fb]2826 } // if
[cd28605]2827 $$ = DeclarationNode::newEnum( nullptr, $6, true, true, $2 )->addQualifiers( $3 )->addQualifiers( $9 );
[b0d9ff7]2828 }
[30aab55]2829
[42422fb]2830 // named type
[cd28605]2831 | ENUM attribute_list_opt identifier attribute_list_opt
[30aab55]2832 { typedefTable.makeTypedef( *$3, "enum_type 1" ); }
[cd28605]2833 hide_opt '{' enumerator_list comma_opt '}' attribute_list_opt
2834 { $$ = DeclarationNode::newEnum( $3, $8, true, false, nullptr, $6 )->addQualifiers( $2 ->addQualifiers( $4 ))->addQualifiers( $11 ); }
2835 | ENUM attribute_list_opt typedef_name attribute_list_opt hide_opt '{' enumerator_list comma_opt '}' attribute_list_opt // unqualified type name
2836 { $$ = DeclarationNode::newEnum( $3->name, $7, true, false, nullptr, $5 )->addQualifiers( $2 )->addQualifiers( $4 )->addQualifiers( $10 ); }
[42422fb]2837 | ENUM enumerator_type attribute_list_opt identifier attribute_list_opt
[b0d9ff7]2838 {
[42422fb]2839 if ( $2 && ($2->storageClasses.any() || $2->type->qualifiers.val != 0) ) {
[253d0b4]2840 SemanticError( yylloc, "illegal syntax, storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." );
[55266c7]2841 }
[42422fb]2842 typedefTable.makeTypedef( *$4, "enum_type 2" );
[8bea701]2843 }
[cd28605]2844 hide_opt '{' enumerator_list comma_opt '}' attribute_list_opt
2845 { $$ = DeclarationNode::newEnum( $4, $9, true, true, $2, $7 )->addQualifiers( $3 )->addQualifiers( $5 )->addQualifiers( $12 ); }
2846 | ENUM enumerator_type attribute_list_opt typedef_name attribute_list_opt hide_opt '{' enumerator_list comma_opt '}' attribute_list_opt
2847 { $$ = DeclarationNode::newEnum( $4->name, $8, true, true, $2, $6 )->addQualifiers( $3 )->addQualifiers( $5 )->addQualifiers( $11 ); }
[30aab55]2848
2849 // forward declaration
[d0ffed1]2850 | enum_type_nobody
2851 ;
2852
[42422fb]2853enumerator_type:
2854 '(' ')' // pure enumeration
2855 { $$ = nullptr; }
2856 | '(' cfa_abstract_parameter_declaration ')' // typed enumeration
2857 { $$ = $2; }
2858 ;
2859
[c7f12a4]2860hide_opt:
2861 // empty
[7cf8006]2862 { $$ = EnumHiding::Visible; }
[c7f12a4]2863 | '!'
[7cf8006]2864 { $$ = EnumHiding::Hide; }
[c7f12a4]2865 ;
2866
[d0ffed1]2867enum_type_nobody: // enum - {...}
[f9c3100]2868 ENUM attribute_list_opt identifier
[71a422a]2869 {
2870 typedefTable.makeTypedef( *$3, "enum_type_nobody 1" );
2871 $$ = DeclarationNode::newEnum( $3, nullptr, false, false )->addQualifiers( $2 );
2872 }
[0bd46fd]2873 | ENUM attribute_list_opt type_name
[71a422a]2874 {
[6cef439]2875 typedefTable.makeTypedef( *$3->symbolic.name, "enum_type_nobody 2" );
2876 $$ = DeclarationNode::newEnum( $3->symbolic.name, nullptr, false, false )->addQualifiers( $2 );
[71a422a]2877 }
[4d51835]2878 ;
[51b73452]2879
2880enumerator_list:
[2ca7fc2]2881 // empty
2882 { SemanticError( yylloc, "enumeration must have a minimum of one enumerator, empty enumerator list is meaningless." ); $$ = nullptr; }
2883 | visible_hide_opt identifier_or_type_name enumerator_value_opt
[c7f12a4]2884 { $$ = DeclarationNode::newEnumValueGeneric( $2, $3 ); }
[f9c3100]2885 | INLINE type_name
[d9bad51]2886 {
2887 $$ = DeclarationNode::newEnumInLine( $2->symbolic.name );
2888 $2->symbolic.name = nullptr;
2889 delete $2;
2890 }
[7cf8006]2891 | enumerator_list ',' visible_hide_opt identifier_or_type_name enumerator_value_opt
[dc3fbe5]2892 { $$ = $1->set_last( DeclarationNode::newEnumValueGeneric( $4, $5 ) ); }
[85855b0]2893 | enumerator_list ',' INLINE type_name
2894 { $$ = $1->set_last( DeclarationNode::newEnumInLine( $4->symbolic.name ) ); }
[4d51835]2895 ;
[51b73452]2896
[7cf8006]2897visible_hide_opt:
[c7f12a4]2898 hide_opt
2899 | '^'
[7cf8006]2900 { $$ = EnumHiding::Visible; }
[c7f12a4]2901 ;
2902
[51b73452]2903enumerator_value_opt:
[4d51835]2904 // empty
[58dd019]2905 { $$ = nullptr; }
[7991c7d]2906 | '=' constant_expression { $$ = new InitializerNode( $2 ); }
2907 | '=' '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $3, true ); }
2908 // | simple_assignment_operator initializer
2909 // { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
[4d51835]2910 ;
[51b73452]2911
[7e13b11]2912// ************************** FUNCTION PARAMETERS *******************************
[647e2ea]2913
2914parameter_list_ellipsis_opt:
2915 // empty
[db19e1d]2916 { $$ = DeclarationNode::newFromTypeData( build_basic_type( TypeData::Void ) ); }
[647e2ea]2917 | ELLIPSIS
2918 { $$ = nullptr; }
2919 | parameter_list
2920 | parameter_list ',' ELLIPSIS
2921 { $$ = $1->addVarArgs(); }
2922 ;
2923
2924parameter_list: // abstract + real
2925 parameter_declaration
[cd28605]2926 | attribute_list parameter_declaration
2927 { $$ = $2->addQualifiers( $1 ); }
[647e2ea]2928 | abstract_parameter_declaration
[cd28605]2929 | attribute_list abstract_parameter_declaration
2930 { $$ = $2->addQualifiers( $1 ); }
2931 | parameter_list ',' attribute_list_opt parameter_declaration
2932 { $4->addQualifiers( $3 ); $$ = $1->set_last( $4 ); }
2933 | parameter_list ',' attribute_list_opt abstract_parameter_declaration
2934 { $4->addQualifiers( $3 ); $$ = $1->set_last( $4 ); }
[647e2ea]2935 ;
2936
2937cfa_parameter_list_ellipsis_opt: // CFA, abstract + real
[4d51835]2938 // empty
[e048ece]2939 { $$ = DeclarationNode::newFromTypeData( build_basic_type( TypeData::Void ) ); }
[2a8427c6]2940 | ELLIPSIS
[58dd019]2941 { $$ = nullptr; }
[c0aa336]2942 | cfa_parameter_list
[647e2ea]2943 | cfa_abstract_parameter_list
2944 | cfa_parameter_list ',' cfa_abstract_parameter_list
2945 { $$ = $1->set_last( $3 ); }
2946 | cfa_parameter_list ',' ELLIPSIS
[4d51835]2947 { $$ = $1->addVarArgs(); }
[647e2ea]2948 | cfa_abstract_parameter_list ',' ELLIPSIS
[4d51835]2949 { $$ = $1->addVarArgs(); }
2950 ;
[b87a5ed]2951
[c0aa336]2952cfa_parameter_list: // CFA
2953 // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
2954 // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
2955 cfa_parameter_declaration
[647e2ea]2956 | cfa_abstract_parameter_list ',' cfa_parameter_declaration
2957 { $$ = $1->set_last( $3 ); }
2958 | cfa_parameter_list ',' cfa_parameter_declaration
2959 { $$ = $1->set_last( $3 ); }
2960 | cfa_parameter_list ',' cfa_abstract_parameter_list ',' cfa_parameter_declaration
2961 { $$ = $1->set_last( $3 )->set_last( $5 ); }
[4d51835]2962 ;
[b87a5ed]2963
[c0aa336]2964cfa_abstract_parameter_list: // CFA, new & old style abstract
2965 cfa_abstract_parameter_declaration
[647e2ea]2966 | cfa_abstract_parameter_list ',' cfa_abstract_parameter_declaration
[dc3fbe5]2967 { $$ = $1->set_last( $3 ); }
[4d51835]2968 ;
[51b73452]2969
[de62360d]2970// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
[2871210]2971// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
[51b73452]2972
[647e2ea]2973parameter_declaration:
2974 // No SUE declaration in parameter list.
2975 declaration_specifier_nobody identifier_parameter_declarator default_initializer_opt
2976 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2977 | declaration_specifier_nobody type_parameter_redeclarator default_initializer_opt
2978 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2979 ;
2980
2981abstract_parameter_declaration:
2982 declaration_specifier_nobody default_initializer_opt
2983 { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
2984 | declaration_specifier_nobody abstract_parameter_declarator default_initializer_opt
2985 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2986 ;
2987
[c0aa336]2988cfa_parameter_declaration: // CFA, new & old style parameter declaration
[4d51835]2989 parameter_declaration
[5a51798]2990 | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initializer_opt
[4d51835]2991 { $$ = $1->addName( $2 ); }
[5a51798]2992 | cfa_abstract_tuple identifier_or_type_name default_initializer_opt
[c0aa336]2993 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
[4d51835]2994 { $$ = $1->addName( $2 ); }
[5a51798]2995 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initializer_opt
[4d51835]2996 { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
[7e13b11]2997 | cfa_function_specifier // int f( "int fp()" );
[4d51835]2998 ;
[b87a5ed]2999
[c0aa336]3000cfa_abstract_parameter_declaration: // CFA, new & old style parameter declaration
[0ac8d07]3001 abstract_parameter_declaration
[c0aa336]3002 | cfa_identifier_parameter_declarator_no_tuple
3003 | cfa_abstract_tuple
3004 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
3005 | type_qualifier_list cfa_abstract_tuple
[4d51835]3006 { $$ = $2->addQualifiers( $1 ); }
[7e13b11]3007 | cfa_abstract_function // int f( "int ()" );
[4d51835]3008 ;
[51b73452]3009
[c11e31c]3010// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
[de62360d]3011// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
3012// identifiers. The ANSI-style parameter-list can redefine a typedef name.
[51b73452]3013
[b87a5ed]3014identifier_list: // K&R-style parameter list => no types
[033ff37]3015 identifier
[4d51835]3016 { $$ = DeclarationNode::newName( $1 ); }
[033ff37]3017 | identifier_list ',' identifier
[dc3fbe5]3018 { $$ = $1->set_last( DeclarationNode::newName( $3 ) ); }
[4d51835]3019 ;
[51b73452]3020
[84d58c5]3021type_no_function: // sizeof, alignof, cast (constructor)
[cd28605]3022 type_specifier // cannot be type_specifier_nobody, e.g., (struct S {}){} is a thing
[c0aa336]3023 | type_specifier abstract_declarator
[4d51835]3024 { $$ = $2->addType( $1 ); }
[cd28605]3025 | cfa_abstract_declarator_tuple // CFA
[4d51835]3026 ;
[b87a5ed]3027
[84d58c5]3028type: // typeof, assertion
3029 type_no_function
[cd28605]3030 | attribute_list type_no_function
3031 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]3032 | cfa_abstract_function // CFA
[cd28605]3033 | attribute_list cfa_abstract_function // CFA
3034 { $$ = $2->addQualifiers( $1 ); }
[4d51835]3035 ;
[51b73452]3036
3037initializer_opt:
[4d51835]3038 // empty
[58dd019]3039 { $$ = nullptr; }
[f9c3100]3040 | simple_assignment_operator initializer { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
3041 | '=' VOID { $$ = new InitializerNode( true ); }
[63b3279e]3042 | '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $2, true ); }
[4d51835]3043 ;
[51b73452]3044
3045initializer:
[de62360d]3046 assignment_expression { $$ = new InitializerNode( $1 ); }
[7fdb94e1]3047 | '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $2, true ); }
[4d51835]3048 ;
[51b73452]3049
[7fdb94e1]3050initializer_list_opt:
[097e2b0]3051 // empty
[58dd019]3052 { $$ = nullptr; }
[097e2b0]3053 | initializer
[4d51835]3054 | designation initializer { $$ = $2->set_designators( $1 ); }
[b93c544]3055 | initializer_list_opt ',' initializer { $$ = $1->set_last( $3 ); }
3056 | initializer_list_opt ',' designation initializer { $$ = $1->set_last( $4->set_designators( $3 ) ); }
[4d51835]3057 ;
[b87a5ed]3058
[de62360d]3059// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
3060// '=' to separator the designator from the initializer value, as in:
[c11e31c]3061//
[b87a5ed]3062// int x[10] = { [1] = 3 };
[c11e31c]3063//
[de62360d]3064// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this case, CFA
3065// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
3066// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
3067// shift/reduce conflicts
[51b73452]3068
3069designation:
[1a8b17a]3070 designator_list '=' // C99, CFA uses ":" instead of "="
3071 | identifier_at ':' // GCC, field name, obsolete since GCC 2.5
[bb7422a]3072 { $$ = new ExpressionNode( build_varref( yylloc, $1 ) ); }
[4d51835]3073 ;
[51b73452]3074
[b87a5ed]3075designator_list: // C99
[4d51835]3076 designator
[2871210]3077 | designator_list designator
[b93c544]3078 { $$ = $1->set_last( $2 ); }
[4d51835]3079 ;
[51b73452]3080
3081designator:
[e16eb460]3082 '.' identifier_at // C99, field name
[bb7422a]3083 { $$ = new ExpressionNode( build_varref( yylloc, $2 ) ); }
[1a8b17a]3084 | '[' constant_expression ']' // C99, single array element
[917f67dd]3085 { $$ = $2; }
3086 | '[' subrange ']' // CFA, multiple array elements
3087 { $$ = $2; }
3088 | '[' constant_expression ELLIPSIS constant_expression ']' // GCC, multiple array elements
3089 { $$ = new ExpressionNode( new ast::RangeExpr( yylloc, maybeMoveBuild( $2 ), maybeMoveBuild( $4 ) ) ); }
3090 | '.' '[' field_name_list ']' // CFA, tuple field selector
[d1625f8]3091 { $$ = $3; }
[4d51835]3092 ;
[51b73452]3093
[de62360d]3094// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
3095// rather than an object-oriented type system. This required four groups of extensions:
[c11e31c]3096//
3097// Overloading: function, data, and operator identifiers may be overloaded.
3098//
[3ca7ef3]3099// Type declarations: "otype" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
[de62360d]3100// and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
3101// definitions of new types. Type declarations with storage class "extern" provide opaque types.
[c11e31c]3102//
[de62360d]3103// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
3104// site. A polymorphic function is not a template; it is a function, with an address and a type.
[c11e31c]3105//
3106// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
[de62360d]3107// types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
3108// hierarchies. Unlike classes, they can define relationships between types. Assertions declare that a type or
3109// types provide the operations declared by a specification. Assertions are normally used to declare requirements
3110// on type arguments of polymorphic functions.
[c11e31c]3111
[b87a5ed]3112type_parameter_list: // CFA
[67cf18c]3113 type_parameter
3114 | type_parameter_list ',' type_parameter
[dc3fbe5]3115 { $$ = $1->set_last( $3 ); }
[4d51835]3116 ;
[b87a5ed]3117
[84d58c5]3118type_initializer_opt: // CFA
3119 // empty
3120 { $$ = nullptr; }
3121 | '=' type
3122 { $$ = $2; }
3123 ;
3124
[b87a5ed]3125type_parameter: // CFA
[033ff37]3126 type_class identifier_or_type_name
[597ddfeb]3127 { typedefTable.addToScope( *$2, TYPEDEFname, "type_parameter 1" ); }
[5a51798]3128 type_initializer_opt assertion_list_opt
[67cf18c]3129 { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
[5a51798]3130 | identifier_or_type_name new_type_class
[71a422a]3131 { typedefTable.addToScope( *$1, TYPEDEFname, "type_parameter 2" ); }
[5a51798]3132 type_initializer_opt assertion_list_opt
3133 { $$ = DeclarationNode::newTypeParam( $2, $1 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
3134 | '[' identifier_or_type_name ']'
[b66d14a]3135 {
[71a422a]3136 typedefTable.addToScope( *$2, TYPEDIMname, "type_parameter 3" );
[bb7422a]3137 $$ = DeclarationNode::newTypeParam( ast::TypeDecl::Dimension, $2 );
[b66d14a]3138 }
[5a51798]3139 // | type_specifier identifier_parameter_declarator
[9997fee]3140 | assertion_list
[119889f]3141 // Invent a placeholder type to wrap these bare assertions. Rely on buildFunctionDecl to remove the placeholder.
3142 { $$ = DeclarationNode::newTypeParam( ast::TypeDecl::Dtype, new string( "" ) )->addAssertions( $1 ); }
[0522ebe]3143 | ENUM '(' identifier_or_type_name ')' identifier_or_type_name new_type_class type_initializer_opt assertion_list_opt
3144 {
3145 typedefTable.addToScope( *$3, TYPEDIMname, "type_parameter 4" );
3146 typedefTable.addToScope( *$5, TYPEDIMname, "type_parameter 5" );
3147 $$ = DeclarationNode::newTypeParam( $6, $5 )->addTypeInitializer( $7 )->addAssertions( $8 );
3148 }
[4d51835]3149 ;
[b87a5ed]3150
[5a51798]3151new_type_class: // CFA
3152 // empty
[bb7422a]3153 { $$ = ast::TypeDecl::Otype; }
[5a51798]3154 | '&'
[bb7422a]3155 { $$ = ast::TypeDecl::Dtype; }
[5a51798]3156 | '*'
[7e13b11]3157 { $$ = ast::TypeDecl::DStype; } // Dtype + sized
3158 // | '(' '*' ')' // Gregor made me do it
3159 // { $$ = ast::TypeDecl::Ftype; }
[5a51798]3160 | ELLIPSIS
[bb7422a]3161 { $$ = ast::TypeDecl::Ttype; }
[5a51798]3162 ;
3163
[b87a5ed]3164type_class: // CFA
[4040425]3165 OTYPE
[597ddfeb]3166 { SemanticError( yylloc, "otype keyword is deprecated, use T " ); }
[4d51835]3167 | DTYPE
[597ddfeb]3168 { SemanticError( yylloc, "dtype keyword is deprecated, use T &" ); }
[8f60f0b]3169 | FTYPE
[bb7422a]3170 { $$ = ast::TypeDecl::Ftype; }
[8f60f0b]3171 | TTYPE
[597ddfeb]3172 { SemanticError( yylloc, "ttype keyword is deprecated, use T ..." ); }
[4d51835]3173 ;
[b87a5ed]3174
3175assertion_list_opt: // CFA
[4d51835]3176 // empty
[58dd019]3177 { $$ = nullptr; }
[9997fee]3178 | assertion_list
3179 ;
3180
3181assertion_list: // CFA
3182 assertion
3183 | assertion_list assertion
[dc3fbe5]3184 { $$ = $1->set_last( $2 ); }
[4d51835]3185 ;
[b87a5ed]3186
3187assertion: // CFA
[033ff37]3188 '|' identifier_or_type_name '(' type_list ')'
[7fdb94e1]3189 { $$ = DeclarationNode::newTraitUse( $2, $4 ); }
[206ecae]3190 | '|' '{' trait_declaration_list '}'
3191 { $$ = $3; }
[35718a9]3192 // | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list pop '}' '(' type_list ')'
3193 // { SemanticError( yylloc, "Generic data-type assertion is currently unimplemented." ); $$ = nullptr; }
[4d51835]3194 ;
[b87a5ed]3195
[84d58c5]3196type_list: // CFA
3197 type
[bb7422a]3198 { $$ = new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $1 ) ) ); }
[4d51835]3199 | assignment_expression
[84d58c5]3200 | type_list ',' type
[b93c544]3201 { $$ = $1->set_last( new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $3 ) ) ) ); }
[84d58c5]3202 | type_list ',' assignment_expression
[b93c544]3203 { $$ = $1->set_last( $3 ); }
[4d51835]3204 ;
[b87a5ed]3205
3206type_declaring_list: // CFA
[4040425]3207 OTYPE type_declarator
[4d51835]3208 { $$ = $2; }
[4040425]3209 | storage_class_list OTYPE type_declarator
[4d51835]3210 { $$ = $3->addQualifiers( $1 ); }
3211 | type_declaring_list ',' type_declarator
[dc3fbe5]3212 { $$ = $1->set_last( $3->copySpecifiers( $1 ) ); }
[4d51835]3213 ;
[b87a5ed]3214
3215type_declarator: // CFA
[4d51835]3216 type_declarator_name assertion_list_opt
3217 { $$ = $1->addAssertions( $2 ); }
[84d58c5]3218 | type_declarator_name assertion_list_opt '=' type
[4d51835]3219 { $$ = $1->addAssertions( $2 )->addType( $4 ); }
3220 ;
[b87a5ed]3221
3222type_declarator_name: // CFA
[033ff37]3223 identifier_or_type_name
[4d51835]3224 {
[71a422a]3225 typedefTable.addToEnclosingScope( *$1, TYPEDEFname, "type_declarator_name 1" );
[a5f9444]3226 $$ = DeclarationNode::newTypeDecl( $1, nullptr );
[4d51835]3227 }
[033ff37]3228 | identifier_or_type_name '(' type_parameter_list ')'
[4d51835]3229 {
[71a422a]3230 typedefTable.addToEnclosingScope( *$1, TYPEGENname, "type_declarator_name 2" );
[35718a9]3231 $$ = DeclarationNode::newTypeDecl( $1, $3 );
[4d51835]3232 }
3233 ;
[b87a5ed]3234
[4040425]3235trait_specifier: // CFA
[033ff37]3236 TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' '}'
[8a97248]3237 {
[3d937e2]3238 SemanticWarning( yylloc, Warning::DeprecTraitSyntax );
[8a97248]3239 $$ = DeclarationNode::newTrait( $2, $4, nullptr );
3240 }
[597ddfeb]3241 | forall TRAIT identifier_or_type_name '{' '}' // new alternate
[7a24d76]3242 { $$ = DeclarationNode::newTrait( $3, $1, nullptr ); }
[206ecae]3243 | TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' trait_declaration_list '}'
[8a97248]3244 {
[3d937e2]3245 SemanticWarning( yylloc, Warning::DeprecTraitSyntax );
[206ecae]3246 $$ = DeclarationNode::newTrait( $2, $4, $7 );
[8a97248]3247 }
[206ecae]3248 | forall TRAIT identifier_or_type_name '{' trait_declaration_list '}' // alternate
3249 { $$ = DeclarationNode::newTrait( $3, $1, $5 ); }
[4d51835]3250 ;
[b87a5ed]3251
[84d58c5]3252trait_declaration_list: // CFA
[4040425]3253 trait_declaration
[206ecae]3254 | trait_declaration_list trait_declaration
3255 { $$ = $1->set_last( $2 ); }
[4d51835]3256 ;
[b87a5ed]3257
[84d58c5]3258trait_declaration: // CFA
[13e8427]3259 cfa_trait_declaring_list ';'
3260 | trait_declaring_list ';'
[4d51835]3261 ;
[b87a5ed]3262
[c0aa336]3263cfa_trait_declaring_list: // CFA
3264 cfa_variable_specifier
3265 | cfa_function_specifier
[206ecae]3266 | cfa_trait_declaring_list ',' identifier_or_type_name
3267 { $$ = $1->set_last( $1->cloneType( $3 ) ); }
[4d51835]3268 ;
[b87a5ed]3269
[4040425]3270trait_declaring_list: // CFA
[4941716]3271 // Cannot declare an aggregate or enumeration in a trait.
3272 type_specifier_nobody declarator
[7fdb94e1]3273 { $$ = $2->addType( $1 ); }
[206ecae]3274 | trait_declaring_list ',' declarator
3275 { $$ = $1->set_last( $1->cloneBaseType( $3 ) ); }
[4941716]3276 | error
3277 { SemanticError( yylloc, "Possible cause is declaring an aggregate or enumeration type in a trait." ); $$ = nullptr; }
[4d51835]3278 ;
[51b73452]3279
[e1d66c84]3280// **************************** EXTERNAL DEFINITIONS *****************************
[51b73452]3281
3282translation_unit:
[3d56d15b]3283 // empty, input file
[4d51835]3284 | external_definition_list
[12f1156]3285 { parseTree = parseTree ? parseTree->set_last( $1 ) : $1; }
[4d51835]3286 ;
[51b73452]3287
3288external_definition_list_opt:
[4d51835]3289 // empty
[58dd019]3290 { $$ = nullptr; }
[3d56d15b]3291 | external_definition_list
3292 ;
3293
[cd28605]3294external_definition_list:
3295 attribute_list_opt push external_definition pop
3296 { distAttr( $1, $3 ); $$ = $3; }
3297 | external_definition_list attribute_list_opt push external_definition pop
3298 { distAttr( $2, $4 ); $$ = $1 ? $1->set_last( $4 ) : $4->addQualifiers( $2 ); }
3299 ;
3300
[3d56d15b]3301up:
[fc20514]3302 { typedefTable.up( forall ); forall = false; }
[3d56d15b]3303 ;
3304
3305down:
3306 { typedefTable.down(); }
[4d51835]3307 ;
[51b73452]3308
3309external_definition:
[2d019af]3310 DIRECTIVE
[bb7422a]3311 { $$ = DeclarationNode::newDirectiveStmt( new StatementNode( build_directive( yylloc, $1 ) ) ); }
[2d019af]3312 | declaration
[1a73dbb]3313 {
3314 // Variable declarations of anonymous types requires creating a unique type-name across multiple translation
3315 // unit, which is a dubious task, especially because C uses name rather than structural typing; hence it is
3316 // disallowed at the moment.
[4eb3a7c5]3317 if ( $1->linkage == ast::Linkage::Cforall && ! $1->storageClasses.is_static &&
3318 $1->type && $1->type->kind == TypeData::AggregateInst ) {
[67467a3]3319 if ( $1->type->aggInst.aggregate->aggregate.anon ) {
3320 SemanticError( yylloc, "extern anonymous aggregate is currently unimplemented." ); $$ = nullptr;
[1a73dbb]3321 }
3322 }
3323 }
[996c8ed]3324 | IDENTIFIER IDENTIFIER
3325 { IdentifierBeforeIdentifier( *$1.str, *$2.str, " declaration" ); $$ = nullptr; }
[65ef0cd]3326 | IDENTIFIER type_qualifier // invalid syntax rule
[996c8ed]3327 { IdentifierBeforeType( *$1.str, "type qualifier" ); $$ = nullptr; }
[65ef0cd]3328 | IDENTIFIER storage_class // invalid syntax rule
[996c8ed]3329 { IdentifierBeforeType( *$1.str, "storage class" ); $$ = nullptr; }
[65ef0cd]3330 | IDENTIFIER basic_type_name // invalid syntax rule
[996c8ed]3331 { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
[65ef0cd]3332 | IDENTIFIER TYPEDEFname // invalid syntax rule
[996c8ed]3333 { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
[65ef0cd]3334 | IDENTIFIER TYPEGENname // invalid syntax rule
[996c8ed]3335 { IdentifierBeforeType( *$1.str, "type" ); $$ = nullptr; }
[4d51835]3336 | external_function_definition
[ecae5860]3337 | EXTENSION external_definition // GCC, multiple __extension__ allowed, meaning unknown
3338 {
3339 distExt( $2 ); // mark all fields in list
3340 $$ = $2;
3341 }
[e994912]3342 | ASM '(' string_literal ')' ';' // GCC, global assembler statement
[bb7422a]3343 { $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asm( yylloc, false, $3, nullptr ) ) ); }
[aac37fa]3344 | EXTERN STRINGliteral
3345 {
3346 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
[bb7422a]3347 linkage = ast::Linkage::update( yylloc, linkage, $2 );
[aac37fa]3348 }
[ae2f2ae]3349 up external_definition down
[aac37fa]3350 {
3351 linkage = linkageStack.top();
3352 linkageStack.pop();
3353 $$ = $5;
3354 }
[c0aa336]3355 | EXTERN STRINGliteral // C++-style linkage specifier
[4d51835]3356 {
[3b8e52c]3357 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
[bb7422a]3358 linkage = ast::Linkage::update( yylloc, linkage, $2 );
[4d51835]3359 }
[3d56d15b]3360 '{' up external_definition_list_opt down '}'
[4d51835]3361 {
3362 linkage = linkageStack.top();
3363 linkageStack.pop();
[3d56d15b]3364 $$ = $6;
[8e9cbb2]3365 }
[7e13b11]3366 // global distribution
[9997fee]3367 | type_qualifier_list
3368 {
[55266c7]3369 if ( $1->type->qualifiers.any() ) {
[253d0b4]3370 SemanticError( yylloc, "illegal syntax, CV qualifiers cannot be distributed; only storage-class and forall qualifiers." );
[55266c7]3371 }
[fc20514]3372 if ( $1->type->forall ) forall = true; // remember generic type
[3d56d15b]3373 }
3374 '{' up external_definition_list_opt down '}' // CFA, namespace
3375 {
[4c3ee8d]3376 distQual( $5, $1 );
[9fd9d015]3377 forall = false;
[3d56d15b]3378 $$ = $5;
[9997fee]3379 }
3380 | declaration_qualifier_list
3381 {
[55266c7]3382 if ( $1->type && $1->type->qualifiers.any() ) {
[253d0b4]3383 SemanticError( yylloc, "illegal syntax, CV qualifiers cannot be distributed; only storage-class and forall qualifiers." );
[55266c7]3384 }
[fc20514]3385 if ( $1->type && $1->type->forall ) forall = true; // remember generic type
[3d56d15b]3386 }
3387 '{' up external_definition_list_opt down '}' // CFA, namespace
3388 {
[4c3ee8d]3389 distQual( $5, $1 );
[9fd9d015]3390 forall = false;
[3d56d15b]3391 $$ = $5;
[9997fee]3392 }
3393 | declaration_qualifier_list type_qualifier_list
3394 {
[55266c7]3395 if ( ($1->type && $1->type->qualifiers.any()) || ($2->type && $2->type->qualifiers.any()) ) {
[253d0b4]3396 SemanticError( yylloc, "illegal syntax, CV qualifiers cannot be distributed; only storage-class and forall qualifiers." );
[55266c7]3397 }
[b2ddaf3]3398 if ( ($1->type && $1->type->forall) || ($2->type && $2->type->forall) ) forall = true; // remember generic type
[9997fee]3399 }
[3d56d15b]3400 '{' up external_definition_list_opt down '}' // CFA, namespace
[9997fee]3401 {
[284da8c]3402 distQual( $6, $1->addQualifiers( $2 ) );
[9fd9d015]3403 forall = false;
[3d56d15b]3404 $$ = $6;
[9997fee]3405 }
[b9f6791f]3406 | ';' // empty declaration
3407 { $$ = nullptr; }
[4d51835]3408 ;
3409
3410external_function_definition:
3411 function_definition
[de62360d]3412 // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
[c6b1105]3413 // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
3414 // Parsing is possible because function_definition does not appear in the context of an expression (nested
3415 // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
3416 // declaration must still have a type_specifier. OBSOLESCENT (see 1)
[4d51835]3417 | function_declarator compound_statement
[c0a33d2]3418 { $$ = $1->addFunctionBody( $2 ); }
[35718a9]3419 | KR_function_declarator KR_parameter_list_opt compound_statement
[c0a33d2]3420 { $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 ); }
[4d51835]3421 ;
[51b73452]3422
[8b47e50]3423with_clause_opt:
3424 // empty
[9997fee]3425 { $$ = nullptr; forall = false; }
[e78966e]3426 | WITH '(' type_list ')' attribute_list_opt // support scoped enumeration
[d8454b9]3427 {
3428 $$ = $3; forall = false;
3429 if ( $5 ) {
[253d0b4]3430 SemanticError( yylloc, "illegal syntax, attributes cannot be associated with function body. Move attribute(s) before \"with\" clause." );
[d8454b9]3431 $$ = nullptr;
3432 } // if
3433 }
[8b47e50]3434 ;
3435
[51b73452]3436function_definition:
[8b47e50]3437 cfa_function_declaration with_clause_opt compound_statement // CFA
[4d51835]3438 {
[481115f]3439 // Add the function body to the last identifier in the function definition list, i.e., foo3:
3440 // [const double] foo1(), foo2( int ), foo3( double ) { return 3.0; }
[5fcba14]3441 $1->get_last()->addFunctionBody( $3, $2 );
[481115f]3442 $$ = $1;
[4d51835]3443 }
[8b47e50]3444 | declaration_specifier function_declarator with_clause_opt compound_statement
[4d51835]3445 {
[c38ae92]3446 rebindForall( $1, $2 );
[7fdb94e1]3447 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
3448 }
[1f771fc]3449 | declaration_specifier function_type_redeclarator with_clause_opt compound_statement
[7fdb94e1]3450 {
3451 rebindForall( $1, $2 );
[5fcba14]3452 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
[4d51835]3453 }
[a16a7ec]3454 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]3455 | type_qualifier_list function_declarator with_clause_opt compound_statement
[c0a33d2]3456 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
[a16a7ec]3457 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]3458 | declaration_qualifier_list function_declarator with_clause_opt compound_statement
[c0a33d2]3459 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
[a16a7ec]3460 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]3461 | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
[c0a33d2]3462 { $$ = $3->addFunctionBody( $5, $4 )->addQualifiers( $2 )->addQualifiers( $1 ); }
[4d51835]3463
3464 // Old-style K&R function definition, OBSOLESCENT (see 4)
[35718a9]3465 | declaration_specifier KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[4d51835]3466 {
[c38ae92]3467 rebindForall( $1, $2 );
[5fcba14]3468 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addType( $1 );
[4d51835]3469 }
[a16a7ec]3470 // handles default int return type, OBSOLESCENT (see 1)
[35718a9]3471 | type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[c0a33d2]3472 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
[a16a7ec]3473 // handles default int return type, OBSOLESCENT (see 1)
[35718a9]3474 | declaration_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[c0a33d2]3475 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
[a16a7ec]3476 // handles default int return type, OBSOLESCENT (see 1)
[35718a9]3477 | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[c0a33d2]3478 { $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6, $5 )->addQualifiers( $2 )->addQualifiers( $1 ); }
[4d51835]3479 ;
[51b73452]3480
3481declarator:
[4d51835]3482 variable_declarator
[c6b1105]3483 | variable_type_redeclarator
[4d51835]3484 | function_declarator
[1f771fc]3485 | function_type_redeclarator
[4d51835]3486 ;
[51b73452]3487
3488subrange:
[4d51835]3489 constant_expression '~' constant_expression // CFA, integer subrange
[bb7422a]3490 { $$ = new ExpressionNode( new ast::RangeExpr( yylloc, maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
[4d51835]3491 ;
[b87a5ed]3492
[7e13b11]3493// **************************** ASM *****************************
3494
[b87a5ed]3495asm_name_opt: // GCC
[4d51835]3496 // empty
[58dd019]3497 { $$ = nullptr; }
3498 | ASM '(' string_literal ')' attribute_list_opt
[c0aa336]3499 {
3500 DeclarationNode * name = new DeclarationNode();
[32d6fdc]3501 name->asmName = maybeMoveBuild( $3 );
[c0aa336]3502 $$ = name->addQualifiers( $5 );
3503 }
[4d51835]3504 ;
[b87a5ed]3505
[7e13b11]3506// **************************** ATTRIBUTE *****************************
3507
[b87a5ed]3508attribute_list_opt: // GCC
[4d51835]3509 // empty
[58dd019]3510 { $$ = nullptr; }
[4d51835]3511 | attribute_list
3512 ;
[b87a5ed]3513
3514attribute_list: // GCC
[4d51835]3515 attribute
3516 | attribute_list attribute
[1db21619]3517 { $$ = $2->addQualifiers( $1 ); }
[4d51835]3518 ;
[b87a5ed]3519
3520attribute: // GCC
[44a81853]3521 ATTRIBUTE '(' '(' attribute_name_list ')' ')'
3522 { $$ = $4; }
[647e2ea]3523 | ATTRIBUTE '(' attribute_name_list ')' // CFA
3524 { $$ = $3; }
[a16f2b6]3525 | ATTR attribute_name_list ']' // CFA, @[...]
3526 { $$ = $2; }
3527 | C23_ATTRIBUTE // C23, [[...]], see lexer
[1a8b17a]3528 { $$ = DeclarationNode::newAttribute( $1 ); }
[4d51835]3529 ;
[b87a5ed]3530
[44a81853]3531attribute_name_list: // GCC
3532 attribute_name
3533 | attribute_name_list ',' attribute_name
[c0aa336]3534 { $$ = $3->addQualifiers( $1 ); }
[4d51835]3535 ;
[b87a5ed]3536
[44a81853]3537attribute_name: // GCC
[4d51835]3538 // empty
[44a81853]3539 { $$ = nullptr; }
3540 | attr_name
3541 { $$ = DeclarationNode::newAttribute( $1 ); }
[cbbd8fd7]3542 | attr_name '(' argument_expression_list_opt ')'
[44a81853]3543 { $$ = DeclarationNode::newAttribute( $1, $3 ); }
[4d51835]3544 ;
[b87a5ed]3545
[44a81853]3546attr_name: // GCC
[12f1156]3547 identifier_or_type_name
[114014c]3548 | FALLTHROUGH
3549 { $$ = Token{ new string( "fallthrough" ), { nullptr, -1 } }; }
[44a81853]3550 | CONST
[9ff56e7]3551 { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
[4d51835]3552 ;
[51b73452]3553
[c11e31c]3554// ============================================================================
[de62360d]3555// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
3556// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
3557// in:
[c11e31c]3558//
[b87a5ed]3559// int (*f())[10] { ... };
3560// ... (*f())[3] += 1; // definition mimics usage
[c11e31c]3561//
[de62360d]3562// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
3563// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
[c11e31c]3564// ============================================================================
3565
3566// ----------------------------------------------------------------------------
[de62360d]3567// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
3568// to define a variable or function prototype, e.g.:
[c11e31c]3569//
[b87a5ed]3570// valid declaration invalid definition
3571// ----------------- ------------------
[4d51835]3572// int f; int f {}
3573// int *f; int *f {}
[b87a5ed]3574// int f[10]; int f[10] {}
[4d51835]3575// int (*f)(int); int (*f)(int) {}
[c11e31c]3576//
[de62360d]3577// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
3578// variable_declarator and function_declarator.
[c11e31c]3579// ----------------------------------------------------------------------------
3580
[de62360d]3581// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
3582// declaring an array of functions versus a pointer to an array of functions.
[51b73452]3583
[5e25953]3584paren_identifier:
[e16eb460]3585 identifier_at
[5e25953]3586 { $$ = DeclarationNode::newName( $1 ); }
[c1b0b8f]3587 | '?' identifier
[daa4cc1]3588 // { SemanticError( yylloc, "keyword parameter is currently unimplemented." ); $$ = nullptr; }
3589 { $$ = DeclarationNode::newName( $2 ); }
[5e25953]3590 | '(' paren_identifier ')' // redundant parenthesis
3591 { $$ = $2; }
3592 ;
3593
[51b73452]3594variable_declarator:
[4d51835]3595 paren_identifier attribute_list_opt
[1db21619]3596 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3597 | variable_ptr
3598 | variable_array attribute_list_opt
[1db21619]3599 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3600 | variable_function attribute_list_opt
[1db21619]3601 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3602 ;
[51b73452]3603
3604variable_ptr:
[dd51906]3605 ptrref_operator variable_declarator
[a5f9444]3606 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[cd28605]3607 | ptrref_operator attribute_list variable_declarator
3608 { $$ = $3->addPointer( DeclarationNode::newPointer( nullptr, $1 ) )->addQualifiers( $2 ); }
[dd51906]3609 | ptrref_operator type_qualifier_list variable_declarator
[ce8c12f]3610 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3611 | '(' variable_ptr ')' attribute_list_opt // redundant parenthesis
3612 { $$ = $2->addQualifiers( $4 ); }
3613 | '(' attribute_list variable_ptr ')' attribute_list_opt // redundant parenthesis
3614 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
[4d51835]3615 ;
[51b73452]3616
3617variable_array:
[4d51835]3618 paren_identifier array_dimension
3619 { $$ = $1->addArray( $2 ); }
3620 | '(' variable_ptr ')' array_dimension
3621 { $$ = $2->addArray( $4 ); }
[5e25953]3622 | '(' attribute_list variable_ptr ')' array_dimension
3623 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[9fd9d015]3624 | '(' variable_array ')' multi_array_dimension // redundant parenthesis
[4d51835]3625 { $$ = $2->addArray( $4 ); }
[5e25953]3626 | '(' attribute_list variable_array ')' multi_array_dimension // redundant parenthesis
3627 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[4d51835]3628 | '(' variable_array ')' // redundant parenthesis
3629 { $$ = $2; }
[5e25953]3630 | '(' attribute_list variable_array ')' // redundant parenthesis
3631 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3632 ;
[51b73452]3633
3634variable_function:
[647e2ea]3635 '(' variable_ptr ')' '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]3636 { $$ = $2->addParamList( $5 ); }
[647e2ea]3637 | '(' attribute_list variable_ptr ')' '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]3638 { $$ = $3->addQualifiers( $2 )->addParamList( $6 ); }
[4d51835]3639 | '(' variable_function ')' // redundant parenthesis
3640 { $$ = $2; }
[5e25953]3641 | '(' attribute_list variable_function ')' // redundant parenthesis
3642 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3643 ;
[51b73452]3644
[c6b1105]3645// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
3646// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
3647// is the same scope. The pattern precludes returning arrays and functions versus pointers to arrays and functions.
[51b73452]3648
3649function_declarator:
[4d51835]3650 function_no_ptr attribute_list_opt
[1db21619]3651 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3652 | function_ptr
3653 | function_array attribute_list_opt
[1db21619]3654 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3655 ;
[51b73452]3656
3657function_no_ptr:
[647e2ea]3658 paren_identifier '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]3659 { $$ = $1->addParamList( $3 ); }
[647e2ea]3660 | '(' function_ptr ')' '(' parameter_list_ellipsis_opt ')'
[71a422a]3661 { $$ = $2->addParamList( $5 ); }
[647e2ea]3662 | '(' attribute_list function_ptr ')' '(' parameter_list_ellipsis_opt ')'
[71a422a]3663 { $$ = $3->addQualifiers( $2 )->addParamList( $6 ); }
[4d51835]3664 | '(' function_no_ptr ')' // redundant parenthesis
3665 { $$ = $2; }
[5e25953]3666 | '(' attribute_list function_no_ptr ')' // redundant parenthesis
3667 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3668 ;
[51b73452]3669
3670function_ptr:
[dd51906]3671 ptrref_operator function_declarator
[a5f9444]3672 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[cd28605]3673 | ptrref_operator attribute_list function_declarator
3674 { $$ = $3->addPointer( DeclarationNode::newPointer( nullptr, $1 ) )->addQualifiers( $2 ); }
[dd51906]3675 | ptrref_operator type_qualifier_list function_declarator
[ce8c12f]3676 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3677 | '(' function_ptr ')' attribute_list_opt
3678 { $$ = $2->addQualifiers( $4 ); }
3679 | '(' attribute_list function_ptr ')' attribute_list_opt
3680 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
[4d51835]3681 ;
[51b73452]3682
3683function_array:
[4d51835]3684 '(' function_ptr ')' array_dimension
3685 { $$ = $2->addArray( $4 ); }
[5e25953]3686 | '(' attribute_list function_ptr ')' array_dimension
3687 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[4d51835]3688 | '(' function_array ')' multi_array_dimension // redundant parenthesis
3689 { $$ = $2->addArray( $4 ); }
[5e25953]3690 | '(' attribute_list function_array ')' multi_array_dimension // redundant parenthesis
3691 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[4d51835]3692 | '(' function_array ')' // redundant parenthesis
3693 { $$ = $2; }
[5e25953]3694 | '(' attribute_list function_array ')' // redundant parenthesis
3695 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3696 ;
[51b73452]3697
[c0aa336]3698// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
3699//
3700// f( a, b, c ) int a, *b, c[]; {}
3701//
3702// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
3703// returning arrays and functions versus pointers to arrays and functions.
[51b73452]3704
[c0aa336]3705KR_function_declarator:
3706 KR_function_no_ptr
3707 | KR_function_ptr
3708 | KR_function_array
[4d51835]3709 ;
[51b73452]3710
[c0aa336]3711KR_function_no_ptr:
[4d51835]3712 paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
3713 { $$ = $1->addIdList( $3 ); }
[647e2ea]3714 | '(' KR_function_ptr ')' '(' parameter_list_ellipsis_opt ')'
[71a422a]3715 { $$ = $2->addParamList( $5 ); }
[647e2ea]3716 | '(' attribute_list KR_function_ptr ')' '(' parameter_list_ellipsis_opt ')'
[71a422a]3717 { $$ = $3->addQualifiers( $2 )->addParamList( $6 ); }
[c0aa336]3718 | '(' KR_function_no_ptr ')' // redundant parenthesis
[4d51835]3719 { $$ = $2; }
[5e25953]3720 | '(' attribute_list KR_function_no_ptr ')' // redundant parenthesis
3721 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3722 ;
[51b73452]3723
[c0aa336]3724KR_function_ptr:
3725 ptrref_operator KR_function_declarator
[a5f9444]3726 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[cd28605]3727 | ptrref_operator attribute_list KR_function_declarator
3728 { $$ = $3->addPointer( DeclarationNode::newPointer( nullptr, $1 ) )->addQualifiers( $2 ); }
[c0aa336]3729 | ptrref_operator type_qualifier_list KR_function_declarator
[ce8c12f]3730 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]3731 | '(' KR_function_ptr ')'
[4d51835]3732 { $$ = $2; }
[5e25953]3733 | '(' attribute_list KR_function_ptr ')'
3734 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3735 ;
[51b73452]3736
[c0aa336]3737KR_function_array:
3738 '(' KR_function_ptr ')' array_dimension
[4d51835]3739 { $$ = $2->addArray( $4 ); }
[5e25953]3740 | '(' attribute_list KR_function_ptr ')' array_dimension
3741 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[c0aa336]3742 | '(' KR_function_array ')' multi_array_dimension // redundant parenthesis
[4d51835]3743 { $$ = $2->addArray( $4 ); }
[5e25953]3744 | '(' attribute_list KR_function_array ')' multi_array_dimension // redundant parenthesis
3745 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[c0aa336]3746 | '(' KR_function_array ')' // redundant parenthesis
[4d51835]3747 { $$ = $2; }
[5e25953]3748 | '(' attribute_list KR_function_array ')' // redundant parenthesis
3749 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3750 ;
[51b73452]3751
[1f771fc]3752// This pattern parses a declaration for a variable that redefines a type name, e.g.:
[c11e31c]3753//
[b87a5ed]3754// typedef int foo;
3755// {
3756// int foo; // redefine typedef name in new scope
3757// }
[51b73452]3758
[2871210]3759paren_type:
[f9c3100]3760 typedef_name
[c4f68dc]3761 {
[f9c3100]3762 // hide type name in enclosing scope by variable name
[71a422a]3763 typedefTable.addToEnclosingScope( *$1->name, IDENTIFIER, "paren_type" );
[c4f68dc]3764 }
[2871210]3765 | '(' paren_type ')'
[4d51835]3766 { $$ = $2; }
3767 ;
[51b73452]3768
[5e25953]3769variable_type_redeclarator:
3770 paren_type attribute_list_opt
3771 { $$ = $1->addQualifiers( $2 ); }
[1f771fc]3772 | variable_type_ptr
3773 | variable_type_array attribute_list_opt
[5e25953]3774 { $$ = $1->addQualifiers( $2 ); }
[1f771fc]3775 | variable_type_function attribute_list_opt
[5e25953]3776 { $$ = $1->addQualifiers( $2 ); }
3777 ;
3778
[1f771fc]3779variable_type_ptr:
[c6b1105]3780 ptrref_operator variable_type_redeclarator
[a5f9444]3781 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[cd28605]3782 | ptrref_operator attribute_list variable_type_redeclarator
3783 { $$ = $3->addPointer( DeclarationNode::newPointer( nullptr, $1 ) )->addQualifiers( $2 ); }
[c6b1105]3784 | ptrref_operator type_qualifier_list variable_type_redeclarator
[ce8c12f]3785 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[1f771fc]3786 | '(' variable_type_ptr ')' attribute_list_opt // redundant parenthesis
[5e25953]3787 { $$ = $2->addQualifiers( $4 ); }
[1f771fc]3788 | '(' attribute_list variable_type_ptr ')' attribute_list_opt // redundant parenthesis
[5e25953]3789 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
[4d51835]3790 ;
[51b73452]3791
[1f771fc]3792variable_type_array:
[2871210]3793 paren_type array_dimension
[4d51835]3794 { $$ = $1->addArray( $2 ); }
[1f771fc]3795 | '(' variable_type_ptr ')' array_dimension
[4d51835]3796 { $$ = $2->addArray( $4 ); }
[1f771fc]3797 | '(' attribute_list variable_type_ptr ')' array_dimension
[5e25953]3798 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[1f771fc]3799 | '(' variable_type_array ')' multi_array_dimension // redundant parenthesis
[4d51835]3800 { $$ = $2->addArray( $4 ); }
[1f771fc]3801 | '(' attribute_list variable_type_array ')' multi_array_dimension // redundant parenthesis
[5e25953]3802 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
[1f771fc]3803 | '(' variable_type_array ')' // redundant parenthesis
[4d51835]3804 { $$ = $2; }
[1f771fc]3805 | '(' attribute_list variable_type_array ')' // redundant parenthesis
[5e25953]3806 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3807 ;
[51b73452]3808
[1f771fc]3809variable_type_function:
[647e2ea]3810 '(' variable_type_ptr ')' '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]3811 { $$ = $2->addParamList( $5 ); }
[647e2ea]3812 | '(' attribute_list variable_type_ptr ')' '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]3813 { $$ = $3->addQualifiers( $2 )->addParamList( $6 ); }
[1f771fc]3814 | '(' variable_type_function ')' // redundant parenthesis
3815 { $$ = $2; }
3816 | '(' attribute_list variable_type_function ')' // redundant parenthesis
3817 { $$ = $3->addQualifiers( $2 ); }
3818 ;
3819
3820// This pattern parses a declaration for a function prototype that redefines a type name. It precludes declaring an
3821// array of functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to
3822// arrays and functions.
3823
3824function_type_redeclarator:
3825 function_type_no_ptr attribute_list_opt
3826 { $$ = $1->addQualifiers( $2 ); }
3827 | function_type_ptr
3828 | function_type_array attribute_list_opt
3829 { $$ = $1->addQualifiers( $2 ); }
3830 ;
3831
3832function_type_no_ptr:
[647e2ea]3833 paren_type '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]3834 { $$ = $1->addParamList( $3 ); }
[647e2ea]3835 | '(' function_type_ptr ')' '(' parameter_list_ellipsis_opt ')'
[71a422a]3836 { $$ = $2->addParamList( $5 ); }
[647e2ea]3837 | '(' attribute_list function_type_ptr ')' '(' parameter_list_ellipsis_opt ')'
[71a422a]3838 { $$ = $3->addQualifiers( $2 )->addParamList( $6 ); }
[1f771fc]3839 | '(' function_type_no_ptr ')' // redundant parenthesis
[4d51835]3840 { $$ = $2; }
[1f771fc]3841 | '(' attribute_list function_type_no_ptr ')' // redundant parenthesis
3842 { $$ = $3->addQualifiers( $2 ); }
3843 ;
3844
3845function_type_ptr:
3846 ptrref_operator function_type_redeclarator
3847 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[cd28605]3848 | ptrref_operator attribute_list function_type_redeclarator
3849 { $$ = $3->addPointer( DeclarationNode::newPointer( nullptr, $1 ) )->addQualifiers( $2 ); }
[1f771fc]3850 | ptrref_operator type_qualifier_list function_type_redeclarator
3851 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3852 | '(' function_type_ptr ')' attribute_list_opt
3853 { $$ = $2->addQualifiers( $4 ); }
3854 | '(' attribute_list function_type_ptr ')' attribute_list_opt
3855 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3856 ;
3857
3858function_type_array:
3859 '(' function_type_ptr ')' array_dimension
3860 { $$ = $2->addArray( $4 ); }
3861 | '(' attribute_list function_type_ptr ')' array_dimension
3862 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3863 | '(' function_type_array ')' multi_array_dimension // redundant parenthesis
3864 { $$ = $2->addArray( $4 ); }
3865 | '(' attribute_list function_type_array ')' multi_array_dimension // redundant parenthesis
3866 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3867 | '(' function_type_array ')' // redundant parenthesis
[4d51835]3868 { $$ = $2; }
[1f771fc]3869 | '(' attribute_list function_type_array ')' // redundant parenthesis
[5e25953]3870 { $$ = $3->addQualifiers( $2 ); }
[4d51835]3871 ;
[51b73452]3872
[c0aa336]3873// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
3874// typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes
3875// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
3876// pointers to arrays and functions.
[51b73452]3877
3878identifier_parameter_declarator:
[4d51835]3879 paren_identifier attribute_list_opt
[1db21619]3880 { $$ = $1->addQualifiers( $2 ); }
[b6b3c42]3881 | '&' MUTEX paren_identifier attribute_list_opt
[6cef439]3882 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newFromTypeData( build_type_qualifier( ast::CV::Mutex ) ),
[647e2ea]3883 OperKinds::AddressOf ) )->addQualifiers( $4 ); }
[4d51835]3884 | identifier_parameter_ptr
3885 | identifier_parameter_array attribute_list_opt
[1db21619]3886 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3887 | identifier_parameter_function attribute_list_opt
[1db21619]3888 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3889 ;
[51b73452]3890
3891identifier_parameter_ptr:
[dd51906]3892 ptrref_operator identifier_parameter_declarator
[a5f9444]3893 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[cd28605]3894 | ptrref_operator attribute_list identifier_parameter_declarator
3895 { $$ = $3->addPointer( DeclarationNode::newPointer( nullptr, $1 ) )->addQualifiers( $2 ); }
[dd51906]3896 | ptrref_operator type_qualifier_list identifier_parameter_declarator
[ce8c12f]3897 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3898 | '(' identifier_parameter_ptr ')' attribute_list_opt // redundant parenthesis
[c0aa336]3899 { $$ = $2->addQualifiers( $4 ); }
[4d51835]3900 ;
[51b73452]3901
3902identifier_parameter_array:
[4d51835]3903 paren_identifier array_parameter_dimension
3904 { $$ = $1->addArray( $2 ); }
3905 | '(' identifier_parameter_ptr ')' array_dimension
3906 { $$ = $2->addArray( $4 ); }
3907 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
3908 { $$ = $2->addArray( $4 ); }
3909 | '(' identifier_parameter_array ')' // redundant parenthesis
3910 { $$ = $2; }
3911 ;
[51b73452]3912
3913identifier_parameter_function:
[647e2ea]3914 paren_identifier '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]3915 { $$ = $1->addParamList( $3 ); }
[647e2ea]3916 | '(' identifier_parameter_ptr ')' '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]3917 { $$ = $2->addParamList( $5 ); }
[4d51835]3918 | '(' identifier_parameter_function ')' // redundant parenthesis
3919 { $$ = $2; }
3920 ;
[b87a5ed]3921
[de62360d]3922// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
3923// e.g.:
[c11e31c]3924//
[b87a5ed]3925// typedef int foo;
[114014c]3926// forall( otype T ) struct foo;
[b87a5ed]3927// int f( int foo ); // redefine typedef name in new scope
[c11e31c]3928//
[c0aa336]3929// and allows the C99 array options, which can only appear in a parameter list.
[51b73452]3930
[2871210]3931type_parameter_redeclarator:
[f9c3100]3932 typedef_name attribute_list_opt
[1db21619]3933 { $$ = $1->addQualifiers( $2 ); }
[f9c3100]3934 | '&' MUTEX typedef_name attribute_list_opt
[6cef439]3935 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newFromTypeData( build_type_qualifier( ast::CV::Mutex ) ),
[647e2ea]3936 OperKinds::AddressOf ) )->addQualifiers( $4 ); }
[2871210]3937 | type_parameter_ptr
3938 | type_parameter_array attribute_list_opt
[1db21619]3939 { $$ = $1->addQualifiers( $2 ); }
[2871210]3940 | type_parameter_function attribute_list_opt
[1db21619]3941 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3942 ;
[51b73452]3943
[f9c3100]3944typedef_name:
[4d51835]3945 TYPEDEFname
[7fdb94e1]3946 { $$ = DeclarationNode::newName( $1 ); }
[2871210]3947 | TYPEGENname
[7fdb94e1]3948 { $$ = DeclarationNode::newName( $1 ); }
[4d51835]3949 ;
[51b73452]3950
[2871210]3951type_parameter_ptr:
[dd51906]3952 ptrref_operator type_parameter_redeclarator
[a5f9444]3953 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[cd28605]3954 | ptrref_operator attribute_list type_parameter_redeclarator
3955 { $$ = $3->addPointer( DeclarationNode::newPointer( nullptr, $1 ) )->addQualifiers( $2 ); }
[dd51906]3956 | ptrref_operator type_qualifier_list type_parameter_redeclarator
[ce8c12f]3957 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]3958 | '(' type_parameter_ptr ')' attribute_list_opt // redundant parenthesis
[c0aa336]3959 { $$ = $2->addQualifiers( $4 ); }
[4d51835]3960 ;
[51b73452]3961
[2871210]3962type_parameter_array:
[f9c3100]3963 typedef_name array_parameter_dimension
[4d51835]3964 { $$ = $1->addArray( $2 ); }
[2871210]3965 | '(' type_parameter_ptr ')' array_parameter_dimension
[4d51835]3966 { $$ = $2->addArray( $4 ); }
3967 ;
[51b73452]3968
[2871210]3969type_parameter_function:
[7e13b11]3970 typedef_name '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]3971 { $$ = $1->addParamList( $3 ); }
[647e2ea]3972 | '(' type_parameter_ptr ')' '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]3973 { $$ = $2->addParamList( $5 ); }
[4d51835]3974 ;
[b87a5ed]3975
[de62360d]3976// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
3977// which the type applies, e.g.:
[c11e31c]3978//
[b87a5ed]3979// sizeof( int );
[c0aa336]3980// sizeof( int * );
[b87a5ed]3981// sizeof( int [10] );
[c0aa336]3982// sizeof( int (*)() );
3983// sizeof( int () );
[c11e31c]3984//
[de62360d]3985// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3986// and functions versus pointers to arrays and functions.
[51b73452]3987
3988abstract_declarator:
[4d51835]3989 abstract_ptr
3990 | abstract_array attribute_list_opt
[1db21619]3991 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3992 | abstract_function attribute_list_opt
[1db21619]3993 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3994 ;
[51b73452]3995
3996abstract_ptr:
[cd28605]3997 ptrref_operator attribute_list_opt
3998 { $$ = DeclarationNode::newPointer( nullptr, $1 )->addQualifiers( $2 ); }
[dd51906]3999 | ptrref_operator type_qualifier_list
[ce8c12f]4000 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]4001 | ptrref_operator abstract_declarator
[a5f9444]4002 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[cd28605]4003 | ptrref_operator attribute_list abstract_declarator
4004 { $$ = $3->addPointer( DeclarationNode::newPointer( nullptr, $1 )->addQualifiers( $2 ) ); }
[dd51906]4005 | ptrref_operator type_qualifier_list abstract_declarator
[ce8c12f]4006 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]4007 | '(' abstract_ptr ')' attribute_list_opt
4008 { $$ = $2->addQualifiers( $4 ); }
[4d51835]4009 ;
[51b73452]4010
4011abstract_array:
[4d51835]4012 array_dimension
4013 | '(' abstract_ptr ')' array_dimension
4014 { $$ = $2->addArray( $4 ); }
4015 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
4016 { $$ = $2->addArray( $4 ); }
4017 | '(' abstract_array ')' // redundant parenthesis
4018 { $$ = $2; }
4019 ;
[51b73452]4020
4021abstract_function:
[7e13b11]4022 '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]4023 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $2, nullptr ); }
[647e2ea]4024 | '(' abstract_ptr ')' '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]4025 { $$ = $2->addParamList( $5 ); }
[4d51835]4026 | '(' abstract_function ')' // redundant parenthesis
4027 { $$ = $2; }
4028 ;
[51b73452]4029
4030array_dimension:
[4d51835]4031 // Only the first dimension can be empty.
[2871210]4032 '[' ']'
[a5f9444]4033 { $$ = DeclarationNode::newArray( nullptr, nullptr, false ); }
[2871210]4034 | '[' ']' multi_array_dimension
[a5f9444]4035 { $$ = DeclarationNode::newArray( nullptr, nullptr, false )->addArray( $3 ); }
[910e1d0]4036 // Cannot use constant_expression because of tuples => semantic check
[917f67dd]4037 | '[' assignment_expression ',' comma_expression ']' // CFA
4038 { $$ = DeclarationNode::newArray( $2, nullptr, false )->addArray( DeclarationNode::newArray( $4, nullptr, false ) ); }
[6a99803]4039 // { SemanticError( yylloc, "New array dimension is currently unimplemented." ); $$ = nullptr; }
[647e2ea]4040
4041 // If needed, the following parses and does not use comma_expression, so the array structure can be built.
4042 // | '[' push assignment_expression pop ',' push array_dimension_list pop ']' // CFA
4043
[917f67dd]4044 | '[' array_type_list ']' // CFA
4045 { $$ = DeclarationNode::newArray( $2, nullptr, false ); }
[4d51835]4046 | multi_array_dimension
4047 ;
[51b73452]4048
[647e2ea]4049// array_dimension_list:
4050// assignment_expression
4051// | array_dimension_list ',' assignment_expression
4052// ;
4053
[d41735a]4054array_type_list:
4055 basic_type_name
[bb7422a]4056 { $$ = new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $1 ) ) ); }
[d41735a]4057 | type_name
[bb7422a]4058 { $$ = new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $1 ) ) ); }
[d41735a]4059 | assignment_expression upupeq assignment_expression
4060 | array_type_list ',' basic_type_name
[b93c544]4061 { $$ = $1->set_last( new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $3 ) ) ) ); }
[bb7422a]4062 | array_type_list ',' type_name
[b93c544]4063 { $$ = $1->set_last( new ExpressionNode( new ast::TypeExpr( yylloc, maybeMoveBuildType( $3 ) ) ) ); }
[d41735a]4064 | array_type_list ',' assignment_expression upupeq assignment_expression
4065 ;
4066
4067upupeq:
4068 '~'
4069 { $$ = OperKinds::LThan; }
[e86735ba]4070 | ErangeUpLe
[d41735a]4071 { $$ = OperKinds::LEThan; }
[9fd9d015]4072 ;
[d41735a]4073
[51b73452]4074multi_array_dimension:
[917f67dd]4075 '[' assignment_expression ']'
4076 { $$ = DeclarationNode::newArray( $2, nullptr, false ); }
4077 | '[' '*' ']' // C99
[4d51835]4078 { $$ = DeclarationNode::newVarArray( 0 ); }
[917f67dd]4079 | multi_array_dimension '[' assignment_expression ']'
4080 { $$ = $1->addArray( DeclarationNode::newArray( $3, nullptr, false ) ); }
4081 | multi_array_dimension '[' '*' ']' // C99
[4d51835]4082 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
4083 ;
[51b73452]4084
[c11e31c]4085// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
4086// identifier to which the type applies, e.g.:
4087//
[c0aa336]4088// int f( int ); // not handled here
4089// int f( int * ); // abstract function-prototype parameter; no parameter name specified
4090// int f( int (*)() ); // abstract function-prototype parameter; no parameter name specified
[b87a5ed]4091// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
[c11e31c]4092//
[de62360d]4093// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
[f678c53b]4094// and functions versus pointers to arrays and functions. In addition, the pattern handles the special meaning of
4095// parenthesis around a typedef name:
[c0aa336]4096//
4097// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
4098// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
4099// not as redundant parentheses around the identifier."
4100//
4101// For example:
4102//
4103// typedef float T;
4104// int f( int ( T [5] ) ); // see abstract_parameter_declarator
4105// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
4106// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
4107// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
4108//
4109// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
4110// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
4111// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
4112// functions.
[51b73452]4113
[59c7e3e]4114abstract_parameter_declarator_opt:
4115 // empty
4116 { $$ = nullptr; }
4117 | abstract_parameter_declarator
4118 ;
4119
[51b73452]4120abstract_parameter_declarator:
[4d51835]4121 abstract_parameter_ptr
[b6b3c42]4122 | '&' MUTEX attribute_list_opt
[446740a]4123 { $$ = DeclarationNode::newPointer( DeclarationNode::newFromTypeData( build_type_qualifier( ast::CV::Mutex ) ),
4124 OperKinds::AddressOf )->addQualifiers( $3 ); }
[4d51835]4125 | abstract_parameter_array attribute_list_opt
[1db21619]4126 { $$ = $1->addQualifiers( $2 ); }
[4d51835]4127 | abstract_parameter_function attribute_list_opt
[1db21619]4128 { $$ = $1->addQualifiers( $2 ); }
[4d51835]4129 ;
[51b73452]4130
4131abstract_parameter_ptr:
[cd28605]4132 ptrref_operator attribute_list_opt
4133 { $$ = DeclarationNode::newPointer( nullptr, $1 )->addQualifiers( $2 ); }
[dd51906]4134 | ptrref_operator type_qualifier_list
[ce8c12f]4135 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]4136 | ptrref_operator abstract_parameter_declarator
[ce8c12f]4137 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[dd51906]4138 | ptrref_operator type_qualifier_list abstract_parameter_declarator
[ce8c12f]4139 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]4140 | '(' abstract_parameter_ptr ')' attribute_list_opt // redundant parenthesis
[c0aa336]4141 { $$ = $2->addQualifiers( $4 ); }
[4d51835]4142 ;
[51b73452]4143
4144abstract_parameter_array:
[4d51835]4145 array_parameter_dimension
4146 | '(' abstract_parameter_ptr ')' array_parameter_dimension
4147 { $$ = $2->addArray( $4 ); }
4148 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
4149 { $$ = $2->addArray( $4 ); }
4150 | '(' abstract_parameter_array ')' // redundant parenthesis
4151 { $$ = $2; }
4152 ;
[51b73452]4153
4154abstract_parameter_function:
[7e13b11]4155 '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]4156 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $2, nullptr ); }
[647e2ea]4157 | '(' abstract_parameter_ptr ')' '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]4158 { $$ = $2->addParamList( $5 ); }
[4d51835]4159 | '(' abstract_parameter_function ')' // redundant parenthesis
4160 { $$ = $2; }
4161 ;
[51b73452]4162
4163array_parameter_dimension:
[4d51835]4164 // Only the first dimension can be empty or have qualifiers.
4165 array_parameter_1st_dimension
4166 | array_parameter_1st_dimension multi_array_dimension
4167 { $$ = $1->addArray( $2 ); }
4168 | multi_array_dimension
4169 ;
[51b73452]4170
[c11e31c]4171// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
4172//
[de62360d]4173// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
4174// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
[51b73452]4175
4176array_parameter_1st_dimension:
[2871210]4177 '[' ']'
[a5f9444]4178 { $$ = DeclarationNode::newArray( nullptr, nullptr, false ); }
[13e8427]4179 // multi_array_dimension handles the '[' '*' ']' case
[c0a33d2]4180 | '[' push type_qualifier_list '*' pop ']' // remaining C99
4181 { $$ = DeclarationNode::newVarArray( $3 ); }
4182 | '[' push type_qualifier_list pop ']'
[a5f9444]4183 { $$ = DeclarationNode::newArray( nullptr, $3, false ); }
[13e8427]4184 // multi_array_dimension handles the '[' assignment_expression ']' case
[c0a33d2]4185 | '[' push type_qualifier_list assignment_expression pop ']'
4186 { $$ = DeclarationNode::newArray( $4, $3, false ); }
4187 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
4188 { $$ = DeclarationNode::newArray( $5, $4, true ); }
4189 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
4190 { $$ = DeclarationNode::newArray( $5, $3, true ); }
[4d51835]4191 ;
[b87a5ed]4192
[c0aa336]4193// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
[c11e31c]4194//
[71a422a]4195// struct S {
4196// int;
4197// int *;
4198// int [10];
4199// int (*)();
4200// };
[51b73452]4201
4202variable_abstract_declarator:
[4d51835]4203 variable_abstract_ptr
4204 | variable_abstract_array attribute_list_opt
[1db21619]4205 { $$ = $1->addQualifiers( $2 ); }
[4d51835]4206 | variable_abstract_function attribute_list_opt
[1db21619]4207 { $$ = $1->addQualifiers( $2 ); }
[4d51835]4208 ;
[51b73452]4209
4210variable_abstract_ptr:
[cd28605]4211 ptrref_operator attribute_list_opt
4212 { $$ = DeclarationNode::newPointer( nullptr, $1 )->addQualifiers( $2 ); }
[dd51906]4213 | ptrref_operator type_qualifier_list
[ce8c12f]4214 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]4215 | ptrref_operator variable_abstract_declarator
[a5f9444]4216 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[dd51906]4217 | ptrref_operator type_qualifier_list variable_abstract_declarator
[ce8c12f]4218 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[5e25953]4219 | '(' variable_abstract_ptr ')' attribute_list_opt // redundant parenthesis
[c0aa336]4220 { $$ = $2->addQualifiers( $4 ); }
[4d51835]4221 ;
[51b73452]4222
4223variable_abstract_array:
[4d51835]4224 array_dimension
4225 | '(' variable_abstract_ptr ')' array_dimension
4226 { $$ = $2->addArray( $4 ); }
4227 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
4228 { $$ = $2->addArray( $4 ); }
4229 | '(' variable_abstract_array ')' // redundant parenthesis
4230 { $$ = $2; }
4231 ;
[51b73452]4232
4233variable_abstract_function:
[647e2ea]4234 '(' variable_abstract_ptr ')' '(' parameter_list_ellipsis_opt ')' // empty parameter list OBSOLESCENT (see 3)
[71a422a]4235 { $$ = $2->addParamList( $5 ); }
[4d51835]4236 | '(' variable_abstract_function ')' // redundant parenthesis
4237 { $$ = $2; }
4238 ;
[b87a5ed]4239
[de62360d]4240// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
4241// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
[b87a5ed]4242
[c0aa336]4243cfa_identifier_parameter_declarator_tuple: // CFA
4244 cfa_identifier_parameter_declarator_no_tuple
4245 | cfa_abstract_tuple
4246 | type_qualifier_list cfa_abstract_tuple
[4d51835]4247 { $$ = $2->addQualifiers( $1 ); }
4248 ;
[b87a5ed]4249
[c0aa336]4250cfa_identifier_parameter_declarator_no_tuple: // CFA
4251 cfa_identifier_parameter_ptr
4252 | cfa_identifier_parameter_array
[dd54139]4253 | type_qualifier_list cfa_identifier_parameter_array
4254 { $$ = $2->addQualifiers( $1 ); }
[4d51835]4255 ;
[b87a5ed]4256
[c0aa336]4257cfa_identifier_parameter_ptr: // CFA
[d0ffed1]4258 // No SUE declaration in parameter list.
[cd28605]4259 ptrref_operator type_specifier_nobody
[a5f9444]4260 { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[cd28605]4261 | ptrref_operator attribute_list type_specifier_nobody
4262 { $$ = $3->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) )->addQualifiers( $2 ); }
[d0ffed1]4263 | type_qualifier_list ptrref_operator type_specifier_nobody
[ce8c12f]4264 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]4265 | ptrref_operator cfa_abstract_function
[a5f9444]4266 { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[c0aa336]4267 | type_qualifier_list ptrref_operator cfa_abstract_function
[ce8c12f]4268 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]4269 | ptrref_operator cfa_identifier_parameter_declarator_tuple
[a5f9444]4270 { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[c0aa336]4271 | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
[ce8c12f]4272 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[4d51835]4273 ;
[b87a5ed]4274
[c0aa336]4275cfa_identifier_parameter_array: // CFA
[de62360d]4276 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
4277 // shift/reduce conflict with new-style empty (void) function return type.
[d0ffed1]4278 '[' ']' type_specifier_nobody
[a5f9444]4279 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[dd54139]4280 | '[' ']' cfa_abstract_tuple
4281 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[d0ffed1]4282 | cfa_array_parameter_1st_dimension type_specifier_nobody
[4d51835]4283 { $$ = $2->addNewArray( $1 ); }
[dd54139]4284 | cfa_array_parameter_1st_dimension cfa_abstract_tuple
4285 { $$ = $2->addNewArray( $1 ); }
[d0ffed1]4286 | '[' ']' multi_array_dimension type_specifier_nobody
[a5f9444]4287 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[dd54139]4288 | '[' ']' multi_array_dimension cfa_abstract_tuple
4289 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[d0ffed1]4290 | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
[4d51835]4291 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[dd54139]4292 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_abstract_tuple
4293 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[d0ffed1]4294 | multi_array_dimension type_specifier_nobody
[4d51835]4295 { $$ = $2->addNewArray( $1 ); }
[dd54139]4296 | multi_array_dimension cfa_abstract_tuple
4297 { $$ = $2->addNewArray( $1 ); }
[9059213]4298
[c0aa336]4299 | '[' ']' cfa_identifier_parameter_ptr
[a5f9444]4300 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]4301 | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
[4d51835]4302 { $$ = $2->addNewArray( $1 ); }
[c0aa336]4303 | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
[a5f9444]4304 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]4305 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]4306 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[c0aa336]4307 | multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]4308 { $$ = $2->addNewArray( $1 ); }
4309 ;
[51b73452]4310
[c0aa336]4311cfa_array_parameter_1st_dimension:
[917f67dd]4312 '[' type_qualifier_list '*' ']' // remaining C99
4313 { $$ = DeclarationNode::newVarArray( $2 ); }
4314 | '[' type_qualifier_list assignment_expression ']'
4315 { $$ = DeclarationNode::newArray( $3, $2, false ); }
4316 | '[' declaration_qualifier_list assignment_expression ']'
[4d51835]4317 // declaration_qualifier_list must be used because of shift/reduce conflict with
4318 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
4319 // appear in this context.
[917f67dd]4320 { $$ = DeclarationNode::newArray( $3, $2, true ); }
4321 | '[' declaration_qualifier_list type_qualifier_list assignment_expression ']'
4322 { $$ = DeclarationNode::newArray( $4, $3->addQualifiers( $2 ), true ); }
[4d51835]4323 ;
[b87a5ed]4324
[de62360d]4325// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
4326// identifier to which the type applies, e.g.:
[c11e31c]4327//
[b87a5ed]4328// [int] f( int ); // abstract variable parameter; no parameter name specified
4329// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
[c11e31c]4330//
4331// These rules need LR(3):
4332//
[c0aa336]4333// cfa_abstract_tuple identifier_or_type_name
[647e2ea]4334// '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_list_ellipsis_opt ')'
[c11e31c]4335//
4336// since a function return type can be syntactically identical to a tuple type:
4337//
[b87a5ed]4338// [int, int] t;
4339// [int, int] f( int );
[c11e31c]4340//
[2871210]4341// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
[c0aa336]4342// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
4343// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
4344// duplicated when appearing with cfa_function_specifier.
[b87a5ed]4345
[c0aa336]4346cfa_abstract_declarator_tuple: // CFA
4347 cfa_abstract_tuple
4348 | type_qualifier_list cfa_abstract_tuple
[4d51835]4349 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]4350 | cfa_abstract_declarator_no_tuple
[4d51835]4351 ;
[b87a5ed]4352
[c0aa336]4353cfa_abstract_declarator_no_tuple: // CFA
4354 cfa_abstract_ptr
4355 | cfa_abstract_array
[4d51835]4356 ;
[b87a5ed]4357
[c0aa336]4358cfa_abstract_ptr: // CFA
[dd51906]4359 ptrref_operator type_specifier
[a5f9444]4360 { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[cd28605]4361 | ptrref_operator attribute_list type_specifier
4362 { $$ = $3->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) )->addQualifiers( $2 ); }
[dd51906]4363 | type_qualifier_list ptrref_operator type_specifier
[ce8c12f]4364 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]4365 | ptrref_operator cfa_abstract_function
[a5f9444]4366 { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[c0aa336]4367 | type_qualifier_list ptrref_operator cfa_abstract_function
[ce8c12f]4368 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]4369 | ptrref_operator cfa_abstract_declarator_tuple
[a5f9444]4370 { $$ = $2->addNewPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[c0aa336]4371 | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
[ce8c12f]4372 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[4d51835]4373 ;
[b87a5ed]4374
[c0aa336]4375cfa_abstract_array: // CFA
[de62360d]4376 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
4377 // empty (void) function return type.
[2871210]4378 '[' ']' type_specifier
[2298f728]4379 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[2871210]4380 | '[' ']' multi_array_dimension type_specifier
[2298f728]4381 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[4d51835]4382 | multi_array_dimension type_specifier
4383 { $$ = $2->addNewArray( $1 ); }
[c0aa336]4384 | '[' ']' cfa_abstract_ptr
[2298f728]4385 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]4386 | '[' ']' multi_array_dimension cfa_abstract_ptr
[2298f728]4387 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]4388 | multi_array_dimension cfa_abstract_ptr
[4d51835]4389 { $$ = $2->addNewArray( $1 ); }
4390 ;
[b87a5ed]4391
[c0aa336]4392cfa_abstract_tuple: // CFA
[917f67dd]4393 '[' cfa_abstract_parameter_list ']'
4394 { $$ = DeclarationNode::newTuple( $2 ); }
4395 | '[' type_specifier_nobody ELLIPSIS ']'
[13e8427]4396 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
[917f67dd]4397 | '[' type_specifier_nobody ELLIPSIS constant_expression ']'
[13e8427]4398 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
[4d51835]4399 ;
[b87a5ed]4400
[c0aa336]4401cfa_abstract_function: // CFA
[647e2ea]4402 '[' ']' '(' cfa_parameter_list_ellipsis_opt ')'
4403 { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
4404 | cfa_abstract_tuple '(' push cfa_parameter_list_ellipsis_opt pop ')'
[c0a33d2]4405 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[647e2ea]4406 | cfa_function_return '(' push cfa_parameter_list_ellipsis_opt pop ')'
[c0a33d2]4407 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[4d51835]4408 ;
[b87a5ed]4409
[de62360d]4410// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
4411// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
[c11e31c]4412//
[de62360d]4413// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
4414// the declaration specifiers in a declaration is an obsolescent feature."
[c11e31c]4415//
4416// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
4417// prototype-format parameter type declarators) is an obsolescent feature."
4418//
[de62360d]4419// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
4420// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
[51b73452]4421
[e1d66c84]4422// ************************ MISCELLANEOUS ********************************
[51b73452]4423
[b87a5ed]4424comma_opt: // redundant comma
[4d51835]4425 // empty
4426 | ','
4427 ;
[51b73452]4428
[5a51798]4429default_initializer_opt:
[4d51835]4430 // empty
[58dd019]4431 { $$ = nullptr; }
[4d51835]4432 | '=' assignment_expression
4433 { $$ = $2; }
4434 ;
[51b73452]4435
4436%%
[a1c9ddd]4437
[c11e31c]4438// ----end of grammar----
[51b73452]4439
[c11e31c]4440// Local Variables: //
[b87a5ed]4441// mode: c++ //
[de62360d]4442// tab-width: 4 //
[7e13b11]4443// compile-command: "bison -Wcounterexamples parser.yy" //
[c11e31c]4444// End: //
Note: See TracBrowser for help on using the repository browser.