1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #ifndef RUST_HIR_ITEM_H
20 #define RUST_HIR_ITEM_H
23 #include "rust-ast-full-decls.h"
24 #include "rust-common.h"
26 #include "rust-hir-path.h"
34 // A type generic parameter (as opposed to a lifetime generic parameter)
35 class TypeParam
: public GenericParam
37 // bool has_outer_attribute;
38 // std::unique_ptr<Attribute> outer_attr;
39 AST::Attribute outer_attr
;
41 Identifier type_representation
;
43 // bool has_type_param_bounds;
44 // TypeParamBounds type_param_bounds;
45 std::vector
<std::unique_ptr
<TypeParamBound
>>
46 type_param_bounds
; // inlined form
49 std::unique_ptr
<Type
> type
;
54 // Returns whether the type of the type param has been specified.
55 bool has_type () const { return type
!= nullptr; }
57 // Returns whether the type param has type param bounds.
58 bool has_type_param_bounds () const { return !type_param_bounds
.empty (); }
60 // Returns whether the type param has an outer attribute.
61 bool has_outer_attribute () const { return !outer_attr
.is_empty (); }
63 TypeParam (Analysis::NodeMapping mappings
, Identifier type_representation
,
64 Location locus
= Location (),
65 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
66 = std::vector
<std::unique_ptr
<TypeParamBound
>> (),
67 std::unique_ptr
<Type
> type
= nullptr,
68 AST::Attribute outer_attr
= AST::Attribute::create_empty ())
69 : GenericParam (mappings
), outer_attr (std::move (outer_attr
)),
70 type_representation (std::move (type_representation
)),
71 type_param_bounds (std::move (type_param_bounds
)),
72 type (std::move (type
)), locus (locus
)
75 // Copy constructor uses clone
76 TypeParam (TypeParam
const &other
)
77 : GenericParam (other
.mappings
), outer_attr (other
.outer_attr
),
78 type_representation (other
.type_representation
), locus (other
.locus
)
80 // guard to prevent null pointer dereference
81 if (other
.type
!= nullptr)
82 type
= other
.type
->clone_type ();
84 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
85 for (const auto &e
: other
.type_param_bounds
)
86 type_param_bounds
.push_back (e
->clone_type_param_bound ());
89 // Overloaded assignment operator to clone
90 TypeParam
&operator= (TypeParam
const &other
)
92 type_representation
= other
.type_representation
;
93 outer_attr
= other
.outer_attr
;
95 mappings
= other
.mappings
;
97 // guard to prevent null pointer dereference
98 if (other
.type
!= nullptr)
99 type
= other
.type
->clone_type ();
103 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
104 for (const auto &e
: other
.type_param_bounds
)
105 type_param_bounds
.push_back (e
->clone_type_param_bound ());
110 TypeParam (TypeParam
&&other
) = default;
111 TypeParam
&operator= (TypeParam
&&other
) = default;
113 std::string
as_string () const override
;
115 Location
get_locus () const override final
{ return locus
; }
117 void accept_vis (HIRFullVisitor
&vis
) override
;
119 Identifier
get_type_representation () const { return type_representation
; }
121 std::unique_ptr
<Type
> &get_type ()
123 rust_assert (type
!= nullptr);
127 Analysis::NodeMapping
get_type_mappings () const
129 rust_assert (type
!= nullptr);
130 return type
->get_mappings ();
133 std::vector
<std::unique_ptr
<TypeParamBound
>> &get_type_param_bounds ()
135 return type_param_bounds
;
139 // Clone function implementation as (not pure) virtual method
140 TypeParam
*clone_generic_param_impl () const override
142 return new TypeParam (*this);
146 /* "where" clause item base. Abstract - use LifetimeWhereClauseItem,
147 * TypeBoundWhereClauseItem */
148 class WhereClauseItem
157 virtual ~WhereClauseItem () {}
159 // Unique pointer custom clone function
160 std::unique_ptr
<WhereClauseItem
> clone_where_clause_item () const
162 return std::unique_ptr
<WhereClauseItem
> (clone_where_clause_item_impl ());
165 virtual std::string
as_string () const = 0;
167 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
169 virtual Analysis::NodeMapping
get_mappings () const = 0;
171 virtual ItemType
get_item_type () const = 0;
174 // Clone function implementation as pure virtual method
175 virtual WhereClauseItem
*clone_where_clause_item_impl () const = 0;
178 // A lifetime where clause item
179 class LifetimeWhereClauseItem
: public WhereClauseItem
182 std::vector
<Lifetime
> lifetime_bounds
;
184 Analysis::NodeMapping mappings
;
187 LifetimeWhereClauseItem (Analysis::NodeMapping mappings
, Lifetime lifetime
,
188 std::vector
<Lifetime
> lifetime_bounds
,
190 : lifetime (std::move (lifetime
)),
191 lifetime_bounds (std::move (lifetime_bounds
)), locus (locus
),
192 mappings (std::move (mappings
))
195 std::string
as_string () const override
;
197 void accept_vis (HIRFullVisitor
&vis
) override
;
199 Lifetime
&get_lifetime () { return lifetime
; }
201 std::vector
<Lifetime
> &get_lifetime_bounds () { return lifetime_bounds
; }
203 Analysis::NodeMapping
get_mappings () const override final
208 ItemType
get_item_type () const override final
210 return WhereClauseItem::ItemType::LIFETIME
;
214 // Clone function implementation as (not pure) virtual method
215 LifetimeWhereClauseItem
*clone_where_clause_item_impl () const override
217 return new LifetimeWhereClauseItem (*this);
221 // A type bound where clause item
222 class TypeBoundWhereClauseItem
: public WhereClauseItem
224 std::vector
<LifetimeParam
> for_lifetimes
;
225 std::unique_ptr
<Type
> bound_type
;
226 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
;
227 Analysis::NodeMapping mappings
;
231 // Returns whether the item has ForLifetimes
232 bool has_for_lifetimes () const { return !for_lifetimes
.empty (); }
234 // Returns whether the item has type param bounds
235 bool has_type_param_bounds () const { return !type_param_bounds
.empty (); }
237 TypeBoundWhereClauseItem (
238 Analysis::NodeMapping mappings
, std::vector
<LifetimeParam
> for_lifetimes
,
239 std::unique_ptr
<Type
> bound_type
,
240 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
,
242 : for_lifetimes (std::move (for_lifetimes
)),
243 bound_type (std::move (bound_type
)),
244 type_param_bounds (std::move (type_param_bounds
)),
245 mappings (std::move (mappings
)), locus (locus
)
248 // Copy constructor requires clone
249 TypeBoundWhereClauseItem (TypeBoundWhereClauseItem
const &other
)
250 : for_lifetimes (other
.for_lifetimes
),
251 bound_type (other
.bound_type
->clone_type ()), mappings (other
.mappings
)
253 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
254 for (const auto &e
: other
.type_param_bounds
)
255 type_param_bounds
.push_back (e
->clone_type_param_bound ());
258 // Overload assignment operator to clone
259 TypeBoundWhereClauseItem
&operator= (TypeBoundWhereClauseItem
const &other
)
261 mappings
= other
.mappings
;
262 for_lifetimes
= other
.for_lifetimes
;
263 bound_type
= other
.bound_type
->clone_type ();
264 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
265 for (const auto &e
: other
.type_param_bounds
)
266 type_param_bounds
.push_back (e
->clone_type_param_bound ());
272 TypeBoundWhereClauseItem (TypeBoundWhereClauseItem
&&other
) = default;
273 TypeBoundWhereClauseItem
&operator= (TypeBoundWhereClauseItem
&&other
)
276 std::string
as_string () const override
;
278 void accept_vis (HIRFullVisitor
&vis
) override
;
280 std::vector
<LifetimeParam
> &get_for_lifetimes () { return for_lifetimes
; }
282 std::unique_ptr
<Type
> &get_bound_type () { return bound_type
; }
284 std::vector
<std::unique_ptr
<TypeParamBound
>> &get_type_param_bounds ()
286 return type_param_bounds
;
289 Analysis::NodeMapping
get_mappings () const override final
294 ItemType
get_item_type () const override final
296 return WhereClauseItem::ItemType::TYPE_BOUND
;
300 // Clone function implementation as (not pure) virtual method
301 TypeBoundWhereClauseItem
*clone_where_clause_item_impl () const override
303 return new TypeBoundWhereClauseItem (*this);
311 std::vector
<std::unique_ptr
<WhereClauseItem
>> where_clause_items
;
313 // should this store location info?
316 WhereClause (std::vector
<std::unique_ptr
<WhereClauseItem
>> where_clause_items
)
317 : where_clause_items (std::move (where_clause_items
))
320 // copy constructor with vector clone
321 WhereClause (WhereClause
const &other
)
323 where_clause_items
.reserve (other
.where_clause_items
.size ());
324 for (const auto &e
: other
.where_clause_items
)
325 where_clause_items
.push_back (e
->clone_where_clause_item ());
328 // overloaded assignment operator with vector clone
329 WhereClause
&operator= (WhereClause
const &other
)
331 where_clause_items
.reserve (other
.where_clause_items
.size ());
332 for (const auto &e
: other
.where_clause_items
)
333 where_clause_items
.push_back (e
->clone_where_clause_item ());
339 WhereClause (WhereClause
&&other
) = default;
340 WhereClause
&operator= (WhereClause
&&other
) = default;
342 // Creates a WhereClause with no items.
343 static WhereClause
create_empty ()
345 return WhereClause (std::vector
<std::unique_ptr
<WhereClauseItem
>> ());
348 // Returns whether the WhereClause has no items.
349 bool is_empty () const { return where_clause_items
.empty (); }
351 std::string
as_string () const;
353 std::vector
<std::unique_ptr
<WhereClauseItem
>> &get_items ()
355 return where_clause_items
;
357 const std::vector
<std::unique_ptr
<WhereClauseItem
>> &get_items () const
359 return where_clause_items
;
363 // A self parameter in a method
367 enum ImplicitSelfKind
372 MUT_REF
, // &mut self
377 ImplicitSelfKind self_kind
;
379 std::unique_ptr
<Type
> type
;
381 Analysis::NodeMapping mappings
;
383 SelfParam (Analysis::NodeMapping mappings
, ImplicitSelfKind self_kind
,
384 Lifetime lifetime
, Type
*type
)
385 : self_kind (self_kind
), lifetime (std::move (lifetime
)), type (type
),
390 // Type-based self parameter (not ref, no lifetime)
391 SelfParam (Analysis::NodeMapping mappings
, std::unique_ptr
<Type
> type
,
392 bool is_mut
, Location locus
)
393 : self_kind (is_mut
? ImplicitSelfKind::MUT
: ImplicitSelfKind::IMM
),
395 Lifetime (mappings
, AST::Lifetime::LifetimeType::NAMED
, "", locus
)),
396 type (std::move (type
)), locus (locus
), mappings (mappings
)
399 // Lifetime-based self parameter (is ref, no type)
400 SelfParam (Analysis::NodeMapping mappings
, Lifetime lifetime
, bool is_mut
,
402 : self_kind (is_mut
? ImplicitSelfKind::MUT_REF
403 : ImplicitSelfKind::IMM_REF
),
404 lifetime (std::move (lifetime
)), locus (locus
), mappings (mappings
)
407 // Copy constructor requires clone
408 SelfParam (SelfParam
const &other
)
409 : self_kind (other
.self_kind
), lifetime (other
.lifetime
),
410 locus (other
.locus
), mappings (other
.mappings
)
412 if (other
.type
!= nullptr)
413 type
= other
.type
->clone_type ();
416 // Overload assignment operator to use clone
417 SelfParam
&operator= (SelfParam
const &other
)
419 if (other
.type
!= nullptr)
420 type
= other
.type
->clone_type ();
422 self_kind
= other
.self_kind
;
423 lifetime
= other
.lifetime
;
425 mappings
= other
.mappings
;
431 SelfParam (SelfParam
&&other
) = default;
432 SelfParam
&operator= (SelfParam
&&other
) = default;
434 static SelfParam
error ()
436 return SelfParam (Analysis::NodeMapping::get_error (),
437 ImplicitSelfKind::NONE
, Lifetime::error (), nullptr);
440 // Returns whether the self-param has a type field.
441 bool has_type () const { return type
!= nullptr; }
443 // Returns whether the self-param has a valid lifetime.
444 bool has_lifetime () const { return !lifetime
.is_error (); }
446 // Returns whether the self-param is in an error state.
447 bool is_error () const { return self_kind
== ImplicitSelfKind::NONE
; }
449 std::string
as_string () const;
451 Location
get_locus () const { return locus
; }
453 ImplicitSelfKind
get_self_kind () const { return self_kind
; }
455 std::unique_ptr
<Type
> &get_type ()
457 rust_assert (has_type ());
461 Analysis::NodeMapping
get_mappings () { return mappings
; }
463 Mutability
get_mut () const
465 return (self_kind
== ImplicitSelfKind::MUT
466 || self_kind
== ImplicitSelfKind::MUT_REF
)
473 return self_kind
== ImplicitSelfKind::MUT
474 || self_kind
== ImplicitSelfKind::MUT_REF
;
479 return self_kind
== ImplicitSelfKind::IMM_REF
480 || self_kind
== ImplicitSelfKind::MUT_REF
;
484 // Qualifiers for function, i.e. const, unsafe, extern etc.
485 struct FunctionQualifiers
488 AsyncConstStatus const_status
;
494 FunctionQualifiers (AsyncConstStatus const_status
, Unsafety unsafety
,
495 bool has_extern
, ABI abi
)
496 : const_status (const_status
), unsafety (unsafety
), has_extern (has_extern
),
500 std::string
as_string () const;
502 AsyncConstStatus
get_status () const { return const_status
; }
504 bool is_const () const { return const_status
== AsyncConstStatus::CONST_FN
; }
505 bool is_unsafe () const { return unsafety
== Unsafety::Unsafe
; }
507 ABI
get_abi () const { return abi
; }
510 // A function parameter
513 std::unique_ptr
<Pattern
> param_name
;
514 std::unique_ptr
<Type
> type
;
516 Analysis::NodeMapping mappings
;
519 FunctionParam (Analysis::NodeMapping mappings
,
520 std::unique_ptr
<Pattern
> param_name
,
521 std::unique_ptr
<Type
> param_type
, Location locus
)
522 : param_name (std::move (param_name
)), type (std::move (param_type
)),
523 locus (locus
), mappings (mappings
)
526 // Copy constructor uses clone
527 FunctionParam (FunctionParam
const &other
)
528 : param_name (other
.param_name
->clone_pattern ()),
529 type (other
.type
->clone_type ()), locus (other
.locus
),
530 mappings (other
.mappings
)
533 // Overload assignment operator to use clone
534 FunctionParam
&operator= (FunctionParam
const &other
)
536 param_name
= other
.param_name
->clone_pattern ();
537 type
= other
.type
->clone_type ();
539 mappings
= other
.mappings
;
545 FunctionParam (FunctionParam
&&other
) = default;
546 FunctionParam
&operator= (FunctionParam
&&other
) = default;
548 std::string
as_string () const;
550 Location
get_locus () const { return locus
; }
552 Pattern
*get_param_name () { return param_name
.get (); }
554 Type
*get_type () { return type
.get (); }
556 const Analysis::NodeMapping
&get_mappings () const { return mappings
; }
559 // Visibility of an item
573 HIR::SimplePath path
;
575 // should this store location info?
578 Visibility (VisType vis_type
,
579 HIR::SimplePath path
= HIR::SimplePath::create_empty ())
580 : vis_type (vis_type
), path (std::move (path
))
583 // Returns whether visibility is in an error state.
584 bool is_error () const { return vis_type
== ERROR
; }
586 // Does the current visibility refer to a simple `pub <item>` entirely public
587 bool is_public () const { return vis_type
== PUBLIC
; }
589 // Is the current visibility public restricted to a certain path
590 bool is_restricted () const { return vis_type
== RESTRICTED
; }
592 // Creates an error visibility.
593 static Visibility
create_error ()
595 return Visibility (ERROR
, HIR::SimplePath::create_empty ());
598 VisType
get_vis_type () const { return vis_type
; }
600 const HIR::SimplePath
&get_path () const
602 rust_assert (!is_error ());
606 std::string
as_string () const;
609 // Item that supports visibility - abstract base class
610 class VisItem
: public Item
612 Visibility visibility
;
615 // Visibility constructor
616 VisItem (Analysis::NodeMapping mappings
, Visibility visibility
,
617 AST::AttrVec outer_attrs
= AST::AttrVec ())
618 : Item (std::move (mappings
), std::move (outer_attrs
)),
619 visibility (std::move (visibility
))
622 // Visibility copy constructor
623 VisItem (VisItem
const &other
) : Item (other
), visibility (other
.visibility
)
626 // Overload assignment operator to clone
627 VisItem
&operator= (VisItem
const &other
)
629 Item::operator= (other
);
630 visibility
= other
.visibility
;
631 // outer_attrs = other.outer_attrs;
637 VisItem (VisItem
&&other
) = default;
638 VisItem
&operator= (VisItem
&&other
) = default;
641 using HIR::Stmt::accept_vis
;
643 BaseKind
get_hir_kind () override final
{ return VIS_ITEM
; }
645 /* Does the item have some kind of public visibility (non-default
647 bool has_visibility () const { return !visibility
.is_error (); }
649 virtual void accept_vis (HIRVisItemVisitor
&vis
) = 0;
651 Visibility
&get_visibility () { return visibility
; }
652 const Visibility
&get_visibility () const { return visibility
; }
654 std::string
as_string () const override
;
657 // Rust module item - abstract base class
658 class Module
: public VisItem
660 Identifier module_name
;
662 // bool has_inner_attrs;
663 AST::AttrVec inner_attrs
;
665 std::vector
<std::unique_ptr
<Item
>> items
;
668 std::string
as_string () const override
;
670 // Returns whether the module has items in its body.
671 bool has_items () const { return !items
.empty (); }
673 // Returns whether the module has any inner attributes.
674 bool has_inner_attrs () const { return !inner_attrs
.empty (); }
677 Module (Analysis::NodeMapping mappings
, Identifier module_name
,
678 Location locus
, std::vector
<std::unique_ptr
<Item
>> items
,
679 Visibility visibility
= Visibility::create_error (),
680 AST::AttrVec inner_attrs
= AST::AttrVec (),
681 AST::AttrVec outer_attrs
= AST::AttrVec ())
682 : VisItem (std::move (mappings
), std::move (visibility
),
683 std::move (outer_attrs
)),
684 module_name (module_name
), locus (locus
),
685 inner_attrs (std::move (inner_attrs
)), items (std::move (items
))
688 // Copy constructor with vector clone
689 Module (Module
const &other
)
690 : VisItem (other
), inner_attrs (other
.inner_attrs
)
692 items
.reserve (other
.items
.size ());
693 for (const auto &e
: other
.items
)
694 items
.push_back (e
->clone_item ());
697 // Overloaded assignment operator with vector clone
698 Module
&operator= (Module
const &other
)
700 VisItem::operator= (other
);
701 inner_attrs
= other
.inner_attrs
;
703 items
.reserve (other
.items
.size ());
704 for (const auto &e
: other
.items
)
705 items
.push_back (e
->clone_item ());
711 Module (Module
&&other
) = default;
712 Module
&operator= (Module
&&other
) = default;
714 void accept_vis (HIRFullVisitor
&vis
) override
;
715 void accept_vis (HIRStmtVisitor
&vis
) override
;
716 void accept_vis (HIRVisItemVisitor
&vis
) override
;
718 std::vector
<std::unique_ptr
<Item
>> &get_items () { return items
; };
720 /* Override that runs the function recursively on all items contained within
722 void add_crate_name (std::vector
<std::string
> &names
) const override
;
724 Location
get_locus () const override final
{ return locus
; }
726 ItemKind
get_item_kind () const override
{ return ItemKind::Module
; }
729 /* Use covariance to implement clone function as returning this object
730 * rather than base */
731 Module
*clone_item_impl () const override
{ return new Module (*this); }
733 /* Use covariance to implement clone function as returning this object
734 * rather than base */
735 /*virtual Module* clone_statement_impl() const override {
736 return new Module(*this);
740 // Rust extern crate declaration HIR node
741 class ExternCrate
: public VisItem
743 // this is either an identifier or "self", with self parsed to string
744 std::string referenced_crate
;
745 // bool has_as_clause;
746 // AsClause as_clause;
747 // this is either an identifier or "_", with _ parsed to string
748 std::string as_clause_name
;
753 "extern crate foo as _"
755 "extern crate std as cool_std" */
757 std::string
as_string () const override
;
759 // Returns whether extern crate declaration has an as clause.
760 bool has_as_clause () const { return !as_clause_name
.empty (); }
762 /* Returns whether extern crate declaration references the current crate
764 bool references_self () const { return referenced_crate
== "self"; }
767 ExternCrate (Analysis::NodeMapping mappings
, std::string referenced_crate
,
768 Visibility visibility
, AST::AttrVec outer_attrs
, Location locus
,
769 std::string as_clause_name
= std::string ())
770 : VisItem (std::move (mappings
), std::move (visibility
),
771 std::move (outer_attrs
)),
772 referenced_crate (std::move (referenced_crate
)),
773 as_clause_name (std::move (as_clause_name
)), locus (locus
)
776 Location
get_locus () const override final
{ return locus
; }
778 ItemKind
get_item_kind () const override
{ return ItemKind::ExternCrate
; }
780 void accept_vis (HIRFullVisitor
&vis
) override
;
781 void accept_vis (HIRStmtVisitor
&vis
) override
;
782 void accept_vis (HIRVisItemVisitor
&vis
) override
;
784 // Override that adds extern crate name in decl to passed list of names.
785 void add_crate_name (std::vector
<std::string
> &names
) const override
787 names
.push_back (referenced_crate
);
791 /* Use covariance to implement clone function as returning this object
792 * rather than base */
793 ExternCrate
*clone_item_impl () const override
795 return new ExternCrate (*this);
798 /* Use covariance to implement clone function as returning this object
799 * rather than base */
800 /*virtual ExternCrate* clone_statement_impl() const override {
801 return new ExternCrate(*this);
805 // The path-ish thing referred to in a use declaration - abstract base class
811 virtual ~UseTree () {}
813 // Unique pointer custom clone function
814 std::unique_ptr
<UseTree
> clone_use_tree () const
816 return std::unique_ptr
<UseTree
> (clone_use_tree_impl ());
819 virtual std::string
as_string () const = 0;
821 Location
get_locus () const { return locus
; }
823 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
826 // Clone function implementation as pure virtual method
827 virtual UseTree
*clone_use_tree_impl () const = 0;
829 UseTree (Location locus
) : locus (locus
) {}
832 // Use tree with a glob (wildcard) operator
833 class UseTreeGlob
: public UseTree
845 AST::SimplePath path
;
848 UseTreeGlob (PathType glob_type
, AST::SimplePath path
, Location locus
)
849 : UseTree (locus
), glob_type (glob_type
), path (std::move (path
))
851 if (this->glob_type
!= PATH_PREFIXED
)
853 // compiler implementation error if there is a path with a
854 // non-path-prefixed use tree glob
855 gcc_assert (!has_path ());
857 // TODO: do path-prefixed paths also have to have a path? If so, have an
858 // assert for that too.
861 /* Returns whether has path. Should be made redundant by PathType
863 bool has_path () const { return !path
.is_empty (); }
865 std::string
as_string () const override
;
867 void accept_vis (HIRFullVisitor
&vis
) override
;
869 /* TODO: find way to ensure only PATH_PREFIXED glob_type has path - factory
872 /* Use covariance to implement clone function as returning this object
873 * rather than base */
874 UseTreeGlob
*clone_use_tree_impl () const override
876 return new UseTreeGlob (*this);
880 // Use tree with a list of paths with a common prefix
881 class UseTreeList
: public UseTree
893 AST::SimplePath path
;
895 std::vector
<std::unique_ptr
<UseTree
>> trees
;
898 UseTreeList (PathType path_type
, AST::SimplePath path
,
899 std::vector
<std::unique_ptr
<UseTree
>> trees
, Location locus
)
900 : UseTree (locus
), path_type (path_type
), path (std::move (path
)),
901 trees (std::move (trees
))
903 if (this->path_type
!= PATH_PREFIXED
)
905 // compiler implementation error if there is a path with a
906 // non-path-prefixed use tree glob
907 gcc_assert (!has_path ());
909 // TODO: do path-prefixed paths also have to have a path? If so, have an
910 // assert for that too.
913 // copy constructor with vector clone
914 UseTreeList (UseTreeList
const &other
)
915 : UseTree (other
), path_type (other
.path_type
), path (other
.path
)
917 trees
.reserve (other
.trees
.size ());
918 for (const auto &e
: other
.trees
)
919 trees
.push_back (e
->clone_use_tree ());
922 // overloaded assignment operator with vector clone
923 UseTreeList
&operator= (UseTreeList
const &other
)
925 UseTree::operator= (other
);
926 path_type
= other
.path_type
;
929 trees
.reserve (other
.trees
.size ());
930 for (const auto &e
: other
.trees
)
931 trees
.push_back (e
->clone_use_tree ());
937 UseTreeList (UseTreeList
&&other
) = default;
938 UseTreeList
&operator= (UseTreeList
&&other
) = default;
940 // Returns whether has path. Should be made redundant by path_type.
941 bool has_path () const { return !path
.is_empty (); }
943 // Returns whether has inner tree elements.
944 bool has_trees () const { return !trees
.empty (); }
946 std::string
as_string () const override
;
948 void accept_vis (HIRFullVisitor
&vis
) override
;
950 // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
953 /* Use covariance to implement clone function as returning this object
954 * rather than base */
955 UseTreeList
*clone_use_tree_impl () const override
957 return new UseTreeList (*this);
961 // Use tree where it rebinds the module name as something else
962 class UseTreeRebind
: public UseTree
973 AST::SimplePath path
;
975 NewBindType bind_type
;
976 Identifier identifier
; // only if NewBindType is IDENTIFIER
979 UseTreeRebind (NewBindType bind_type
, AST::SimplePath path
, Location locus
,
980 Identifier identifier
= std::string ())
981 : UseTree (locus
), path (std::move (path
)), bind_type (bind_type
),
982 identifier (std::move (identifier
))
985 // Returns whether has path (this should always be true).
986 bool has_path () const { return !path
.is_empty (); }
988 // Returns whether has identifier (or, rather, is allowed to).
989 bool has_identifier () const { return bind_type
== IDENTIFIER
; }
991 std::string
as_string () const override
;
993 void accept_vis (HIRFullVisitor
&vis
) override
;
995 // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
998 /* Use covariance to implement clone function as returning this object
999 * rather than base */
1000 virtual UseTreeRebind
*clone_use_tree_impl () const override
1002 return new UseTreeRebind (*this);
1006 // Rust use declaration (i.e. for modules) HIR node
1007 class UseDeclaration
: public VisItem
1009 std::unique_ptr
<UseTree
> use_tree
;
1013 std::string
as_string () const override
;
1015 UseDeclaration (Analysis::NodeMapping mappings
,
1016 std::unique_ptr
<UseTree
> use_tree
, Visibility visibility
,
1017 AST::AttrVec outer_attrs
, Location locus
)
1018 : VisItem (std::move (mappings
), std::move (visibility
),
1019 std::move (outer_attrs
)),
1020 use_tree (std::move (use_tree
)), locus (locus
)
1023 // Copy constructor with clone
1024 UseDeclaration (UseDeclaration
const &other
)
1025 : VisItem (other
), use_tree (other
.use_tree
->clone_use_tree ()),
1029 // Overloaded assignment operator to clone
1030 UseDeclaration
&operator= (UseDeclaration
const &other
)
1032 VisItem::operator= (other
);
1033 use_tree
= other
.use_tree
->clone_use_tree ();
1034 // visibility = other.visibility->clone_visibility();
1035 // outer_attrs = other.outer_attrs;
1036 locus
= other
.locus
;
1041 // move constructors
1042 UseDeclaration (UseDeclaration
&&other
) = default;
1043 UseDeclaration
&operator= (UseDeclaration
&&other
) = default;
1045 Location
get_locus () const override final
{ return locus
; }
1046 ItemKind
get_item_kind () const override
{ return ItemKind::UseDeclaration
; }
1048 void accept_vis (HIRFullVisitor
&vis
) override
;
1049 void accept_vis (HIRStmtVisitor
&vis
) override
;
1050 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1053 /* Use covariance to implement clone function as returning this object
1054 * rather than base */
1055 UseDeclaration
*clone_item_impl () const override
1057 return new UseDeclaration (*this);
1060 /* Use covariance to implement clone function as returning this object
1061 * rather than base */
1062 /*virtual UseDeclaration* clone_statement_impl() const override {
1063 return new UseDeclaration(*this);
1069 // Rust function declaration HIR node
1070 class Function
: public VisItem
, public ImplItem
1072 FunctionQualifiers qualifiers
;
1073 Identifier function_name
;
1074 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
1075 std::vector
<FunctionParam
> function_params
;
1076 std::unique_ptr
<Type
> return_type
;
1077 WhereClause where_clause
;
1078 std::unique_ptr
<BlockExpr
> function_body
;
1083 std::string
as_string () const override
;
1085 // Returns whether function has generic parameters.
1086 bool has_generics () const { return !generic_params
.empty (); }
1088 // Returns whether function has regular parameters.
1089 bool has_function_params () const { return !function_params
.empty (); }
1091 // Returns whether function has return type - if not, it is void.
1092 bool has_function_return_type () const { return return_type
!= nullptr; }
1094 // Returns whether function has a where clause.
1095 bool has_where_clause () const { return !where_clause
.is_empty (); }
1097 ImplItemType
get_impl_item_type () const override final
1099 return ImplItem::ImplItemType::FUNCTION
;
1102 ItemKind
get_item_kind () const override
{ return ItemKind::Function
; }
1104 // Mega-constructor with all possible fields
1105 Function (Analysis::NodeMapping mappings
, Identifier function_name
,
1106 FunctionQualifiers qualifiers
,
1107 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1108 std::vector
<FunctionParam
> function_params
,
1109 std::unique_ptr
<Type
> return_type
, WhereClause where_clause
,
1110 std::unique_ptr
<BlockExpr
> function_body
, Visibility vis
,
1111 AST::AttrVec outer_attrs
, SelfParam self
, Location locus
)
1112 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
1113 qualifiers (std::move (qualifiers
)),
1114 function_name (std::move (function_name
)),
1115 generic_params (std::move (generic_params
)),
1116 function_params (std::move (function_params
)),
1117 return_type (std::move (return_type
)),
1118 where_clause (std::move (where_clause
)),
1119 function_body (std::move (function_body
)), self (std::move (self
)),
1123 // Copy constructor with clone
1124 Function (Function
const &other
)
1125 : VisItem (other
), qualifiers (other
.qualifiers
),
1126 function_name (other
.function_name
),
1127 function_params (other
.function_params
),
1128 where_clause (other
.where_clause
),
1129 function_body (other
.function_body
->clone_block_expr ()),
1130 self (other
.self
), locus (other
.locus
)
1132 // guard to prevent null dereference (always required)
1133 if (other
.return_type
!= nullptr)
1134 return_type
= other
.return_type
->clone_type ();
1136 return_type
= nullptr;
1138 generic_params
.reserve (other
.generic_params
.size ());
1139 for (const auto &e
: other
.generic_params
)
1140 generic_params
.push_back (e
->clone_generic_param ());
1143 // Overloaded assignment operator to clone
1144 Function
&operator= (Function
const &other
)
1146 VisItem::operator= (other
);
1147 function_name
= other
.function_name
;
1148 qualifiers
= other
.qualifiers
;
1149 function_params
= other
.function_params
;
1151 // guard to prevent null dereference (always required)
1152 if (other
.return_type
!= nullptr)
1153 return_type
= other
.return_type
->clone_type ();
1155 return_type
= nullptr;
1157 where_clause
= other
.where_clause
;
1158 function_body
= other
.function_body
->clone_block_expr ();
1159 locus
= other
.locus
;
1162 generic_params
.reserve (other
.generic_params
.size ());
1163 for (const auto &e
: other
.generic_params
)
1164 generic_params
.push_back (e
->clone_generic_param ());
1169 // move constructors
1170 Function (Function
&&other
) = default;
1171 Function
&operator= (Function
&&other
) = default;
1173 Location
get_locus () const override final
{ return locus
; }
1175 void accept_vis (HIRFullVisitor
&vis
) override
;
1176 void accept_vis (HIRImplVisitor
&vis
) override
;
1177 void accept_vis (HIRStmtVisitor
&vis
) override
;
1178 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1180 Analysis::NodeMapping
get_impl_mappings () const override
1182 return get_mappings ();
1185 std::vector
<FunctionParam
> &get_function_params () { return function_params
; }
1186 const std::vector
<FunctionParam
> &get_function_params () const
1188 return function_params
;
1191 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1193 return generic_params
;
1195 const std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params () const
1197 return generic_params
;
1200 // TODO: is this better? Or is a "vis_block" better?
1201 std::unique_ptr
<BlockExpr
> &get_definition ()
1203 rust_assert (function_body
!= nullptr);
1204 return function_body
;
1207 const FunctionQualifiers
&get_qualifiers () const { return qualifiers
; }
1209 Identifier
get_function_name () const { return function_name
; }
1211 // TODO: is this better? Or is a "vis_block" better?
1212 WhereClause
&get_where_clause () { return where_clause
; }
1214 bool has_return_type () const { return return_type
!= nullptr; }
1216 // TODO: is this better? Or is a "vis_block" better?
1217 std::unique_ptr
<Type
> &get_return_type ()
1219 rust_assert (has_return_type ());
1223 bool is_method () const { return !self
.is_error (); }
1225 SelfParam
&get_self_param () { return self
; }
1228 /* Use covariance to implement clone function as returning this object
1229 * rather than base */
1230 Function
*clone_item_impl () const override
{ return new Function (*this); }
1232 /* Use covariance to implement clone function as returning this object
1233 * rather than base */
1234 Function
*clone_inherent_impl_item_impl () const override
1236 return new Function (*this);
1240 // Rust type alias (i.e. typedef) HIR node
1241 class TypeAlias
: public VisItem
, public ImplItem
1243 Identifier new_type_name
;
1245 // bool has_generics;
1246 // Generics generic_params;
1247 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1249 // bool has_where_clause;
1250 WhereClause where_clause
;
1252 std::unique_ptr
<Type
> existing_type
;
1257 std::string
as_string () const override
;
1259 // Returns whether type alias has generic parameters.
1260 bool has_generics () const { return !generic_params
.empty (); }
1262 // Returns whether type alias has a where clause.
1263 bool has_where_clause () const { return !where_clause
.is_empty (); }
1265 ImplItemType
get_impl_item_type () const override final
1267 return ImplItem::ImplItemType::TYPE_ALIAS
;
1270 // Mega-constructor with all possible fields
1271 TypeAlias (Analysis::NodeMapping mappings
, Identifier new_type_name
,
1272 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1273 WhereClause where_clause
, std::unique_ptr
<Type
> existing_type
,
1274 Visibility vis
, AST::AttrVec outer_attrs
, Location locus
)
1275 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
1276 new_type_name (std::move (new_type_name
)),
1277 generic_params (std::move (generic_params
)),
1278 where_clause (std::move (where_clause
)),
1279 existing_type (std::move (existing_type
)), locus (locus
)
1283 TypeAlias (TypeAlias
const &other
)
1284 : VisItem (other
), new_type_name (other
.new_type_name
),
1285 where_clause (other
.where_clause
),
1286 existing_type (other
.existing_type
->clone_type ()), locus (other
.locus
)
1288 generic_params
.reserve (other
.generic_params
.size ());
1289 for (const auto &e
: other
.generic_params
)
1290 generic_params
.push_back (e
->clone_generic_param ());
1293 // Overloaded assignment operator to clone
1294 TypeAlias
&operator= (TypeAlias
const &other
)
1296 VisItem::operator= (other
);
1297 new_type_name
= other
.new_type_name
;
1298 where_clause
= other
.where_clause
;
1299 existing_type
= other
.existing_type
->clone_type ();
1300 locus
= other
.locus
;
1302 generic_params
.reserve (other
.generic_params
.size ());
1303 for (const auto &e
: other
.generic_params
)
1304 generic_params
.push_back (e
->clone_generic_param ());
1309 // move constructors
1310 TypeAlias (TypeAlias
&&other
) = default;
1311 TypeAlias
&operator= (TypeAlias
&&other
) = default;
1313 Location
get_locus () const override final
{ return locus
; }
1315 void accept_vis (HIRFullVisitor
&vis
) override
;
1316 void accept_vis (HIRImplVisitor
&vis
) override
;
1317 void accept_vis (HIRStmtVisitor
&vis
) override
;
1318 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1320 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1322 return generic_params
;
1324 const std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params () const
1326 return generic_params
;
1329 WhereClause
&get_where_clause () { return where_clause
; }
1331 std::unique_ptr
<Type
> &get_type_aliased ()
1333 rust_assert (existing_type
!= nullptr);
1334 return existing_type
;
1337 Identifier
get_new_type_name () const { return new_type_name
; }
1339 ItemKind
get_item_kind () const override
{ return ItemKind::TypeAlias
; }
1341 Analysis::NodeMapping
get_impl_mappings () const override
1343 return get_mappings ();
1347 /* Use covariance to implement clone function as returning this object
1348 * rather than base */
1349 TypeAlias
*clone_item_impl () const override
{ return new TypeAlias (*this); }
1351 /* Use covariance to implement clone function as returning this object
1352 * rather than base */
1353 TypeAlias
*clone_inherent_impl_item_impl () const override
1355 return new TypeAlias (*this);
1359 // Rust base struct declaration HIR node - abstract base class
1360 class Struct
: public VisItem
1363 // protected to enable access by derived classes - allows better as_string
1364 Identifier struct_name
;
1366 // bool has_generics;
1367 // Generics generic_params;
1368 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1370 // bool has_where_clause;
1371 WhereClause where_clause
;
1376 Identifier
get_identifier () const { return struct_name
; }
1378 // Returns whether struct has generic parameters.
1379 bool has_generics () const { return !generic_params
.empty (); }
1381 // Returns whether struct has a where clause.
1382 bool has_where_clause () const { return !where_clause
.is_empty (); }
1384 Location
get_locus () const override final
{ return locus
; }
1385 ItemKind
get_item_kind () const override
{ return ItemKind::Struct
; }
1387 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1389 return generic_params
;
1392 WhereClause
&get_where_clause () { return where_clause
; }
1395 Struct (Analysis::NodeMapping mappings
, Identifier struct_name
,
1396 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1397 WhereClause where_clause
, Visibility vis
, Location locus
,
1398 AST::AttrVec outer_attrs
= AST::AttrVec ())
1399 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
1400 struct_name (std::move (struct_name
)),
1401 generic_params (std::move (generic_params
)),
1402 where_clause (std::move (where_clause
)), locus (locus
)
1405 // Copy constructor with vector clone
1406 Struct (Struct
const &other
)
1407 : VisItem (other
), struct_name (other
.struct_name
),
1408 where_clause (other
.where_clause
), locus (other
.locus
)
1410 generic_params
.reserve (other
.generic_params
.size ());
1411 for (const auto &e
: other
.generic_params
)
1412 generic_params
.push_back (e
->clone_generic_param ());
1415 // Overloaded assignment operator with vector clone
1416 Struct
&operator= (Struct
const &other
)
1418 VisItem::operator= (other
);
1419 struct_name
= other
.struct_name
;
1420 where_clause
= other
.where_clause
;
1421 locus
= other
.locus
;
1423 generic_params
.reserve (other
.generic_params
.size ());
1424 for (const auto &e
: other
.generic_params
)
1425 generic_params
.push_back (e
->clone_generic_param ());
1430 // move constructors
1431 Struct (Struct
&&other
) = default;
1432 Struct
&operator= (Struct
&&other
) = default;
1435 // A single field in a struct
1439 // bool has_outer_attributes;
1440 AST::AttrVec outer_attrs
;
1442 // bool has_visibility;
1443 Visibility visibility
;
1445 Identifier field_name
;
1446 std::unique_ptr
<Type
> field_type
;
1448 Analysis::NodeMapping mappings
;
1452 // Returns whether struct field has any outer attributes.
1453 bool has_outer_attributes () const { return !outer_attrs
.empty (); }
1455 // Returns whether struct field has a non-private (non-default) visibility.
1456 bool has_visibility () const { return !visibility
.is_error (); }
1458 StructField (Analysis::NodeMapping mappings
, Identifier field_name
,
1459 std::unique_ptr
<Type
> field_type
, Visibility vis
, Location locus
,
1460 AST::AttrVec outer_attrs
= AST::AttrVec ())
1461 : outer_attrs (std::move (outer_attrs
)), visibility (std::move (vis
)),
1462 field_name (std::move (field_name
)), field_type (std::move (field_type
)),
1463 mappings (mappings
), locus (locus
)
1467 StructField (StructField
const &other
)
1468 : outer_attrs (other
.outer_attrs
), visibility (other
.visibility
),
1469 field_name (other
.field_name
),
1470 field_type (other
.field_type
->clone_type ()), mappings (other
.mappings
)
1473 ~StructField () = default;
1475 // Overloaded assignment operator to clone
1476 StructField
&operator= (StructField
const &other
)
1478 field_name
= other
.field_name
;
1479 field_type
= other
.field_type
->clone_type ();
1480 visibility
= other
.visibility
;
1481 outer_attrs
= other
.outer_attrs
;
1482 mappings
= other
.mappings
;
1487 // move constructors
1488 StructField (StructField
&&other
) = default;
1489 StructField
&operator= (StructField
&&other
) = default;
1491 std::string
as_string () const;
1493 Identifier
get_field_name () const { return field_name
; }
1495 std::unique_ptr
<Type
> &get_field_type ()
1497 rust_assert (field_type
!= nullptr);
1501 Analysis::NodeMapping
get_mappings () const { return mappings
; }
1503 Location
get_locus () { return locus
; }
1505 Visibility
&get_visibility () { return visibility
; }
1508 // Rust struct declaration with true struct type HIR node
1509 class StructStruct
: public Struct
1512 std::vector
<StructField
> fields
;
1515 std::string
as_string () const override
;
1517 // Mega-constructor with all possible fields
1518 StructStruct (Analysis::NodeMapping mappings
, std::vector
<StructField
> fields
,
1519 Identifier struct_name
,
1520 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1521 WhereClause where_clause
, bool is_unit
, Visibility vis
,
1522 AST::AttrVec outer_attrs
, Location locus
)
1523 : Struct (std::move (mappings
), std::move (struct_name
),
1524 std::move (generic_params
), std::move (where_clause
),
1525 std::move (vis
), locus
, std::move (outer_attrs
)),
1526 fields (std::move (fields
)), is_unit (is_unit
)
1529 // Unit struct constructor
1530 StructStruct (Analysis::NodeMapping mappings
, Identifier struct_name
,
1531 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1532 WhereClause where_clause
, Visibility vis
,
1533 AST::AttrVec outer_attrs
, Location locus
)
1534 : Struct (std::move (mappings
), std::move (struct_name
),
1535 std::move (generic_params
), std::move (where_clause
),
1536 std::move (vis
), locus
, std::move (outer_attrs
)),
1539 // TODO: can a unit struct have generic fields? assuming yes for now.
1541 /* Returns whether the struct is a unit struct - struct defined without
1542 * fields. This is important because it also means an implicit constant of its
1543 * type is defined. */
1544 bool is_unit_struct () const { return is_unit
; }
1546 void accept_vis (HIRFullVisitor
&vis
) override
;
1547 void accept_vis (HIRStmtVisitor
&vis
) override
;
1548 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1550 std::vector
<StructField
> &get_fields () { return fields
; }
1553 /* Use covariance to implement clone function as returning this object
1554 * rather than base */
1555 StructStruct
*clone_item_impl () const override
1557 return new StructStruct (*this);
1560 /* Use covariance to implement clone function as returning this object
1561 * rather than base */
1562 /*virtual StructStruct* clone_statement_impl() const override {
1563 return new StructStruct(*this);
1567 // A single field in a tuple
1571 // bool has_outer_attributes;
1572 AST::AttrVec outer_attrs
;
1574 // bool has_visibility;
1575 Visibility visibility
;
1577 std::unique_ptr
<Type
> field_type
;
1581 Analysis::NodeMapping mappings
;
1584 // Returns whether tuple field has outer attributes.
1585 bool has_outer_attributes () const { return !outer_attrs
.empty (); }
1587 /* Returns whether tuple field has a non-default visibility (i.e. a public
1589 bool has_visibility () const { return !visibility
.is_error (); }
1591 // Complete constructor
1592 TupleField (Analysis::NodeMapping mapping
, std::unique_ptr
<Type
> field_type
,
1593 Visibility vis
, Location locus
,
1594 AST::AttrVec outer_attrs
= AST::AttrVec ())
1595 : outer_attrs (std::move (outer_attrs
)), visibility (std::move (vis
)),
1596 field_type (std::move (field_type
)), locus (locus
), mappings (mapping
)
1599 // Copy constructor with clone
1600 TupleField (TupleField
const &other
)
1601 : outer_attrs (other
.outer_attrs
), visibility (other
.visibility
),
1602 field_type (other
.field_type
->clone_type ()), locus (other
.locus
),
1603 mappings (other
.mappings
)
1606 ~TupleField () = default;
1608 // Overloaded assignment operator to clone
1609 TupleField
&operator= (TupleField
const &other
)
1611 field_type
= other
.field_type
->clone_type ();
1612 visibility
= other
.visibility
;
1613 outer_attrs
= other
.outer_attrs
;
1614 locus
= other
.locus
;
1615 mappings
= other
.mappings
;
1620 // move constructors
1621 TupleField (TupleField
&&other
) = default;
1622 TupleField
&operator= (TupleField
&&other
) = default;
1624 // Returns whether tuple field is in an error state.
1625 bool is_error () const { return field_type
== nullptr; }
1627 std::string
as_string () const;
1629 Analysis::NodeMapping
get_mappings () const { return mappings
; }
1631 Location
get_locus () const { return locus
; }
1633 std::unique_ptr
<HIR::Type
> &get_field_type () { return field_type
; }
1636 // Rust tuple declared using struct keyword HIR node
1637 class TupleStruct
: public Struct
1639 std::vector
<TupleField
> fields
;
1642 std::string
as_string () const override
;
1644 // Mega-constructor with all possible fields
1645 TupleStruct (Analysis::NodeMapping mappings
, std::vector
<TupleField
> fields
,
1646 Identifier struct_name
,
1647 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1648 WhereClause where_clause
, Visibility vis
,
1649 AST::AttrVec outer_attrs
, Location locus
)
1650 : Struct (std::move (mappings
), std::move (struct_name
),
1651 std::move (generic_params
), std::move (where_clause
),
1652 std::move (vis
), locus
, std::move (outer_attrs
)),
1653 fields (std::move (fields
))
1656 void accept_vis (HIRFullVisitor
&vis
) override
;
1657 void accept_vis (HIRStmtVisitor
&vis
) override
;
1658 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1660 std::vector
<TupleField
> &get_fields () { return fields
; }
1661 const std::vector
<TupleField
> &get_fields () const { return fields
; }
1664 /* Use covariance to implement clone function as returning this object
1665 * rather than base */
1666 TupleStruct
*clone_item_impl () const override
1668 return new TupleStruct (*this);
1671 /* Use covariance to implement clone function as returning this object
1672 * rather than base */
1673 /*virtual TupleStruct* clone_statement_impl() const override {
1674 return new TupleStruct(*this);
1678 /* An item used in an "enum" tagged union - not abstract: base represents a
1679 name-only enum. Syntactically EnumItem's can have a Visibility. But not
1680 Semantically. So check there is no Visibility when lowering and make this
1681 an Item, not an VisItem. */
1682 class EnumItem
: public Item
1684 Identifier variant_name
;
1688 virtual ~EnumItem () {}
1698 EnumItem (Analysis::NodeMapping mappings
, Identifier variant_name
,
1699 AST::AttrVec outer_attrs
, Location locus
)
1700 : Item (std::move (mappings
), std::move (outer_attrs
)),
1701 variant_name (std::move (variant_name
)), locus (locus
)
1704 // Unique pointer custom clone function
1705 std::unique_ptr
<EnumItem
> clone_enum_item () const
1707 return std::unique_ptr
<EnumItem
> (clone_item_impl ());
1710 virtual std::string
as_string () const override
;
1711 virtual EnumItemKind
get_enum_item_kind () const { return Named
; };
1713 // not pure virtual as not abstract
1714 void accept_vis (HIRFullVisitor
&vis
) override
;
1715 void accept_vis (HIRStmtVisitor
&vis
) override
;
1716 // void accept_vis (HIRVisItemVisitor &vis) override;
1718 Location
get_locus () const override
{ return locus
; }
1720 Identifier
get_identifier () const { return variant_name
; }
1722 ItemKind
get_item_kind () const override
{ return ItemKind::EnumItem
; }
1725 EnumItem
*clone_item_impl () const override
{ return new EnumItem (*this); }
1728 // A tuple item used in an "enum" tagged union
1729 class EnumItemTuple
: public EnumItem
1731 // bool has_tuple_fields;
1732 std::vector
<TupleField
> tuple_fields
;
1735 // Returns whether tuple enum item has tuple fields.
1736 bool has_tuple_fields () const { return !tuple_fields
.empty (); }
1738 EnumItemKind
get_enum_item_kind () const override
1740 return EnumItemKind::Tuple
;
1743 EnumItemTuple (Analysis::NodeMapping mappings
, Identifier variant_name
,
1744 std::vector
<TupleField
> tuple_fields
, AST::AttrVec outer_attrs
,
1746 : EnumItem (std::move (mappings
), std::move (variant_name
),
1747 std::move (outer_attrs
), locus
),
1748 tuple_fields (std::move (tuple_fields
))
1751 std::string
as_string () const override
;
1753 void accept_vis (HIRFullVisitor
&vis
) override
;
1754 void accept_vis (HIRStmtVisitor
&vis
) override
;
1756 std::vector
<TupleField
> &get_tuple_fields () { return tuple_fields
; }
1759 // Clone function implementation as (not pure) virtual method
1760 EnumItemTuple
*clone_item_impl () const override
1762 return new EnumItemTuple (*this);
1766 // A struct item used in an "enum" tagged union
1767 class EnumItemStruct
: public EnumItem
1769 // bool has_struct_fields;
1770 std::vector
<StructField
> struct_fields
;
1773 // Returns whether struct enum item has struct fields.
1774 bool has_struct_fields () const { return !struct_fields
.empty (); }
1776 EnumItemKind
get_enum_item_kind () const override
1778 return EnumItemKind::Struct
;
1781 EnumItemStruct (Analysis::NodeMapping mappings
, Identifier variant_name
,
1782 std::vector
<StructField
> struct_fields
,
1783 AST::AttrVec outer_attrs
, Location locus
)
1784 : EnumItem (std::move (mappings
), std::move (variant_name
),
1785 std::move (outer_attrs
), locus
),
1786 struct_fields (std::move (struct_fields
))
1789 std::string
as_string () const override
;
1791 void accept_vis (HIRFullVisitor
&vis
) override
;
1792 void accept_vis (HIRStmtVisitor
&vis
) override
;
1794 std::vector
<StructField
> &get_struct_fields () { return struct_fields
; }
1797 // Clone function implementation as (not pure) virtual method
1798 EnumItemStruct
*clone_item_impl () const override
1800 return new EnumItemStruct (*this);
1804 // A discriminant (numbered enum) item used in an "enum" tagged union
1805 class EnumItemDiscriminant
: public EnumItem
1807 std::unique_ptr
<Expr
> expression
;
1810 EnumItemDiscriminant (Analysis::NodeMapping mappings
, Identifier variant_name
,
1811 std::unique_ptr
<Expr
> expr
, AST::AttrVec outer_attrs
,
1813 : EnumItem (std::move (mappings
), std::move (variant_name
),
1814 std::move (outer_attrs
), locus
),
1815 expression (std::move (expr
))
1818 // Copy constructor with clone
1819 EnumItemDiscriminant (EnumItemDiscriminant
const &other
)
1820 : EnumItem (other
), expression (other
.expression
->clone_expr ())
1823 // Overloaded assignment operator to clone
1824 EnumItemDiscriminant
&operator= (EnumItemDiscriminant
const &other
)
1826 EnumItem::operator= (other
);
1827 expression
= other
.expression
->clone_expr ();
1828 // variant_name = other.variant_name;
1829 // outer_attrs = other.outer_attrs;
1834 // move constructors
1835 EnumItemDiscriminant (EnumItemDiscriminant
&&other
) = default;
1836 EnumItemDiscriminant
&operator= (EnumItemDiscriminant
&&other
) = default;
1838 EnumItemKind
get_enum_item_kind () const override
1840 return EnumItemKind::Discriminant
;
1843 std::string
as_string () const override
;
1845 void accept_vis (HIRFullVisitor
&vis
) override
;
1846 void accept_vis (HIRStmtVisitor
&vis
) override
;
1848 std::unique_ptr
<Expr
> &get_discriminant_expression () { return expression
; }
1851 // Clone function implementation as (not pure) virtual method
1852 EnumItemDiscriminant
*clone_item_impl () const override
1854 return new EnumItemDiscriminant (*this);
1858 // HIR node for Rust "enum" - tagged union
1859 class Enum
: public VisItem
1861 Identifier enum_name
;
1863 // bool has_generics;
1864 // Generics generic_params;
1865 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1867 // bool has_where_clause;
1868 WhereClause where_clause
;
1870 std::vector
<std::unique_ptr
<EnumItem
>> items
;
1875 std::string
as_string () const override
;
1877 // Returns whether "enum" has generic parameters.
1878 bool has_generics () const { return !generic_params
.empty (); }
1880 // Returns whether "enum" has a where clause.
1881 bool has_where_clause () const { return !where_clause
.is_empty (); }
1883 /* Returns whether enum is a "zero-variant" (no possible variant) enum,
1884 * which cannot be instantiated. */
1885 bool is_zero_variant () const { return items
.empty (); }
1888 Enum (Analysis::NodeMapping mappings
, Identifier enum_name
, Visibility vis
,
1889 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1890 WhereClause where_clause
, std::vector
<std::unique_ptr
<EnumItem
>> items
,
1891 AST::AttrVec outer_attrs
, Location locus
)
1892 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
1893 enum_name (std::move (enum_name
)),
1894 generic_params (std::move (generic_params
)),
1895 where_clause (std::move (where_clause
)), items (std::move (items
)),
1899 // TODO: constructor with less arguments
1901 // Copy constructor with vector clone
1902 Enum (Enum
const &other
)
1903 : VisItem (other
), enum_name (other
.enum_name
),
1904 where_clause (other
.where_clause
), locus (other
.locus
)
1906 generic_params
.reserve (other
.generic_params
.size ());
1907 for (const auto &e
: other
.generic_params
)
1908 generic_params
.push_back (e
->clone_generic_param ());
1910 items
.reserve (other
.items
.size ());
1911 for (const auto &e
: other
.items
)
1912 items
.push_back (e
->clone_enum_item ());
1915 // Overloaded assignment operator with vector clone
1916 Enum
&operator= (Enum
const &other
)
1918 VisItem::operator= (other
);
1919 enum_name
= other
.enum_name
;
1920 where_clause
= other
.where_clause
;
1921 locus
= other
.locus
;
1923 generic_params
.reserve (other
.generic_params
.size ());
1924 for (const auto &e
: other
.generic_params
)
1925 generic_params
.push_back (e
->clone_generic_param ());
1927 items
.reserve (other
.items
.size ());
1928 for (const auto &e
: other
.items
)
1929 items
.push_back (e
->clone_enum_item ());
1934 // Move constructors
1935 Enum (Enum
&&other
) = default;
1936 Enum
&operator= (Enum
&&other
) = default;
1938 Location
get_locus () const override final
{ return locus
; }
1940 void accept_vis (HIRFullVisitor
&vis
) override
;
1941 void accept_vis (HIRStmtVisitor
&vis
) override
;
1942 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1944 Identifier
get_identifier () const { return enum_name
; }
1945 ItemKind
get_item_kind () const override
{ return ItemKind::Enum
; }
1947 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1949 return generic_params
;
1952 const std::vector
<std::unique_ptr
<EnumItem
>> &get_variants () const
1958 /* Use covariance to implement clone function as returning this object
1959 * rather than base */
1960 Enum
*clone_item_impl () const override
{ return new Enum (*this); }
1962 /* Use covariance to implement clone function as returning this object
1963 * rather than base */
1964 /*virtual Enum* clone_statement_impl() const override {
1965 return new Enum(*this);
1969 // Rust untagged union used for C compat HIR node
1970 class Union
: public VisItem
1972 Identifier union_name
;
1974 // bool has_generics;
1975 // Generics generic_params;
1976 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1978 // bool has_where_clause;
1979 WhereClause where_clause
;
1981 std::vector
<StructField
> variants
;
1986 std::string
as_string () const override
;
1988 // Returns whether union has generic params.
1989 bool has_generics () const { return !generic_params
.empty (); }
1991 // Returns whether union has where clause.
1992 bool has_where_clause () const { return !where_clause
.is_empty (); }
1994 Union (Analysis::NodeMapping mappings
, Identifier union_name
, Visibility vis
,
1995 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1996 WhereClause where_clause
, std::vector
<StructField
> variants
,
1997 AST::AttrVec outer_attrs
, Location locus
)
1998 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
1999 union_name (std::move (union_name
)),
2000 generic_params (std::move (generic_params
)),
2001 where_clause (std::move (where_clause
)), variants (std::move (variants
)),
2005 // copy constructor with vector clone
2006 Union (Union
const &other
)
2007 : VisItem (other
), union_name (other
.union_name
),
2008 where_clause (other
.where_clause
), variants (other
.variants
),
2011 generic_params
.reserve (other
.generic_params
.size ());
2012 for (const auto &e
: other
.generic_params
)
2013 generic_params
.push_back (e
->clone_generic_param ());
2016 // overloaded assignment operator with vector clone
2017 Union
&operator= (Union
const &other
)
2019 VisItem::operator= (other
);
2020 union_name
= other
.union_name
;
2021 where_clause
= other
.where_clause
;
2022 variants
= other
.variants
;
2023 locus
= other
.locus
;
2025 generic_params
.reserve (other
.generic_params
.size ());
2026 for (const auto &e
: other
.generic_params
)
2027 generic_params
.push_back (e
->clone_generic_param ());
2032 // move constructors
2033 Union (Union
&&other
) = default;
2034 Union
&operator= (Union
&&other
) = default;
2036 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2038 return generic_params
;
2041 Identifier
get_identifier () const { return union_name
; }
2043 Location
get_locus () const override final
{ return locus
; }
2045 void accept_vis (HIRFullVisitor
&vis
) override
;
2046 void accept_vis (HIRStmtVisitor
&vis
) override
;
2047 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2049 std::vector
<StructField
> &get_variants () { return variants
; }
2051 WhereClause
&get_where_clause () { return where_clause
; }
2053 ItemKind
get_item_kind () const override
{ return ItemKind::Union
; }
2056 /* Use covariance to implement clone function as returning this object
2057 * rather than base */
2058 Union
*clone_item_impl () const override
{ return new Union (*this); }
2061 class ConstantItem
: public VisItem
, public ImplItem
2063 Identifier identifier
;
2064 std::unique_ptr
<Type
> type
;
2065 std::unique_ptr
<Expr
> const_expr
;
2069 std::string
as_string () const override
;
2071 ConstantItem (Analysis::NodeMapping mappings
, Identifier ident
,
2072 Visibility vis
, std::unique_ptr
<Type
> type
,
2073 std::unique_ptr
<Expr
> const_expr
, AST::AttrVec outer_attrs
,
2075 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
2076 identifier (std::move (ident
)), type (std::move (type
)),
2077 const_expr (std::move (const_expr
)), locus (locus
)
2080 ConstantItem (ConstantItem
const &other
)
2081 : VisItem (other
), identifier (other
.identifier
),
2082 type (other
.type
->clone_type ()),
2083 const_expr (other
.const_expr
->clone_expr ()), locus (other
.locus
)
2086 // Overload assignment operator to clone
2087 ConstantItem
&operator= (ConstantItem
const &other
)
2089 VisItem::operator= (other
);
2090 identifier
= other
.identifier
;
2091 type
= other
.type
->clone_type ();
2092 const_expr
= other
.const_expr
->clone_expr ();
2093 locus
= other
.locus
;
2098 // move constructors
2099 ConstantItem (ConstantItem
&&other
) = default;
2100 ConstantItem
&operator= (ConstantItem
&&other
) = default;
2102 // Returns whether constant item is an "unnamed" (wildcard underscore used
2103 // as identifier) constant.
2104 bool is_unnamed () const { return identifier
== std::string ("_"); }
2106 Location
get_locus () const override final
{ return locus
; }
2108 void accept_vis (HIRFullVisitor
&vis
) override
;
2109 void accept_vis (HIRStmtVisitor
&vis
) override
;
2110 void accept_vis (HIRImplVisitor
&vis
) override
;
2111 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2113 Type
*get_type () { return type
.get (); }
2115 Expr
*get_expr () { return const_expr
.get (); }
2117 std::string
get_identifier () { return identifier
; }
2119 Analysis::NodeMapping
get_impl_mappings () const override
2121 return get_mappings ();
2124 ImplItemType
get_impl_item_type () const override final
2126 return ImplItem::ImplItemType::CONSTANT
;
2129 ItemKind
get_item_kind () const override
{ return ItemKind::Constant
; }
2132 /* Use covariance to implement clone function as returning this object
2133 * rather than base */
2134 ConstantItem
*clone_item_impl () const override
2136 return new ConstantItem (*this);
2139 /* Use covariance to implement clone function as returning this object
2140 * rather than base */
2141 ConstantItem
*clone_inherent_impl_item_impl () const override
2143 return new ConstantItem (*this);
2147 /* Static item HIR node - items within module scope with fixed storage
2149 class StaticItem
: public VisItem
2153 std::unique_ptr
<Type
> type
;
2154 std::unique_ptr
<Expr
> expr
;
2158 std::string
as_string () const override
;
2160 StaticItem (Analysis::NodeMapping mappings
, Identifier name
, Mutability mut
,
2161 std::unique_ptr
<Type
> type
, std::unique_ptr
<Expr
> expr
,
2162 Visibility vis
, AST::AttrVec outer_attrs
, Location locus
)
2163 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
2164 mut (mut
), name (std::move (name
)), type (std::move (type
)),
2165 expr (std::move (expr
)), locus (locus
)
2168 // Copy constructor with clone
2169 StaticItem (StaticItem
const &other
)
2170 : VisItem (other
), mut (other
.mut
), name (other
.name
),
2171 type (other
.type
->clone_type ()), expr (other
.expr
->clone_expr ()),
2175 // Overloaded assignment operator to clone
2176 StaticItem
&operator= (StaticItem
const &other
)
2178 VisItem::operator= (other
);
2181 type
= other
.type
->clone_type ();
2182 expr
= other
.expr
->clone_expr ();
2183 locus
= other
.locus
;
2188 // move constructors
2189 StaticItem (StaticItem
&&other
) = default;
2190 StaticItem
&operator= (StaticItem
&&other
) = default;
2192 Location
get_locus () const override final
{ return locus
; }
2194 void accept_vis (HIRFullVisitor
&vis
) override
;
2195 void accept_vis (HIRStmtVisitor
&vis
) override
;
2196 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2198 Identifier
get_identifier () const { return name
; }
2200 Mutability
get_mut () const { return mut
; }
2202 bool is_mut () const { return mut
== Mutability::Mut
; }
2204 Expr
*get_expr () { return expr
.get (); }
2206 Type
*get_type () { return type
.get (); }
2208 ItemKind
get_item_kind () const override
{ return ItemKind::Static
; }
2211 StaticItem
*clone_item_impl () const override
2213 return new StaticItem (*this);
2217 // Function declaration in traits
2218 struct TraitFunctionDecl
2221 FunctionQualifiers qualifiers
;
2222 Identifier function_name
;
2223 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
2224 std::vector
<FunctionParam
> function_params
;
2225 std::unique_ptr
<Type
> return_type
;
2226 WhereClause where_clause
;
2231 TraitFunctionDecl (Identifier function_name
, FunctionQualifiers qualifiers
,
2232 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
2233 SelfParam self
, std::vector
<FunctionParam
> function_params
,
2234 std::unique_ptr
<Type
> return_type
,
2235 WhereClause where_clause
)
2236 : qualifiers (std::move (qualifiers
)),
2237 function_name (std::move (function_name
)),
2238 generic_params (std::move (generic_params
)),
2239 function_params (std::move (function_params
)),
2240 return_type (std::move (return_type
)),
2241 where_clause (std::move (where_clause
)), self (std::move (self
))
2244 // Copy constructor with clone
2245 TraitFunctionDecl (TraitFunctionDecl
const &other
)
2246 : qualifiers (other
.qualifiers
), function_name (other
.function_name
),
2247 function_params (other
.function_params
),
2248 return_type (other
.return_type
->clone_type ()),
2249 where_clause (other
.where_clause
), self (other
.self
)
2251 generic_params
.reserve (other
.generic_params
.size ());
2252 for (const auto &e
: other
.generic_params
)
2253 generic_params
.push_back (e
->clone_generic_param ());
2256 ~TraitFunctionDecl () = default;
2258 // Overloaded assignment operator with clone
2259 TraitFunctionDecl
&operator= (TraitFunctionDecl
const &other
)
2261 function_name
= other
.function_name
;
2262 qualifiers
= other
.qualifiers
;
2263 function_params
= other
.function_params
;
2264 return_type
= other
.return_type
->clone_type ();
2265 where_clause
= other
.where_clause
;
2268 generic_params
.reserve (other
.generic_params
.size ());
2269 for (const auto &e
: other
.generic_params
)
2270 generic_params
.push_back (e
->clone_generic_param ());
2275 // move constructors
2276 TraitFunctionDecl (TraitFunctionDecl
&&other
) = default;
2277 TraitFunctionDecl
&operator= (TraitFunctionDecl
&&other
) = default;
2279 std::string
as_string () const;
2281 // Returns whether function decl has generic parameters.
2282 bool has_generics () const { return !generic_params
.empty (); }
2284 // Returns whether function decl has regular parameters.
2285 bool has_params () const { return !function_params
.empty (); }
2287 // Returns whether function has return type (otherwise is void).
2288 bool has_return_type () const { return return_type
!= nullptr; }
2290 // Returns whether function has a where clause.
2291 bool has_where_clause () const { return !where_clause
.is_empty (); }
2293 bool is_method () const { return !self
.is_error (); }
2295 SelfParam
&get_self () { return self
; }
2297 Identifier
get_function_name () const { return function_name
; }
2299 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2301 return generic_params
;
2304 std::unique_ptr
<Type
> &get_return_type ()
2306 rust_assert (has_return_type ());
2310 std::vector
<FunctionParam
> &get_function_params () { return function_params
; }
2312 const FunctionQualifiers
&get_qualifiers () const { return qualifiers
; }
2315 // Actual trait item function declaration within traits
2316 class TraitItemFunc
: public TraitItem
2318 AST::AttrVec outer_attrs
;
2319 TraitFunctionDecl decl
;
2320 std::unique_ptr
<BlockExpr
> block_expr
;
2324 // Returns whether function has a definition or is just a declaration.
2325 bool has_definition () const { return block_expr
!= nullptr; }
2327 TraitItemFunc (Analysis::NodeMapping mappings
, TraitFunctionDecl decl
,
2328 std::unique_ptr
<BlockExpr
> block_expr
,
2329 AST::AttrVec outer_attrs
, Location locus
)
2330 : TraitItem (mappings
), outer_attrs (std::move (outer_attrs
)),
2331 decl (std::move (decl
)), block_expr (std::move (block_expr
)),
2335 // Copy constructor with clone
2336 TraitItemFunc (TraitItemFunc
const &other
)
2337 : TraitItem (other
.mappings
), outer_attrs (other
.outer_attrs
),
2338 decl (other
.decl
), locus (other
.locus
)
2340 if (other
.block_expr
!= nullptr)
2341 block_expr
= other
.block_expr
->clone_block_expr ();
2344 // Overloaded assignment operator to clone
2345 TraitItemFunc
&operator= (TraitItemFunc
const &other
)
2347 TraitItem::operator= (other
);
2348 outer_attrs
= other
.outer_attrs
;
2350 locus
= other
.locus
;
2351 mappings
= other
.mappings
;
2352 if (other
.block_expr
!= nullptr)
2353 block_expr
= other
.block_expr
->clone_block_expr ();
2358 // move constructors
2359 TraitItemFunc (TraitItemFunc
&&other
) = default;
2360 TraitItemFunc
&operator= (TraitItemFunc
&&other
) = default;
2362 std::string
as_string () const override
;
2364 Location
get_locus () const { return locus
; }
2366 void accept_vis (HIRFullVisitor
&vis
) override
;
2367 void accept_vis (HIRTraitItemVisitor
&vis
) override
;
2369 TraitFunctionDecl
&get_decl () { return decl
; }
2371 const TraitFunctionDecl
&get_decl () const { return decl
; }
2373 bool has_block_defined () const { return block_expr
!= nullptr; }
2375 std::unique_ptr
<BlockExpr
> &get_block_expr ()
2377 rust_assert (has_block_defined ());
2381 const std::string
trait_identifier () const override final
2383 return decl
.get_function_name ();
2386 TraitItemKind
get_item_kind () const override final
2388 return TraitItemKind::FUNC
;
2391 AST::AttrVec
&get_outer_attrs () override final
{ return outer_attrs
; }
2392 const AST::AttrVec
&get_outer_attrs () const override final
2398 // Clone function implementation as (not pure) virtual method
2399 TraitItemFunc
*clone_trait_item_impl () const override
2401 return new TraitItemFunc (*this);
2405 // Constant item within traits
2406 class TraitItemConst
: public TraitItem
2408 AST::AttrVec outer_attrs
;
2410 std::unique_ptr
<Type
> type
;
2411 std::unique_ptr
<Expr
> expr
;
2415 // Whether the constant item has an associated expression.
2416 bool has_expression () const { return expr
!= nullptr; }
2418 TraitItemConst (Analysis::NodeMapping mappings
, Identifier name
,
2419 std::unique_ptr
<Type
> type
, std::unique_ptr
<Expr
> expr
,
2420 AST::AttrVec outer_attrs
, Location locus
)
2421 : TraitItem (mappings
), outer_attrs (std::move (outer_attrs
)),
2422 name (std::move (name
)), type (std::move (type
)), expr (std::move (expr
)),
2426 // Copy constructor with clones
2427 TraitItemConst (TraitItemConst
const &other
)
2428 : TraitItem (other
.mappings
), outer_attrs (other
.outer_attrs
),
2429 name (other
.name
), type (other
.type
->clone_type ()),
2430 expr (other
.expr
->clone_expr ()), locus (other
.locus
)
2433 // Overloaded assignment operator to clone
2434 TraitItemConst
&operator= (TraitItemConst
const &other
)
2436 TraitItem::operator= (other
);
2437 outer_attrs
= other
.outer_attrs
;
2439 type
= other
.type
->clone_type ();
2440 expr
= other
.expr
->clone_expr ();
2441 locus
= other
.locus
;
2442 mappings
= other
.mappings
;
2447 // move constructors
2448 TraitItemConst (TraitItemConst
&&other
) = default;
2449 TraitItemConst
&operator= (TraitItemConst
&&other
) = default;
2451 std::string
as_string () const override
;
2453 Location
get_locus () const { return locus
; }
2455 void accept_vis (HIRFullVisitor
&vis
) override
;
2456 void accept_vis (HIRTraitItemVisitor
&vis
) override
;
2458 Identifier
get_name () const { return name
; }
2460 bool has_expr () const { return expr
!= nullptr; }
2462 std::unique_ptr
<Type
> &get_type () { return type
; }
2464 std::unique_ptr
<Expr
> &get_expr ()
2466 rust_assert (has_expr ());
2470 const std::string
trait_identifier () const override final
{ return name
; }
2472 TraitItemKind
get_item_kind () const override final
2474 return TraitItemKind::CONST
;
2477 AST::AttrVec
&get_outer_attrs () override final
{ return outer_attrs
; }
2478 const AST::AttrVec
&get_outer_attrs () const override final
2484 // Clone function implementation as (not pure) virtual method
2485 TraitItemConst
*clone_trait_item_impl () const override
2487 return new TraitItemConst (*this);
2491 // Type items within traits
2492 class TraitItemType
: public TraitItem
2494 AST::AttrVec outer_attrs
;
2497 std::vector
<std::unique_ptr
<TypeParamBound
>>
2498 type_param_bounds
; // inlined form
2502 // Returns whether trait item type has type param bounds.
2503 bool has_type_param_bounds () const { return !type_param_bounds
.empty (); }
2505 TraitItemType (Analysis::NodeMapping mappings
, Identifier name
,
2506 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
,
2507 AST::AttrVec outer_attrs
, Location locus
)
2508 : TraitItem (mappings
), outer_attrs (std::move (outer_attrs
)),
2509 name (std::move (name
)),
2510 type_param_bounds (std::move (type_param_bounds
)), locus (locus
)
2513 // Copy constructor with vector clone
2514 TraitItemType (TraitItemType
const &other
)
2515 : TraitItem (other
.mappings
), outer_attrs (other
.outer_attrs
),
2516 name (other
.name
), locus (other
.locus
)
2518 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
2519 for (const auto &e
: other
.type_param_bounds
)
2520 type_param_bounds
.push_back (e
->clone_type_param_bound ());
2523 // Overloaded assignment operator with vector clone
2524 TraitItemType
&operator= (TraitItemType
const &other
)
2526 TraitItem::operator= (other
);
2527 outer_attrs
= other
.outer_attrs
;
2529 locus
= other
.locus
;
2530 mappings
= other
.mappings
;
2532 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
2533 for (const auto &e
: other
.type_param_bounds
)
2534 type_param_bounds
.push_back (e
->clone_type_param_bound ());
2539 // default move constructors
2540 TraitItemType (TraitItemType
&&other
) = default;
2541 TraitItemType
&operator= (TraitItemType
&&other
) = default;
2543 std::string
as_string () const override
;
2545 Location
get_locus () const { return locus
; }
2547 void accept_vis (HIRFullVisitor
&vis
) override
;
2548 void accept_vis (HIRTraitItemVisitor
&vis
) override
;
2550 Identifier
get_name () const { return name
; }
2552 std::vector
<std::unique_ptr
<TypeParamBound
>> &get_type_param_bounds ()
2554 return type_param_bounds
;
2557 const std::string
trait_identifier () const override final
{ return name
; }
2559 TraitItemKind
get_item_kind () const override final
2561 return TraitItemKind::TYPE
;
2564 AST::AttrVec
&get_outer_attrs () override final
{ return outer_attrs
; }
2565 const AST::AttrVec
&get_outer_attrs () const override final
2571 // Clone function implementation as (not pure) virtual method
2572 TraitItemType
*clone_trait_item_impl () const override
2574 return new TraitItemType (*this);
2578 // Rust trait item declaration HIR node
2579 class Trait
: public VisItem
2583 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
2584 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
;
2585 WhereClause where_clause
;
2586 std::vector
<std::unique_ptr
<TraitItem
>> trait_items
;
2590 std::string
as_string () const override
;
2592 // Returns whether trait has generic parameters.
2593 bool has_generics () const { return !generic_params
.empty (); }
2595 // Returns whether trait has type parameter bounds.
2596 bool has_type_param_bounds () const { return !type_param_bounds
.empty (); }
2598 // Returns whether trait has where clause.
2599 bool has_where_clause () const { return !where_clause
.is_empty (); }
2601 // Returns whether trait has trait items.
2602 bool has_trait_items () const { return !trait_items
.empty (); }
2604 std::vector
<std::unique_ptr
<TraitItem
>> &get_trait_items ()
2609 Identifier
get_name () const { return name
; }
2612 Trait (Analysis::NodeMapping mappings
, Identifier name
, Unsafety unsafety
,
2613 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
2614 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
,
2615 WhereClause where_clause
,
2616 std::vector
<std::unique_ptr
<TraitItem
>> trait_items
, Visibility vis
,
2617 AST::AttrVec outer_attrs
, Location locus
)
2618 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
2619 unsafety (unsafety
), name (std::move (name
)),
2620 generic_params (std::move (generic_params
)),
2621 type_param_bounds (std::move (type_param_bounds
)),
2622 where_clause (std::move (where_clause
)),
2623 trait_items (std::move (trait_items
)), locus (locus
)
2626 // Copy constructor with vector clone
2627 Trait (Trait
const &other
)
2628 : VisItem (other
), unsafety (other
.unsafety
), name (other
.name
),
2629 where_clause (other
.where_clause
), locus (other
.locus
)
2631 generic_params
.reserve (other
.generic_params
.size ());
2632 for (const auto &e
: other
.generic_params
)
2633 generic_params
.push_back (e
->clone_generic_param ());
2635 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
2636 for (const auto &e
: other
.type_param_bounds
)
2637 type_param_bounds
.push_back (e
->clone_type_param_bound ());
2639 trait_items
.reserve (other
.trait_items
.size ());
2640 for (const auto &e
: other
.trait_items
)
2641 trait_items
.push_back (e
->clone_trait_item ());
2644 // Overloaded assignment operator with vector clone
2645 Trait
&operator= (Trait
const &other
)
2647 VisItem::operator= (other
);
2649 unsafety
= other
.unsafety
;
2650 where_clause
= other
.where_clause
;
2651 locus
= other
.locus
;
2653 generic_params
.reserve (other
.generic_params
.size ());
2654 for (const auto &e
: other
.generic_params
)
2655 generic_params
.push_back (e
->clone_generic_param ());
2657 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
2658 for (const auto &e
: other
.type_param_bounds
)
2659 type_param_bounds
.push_back (e
->clone_type_param_bound ());
2661 trait_items
.reserve (other
.trait_items
.size ());
2662 for (const auto &e
: other
.trait_items
)
2663 trait_items
.push_back (e
->clone_trait_item ());
2668 // default move constructors
2669 Trait (Trait
&&other
) = default;
2670 Trait
&operator= (Trait
&&other
) = default;
2672 Location
get_locus () const override final
{ return locus
; }
2674 void accept_vis (HIRFullVisitor
&vis
) override
;
2675 void accept_vis (HIRStmtVisitor
&vis
) override
;
2676 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2678 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2680 return generic_params
;
2683 const std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params () const
2685 return generic_params
;
2688 std::vector
<std::unique_ptr
<TypeParamBound
>> &get_type_param_bounds ()
2690 return type_param_bounds
;
2693 const std::vector
<std::unique_ptr
<TypeParamBound
>> &
2694 get_type_param_bounds () const
2696 return type_param_bounds
;
2699 ItemKind
get_item_kind () const override
{ return ItemKind::Trait
; }
2702 /* Use covariance to implement clone function as returning this object
2703 * rather than base */
2704 Trait
*clone_item_impl () const override
{ return new Trait (*this); }
2707 class ImplBlock
: public VisItem
2709 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
2710 std::unique_ptr
<Type
> impl_type
;
2711 std::unique_ptr
<TypePath
> trait_ref
;
2712 WhereClause where_clause
;
2714 AST::AttrVec inner_attrs
;
2716 std::vector
<std::unique_ptr
<ImplItem
>> impl_items
;
2719 ImplBlock (Analysis::NodeMapping mappings
,
2720 std::vector
<std::unique_ptr
<ImplItem
>> impl_items
,
2721 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
2722 std::unique_ptr
<Type
> impl_type
,
2723 std::unique_ptr
<TypePath
> trait_ref
, WhereClause where_clause
,
2724 Polarity polarity
, Visibility vis
, AST::AttrVec inner_attrs
,
2725 AST::AttrVec outer_attrs
, Location locus
)
2726 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
2727 generic_params (std::move (generic_params
)),
2728 impl_type (std::move (impl_type
)), trait_ref (std::move (trait_ref
)),
2729 where_clause (std::move (where_clause
)), polarity (polarity
),
2730 inner_attrs (std::move (inner_attrs
)), locus (locus
),
2731 impl_items (std::move (impl_items
))
2734 ImplBlock (ImplBlock
const &other
)
2735 : VisItem (other
), impl_type (other
.impl_type
->clone_type ()),
2736 where_clause (other
.where_clause
), polarity (other
.polarity
),
2737 inner_attrs (other
.inner_attrs
), locus (other
.locus
)
2739 generic_params
.reserve (other
.generic_params
.size ());
2740 for (const auto &e
: other
.generic_params
)
2741 generic_params
.push_back (e
->clone_generic_param ());
2743 impl_items
.reserve (other
.impl_items
.size ());
2744 for (const auto &e
: other
.impl_items
)
2745 impl_items
.push_back (e
->clone_inherent_impl_item ());
2748 ImplBlock
&operator= (ImplBlock
const &other
)
2750 VisItem::operator= (other
);
2751 impl_type
= other
.impl_type
->clone_type ();
2752 where_clause
= other
.where_clause
;
2753 polarity
= other
.polarity
;
2754 inner_attrs
= other
.inner_attrs
;
2755 locus
= other
.locus
;
2757 generic_params
.reserve (other
.generic_params
.size ());
2758 for (const auto &e
: other
.generic_params
)
2759 generic_params
.push_back (e
->clone_generic_param ());
2761 impl_items
.reserve (other
.impl_items
.size ());
2762 for (const auto &e
: other
.impl_items
)
2763 impl_items
.push_back (e
->clone_inherent_impl_item ());
2768 ImplBlock (ImplBlock
&&other
) = default;
2769 ImplBlock
&operator= (ImplBlock
&&other
) = default;
2771 std::string
as_string () const override
;
2773 // Returns whether inherent impl block has inherent impl items.
2774 bool has_impl_items () const { return !impl_items
.empty (); }
2776 void accept_vis (HIRFullVisitor
&vis
) override
;
2777 void accept_vis (HIRStmtVisitor
&vis
) override
;
2778 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2780 std::vector
<std::unique_ptr
<ImplItem
>> &get_impl_items ()
2785 const std::vector
<std::unique_ptr
<ImplItem
>> &get_impl_items () const
2790 // Returns whether impl has generic parameters.
2791 bool has_generics () const { return !generic_params
.empty (); }
2793 // Returns whether impl has where clause.
2794 bool has_where_clause () const { return !where_clause
.is_empty (); }
2796 // Returns the polarity of the impl.
2797 Polarity
get_polarity () const { return polarity
; }
2799 // Returns whether impl has inner attributes.
2800 bool has_inner_attrs () const { return !inner_attrs
.empty (); }
2802 Location
get_locus () const override final
{ return locus
; }
2804 std::unique_ptr
<Type
> &get_type () { return impl_type
; };
2806 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2808 return generic_params
;
2811 bool has_trait_ref () const { return trait_ref
!= nullptr; }
2813 std::unique_ptr
<TypePath
> &get_trait_ref ()
2815 rust_assert (has_trait_ref ());
2819 WhereClause
&get_where_clause () { return where_clause
; }
2821 ItemKind
get_item_kind () const override
{ return ItemKind::Impl
; }
2824 ImplBlock
*clone_item_impl () const override
{ return new ImplBlock (*this); }
2827 // Abstract base class for an item used inside an extern block
2828 class ExternalItem
: public Node
2830 Analysis::NodeMapping mappings
;
2831 AST::AttrVec outer_attrs
;
2832 Visibility visibility
;
2833 Identifier item_name
;
2837 enum class ExternKind
2843 virtual ~ExternalItem () {}
2845 BaseKind
get_hir_kind () override final
{ return EXTERNAL
; }
2847 virtual ExternKind
get_extern_kind () = 0;
2849 // Returns whether item has outer attributes.
2850 bool has_outer_attrs () const { return !outer_attrs
.empty (); }
2852 // Returns whether item has non-default visibility.
2853 bool has_visibility () const { return !visibility
.is_error (); }
2855 // Unique pointer custom clone function
2856 std::unique_ptr
<ExternalItem
> clone_external_item () const
2858 return std::unique_ptr
<ExternalItem
> (clone_external_item_impl ());
2861 virtual std::string
as_string () const;
2863 Location
get_locus () const { return locus
; }
2865 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
2866 virtual void accept_vis (HIRExternalItemVisitor
&vis
) = 0;
2868 Analysis::NodeMapping
get_mappings () const { return mappings
; }
2870 Identifier
get_item_name () const { return item_name
; }
2872 AST::AttrVec
&get_outer_attrs () { return outer_attrs
; }
2875 ExternalItem (Analysis::NodeMapping mappings
, Identifier item_name
,
2876 Visibility vis
, AST::AttrVec outer_attrs
, Location locus
)
2877 : mappings (mappings
), outer_attrs (std::move (outer_attrs
)),
2878 visibility (std::move (vis
)), item_name (std::move (item_name
)),
2883 ExternalItem (ExternalItem
const &other
)
2884 : mappings (other
.mappings
), outer_attrs (other
.outer_attrs
),
2885 visibility (other
.visibility
), item_name (other
.item_name
),
2889 // Overloaded assignment operator to clone
2890 ExternalItem
&operator= (ExternalItem
const &other
)
2892 mappings
= other
.mappings
;
2893 item_name
= other
.item_name
;
2894 visibility
= other
.visibility
;
2895 outer_attrs
= other
.outer_attrs
;
2896 locus
= other
.locus
;
2901 // move constructors
2902 ExternalItem (ExternalItem
&&other
) = default;
2903 ExternalItem
&operator= (ExternalItem
&&other
) = default;
2905 // Clone function implementation as pure virtual method
2906 virtual ExternalItem
*clone_external_item_impl () const = 0;
2909 // A static item used in an extern block
2910 class ExternalStaticItem
: public ExternalItem
2913 std::unique_ptr
<Type
> item_type
;
2916 ExternalStaticItem (Analysis::NodeMapping mappings
, Identifier item_name
,
2917 std::unique_ptr
<Type
> item_type
, Mutability mut
,
2918 Visibility vis
, AST::AttrVec outer_attrs
, Location locus
)
2919 : ExternalItem (std::move (mappings
), std::move (item_name
),
2920 std::move (vis
), std::move (outer_attrs
), locus
),
2921 mut (mut
), item_type (std::move (item_type
))
2925 ExternalStaticItem (ExternalStaticItem
const &other
)
2926 : ExternalItem (other
), mut (other
.mut
),
2927 item_type (other
.item_type
->clone_type ())
2930 // Overloaded assignment operator to clone
2931 ExternalStaticItem
&operator= (ExternalStaticItem
const &other
)
2933 ExternalItem::operator= (other
);
2934 item_type
= other
.item_type
->clone_type ();
2940 // move constructors
2941 ExternalStaticItem (ExternalStaticItem
&&other
) = default;
2942 ExternalStaticItem
&operator= (ExternalStaticItem
&&other
) = default;
2944 std::string
as_string () const override
;
2946 void accept_vis (HIRFullVisitor
&vis
) override
;
2947 void accept_vis (HIRExternalItemVisitor
&vis
) override
;
2949 bool is_mut () const { return mut
== Mutability::Mut
; }
2951 Mutability
get_mut () { return mut
; }
2953 std::unique_ptr
<Type
> &get_item_type () { return item_type
; }
2955 ExternKind
get_extern_kind () override
{ return ExternKind::Static
; }
2958 /* Use covariance to implement clone function as returning this object
2959 * rather than base */
2960 ExternalStaticItem
*clone_external_item_impl () const override
2962 return new ExternalStaticItem (*this);
2966 // A named function parameter used in external functions
2967 struct NamedFunctionParam
2971 std::unique_ptr
<Type
> param_type
;
2972 Analysis::NodeMapping mappings
;
2975 bool has_name () const { return name
!= "_"; }
2977 NamedFunctionParam (Analysis::NodeMapping mappings
, Identifier name
,
2978 std::unique_ptr
<Type
> param_type
)
2979 : name (std::move (name
)), param_type (std::move (param_type
)),
2980 mappings (std::move (mappings
))
2984 NamedFunctionParam (NamedFunctionParam
const &other
)
2985 : name (other
.name
), param_type (other
.param_type
->clone_type ()),
2986 mappings (other
.mappings
)
2989 ~NamedFunctionParam () = default;
2991 // Overloaded assignment operator to clone
2992 NamedFunctionParam
&operator= (NamedFunctionParam
const &other
)
2994 mappings
= other
.mappings
;
2996 param_type
= other
.param_type
->clone_type ();
2997 // has_name = other.has_name;
3002 // move constructors
3003 NamedFunctionParam (NamedFunctionParam
&&other
) = default;
3004 NamedFunctionParam
&operator= (NamedFunctionParam
&&other
) = default;
3006 std::string
as_string () const;
3008 Identifier
get_param_name () const { return name
; }
3010 std::unique_ptr
<Type
> &get_type () { return param_type
; }
3012 Analysis::NodeMapping
get_mappings () const { return mappings
; }
3015 // A function item used in an extern block
3016 class ExternalFunctionItem
: public ExternalItem
3018 // bool has_generics;
3019 // Generics generic_params;
3020 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
3022 // bool has_return_type;
3023 // FunctionReturnType return_type;
3024 std::unique_ptr
<Type
> return_type
; // inlined
3026 // bool has_where_clause;
3027 WhereClause where_clause
;
3029 std::vector
<NamedFunctionParam
> function_params
;
3033 // Returns whether item has generic parameters.
3034 bool has_generics () const { return !generic_params
.empty (); }
3036 // Returns whether item has a return type (otherwise void).
3037 bool has_return_type () const { return return_type
!= nullptr; }
3039 // Returns whether item has a where clause.
3040 bool has_where_clause () const { return !where_clause
.is_empty (); }
3042 ExternalFunctionItem (
3043 Analysis::NodeMapping mappings
, Identifier item_name
,
3044 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
3045 std::unique_ptr
<Type
> return_type
, WhereClause where_clause
,
3046 std::vector
<NamedFunctionParam
> function_params
, bool has_variadics
,
3047 Visibility vis
, AST::AttrVec outer_attrs
, Location locus
)
3048 : ExternalItem (std::move (mappings
), std::move (item_name
),
3049 std::move (vis
), std::move (outer_attrs
), locus
),
3050 generic_params (std::move (generic_params
)),
3051 return_type (std::move (return_type
)),
3052 where_clause (std::move (where_clause
)),
3053 function_params (std::move (function_params
)),
3054 has_variadics (has_variadics
)
3057 // Copy constructor with clone
3058 ExternalFunctionItem (ExternalFunctionItem
const &other
)
3059 : ExternalItem (other
), return_type (other
.return_type
->clone_type ()),
3060 where_clause (other
.where_clause
),
3061 function_params (other
.function_params
),
3062 has_variadics (other
.has_variadics
)
3064 generic_params
.reserve (other
.generic_params
.size ());
3065 for (const auto &e
: other
.generic_params
)
3066 generic_params
.push_back (e
->clone_generic_param ());
3069 // Overloaded assignment operator with clone
3070 ExternalFunctionItem
&operator= (ExternalFunctionItem
const &other
)
3072 ExternalItem::operator= (other
);
3073 return_type
= other
.return_type
->clone_type ();
3074 where_clause
= other
.where_clause
;
3075 function_params
= other
.function_params
;
3076 has_variadics
= other
.has_variadics
;
3078 generic_params
.reserve (other
.generic_params
.size ());
3079 for (const auto &e
: other
.generic_params
)
3080 generic_params
.push_back (e
->clone_generic_param ());
3085 // move constructors
3086 ExternalFunctionItem (ExternalFunctionItem
&&other
) = default;
3087 ExternalFunctionItem
&operator= (ExternalFunctionItem
&&other
) = default;
3089 std::string
as_string () const override
;
3091 void accept_vis (HIRFullVisitor
&vis
) override
;
3092 void accept_vis (HIRExternalItemVisitor
&vis
) override
;
3094 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
3096 return generic_params
;
3099 std::unique_ptr
<Type
> &get_return_type () { return return_type
; }
3101 std::vector
<NamedFunctionParam
> &get_function_params ()
3103 return function_params
;
3106 bool is_variadic () const { return has_variadics
; }
3108 ExternKind
get_extern_kind () override
{ return ExternKind::Function
; }
3111 /* Use covariance to implement clone function as returning this object
3112 * rather than base */
3113 ExternalFunctionItem
*clone_external_item_impl () const override
3115 return new ExternalFunctionItem (*this);
3119 // An extern block HIR node
3120 class ExternBlock
: public VisItem
3123 AST::AttrVec inner_attrs
;
3124 std::vector
<std::unique_ptr
<ExternalItem
>> extern_items
;
3128 std::string
as_string () const override
;
3130 // Returns whether extern block has inner attributes.
3131 bool has_inner_attrs () const { return !inner_attrs
.empty (); }
3133 // Returns whether extern block has extern items.
3134 bool has_extern_items () const { return !extern_items
.empty (); }
3136 ABI
get_abi () const { return abi
; }
3138 ExternBlock (Analysis::NodeMapping mappings
, ABI abi
,
3139 std::vector
<std::unique_ptr
<ExternalItem
>> extern_items
,
3140 Visibility vis
, AST::AttrVec inner_attrs
,
3141 AST::AttrVec outer_attrs
, Location locus
)
3142 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
3143 abi (abi
), inner_attrs (std::move (inner_attrs
)),
3144 extern_items (std::move (extern_items
)), locus (locus
)
3147 // Copy constructor with vector clone
3148 ExternBlock (ExternBlock
const &other
)
3149 : VisItem (other
), abi (other
.abi
), inner_attrs (other
.inner_attrs
),
3152 extern_items
.reserve (other
.extern_items
.size ());
3153 for (const auto &e
: other
.extern_items
)
3154 extern_items
.push_back (e
->clone_external_item ());
3157 // Overloaded assignment operator with vector clone
3158 ExternBlock
&operator= (ExternBlock
const &other
)
3160 VisItem::operator= (other
);
3162 inner_attrs
= other
.inner_attrs
;
3163 locus
= other
.locus
;
3165 extern_items
.reserve (other
.extern_items
.size ());
3166 for (const auto &e
: other
.extern_items
)
3167 extern_items
.push_back (e
->clone_external_item ());
3172 // move constructors
3173 ExternBlock (ExternBlock
&&other
) = default;
3174 ExternBlock
&operator= (ExternBlock
&&other
) = default;
3176 Location
get_locus () const override final
{ return locus
; }
3178 void accept_vis (HIRFullVisitor
&vis
) override
;
3179 void accept_vis (HIRStmtVisitor
&vis
) override
;
3180 void accept_vis (HIRVisItemVisitor
&vis
) override
;
3182 std::vector
<std::unique_ptr
<ExternalItem
>> &get_extern_items ()
3184 return extern_items
;
3187 ItemKind
get_item_kind () const override
{ return ItemKind::ExternBlock
; }
3190 /* Use covariance to implement clone function as returning this object
3191 * rather than base */
3192 ExternBlock
*clone_item_impl () const override
3194 return new ExternBlock (*this);
3197 /* Use covariance to implement clone function as returning this object
3198 * rather than base */
3199 /*virtual ExternBlock* clone_statement_impl() const override {
3200 return new ExternBlock(*this);