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