source: src/Parser/parser.yy@ 8e4f34e

Last change on this file since 8e4f34e was ecf3812, checked in by Andrew Beach <ajbeach@…>, 11 months ago

CastExpr reorganization and clean-up in Lvalue. I kept these from a fix that is not working.

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