source: src/Parser/parser.yy@ 9d7a19f

Last change on this file since 9d7a19f was 366f5cd, checked in by Peter A. Buhr <pabuhr@…>, 26 hours ago

differentiate between C _Alignof and gcc alignof

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