| 1 |
#ifndef RUST_AST_EXPR_H |
| 2 |
#define RUST_AST_EXPR_H |
| 3 |
|
| 4 |
#include "rust-ast.h" |
| 5 |
#include "rust-common.h" |
| 6 |
#include "rust-path.h" |
| 7 |
#include "rust-macro.h" |
| 8 |
#include "rust-operators.h" |
| 9 |
|
| 10 |
namespace Rust { |
| 11 |
namespace AST { |
| 12 |
/* TODO: if GCC moves to C++17 or allows boost, replace some boolean |
| 13 |
* "has_whatever" pairs with |
| 14 |
* optional types (std::optional or boost::optional)? */ |
| 15 |
|
| 16 |
// Loop label expression AST node used with break and continue expressions |
| 17 |
// TODO: inline? |
| 18 |
class LoopLabel /*: public Visitable*/ |
| 19 |
{ |
| 20 |
Lifetime label; // or type LIFETIME_OR_LABEL |
| 21 |
location_t locus; |
| 22 |
|
| 23 |
NodeId node_id; |
| 24 |
|
| 25 |
public: |
| 26 |
std::string as_string () const; |
| 27 |
|
| 28 |
LoopLabel (Lifetime loop_label, location_t locus = UNDEF_LOCATION) |
| 29 |
: label (std::move (loop_label)), locus (locus), |
| 30 |
node_id (Analysis::Mappings::get ().get_next_node_id ()) |
| 31 |
{} |
| 32 |
|
| 33 |
// Returns whether the LoopLabel is in an error state. |
| 34 |
location_t get_locus () const { return locus; } |
| 35 |
|
| 36 |
Lifetime &get_lifetime () { return label; } |
| 37 |
|
| 38 |
NodeId get_node_id () const { return node_id; } |
| 39 |
}; |
| 40 |
|
| 41 |
// AST node for an expression with an accompanying block - abstract |
| 42 |
class ExprWithBlock : public Expr |
| 43 |
{ |
| 44 |
protected: |
| 45 |
// pure virtual clone implementation |
| 46 |
virtual ExprWithBlock *clone_expr_with_block_impl () const = 0; |
| 47 |
|
| 48 |
// prevent having to define multiple clone expressions |
| 49 |
ExprWithBlock *clone_expr_impl () const final override |
| 50 |
{ |
| 51 |
return clone_expr_with_block_impl (); |
| 52 |
} |
| 53 |
|
| 54 |
bool is_expr_without_block () const final override { return false; }; |
| 55 |
|
| 56 |
public: |
| 57 |
// Unique pointer custom clone function |
| 58 |
std::unique_ptr<ExprWithBlock> clone_expr_with_block () const |
| 59 |
{ |
| 60 |
return std::unique_ptr<ExprWithBlock> (clone_expr_with_block_impl ()); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
// Literals? Or literal base? |
| 65 |
class LiteralExpr : public ExprWithoutBlock |
| 66 |
{ |
| 67 |
std::vector<Attribute> outer_attrs; |
| 68 |
Literal literal; |
| 69 |
location_t locus; |
| 70 |
|
| 71 |
public: |
| 72 |
std::string as_string () const override { return literal.as_string (); } |
| 73 |
|
| 74 |
Literal::LitType get_lit_type () const { return literal.get_lit_type (); } |
| 75 |
|
| 76 |
LiteralExpr (std::string value_as_string, Literal::LitType type, |
| 77 |
PrimitiveCoreType type_hint, std::vector<Attribute> outer_attrs, |
| 78 |
location_t locus) |
| 79 |
: outer_attrs (std::move (outer_attrs)), |
| 80 |
literal (std::move (value_as_string), type, type_hint), locus (locus) |
| 81 |
{} |
| 82 |
|
| 83 |
LiteralExpr (Literal literal, std::vector<Attribute> outer_attrs, |
| 84 |
location_t locus) |
| 85 |
: outer_attrs (std::move (outer_attrs)), literal (std::move (literal)), |
| 86 |
locus (locus) |
| 87 |
{} |
| 88 |
|
| 89 |
// Unique pointer custom clone function |
| 90 |
std::unique_ptr<LiteralExpr> clone_literal_expr () const |
| 91 |
{ |
| 92 |
return std::unique_ptr<LiteralExpr> (clone_literal_expr_impl ()); |
| 93 |
} |
| 94 |
|
| 95 |
location_t get_locus () const override final { return locus; } |
| 96 |
|
| 97 |
bool is_literal () const override final { return true; } |
| 98 |
|
| 99 |
Literal get_literal () const { return literal; } |
| 100 |
|
| 101 |
void accept_vis (ASTVisitor &vis) override; |
| 102 |
|
| 103 |
// Invalid if literal is in error state, so base stripping on that. |
| 104 |
void mark_for_strip () override { literal = Literal::create_error (); } |
| 105 |
bool is_marked_for_strip () const override { return literal.is_error (); } |
| 106 |
|
| 107 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 108 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 109 |
|
| 110 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 111 |
{ |
| 112 |
outer_attrs = std::move (new_attrs); |
| 113 |
} |
| 114 |
|
| 115 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Literal; } |
| 116 |
|
| 117 |
protected: |
| 118 |
/* Use covariance to implement clone function as returning this object rather |
| 119 |
* than base */ |
| 120 |
LiteralExpr *clone_expr_without_block_impl () const final override |
| 121 |
{ |
| 122 |
return clone_literal_expr_impl (); |
| 123 |
} |
| 124 |
|
| 125 |
/* not virtual as currently no subclasses of LiteralExpr, but could be in |
| 126 |
* future */ |
| 127 |
/*virtual*/ LiteralExpr *clone_literal_expr_impl () const |
| 128 |
{ |
| 129 |
return new LiteralExpr (*this); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
// Literal expression attribute body (non-macro attribute) |
| 134 |
class AttrInputLiteral : public AttrInput |
| 135 |
{ |
| 136 |
LiteralExpr literal_expr; |
| 137 |
|
| 138 |
public: |
| 139 |
AttrInputLiteral (LiteralExpr lit_expr) : literal_expr (std::move (lit_expr)) |
| 140 |
{} |
| 141 |
|
| 142 |
std::string as_string () const override |
| 143 |
{ |
| 144 |
return " = " + literal_expr.as_string (); |
| 145 |
} |
| 146 |
|
| 147 |
void accept_vis (ASTVisitor &vis) override; |
| 148 |
|
| 149 |
/* this can never be a cfg predicate - cfg and cfg_attr require a token-tree |
| 150 |
* cfg */ |
| 151 |
bool check_cfg_predicate (const Session &) const override { return false; } |
| 152 |
|
| 153 |
bool is_meta_item () const override { return false; } |
| 154 |
|
| 155 |
LiteralExpr &get_literal () { return literal_expr; } |
| 156 |
|
| 157 |
AttrInputType get_attr_input_type () const final override |
| 158 |
{ |
| 159 |
return AttrInput::AttrInputType::LITERAL; |
| 160 |
} |
| 161 |
|
| 162 |
protected: |
| 163 |
/* Use covariance to implement clone function as returning this object rather |
| 164 |
* than base */ |
| 165 |
AttrInputLiteral *clone_attr_input_impl () const override |
| 166 |
{ |
| 167 |
return new AttrInputLiteral (*this); |
| 168 |
} |
| 169 |
}; |
| 170 |
|
| 171 |
// Like an AttrInputLiteral, but stores a MacroInvocation |
| 172 |
class AttrInputMacro : public AttrInput |
| 173 |
{ |
| 174 |
std::unique_ptr<MacroInvocation> macro; |
| 175 |
|
| 176 |
public: |
| 177 |
AttrInputMacro (std::unique_ptr<MacroInvocation> macro) |
| 178 |
: macro (std::move (macro)) |
| 179 |
{} |
| 180 |
|
| 181 |
AttrInputMacro (const AttrInputMacro &oth); |
| 182 |
|
| 183 |
AttrInputMacro (AttrInputMacro &&oth) : macro (std::move (oth.macro)) {} |
| 184 |
|
| 185 |
void operator= (const AttrInputMacro &oth); |
| 186 |
|
| 187 |
void operator= (AttrInputMacro &&oth) { macro = std::move (oth.macro); } |
| 188 |
|
| 189 |
std::string as_string () const override; |
| 190 |
|
| 191 |
void accept_vis (ASTVisitor &vis) override; |
| 192 |
|
| 193 |
// assuming this can't be a cfg predicate |
| 194 |
bool check_cfg_predicate (const Session &) const override { return false; } |
| 195 |
|
| 196 |
// assuming this is like AttrInputLiteral |
| 197 |
bool is_meta_item () const override { return false; } |
| 198 |
|
| 199 |
std::unique_ptr<MacroInvocation> &get_macro () { return macro; } |
| 200 |
|
| 201 |
AttrInputType get_attr_input_type () const final override |
| 202 |
{ |
| 203 |
return AttrInput::AttrInputType::MACRO; |
| 204 |
} |
| 205 |
|
| 206 |
protected: |
| 207 |
AttrInputMacro *clone_attr_input_impl () const override |
| 208 |
{ |
| 209 |
return new AttrInputMacro (*this); |
| 210 |
} |
| 211 |
}; |
| 212 |
|
| 213 |
/* literal expr only meta item inner - TODO possibly replace with inheritance of |
| 214 |
* LiteralExpr itself? */ |
| 215 |
class MetaItemLitExpr : public MetaItemInner |
| 216 |
{ |
| 217 |
LiteralExpr lit_expr; |
| 218 |
|
| 219 |
public: |
| 220 |
MetaItemLitExpr (LiteralExpr lit_expr) : lit_expr (std::move (lit_expr)) {} |
| 221 |
|
| 222 |
std::string as_string () const override { return lit_expr.as_string (); } |
| 223 |
|
| 224 |
location_t get_locus () const override { return lit_expr.get_locus (); } |
| 225 |
|
| 226 |
LiteralExpr get_literal () const { return lit_expr; } |
| 227 |
|
| 228 |
LiteralExpr &get_literal () { return lit_expr; } |
| 229 |
|
| 230 |
void accept_vis (ASTVisitor &vis) override; |
| 231 |
|
| 232 |
bool check_cfg_predicate (const Session &session) const override; |
| 233 |
|
| 234 |
MetaItemInner::Kind get_kind () override |
| 235 |
{ |
| 236 |
return MetaItemInner::Kind::LitExpr; |
| 237 |
} |
| 238 |
|
| 239 |
protected: |
| 240 |
// Use covariance to implement clone function as returning this type |
| 241 |
MetaItemLitExpr *clone_meta_item_inner_impl () const override |
| 242 |
{ |
| 243 |
return new MetaItemLitExpr (*this); |
| 244 |
} |
| 245 |
}; |
| 246 |
|
| 247 |
// more generic meta item "path = lit" form |
| 248 |
class MetaItemPathLit : public MetaItem |
| 249 |
{ |
| 250 |
SimplePath path; |
| 251 |
LiteralExpr lit; |
| 252 |
|
| 253 |
public: |
| 254 |
MetaItemPathLit (SimplePath path, LiteralExpr lit_expr) |
| 255 |
: path (std::move (path)), lit (std::move (lit_expr)) |
| 256 |
{} |
| 257 |
|
| 258 |
SimplePath get_path () const { return path; } |
| 259 |
|
| 260 |
SimplePath &get_path () { return path; } |
| 261 |
|
| 262 |
LiteralExpr get_literal () const { return lit; } |
| 263 |
|
| 264 |
LiteralExpr &get_literal () { return lit; } |
| 265 |
|
| 266 |
std::string as_string () const override |
| 267 |
{ |
| 268 |
return path.as_string () + " = " + lit.as_string (); |
| 269 |
} |
| 270 |
|
| 271 |
MetaItem::ItemKind get_item_kind () const override |
| 272 |
{ |
| 273 |
return MetaItem::ItemKind::PathLit; |
| 274 |
} |
| 275 |
|
| 276 |
// There are two Locations in MetaItemPathLit (path and lit_expr), |
| 277 |
// we have no idea use which of them, just simply return UNKNOWN_LOCATION |
| 278 |
// now. |
| 279 |
// Maybe we will figure out when we really need the location in the future. |
| 280 |
location_t get_locus () const override { return UNKNOWN_LOCATION; } |
| 281 |
|
| 282 |
void accept_vis (ASTVisitor &vis) override; |
| 283 |
|
| 284 |
bool check_cfg_predicate (const Session &session) const override; |
| 285 |
/* TODO: return true if "ident" is defined and value of it is "lit", return |
| 286 |
* false otherwise */ |
| 287 |
|
| 288 |
Attribute to_attribute () const override; |
| 289 |
|
| 290 |
protected: |
| 291 |
// Use covariance to implement clone function as returning this type |
| 292 |
MetaItemPathLit *clone_meta_item_inner_impl () const override |
| 293 |
{ |
| 294 |
return new MetaItemPathLit (*this); |
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
/* Represents an expression using unary or binary operators as AST node. Can be |
| 299 |
* overloaded. */ |
| 300 |
class OperatorExpr : public ExprWithoutBlock |
| 301 |
{ |
| 302 |
// TODO: create binary and unary operator subclasses? |
| 303 |
private: |
| 304 |
location_t locus; |
| 305 |
|
| 306 |
protected: |
| 307 |
/* Variables must be protected to allow derived classes to use them as first |
| 308 |
* class citizens */ |
| 309 |
std::vector<Attribute> outer_attrs; |
| 310 |
std::unique_ptr<Expr> main_or_left_expr; |
| 311 |
|
| 312 |
// Constructor (only for initialisation of expr purposes) |
| 313 |
OperatorExpr (std::unique_ptr<Expr> main_or_left_expr, |
| 314 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 315 |
: locus (locus), outer_attrs (std::move (outer_attribs)), |
| 316 |
main_or_left_expr (std::move (main_or_left_expr)) |
| 317 |
{} |
| 318 |
|
| 319 |
// Copy constructor (only for initialisation of expr purposes) |
| 320 |
OperatorExpr (OperatorExpr const &other) |
| 321 |
: locus (other.locus), outer_attrs (other.outer_attrs) |
| 322 |
{ |
| 323 |
// guard to prevent null dereference (only required if error state) |
| 324 |
if (other.main_or_left_expr != nullptr) |
| 325 |
main_or_left_expr = other.main_or_left_expr->clone_expr (); |
| 326 |
} |
| 327 |
|
| 328 |
// Overload assignment operator to deep copy expr |
| 329 |
OperatorExpr &operator= (OperatorExpr const &other) |
| 330 |
{ |
| 331 |
ExprWithoutBlock::operator= (other); |
| 332 |
locus = other.locus; |
| 333 |
outer_attrs = other.outer_attrs; |
| 334 |
|
| 335 |
// guard to prevent null dereference (only required if error state) |
| 336 |
if (other.main_or_left_expr != nullptr) |
| 337 |
main_or_left_expr = other.main_or_left_expr->clone_expr (); |
| 338 |
else |
| 339 |
main_or_left_expr = nullptr; |
| 340 |
|
| 341 |
return *this; |
| 342 |
} |
| 343 |
|
| 344 |
// move constructors |
| 345 |
OperatorExpr (OperatorExpr &&other) = default; |
| 346 |
OperatorExpr &operator= (OperatorExpr &&other) = default; |
| 347 |
|
| 348 |
public: |
| 349 |
location_t get_locus () const override final { return locus; } |
| 350 |
|
| 351 |
// Invalid if expr is null, so base stripping on that. |
| 352 |
void mark_for_strip () override { main_or_left_expr = nullptr; } |
| 353 |
bool is_marked_for_strip () const override |
| 354 |
{ |
| 355 |
return main_or_left_expr == nullptr; |
| 356 |
} |
| 357 |
|
| 358 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 359 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 360 |
|
| 361 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 362 |
{ |
| 363 |
outer_attrs = std::move (new_attrs); |
| 364 |
} |
| 365 |
|
| 366 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Operator; } |
| 367 |
}; |
| 368 |
|
| 369 |
/* Unary prefix & or &mut (or && and &&mut) borrow operator. Cannot be |
| 370 |
* overloaded. */ |
| 371 |
class BorrowExpr : public OperatorExpr |
| 372 |
{ |
| 373 |
Mutability mutability; |
| 374 |
bool raw_borrow; |
| 375 |
bool double_borrow; |
| 376 |
|
| 377 |
public: |
| 378 |
std::string as_string () const override; |
| 379 |
|
| 380 |
BorrowExpr (std::unique_ptr<Expr> borrow_lvalue, Mutability mutability, |
| 381 |
bool raw_borrow, bool is_double_borrow, |
| 382 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 383 |
: OperatorExpr (std::move (borrow_lvalue), std::move (outer_attribs), |
| 384 |
locus), |
| 385 |
mutability (mutability), raw_borrow (raw_borrow), |
| 386 |
double_borrow (is_double_borrow) |
| 387 |
{} |
| 388 |
|
| 389 |
void accept_vis (ASTVisitor &vis) override; |
| 390 |
|
| 391 |
// TODO: is this better? Or is a "vis_block" better? |
| 392 |
Expr &get_borrowed_expr () |
| 393 |
{ |
| 394 |
rust_assert (main_or_left_expr != nullptr); |
| 395 |
return *main_or_left_expr; |
| 396 |
} |
| 397 |
|
| 398 |
bool get_is_mut () const { return mutability == Mutability::Mut; } |
| 399 |
|
| 400 |
Mutability get_mutability () const { return mutability; } |
| 401 |
|
| 402 |
bool get_is_double_borrow () const { return double_borrow; } |
| 403 |
bool is_raw_borrow () const { return raw_borrow; } |
| 404 |
|
| 405 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Borrow; } |
| 406 |
|
| 407 |
protected: |
| 408 |
/* Use covariance to implement clone function as returning this object rather |
| 409 |
* than base */ |
| 410 |
BorrowExpr *clone_expr_without_block_impl () const override |
| 411 |
{ |
| 412 |
return new BorrowExpr (*this); |
| 413 |
} |
| 414 |
}; |
| 415 |
|
| 416 |
// Unary prefix * deference operator |
| 417 |
class DereferenceExpr : public OperatorExpr |
| 418 |
{ |
| 419 |
public: |
| 420 |
std::string as_string () const override; |
| 421 |
|
| 422 |
// Constructor calls OperatorExpr's protected constructor |
| 423 |
DereferenceExpr (std::unique_ptr<Expr> deref_lvalue, |
| 424 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 425 |
: OperatorExpr (std::move (deref_lvalue), std::move (outer_attribs), locus) |
| 426 |
{} |
| 427 |
|
| 428 |
void accept_vis (ASTVisitor &vis) override; |
| 429 |
|
| 430 |
// TODO: is this better? Or is a "vis_block" better? |
| 431 |
Expr &get_dereferenced_expr () |
| 432 |
{ |
| 433 |
rust_assert (main_or_left_expr != nullptr); |
| 434 |
return *main_or_left_expr; |
| 435 |
} |
| 436 |
|
| 437 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Dereference; } |
| 438 |
|
| 439 |
protected: |
| 440 |
/* Use covariance to implement clone function as returning this object rather |
| 441 |
* than base */ |
| 442 |
DereferenceExpr *clone_expr_without_block_impl () const override |
| 443 |
{ |
| 444 |
return new DereferenceExpr (*this); |
| 445 |
} |
| 446 |
}; |
| 447 |
|
| 448 |
// Unary postfix ? error propogation operator. Cannot be overloaded. |
| 449 |
class ErrorPropagationExpr : public OperatorExpr |
| 450 |
{ |
| 451 |
public: |
| 452 |
std::string as_string () const override; |
| 453 |
|
| 454 |
// Constructor calls OperatorExpr's protected constructor |
| 455 |
ErrorPropagationExpr (std::unique_ptr<Expr> potential_error_value, |
| 456 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 457 |
: OperatorExpr (std::move (potential_error_value), |
| 458 |
std::move (outer_attribs), locus) |
| 459 |
{} |
| 460 |
|
| 461 |
void accept_vis (ASTVisitor &vis) override; |
| 462 |
|
| 463 |
// TODO: is this better? Or is a "vis_block" better? |
| 464 |
Expr &get_propagating_expr () |
| 465 |
{ |
| 466 |
rust_assert (main_or_left_expr != nullptr); |
| 467 |
return *main_or_left_expr; |
| 468 |
} |
| 469 |
|
| 470 |
Expr::Kind get_expr_kind () const override |
| 471 |
{ |
| 472 |
return Expr::Kind::ErrorPropagation; |
| 473 |
} |
| 474 |
|
| 475 |
protected: |
| 476 |
/* Use covariance to implement clone function as returning this object rather |
| 477 |
* than base */ |
| 478 |
ErrorPropagationExpr *clone_expr_without_block_impl () const override |
| 479 |
{ |
| 480 |
return new ErrorPropagationExpr (*this); |
| 481 |
} |
| 482 |
}; |
| 483 |
|
| 484 |
// Unary prefix - or ! negation or NOT operators. |
| 485 |
class NegationExpr : public OperatorExpr |
| 486 |
{ |
| 487 |
public: |
| 488 |
using ExprType = NegationOperator; |
| 489 |
|
| 490 |
private: |
| 491 |
/* Note: overload negation via std::ops::Neg and not via std::ops::Not |
| 492 |
* Negation only works for signed integer and floating-point types, NOT only |
| 493 |
* works for boolean and integer types (via bitwise NOT) */ |
| 494 |
ExprType expr_type; |
| 495 |
|
| 496 |
public: |
| 497 |
std::string as_string () const override; |
| 498 |
|
| 499 |
ExprType get_expr_type () const { return expr_type; } |
| 500 |
|
| 501 |
// Constructor calls OperatorExpr's protected constructor |
| 502 |
NegationExpr (std::unique_ptr<Expr> negated_value, ExprType expr_kind, |
| 503 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 504 |
: OperatorExpr (std::move (negated_value), std::move (outer_attribs), |
| 505 |
locus), |
| 506 |
expr_type (expr_kind) |
| 507 |
{} |
| 508 |
|
| 509 |
void accept_vis (ASTVisitor &vis) override; |
| 510 |
|
| 511 |
// TODO: is this better? Or is a "vis_block" better? |
| 512 |
Expr &get_negated_expr () |
| 513 |
{ |
| 514 |
rust_assert (main_or_left_expr != nullptr); |
| 515 |
return *main_or_left_expr; |
| 516 |
} |
| 517 |
|
| 518 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Negation; } |
| 519 |
|
| 520 |
protected: |
| 521 |
/* Use covariance to implement clone function as returning this object rather |
| 522 |
* than base */ |
| 523 |
NegationExpr *clone_expr_without_block_impl () const override |
| 524 |
{ |
| 525 |
return new NegationExpr (*this); |
| 526 |
} |
| 527 |
}; |
| 528 |
|
| 529 |
// Infix binary operators. +, -, *, /, %, &, |, ^, <<, >> |
| 530 |
class ArithmeticOrLogicalExpr : public OperatorExpr |
| 531 |
{ |
| 532 |
public: |
| 533 |
using ExprType = ArithmeticOrLogicalOperator; |
| 534 |
|
| 535 |
private: |
| 536 |
// Note: overloading trait specified in comments |
| 537 |
ExprType expr_type; |
| 538 |
|
| 539 |
std::unique_ptr<Expr> right_expr; |
| 540 |
|
| 541 |
public: |
| 542 |
std::string as_string () const override; |
| 543 |
|
| 544 |
ExprType get_expr_type () const { return expr_type; } |
| 545 |
|
| 546 |
// Constructor calls OperatorExpr's protected constructor |
| 547 |
ArithmeticOrLogicalExpr (std::unique_ptr<Expr> left_value, |
| 548 |
std::unique_ptr<Expr> right_value, |
| 549 |
ExprType expr_kind, location_t locus) |
| 550 |
: OperatorExpr (std::move (left_value), std::vector<Attribute> (), locus), |
| 551 |
expr_type (expr_kind), right_expr (std::move (right_value)) |
| 552 |
{} |
| 553 |
// outer attributes not allowed |
| 554 |
|
| 555 |
// Copy constructor - probably required due to unique pointer |
| 556 |
ArithmeticOrLogicalExpr (ArithmeticOrLogicalExpr const &other) |
| 557 |
: OperatorExpr (other), expr_type (other.expr_type), |
| 558 |
right_expr (other.right_expr->clone_expr ()) |
| 559 |
{} |
| 560 |
|
| 561 |
// Overload assignment operator |
| 562 |
ArithmeticOrLogicalExpr &operator= (ArithmeticOrLogicalExpr const &other) |
| 563 |
{ |
| 564 |
OperatorExpr::operator= (other); |
| 565 |
// main_or_left_expr = other.main_or_left_expr->clone_expr(); |
| 566 |
right_expr = other.right_expr->clone_expr (); |
| 567 |
expr_type = other.expr_type; |
| 568 |
|
| 569 |
return *this; |
| 570 |
} |
| 571 |
|
| 572 |
// move constructors |
| 573 |
ArithmeticOrLogicalExpr (ArithmeticOrLogicalExpr &&other) = default; |
| 574 |
ArithmeticOrLogicalExpr &operator= (ArithmeticOrLogicalExpr &&other) |
| 575 |
= default; |
| 576 |
|
| 577 |
void accept_vis (ASTVisitor &vis) override; |
| 578 |
|
| 579 |
// TODO: is this better? Or is a "vis_block" better? |
| 580 |
Expr &get_left_expr () |
| 581 |
{ |
| 582 |
rust_assert (main_or_left_expr != nullptr); |
| 583 |
return *main_or_left_expr; |
| 584 |
} |
| 585 |
|
| 586 |
std::unique_ptr<Expr> &get_left_expr_ptr () |
| 587 |
{ |
| 588 |
rust_assert (main_or_left_expr != nullptr); |
| 589 |
return main_or_left_expr; |
| 590 |
} |
| 591 |
|
| 592 |
// TODO: is this better? Or is a "vis_block" better? |
| 593 |
Expr &get_right_expr () |
| 594 |
{ |
| 595 |
rust_assert (right_expr != nullptr); |
| 596 |
return *right_expr; |
| 597 |
} |
| 598 |
|
| 599 |
std::unique_ptr<Expr> &get_right_expr_ptr () |
| 600 |
{ |
| 601 |
rust_assert (right_expr != nullptr); |
| 602 |
return right_expr; |
| 603 |
} |
| 604 |
|
| 605 |
void visit_lhs (ASTVisitor &vis) { main_or_left_expr->accept_vis (vis); } |
| 606 |
void visit_rhs (ASTVisitor &vis) { right_expr->accept_vis (vis); } |
| 607 |
|
| 608 |
Expr::Kind get_expr_kind () const override |
| 609 |
{ |
| 610 |
return Expr::Kind::ArithmeticOrLogical; |
| 611 |
} |
| 612 |
|
| 613 |
protected: |
| 614 |
/* Use covariance to implement clone function as returning this object rather |
| 615 |
* than base */ |
| 616 |
ArithmeticOrLogicalExpr *clone_expr_without_block_impl () const override |
| 617 |
{ |
| 618 |
return new ArithmeticOrLogicalExpr (*this); |
| 619 |
} |
| 620 |
}; |
| 621 |
|
| 622 |
// Infix binary comparison operators. ==, !=, <, <=, >, >= |
| 623 |
class ComparisonExpr : public OperatorExpr |
| 624 |
{ |
| 625 |
public: |
| 626 |
using ExprType = ComparisonOperator; |
| 627 |
|
| 628 |
private: |
| 629 |
// Note: overloading trait specified in comments |
| 630 |
ExprType expr_type; |
| 631 |
|
| 632 |
std::unique_ptr<Expr> right_expr; |
| 633 |
|
| 634 |
public: |
| 635 |
std::string as_string () const override; |
| 636 |
|
| 637 |
ExprType get_expr_type () const { return expr_type; } |
| 638 |
|
| 639 |
// Constructor requires pointers for polymorphism |
| 640 |
ComparisonExpr (std::unique_ptr<Expr> left_value, |
| 641 |
std::unique_ptr<Expr> right_value, ExprType comparison_kind, |
| 642 |
location_t locus) |
| 643 |
: OperatorExpr (std::move (left_value), std::vector<Attribute> (), locus), |
| 644 |
expr_type (comparison_kind), right_expr (std::move (right_value)) |
| 645 |
{} |
| 646 |
// outer attributes not allowed |
| 647 |
|
| 648 |
// Copy constructor also calls OperatorExpr's protected constructor |
| 649 |
ComparisonExpr (ComparisonExpr const &other) |
| 650 |
: OperatorExpr (other), expr_type (other.expr_type), |
| 651 |
right_expr (other.right_expr->clone_expr ()) |
| 652 |
{} |
| 653 |
|
| 654 |
// Overload assignment operator to deep copy |
| 655 |
ComparisonExpr &operator= (ComparisonExpr const &other) |
| 656 |
{ |
| 657 |
OperatorExpr::operator= (other); |
| 658 |
// main_or_left_expr = other.main_or_left_expr->clone_expr(); |
| 659 |
right_expr = other.right_expr->clone_expr (); |
| 660 |
expr_type = other.expr_type; |
| 661 |
// outer_attrs = other.outer_attrs; |
| 662 |
|
| 663 |
return *this; |
| 664 |
} |
| 665 |
|
| 666 |
// move constructors |
| 667 |
ComparisonExpr (ComparisonExpr &&other) = default; |
| 668 |
ComparisonExpr &operator= (ComparisonExpr &&other) = default; |
| 669 |
|
| 670 |
void accept_vis (ASTVisitor &vis) override; |
| 671 |
|
| 672 |
// TODO: is this better? Or is a "vis_block" better? |
| 673 |
Expr &get_left_expr () |
| 674 |
{ |
| 675 |
rust_assert (main_or_left_expr != nullptr); |
| 676 |
return *main_or_left_expr; |
| 677 |
} |
| 678 |
|
| 679 |
std::unique_ptr<Expr> &get_left_expr_ptr () |
| 680 |
{ |
| 681 |
rust_assert (main_or_left_expr != nullptr); |
| 682 |
return main_or_left_expr; |
| 683 |
} |
| 684 |
|
| 685 |
// TODO: is this better? Or is a "vis_block" better? |
| 686 |
Expr &get_right_expr () |
| 687 |
{ |
| 688 |
rust_assert (right_expr != nullptr); |
| 689 |
return *right_expr; |
| 690 |
} |
| 691 |
|
| 692 |
std::unique_ptr<Expr> &get_right_expr_ptr () |
| 693 |
{ |
| 694 |
rust_assert (right_expr != nullptr); |
| 695 |
return right_expr; |
| 696 |
} |
| 697 |
|
| 698 |
ExprType get_kind () { return expr_type; } |
| 699 |
|
| 700 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Comparison; } |
| 701 |
|
| 702 |
/* TODO: implement via a function call to std::cmp::PartialEq::eq(&op1, &op2) |
| 703 |
* maybe? */ |
| 704 |
protected: |
| 705 |
/* Use covariance to implement clone function as returning this object rather |
| 706 |
* than base */ |
| 707 |
ComparisonExpr *clone_expr_without_block_impl () const override |
| 708 |
{ |
| 709 |
return new ComparisonExpr (*this); |
| 710 |
} |
| 711 |
}; |
| 712 |
|
| 713 |
// Infix binary lazy boolean logical operators && and ||. |
| 714 |
class LazyBooleanExpr : public OperatorExpr |
| 715 |
{ |
| 716 |
public: |
| 717 |
using ExprType = LazyBooleanOperator; |
| 718 |
|
| 719 |
private: |
| 720 |
ExprType expr_type; |
| 721 |
|
| 722 |
std::unique_ptr<Expr> right_expr; |
| 723 |
|
| 724 |
public: |
| 725 |
// Constructor calls OperatorExpr's protected constructor |
| 726 |
LazyBooleanExpr (std::unique_ptr<Expr> left_bool_expr, |
| 727 |
std::unique_ptr<Expr> right_bool_expr, ExprType expr_kind, |
| 728 |
location_t locus) |
| 729 |
: OperatorExpr (std::move (left_bool_expr), std::vector<Attribute> (), |
| 730 |
locus), |
| 731 |
expr_type (expr_kind), right_expr (std::move (right_bool_expr)) |
| 732 |
{} |
| 733 |
// outer attributes not allowed |
| 734 |
|
| 735 |
// Copy constructor also calls OperatorExpr's protected constructor |
| 736 |
LazyBooleanExpr (LazyBooleanExpr const &other) |
| 737 |
: OperatorExpr (other), expr_type (other.expr_type), |
| 738 |
right_expr (other.right_expr->clone_expr ()) |
| 739 |
{} |
| 740 |
|
| 741 |
// Overload assignment operator to deep copy |
| 742 |
LazyBooleanExpr &operator= (LazyBooleanExpr const &other) |
| 743 |
{ |
| 744 |
OperatorExpr::operator= (other); |
| 745 |
// main_or_left_expr = other.main_or_left_expr->clone_expr(); |
| 746 |
right_expr = other.right_expr->clone_expr (); |
| 747 |
expr_type = other.expr_type; |
| 748 |
|
| 749 |
return *this; |
| 750 |
} |
| 751 |
|
| 752 |
// move constructors |
| 753 |
LazyBooleanExpr (LazyBooleanExpr &&other) = default; |
| 754 |
LazyBooleanExpr &operator= (LazyBooleanExpr &&other) = default; |
| 755 |
|
| 756 |
std::string as_string () const override; |
| 757 |
|
| 758 |
ExprType get_expr_type () const { return expr_type; } |
| 759 |
|
| 760 |
void accept_vis (ASTVisitor &vis) override; |
| 761 |
|
| 762 |
// TODO: is this better? Or is a "vis_block" better? |
| 763 |
Expr &get_left_expr () |
| 764 |
{ |
| 765 |
rust_assert (main_or_left_expr != nullptr); |
| 766 |
return *main_or_left_expr; |
| 767 |
} |
| 768 |
|
| 769 |
std::unique_ptr<Expr> &get_left_expr_ptr () |
| 770 |
{ |
| 771 |
rust_assert (main_or_left_expr != nullptr); |
| 772 |
return main_or_left_expr; |
| 773 |
} |
| 774 |
|
| 775 |
// TODO: is this better? Or is a "vis_block" better? |
| 776 |
Expr &get_right_expr () |
| 777 |
{ |
| 778 |
rust_assert (right_expr != nullptr); |
| 779 |
return *right_expr; |
| 780 |
} |
| 781 |
|
| 782 |
std::unique_ptr<Expr> &get_right_expr_ptr () |
| 783 |
{ |
| 784 |
rust_assert (right_expr != nullptr); |
| 785 |
return right_expr; |
| 786 |
} |
| 787 |
|
| 788 |
ExprType get_kind () { return expr_type; } |
| 789 |
|
| 790 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::LazyBoolean; } |
| 791 |
|
| 792 |
protected: |
| 793 |
/* Use covariance to implement clone function as returning this object rather |
| 794 |
* than base */ |
| 795 |
LazyBooleanExpr *clone_expr_without_block_impl () const override |
| 796 |
{ |
| 797 |
return new LazyBooleanExpr (*this); |
| 798 |
} |
| 799 |
}; |
| 800 |
|
| 801 |
// Binary infix "as" cast expression. |
| 802 |
class TypeCastExpr : public OperatorExpr |
| 803 |
{ |
| 804 |
std::unique_ptr<TypeNoBounds> type_to_convert_to; |
| 805 |
|
| 806 |
// Note: only certain type casts allowed, outlined in reference |
| 807 |
public: |
| 808 |
std::string as_string () const override; |
| 809 |
|
| 810 |
// Constructor requires calling protected constructor of OperatorExpr |
| 811 |
TypeCastExpr (std::unique_ptr<Expr> expr_to_cast, |
| 812 |
std::unique_ptr<TypeNoBounds> type_to_cast_to, location_t locus) |
| 813 |
: OperatorExpr (std::move (expr_to_cast), std::vector<Attribute> (), locus), |
| 814 |
type_to_convert_to (std::move (type_to_cast_to)) |
| 815 |
{} |
| 816 |
// outer attributes not allowed |
| 817 |
|
| 818 |
// Copy constructor also requires calling protected constructor |
| 819 |
TypeCastExpr (TypeCastExpr const &other) |
| 820 |
: OperatorExpr (other), |
| 821 |
type_to_convert_to (other.type_to_convert_to->clone_type_no_bounds ()) |
| 822 |
{} |
| 823 |
|
| 824 |
// Overload assignment operator to deep copy |
| 825 |
TypeCastExpr &operator= (TypeCastExpr const &other) |
| 826 |
{ |
| 827 |
OperatorExpr::operator= (other); |
| 828 |
// main_or_left_expr = other.main_or_left_expr->clone_expr(); |
| 829 |
type_to_convert_to = other.type_to_convert_to->clone_type_no_bounds (); |
| 830 |
|
| 831 |
return *this; |
| 832 |
} |
| 833 |
|
| 834 |
// move constructors |
| 835 |
TypeCastExpr (TypeCastExpr &&other) = default; |
| 836 |
TypeCastExpr &operator= (TypeCastExpr &&other) = default; |
| 837 |
|
| 838 |
void accept_vis (ASTVisitor &vis) override; |
| 839 |
|
| 840 |
// TODO: is this better? Or is a "vis_block" better? |
| 841 |
Expr &get_casted_expr () |
| 842 |
{ |
| 843 |
rust_assert (main_or_left_expr != nullptr); |
| 844 |
return *main_or_left_expr; |
| 845 |
} |
| 846 |
|
| 847 |
// TODO: is this better? Or is a "vis_block" better? |
| 848 |
TypeNoBounds &get_type_to_cast_to () |
| 849 |
{ |
| 850 |
rust_assert (type_to_convert_to != nullptr); |
| 851 |
return *type_to_convert_to; |
| 852 |
} |
| 853 |
|
| 854 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::TypeCast; } |
| 855 |
|
| 856 |
protected: |
| 857 |
/* Use covariance to implement clone function as returning this object rather |
| 858 |
* than base */ |
| 859 |
TypeCastExpr *clone_expr_without_block_impl () const override |
| 860 |
{ |
| 861 |
return new TypeCastExpr (*this); |
| 862 |
} |
| 863 |
}; |
| 864 |
|
| 865 |
// Binary assignment expression. |
| 866 |
class AssignmentExpr : public OperatorExpr |
| 867 |
{ |
| 868 |
std::unique_ptr<Expr> right_expr; |
| 869 |
|
| 870 |
public: |
| 871 |
std::string as_string () const override; |
| 872 |
|
| 873 |
// Call OperatorExpr constructor to initialise left_expr |
| 874 |
AssignmentExpr (std::unique_ptr<Expr> value_to_assign_to, |
| 875 |
std::unique_ptr<Expr> value_to_assign, |
| 876 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 877 |
: OperatorExpr (std::move (value_to_assign_to), std::move (outer_attribs), |
| 878 |
locus), |
| 879 |
right_expr (std::move (value_to_assign)) |
| 880 |
{} |
| 881 |
// outer attributes not allowed |
| 882 |
|
| 883 |
// Call OperatorExpr constructor in copy constructor, as well as clone |
| 884 |
AssignmentExpr (AssignmentExpr const &other) |
| 885 |
: OperatorExpr (other), right_expr (other.right_expr->clone_expr ()) |
| 886 |
{} |
| 887 |
|
| 888 |
// Overload assignment operator to clone unique_ptr right_expr |
| 889 |
AssignmentExpr &operator= (AssignmentExpr const &other) |
| 890 |
{ |
| 891 |
OperatorExpr::operator= (other); |
| 892 |
// main_or_left_expr = other.main_or_left_expr->clone_expr(); |
| 893 |
right_expr = other.right_expr->clone_expr (); |
| 894 |
// outer_attrs = other.outer_attrs; |
| 895 |
|
| 896 |
return *this; |
| 897 |
} |
| 898 |
|
| 899 |
// move constructors |
| 900 |
AssignmentExpr (AssignmentExpr &&other) = default; |
| 901 |
AssignmentExpr &operator= (AssignmentExpr &&other) = default; |
| 902 |
|
| 903 |
void accept_vis (ASTVisitor &vis) override; |
| 904 |
|
| 905 |
void visit_lhs (ASTVisitor &vis) { main_or_left_expr->accept_vis (vis); } |
| 906 |
void visit_rhs (ASTVisitor &vis) { right_expr->accept_vis (vis); } |
| 907 |
|
| 908 |
// TODO: is this better? Or is a "vis_block" better? |
| 909 |
Expr &get_left_expr () |
| 910 |
{ |
| 911 |
rust_assert (main_or_left_expr != nullptr); |
| 912 |
return *main_or_left_expr; |
| 913 |
} |
| 914 |
|
| 915 |
std::unique_ptr<Expr> &get_left_expr_ptr () |
| 916 |
{ |
| 917 |
rust_assert (main_or_left_expr != nullptr); |
| 918 |
return main_or_left_expr; |
| 919 |
} |
| 920 |
|
| 921 |
std::unique_ptr<Expr> &get_right_expr_ptr () |
| 922 |
{ |
| 923 |
rust_assert (right_expr != nullptr); |
| 924 |
return right_expr; |
| 925 |
} |
| 926 |
|
| 927 |
// TODO: is this better? Or is a "vis_block" better? |
| 928 |
Expr &get_right_expr () |
| 929 |
{ |
| 930 |
rust_assert (right_expr != nullptr); |
| 931 |
return *right_expr; |
| 932 |
} |
| 933 |
|
| 934 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Assignment; } |
| 935 |
|
| 936 |
protected: |
| 937 |
/* Use covariance to implement clone function as returning this object rather |
| 938 |
* than base */ |
| 939 |
AssignmentExpr *clone_expr_without_block_impl () const override |
| 940 |
{ |
| 941 |
return new AssignmentExpr (*this); |
| 942 |
} |
| 943 |
}; |
| 944 |
|
| 945 |
/* Binary infix compound assignment (arithmetic or logic then assignment) |
| 946 |
* expressions. */ |
| 947 |
class CompoundAssignmentExpr : public OperatorExpr |
| 948 |
{ |
| 949 |
public: |
| 950 |
using ExprType = CompoundAssignmentOperator; |
| 951 |
|
| 952 |
private: |
| 953 |
// Note: overloading trait specified in comments |
| 954 |
ExprType expr_type; |
| 955 |
std::unique_ptr<Expr> right_expr; |
| 956 |
|
| 957 |
public: |
| 958 |
std::string as_string () const override; |
| 959 |
|
| 960 |
ExprType get_expr_type () const { return expr_type; } |
| 961 |
|
| 962 |
// Use pointers in constructor to enable polymorphism |
| 963 |
CompoundAssignmentExpr (std::unique_ptr<Expr> value_to_assign_to, |
| 964 |
std::unique_ptr<Expr> value_to_assign, |
| 965 |
ExprType expr_kind, location_t locus) |
| 966 |
: OperatorExpr (std::move (value_to_assign_to), std::vector<Attribute> (), |
| 967 |
locus), |
| 968 |
expr_type (expr_kind), right_expr (std::move (value_to_assign)) |
| 969 |
{} |
| 970 |
// outer attributes not allowed |
| 971 |
|
| 972 |
// Have clone in copy constructor |
| 973 |
CompoundAssignmentExpr (CompoundAssignmentExpr const &other) |
| 974 |
: OperatorExpr (other), expr_type (other.expr_type), |
| 975 |
right_expr (other.right_expr->clone_expr ()) |
| 976 |
{} |
| 977 |
|
| 978 |
// Overload assignment operator to clone |
| 979 |
CompoundAssignmentExpr &operator= (CompoundAssignmentExpr const &other) |
| 980 |
{ |
| 981 |
OperatorExpr::operator= (other); |
| 982 |
// main_or_left_expr = other.main_or_left_expr->clone_expr(); |
| 983 |
right_expr = other.right_expr->clone_expr (); |
| 984 |
expr_type = other.expr_type; |
| 985 |
// outer_attrs = other.outer_attrs; |
| 986 |
|
| 987 |
return *this; |
| 988 |
} |
| 989 |
|
| 990 |
// move constructors |
| 991 |
CompoundAssignmentExpr (CompoundAssignmentExpr &&other) = default; |
| 992 |
CompoundAssignmentExpr &operator= (CompoundAssignmentExpr &&other) = default; |
| 993 |
|
| 994 |
void accept_vis (ASTVisitor &vis) override; |
| 995 |
|
| 996 |
// TODO: is this better? Or is a "vis_block" better? |
| 997 |
Expr &get_left_expr () |
| 998 |
{ |
| 999 |
rust_assert (main_or_left_expr != nullptr); |
| 1000 |
return *main_or_left_expr; |
| 1001 |
} |
| 1002 |
|
| 1003 |
std::unique_ptr<Expr> &get_left_expr_ptr () |
| 1004 |
{ |
| 1005 |
rust_assert (main_or_left_expr != nullptr); |
| 1006 |
return main_or_left_expr; |
| 1007 |
} |
| 1008 |
|
| 1009 |
// TODO: is this better? Or is a "vis_block" better? |
| 1010 |
Expr &get_right_expr () |
| 1011 |
{ |
| 1012 |
rust_assert (right_expr != nullptr); |
| 1013 |
return *right_expr; |
| 1014 |
} |
| 1015 |
|
| 1016 |
std::unique_ptr<Expr> &get_right_expr_ptr () |
| 1017 |
{ |
| 1018 |
rust_assert (right_expr != nullptr); |
| 1019 |
return right_expr; |
| 1020 |
} |
| 1021 |
|
| 1022 |
Expr::Kind get_expr_kind () const override |
| 1023 |
{ |
| 1024 |
return Expr::Kind::CompoundAssignment; |
| 1025 |
} |
| 1026 |
|
| 1027 |
protected: |
| 1028 |
/* Use covariance to implement clone function as returning this object rather |
| 1029 |
* than base */ |
| 1030 |
CompoundAssignmentExpr *clone_expr_without_block_impl () const override |
| 1031 |
{ |
| 1032 |
return new CompoundAssignmentExpr (*this); |
| 1033 |
} |
| 1034 |
}; |
| 1035 |
|
| 1036 |
// Expression in parentheses (i.e. like literally just any 3 + (2 * 6)) |
| 1037 |
class GroupedExpr : public ExprWithoutBlock |
| 1038 |
{ |
| 1039 |
std::vector<Attribute> outer_attrs; |
| 1040 |
std::vector<Attribute> inner_attrs; |
| 1041 |
std::unique_ptr<Expr> expr_in_parens; |
| 1042 |
location_t locus; |
| 1043 |
|
| 1044 |
public: |
| 1045 |
std::string as_string () const override; |
| 1046 |
|
| 1047 |
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; } |
| 1048 |
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; } |
| 1049 |
|
| 1050 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 1051 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 1052 |
|
| 1053 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 1054 |
{ |
| 1055 |
outer_attrs = std::move (new_attrs); |
| 1056 |
} |
| 1057 |
|
| 1058 |
GroupedExpr (std::unique_ptr<Expr> parenthesised_expr, |
| 1059 |
std::vector<Attribute> inner_attribs, |
| 1060 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 1061 |
: outer_attrs (std::move (outer_attribs)), |
| 1062 |
inner_attrs (std::move (inner_attribs)), |
| 1063 |
expr_in_parens (std::move (parenthesised_expr)), locus (locus) |
| 1064 |
{} |
| 1065 |
|
| 1066 |
// Copy constructor includes clone for expr_in_parens |
| 1067 |
GroupedExpr (GroupedExpr const &other) |
| 1068 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 1069 |
inner_attrs (other.inner_attrs), locus (other.locus) |
| 1070 |
{ |
| 1071 |
// guard to prevent null dereference (only required if error state) |
| 1072 |
if (other.expr_in_parens != nullptr) |
| 1073 |
expr_in_parens = other.expr_in_parens->clone_expr (); |
| 1074 |
} |
| 1075 |
|
| 1076 |
// Overloaded assignment operator to clone expr_in_parens |
| 1077 |
GroupedExpr &operator= (GroupedExpr const &other) |
| 1078 |
{ |
| 1079 |
ExprWithoutBlock::operator= (other); |
| 1080 |
inner_attrs = other.inner_attrs; |
| 1081 |
locus = other.locus; |
| 1082 |
outer_attrs = other.outer_attrs; |
| 1083 |
|
| 1084 |
// guard to prevent null dereference (only required if error state) |
| 1085 |
if (other.expr_in_parens != nullptr) |
| 1086 |
expr_in_parens = other.expr_in_parens->clone_expr (); |
| 1087 |
else |
| 1088 |
expr_in_parens = nullptr; |
| 1089 |
|
| 1090 |
return *this; |
| 1091 |
} |
| 1092 |
|
| 1093 |
// move constructors |
| 1094 |
GroupedExpr (GroupedExpr &&other) = default; |
| 1095 |
GroupedExpr &operator= (GroupedExpr &&other) = default; |
| 1096 |
|
| 1097 |
location_t get_locus () const override final { return locus; } |
| 1098 |
|
| 1099 |
void accept_vis (ASTVisitor &vis) override; |
| 1100 |
|
| 1101 |
// Invalid if inner expr is null, so base stripping on that. |
| 1102 |
void mark_for_strip () override { expr_in_parens = nullptr; } |
| 1103 |
bool is_marked_for_strip () const override |
| 1104 |
{ |
| 1105 |
return expr_in_parens == nullptr; |
| 1106 |
} |
| 1107 |
|
| 1108 |
// TODO: is this better? Or is a "vis_block" better? |
| 1109 |
Expr &get_expr_in_parens () |
| 1110 |
{ |
| 1111 |
rust_assert (expr_in_parens != nullptr); |
| 1112 |
return *expr_in_parens; |
| 1113 |
} |
| 1114 |
|
| 1115 |
std::unique_ptr<Expr> &get_expr_in_parens_ptr () |
| 1116 |
{ |
| 1117 |
rust_assert (expr_in_parens != nullptr); |
| 1118 |
return expr_in_parens; |
| 1119 |
} |
| 1120 |
|
| 1121 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Grouped; } |
| 1122 |
|
| 1123 |
protected: |
| 1124 |
/* Use covariance to implement clone function as returning this object rather |
| 1125 |
* than base */ |
| 1126 |
GroupedExpr *clone_expr_without_block_impl () const override |
| 1127 |
{ |
| 1128 |
return new GroupedExpr (*this); |
| 1129 |
} |
| 1130 |
}; |
| 1131 |
|
| 1132 |
// Base array initialisation internal element representation thing (abstract) |
| 1133 |
// aka ArrayElements |
| 1134 |
class ArrayElems |
| 1135 |
{ |
| 1136 |
public: |
| 1137 |
virtual ~ArrayElems () {} |
| 1138 |
|
| 1139 |
// Unique pointer custom clone ArrayElems function |
| 1140 |
std::unique_ptr<ArrayElems> clone_array_elems () const |
| 1141 |
{ |
| 1142 |
return std::unique_ptr<ArrayElems> (clone_array_elems_impl ()); |
| 1143 |
} |
| 1144 |
|
| 1145 |
virtual std::string as_string () const = 0; |
| 1146 |
|
| 1147 |
virtual void accept_vis (ASTVisitor &vis) = 0; |
| 1148 |
|
| 1149 |
NodeId get_node_id () const { return node_id; } |
| 1150 |
|
| 1151 |
protected: |
| 1152 |
ArrayElems () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} |
| 1153 |
|
| 1154 |
// pure virtual clone implementation |
| 1155 |
virtual ArrayElems *clone_array_elems_impl () const = 0; |
| 1156 |
|
| 1157 |
NodeId node_id; |
| 1158 |
}; |
| 1159 |
|
| 1160 |
// Value array elements |
| 1161 |
class ArrayElemsValues : public ArrayElems |
| 1162 |
{ |
| 1163 |
std::vector<std::unique_ptr<Expr> > values; |
| 1164 |
location_t locus; |
| 1165 |
|
| 1166 |
public: |
| 1167 |
ArrayElemsValues (std::vector<std::unique_ptr<Expr> > elems, location_t locus) |
| 1168 |
: ArrayElems (), values (std::move (elems)), locus (locus) |
| 1169 |
{} |
| 1170 |
|
| 1171 |
// copy constructor with vector clone |
| 1172 |
ArrayElemsValues (ArrayElemsValues const &other) |
| 1173 |
{ |
| 1174 |
values.reserve (other.values.size ()); |
| 1175 |
for (const auto &e : other.values) |
| 1176 |
values.push_back (e->clone_expr ()); |
| 1177 |
} |
| 1178 |
|
| 1179 |
// overloaded assignment operator with vector clone |
| 1180 |
ArrayElemsValues &operator= (ArrayElemsValues const &other) |
| 1181 |
{ |
| 1182 |
values.reserve (other.values.size ()); |
| 1183 |
for (const auto &e : other.values) |
| 1184 |
values.push_back (e->clone_expr ()); |
| 1185 |
|
| 1186 |
return *this; |
| 1187 |
} |
| 1188 |
|
| 1189 |
// move constructors |
| 1190 |
ArrayElemsValues (ArrayElemsValues &&other) = default; |
| 1191 |
ArrayElemsValues &operator= (ArrayElemsValues &&other) = default; |
| 1192 |
|
| 1193 |
std::string as_string () const override; |
| 1194 |
|
| 1195 |
void accept_vis (ASTVisitor &vis) override; |
| 1196 |
|
| 1197 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 1198 |
const std::vector<std::unique_ptr<Expr> > &get_values () const |
| 1199 |
{ |
| 1200 |
return values; |
| 1201 |
} |
| 1202 |
std::vector<std::unique_ptr<Expr> > &get_values () { return values; } |
| 1203 |
|
| 1204 |
size_t get_num_values () const { return values.size (); } |
| 1205 |
|
| 1206 |
protected: |
| 1207 |
ArrayElemsValues *clone_array_elems_impl () const override |
| 1208 |
{ |
| 1209 |
return new ArrayElemsValues (*this); |
| 1210 |
} |
| 1211 |
}; |
| 1212 |
|
| 1213 |
// Copied array element and number of copies |
| 1214 |
class ArrayElemsCopied : public ArrayElems |
| 1215 |
{ |
| 1216 |
std::unique_ptr<Expr> elem_to_copy; |
| 1217 |
std::unique_ptr<Expr> num_copies; |
| 1218 |
location_t locus; |
| 1219 |
|
| 1220 |
public: |
| 1221 |
// Constructor requires pointers for polymorphism |
| 1222 |
ArrayElemsCopied (std::unique_ptr<Expr> copied_elem, |
| 1223 |
std::unique_ptr<Expr> copy_amount, location_t locus) |
| 1224 |
: ArrayElems (), elem_to_copy (std::move (copied_elem)), |
| 1225 |
num_copies (std::move (copy_amount)), locus (locus) |
| 1226 |
{} |
| 1227 |
|
| 1228 |
// Copy constructor required due to unique_ptr - uses custom clone |
| 1229 |
ArrayElemsCopied (ArrayElemsCopied const &other) |
| 1230 |
: elem_to_copy (other.elem_to_copy->clone_expr ()), |
| 1231 |
num_copies (other.num_copies->clone_expr ()) |
| 1232 |
{} |
| 1233 |
|
| 1234 |
// Overloaded assignment operator for deep copying |
| 1235 |
ArrayElemsCopied &operator= (ArrayElemsCopied const &other) |
| 1236 |
{ |
| 1237 |
elem_to_copy = other.elem_to_copy->clone_expr (); |
| 1238 |
num_copies = other.num_copies->clone_expr (); |
| 1239 |
|
| 1240 |
return *this; |
| 1241 |
} |
| 1242 |
|
| 1243 |
// move constructors |
| 1244 |
ArrayElemsCopied (ArrayElemsCopied &&other) = default; |
| 1245 |
ArrayElemsCopied &operator= (ArrayElemsCopied &&other) = default; |
| 1246 |
|
| 1247 |
std::string as_string () const override; |
| 1248 |
|
| 1249 |
void accept_vis (ASTVisitor &vis) override; |
| 1250 |
|
| 1251 |
// TODO: is this better? Or is a "vis_block" better? |
| 1252 |
Expr &get_elem_to_copy () |
| 1253 |
{ |
| 1254 |
rust_assert (elem_to_copy != nullptr); |
| 1255 |
return *elem_to_copy; |
| 1256 |
} |
| 1257 |
|
| 1258 |
// TODO: is this better? Or is a "vis_block" better? |
| 1259 |
Expr &get_num_copies () |
| 1260 |
{ |
| 1261 |
rust_assert (num_copies != nullptr); |
| 1262 |
return *num_copies; |
| 1263 |
} |
| 1264 |
|
| 1265 |
protected: |
| 1266 |
ArrayElemsCopied *clone_array_elems_impl () const override |
| 1267 |
{ |
| 1268 |
return new ArrayElemsCopied (*this); |
| 1269 |
} |
| 1270 |
}; |
| 1271 |
|
| 1272 |
// Array definition-ish expression |
| 1273 |
class ArrayExpr : public ExprWithoutBlock |
| 1274 |
{ |
| 1275 |
std::vector<Attribute> outer_attrs; |
| 1276 |
std::vector<Attribute> inner_attrs; |
| 1277 |
std::unique_ptr<ArrayElems> internal_elements; |
| 1278 |
location_t locus; |
| 1279 |
|
| 1280 |
// TODO: find another way to store this to save memory? |
| 1281 |
bool marked_for_strip = false; |
| 1282 |
|
| 1283 |
public: |
| 1284 |
std::string as_string () const override; |
| 1285 |
|
| 1286 |
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; } |
| 1287 |
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; } |
| 1288 |
|
| 1289 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 1290 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 1291 |
|
| 1292 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 1293 |
{ |
| 1294 |
outer_attrs = std::move (new_attrs); |
| 1295 |
} |
| 1296 |
|
| 1297 |
// Constructor requires ArrayElems pointer |
| 1298 |
ArrayExpr (std::unique_ptr<ArrayElems> array_elems, |
| 1299 |
std::vector<Attribute> inner_attribs, |
| 1300 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 1301 |
: outer_attrs (std::move (outer_attribs)), |
| 1302 |
inner_attrs (std::move (inner_attribs)), |
| 1303 |
internal_elements (std::move (array_elems)), locus (locus) |
| 1304 |
{ |
| 1305 |
rust_assert (internal_elements != nullptr); |
| 1306 |
} |
| 1307 |
|
| 1308 |
// Copy constructor requires cloning ArrayElems for polymorphism to hold |
| 1309 |
ArrayExpr (ArrayExpr const &other) |
| 1310 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 1311 |
inner_attrs (other.inner_attrs), locus (other.locus), |
| 1312 |
marked_for_strip (other.marked_for_strip) |
| 1313 |
{ |
| 1314 |
internal_elements = other.internal_elements->clone_array_elems (); |
| 1315 |
rust_assert (internal_elements != nullptr); |
| 1316 |
} |
| 1317 |
|
| 1318 |
// Overload assignment operator to clone internal_elements |
| 1319 |
ArrayExpr &operator= (ArrayExpr const &other) |
| 1320 |
{ |
| 1321 |
ExprWithoutBlock::operator= (other); |
| 1322 |
inner_attrs = other.inner_attrs; |
| 1323 |
locus = other.locus; |
| 1324 |
marked_for_strip = other.marked_for_strip; |
| 1325 |
outer_attrs = other.outer_attrs; |
| 1326 |
|
| 1327 |
internal_elements = other.internal_elements->clone_array_elems (); |
| 1328 |
|
| 1329 |
rust_assert (internal_elements != nullptr); |
| 1330 |
return *this; |
| 1331 |
} |
| 1332 |
|
| 1333 |
// move constructors |
| 1334 |
ArrayExpr (ArrayExpr &&other) = default; |
| 1335 |
ArrayExpr &operator= (ArrayExpr &&other) = default; |
| 1336 |
|
| 1337 |
location_t get_locus () const override final { return locus; } |
| 1338 |
|
| 1339 |
void accept_vis (ASTVisitor &vis) override; |
| 1340 |
|
| 1341 |
// Can't think of any invalid invariants, so store boolean. |
| 1342 |
void mark_for_strip () override { marked_for_strip = true; } |
| 1343 |
bool is_marked_for_strip () const override { return marked_for_strip; } |
| 1344 |
|
| 1345 |
// TODO: is this better? Or is a "vis_block" better? |
| 1346 |
std::unique_ptr<ArrayElems> &get_array_elems () |
| 1347 |
{ |
| 1348 |
rust_assert (internal_elements != nullptr); |
| 1349 |
return internal_elements; |
| 1350 |
} |
| 1351 |
|
| 1352 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Array; } |
| 1353 |
|
| 1354 |
protected: |
| 1355 |
/* Use covariance to implement clone function as returning this object rather |
| 1356 |
* than base */ |
| 1357 |
ArrayExpr *clone_expr_without_block_impl () const override |
| 1358 |
{ |
| 1359 |
return new ArrayExpr (*this); |
| 1360 |
} |
| 1361 |
}; |
| 1362 |
|
| 1363 |
// Aka IndexExpr (also applies to slices) |
| 1364 |
/* Apparently a[b] is equivalent to *std::ops::Index::index(&a, b) or |
| 1365 |
* *std::ops::Index::index_mut(&mut a, b) */ |
| 1366 |
/* Also apparently deref operations on a will be repeatedly applied to find an |
| 1367 |
* implementation */ |
| 1368 |
class ArrayIndexExpr : public ExprWithoutBlock |
| 1369 |
{ |
| 1370 |
std::vector<Attribute> outer_attrs; |
| 1371 |
std::unique_ptr<Expr> array_expr; |
| 1372 |
std::unique_ptr<Expr> index_expr; |
| 1373 |
location_t locus; |
| 1374 |
|
| 1375 |
public: |
| 1376 |
std::string as_string () const override; |
| 1377 |
|
| 1378 |
ArrayIndexExpr (std::unique_ptr<Expr> array_expr, |
| 1379 |
std::unique_ptr<Expr> array_index_expr, |
| 1380 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 1381 |
: outer_attrs (std::move (outer_attribs)), |
| 1382 |
array_expr (std::move (array_expr)), |
| 1383 |
index_expr (std::move (array_index_expr)), locus (locus) |
| 1384 |
{} |
| 1385 |
|
| 1386 |
// Copy constructor requires special cloning due to unique_ptr |
| 1387 |
ArrayIndexExpr (ArrayIndexExpr const &other) |
| 1388 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 1389 |
locus (other.locus) |
| 1390 |
{ |
| 1391 |
// guard to prevent null dereference (only required if error state) |
| 1392 |
if (other.array_expr != nullptr) |
| 1393 |
array_expr = other.array_expr->clone_expr (); |
| 1394 |
if (other.index_expr != nullptr) |
| 1395 |
index_expr = other.index_expr->clone_expr (); |
| 1396 |
} |
| 1397 |
|
| 1398 |
// Overload assignment operator to clone unique_ptrs |
| 1399 |
ArrayIndexExpr &operator= (ArrayIndexExpr const &other) |
| 1400 |
{ |
| 1401 |
ExprWithoutBlock::operator= (other); |
| 1402 |
outer_attrs = other.outer_attrs; |
| 1403 |
locus = other.locus; |
| 1404 |
|
| 1405 |
// guard to prevent null dereference (only required if error state) |
| 1406 |
if (other.array_expr != nullptr) |
| 1407 |
array_expr = other.array_expr->clone_expr (); |
| 1408 |
else |
| 1409 |
array_expr = nullptr; |
| 1410 |
if (other.index_expr != nullptr) |
| 1411 |
index_expr = other.index_expr->clone_expr (); |
| 1412 |
else |
| 1413 |
index_expr = nullptr; |
| 1414 |
|
| 1415 |
return *this; |
| 1416 |
} |
| 1417 |
|
| 1418 |
// move constructors |
| 1419 |
ArrayIndexExpr (ArrayIndexExpr &&other) = default; |
| 1420 |
ArrayIndexExpr &operator= (ArrayIndexExpr &&other) = default; |
| 1421 |
|
| 1422 |
location_t get_locus () const override final { return locus; } |
| 1423 |
|
| 1424 |
void accept_vis (ASTVisitor &vis) override; |
| 1425 |
|
| 1426 |
// Invalid if either expr is null, so base stripping on that. |
| 1427 |
void mark_for_strip () override |
| 1428 |
{ |
| 1429 |
array_expr = nullptr; |
| 1430 |
index_expr = nullptr; |
| 1431 |
} |
| 1432 |
bool is_marked_for_strip () const override |
| 1433 |
{ |
| 1434 |
return array_expr == nullptr && index_expr == nullptr; |
| 1435 |
} |
| 1436 |
|
| 1437 |
// TODO: is this better? Or is a "vis_block" better? |
| 1438 |
Expr &get_array_expr () |
| 1439 |
{ |
| 1440 |
rust_assert (array_expr != nullptr); |
| 1441 |
return *array_expr; |
| 1442 |
} |
| 1443 |
|
| 1444 |
// TODO: is this better? Or is a "vis_block" better? |
| 1445 |
Expr &get_index_expr () |
| 1446 |
{ |
| 1447 |
rust_assert (index_expr != nullptr); |
| 1448 |
return *index_expr; |
| 1449 |
} |
| 1450 |
|
| 1451 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 1452 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 1453 |
|
| 1454 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 1455 |
{ |
| 1456 |
outer_attrs = std::move (new_attrs); |
| 1457 |
} |
| 1458 |
|
| 1459 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::ArrayIndex; } |
| 1460 |
|
| 1461 |
protected: |
| 1462 |
/* Use covariance to implement clone function as returning this object rather |
| 1463 |
* than base */ |
| 1464 |
ArrayIndexExpr *clone_expr_without_block_impl () const override |
| 1465 |
{ |
| 1466 |
return new ArrayIndexExpr (*this); |
| 1467 |
} |
| 1468 |
}; |
| 1469 |
|
| 1470 |
// AST representation of a tuple |
| 1471 |
class TupleExpr : public ExprWithoutBlock |
| 1472 |
{ |
| 1473 |
std::vector<Attribute> outer_attrs; |
| 1474 |
std::vector<Attribute> inner_attrs; |
| 1475 |
std::vector<std::unique_ptr<Expr> > tuple_elems; |
| 1476 |
location_t locus; |
| 1477 |
|
| 1478 |
// TODO: find another way to store this to save memory? |
| 1479 |
bool marked_for_strip = false; |
| 1480 |
|
| 1481 |
public: |
| 1482 |
std::string as_string () const override; |
| 1483 |
|
| 1484 |
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; } |
| 1485 |
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; } |
| 1486 |
|
| 1487 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 1488 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 1489 |
|
| 1490 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 1491 |
{ |
| 1492 |
outer_attrs = std::move (new_attrs); |
| 1493 |
} |
| 1494 |
|
| 1495 |
TupleExpr (std::vector<std::unique_ptr<Expr> > tuple_elements, |
| 1496 |
std::vector<Attribute> inner_attribs, |
| 1497 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 1498 |
: outer_attrs (std::move (outer_attribs)), |
| 1499 |
inner_attrs (std::move (inner_attribs)), |
| 1500 |
tuple_elems (std::move (tuple_elements)), locus (locus) |
| 1501 |
{} |
| 1502 |
|
| 1503 |
// copy constructor with vector clone |
| 1504 |
TupleExpr (TupleExpr const &other) |
| 1505 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 1506 |
inner_attrs (other.inner_attrs), locus (other.locus), |
| 1507 |
marked_for_strip (other.marked_for_strip) |
| 1508 |
{ |
| 1509 |
tuple_elems.reserve (other.tuple_elems.size ()); |
| 1510 |
for (const auto &e : other.tuple_elems) |
| 1511 |
tuple_elems.push_back (e->clone_expr ()); |
| 1512 |
} |
| 1513 |
|
| 1514 |
// overloaded assignment operator to vector clone |
| 1515 |
TupleExpr &operator= (TupleExpr const &other) |
| 1516 |
{ |
| 1517 |
ExprWithoutBlock::operator= (other); |
| 1518 |
outer_attrs = other.outer_attrs; |
| 1519 |
inner_attrs = other.inner_attrs; |
| 1520 |
locus = other.locus; |
| 1521 |
marked_for_strip = other.marked_for_strip; |
| 1522 |
|
| 1523 |
tuple_elems.reserve (other.tuple_elems.size ()); |
| 1524 |
for (const auto &e : other.tuple_elems) |
| 1525 |
tuple_elems.push_back (e->clone_expr ()); |
| 1526 |
|
| 1527 |
return *this; |
| 1528 |
} |
| 1529 |
|
| 1530 |
// move constructors |
| 1531 |
TupleExpr (TupleExpr &&other) = default; |
| 1532 |
TupleExpr &operator= (TupleExpr &&other) = default; |
| 1533 |
|
| 1534 |
/* Note: syntactically, can disambiguate single-element tuple from parens with |
| 1535 |
* comma, i.e. (0,) rather than (0) */ |
| 1536 |
|
| 1537 |
location_t get_locus () const override final { return locus; } |
| 1538 |
|
| 1539 |
void accept_vis (ASTVisitor &vis) override; |
| 1540 |
|
| 1541 |
// Can't think of any invalid invariants, so store boolean. |
| 1542 |
void mark_for_strip () override { marked_for_strip = true; } |
| 1543 |
bool is_marked_for_strip () const override { return marked_for_strip; } |
| 1544 |
|
| 1545 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 1546 |
const std::vector<std::unique_ptr<Expr> > &get_tuple_elems () const |
| 1547 |
{ |
| 1548 |
return tuple_elems; |
| 1549 |
} |
| 1550 |
std::vector<std::unique_ptr<Expr> > &get_tuple_elems () |
| 1551 |
{ |
| 1552 |
return tuple_elems; |
| 1553 |
} |
| 1554 |
|
| 1555 |
bool is_unit () const { return tuple_elems.size () == 0; } |
| 1556 |
|
| 1557 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Tuple; } |
| 1558 |
|
| 1559 |
protected: |
| 1560 |
/* Use covariance to implement clone function as returning this object rather |
| 1561 |
* than base */ |
| 1562 |
TupleExpr *clone_expr_without_block_impl () const override |
| 1563 |
{ |
| 1564 |
return new TupleExpr (*this); |
| 1565 |
} |
| 1566 |
}; |
| 1567 |
|
| 1568 |
// aka TupleIndexingExpr |
| 1569 |
// AST representation of a tuple indexing expression |
| 1570 |
class TupleIndexExpr : public ExprWithoutBlock |
| 1571 |
{ |
| 1572 |
std::vector<Attribute> outer_attrs; |
| 1573 |
std::unique_ptr<Expr> tuple_expr; |
| 1574 |
// TupleIndex is a decimal int literal with no underscores or suffix |
| 1575 |
TupleIndex tuple_index; |
| 1576 |
|
| 1577 |
location_t locus; |
| 1578 |
|
| 1579 |
// i.e. pair.0 |
| 1580 |
|
| 1581 |
public: |
| 1582 |
std::string as_string () const override; |
| 1583 |
|
| 1584 |
TupleIndex get_tuple_index () const { return tuple_index; } |
| 1585 |
|
| 1586 |
TupleIndexExpr (std::unique_ptr<Expr> tuple_expr, TupleIndex index, |
| 1587 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 1588 |
: outer_attrs (std::move (outer_attribs)), |
| 1589 |
tuple_expr (std::move (tuple_expr)), tuple_index (index), locus (locus) |
| 1590 |
{} |
| 1591 |
|
| 1592 |
// Copy constructor requires a clone for tuple_expr |
| 1593 |
TupleIndexExpr (TupleIndexExpr const &other) |
| 1594 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 1595 |
tuple_index (other.tuple_index), locus (other.locus) |
| 1596 |
{ |
| 1597 |
// guard to prevent null dereference (only required if error state) |
| 1598 |
if (other.tuple_expr != nullptr) |
| 1599 |
tuple_expr = other.tuple_expr->clone_expr (); |
| 1600 |
} |
| 1601 |
|
| 1602 |
// Overload assignment operator in order to clone |
| 1603 |
TupleIndexExpr &operator= (TupleIndexExpr const &other) |
| 1604 |
{ |
| 1605 |
ExprWithoutBlock::operator= (other); |
| 1606 |
tuple_index = other.tuple_index; |
| 1607 |
locus = other.locus; |
| 1608 |
outer_attrs = other.outer_attrs; |
| 1609 |
|
| 1610 |
// guard to prevent null dereference (only required if error state) |
| 1611 |
if (other.tuple_expr != nullptr) |
| 1612 |
tuple_expr = other.tuple_expr->clone_expr (); |
| 1613 |
else |
| 1614 |
tuple_expr = nullptr; |
| 1615 |
|
| 1616 |
return *this; |
| 1617 |
} |
| 1618 |
|
| 1619 |
// move constructors |
| 1620 |
TupleIndexExpr (TupleIndexExpr &&other) = default; |
| 1621 |
TupleIndexExpr &operator= (TupleIndexExpr &&other) = default; |
| 1622 |
|
| 1623 |
location_t get_locus () const override final { return locus; } |
| 1624 |
|
| 1625 |
void accept_vis (ASTVisitor &vis) override; |
| 1626 |
|
| 1627 |
// Invalid if tuple expr is null, so base stripping on that. |
| 1628 |
void mark_for_strip () override { tuple_expr = nullptr; } |
| 1629 |
bool is_marked_for_strip () const override { return tuple_expr == nullptr; } |
| 1630 |
|
| 1631 |
// TODO: is this better? Or is a "vis_block" better? |
| 1632 |
Expr &get_tuple_expr () |
| 1633 |
{ |
| 1634 |
rust_assert (tuple_expr != nullptr); |
| 1635 |
return *tuple_expr; |
| 1636 |
} |
| 1637 |
|
| 1638 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 1639 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 1640 |
|
| 1641 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 1642 |
{ |
| 1643 |
outer_attrs = std::move (new_attrs); |
| 1644 |
} |
| 1645 |
|
| 1646 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::TupleIndex; } |
| 1647 |
|
| 1648 |
protected: |
| 1649 |
/* Use covariance to implement clone function as returning this object rather |
| 1650 |
* than base */ |
| 1651 |
TupleIndexExpr *clone_expr_without_block_impl () const override |
| 1652 |
{ |
| 1653 |
return new TupleIndexExpr (*this); |
| 1654 |
} |
| 1655 |
}; |
| 1656 |
|
| 1657 |
// Base struct/tuple/union value creator AST node (abstract) |
| 1658 |
class StructExpr : public ExprWithoutBlock |
| 1659 |
{ |
| 1660 |
std::vector<Attribute> outer_attrs; |
| 1661 |
PathInExpression struct_name; |
| 1662 |
|
| 1663 |
protected: |
| 1664 |
// Protected constructor to allow initialising struct_name |
| 1665 |
StructExpr (PathInExpression struct_path, |
| 1666 |
std::vector<Attribute> outer_attribs) |
| 1667 |
: outer_attrs (std::move (outer_attribs)), |
| 1668 |
struct_name (std::move (struct_path)) |
| 1669 |
{} |
| 1670 |
|
| 1671 |
public: |
| 1672 |
const PathInExpression &get_struct_name () const { return struct_name; } |
| 1673 |
PathInExpression &get_struct_name () { return struct_name; } |
| 1674 |
|
| 1675 |
std::string as_string () const override; |
| 1676 |
|
| 1677 |
// Invalid if path is empty, so base stripping on that. |
| 1678 |
void mark_for_strip () override |
| 1679 |
{ |
| 1680 |
struct_name = PathInExpression::create_error (); |
| 1681 |
} |
| 1682 |
bool is_marked_for_strip () const override { return struct_name.is_error (); } |
| 1683 |
|
| 1684 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 1685 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 1686 |
|
| 1687 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 1688 |
{ |
| 1689 |
outer_attrs = std::move (new_attrs); |
| 1690 |
} |
| 1691 |
|
| 1692 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Struct; } |
| 1693 |
}; |
| 1694 |
|
| 1695 |
// Actual AST node of the struct creator (with no fields). Not abstract! |
| 1696 |
class StructExprStruct : public StructExpr |
| 1697 |
{ |
| 1698 |
std::vector<Attribute> inner_attrs; |
| 1699 |
|
| 1700 |
location_t locus; |
| 1701 |
|
| 1702 |
public: |
| 1703 |
std::string as_string () const override; |
| 1704 |
|
| 1705 |
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; } |
| 1706 |
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; } |
| 1707 |
|
| 1708 |
// Constructor has to call protected constructor of base class |
| 1709 |
StructExprStruct (PathInExpression struct_path, |
| 1710 |
std::vector<Attribute> inner_attribs, |
| 1711 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 1712 |
: StructExpr (std::move (struct_path), std::move (outer_attribs)), |
| 1713 |
inner_attrs (std::move (inner_attribs)), locus (locus) |
| 1714 |
{} |
| 1715 |
|
| 1716 |
location_t get_locus () const override final { return locus; } |
| 1717 |
|
| 1718 |
void accept_vis (ASTVisitor &vis) override; |
| 1719 |
|
| 1720 |
protected: |
| 1721 |
/* Use covariance to implement clone function as returning this object rather |
| 1722 |
* than base */ |
| 1723 |
StructExprStruct *clone_expr_without_block_impl () const override |
| 1724 |
{ |
| 1725 |
return new StructExprStruct (*this); |
| 1726 |
} |
| 1727 |
}; |
| 1728 |
|
| 1729 |
/* AST node representing expression used to fill a struct's fields from another |
| 1730 |
* struct */ |
| 1731 |
struct StructBase |
| 1732 |
{ |
| 1733 |
private: |
| 1734 |
std::unique_ptr<Expr> base_struct; |
| 1735 |
location_t locus; |
| 1736 |
|
| 1737 |
public: |
| 1738 |
StructBase (std::unique_ptr<Expr> base_struct_ptr, location_t locus) |
| 1739 |
: base_struct (std::move (base_struct_ptr)), locus (locus) |
| 1740 |
{} |
| 1741 |
|
| 1742 |
// Copy constructor requires clone |
| 1743 |
StructBase (StructBase const &other) |
| 1744 |
{ |
| 1745 |
/* HACK: gets around base_struct pointer being null (e.g. if no struct base |
| 1746 |
* exists) */ |
| 1747 |
if (other.base_struct != nullptr) |
| 1748 |
base_struct = other.base_struct->clone_expr (); |
| 1749 |
} |
| 1750 |
|
| 1751 |
// Destructor |
| 1752 |
~StructBase () = default; |
| 1753 |
|
| 1754 |
// Overload assignment operator to clone base_struct |
| 1755 |
StructBase &operator= (StructBase const &other) |
| 1756 |
{ |
| 1757 |
// prevent null pointer dereference |
| 1758 |
if (other.base_struct != nullptr) |
| 1759 |
base_struct = other.base_struct->clone_expr (); |
| 1760 |
else |
| 1761 |
base_struct = nullptr; |
| 1762 |
|
| 1763 |
return *this; |
| 1764 |
} |
| 1765 |
|
| 1766 |
// move constructors |
| 1767 |
StructBase (StructBase &&other) = default; |
| 1768 |
StructBase &operator= (StructBase &&other) = default; |
| 1769 |
|
| 1770 |
// Returns a null expr-ed StructBase - error state |
| 1771 |
static StructBase error () { return StructBase (nullptr, UNDEF_LOCATION); } |
| 1772 |
|
| 1773 |
// Returns whether StructBase is in error state |
| 1774 |
bool is_invalid () const { return base_struct == nullptr; } |
| 1775 |
|
| 1776 |
std::string as_string () const; |
| 1777 |
|
| 1778 |
// TODO: is this better? Or is a "vis_block" better? |
| 1779 |
Expr &get_base_struct () |
| 1780 |
{ |
| 1781 |
rust_assert (base_struct != nullptr); |
| 1782 |
return *base_struct; |
| 1783 |
} |
| 1784 |
}; |
| 1785 |
|
| 1786 |
/* Base AST node for a single struct expression field (in struct instance |
| 1787 |
* creation) - abstract */ |
| 1788 |
class StructExprField |
| 1789 |
{ |
| 1790 |
public: |
| 1791 |
virtual ~StructExprField () {} |
| 1792 |
|
| 1793 |
// Unique pointer custom clone function |
| 1794 |
std::unique_ptr<StructExprField> clone_struct_expr_field () const |
| 1795 |
{ |
| 1796 |
return std::unique_ptr<StructExprField> (clone_struct_expr_field_impl ()); |
| 1797 |
} |
| 1798 |
|
| 1799 |
virtual std::string as_string () const = 0; |
| 1800 |
|
| 1801 |
virtual void accept_vis (ASTVisitor &vis) = 0; |
| 1802 |
|
| 1803 |
virtual location_t get_locus () const = 0; |
| 1804 |
|
| 1805 |
NodeId get_node_id () const { return node_id; } |
| 1806 |
|
| 1807 |
const std::vector<AST::Attribute> &get_outer_attrs () const |
| 1808 |
{ |
| 1809 |
return outer_attrs; |
| 1810 |
} |
| 1811 |
|
| 1812 |
std::vector<AST::Attribute> &get_outer_attrs () { return outer_attrs; } |
| 1813 |
|
| 1814 |
protected: |
| 1815 |
// pure virtual clone implementation |
| 1816 |
virtual StructExprField *clone_struct_expr_field_impl () const = 0; |
| 1817 |
|
| 1818 |
StructExprField () : node_id (Analysis::Mappings::get ().get_next_node_id ()) |
| 1819 |
{} |
| 1820 |
|
| 1821 |
StructExprField (AST::AttrVec outer_attrs) |
| 1822 |
: outer_attrs (std::move (outer_attrs)), |
| 1823 |
node_id (Analysis::Mappings::get ().get_next_node_id ()) |
| 1824 |
{} |
| 1825 |
|
| 1826 |
AST::AttrVec outer_attrs; |
| 1827 |
NodeId node_id; |
| 1828 |
}; |
| 1829 |
|
| 1830 |
// Identifier-only variant of StructExprField AST node |
| 1831 |
class StructExprFieldIdentifier : public StructExprField |
| 1832 |
{ |
| 1833 |
Identifier field_name; |
| 1834 |
location_t locus; |
| 1835 |
|
| 1836 |
public: |
| 1837 |
StructExprFieldIdentifier (Identifier field_identifier, |
| 1838 |
AST::AttrVec outer_attrs, location_t locus) |
| 1839 |
: StructExprField (std::move (outer_attrs)), |
| 1840 |
field_name (std::move (field_identifier)), locus (locus) |
| 1841 |
{} |
| 1842 |
|
| 1843 |
std::string as_string () const override { return field_name.as_string (); } |
| 1844 |
|
| 1845 |
location_t get_locus () const override final { return locus; } |
| 1846 |
|
| 1847 |
void accept_vis (ASTVisitor &vis) override; |
| 1848 |
|
| 1849 |
Identifier get_field_name () const { return field_name; } |
| 1850 |
|
| 1851 |
protected: |
| 1852 |
/* Use covariance to implement clone function as returning this object rather |
| 1853 |
* than base */ |
| 1854 |
StructExprFieldIdentifier *clone_struct_expr_field_impl () const override |
| 1855 |
{ |
| 1856 |
return new StructExprFieldIdentifier (*this); |
| 1857 |
} |
| 1858 |
}; |
| 1859 |
|
| 1860 |
/* Base AST node for a single struct expression field with an assigned value - |
| 1861 |
* abstract */ |
| 1862 |
class StructExprFieldWithVal : public StructExprField |
| 1863 |
{ |
| 1864 |
std::unique_ptr<Expr> value; |
| 1865 |
|
| 1866 |
protected: |
| 1867 |
StructExprFieldWithVal (std::unique_ptr<Expr> field_value, |
| 1868 |
AST::AttrVec outer_attrs) |
| 1869 |
: StructExprField (std::move (outer_attrs)), value (std::move (field_value)) |
| 1870 |
{} |
| 1871 |
|
| 1872 |
// Copy constructor requires clone |
| 1873 |
StructExprFieldWithVal (StructExprFieldWithVal const &other) |
| 1874 |
: StructExprField (other.get_outer_attrs ()), |
| 1875 |
value (other.value->clone_expr ()) |
| 1876 |
{} |
| 1877 |
|
| 1878 |
// Overload assignment operator to clone unique_ptr |
| 1879 |
StructExprFieldWithVal &operator= (StructExprFieldWithVal const &other) |
| 1880 |
{ |
| 1881 |
value = other.value->clone_expr (); |
| 1882 |
outer_attrs = other.get_outer_attrs (); |
| 1883 |
|
| 1884 |
return *this; |
| 1885 |
} |
| 1886 |
|
| 1887 |
// move constructors |
| 1888 |
StructExprFieldWithVal (StructExprFieldWithVal &&other) = default; |
| 1889 |
StructExprFieldWithVal &operator= (StructExprFieldWithVal &&other) = default; |
| 1890 |
|
| 1891 |
public: |
| 1892 |
std::string as_string () const override; |
| 1893 |
|
| 1894 |
// TODO: is this better? Or is a "vis_block" better? |
| 1895 |
Expr &get_value () |
| 1896 |
{ |
| 1897 |
rust_assert (value != nullptr); |
| 1898 |
return *value; |
| 1899 |
} |
| 1900 |
}; |
| 1901 |
|
| 1902 |
// Identifier and value variant of StructExprField AST node |
| 1903 |
class StructExprFieldIdentifierValue : public StructExprFieldWithVal |
| 1904 |
{ |
| 1905 |
Identifier field_name; |
| 1906 |
location_t locus; |
| 1907 |
|
| 1908 |
public: |
| 1909 |
StructExprFieldIdentifierValue (Identifier field_identifier, |
| 1910 |
std::unique_ptr<Expr> field_value, |
| 1911 |
AST::AttrVec outer_attrs, location_t locus) |
| 1912 |
: StructExprFieldWithVal (std::move (field_value), std::move (outer_attrs)), |
| 1913 |
field_name (std::move (field_identifier)), locus (locus) |
| 1914 |
{} |
| 1915 |
|
| 1916 |
StructExprFieldIdentifierValue (Identifier field_identifier, |
| 1917 |
std::unique_ptr<Expr> field_value, |
| 1918 |
location_t locus) |
| 1919 |
: StructExprFieldWithVal (std::move (field_value), {}), |
| 1920 |
field_name (std::move (field_identifier)), locus (locus) |
| 1921 |
{} |
| 1922 |
|
| 1923 |
std::string as_string () const override; |
| 1924 |
|
| 1925 |
void accept_vis (ASTVisitor &vis) override; |
| 1926 |
|
| 1927 |
std::string get_field_name () const { return field_name.as_string (); } |
| 1928 |
|
| 1929 |
location_t get_locus () const override final { return locus; } |
| 1930 |
|
| 1931 |
protected: |
| 1932 |
/* Use covariance to implement clone function as returning this object rather |
| 1933 |
* than base */ |
| 1934 |
StructExprFieldIdentifierValue *clone_struct_expr_field_impl () const override |
| 1935 |
{ |
| 1936 |
return new StructExprFieldIdentifierValue (*this); |
| 1937 |
} |
| 1938 |
}; |
| 1939 |
|
| 1940 |
// Tuple index and value variant of StructExprField AST node |
| 1941 |
class StructExprFieldIndexValue : public StructExprFieldWithVal |
| 1942 |
{ |
| 1943 |
TupleIndex index; |
| 1944 |
location_t locus; |
| 1945 |
|
| 1946 |
public: |
| 1947 |
StructExprFieldIndexValue (TupleIndex tuple_index, |
| 1948 |
std::unique_ptr<Expr> field_value, |
| 1949 |
AST::AttrVec outer_attrs, location_t locus) |
| 1950 |
: StructExprFieldWithVal (std::move (field_value), std::move (outer_attrs)), |
| 1951 |
index (tuple_index), locus (locus) |
| 1952 |
{} |
| 1953 |
|
| 1954 |
std::string as_string () const override; |
| 1955 |
|
| 1956 |
void accept_vis (ASTVisitor &vis) override; |
| 1957 |
|
| 1958 |
TupleIndex get_index () const { return index; } |
| 1959 |
|
| 1960 |
location_t get_locus () const override final { return locus; } |
| 1961 |
|
| 1962 |
protected: |
| 1963 |
/* Use covariance to implement clone function as returning this object rather |
| 1964 |
* than base */ |
| 1965 |
StructExprFieldIndexValue *clone_struct_expr_field_impl () const override |
| 1966 |
{ |
| 1967 |
return new StructExprFieldIndexValue (*this); |
| 1968 |
} |
| 1969 |
}; |
| 1970 |
|
| 1971 |
// AST node of a struct creator with fields |
| 1972 |
class StructExprStructFields : public StructExprStruct |
| 1973 |
{ |
| 1974 |
// std::vector<StructExprField> fields; |
| 1975 |
std::vector<std::unique_ptr<StructExprField> > fields; |
| 1976 |
|
| 1977 |
// bool has_struct_base; |
| 1978 |
StructBase struct_base; |
| 1979 |
|
| 1980 |
public: |
| 1981 |
std::string as_string () const override; |
| 1982 |
|
| 1983 |
bool has_struct_base () const { return !struct_base.is_invalid (); } |
| 1984 |
|
| 1985 |
// Constructor for StructExprStructFields when no struct base is used |
| 1986 |
StructExprStructFields ( |
| 1987 |
PathInExpression struct_path, |
| 1988 |
std::vector<std::unique_ptr<StructExprField> > expr_fields, |
| 1989 |
location_t locus, StructBase base_struct = StructBase::error (), |
| 1990 |
std::vector<Attribute> inner_attribs = std::vector<Attribute> (), |
| 1991 |
std::vector<Attribute> outer_attribs = std::vector<Attribute> ()) |
| 1992 |
: StructExprStruct (std::move (struct_path), std::move (inner_attribs), |
| 1993 |
std::move (outer_attribs), locus), |
| 1994 |
fields (std::move (expr_fields)), struct_base (std::move (base_struct)) |
| 1995 |
{} |
| 1996 |
|
| 1997 |
// copy constructor with vector clone |
| 1998 |
StructExprStructFields (StructExprStructFields const &other) |
| 1999 |
: StructExprStruct (other), struct_base (other.struct_base) |
| 2000 |
{ |
| 2001 |
fields.reserve (other.fields.size ()); |
| 2002 |
for (const auto &e : other.fields) |
| 2003 |
fields.push_back (e->clone_struct_expr_field ()); |
| 2004 |
} |
| 2005 |
|
| 2006 |
// overloaded assignment operator with vector clone |
| 2007 |
StructExprStructFields &operator= (StructExprStructFields const &other) |
| 2008 |
{ |
| 2009 |
StructExprStruct::operator= (other); |
| 2010 |
struct_base = other.struct_base; |
| 2011 |
|
| 2012 |
fields.reserve (other.fields.size ()); |
| 2013 |
for (const auto &e : other.fields) |
| 2014 |
fields.push_back (e->clone_struct_expr_field ()); |
| 2015 |
|
| 2016 |
return *this; |
| 2017 |
} |
| 2018 |
|
| 2019 |
// move constructors |
| 2020 |
StructExprStructFields (StructExprStructFields &&other) = default; |
| 2021 |
StructExprStructFields &operator= (StructExprStructFields &&other) = default; |
| 2022 |
|
| 2023 |
void accept_vis (ASTVisitor &vis) override; |
| 2024 |
|
| 2025 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 2026 |
std::vector<std::unique_ptr<StructExprField> > &get_fields () |
| 2027 |
{ |
| 2028 |
return fields; |
| 2029 |
} |
| 2030 |
const std::vector<std::unique_ptr<StructExprField> > &get_fields () const |
| 2031 |
{ |
| 2032 |
return fields; |
| 2033 |
} |
| 2034 |
|
| 2035 |
StructBase &get_struct_base () { return struct_base; } |
| 2036 |
const StructBase &get_struct_base () const { return struct_base; } |
| 2037 |
|
| 2038 |
protected: |
| 2039 |
/* Use covariance to implement clone function as returning this object rather |
| 2040 |
* than base */ |
| 2041 |
StructExprStructFields *clone_expr_without_block_impl () const override |
| 2042 |
{ |
| 2043 |
return new StructExprStructFields (*this); |
| 2044 |
} |
| 2045 |
}; |
| 2046 |
|
| 2047 |
// AST node of the functional update struct creator |
| 2048 |
/* TODO: remove and replace with StructExprStructFields, except with empty |
| 2049 |
* vector of fields? */ |
| 2050 |
class StructExprStructBase : public StructExprStruct |
| 2051 |
{ |
| 2052 |
StructBase struct_base; |
| 2053 |
|
| 2054 |
public: |
| 2055 |
std::string as_string () const override; |
| 2056 |
|
| 2057 |
StructExprStructBase (PathInExpression struct_path, StructBase base_struct, |
| 2058 |
std::vector<Attribute> inner_attribs, |
| 2059 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 2060 |
: StructExprStruct (std::move (struct_path), std::move (inner_attribs), |
| 2061 |
std::move (outer_attribs), locus), |
| 2062 |
struct_base (std::move (base_struct)) |
| 2063 |
{} |
| 2064 |
|
| 2065 |
void accept_vis (ASTVisitor &vis) override; |
| 2066 |
|
| 2067 |
StructBase &get_struct_base () { return struct_base; } |
| 2068 |
const StructBase &get_struct_base () const { return struct_base; } |
| 2069 |
|
| 2070 |
protected: |
| 2071 |
/* Use covariance to implement clone function as returning this object rather |
| 2072 |
* than base */ |
| 2073 |
StructExprStructBase *clone_expr_without_block_impl () const override |
| 2074 |
{ |
| 2075 |
return new StructExprStructBase (*this); |
| 2076 |
} |
| 2077 |
}; |
| 2078 |
|
| 2079 |
// Forward decl for Function - used in CallExpr |
| 2080 |
class Function; |
| 2081 |
|
| 2082 |
// Function call expression AST node |
| 2083 |
class CallExpr : public ExprWithoutBlock |
| 2084 |
{ |
| 2085 |
std::vector<Attribute> outer_attrs; |
| 2086 |
std::unique_ptr<Expr> function; |
| 2087 |
std::vector<std::unique_ptr<Expr> > params; |
| 2088 |
location_t locus; |
| 2089 |
|
| 2090 |
public: |
| 2091 |
Function *fndeclRef; |
| 2092 |
|
| 2093 |
std::string as_string () const override; |
| 2094 |
|
| 2095 |
CallExpr (std::unique_ptr<Expr> function_expr, |
| 2096 |
std::vector<std::unique_ptr<Expr> > function_params, |
| 2097 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 2098 |
: outer_attrs (std::move (outer_attribs)), |
| 2099 |
function (std::move (function_expr)), |
| 2100 |
params (std::move (function_params)), locus (locus) |
| 2101 |
{} |
| 2102 |
|
| 2103 |
// copy constructor requires clone |
| 2104 |
CallExpr (CallExpr const &other) |
| 2105 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 2106 |
locus (other.locus) |
| 2107 |
{ |
| 2108 |
// guard to prevent null dereference (only required if error state) |
| 2109 |
if (other.function != nullptr) |
| 2110 |
function = other.function->clone_expr (); |
| 2111 |
|
| 2112 |
params.reserve (other.params.size ()); |
| 2113 |
for (const auto &e : other.params) |
| 2114 |
params.push_back (e->clone_expr ()); |
| 2115 |
} |
| 2116 |
|
| 2117 |
// Overload assignment operator to clone |
| 2118 |
CallExpr &operator= (CallExpr const &other) |
| 2119 |
{ |
| 2120 |
ExprWithoutBlock::operator= (other); |
| 2121 |
locus = other.locus; |
| 2122 |
outer_attrs = other.outer_attrs; |
| 2123 |
|
| 2124 |
// guard to prevent null dereference (only required if error state) |
| 2125 |
if (other.function != nullptr) |
| 2126 |
function = other.function->clone_expr (); |
| 2127 |
else |
| 2128 |
function = nullptr; |
| 2129 |
|
| 2130 |
params.reserve (other.params.size ()); |
| 2131 |
for (const auto &e : other.params) |
| 2132 |
params.push_back (e->clone_expr ()); |
| 2133 |
|
| 2134 |
return *this; |
| 2135 |
} |
| 2136 |
|
| 2137 |
// move constructors |
| 2138 |
CallExpr (CallExpr &&other) = default; |
| 2139 |
CallExpr &operator= (CallExpr &&other) = default; |
| 2140 |
|
| 2141 |
// Returns whether function call has parameters. |
| 2142 |
bool has_params () const { return !params.empty (); } |
| 2143 |
|
| 2144 |
location_t get_locus () const override final { return locus; } |
| 2145 |
|
| 2146 |
void accept_vis (ASTVisitor &vis) override; |
| 2147 |
|
| 2148 |
// Invalid if function expr is null, so base stripping on that. |
| 2149 |
void mark_for_strip () override { function = nullptr; } |
| 2150 |
bool is_marked_for_strip () const override { return function == nullptr; } |
| 2151 |
|
| 2152 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 2153 |
const std::vector<std::unique_ptr<Expr> > &get_params () const |
| 2154 |
{ |
| 2155 |
return params; |
| 2156 |
} |
| 2157 |
std::vector<std::unique_ptr<Expr> > &get_params () { return params; } |
| 2158 |
|
| 2159 |
// TODO: is this better? Or is a "vis_block" better? |
| 2160 |
Expr &get_function_expr () |
| 2161 |
{ |
| 2162 |
rust_assert (function != nullptr); |
| 2163 |
return *function; |
| 2164 |
} |
| 2165 |
|
| 2166 |
std::unique_ptr<Expr> &get_function_expr_ptr () { return function; } |
| 2167 |
|
| 2168 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 2169 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 2170 |
|
| 2171 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 2172 |
{ |
| 2173 |
outer_attrs = std::move (new_attrs); |
| 2174 |
} |
| 2175 |
|
| 2176 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Call; } |
| 2177 |
|
| 2178 |
protected: |
| 2179 |
/* Use covariance to implement clone function as returning this object rather |
| 2180 |
* than base */ |
| 2181 |
CallExpr *clone_expr_without_block_impl () const override |
| 2182 |
{ |
| 2183 |
return new CallExpr (*this); |
| 2184 |
} |
| 2185 |
}; |
| 2186 |
|
| 2187 |
// Method call expression AST node |
| 2188 |
class MethodCallExpr : public ExprWithoutBlock |
| 2189 |
{ |
| 2190 |
std::vector<Attribute> outer_attrs; |
| 2191 |
std::unique_ptr<Expr> receiver; |
| 2192 |
PathExprSegment method_name; |
| 2193 |
std::vector<std::unique_ptr<Expr> > params; |
| 2194 |
location_t locus; |
| 2195 |
|
| 2196 |
public: |
| 2197 |
std::string as_string () const override; |
| 2198 |
|
| 2199 |
MethodCallExpr (std::unique_ptr<Expr> call_receiver, |
| 2200 |
PathExprSegment method_path, |
| 2201 |
std::vector<std::unique_ptr<Expr> > method_params, |
| 2202 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 2203 |
: outer_attrs (std::move (outer_attribs)), |
| 2204 |
receiver (std::move (call_receiver)), |
| 2205 |
method_name (std::move (method_path)), params (std::move (method_params)), |
| 2206 |
locus (locus) |
| 2207 |
{} |
| 2208 |
|
| 2209 |
// copy constructor required due to cloning |
| 2210 |
MethodCallExpr (MethodCallExpr const &other) |
| 2211 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 2212 |
method_name (other.method_name), locus (other.locus) |
| 2213 |
{ |
| 2214 |
// guard to prevent null dereference (only required if error state) |
| 2215 |
if (other.receiver != nullptr) |
| 2216 |
receiver = other.receiver->clone_expr (); |
| 2217 |
|
| 2218 |
params.reserve (other.params.size ()); |
| 2219 |
for (const auto &e : other.params) |
| 2220 |
params.push_back (e->clone_expr ()); |
| 2221 |
} |
| 2222 |
|
| 2223 |
// Overload assignment operator to clone receiver object |
| 2224 |
MethodCallExpr &operator= (MethodCallExpr const &other) |
| 2225 |
{ |
| 2226 |
ExprWithoutBlock::operator= (other); |
| 2227 |
method_name = other.method_name; |
| 2228 |
locus = other.locus; |
| 2229 |
outer_attrs = other.outer_attrs; |
| 2230 |
|
| 2231 |
// guard to prevent null dereference (only required if error state) |
| 2232 |
if (other.receiver != nullptr) |
| 2233 |
receiver = other.receiver->clone_expr (); |
| 2234 |
else |
| 2235 |
receiver = nullptr; |
| 2236 |
|
| 2237 |
params.reserve (other.params.size ()); |
| 2238 |
for (const auto &e : other.params) |
| 2239 |
params.push_back (e->clone_expr ()); |
| 2240 |
|
| 2241 |
return *this; |
| 2242 |
} |
| 2243 |
|
| 2244 |
// move constructors |
| 2245 |
MethodCallExpr (MethodCallExpr &&other) = default; |
| 2246 |
MethodCallExpr &operator= (MethodCallExpr &&other) = default; |
| 2247 |
|
| 2248 |
location_t get_locus () const override final { return locus; } |
| 2249 |
|
| 2250 |
void accept_vis (ASTVisitor &vis) override; |
| 2251 |
|
| 2252 |
// Invalid if receiver expr is null, so base stripping on that. |
| 2253 |
void mark_for_strip () override { receiver = nullptr; } |
| 2254 |
bool is_marked_for_strip () const override { return receiver == nullptr; } |
| 2255 |
|
| 2256 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 2257 |
const std::vector<std::unique_ptr<Expr> > &get_params () const |
| 2258 |
{ |
| 2259 |
return params; |
| 2260 |
} |
| 2261 |
std::vector<std::unique_ptr<Expr> > &get_params () { return params; } |
| 2262 |
|
| 2263 |
// TODO: is this better? Or is a "vis_block" better? |
| 2264 |
Expr &get_receiver_expr () |
| 2265 |
{ |
| 2266 |
rust_assert (receiver != nullptr); |
| 2267 |
return *receiver; |
| 2268 |
} |
| 2269 |
|
| 2270 |
const PathExprSegment &get_method_name () const { return method_name; } |
| 2271 |
PathExprSegment &get_method_name () { return method_name; } |
| 2272 |
|
| 2273 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 2274 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 2275 |
|
| 2276 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 2277 |
{ |
| 2278 |
outer_attrs = std::move (new_attrs); |
| 2279 |
} |
| 2280 |
|
| 2281 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::MethodCall; } |
| 2282 |
|
| 2283 |
protected: |
| 2284 |
/* Use covariance to implement clone function as returning this object rather |
| 2285 |
* than base */ |
| 2286 |
MethodCallExpr *clone_expr_without_block_impl () const override |
| 2287 |
{ |
| 2288 |
return new MethodCallExpr (*this); |
| 2289 |
} |
| 2290 |
}; |
| 2291 |
|
| 2292 |
// aka FieldExpression |
| 2293 |
// Struct or union field access expression AST node |
| 2294 |
class FieldAccessExpr : public ExprWithoutBlock |
| 2295 |
{ |
| 2296 |
std::vector<Attribute> outer_attrs; |
| 2297 |
std::unique_ptr<Expr> receiver; |
| 2298 |
Identifier field; |
| 2299 |
location_t locus; |
| 2300 |
|
| 2301 |
public: |
| 2302 |
std::string as_string () const override; |
| 2303 |
|
| 2304 |
FieldAccessExpr (std::unique_ptr<Expr> field_access_receiver, |
| 2305 |
Identifier field_name, std::vector<Attribute> outer_attribs, |
| 2306 |
location_t locus) |
| 2307 |
: outer_attrs (std::move (outer_attribs)), |
| 2308 |
receiver (std::move (field_access_receiver)), |
| 2309 |
field (std::move (field_name)), locus (locus) |
| 2310 |
{} |
| 2311 |
|
| 2312 |
// Copy constructor required due to unique_ptr cloning |
| 2313 |
FieldAccessExpr (FieldAccessExpr const &other) |
| 2314 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 2315 |
field (other.field), locus (other.locus) |
| 2316 |
{ |
| 2317 |
// guard to prevent null dereference (only required if error state) |
| 2318 |
if (other.receiver != nullptr) |
| 2319 |
receiver = other.receiver->clone_expr (); |
| 2320 |
} |
| 2321 |
|
| 2322 |
// Overload assignment operator to clone unique_ptr |
| 2323 |
FieldAccessExpr &operator= (FieldAccessExpr const &other) |
| 2324 |
{ |
| 2325 |
ExprWithoutBlock::operator= (other); |
| 2326 |
field = other.field; |
| 2327 |
locus = other.locus; |
| 2328 |
outer_attrs = other.outer_attrs; |
| 2329 |
|
| 2330 |
// guard to prevent null dereference (only required if error state) |
| 2331 |
if (other.receiver != nullptr) |
| 2332 |
receiver = other.receiver->clone_expr (); |
| 2333 |
else |
| 2334 |
receiver = nullptr; |
| 2335 |
|
| 2336 |
return *this; |
| 2337 |
} |
| 2338 |
|
| 2339 |
// move constructors |
| 2340 |
FieldAccessExpr (FieldAccessExpr &&other) = default; |
| 2341 |
FieldAccessExpr &operator= (FieldAccessExpr &&other) = default; |
| 2342 |
|
| 2343 |
location_t get_locus () const override final { return locus; } |
| 2344 |
|
| 2345 |
void accept_vis (ASTVisitor &vis) override; |
| 2346 |
|
| 2347 |
// Invalid if receiver expr is null, so base stripping on that. |
| 2348 |
void mark_for_strip () override { receiver = nullptr; } |
| 2349 |
bool is_marked_for_strip () const override { return receiver == nullptr; } |
| 2350 |
|
| 2351 |
// TODO: is this better? Or is a "vis_block" better? |
| 2352 |
Expr &get_receiver_expr () |
| 2353 |
{ |
| 2354 |
rust_assert (receiver != nullptr); |
| 2355 |
return *receiver; |
| 2356 |
} |
| 2357 |
|
| 2358 |
Identifier get_field_name () const { return field; } |
| 2359 |
|
| 2360 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 2361 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 2362 |
|
| 2363 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 2364 |
{ |
| 2365 |
outer_attrs = std::move (new_attrs); |
| 2366 |
} |
| 2367 |
|
| 2368 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::FieldAccess; } |
| 2369 |
|
| 2370 |
protected: |
| 2371 |
/* Use covariance to implement clone function as returning this object rather |
| 2372 |
* than base */ |
| 2373 |
FieldAccessExpr *clone_expr_without_block_impl () const override |
| 2374 |
{ |
| 2375 |
return new FieldAccessExpr (*this); |
| 2376 |
} |
| 2377 |
}; |
| 2378 |
|
| 2379 |
// Closure parameter data structure |
| 2380 |
struct ClosureParam |
| 2381 |
{ |
| 2382 |
private: |
| 2383 |
std::vector<Attribute> outer_attrs; |
| 2384 |
std::unique_ptr<Pattern> pattern; |
| 2385 |
std::unique_ptr<Type> type; |
| 2386 |
location_t locus; |
| 2387 |
|
| 2388 |
public: |
| 2389 |
// Returns whether the type of the parameter has been given. |
| 2390 |
bool has_type_given () const { return type != nullptr; } |
| 2391 |
|
| 2392 |
bool has_outer_attrs () const { return !outer_attrs.empty (); } |
| 2393 |
|
| 2394 |
// Constructor for closure parameter |
| 2395 |
ClosureParam (std::unique_ptr<Pattern> param_pattern, location_t locus, |
| 2396 |
std::unique_ptr<Type> param_type = nullptr, |
| 2397 |
std::vector<Attribute> outer_attrs = {}) |
| 2398 |
: outer_attrs (std::move (outer_attrs)), |
| 2399 |
pattern (std::move (param_pattern)), type (std::move (param_type)), |
| 2400 |
locus (locus) |
| 2401 |
{} |
| 2402 |
|
| 2403 |
// Copy constructor required due to cloning as a result of unique_ptrs |
| 2404 |
ClosureParam (ClosureParam const &other) : outer_attrs (other.outer_attrs) |
| 2405 |
{ |
| 2406 |
// guard to protect from null pointer dereference |
| 2407 |
if (other.pattern != nullptr) |
| 2408 |
pattern = other.pattern->clone_pattern (); |
| 2409 |
if (other.type != nullptr) |
| 2410 |
type = other.type->clone_type (); |
| 2411 |
} |
| 2412 |
|
| 2413 |
~ClosureParam () = default; |
| 2414 |
|
| 2415 |
// Assignment operator must be overloaded to clone as well |
| 2416 |
ClosureParam &operator= (ClosureParam const &other) |
| 2417 |
{ |
| 2418 |
outer_attrs = other.outer_attrs; |
| 2419 |
|
| 2420 |
// guard to protect from null pointer dereference |
| 2421 |
if (other.pattern != nullptr) |
| 2422 |
pattern = other.pattern->clone_pattern (); |
| 2423 |
else |
| 2424 |
pattern = nullptr; |
| 2425 |
if (other.type != nullptr) |
| 2426 |
type = other.type->clone_type (); |
| 2427 |
else |
| 2428 |
type = nullptr; |
| 2429 |
|
| 2430 |
return *this; |
| 2431 |
} |
| 2432 |
|
| 2433 |
// move constructors |
| 2434 |
ClosureParam (ClosureParam &&other) = default; |
| 2435 |
ClosureParam &operator= (ClosureParam &&other) = default; |
| 2436 |
|
| 2437 |
// Returns whether closure parameter is in an error state. |
| 2438 |
bool is_error () const { return pattern == nullptr; } |
| 2439 |
|
| 2440 |
// Creates an error state closure parameter. |
| 2441 |
static ClosureParam create_error () |
| 2442 |
{ |
| 2443 |
return ClosureParam (nullptr, UNDEF_LOCATION); |
| 2444 |
} |
| 2445 |
|
| 2446 |
std::string as_string () const; |
| 2447 |
|
| 2448 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 2449 |
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; } |
| 2450 |
|
| 2451 |
Pattern &get_pattern () |
| 2452 |
{ |
| 2453 |
rust_assert (pattern != nullptr); |
| 2454 |
return *pattern; |
| 2455 |
} |
| 2456 |
|
| 2457 |
Type &get_type () |
| 2458 |
{ |
| 2459 |
rust_assert (has_type_given ()); |
| 2460 |
return *type; |
| 2461 |
} |
| 2462 |
|
| 2463 |
std::unique_ptr<Type> &get_type_ptr () |
| 2464 |
{ |
| 2465 |
rust_assert (has_type_given ()); |
| 2466 |
return type; |
| 2467 |
} |
| 2468 |
|
| 2469 |
location_t get_locus () const { return locus; } |
| 2470 |
}; |
| 2471 |
|
| 2472 |
// Base closure definition expression AST node - abstract |
| 2473 |
class ClosureExpr : public ExprWithoutBlock |
| 2474 |
{ |
| 2475 |
std::vector<Attribute> outer_attrs; |
| 2476 |
bool has_move; |
| 2477 |
std::vector<ClosureParam> params; // may be empty |
| 2478 |
location_t locus; |
| 2479 |
|
| 2480 |
protected: |
| 2481 |
ClosureExpr (std::vector<ClosureParam> closure_params, bool has_move, |
| 2482 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 2483 |
: outer_attrs (std::move (outer_attribs)), has_move (has_move), |
| 2484 |
params (std::move (closure_params)), locus (locus) |
| 2485 |
{} |
| 2486 |
|
| 2487 |
public: |
| 2488 |
std::string as_string () const override; |
| 2489 |
|
| 2490 |
location_t get_locus () const override final { return locus; } |
| 2491 |
|
| 2492 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 2493 |
const std::vector<ClosureParam> &get_params () const { return params; } |
| 2494 |
std::vector<ClosureParam> &get_params () { return params; } |
| 2495 |
|
| 2496 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 2497 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 2498 |
|
| 2499 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 2500 |
{ |
| 2501 |
outer_attrs = std::move (new_attrs); |
| 2502 |
} |
| 2503 |
|
| 2504 |
bool get_has_move () const { return has_move; } |
| 2505 |
|
| 2506 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Closure; } |
| 2507 |
}; |
| 2508 |
|
| 2509 |
// Represents a non-type-specified closure expression AST node |
| 2510 |
class ClosureExprInner : public ClosureExpr |
| 2511 |
{ |
| 2512 |
std::unique_ptr<Expr> closure_inner; |
| 2513 |
|
| 2514 |
public: |
| 2515 |
std::string as_string () const override; |
| 2516 |
|
| 2517 |
// Constructor for a ClosureExprInner |
| 2518 |
ClosureExprInner (std::unique_ptr<Expr> closure_inner_expr, |
| 2519 |
std::vector<ClosureParam> closure_params, location_t locus, |
| 2520 |
bool is_move = false, |
| 2521 |
std::vector<Attribute> outer_attribs |
| 2522 |
= std::vector<Attribute> ()) |
| 2523 |
: ClosureExpr (std::move (closure_params), is_move, |
| 2524 |
std::move (outer_attribs), locus), |
| 2525 |
closure_inner (std::move (closure_inner_expr)) |
| 2526 |
{} |
| 2527 |
|
| 2528 |
// Copy constructor must be defined to allow copying via cloning of unique_ptr |
| 2529 |
ClosureExprInner (ClosureExprInner const &other) : ClosureExpr (other) |
| 2530 |
{ |
| 2531 |
// guard to prevent null dereference (only required if error state) |
| 2532 |
if (other.closure_inner != nullptr) |
| 2533 |
closure_inner = other.closure_inner->clone_expr (); |
| 2534 |
} |
| 2535 |
|
| 2536 |
// Overload assignment operator to clone closure_inner |
| 2537 |
ClosureExprInner &operator= (ClosureExprInner const &other) |
| 2538 |
{ |
| 2539 |
ClosureExpr::operator= (other); |
| 2540 |
// params = other.params; |
| 2541 |
// has_move = other.has_move; |
| 2542 |
// outer_attrs = other.outer_attrs; |
| 2543 |
|
| 2544 |
// guard to prevent null dereference (only required if error state) |
| 2545 |
if (other.closure_inner != nullptr) |
| 2546 |
closure_inner = other.closure_inner->clone_expr (); |
| 2547 |
else |
| 2548 |
closure_inner = nullptr; |
| 2549 |
|
| 2550 |
return *this; |
| 2551 |
} |
| 2552 |
|
| 2553 |
// move constructors |
| 2554 |
ClosureExprInner (ClosureExprInner &&other) = default; |
| 2555 |
ClosureExprInner &operator= (ClosureExprInner &&other) = default; |
| 2556 |
|
| 2557 |
void accept_vis (ASTVisitor &vis) override; |
| 2558 |
|
| 2559 |
// Invalid if inner expr is null, so base stripping on that. |
| 2560 |
void mark_for_strip () override { closure_inner = nullptr; } |
| 2561 |
bool is_marked_for_strip () const override |
| 2562 |
{ |
| 2563 |
return closure_inner == nullptr; |
| 2564 |
} |
| 2565 |
|
| 2566 |
Expr &get_definition_expr () |
| 2567 |
{ |
| 2568 |
rust_assert (closure_inner != nullptr); |
| 2569 |
return *closure_inner; |
| 2570 |
} |
| 2571 |
|
| 2572 |
protected: |
| 2573 |
/* Use covariance to implement clone function as returning this object rather |
| 2574 |
* than base */ |
| 2575 |
ClosureExprInner *clone_expr_without_block_impl () const override |
| 2576 |
{ |
| 2577 |
return new ClosureExprInner (*this); |
| 2578 |
} |
| 2579 |
}; |
| 2580 |
|
| 2581 |
// A block AST node |
| 2582 |
class BlockExpr : public ExprWithBlock |
| 2583 |
{ |
| 2584 |
std::vector<Attribute> outer_attrs; |
| 2585 |
std::vector<Attribute> inner_attrs; |
| 2586 |
std::vector<std::unique_ptr<Stmt> > statements; |
| 2587 |
std::unique_ptr<Expr> expr; |
| 2588 |
tl::optional<LoopLabel> label; |
| 2589 |
location_t start_locus; |
| 2590 |
location_t end_locus; |
| 2591 |
bool marked_for_strip = false; |
| 2592 |
|
| 2593 |
public: |
| 2594 |
std::string as_string () const override; |
| 2595 |
|
| 2596 |
// Returns whether the block contains statements. |
| 2597 |
bool has_statements () const { return !statements.empty (); } |
| 2598 |
|
| 2599 |
// Returns whether the block contains a final expression. |
| 2600 |
bool has_tail_expr () const { return expr != nullptr; } |
| 2601 |
|
| 2602 |
BlockExpr (std::vector<std::unique_ptr<Stmt> > block_statements, |
| 2603 |
std::unique_ptr<Expr> block_expr, |
| 2604 |
std::vector<Attribute> inner_attribs, |
| 2605 |
std::vector<Attribute> outer_attribs, |
| 2606 |
tl::optional<LoopLabel> label, location_t start_locus, |
| 2607 |
location_t end_locus) |
| 2608 |
: outer_attrs (std::move (outer_attribs)), |
| 2609 |
inner_attrs (std::move (inner_attribs)), |
| 2610 |
statements (std::move (block_statements)), expr (std::move (block_expr)), |
| 2611 |
label (std::move (label)), start_locus (start_locus), |
| 2612 |
end_locus (end_locus) |
| 2613 |
{} |
| 2614 |
|
| 2615 |
// Copy constructor with clone |
| 2616 |
BlockExpr (BlockExpr const &other) |
| 2617 |
: ExprWithBlock (other), outer_attrs (other.outer_attrs), |
| 2618 |
inner_attrs (other.inner_attrs), label (other.label), |
| 2619 |
start_locus (other.start_locus), end_locus (other.end_locus), |
| 2620 |
marked_for_strip (other.marked_for_strip) |
| 2621 |
{ |
| 2622 |
// guard to protect from null pointer dereference |
| 2623 |
if (other.expr != nullptr) |
| 2624 |
expr = other.expr->clone_expr (); |
| 2625 |
|
| 2626 |
statements.reserve (other.statements.size ()); |
| 2627 |
for (const auto &e : other.statements) |
| 2628 |
statements.push_back (e->clone_stmt ()); |
| 2629 |
} |
| 2630 |
|
| 2631 |
// Overloaded assignment operator to clone pointer |
| 2632 |
BlockExpr &operator= (BlockExpr const &other) |
| 2633 |
{ |
| 2634 |
ExprWithBlock::operator= (other); |
| 2635 |
inner_attrs = other.inner_attrs; |
| 2636 |
start_locus = other.start_locus; |
| 2637 |
end_locus = other.end_locus; |
| 2638 |
marked_for_strip = other.marked_for_strip; |
| 2639 |
outer_attrs = other.outer_attrs; |
| 2640 |
|
| 2641 |
// guard to protect from null pointer dereference |
| 2642 |
if (other.expr != nullptr) |
| 2643 |
expr = other.expr->clone_expr (); |
| 2644 |
else |
| 2645 |
expr = nullptr; |
| 2646 |
|
| 2647 |
statements.reserve (other.statements.size ()); |
| 2648 |
for (const auto &e : other.statements) |
| 2649 |
statements.push_back (e->clone_stmt ()); |
| 2650 |
|
| 2651 |
return *this; |
| 2652 |
} |
| 2653 |
|
| 2654 |
// move constructors |
| 2655 |
BlockExpr (BlockExpr &&other) = default; |
| 2656 |
BlockExpr &operator= (BlockExpr &&other) = default; |
| 2657 |
|
| 2658 |
// Unique pointer custom clone function |
| 2659 |
std::unique_ptr<BlockExpr> clone_block_expr () const |
| 2660 |
{ |
| 2661 |
return std::unique_ptr<BlockExpr> (clone_block_expr_impl ()); |
| 2662 |
} |
| 2663 |
|
| 2664 |
location_t get_locus () const override final { return start_locus; } |
| 2665 |
|
| 2666 |
location_t get_start_locus () const { return start_locus; } |
| 2667 |
location_t get_end_locus () const { return end_locus; } |
| 2668 |
|
| 2669 |
void accept_vis (ASTVisitor &vis) override; |
| 2670 |
|
| 2671 |
// Can be completely empty, so have to have a separate flag. |
| 2672 |
void mark_for_strip () override { marked_for_strip = true; } |
| 2673 |
bool is_marked_for_strip () const override { return marked_for_strip; } |
| 2674 |
|
| 2675 |
size_t num_statements () const { return statements.size (); } |
| 2676 |
|
| 2677 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 2678 |
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; } |
| 2679 |
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; } |
| 2680 |
|
| 2681 |
const std::vector<std::unique_ptr<Stmt> > &get_statements () const |
| 2682 |
{ |
| 2683 |
return statements; |
| 2684 |
} |
| 2685 |
std::vector<std::unique_ptr<Stmt> > &get_statements () { return statements; } |
| 2686 |
|
| 2687 |
// TODO: is this better? Or is a "vis_block" better? |
| 2688 |
Expr &get_tail_expr () |
| 2689 |
{ |
| 2690 |
rust_assert (has_tail_expr ()); |
| 2691 |
return *expr; |
| 2692 |
} |
| 2693 |
|
| 2694 |
std::unique_ptr<Expr> &get_tail_expr_ptr () |
| 2695 |
{ |
| 2696 |
rust_assert (has_tail_expr ()); |
| 2697 |
return expr; |
| 2698 |
} |
| 2699 |
|
| 2700 |
std::unique_ptr<Expr> take_tail_expr () |
| 2701 |
{ |
| 2702 |
rust_assert (has_tail_expr ()); |
| 2703 |
return std::move (expr); |
| 2704 |
} |
| 2705 |
|
| 2706 |
void set_tail_expr (std::unique_ptr<Expr> expr) |
| 2707 |
{ |
| 2708 |
this->expr = std::move (expr); |
| 2709 |
} |
| 2710 |
|
| 2711 |
// Removes the tail expression from the block. |
| 2712 |
void strip_tail_expr () { expr = nullptr; } |
| 2713 |
// Normalizes a trailing statement without a semicolon to a tail expression. |
| 2714 |
void normalize_tail_expr (); |
| 2715 |
|
| 2716 |
void try_convert_last_stmt (); |
| 2717 |
|
| 2718 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 2719 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 2720 |
|
| 2721 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 2722 |
{ |
| 2723 |
outer_attrs = std::move (new_attrs); |
| 2724 |
} |
| 2725 |
|
| 2726 |
bool has_label () { return label.has_value (); } |
| 2727 |
LoopLabel &get_label () { return label.value (); } |
| 2728 |
|
| 2729 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Block; } |
| 2730 |
|
| 2731 |
protected: |
| 2732 |
/* Use covariance to implement clone function as returning this object rather |
| 2733 |
* than base */ |
| 2734 |
BlockExpr *clone_expr_with_block_impl () const final override |
| 2735 |
{ |
| 2736 |
return clone_block_expr_impl (); |
| 2737 |
} |
| 2738 |
|
| 2739 |
/* This is the base method as not an abstract class - not virtual but could be |
| 2740 |
* in future if required. */ |
| 2741 |
/*virtual*/ BlockExpr *clone_block_expr_impl () const |
| 2742 |
{ |
| 2743 |
return new BlockExpr (*this); |
| 2744 |
} |
| 2745 |
}; |
| 2746 |
|
| 2747 |
// Represents a type-specified closure expression AST node |
| 2748 |
class ClosureExprInnerTyped : public ClosureExpr |
| 2749 |
{ |
| 2750 |
// TODO: spec says typenobounds |
| 2751 |
std::unique_ptr<Type> return_type; |
| 2752 |
std::unique_ptr<BlockExpr> |
| 2753 |
expr; // only used because may be polymorphic in future |
| 2754 |
|
| 2755 |
public: |
| 2756 |
std::string as_string () const override; |
| 2757 |
|
| 2758 |
// Constructor potentially with a move |
| 2759 |
ClosureExprInnerTyped (std::unique_ptr<Type> closure_return_type, |
| 2760 |
std::unique_ptr<BlockExpr> closure_expr, |
| 2761 |
std::vector<ClosureParam> closure_params, |
| 2762 |
location_t locus, bool is_move = false, |
| 2763 |
std::vector<Attribute> outer_attribs |
| 2764 |
= std::vector<Attribute> ()) |
| 2765 |
: ClosureExpr (std::move (closure_params), is_move, |
| 2766 |
std::move (outer_attribs), locus), |
| 2767 |
return_type (std::move (closure_return_type)), |
| 2768 |
expr (std::move (closure_expr)) |
| 2769 |
{} |
| 2770 |
|
| 2771 |
// Copy constructor requires cloning |
| 2772 |
ClosureExprInnerTyped (ClosureExprInnerTyped const &other) |
| 2773 |
: ClosureExpr (other) |
| 2774 |
{ |
| 2775 |
// guard to prevent null dereference (only required if error state) |
| 2776 |
if (other.expr != nullptr) |
| 2777 |
expr = other.expr->clone_block_expr (); |
| 2778 |
if (other.return_type != nullptr) |
| 2779 |
return_type = other.return_type->clone_type (); |
| 2780 |
} |
| 2781 |
|
| 2782 |
// Overload assignment operator to clone unique_ptrs |
| 2783 |
ClosureExprInnerTyped &operator= (ClosureExprInnerTyped const &other) |
| 2784 |
{ |
| 2785 |
ClosureExpr::operator= (other); |
| 2786 |
// params = other.params; |
| 2787 |
// has_move = other.has_move; |
| 2788 |
// outer_attrs = other.outer_attrs; |
| 2789 |
|
| 2790 |
// guard to prevent null dereference (only required if error state) |
| 2791 |
if (other.expr != nullptr) |
| 2792 |
expr = other.expr->clone_block_expr (); |
| 2793 |
else |
| 2794 |
expr = nullptr; |
| 2795 |
if (other.return_type != nullptr) |
| 2796 |
return_type = other.return_type->clone_type (); |
| 2797 |
else |
| 2798 |
return_type = nullptr; |
| 2799 |
|
| 2800 |
return *this; |
| 2801 |
} |
| 2802 |
|
| 2803 |
// move constructors |
| 2804 |
ClosureExprInnerTyped (ClosureExprInnerTyped &&other) = default; |
| 2805 |
ClosureExprInnerTyped &operator= (ClosureExprInnerTyped &&other) = default; |
| 2806 |
|
| 2807 |
void accept_vis (ASTVisitor &vis) override; |
| 2808 |
|
| 2809 |
/* Invalid if inner expr is null, so base stripping on that. Technically, |
| 2810 |
* type should also not be null. */ |
| 2811 |
void mark_for_strip () override { expr = nullptr; } |
| 2812 |
bool is_marked_for_strip () const override { return expr == nullptr; } |
| 2813 |
|
| 2814 |
// TODO: is this better? Or is a "vis_block" better? |
| 2815 |
BlockExpr &get_definition_block () |
| 2816 |
{ |
| 2817 |
rust_assert (expr != nullptr); |
| 2818 |
return *expr; |
| 2819 |
} |
| 2820 |
|
| 2821 |
// TODO: is this better? Or is a "vis_block" better? |
| 2822 |
Type &get_return_type () |
| 2823 |
{ |
| 2824 |
rust_assert (return_type != nullptr); |
| 2825 |
return *return_type; |
| 2826 |
} |
| 2827 |
|
| 2828 |
std::unique_ptr<Type> &get_return_type_ptr () |
| 2829 |
{ |
| 2830 |
rust_assert (return_type != nullptr); |
| 2831 |
return return_type; |
| 2832 |
} |
| 2833 |
|
| 2834 |
protected: |
| 2835 |
/* Use covariance to implement clone function as returning this object rather |
| 2836 |
* than base */ |
| 2837 |
ClosureExprInnerTyped *clone_expr_without_block_impl () const override |
| 2838 |
{ |
| 2839 |
return new ClosureExprInnerTyped (*this); |
| 2840 |
} |
| 2841 |
}; |
| 2842 |
|
| 2843 |
// AST node representing continue expression within loops |
| 2844 |
class ContinueExpr : public ExprWithoutBlock |
| 2845 |
{ |
| 2846 |
std::vector<Attribute> outer_attrs; |
| 2847 |
tl::optional<Lifetime> label; |
| 2848 |
location_t locus; |
| 2849 |
|
| 2850 |
// TODO: find another way to store this to save memory? |
| 2851 |
bool marked_for_strip = false; |
| 2852 |
|
| 2853 |
public: |
| 2854 |
std::string as_string () const override; |
| 2855 |
|
| 2856 |
// Returns true if the continue expr has a label. |
| 2857 |
bool has_label () const { return label.has_value (); } |
| 2858 |
|
| 2859 |
// Constructor for a ContinueExpr with a label. |
| 2860 |
ContinueExpr (tl::optional<Lifetime> label, |
| 2861 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 2862 |
: outer_attrs (std::move (outer_attribs)), label (std::move (label)), |
| 2863 |
locus (locus) |
| 2864 |
{} |
| 2865 |
|
| 2866 |
location_t get_locus () const override final { return locus; } |
| 2867 |
|
| 2868 |
void accept_vis (ASTVisitor &vis) override; |
| 2869 |
|
| 2870 |
// Can't think of any invalid invariants, so store boolean. |
| 2871 |
void mark_for_strip () override { marked_for_strip = true; } |
| 2872 |
bool is_marked_for_strip () const override { return marked_for_strip; } |
| 2873 |
|
| 2874 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 2875 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 2876 |
|
| 2877 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 2878 |
{ |
| 2879 |
outer_attrs = std::move (new_attrs); |
| 2880 |
} |
| 2881 |
|
| 2882 |
Lifetime &get_label_unchecked () { return label.value (); } |
| 2883 |
const Lifetime &get_label_unchecked () const { return label.value (); } |
| 2884 |
|
| 2885 |
tl::optional<Lifetime> &get_label () { return label; } |
| 2886 |
const tl::optional<Lifetime> &get_label () const { return label; } |
| 2887 |
|
| 2888 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Continue; } |
| 2889 |
|
| 2890 |
protected: |
| 2891 |
/* Use covariance to implement clone function as returning this object rather |
| 2892 |
* than base */ |
| 2893 |
ContinueExpr *clone_expr_without_block_impl () const override |
| 2894 |
{ |
| 2895 |
return new ContinueExpr (*this); |
| 2896 |
} |
| 2897 |
}; |
| 2898 |
// TODO: merge "break" and "continue"? Or even merge in "return"? |
| 2899 |
|
| 2900 |
// AST node representing break expression within loops |
| 2901 |
class BreakExpr : public ExprWithoutBlock |
| 2902 |
{ |
| 2903 |
std::vector<Attribute> outer_attrs; |
| 2904 |
tl::optional<LoopLabel> label; |
| 2905 |
std::unique_ptr<Expr> break_expr; |
| 2906 |
location_t locus; |
| 2907 |
|
| 2908 |
// TODO: find another way to store this to save memory? |
| 2909 |
bool marked_for_strip = false; |
| 2910 |
|
| 2911 |
public: |
| 2912 |
std::string as_string () const override; |
| 2913 |
|
| 2914 |
// Returns whether the break expression has a label or not. |
| 2915 |
bool has_label () const { return label.has_value (); } |
| 2916 |
|
| 2917 |
/* Returns whether the break expression has an expression used in the break or |
| 2918 |
* not. */ |
| 2919 |
bool has_break_expr () const { return break_expr != nullptr; } |
| 2920 |
|
| 2921 |
// Constructor for a break expression |
| 2922 |
BreakExpr (tl::optional<LoopLabel> break_label, |
| 2923 |
std::unique_ptr<Expr> expr_in_break, |
| 2924 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 2925 |
: outer_attrs (std::move (outer_attribs)), label (std::move (break_label)), |
| 2926 |
break_expr (std::move (expr_in_break)), locus (locus) |
| 2927 |
{} |
| 2928 |
|
| 2929 |
// Copy constructor defined to use clone for unique pointer |
| 2930 |
BreakExpr (BreakExpr const &other) |
| 2931 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 2932 |
label (other.label), locus (other.locus), |
| 2933 |
marked_for_strip (other.marked_for_strip) |
| 2934 |
{ |
| 2935 |
// guard to protect from null pointer dereference |
| 2936 |
if (other.break_expr != nullptr) |
| 2937 |
break_expr = other.break_expr->clone_expr (); |
| 2938 |
} |
| 2939 |
|
| 2940 |
// Overload assignment operator to clone unique pointer |
| 2941 |
BreakExpr &operator= (BreakExpr const &other) |
| 2942 |
{ |
| 2943 |
ExprWithoutBlock::operator= (other); |
| 2944 |
label = other.label; |
| 2945 |
locus = other.locus; |
| 2946 |
marked_for_strip = other.marked_for_strip; |
| 2947 |
outer_attrs = other.outer_attrs; |
| 2948 |
|
| 2949 |
// guard to protect from null pointer dereference |
| 2950 |
if (other.break_expr != nullptr) |
| 2951 |
break_expr = other.break_expr->clone_expr (); |
| 2952 |
else |
| 2953 |
break_expr = nullptr; |
| 2954 |
|
| 2955 |
return *this; |
| 2956 |
} |
| 2957 |
|
| 2958 |
// move constructors |
| 2959 |
BreakExpr (BreakExpr &&other) = default; |
| 2960 |
BreakExpr &operator= (BreakExpr &&other) = default; |
| 2961 |
|
| 2962 |
location_t get_locus () const override final { return locus; } |
| 2963 |
|
| 2964 |
void accept_vis (ASTVisitor &vis) override; |
| 2965 |
|
| 2966 |
// Can't think of any invalid invariants, so store boolean. |
| 2967 |
void mark_for_strip () override { marked_for_strip = true; } |
| 2968 |
bool is_marked_for_strip () const override { return marked_for_strip; } |
| 2969 |
|
| 2970 |
// TODO: is this better? Or is a "vis_block" better? |
| 2971 |
Expr &get_break_expr () |
| 2972 |
{ |
| 2973 |
rust_assert (has_break_expr ()); |
| 2974 |
return *break_expr; |
| 2975 |
} |
| 2976 |
|
| 2977 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 2978 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 2979 |
|
| 2980 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 2981 |
{ |
| 2982 |
outer_attrs = std::move (new_attrs); |
| 2983 |
} |
| 2984 |
|
| 2985 |
LoopLabel &get_label_unchecked () { return label.value (); } |
| 2986 |
const LoopLabel &get_label_unchecked () const { return label.value (); } |
| 2987 |
|
| 2988 |
tl::optional<LoopLabel> &get_label () { return label; } |
| 2989 |
const tl::optional<LoopLabel> &get_label () const { return label; } |
| 2990 |
|
| 2991 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Break; } |
| 2992 |
|
| 2993 |
protected: |
| 2994 |
/* Use covariance to implement clone function as returning this object rather |
| 2995 |
* than base */ |
| 2996 |
BreakExpr *clone_expr_without_block_impl () const override |
| 2997 |
{ |
| 2998 |
return new BreakExpr (*this); |
| 2999 |
} |
| 3000 |
}; |
| 3001 |
|
| 3002 |
// Base range expression AST node object - abstract |
| 3003 |
class RangeExpr : public ExprWithoutBlock |
| 3004 |
{ |
| 3005 |
location_t locus; |
| 3006 |
|
| 3007 |
// Some visitors still check for attributes on RangeExprs, and they will need |
| 3008 |
// to be supported in the future - so keep that for now |
| 3009 |
std::vector<Attribute> empty_attributes = {}; |
| 3010 |
|
| 3011 |
protected: |
| 3012 |
// outer attributes not allowed before range expressions |
| 3013 |
RangeExpr (location_t locus) : locus (locus) {} |
| 3014 |
|
| 3015 |
public: |
| 3016 |
location_t get_locus () const override final { return locus; } |
| 3017 |
|
| 3018 |
std::vector<Attribute> &get_outer_attrs () override final |
| 3019 |
{ |
| 3020 |
return empty_attributes; |
| 3021 |
} |
| 3022 |
|
| 3023 |
// should never be called - error if called |
| 3024 |
void set_outer_attrs (std::vector<Attribute> /* new_attrs */) override {} |
| 3025 |
|
| 3026 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Range; } |
| 3027 |
}; |
| 3028 |
|
| 3029 |
// Range from (inclusive) and to (exclusive) expression AST node object |
| 3030 |
// aka RangeExpr; constructs a std::ops::Range object |
| 3031 |
class RangeFromToExpr : public RangeExpr |
| 3032 |
{ |
| 3033 |
std::unique_ptr<Expr> from; |
| 3034 |
std::unique_ptr<Expr> to; |
| 3035 |
|
| 3036 |
public: |
| 3037 |
std::string as_string () const override; |
| 3038 |
|
| 3039 |
RangeFromToExpr (std::unique_ptr<Expr> range_from, |
| 3040 |
std::unique_ptr<Expr> range_to, location_t locus) |
| 3041 |
: RangeExpr (locus), from (std::move (range_from)), |
| 3042 |
to (std::move (range_to)) |
| 3043 |
{} |
| 3044 |
|
| 3045 |
// Copy constructor with cloning |
| 3046 |
RangeFromToExpr (RangeFromToExpr const &other) : RangeExpr (other) |
| 3047 |
{ |
| 3048 |
// guard to prevent null dereference (only required if error state) |
| 3049 |
if (other.from != nullptr) |
| 3050 |
from = other.from->clone_expr (); |
| 3051 |
if (other.to != nullptr) |
| 3052 |
to = other.to->clone_expr (); |
| 3053 |
} |
| 3054 |
|
| 3055 |
// Overload assignment operator to clone unique pointers |
| 3056 |
RangeFromToExpr &operator= (RangeFromToExpr const &other) |
| 3057 |
{ |
| 3058 |
RangeExpr::operator= (other); |
| 3059 |
|
| 3060 |
// guard to prevent null dereference (only required if error state) |
| 3061 |
if (other.from != nullptr) |
| 3062 |
from = other.from->clone_expr (); |
| 3063 |
else |
| 3064 |
from = nullptr; |
| 3065 |
if (other.to != nullptr) |
| 3066 |
to = other.to->clone_expr (); |
| 3067 |
else |
| 3068 |
to = nullptr; |
| 3069 |
|
| 3070 |
return *this; |
| 3071 |
} |
| 3072 |
|
| 3073 |
// move constructors |
| 3074 |
RangeFromToExpr (RangeFromToExpr &&other) = default; |
| 3075 |
RangeFromToExpr &operator= (RangeFromToExpr &&other) = default; |
| 3076 |
|
| 3077 |
void accept_vis (ASTVisitor &vis) override; |
| 3078 |
|
| 3079 |
// Invalid if either expr is null, so base stripping on that. |
| 3080 |
void mark_for_strip () override |
| 3081 |
{ |
| 3082 |
from = nullptr; |
| 3083 |
to = nullptr; |
| 3084 |
} |
| 3085 |
bool is_marked_for_strip () const override |
| 3086 |
{ |
| 3087 |
return from == nullptr && to == nullptr; |
| 3088 |
} |
| 3089 |
|
| 3090 |
// TODO: is this better? Or is a "vis_block" better? |
| 3091 |
Expr &get_from_expr () |
| 3092 |
{ |
| 3093 |
rust_assert (from != nullptr); |
| 3094 |
return *from; |
| 3095 |
} |
| 3096 |
|
| 3097 |
// TODO: is this better? Or is a "vis_block" better? |
| 3098 |
Expr &get_to_expr () |
| 3099 |
{ |
| 3100 |
rust_assert (to != nullptr); |
| 3101 |
return *to; |
| 3102 |
} |
| 3103 |
|
| 3104 |
protected: |
| 3105 |
/* Use covariance to implement clone function as returning this object rather |
| 3106 |
* than base */ |
| 3107 |
RangeFromToExpr *clone_expr_without_block_impl () const override |
| 3108 |
{ |
| 3109 |
return new RangeFromToExpr (*this); |
| 3110 |
} |
| 3111 |
}; |
| 3112 |
|
| 3113 |
// Range from (inclusive) expression AST node object |
| 3114 |
// constructs a std::ops::RangeFrom object |
| 3115 |
class RangeFromExpr : public RangeExpr |
| 3116 |
{ |
| 3117 |
std::unique_ptr<Expr> from; |
| 3118 |
|
| 3119 |
public: |
| 3120 |
std::string as_string () const override; |
| 3121 |
|
| 3122 |
RangeFromExpr (std::unique_ptr<Expr> range_from, location_t locus) |
| 3123 |
: RangeExpr (locus), from (std::move (range_from)) |
| 3124 |
{} |
| 3125 |
|
| 3126 |
// Copy constructor with clone |
| 3127 |
RangeFromExpr (RangeFromExpr const &other) : RangeExpr (other) |
| 3128 |
{ |
| 3129 |
// guard to prevent null dereference (only required if error state) |
| 3130 |
if (other.from != nullptr) |
| 3131 |
from = other.from->clone_expr (); |
| 3132 |
} |
| 3133 |
|
| 3134 |
// Overload assignment operator to clone unique_ptr |
| 3135 |
RangeFromExpr &operator= (RangeFromExpr const &other) |
| 3136 |
{ |
| 3137 |
RangeExpr::operator= (other); |
| 3138 |
|
| 3139 |
// guard to prevent null dereference (only required if error state) |
| 3140 |
if (other.from != nullptr) |
| 3141 |
from = other.from->clone_expr (); |
| 3142 |
else |
| 3143 |
from = nullptr; |
| 3144 |
|
| 3145 |
return *this; |
| 3146 |
} |
| 3147 |
|
| 3148 |
// move constructors |
| 3149 |
RangeFromExpr (RangeFromExpr &&other) = default; |
| 3150 |
RangeFromExpr &operator= (RangeFromExpr &&other) = default; |
| 3151 |
|
| 3152 |
void accept_vis (ASTVisitor &vis) override; |
| 3153 |
|
| 3154 |
// Invalid if expr is null, so base stripping on that. |
| 3155 |
void mark_for_strip () override { from = nullptr; } |
| 3156 |
bool is_marked_for_strip () const override { return from == nullptr; } |
| 3157 |
|
| 3158 |
// TODO: is this better? Or is a "vis_block" better? |
| 3159 |
Expr &get_from_expr () |
| 3160 |
{ |
| 3161 |
rust_assert (from != nullptr); |
| 3162 |
return *from; |
| 3163 |
} |
| 3164 |
|
| 3165 |
protected: |
| 3166 |
/* Use covariance to implement clone function as returning this object rather |
| 3167 |
* than base */ |
| 3168 |
RangeFromExpr *clone_expr_without_block_impl () const override |
| 3169 |
{ |
| 3170 |
return new RangeFromExpr (*this); |
| 3171 |
} |
| 3172 |
}; |
| 3173 |
|
| 3174 |
// Range to (exclusive) expression AST node object |
| 3175 |
// constructs a std::ops::RangeTo object |
| 3176 |
class RangeToExpr : public RangeExpr |
| 3177 |
{ |
| 3178 |
std::unique_ptr<Expr> to; |
| 3179 |
|
| 3180 |
public: |
| 3181 |
std::string as_string () const override; |
| 3182 |
|
| 3183 |
// outer attributes not allowed |
| 3184 |
RangeToExpr (std::unique_ptr<Expr> range_to, location_t locus) |
| 3185 |
: RangeExpr (locus), to (std::move (range_to)) |
| 3186 |
{} |
| 3187 |
|
| 3188 |
// Copy constructor with clone |
| 3189 |
RangeToExpr (RangeToExpr const &other) : RangeExpr (other) |
| 3190 |
{ |
| 3191 |
// guard to prevent null dereference (only required if error state) |
| 3192 |
if (other.to != nullptr) |
| 3193 |
to = other.to->clone_expr (); |
| 3194 |
} |
| 3195 |
|
| 3196 |
// Overload assignment operator to clone unique_ptr |
| 3197 |
RangeToExpr &operator= (RangeToExpr const &other) |
| 3198 |
{ |
| 3199 |
RangeExpr::operator= (other); |
| 3200 |
|
| 3201 |
// guard to prevent null dereference (only required if error state) |
| 3202 |
if (other.to != nullptr) |
| 3203 |
to = other.to->clone_expr (); |
| 3204 |
else |
| 3205 |
to = nullptr; |
| 3206 |
|
| 3207 |
return *this; |
| 3208 |
} |
| 3209 |
|
| 3210 |
// move constructors |
| 3211 |
RangeToExpr (RangeToExpr &&other) = default; |
| 3212 |
RangeToExpr &operator= (RangeToExpr &&other) = default; |
| 3213 |
|
| 3214 |
void accept_vis (ASTVisitor &vis) override; |
| 3215 |
|
| 3216 |
// Invalid if expr is null, so base stripping on that. |
| 3217 |
void mark_for_strip () override { to = nullptr; } |
| 3218 |
bool is_marked_for_strip () const override { return to == nullptr; } |
| 3219 |
|
| 3220 |
// TODO: is this better? Or is a "vis_block" better? |
| 3221 |
Expr &get_to_expr () |
| 3222 |
{ |
| 3223 |
rust_assert (to != nullptr); |
| 3224 |
return *to; |
| 3225 |
} |
| 3226 |
|
| 3227 |
protected: |
| 3228 |
/* Use covariance to implement clone function as returning this object rather |
| 3229 |
* than base */ |
| 3230 |
RangeToExpr *clone_expr_without_block_impl () const override |
| 3231 |
{ |
| 3232 |
return new RangeToExpr (*this); |
| 3233 |
} |
| 3234 |
}; |
| 3235 |
|
| 3236 |
// Full range expression AST node object |
| 3237 |
// constructs a std::ops::RangeFull object |
| 3238 |
class RangeFullExpr : public RangeExpr |
| 3239 |
{ |
| 3240 |
// TODO: find another way to store this to save memory? |
| 3241 |
bool marked_for_strip = false; |
| 3242 |
|
| 3243 |
public: |
| 3244 |
std::string as_string () const override; |
| 3245 |
|
| 3246 |
RangeFullExpr (location_t locus) : RangeExpr (locus) {} |
| 3247 |
// outer attributes not allowed |
| 3248 |
|
| 3249 |
void accept_vis (ASTVisitor &vis) override; |
| 3250 |
|
| 3251 |
// Can't think of any invalid invariants, so store boolean. |
| 3252 |
void mark_for_strip () override { marked_for_strip = true; } |
| 3253 |
bool is_marked_for_strip () const override { return marked_for_strip; } |
| 3254 |
|
| 3255 |
protected: |
| 3256 |
/* Use covariance to implement clone function as returning this object rather |
| 3257 |
* than base */ |
| 3258 |
RangeFullExpr *clone_expr_without_block_impl () const override |
| 3259 |
{ |
| 3260 |
return new RangeFullExpr (*this); |
| 3261 |
} |
| 3262 |
}; |
| 3263 |
|
| 3264 |
// Range from (inclusive) and to (inclusive) expression AST node object |
| 3265 |
// aka RangeInclusiveExpr; constructs a std::ops::RangeInclusive object |
| 3266 |
class RangeFromToInclExpr : public RangeExpr |
| 3267 |
{ |
| 3268 |
std::unique_ptr<Expr> from; |
| 3269 |
std::unique_ptr<Expr> to; |
| 3270 |
|
| 3271 |
public: |
| 3272 |
std::string as_string () const override; |
| 3273 |
|
| 3274 |
RangeFromToInclExpr (std::unique_ptr<Expr> range_from, |
| 3275 |
std::unique_ptr<Expr> range_to, location_t locus) |
| 3276 |
: RangeExpr (locus), from (std::move (range_from)), |
| 3277 |
to (std::move (range_to)) |
| 3278 |
{} |
| 3279 |
// outer attributes not allowed |
| 3280 |
|
| 3281 |
// Copy constructor with clone |
| 3282 |
RangeFromToInclExpr (RangeFromToInclExpr const &other) : RangeExpr (other) |
| 3283 |
{ |
| 3284 |
// guard to prevent null dereference (only required if error state) |
| 3285 |
if (other.from != nullptr) |
| 3286 |
from = other.from->clone_expr (); |
| 3287 |
if (other.to != nullptr) |
| 3288 |
to = other.to->clone_expr (); |
| 3289 |
} |
| 3290 |
|
| 3291 |
// Overload assignment operator to use clone |
| 3292 |
RangeFromToInclExpr &operator= (RangeFromToInclExpr const &other) |
| 3293 |
{ |
| 3294 |
RangeExpr::operator= (other); |
| 3295 |
|
| 3296 |
// guard to prevent null dereference (only required if error state) |
| 3297 |
if (other.from != nullptr) |
| 3298 |
from = other.from->clone_expr (); |
| 3299 |
else |
| 3300 |
from = nullptr; |
| 3301 |
if (other.to != nullptr) |
| 3302 |
to = other.to->clone_expr (); |
| 3303 |
else |
| 3304 |
to = nullptr; |
| 3305 |
|
| 3306 |
return *this; |
| 3307 |
} |
| 3308 |
|
| 3309 |
// move constructors |
| 3310 |
RangeFromToInclExpr (RangeFromToInclExpr &&other) = default; |
| 3311 |
RangeFromToInclExpr &operator= (RangeFromToInclExpr &&other) = default; |
| 3312 |
|
| 3313 |
void accept_vis (ASTVisitor &vis) override; |
| 3314 |
|
| 3315 |
// Invalid if either expr is null, so base stripping on that. |
| 3316 |
void mark_for_strip () override |
| 3317 |
{ |
| 3318 |
from = nullptr; |
| 3319 |
to = nullptr; |
| 3320 |
} |
| 3321 |
bool is_marked_for_strip () const override |
| 3322 |
{ |
| 3323 |
return from == nullptr && to == nullptr; |
| 3324 |
} |
| 3325 |
|
| 3326 |
// TODO: is this better? Or is a "vis_block" better? |
| 3327 |
Expr &get_from_expr () |
| 3328 |
{ |
| 3329 |
rust_assert (from != nullptr); |
| 3330 |
return *from; |
| 3331 |
} |
| 3332 |
|
| 3333 |
// TODO: is this better? Or is a "vis_block" better? |
| 3334 |
Expr &get_to_expr () |
| 3335 |
{ |
| 3336 |
rust_assert (to != nullptr); |
| 3337 |
return *to; |
| 3338 |
} |
| 3339 |
|
| 3340 |
protected: |
| 3341 |
/* Use covariance to implement clone function as returning this object rather |
| 3342 |
* than base */ |
| 3343 |
RangeFromToInclExpr *clone_expr_without_block_impl () const override |
| 3344 |
{ |
| 3345 |
return new RangeFromToInclExpr (*this); |
| 3346 |
} |
| 3347 |
}; |
| 3348 |
|
| 3349 |
// Range to (inclusive) expression AST node object |
| 3350 |
// aka RangeToInclusiveExpr; constructs a std::ops::RangeToInclusive object |
| 3351 |
class RangeToInclExpr : public RangeExpr |
| 3352 |
{ |
| 3353 |
std::unique_ptr<Expr> to; |
| 3354 |
|
| 3355 |
public: |
| 3356 |
std::string as_string () const override; |
| 3357 |
|
| 3358 |
RangeToInclExpr (std::unique_ptr<Expr> range_to, location_t locus) |
| 3359 |
: RangeExpr (locus), to (std::move (range_to)) |
| 3360 |
{} |
| 3361 |
// outer attributes not allowed |
| 3362 |
|
| 3363 |
// Copy constructor with clone |
| 3364 |
RangeToInclExpr (RangeToInclExpr const &other) : RangeExpr (other) |
| 3365 |
{ |
| 3366 |
// guard to prevent null dereference (only required if error state) |
| 3367 |
if (other.to != nullptr) |
| 3368 |
to = other.to->clone_expr (); |
| 3369 |
} |
| 3370 |
|
| 3371 |
// Overload assignment operator to clone pointer |
| 3372 |
RangeToInclExpr &operator= (RangeToInclExpr const &other) |
| 3373 |
{ |
| 3374 |
RangeExpr::operator= (other); |
| 3375 |
|
| 3376 |
// guard to prevent null dereference (only required if error state) |
| 3377 |
if (other.to != nullptr) |
| 3378 |
to = other.to->clone_expr (); |
| 3379 |
else |
| 3380 |
to = nullptr; |
| 3381 |
|
| 3382 |
return *this; |
| 3383 |
} |
| 3384 |
|
| 3385 |
// move constructors |
| 3386 |
RangeToInclExpr (RangeToInclExpr &&other) = default; |
| 3387 |
RangeToInclExpr &operator= (RangeToInclExpr &&other) = default; |
| 3388 |
|
| 3389 |
void accept_vis (ASTVisitor &vis) override; |
| 3390 |
|
| 3391 |
// Invalid if expr is null, so base stripping on that. |
| 3392 |
void mark_for_strip () override { to = nullptr; } |
| 3393 |
bool is_marked_for_strip () const override { return to == nullptr; } |
| 3394 |
|
| 3395 |
// TODO: is this better? Or is a "vis_block" better? |
| 3396 |
Expr &get_to_expr () |
| 3397 |
{ |
| 3398 |
rust_assert (to != nullptr); |
| 3399 |
return *to; |
| 3400 |
} |
| 3401 |
|
| 3402 |
protected: |
| 3403 |
/* Use covariance to implement clone function as returning this object rather |
| 3404 |
* than base */ |
| 3405 |
RangeToInclExpr *clone_expr_without_block_impl () const override |
| 3406 |
{ |
| 3407 |
return new RangeToInclExpr (*this); |
| 3408 |
} |
| 3409 |
}; |
| 3410 |
|
| 3411 |
class BoxExpr : public ExprWithoutBlock |
| 3412 |
{ |
| 3413 |
std::unique_ptr<Expr> expr; |
| 3414 |
std::vector<Attribute> outer_attrs; |
| 3415 |
location_t locus; |
| 3416 |
|
| 3417 |
public: |
| 3418 |
BoxExpr (std::unique_ptr<Expr> expr, std::vector<Attribute> outer_attrs, |
| 3419 |
location_t locus) |
| 3420 |
: expr (std::move (expr)), outer_attrs (outer_attrs), locus (locus) |
| 3421 |
{} |
| 3422 |
|
| 3423 |
// Copy constructor with clone |
| 3424 |
BoxExpr (BoxExpr const &other) |
| 3425 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 3426 |
locus (other.locus) |
| 3427 |
{ |
| 3428 |
// guard to protect from null pointer dereference |
| 3429 |
if (other.expr != nullptr) |
| 3430 |
expr = other.expr->clone_expr (); |
| 3431 |
} |
| 3432 |
|
| 3433 |
BoxExpr &operator= (BoxExpr const &other) |
| 3434 |
{ |
| 3435 |
ExprWithoutBlock::operator= (other); |
| 3436 |
locus = other.locus; |
| 3437 |
outer_attrs = other.outer_attrs; |
| 3438 |
|
| 3439 |
// guard to protect from null pointer dereference |
| 3440 |
if (other.expr != nullptr) |
| 3441 |
expr = other.expr->clone_expr (); |
| 3442 |
else |
| 3443 |
expr = nullptr; |
| 3444 |
|
| 3445 |
return *this; |
| 3446 |
} |
| 3447 |
|
| 3448 |
// move constructors |
| 3449 |
BoxExpr (BoxExpr &&other) = default; |
| 3450 |
BoxExpr &operator= (BoxExpr &&other) = default; |
| 3451 |
|
| 3452 |
location_t get_locus () const override final { return locus; } |
| 3453 |
|
| 3454 |
void accept_vis (ASTVisitor &vis) override; |
| 3455 |
|
| 3456 |
void mark_for_strip () override { expr = nullptr; } |
| 3457 |
bool is_marked_for_strip () const override { return expr == nullptr; } |
| 3458 |
|
| 3459 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 3460 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 3461 |
|
| 3462 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 3463 |
{ |
| 3464 |
outer_attrs = std::move (new_attrs); |
| 3465 |
} |
| 3466 |
|
| 3467 |
std::string as_string () const override; |
| 3468 |
|
| 3469 |
Expr &get_boxed_expr () |
| 3470 |
{ |
| 3471 |
rust_assert (expr != nullptr); |
| 3472 |
return *expr; |
| 3473 |
} |
| 3474 |
|
| 3475 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Box; } |
| 3476 |
|
| 3477 |
protected: |
| 3478 |
/* Use covariance to implement clone function as returning this object rather |
| 3479 |
* than base */ |
| 3480 |
BoxExpr *clone_expr_without_block_impl () const override |
| 3481 |
{ |
| 3482 |
return new BoxExpr (*this); |
| 3483 |
} |
| 3484 |
}; |
| 3485 |
|
| 3486 |
// Return expression AST node representation |
| 3487 |
class ReturnExpr : public ExprWithoutBlock |
| 3488 |
{ |
| 3489 |
std::vector<Attribute> outer_attrs; |
| 3490 |
std::unique_ptr<Expr> return_expr; |
| 3491 |
location_t locus; |
| 3492 |
|
| 3493 |
// TODO: find another way to store this to save memory? |
| 3494 |
bool marked_for_strip = false; |
| 3495 |
|
| 3496 |
public: |
| 3497 |
std::string as_string () const override; |
| 3498 |
|
| 3499 |
/* Returns whether the object has an expression returned (i.e. not void return |
| 3500 |
* type). */ |
| 3501 |
bool has_returned_expr () const { return return_expr != nullptr; } |
| 3502 |
|
| 3503 |
// Constructor for ReturnExpr. |
| 3504 |
ReturnExpr (std::unique_ptr<Expr> returned_expr, |
| 3505 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 3506 |
: outer_attrs (std::move (outer_attribs)), |
| 3507 |
return_expr (std::move (returned_expr)), locus (locus) |
| 3508 |
{} |
| 3509 |
|
| 3510 |
// Copy constructor with clone |
| 3511 |
ReturnExpr (ReturnExpr const &other) |
| 3512 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 3513 |
locus (other.locus), marked_for_strip (other.marked_for_strip) |
| 3514 |
{ |
| 3515 |
// guard to protect from null pointer dereference |
| 3516 |
if (other.return_expr != nullptr) |
| 3517 |
return_expr = other.return_expr->clone_expr (); |
| 3518 |
} |
| 3519 |
|
| 3520 |
// Overloaded assignment operator to clone return_expr pointer |
| 3521 |
ReturnExpr &operator= (ReturnExpr const &other) |
| 3522 |
{ |
| 3523 |
ExprWithoutBlock::operator= (other); |
| 3524 |
locus = other.locus; |
| 3525 |
marked_for_strip = other.marked_for_strip; |
| 3526 |
outer_attrs = other.outer_attrs; |
| 3527 |
|
| 3528 |
// guard to protect from null pointer dereference |
| 3529 |
if (other.return_expr != nullptr) |
| 3530 |
return_expr = other.return_expr->clone_expr (); |
| 3531 |
else |
| 3532 |
return_expr = nullptr; |
| 3533 |
|
| 3534 |
return *this; |
| 3535 |
} |
| 3536 |
|
| 3537 |
// move constructors |
| 3538 |
ReturnExpr (ReturnExpr &&other) = default; |
| 3539 |
ReturnExpr &operator= (ReturnExpr &&other) = default; |
| 3540 |
|
| 3541 |
location_t get_locus () const override final { return locus; } |
| 3542 |
|
| 3543 |
void accept_vis (ASTVisitor &vis) override; |
| 3544 |
|
| 3545 |
// Can't think of any invalid invariants, so store boolean. |
| 3546 |
void mark_for_strip () override { marked_for_strip = true; } |
| 3547 |
bool is_marked_for_strip () const override { return marked_for_strip; } |
| 3548 |
|
| 3549 |
// TODO: is this better? Or is a "vis_block" better? |
| 3550 |
Expr &get_returned_expr () |
| 3551 |
{ |
| 3552 |
rust_assert (return_expr != nullptr); |
| 3553 |
return *return_expr; |
| 3554 |
} |
| 3555 |
|
| 3556 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 3557 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 3558 |
|
| 3559 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 3560 |
{ |
| 3561 |
outer_attrs = std::move (new_attrs); |
| 3562 |
} |
| 3563 |
|
| 3564 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Return; } |
| 3565 |
|
| 3566 |
protected: |
| 3567 |
/* Use covariance to implement clone function as returning this object rather |
| 3568 |
* than base */ |
| 3569 |
ReturnExpr *clone_expr_without_block_impl () const override |
| 3570 |
{ |
| 3571 |
return new ReturnExpr (*this); |
| 3572 |
} |
| 3573 |
}; |
| 3574 |
|
| 3575 |
// Forward decl - defined in rust-macro.h |
| 3576 |
class MacroInvocation; |
| 3577 |
|
| 3578 |
// An unsafe block AST node |
| 3579 |
class UnsafeBlockExpr : public ExprWithBlock |
| 3580 |
{ |
| 3581 |
std::vector<Attribute> outer_attrs; |
| 3582 |
// Or just have it extend BlockExpr |
| 3583 |
std::unique_ptr<BlockExpr> expr; |
| 3584 |
location_t locus; |
| 3585 |
|
| 3586 |
public: |
| 3587 |
std::string as_string () const override; |
| 3588 |
|
| 3589 |
UnsafeBlockExpr (std::unique_ptr<BlockExpr> block_expr, |
| 3590 |
std::vector<Attribute> outer_attribs, location_t locus) |
| 3591 |
: outer_attrs (std::move (outer_attribs)), expr (std::move (block_expr)), |
| 3592 |
locus (locus) |
| 3593 |
{} |
| 3594 |
|
| 3595 |
// Copy constructor with clone |
| 3596 |
UnsafeBlockExpr (UnsafeBlockExpr const &other) |
| 3597 |
: ExprWithBlock (other), outer_attrs (other.outer_attrs), |
| 3598 |
locus (other.locus) |
| 3599 |
{ |
| 3600 |
// guard to prevent null dereference (only required if error state) |
| 3601 |
if (other.expr != nullptr) |
| 3602 |
expr = other.expr->clone_block_expr (); |
| 3603 |
} |
| 3604 |
|
| 3605 |
// Overloaded assignment operator to clone |
| 3606 |
UnsafeBlockExpr &operator= (UnsafeBlockExpr const &other) |
| 3607 |
{ |
| 3608 |
ExprWithBlock::operator= (other); |
| 3609 |
locus = other.locus; |
| 3610 |
outer_attrs = other.outer_attrs; |
| 3611 |
|
| 3612 |
// guard to prevent null dereference (only required if error state) |
| 3613 |
if (other.expr != nullptr) |
| 3614 |
expr = other.expr->clone_block_expr (); |
| 3615 |
else |
| 3616 |
expr = nullptr; |
| 3617 |
|
| 3618 |
return *this; |
| 3619 |
} |
| 3620 |
|
| 3621 |
// move constructors |
| 3622 |
UnsafeBlockExpr (UnsafeBlockExpr &&other) = default; |
| 3623 |
UnsafeBlockExpr &operator= (UnsafeBlockExpr &&other) = default; |
| 3624 |
|
| 3625 |
location_t get_locus () const override final { return locus; } |
| 3626 |
|
| 3627 |
void accept_vis (ASTVisitor &vis) override; |
| 3628 |
|
| 3629 |
// Invalid if block is null, so base stripping on that. |
| 3630 |
void mark_for_strip () override { expr = nullptr; } |
| 3631 |
bool is_marked_for_strip () const override { return expr == nullptr; } |
| 3632 |
|
| 3633 |
// TODO: is this better? Or is a "vis_block" better? |
| 3634 |
BlockExpr &get_block_expr () |
| 3635 |
{ |
| 3636 |
rust_assert (expr != nullptr); |
| 3637 |
return *expr; |
| 3638 |
} |
| 3639 |
|
| 3640 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 3641 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 3642 |
|
| 3643 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 3644 |
{ |
| 3645 |
outer_attrs = std::move (new_attrs); |
| 3646 |
} |
| 3647 |
|
| 3648 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::UnsafeBlock; } |
| 3649 |
|
| 3650 |
protected: |
| 3651 |
/* Use covariance to implement clone function as returning this object rather |
| 3652 |
* than base */ |
| 3653 |
UnsafeBlockExpr *clone_expr_with_block_impl () const override |
| 3654 |
{ |
| 3655 |
return new UnsafeBlockExpr (*this); |
| 3656 |
} |
| 3657 |
}; |
| 3658 |
|
| 3659 |
// Base loop expression AST node - aka LoopExpr |
| 3660 |
class BaseLoopExpr : public ExprWithBlock |
| 3661 |
{ |
| 3662 |
protected: |
| 3663 |
// protected to allow subclasses better use of them |
| 3664 |
std::vector<Attribute> outer_attrs; |
| 3665 |
tl::optional<LoopLabel> loop_label; |
| 3666 |
std::unique_ptr<BlockExpr> loop_block; |
| 3667 |
|
| 3668 |
private: |
| 3669 |
location_t locus; |
| 3670 |
|
| 3671 |
protected: |
| 3672 |
// Constructor for BaseLoopExpr |
| 3673 |
BaseLoopExpr (std::unique_ptr<BlockExpr> loop_block, location_t locus, |
| 3674 |
tl::optional<LoopLabel> loop_label = tl::nullopt, |
| 3675 |
std::vector<Attribute> outer_attribs |
| 3676 |
= std::vector<Attribute> ()) |
| 3677 |
: outer_attrs (std::move (outer_attribs)), |
| 3678 |
loop_label (std::move (loop_label)), loop_block (std::move (loop_block)), |
| 3679 |
locus (locus) |
| 3680 |
{} |
| 3681 |
|
| 3682 |
// Copy constructor for BaseLoopExpr with clone |
| 3683 |
BaseLoopExpr (BaseLoopExpr const &other) |
| 3684 |
: ExprWithBlock (other), outer_attrs (other.outer_attrs), |
| 3685 |
loop_label (other.loop_label), locus (other.locus) |
| 3686 |
{ |
| 3687 |
// guard to prevent null dereference (only required if error state) |
| 3688 |
if (other.loop_block != nullptr) |
| 3689 |
loop_block = other.loop_block->clone_block_expr (); |
| 3690 |
} |
| 3691 |
|
| 3692 |
// Overloaded assignment operator to clone |
| 3693 |
BaseLoopExpr &operator= (BaseLoopExpr const &other) |
| 3694 |
{ |
| 3695 |
ExprWithBlock::operator= (other); |
| 3696 |
loop_label = other.loop_label; |
| 3697 |
locus = other.locus; |
| 3698 |
outer_attrs = other.outer_attrs; |
| 3699 |
|
| 3700 |
// guard to prevent null dereference (only required if error state) |
| 3701 |
if (other.loop_block != nullptr) |
| 3702 |
loop_block = other.loop_block->clone_block_expr (); |
| 3703 |
else |
| 3704 |
loop_block = nullptr; |
| 3705 |
|
| 3706 |
return *this; |
| 3707 |
} |
| 3708 |
|
| 3709 |
// move constructors |
| 3710 |
BaseLoopExpr (BaseLoopExpr &&other) = default; |
| 3711 |
BaseLoopExpr &operator= (BaseLoopExpr &&other) = default; |
| 3712 |
|
| 3713 |
public: |
| 3714 |
bool has_loop_label () const { return loop_label.has_value (); } |
| 3715 |
|
| 3716 |
LoopLabel &get_loop_label () { return loop_label.value (); } |
| 3717 |
const LoopLabel &get_loop_label () const { return loop_label.value (); } |
| 3718 |
|
| 3719 |
location_t get_locus () const override final { return locus; } |
| 3720 |
|
| 3721 |
// Invalid if loop block is null, so base stripping on that. |
| 3722 |
void mark_for_strip () override { loop_block = nullptr; } |
| 3723 |
bool is_marked_for_strip () const override { return loop_block == nullptr; } |
| 3724 |
|
| 3725 |
// TODO: is this better? Or is a "vis_block" better? |
| 3726 |
BlockExpr &get_loop_block () |
| 3727 |
{ |
| 3728 |
rust_assert (loop_block != nullptr); |
| 3729 |
return *loop_block; |
| 3730 |
} |
| 3731 |
|
| 3732 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 3733 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 3734 |
|
| 3735 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 3736 |
{ |
| 3737 |
outer_attrs = std::move (new_attrs); |
| 3738 |
} |
| 3739 |
|
| 3740 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Loop; } |
| 3741 |
|
| 3742 |
enum class Kind |
| 3743 |
{ |
| 3744 |
Loop, |
| 3745 |
While, |
| 3746 |
WhileLet, |
| 3747 |
For |
| 3748 |
}; |
| 3749 |
|
| 3750 |
virtual Kind get_loop_kind () const = 0; |
| 3751 |
}; |
| 3752 |
|
| 3753 |
// 'Loop' expression (i.e. the infinite loop) AST node |
| 3754 |
class LoopExpr : public BaseLoopExpr |
| 3755 |
{ |
| 3756 |
public: |
| 3757 |
std::string as_string () const override; |
| 3758 |
|
| 3759 |
// Constructor for LoopExpr |
| 3760 |
LoopExpr (std::unique_ptr<BlockExpr> loop_block, location_t locus, |
| 3761 |
tl::optional<LoopLabel> loop_label = tl::nullopt, |
| 3762 |
std::vector<Attribute> outer_attribs = std::vector<Attribute> ()) |
| 3763 |
: BaseLoopExpr (std::move (loop_block), locus, std::move (loop_label), |
| 3764 |
std::move (outer_attribs)) |
| 3765 |
{} |
| 3766 |
|
| 3767 |
void accept_vis (ASTVisitor &vis) override; |
| 3768 |
|
| 3769 |
BaseLoopExpr::Kind get_loop_kind () const override |
| 3770 |
{ |
| 3771 |
return BaseLoopExpr::Kind::Loop; |
| 3772 |
} |
| 3773 |
|
| 3774 |
protected: |
| 3775 |
/* Use covariance to implement clone function as returning this object rather |
| 3776 |
* than base */ |
| 3777 |
LoopExpr *clone_expr_with_block_impl () const override |
| 3778 |
{ |
| 3779 |
return new LoopExpr (*this); |
| 3780 |
} |
| 3781 |
}; |
| 3782 |
|
| 3783 |
// While loop expression AST node (predicate loop) |
| 3784 |
class WhileLoopExpr : public BaseLoopExpr |
| 3785 |
{ |
| 3786 |
std::unique_ptr<Expr> condition; |
| 3787 |
|
| 3788 |
public: |
| 3789 |
std::string as_string () const override; |
| 3790 |
|
| 3791 |
// Constructor for while loop with loop label |
| 3792 |
WhileLoopExpr (std::unique_ptr<Expr> loop_condition, |
| 3793 |
std::unique_ptr<BlockExpr> loop_block, location_t locus, |
| 3794 |
tl::optional<LoopLabel> loop_label = tl::nullopt, |
| 3795 |
std::vector<Attribute> outer_attribs |
| 3796 |
= std::vector<Attribute> ()) |
| 3797 |
: BaseLoopExpr (std::move (loop_block), locus, std::move (loop_label), |
| 3798 |
std::move (outer_attribs)), |
| 3799 |
condition (std::move (loop_condition)) |
| 3800 |
{} |
| 3801 |
|
| 3802 |
// Copy constructor with clone |
| 3803 |
WhileLoopExpr (WhileLoopExpr const &other) |
| 3804 |
: BaseLoopExpr (other), condition (other.condition->clone_expr ()) |
| 3805 |
{} |
| 3806 |
|
| 3807 |
// Overloaded assignment operator to clone |
| 3808 |
WhileLoopExpr &operator= (WhileLoopExpr const &other) |
| 3809 |
{ |
| 3810 |
BaseLoopExpr::operator= (other); |
| 3811 |
condition = other.condition->clone_expr (); |
| 3812 |
// loop_block = other.loop_block->clone_block_expr(); |
| 3813 |
// loop_label = other.loop_label; |
| 3814 |
// outer_attrs = other.outer_attrs; |
| 3815 |
|
| 3816 |
return *this; |
| 3817 |
} |
| 3818 |
|
| 3819 |
// move constructors |
| 3820 |
WhileLoopExpr (WhileLoopExpr &&other) = default; |
| 3821 |
WhileLoopExpr &operator= (WhileLoopExpr &&other) = default; |
| 3822 |
|
| 3823 |
void accept_vis (ASTVisitor &vis) override; |
| 3824 |
|
| 3825 |
// TODO: is this better? Or is a "vis_block" better? |
| 3826 |
Expr &get_predicate_expr () |
| 3827 |
{ |
| 3828 |
rust_assert (condition != nullptr); |
| 3829 |
return *condition; |
| 3830 |
} |
| 3831 |
|
| 3832 |
BaseLoopExpr::Kind get_loop_kind () const override |
| 3833 |
{ |
| 3834 |
return BaseLoopExpr::Kind::While; |
| 3835 |
} |
| 3836 |
|
| 3837 |
protected: |
| 3838 |
/* Use covariance to implement clone function as returning this object rather |
| 3839 |
* than base */ |
| 3840 |
WhileLoopExpr *clone_expr_with_block_impl () const override |
| 3841 |
{ |
| 3842 |
return new WhileLoopExpr (*this); |
| 3843 |
} |
| 3844 |
}; |
| 3845 |
|
| 3846 |
// While let loop expression AST node (predicate pattern loop) |
| 3847 |
class WhileLetLoopExpr : public BaseLoopExpr |
| 3848 |
{ |
| 3849 |
// MatchArmPatterns patterns; |
| 3850 |
std::vector<std::unique_ptr<Pattern> > match_arm_patterns; // inlined |
| 3851 |
std::unique_ptr<Expr> scrutinee; |
| 3852 |
|
| 3853 |
public: |
| 3854 |
std::string as_string () const override; |
| 3855 |
|
| 3856 |
// Constructor with a loop label |
| 3857 |
WhileLetLoopExpr (std::vector<std::unique_ptr<Pattern> > match_arm_patterns, |
| 3858 |
std::unique_ptr<Expr> scrutinee, |
| 3859 |
std::unique_ptr<BlockExpr> loop_block, location_t locus, |
| 3860 |
tl::optional<LoopLabel> loop_label = tl::nullopt, |
| 3861 |
std::vector<Attribute> outer_attribs |
| 3862 |
= std::vector<Attribute> ()) |
| 3863 |
: BaseLoopExpr (std::move (loop_block), locus, std::move (loop_label), |
| 3864 |
std::move (outer_attribs)), |
| 3865 |
match_arm_patterns (std::move (match_arm_patterns)), |
| 3866 |
scrutinee (std::move (scrutinee)) |
| 3867 |
{} |
| 3868 |
|
| 3869 |
// Copy constructor with clone |
| 3870 |
WhileLetLoopExpr (WhileLetLoopExpr const &other) |
| 3871 |
: BaseLoopExpr (other), |
| 3872 |
/*match_arm_patterns(other.match_arm_patterns),*/ scrutinee ( |
| 3873 |
other.scrutinee->clone_expr ()) |
| 3874 |
{ |
| 3875 |
match_arm_patterns.reserve (other.match_arm_patterns.size ()); |
| 3876 |
for (const auto &e : other.match_arm_patterns) |
| 3877 |
match_arm_patterns.push_back (e->clone_pattern ()); |
| 3878 |
} |
| 3879 |
|
| 3880 |
// Overloaded assignment operator to clone pointers |
| 3881 |
WhileLetLoopExpr &operator= (WhileLetLoopExpr const &other) |
| 3882 |
{ |
| 3883 |
BaseLoopExpr::operator= (other); |
| 3884 |
// match_arm_patterns = other.match_arm_patterns; |
| 3885 |
scrutinee = other.scrutinee->clone_expr (); |
| 3886 |
// loop_block = other.loop_block->clone_block_expr(); |
| 3887 |
// loop_label = other.loop_label; |
| 3888 |
// outer_attrs = other.outer_attrs; |
| 3889 |
|
| 3890 |
match_arm_patterns.reserve (other.match_arm_patterns.size ()); |
| 3891 |
for (const auto &e : other.match_arm_patterns) |
| 3892 |
match_arm_patterns.push_back (e->clone_pattern ()); |
| 3893 |
|
| 3894 |
return *this; |
| 3895 |
} |
| 3896 |
|
| 3897 |
// move constructors |
| 3898 |
WhileLetLoopExpr (WhileLetLoopExpr &&other) = default; |
| 3899 |
WhileLetLoopExpr &operator= (WhileLetLoopExpr &&other) = default; |
| 3900 |
|
| 3901 |
void accept_vis (ASTVisitor &vis) override; |
| 3902 |
|
| 3903 |
// TODO: is this better? Or is a "vis_block" better? |
| 3904 |
Expr &get_scrutinee_expr () |
| 3905 |
{ |
| 3906 |
rust_assert (scrutinee != nullptr); |
| 3907 |
return *scrutinee; |
| 3908 |
} |
| 3909 |
|
| 3910 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 3911 |
const std::vector<std::unique_ptr<Pattern> > &get_patterns () const |
| 3912 |
{ |
| 3913 |
return match_arm_patterns; |
| 3914 |
} |
| 3915 |
std::vector<std::unique_ptr<Pattern> > &get_patterns () |
| 3916 |
{ |
| 3917 |
return match_arm_patterns; |
| 3918 |
} |
| 3919 |
|
| 3920 |
BaseLoopExpr::Kind get_loop_kind () const override |
| 3921 |
{ |
| 3922 |
return BaseLoopExpr::Kind::WhileLet; |
| 3923 |
} |
| 3924 |
|
| 3925 |
protected: |
| 3926 |
/* Use covariance to implement clone function as returning this object rather |
| 3927 |
* than base */ |
| 3928 |
WhileLetLoopExpr *clone_expr_with_block_impl () const override |
| 3929 |
{ |
| 3930 |
return new WhileLetLoopExpr (*this); |
| 3931 |
} |
| 3932 |
}; |
| 3933 |
|
| 3934 |
// For loop expression AST node (iterator loop) |
| 3935 |
class ForLoopExpr : public BaseLoopExpr |
| 3936 |
{ |
| 3937 |
std::unique_ptr<Pattern> pattern; |
| 3938 |
std::unique_ptr<Expr> iterator_expr; |
| 3939 |
|
| 3940 |
public: |
| 3941 |
std::string as_string () const override; |
| 3942 |
|
| 3943 |
// Constructor with loop label |
| 3944 |
ForLoopExpr (std::unique_ptr<Pattern> loop_pattern, |
| 3945 |
std::unique_ptr<Expr> iterator_expr, |
| 3946 |
std::unique_ptr<BlockExpr> loop_body, location_t locus, |
| 3947 |
tl::optional<LoopLabel> loop_label = tl::nullopt, |
| 3948 |
std::vector<Attribute> outer_attribs = std::vector<Attribute> ()) |
| 3949 |
: BaseLoopExpr (std::move (loop_body), locus, std::move (loop_label), |
| 3950 |
std::move (outer_attribs)), |
| 3951 |
pattern (std::move (loop_pattern)), |
| 3952 |
iterator_expr (std::move (iterator_expr)) |
| 3953 |
{} |
| 3954 |
|
| 3955 |
// Copy constructor with clone |
| 3956 |
ForLoopExpr (ForLoopExpr const &other) |
| 3957 |
: BaseLoopExpr (other), pattern (other.pattern->clone_pattern ()), |
| 3958 |
iterator_expr (other.iterator_expr->clone_expr ()) |
| 3959 |
{} |
| 3960 |
|
| 3961 |
// Overloaded assignment operator to clone |
| 3962 |
ForLoopExpr &operator= (ForLoopExpr const &other) |
| 3963 |
{ |
| 3964 |
BaseLoopExpr::operator= (other); |
| 3965 |
pattern = other.pattern->clone_pattern (); |
| 3966 |
iterator_expr = other.iterator_expr->clone_expr (); |
| 3967 |
/*loop_block = other.loop_block->clone_block_expr(); |
| 3968 |
loop_label = other.loop_label; |
| 3969 |
outer_attrs = other.outer_attrs;*/ |
| 3970 |
|
| 3971 |
return *this; |
| 3972 |
} |
| 3973 |
|
| 3974 |
// move constructors |
| 3975 |
ForLoopExpr (ForLoopExpr &&other) = default; |
| 3976 |
ForLoopExpr &operator= (ForLoopExpr &&other) = default; |
| 3977 |
|
| 3978 |
void accept_vis (ASTVisitor &vis) override; |
| 3979 |
|
| 3980 |
// TODO: is this better? Or is a "vis_block" better? |
| 3981 |
Expr &get_iterator_expr () |
| 3982 |
{ |
| 3983 |
rust_assert (iterator_expr != nullptr); |
| 3984 |
return *iterator_expr; |
| 3985 |
} |
| 3986 |
|
| 3987 |
// TODO: is this better? Or is a "vis_block" better? |
| 3988 |
Pattern &get_pattern () |
| 3989 |
{ |
| 3990 |
rust_assert (pattern != nullptr); |
| 3991 |
return *pattern; |
| 3992 |
} |
| 3993 |
|
| 3994 |
BaseLoopExpr::Kind get_loop_kind () const override |
| 3995 |
{ |
| 3996 |
return BaseLoopExpr::Kind::For; |
| 3997 |
} |
| 3998 |
|
| 3999 |
protected: |
| 4000 |
/* Use covariance to implement clone function as returning this object rather |
| 4001 |
* than base */ |
| 4002 |
ForLoopExpr *clone_expr_with_block_impl () const override |
| 4003 |
{ |
| 4004 |
return new ForLoopExpr (*this); |
| 4005 |
} |
| 4006 |
}; |
| 4007 |
|
| 4008 |
// forward decl for IfExpr |
| 4009 |
class IfLetExpr; |
| 4010 |
|
| 4011 |
// Base if expression with no "else" or "if let" AST node |
| 4012 |
class IfExpr : public ExprWithBlock |
| 4013 |
{ |
| 4014 |
std::vector<Attribute> outer_attrs; |
| 4015 |
std::unique_ptr<Expr> condition; |
| 4016 |
std::unique_ptr<BlockExpr> if_block; |
| 4017 |
location_t locus; |
| 4018 |
|
| 4019 |
public: |
| 4020 |
std::string as_string () const override; |
| 4021 |
|
| 4022 |
IfExpr (std::unique_ptr<Expr> condition, std::unique_ptr<BlockExpr> if_block, |
| 4023 |
std::vector<Attribute> outer_attrs, location_t locus) |
| 4024 |
: outer_attrs (std::move (outer_attrs)), condition (std::move (condition)), |
| 4025 |
if_block (std::move (if_block)), locus (locus) |
| 4026 |
{} |
| 4027 |
// outer attributes are never allowed on IfExprs |
| 4028 |
|
| 4029 |
// Copy constructor with clone |
| 4030 |
IfExpr (IfExpr const &other) |
| 4031 |
: ExprWithBlock (other), outer_attrs (other.outer_attrs), |
| 4032 |
locus (other.locus) |
| 4033 |
{ |
| 4034 |
// guard to prevent null dereference (only required if error state) |
| 4035 |
if (other.condition != nullptr) |
| 4036 |
condition = other.condition->clone_expr (); |
| 4037 |
if (other.if_block != nullptr) |
| 4038 |
if_block = other.if_block->clone_block_expr (); |
| 4039 |
} |
| 4040 |
|
| 4041 |
// Overloaded assignment operator to clone expressions |
| 4042 |
IfExpr &operator= (IfExpr const &other) |
| 4043 |
{ |
| 4044 |
ExprWithBlock::operator= (other); |
| 4045 |
outer_attrs = other.outer_attrs; |
| 4046 |
locus = other.locus; |
| 4047 |
|
| 4048 |
// guard to prevent null dereference (only required if error state) |
| 4049 |
if (other.condition != nullptr) |
| 4050 |
condition = other.condition->clone_expr (); |
| 4051 |
else |
| 4052 |
condition = nullptr; |
| 4053 |
if (other.if_block != nullptr) |
| 4054 |
if_block = other.if_block->clone_block_expr (); |
| 4055 |
else |
| 4056 |
if_block = nullptr; |
| 4057 |
|
| 4058 |
return *this; |
| 4059 |
} |
| 4060 |
|
| 4061 |
// move constructors |
| 4062 |
IfExpr (IfExpr &&other) = default; |
| 4063 |
IfExpr &operator= (IfExpr &&other) = default; |
| 4064 |
|
| 4065 |
// Unique pointer custom clone function |
| 4066 |
std::unique_ptr<IfExpr> clone_if_expr () const |
| 4067 |
{ |
| 4068 |
return std::unique_ptr<IfExpr> (clone_if_expr_impl ()); |
| 4069 |
} |
| 4070 |
|
| 4071 |
/* Note that multiple "else if"s are handled via nested ASTs rather than a |
| 4072 |
* vector of else ifs - i.e. not like a switch statement. TODO - is this a |
| 4073 |
* better approach? or does it not parse correctly and have downsides? */ |
| 4074 |
|
| 4075 |
location_t get_locus () const override final { return locus; } |
| 4076 |
|
| 4077 |
void accept_vis (ASTVisitor &vis) override; |
| 4078 |
|
| 4079 |
void vis_if_condition (ASTVisitor &vis) { condition->accept_vis (vis); } |
| 4080 |
void vis_if_block (ASTVisitor &vis) { if_block->accept_vis (vis); } |
| 4081 |
|
| 4082 |
// TODO: is this better? Or is a "vis_block" better? |
| 4083 |
Expr &get_condition_expr () |
| 4084 |
{ |
| 4085 |
rust_assert (condition != nullptr); |
| 4086 |
return *condition; |
| 4087 |
} |
| 4088 |
|
| 4089 |
std::unique_ptr<Expr> &get_condition_expr_ptr () |
| 4090 |
{ |
| 4091 |
rust_assert (condition != nullptr); |
| 4092 |
return condition; |
| 4093 |
} |
| 4094 |
|
| 4095 |
// TODO: is this better? Or is a "vis_block" better? |
| 4096 |
BlockExpr &get_if_block () |
| 4097 |
{ |
| 4098 |
rust_assert (if_block != nullptr); |
| 4099 |
return *if_block; |
| 4100 |
} |
| 4101 |
|
| 4102 |
// Invalid if if block or condition is null, so base stripping on that. |
| 4103 |
void mark_for_strip () override |
| 4104 |
{ |
| 4105 |
if_block = nullptr; |
| 4106 |
condition = nullptr; |
| 4107 |
} |
| 4108 |
bool is_marked_for_strip () const override |
| 4109 |
{ |
| 4110 |
return if_block == nullptr && condition == nullptr; |
| 4111 |
} |
| 4112 |
|
| 4113 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 4114 |
{ |
| 4115 |
outer_attrs = std::move (new_attrs); |
| 4116 |
} |
| 4117 |
|
| 4118 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 4119 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 4120 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 4121 |
|
| 4122 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::If; } |
| 4123 |
|
| 4124 |
protected: |
| 4125 |
// Base clone function but still concrete as concrete base class |
| 4126 |
virtual IfExpr *clone_if_expr_impl () const { return new IfExpr (*this); } |
| 4127 |
|
| 4128 |
/* Use covariance to implement clone function as returning this object rather |
| 4129 |
* than base */ |
| 4130 |
IfExpr *clone_expr_with_block_impl () const final override |
| 4131 |
{ |
| 4132 |
return clone_if_expr_impl (); |
| 4133 |
} |
| 4134 |
}; |
| 4135 |
|
| 4136 |
// If expression with an ending "else" expression AST node (trailing) |
| 4137 |
class IfExprConseqElse : public IfExpr |
| 4138 |
{ |
| 4139 |
std::unique_ptr<ExprWithBlock> else_block; |
| 4140 |
|
| 4141 |
public: |
| 4142 |
std::string as_string () const override; |
| 4143 |
|
| 4144 |
IfExprConseqElse (std::unique_ptr<Expr> condition, |
| 4145 |
std::unique_ptr<BlockExpr> if_block, |
| 4146 |
std::unique_ptr<ExprWithBlock> else_block, |
| 4147 |
std::vector<Attribute> outer_attrs, location_t locus) |
| 4148 |
: IfExpr (std::move (condition), std::move (if_block), |
| 4149 |
std::move (outer_attrs), locus), |
| 4150 |
else_block (std::move (else_block)) |
| 4151 |
{} |
| 4152 |
// again, outer attributes not allowed |
| 4153 |
|
| 4154 |
// Copy constructor with clone |
| 4155 |
IfExprConseqElse (IfExprConseqElse const &other) |
| 4156 |
: IfExpr (other), else_block (other.else_block->clone_expr_with_block ()) |
| 4157 |
{} |
| 4158 |
|
| 4159 |
// Overloaded assignment operator with cloning |
| 4160 |
IfExprConseqElse &operator= (IfExprConseqElse const &other) |
| 4161 |
{ |
| 4162 |
IfExpr::operator= (other); |
| 4163 |
// condition = other.condition->clone_expr(); |
| 4164 |
// if_block = other.if_block->clone_block_expr(); |
| 4165 |
else_block = other.else_block->clone_expr_with_block (); |
| 4166 |
|
| 4167 |
return *this; |
| 4168 |
} |
| 4169 |
|
| 4170 |
// move constructors |
| 4171 |
IfExprConseqElse (IfExprConseqElse &&other) = default; |
| 4172 |
IfExprConseqElse &operator= (IfExprConseqElse &&other) = default; |
| 4173 |
|
| 4174 |
void accept_vis (ASTVisitor &vis) override; |
| 4175 |
|
| 4176 |
void vis_else_block (ASTVisitor &vis) { else_block->accept_vis (vis); } |
| 4177 |
|
| 4178 |
// TODO: is this better? Or is a "vis_block" better? |
| 4179 |
ExprWithBlock &get_else_block () |
| 4180 |
{ |
| 4181 |
rust_assert (else_block != nullptr); |
| 4182 |
return *else_block; |
| 4183 |
} |
| 4184 |
|
| 4185 |
protected: |
| 4186 |
/* Use covariance to implement clone function as returning this object rather |
| 4187 |
* than base */ |
| 4188 |
IfExprConseqElse *clone_if_expr_impl () const override |
| 4189 |
{ |
| 4190 |
return new IfExprConseqElse (*this); |
| 4191 |
} |
| 4192 |
}; |
| 4193 |
|
| 4194 |
// Basic "if let" expression AST node with no else |
| 4195 |
class IfLetExpr : public ExprWithBlock |
| 4196 |
{ |
| 4197 |
std::vector<Attribute> outer_attrs; |
| 4198 |
std::vector<std::unique_ptr<Pattern> > match_arm_patterns; // inlined |
| 4199 |
std::unique_ptr<Expr> value; |
| 4200 |
std::unique_ptr<BlockExpr> if_block; |
| 4201 |
location_t locus; |
| 4202 |
|
| 4203 |
public: |
| 4204 |
std::string as_string () const override; |
| 4205 |
|
| 4206 |
IfLetExpr (std::vector<std::unique_ptr<Pattern> > match_arm_patterns, |
| 4207 |
std::unique_ptr<Expr> value, std::unique_ptr<BlockExpr> if_block, |
| 4208 |
std::vector<Attribute> outer_attrs, location_t locus) |
| 4209 |
: outer_attrs (std::move (outer_attrs)), |
| 4210 |
match_arm_patterns (std::move (match_arm_patterns)), |
| 4211 |
value (std::move (value)), if_block (std::move (if_block)), locus (locus) |
| 4212 |
{} |
| 4213 |
|
| 4214 |
// copy constructor with clone |
| 4215 |
IfLetExpr (IfLetExpr const &other) |
| 4216 |
: ExprWithBlock (other), outer_attrs (other.outer_attrs), |
| 4217 |
locus (other.locus) |
| 4218 |
{ |
| 4219 |
// guard to prevent null dereference (only required if error state) |
| 4220 |
if (other.value != nullptr) |
| 4221 |
value = other.value->clone_expr (); |
| 4222 |
if (other.if_block != nullptr) |
| 4223 |
if_block = other.if_block->clone_block_expr (); |
| 4224 |
|
| 4225 |
match_arm_patterns.reserve (other.match_arm_patterns.size ()); |
| 4226 |
for (const auto &e : other.match_arm_patterns) |
| 4227 |
match_arm_patterns.push_back (e->clone_pattern ()); |
| 4228 |
} |
| 4229 |
|
| 4230 |
// overload assignment operator to clone |
| 4231 |
IfLetExpr &operator= (IfLetExpr const &other) |
| 4232 |
{ |
| 4233 |
ExprWithBlock::operator= (other); |
| 4234 |
outer_attrs = other.outer_attrs; |
| 4235 |
locus = other.locus; |
| 4236 |
|
| 4237 |
// guard to prevent null dereference (only required if error state) |
| 4238 |
if (other.value != nullptr) |
| 4239 |
value = other.value->clone_expr (); |
| 4240 |
else |
| 4241 |
value = nullptr; |
| 4242 |
if (other.if_block != nullptr) |
| 4243 |
if_block = other.if_block->clone_block_expr (); |
| 4244 |
else |
| 4245 |
if_block = nullptr; |
| 4246 |
|
| 4247 |
match_arm_patterns.reserve (other.match_arm_patterns.size ()); |
| 4248 |
for (const auto &e : other.match_arm_patterns) |
| 4249 |
match_arm_patterns.push_back (e->clone_pattern ()); |
| 4250 |
|
| 4251 |
return *this; |
| 4252 |
} |
| 4253 |
|
| 4254 |
// move constructors |
| 4255 |
IfLetExpr (IfLetExpr &&other) = default; |
| 4256 |
IfLetExpr &operator= (IfLetExpr &&other) = default; |
| 4257 |
|
| 4258 |
// Unique pointer custom clone function |
| 4259 |
std::unique_ptr<IfLetExpr> clone_if_let_expr () const |
| 4260 |
{ |
| 4261 |
return std::unique_ptr<IfLetExpr> (clone_if_let_expr_impl ()); |
| 4262 |
} |
| 4263 |
|
| 4264 |
location_t get_locus () const override final { return locus; } |
| 4265 |
|
| 4266 |
void accept_vis (ASTVisitor &vis) override; |
| 4267 |
|
| 4268 |
// Invalid if block or value is null, so base stripping on that. |
| 4269 |
void mark_for_strip () override |
| 4270 |
{ |
| 4271 |
if_block = nullptr; |
| 4272 |
value = nullptr; |
| 4273 |
} |
| 4274 |
bool is_marked_for_strip () const override |
| 4275 |
{ |
| 4276 |
return if_block == nullptr && value == nullptr; |
| 4277 |
} |
| 4278 |
|
| 4279 |
// TODO: is this better? Or is a "vis_block" better? |
| 4280 |
Expr &get_value_expr () |
| 4281 |
{ |
| 4282 |
rust_assert (value != nullptr); |
| 4283 |
return *value; |
| 4284 |
} |
| 4285 |
|
| 4286 |
std::unique_ptr<Expr> &get_value_expr_ptr () |
| 4287 |
{ |
| 4288 |
rust_assert (value != nullptr); |
| 4289 |
return value; |
| 4290 |
} |
| 4291 |
|
| 4292 |
// TODO: is this better? Or is a "vis_block" better? |
| 4293 |
BlockExpr &get_if_block () |
| 4294 |
{ |
| 4295 |
rust_assert (if_block != nullptr); |
| 4296 |
return *if_block; |
| 4297 |
} |
| 4298 |
|
| 4299 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 4300 |
const std::vector<std::unique_ptr<Pattern> > &get_patterns () const |
| 4301 |
{ |
| 4302 |
return match_arm_patterns; |
| 4303 |
} |
| 4304 |
std::vector<std::unique_ptr<Pattern> > &get_patterns () |
| 4305 |
{ |
| 4306 |
return match_arm_patterns; |
| 4307 |
} |
| 4308 |
|
| 4309 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 4310 |
{ |
| 4311 |
outer_attrs = std::move (new_attrs); |
| 4312 |
} |
| 4313 |
|
| 4314 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 4315 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 4316 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 4317 |
|
| 4318 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::IfLet; } |
| 4319 |
|
| 4320 |
protected: |
| 4321 |
/* Use covariance to implement clone function as returning this object rather |
| 4322 |
* than base (or rather this or any derived object) */ |
| 4323 |
IfLetExpr *clone_expr_with_block_impl () const final override |
| 4324 |
{ |
| 4325 |
return clone_if_let_expr_impl (); |
| 4326 |
} |
| 4327 |
|
| 4328 |
// Base clone function but still concrete as concrete base class |
| 4329 |
virtual IfLetExpr *clone_if_let_expr_impl () const |
| 4330 |
{ |
| 4331 |
return new IfLetExpr (*this); |
| 4332 |
} |
| 4333 |
}; |
| 4334 |
|
| 4335 |
/* AST node representing "if let" expression with an "else" expression at the |
| 4336 |
* end */ |
| 4337 |
class IfLetExprConseqElse : public IfLetExpr |
| 4338 |
{ |
| 4339 |
std::unique_ptr<ExprWithBlock> else_block; |
| 4340 |
|
| 4341 |
public: |
| 4342 |
std::string as_string () const override; |
| 4343 |
|
| 4344 |
IfLetExprConseqElse ( |
| 4345 |
std::vector<std::unique_ptr<Pattern> > match_arm_patterns, |
| 4346 |
std::unique_ptr<Expr> value, std::unique_ptr<BlockExpr> if_block, |
| 4347 |
std::unique_ptr<ExprWithBlock> else_block, |
| 4348 |
std::vector<Attribute> outer_attrs, location_t locus) |
| 4349 |
: IfLetExpr (std::move (match_arm_patterns), std::move (value), |
| 4350 |
std::move (if_block), std::move (outer_attrs), locus), |
| 4351 |
else_block (std::move (else_block)) |
| 4352 |
{} |
| 4353 |
// outer attributes not allowed |
| 4354 |
|
| 4355 |
// copy constructor with clone |
| 4356 |
IfLetExprConseqElse (IfLetExprConseqElse const &other) |
| 4357 |
: IfLetExpr (other), else_block (other.else_block->clone_expr_with_block ()) |
| 4358 |
{} |
| 4359 |
|
| 4360 |
// overload assignment operator to clone |
| 4361 |
IfLetExprConseqElse &operator= (IfLetExprConseqElse const &other) |
| 4362 |
{ |
| 4363 |
IfLetExpr::operator= (other); |
| 4364 |
// match_arm_patterns = other.match_arm_patterns; |
| 4365 |
// value = other.value->clone_expr(); |
| 4366 |
// if_block = other.if_block->clone_block_expr(); |
| 4367 |
else_block = other.else_block->clone_expr_with_block (); |
| 4368 |
// outer_attrs = other.outer_attrs; |
| 4369 |
|
| 4370 |
return *this; |
| 4371 |
} |
| 4372 |
|
| 4373 |
// move constructors |
| 4374 |
IfLetExprConseqElse (IfLetExprConseqElse &&other) = default; |
| 4375 |
IfLetExprConseqElse &operator= (IfLetExprConseqElse &&other) = default; |
| 4376 |
|
| 4377 |
void accept_vis (ASTVisitor &vis) override; |
| 4378 |
|
| 4379 |
// TODO: is this better? Or is a "vis_block" better? |
| 4380 |
ExprWithBlock &get_else_block () |
| 4381 |
{ |
| 4382 |
rust_assert (else_block != nullptr); |
| 4383 |
return *else_block; |
| 4384 |
} |
| 4385 |
|
| 4386 |
protected: |
| 4387 |
/* Use covariance to implement clone function as returning this object rather |
| 4388 |
* than base */ |
| 4389 |
IfLetExprConseqElse *clone_if_let_expr_impl () const override |
| 4390 |
{ |
| 4391 |
return new IfLetExprConseqElse (*this); |
| 4392 |
} |
| 4393 |
}; |
| 4394 |
|
| 4395 |
// Match arm expression |
| 4396 |
struct MatchArm |
| 4397 |
{ |
| 4398 |
private: |
| 4399 |
std::vector<Attribute> outer_attrs; |
| 4400 |
// MatchArmPatterns patterns; |
| 4401 |
std::vector<std::unique_ptr<Pattern> > match_arm_patterns; // inlined |
| 4402 |
|
| 4403 |
// bool has_match_arm_guard; |
| 4404 |
// inlined from MatchArmGuard |
| 4405 |
std::unique_ptr<Expr> guard_expr; |
| 4406 |
|
| 4407 |
location_t locus; |
| 4408 |
|
| 4409 |
public: |
| 4410 |
// Returns whether the MatchArm has a match arm guard expression |
| 4411 |
bool has_match_arm_guard () const { return guard_expr != nullptr; } |
| 4412 |
|
| 4413 |
// Constructor for match arm with a guard expression |
| 4414 |
MatchArm (std::vector<std::unique_ptr<Pattern> > match_arm_patterns, |
| 4415 |
location_t locus, std::unique_ptr<Expr> guard_expr = nullptr, |
| 4416 |
std::vector<Attribute> outer_attrs = std::vector<Attribute> ()) |
| 4417 |
: outer_attrs (std::move (outer_attrs)), |
| 4418 |
match_arm_patterns (std::move (match_arm_patterns)), |
| 4419 |
guard_expr (std::move (guard_expr)), locus (locus) |
| 4420 |
{} |
| 4421 |
|
| 4422 |
// Copy constructor with clone |
| 4423 |
MatchArm (MatchArm const &other) : outer_attrs (other.outer_attrs) |
| 4424 |
{ |
| 4425 |
// guard to protect from null pointer dereference |
| 4426 |
if (other.guard_expr != nullptr) |
| 4427 |
guard_expr = other.guard_expr->clone_expr (); |
| 4428 |
|
| 4429 |
match_arm_patterns.reserve (other.match_arm_patterns.size ()); |
| 4430 |
for (const auto &e : other.match_arm_patterns) |
| 4431 |
match_arm_patterns.push_back (e->clone_pattern ()); |
| 4432 |
|
| 4433 |
locus = other.locus; |
| 4434 |
} |
| 4435 |
|
| 4436 |
~MatchArm () = default; |
| 4437 |
|
| 4438 |
// Overload assignment operator to clone |
| 4439 |
MatchArm &operator= (MatchArm const &other) |
| 4440 |
{ |
| 4441 |
outer_attrs = other.outer_attrs; |
| 4442 |
|
| 4443 |
if (other.guard_expr != nullptr) |
| 4444 |
guard_expr = other.guard_expr->clone_expr (); |
| 4445 |
else |
| 4446 |
guard_expr = nullptr; |
| 4447 |
|
| 4448 |
match_arm_patterns.reserve (other.match_arm_patterns.size ()); |
| 4449 |
for (const auto &e : other.match_arm_patterns) |
| 4450 |
match_arm_patterns.push_back (e->clone_pattern ()); |
| 4451 |
|
| 4452 |
return *this; |
| 4453 |
} |
| 4454 |
|
| 4455 |
// move constructors |
| 4456 |
MatchArm (MatchArm &&other) = default; |
| 4457 |
MatchArm &operator= (MatchArm &&other) = default; |
| 4458 |
|
| 4459 |
// Returns whether match arm is in an error state. |
| 4460 |
bool is_error () const { return match_arm_patterns.empty (); } |
| 4461 |
|
| 4462 |
// Creates a match arm in an error state. |
| 4463 |
static MatchArm create_error () |
| 4464 |
{ |
| 4465 |
location_t locus = UNDEF_LOCATION; |
| 4466 |
return MatchArm (std::vector<std::unique_ptr<Pattern> > (), locus); |
| 4467 |
} |
| 4468 |
|
| 4469 |
std::string as_string () const; |
| 4470 |
|
| 4471 |
// TODO: is this better? Or is a "vis_block" better? |
| 4472 |
Expr &get_guard_expr () |
| 4473 |
{ |
| 4474 |
rust_assert (has_match_arm_guard ()); |
| 4475 |
return *guard_expr; |
| 4476 |
} |
| 4477 |
|
| 4478 |
std::unique_ptr<Expr> &get_guard_expr_ptr () |
| 4479 |
{ |
| 4480 |
rust_assert (has_match_arm_guard ()); |
| 4481 |
return guard_expr; |
| 4482 |
} |
| 4483 |
|
| 4484 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 4485 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 4486 |
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; } |
| 4487 |
|
| 4488 |
const std::vector<std::unique_ptr<Pattern> > &get_patterns () const |
| 4489 |
{ |
| 4490 |
return match_arm_patterns; |
| 4491 |
} |
| 4492 |
std::vector<std::unique_ptr<Pattern> > &get_patterns () |
| 4493 |
{ |
| 4494 |
return match_arm_patterns; |
| 4495 |
} |
| 4496 |
|
| 4497 |
location_t get_locus () const { return locus; } |
| 4498 |
}; |
| 4499 |
|
| 4500 |
/* A "match case" - a correlated match arm and resulting expression. Not |
| 4501 |
* abstract. */ |
| 4502 |
struct MatchCase |
| 4503 |
{ |
| 4504 |
private: |
| 4505 |
MatchArm arm; |
| 4506 |
std::unique_ptr<Expr> expr; |
| 4507 |
NodeId node_id; |
| 4508 |
|
| 4509 |
/* TODO: does whether trailing comma exists need to be stored? currently |
| 4510 |
* assuming it is only syntactical and has no effect on meaning. */ |
| 4511 |
|
| 4512 |
public: |
| 4513 |
MatchCase (MatchArm arm, std::unique_ptr<Expr> expr) |
| 4514 |
: arm (std::move (arm)), expr (std::move (expr)), |
| 4515 |
node_id (Analysis::Mappings::get ().get_next_node_id ()) |
| 4516 |
{} |
| 4517 |
|
| 4518 |
MatchCase (const MatchCase &other) |
| 4519 |
: arm (other.arm), expr (other.expr->clone_expr ()), node_id (other.node_id) |
| 4520 |
{} |
| 4521 |
|
| 4522 |
MatchCase &operator= (const MatchCase &other) |
| 4523 |
{ |
| 4524 |
arm = other.arm; |
| 4525 |
expr = other.expr->clone_expr (); |
| 4526 |
node_id = other.node_id; |
| 4527 |
|
| 4528 |
return *this; |
| 4529 |
} |
| 4530 |
|
| 4531 |
MatchCase (MatchCase &&other) = default; |
| 4532 |
MatchCase &operator= (MatchCase &&other) = default; |
| 4533 |
|
| 4534 |
~MatchCase () = default; |
| 4535 |
|
| 4536 |
std::string as_string () const; |
| 4537 |
|
| 4538 |
// TODO: is this better? Or is a "vis_block" better? |
| 4539 |
Expr &get_expr () |
| 4540 |
{ |
| 4541 |
rust_assert (expr != nullptr); |
| 4542 |
return *expr; |
| 4543 |
} |
| 4544 |
|
| 4545 |
std::unique_ptr<Expr> &get_expr_ptr () |
| 4546 |
{ |
| 4547 |
rust_assert (expr != nullptr); |
| 4548 |
return expr; |
| 4549 |
} |
| 4550 |
|
| 4551 |
// TODO: is this better? Or is a "vis_block" better? |
| 4552 |
MatchArm &get_arm () |
| 4553 |
{ |
| 4554 |
rust_assert (!arm.is_error ()); |
| 4555 |
return arm; |
| 4556 |
} |
| 4557 |
|
| 4558 |
NodeId get_node_id () const { return node_id; } |
| 4559 |
}; |
| 4560 |
|
| 4561 |
// Match expression AST node |
| 4562 |
class MatchExpr : public ExprWithBlock |
| 4563 |
{ |
| 4564 |
std::vector<Attribute> outer_attrs; |
| 4565 |
std::unique_ptr<Expr> branch_value; |
| 4566 |
std::vector<Attribute> inner_attrs; |
| 4567 |
std::vector<MatchCase> match_arms; |
| 4568 |
location_t locus; |
| 4569 |
|
| 4570 |
public: |
| 4571 |
std::string as_string () const override; |
| 4572 |
|
| 4573 |
// Returns whether the match expression has any match arms. |
| 4574 |
bool has_match_arms () const { return !match_arms.empty (); } |
| 4575 |
|
| 4576 |
MatchExpr (std::unique_ptr<Expr> branch_value, |
| 4577 |
std::vector<MatchCase> match_arms, |
| 4578 |
std::vector<Attribute> inner_attrs, |
| 4579 |
std::vector<Attribute> outer_attrs, location_t locus) |
| 4580 |
: outer_attrs (std::move (outer_attrs)), |
| 4581 |
branch_value (std::move (branch_value)), |
| 4582 |
inner_attrs (std::move (inner_attrs)), |
| 4583 |
match_arms (std::move (match_arms)), locus (locus) |
| 4584 |
{} |
| 4585 |
|
| 4586 |
// Copy constructor requires clone due to unique_ptr |
| 4587 |
MatchExpr (MatchExpr const &other) |
| 4588 |
: ExprWithBlock (other), outer_attrs (other.outer_attrs), |
| 4589 |
inner_attrs (other.inner_attrs), match_arms (other.match_arms), |
| 4590 |
locus (other.locus) |
| 4591 |
{ |
| 4592 |
// guard to prevent null dereference (only required if error state) |
| 4593 |
if (other.branch_value != nullptr) |
| 4594 |
branch_value = other.branch_value->clone_expr (); |
| 4595 |
} |
| 4596 |
|
| 4597 |
// Overloaded assignment operator to clone due to unique_ptr |
| 4598 |
MatchExpr &operator= (MatchExpr const &other) |
| 4599 |
{ |
| 4600 |
ExprWithBlock::operator= (other); |
| 4601 |
inner_attrs = other.inner_attrs; |
| 4602 |
match_arms = other.match_arms; |
| 4603 |
outer_attrs = other.outer_attrs; |
| 4604 |
locus = other.locus; |
| 4605 |
|
| 4606 |
// guard to prevent null dereference (only required if error state) |
| 4607 |
if (other.branch_value != nullptr) |
| 4608 |
branch_value = other.branch_value->clone_expr (); |
| 4609 |
else |
| 4610 |
branch_value = nullptr; |
| 4611 |
|
| 4612 |
return *this; |
| 4613 |
} |
| 4614 |
|
| 4615 |
// move constructors |
| 4616 |
MatchExpr (MatchExpr &&other) = default; |
| 4617 |
MatchExpr &operator= (MatchExpr &&other) = default; |
| 4618 |
|
| 4619 |
location_t get_locus () const override final { return locus; } |
| 4620 |
|
| 4621 |
void accept_vis (ASTVisitor &vis) override; |
| 4622 |
|
| 4623 |
// Invalid if branch value is null, so base stripping on that. |
| 4624 |
void mark_for_strip () override { branch_value = nullptr; } |
| 4625 |
bool is_marked_for_strip () const override { return branch_value == nullptr; } |
| 4626 |
|
| 4627 |
// TODO: this mutable getter seems really dodgy. Think up better way. |
| 4628 |
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; } |
| 4629 |
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; } |
| 4630 |
|
| 4631 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 4632 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 4633 |
|
| 4634 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 4635 |
{ |
| 4636 |
outer_attrs = std::move (new_attrs); |
| 4637 |
} |
| 4638 |
|
| 4639 |
// TODO: is this better? Or is a "vis_block" better? |
| 4640 |
Expr &get_scrutinee_expr () |
| 4641 |
{ |
| 4642 |
rust_assert (branch_value != nullptr); |
| 4643 |
return *branch_value; |
| 4644 |
} |
| 4645 |
|
| 4646 |
const std::vector<MatchCase> &get_match_cases () const { return match_arms; } |
| 4647 |
std::vector<MatchCase> &get_match_cases () { return match_arms; } |
| 4648 |
|
| 4649 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Match; } |
| 4650 |
|
| 4651 |
protected: |
| 4652 |
/* Use covariance to implement clone function as returning this object rather |
| 4653 |
* than base */ |
| 4654 |
MatchExpr *clone_expr_with_block_impl () const override |
| 4655 |
{ |
| 4656 |
return new MatchExpr (*this); |
| 4657 |
} |
| 4658 |
}; |
| 4659 |
|
| 4660 |
// Await expression AST node (pseudo-member variable access) |
| 4661 |
class AwaitExpr : public ExprWithoutBlock |
| 4662 |
{ |
| 4663 |
std::vector<Attribute> outer_attrs; |
| 4664 |
std::unique_ptr<Expr> awaited_expr; |
| 4665 |
location_t locus; |
| 4666 |
|
| 4667 |
public: |
| 4668 |
// TODO: ensure outer attributes are actually allowed |
| 4669 |
AwaitExpr (std::unique_ptr<Expr> awaited_expr, |
| 4670 |
std::vector<Attribute> outer_attrs, location_t locus) |
| 4671 |
: outer_attrs (std::move (outer_attrs)), |
| 4672 |
awaited_expr (std::move (awaited_expr)), locus (locus) |
| 4673 |
{} |
| 4674 |
|
| 4675 |
// copy constructor with clone |
| 4676 |
AwaitExpr (AwaitExpr const &other) |
| 4677 |
: ExprWithoutBlock (other), outer_attrs (other.outer_attrs), |
| 4678 |
locus (other.locus) |
| 4679 |
{ |
| 4680 |
// guard to prevent null dereference (only required if error state) |
| 4681 |
if (other.awaited_expr != nullptr) |
| 4682 |
awaited_expr = other.awaited_expr->clone_expr (); |
| 4683 |
} |
| 4684 |
|
| 4685 |
// overloaded assignment operator with clone |
| 4686 |
AwaitExpr &operator= (AwaitExpr const &other) |
| 4687 |
{ |
| 4688 |
ExprWithoutBlock::operator= (other); |
| 4689 |
outer_attrs = other.outer_attrs; |
| 4690 |
locus = other.locus; |
| 4691 |
|
| 4692 |
// guard to prevent null dereference (only required if error state) |
| 4693 |
if (other.awaited_expr != nullptr) |
| 4694 |
awaited_expr = other.awaited_expr->clone_expr (); |
| 4695 |
else |
| 4696 |
awaited_expr = nullptr; |
| 4697 |
|
| 4698 |
return *this; |
| 4699 |
} |
| 4700 |
|
| 4701 |
// move constructors |
| 4702 |
AwaitExpr (AwaitExpr &&other) = default; |
| 4703 |
AwaitExpr &operator= (AwaitExpr &&other) = default; |
| 4704 |
|
| 4705 |
std::string as_string () const override; |
| 4706 |
|
| 4707 |
location_t get_locus () const override final { return locus; } |
| 4708 |
|
| 4709 |
void accept_vis (ASTVisitor &vis) override; |
| 4710 |
|
| 4711 |
// Invalid if awaited expr is null, so base stripping on that. |
| 4712 |
void mark_for_strip () override { awaited_expr = nullptr; } |
| 4713 |
bool is_marked_for_strip () const override { return awaited_expr == nullptr; } |
| 4714 |
|
| 4715 |
// TODO: is this better? Or is a "vis_block" better? |
| 4716 |
std::unique_ptr<Expr> &get_awaited_expr () |
| 4717 |
{ |
| 4718 |
rust_assert (awaited_expr != nullptr); |
| 4719 |
return awaited_expr; |
| 4720 |
} |
| 4721 |
|
| 4722 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 4723 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 4724 |
|
| 4725 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 4726 |
{ |
| 4727 |
outer_attrs = std::move (new_attrs); |
| 4728 |
} |
| 4729 |
|
| 4730 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::Await; } |
| 4731 |
|
| 4732 |
protected: |
| 4733 |
/* Use covariance to implement clone function as returning this object rather |
| 4734 |
* than base */ |
| 4735 |
AwaitExpr *clone_expr_without_block_impl () const override |
| 4736 |
{ |
| 4737 |
return new AwaitExpr (*this); |
| 4738 |
} |
| 4739 |
}; |
| 4740 |
|
| 4741 |
// Async block expression AST node (block expr that evaluates to a future) |
| 4742 |
class AsyncBlockExpr : public ExprWithBlock |
| 4743 |
{ |
| 4744 |
// TODO: should this extend BlockExpr rather than be a composite of it? |
| 4745 |
std::vector<Attribute> outer_attrs; |
| 4746 |
bool has_move; |
| 4747 |
std::unique_ptr<BlockExpr> block_expr; |
| 4748 |
location_t locus; |
| 4749 |
|
| 4750 |
public: |
| 4751 |
AsyncBlockExpr (std::unique_ptr<BlockExpr> block_expr, bool has_move, |
| 4752 |
std::vector<Attribute> outer_attrs, location_t locus) |
| 4753 |
: outer_attrs (std::move (outer_attrs)), has_move (has_move), |
| 4754 |
block_expr (std::move (block_expr)), locus (locus) |
| 4755 |
{} |
| 4756 |
|
| 4757 |
// copy constructor with clone |
| 4758 |
AsyncBlockExpr (AsyncBlockExpr const &other) |
| 4759 |
: ExprWithBlock (other), outer_attrs (other.outer_attrs), |
| 4760 |
has_move (other.has_move), locus (other.locus) |
| 4761 |
{ |
| 4762 |
// guard to prevent null dereference (only required if error state) |
| 4763 |
if (other.block_expr != nullptr) |
| 4764 |
block_expr = other.block_expr->clone_block_expr (); |
| 4765 |
} |
| 4766 |
|
| 4767 |
// overloaded assignment operator to clone |
| 4768 |
AsyncBlockExpr &operator= (AsyncBlockExpr const &other) |
| 4769 |
{ |
| 4770 |
ExprWithBlock::operator= (other); |
| 4771 |
outer_attrs = other.outer_attrs; |
| 4772 |
has_move = other.has_move; |
| 4773 |
locus = other.locus; |
| 4774 |
|
| 4775 |
// guard to prevent null dereference (only required if error state) |
| 4776 |
if (other.block_expr != nullptr) |
| 4777 |
block_expr = other.block_expr->clone_block_expr (); |
| 4778 |
else |
| 4779 |
block_expr = nullptr; |
| 4780 |
|
| 4781 |
return *this; |
| 4782 |
} |
| 4783 |
|
| 4784 |
// move constructors |
| 4785 |
AsyncBlockExpr (AsyncBlockExpr &&other) = default; |
| 4786 |
AsyncBlockExpr &operator= (AsyncBlockExpr &&other) = default; |
| 4787 |
|
| 4788 |
std::string as_string () const override; |
| 4789 |
|
| 4790 |
bool get_has_move () { return has_move; } |
| 4791 |
location_t get_locus () const override final { return locus; } |
| 4792 |
|
| 4793 |
void accept_vis (ASTVisitor &vis) override; |
| 4794 |
|
| 4795 |
// Invalid if block is null, so base stripping on that. |
| 4796 |
void mark_for_strip () override { block_expr = nullptr; } |
| 4797 |
bool is_marked_for_strip () const override { return block_expr == nullptr; } |
| 4798 |
|
| 4799 |
// TODO: is this better? Or is a "vis_block" better? |
| 4800 |
std::unique_ptr<BlockExpr> &get_block_expr () |
| 4801 |
{ |
| 4802 |
rust_assert (block_expr != nullptr); |
| 4803 |
return block_expr; |
| 4804 |
} |
| 4805 |
|
| 4806 |
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } |
| 4807 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 4808 |
|
| 4809 |
void set_outer_attrs (std::vector<Attribute> new_attrs) override |
| 4810 |
{ |
| 4811 |
outer_attrs = std::move (new_attrs); |
| 4812 |
} |
| 4813 |
|
| 4814 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::AsyncBlock; } |
| 4815 |
|
| 4816 |
protected: |
| 4817 |
/* Use covariance to implement clone function as returning this object rather |
| 4818 |
* than base */ |
| 4819 |
AsyncBlockExpr *clone_expr_with_block_impl () const override |
| 4820 |
{ |
| 4821 |
return new AsyncBlockExpr (*this); |
| 4822 |
} |
| 4823 |
}; |
| 4824 |
|
| 4825 |
// Inline-assembly specific options |
| 4826 |
enum class InlineAsmOption |
| 4827 |
{ |
| 4828 |
PURE = 1 << 0, |
| 4829 |
NOMEM = 1 << 1, |
| 4830 |
READONLY = 1 << 2, |
| 4831 |
PRESERVES_FLAGS = 1 << 3, |
| 4832 |
NORETURN = 1 << 4, |
| 4833 |
NOSTACK = 1 << 5, |
| 4834 |
ATT_SYNTAX = 1 << 6, |
| 4835 |
RAW = 1 << 7, |
| 4836 |
MAY_UNWIND = 1 << 8, |
| 4837 |
}; |
| 4838 |
|
| 4839 |
struct AnonConst |
| 4840 |
{ |
| 4841 |
NodeId id; |
| 4842 |
std::unique_ptr<Expr> expr; |
| 4843 |
AnonConst (NodeId id, std::unique_ptr<Expr> expr) |
| 4844 |
: id (id), expr (std::move (expr)) |
| 4845 |
{ |
| 4846 |
rust_assert (this->expr != nullptr); |
| 4847 |
} |
| 4848 |
AnonConst (const AnonConst &other) |
| 4849 |
{ |
| 4850 |
id = other.id; |
| 4851 |
expr = other.expr->clone_expr (); |
| 4852 |
} |
| 4853 |
|
| 4854 |
AnonConst operator= (const AnonConst &other) |
| 4855 |
{ |
| 4856 |
id = other.id; |
| 4857 |
expr = other.expr->clone_expr (); |
| 4858 |
return *this; |
| 4859 |
} |
| 4860 |
}; |
| 4861 |
|
| 4862 |
struct InlineAsmRegOrRegClass |
| 4863 |
{ |
| 4864 |
enum Type |
| 4865 |
{ |
| 4866 |
Reg, |
| 4867 |
RegClass, |
| 4868 |
}; |
| 4869 |
|
| 4870 |
struct Reg |
| 4871 |
{ |
| 4872 |
std::string Symbol; |
| 4873 |
}; |
| 4874 |
|
| 4875 |
struct RegClass |
| 4876 |
{ |
| 4877 |
std::string Symbol; |
| 4878 |
}; |
| 4879 |
|
| 4880 |
Type type; |
| 4881 |
struct Reg reg; |
| 4882 |
struct RegClass reg_class; |
| 4883 |
|
| 4884 |
Identifier name; |
| 4885 |
location_t locus; |
| 4886 |
}; |
| 4887 |
|
| 4888 |
class InlineAsmOperand |
| 4889 |
{ |
| 4890 |
public: |
| 4891 |
enum class RegisterType |
| 4892 |
{ |
| 4893 |
In, |
| 4894 |
Out, |
| 4895 |
InOut, |
| 4896 |
SplitInOut, |
| 4897 |
Const, |
| 4898 |
Sym, |
| 4899 |
Label, |
| 4900 |
}; |
| 4901 |
|
| 4902 |
class Register |
| 4903 |
{ |
| 4904 |
public: |
| 4905 |
Register () {} |
| 4906 |
virtual ~Register () = default; |
| 4907 |
|
| 4908 |
std::unique_ptr<Register> clone () const |
| 4909 |
{ |
| 4910 |
return std::unique_ptr<Register> (clone_impl ()); |
| 4911 |
} |
| 4912 |
|
| 4913 |
protected: |
| 4914 |
virtual Register *clone_impl () const = 0; |
| 4915 |
}; |
| 4916 |
|
| 4917 |
class In : public Register |
| 4918 |
{ |
| 4919 |
public: |
| 4920 |
tl::optional<InlineAsmRegOrRegClass> reg; |
| 4921 |
std::unique_ptr<Expr> expr; |
| 4922 |
|
| 4923 |
In (tl::optional<struct InlineAsmRegOrRegClass> ®, |
| 4924 |
std::unique_ptr<Expr> expr) |
| 4925 |
: reg (reg), expr (std::move (expr)) |
| 4926 |
{ |
| 4927 |
rust_assert (this->expr != nullptr); |
| 4928 |
} |
| 4929 |
|
| 4930 |
In (const In &other) |
| 4931 |
{ |
| 4932 |
reg = other.reg; |
| 4933 |
|
| 4934 |
expr = other.expr->clone_expr (); |
| 4935 |
} |
| 4936 |
|
| 4937 |
In operator= (const In &other) |
| 4938 |
{ |
| 4939 |
reg = other.reg; |
| 4940 |
expr = other.expr->clone_expr (); |
| 4941 |
|
| 4942 |
return *this; |
| 4943 |
} |
| 4944 |
|
| 4945 |
private: |
| 4946 |
In *clone_impl () const { return new In (*this); } |
| 4947 |
}; |
| 4948 |
|
| 4949 |
class Out : public Register |
| 4950 |
{ |
| 4951 |
public: |
| 4952 |
tl::optional<InlineAsmRegOrRegClass> reg; |
| 4953 |
bool late; |
| 4954 |
std::unique_ptr<Expr> expr; // can be null |
| 4955 |
|
| 4956 |
Out (tl::optional<struct InlineAsmRegOrRegClass> ®, bool late, |
| 4957 |
std::unique_ptr<Expr> expr) |
| 4958 |
: reg (reg), late (late), expr (std::move (expr)) |
| 4959 |
{ |
| 4960 |
rust_assert (this->expr != nullptr); |
| 4961 |
} |
| 4962 |
|
| 4963 |
Out (const Out &other) |
| 4964 |
{ |
| 4965 |
reg = other.reg; |
| 4966 |
late = other.late; |
| 4967 |
expr = other.expr->clone_expr (); |
| 4968 |
} |
| 4969 |
|
| 4970 |
Out operator= (const Out &other) |
| 4971 |
{ |
| 4972 |
reg = other.reg; |
| 4973 |
late = other.late; |
| 4974 |
expr = other.expr->clone_expr (); |
| 4975 |
return *this; |
| 4976 |
} |
| 4977 |
|
| 4978 |
private: |
| 4979 |
Out *clone_impl () const { return new Out (*this); } |
| 4980 |
}; |
| 4981 |
|
| 4982 |
class InOut : public Register |
| 4983 |
{ |
| 4984 |
public: |
| 4985 |
tl::optional<InlineAsmRegOrRegClass> reg; |
| 4986 |
bool late; |
| 4987 |
std::unique_ptr<Expr> expr; // this can't be null |
| 4988 |
|
| 4989 |
InOut (tl::optional<struct InlineAsmRegOrRegClass> ®, bool late, |
| 4990 |
std::unique_ptr<Expr> expr) |
| 4991 |
: reg (reg), late (late), expr (std::move (expr)) |
| 4992 |
{ |
| 4993 |
rust_assert (this->expr != nullptr); |
| 4994 |
} |
| 4995 |
|
| 4996 |
InOut (const InOut &other) |
| 4997 |
{ |
| 4998 |
reg = other.reg; |
| 4999 |
late = other.late; |
| 5000 |
expr = other.expr->clone_expr (); |
| 5001 |
} |
| 5002 |
|
| 5003 |
InOut operator= (const InOut &other) |
| 5004 |
{ |
| 5005 |
reg = other.reg; |
| 5006 |
late = other.late; |
| 5007 |
expr = other.expr->clone_expr (); |
| 5008 |
|
| 5009 |
return *this; |
| 5010 |
} |
| 5011 |
|
| 5012 |
private: |
| 5013 |
InOut *clone_impl () const { return new InOut (*this); } |
| 5014 |
}; |
| 5015 |
|
| 5016 |
class SplitInOut : public Register |
| 5017 |
{ |
| 5018 |
public: |
| 5019 |
tl::optional<InlineAsmRegOrRegClass> reg; |
| 5020 |
bool late; |
| 5021 |
std::unique_ptr<Expr> in_expr; |
| 5022 |
std::unique_ptr<Expr> out_expr; // could be null |
| 5023 |
|
| 5024 |
SplitInOut (tl::optional<struct InlineAsmRegOrRegClass> ®, bool late, |
| 5025 |
std::unique_ptr<Expr> in_expr, std::unique_ptr<Expr> out_expr) |
| 5026 |
: reg (reg), late (late), in_expr (std::move (in_expr)), |
| 5027 |
out_expr (std::move (out_expr)) |
| 5028 |
{ |
| 5029 |
rust_assert (this->in_expr != nullptr); |
| 5030 |
rust_assert (this->out_expr != nullptr); |
| 5031 |
} |
| 5032 |
|
| 5033 |
SplitInOut (const SplitInOut &other) |
| 5034 |
{ |
| 5035 |
reg = other.reg; |
| 5036 |
late = other.late; |
| 5037 |
in_expr = other.in_expr->clone_expr (); |
| 5038 |
out_expr = other.out_expr->clone_expr (); |
| 5039 |
} |
| 5040 |
|
| 5041 |
SplitInOut operator= (const SplitInOut &other) |
| 5042 |
{ |
| 5043 |
reg = other.reg; |
| 5044 |
late = other.late; |
| 5045 |
in_expr = other.in_expr->clone_expr (); |
| 5046 |
out_expr = other.out_expr->clone_expr (); |
| 5047 |
|
| 5048 |
return *this; |
| 5049 |
} |
| 5050 |
|
| 5051 |
private: |
| 5052 |
SplitInOut *clone_impl () const { return new SplitInOut (*this); } |
| 5053 |
}; |
| 5054 |
|
| 5055 |
class Const : public Register |
| 5056 |
{ |
| 5057 |
public: |
| 5058 |
AnonConst anon_const; |
| 5059 |
|
| 5060 |
private: |
| 5061 |
Const *clone_impl () const { return new Const (*this); } |
| 5062 |
}; |
| 5063 |
|
| 5064 |
class Sym : public Register |
| 5065 |
{ |
| 5066 |
public: |
| 5067 |
std::unique_ptr<Expr> expr; |
| 5068 |
|
| 5069 |
Sym (std::unique_ptr<Expr> expr) : expr (std::move (expr)) |
| 5070 |
{ |
| 5071 |
rust_assert (this->expr != nullptr); |
| 5072 |
} |
| 5073 |
Sym (const Sym &other) |
| 5074 |
{ |
| 5075 |
expr = std::unique_ptr<Expr> (other.expr->clone_expr ()); |
| 5076 |
} |
| 5077 |
|
| 5078 |
Sym operator= (const Sym &other) |
| 5079 |
{ |
| 5080 |
expr = std::unique_ptr<Expr> (other.expr->clone_expr ()); |
| 5081 |
return *this; |
| 5082 |
} |
| 5083 |
|
| 5084 |
private: |
| 5085 |
Sym *clone_impl () const { return new Sym (*this); } |
| 5086 |
}; |
| 5087 |
|
| 5088 |
class Label : public Register |
| 5089 |
{ |
| 5090 |
public: |
| 5091 |
std::string label_name; |
| 5092 |
std::unique_ptr<Expr> expr; |
| 5093 |
|
| 5094 |
Label (tl::optional<std::string> label_name, std::unique_ptr<Expr> expr) |
| 5095 |
: expr (std::move (expr)) |
| 5096 |
{ |
| 5097 |
rust_assert (this->expr != nullptr); |
| 5098 |
if (label_name.has_value ()) |
| 5099 |
this->label_name = label_name.value (); |
| 5100 |
} |
| 5101 |
Label (const Label &other) |
| 5102 |
{ |
| 5103 |
expr = std::unique_ptr<Expr> (other.expr->clone_expr ()); |
| 5104 |
} |
| 5105 |
|
| 5106 |
Label operator= (const Label &other) |
| 5107 |
{ |
| 5108 |
expr = std::unique_ptr<Expr> (other.expr->clone_expr ()); |
| 5109 |
return *this; |
| 5110 |
} |
| 5111 |
|
| 5112 |
private: |
| 5113 |
Label *clone_impl () const { return new Label (*this); } |
| 5114 |
}; |
| 5115 |
|
| 5116 |
InlineAsmOperand (const InlineAsmOperand &other) |
| 5117 |
: register_type (other.register_type), locus (other.locus), |
| 5118 |
reg (other.reg->clone ()) |
| 5119 |
{} |
| 5120 |
|
| 5121 |
InlineAsmOperand (const In ®, location_t locus) |
| 5122 |
: register_type (RegisterType::In), locus (locus), reg (new In (reg)) |
| 5123 |
{} |
| 5124 |
InlineAsmOperand (const Out ®, location_t locus) |
| 5125 |
: register_type (RegisterType::Out), locus (locus), reg (new Out (reg)) |
| 5126 |
{} |
| 5127 |
InlineAsmOperand (const InOut ®, location_t locus) |
| 5128 |
: register_type (RegisterType::InOut), locus (locus), reg (new InOut (reg)) |
| 5129 |
{} |
| 5130 |
InlineAsmOperand (const SplitInOut ®, location_t locus) |
| 5131 |
: register_type (RegisterType::SplitInOut), locus (locus), |
| 5132 |
reg (new SplitInOut (reg)) |
| 5133 |
{} |
| 5134 |
InlineAsmOperand (const Const ®, location_t locus) |
| 5135 |
: register_type (RegisterType::Const), locus (locus), reg (new Const (reg)) |
| 5136 |
{} |
| 5137 |
InlineAsmOperand (const Sym ®, location_t locus) |
| 5138 |
: register_type (RegisterType::Sym), locus (locus), reg (new Sym (reg)) |
| 5139 |
{} |
| 5140 |
InlineAsmOperand (const Label ®, location_t locus) |
| 5141 |
: register_type (RegisterType::Label), locus (locus), reg (new Label (reg)) |
| 5142 |
{} |
| 5143 |
|
| 5144 |
location_t get_locus () const { return locus; } |
| 5145 |
RegisterType get_register_type () const { return register_type; } |
| 5146 |
|
| 5147 |
// Potentially fail immediately if you don't use get_register_type() to |
| 5148 |
// inspect the RegisterType first before calling the following functions Check |
| 5149 |
// first |
| 5150 |
In &get_in () |
| 5151 |
{ |
| 5152 |
rust_assert (register_type == RegisterType::In); |
| 5153 |
return static_cast<In &> (*reg); |
| 5154 |
} |
| 5155 |
const In &get_in () const |
| 5156 |
{ |
| 5157 |
rust_assert (register_type == RegisterType::In); |
| 5158 |
return static_cast<const In &> (*reg); |
| 5159 |
} |
| 5160 |
|
| 5161 |
Out &get_out () |
| 5162 |
{ |
| 5163 |
rust_assert (register_type == RegisterType::Out); |
| 5164 |
return static_cast<Out &> (*reg); |
| 5165 |
} |
| 5166 |
const Out &get_out () const |
| 5167 |
{ |
| 5168 |
rust_assert (register_type == RegisterType::Out); |
| 5169 |
return static_cast<const Out &> (*reg); |
| 5170 |
} |
| 5171 |
|
| 5172 |
InOut &get_in_out () |
| 5173 |
{ |
| 5174 |
rust_assert (register_type == RegisterType::InOut); |
| 5175 |
return static_cast<InOut &> (*reg); |
| 5176 |
} |
| 5177 |
const InOut &get_in_out () const |
| 5178 |
{ |
| 5179 |
rust_assert (register_type == RegisterType::InOut); |
| 5180 |
return static_cast<const InOut &> (*reg); |
| 5181 |
} |
| 5182 |
|
| 5183 |
SplitInOut &get_split_in_out () |
| 5184 |
{ |
| 5185 |
rust_assert (register_type == RegisterType::SplitInOut); |
| 5186 |
return static_cast<SplitInOut &> (*reg); |
| 5187 |
} |
| 5188 |
const SplitInOut &get_split_in_out () const |
| 5189 |
{ |
| 5190 |
rust_assert (register_type == RegisterType::SplitInOut); |
| 5191 |
return static_cast<const SplitInOut &> (*reg); |
| 5192 |
} |
| 5193 |
|
| 5194 |
Const &get_const () |
| 5195 |
{ |
| 5196 |
rust_assert (register_type == RegisterType::Const); |
| 5197 |
return static_cast<Const &> (*reg); |
| 5198 |
} |
| 5199 |
const Const &get_const () const |
| 5200 |
{ |
| 5201 |
rust_assert (register_type == RegisterType::Const); |
| 5202 |
return static_cast<Const &> (*reg); |
| 5203 |
} |
| 5204 |
|
| 5205 |
Sym &get_sym () |
| 5206 |
{ |
| 5207 |
rust_assert (register_type == RegisterType::Sym); |
| 5208 |
return static_cast<Sym &> (*reg); |
| 5209 |
} |
| 5210 |
const Sym &get_sym () const |
| 5211 |
{ |
| 5212 |
rust_assert (register_type == RegisterType::Sym); |
| 5213 |
return static_cast<const Sym &> (*reg); |
| 5214 |
} |
| 5215 |
|
| 5216 |
Label &get_label () |
| 5217 |
{ |
| 5218 |
rust_assert (register_type == RegisterType::Label); |
| 5219 |
return static_cast<Label &> (*reg); |
| 5220 |
} |
| 5221 |
const Label &get_label () const |
| 5222 |
{ |
| 5223 |
rust_assert (register_type == RegisterType::Label); |
| 5224 |
return static_cast<const Label &> (*reg); |
| 5225 |
} |
| 5226 |
|
| 5227 |
private: |
| 5228 |
RegisterType register_type; |
| 5229 |
|
| 5230 |
location_t locus; |
| 5231 |
std::unique_ptr<Register> reg; |
| 5232 |
}; |
| 5233 |
|
| 5234 |
struct InlineAsmPlaceHolder |
| 5235 |
{ |
| 5236 |
size_t operand_idx; |
| 5237 |
char modifier; // can be null |
| 5238 |
location_t locus; |
| 5239 |
}; |
| 5240 |
|
| 5241 |
struct InlineAsmTemplatePiece |
| 5242 |
{ |
| 5243 |
bool is_placeholder; |
| 5244 |
std::string string; |
| 5245 |
InlineAsmPlaceHolder placeholder; |
| 5246 |
}; |
| 5247 |
|
| 5248 |
struct TupleClobber |
| 5249 |
{ |
| 5250 |
// as gccrs still doesn't contain a symbol class I have put them as strings |
| 5251 |
std::string symbol; |
| 5252 |
location_t loc; |
| 5253 |
}; |
| 5254 |
|
| 5255 |
struct TupleTemplateStr |
| 5256 |
{ |
| 5257 |
// as gccrs still doesn't contain a symbol class I have put them as strings |
| 5258 |
location_t loc; |
| 5259 |
std::string symbol; |
| 5260 |
|
| 5261 |
TupleTemplateStr (location_t loc, const std::string &symbol) |
| 5262 |
: loc (loc), symbol (symbol) |
| 5263 |
{} |
| 5264 |
}; |
| 5265 |
|
| 5266 |
// Inline Assembly Node |
| 5267 |
class InlineAsm : public ExprWithoutBlock |
| 5268 |
{ |
| 5269 |
private: |
| 5270 |
location_t locus; |
| 5271 |
// TODO: Not sure how outer_attrs plays with InlineAsm, I put it here in order |
| 5272 |
// to override, very hacky. |
| 5273 |
std::vector<Attribute> outer_attrs; |
| 5274 |
|
| 5275 |
public: |
| 5276 |
// https://github.com/rust-lang/rust/blob/55cac26a9ef17da1c9c77c0816e88e178b7cc5dd/compiler/rustc_builtin_macros/src/asm.rs#L56C1-L64C7 |
| 5277 |
// let mut args = AsmArgs { |
| 5278 |
// templates: vec![first_template], |
| 5279 |
// operands: vec![], |
| 5280 |
// named_args: Default::default(), |
| 5281 |
// reg_args: Default::default(), |
| 5282 |
// clobber_abis: Vec::new(), |
| 5283 |
// options: ast::InlineAsmOptions::empty(), |
| 5284 |
// options_spans: vec![], |
| 5285 |
// }; |
| 5286 |
std::vector<InlineAsmTemplatePiece> template_; |
| 5287 |
std::vector<TupleTemplateStr> template_strs; |
| 5288 |
std::vector<InlineAsmOperand> operands; |
| 5289 |
std::map<std::string, int> named_args; |
| 5290 |
std::set<int> reg_args; |
| 5291 |
std::vector<TupleClobber> clobber_abi; |
| 5292 |
std::set<InlineAsmOption> options; |
| 5293 |
|
| 5294 |
std::vector<location_t> line_spans; |
| 5295 |
|
| 5296 |
bool is_global_asm; |
| 5297 |
|
| 5298 |
InlineAsm (location_t locus, bool is_global_asm) |
| 5299 |
: locus (locus), is_global_asm (is_global_asm) |
| 5300 |
{} |
| 5301 |
|
| 5302 |
void accept_vis (ASTVisitor &vis) override; |
| 5303 |
std::string as_string () const override { return "InlineAsm AST Node"; } |
| 5304 |
|
| 5305 |
location_t get_locus () const override { return locus; } |
| 5306 |
|
| 5307 |
void mark_for_strip () override {} |
| 5308 |
|
| 5309 |
bool is_marked_for_strip () const override { return false; } |
| 5310 |
|
| 5311 |
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |
| 5312 |
|
| 5313 |
void set_outer_attrs (std::vector<Attribute> v) override { outer_attrs = v; } |
| 5314 |
|
| 5315 |
std::vector<InlineAsmTemplatePiece> get_template_ () { return template_; } |
| 5316 |
|
| 5317 |
std::vector<TupleTemplateStr> get_template_strs () { return template_strs; } |
| 5318 |
|
| 5319 |
std::vector<InlineAsmOperand> get_operands () { return operands; } |
| 5320 |
|
| 5321 |
std::vector<TupleClobber> get_clobber_abi () { return clobber_abi; } |
| 5322 |
|
| 5323 |
std::set<InlineAsmOption> get_options () { return options; } |
| 5324 |
|
| 5325 |
InlineAsm *clone_expr_without_block_impl () const override |
| 5326 |
{ |
| 5327 |
return new InlineAsm (*this); |
| 5328 |
} |
| 5329 |
|
| 5330 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::InlineAsm; } |
| 5331 |
}; |
| 5332 |
|
| 5333 |
} // namespace AST |
| 5334 |
} // namespace Rust |
| 5335 |
|
| 5336 |
#endif |