1 // Copyright (C) 2020-2024 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
;
576 // should this store location info?
579 Visibility (VisType vis_type
,
580 HIR::SimplePath path
= HIR::SimplePath::create_empty (),
581 Location locus
= Location ())
582 : vis_type (vis_type
), path (std::move (path
)), locus (locus
)
585 // Returns whether visibility is in an error state.
586 bool is_error () const { return vis_type
== ERROR
; }
588 // Does the current visibility refer to a simple `pub <item>` entirely public
589 bool is_public () const { return vis_type
== PUBLIC
; }
591 // Is the current visibility public restricted to a certain path
592 bool is_restricted () const { return vis_type
== RESTRICTED
; }
594 // Creates an error visibility.
595 static Visibility
create_error ()
597 return Visibility (ERROR
, HIR::SimplePath::create_empty ());
600 VisType
get_vis_type () const { return vis_type
; }
602 const HIR::SimplePath
&get_path () const
604 rust_assert (!is_error ());
608 std::string
as_string () const;
611 // Item that supports visibility - abstract base class
612 class VisItem
: public Item
614 Visibility visibility
;
617 // Visibility constructor
618 VisItem (Analysis::NodeMapping mappings
, Visibility visibility
,
619 AST::AttrVec outer_attrs
= AST::AttrVec ())
620 : Item (std::move (mappings
), std::move (outer_attrs
)),
621 visibility (std::move (visibility
))
624 // Visibility copy constructor
625 VisItem (VisItem
const &other
) : Item (other
), visibility (other
.visibility
)
628 // Overload assignment operator to clone
629 VisItem
&operator= (VisItem
const &other
)
631 Item::operator= (other
);
632 visibility
= other
.visibility
;
633 // outer_attrs = other.outer_attrs;
639 VisItem (VisItem
&&other
) = default;
640 VisItem
&operator= (VisItem
&&other
) = default;
643 using HIR::Stmt::accept_vis
;
645 BaseKind
get_hir_kind () override final
{ return VIS_ITEM
; }
647 /* Does the item have some kind of public visibility (non-default
649 bool has_visibility () const { return !visibility
.is_error (); }
651 virtual void accept_vis (HIRVisItemVisitor
&vis
) = 0;
653 Visibility
&get_visibility () { return visibility
; }
654 const Visibility
&get_visibility () const { return visibility
; }
656 std::string
as_string () const override
;
659 // Rust module item - abstract base class
660 class Module
: public VisItem
662 Identifier module_name
;
664 // bool has_inner_attrs;
665 AST::AttrVec inner_attrs
;
667 std::vector
<std::unique_ptr
<Item
>> items
;
670 std::string
as_string () const override
;
672 // Returns whether the module has items in its body.
673 bool has_items () const { return !items
.empty (); }
675 // Returns whether the module has any inner attributes.
676 bool has_inner_attrs () const { return !inner_attrs
.empty (); }
679 Module (Analysis::NodeMapping mappings
, Identifier module_name
,
680 Location locus
, std::vector
<std::unique_ptr
<Item
>> items
,
681 Visibility visibility
= Visibility::create_error (),
682 AST::AttrVec inner_attrs
= AST::AttrVec (),
683 AST::AttrVec outer_attrs
= AST::AttrVec ())
684 : VisItem (std::move (mappings
), std::move (visibility
),
685 std::move (outer_attrs
)),
686 module_name (module_name
), locus (locus
),
687 inner_attrs (std::move (inner_attrs
)), items (std::move (items
))
690 // Copy constructor with vector clone
691 Module (Module
const &other
)
692 : VisItem (other
), inner_attrs (other
.inner_attrs
)
694 items
.reserve (other
.items
.size ());
695 for (const auto &e
: other
.items
)
696 items
.push_back (e
->clone_item ());
699 // Overloaded assignment operator with vector clone
700 Module
&operator= (Module
const &other
)
702 VisItem::operator= (other
);
703 inner_attrs
= other
.inner_attrs
;
705 items
.reserve (other
.items
.size ());
706 for (const auto &e
: other
.items
)
707 items
.push_back (e
->clone_item ());
713 Module (Module
&&other
) = default;
714 Module
&operator= (Module
&&other
) = default;
716 void accept_vis (HIRFullVisitor
&vis
) override
;
717 void accept_vis (HIRStmtVisitor
&vis
) override
;
718 void accept_vis (HIRVisItemVisitor
&vis
) override
;
720 std::vector
<std::unique_ptr
<Item
>> &get_items () { return items
; };
722 /* Override that runs the function recursively on all items contained within
724 void add_crate_name (std::vector
<std::string
> &names
) const override
;
726 Location
get_locus () const override final
{ return locus
; }
728 ItemKind
get_item_kind () const override
{ return ItemKind::Module
; }
731 /* Use covariance to implement clone function as returning this object
732 * rather than base */
733 Module
*clone_item_impl () const override
{ return new Module (*this); }
735 /* Use covariance to implement clone function as returning this object
736 * rather than base */
737 /*virtual Module* clone_statement_impl() const override {
738 return new Module(*this);
742 // Rust extern crate declaration HIR node
743 class ExternCrate
: public VisItem
745 // this is either an identifier or "self", with self parsed to string
746 std::string referenced_crate
;
747 // bool has_as_clause;
748 // AsClause as_clause;
749 // this is either an identifier or "_", with _ parsed to string
750 std::string as_clause_name
;
755 "extern crate foo as _"
757 "extern crate std as cool_std" */
759 std::string
as_string () const override
;
761 // Returns whether extern crate declaration has an as clause.
762 bool has_as_clause () const { return !as_clause_name
.empty (); }
764 /* Returns whether extern crate declaration references the current crate
766 bool references_self () const { return referenced_crate
== "self"; }
769 ExternCrate (Analysis::NodeMapping mappings
, std::string referenced_crate
,
770 Visibility visibility
, AST::AttrVec outer_attrs
, Location locus
,
771 std::string as_clause_name
= std::string ())
772 : VisItem (std::move (mappings
), std::move (visibility
),
773 std::move (outer_attrs
)),
774 referenced_crate (std::move (referenced_crate
)),
775 as_clause_name (std::move (as_clause_name
)), locus (locus
)
778 Location
get_locus () const override final
{ return locus
; }
780 ItemKind
get_item_kind () const override
{ return ItemKind::ExternCrate
; }
782 void accept_vis (HIRFullVisitor
&vis
) override
;
783 void accept_vis (HIRStmtVisitor
&vis
) override
;
784 void accept_vis (HIRVisItemVisitor
&vis
) override
;
786 // Override that adds extern crate name in decl to passed list of names.
787 void add_crate_name (std::vector
<std::string
> &names
) const override
789 names
.push_back (referenced_crate
);
793 /* Use covariance to implement clone function as returning this object
794 * rather than base */
795 ExternCrate
*clone_item_impl () const override
797 return new ExternCrate (*this);
800 /* Use covariance to implement clone function as returning this object
801 * rather than base */
802 /*virtual ExternCrate* clone_statement_impl() const override {
803 return new ExternCrate(*this);
807 // The path-ish thing referred to in a use declaration - abstract base class
813 virtual ~UseTree () {}
815 // Unique pointer custom clone function
816 std::unique_ptr
<UseTree
> clone_use_tree () const
818 return std::unique_ptr
<UseTree
> (clone_use_tree_impl ());
821 virtual std::string
as_string () const = 0;
823 Location
get_locus () const { return locus
; }
825 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
828 // Clone function implementation as pure virtual method
829 virtual UseTree
*clone_use_tree_impl () const = 0;
831 UseTree (Location locus
) : locus (locus
) {}
834 // Use tree with a glob (wildcard) operator
835 class UseTreeGlob
: public UseTree
847 AST::SimplePath path
;
850 UseTreeGlob (PathType glob_type
, AST::SimplePath path
, Location locus
)
851 : UseTree (locus
), glob_type (glob_type
), path (std::move (path
))
853 if (this->glob_type
!= PATH_PREFIXED
)
855 // compiler implementation error if there is a path with a
856 // non-path-prefixed use tree glob
857 gcc_assert (!has_path ());
859 // TODO: do path-prefixed paths also have to have a path? If so, have an
860 // assert for that too.
863 /* Returns whether has path. Should be made redundant by PathType
865 bool has_path () const { return !path
.is_empty (); }
867 std::string
as_string () const override
;
869 void accept_vis (HIRFullVisitor
&vis
) override
;
871 /* TODO: find way to ensure only PATH_PREFIXED glob_type has path - factory
874 /* Use covariance to implement clone function as returning this object
875 * rather than base */
876 UseTreeGlob
*clone_use_tree_impl () const override
878 return new UseTreeGlob (*this);
882 // Use tree with a list of paths with a common prefix
883 class UseTreeList
: public UseTree
895 AST::SimplePath path
;
897 std::vector
<std::unique_ptr
<UseTree
>> trees
;
900 UseTreeList (PathType path_type
, AST::SimplePath path
,
901 std::vector
<std::unique_ptr
<UseTree
>> trees
, Location locus
)
902 : UseTree (locus
), path_type (path_type
), path (std::move (path
)),
903 trees (std::move (trees
))
905 if (this->path_type
!= PATH_PREFIXED
)
907 // compiler implementation error if there is a path with a
908 // non-path-prefixed use tree glob
909 gcc_assert (!has_path ());
911 // TODO: do path-prefixed paths also have to have a path? If so, have an
912 // assert for that too.
915 // copy constructor with vector clone
916 UseTreeList (UseTreeList
const &other
)
917 : UseTree (other
), path_type (other
.path_type
), path (other
.path
)
919 trees
.reserve (other
.trees
.size ());
920 for (const auto &e
: other
.trees
)
921 trees
.push_back (e
->clone_use_tree ());
924 // overloaded assignment operator with vector clone
925 UseTreeList
&operator= (UseTreeList
const &other
)
927 UseTree::operator= (other
);
928 path_type
= other
.path_type
;
931 trees
.reserve (other
.trees
.size ());
932 for (const auto &e
: other
.trees
)
933 trees
.push_back (e
->clone_use_tree ());
939 UseTreeList (UseTreeList
&&other
) = default;
940 UseTreeList
&operator= (UseTreeList
&&other
) = default;
942 // Returns whether has path. Should be made redundant by path_type.
943 bool has_path () const { return !path
.is_empty (); }
945 // Returns whether has inner tree elements.
946 bool has_trees () const { return !trees
.empty (); }
948 std::string
as_string () const override
;
950 void accept_vis (HIRFullVisitor
&vis
) override
;
952 // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
955 /* Use covariance to implement clone function as returning this object
956 * rather than base */
957 UseTreeList
*clone_use_tree_impl () const override
959 return new UseTreeList (*this);
963 // Use tree where it rebinds the module name as something else
964 class UseTreeRebind
: public UseTree
975 AST::SimplePath path
;
977 NewBindType bind_type
;
978 Identifier identifier
; // only if NewBindType is IDENTIFIER
981 UseTreeRebind (NewBindType bind_type
, AST::SimplePath path
, Location locus
,
982 Identifier identifier
= std::string ())
983 : UseTree (locus
), path (std::move (path
)), bind_type (bind_type
),
984 identifier (std::move (identifier
))
987 // Returns whether has path (this should always be true).
988 bool has_path () const { return !path
.is_empty (); }
990 // Returns whether has identifier (or, rather, is allowed to).
991 bool has_identifier () const { return bind_type
== IDENTIFIER
; }
993 std::string
as_string () const override
;
995 void accept_vis (HIRFullVisitor
&vis
) override
;
997 // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
1000 /* Use covariance to implement clone function as returning this object
1001 * rather than base */
1002 virtual UseTreeRebind
*clone_use_tree_impl () const override
1004 return new UseTreeRebind (*this);
1008 // Rust use declaration (i.e. for modules) HIR node
1009 class UseDeclaration
: public VisItem
1011 std::unique_ptr
<UseTree
> use_tree
;
1015 std::string
as_string () const override
;
1017 UseDeclaration (Analysis::NodeMapping mappings
,
1018 std::unique_ptr
<UseTree
> use_tree
, Visibility visibility
,
1019 AST::AttrVec outer_attrs
, Location locus
)
1020 : VisItem (std::move (mappings
), std::move (visibility
),
1021 std::move (outer_attrs
)),
1022 use_tree (std::move (use_tree
)), locus (locus
)
1025 // Copy constructor with clone
1026 UseDeclaration (UseDeclaration
const &other
)
1027 : VisItem (other
), use_tree (other
.use_tree
->clone_use_tree ()),
1031 // Overloaded assignment operator to clone
1032 UseDeclaration
&operator= (UseDeclaration
const &other
)
1034 VisItem::operator= (other
);
1035 use_tree
= other
.use_tree
->clone_use_tree ();
1036 // visibility = other.visibility->clone_visibility();
1037 // outer_attrs = other.outer_attrs;
1038 locus
= other
.locus
;
1043 // move constructors
1044 UseDeclaration (UseDeclaration
&&other
) = default;
1045 UseDeclaration
&operator= (UseDeclaration
&&other
) = default;
1047 Location
get_locus () const override final
{ return locus
; }
1048 ItemKind
get_item_kind () const override
{ return ItemKind::UseDeclaration
; }
1050 void accept_vis (HIRFullVisitor
&vis
) override
;
1051 void accept_vis (HIRStmtVisitor
&vis
) override
;
1052 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1055 /* Use covariance to implement clone function as returning this object
1056 * rather than base */
1057 UseDeclaration
*clone_item_impl () const override
1059 return new UseDeclaration (*this);
1062 /* Use covariance to implement clone function as returning this object
1063 * rather than base */
1064 /*virtual UseDeclaration* clone_statement_impl() const override {
1065 return new UseDeclaration(*this);
1071 // Rust function declaration HIR node
1072 class Function
: public VisItem
, public ImplItem
1074 FunctionQualifiers qualifiers
;
1075 Identifier function_name
;
1076 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
1077 std::vector
<FunctionParam
> function_params
;
1078 std::unique_ptr
<Type
> return_type
;
1079 WhereClause where_clause
;
1080 std::unique_ptr
<BlockExpr
> function_body
;
1085 std::string
as_string () const override
;
1087 // Returns whether function has generic parameters.
1088 bool has_generics () const { return !generic_params
.empty (); }
1090 // Returns whether function has regular parameters.
1091 bool has_function_params () const { return !function_params
.empty (); }
1093 // Returns whether function has return type - if not, it is void.
1094 bool has_function_return_type () const { return return_type
!= nullptr; }
1096 // Returns whether function has a where clause.
1097 bool has_where_clause () const { return !where_clause
.is_empty (); }
1099 ImplItemType
get_impl_item_type () const override final
1101 return ImplItem::ImplItemType::FUNCTION
;
1104 ItemKind
get_item_kind () const override
{ return ItemKind::Function
; }
1106 // Mega-constructor with all possible fields
1107 Function (Analysis::NodeMapping mappings
, Identifier function_name
,
1108 FunctionQualifiers qualifiers
,
1109 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1110 std::vector
<FunctionParam
> function_params
,
1111 std::unique_ptr
<Type
> return_type
, WhereClause where_clause
,
1112 std::unique_ptr
<BlockExpr
> function_body
, Visibility vis
,
1113 AST::AttrVec outer_attrs
, SelfParam self
, Location locus
)
1114 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
1115 qualifiers (std::move (qualifiers
)),
1116 function_name (std::move (function_name
)),
1117 generic_params (std::move (generic_params
)),
1118 function_params (std::move (function_params
)),
1119 return_type (std::move (return_type
)),
1120 where_clause (std::move (where_clause
)),
1121 function_body (std::move (function_body
)), self (std::move (self
)),
1125 // Copy constructor with clone
1126 Function (Function
const &other
)
1127 : VisItem (other
), qualifiers (other
.qualifiers
),
1128 function_name (other
.function_name
),
1129 function_params (other
.function_params
),
1130 where_clause (other
.where_clause
),
1131 function_body (other
.function_body
->clone_block_expr ()),
1132 self (other
.self
), locus (other
.locus
)
1134 // guard to prevent null dereference (always required)
1135 if (other
.return_type
!= nullptr)
1136 return_type
= other
.return_type
->clone_type ();
1138 return_type
= nullptr;
1140 generic_params
.reserve (other
.generic_params
.size ());
1141 for (const auto &e
: other
.generic_params
)
1142 generic_params
.push_back (e
->clone_generic_param ());
1145 // Overloaded assignment operator to clone
1146 Function
&operator= (Function
const &other
)
1148 VisItem::operator= (other
);
1149 function_name
= other
.function_name
;
1150 qualifiers
= other
.qualifiers
;
1151 function_params
= other
.function_params
;
1153 // guard to prevent null dereference (always required)
1154 if (other
.return_type
!= nullptr)
1155 return_type
= other
.return_type
->clone_type ();
1157 return_type
= nullptr;
1159 where_clause
= other
.where_clause
;
1160 function_body
= other
.function_body
->clone_block_expr ();
1161 locus
= other
.locus
;
1164 generic_params
.reserve (other
.generic_params
.size ());
1165 for (const auto &e
: other
.generic_params
)
1166 generic_params
.push_back (e
->clone_generic_param ());
1171 // move constructors
1172 Function (Function
&&other
) = default;
1173 Function
&operator= (Function
&&other
) = default;
1175 Location
get_locus () const override final
{ return locus
; }
1177 void accept_vis (HIRFullVisitor
&vis
) override
;
1178 void accept_vis (HIRImplVisitor
&vis
) override
;
1179 void accept_vis (HIRStmtVisitor
&vis
) override
;
1180 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1182 Analysis::NodeMapping
get_impl_mappings () const override
1184 return get_mappings ();
1187 std::vector
<FunctionParam
> &get_function_params () { return function_params
; }
1188 const std::vector
<FunctionParam
> &get_function_params () const
1190 return function_params
;
1193 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1195 return generic_params
;
1197 const std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params () const
1199 return generic_params
;
1202 // TODO: is this better? Or is a "vis_block" better?
1203 std::unique_ptr
<BlockExpr
> &get_definition ()
1205 rust_assert (function_body
!= nullptr);
1206 return function_body
;
1209 const FunctionQualifiers
&get_qualifiers () const { return qualifiers
; }
1211 Identifier
get_function_name () const { return function_name
; }
1213 // TODO: is this better? Or is a "vis_block" better?
1214 WhereClause
&get_where_clause () { return where_clause
; }
1216 bool has_return_type () const { return return_type
!= nullptr; }
1218 // TODO: is this better? Or is a "vis_block" better?
1219 std::unique_ptr
<Type
> &get_return_type ()
1221 rust_assert (has_return_type ());
1225 bool is_method () const { return !self
.is_error (); }
1227 SelfParam
&get_self_param () { return self
; }
1229 std::string
get_impl_item_name () const override final
1231 return get_function_name ();
1235 /* Use covariance to implement clone function as returning this object
1236 * rather than base */
1237 Function
*clone_item_impl () const override
{ return new Function (*this); }
1239 /* Use covariance to implement clone function as returning this object
1240 * rather than base */
1241 Function
*clone_inherent_impl_item_impl () const override
1243 return new Function (*this);
1247 // Rust type alias (i.e. typedef) HIR node
1248 class TypeAlias
: public VisItem
, public ImplItem
1250 Identifier new_type_name
;
1252 // bool has_generics;
1253 // Generics generic_params;
1254 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1256 // bool has_where_clause;
1257 WhereClause where_clause
;
1259 std::unique_ptr
<Type
> existing_type
;
1264 std::string
as_string () const override
;
1266 // Returns whether type alias has generic parameters.
1267 bool has_generics () const { return !generic_params
.empty (); }
1269 // Returns whether type alias has a where clause.
1270 bool has_where_clause () const { return !where_clause
.is_empty (); }
1272 ImplItemType
get_impl_item_type () const override final
1274 return ImplItem::ImplItemType::TYPE_ALIAS
;
1277 // Mega-constructor with all possible fields
1278 TypeAlias (Analysis::NodeMapping mappings
, Identifier new_type_name
,
1279 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1280 WhereClause where_clause
, std::unique_ptr
<Type
> existing_type
,
1281 Visibility vis
, AST::AttrVec outer_attrs
, Location locus
)
1282 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
1283 new_type_name (std::move (new_type_name
)),
1284 generic_params (std::move (generic_params
)),
1285 where_clause (std::move (where_clause
)),
1286 existing_type (std::move (existing_type
)), locus (locus
)
1290 TypeAlias (TypeAlias
const &other
)
1291 : VisItem (other
), new_type_name (other
.new_type_name
),
1292 where_clause (other
.where_clause
),
1293 existing_type (other
.existing_type
->clone_type ()), locus (other
.locus
)
1295 generic_params
.reserve (other
.generic_params
.size ());
1296 for (const auto &e
: other
.generic_params
)
1297 generic_params
.push_back (e
->clone_generic_param ());
1300 // Overloaded assignment operator to clone
1301 TypeAlias
&operator= (TypeAlias
const &other
)
1303 VisItem::operator= (other
);
1304 new_type_name
= other
.new_type_name
;
1305 where_clause
= other
.where_clause
;
1306 existing_type
= other
.existing_type
->clone_type ();
1307 locus
= other
.locus
;
1309 generic_params
.reserve (other
.generic_params
.size ());
1310 for (const auto &e
: other
.generic_params
)
1311 generic_params
.push_back (e
->clone_generic_param ());
1316 // move constructors
1317 TypeAlias (TypeAlias
&&other
) = default;
1318 TypeAlias
&operator= (TypeAlias
&&other
) = default;
1320 Location
get_locus () const override final
{ return locus
; }
1322 void accept_vis (HIRFullVisitor
&vis
) override
;
1323 void accept_vis (HIRImplVisitor
&vis
) override
;
1324 void accept_vis (HIRStmtVisitor
&vis
) override
;
1325 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1327 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1329 return generic_params
;
1331 const std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params () const
1333 return generic_params
;
1336 WhereClause
&get_where_clause () { return where_clause
; }
1338 std::unique_ptr
<Type
> &get_type_aliased ()
1340 rust_assert (existing_type
!= nullptr);
1341 return existing_type
;
1344 Identifier
get_new_type_name () const { return new_type_name
; }
1346 ItemKind
get_item_kind () const override
{ return ItemKind::TypeAlias
; }
1348 Analysis::NodeMapping
get_impl_mappings () const override
1350 return get_mappings ();
1353 std::string
get_impl_item_name () const override final
1355 return get_new_type_name ();
1359 /* Use covariance to implement clone function as returning this object
1360 * rather than base */
1361 TypeAlias
*clone_item_impl () const override
{ return new TypeAlias (*this); }
1363 /* Use covariance to implement clone function as returning this object
1364 * rather than base */
1365 TypeAlias
*clone_inherent_impl_item_impl () const override
1367 return new TypeAlias (*this);
1371 // Rust base struct declaration HIR node - abstract base class
1372 class Struct
: public VisItem
1375 // protected to enable access by derived classes - allows better as_string
1376 Identifier struct_name
;
1378 // bool has_generics;
1379 // Generics generic_params;
1380 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1382 // bool has_where_clause;
1383 WhereClause where_clause
;
1388 Identifier
get_identifier () const { return struct_name
; }
1390 // Returns whether struct has generic parameters.
1391 bool has_generics () const { return !generic_params
.empty (); }
1393 // Returns whether struct has a where clause.
1394 bool has_where_clause () const { return !where_clause
.is_empty (); }
1396 Location
get_locus () const override final
{ return locus
; }
1397 ItemKind
get_item_kind () const override
{ return ItemKind::Struct
; }
1399 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1401 return generic_params
;
1404 WhereClause
&get_where_clause () { return where_clause
; }
1407 Struct (Analysis::NodeMapping mappings
, Identifier struct_name
,
1408 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1409 WhereClause where_clause
, Visibility vis
, Location locus
,
1410 AST::AttrVec outer_attrs
= AST::AttrVec ())
1411 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
1412 struct_name (std::move (struct_name
)),
1413 generic_params (std::move (generic_params
)),
1414 where_clause (std::move (where_clause
)), locus (locus
)
1417 // Copy constructor with vector clone
1418 Struct (Struct
const &other
)
1419 : VisItem (other
), struct_name (other
.struct_name
),
1420 where_clause (other
.where_clause
), locus (other
.locus
)
1422 generic_params
.reserve (other
.generic_params
.size ());
1423 for (const auto &e
: other
.generic_params
)
1424 generic_params
.push_back (e
->clone_generic_param ());
1427 // Overloaded assignment operator with vector clone
1428 Struct
&operator= (Struct
const &other
)
1430 VisItem::operator= (other
);
1431 struct_name
= other
.struct_name
;
1432 where_clause
= other
.where_clause
;
1433 locus
= other
.locus
;
1435 generic_params
.reserve (other
.generic_params
.size ());
1436 for (const auto &e
: other
.generic_params
)
1437 generic_params
.push_back (e
->clone_generic_param ());
1442 // move constructors
1443 Struct (Struct
&&other
) = default;
1444 Struct
&operator= (Struct
&&other
) = default;
1447 // A single field in a struct
1451 // bool has_outer_attributes;
1452 AST::AttrVec outer_attrs
;
1454 // bool has_visibility;
1455 Visibility visibility
;
1457 Identifier field_name
;
1458 std::unique_ptr
<Type
> field_type
;
1460 Analysis::NodeMapping mappings
;
1464 // Returns whether struct field has any outer attributes.
1465 bool has_outer_attributes () const { return !outer_attrs
.empty (); }
1467 // Returns whether struct field has a non-private (non-default) visibility.
1468 bool has_visibility () const { return !visibility
.is_error (); }
1470 StructField (Analysis::NodeMapping mappings
, Identifier field_name
,
1471 std::unique_ptr
<Type
> field_type
, Visibility vis
, Location locus
,
1472 AST::AttrVec outer_attrs
= AST::AttrVec ())
1473 : outer_attrs (std::move (outer_attrs
)), visibility (std::move (vis
)),
1474 field_name (std::move (field_name
)), field_type (std::move (field_type
)),
1475 mappings (mappings
), locus (locus
)
1479 StructField (StructField
const &other
)
1480 : outer_attrs (other
.outer_attrs
), visibility (other
.visibility
),
1481 field_name (other
.field_name
),
1482 field_type (other
.field_type
->clone_type ()), mappings (other
.mappings
)
1485 ~StructField () = default;
1487 // Overloaded assignment operator to clone
1488 StructField
&operator= (StructField
const &other
)
1490 field_name
= other
.field_name
;
1491 field_type
= other
.field_type
->clone_type ();
1492 visibility
= other
.visibility
;
1493 outer_attrs
= other
.outer_attrs
;
1494 mappings
= other
.mappings
;
1499 // move constructors
1500 StructField (StructField
&&other
) = default;
1501 StructField
&operator= (StructField
&&other
) = default;
1503 std::string
as_string () const;
1505 Identifier
get_field_name () const { return field_name
; }
1507 std::unique_ptr
<Type
> &get_field_type ()
1509 rust_assert (field_type
!= nullptr);
1513 Analysis::NodeMapping
get_mappings () const { return mappings
; }
1515 Location
get_locus () { return locus
; }
1517 Visibility
&get_visibility () { return visibility
; }
1520 // Rust struct declaration with true struct type HIR node
1521 class StructStruct
: public Struct
1524 std::vector
<StructField
> fields
;
1527 std::string
as_string () const override
;
1529 // Mega-constructor with all possible fields
1530 StructStruct (Analysis::NodeMapping mappings
, std::vector
<StructField
> fields
,
1531 Identifier struct_name
,
1532 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1533 WhereClause where_clause
, bool is_unit
, Visibility vis
,
1534 AST::AttrVec outer_attrs
, Location locus
)
1535 : Struct (std::move (mappings
), std::move (struct_name
),
1536 std::move (generic_params
), std::move (where_clause
),
1537 std::move (vis
), locus
, std::move (outer_attrs
)),
1538 fields (std::move (fields
)), is_unit (is_unit
)
1541 // Unit struct constructor
1542 StructStruct (Analysis::NodeMapping mappings
, Identifier struct_name
,
1543 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1544 WhereClause where_clause
, Visibility vis
,
1545 AST::AttrVec outer_attrs
, Location locus
)
1546 : Struct (std::move (mappings
), std::move (struct_name
),
1547 std::move (generic_params
), std::move (where_clause
),
1548 std::move (vis
), locus
, std::move (outer_attrs
)),
1551 // TODO: can a unit struct have generic fields? assuming yes for now.
1553 /* Returns whether the struct is a unit struct - struct defined without
1554 * fields. This is important because it also means an implicit constant of its
1555 * type is defined. */
1556 bool is_unit_struct () const { return is_unit
; }
1558 void accept_vis (HIRFullVisitor
&vis
) override
;
1559 void accept_vis (HIRStmtVisitor
&vis
) override
;
1560 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1562 std::vector
<StructField
> &get_fields () { return fields
; }
1565 /* Use covariance to implement clone function as returning this object
1566 * rather than base */
1567 StructStruct
*clone_item_impl () const override
1569 return new StructStruct (*this);
1572 /* Use covariance to implement clone function as returning this object
1573 * rather than base */
1574 /*virtual StructStruct* clone_statement_impl() const override {
1575 return new StructStruct(*this);
1579 // A single field in a tuple
1583 // bool has_outer_attributes;
1584 AST::AttrVec outer_attrs
;
1586 // bool has_visibility;
1587 Visibility visibility
;
1589 std::unique_ptr
<Type
> field_type
;
1593 Analysis::NodeMapping mappings
;
1596 // Returns whether tuple field has outer attributes.
1597 bool has_outer_attributes () const { return !outer_attrs
.empty (); }
1599 /* Returns whether tuple field has a non-default visibility (i.e. a public
1601 bool has_visibility () const { return !visibility
.is_error (); }
1603 // Complete constructor
1604 TupleField (Analysis::NodeMapping mapping
, std::unique_ptr
<Type
> field_type
,
1605 Visibility vis
, Location locus
,
1606 AST::AttrVec outer_attrs
= AST::AttrVec ())
1607 : outer_attrs (std::move (outer_attrs
)), visibility (std::move (vis
)),
1608 field_type (std::move (field_type
)), locus (locus
), mappings (mapping
)
1611 // Copy constructor with clone
1612 TupleField (TupleField
const &other
)
1613 : outer_attrs (other
.outer_attrs
), visibility (other
.visibility
),
1614 field_type (other
.field_type
->clone_type ()), locus (other
.locus
),
1615 mappings (other
.mappings
)
1618 ~TupleField () = default;
1620 // Overloaded assignment operator to clone
1621 TupleField
&operator= (TupleField
const &other
)
1623 field_type
= other
.field_type
->clone_type ();
1624 visibility
= other
.visibility
;
1625 outer_attrs
= other
.outer_attrs
;
1626 locus
= other
.locus
;
1627 mappings
= other
.mappings
;
1632 // move constructors
1633 TupleField (TupleField
&&other
) = default;
1634 TupleField
&operator= (TupleField
&&other
) = default;
1636 // Returns whether tuple field is in an error state.
1637 bool is_error () const { return field_type
== nullptr; }
1639 std::string
as_string () const;
1641 Analysis::NodeMapping
get_mappings () const { return mappings
; }
1643 Location
get_locus () const { return locus
; }
1645 std::unique_ptr
<HIR::Type
> &get_field_type () { return field_type
; }
1648 // Rust tuple declared using struct keyword HIR node
1649 class TupleStruct
: public Struct
1651 std::vector
<TupleField
> fields
;
1654 std::string
as_string () const override
;
1656 // Mega-constructor with all possible fields
1657 TupleStruct (Analysis::NodeMapping mappings
, std::vector
<TupleField
> fields
,
1658 Identifier struct_name
,
1659 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1660 WhereClause where_clause
, Visibility vis
,
1661 AST::AttrVec outer_attrs
, Location locus
)
1662 : Struct (std::move (mappings
), std::move (struct_name
),
1663 std::move (generic_params
), std::move (where_clause
),
1664 std::move (vis
), locus
, std::move (outer_attrs
)),
1665 fields (std::move (fields
))
1668 void accept_vis (HIRFullVisitor
&vis
) override
;
1669 void accept_vis (HIRStmtVisitor
&vis
) override
;
1670 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1672 std::vector
<TupleField
> &get_fields () { return fields
; }
1673 const std::vector
<TupleField
> &get_fields () const { return fields
; }
1676 /* Use covariance to implement clone function as returning this object
1677 * rather than base */
1678 TupleStruct
*clone_item_impl () const override
1680 return new TupleStruct (*this);
1683 /* Use covariance to implement clone function as returning this object
1684 * rather than base */
1685 /*virtual TupleStruct* clone_statement_impl() const override {
1686 return new TupleStruct(*this);
1690 /* An item used in an "enum" tagged union - not abstract: base represents a
1691 name-only enum. Syntactically EnumItem's can have a Visibility. But not
1692 Semantically. So check there is no Visibility when lowering and make this
1693 an Item, not an VisItem. */
1694 class EnumItem
: public Item
1696 Identifier variant_name
;
1700 virtual ~EnumItem () {}
1710 EnumItem (Analysis::NodeMapping mappings
, Identifier variant_name
,
1711 AST::AttrVec outer_attrs
, Location locus
)
1712 : Item (std::move (mappings
), std::move (outer_attrs
)),
1713 variant_name (std::move (variant_name
)), locus (locus
)
1716 // Unique pointer custom clone function
1717 std::unique_ptr
<EnumItem
> clone_enum_item () const
1719 return std::unique_ptr
<EnumItem
> (clone_item_impl ());
1722 virtual std::string
as_string () const override
;
1723 virtual EnumItemKind
get_enum_item_kind () const { return Named
; };
1725 // not pure virtual as not abstract
1726 void accept_vis (HIRFullVisitor
&vis
) override
;
1727 void accept_vis (HIRStmtVisitor
&vis
) override
;
1728 // void accept_vis (HIRVisItemVisitor &vis) override;
1730 Location
get_locus () const override
{ return locus
; }
1732 Identifier
get_identifier () const { return variant_name
; }
1734 ItemKind
get_item_kind () const override
{ return ItemKind::EnumItem
; }
1737 EnumItem
*clone_item_impl () const override
{ return new EnumItem (*this); }
1740 // A tuple item used in an "enum" tagged union
1741 class EnumItemTuple
: public EnumItem
1743 // bool has_tuple_fields;
1744 std::vector
<TupleField
> tuple_fields
;
1747 // Returns whether tuple enum item has tuple fields.
1748 bool has_tuple_fields () const { return !tuple_fields
.empty (); }
1750 EnumItemKind
get_enum_item_kind () const override
1752 return EnumItemKind::Tuple
;
1755 EnumItemTuple (Analysis::NodeMapping mappings
, Identifier variant_name
,
1756 std::vector
<TupleField
> tuple_fields
, AST::AttrVec outer_attrs
,
1758 : EnumItem (std::move (mappings
), std::move (variant_name
),
1759 std::move (outer_attrs
), locus
),
1760 tuple_fields (std::move (tuple_fields
))
1763 std::string
as_string () const override
;
1765 void accept_vis (HIRFullVisitor
&vis
) override
;
1766 void accept_vis (HIRStmtVisitor
&vis
) override
;
1768 std::vector
<TupleField
> &get_tuple_fields () { return tuple_fields
; }
1771 // Clone function implementation as (not pure) virtual method
1772 EnumItemTuple
*clone_item_impl () const override
1774 return new EnumItemTuple (*this);
1778 // A struct item used in an "enum" tagged union
1779 class EnumItemStruct
: public EnumItem
1781 // bool has_struct_fields;
1782 std::vector
<StructField
> struct_fields
;
1785 // Returns whether struct enum item has struct fields.
1786 bool has_struct_fields () const { return !struct_fields
.empty (); }
1788 EnumItemKind
get_enum_item_kind () const override
1790 return EnumItemKind::Struct
;
1793 EnumItemStruct (Analysis::NodeMapping mappings
, Identifier variant_name
,
1794 std::vector
<StructField
> struct_fields
,
1795 AST::AttrVec outer_attrs
, Location locus
)
1796 : EnumItem (std::move (mappings
), std::move (variant_name
),
1797 std::move (outer_attrs
), locus
),
1798 struct_fields (std::move (struct_fields
))
1801 std::string
as_string () const override
;
1803 void accept_vis (HIRFullVisitor
&vis
) override
;
1804 void accept_vis (HIRStmtVisitor
&vis
) override
;
1806 std::vector
<StructField
> &get_struct_fields () { return struct_fields
; }
1809 // Clone function implementation as (not pure) virtual method
1810 EnumItemStruct
*clone_item_impl () const override
1812 return new EnumItemStruct (*this);
1816 // A discriminant (numbered enum) item used in an "enum" tagged union
1817 class EnumItemDiscriminant
: public EnumItem
1819 std::unique_ptr
<Expr
> expression
;
1822 EnumItemDiscriminant (Analysis::NodeMapping mappings
, Identifier variant_name
,
1823 std::unique_ptr
<Expr
> expr
, AST::AttrVec outer_attrs
,
1825 : EnumItem (std::move (mappings
), std::move (variant_name
),
1826 std::move (outer_attrs
), locus
),
1827 expression (std::move (expr
))
1830 // Copy constructor with clone
1831 EnumItemDiscriminant (EnumItemDiscriminant
const &other
)
1832 : EnumItem (other
), expression (other
.expression
->clone_expr ())
1835 // Overloaded assignment operator to clone
1836 EnumItemDiscriminant
&operator= (EnumItemDiscriminant
const &other
)
1838 EnumItem::operator= (other
);
1839 expression
= other
.expression
->clone_expr ();
1840 // variant_name = other.variant_name;
1841 // outer_attrs = other.outer_attrs;
1846 // move constructors
1847 EnumItemDiscriminant (EnumItemDiscriminant
&&other
) = default;
1848 EnumItemDiscriminant
&operator= (EnumItemDiscriminant
&&other
) = default;
1850 EnumItemKind
get_enum_item_kind () const override
1852 return EnumItemKind::Discriminant
;
1855 std::string
as_string () const override
;
1857 void accept_vis (HIRFullVisitor
&vis
) override
;
1858 void accept_vis (HIRStmtVisitor
&vis
) override
;
1860 std::unique_ptr
<Expr
> &get_discriminant_expression () { return expression
; }
1863 // Clone function implementation as (not pure) virtual method
1864 EnumItemDiscriminant
*clone_item_impl () const override
1866 return new EnumItemDiscriminant (*this);
1870 // HIR node for Rust "enum" - tagged union
1871 class Enum
: public VisItem
1873 Identifier enum_name
;
1875 // bool has_generics;
1876 // Generics generic_params;
1877 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1879 // bool has_where_clause;
1880 WhereClause where_clause
;
1882 std::vector
<std::unique_ptr
<EnumItem
>> items
;
1887 std::string
as_string () const override
;
1889 // Returns whether "enum" has generic parameters.
1890 bool has_generics () const { return !generic_params
.empty (); }
1892 // Returns whether "enum" has a where clause.
1893 bool has_where_clause () const { return !where_clause
.is_empty (); }
1895 /* Returns whether enum is a "zero-variant" (no possible variant) enum,
1896 * which cannot be instantiated. */
1897 bool is_zero_variant () const { return items
.empty (); }
1900 Enum (Analysis::NodeMapping mappings
, Identifier enum_name
, Visibility vis
,
1901 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1902 WhereClause where_clause
, std::vector
<std::unique_ptr
<EnumItem
>> items
,
1903 AST::AttrVec outer_attrs
, Location locus
)
1904 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
1905 enum_name (std::move (enum_name
)),
1906 generic_params (std::move (generic_params
)),
1907 where_clause (std::move (where_clause
)), items (std::move (items
)),
1911 // TODO: constructor with less arguments
1913 // Copy constructor with vector clone
1914 Enum (Enum
const &other
)
1915 : VisItem (other
), enum_name (other
.enum_name
),
1916 where_clause (other
.where_clause
), locus (other
.locus
)
1918 generic_params
.reserve (other
.generic_params
.size ());
1919 for (const auto &e
: other
.generic_params
)
1920 generic_params
.push_back (e
->clone_generic_param ());
1922 items
.reserve (other
.items
.size ());
1923 for (const auto &e
: other
.items
)
1924 items
.push_back (e
->clone_enum_item ());
1927 // Overloaded assignment operator with vector clone
1928 Enum
&operator= (Enum
const &other
)
1930 VisItem::operator= (other
);
1931 enum_name
= other
.enum_name
;
1932 where_clause
= other
.where_clause
;
1933 locus
= other
.locus
;
1935 generic_params
.reserve (other
.generic_params
.size ());
1936 for (const auto &e
: other
.generic_params
)
1937 generic_params
.push_back (e
->clone_generic_param ());
1939 items
.reserve (other
.items
.size ());
1940 for (const auto &e
: other
.items
)
1941 items
.push_back (e
->clone_enum_item ());
1946 // Move constructors
1947 Enum (Enum
&&other
) = default;
1948 Enum
&operator= (Enum
&&other
) = default;
1950 Location
get_locus () const override final
{ return locus
; }
1952 void accept_vis (HIRFullVisitor
&vis
) override
;
1953 void accept_vis (HIRStmtVisitor
&vis
) override
;
1954 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1956 Identifier
get_identifier () const { return enum_name
; }
1957 ItemKind
get_item_kind () const override
{ return ItemKind::Enum
; }
1959 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1961 return generic_params
;
1964 const std::vector
<std::unique_ptr
<EnumItem
>> &get_variants () const
1970 /* Use covariance to implement clone function as returning this object
1971 * rather than base */
1972 Enum
*clone_item_impl () const override
{ return new Enum (*this); }
1974 /* Use covariance to implement clone function as returning this object
1975 * rather than base */
1976 /*virtual Enum* clone_statement_impl() const override {
1977 return new Enum(*this);
1981 // Rust untagged union used for C compat HIR node
1982 class Union
: public VisItem
1984 Identifier union_name
;
1986 // bool has_generics;
1987 // Generics generic_params;
1988 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1990 // bool has_where_clause;
1991 WhereClause where_clause
;
1993 std::vector
<StructField
> variants
;
1998 std::string
as_string () const override
;
2000 // Returns whether union has generic params.
2001 bool has_generics () const { return !generic_params
.empty (); }
2003 // Returns whether union has where clause.
2004 bool has_where_clause () const { return !where_clause
.is_empty (); }
2006 Union (Analysis::NodeMapping mappings
, Identifier union_name
, Visibility vis
,
2007 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
2008 WhereClause where_clause
, std::vector
<StructField
> variants
,
2009 AST::AttrVec outer_attrs
, Location locus
)
2010 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
2011 union_name (std::move (union_name
)),
2012 generic_params (std::move (generic_params
)),
2013 where_clause (std::move (where_clause
)), variants (std::move (variants
)),
2017 // copy constructor with vector clone
2018 Union (Union
const &other
)
2019 : VisItem (other
), union_name (other
.union_name
),
2020 where_clause (other
.where_clause
), variants (other
.variants
),
2023 generic_params
.reserve (other
.generic_params
.size ());
2024 for (const auto &e
: other
.generic_params
)
2025 generic_params
.push_back (e
->clone_generic_param ());
2028 // overloaded assignment operator with vector clone
2029 Union
&operator= (Union
const &other
)
2031 VisItem::operator= (other
);
2032 union_name
= other
.union_name
;
2033 where_clause
= other
.where_clause
;
2034 variants
= other
.variants
;
2035 locus
= other
.locus
;
2037 generic_params
.reserve (other
.generic_params
.size ());
2038 for (const auto &e
: other
.generic_params
)
2039 generic_params
.push_back (e
->clone_generic_param ());
2044 // move constructors
2045 Union (Union
&&other
) = default;
2046 Union
&operator= (Union
&&other
) = default;
2048 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2050 return generic_params
;
2053 Identifier
get_identifier () const { return union_name
; }
2055 Location
get_locus () const override final
{ return locus
; }
2057 void accept_vis (HIRFullVisitor
&vis
) override
;
2058 void accept_vis (HIRStmtVisitor
&vis
) override
;
2059 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2061 std::vector
<StructField
> &get_variants () { return variants
; }
2063 WhereClause
&get_where_clause () { return where_clause
; }
2065 ItemKind
get_item_kind () const override
{ return ItemKind::Union
; }
2068 /* Use covariance to implement clone function as returning this object
2069 * rather than base */
2070 Union
*clone_item_impl () const override
{ return new Union (*this); }
2073 class ConstantItem
: public VisItem
, public ImplItem
2075 Identifier identifier
;
2076 std::unique_ptr
<Type
> type
;
2077 std::unique_ptr
<Expr
> const_expr
;
2081 std::string
as_string () const override
;
2083 ConstantItem (Analysis::NodeMapping mappings
, Identifier ident
,
2084 Visibility vis
, std::unique_ptr
<Type
> type
,
2085 std::unique_ptr
<Expr
> const_expr
, AST::AttrVec outer_attrs
,
2087 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
2088 identifier (std::move (ident
)), type (std::move (type
)),
2089 const_expr (std::move (const_expr
)), locus (locus
)
2092 ConstantItem (ConstantItem
const &other
)
2093 : VisItem (other
), identifier (other
.identifier
),
2094 type (other
.type
->clone_type ()),
2095 const_expr (other
.const_expr
->clone_expr ()), locus (other
.locus
)
2098 // Overload assignment operator to clone
2099 ConstantItem
&operator= (ConstantItem
const &other
)
2101 VisItem::operator= (other
);
2102 identifier
= other
.identifier
;
2103 type
= other
.type
->clone_type ();
2104 const_expr
= other
.const_expr
->clone_expr ();
2105 locus
= other
.locus
;
2110 // move constructors
2111 ConstantItem (ConstantItem
&&other
) = default;
2112 ConstantItem
&operator= (ConstantItem
&&other
) = default;
2114 // Returns whether constant item is an "unnamed" (wildcard underscore used
2115 // as identifier) constant.
2116 bool is_unnamed () const { return identifier
== std::string ("_"); }
2118 Location
get_locus () const override final
{ return locus
; }
2120 void accept_vis (HIRFullVisitor
&vis
) override
;
2121 void accept_vis (HIRStmtVisitor
&vis
) override
;
2122 void accept_vis (HIRImplVisitor
&vis
) override
;
2123 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2125 Type
*get_type () { return type
.get (); }
2127 Expr
*get_expr () { return const_expr
.get (); }
2129 std::string
get_identifier () const { return identifier
; }
2131 Analysis::NodeMapping
get_impl_mappings () const override
2133 return get_mappings ();
2136 ImplItemType
get_impl_item_type () const override final
2138 return ImplItem::ImplItemType::CONSTANT
;
2141 ItemKind
get_item_kind () const override
{ return ItemKind::Constant
; }
2143 std::string
get_impl_item_name () const override final
2145 return get_identifier ();
2149 /* Use covariance to implement clone function as returning this object
2150 * rather than base */
2151 ConstantItem
*clone_item_impl () const override
2153 return new ConstantItem (*this);
2156 /* Use covariance to implement clone function as returning this object
2157 * rather than base */
2158 ConstantItem
*clone_inherent_impl_item_impl () const override
2160 return new ConstantItem (*this);
2164 /* Static item HIR node - items within module scope with fixed storage
2166 class StaticItem
: public VisItem
2170 std::unique_ptr
<Type
> type
;
2171 std::unique_ptr
<Expr
> expr
;
2175 std::string
as_string () const override
;
2177 StaticItem (Analysis::NodeMapping mappings
, Identifier name
, Mutability mut
,
2178 std::unique_ptr
<Type
> type
, std::unique_ptr
<Expr
> expr
,
2179 Visibility vis
, AST::AttrVec outer_attrs
, Location locus
)
2180 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
2181 mut (mut
), name (std::move (name
)), type (std::move (type
)),
2182 expr (std::move (expr
)), locus (locus
)
2185 // Copy constructor with clone
2186 StaticItem (StaticItem
const &other
)
2187 : VisItem (other
), mut (other
.mut
), name (other
.name
),
2188 type (other
.type
->clone_type ()), expr (other
.expr
->clone_expr ()),
2192 // Overloaded assignment operator to clone
2193 StaticItem
&operator= (StaticItem
const &other
)
2195 VisItem::operator= (other
);
2198 type
= other
.type
->clone_type ();
2199 expr
= other
.expr
->clone_expr ();
2200 locus
= other
.locus
;
2205 // move constructors
2206 StaticItem (StaticItem
&&other
) = default;
2207 StaticItem
&operator= (StaticItem
&&other
) = default;
2209 Location
get_locus () const override final
{ return locus
; }
2211 void accept_vis (HIRFullVisitor
&vis
) override
;
2212 void accept_vis (HIRStmtVisitor
&vis
) override
;
2213 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2215 Identifier
get_identifier () const { return name
; }
2217 Mutability
get_mut () const { return mut
; }
2219 bool is_mut () const { return mut
== Mutability::Mut
; }
2221 Expr
*get_expr () { return expr
.get (); }
2223 Type
*get_type () { return type
.get (); }
2225 ItemKind
get_item_kind () const override
{ return ItemKind::Static
; }
2228 StaticItem
*clone_item_impl () const override
2230 return new StaticItem (*this);
2234 // Function declaration in traits
2235 struct TraitFunctionDecl
2238 FunctionQualifiers qualifiers
;
2239 Identifier function_name
;
2240 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
2241 std::vector
<FunctionParam
> function_params
;
2242 std::unique_ptr
<Type
> return_type
;
2243 WhereClause where_clause
;
2248 TraitFunctionDecl (Identifier function_name
, FunctionQualifiers qualifiers
,
2249 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
2250 SelfParam self
, std::vector
<FunctionParam
> function_params
,
2251 std::unique_ptr
<Type
> return_type
,
2252 WhereClause where_clause
)
2253 : qualifiers (std::move (qualifiers
)),
2254 function_name (std::move (function_name
)),
2255 generic_params (std::move (generic_params
)),
2256 function_params (std::move (function_params
)),
2257 return_type (std::move (return_type
)),
2258 where_clause (std::move (where_clause
)), self (std::move (self
))
2261 // Copy constructor with clone
2262 TraitFunctionDecl (TraitFunctionDecl
const &other
)
2263 : qualifiers (other
.qualifiers
), function_name (other
.function_name
),
2264 function_params (other
.function_params
),
2265 return_type (other
.return_type
->clone_type ()),
2266 where_clause (other
.where_clause
), self (other
.self
)
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 ());
2273 ~TraitFunctionDecl () = default;
2275 // Overloaded assignment operator with clone
2276 TraitFunctionDecl
&operator= (TraitFunctionDecl
const &other
)
2278 function_name
= other
.function_name
;
2279 qualifiers
= other
.qualifiers
;
2280 function_params
= other
.function_params
;
2281 return_type
= other
.return_type
->clone_type ();
2282 where_clause
= other
.where_clause
;
2285 generic_params
.reserve (other
.generic_params
.size ());
2286 for (const auto &e
: other
.generic_params
)
2287 generic_params
.push_back (e
->clone_generic_param ());
2292 // move constructors
2293 TraitFunctionDecl (TraitFunctionDecl
&&other
) = default;
2294 TraitFunctionDecl
&operator= (TraitFunctionDecl
&&other
) = default;
2296 std::string
as_string () const;
2298 // Returns whether function decl has generic parameters.
2299 bool has_generics () const { return !generic_params
.empty (); }
2301 // Returns whether function decl has regular parameters.
2302 bool has_params () const { return !function_params
.empty (); }
2304 // Returns whether function has return type (otherwise is void).
2305 bool has_return_type () const { return return_type
!= nullptr; }
2307 // Returns whether function has a where clause.
2308 bool has_where_clause () const { return !where_clause
.is_empty (); }
2310 bool is_method () const { return !self
.is_error (); }
2312 SelfParam
&get_self () { return self
; }
2314 Identifier
get_function_name () const { return function_name
; }
2316 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2318 return generic_params
;
2321 std::unique_ptr
<Type
> &get_return_type ()
2323 rust_assert (has_return_type ());
2327 std::vector
<FunctionParam
> &get_function_params () { return function_params
; }
2329 const FunctionQualifiers
&get_qualifiers () const { return qualifiers
; }
2332 // Actual trait item function declaration within traits
2333 class TraitItemFunc
: public TraitItem
2335 AST::AttrVec outer_attrs
;
2336 TraitFunctionDecl decl
;
2337 std::unique_ptr
<BlockExpr
> block_expr
;
2341 // Returns whether function has a definition or is just a declaration.
2342 bool has_definition () const { return block_expr
!= nullptr; }
2344 TraitItemFunc (Analysis::NodeMapping mappings
, TraitFunctionDecl decl
,
2345 std::unique_ptr
<BlockExpr
> block_expr
,
2346 AST::AttrVec outer_attrs
, Location locus
)
2347 : TraitItem (mappings
), outer_attrs (std::move (outer_attrs
)),
2348 decl (std::move (decl
)), block_expr (std::move (block_expr
)),
2352 // Copy constructor with clone
2353 TraitItemFunc (TraitItemFunc
const &other
)
2354 : TraitItem (other
.mappings
), outer_attrs (other
.outer_attrs
),
2355 decl (other
.decl
), locus (other
.locus
)
2357 if (other
.block_expr
!= nullptr)
2358 block_expr
= other
.block_expr
->clone_block_expr ();
2361 // Overloaded assignment operator to clone
2362 TraitItemFunc
&operator= (TraitItemFunc
const &other
)
2364 TraitItem::operator= (other
);
2365 outer_attrs
= other
.outer_attrs
;
2367 locus
= other
.locus
;
2368 mappings
= other
.mappings
;
2369 if (other
.block_expr
!= nullptr)
2370 block_expr
= other
.block_expr
->clone_block_expr ();
2375 // move constructors
2376 TraitItemFunc (TraitItemFunc
&&other
) = default;
2377 TraitItemFunc
&operator= (TraitItemFunc
&&other
) = default;
2379 std::string
as_string () const override
;
2381 Location
get_locus () const { return locus
; }
2383 void accept_vis (HIRFullVisitor
&vis
) override
;
2384 void accept_vis (HIRTraitItemVisitor
&vis
) override
;
2386 TraitFunctionDecl
&get_decl () { return decl
; }
2388 const TraitFunctionDecl
&get_decl () const { return decl
; }
2390 bool has_block_defined () const { return block_expr
!= nullptr; }
2392 std::unique_ptr
<BlockExpr
> &get_block_expr ()
2394 rust_assert (has_block_defined ());
2398 const std::string
trait_identifier () const override final
2400 return decl
.get_function_name ();
2403 TraitItemKind
get_item_kind () const override final
2405 return TraitItemKind::FUNC
;
2408 AST::AttrVec
&get_outer_attrs () override final
{ return outer_attrs
; }
2409 const AST::AttrVec
&get_outer_attrs () const override final
2414 Location
get_trait_locus () const override
{ return get_locus (); }
2417 // Clone function implementation as (not pure) virtual method
2418 TraitItemFunc
*clone_trait_item_impl () const override
2420 return new TraitItemFunc (*this);
2424 // Constant item within traits
2425 class TraitItemConst
: public TraitItem
2427 AST::AttrVec outer_attrs
;
2429 std::unique_ptr
<Type
> type
;
2430 std::unique_ptr
<Expr
> expr
;
2434 // Whether the constant item has an associated expression.
2435 bool has_expression () const { return expr
!= nullptr; }
2437 TraitItemConst (Analysis::NodeMapping mappings
, Identifier name
,
2438 std::unique_ptr
<Type
> type
, std::unique_ptr
<Expr
> expr
,
2439 AST::AttrVec outer_attrs
, Location locus
)
2440 : TraitItem (mappings
), outer_attrs (std::move (outer_attrs
)),
2441 name (std::move (name
)), type (std::move (type
)), expr (std::move (expr
)),
2445 // Copy constructor with clones
2446 TraitItemConst (TraitItemConst
const &other
)
2447 : TraitItem (other
.mappings
), outer_attrs (other
.outer_attrs
),
2448 name (other
.name
), type (other
.type
->clone_type ()),
2449 expr (other
.expr
->clone_expr ()), locus (other
.locus
)
2452 // Overloaded assignment operator to clone
2453 TraitItemConst
&operator= (TraitItemConst
const &other
)
2455 TraitItem::operator= (other
);
2456 outer_attrs
= other
.outer_attrs
;
2458 type
= other
.type
->clone_type ();
2459 expr
= other
.expr
->clone_expr ();
2460 locus
= other
.locus
;
2461 mappings
= other
.mappings
;
2466 // move constructors
2467 TraitItemConst (TraitItemConst
&&other
) = default;
2468 TraitItemConst
&operator= (TraitItemConst
&&other
) = default;
2470 std::string
as_string () const override
;
2472 Location
get_locus () const { return locus
; }
2474 void accept_vis (HIRFullVisitor
&vis
) override
;
2475 void accept_vis (HIRTraitItemVisitor
&vis
) override
;
2477 Identifier
get_name () const { return name
; }
2479 bool has_expr () const { return expr
!= nullptr; }
2481 std::unique_ptr
<Type
> &get_type () { return type
; }
2483 std::unique_ptr
<Expr
> &get_expr ()
2485 rust_assert (has_expr ());
2489 const std::string
trait_identifier () const override final
{ return name
; }
2491 TraitItemKind
get_item_kind () const override final
2493 return TraitItemKind::CONST
;
2496 AST::AttrVec
&get_outer_attrs () override final
{ return outer_attrs
; }
2497 const AST::AttrVec
&get_outer_attrs () const override final
2502 Location
get_trait_locus () const override
{ return get_locus (); }
2505 // Clone function implementation as (not pure) virtual method
2506 TraitItemConst
*clone_trait_item_impl () const override
2508 return new TraitItemConst (*this);
2512 // Type items within traits
2513 class TraitItemType
: public TraitItem
2515 AST::AttrVec outer_attrs
;
2518 std::vector
<std::unique_ptr
<TypeParamBound
>>
2519 type_param_bounds
; // inlined form
2523 // Returns whether trait item type has type param bounds.
2524 bool has_type_param_bounds () const { return !type_param_bounds
.empty (); }
2526 TraitItemType (Analysis::NodeMapping mappings
, Identifier name
,
2527 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
,
2528 AST::AttrVec outer_attrs
, Location locus
)
2529 : TraitItem (mappings
), outer_attrs (std::move (outer_attrs
)),
2530 name (std::move (name
)),
2531 type_param_bounds (std::move (type_param_bounds
)), locus (locus
)
2534 // Copy constructor with vector clone
2535 TraitItemType (TraitItemType
const &other
)
2536 : TraitItem (other
.mappings
), outer_attrs (other
.outer_attrs
),
2537 name (other
.name
), locus (other
.locus
)
2539 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
2540 for (const auto &e
: other
.type_param_bounds
)
2541 type_param_bounds
.push_back (e
->clone_type_param_bound ());
2544 // Overloaded assignment operator with vector clone
2545 TraitItemType
&operator= (TraitItemType
const &other
)
2547 TraitItem::operator= (other
);
2548 outer_attrs
= other
.outer_attrs
;
2550 locus
= other
.locus
;
2551 mappings
= other
.mappings
;
2553 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
2554 for (const auto &e
: other
.type_param_bounds
)
2555 type_param_bounds
.push_back (e
->clone_type_param_bound ());
2560 // default move constructors
2561 TraitItemType (TraitItemType
&&other
) = default;
2562 TraitItemType
&operator= (TraitItemType
&&other
) = default;
2564 std::string
as_string () const override
;
2566 Location
get_locus () const { return locus
; }
2568 void accept_vis (HIRFullVisitor
&vis
) override
;
2569 void accept_vis (HIRTraitItemVisitor
&vis
) override
;
2571 Identifier
get_name () const { return name
; }
2573 std::vector
<std::unique_ptr
<TypeParamBound
>> &get_type_param_bounds ()
2575 return type_param_bounds
;
2578 const std::string
trait_identifier () const override final
{ return name
; }
2580 TraitItemKind
get_item_kind () const override final
2582 return TraitItemKind::TYPE
;
2585 AST::AttrVec
&get_outer_attrs () override final
{ return outer_attrs
; }
2586 const AST::AttrVec
&get_outer_attrs () const override final
2591 Location
get_trait_locus () const override
{ return get_locus (); }
2594 // Clone function implementation as (not pure) virtual method
2595 TraitItemType
*clone_trait_item_impl () const override
2597 return new TraitItemType (*this);
2601 // Rust trait item declaration HIR node
2602 class Trait
: public VisItem
2606 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
2607 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
;
2608 WhereClause where_clause
;
2609 std::vector
<std::unique_ptr
<TraitItem
>> trait_items
;
2613 std::string
as_string () const override
;
2615 // Returns whether trait has generic parameters.
2616 bool has_generics () const { return !generic_params
.empty (); }
2618 // Returns whether trait has type parameter bounds.
2619 bool has_type_param_bounds () const { return !type_param_bounds
.empty (); }
2621 // Returns whether trait has where clause.
2622 bool has_where_clause () const { return !where_clause
.is_empty (); }
2624 // Returns whether trait has trait items.
2625 bool has_trait_items () const { return !trait_items
.empty (); }
2627 std::vector
<std::unique_ptr
<TraitItem
>> &get_trait_items ()
2632 Identifier
get_name () const { return name
; }
2635 Trait (Analysis::NodeMapping mappings
, Identifier name
, Unsafety unsafety
,
2636 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
2637 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
,
2638 WhereClause where_clause
,
2639 std::vector
<std::unique_ptr
<TraitItem
>> trait_items
, Visibility vis
,
2640 AST::AttrVec outer_attrs
, Location locus
)
2641 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
2642 unsafety (unsafety
), name (std::move (name
)),
2643 generic_params (std::move (generic_params
)),
2644 type_param_bounds (std::move (type_param_bounds
)),
2645 where_clause (std::move (where_clause
)),
2646 trait_items (std::move (trait_items
)), locus (locus
)
2649 // Copy constructor with vector clone
2650 Trait (Trait
const &other
)
2651 : VisItem (other
), unsafety (other
.unsafety
), name (other
.name
),
2652 where_clause (other
.where_clause
), locus (other
.locus
)
2654 generic_params
.reserve (other
.generic_params
.size ());
2655 for (const auto &e
: other
.generic_params
)
2656 generic_params
.push_back (e
->clone_generic_param ());
2658 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
2659 for (const auto &e
: other
.type_param_bounds
)
2660 type_param_bounds
.push_back (e
->clone_type_param_bound ());
2662 trait_items
.reserve (other
.trait_items
.size ());
2663 for (const auto &e
: other
.trait_items
)
2664 trait_items
.push_back (e
->clone_trait_item ());
2667 // Overloaded assignment operator with vector clone
2668 Trait
&operator= (Trait
const &other
)
2670 VisItem::operator= (other
);
2672 unsafety
= other
.unsafety
;
2673 where_clause
= other
.where_clause
;
2674 locus
= other
.locus
;
2676 generic_params
.reserve (other
.generic_params
.size ());
2677 for (const auto &e
: other
.generic_params
)
2678 generic_params
.push_back (e
->clone_generic_param ());
2680 type_param_bounds
.reserve (other
.type_param_bounds
.size ());
2681 for (const auto &e
: other
.type_param_bounds
)
2682 type_param_bounds
.push_back (e
->clone_type_param_bound ());
2684 trait_items
.reserve (other
.trait_items
.size ());
2685 for (const auto &e
: other
.trait_items
)
2686 trait_items
.push_back (e
->clone_trait_item ());
2691 // default move constructors
2692 Trait (Trait
&&other
) = default;
2693 Trait
&operator= (Trait
&&other
) = default;
2695 Location
get_locus () const override final
{ return locus
; }
2697 void accept_vis (HIRFullVisitor
&vis
) override
;
2698 void accept_vis (HIRStmtVisitor
&vis
) override
;
2699 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2701 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2703 return generic_params
;
2706 const std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params () const
2708 return generic_params
;
2711 std::vector
<std::unique_ptr
<TypeParamBound
>> &get_type_param_bounds ()
2713 return type_param_bounds
;
2716 const std::vector
<std::unique_ptr
<TypeParamBound
>> &
2717 get_type_param_bounds () const
2719 return type_param_bounds
;
2722 ItemKind
get_item_kind () const override
{ return ItemKind::Trait
; }
2725 /* Use covariance to implement clone function as returning this object
2726 * rather than base */
2727 Trait
*clone_item_impl () const override
{ return new Trait (*this); }
2730 class ImplBlock
: public VisItem
2732 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
2733 std::unique_ptr
<Type
> impl_type
;
2734 std::unique_ptr
<TypePath
> trait_ref
;
2735 WhereClause where_clause
;
2737 AST::AttrVec inner_attrs
;
2739 std::vector
<std::unique_ptr
<ImplItem
>> impl_items
;
2742 ImplBlock (Analysis::NodeMapping mappings
,
2743 std::vector
<std::unique_ptr
<ImplItem
>> impl_items
,
2744 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
2745 std::unique_ptr
<Type
> impl_type
,
2746 std::unique_ptr
<TypePath
> trait_ref
, WhereClause where_clause
,
2747 Polarity polarity
, Visibility vis
, AST::AttrVec inner_attrs
,
2748 AST::AttrVec outer_attrs
, Location locus
)
2749 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
2750 generic_params (std::move (generic_params
)),
2751 impl_type (std::move (impl_type
)), trait_ref (std::move (trait_ref
)),
2752 where_clause (std::move (where_clause
)), polarity (polarity
),
2753 inner_attrs (std::move (inner_attrs
)), locus (locus
),
2754 impl_items (std::move (impl_items
))
2757 ImplBlock (ImplBlock
const &other
)
2758 : VisItem (other
), impl_type (other
.impl_type
->clone_type ()),
2759 where_clause (other
.where_clause
), polarity (other
.polarity
),
2760 inner_attrs (other
.inner_attrs
), locus (other
.locus
)
2762 generic_params
.reserve (other
.generic_params
.size ());
2763 for (const auto &e
: other
.generic_params
)
2764 generic_params
.push_back (e
->clone_generic_param ());
2766 impl_items
.reserve (other
.impl_items
.size ());
2767 for (const auto &e
: other
.impl_items
)
2768 impl_items
.push_back (e
->clone_inherent_impl_item ());
2771 ImplBlock
&operator= (ImplBlock
const &other
)
2773 VisItem::operator= (other
);
2774 impl_type
= other
.impl_type
->clone_type ();
2775 where_clause
= other
.where_clause
;
2776 polarity
= other
.polarity
;
2777 inner_attrs
= other
.inner_attrs
;
2778 locus
= other
.locus
;
2780 generic_params
.reserve (other
.generic_params
.size ());
2781 for (const auto &e
: other
.generic_params
)
2782 generic_params
.push_back (e
->clone_generic_param ());
2784 impl_items
.reserve (other
.impl_items
.size ());
2785 for (const auto &e
: other
.impl_items
)
2786 impl_items
.push_back (e
->clone_inherent_impl_item ());
2791 ImplBlock (ImplBlock
&&other
) = default;
2792 ImplBlock
&operator= (ImplBlock
&&other
) = default;
2794 std::string
as_string () const override
;
2796 // Returns whether inherent impl block has inherent impl items.
2797 bool has_impl_items () const { return !impl_items
.empty (); }
2799 void accept_vis (HIRFullVisitor
&vis
) override
;
2800 void accept_vis (HIRStmtVisitor
&vis
) override
;
2801 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2803 std::vector
<std::unique_ptr
<ImplItem
>> &get_impl_items ()
2808 const std::vector
<std::unique_ptr
<ImplItem
>> &get_impl_items () const
2813 // Returns whether impl has generic parameters.
2814 bool has_generics () const { return !generic_params
.empty (); }
2816 // Returns whether impl has where clause.
2817 bool has_where_clause () const { return !where_clause
.is_empty (); }
2819 // Returns the polarity of the impl.
2820 Polarity
get_polarity () const { return polarity
; }
2822 // Returns whether impl has inner attributes.
2823 bool has_inner_attrs () const { return !inner_attrs
.empty (); }
2825 Location
get_locus () const override final
{ return locus
; }
2827 std::unique_ptr
<Type
> &get_type () { return impl_type
; };
2829 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2831 return generic_params
;
2834 bool has_trait_ref () const { return trait_ref
!= nullptr; }
2836 std::unique_ptr
<TypePath
> &get_trait_ref ()
2838 rust_assert (has_trait_ref ());
2842 WhereClause
&get_where_clause () { return where_clause
; }
2844 ItemKind
get_item_kind () const override
{ return ItemKind::Impl
; }
2847 ImplBlock
*clone_item_impl () const override
{ return new ImplBlock (*this); }
2850 // Abstract base class for an item used inside an extern block
2851 class ExternalItem
: public Node
2853 Analysis::NodeMapping mappings
;
2854 AST::AttrVec outer_attrs
;
2855 Visibility visibility
;
2856 Identifier item_name
;
2860 enum class ExternKind
2866 virtual ~ExternalItem () {}
2868 BaseKind
get_hir_kind () override final
{ return EXTERNAL
; }
2870 virtual ExternKind
get_extern_kind () = 0;
2872 // Returns whether item has outer attributes.
2873 bool has_outer_attrs () const { return !outer_attrs
.empty (); }
2875 // Returns whether item has non-default visibility.
2876 bool has_visibility () const { return !visibility
.is_error (); }
2878 // Unique pointer custom clone function
2879 std::unique_ptr
<ExternalItem
> clone_external_item () const
2881 return std::unique_ptr
<ExternalItem
> (clone_external_item_impl ());
2884 virtual std::string
as_string () const;
2886 Location
get_locus () const { return locus
; }
2888 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
2889 virtual void accept_vis (HIRExternalItemVisitor
&vis
) = 0;
2891 Analysis::NodeMapping
get_mappings () const { return mappings
; }
2893 Identifier
get_item_name () const { return item_name
; }
2895 AST::AttrVec
&get_outer_attrs () { return outer_attrs
; }
2898 ExternalItem (Analysis::NodeMapping mappings
, Identifier item_name
,
2899 Visibility vis
, AST::AttrVec outer_attrs
, Location locus
)
2900 : mappings (mappings
), outer_attrs (std::move (outer_attrs
)),
2901 visibility (std::move (vis
)), item_name (std::move (item_name
)),
2906 ExternalItem (ExternalItem
const &other
)
2907 : mappings (other
.mappings
), outer_attrs (other
.outer_attrs
),
2908 visibility (other
.visibility
), item_name (other
.item_name
),
2912 // Overloaded assignment operator to clone
2913 ExternalItem
&operator= (ExternalItem
const &other
)
2915 mappings
= other
.mappings
;
2916 item_name
= other
.item_name
;
2917 visibility
= other
.visibility
;
2918 outer_attrs
= other
.outer_attrs
;
2919 locus
= other
.locus
;
2924 // move constructors
2925 ExternalItem (ExternalItem
&&other
) = default;
2926 ExternalItem
&operator= (ExternalItem
&&other
) = default;
2928 // Clone function implementation as pure virtual method
2929 virtual ExternalItem
*clone_external_item_impl () const = 0;
2932 // A static item used in an extern block
2933 class ExternalStaticItem
: public ExternalItem
2936 std::unique_ptr
<Type
> item_type
;
2939 ExternalStaticItem (Analysis::NodeMapping mappings
, Identifier item_name
,
2940 std::unique_ptr
<Type
> item_type
, Mutability mut
,
2941 Visibility vis
, AST::AttrVec outer_attrs
, Location locus
)
2942 : ExternalItem (std::move (mappings
), std::move (item_name
),
2943 std::move (vis
), std::move (outer_attrs
), locus
),
2944 mut (mut
), item_type (std::move (item_type
))
2948 ExternalStaticItem (ExternalStaticItem
const &other
)
2949 : ExternalItem (other
), mut (other
.mut
),
2950 item_type (other
.item_type
->clone_type ())
2953 // Overloaded assignment operator to clone
2954 ExternalStaticItem
&operator= (ExternalStaticItem
const &other
)
2956 ExternalItem::operator= (other
);
2957 item_type
= other
.item_type
->clone_type ();
2963 // move constructors
2964 ExternalStaticItem (ExternalStaticItem
&&other
) = default;
2965 ExternalStaticItem
&operator= (ExternalStaticItem
&&other
) = default;
2967 std::string
as_string () const override
;
2969 void accept_vis (HIRFullVisitor
&vis
) override
;
2970 void accept_vis (HIRExternalItemVisitor
&vis
) override
;
2972 bool is_mut () const { return mut
== Mutability::Mut
; }
2974 Mutability
get_mut () { return mut
; }
2976 std::unique_ptr
<Type
> &get_item_type () { return item_type
; }
2978 ExternKind
get_extern_kind () override
{ return ExternKind::Static
; }
2981 /* Use covariance to implement clone function as returning this object
2982 * rather than base */
2983 ExternalStaticItem
*clone_external_item_impl () const override
2985 return new ExternalStaticItem (*this);
2989 // A named function parameter used in external functions
2990 struct NamedFunctionParam
2994 std::unique_ptr
<Type
> param_type
;
2995 Analysis::NodeMapping mappings
;
2998 bool has_name () const { return name
!= "_"; }
3000 NamedFunctionParam (Analysis::NodeMapping mappings
, Identifier name
,
3001 std::unique_ptr
<Type
> param_type
)
3002 : name (std::move (name
)), param_type (std::move (param_type
)),
3003 mappings (std::move (mappings
))
3007 NamedFunctionParam (NamedFunctionParam
const &other
)
3008 : name (other
.name
), param_type (other
.param_type
->clone_type ()),
3009 mappings (other
.mappings
)
3012 ~NamedFunctionParam () = default;
3014 // Overloaded assignment operator to clone
3015 NamedFunctionParam
&operator= (NamedFunctionParam
const &other
)
3017 mappings
= other
.mappings
;
3019 param_type
= other
.param_type
->clone_type ();
3020 // has_name = other.has_name;
3025 // move constructors
3026 NamedFunctionParam (NamedFunctionParam
&&other
) = default;
3027 NamedFunctionParam
&operator= (NamedFunctionParam
&&other
) = default;
3029 std::string
as_string () const;
3031 Identifier
get_param_name () const { return name
; }
3033 std::unique_ptr
<Type
> &get_type () { return param_type
; }
3035 Analysis::NodeMapping
get_mappings () const { return mappings
; }
3038 // A function item used in an extern block
3039 class ExternalFunctionItem
: public ExternalItem
3041 // bool has_generics;
3042 // Generics generic_params;
3043 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
3045 // bool has_return_type;
3046 // FunctionReturnType return_type;
3047 std::unique_ptr
<Type
> return_type
; // inlined
3049 // bool has_where_clause;
3050 WhereClause where_clause
;
3052 std::vector
<NamedFunctionParam
> function_params
;
3056 // Returns whether item has generic parameters.
3057 bool has_generics () const { return !generic_params
.empty (); }
3059 // Returns whether item has a return type (otherwise void).
3060 bool has_return_type () const { return return_type
!= nullptr; }
3062 // Returns whether item has a where clause.
3063 bool has_where_clause () const { return !where_clause
.is_empty (); }
3065 ExternalFunctionItem (
3066 Analysis::NodeMapping mappings
, Identifier item_name
,
3067 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
3068 std::unique_ptr
<Type
> return_type
, WhereClause where_clause
,
3069 std::vector
<NamedFunctionParam
> function_params
, bool has_variadics
,
3070 Visibility vis
, AST::AttrVec outer_attrs
, Location locus
)
3071 : ExternalItem (std::move (mappings
), std::move (item_name
),
3072 std::move (vis
), std::move (outer_attrs
), locus
),
3073 generic_params (std::move (generic_params
)),
3074 return_type (std::move (return_type
)),
3075 where_clause (std::move (where_clause
)),
3076 function_params (std::move (function_params
)),
3077 has_variadics (has_variadics
)
3080 // Copy constructor with clone
3081 ExternalFunctionItem (ExternalFunctionItem
const &other
)
3082 : ExternalItem (other
), return_type (other
.return_type
->clone_type ()),
3083 where_clause (other
.where_clause
),
3084 function_params (other
.function_params
),
3085 has_variadics (other
.has_variadics
)
3087 generic_params
.reserve (other
.generic_params
.size ());
3088 for (const auto &e
: other
.generic_params
)
3089 generic_params
.push_back (e
->clone_generic_param ());
3092 // Overloaded assignment operator with clone
3093 ExternalFunctionItem
&operator= (ExternalFunctionItem
const &other
)
3095 ExternalItem::operator= (other
);
3096 return_type
= other
.return_type
->clone_type ();
3097 where_clause
= other
.where_clause
;
3098 function_params
= other
.function_params
;
3099 has_variadics
= other
.has_variadics
;
3101 generic_params
.reserve (other
.generic_params
.size ());
3102 for (const auto &e
: other
.generic_params
)
3103 generic_params
.push_back (e
->clone_generic_param ());
3108 // move constructors
3109 ExternalFunctionItem (ExternalFunctionItem
&&other
) = default;
3110 ExternalFunctionItem
&operator= (ExternalFunctionItem
&&other
) = default;
3112 std::string
as_string () const override
;
3114 void accept_vis (HIRFullVisitor
&vis
) override
;
3115 void accept_vis (HIRExternalItemVisitor
&vis
) override
;
3117 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
3119 return generic_params
;
3122 std::unique_ptr
<Type
> &get_return_type () { return return_type
; }
3124 std::vector
<NamedFunctionParam
> &get_function_params ()
3126 return function_params
;
3129 bool is_variadic () const { return has_variadics
; }
3131 ExternKind
get_extern_kind () override
{ return ExternKind::Function
; }
3134 /* Use covariance to implement clone function as returning this object
3135 * rather than base */
3136 ExternalFunctionItem
*clone_external_item_impl () const override
3138 return new ExternalFunctionItem (*this);
3142 // An extern block HIR node
3143 class ExternBlock
: public VisItem
3146 AST::AttrVec inner_attrs
;
3147 std::vector
<std::unique_ptr
<ExternalItem
>> extern_items
;
3151 std::string
as_string () const override
;
3153 // Returns whether extern block has inner attributes.
3154 bool has_inner_attrs () const { return !inner_attrs
.empty (); }
3156 // Returns whether extern block has extern items.
3157 bool has_extern_items () const { return !extern_items
.empty (); }
3159 ABI
get_abi () const { return abi
; }
3161 ExternBlock (Analysis::NodeMapping mappings
, ABI abi
,
3162 std::vector
<std::unique_ptr
<ExternalItem
>> extern_items
,
3163 Visibility vis
, AST::AttrVec inner_attrs
,
3164 AST::AttrVec outer_attrs
, Location locus
)
3165 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
3166 abi (abi
), inner_attrs (std::move (inner_attrs
)),
3167 extern_items (std::move (extern_items
)), locus (locus
)
3170 // Copy constructor with vector clone
3171 ExternBlock (ExternBlock
const &other
)
3172 : VisItem (other
), abi (other
.abi
), inner_attrs (other
.inner_attrs
),
3175 extern_items
.reserve (other
.extern_items
.size ());
3176 for (const auto &e
: other
.extern_items
)
3177 extern_items
.push_back (e
->clone_external_item ());
3180 // Overloaded assignment operator with vector clone
3181 ExternBlock
&operator= (ExternBlock
const &other
)
3183 VisItem::operator= (other
);
3185 inner_attrs
= other
.inner_attrs
;
3186 locus
= other
.locus
;
3188 extern_items
.reserve (other
.extern_items
.size ());
3189 for (const auto &e
: other
.extern_items
)
3190 extern_items
.push_back (e
->clone_external_item ());
3195 // move constructors
3196 ExternBlock (ExternBlock
&&other
) = default;
3197 ExternBlock
&operator= (ExternBlock
&&other
) = default;
3199 Location
get_locus () const override final
{ return locus
; }
3201 void accept_vis (HIRFullVisitor
&vis
) override
;
3202 void accept_vis (HIRStmtVisitor
&vis
) override
;
3203 void accept_vis (HIRVisItemVisitor
&vis
) override
;
3205 std::vector
<std::unique_ptr
<ExternalItem
>> &get_extern_items ()
3207 return extern_items
;
3210 ItemKind
get_item_kind () const override
{ return ItemKind::ExternBlock
; }
3213 /* Use covariance to implement clone function as returning this object
3214 * rather than base */
3215 ExternBlock
*clone_item_impl () const override
3217 return new ExternBlock (*this);
3220 /* Use covariance to implement clone function as returning this object
3221 * rather than base */
3222 /*virtual ExternBlock* clone_statement_impl() const override {
3223 return new ExternBlock(*this);