source: src/Parser/parser.yy@ b166b1c

Last change on this file since b166b1c was 12f1156, checked in by Peter A. Buhr <pabuhr@…>, 20 months ago

simplify grammar in a few places

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