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

Last change on this file since 2fd3de7 was 2737b37, checked in by Peter A. Buhr <pabuhr@…>, 12 days ago

formatting

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