| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 5 |
// GCC is free software; you can redistribute it and/or modify it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#ifndef RUST_HIR_ITEM_H |
| 20 |
#define RUST_HIR_ITEM_H |
| 21 |
|
| 22 |
#include "optional.h" |
| 23 |
#include "rust-abi.h" |
| 24 |
#include "rust-hir-stmt.h" |
| 25 |
#include "rust-common.h" |
| 26 |
#include "rust-hir-visibility.h" |
| 27 |
#include "rust-hir-generic-param.h" |
| 28 |
#include "rust-system.h" |
| 29 |
|
| 30 |
namespace Rust { |
| 31 |
namespace HIR { |
| 32 |
|
| 33 |
// Rust "item" HIR node (declaration of top-level/module-level allowed stuff) |
| 34 |
class Item : public Stmt, public WithOuterAttrs |
| 35 |
{ |
| 36 |
// TODO: should outer attrs be defined here or in each derived class? |
| 37 |
public: |
| 38 |
enum class ItemKind |
| 39 |
{ |
| 40 |
Static, |
| 41 |
Constant, |
| 42 |
TypeAlias, |
| 43 |
Function, |
| 44 |
UseDeclaration, |
| 45 |
ExternBlock, |
| 46 |
ExternCrate, |
| 47 |
Struct, |
| 48 |
Union, |
| 49 |
Enum, |
| 50 |
EnumItem, // FIXME: ARTHUR: Do we need that? |
| 51 |
Trait, |
| 52 |
Impl, |
| 53 |
Module, |
| 54 |
}; |
| 55 |
|
| 56 |
static std::string item_kind_string (ItemKind kind); |
| 57 |
|
| 58 |
virtual ItemKind get_item_kind () const = 0; |
| 59 |
|
| 60 |
// Unique pointer custom clone function |
| 61 |
std::unique_ptr<Item> clone_item () const |
| 62 |
{ |
| 63 |
return std::unique_ptr<Item> (clone_item_impl ()); |
| 64 |
} |
| 65 |
|
| 66 |
BaseKind get_hir_kind () override { return Node::BaseKind::ITEM; } |
| 67 |
|
| 68 |
std::string as_string () const override; |
| 69 |
|
| 70 |
/* Adds crate names to the vector passed by reference, if it can |
| 71 |
* (polymorphism). */ |
| 72 |
virtual void |
| 73 |
add_crate_name (std::vector<std::string> &names ATTRIBUTE_UNUSED) const |
| 74 |
{} |
| 75 |
|
| 76 |
bool is_item () const override final { return true; } |
| 77 |
|
| 78 |
protected: |
| 79 |
// Constructor |
| 80 |
Item (Analysis::NodeMapping mappings, |
| 81 |
AST::AttrVec outer_attribs = AST::AttrVec ()) |
| 82 |
: Stmt (std::move (mappings)), WithOuterAttrs (std::move (outer_attribs)) |
| 83 |
{} |
| 84 |
|
| 85 |
// Clone function implementation as pure virtual method |
| 86 |
virtual Item *clone_item_impl () const = 0; |
| 87 |
|
| 88 |
/* Save having to specify two clone methods in derived classes by making |
| 89 |
* statement clone return item clone. Hopefully won't affect performance too |
| 90 |
* much. */ |
| 91 |
Item *clone_stmt_impl () const override { return clone_item_impl (); } |
| 92 |
}; |
| 93 |
|
| 94 |
// A type generic parameter (as opposed to a lifetime generic parameter) |
| 95 |
class TypeParam : public GenericParam |
| 96 |
{ |
| 97 |
AST::AttrVec outer_attrs; |
| 98 |
|
| 99 |
Identifier type_representation; |
| 100 |
|
| 101 |
// bool has_type_param_bounds; |
| 102 |
// TypeParamBounds type_param_bounds; |
| 103 |
std::vector<std::unique_ptr<TypeParamBound>> |
| 104 |
type_param_bounds; // inlined form |
| 105 |
|
| 106 |
tl::optional<std::unique_ptr<Type>> type; |
| 107 |
|
| 108 |
location_t locus; |
| 109 |
|
| 110 |
public: |
| 111 |
// Returns whether the type of the type param has been specified. |
| 112 |
bool has_type () const { return type.has_value (); } |
| 113 |
|
| 114 |
// Returns whether the type param has type param bounds. |
| 115 |
bool has_type_param_bounds () const { return !type_param_bounds.empty (); } |
| 116 |
|
| 117 |
// Returns whether the type param has an outer attribute. |
| 118 |
bool has_outer_attribute () const override { return outer_attrs.size () > 0; } |
| 119 |
AST::AttrVec &get_outer_attrs () override { return outer_attrs; } |
| 120 |
|
| 121 |
TypeParam (Analysis::NodeMapping mappings, Identifier type_representation, |
| 122 |
location_t locus = UNDEF_LOCATION, |
| 123 |
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds |
| 124 |
= std::vector<std::unique_ptr<TypeParamBound>> (), |
| 125 |
tl::optional<std::unique_ptr<Type>> type = tl::nullopt, |
| 126 |
AST::AttrVec outer_attrs = std::vector<AST::Attribute> ()); |
| 127 |
|
| 128 |
// Copy constructor uses clone |
| 129 |
TypeParam (TypeParam const &other); |
| 130 |
|
| 131 |
// Overloaded assignment operator to clone |
| 132 |
TypeParam &operator= (TypeParam const &other); |
| 133 |
|
| 134 |
// move constructors |
| 135 |
TypeParam (TypeParam &&other) = default; |
| 136 |
|
| 137 |
TypeParam &operator= (TypeParam &&other) = default; |
| 138 |
|
| 139 |
std::string as_string () const override; |
| 140 |
|
| 141 |
location_t get_locus () const override final { return locus; } |
| 142 |
|
| 143 |
void accept_vis (HIRFullVisitor &vis) override; |
| 144 |
|
| 145 |
Identifier get_type_representation () const { return type_representation; } |
| 146 |
|
| 147 |
Type &get_type () |
| 148 |
{ |
| 149 |
rust_assert (*type); |
| 150 |
return *type.value (); |
| 151 |
} |
| 152 |
|
| 153 |
Analysis::NodeMapping get_type_mappings () const; |
| 154 |
|
| 155 |
std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds (); |
| 156 |
|
| 157 |
protected: |
| 158 |
// Clone function implementation as (not pure) virtual method |
| 159 |
TypeParam *clone_generic_param_impl () const override |
| 160 |
{ |
| 161 |
return new TypeParam (*this); |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
/* "where" clause item base. Abstract - use LifetimeWhereClauseItem, |
| 166 |
* TypeBoundWhereClauseItem */ |
| 167 |
class WhereClauseItem : public FullVisitable |
| 168 |
{ |
| 169 |
public: |
| 170 |
enum ItemType |
| 171 |
{ |
| 172 |
LIFETIME, |
| 173 |
TYPE_BOUND, |
| 174 |
}; |
| 175 |
|
| 176 |
virtual ~WhereClauseItem () {} |
| 177 |
|
| 178 |
// Unique pointer custom clone function |
| 179 |
std::unique_ptr<WhereClauseItem> clone_where_clause_item () const |
| 180 |
{ |
| 181 |
return std::unique_ptr<WhereClauseItem> (clone_where_clause_item_impl ()); |
| 182 |
} |
| 183 |
|
| 184 |
virtual std::string as_string () const = 0; |
| 185 |
|
| 186 |
virtual void accept_vis (HIRFullVisitor &vis) = 0; |
| 187 |
|
| 188 |
virtual Analysis::NodeMapping get_mappings () const = 0; |
| 189 |
|
| 190 |
virtual ItemType get_item_type () const = 0; |
| 191 |
|
| 192 |
protected: |
| 193 |
// Clone function implementation as pure virtual method |
| 194 |
virtual WhereClauseItem *clone_where_clause_item_impl () const = 0; |
| 195 |
}; |
| 196 |
|
| 197 |
// A lifetime where clause item |
| 198 |
class LifetimeWhereClauseItem : public WhereClauseItem |
| 199 |
{ |
| 200 |
Lifetime lifetime; |
| 201 |
std::vector<Lifetime> lifetime_bounds; |
| 202 |
location_t locus; |
| 203 |
Analysis::NodeMapping mappings; |
| 204 |
|
| 205 |
public: |
| 206 |
LifetimeWhereClauseItem (Analysis::NodeMapping mappings, Lifetime lifetime, |
| 207 |
std::vector<Lifetime> lifetime_bounds, |
| 208 |
location_t locus) |
| 209 |
: lifetime (std::move (lifetime)), |
| 210 |
lifetime_bounds (std::move (lifetime_bounds)), locus (locus), |
| 211 |
mappings (std::move (mappings)) |
| 212 |
{} |
| 213 |
|
| 214 |
std::string as_string () const override; |
| 215 |
|
| 216 |
void accept_vis (HIRFullVisitor &vis) override; |
| 217 |
|
| 218 |
Lifetime &get_lifetime () { return lifetime; } |
| 219 |
|
| 220 |
std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; } |
| 221 |
|
| 222 |
Analysis::NodeMapping get_mappings () const override final |
| 223 |
{ |
| 224 |
return mappings; |
| 225 |
}; |
| 226 |
|
| 227 |
ItemType get_item_type () const override final |
| 228 |
{ |
| 229 |
return WhereClauseItem::ItemType::LIFETIME; |
| 230 |
} |
| 231 |
|
| 232 |
protected: |
| 233 |
// Clone function implementation as (not pure) virtual method |
| 234 |
LifetimeWhereClauseItem *clone_where_clause_item_impl () const override |
| 235 |
{ |
| 236 |
return new LifetimeWhereClauseItem (*this); |
| 237 |
} |
| 238 |
}; |
| 239 |
|
| 240 |
// A type bound where clause item |
| 241 |
class TypeBoundWhereClauseItem : public WhereClauseItem |
| 242 |
{ |
| 243 |
std::vector<LifetimeParam> for_lifetimes; |
| 244 |
std::unique_ptr<Type> bound_type; |
| 245 |
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds; |
| 246 |
Analysis::NodeMapping mappings; |
| 247 |
location_t locus; |
| 248 |
|
| 249 |
public: |
| 250 |
// Returns whether the item has ForLifetimes |
| 251 |
bool has_for_lifetimes () const { return !for_lifetimes.empty (); } |
| 252 |
|
| 253 |
// Returns whether the item has type param bounds |
| 254 |
bool has_type_param_bounds () const { return !type_param_bounds.empty (); } |
| 255 |
|
| 256 |
TypeBoundWhereClauseItem ( |
| 257 |
Analysis::NodeMapping mappings, std::vector<LifetimeParam> for_lifetimes, |
| 258 |
std::unique_ptr<Type> bound_type, |
| 259 |
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds, |
| 260 |
location_t locus); |
| 261 |
|
| 262 |
// Copy constructor requires clone |
| 263 |
TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other); |
| 264 |
|
| 265 |
// Overload assignment operator to clone |
| 266 |
TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other); |
| 267 |
|
| 268 |
// move constructors |
| 269 |
TypeBoundWhereClauseItem (TypeBoundWhereClauseItem &&other) = default; |
| 270 |
TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem &&other) |
| 271 |
= default; |
| 272 |
|
| 273 |
location_t get_locus () const { return locus; } |
| 274 |
|
| 275 |
std::string as_string () const override; |
| 276 |
|
| 277 |
void accept_vis (HIRFullVisitor &vis) override; |
| 278 |
|
| 279 |
std::vector<LifetimeParam> &get_for_lifetimes () { return for_lifetimes; } |
| 280 |
|
| 281 |
Type &get_bound_type () { return *bound_type; } |
| 282 |
|
| 283 |
std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds (); |
| 284 |
|
| 285 |
Analysis::NodeMapping get_mappings () const override final |
| 286 |
{ |
| 287 |
return mappings; |
| 288 |
}; |
| 289 |
|
| 290 |
ItemType get_item_type () const override final |
| 291 |
{ |
| 292 |
return WhereClauseItem::ItemType::TYPE_BOUND; |
| 293 |
} |
| 294 |
|
| 295 |
protected: |
| 296 |
// Clone function implementation as (not pure) virtual method |
| 297 |
TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override |
| 298 |
{ |
| 299 |
return new TypeBoundWhereClauseItem (*this); |
| 300 |
} |
| 301 |
}; |
| 302 |
|
| 303 |
// A where clause |
| 304 |
struct WhereClause |
| 305 |
{ |
| 306 |
private: |
| 307 |
std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items; |
| 308 |
|
| 309 |
// should this store location info? |
| 310 |
|
| 311 |
public: |
| 312 |
WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items) |
| 313 |
: where_clause_items (std::move (where_clause_items)) |
| 314 |
{} |
| 315 |
|
| 316 |
// copy constructor with vector clone |
| 317 |
WhereClause (WhereClause const &other) |
| 318 |
{ |
| 319 |
where_clause_items.reserve (other.where_clause_items.size ()); |
| 320 |
for (const auto &e : other.where_clause_items) |
| 321 |
where_clause_items.push_back (e->clone_where_clause_item ()); |
| 322 |
} |
| 323 |
|
| 324 |
// overloaded assignment operator with vector clone |
| 325 |
WhereClause &operator= (WhereClause const &other) |
| 326 |
{ |
| 327 |
where_clause_items.reserve (other.where_clause_items.size ()); |
| 328 |
for (const auto &e : other.where_clause_items) |
| 329 |
where_clause_items.push_back (e->clone_where_clause_item ()); |
| 330 |
|
| 331 |
return *this; |
| 332 |
} |
| 333 |
|
| 334 |
// move constructors |
| 335 |
WhereClause (WhereClause &&other) = default; |
| 336 |
WhereClause &operator= (WhereClause &&other) = default; |
| 337 |
|
| 338 |
// Creates a WhereClause with no items. |
| 339 |
static WhereClause create_empty () |
| 340 |
{ |
| 341 |
return WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> ()); |
| 342 |
} |
| 343 |
|
| 344 |
// Returns whether the WhereClause has no items. |
| 345 |
bool is_empty () const { return where_clause_items.empty (); } |
| 346 |
|
| 347 |
std::string as_string () const; |
| 348 |
|
| 349 |
std::vector<std::unique_ptr<WhereClauseItem>> &get_items () |
| 350 |
{ |
| 351 |
return where_clause_items; |
| 352 |
} |
| 353 |
const std::vector<std::unique_ptr<WhereClauseItem>> &get_items () const |
| 354 |
{ |
| 355 |
return where_clause_items; |
| 356 |
} |
| 357 |
}; |
| 358 |
|
| 359 |
// A self parameter in a method |
| 360 |
struct SelfParam |
| 361 |
{ |
| 362 |
public: |
| 363 |
enum ImplicitSelfKind |
| 364 |
{ |
| 365 |
IMM, // self |
| 366 |
MUT, // mut self |
| 367 |
IMM_REF, // &self |
| 368 |
MUT_REF, // &mut self |
| 369 |
NONE |
| 370 |
}; |
| 371 |
|
| 372 |
private: |
| 373 |
ImplicitSelfKind self_kind; |
| 374 |
tl::optional<Lifetime> lifetime; |
| 375 |
std::unique_ptr<Type> type; |
| 376 |
location_t locus; |
| 377 |
Analysis::NodeMapping mappings; |
| 378 |
|
| 379 |
SelfParam (Analysis::NodeMapping mappings, ImplicitSelfKind self_kind, |
| 380 |
tl::optional<Lifetime> lifetime, Type *type); |
| 381 |
|
| 382 |
public: |
| 383 |
// Type-based self parameter (not ref, no lifetime) |
| 384 |
SelfParam (Analysis::NodeMapping mappings, std::unique_ptr<Type> type, |
| 385 |
bool is_mut, location_t locus); |
| 386 |
|
| 387 |
// Lifetime-based self parameter (is ref, no type) |
| 388 |
SelfParam (Analysis::NodeMapping mappings, tl::optional<Lifetime> lifetime, |
| 389 |
bool is_mut, location_t locus); |
| 390 |
|
| 391 |
// Copy constructor requires clone |
| 392 |
SelfParam (SelfParam const &other); |
| 393 |
|
| 394 |
// Overload assignment operator to use clone |
| 395 |
SelfParam &operator= (SelfParam const &other); |
| 396 |
|
| 397 |
// move constructors |
| 398 |
SelfParam (SelfParam &&other) = default; |
| 399 |
SelfParam &operator= (SelfParam &&other) = default; |
| 400 |
|
| 401 |
// Returns whether the self-param has a type field. |
| 402 |
bool has_type () const { return type != nullptr; } |
| 403 |
|
| 404 |
// Returns whether the self-param has a valid lifetime. |
| 405 |
bool has_lifetime () const { return lifetime.has_value (); } |
| 406 |
|
| 407 |
const Lifetime &get_lifetime () const { return lifetime.value (); } |
| 408 |
|
| 409 |
std::string as_string () const; |
| 410 |
|
| 411 |
location_t get_locus () const { return locus; } |
| 412 |
|
| 413 |
ImplicitSelfKind get_self_kind () const { return self_kind; } |
| 414 |
|
| 415 |
Type &get_type () |
| 416 |
{ |
| 417 |
rust_assert (type); |
| 418 |
return *type; |
| 419 |
} |
| 420 |
|
| 421 |
Analysis::NodeMapping get_mappings () { return mappings; } |
| 422 |
|
| 423 |
Mutability get_mut () const; |
| 424 |
|
| 425 |
bool is_mut () const; |
| 426 |
|
| 427 |
bool is_ref () const; |
| 428 |
}; |
| 429 |
|
| 430 |
// Qualifiers for function, i.e. const, unsafe, extern etc. |
| 431 |
struct FunctionQualifiers |
| 432 |
{ |
| 433 |
private: |
| 434 |
Async async_status; |
| 435 |
Const const_status; |
| 436 |
Unsafety unsafety; |
| 437 |
bool has_extern; |
| 438 |
ABI abi; |
| 439 |
|
| 440 |
public: |
| 441 |
FunctionQualifiers (Async async_status, Const const_status, Unsafety unsafety, |
| 442 |
bool has_extern, ABI abi) |
| 443 |
: async_status (async_status), const_status (const_status), |
| 444 |
unsafety (unsafety), has_extern (has_extern), abi (abi) |
| 445 |
{} |
| 446 |
|
| 447 |
std::string as_string () const; |
| 448 |
|
| 449 |
Const get_const_status () const { return const_status; } |
| 450 |
|
| 451 |
bool is_const () const { return const_status == Const::Yes; } |
| 452 |
bool is_unsafe () const { return unsafety == Unsafety::Unsafe; } |
| 453 |
bool is_async () const { return async_status == Async::Yes; } |
| 454 |
|
| 455 |
ABI get_abi () const { return abi; } |
| 456 |
}; |
| 457 |
|
| 458 |
// A function parameter |
| 459 |
struct FunctionParam |
| 460 |
{ |
| 461 |
std::unique_ptr<Pattern> param_name; |
| 462 |
std::unique_ptr<Type> type; |
| 463 |
location_t locus; |
| 464 |
Analysis::NodeMapping mappings; |
| 465 |
|
| 466 |
public: |
| 467 |
FunctionParam (Analysis::NodeMapping mappings, |
| 468 |
std::unique_ptr<Pattern> param_name, |
| 469 |
std::unique_ptr<Type> param_type, location_t locus); |
| 470 |
|
| 471 |
// Copy constructor uses clone |
| 472 |
FunctionParam (FunctionParam const &other); |
| 473 |
|
| 474 |
// Overload assignment operator to use clone |
| 475 |
FunctionParam &operator= (FunctionParam const &other); |
| 476 |
|
| 477 |
// move constructors |
| 478 |
FunctionParam (FunctionParam &&other) = default; |
| 479 |
FunctionParam &operator= (FunctionParam &&other) = default; |
| 480 |
|
| 481 |
std::string as_string () const; |
| 482 |
|
| 483 |
location_t get_locus () const { return locus; } |
| 484 |
|
| 485 |
Pattern &get_param_name () { return *param_name; } |
| 486 |
|
| 487 |
Type &get_type () |
| 488 |
{ |
| 489 |
rust_assert (type); |
| 490 |
return *type; |
| 491 |
} |
| 492 |
|
| 493 |
const Analysis::NodeMapping &get_mappings () const { return mappings; } |
| 494 |
}; |
| 495 |
|
| 496 |
// Item that supports visibility - abstract base class |
| 497 |
class VisItem : public Item |
| 498 |
{ |
| 499 |
Visibility visibility; |
| 500 |
|
| 501 |
protected: |
| 502 |
// Visibility constructor |
| 503 |
VisItem (Analysis::NodeMapping mappings, Visibility visibility, |
| 504 |
AST::AttrVec outer_attrs = AST::AttrVec ()) |
| 505 |
: Item (std::move (mappings), std::move (outer_attrs)), |
| 506 |
visibility (std::move (visibility)) |
| 507 |
{} |
| 508 |
|
| 509 |
// Visibility copy constructor |
| 510 |
VisItem (VisItem const &other); |
| 511 |
|
| 512 |
// Overload assignment operator to clone |
| 513 |
VisItem &operator= (VisItem const &other); |
| 514 |
|
| 515 |
// move constructors |
| 516 |
VisItem (VisItem &&other) = default; |
| 517 |
VisItem &operator= (VisItem &&other) = default; |
| 518 |
|
| 519 |
public: |
| 520 |
using HIR::Stmt::accept_vis; |
| 521 |
|
| 522 |
BaseKind get_hir_kind () override final { return VIS_ITEM; } |
| 523 |
|
| 524 |
/* Does the item have some kind of public visibility (non-default |
| 525 |
* visibility)? */ |
| 526 |
bool has_visibility () const { return !visibility.is_error (); } |
| 527 |
|
| 528 |
virtual void accept_vis (HIRVisItemVisitor &vis) = 0; |
| 529 |
|
| 530 |
Visibility &get_visibility () { return visibility; } |
| 531 |
const Visibility &get_visibility () const { return visibility; } |
| 532 |
|
| 533 |
std::string as_string () const override; |
| 534 |
}; |
| 535 |
|
| 536 |
// Rust module item - abstract base class |
| 537 |
class Module : public VisItem, public WithInnerAttrs |
| 538 |
{ |
| 539 |
Identifier module_name; |
| 540 |
location_t locus; |
| 541 |
// bool has_items; |
| 542 |
std::vector<std::unique_ptr<Item>> items; |
| 543 |
|
| 544 |
public: |
| 545 |
std::string as_string () const override; |
| 546 |
|
| 547 |
// Returns whether the module has items in its body. |
| 548 |
bool has_items () const { return !items.empty (); } |
| 549 |
|
| 550 |
// Full constructor |
| 551 |
Module (Analysis::NodeMapping mappings, Identifier module_name, |
| 552 |
location_t locus, std::vector<std::unique_ptr<Item>> items, |
| 553 |
Visibility visibility = Visibility::create_error (), |
| 554 |
AST::AttrVec inner_attrs = AST::AttrVec (), |
| 555 |
AST::AttrVec outer_attrs = AST::AttrVec ()); |
| 556 |
|
| 557 |
// Copy constructor with vector clone |
| 558 |
Module (Module const &other); |
| 559 |
|
| 560 |
// Overloaded assignment operator with vector clone |
| 561 |
Module &operator= (Module const &other); |
| 562 |
|
| 563 |
// move constructors |
| 564 |
Module (Module &&other) = default; |
| 565 |
Module &operator= (Module &&other) = default; |
| 566 |
|
| 567 |
void accept_vis (HIRFullVisitor &vis) override; |
| 568 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 569 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 570 |
|
| 571 |
Identifier get_module_name () const { return module_name; } |
| 572 |
std::vector<std::unique_ptr<Item>> &get_items () { return items; }; |
| 573 |
|
| 574 |
/* Override that runs the function recursively on all items contained within |
| 575 |
* the module. */ |
| 576 |
void add_crate_name (std::vector<std::string> &names) const override; |
| 577 |
|
| 578 |
location_t get_locus () const override final { return locus; } |
| 579 |
|
| 580 |
ItemKind get_item_kind () const override { return ItemKind::Module; } |
| 581 |
|
| 582 |
protected: |
| 583 |
/* Use covariance to implement clone function as returning this object |
| 584 |
* rather than base */ |
| 585 |
Module *clone_item_impl () const override { return new Module (*this); } |
| 586 |
|
| 587 |
/* Use covariance to implement clone function as returning this object |
| 588 |
* rather than base */ |
| 589 |
/*virtual Module* clone_statement_impl() const override { |
| 590 |
return new Module(*this); |
| 591 |
}*/ |
| 592 |
}; |
| 593 |
|
| 594 |
// Rust extern crate declaration HIR node |
| 595 |
class ExternCrate : public VisItem |
| 596 |
{ |
| 597 |
// this is either an identifier or "self", with self parsed to string |
| 598 |
std::string referenced_crate; |
| 599 |
// bool has_as_clause; |
| 600 |
// AsClause as_clause; |
| 601 |
// this is either an identifier or "_", with _ parsed to string |
| 602 |
std::string as_clause_name; |
| 603 |
|
| 604 |
location_t locus; |
| 605 |
|
| 606 |
/* e.g. |
| 607 |
"extern crate foo as _" |
| 608 |
"extern crate foo" |
| 609 |
"extern crate std as cool_std" */ |
| 610 |
public: |
| 611 |
std::string as_string () const override; |
| 612 |
|
| 613 |
// Returns whether extern crate declaration has an as clause. |
| 614 |
bool has_as_clause () const { return !as_clause_name.empty (); } |
| 615 |
|
| 616 |
/* Returns whether extern crate declaration references the current crate |
| 617 |
* (i.e. self). */ |
| 618 |
bool references_self () const { return referenced_crate == "self"; } |
| 619 |
|
| 620 |
// Constructor |
| 621 |
ExternCrate (Analysis::NodeMapping mappings, std::string referenced_crate, |
| 622 |
Visibility visibility, AST::AttrVec outer_attrs, |
| 623 |
location_t locus, std::string as_clause_name = std::string ()) |
| 624 |
: VisItem (std::move (mappings), std::move (visibility), |
| 625 |
std::move (outer_attrs)), |
| 626 |
referenced_crate (std::move (referenced_crate)), |
| 627 |
as_clause_name (std::move (as_clause_name)), locus (locus) |
| 628 |
{} |
| 629 |
|
| 630 |
location_t get_locus () const override final { return locus; } |
| 631 |
|
| 632 |
ItemKind get_item_kind () const override { return ItemKind::ExternCrate; } |
| 633 |
std::string get_referenced_crate () { return referenced_crate; } |
| 634 |
std::string get_as_clause_name () { return as_clause_name; } |
| 635 |
|
| 636 |
void accept_vis (HIRFullVisitor &vis) override; |
| 637 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 638 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 639 |
|
| 640 |
// Override that adds extern crate name in decl to passed list of names. |
| 641 |
void add_crate_name (std::vector<std::string> &names) const override |
| 642 |
{ |
| 643 |
names.push_back (referenced_crate); |
| 644 |
} |
| 645 |
|
| 646 |
protected: |
| 647 |
/* Use covariance to implement clone function as returning this object |
| 648 |
* rather than base */ |
| 649 |
ExternCrate *clone_item_impl () const override |
| 650 |
{ |
| 651 |
return new ExternCrate (*this); |
| 652 |
} |
| 653 |
|
| 654 |
/* Use covariance to implement clone function as returning this object |
| 655 |
* rather than base */ |
| 656 |
/*virtual ExternCrate* clone_statement_impl() const override { |
| 657 |
return new ExternCrate(*this); |
| 658 |
}*/ |
| 659 |
}; |
| 660 |
|
| 661 |
// The path-ish thing referred to in a use declaration - abstract base class |
| 662 |
class UseTree : public FullVisitable |
| 663 |
{ |
| 664 |
location_t locus; |
| 665 |
|
| 666 |
public: |
| 667 |
virtual ~UseTree () {} |
| 668 |
|
| 669 |
// Unique pointer custom clone function |
| 670 |
std::unique_ptr<UseTree> clone_use_tree () const |
| 671 |
{ |
| 672 |
return std::unique_ptr<UseTree> (clone_use_tree_impl ()); |
| 673 |
} |
| 674 |
|
| 675 |
virtual std::string as_string () const = 0; |
| 676 |
|
| 677 |
location_t get_locus () const { return locus; } |
| 678 |
|
| 679 |
protected: |
| 680 |
// Clone function implementation as pure virtual method |
| 681 |
virtual UseTree *clone_use_tree_impl () const = 0; |
| 682 |
|
| 683 |
UseTree (location_t locus) : locus (locus) {} |
| 684 |
}; |
| 685 |
|
| 686 |
// Use tree with a glob (wildcard) operator |
| 687 |
class UseTreeGlob : public UseTree |
| 688 |
{ |
| 689 |
public: |
| 690 |
enum PathType |
| 691 |
{ |
| 692 |
NO_PATH, |
| 693 |
GLOBAL, |
| 694 |
PATH_PREFIXED |
| 695 |
}; |
| 696 |
|
| 697 |
private: |
| 698 |
PathType glob_type; |
| 699 |
AST::SimplePath path; |
| 700 |
|
| 701 |
public: |
| 702 |
UseTreeGlob (PathType glob_type, AST::SimplePath path, location_t locus) |
| 703 |
: UseTree (locus), glob_type (glob_type), path (std::move (path)) |
| 704 |
{ |
| 705 |
if (this->glob_type != PATH_PREFIXED) |
| 706 |
{ |
| 707 |
// compiler implementation error if there is a path with a |
| 708 |
// non-path-prefixed use tree glob |
| 709 |
gcc_assert (!has_path ()); |
| 710 |
} |
| 711 |
// TODO: do path-prefixed paths also have to have a path? If so, have an |
| 712 |
// assert for that too. |
| 713 |
} |
| 714 |
|
| 715 |
/* Returns whether has path. Should be made redundant by PathType |
| 716 |
* PATH_PREFIXED. */ |
| 717 |
bool has_path () const { return !path.is_empty (); } |
| 718 |
|
| 719 |
PathType get_glob_type () { return glob_type; } |
| 720 |
AST::SimplePath get_path () { return path; }; |
| 721 |
|
| 722 |
std::string as_string () const override; |
| 723 |
|
| 724 |
void accept_vis (HIRFullVisitor &vis) override; |
| 725 |
|
| 726 |
/* TODO: find way to ensure only PATH_PREFIXED glob_type has path - factory |
| 727 |
* methods? */ |
| 728 |
protected: |
| 729 |
/* Use covariance to implement clone function as returning this object |
| 730 |
* rather than base */ |
| 731 |
UseTreeGlob *clone_use_tree_impl () const override |
| 732 |
{ |
| 733 |
return new UseTreeGlob (*this); |
| 734 |
} |
| 735 |
}; |
| 736 |
|
| 737 |
// Use tree with a list of paths with a common prefix |
| 738 |
class UseTreeList : public UseTree |
| 739 |
{ |
| 740 |
public: |
| 741 |
enum PathType |
| 742 |
{ |
| 743 |
NO_PATH, |
| 744 |
GLOBAL, |
| 745 |
PATH_PREFIXED |
| 746 |
}; |
| 747 |
|
| 748 |
private: |
| 749 |
PathType path_type; |
| 750 |
AST::SimplePath path; |
| 751 |
|
| 752 |
std::vector<std::unique_ptr<UseTree>> trees; |
| 753 |
|
| 754 |
public: |
| 755 |
UseTreeList (PathType path_type, AST::SimplePath path, |
| 756 |
std::vector<std::unique_ptr<UseTree>> trees, location_t locus) |
| 757 |
: UseTree (locus), path_type (path_type), path (std::move (path)), |
| 758 |
trees (std::move (trees)) |
| 759 |
{ |
| 760 |
if (this->path_type != PATH_PREFIXED) |
| 761 |
{ |
| 762 |
// compiler implementation error if there is a path with a |
| 763 |
// non-path-prefixed use tree glob |
| 764 |
gcc_assert (!has_path ()); |
| 765 |
} |
| 766 |
// TODO: do path-prefixed paths also have to have a path? If so, have an |
| 767 |
// assert for that too. |
| 768 |
} |
| 769 |
|
| 770 |
// copy constructor with vector clone |
| 771 |
UseTreeList (UseTreeList const &other) |
| 772 |
: UseTree (other), path_type (other.path_type), path (other.path) |
| 773 |
{ |
| 774 |
trees.reserve (other.trees.size ()); |
| 775 |
for (const auto &e : other.trees) |
| 776 |
trees.push_back (e->clone_use_tree ()); |
| 777 |
} |
| 778 |
|
| 779 |
// overloaded assignment operator with vector clone |
| 780 |
UseTreeList &operator= (UseTreeList const &other) |
| 781 |
{ |
| 782 |
UseTree::operator= (other); |
| 783 |
path_type = other.path_type; |
| 784 |
path = other.path; |
| 785 |
|
| 786 |
trees.reserve (other.trees.size ()); |
| 787 |
for (const auto &e : other.trees) |
| 788 |
trees.push_back (e->clone_use_tree ()); |
| 789 |
|
| 790 |
return *this; |
| 791 |
} |
| 792 |
|
| 793 |
// move constructors |
| 794 |
UseTreeList (UseTreeList &&other) = default; |
| 795 |
UseTreeList &operator= (UseTreeList &&other) = default; |
| 796 |
|
| 797 |
// Returns whether has path. Should be made redundant by path_type. |
| 798 |
bool has_path () const { return !path.is_empty (); } |
| 799 |
|
| 800 |
// Returns whether has inner tree elements. |
| 801 |
bool has_trees () const { return !trees.empty (); } |
| 802 |
|
| 803 |
std::string as_string () const override; |
| 804 |
|
| 805 |
void accept_vis (HIRFullVisitor &vis) override; |
| 806 |
|
| 807 |
PathType get_path_type () { return path_type; } |
| 808 |
AST::SimplePath get_path () { return path; } |
| 809 |
std::vector<std::unique_ptr<UseTree>> &get_trees () { return trees; } |
| 810 |
|
| 811 |
// TODO: find way to ensure only PATH_PREFIXED path_type has path - factory |
| 812 |
// methods? |
| 813 |
protected: |
| 814 |
/* Use covariance to implement clone function as returning this object |
| 815 |
* rather than base */ |
| 816 |
UseTreeList *clone_use_tree_impl () const override |
| 817 |
{ |
| 818 |
return new UseTreeList (*this); |
| 819 |
} |
| 820 |
}; |
| 821 |
|
| 822 |
// Use tree where it rebinds the module name as something else |
| 823 |
class UseTreeRebind : public UseTree |
| 824 |
{ |
| 825 |
public: |
| 826 |
enum NewBindType |
| 827 |
{ |
| 828 |
NONE, |
| 829 |
IDENTIFIER, |
| 830 |
WILDCARD |
| 831 |
}; |
| 832 |
|
| 833 |
private: |
| 834 |
AST::SimplePath path; |
| 835 |
|
| 836 |
NewBindType bind_type; |
| 837 |
Identifier identifier; // only if NewBindType is IDENTIFIER |
| 838 |
|
| 839 |
public: |
| 840 |
UseTreeRebind (NewBindType bind_type, AST::SimplePath path, location_t locus, |
| 841 |
Identifier identifier = std::string ()) |
| 842 |
: UseTree (locus), path (std::move (path)), bind_type (bind_type), |
| 843 |
identifier (std::move (identifier)) |
| 844 |
{} |
| 845 |
|
| 846 |
// Returns whether has path (this should always be true). |
| 847 |
bool has_path () const { return !path.is_empty (); } |
| 848 |
|
| 849 |
AST::SimplePath get_path () { return path; } |
| 850 |
|
| 851 |
Identifier get_identifier () const { return identifier; } |
| 852 |
|
| 853 |
NewBindType get_bind_type () const { return bind_type; } |
| 854 |
|
| 855 |
// Returns whether has identifier (or, rather, is allowed to). |
| 856 |
bool has_identifier () const { return bind_type == IDENTIFIER; } |
| 857 |
|
| 858 |
std::string as_string () const override; |
| 859 |
|
| 860 |
void accept_vis (HIRFullVisitor &vis) override; |
| 861 |
|
| 862 |
// TODO: find way to ensure only PATH_PREFIXED path_type has path - factory |
| 863 |
// methods? |
| 864 |
protected: |
| 865 |
/* Use covariance to implement clone function as returning this object |
| 866 |
* rather than base */ |
| 867 |
virtual UseTreeRebind *clone_use_tree_impl () const override |
| 868 |
{ |
| 869 |
return new UseTreeRebind (*this); |
| 870 |
} |
| 871 |
}; |
| 872 |
|
| 873 |
std::string enum_to_str (UseTreeRebind::NewBindType); |
| 874 |
|
| 875 |
// Rust use declaration (i.e. for modules) HIR node |
| 876 |
class UseDeclaration : public VisItem |
| 877 |
{ |
| 878 |
std::unique_ptr<UseTree> use_tree; |
| 879 |
location_t locus; |
| 880 |
|
| 881 |
public: |
| 882 |
std::string as_string () const override; |
| 883 |
|
| 884 |
UseDeclaration (Analysis::NodeMapping mappings, |
| 885 |
std::unique_ptr<UseTree> use_tree, Visibility visibility, |
| 886 |
AST::AttrVec outer_attrs, location_t locus) |
| 887 |
: VisItem (std::move (mappings), std::move (visibility), |
| 888 |
std::move (outer_attrs)), |
| 889 |
use_tree (std::move (use_tree)), locus (locus) |
| 890 |
{} |
| 891 |
|
| 892 |
// Copy constructor with clone |
| 893 |
UseDeclaration (UseDeclaration const &other) |
| 894 |
: VisItem (other), use_tree (other.use_tree->clone_use_tree ()), |
| 895 |
locus (other.locus) |
| 896 |
{} |
| 897 |
|
| 898 |
// Overloaded assignment operator to clone |
| 899 |
UseDeclaration &operator= (UseDeclaration const &other) |
| 900 |
{ |
| 901 |
VisItem::operator= (other); |
| 902 |
use_tree = other.use_tree->clone_use_tree (); |
| 903 |
// visibility = other.visibility->clone_visibility(); |
| 904 |
// outer_attrs = other.outer_attrs; |
| 905 |
locus = other.locus; |
| 906 |
|
| 907 |
return *this; |
| 908 |
} |
| 909 |
|
| 910 |
// move constructors |
| 911 |
UseDeclaration (UseDeclaration &&other) = default; |
| 912 |
UseDeclaration &operator= (UseDeclaration &&other) = default; |
| 913 |
|
| 914 |
location_t get_locus () const override final { return locus; } |
| 915 |
ItemKind get_item_kind () const override { return ItemKind::UseDeclaration; } |
| 916 |
|
| 917 |
UseTree &get_use_tree () { return *use_tree; } |
| 918 |
void accept_vis (HIRFullVisitor &vis) override; |
| 919 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 920 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 921 |
|
| 922 |
protected: |
| 923 |
/* Use covariance to implement clone function as returning this object |
| 924 |
* rather than base */ |
| 925 |
UseDeclaration *clone_item_impl () const override |
| 926 |
{ |
| 927 |
return new UseDeclaration (*this); |
| 928 |
} |
| 929 |
|
| 930 |
/* Use covariance to implement clone function as returning this object |
| 931 |
* rather than base */ |
| 932 |
/*virtual UseDeclaration* clone_statement_impl() const override { |
| 933 |
return new UseDeclaration(*this); |
| 934 |
}*/ |
| 935 |
}; |
| 936 |
|
| 937 |
class LetStmt; |
| 938 |
|
| 939 |
enum class Defaultness |
| 940 |
{ |
| 941 |
Default, |
| 942 |
Final, |
| 943 |
}; |
| 944 |
|
| 945 |
// Rust function declaration HIR node |
| 946 |
class Function : public VisItem, public ImplItem |
| 947 |
{ |
| 948 |
FunctionQualifiers qualifiers; |
| 949 |
Identifier function_name; |
| 950 |
std::vector<std::unique_ptr<GenericParam>> generic_params; |
| 951 |
std::vector<FunctionParam> function_params; |
| 952 |
std::unique_ptr<Type> return_type; |
| 953 |
WhereClause where_clause; |
| 954 |
std::unique_ptr<BlockExpr> function_body; |
| 955 |
tl::optional<SelfParam> self; |
| 956 |
location_t locus; |
| 957 |
|
| 958 |
// NOTE: This should be moved to the trait item base class once we start |
| 959 |
// implementing specialization for real, instead of just stubbing out the |
| 960 |
// feature |
| 961 |
Defaultness defaultness; |
| 962 |
|
| 963 |
public: |
| 964 |
std::string as_string () const override; |
| 965 |
|
| 966 |
// Returns whether function has generic parameters. |
| 967 |
bool has_generics () const { return !generic_params.empty (); } |
| 968 |
|
| 969 |
// Returns whether function has regular parameters. |
| 970 |
bool has_function_params () const { return !function_params.empty (); } |
| 971 |
|
| 972 |
// Returns whether function has return type - if not, it is void. |
| 973 |
bool has_function_return_type () const { return return_type != nullptr; } |
| 974 |
|
| 975 |
// Returns whether function has a where clause. |
| 976 |
bool has_where_clause () const { return !where_clause.is_empty (); } |
| 977 |
|
| 978 |
// Returns whether function has a default qualifier |
| 979 |
bool is_default () const { return defaultness == Defaultness::Default; } |
| 980 |
|
| 981 |
ImplItemType get_impl_item_type () const override final |
| 982 |
{ |
| 983 |
return ImplItem::ImplItemType::FUNCTION; |
| 984 |
} |
| 985 |
|
| 986 |
ItemKind get_item_kind () const override { return ItemKind::Function; } |
| 987 |
|
| 988 |
// Mega-constructor with all possible fields |
| 989 |
Function (Analysis::NodeMapping mappings, Identifier function_name, |
| 990 |
FunctionQualifiers qualifiers, |
| 991 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 992 |
std::vector<FunctionParam> function_params, |
| 993 |
std::unique_ptr<Type> return_type, WhereClause where_clause, |
| 994 |
std::unique_ptr<BlockExpr> function_body, Visibility vis, |
| 995 |
AST::AttrVec outer_attrs, tl::optional<SelfParam> self, |
| 996 |
Defaultness defaultness, location_t locus); |
| 997 |
|
| 998 |
// Copy constructor with clone |
| 999 |
Function (Function const &other); |
| 1000 |
|
| 1001 |
// Overloaded assignment operator to clone |
| 1002 |
Function &operator= (Function const &other); |
| 1003 |
|
| 1004 |
// move constructors |
| 1005 |
Function (Function &&other) = default; |
| 1006 |
Function &operator= (Function &&other) = default; |
| 1007 |
|
| 1008 |
location_t get_locus () const override final { return locus; } |
| 1009 |
|
| 1010 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1011 |
void accept_vis (HIRImplVisitor &vis) override; |
| 1012 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1013 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 1014 |
|
| 1015 |
Analysis::NodeMapping get_impl_mappings () const override |
| 1016 |
{ |
| 1017 |
return get_mappings (); |
| 1018 |
}; |
| 1019 |
|
| 1020 |
std::vector<FunctionParam> &get_function_params () { return function_params; } |
| 1021 |
const std::vector<FunctionParam> &get_function_params () const |
| 1022 |
{ |
| 1023 |
return function_params; |
| 1024 |
} |
| 1025 |
|
| 1026 |
std::vector<std::unique_ptr<GenericParam>> &get_generic_params () |
| 1027 |
{ |
| 1028 |
return generic_params; |
| 1029 |
} |
| 1030 |
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const |
| 1031 |
{ |
| 1032 |
return generic_params; |
| 1033 |
} |
| 1034 |
|
| 1035 |
// TODO: is this better? Or is a "vis_block" better? |
| 1036 |
BlockExpr &get_definition () { return *function_body; } |
| 1037 |
|
| 1038 |
const FunctionQualifiers &get_qualifiers () const { return qualifiers; } |
| 1039 |
|
| 1040 |
Identifier get_function_name () const { return function_name; } |
| 1041 |
|
| 1042 |
// TODO: is this better? Or is a "vis_block" better? |
| 1043 |
WhereClause &get_where_clause () { return where_clause; } |
| 1044 |
|
| 1045 |
bool has_return_type () const { return return_type != nullptr; } |
| 1046 |
|
| 1047 |
// TODO: is this better? Or is a "vis_block" better? |
| 1048 |
Type &get_return_type () { return *return_type; } |
| 1049 |
|
| 1050 |
bool is_method () const { return self.has_value (); } |
| 1051 |
|
| 1052 |
tl::optional<SelfParam> &get_self_param () { return self; } |
| 1053 |
const tl::optional<SelfParam> &get_self_param () const { return self; } |
| 1054 |
|
| 1055 |
SelfParam &get_self_param_unchecked () { return self.value (); } |
| 1056 |
const SelfParam &get_self_param_unchecked () const { return self.value (); } |
| 1057 |
|
| 1058 |
std::string get_impl_item_name () const override final |
| 1059 |
{ |
| 1060 |
return get_function_name ().as_string (); |
| 1061 |
} |
| 1062 |
|
| 1063 |
protected: |
| 1064 |
/* Use covariance to implement clone function as returning this object |
| 1065 |
* rather than base */ |
| 1066 |
Function *clone_item_impl () const override { return new Function (*this); } |
| 1067 |
|
| 1068 |
/* Use covariance to implement clone function as returning this object |
| 1069 |
* rather than base */ |
| 1070 |
Function *clone_inherent_impl_item_impl () const override |
| 1071 |
{ |
| 1072 |
return new Function (*this); |
| 1073 |
} |
| 1074 |
}; |
| 1075 |
|
| 1076 |
// Rust type alias (i.e. typedef) HIR node |
| 1077 |
class TypeAlias : public VisItem, public ImplItem |
| 1078 |
{ |
| 1079 |
Identifier new_type_name; |
| 1080 |
|
| 1081 |
// bool has_generics; |
| 1082 |
// Generics generic_params; |
| 1083 |
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined |
| 1084 |
|
| 1085 |
// bool has_where_clause; |
| 1086 |
WhereClause where_clause; |
| 1087 |
|
| 1088 |
std::unique_ptr<Type> existing_type; |
| 1089 |
|
| 1090 |
location_t locus; |
| 1091 |
|
| 1092 |
public: |
| 1093 |
std::string as_string () const override; |
| 1094 |
|
| 1095 |
// Returns whether type alias has generic parameters. |
| 1096 |
bool has_generics () const { return !generic_params.empty (); } |
| 1097 |
|
| 1098 |
// Returns whether type alias has a where clause. |
| 1099 |
bool has_where_clause () const { return !where_clause.is_empty (); } |
| 1100 |
|
| 1101 |
ImplItemType get_impl_item_type () const override final |
| 1102 |
{ |
| 1103 |
return ImplItem::ImplItemType::TYPE_ALIAS; |
| 1104 |
} |
| 1105 |
|
| 1106 |
// Mega-constructor with all possible fields |
| 1107 |
TypeAlias (Analysis::NodeMapping mappings, Identifier new_type_name, |
| 1108 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 1109 |
WhereClause where_clause, std::unique_ptr<Type> existing_type, |
| 1110 |
Visibility vis, AST::AttrVec outer_attrs, location_t locus); |
| 1111 |
|
| 1112 |
// Copy constructor |
| 1113 |
TypeAlias (TypeAlias const &other); |
| 1114 |
|
| 1115 |
// Overloaded assignment operator to clone |
| 1116 |
TypeAlias &operator= (TypeAlias const &other); |
| 1117 |
|
| 1118 |
// move constructors |
| 1119 |
TypeAlias (TypeAlias &&other) = default; |
| 1120 |
TypeAlias &operator= (TypeAlias &&other) = default; |
| 1121 |
|
| 1122 |
location_t get_locus () const override final { return locus; } |
| 1123 |
|
| 1124 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1125 |
void accept_vis (HIRImplVisitor &vis) override; |
| 1126 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1127 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 1128 |
|
| 1129 |
std::vector<std::unique_ptr<GenericParam>> &get_generic_params () |
| 1130 |
{ |
| 1131 |
return generic_params; |
| 1132 |
} |
| 1133 |
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const |
| 1134 |
{ |
| 1135 |
return generic_params; |
| 1136 |
} |
| 1137 |
|
| 1138 |
WhereClause &get_where_clause () { return where_clause; } |
| 1139 |
|
| 1140 |
Type &get_type_aliased () |
| 1141 |
{ |
| 1142 |
rust_assert (existing_type); |
| 1143 |
return *existing_type; |
| 1144 |
} |
| 1145 |
|
| 1146 |
Identifier get_new_type_name () const { return new_type_name; } |
| 1147 |
|
| 1148 |
ItemKind get_item_kind () const override { return ItemKind::TypeAlias; } |
| 1149 |
|
| 1150 |
Analysis::NodeMapping get_impl_mappings () const override |
| 1151 |
{ |
| 1152 |
return get_mappings (); |
| 1153 |
}; |
| 1154 |
|
| 1155 |
std::string get_impl_item_name () const override final |
| 1156 |
{ |
| 1157 |
return get_new_type_name ().as_string (); |
| 1158 |
} |
| 1159 |
|
| 1160 |
protected: |
| 1161 |
/* Use covariance to implement clone function as returning this object |
| 1162 |
* rather than base */ |
| 1163 |
TypeAlias *clone_item_impl () const override { return new TypeAlias (*this); } |
| 1164 |
|
| 1165 |
/* Use covariance to implement clone function as returning this object |
| 1166 |
* rather than base */ |
| 1167 |
TypeAlias *clone_inherent_impl_item_impl () const override |
| 1168 |
{ |
| 1169 |
return new TypeAlias (*this); |
| 1170 |
} |
| 1171 |
}; |
| 1172 |
|
| 1173 |
// Rust base struct declaration HIR node - abstract base class |
| 1174 |
class Struct : public VisItem |
| 1175 |
{ |
| 1176 |
protected: |
| 1177 |
// protected to enable access by derived classes - allows better as_string |
| 1178 |
Identifier struct_name; |
| 1179 |
|
| 1180 |
// bool has_generics; |
| 1181 |
// Generics generic_params; |
| 1182 |
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined |
| 1183 |
|
| 1184 |
// bool has_where_clause; |
| 1185 |
WhereClause where_clause; |
| 1186 |
|
| 1187 |
location_t locus; |
| 1188 |
|
| 1189 |
public: |
| 1190 |
Identifier get_identifier () const { return struct_name; } |
| 1191 |
|
| 1192 |
// Returns whether struct has generic parameters. |
| 1193 |
bool has_generics () const { return !generic_params.empty (); } |
| 1194 |
|
| 1195 |
// Returns whether struct has a where clause. |
| 1196 |
bool has_where_clause () const { return !where_clause.is_empty (); } |
| 1197 |
|
| 1198 |
location_t get_locus () const override final { return locus; } |
| 1199 |
ItemKind get_item_kind () const override { return ItemKind::Struct; } |
| 1200 |
|
| 1201 |
std::vector<std::unique_ptr<GenericParam>> &get_generic_params () |
| 1202 |
{ |
| 1203 |
return generic_params; |
| 1204 |
} |
| 1205 |
|
| 1206 |
WhereClause &get_where_clause () { return where_clause; } |
| 1207 |
|
| 1208 |
protected: |
| 1209 |
Struct (Analysis::NodeMapping mappings, Identifier struct_name, |
| 1210 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 1211 |
WhereClause where_clause, Visibility vis, location_t locus, |
| 1212 |
AST::AttrVec outer_attrs = AST::AttrVec ()) |
| 1213 |
: VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), |
| 1214 |
struct_name (std::move (struct_name)), |
| 1215 |
generic_params (std::move (generic_params)), |
| 1216 |
where_clause (std::move (where_clause)), locus (locus) |
| 1217 |
{} |
| 1218 |
|
| 1219 |
// Copy constructor with vector clone |
| 1220 |
Struct (Struct const &other) |
| 1221 |
: VisItem (other), struct_name (other.struct_name), |
| 1222 |
where_clause (other.where_clause), locus (other.locus) |
| 1223 |
{ |
| 1224 |
generic_params.reserve (other.generic_params.size ()); |
| 1225 |
for (const auto &e : other.generic_params) |
| 1226 |
generic_params.push_back (e->clone_generic_param ()); |
| 1227 |
} |
| 1228 |
|
| 1229 |
// Overloaded assignment operator with vector clone |
| 1230 |
Struct &operator= (Struct const &other) |
| 1231 |
{ |
| 1232 |
VisItem::operator= (other); |
| 1233 |
struct_name = other.struct_name; |
| 1234 |
where_clause = other.where_clause; |
| 1235 |
locus = other.locus; |
| 1236 |
|
| 1237 |
generic_params.reserve (other.generic_params.size ()); |
| 1238 |
for (const auto &e : other.generic_params) |
| 1239 |
generic_params.push_back (e->clone_generic_param ()); |
| 1240 |
|
| 1241 |
return *this; |
| 1242 |
} |
| 1243 |
|
| 1244 |
// move constructors |
| 1245 |
Struct (Struct &&other) = default; |
| 1246 |
Struct &operator= (Struct &&other) = default; |
| 1247 |
}; |
| 1248 |
|
| 1249 |
// A single field in a struct |
| 1250 |
// FIXME can't this be a TupleStruct + field_name? |
| 1251 |
class StructField |
| 1252 |
{ |
| 1253 |
public: |
| 1254 |
// bool has_outer_attributes; |
| 1255 |
AST::AttrVec outer_attrs; |
| 1256 |
|
| 1257 |
// bool has_visibility; |
| 1258 |
Visibility visibility; |
| 1259 |
|
| 1260 |
Identifier field_name; |
| 1261 |
std::unique_ptr<Type> field_type; |
| 1262 |
|
| 1263 |
Analysis::NodeMapping mappings; |
| 1264 |
|
| 1265 |
location_t locus; |
| 1266 |
|
| 1267 |
// Returns whether struct field has any outer attributes. |
| 1268 |
bool has_outer_attributes () const { return !outer_attrs.empty (); } |
| 1269 |
|
| 1270 |
// Returns whether struct field has a non-private (non-default) visibility. |
| 1271 |
bool has_visibility () const { return !visibility.is_error (); } |
| 1272 |
|
| 1273 |
StructField (Analysis::NodeMapping mappings, Identifier field_name, |
| 1274 |
std::unique_ptr<Type> field_type, Visibility vis, |
| 1275 |
location_t locus, AST::AttrVec outer_attrs = AST::AttrVec ()); |
| 1276 |
|
| 1277 |
// Copy constructor |
| 1278 |
StructField (StructField const &other); |
| 1279 |
|
| 1280 |
~StructField () = default; |
| 1281 |
|
| 1282 |
// Overloaded assignment operator to clone |
| 1283 |
StructField &operator= (StructField const &other); |
| 1284 |
|
| 1285 |
// move constructors |
| 1286 |
StructField (StructField &&other) = default; |
| 1287 |
StructField &operator= (StructField &&other) = default; |
| 1288 |
|
| 1289 |
std::string as_string () const; |
| 1290 |
|
| 1291 |
Identifier get_field_name () const { return field_name; } |
| 1292 |
|
| 1293 |
Type &get_field_type () { return *field_type; } |
| 1294 |
|
| 1295 |
Analysis::NodeMapping get_mappings () const { return mappings; } |
| 1296 |
|
| 1297 |
location_t get_locus () { return locus; } |
| 1298 |
AST::AttrVec &get_outer_attrs () { return outer_attrs; } |
| 1299 |
Visibility &get_visibility () { return visibility; } |
| 1300 |
}; |
| 1301 |
|
| 1302 |
// Rust struct declaration with true struct type HIR node |
| 1303 |
class StructStruct : public Struct |
| 1304 |
{ |
| 1305 |
public: |
| 1306 |
std::vector<StructField> fields; |
| 1307 |
bool is_unit; |
| 1308 |
|
| 1309 |
std::string as_string () const override; |
| 1310 |
|
| 1311 |
// Mega-constructor with all possible fields |
| 1312 |
StructStruct (Analysis::NodeMapping mappings, std::vector<StructField> fields, |
| 1313 |
Identifier struct_name, |
| 1314 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 1315 |
WhereClause where_clause, bool is_unit, Visibility vis, |
| 1316 |
AST::AttrVec outer_attrs, location_t locus) |
| 1317 |
: Struct (std::move (mappings), std::move (struct_name), |
| 1318 |
std::move (generic_params), std::move (where_clause), |
| 1319 |
std::move (vis), locus, std::move (outer_attrs)), |
| 1320 |
fields (std::move (fields)), is_unit (is_unit) |
| 1321 |
{} |
| 1322 |
|
| 1323 |
// Unit struct constructor |
| 1324 |
StructStruct (Analysis::NodeMapping mappings, Identifier struct_name, |
| 1325 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 1326 |
WhereClause where_clause, Visibility vis, |
| 1327 |
AST::AttrVec outer_attrs, location_t locus) |
| 1328 |
: Struct (std::move (mappings), std::move (struct_name), |
| 1329 |
std::move (generic_params), std::move (where_clause), |
| 1330 |
std::move (vis), locus, std::move (outer_attrs)), |
| 1331 |
is_unit (true) |
| 1332 |
{} |
| 1333 |
// TODO: can a unit struct have generic fields? assuming yes for now. |
| 1334 |
|
| 1335 |
/* Returns whether the struct is a unit struct - struct defined without |
| 1336 |
* fields. This is important because it also means an implicit constant of its |
| 1337 |
* type is defined. */ |
| 1338 |
bool is_unit_struct () const { return is_unit; } |
| 1339 |
|
| 1340 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1341 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1342 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 1343 |
|
| 1344 |
std::vector<StructField> &get_fields () { return fields; } |
| 1345 |
|
| 1346 |
protected: |
| 1347 |
/* Use covariance to implement clone function as returning this object |
| 1348 |
* rather than base */ |
| 1349 |
StructStruct *clone_item_impl () const override |
| 1350 |
{ |
| 1351 |
return new StructStruct (*this); |
| 1352 |
} |
| 1353 |
|
| 1354 |
/* Use covariance to implement clone function as returning this object |
| 1355 |
* rather than base */ |
| 1356 |
/*virtual StructStruct* clone_statement_impl() const override { |
| 1357 |
return new StructStruct(*this); |
| 1358 |
}*/ |
| 1359 |
}; |
| 1360 |
|
| 1361 |
// A single field in a tuple |
| 1362 |
class TupleField |
| 1363 |
{ |
| 1364 |
private: |
| 1365 |
// bool has_outer_attributes; |
| 1366 |
AST::AttrVec outer_attrs; |
| 1367 |
|
| 1368 |
// bool has_visibility; |
| 1369 |
Visibility visibility; |
| 1370 |
|
| 1371 |
std::unique_ptr<Type> field_type; |
| 1372 |
|
| 1373 |
location_t locus; |
| 1374 |
|
| 1375 |
Analysis::NodeMapping mappings; |
| 1376 |
|
| 1377 |
public: |
| 1378 |
// Returns whether tuple field has outer attributes. |
| 1379 |
bool has_outer_attributes () const { return !outer_attrs.empty (); } |
| 1380 |
|
| 1381 |
/* Returns whether tuple field has a non-default visibility (i.e. a public |
| 1382 |
* one) */ |
| 1383 |
bool has_visibility () const { return !visibility.is_error (); } |
| 1384 |
|
| 1385 |
// Complete constructor |
| 1386 |
TupleField (Analysis::NodeMapping mapping, std::unique_ptr<Type> field_type, |
| 1387 |
Visibility vis, location_t locus, |
| 1388 |
AST::AttrVec outer_attrs = AST::AttrVec ()); |
| 1389 |
|
| 1390 |
// Copy constructor with clone |
| 1391 |
TupleField (TupleField const &other); |
| 1392 |
|
| 1393 |
~TupleField () = default; |
| 1394 |
|
| 1395 |
// Overloaded assignment operator to clone |
| 1396 |
TupleField &operator= (TupleField const &other); |
| 1397 |
|
| 1398 |
// move constructors |
| 1399 |
TupleField (TupleField &&other) = default; |
| 1400 |
TupleField &operator= (TupleField &&other) = default; |
| 1401 |
|
| 1402 |
// Returns whether tuple field is in an error state. |
| 1403 |
bool is_error () const { return field_type == nullptr; } |
| 1404 |
|
| 1405 |
std::string as_string () const; |
| 1406 |
|
| 1407 |
Analysis::NodeMapping get_mappings () const { return mappings; } |
| 1408 |
|
| 1409 |
Visibility &get_visibility () { return visibility; } |
| 1410 |
|
| 1411 |
location_t get_locus () const { return locus; } |
| 1412 |
|
| 1413 |
AST::AttrVec &get_outer_attrs () { return outer_attrs; } |
| 1414 |
HIR::Type &get_field_type () { return *field_type; } |
| 1415 |
}; |
| 1416 |
|
| 1417 |
// Rust tuple declared using struct keyword HIR node |
| 1418 |
class TupleStruct : public Struct |
| 1419 |
{ |
| 1420 |
std::vector<TupleField> fields; |
| 1421 |
|
| 1422 |
public: |
| 1423 |
std::string as_string () const override; |
| 1424 |
|
| 1425 |
// Mega-constructor with all possible fields |
| 1426 |
TupleStruct (Analysis::NodeMapping mappings, std::vector<TupleField> fields, |
| 1427 |
Identifier struct_name, |
| 1428 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 1429 |
WhereClause where_clause, Visibility vis, |
| 1430 |
AST::AttrVec outer_attrs, location_t locus); |
| 1431 |
|
| 1432 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1433 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1434 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 1435 |
|
| 1436 |
std::vector<TupleField> &get_fields () { return fields; } |
| 1437 |
const std::vector<TupleField> &get_fields () const { return fields; } |
| 1438 |
|
| 1439 |
protected: |
| 1440 |
/* Use covariance to implement clone function as returning this object |
| 1441 |
* rather than base */ |
| 1442 |
TupleStruct *clone_item_impl () const override |
| 1443 |
{ |
| 1444 |
return new TupleStruct (*this); |
| 1445 |
} |
| 1446 |
|
| 1447 |
/* Use covariance to implement clone function as returning this object |
| 1448 |
* rather than base */ |
| 1449 |
/*virtual TupleStruct* clone_statement_impl() const override { |
| 1450 |
return new TupleStruct(*this); |
| 1451 |
}*/ |
| 1452 |
}; |
| 1453 |
|
| 1454 |
/* An item used in an "enum" tagged union - not abstract: base represents a |
| 1455 |
name-only enum. Syntactically EnumItem's can have a Visibility. But not |
| 1456 |
Semantically. So check there is no Visibility when lowering and make this |
| 1457 |
an Item, not an VisItem. */ |
| 1458 |
class EnumItem : public Item |
| 1459 |
{ |
| 1460 |
Identifier variant_name; |
| 1461 |
location_t locus; |
| 1462 |
|
| 1463 |
public: |
| 1464 |
virtual ~EnumItem () {} |
| 1465 |
|
| 1466 |
enum EnumItemKind |
| 1467 |
{ |
| 1468 |
Named, |
| 1469 |
Tuple, |
| 1470 |
Struct, |
| 1471 |
Discriminant, |
| 1472 |
}; |
| 1473 |
|
| 1474 |
EnumItem (Analysis::NodeMapping mappings, Identifier variant_name, |
| 1475 |
AST::AttrVec outer_attrs, location_t locus); |
| 1476 |
|
| 1477 |
// Unique pointer custom clone function |
| 1478 |
std::unique_ptr<EnumItem> clone_enum_item () const |
| 1479 |
{ |
| 1480 |
return std::unique_ptr<EnumItem> (clone_item_impl ()); |
| 1481 |
} |
| 1482 |
|
| 1483 |
virtual std::string as_string () const override; |
| 1484 |
virtual EnumItemKind get_enum_item_kind () const { return Named; }; |
| 1485 |
|
| 1486 |
// not pure virtual as not abstract |
| 1487 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1488 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1489 |
// void accept_vis (HIRVisItemVisitor &vis) override; |
| 1490 |
|
| 1491 |
location_t get_locus () const override { return locus; } |
| 1492 |
|
| 1493 |
Identifier get_identifier () const { return variant_name; } |
| 1494 |
|
| 1495 |
ItemKind get_item_kind () const override { return ItemKind::EnumItem; } |
| 1496 |
|
| 1497 |
protected: |
| 1498 |
EnumItem *clone_item_impl () const override { return new EnumItem (*this); } |
| 1499 |
}; |
| 1500 |
|
| 1501 |
// A tuple item used in an "enum" tagged union |
| 1502 |
class EnumItemTuple : public EnumItem |
| 1503 |
{ |
| 1504 |
// bool has_tuple_fields; |
| 1505 |
std::vector<TupleField> tuple_fields; |
| 1506 |
|
| 1507 |
public: |
| 1508 |
// Returns whether tuple enum item has tuple fields. |
| 1509 |
bool has_tuple_fields () const { return !tuple_fields.empty (); } |
| 1510 |
|
| 1511 |
EnumItemKind get_enum_item_kind () const override |
| 1512 |
{ |
| 1513 |
return EnumItemKind::Tuple; |
| 1514 |
} |
| 1515 |
|
| 1516 |
EnumItemTuple (Analysis::NodeMapping mappings, Identifier variant_name, |
| 1517 |
std::vector<TupleField> tuple_fields, AST::AttrVec outer_attrs, |
| 1518 |
location_t locus); |
| 1519 |
|
| 1520 |
std::string as_string () const override; |
| 1521 |
|
| 1522 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1523 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1524 |
|
| 1525 |
std::vector<TupleField> &get_tuple_fields () { return tuple_fields; } |
| 1526 |
|
| 1527 |
protected: |
| 1528 |
// Clone function implementation as (not pure) virtual method |
| 1529 |
EnumItemTuple *clone_item_impl () const override |
| 1530 |
{ |
| 1531 |
return new EnumItemTuple (*this); |
| 1532 |
} |
| 1533 |
}; |
| 1534 |
|
| 1535 |
// A struct item used in an "enum" tagged union |
| 1536 |
class EnumItemStruct : public EnumItem |
| 1537 |
{ |
| 1538 |
// bool has_struct_fields; |
| 1539 |
std::vector<StructField> struct_fields; |
| 1540 |
|
| 1541 |
public: |
| 1542 |
// Returns whether struct enum item has struct fields. |
| 1543 |
bool has_struct_fields () const { return !struct_fields.empty (); } |
| 1544 |
|
| 1545 |
EnumItemKind get_enum_item_kind () const override |
| 1546 |
{ |
| 1547 |
return EnumItemKind::Struct; |
| 1548 |
} |
| 1549 |
|
| 1550 |
EnumItemStruct (Analysis::NodeMapping mappings, Identifier variant_name, |
| 1551 |
std::vector<StructField> struct_fields, |
| 1552 |
AST::AttrVec outer_attrs, location_t locus); |
| 1553 |
|
| 1554 |
std::string as_string () const override; |
| 1555 |
|
| 1556 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1557 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1558 |
|
| 1559 |
std::vector<StructField> &get_struct_fields () { return struct_fields; } |
| 1560 |
|
| 1561 |
protected: |
| 1562 |
// Clone function implementation as (not pure) virtual method |
| 1563 |
EnumItemStruct *clone_item_impl () const override |
| 1564 |
{ |
| 1565 |
return new EnumItemStruct (*this); |
| 1566 |
} |
| 1567 |
}; |
| 1568 |
|
| 1569 |
// A discriminant (numbered enum) item used in an "enum" tagged union |
| 1570 |
class EnumItemDiscriminant : public EnumItem |
| 1571 |
{ |
| 1572 |
std::unique_ptr<Expr> expression; |
| 1573 |
|
| 1574 |
public: |
| 1575 |
EnumItemDiscriminant (Analysis::NodeMapping mappings, Identifier variant_name, |
| 1576 |
std::unique_ptr<Expr> expr, AST::AttrVec outer_attrs, |
| 1577 |
location_t locus); |
| 1578 |
|
| 1579 |
// Copy constructor with clone |
| 1580 |
EnumItemDiscriminant (EnumItemDiscriminant const &other); |
| 1581 |
|
| 1582 |
// Overloaded assignment operator to clone |
| 1583 |
EnumItemDiscriminant &operator= (EnumItemDiscriminant const &other); |
| 1584 |
|
| 1585 |
// move constructors |
| 1586 |
EnumItemDiscriminant (EnumItemDiscriminant &&other) = default; |
| 1587 |
EnumItemDiscriminant &operator= (EnumItemDiscriminant &&other) = default; |
| 1588 |
|
| 1589 |
EnumItemKind get_enum_item_kind () const override |
| 1590 |
{ |
| 1591 |
return EnumItemKind::Discriminant; |
| 1592 |
} |
| 1593 |
|
| 1594 |
std::string as_string () const override; |
| 1595 |
|
| 1596 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1597 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1598 |
|
| 1599 |
Expr &get_discriminant_expression () { return *expression; } |
| 1600 |
|
| 1601 |
std::unique_ptr<Expr> take_discriminant_expression () |
| 1602 |
{ |
| 1603 |
return std::move (expression); |
| 1604 |
} |
| 1605 |
|
| 1606 |
protected: |
| 1607 |
// Clone function implementation as (not pure) virtual method |
| 1608 |
EnumItemDiscriminant *clone_item_impl () const override |
| 1609 |
{ |
| 1610 |
return new EnumItemDiscriminant (*this); |
| 1611 |
} |
| 1612 |
}; |
| 1613 |
|
| 1614 |
// HIR node for Rust "enum" - tagged union |
| 1615 |
class Enum : public VisItem |
| 1616 |
{ |
| 1617 |
Identifier enum_name; |
| 1618 |
|
| 1619 |
// bool has_generics; |
| 1620 |
// Generics generic_params; |
| 1621 |
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined |
| 1622 |
|
| 1623 |
// bool has_where_clause; |
| 1624 |
WhereClause where_clause; |
| 1625 |
|
| 1626 |
std::vector<std::unique_ptr<EnumItem>> items; |
| 1627 |
|
| 1628 |
location_t locus; |
| 1629 |
|
| 1630 |
public: |
| 1631 |
std::string as_string () const override; |
| 1632 |
|
| 1633 |
// Returns whether "enum" has generic parameters. |
| 1634 |
bool has_generics () const { return !generic_params.empty (); } |
| 1635 |
|
| 1636 |
// Returns whether "enum" has a where clause. |
| 1637 |
bool has_where_clause () const { return !where_clause.is_empty (); } |
| 1638 |
|
| 1639 |
/* Returns whether enum is a "zero-variant" (no possible variant) enum, |
| 1640 |
* which cannot be instantiated. */ |
| 1641 |
bool is_zero_variant () const { return items.empty (); } |
| 1642 |
|
| 1643 |
// Mega-constructor |
| 1644 |
Enum (Analysis::NodeMapping mappings, Identifier enum_name, Visibility vis, |
| 1645 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 1646 |
WhereClause where_clause, std::vector<std::unique_ptr<EnumItem>> items, |
| 1647 |
AST::AttrVec outer_attrs, location_t locus); |
| 1648 |
|
| 1649 |
// TODO: constructor with less arguments |
| 1650 |
|
| 1651 |
// Copy constructor with vector clone |
| 1652 |
Enum (Enum const &other); |
| 1653 |
|
| 1654 |
// Overloaded assignment operator with vector clone |
| 1655 |
Enum &operator= (Enum const &other); |
| 1656 |
|
| 1657 |
// Move constructors |
| 1658 |
Enum (Enum &&other) = default; |
| 1659 |
Enum &operator= (Enum &&other) = default; |
| 1660 |
|
| 1661 |
location_t get_locus () const override final { return locus; } |
| 1662 |
|
| 1663 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1664 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1665 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 1666 |
|
| 1667 |
Identifier get_identifier () const { return enum_name; } |
| 1668 |
ItemKind get_item_kind () const override { return ItemKind::Enum; } |
| 1669 |
|
| 1670 |
std::vector<std::unique_ptr<GenericParam>> &get_generic_params () |
| 1671 |
{ |
| 1672 |
return generic_params; |
| 1673 |
} |
| 1674 |
|
| 1675 |
const std::vector<std::unique_ptr<EnumItem>> &get_variants () const |
| 1676 |
{ |
| 1677 |
return items; |
| 1678 |
} |
| 1679 |
|
| 1680 |
std::vector<std::unique_ptr<EnumItem>> &get_variants () { return items; } |
| 1681 |
WhereClause &get_where_clause () { return where_clause; } |
| 1682 |
|
| 1683 |
protected: |
| 1684 |
/* Use covariance to implement clone function as returning this object |
| 1685 |
* rather than base */ |
| 1686 |
Enum *clone_item_impl () const override { return new Enum (*this); } |
| 1687 |
|
| 1688 |
/* Use covariance to implement clone function as returning this object |
| 1689 |
* rather than base */ |
| 1690 |
/*virtual Enum* clone_statement_impl() const override { |
| 1691 |
return new Enum(*this); |
| 1692 |
}*/ |
| 1693 |
}; |
| 1694 |
|
| 1695 |
// Rust untagged union used for C compat HIR node |
| 1696 |
class Union : public VisItem |
| 1697 |
{ |
| 1698 |
Identifier union_name; |
| 1699 |
|
| 1700 |
// bool has_generics; |
| 1701 |
// Generics generic_params; |
| 1702 |
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined |
| 1703 |
|
| 1704 |
// bool has_where_clause; |
| 1705 |
WhereClause where_clause; |
| 1706 |
|
| 1707 |
std::vector<StructField> variants; |
| 1708 |
|
| 1709 |
location_t locus; |
| 1710 |
|
| 1711 |
public: |
| 1712 |
std::string as_string () const override; |
| 1713 |
|
| 1714 |
// Returns whether union has generic params. |
| 1715 |
bool has_generics () const { return !generic_params.empty (); } |
| 1716 |
|
| 1717 |
// Returns whether union has where clause. |
| 1718 |
bool has_where_clause () const { return !where_clause.is_empty (); } |
| 1719 |
|
| 1720 |
Union (Analysis::NodeMapping mappings, Identifier union_name, Visibility vis, |
| 1721 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 1722 |
WhereClause where_clause, std::vector<StructField> variants, |
| 1723 |
AST::AttrVec outer_attrs, location_t locus); |
| 1724 |
|
| 1725 |
// copy constructor with vector clone |
| 1726 |
Union (Union const &other); |
| 1727 |
|
| 1728 |
// overloaded assignment operator with vector clone |
| 1729 |
Union &operator= (Union const &other); |
| 1730 |
|
| 1731 |
// move constructors |
| 1732 |
Union (Union &&other) = default; |
| 1733 |
Union &operator= (Union &&other) = default; |
| 1734 |
|
| 1735 |
std::vector<std::unique_ptr<GenericParam>> &get_generic_params () |
| 1736 |
{ |
| 1737 |
return generic_params; |
| 1738 |
} |
| 1739 |
|
| 1740 |
Identifier get_identifier () const { return union_name; } |
| 1741 |
|
| 1742 |
location_t get_locus () const override final { return locus; } |
| 1743 |
|
| 1744 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1745 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1746 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 1747 |
|
| 1748 |
std::vector<StructField> &get_variants () { return variants; } |
| 1749 |
|
| 1750 |
WhereClause &get_where_clause () { return where_clause; } |
| 1751 |
|
| 1752 |
ItemKind get_item_kind () const override { return ItemKind::Union; } |
| 1753 |
|
| 1754 |
protected: |
| 1755 |
/* Use covariance to implement clone function as returning this object |
| 1756 |
* rather than base */ |
| 1757 |
Union *clone_item_impl () const override { return new Union (*this); } |
| 1758 |
}; |
| 1759 |
|
| 1760 |
class ConstantItem : public VisItem, public ImplItem |
| 1761 |
{ |
| 1762 |
Identifier identifier; |
| 1763 |
std::unique_ptr<Type> type; |
| 1764 |
std::unique_ptr<Expr> const_expr; |
| 1765 |
location_t locus; |
| 1766 |
|
| 1767 |
public: |
| 1768 |
std::string as_string () const override; |
| 1769 |
|
| 1770 |
ConstantItem (Analysis::NodeMapping mappings, Identifier ident, |
| 1771 |
Visibility vis, std::unique_ptr<Type> type, |
| 1772 |
std::unique_ptr<Expr> const_expr, AST::AttrVec outer_attrs, |
| 1773 |
location_t locus); |
| 1774 |
|
| 1775 |
ConstantItem (ConstantItem const &other); |
| 1776 |
|
| 1777 |
// Overload assignment operator to clone |
| 1778 |
ConstantItem &operator= (ConstantItem const &other); |
| 1779 |
|
| 1780 |
// move constructors |
| 1781 |
ConstantItem (ConstantItem &&other) = default; |
| 1782 |
ConstantItem &operator= (ConstantItem &&other) = default; |
| 1783 |
|
| 1784 |
// Returns whether constant item is an "unnamed" (wildcard underscore used |
| 1785 |
// as identifier) constant. |
| 1786 |
bool is_unnamed () const |
| 1787 |
{ |
| 1788 |
return identifier.as_string () == std::string ("_"); |
| 1789 |
} |
| 1790 |
|
| 1791 |
location_t get_locus () const override final { return locus; } |
| 1792 |
|
| 1793 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1794 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1795 |
void accept_vis (HIRImplVisitor &vis) override; |
| 1796 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 1797 |
|
| 1798 |
Type &get_type () |
| 1799 |
{ |
| 1800 |
rust_assert (type); |
| 1801 |
return *type; |
| 1802 |
} |
| 1803 |
|
| 1804 |
Expr &get_expr () { return *const_expr; } |
| 1805 |
|
| 1806 |
Identifier get_identifier () const { return identifier; } |
| 1807 |
|
| 1808 |
Analysis::NodeMapping get_impl_mappings () const override |
| 1809 |
{ |
| 1810 |
return get_mappings (); |
| 1811 |
}; |
| 1812 |
|
| 1813 |
ImplItemType get_impl_item_type () const override final |
| 1814 |
{ |
| 1815 |
return ImplItem::ImplItemType::CONSTANT; |
| 1816 |
} |
| 1817 |
|
| 1818 |
ItemKind get_item_kind () const override { return ItemKind::Constant; } |
| 1819 |
|
| 1820 |
std::string get_impl_item_name () const override final |
| 1821 |
{ |
| 1822 |
return get_identifier ().as_string (); |
| 1823 |
} |
| 1824 |
|
| 1825 |
protected: |
| 1826 |
/* Use covariance to implement clone function as returning this object |
| 1827 |
* rather than base */ |
| 1828 |
ConstantItem *clone_item_impl () const override |
| 1829 |
{ |
| 1830 |
return new ConstantItem (*this); |
| 1831 |
} |
| 1832 |
|
| 1833 |
/* Use covariance to implement clone function as returning this object |
| 1834 |
* rather than base */ |
| 1835 |
ConstantItem *clone_inherent_impl_item_impl () const override |
| 1836 |
{ |
| 1837 |
return new ConstantItem (*this); |
| 1838 |
} |
| 1839 |
}; |
| 1840 |
|
| 1841 |
/* Static item HIR node - items within module scope with fixed storage |
| 1842 |
* duration? */ |
| 1843 |
class StaticItem : public VisItem |
| 1844 |
{ |
| 1845 |
Mutability mut; |
| 1846 |
Identifier name; |
| 1847 |
std::unique_ptr<Type> type; |
| 1848 |
std::unique_ptr<Expr> expr; |
| 1849 |
location_t locus; |
| 1850 |
|
| 1851 |
public: |
| 1852 |
std::string as_string () const override; |
| 1853 |
|
| 1854 |
StaticItem (Analysis::NodeMapping mappings, Identifier name, Mutability mut, |
| 1855 |
std::unique_ptr<Type> type, std::unique_ptr<Expr> expr, |
| 1856 |
Visibility vis, AST::AttrVec outer_attrs, location_t locus); |
| 1857 |
|
| 1858 |
// Copy constructor with clone |
| 1859 |
StaticItem (StaticItem const &other); |
| 1860 |
|
| 1861 |
// Overloaded assignment operator to clone |
| 1862 |
StaticItem &operator= (StaticItem const &other); |
| 1863 |
|
| 1864 |
// move constructors |
| 1865 |
StaticItem (StaticItem &&other) = default; |
| 1866 |
StaticItem &operator= (StaticItem &&other) = default; |
| 1867 |
|
| 1868 |
location_t get_locus () const override final { return locus; } |
| 1869 |
|
| 1870 |
void accept_vis (HIRFullVisitor &vis) override; |
| 1871 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 1872 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 1873 |
|
| 1874 |
Identifier get_identifier () const { return name; } |
| 1875 |
|
| 1876 |
Mutability get_mut () const { return mut; } |
| 1877 |
|
| 1878 |
bool is_mut () const { return mut == Mutability::Mut; } |
| 1879 |
|
| 1880 |
Expr &get_expr () |
| 1881 |
{ |
| 1882 |
rust_assert (expr); |
| 1883 |
return *expr; |
| 1884 |
} |
| 1885 |
|
| 1886 |
Type &get_type () |
| 1887 |
{ |
| 1888 |
rust_assert (type); |
| 1889 |
return *type; |
| 1890 |
} |
| 1891 |
|
| 1892 |
ItemKind get_item_kind () const override { return ItemKind::Static; } |
| 1893 |
|
| 1894 |
protected: |
| 1895 |
StaticItem *clone_item_impl () const override |
| 1896 |
{ |
| 1897 |
return new StaticItem (*this); |
| 1898 |
} |
| 1899 |
}; |
| 1900 |
|
| 1901 |
// Function declaration in traits |
| 1902 |
class TraitFunctionDecl |
| 1903 |
{ |
| 1904 |
private: |
| 1905 |
FunctionQualifiers qualifiers; |
| 1906 |
Identifier function_name; |
| 1907 |
std::vector<std::unique_ptr<GenericParam>> generic_params; |
| 1908 |
std::vector<FunctionParam> function_params; |
| 1909 |
std::unique_ptr<Type> return_type; |
| 1910 |
WhereClause where_clause; |
| 1911 |
tl::optional<SelfParam> self; |
| 1912 |
|
| 1913 |
public: |
| 1914 |
// Mega-constructor |
| 1915 |
TraitFunctionDecl (Identifier function_name, FunctionQualifiers qualifiers, |
| 1916 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 1917 |
tl::optional<SelfParam> self, |
| 1918 |
std::vector<FunctionParam> function_params, |
| 1919 |
std::unique_ptr<Type> return_type, |
| 1920 |
WhereClause where_clause); |
| 1921 |
|
| 1922 |
// Copy constructor with clone |
| 1923 |
TraitFunctionDecl (TraitFunctionDecl const &other); |
| 1924 |
|
| 1925 |
~TraitFunctionDecl () = default; |
| 1926 |
|
| 1927 |
// Overloaded assignment operator with clone |
| 1928 |
TraitFunctionDecl &operator= (TraitFunctionDecl const &other); |
| 1929 |
|
| 1930 |
// move constructors |
| 1931 |
TraitFunctionDecl (TraitFunctionDecl &&other) = default; |
| 1932 |
TraitFunctionDecl &operator= (TraitFunctionDecl &&other) = default; |
| 1933 |
|
| 1934 |
std::string as_string () const; |
| 1935 |
|
| 1936 |
// Returns whether function decl has generic parameters. |
| 1937 |
bool has_generics () const { return !generic_params.empty (); } |
| 1938 |
|
| 1939 |
// Returns whether function decl has regular parameters. |
| 1940 |
bool has_params () const { return !function_params.empty (); } |
| 1941 |
|
| 1942 |
// Returns whether function has return type (otherwise is void). |
| 1943 |
bool has_return_type () const { return return_type != nullptr; } |
| 1944 |
|
| 1945 |
// Returns whether function has a where clause. |
| 1946 |
bool has_where_clause () const { return !where_clause.is_empty (); } |
| 1947 |
|
| 1948 |
WhereClause &get_where_clause () { return where_clause; } |
| 1949 |
|
| 1950 |
bool is_method () const { return self.has_value (); } |
| 1951 |
|
| 1952 |
SelfParam &get_self_unchecked () { return self.value (); } |
| 1953 |
const SelfParam &get_self_unchecked () const { return self.value (); } |
| 1954 |
|
| 1955 |
tl::optional<SelfParam> &get_self () { return self; } |
| 1956 |
const tl::optional<SelfParam> &get_self () const { return self; } |
| 1957 |
|
| 1958 |
Identifier get_function_name () const { return function_name; } |
| 1959 |
|
| 1960 |
std::vector<std::unique_ptr<GenericParam>> &get_generic_params () |
| 1961 |
{ |
| 1962 |
return generic_params; |
| 1963 |
} |
| 1964 |
|
| 1965 |
Type &get_return_type () { return *return_type; } |
| 1966 |
|
| 1967 |
std::vector<FunctionParam> &get_function_params () { return function_params; } |
| 1968 |
|
| 1969 |
const FunctionQualifiers &get_qualifiers () const { return qualifiers; } |
| 1970 |
}; |
| 1971 |
|
| 1972 |
// Actual trait item function declaration within traits |
| 1973 |
class TraitItemFunc : public TraitItem |
| 1974 |
{ |
| 1975 |
AST::AttrVec outer_attrs; |
| 1976 |
TraitFunctionDecl decl; |
| 1977 |
std::unique_ptr<BlockExpr> block_expr; |
| 1978 |
location_t locus; |
| 1979 |
|
| 1980 |
public: |
| 1981 |
// Returns whether function has a definition or is just a declaration. |
| 1982 |
bool has_definition () const { return block_expr != nullptr; } |
| 1983 |
|
| 1984 |
TraitItemFunc (Analysis::NodeMapping mappings, TraitFunctionDecl decl, |
| 1985 |
std::unique_ptr<BlockExpr> block_expr, |
| 1986 |
AST::AttrVec outer_attrs, location_t locus); |
| 1987 |
|
| 1988 |
// Copy constructor with clone |
| 1989 |
TraitItemFunc (TraitItemFunc const &other); |
| 1990 |
|
| 1991 |
// Overloaded assignment operator to clone |
| 1992 |
TraitItemFunc &operator= (TraitItemFunc const &other); |
| 1993 |
|
| 1994 |
// move constructors |
| 1995 |
TraitItemFunc (TraitItemFunc &&other) = default; |
| 1996 |
TraitItemFunc &operator= (TraitItemFunc &&other) = default; |
| 1997 |
|
| 1998 |
std::string as_string () const override; |
| 1999 |
|
| 2000 |
location_t get_locus () const { return locus; } |
| 2001 |
|
| 2002 |
void accept_vis (HIRFullVisitor &vis) override; |
| 2003 |
void accept_vis (HIRTraitItemVisitor &vis) override; |
| 2004 |
|
| 2005 |
TraitFunctionDecl &get_decl () { return decl; } |
| 2006 |
|
| 2007 |
const TraitFunctionDecl &get_decl () const { return decl; } |
| 2008 |
|
| 2009 |
BlockExpr &get_block_expr () { return *block_expr; } |
| 2010 |
|
| 2011 |
const std::string trait_identifier () const override final |
| 2012 |
{ |
| 2013 |
return decl.get_function_name ().as_string (); |
| 2014 |
} |
| 2015 |
|
| 2016 |
TraitItemKind get_item_kind () const override final |
| 2017 |
{ |
| 2018 |
return TraitItemKind::FUNC; |
| 2019 |
} |
| 2020 |
|
| 2021 |
AST::AttrVec &get_outer_attrs () override final { return outer_attrs; } |
| 2022 |
const AST::AttrVec &get_outer_attrs () const override final |
| 2023 |
{ |
| 2024 |
return outer_attrs; |
| 2025 |
} |
| 2026 |
|
| 2027 |
location_t get_trait_locus () const override { return get_locus (); } |
| 2028 |
|
| 2029 |
protected: |
| 2030 |
// Clone function implementation as (not pure) virtual method |
| 2031 |
TraitItemFunc *clone_trait_item_impl () const override |
| 2032 |
{ |
| 2033 |
return new TraitItemFunc (*this); |
| 2034 |
} |
| 2035 |
}; |
| 2036 |
|
| 2037 |
// Constant item within traits |
| 2038 |
class TraitItemConst : public TraitItem |
| 2039 |
{ |
| 2040 |
AST::AttrVec outer_attrs; |
| 2041 |
Identifier name; |
| 2042 |
std::unique_ptr<Type> type; |
| 2043 |
std::unique_ptr<Expr> expr; |
| 2044 |
location_t locus; |
| 2045 |
|
| 2046 |
public: |
| 2047 |
// Whether the constant item has an associated expression. |
| 2048 |
bool has_expression () const { return expr != nullptr; } |
| 2049 |
|
| 2050 |
TraitItemConst (Analysis::NodeMapping mappings, Identifier name, |
| 2051 |
std::unique_ptr<Type> type, std::unique_ptr<Expr> expr, |
| 2052 |
AST::AttrVec outer_attrs, location_t locus); |
| 2053 |
|
| 2054 |
// Copy constructor with clones |
| 2055 |
TraitItemConst (TraitItemConst const &other); |
| 2056 |
|
| 2057 |
// Overloaded assignment operator to clone |
| 2058 |
TraitItemConst &operator= (TraitItemConst const &other); |
| 2059 |
|
| 2060 |
// move constructors |
| 2061 |
TraitItemConst (TraitItemConst &&other) = default; |
| 2062 |
TraitItemConst &operator= (TraitItemConst &&other) = default; |
| 2063 |
|
| 2064 |
std::string as_string () const override; |
| 2065 |
|
| 2066 |
location_t get_locus () const { return locus; } |
| 2067 |
|
| 2068 |
void accept_vis (HIRFullVisitor &vis) override; |
| 2069 |
void accept_vis (HIRTraitItemVisitor &vis) override; |
| 2070 |
|
| 2071 |
Identifier get_name () const { return name; } |
| 2072 |
|
| 2073 |
bool has_expr () const { return expr != nullptr; } |
| 2074 |
|
| 2075 |
Type &get_type () |
| 2076 |
{ |
| 2077 |
rust_assert (type); |
| 2078 |
return *type; |
| 2079 |
} |
| 2080 |
|
| 2081 |
Expr &get_expr () |
| 2082 |
{ |
| 2083 |
rust_assert (expr); |
| 2084 |
return *expr; |
| 2085 |
} |
| 2086 |
|
| 2087 |
const std::string trait_identifier () const override final |
| 2088 |
{ |
| 2089 |
return name.as_string (); |
| 2090 |
} |
| 2091 |
|
| 2092 |
TraitItemKind get_item_kind () const override final |
| 2093 |
{ |
| 2094 |
return TraitItemKind::CONST; |
| 2095 |
} |
| 2096 |
|
| 2097 |
AST::AttrVec &get_outer_attrs () override final { return outer_attrs; } |
| 2098 |
const AST::AttrVec &get_outer_attrs () const override final |
| 2099 |
{ |
| 2100 |
return outer_attrs; |
| 2101 |
} |
| 2102 |
|
| 2103 |
location_t get_trait_locus () const override { return get_locus (); } |
| 2104 |
|
| 2105 |
protected: |
| 2106 |
// Clone function implementation as (not pure) virtual method |
| 2107 |
TraitItemConst *clone_trait_item_impl () const override |
| 2108 |
{ |
| 2109 |
return new TraitItemConst (*this); |
| 2110 |
} |
| 2111 |
}; |
| 2112 |
|
| 2113 |
// Type items within traits |
| 2114 |
class TraitItemType : public TraitItem |
| 2115 |
{ |
| 2116 |
AST::AttrVec outer_attrs; |
| 2117 |
|
| 2118 |
Identifier name; |
| 2119 |
std::vector<std::unique_ptr<TypeParamBound>> |
| 2120 |
type_param_bounds; // inlined form |
| 2121 |
location_t locus; |
| 2122 |
|
| 2123 |
public: |
| 2124 |
// Returns whether trait item type has type param bounds. |
| 2125 |
bool has_type_param_bounds () const { return !type_param_bounds.empty (); } |
| 2126 |
|
| 2127 |
TraitItemType (Analysis::NodeMapping mappings, Identifier name, |
| 2128 |
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds, |
| 2129 |
AST::AttrVec outer_attrs, location_t locus); |
| 2130 |
|
| 2131 |
// Copy constructor with vector clone |
| 2132 |
TraitItemType (TraitItemType const &other); |
| 2133 |
|
| 2134 |
// Overloaded assignment operator with vector clone |
| 2135 |
TraitItemType &operator= (TraitItemType const &other); |
| 2136 |
|
| 2137 |
// default move constructors |
| 2138 |
TraitItemType (TraitItemType &&other) = default; |
| 2139 |
TraitItemType &operator= (TraitItemType &&other) = default; |
| 2140 |
|
| 2141 |
std::string as_string () const override; |
| 2142 |
|
| 2143 |
location_t get_locus () const { return locus; } |
| 2144 |
|
| 2145 |
void accept_vis (HIRFullVisitor &vis) override; |
| 2146 |
void accept_vis (HIRTraitItemVisitor &vis) override; |
| 2147 |
|
| 2148 |
Identifier get_name () const { return name; } |
| 2149 |
|
| 2150 |
std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds () |
| 2151 |
{ |
| 2152 |
return type_param_bounds; |
| 2153 |
} |
| 2154 |
|
| 2155 |
const std::string trait_identifier () const override final |
| 2156 |
{ |
| 2157 |
return name.as_string (); |
| 2158 |
} |
| 2159 |
|
| 2160 |
TraitItemKind get_item_kind () const override final |
| 2161 |
{ |
| 2162 |
return TraitItemKind::TYPE; |
| 2163 |
} |
| 2164 |
|
| 2165 |
AST::AttrVec &get_outer_attrs () override final { return outer_attrs; } |
| 2166 |
const AST::AttrVec &get_outer_attrs () const override final |
| 2167 |
{ |
| 2168 |
return outer_attrs; |
| 2169 |
} |
| 2170 |
|
| 2171 |
location_t get_trait_locus () const override { return get_locus (); } |
| 2172 |
|
| 2173 |
protected: |
| 2174 |
// Clone function implementation as (not pure) virtual method |
| 2175 |
TraitItemType *clone_trait_item_impl () const override |
| 2176 |
{ |
| 2177 |
return new TraitItemType (*this); |
| 2178 |
} |
| 2179 |
}; |
| 2180 |
|
| 2181 |
// Rust trait item declaration HIR node |
| 2182 |
class Trait : public VisItem |
| 2183 |
{ |
| 2184 |
Unsafety unsafety; |
| 2185 |
Identifier name; |
| 2186 |
std::vector<std::unique_ptr<GenericParam>> generic_params; |
| 2187 |
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds; |
| 2188 |
WhereClause where_clause; |
| 2189 |
std::vector<std::unique_ptr<TraitItem>> trait_items; |
| 2190 |
location_t locus; |
| 2191 |
|
| 2192 |
public: |
| 2193 |
std::string as_string () const override; |
| 2194 |
|
| 2195 |
// Returns whether trait has generic parameters. |
| 2196 |
bool has_generics () const { return !generic_params.empty (); } |
| 2197 |
|
| 2198 |
// Returns whether trait has type parameter bounds. |
| 2199 |
bool has_type_param_bounds () const { return !type_param_bounds.empty (); } |
| 2200 |
|
| 2201 |
// Returns whether trait has where clause. |
| 2202 |
bool has_where_clause () const { return !where_clause.is_empty (); } |
| 2203 |
|
| 2204 |
// Returns whether trait has trait items. |
| 2205 |
bool has_trait_items () const { return !trait_items.empty (); } |
| 2206 |
|
| 2207 |
std::vector<std::unique_ptr<TraitItem>> &get_trait_items () |
| 2208 |
{ |
| 2209 |
return trait_items; |
| 2210 |
} |
| 2211 |
|
| 2212 |
WhereClause &get_where_clause () { return where_clause; } |
| 2213 |
|
| 2214 |
Identifier get_name () const { return name; } |
| 2215 |
bool is_unsafe () const { return unsafety == Unsafety::Unsafe; } |
| 2216 |
|
| 2217 |
// Mega-constructor |
| 2218 |
Trait (Analysis::NodeMapping mappings, Identifier name, Unsafety unsafety, |
| 2219 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 2220 |
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds, |
| 2221 |
WhereClause where_clause, |
| 2222 |
std::vector<std::unique_ptr<TraitItem>> trait_items, Visibility vis, |
| 2223 |
AST::AttrVec outer_attrs, location_t locus); |
| 2224 |
|
| 2225 |
// Copy constructor with vector clone |
| 2226 |
Trait (Trait const &other); |
| 2227 |
|
| 2228 |
// Overloaded assignment operator with vector clone |
| 2229 |
Trait &operator= (Trait const &other); |
| 2230 |
|
| 2231 |
// default move constructors |
| 2232 |
Trait (Trait &&other) = default; |
| 2233 |
Trait &operator= (Trait &&other) = default; |
| 2234 |
|
| 2235 |
location_t get_locus () const override final { return locus; } |
| 2236 |
|
| 2237 |
void accept_vis (HIRFullVisitor &vis) override; |
| 2238 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 2239 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 2240 |
|
| 2241 |
std::vector<std::unique_ptr<GenericParam>> &get_generic_params () |
| 2242 |
{ |
| 2243 |
return generic_params; |
| 2244 |
} |
| 2245 |
|
| 2246 |
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const |
| 2247 |
{ |
| 2248 |
return generic_params; |
| 2249 |
} |
| 2250 |
|
| 2251 |
std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds () |
| 2252 |
{ |
| 2253 |
return type_param_bounds; |
| 2254 |
} |
| 2255 |
|
| 2256 |
const std::vector<std::unique_ptr<TypeParamBound>> & |
| 2257 |
get_type_param_bounds () const |
| 2258 |
{ |
| 2259 |
return type_param_bounds; |
| 2260 |
} |
| 2261 |
|
| 2262 |
ItemKind get_item_kind () const override { return ItemKind::Trait; } |
| 2263 |
|
| 2264 |
protected: |
| 2265 |
/* Use covariance to implement clone function as returning this object |
| 2266 |
* rather than base */ |
| 2267 |
Trait *clone_item_impl () const override { return new Trait (*this); } |
| 2268 |
}; |
| 2269 |
|
| 2270 |
class ImplBlock : public VisItem, public WithInnerAttrs |
| 2271 |
{ |
| 2272 |
std::vector<std::unique_ptr<GenericParam>> generic_params; |
| 2273 |
std::unique_ptr<Type> impl_type; |
| 2274 |
std::unique_ptr<TypePath> trait_ref; |
| 2275 |
WhereClause where_clause; |
| 2276 |
BoundPolarity polarity; |
| 2277 |
location_t locus; |
| 2278 |
std::vector<std::unique_ptr<ImplItem>> impl_items; |
| 2279 |
bool unsafe; |
| 2280 |
|
| 2281 |
public: |
| 2282 |
ImplBlock (Analysis::NodeMapping mappings, |
| 2283 |
std::vector<std::unique_ptr<ImplItem>> impl_items, |
| 2284 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 2285 |
std::unique_ptr<Type> impl_type, |
| 2286 |
std::unique_ptr<TypePath> trait_ref, WhereClause where_clause, |
| 2287 |
BoundPolarity polarity, Visibility vis, AST::AttrVec inner_attrs, |
| 2288 |
AST::AttrVec outer_attrs, location_t locus, bool unsafe = false); |
| 2289 |
|
| 2290 |
ImplBlock (ImplBlock const &other); |
| 2291 |
|
| 2292 |
ImplBlock &operator= (ImplBlock const &other); |
| 2293 |
|
| 2294 |
ImplBlock (ImplBlock &&other) = default; |
| 2295 |
ImplBlock &operator= (ImplBlock &&other) = default; |
| 2296 |
|
| 2297 |
std::string as_string () const override; |
| 2298 |
|
| 2299 |
// Returns whether inherent impl block has inherent impl items. |
| 2300 |
bool has_impl_items () const { return !impl_items.empty (); } |
| 2301 |
|
| 2302 |
bool is_unsafe () const { return unsafe; } |
| 2303 |
|
| 2304 |
void accept_vis (HIRFullVisitor &vis) override; |
| 2305 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 2306 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 2307 |
|
| 2308 |
std::vector<std::unique_ptr<ImplItem>> &get_impl_items () |
| 2309 |
{ |
| 2310 |
return impl_items; |
| 2311 |
}; |
| 2312 |
|
| 2313 |
const std::vector<std::unique_ptr<ImplItem>> &get_impl_items () const |
| 2314 |
{ |
| 2315 |
return impl_items; |
| 2316 |
}; |
| 2317 |
|
| 2318 |
// Returns whether impl has generic parameters. |
| 2319 |
bool has_generics () const { return !generic_params.empty (); } |
| 2320 |
|
| 2321 |
// Returns whether impl has where clause. |
| 2322 |
bool has_where_clause () const { return !where_clause.is_empty (); } |
| 2323 |
|
| 2324 |
// Returns the polarity of the impl. |
| 2325 |
BoundPolarity get_polarity () const { return polarity; } |
| 2326 |
|
| 2327 |
location_t get_locus () const override final { return locus; } |
| 2328 |
|
| 2329 |
Type &get_type () |
| 2330 |
{ |
| 2331 |
rust_assert (impl_type); |
| 2332 |
return *impl_type; |
| 2333 |
}; |
| 2334 |
|
| 2335 |
bool has_type () { return impl_type != nullptr; } |
| 2336 |
|
| 2337 |
std::vector<std::unique_ptr<GenericParam>> &get_generic_params () |
| 2338 |
{ |
| 2339 |
return generic_params; |
| 2340 |
} |
| 2341 |
|
| 2342 |
bool has_trait_ref () const { return trait_ref != nullptr; } |
| 2343 |
|
| 2344 |
TypePath &get_trait_ref () { return *trait_ref; } |
| 2345 |
|
| 2346 |
WhereClause &get_where_clause () { return where_clause; } |
| 2347 |
|
| 2348 |
ItemKind get_item_kind () const override { return ItemKind::Impl; } |
| 2349 |
|
| 2350 |
protected: |
| 2351 |
ImplBlock *clone_item_impl () const override { return new ImplBlock (*this); } |
| 2352 |
}; |
| 2353 |
|
| 2354 |
// Abstract base class for an item used inside an extern block |
| 2355 |
class ExternalItem : public Node |
| 2356 |
{ |
| 2357 |
Analysis::NodeMapping mappings; |
| 2358 |
AST::AttrVec outer_attrs; |
| 2359 |
Visibility visibility; |
| 2360 |
Identifier item_name; |
| 2361 |
location_t locus; |
| 2362 |
|
| 2363 |
public: |
| 2364 |
enum class ExternKind |
| 2365 |
{ |
| 2366 |
Static, |
| 2367 |
Function, |
| 2368 |
Type, |
| 2369 |
}; |
| 2370 |
|
| 2371 |
virtual ~ExternalItem () {} |
| 2372 |
|
| 2373 |
BaseKind get_hir_kind () override final { return EXTERNAL; } |
| 2374 |
|
| 2375 |
virtual ExternKind get_extern_kind () = 0; |
| 2376 |
|
| 2377 |
// Returns whether item has outer attributes. |
| 2378 |
bool has_outer_attrs () const { return !outer_attrs.empty (); } |
| 2379 |
|
| 2380 |
// Returns whether item has non-default visibility. |
| 2381 |
bool has_visibility () const { return !visibility.is_error (); } |
| 2382 |
|
| 2383 |
// Unique pointer custom clone function |
| 2384 |
std::unique_ptr<ExternalItem> clone_external_item () const |
| 2385 |
{ |
| 2386 |
return std::unique_ptr<ExternalItem> (clone_external_item_impl ()); |
| 2387 |
} |
| 2388 |
|
| 2389 |
virtual std::string as_string () const; |
| 2390 |
|
| 2391 |
location_t get_locus () const { return locus; } |
| 2392 |
|
| 2393 |
virtual void accept_vis (HIRFullVisitor &vis) = 0; |
| 2394 |
virtual void accept_vis (HIRExternalItemVisitor &vis) = 0; |
| 2395 |
|
| 2396 |
Visibility &get_visibility () { return visibility; } |
| 2397 |
Analysis::NodeMapping get_mappings () const { return mappings; } |
| 2398 |
|
| 2399 |
Identifier get_item_name () const { return item_name; } |
| 2400 |
|
| 2401 |
AST::AttrVec &get_outer_attrs () { return outer_attrs; } |
| 2402 |
|
| 2403 |
protected: |
| 2404 |
ExternalItem (Analysis::NodeMapping mappings, Identifier item_name, |
| 2405 |
Visibility vis, AST::AttrVec outer_attrs, location_t locus); |
| 2406 |
|
| 2407 |
// Copy constructor |
| 2408 |
ExternalItem (ExternalItem const &other); |
| 2409 |
|
| 2410 |
// Overloaded assignment operator to clone |
| 2411 |
ExternalItem &operator= (ExternalItem const &other); |
| 2412 |
|
| 2413 |
// move constructors |
| 2414 |
ExternalItem (ExternalItem &&other) = default; |
| 2415 |
ExternalItem &operator= (ExternalItem &&other) = default; |
| 2416 |
|
| 2417 |
// Clone function implementation as pure virtual method |
| 2418 |
virtual ExternalItem *clone_external_item_impl () const = 0; |
| 2419 |
}; |
| 2420 |
|
| 2421 |
// A static item used in an extern block |
| 2422 |
class ExternalStaticItem : public ExternalItem |
| 2423 |
{ |
| 2424 |
Mutability mut; |
| 2425 |
std::unique_ptr<Type> item_type; |
| 2426 |
|
| 2427 |
public: |
| 2428 |
ExternalStaticItem (Analysis::NodeMapping mappings, Identifier item_name, |
| 2429 |
std::unique_ptr<Type> item_type, Mutability mut, |
| 2430 |
Visibility vis, AST::AttrVec outer_attrs, |
| 2431 |
location_t locus); |
| 2432 |
|
| 2433 |
// Copy constructor |
| 2434 |
ExternalStaticItem (ExternalStaticItem const &other); |
| 2435 |
|
| 2436 |
// Overloaded assignment operator to clone |
| 2437 |
ExternalStaticItem &operator= (ExternalStaticItem const &other); |
| 2438 |
|
| 2439 |
// move constructors |
| 2440 |
ExternalStaticItem (ExternalStaticItem &&other) = default; |
| 2441 |
ExternalStaticItem &operator= (ExternalStaticItem &&other) = default; |
| 2442 |
|
| 2443 |
std::string as_string () const override; |
| 2444 |
|
| 2445 |
void accept_vis (HIRFullVisitor &vis) override; |
| 2446 |
void accept_vis (HIRExternalItemVisitor &vis) override; |
| 2447 |
|
| 2448 |
bool is_mut () const { return mut == Mutability::Mut; } |
| 2449 |
|
| 2450 |
Mutability get_mut () { return mut; } |
| 2451 |
|
| 2452 |
Type &get_item_type () { return *item_type; } |
| 2453 |
|
| 2454 |
ExternKind get_extern_kind () override { return ExternKind::Static; } |
| 2455 |
|
| 2456 |
protected: |
| 2457 |
/* Use covariance to implement clone function as returning this object |
| 2458 |
* rather than base */ |
| 2459 |
ExternalStaticItem *clone_external_item_impl () const override |
| 2460 |
{ |
| 2461 |
return new ExternalStaticItem (*this); |
| 2462 |
} |
| 2463 |
}; |
| 2464 |
|
| 2465 |
// A named function parameter used in external functions |
| 2466 |
struct NamedFunctionParam |
| 2467 |
{ |
| 2468 |
private: |
| 2469 |
Identifier name; |
| 2470 |
std::unique_ptr<Type> param_type; |
| 2471 |
Analysis::NodeMapping mappings; |
| 2472 |
|
| 2473 |
public: |
| 2474 |
bool has_name () const { return name.as_string () != "_"; } |
| 2475 |
|
| 2476 |
NamedFunctionParam (Analysis::NodeMapping mappings, Identifier name, |
| 2477 |
std::unique_ptr<Type> param_type); |
| 2478 |
|
| 2479 |
// Copy constructor |
| 2480 |
NamedFunctionParam (NamedFunctionParam const &other); |
| 2481 |
|
| 2482 |
~NamedFunctionParam () = default; |
| 2483 |
|
| 2484 |
// Overloaded assignment operator to clone |
| 2485 |
NamedFunctionParam &operator= (NamedFunctionParam const &other); |
| 2486 |
|
| 2487 |
// move constructors |
| 2488 |
NamedFunctionParam (NamedFunctionParam &&other) = default; |
| 2489 |
NamedFunctionParam &operator= (NamedFunctionParam &&other) = default; |
| 2490 |
|
| 2491 |
std::string as_string () const; |
| 2492 |
|
| 2493 |
Identifier get_param_name () const { return name; } |
| 2494 |
|
| 2495 |
Type &get_type () |
| 2496 |
{ |
| 2497 |
rust_assert (param_type); |
| 2498 |
return *param_type; |
| 2499 |
} |
| 2500 |
|
| 2501 |
Analysis::NodeMapping get_mappings () const { return mappings; } |
| 2502 |
}; |
| 2503 |
|
| 2504 |
// A function item used in an extern block |
| 2505 |
class ExternalFunctionItem : public ExternalItem |
| 2506 |
{ |
| 2507 |
// bool has_generics; |
| 2508 |
// Generics generic_params; |
| 2509 |
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined |
| 2510 |
|
| 2511 |
// bool has_return_type; |
| 2512 |
// FunctionReturnType return_type; |
| 2513 |
std::unique_ptr<Type> return_type; // inlined |
| 2514 |
|
| 2515 |
// bool has_where_clause; |
| 2516 |
WhereClause where_clause; |
| 2517 |
|
| 2518 |
std::vector<NamedFunctionParam> function_params; |
| 2519 |
bool has_variadics; |
| 2520 |
|
| 2521 |
public: |
| 2522 |
// Returns whether item has generic parameters. |
| 2523 |
bool has_generics () const { return !generic_params.empty (); } |
| 2524 |
|
| 2525 |
// Returns whether item has a return type (otherwise void). |
| 2526 |
bool has_return_type () const { return return_type != nullptr; } |
| 2527 |
|
| 2528 |
// Returns whether item has a where clause. |
| 2529 |
bool has_where_clause () const { return !where_clause.is_empty (); } |
| 2530 |
|
| 2531 |
WARN_UNUSED_RESULT const WhereClause &get_where_clause () const |
| 2532 |
{ |
| 2533 |
return where_clause; |
| 2534 |
} |
| 2535 |
|
| 2536 |
ExternalFunctionItem ( |
| 2537 |
Analysis::NodeMapping mappings, Identifier item_name, |
| 2538 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 2539 |
std::unique_ptr<Type> return_type, WhereClause where_clause, |
| 2540 |
std::vector<NamedFunctionParam> function_params, bool has_variadics, |
| 2541 |
Visibility vis, AST::AttrVec outer_attrs, location_t locus); |
| 2542 |
|
| 2543 |
// Copy constructor with clone |
| 2544 |
ExternalFunctionItem (ExternalFunctionItem const &other); |
| 2545 |
|
| 2546 |
// Overloaded assignment operator with clone |
| 2547 |
ExternalFunctionItem &operator= (ExternalFunctionItem const &other); |
| 2548 |
|
| 2549 |
// move constructors |
| 2550 |
ExternalFunctionItem (ExternalFunctionItem &&other) = default; |
| 2551 |
ExternalFunctionItem &operator= (ExternalFunctionItem &&other) = default; |
| 2552 |
|
| 2553 |
std::string as_string () const override; |
| 2554 |
|
| 2555 |
void accept_vis (HIRFullVisitor &vis) override; |
| 2556 |
void accept_vis (HIRExternalItemVisitor &vis) override; |
| 2557 |
|
| 2558 |
std::vector<std::unique_ptr<GenericParam>> &get_generic_params () |
| 2559 |
{ |
| 2560 |
return generic_params; |
| 2561 |
} |
| 2562 |
|
| 2563 |
Type &get_return_type () { return *return_type; } |
| 2564 |
|
| 2565 |
std::vector<NamedFunctionParam> &get_function_params () |
| 2566 |
{ |
| 2567 |
return function_params; |
| 2568 |
} |
| 2569 |
|
| 2570 |
bool is_variadic () const { return has_variadics; } |
| 2571 |
|
| 2572 |
ExternKind get_extern_kind () override { return ExternKind::Function; } |
| 2573 |
|
| 2574 |
protected: |
| 2575 |
/* Use covariance to implement clone function as returning this object |
| 2576 |
* rather than base */ |
| 2577 |
ExternalFunctionItem *clone_external_item_impl () const override |
| 2578 |
{ |
| 2579 |
return new ExternalFunctionItem (*this); |
| 2580 |
} |
| 2581 |
}; |
| 2582 |
|
| 2583 |
class ExternalTypeItem : public ExternalItem |
| 2584 |
{ |
| 2585 |
public: |
| 2586 |
ExternalTypeItem (Analysis::NodeMapping mappings, Identifier item_name, |
| 2587 |
Visibility vis, location_t locus); |
| 2588 |
|
| 2589 |
ExternalTypeItem (ExternalTypeItem const &other); |
| 2590 |
|
| 2591 |
ExternalTypeItem (ExternalTypeItem &&other) = default; |
| 2592 |
ExternalTypeItem &operator= (ExternalTypeItem &&other) = default; |
| 2593 |
ExternalTypeItem &operator= (ExternalTypeItem const &other) = default; |
| 2594 |
|
| 2595 |
std::string as_string () const override; |
| 2596 |
|
| 2597 |
void accept_vis (HIRFullVisitor &vis) override; |
| 2598 |
void accept_vis (HIRExternalItemVisitor &vis) override; |
| 2599 |
|
| 2600 |
ExternKind get_extern_kind () override { return ExternKind::Type; } |
| 2601 |
|
| 2602 |
protected: |
| 2603 |
/* Use covariance to implement clone function as returning this object |
| 2604 |
* rather than base */ |
| 2605 |
ExternalTypeItem *clone_external_item_impl () const override |
| 2606 |
{ |
| 2607 |
return new ExternalTypeItem (*this); |
| 2608 |
} |
| 2609 |
}; |
| 2610 |
|
| 2611 |
// An extern block HIR node |
| 2612 |
class ExternBlock : public VisItem, public WithInnerAttrs |
| 2613 |
{ |
| 2614 |
ABI abi; |
| 2615 |
std::vector<std::unique_ptr<ExternalItem>> extern_items; |
| 2616 |
location_t locus; |
| 2617 |
|
| 2618 |
public: |
| 2619 |
std::string as_string () const override; |
| 2620 |
|
| 2621 |
// Returns whether extern block has extern items. |
| 2622 |
bool has_extern_items () const { return !extern_items.empty (); } |
| 2623 |
|
| 2624 |
ABI get_abi () const { return abi; } |
| 2625 |
|
| 2626 |
ExternBlock (Analysis::NodeMapping mappings, ABI abi, |
| 2627 |
std::vector<std::unique_ptr<ExternalItem>> extern_items, |
| 2628 |
Visibility vis, AST::AttrVec inner_attrs, |
| 2629 |
AST::AttrVec outer_attrs, location_t locus); |
| 2630 |
|
| 2631 |
// Copy constructor with vector clone |
| 2632 |
ExternBlock (ExternBlock const &other); |
| 2633 |
|
| 2634 |
// Overloaded assignment operator with vector clone |
| 2635 |
ExternBlock &operator= (ExternBlock const &other); |
| 2636 |
|
| 2637 |
// move constructors |
| 2638 |
ExternBlock (ExternBlock &&other) = default; |
| 2639 |
ExternBlock &operator= (ExternBlock &&other) = default; |
| 2640 |
|
| 2641 |
location_t get_locus () const override final { return locus; } |
| 2642 |
|
| 2643 |
void accept_vis (HIRFullVisitor &vis) override; |
| 2644 |
void accept_vis (HIRStmtVisitor &vis) override; |
| 2645 |
void accept_vis (HIRVisItemVisitor &vis) override; |
| 2646 |
|
| 2647 |
std::vector<std::unique_ptr<ExternalItem>> &get_extern_items () |
| 2648 |
{ |
| 2649 |
return extern_items; |
| 2650 |
} |
| 2651 |
|
| 2652 |
ItemKind get_item_kind () const override { return ItemKind::ExternBlock; } |
| 2653 |
|
| 2654 |
protected: |
| 2655 |
/* Use covariance to implement clone function as returning this object |
| 2656 |
* rather than base */ |
| 2657 |
ExternBlock *clone_item_impl () const override |
| 2658 |
{ |
| 2659 |
return new ExternBlock (*this); |
| 2660 |
} |
| 2661 |
|
| 2662 |
/* Use covariance to implement clone function as returning this object |
| 2663 |
* rather than base */ |
| 2664 |
/*virtual ExternBlock* clone_statement_impl() const override { |
| 2665 |
return new ExternBlock(*this); |
| 2666 |
}*/ |
| 2667 |
}; |
| 2668 |
|
| 2669 |
} // namespace HIR |
| 2670 |
} // namespace Rust |
| 2671 |
|
| 2672 |
#endif |