]>
Commit | Line | Data |
---|---|---|
6441eb6d | 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
8ad1d56d PH |
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 | ||
a99d2909 | 22 | #include "optional.h" |
8ad1d56d | 23 | #include "rust-abi.h" |
417f4bd3 | 24 | #include "rust-hir-stmt.h" |
8ad1d56d | 25 | #include "rust-common.h" |
417f4bd3 PEP |
26 | #include "rust-hir-visibility.h" |
27 | #include "rust-hir-generic-param.h" | |
6246f658 | 28 | #include "rust-system.h" |
8ad1d56d PH |
29 | |
30 | namespace Rust { | |
31 | namespace HIR { | |
417f4bd3 PEP |
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 | }; | |
8ad1d56d PH |
93 | |
94 | // A type generic parameter (as opposed to a lifetime generic parameter) | |
95 | class TypeParam : public GenericParam | |
96 | { | |
3c5e8729 | 97 | AST::AttrVec outer_attrs; |
8ad1d56d PH |
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 | ||
a99d2909 | 106 | tl::optional<std::unique_ptr<Type>> type; |
8ad1d56d | 107 | |
d991a3f1 | 108 | location_t locus; |
8ad1d56d PH |
109 | |
110 | public: | |
111 | // Returns whether the type of the type param has been specified. | |
a99d2909 | 112 | bool has_type () const { return type.has_value (); } |
8ad1d56d PH |
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. | |
3c5e8729 LN |
118 | bool has_outer_attribute () const override { return outer_attrs.size () > 0; } |
119 | AST::AttrVec &get_outer_attrs () override { return outer_attrs; } | |
8ad1d56d PH |
120 | |
121 | TypeParam (Analysis::NodeMapping mappings, Identifier type_representation, | |
d991a3f1 | 122 | location_t locus = UNDEF_LOCATION, |
8ad1d56d PH |
123 | std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds |
124 | = std::vector<std::unique_ptr<TypeParamBound>> (), | |
a99d2909 | 125 | tl::optional<std::unique_ptr<Type>> type = tl::nullopt, |
417f4bd3 | 126 | AST::AttrVec outer_attrs = std::vector<AST::Attribute> ()); |
8ad1d56d PH |
127 | |
128 | // Copy constructor uses clone | |
417f4bd3 | 129 | TypeParam (TypeParam const &other); |
8ad1d56d PH |
130 | |
131 | // Overloaded assignment operator to clone | |
417f4bd3 | 132 | TypeParam &operator= (TypeParam const &other); |
a99d2909 | 133 | |
8ad1d56d PH |
134 | // move constructors |
135 | TypeParam (TypeParam &&other) = default; | |
a99d2909 | 136 | |
8ad1d56d PH |
137 | TypeParam &operator= (TypeParam &&other) = default; |
138 | ||
139 | std::string as_string () const override; | |
140 | ||
df1da364 | 141 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
142 | |
143 | void accept_vis (HIRFullVisitor &vis) override; | |
144 | ||
145 | Identifier get_type_representation () const { return type_representation; } | |
146 | ||
6246f658 PEP |
147 | Type &get_type () |
148 | { | |
a99d2909 PEP |
149 | rust_assert (*type); |
150 | return *type.value (); | |
6246f658 | 151 | } |
8ad1d56d | 152 | |
417f4bd3 | 153 | Analysis::NodeMapping get_type_mappings () const; |
8ad1d56d | 154 | |
417f4bd3 | 155 | std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds (); |
8ad1d56d PH |
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 */ | |
9499b41f | 167 | class WhereClauseItem : public FullVisitable |
8ad1d56d PH |
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; | |
d991a3f1 | 202 | location_t locus; |
8ad1d56d PH |
203 | Analysis::NodeMapping mappings; |
204 | ||
205 | public: | |
206 | LifetimeWhereClauseItem (Analysis::NodeMapping mappings, Lifetime lifetime, | |
207 | std::vector<Lifetime> lifetime_bounds, | |
d991a3f1 | 208 | location_t locus) |
8ad1d56d PH |
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; | |
d991a3f1 | 247 | location_t locus; |
8ad1d56d PH |
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, | |
417f4bd3 | 260 | location_t locus); |
8ad1d56d PH |
261 | |
262 | // Copy constructor requires clone | |
417f4bd3 | 263 | TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other); |
8ad1d56d PH |
264 | |
265 | // Overload assignment operator to clone | |
417f4bd3 | 266 | TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other); |
8ad1d56d PH |
267 | |
268 | // move constructors | |
269 | TypeBoundWhereClauseItem (TypeBoundWhereClauseItem &&other) = default; | |
270 | TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem &&other) | |
271 | = default; | |
272 | ||
df1da364 | 273 | location_t get_locus () const { return locus; } |
bee0c8a1 | 274 | |
8ad1d56d PH |
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 | ||
c29a3bc9 | 281 | Type &get_bound_type () { return *bound_type; } |
8ad1d56d | 282 | |
417f4bd3 | 283 | std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds (); |
8ad1d56d PH |
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; | |
827ceac9 | 374 | tl::optional<Lifetime> lifetime; |
8ad1d56d | 375 | std::unique_ptr<Type> type; |
d991a3f1 | 376 | location_t locus; |
8ad1d56d PH |
377 | Analysis::NodeMapping mappings; |
378 | ||
379 | SelfParam (Analysis::NodeMapping mappings, ImplicitSelfKind self_kind, | |
827ceac9 | 380 | tl::optional<Lifetime> lifetime, Type *type); |
8ad1d56d PH |
381 | |
382 | public: | |
383 | // Type-based self parameter (not ref, no lifetime) | |
384 | SelfParam (Analysis::NodeMapping mappings, std::unique_ptr<Type> type, | |
417f4bd3 | 385 | bool is_mut, location_t locus); |
8ad1d56d PH |
386 | |
387 | // Lifetime-based self parameter (is ref, no type) | |
827ceac9 PEP |
388 | SelfParam (Analysis::NodeMapping mappings, tl::optional<Lifetime> lifetime, |
389 | bool is_mut, location_t locus); | |
8ad1d56d PH |
390 | |
391 | // Copy constructor requires clone | |
417f4bd3 | 392 | SelfParam (SelfParam const &other); |
8ad1d56d PH |
393 | |
394 | // Overload assignment operator to use clone | |
417f4bd3 | 395 | SelfParam &operator= (SelfParam const &other); |
8ad1d56d PH |
396 | |
397 | // move constructors | |
398 | SelfParam (SelfParam &&other) = default; | |
399 | SelfParam &operator= (SelfParam &&other) = default; | |
400 | ||
8ad1d56d PH |
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. | |
827ceac9 | 405 | bool has_lifetime () const { return lifetime.has_value (); } |
cc946533 | 406 | |
827ceac9 | 407 | const Lifetime &get_lifetime () const { return lifetime.value (); } |
8ad1d56d PH |
408 | |
409 | std::string as_string () const; | |
410 | ||
df1da364 | 411 | location_t get_locus () const { return locus; } |
8ad1d56d PH |
412 | |
413 | ImplicitSelfKind get_self_kind () const { return self_kind; } | |
414 | ||
6246f658 PEP |
415 | Type &get_type () |
416 | { | |
417 | rust_assert (type); | |
418 | return *type; | |
419 | } | |
8ad1d56d PH |
420 | |
421 | Analysis::NodeMapping get_mappings () { return mappings; } | |
422 | ||
417f4bd3 | 423 | Mutability get_mut () const; |
8ad1d56d | 424 | |
417f4bd3 | 425 | bool is_mut () const; |
8ad1d56d | 426 | |
417f4bd3 | 427 | bool is_ref () const; |
8ad1d56d PH |
428 | }; |
429 | ||
430 | // Qualifiers for function, i.e. const, unsafe, extern etc. | |
431 | struct FunctionQualifiers | |
432 | { | |
433 | private: | |
12844c82 PEP |
434 | Async async_status; |
435 | Const const_status; | |
8ad1d56d PH |
436 | Unsafety unsafety; |
437 | bool has_extern; | |
438 | ABI abi; | |
439 | ||
440 | public: | |
12844c82 | 441 | FunctionQualifiers (Async async_status, Const const_status, Unsafety unsafety, |
8ad1d56d | 442 | bool has_extern, ABI abi) |
12844c82 PEP |
443 | : async_status (async_status), const_status (const_status), |
444 | unsafety (unsafety), has_extern (has_extern), abi (abi) | |
8ad1d56d PH |
445 | {} |
446 | ||
447 | std::string as_string () const; | |
448 | ||
12844c82 | 449 | Const get_const_status () const { return const_status; } |
8ad1d56d | 450 | |
12844c82 | 451 | bool is_const () const { return const_status == Const::Yes; } |
8ad1d56d | 452 | bool is_unsafe () const { return unsafety == Unsafety::Unsafe; } |
12844c82 | 453 | bool is_async () const { return async_status == Async::Yes; } |
8ad1d56d PH |
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; | |
d991a3f1 | 463 | location_t locus; |
8ad1d56d PH |
464 | Analysis::NodeMapping mappings; |
465 | ||
466 | public: | |
467 | FunctionParam (Analysis::NodeMapping mappings, | |
468 | std::unique_ptr<Pattern> param_name, | |
417f4bd3 | 469 | std::unique_ptr<Type> param_type, location_t locus); |
8ad1d56d PH |
470 | |
471 | // Copy constructor uses clone | |
417f4bd3 | 472 | FunctionParam (FunctionParam const &other); |
8ad1d56d PH |
473 | |
474 | // Overload assignment operator to use clone | |
417f4bd3 | 475 | FunctionParam &operator= (FunctionParam const &other); |
8ad1d56d PH |
476 | |
477 | // move constructors | |
478 | FunctionParam (FunctionParam &&other) = default; | |
479 | FunctionParam &operator= (FunctionParam &&other) = default; | |
480 | ||
481 | std::string as_string () const; | |
482 | ||
df1da364 | 483 | location_t get_locus () const { return locus; } |
8ad1d56d | 484 | |
c29a3bc9 | 485 | Pattern &get_param_name () { return *param_name; } |
8ad1d56d | 486 | |
6246f658 PEP |
487 | Type &get_type () |
488 | { | |
489 | rust_assert (type); | |
490 | return *type; | |
491 | } | |
8ad1d56d PH |
492 | |
493 | const Analysis::NodeMapping &get_mappings () const { return mappings; } | |
494 | }; | |
495 | ||
8ad1d56d PH |
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 | |
417f4bd3 | 510 | VisItem (VisItem const &other); |
8ad1d56d PH |
511 | |
512 | // Overload assignment operator to clone | |
417f4bd3 | 513 | VisItem &operator= (VisItem const &other); |
8ad1d56d PH |
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 | |
16510f1c | 537 | class Module : public VisItem, public WithInnerAttrs |
8ad1d56d PH |
538 | { |
539 | Identifier module_name; | |
d991a3f1 | 540 | location_t locus; |
8ad1d56d PH |
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 | ||
8ad1d56d PH |
550 | // Full constructor |
551 | Module (Analysis::NodeMapping mappings, Identifier module_name, | |
d991a3f1 | 552 | location_t locus, std::vector<std::unique_ptr<Item>> items, |
8ad1d56d PH |
553 | Visibility visibility = Visibility::create_error (), |
554 | AST::AttrVec inner_attrs = AST::AttrVec (), | |
417f4bd3 | 555 | AST::AttrVec outer_attrs = AST::AttrVec ()); |
8ad1d56d PH |
556 | |
557 | // Copy constructor with vector clone | |
417f4bd3 | 558 | Module (Module const &other); |
8ad1d56d PH |
559 | |
560 | // Overloaded assignment operator with vector clone | |
417f4bd3 | 561 | Module &operator= (Module const &other); |
8ad1d56d PH |
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 | ||
d10dbce1 | 571 | Identifier get_module_name () const { return module_name; } |
8ad1d56d PH |
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 | ||
df1da364 | 578 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
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 | ||
d991a3f1 | 604 | location_t locus; |
8ad1d56d PH |
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, | |
d991a3f1 OA |
622 | Visibility visibility, AST::AttrVec outer_attrs, |
623 | location_t locus, std::string as_clause_name = std::string ()) | |
8ad1d56d PH |
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 | ||
df1da364 | 630 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
631 | |
632 | ItemKind get_item_kind () const override { return ItemKind::ExternCrate; } | |
d10dbce1 MP |
633 | std::string get_referenced_crate () { return referenced_crate; } |
634 | std::string get_as_clause_name () { return as_clause_name; } | |
8ad1d56d PH |
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 | |
9499b41f | 662 | class UseTree : public FullVisitable |
8ad1d56d | 663 | { |
d991a3f1 | 664 | location_t locus; |
8ad1d56d PH |
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 | ||
df1da364 | 677 | location_t get_locus () const { return locus; } |
8ad1d56d | 678 | |
8ad1d56d PH |
679 | protected: |
680 | // Clone function implementation as pure virtual method | |
681 | virtual UseTree *clone_use_tree_impl () const = 0; | |
682 | ||
d991a3f1 | 683 | UseTree (location_t locus) : locus (locus) {} |
8ad1d56d PH |
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: | |
d991a3f1 | 702 | UseTreeGlob (PathType glob_type, AST::SimplePath path, location_t locus) |
8ad1d56d PH |
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 | ||
d10dbce1 MP |
719 | PathType get_glob_type () { return glob_type; } |
720 | AST::SimplePath get_path () { return path; }; | |
721 | ||
8ad1d56d PH |
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, | |
d991a3f1 | 756 | std::vector<std::unique_ptr<UseTree>> trees, location_t locus) |
8ad1d56d PH |
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 | ||
d10dbce1 MP |
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 | ||
8ad1d56d PH |
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: | |
d991a3f1 | 840 | UseTreeRebind (NewBindType bind_type, AST::SimplePath path, location_t locus, |
8ad1d56d PH |
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 | ||
9499b41f MP |
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 | ||
8ad1d56d PH |
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 | ||
bcd76dbc MP |
873 | std::string enum_to_str (UseTreeRebind::NewBindType); |
874 | ||
8ad1d56d PH |
875 | // Rust use declaration (i.e. for modules) HIR node |
876 | class UseDeclaration : public VisItem | |
877 | { | |
878 | std::unique_ptr<UseTree> use_tree; | |
d991a3f1 | 879 | location_t locus; |
8ad1d56d PH |
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, | |
d991a3f1 | 886 | AST::AttrVec outer_attrs, location_t locus) |
8ad1d56d PH |
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 | ||
df1da364 | 914 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
915 | ItemKind get_item_kind () const override { return ItemKind::UseDeclaration; } |
916 | ||
c29a3bc9 | 917 | UseTree &get_use_tree () { return *use_tree; } |
8ad1d56d PH |
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 | ||
6410c1dd AC |
939 | enum class Defaultness |
940 | { | |
941 | Default, | |
942 | Final, | |
943 | }; | |
944 | ||
8ad1d56d PH |
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; | |
827ceac9 | 955 | tl::optional<SelfParam> self; |
d991a3f1 | 956 | location_t locus; |
8ad1d56d | 957 | |
6410c1dd AC |
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 | ||
8ad1d56d PH |
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 | ||
6410c1dd AC |
978 | // Returns whether function has a default qualifier |
979 | bool is_default () const { return defaultness == Defaultness::Default; } | |
980 | ||
8ad1d56d PH |
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, | |
827ceac9 PEP |
995 | AST::AttrVec outer_attrs, tl::optional<SelfParam> self, |
996 | Defaultness defaultness, location_t locus); | |
8ad1d56d PH |
997 | |
998 | // Copy constructor with clone | |
417f4bd3 | 999 | Function (Function const &other); |
8ad1d56d PH |
1000 | |
1001 | // Overloaded assignment operator to clone | |
417f4bd3 | 1002 | Function &operator= (Function const &other); |
8ad1d56d PH |
1003 | |
1004 | // move constructors | |
1005 | Function (Function &&other) = default; | |
1006 | Function &operator= (Function &&other) = default; | |
1007 | ||
df1da364 | 1008 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
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? | |
c29a3bc9 | 1036 | BlockExpr &get_definition () { return *function_body; } |
8ad1d56d PH |
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? | |
c29a3bc9 | 1048 | Type &get_return_type () { return *return_type; } |
8ad1d56d | 1049 | |
827ceac9 | 1050 | bool is_method () const { return self.has_value (); } |
8ad1d56d | 1051 | |
827ceac9 PEP |
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 (); } | |
8ad1d56d | 1057 | |
e90d06c5 PH |
1058 | std::string get_impl_item_name () const override final |
1059 | { | |
fcb228d1 | 1060 | return get_function_name ().as_string (); |
e90d06c5 PH |
1061 | } |
1062 | ||
8ad1d56d PH |
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 | ||
d991a3f1 | 1090 | location_t locus; |
8ad1d56d PH |
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, | |
417f4bd3 | 1110 | Visibility vis, AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
1111 | |
1112 | // Copy constructor | |
417f4bd3 | 1113 | TypeAlias (TypeAlias const &other); |
8ad1d56d PH |
1114 | |
1115 | // Overloaded assignment operator to clone | |
417f4bd3 | 1116 | TypeAlias &operator= (TypeAlias const &other); |
8ad1d56d PH |
1117 | |
1118 | // move constructors | |
1119 | TypeAlias (TypeAlias &&other) = default; | |
1120 | TypeAlias &operator= (TypeAlias &&other) = default; | |
1121 | ||
df1da364 | 1122 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
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 | ||
6246f658 PEP |
1140 | Type &get_type_aliased () |
1141 | { | |
1142 | rust_assert (existing_type); | |
1143 | return *existing_type; | |
1144 | } | |
8ad1d56d PH |
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 | ||
e90d06c5 PH |
1155 | std::string get_impl_item_name () const override final |
1156 | { | |
fcb228d1 | 1157 | return get_new_type_name ().as_string (); |
e90d06c5 PH |
1158 | } |
1159 | ||
8ad1d56d PH |
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 | ||
d991a3f1 | 1187 | location_t locus; |
8ad1d56d PH |
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 | ||
df1da364 | 1198 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
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, | |
d991a3f1 | 1211 | WhereClause where_clause, Visibility vis, location_t locus, |
8ad1d56d PH |
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 | |
9499b41f | 1250 | // FIXME can't this be a TupleStruct + field_name? |
16510f1c | 1251 | class StructField |
8ad1d56d PH |
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 | ||
d991a3f1 | 1265 | location_t locus; |
8ad1d56d PH |
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, | |
d991a3f1 | 1274 | std::unique_ptr<Type> field_type, Visibility vis, |
417f4bd3 | 1275 | location_t locus, AST::AttrVec outer_attrs = AST::AttrVec ()); |
8ad1d56d PH |
1276 | |
1277 | // Copy constructor | |
417f4bd3 | 1278 | StructField (StructField const &other); |
8ad1d56d PH |
1279 | |
1280 | ~StructField () = default; | |
1281 | ||
1282 | // Overloaded assignment operator to clone | |
417f4bd3 | 1283 | StructField &operator= (StructField const &other); |
8ad1d56d PH |
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 | ||
c29a3bc9 | 1293 | Type &get_field_type () { return *field_type; } |
8ad1d56d PH |
1294 | |
1295 | Analysis::NodeMapping get_mappings () const { return mappings; } | |
1296 | ||
df1da364 | 1297 | location_t get_locus () { return locus; } |
9499b41f | 1298 | AST::AttrVec &get_outer_attrs () { return outer_attrs; } |
8ad1d56d PH |
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, | |
d991a3f1 | 1316 | AST::AttrVec outer_attrs, location_t locus) |
8ad1d56d PH |
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, | |
d991a3f1 | 1327 | AST::AttrVec outer_attrs, location_t locus) |
8ad1d56d PH |
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 | |
9499b41f | 1362 | class TupleField |
8ad1d56d PH |
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 | ||
d991a3f1 | 1373 | location_t locus; |
8ad1d56d PH |
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, | |
d991a3f1 | 1387 | Visibility vis, location_t locus, |
417f4bd3 | 1388 | AST::AttrVec outer_attrs = AST::AttrVec ()); |
8ad1d56d PH |
1389 | |
1390 | // Copy constructor with clone | |
417f4bd3 | 1391 | TupleField (TupleField const &other); |
8ad1d56d PH |
1392 | |
1393 | ~TupleField () = default; | |
1394 | ||
1395 | // Overloaded assignment operator to clone | |
417f4bd3 | 1396 | TupleField &operator= (TupleField const &other); |
8ad1d56d PH |
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 | ||
9499b41f MP |
1409 | Visibility &get_visibility () { return visibility; } |
1410 | ||
df1da364 | 1411 | location_t get_locus () const { return locus; } |
8ad1d56d | 1412 | |
9499b41f | 1413 | AST::AttrVec &get_outer_attrs () { return outer_attrs; } |
c29a3bc9 | 1414 | HIR::Type &get_field_type () { return *field_type; } |
8ad1d56d PH |
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, | |
417f4bd3 | 1430 | AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
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; | |
d991a3f1 | 1461 | location_t locus; |
8ad1d56d PH |
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, | |
417f4bd3 | 1475 | AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
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 | ||
df1da364 | 1491 | location_t get_locus () const override { return locus; } |
8ad1d56d PH |
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, | |
417f4bd3 | 1518 | location_t locus); |
8ad1d56d PH |
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, | |
417f4bd3 | 1552 | AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
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, | |
417f4bd3 | 1577 | location_t locus); |
8ad1d56d PH |
1578 | |
1579 | // Copy constructor with clone | |
417f4bd3 | 1580 | EnumItemDiscriminant (EnumItemDiscriminant const &other); |
8ad1d56d PH |
1581 | |
1582 | // Overloaded assignment operator to clone | |
417f4bd3 | 1583 | EnumItemDiscriminant &operator= (EnumItemDiscriminant const &other); |
8ad1d56d PH |
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 | ||
c29a3bc9 PEP |
1599 | Expr &get_discriminant_expression () { return *expression; } |
1600 | ||
1601 | std::unique_ptr<Expr> take_discriminant_expression () | |
1602 | { | |
1603 | return std::move (expression); | |
1604 | } | |
8ad1d56d PH |
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 | ||
d991a3f1 | 1628 | location_t locus; |
8ad1d56d PH |
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, | |
417f4bd3 | 1647 | AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
1648 | |
1649 | // TODO: constructor with less arguments | |
1650 | ||
1651 | // Copy constructor with vector clone | |
417f4bd3 | 1652 | Enum (Enum const &other); |
8ad1d56d PH |
1653 | |
1654 | // Overloaded assignment operator with vector clone | |
417f4bd3 | 1655 | Enum &operator= (Enum const &other); |
8ad1d56d PH |
1656 | |
1657 | // Move constructors | |
1658 | Enum (Enum &&other) = default; | |
1659 | Enum &operator= (Enum &&other) = default; | |
1660 | ||
df1da364 | 1661 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
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 | ||
7e7cffb7 | 1680 | std::vector<std::unique_ptr<EnumItem>> &get_variants () { return items; } |
0ee496d5 | 1681 | WhereClause &get_where_clause () { return where_clause; } |
7e7cffb7 | 1682 | |
8ad1d56d PH |
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 | ||
d991a3f1 | 1709 | location_t locus; |
8ad1d56d PH |
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, | |
417f4bd3 | 1723 | AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
1724 | |
1725 | // copy constructor with vector clone | |
417f4bd3 | 1726 | Union (Union const &other); |
8ad1d56d PH |
1727 | |
1728 | // overloaded assignment operator with vector clone | |
417f4bd3 | 1729 | Union &operator= (Union const &other); |
8ad1d56d PH |
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 | ||
df1da364 | 1742 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
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; | |
d991a3f1 | 1765 | location_t locus; |
8ad1d56d PH |
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, | |
417f4bd3 | 1773 | location_t locus); |
8ad1d56d | 1774 | |
417f4bd3 | 1775 | ConstantItem (ConstantItem const &other); |
8ad1d56d PH |
1776 | |
1777 | // Overload assignment operator to clone | |
417f4bd3 | 1778 | ConstantItem &operator= (ConstantItem const &other); |
8ad1d56d PH |
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. | |
fcb228d1 PEP |
1786 | bool is_unnamed () const |
1787 | { | |
1788 | return identifier.as_string () == std::string ("_"); | |
1789 | } | |
8ad1d56d | 1790 | |
df1da364 | 1791 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
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 | ||
6246f658 PEP |
1798 | Type &get_type () |
1799 | { | |
1800 | rust_assert (type); | |
1801 | return *type; | |
1802 | } | |
8ad1d56d | 1803 | |
c29a3bc9 | 1804 | Expr &get_expr () { return *const_expr; } |
8ad1d56d | 1805 | |
fcb228d1 | 1806 | Identifier get_identifier () const { return identifier; } |
8ad1d56d PH |
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 | ||
e90d06c5 PH |
1820 | std::string get_impl_item_name () const override final |
1821 | { | |
fcb228d1 | 1822 | return get_identifier ().as_string (); |
e90d06c5 PH |
1823 | } |
1824 | ||
8ad1d56d PH |
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; | |
d991a3f1 | 1849 | location_t locus; |
8ad1d56d PH |
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, | |
417f4bd3 | 1856 | Visibility vis, AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
1857 | |
1858 | // Copy constructor with clone | |
417f4bd3 | 1859 | StaticItem (StaticItem const &other); |
8ad1d56d PH |
1860 | |
1861 | // Overloaded assignment operator to clone | |
417f4bd3 | 1862 | StaticItem &operator= (StaticItem const &other); |
8ad1d56d PH |
1863 | |
1864 | // move constructors | |
1865 | StaticItem (StaticItem &&other) = default; | |
1866 | StaticItem &operator= (StaticItem &&other) = default; | |
1867 | ||
df1da364 | 1868 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
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 | ||
6246f658 PEP |
1880 | Expr &get_expr () |
1881 | { | |
1882 | rust_assert (expr); | |
1883 | return *expr; | |
1884 | } | |
8ad1d56d | 1885 | |
6246f658 PEP |
1886 | Type &get_type () |
1887 | { | |
1888 | rust_assert (type); | |
1889 | return *type; | |
1890 | } | |
8ad1d56d PH |
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 | |
9499b41f | 1902 | class TraitFunctionDecl |
8ad1d56d PH |
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; | |
827ceac9 | 1911 | tl::optional<SelfParam> self; |
8ad1d56d PH |
1912 | |
1913 | public: | |
1914 | // Mega-constructor | |
1915 | TraitFunctionDecl (Identifier function_name, FunctionQualifiers qualifiers, | |
1916 | std::vector<std::unique_ptr<GenericParam>> generic_params, | |
827ceac9 PEP |
1917 | tl::optional<SelfParam> self, |
1918 | std::vector<FunctionParam> function_params, | |
8ad1d56d | 1919 | std::unique_ptr<Type> return_type, |
417f4bd3 | 1920 | WhereClause where_clause); |
8ad1d56d PH |
1921 | |
1922 | // Copy constructor with clone | |
417f4bd3 | 1923 | TraitFunctionDecl (TraitFunctionDecl const &other); |
8ad1d56d PH |
1924 | |
1925 | ~TraitFunctionDecl () = default; | |
1926 | ||
1927 | // Overloaded assignment operator with clone | |
417f4bd3 | 1928 | TraitFunctionDecl &operator= (TraitFunctionDecl const &other); |
8ad1d56d PH |
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 | ||
9499b41f MP |
1948 | WhereClause &get_where_clause () { return where_clause; } |
1949 | ||
827ceac9 PEP |
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 (); } | |
8ad1d56d | 1954 | |
827ceac9 PEP |
1955 | tl::optional<SelfParam> &get_self () { return self; } |
1956 | const tl::optional<SelfParam> &get_self () const { return self; } | |
8ad1d56d PH |
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 | ||
c29a3bc9 | 1965 | Type &get_return_type () { return *return_type; } |
8ad1d56d PH |
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; | |
d991a3f1 | 1978 | location_t locus; |
8ad1d56d PH |
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, | |
417f4bd3 | 1986 | AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
1987 | |
1988 | // Copy constructor with clone | |
417f4bd3 | 1989 | TraitItemFunc (TraitItemFunc const &other); |
8ad1d56d PH |
1990 | |
1991 | // Overloaded assignment operator to clone | |
417f4bd3 | 1992 | TraitItemFunc &operator= (TraitItemFunc const &other); |
8ad1d56d PH |
1993 | |
1994 | // move constructors | |
1995 | TraitItemFunc (TraitItemFunc &&other) = default; | |
1996 | TraitItemFunc &operator= (TraitItemFunc &&other) = default; | |
1997 | ||
1998 | std::string as_string () const override; | |
1999 | ||
df1da364 | 2000 | location_t get_locus () const { return locus; } |
8ad1d56d PH |
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 | ||
c29a3bc9 | 2009 | BlockExpr &get_block_expr () { return *block_expr; } |
8ad1d56d PH |
2010 | |
2011 | const std::string trait_identifier () const override final | |
2012 | { | |
fcb228d1 | 2013 | return decl.get_function_name ().as_string (); |
8ad1d56d PH |
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 | ||
e99e565e | 2027 | location_t get_trait_locus () const override { return get_locus (); } |
8f0b1526 | 2028 | |
8ad1d56d PH |
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; | |
d991a3f1 | 2044 | location_t locus; |
8ad1d56d PH |
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, | |
417f4bd3 | 2052 | AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
2053 | |
2054 | // Copy constructor with clones | |
417f4bd3 | 2055 | TraitItemConst (TraitItemConst const &other); |
8ad1d56d PH |
2056 | |
2057 | // Overloaded assignment operator to clone | |
417f4bd3 | 2058 | TraitItemConst &operator= (TraitItemConst const &other); |
8ad1d56d PH |
2059 | |
2060 | // move constructors | |
2061 | TraitItemConst (TraitItemConst &&other) = default; | |
2062 | TraitItemConst &operator= (TraitItemConst &&other) = default; | |
2063 | ||
2064 | std::string as_string () const override; | |
2065 | ||
df1da364 | 2066 | location_t get_locus () const { return locus; } |
8ad1d56d PH |
2067 | |
2068 | void accept_vis (HIRFullVisitor &vis) override; | |
2069 | void accept_vis (HIRTraitItemVisitor &vis) override; | |
2070 | ||
2071 | Identifier get_name () const { return name; } | |
2072 | ||
56408be2 PH |
2073 | bool has_type () const { return expr != nullptr; } |
2074 | ||
8ad1d56d PH |
2075 | bool has_expr () const { return expr != nullptr; } |
2076 | ||
6246f658 PEP |
2077 | Type &get_type () |
2078 | { | |
2079 | rust_assert (type); | |
2080 | return *type; | |
2081 | } | |
8ad1d56d | 2082 | |
6246f658 PEP |
2083 | Expr &get_expr () |
2084 | { | |
2085 | rust_assert (expr); | |
2086 | return *expr; | |
2087 | } | |
8ad1d56d | 2088 | |
fcb228d1 PEP |
2089 | const std::string trait_identifier () const override final |
2090 | { | |
2091 | return name.as_string (); | |
2092 | } | |
8ad1d56d PH |
2093 | |
2094 | TraitItemKind get_item_kind () const override final | |
2095 | { | |
2096 | return TraitItemKind::CONST; | |
2097 | } | |
2098 | ||
2099 | AST::AttrVec &get_outer_attrs () override final { return outer_attrs; } | |
2100 | const AST::AttrVec &get_outer_attrs () const override final | |
2101 | { | |
2102 | return outer_attrs; | |
2103 | } | |
2104 | ||
e99e565e | 2105 | location_t get_trait_locus () const override { return get_locus (); } |
8f0b1526 | 2106 | |
8ad1d56d PH |
2107 | protected: |
2108 | // Clone function implementation as (not pure) virtual method | |
2109 | TraitItemConst *clone_trait_item_impl () const override | |
2110 | { | |
2111 | return new TraitItemConst (*this); | |
2112 | } | |
2113 | }; | |
2114 | ||
2115 | // Type items within traits | |
2116 | class TraitItemType : public TraitItem | |
2117 | { | |
2118 | AST::AttrVec outer_attrs; | |
2119 | ||
2120 | Identifier name; | |
2121 | std::vector<std::unique_ptr<TypeParamBound>> | |
2122 | type_param_bounds; // inlined form | |
d991a3f1 | 2123 | location_t locus; |
8ad1d56d PH |
2124 | |
2125 | public: | |
2126 | // Returns whether trait item type has type param bounds. | |
2127 | bool has_type_param_bounds () const { return !type_param_bounds.empty (); } | |
2128 | ||
2129 | TraitItemType (Analysis::NodeMapping mappings, Identifier name, | |
2130 | std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds, | |
417f4bd3 | 2131 | AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
2132 | |
2133 | // Copy constructor with vector clone | |
417f4bd3 | 2134 | TraitItemType (TraitItemType const &other); |
8ad1d56d PH |
2135 | |
2136 | // Overloaded assignment operator with vector clone | |
417f4bd3 | 2137 | TraitItemType &operator= (TraitItemType const &other); |
8ad1d56d PH |
2138 | |
2139 | // default move constructors | |
2140 | TraitItemType (TraitItemType &&other) = default; | |
2141 | TraitItemType &operator= (TraitItemType &&other) = default; | |
2142 | ||
2143 | std::string as_string () const override; | |
2144 | ||
df1da364 | 2145 | location_t get_locus () const { return locus; } |
8ad1d56d PH |
2146 | |
2147 | void accept_vis (HIRFullVisitor &vis) override; | |
2148 | void accept_vis (HIRTraitItemVisitor &vis) override; | |
2149 | ||
2150 | Identifier get_name () const { return name; } | |
2151 | ||
2152 | std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds () | |
2153 | { | |
2154 | return type_param_bounds; | |
2155 | } | |
2156 | ||
fcb228d1 PEP |
2157 | const std::string trait_identifier () const override final |
2158 | { | |
2159 | return name.as_string (); | |
2160 | } | |
8ad1d56d PH |
2161 | |
2162 | TraitItemKind get_item_kind () const override final | |
2163 | { | |
2164 | return TraitItemKind::TYPE; | |
2165 | } | |
2166 | ||
2167 | AST::AttrVec &get_outer_attrs () override final { return outer_attrs; } | |
2168 | const AST::AttrVec &get_outer_attrs () const override final | |
2169 | { | |
2170 | return outer_attrs; | |
2171 | } | |
2172 | ||
e99e565e | 2173 | location_t get_trait_locus () const override { return get_locus (); } |
8f0b1526 | 2174 | |
8ad1d56d PH |
2175 | protected: |
2176 | // Clone function implementation as (not pure) virtual method | |
2177 | TraitItemType *clone_trait_item_impl () const override | |
2178 | { | |
2179 | return new TraitItemType (*this); | |
2180 | } | |
2181 | }; | |
2182 | ||
2183 | // Rust trait item declaration HIR node | |
2184 | class Trait : public VisItem | |
2185 | { | |
2186 | Unsafety unsafety; | |
2187 | Identifier name; | |
2188 | std::vector<std::unique_ptr<GenericParam>> generic_params; | |
2189 | std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds; | |
2190 | WhereClause where_clause; | |
2191 | std::vector<std::unique_ptr<TraitItem>> trait_items; | |
d991a3f1 | 2192 | location_t locus; |
8ad1d56d PH |
2193 | |
2194 | public: | |
2195 | std::string as_string () const override; | |
2196 | ||
2197 | // Returns whether trait has generic parameters. | |
2198 | bool has_generics () const { return !generic_params.empty (); } | |
2199 | ||
2200 | // Returns whether trait has type parameter bounds. | |
2201 | bool has_type_param_bounds () const { return !type_param_bounds.empty (); } | |
2202 | ||
2203 | // Returns whether trait has where clause. | |
2204 | bool has_where_clause () const { return !where_clause.is_empty (); } | |
2205 | ||
2206 | // Returns whether trait has trait items. | |
2207 | bool has_trait_items () const { return !trait_items.empty (); } | |
2208 | ||
2209 | std::vector<std::unique_ptr<TraitItem>> &get_trait_items () | |
2210 | { | |
2211 | return trait_items; | |
2212 | } | |
2213 | ||
0ee496d5 MP |
2214 | WhereClause &get_where_clause () { return where_clause; } |
2215 | ||
8ad1d56d | 2216 | Identifier get_name () const { return name; } |
0ee496d5 | 2217 | bool is_unsafe () const { return unsafety == Unsafety::Unsafe; } |
8ad1d56d PH |
2218 | |
2219 | // Mega-constructor | |
2220 | Trait (Analysis::NodeMapping mappings, Identifier name, Unsafety unsafety, | |
2221 | std::vector<std::unique_ptr<GenericParam>> generic_params, | |
2222 | std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds, | |
2223 | WhereClause where_clause, | |
2224 | std::vector<std::unique_ptr<TraitItem>> trait_items, Visibility vis, | |
417f4bd3 | 2225 | AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
2226 | |
2227 | // Copy constructor with vector clone | |
417f4bd3 | 2228 | Trait (Trait const &other); |
8ad1d56d PH |
2229 | |
2230 | // Overloaded assignment operator with vector clone | |
417f4bd3 | 2231 | Trait &operator= (Trait const &other); |
8ad1d56d PH |
2232 | |
2233 | // default move constructors | |
2234 | Trait (Trait &&other) = default; | |
2235 | Trait &operator= (Trait &&other) = default; | |
2236 | ||
df1da364 | 2237 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
2238 | |
2239 | void accept_vis (HIRFullVisitor &vis) override; | |
2240 | void accept_vis (HIRStmtVisitor &vis) override; | |
2241 | void accept_vis (HIRVisItemVisitor &vis) override; | |
2242 | ||
2243 | std::vector<std::unique_ptr<GenericParam>> &get_generic_params () | |
2244 | { | |
2245 | return generic_params; | |
2246 | } | |
2247 | ||
2248 | const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const | |
2249 | { | |
2250 | return generic_params; | |
2251 | } | |
2252 | ||
2253 | std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds () | |
2254 | { | |
2255 | return type_param_bounds; | |
2256 | } | |
2257 | ||
2258 | const std::vector<std::unique_ptr<TypeParamBound>> & | |
2259 | get_type_param_bounds () const | |
2260 | { | |
2261 | return type_param_bounds; | |
2262 | } | |
2263 | ||
2264 | ItemKind get_item_kind () const override { return ItemKind::Trait; } | |
2265 | ||
2266 | protected: | |
2267 | /* Use covariance to implement clone function as returning this object | |
2268 | * rather than base */ | |
2269 | Trait *clone_item_impl () const override { return new Trait (*this); } | |
2270 | }; | |
2271 | ||
16510f1c | 2272 | class ImplBlock : public VisItem, public WithInnerAttrs |
8ad1d56d PH |
2273 | { |
2274 | std::vector<std::unique_ptr<GenericParam>> generic_params; | |
2275 | std::unique_ptr<Type> impl_type; | |
2276 | std::unique_ptr<TypePath> trait_ref; | |
2277 | WhereClause where_clause; | |
c533a11a | 2278 | BoundPolarity polarity; |
d991a3f1 | 2279 | location_t locus; |
8ad1d56d | 2280 | std::vector<std::unique_ptr<ImplItem>> impl_items; |
3c5e8729 | 2281 | bool unsafe; |
8ad1d56d PH |
2282 | |
2283 | public: | |
2284 | ImplBlock (Analysis::NodeMapping mappings, | |
2285 | std::vector<std::unique_ptr<ImplItem>> impl_items, | |
2286 | std::vector<std::unique_ptr<GenericParam>> generic_params, | |
2287 | std::unique_ptr<Type> impl_type, | |
2288 | std::unique_ptr<TypePath> trait_ref, WhereClause where_clause, | |
c533a11a | 2289 | BoundPolarity polarity, Visibility vis, AST::AttrVec inner_attrs, |
417f4bd3 | 2290 | AST::AttrVec outer_attrs, location_t locus, bool unsafe = false); |
8ad1d56d | 2291 | |
417f4bd3 | 2292 | ImplBlock (ImplBlock const &other); |
8ad1d56d | 2293 | |
417f4bd3 | 2294 | ImplBlock &operator= (ImplBlock const &other); |
8ad1d56d PH |
2295 | |
2296 | ImplBlock (ImplBlock &&other) = default; | |
2297 | ImplBlock &operator= (ImplBlock &&other) = default; | |
2298 | ||
2299 | std::string as_string () const override; | |
2300 | ||
2301 | // Returns whether inherent impl block has inherent impl items. | |
2302 | bool has_impl_items () const { return !impl_items.empty (); } | |
2303 | ||
3c5e8729 LN |
2304 | bool is_unsafe () const { return unsafe; } |
2305 | ||
8ad1d56d PH |
2306 | void accept_vis (HIRFullVisitor &vis) override; |
2307 | void accept_vis (HIRStmtVisitor &vis) override; | |
2308 | void accept_vis (HIRVisItemVisitor &vis) override; | |
2309 | ||
2310 | std::vector<std::unique_ptr<ImplItem>> &get_impl_items () | |
2311 | { | |
2312 | return impl_items; | |
2313 | }; | |
2314 | ||
2315 | const std::vector<std::unique_ptr<ImplItem>> &get_impl_items () const | |
2316 | { | |
2317 | return impl_items; | |
2318 | }; | |
2319 | ||
2320 | // Returns whether impl has generic parameters. | |
2321 | bool has_generics () const { return !generic_params.empty (); } | |
2322 | ||
2323 | // Returns whether impl has where clause. | |
2324 | bool has_where_clause () const { return !where_clause.is_empty (); } | |
2325 | ||
2326 | // Returns the polarity of the impl. | |
c533a11a | 2327 | BoundPolarity get_polarity () const { return polarity; } |
8ad1d56d | 2328 | |
df1da364 | 2329 | location_t get_locus () const override final { return locus; } |
8ad1d56d | 2330 | |
6246f658 PEP |
2331 | Type &get_type () |
2332 | { | |
2333 | rust_assert (impl_type); | |
2334 | return *impl_type; | |
2335 | }; | |
c29a3bc9 | 2336 | |
6246f658 | 2337 | bool has_type () { return impl_type != nullptr; } |
8ad1d56d PH |
2338 | |
2339 | std::vector<std::unique_ptr<GenericParam>> &get_generic_params () | |
2340 | { | |
2341 | return generic_params; | |
2342 | } | |
2343 | ||
2344 | bool has_trait_ref () const { return trait_ref != nullptr; } | |
2345 | ||
c29a3bc9 | 2346 | TypePath &get_trait_ref () { return *trait_ref; } |
8ad1d56d PH |
2347 | |
2348 | WhereClause &get_where_clause () { return where_clause; } | |
2349 | ||
2350 | ItemKind get_item_kind () const override { return ItemKind::Impl; } | |
2351 | ||
2352 | protected: | |
2353 | ImplBlock *clone_item_impl () const override { return new ImplBlock (*this); } | |
2354 | }; | |
2355 | ||
2356 | // Abstract base class for an item used inside an extern block | |
2357 | class ExternalItem : public Node | |
2358 | { | |
2359 | Analysis::NodeMapping mappings; | |
2360 | AST::AttrVec outer_attrs; | |
2361 | Visibility visibility; | |
2362 | Identifier item_name; | |
d991a3f1 | 2363 | location_t locus; |
8ad1d56d PH |
2364 | |
2365 | public: | |
2366 | enum class ExternKind | |
2367 | { | |
2368 | Static, | |
2369 | Function, | |
26e77295 | 2370 | Type, |
8ad1d56d PH |
2371 | }; |
2372 | ||
2373 | virtual ~ExternalItem () {} | |
2374 | ||
2375 | BaseKind get_hir_kind () override final { return EXTERNAL; } | |
2376 | ||
2377 | virtual ExternKind get_extern_kind () = 0; | |
2378 | ||
2379 | // Returns whether item has outer attributes. | |
2380 | bool has_outer_attrs () const { return !outer_attrs.empty (); } | |
2381 | ||
2382 | // Returns whether item has non-default visibility. | |
2383 | bool has_visibility () const { return !visibility.is_error (); } | |
2384 | ||
2385 | // Unique pointer custom clone function | |
2386 | std::unique_ptr<ExternalItem> clone_external_item () const | |
2387 | { | |
2388 | return std::unique_ptr<ExternalItem> (clone_external_item_impl ()); | |
2389 | } | |
2390 | ||
2391 | virtual std::string as_string () const; | |
2392 | ||
df1da364 | 2393 | location_t get_locus () const { return locus; } |
8ad1d56d PH |
2394 | |
2395 | virtual void accept_vis (HIRFullVisitor &vis) = 0; | |
2396 | virtual void accept_vis (HIRExternalItemVisitor &vis) = 0; | |
2397 | ||
0ee496d5 | 2398 | Visibility &get_visibility () { return visibility; } |
8ad1d56d PH |
2399 | Analysis::NodeMapping get_mappings () const { return mappings; } |
2400 | ||
2401 | Identifier get_item_name () const { return item_name; } | |
2402 | ||
2403 | AST::AttrVec &get_outer_attrs () { return outer_attrs; } | |
2404 | ||
2405 | protected: | |
2406 | ExternalItem (Analysis::NodeMapping mappings, Identifier item_name, | |
417f4bd3 | 2407 | Visibility vis, AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
2408 | |
2409 | // Copy constructor | |
417f4bd3 | 2410 | ExternalItem (ExternalItem const &other); |
8ad1d56d PH |
2411 | |
2412 | // Overloaded assignment operator to clone | |
417f4bd3 | 2413 | ExternalItem &operator= (ExternalItem const &other); |
8ad1d56d PH |
2414 | |
2415 | // move constructors | |
2416 | ExternalItem (ExternalItem &&other) = default; | |
2417 | ExternalItem &operator= (ExternalItem &&other) = default; | |
2418 | ||
2419 | // Clone function implementation as pure virtual method | |
2420 | virtual ExternalItem *clone_external_item_impl () const = 0; | |
2421 | }; | |
2422 | ||
2423 | // A static item used in an extern block | |
2424 | class ExternalStaticItem : public ExternalItem | |
2425 | { | |
2426 | Mutability mut; | |
2427 | std::unique_ptr<Type> item_type; | |
2428 | ||
2429 | public: | |
2430 | ExternalStaticItem (Analysis::NodeMapping mappings, Identifier item_name, | |
2431 | std::unique_ptr<Type> item_type, Mutability mut, | |
d991a3f1 | 2432 | Visibility vis, AST::AttrVec outer_attrs, |
417f4bd3 | 2433 | location_t locus); |
8ad1d56d PH |
2434 | |
2435 | // Copy constructor | |
417f4bd3 | 2436 | ExternalStaticItem (ExternalStaticItem const &other); |
8ad1d56d PH |
2437 | |
2438 | // Overloaded assignment operator to clone | |
417f4bd3 | 2439 | ExternalStaticItem &operator= (ExternalStaticItem const &other); |
8ad1d56d PH |
2440 | |
2441 | // move constructors | |
2442 | ExternalStaticItem (ExternalStaticItem &&other) = default; | |
2443 | ExternalStaticItem &operator= (ExternalStaticItem &&other) = default; | |
2444 | ||
2445 | std::string as_string () const override; | |
2446 | ||
2447 | void accept_vis (HIRFullVisitor &vis) override; | |
2448 | void accept_vis (HIRExternalItemVisitor &vis) override; | |
2449 | ||
2450 | bool is_mut () const { return mut == Mutability::Mut; } | |
2451 | ||
2452 | Mutability get_mut () { return mut; } | |
2453 | ||
c29a3bc9 | 2454 | Type &get_item_type () { return *item_type; } |
8ad1d56d PH |
2455 | |
2456 | ExternKind get_extern_kind () override { return ExternKind::Static; } | |
2457 | ||
2458 | protected: | |
2459 | /* Use covariance to implement clone function as returning this object | |
2460 | * rather than base */ | |
2461 | ExternalStaticItem *clone_external_item_impl () const override | |
2462 | { | |
2463 | return new ExternalStaticItem (*this); | |
2464 | } | |
2465 | }; | |
2466 | ||
2467 | // A named function parameter used in external functions | |
2468 | struct NamedFunctionParam | |
2469 | { | |
2470 | private: | |
2471 | Identifier name; | |
2472 | std::unique_ptr<Type> param_type; | |
2473 | Analysis::NodeMapping mappings; | |
2474 | ||
2475 | public: | |
fcb228d1 | 2476 | bool has_name () const { return name.as_string () != "_"; } |
8ad1d56d PH |
2477 | |
2478 | NamedFunctionParam (Analysis::NodeMapping mappings, Identifier name, | |
417f4bd3 | 2479 | std::unique_ptr<Type> param_type); |
8ad1d56d PH |
2480 | |
2481 | // Copy constructor | |
417f4bd3 | 2482 | NamedFunctionParam (NamedFunctionParam const &other); |
8ad1d56d PH |
2483 | |
2484 | ~NamedFunctionParam () = default; | |
2485 | ||
2486 | // Overloaded assignment operator to clone | |
417f4bd3 | 2487 | NamedFunctionParam &operator= (NamedFunctionParam const &other); |
8ad1d56d PH |
2488 | |
2489 | // move constructors | |
2490 | NamedFunctionParam (NamedFunctionParam &&other) = default; | |
2491 | NamedFunctionParam &operator= (NamedFunctionParam &&other) = default; | |
2492 | ||
2493 | std::string as_string () const; | |
2494 | ||
2495 | Identifier get_param_name () const { return name; } | |
2496 | ||
6246f658 PEP |
2497 | Type &get_type () |
2498 | { | |
2499 | rust_assert (param_type); | |
2500 | return *param_type; | |
2501 | } | |
8ad1d56d PH |
2502 | |
2503 | Analysis::NodeMapping get_mappings () const { return mappings; } | |
2504 | }; | |
2505 | ||
2506 | // A function item used in an extern block | |
2507 | class ExternalFunctionItem : public ExternalItem | |
2508 | { | |
2509 | // bool has_generics; | |
2510 | // Generics generic_params; | |
2511 | std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined | |
2512 | ||
2513 | // bool has_return_type; | |
2514 | // FunctionReturnType return_type; | |
2515 | std::unique_ptr<Type> return_type; // inlined | |
2516 | ||
2517 | // bool has_where_clause; | |
2518 | WhereClause where_clause; | |
2519 | ||
2520 | std::vector<NamedFunctionParam> function_params; | |
2521 | bool has_variadics; | |
2522 | ||
2523 | public: | |
2524 | // Returns whether item has generic parameters. | |
2525 | bool has_generics () const { return !generic_params.empty (); } | |
2526 | ||
2527 | // Returns whether item has a return type (otherwise void). | |
2528 | bool has_return_type () const { return return_type != nullptr; } | |
2529 | ||
2530 | // Returns whether item has a where clause. | |
2531 | bool has_where_clause () const { return !where_clause.is_empty (); } | |
2532 | ||
459b61c7 JD |
2533 | WARN_UNUSED_RESULT const WhereClause &get_where_clause () const |
2534 | { | |
2535 | return where_clause; | |
2536 | } | |
2537 | ||
8ad1d56d PH |
2538 | ExternalFunctionItem ( |
2539 | Analysis::NodeMapping mappings, Identifier item_name, | |
2540 | std::vector<std::unique_ptr<GenericParam>> generic_params, | |
2541 | std::unique_ptr<Type> return_type, WhereClause where_clause, | |
2542 | std::vector<NamedFunctionParam> function_params, bool has_variadics, | |
417f4bd3 | 2543 | Visibility vis, AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
2544 | |
2545 | // Copy constructor with clone | |
417f4bd3 | 2546 | ExternalFunctionItem (ExternalFunctionItem const &other); |
8ad1d56d PH |
2547 | |
2548 | // Overloaded assignment operator with clone | |
417f4bd3 | 2549 | ExternalFunctionItem &operator= (ExternalFunctionItem const &other); |
8ad1d56d PH |
2550 | |
2551 | // move constructors | |
2552 | ExternalFunctionItem (ExternalFunctionItem &&other) = default; | |
2553 | ExternalFunctionItem &operator= (ExternalFunctionItem &&other) = default; | |
2554 | ||
2555 | std::string as_string () const override; | |
2556 | ||
2557 | void accept_vis (HIRFullVisitor &vis) override; | |
2558 | void accept_vis (HIRExternalItemVisitor &vis) override; | |
2559 | ||
2560 | std::vector<std::unique_ptr<GenericParam>> &get_generic_params () | |
2561 | { | |
2562 | return generic_params; | |
2563 | } | |
2564 | ||
c29a3bc9 | 2565 | Type &get_return_type () { return *return_type; } |
8ad1d56d PH |
2566 | |
2567 | std::vector<NamedFunctionParam> &get_function_params () | |
2568 | { | |
2569 | return function_params; | |
2570 | } | |
2571 | ||
2572 | bool is_variadic () const { return has_variadics; } | |
2573 | ||
2574 | ExternKind get_extern_kind () override { return ExternKind::Function; } | |
2575 | ||
2576 | protected: | |
2577 | /* Use covariance to implement clone function as returning this object | |
2578 | * rather than base */ | |
2579 | ExternalFunctionItem *clone_external_item_impl () const override | |
2580 | { | |
2581 | return new ExternalFunctionItem (*this); | |
2582 | } | |
2583 | }; | |
2584 | ||
26e77295 AC |
2585 | class ExternalTypeItem : public ExternalItem |
2586 | { | |
07fc7850 | 2587 | public: |
26e77295 | 2588 | ExternalTypeItem (Analysis::NodeMapping mappings, Identifier item_name, |
417f4bd3 | 2589 | Visibility vis, location_t locus); |
26e77295 | 2590 | |
417f4bd3 | 2591 | ExternalTypeItem (ExternalTypeItem const &other); |
26e77295 AC |
2592 | |
2593 | ExternalTypeItem (ExternalTypeItem &&other) = default; | |
2594 | ExternalTypeItem &operator= (ExternalTypeItem &&other) = default; | |
07fc7850 | 2595 | ExternalTypeItem &operator= (ExternalTypeItem const &other) = default; |
26e77295 AC |
2596 | |
2597 | std::string as_string () const override; | |
2598 | ||
2599 | void accept_vis (HIRFullVisitor &vis) override; | |
2600 | void accept_vis (HIRExternalItemVisitor &vis) override; | |
2601 | ||
2602 | ExternKind get_extern_kind () override { return ExternKind::Type; } | |
2603 | ||
2604 | protected: | |
07fc7850 AC |
2605 | /* Use covariance to implement clone function as returning this object |
2606 | * rather than base */ | |
26e77295 AC |
2607 | ExternalTypeItem *clone_external_item_impl () const override |
2608 | { | |
2609 | return new ExternalTypeItem (*this); | |
2610 | } | |
2611 | }; | |
2612 | ||
8ad1d56d | 2613 | // An extern block HIR node |
16510f1c | 2614 | class ExternBlock : public VisItem, public WithInnerAttrs |
8ad1d56d PH |
2615 | { |
2616 | ABI abi; | |
8ad1d56d | 2617 | std::vector<std::unique_ptr<ExternalItem>> extern_items; |
d991a3f1 | 2618 | location_t locus; |
8ad1d56d PH |
2619 | |
2620 | public: | |
2621 | std::string as_string () const override; | |
2622 | ||
8ad1d56d PH |
2623 | // Returns whether extern block has extern items. |
2624 | bool has_extern_items () const { return !extern_items.empty (); } | |
2625 | ||
2626 | ABI get_abi () const { return abi; } | |
2627 | ||
2628 | ExternBlock (Analysis::NodeMapping mappings, ABI abi, | |
2629 | std::vector<std::unique_ptr<ExternalItem>> extern_items, | |
2630 | Visibility vis, AST::AttrVec inner_attrs, | |
417f4bd3 | 2631 | AST::AttrVec outer_attrs, location_t locus); |
8ad1d56d PH |
2632 | |
2633 | // Copy constructor with vector clone | |
417f4bd3 | 2634 | ExternBlock (ExternBlock const &other); |
8ad1d56d PH |
2635 | |
2636 | // Overloaded assignment operator with vector clone | |
417f4bd3 | 2637 | ExternBlock &operator= (ExternBlock const &other); |
8ad1d56d PH |
2638 | |
2639 | // move constructors | |
2640 | ExternBlock (ExternBlock &&other) = default; | |
2641 | ExternBlock &operator= (ExternBlock &&other) = default; | |
2642 | ||
df1da364 | 2643 | location_t get_locus () const override final { return locus; } |
8ad1d56d PH |
2644 | |
2645 | void accept_vis (HIRFullVisitor &vis) override; | |
2646 | void accept_vis (HIRStmtVisitor &vis) override; | |
2647 | void accept_vis (HIRVisItemVisitor &vis) override; | |
2648 | ||
2649 | std::vector<std::unique_ptr<ExternalItem>> &get_extern_items () | |
2650 | { | |
2651 | return extern_items; | |
2652 | } | |
2653 | ||
2654 | ItemKind get_item_kind () const override { return ItemKind::ExternBlock; } | |
2655 | ||
2656 | protected: | |
2657 | /* Use covariance to implement clone function as returning this object | |
2658 | * rather than base */ | |
2659 | ExternBlock *clone_item_impl () const override | |
2660 | { | |
2661 | return new ExternBlock (*this); | |
2662 | } | |
2663 | ||
2664 | /* Use covariance to implement clone function as returning this object | |
2665 | * rather than base */ | |
2666 | /*virtual ExternBlock* clone_statement_impl() const override { | |
2667 | return new ExternBlock(*this); | |
2668 | }*/ | |
2669 | }; | |
2670 | ||
2671 | } // namespace HIR | |
2672 | } // namespace Rust | |
2673 | ||
2674 | #endif |