1 // Copyright (C) 2020-2025 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
24 #include "rust-hir-stmt.h"
25 #include "rust-common.h"
26 #include "rust-hir-visibility.h"
27 #include "rust-hir-generic-param.h"
28 #include "rust-system.h"
33 // Rust "item" HIR node (declaration of top-level/module-level allowed stuff)
34 class Item
: public Stmt
, public WithOuterAttrs
36 // TODO: should outer attrs be defined here or in each derived class?
50 EnumItem
, // FIXME: ARTHUR: Do we need that?
56 static std::string
item_kind_string (ItemKind kind
);
58 virtual ItemKind
get_item_kind () const = 0;
60 // Unique pointer custom clone function
61 std::unique_ptr
<Item
> clone_item () const
63 return std::unique_ptr
<Item
> (clone_item_impl ());
66 BaseKind
get_hir_kind () override
{ return Node::BaseKind::ITEM
; }
68 std::string
as_string () const override
;
70 /* Adds crate names to the vector passed by reference, if it can
73 add_crate_name (std::vector
<std::string
> &names ATTRIBUTE_UNUSED
) const
76 bool is_item () const override final
{ return true; }
80 Item (Analysis::NodeMapping mappings
,
81 AST::AttrVec outer_attribs
= AST::AttrVec ())
82 : Stmt (std::move (mappings
)), WithOuterAttrs (std::move (outer_attribs
))
85 // Clone function implementation as pure virtual method
86 virtual Item
*clone_item_impl () const = 0;
88 /* Save having to specify two clone methods in derived classes by making
89 * statement clone return item clone. Hopefully won't affect performance too
91 Item
*clone_stmt_impl () const override
{ return clone_item_impl (); }
94 // A type generic parameter (as opposed to a lifetime generic parameter)
95 class TypeParam
: public GenericParam
97 AST::AttrVec outer_attrs
;
99 Identifier type_representation
;
101 // bool has_type_param_bounds;
102 // TypeParamBounds type_param_bounds;
103 std::vector
<std::unique_ptr
<TypeParamBound
>>
104 type_param_bounds
; // inlined form
106 tl::optional
<std::unique_ptr
<Type
>> type
;
111 // Returns whether the type of the type param has been specified.
112 bool has_type () const { return type
.has_value (); }
114 // Returns whether the type param has type param bounds.
115 bool has_type_param_bounds () const { return !type_param_bounds
.empty (); }
117 // Returns whether the type param has an outer attribute.
118 bool has_outer_attribute () const override
{ return outer_attrs
.size () > 0; }
119 AST::AttrVec
&get_outer_attrs () override
{ return outer_attrs
; }
121 TypeParam (Analysis::NodeMapping mappings
, Identifier type_representation
,
122 location_t locus
= UNDEF_LOCATION
,
123 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
124 = std::vector
<std::unique_ptr
<TypeParamBound
>> (),
125 tl::optional
<std::unique_ptr
<Type
>> type
= tl::nullopt
,
126 AST::AttrVec outer_attrs
= std::vector
<AST::Attribute
> ());
128 // Copy constructor uses clone
129 TypeParam (TypeParam
const &other
);
131 // Overloaded assignment operator to clone
132 TypeParam
&operator= (TypeParam
const &other
);
135 TypeParam (TypeParam
&&other
) = default;
137 TypeParam
&operator= (TypeParam
&&other
) = default;
139 std::string
as_string () const override
;
141 location_t
get_locus () const override final
{ return locus
; }
143 void accept_vis (HIRFullVisitor
&vis
) override
;
145 Identifier
get_type_representation () const { return type_representation
; }
150 return *type
.value ();
153 Analysis::NodeMapping
get_type_mappings () const;
155 std::vector
<std::unique_ptr
<TypeParamBound
>> &get_type_param_bounds ();
158 // Clone function implementation as (not pure) virtual method
159 TypeParam
*clone_generic_param_impl () const override
161 return new TypeParam (*this);
165 /* "where" clause item base. Abstract - use LifetimeWhereClauseItem,
166 * TypeBoundWhereClauseItem */
167 class WhereClauseItem
: public FullVisitable
176 virtual ~WhereClauseItem () {}
178 // Unique pointer custom clone function
179 std::unique_ptr
<WhereClauseItem
> clone_where_clause_item () const
181 return std::unique_ptr
<WhereClauseItem
> (clone_where_clause_item_impl ());
184 virtual std::string
as_string () const = 0;
186 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
188 virtual Analysis::NodeMapping
get_mappings () const = 0;
190 virtual ItemType
get_item_type () const = 0;
193 // Clone function implementation as pure virtual method
194 virtual WhereClauseItem
*clone_where_clause_item_impl () const = 0;
197 // A lifetime where clause item
198 class LifetimeWhereClauseItem
: public WhereClauseItem
201 std::vector
<Lifetime
> lifetime_bounds
;
203 Analysis::NodeMapping mappings
;
206 LifetimeWhereClauseItem (Analysis::NodeMapping mappings
, Lifetime lifetime
,
207 std::vector
<Lifetime
> lifetime_bounds
,
209 : lifetime (std::move (lifetime
)),
210 lifetime_bounds (std::move (lifetime_bounds
)), locus (locus
),
211 mappings (std::move (mappings
))
214 std::string
as_string () const override
;
216 void accept_vis (HIRFullVisitor
&vis
) override
;
218 Lifetime
&get_lifetime () { return lifetime
; }
220 std::vector
<Lifetime
> &get_lifetime_bounds () { return lifetime_bounds
; }
222 Analysis::NodeMapping
get_mappings () const override final
227 ItemType
get_item_type () const override final
229 return WhereClauseItem::ItemType::LIFETIME
;
233 // Clone function implementation as (not pure) virtual method
234 LifetimeWhereClauseItem
*clone_where_clause_item_impl () const override
236 return new LifetimeWhereClauseItem (*this);
240 // A type bound where clause item
241 class TypeBoundWhereClauseItem
: public WhereClauseItem
243 std::vector
<LifetimeParam
> for_lifetimes
;
244 std::unique_ptr
<Type
> bound_type
;
245 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
;
246 Analysis::NodeMapping mappings
;
250 // Returns whether the item has ForLifetimes
251 bool has_for_lifetimes () const { return !for_lifetimes
.empty (); }
253 // Returns whether the item has type param bounds
254 bool has_type_param_bounds () const { return !type_param_bounds
.empty (); }
256 TypeBoundWhereClauseItem (
257 Analysis::NodeMapping mappings
, std::vector
<LifetimeParam
> for_lifetimes
,
258 std::unique_ptr
<Type
> bound_type
,
259 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
,
262 // Copy constructor requires clone
263 TypeBoundWhereClauseItem (TypeBoundWhereClauseItem
const &other
);
265 // Overload assignment operator to clone
266 TypeBoundWhereClauseItem
&operator= (TypeBoundWhereClauseItem
const &other
);
269 TypeBoundWhereClauseItem (TypeBoundWhereClauseItem
&&other
) = default;
270 TypeBoundWhereClauseItem
&operator= (TypeBoundWhereClauseItem
&&other
)
273 location_t
get_locus () const { return locus
; }
275 std::string
as_string () const override
;
277 void accept_vis (HIRFullVisitor
&vis
) override
;
279 std::vector
<LifetimeParam
> &get_for_lifetimes () { return for_lifetimes
; }
281 Type
&get_bound_type () { return *bound_type
; }
283 std::vector
<std::unique_ptr
<TypeParamBound
>> &get_type_param_bounds ();
285 Analysis::NodeMapping
get_mappings () const override final
290 ItemType
get_item_type () const override final
292 return WhereClauseItem::ItemType::TYPE_BOUND
;
296 // Clone function implementation as (not pure) virtual method
297 TypeBoundWhereClauseItem
*clone_where_clause_item_impl () const override
299 return new TypeBoundWhereClauseItem (*this);
307 std::vector
<std::unique_ptr
<WhereClauseItem
>> where_clause_items
;
309 // should this store location info?
312 WhereClause (std::vector
<std::unique_ptr
<WhereClauseItem
>> where_clause_items
)
313 : where_clause_items (std::move (where_clause_items
))
316 // copy constructor with vector clone
317 WhereClause (WhereClause
const &other
)
319 where_clause_items
.reserve (other
.where_clause_items
.size ());
320 for (const auto &e
: other
.where_clause_items
)
321 where_clause_items
.push_back (e
->clone_where_clause_item ());
324 // overloaded assignment operator with vector clone
325 WhereClause
&operator= (WhereClause
const &other
)
327 where_clause_items
.reserve (other
.where_clause_items
.size ());
328 for (const auto &e
: other
.where_clause_items
)
329 where_clause_items
.push_back (e
->clone_where_clause_item ());
335 WhereClause (WhereClause
&&other
) = default;
336 WhereClause
&operator= (WhereClause
&&other
) = default;
338 // Creates a WhereClause with no items.
339 static WhereClause
create_empty ()
341 return WhereClause (std::vector
<std::unique_ptr
<WhereClauseItem
>> ());
344 // Returns whether the WhereClause has no items.
345 bool is_empty () const { return where_clause_items
.empty (); }
347 std::string
as_string () const;
349 std::vector
<std::unique_ptr
<WhereClauseItem
>> &get_items ()
351 return where_clause_items
;
353 const std::vector
<std::unique_ptr
<WhereClauseItem
>> &get_items () const
355 return where_clause_items
;
359 // A self parameter in a method
363 enum ImplicitSelfKind
368 MUT_REF
, // &mut self
373 ImplicitSelfKind self_kind
;
374 tl::optional
<Lifetime
> lifetime
;
375 std::unique_ptr
<Type
> type
;
377 Analysis::NodeMapping mappings
;
379 SelfParam (Analysis::NodeMapping mappings
, ImplicitSelfKind self_kind
,
380 tl::optional
<Lifetime
> lifetime
, Type
*type
);
383 // Type-based self parameter (not ref, no lifetime)
384 SelfParam (Analysis::NodeMapping mappings
, std::unique_ptr
<Type
> type
,
385 bool is_mut
, location_t locus
);
387 // Lifetime-based self parameter (is ref, no type)
388 SelfParam (Analysis::NodeMapping mappings
, tl::optional
<Lifetime
> lifetime
,
389 bool is_mut
, location_t locus
);
391 // Copy constructor requires clone
392 SelfParam (SelfParam
const &other
);
394 // Overload assignment operator to use clone
395 SelfParam
&operator= (SelfParam
const &other
);
398 SelfParam (SelfParam
&&other
) = default;
399 SelfParam
&operator= (SelfParam
&&other
) = default;
401 // Returns whether the self-param has a type field.
402 bool has_type () const { return type
!= nullptr; }
404 // Returns whether the self-param has a valid lifetime.
405 bool has_lifetime () const { return lifetime
.has_value (); }
407 const Lifetime
&get_lifetime () const { return lifetime
.value (); }
409 std::string
as_string () const;
411 location_t
get_locus () const { return locus
; }
413 ImplicitSelfKind
get_self_kind () const { return self_kind
; }
421 Analysis::NodeMapping
get_mappings () { return mappings
; }
423 Mutability
get_mut () const;
425 bool is_mut () const;
427 bool is_ref () const;
430 // Qualifiers for function, i.e. const, unsafe, extern etc.
431 struct FunctionQualifiers
441 FunctionQualifiers (Async async_status
, Const const_status
, Unsafety unsafety
,
442 bool has_extern
, ABI abi
)
443 : async_status (async_status
), const_status (const_status
),
444 unsafety (unsafety
), has_extern (has_extern
), abi (abi
)
447 std::string
as_string () const;
449 Const
get_const_status () const { return const_status
; }
451 bool is_const () const { return const_status
== Const::Yes
; }
452 bool is_unsafe () const { return unsafety
== Unsafety::Unsafe
; }
453 bool is_async () const { return async_status
== Async::Yes
; }
455 ABI
get_abi () const { return abi
; }
458 // A function parameter
461 std::unique_ptr
<Pattern
> param_name
;
462 std::unique_ptr
<Type
> type
;
464 Analysis::NodeMapping mappings
;
467 FunctionParam (Analysis::NodeMapping mappings
,
468 std::unique_ptr
<Pattern
> param_name
,
469 std::unique_ptr
<Type
> param_type
, location_t locus
);
471 // Copy constructor uses clone
472 FunctionParam (FunctionParam
const &other
);
474 // Overload assignment operator to use clone
475 FunctionParam
&operator= (FunctionParam
const &other
);
478 FunctionParam (FunctionParam
&&other
) = default;
479 FunctionParam
&operator= (FunctionParam
&&other
) = default;
481 std::string
as_string () const;
483 location_t
get_locus () const { return locus
; }
485 Pattern
&get_param_name () { return *param_name
; }
493 const Analysis::NodeMapping
&get_mappings () const { return mappings
; }
496 // Item that supports visibility - abstract base class
497 class VisItem
: public Item
499 Visibility visibility
;
502 // Visibility constructor
503 VisItem (Analysis::NodeMapping mappings
, Visibility visibility
,
504 AST::AttrVec outer_attrs
= AST::AttrVec ())
505 : Item (std::move (mappings
), std::move (outer_attrs
)),
506 visibility (std::move (visibility
))
509 // Visibility copy constructor
510 VisItem (VisItem
const &other
);
512 // Overload assignment operator to clone
513 VisItem
&operator= (VisItem
const &other
);
516 VisItem (VisItem
&&other
) = default;
517 VisItem
&operator= (VisItem
&&other
) = default;
520 using HIR::Stmt::accept_vis
;
522 BaseKind
get_hir_kind () override final
{ return VIS_ITEM
; }
524 /* Does the item have some kind of public visibility (non-default
526 bool has_visibility () const { return !visibility
.is_error (); }
528 virtual void accept_vis (HIRVisItemVisitor
&vis
) = 0;
530 Visibility
&get_visibility () { return visibility
; }
531 const Visibility
&get_visibility () const { return visibility
; }
533 std::string
as_string () const override
;
536 // Rust module item - abstract base class
537 class Module
: public VisItem
, public WithInnerAttrs
539 Identifier module_name
;
542 std::vector
<std::unique_ptr
<Item
>> items
;
545 std::string
as_string () const override
;
547 // Returns whether the module has items in its body.
548 bool has_items () const { return !items
.empty (); }
551 Module (Analysis::NodeMapping mappings
, Identifier module_name
,
552 location_t locus
, std::vector
<std::unique_ptr
<Item
>> items
,
553 Visibility visibility
= Visibility::create_error (),
554 AST::AttrVec inner_attrs
= AST::AttrVec (),
555 AST::AttrVec outer_attrs
= AST::AttrVec ());
557 // Copy constructor with vector clone
558 Module (Module
const &other
);
560 // Overloaded assignment operator with vector clone
561 Module
&operator= (Module
const &other
);
564 Module (Module
&&other
) = default;
565 Module
&operator= (Module
&&other
) = default;
567 void accept_vis (HIRFullVisitor
&vis
) override
;
568 void accept_vis (HIRStmtVisitor
&vis
) override
;
569 void accept_vis (HIRVisItemVisitor
&vis
) override
;
571 Identifier
get_module_name () const { return module_name
; }
572 std::vector
<std::unique_ptr
<Item
>> &get_items () { return items
; };
574 /* Override that runs the function recursively on all items contained within
576 void add_crate_name (std::vector
<std::string
> &names
) const override
;
578 location_t
get_locus () const override final
{ return locus
; }
580 ItemKind
get_item_kind () const override
{ return ItemKind::Module
; }
583 /* Use covariance to implement clone function as returning this object
584 * rather than base */
585 Module
*clone_item_impl () const override
{ return new Module (*this); }
587 /* Use covariance to implement clone function as returning this object
588 * rather than base */
589 /*virtual Module* clone_statement_impl() const override {
590 return new Module(*this);
594 // Rust extern crate declaration HIR node
595 class ExternCrate
: public VisItem
597 // this is either an identifier or "self", with self parsed to string
598 std::string referenced_crate
;
599 // bool has_as_clause;
600 // AsClause as_clause;
601 // this is either an identifier or "_", with _ parsed to string
602 std::string as_clause_name
;
607 "extern crate foo as _"
609 "extern crate std as cool_std" */
611 std::string
as_string () const override
;
613 // Returns whether extern crate declaration has an as clause.
614 bool has_as_clause () const { return !as_clause_name
.empty (); }
616 /* Returns whether extern crate declaration references the current crate
618 bool references_self () const { return referenced_crate
== "self"; }
621 ExternCrate (Analysis::NodeMapping mappings
, std::string referenced_crate
,
622 Visibility visibility
, AST::AttrVec outer_attrs
,
623 location_t locus
, std::string as_clause_name
= std::string ())
624 : VisItem (std::move (mappings
), std::move (visibility
),
625 std::move (outer_attrs
)),
626 referenced_crate (std::move (referenced_crate
)),
627 as_clause_name (std::move (as_clause_name
)), locus (locus
)
630 location_t
get_locus () const override final
{ return locus
; }
632 ItemKind
get_item_kind () const override
{ return ItemKind::ExternCrate
; }
633 std::string
get_referenced_crate () { return referenced_crate
; }
634 std::string
get_as_clause_name () { return as_clause_name
; }
636 void accept_vis (HIRFullVisitor
&vis
) override
;
637 void accept_vis (HIRStmtVisitor
&vis
) override
;
638 void accept_vis (HIRVisItemVisitor
&vis
) override
;
640 // Override that adds extern crate name in decl to passed list of names.
641 void add_crate_name (std::vector
<std::string
> &names
) const override
643 names
.push_back (referenced_crate
);
647 /* Use covariance to implement clone function as returning this object
648 * rather than base */
649 ExternCrate
*clone_item_impl () const override
651 return new ExternCrate (*this);
654 /* Use covariance to implement clone function as returning this object
655 * rather than base */
656 /*virtual ExternCrate* clone_statement_impl() const override {
657 return new ExternCrate(*this);
661 // The path-ish thing referred to in a use declaration - abstract base class
662 class UseTree
: public FullVisitable
667 virtual ~UseTree () {}
669 // Unique pointer custom clone function
670 std::unique_ptr
<UseTree
> clone_use_tree () const
672 return std::unique_ptr
<UseTree
> (clone_use_tree_impl ());
675 virtual std::string
as_string () const = 0;
677 location_t
get_locus () const { return locus
; }
680 // Clone function implementation as pure virtual method
681 virtual UseTree
*clone_use_tree_impl () const = 0;
683 UseTree (location_t locus
) : locus (locus
) {}
686 // Use tree with a glob (wildcard) operator
687 class UseTreeGlob
: public UseTree
699 AST::SimplePath path
;
702 UseTreeGlob (PathType glob_type
, AST::SimplePath path
, location_t locus
)
703 : UseTree (locus
), glob_type (glob_type
), path (std::move (path
))
705 if (this->glob_type
!= PATH_PREFIXED
)
707 // compiler implementation error if there is a path with a
708 // non-path-prefixed use tree glob
709 gcc_assert (!has_path ());
711 // TODO: do path-prefixed paths also have to have a path? If so, have an
712 // assert for that too.
715 /* Returns whether has path. Should be made redundant by PathType
717 bool has_path () const { return !path
.is_empty (); }
719 PathType
get_glob_type () { return glob_type
; }
720 AST::SimplePath
get_path () { return path
; };
722 std::string
as_string () const override
;
724 void accept_vis (HIRFullVisitor
&vis
) override
;
726 /* TODO: find way to ensure only PATH_PREFIXED glob_type has path - factory
729 /* Use covariance to implement clone function as returning this object
730 * rather than base */
731 UseTreeGlob
*clone_use_tree_impl () const override
733 return new UseTreeGlob (*this);
737 // Use tree with a list of paths with a common prefix
738 class UseTreeList
: public UseTree
750 AST::SimplePath path
;
752 std::vector
<std::unique_ptr
<UseTree
>> trees
;
755 UseTreeList (PathType path_type
, AST::SimplePath path
,
756 std::vector
<std::unique_ptr
<UseTree
>> trees
, location_t locus
)
757 : UseTree (locus
), path_type (path_type
), path (std::move (path
)),
758 trees (std::move (trees
))
760 if (this->path_type
!= PATH_PREFIXED
)
762 // compiler implementation error if there is a path with a
763 // non-path-prefixed use tree glob
764 gcc_assert (!has_path ());
766 // TODO: do path-prefixed paths also have to have a path? If so, have an
767 // assert for that too.
770 // copy constructor with vector clone
771 UseTreeList (UseTreeList
const &other
)
772 : UseTree (other
), path_type (other
.path_type
), path (other
.path
)
774 trees
.reserve (other
.trees
.size ());
775 for (const auto &e
: other
.trees
)
776 trees
.push_back (e
->clone_use_tree ());
779 // overloaded assignment operator with vector clone
780 UseTreeList
&operator= (UseTreeList
const &other
)
782 UseTree::operator= (other
);
783 path_type
= other
.path_type
;
786 trees
.reserve (other
.trees
.size ());
787 for (const auto &e
: other
.trees
)
788 trees
.push_back (e
->clone_use_tree ());
794 UseTreeList (UseTreeList
&&other
) = default;
795 UseTreeList
&operator= (UseTreeList
&&other
) = default;
797 // Returns whether has path. Should be made redundant by path_type.
798 bool has_path () const { return !path
.is_empty (); }
800 // Returns whether has inner tree elements.
801 bool has_trees () const { return !trees
.empty (); }
803 std::string
as_string () const override
;
805 void accept_vis (HIRFullVisitor
&vis
) override
;
807 PathType
get_path_type () { return path_type
; }
808 AST::SimplePath
get_path () { return path
; }
809 std::vector
<std::unique_ptr
<UseTree
>> &get_trees () { return trees
; }
811 // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
814 /* Use covariance to implement clone function as returning this object
815 * rather than base */
816 UseTreeList
*clone_use_tree_impl () const override
818 return new UseTreeList (*this);
822 // Use tree where it rebinds the module name as something else
823 class UseTreeRebind
: public UseTree
834 AST::SimplePath path
;
836 NewBindType bind_type
;
837 Identifier identifier
; // only if NewBindType is IDENTIFIER
840 UseTreeRebind (NewBindType bind_type
, AST::SimplePath path
, location_t locus
,
841 Identifier identifier
= std::string ())
842 : UseTree (locus
), path (std::move (path
)), bind_type (bind_type
),
843 identifier (std::move (identifier
))
846 // Returns whether has path (this should always be true).
847 bool has_path () const { return !path
.is_empty (); }
849 AST::SimplePath
get_path () { return path
; }
851 Identifier
get_identifier () const { return identifier
; }
853 NewBindType
get_bind_type () const { return bind_type
; }
855 // Returns whether has identifier (or, rather, is allowed to).
856 bool has_identifier () const { return bind_type
== IDENTIFIER
; }
858 std::string
as_string () const override
;
860 void accept_vis (HIRFullVisitor
&vis
) override
;
862 // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
865 /* Use covariance to implement clone function as returning this object
866 * rather than base */
867 virtual UseTreeRebind
*clone_use_tree_impl () const override
869 return new UseTreeRebind (*this);
873 std::string
enum_to_str (UseTreeRebind::NewBindType
);
875 // Rust use declaration (i.e. for modules) HIR node
876 class UseDeclaration
: public VisItem
878 std::unique_ptr
<UseTree
> use_tree
;
882 std::string
as_string () const override
;
884 UseDeclaration (Analysis::NodeMapping mappings
,
885 std::unique_ptr
<UseTree
> use_tree
, Visibility visibility
,
886 AST::AttrVec outer_attrs
, location_t locus
)
887 : VisItem (std::move (mappings
), std::move (visibility
),
888 std::move (outer_attrs
)),
889 use_tree (std::move (use_tree
)), locus (locus
)
892 // Copy constructor with clone
893 UseDeclaration (UseDeclaration
const &other
)
894 : VisItem (other
), use_tree (other
.use_tree
->clone_use_tree ()),
898 // Overloaded assignment operator to clone
899 UseDeclaration
&operator= (UseDeclaration
const &other
)
901 VisItem::operator= (other
);
902 use_tree
= other
.use_tree
->clone_use_tree ();
903 // visibility = other.visibility->clone_visibility();
904 // outer_attrs = other.outer_attrs;
911 UseDeclaration (UseDeclaration
&&other
) = default;
912 UseDeclaration
&operator= (UseDeclaration
&&other
) = default;
914 location_t
get_locus () const override final
{ return locus
; }
915 ItemKind
get_item_kind () const override
{ return ItemKind::UseDeclaration
; }
917 UseTree
&get_use_tree () { return *use_tree
; }
918 void accept_vis (HIRFullVisitor
&vis
) override
;
919 void accept_vis (HIRStmtVisitor
&vis
) override
;
920 void accept_vis (HIRVisItemVisitor
&vis
) override
;
923 /* Use covariance to implement clone function as returning this object
924 * rather than base */
925 UseDeclaration
*clone_item_impl () const override
927 return new UseDeclaration (*this);
930 /* Use covariance to implement clone function as returning this object
931 * rather than base */
932 /*virtual UseDeclaration* clone_statement_impl() const override {
933 return new UseDeclaration(*this);
939 enum class Defaultness
945 // Rust function declaration HIR node
946 class Function
: public VisItem
, public ImplItem
948 FunctionQualifiers qualifiers
;
949 Identifier function_name
;
950 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
951 std::vector
<FunctionParam
> function_params
;
952 std::unique_ptr
<Type
> return_type
;
953 WhereClause where_clause
;
954 std::unique_ptr
<BlockExpr
> function_body
;
955 tl::optional
<SelfParam
> self
;
958 // NOTE: This should be moved to the trait item base class once we start
959 // implementing specialization for real, instead of just stubbing out the
961 Defaultness defaultness
;
964 std::string
as_string () const override
;
966 // Returns whether function has generic parameters.
967 bool has_generics () const { return !generic_params
.empty (); }
969 // Returns whether function has regular parameters.
970 bool has_function_params () const { return !function_params
.empty (); }
972 // Returns whether function has return type - if not, it is void.
973 bool has_function_return_type () const { return return_type
!= nullptr; }
975 // Returns whether function has a where clause.
976 bool has_where_clause () const { return !where_clause
.is_empty (); }
978 // Returns whether function has a default qualifier
979 bool is_default () const { return defaultness
== Defaultness::Default
; }
981 ImplItemType
get_impl_item_type () const override final
983 return ImplItem::ImplItemType::FUNCTION
;
986 ItemKind
get_item_kind () const override
{ return ItemKind::Function
; }
988 // Mega-constructor with all possible fields
989 Function (Analysis::NodeMapping mappings
, Identifier function_name
,
990 FunctionQualifiers qualifiers
,
991 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
992 std::vector
<FunctionParam
> function_params
,
993 std::unique_ptr
<Type
> return_type
, WhereClause where_clause
,
994 std::unique_ptr
<BlockExpr
> function_body
, Visibility vis
,
995 AST::AttrVec outer_attrs
, tl::optional
<SelfParam
> self
,
996 Defaultness defaultness
, location_t locus
);
998 // Copy constructor with clone
999 Function (Function
const &other
);
1001 // Overloaded assignment operator to clone
1002 Function
&operator= (Function
const &other
);
1004 // move constructors
1005 Function (Function
&&other
) = default;
1006 Function
&operator= (Function
&&other
) = default;
1008 location_t
get_locus () const override final
{ return locus
; }
1010 void accept_vis (HIRFullVisitor
&vis
) override
;
1011 void accept_vis (HIRImplVisitor
&vis
) override
;
1012 void accept_vis (HIRStmtVisitor
&vis
) override
;
1013 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1015 Analysis::NodeMapping
get_impl_mappings () const override
1017 return get_mappings ();
1020 std::vector
<FunctionParam
> &get_function_params () { return function_params
; }
1021 const std::vector
<FunctionParam
> &get_function_params () const
1023 return function_params
;
1026 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1028 return generic_params
;
1030 const std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params () const
1032 return generic_params
;
1035 // TODO: is this better? Or is a "vis_block" better?
1036 BlockExpr
&get_definition () { return *function_body
; }
1038 const FunctionQualifiers
&get_qualifiers () const { return qualifiers
; }
1040 Identifier
get_function_name () const { return function_name
; }
1042 // TODO: is this better? Or is a "vis_block" better?
1043 WhereClause
&get_where_clause () { return where_clause
; }
1045 bool has_return_type () const { return return_type
!= nullptr; }
1047 // TODO: is this better? Or is a "vis_block" better?
1048 Type
&get_return_type () { return *return_type
; }
1050 bool is_method () const { return self
.has_value (); }
1052 tl::optional
<SelfParam
> &get_self_param () { return self
; }
1053 const tl::optional
<SelfParam
> &get_self_param () const { return self
; }
1055 SelfParam
&get_self_param_unchecked () { return self
.value (); }
1056 const SelfParam
&get_self_param_unchecked () const { return self
.value (); }
1058 std::string
get_impl_item_name () const override final
1060 return get_function_name ().as_string ();
1064 /* Use covariance to implement clone function as returning this object
1065 * rather than base */
1066 Function
*clone_item_impl () const override
{ return new Function (*this); }
1068 /* Use covariance to implement clone function as returning this object
1069 * rather than base */
1070 Function
*clone_inherent_impl_item_impl () const override
1072 return new Function (*this);
1076 // Rust type alias (i.e. typedef) HIR node
1077 class TypeAlias
: public VisItem
, public ImplItem
1079 Identifier new_type_name
;
1081 // bool has_generics;
1082 // Generics generic_params;
1083 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1085 // bool has_where_clause;
1086 WhereClause where_clause
;
1088 std::unique_ptr
<Type
> existing_type
;
1093 std::string
as_string () const override
;
1095 // Returns whether type alias has generic parameters.
1096 bool has_generics () const { return !generic_params
.empty (); }
1098 // Returns whether type alias has a where clause.
1099 bool has_where_clause () const { return !where_clause
.is_empty (); }
1101 ImplItemType
get_impl_item_type () const override final
1103 return ImplItem::ImplItemType::TYPE_ALIAS
;
1106 // Mega-constructor with all possible fields
1107 TypeAlias (Analysis::NodeMapping mappings
, Identifier new_type_name
,
1108 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1109 WhereClause where_clause
, std::unique_ptr
<Type
> existing_type
,
1110 Visibility vis
, AST::AttrVec outer_attrs
, location_t locus
);
1113 TypeAlias (TypeAlias
const &other
);
1115 // Overloaded assignment operator to clone
1116 TypeAlias
&operator= (TypeAlias
const &other
);
1118 // move constructors
1119 TypeAlias (TypeAlias
&&other
) = default;
1120 TypeAlias
&operator= (TypeAlias
&&other
) = default;
1122 location_t
get_locus () const override final
{ return locus
; }
1124 void accept_vis (HIRFullVisitor
&vis
) override
;
1125 void accept_vis (HIRImplVisitor
&vis
) override
;
1126 void accept_vis (HIRStmtVisitor
&vis
) override
;
1127 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1129 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1131 return generic_params
;
1133 const std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params () const
1135 return generic_params
;
1138 WhereClause
&get_where_clause () { return where_clause
; }
1140 Type
&get_type_aliased ()
1142 rust_assert (existing_type
);
1143 return *existing_type
;
1146 Identifier
get_new_type_name () const { return new_type_name
; }
1148 ItemKind
get_item_kind () const override
{ return ItemKind::TypeAlias
; }
1150 Analysis::NodeMapping
get_impl_mappings () const override
1152 return get_mappings ();
1155 std::string
get_impl_item_name () const override final
1157 return get_new_type_name ().as_string ();
1161 /* Use covariance to implement clone function as returning this object
1162 * rather than base */
1163 TypeAlias
*clone_item_impl () const override
{ return new TypeAlias (*this); }
1165 /* Use covariance to implement clone function as returning this object
1166 * rather than base */
1167 TypeAlias
*clone_inherent_impl_item_impl () const override
1169 return new TypeAlias (*this);
1173 // Rust base struct declaration HIR node - abstract base class
1174 class Struct
: public VisItem
1177 // protected to enable access by derived classes - allows better as_string
1178 Identifier struct_name
;
1180 // bool has_generics;
1181 // Generics generic_params;
1182 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1184 // bool has_where_clause;
1185 WhereClause where_clause
;
1190 Identifier
get_identifier () const { return struct_name
; }
1192 // Returns whether struct has generic parameters.
1193 bool has_generics () const { return !generic_params
.empty (); }
1195 // Returns whether struct has a where clause.
1196 bool has_where_clause () const { return !where_clause
.is_empty (); }
1198 location_t
get_locus () const override final
{ return locus
; }
1199 ItemKind
get_item_kind () const override
{ return ItemKind::Struct
; }
1201 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1203 return generic_params
;
1206 WhereClause
&get_where_clause () { return where_clause
; }
1209 Struct (Analysis::NodeMapping mappings
, Identifier struct_name
,
1210 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1211 WhereClause where_clause
, Visibility vis
, location_t locus
,
1212 AST::AttrVec outer_attrs
= AST::AttrVec ())
1213 : VisItem (std::move (mappings
), std::move (vis
), std::move (outer_attrs
)),
1214 struct_name (std::move (struct_name
)),
1215 generic_params (std::move (generic_params
)),
1216 where_clause (std::move (where_clause
)), locus (locus
)
1219 // Copy constructor with vector clone
1220 Struct (Struct
const &other
)
1221 : VisItem (other
), struct_name (other
.struct_name
),
1222 where_clause (other
.where_clause
), locus (other
.locus
)
1224 generic_params
.reserve (other
.generic_params
.size ());
1225 for (const auto &e
: other
.generic_params
)
1226 generic_params
.push_back (e
->clone_generic_param ());
1229 // Overloaded assignment operator with vector clone
1230 Struct
&operator= (Struct
const &other
)
1232 VisItem::operator= (other
);
1233 struct_name
= other
.struct_name
;
1234 where_clause
= other
.where_clause
;
1235 locus
= other
.locus
;
1237 generic_params
.reserve (other
.generic_params
.size ());
1238 for (const auto &e
: other
.generic_params
)
1239 generic_params
.push_back (e
->clone_generic_param ());
1244 // move constructors
1245 Struct (Struct
&&other
) = default;
1246 Struct
&operator= (Struct
&&other
) = default;
1249 // A single field in a struct
1250 // FIXME can't this be a TupleStruct + field_name?
1254 // bool has_outer_attributes;
1255 AST::AttrVec outer_attrs
;
1257 // bool has_visibility;
1258 Visibility visibility
;
1260 Identifier field_name
;
1261 std::unique_ptr
<Type
> field_type
;
1263 Analysis::NodeMapping mappings
;
1267 // Returns whether struct field has any outer attributes.
1268 bool has_outer_attributes () const { return !outer_attrs
.empty (); }
1270 // Returns whether struct field has a non-private (non-default) visibility.
1271 bool has_visibility () const { return !visibility
.is_error (); }
1273 StructField (Analysis::NodeMapping mappings
, Identifier field_name
,
1274 std::unique_ptr
<Type
> field_type
, Visibility vis
,
1275 location_t locus
, AST::AttrVec outer_attrs
= AST::AttrVec ());
1278 StructField (StructField
const &other
);
1280 ~StructField () = default;
1282 // Overloaded assignment operator to clone
1283 StructField
&operator= (StructField
const &other
);
1285 // move constructors
1286 StructField (StructField
&&other
) = default;
1287 StructField
&operator= (StructField
&&other
) = default;
1289 std::string
as_string () const;
1291 Identifier
get_field_name () const { return field_name
; }
1293 Type
&get_field_type () { return *field_type
; }
1295 Analysis::NodeMapping
get_mappings () const { return mappings
; }
1297 location_t
get_locus () { return locus
; }
1298 AST::AttrVec
&get_outer_attrs () { return outer_attrs
; }
1299 Visibility
&get_visibility () { return visibility
; }
1302 // Rust struct declaration with true struct type HIR node
1303 class StructStruct
: public Struct
1306 std::vector
<StructField
> fields
;
1309 std::string
as_string () const override
;
1311 // Mega-constructor with all possible fields
1312 StructStruct (Analysis::NodeMapping mappings
, std::vector
<StructField
> fields
,
1313 Identifier struct_name
,
1314 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1315 WhereClause where_clause
, bool is_unit
, Visibility vis
,
1316 AST::AttrVec outer_attrs
, location_t locus
)
1317 : Struct (std::move (mappings
), std::move (struct_name
),
1318 std::move (generic_params
), std::move (where_clause
),
1319 std::move (vis
), locus
, std::move (outer_attrs
)),
1320 fields (std::move (fields
)), is_unit (is_unit
)
1323 // Unit struct constructor
1324 StructStruct (Analysis::NodeMapping mappings
, Identifier struct_name
,
1325 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1326 WhereClause where_clause
, Visibility vis
,
1327 AST::AttrVec outer_attrs
, location_t locus
)
1328 : Struct (std::move (mappings
), std::move (struct_name
),
1329 std::move (generic_params
), std::move (where_clause
),
1330 std::move (vis
), locus
, std::move (outer_attrs
)),
1333 // TODO: can a unit struct have generic fields? assuming yes for now.
1335 /* Returns whether the struct is a unit struct - struct defined without
1336 * fields. This is important because it also means an implicit constant of its
1337 * type is defined. */
1338 bool is_unit_struct () const { return is_unit
; }
1340 void accept_vis (HIRFullVisitor
&vis
) override
;
1341 void accept_vis (HIRStmtVisitor
&vis
) override
;
1342 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1344 std::vector
<StructField
> &get_fields () { return fields
; }
1347 /* Use covariance to implement clone function as returning this object
1348 * rather than base */
1349 StructStruct
*clone_item_impl () const override
1351 return new StructStruct (*this);
1354 /* Use covariance to implement clone function as returning this object
1355 * rather than base */
1356 /*virtual StructStruct* clone_statement_impl() const override {
1357 return new StructStruct(*this);
1361 // A single field in a tuple
1365 // bool has_outer_attributes;
1366 AST::AttrVec outer_attrs
;
1368 // bool has_visibility;
1369 Visibility visibility
;
1371 std::unique_ptr
<Type
> field_type
;
1375 Analysis::NodeMapping mappings
;
1378 // Returns whether tuple field has outer attributes.
1379 bool has_outer_attributes () const { return !outer_attrs
.empty (); }
1381 /* Returns whether tuple field has a non-default visibility (i.e. a public
1383 bool has_visibility () const { return !visibility
.is_error (); }
1385 // Complete constructor
1386 TupleField (Analysis::NodeMapping mapping
, std::unique_ptr
<Type
> field_type
,
1387 Visibility vis
, location_t locus
,
1388 AST::AttrVec outer_attrs
= AST::AttrVec ());
1390 // Copy constructor with clone
1391 TupleField (TupleField
const &other
);
1393 ~TupleField () = default;
1395 // Overloaded assignment operator to clone
1396 TupleField
&operator= (TupleField
const &other
);
1398 // move constructors
1399 TupleField (TupleField
&&other
) = default;
1400 TupleField
&operator= (TupleField
&&other
) = default;
1402 // Returns whether tuple field is in an error state.
1403 bool is_error () const { return field_type
== nullptr; }
1405 std::string
as_string () const;
1407 Analysis::NodeMapping
get_mappings () const { return mappings
; }
1409 Visibility
&get_visibility () { return visibility
; }
1411 location_t
get_locus () const { return locus
; }
1413 AST::AttrVec
&get_outer_attrs () { return outer_attrs
; }
1414 HIR::Type
&get_field_type () { return *field_type
; }
1417 // Rust tuple declared using struct keyword HIR node
1418 class TupleStruct
: public Struct
1420 std::vector
<TupleField
> fields
;
1423 std::string
as_string () const override
;
1425 // Mega-constructor with all possible fields
1426 TupleStruct (Analysis::NodeMapping mappings
, std::vector
<TupleField
> fields
,
1427 Identifier struct_name
,
1428 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1429 WhereClause where_clause
, Visibility vis
,
1430 AST::AttrVec outer_attrs
, location_t locus
);
1432 void accept_vis (HIRFullVisitor
&vis
) override
;
1433 void accept_vis (HIRStmtVisitor
&vis
) override
;
1434 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1436 std::vector
<TupleField
> &get_fields () { return fields
; }
1437 const std::vector
<TupleField
> &get_fields () const { return fields
; }
1440 /* Use covariance to implement clone function as returning this object
1441 * rather than base */
1442 TupleStruct
*clone_item_impl () const override
1444 return new TupleStruct (*this);
1447 /* Use covariance to implement clone function as returning this object
1448 * rather than base */
1449 /*virtual TupleStruct* clone_statement_impl() const override {
1450 return new TupleStruct(*this);
1454 /* An item used in an "enum" tagged union - not abstract: base represents a
1455 name-only enum. Syntactically EnumItem's can have a Visibility. But not
1456 Semantically. So check there is no Visibility when lowering and make this
1457 an Item, not an VisItem. */
1458 class EnumItem
: public Item
1460 Identifier variant_name
;
1464 virtual ~EnumItem () {}
1474 EnumItem (Analysis::NodeMapping mappings
, Identifier variant_name
,
1475 AST::AttrVec outer_attrs
, location_t locus
);
1477 // Unique pointer custom clone function
1478 std::unique_ptr
<EnumItem
> clone_enum_item () const
1480 return std::unique_ptr
<EnumItem
> (clone_item_impl ());
1483 virtual std::string
as_string () const override
;
1484 virtual EnumItemKind
get_enum_item_kind () const { return Named
; };
1486 // not pure virtual as not abstract
1487 void accept_vis (HIRFullVisitor
&vis
) override
;
1488 void accept_vis (HIRStmtVisitor
&vis
) override
;
1489 // void accept_vis (HIRVisItemVisitor &vis) override;
1491 location_t
get_locus () const override
{ return locus
; }
1493 Identifier
get_identifier () const { return variant_name
; }
1495 ItemKind
get_item_kind () const override
{ return ItemKind::EnumItem
; }
1498 EnumItem
*clone_item_impl () const override
{ return new EnumItem (*this); }
1501 // A tuple item used in an "enum" tagged union
1502 class EnumItemTuple
: public EnumItem
1504 // bool has_tuple_fields;
1505 std::vector
<TupleField
> tuple_fields
;
1508 // Returns whether tuple enum item has tuple fields.
1509 bool has_tuple_fields () const { return !tuple_fields
.empty (); }
1511 EnumItemKind
get_enum_item_kind () const override
1513 return EnumItemKind::Tuple
;
1516 EnumItemTuple (Analysis::NodeMapping mappings
, Identifier variant_name
,
1517 std::vector
<TupleField
> tuple_fields
, AST::AttrVec outer_attrs
,
1520 std::string
as_string () const override
;
1522 void accept_vis (HIRFullVisitor
&vis
) override
;
1523 void accept_vis (HIRStmtVisitor
&vis
) override
;
1525 std::vector
<TupleField
> &get_tuple_fields () { return tuple_fields
; }
1528 // Clone function implementation as (not pure) virtual method
1529 EnumItemTuple
*clone_item_impl () const override
1531 return new EnumItemTuple (*this);
1535 // A struct item used in an "enum" tagged union
1536 class EnumItemStruct
: public EnumItem
1538 // bool has_struct_fields;
1539 std::vector
<StructField
> struct_fields
;
1542 // Returns whether struct enum item has struct fields.
1543 bool has_struct_fields () const { return !struct_fields
.empty (); }
1545 EnumItemKind
get_enum_item_kind () const override
1547 return EnumItemKind::Struct
;
1550 EnumItemStruct (Analysis::NodeMapping mappings
, Identifier variant_name
,
1551 std::vector
<StructField
> struct_fields
,
1552 AST::AttrVec outer_attrs
, location_t locus
);
1554 std::string
as_string () const override
;
1556 void accept_vis (HIRFullVisitor
&vis
) override
;
1557 void accept_vis (HIRStmtVisitor
&vis
) override
;
1559 std::vector
<StructField
> &get_struct_fields () { return struct_fields
; }
1562 // Clone function implementation as (not pure) virtual method
1563 EnumItemStruct
*clone_item_impl () const override
1565 return new EnumItemStruct (*this);
1569 // A discriminant (numbered enum) item used in an "enum" tagged union
1570 class EnumItemDiscriminant
: public EnumItem
1572 std::unique_ptr
<Expr
> expression
;
1575 EnumItemDiscriminant (Analysis::NodeMapping mappings
, Identifier variant_name
,
1576 std::unique_ptr
<Expr
> expr
, AST::AttrVec outer_attrs
,
1579 // Copy constructor with clone
1580 EnumItemDiscriminant (EnumItemDiscriminant
const &other
);
1582 // Overloaded assignment operator to clone
1583 EnumItemDiscriminant
&operator= (EnumItemDiscriminant
const &other
);
1585 // move constructors
1586 EnumItemDiscriminant (EnumItemDiscriminant
&&other
) = default;
1587 EnumItemDiscriminant
&operator= (EnumItemDiscriminant
&&other
) = default;
1589 EnumItemKind
get_enum_item_kind () const override
1591 return EnumItemKind::Discriminant
;
1594 std::string
as_string () const override
;
1596 void accept_vis (HIRFullVisitor
&vis
) override
;
1597 void accept_vis (HIRStmtVisitor
&vis
) override
;
1599 Expr
&get_discriminant_expression () { return *expression
; }
1601 std::unique_ptr
<Expr
> take_discriminant_expression ()
1603 return std::move (expression
);
1607 // Clone function implementation as (not pure) virtual method
1608 EnumItemDiscriminant
*clone_item_impl () const override
1610 return new EnumItemDiscriminant (*this);
1614 // HIR node for Rust "enum" - tagged union
1615 class Enum
: public VisItem
1617 Identifier enum_name
;
1619 // bool has_generics;
1620 // Generics generic_params;
1621 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1623 // bool has_where_clause;
1624 WhereClause where_clause
;
1626 std::vector
<std::unique_ptr
<EnumItem
>> items
;
1631 std::string
as_string () const override
;
1633 // Returns whether "enum" has generic parameters.
1634 bool has_generics () const { return !generic_params
.empty (); }
1636 // Returns whether "enum" has a where clause.
1637 bool has_where_clause () const { return !where_clause
.is_empty (); }
1639 /* Returns whether enum is a "zero-variant" (no possible variant) enum,
1640 * which cannot be instantiated. */
1641 bool is_zero_variant () const { return items
.empty (); }
1644 Enum (Analysis::NodeMapping mappings
, Identifier enum_name
, Visibility vis
,
1645 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1646 WhereClause where_clause
, std::vector
<std::unique_ptr
<EnumItem
>> items
,
1647 AST::AttrVec outer_attrs
, location_t locus
);
1649 // TODO: constructor with less arguments
1651 // Copy constructor with vector clone
1652 Enum (Enum
const &other
);
1654 // Overloaded assignment operator with vector clone
1655 Enum
&operator= (Enum
const &other
);
1657 // Move constructors
1658 Enum (Enum
&&other
) = default;
1659 Enum
&operator= (Enum
&&other
) = default;
1661 location_t
get_locus () const override final
{ return locus
; }
1663 void accept_vis (HIRFullVisitor
&vis
) override
;
1664 void accept_vis (HIRStmtVisitor
&vis
) override
;
1665 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1667 Identifier
get_identifier () const { return enum_name
; }
1668 ItemKind
get_item_kind () const override
{ return ItemKind::Enum
; }
1670 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1672 return generic_params
;
1675 const std::vector
<std::unique_ptr
<EnumItem
>> &get_variants () const
1680 std::vector
<std::unique_ptr
<EnumItem
>> &get_variants () { return items
; }
1681 WhereClause
&get_where_clause () { return where_clause
; }
1684 /* Use covariance to implement clone function as returning this object
1685 * rather than base */
1686 Enum
*clone_item_impl () const override
{ return new Enum (*this); }
1688 /* Use covariance to implement clone function as returning this object
1689 * rather than base */
1690 /*virtual Enum* clone_statement_impl() const override {
1691 return new Enum(*this);
1695 // Rust untagged union used for C compat HIR node
1696 class Union
: public VisItem
1698 Identifier union_name
;
1700 // bool has_generics;
1701 // Generics generic_params;
1702 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
1704 // bool has_where_clause;
1705 WhereClause where_clause
;
1707 std::vector
<StructField
> variants
;
1712 std::string
as_string () const override
;
1714 // Returns whether union has generic params.
1715 bool has_generics () const { return !generic_params
.empty (); }
1717 // Returns whether union has where clause.
1718 bool has_where_clause () const { return !where_clause
.is_empty (); }
1720 Union (Analysis::NodeMapping mappings
, Identifier union_name
, Visibility vis
,
1721 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1722 WhereClause where_clause
, std::vector
<StructField
> variants
,
1723 AST::AttrVec outer_attrs
, location_t locus
);
1725 // copy constructor with vector clone
1726 Union (Union
const &other
);
1728 // overloaded assignment operator with vector clone
1729 Union
&operator= (Union
const &other
);
1731 // move constructors
1732 Union (Union
&&other
) = default;
1733 Union
&operator= (Union
&&other
) = default;
1735 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1737 return generic_params
;
1740 Identifier
get_identifier () const { return union_name
; }
1742 location_t
get_locus () const override final
{ return locus
; }
1744 void accept_vis (HIRFullVisitor
&vis
) override
;
1745 void accept_vis (HIRStmtVisitor
&vis
) override
;
1746 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1748 std::vector
<StructField
> &get_variants () { return variants
; }
1750 WhereClause
&get_where_clause () { return where_clause
; }
1752 ItemKind
get_item_kind () const override
{ return ItemKind::Union
; }
1755 /* Use covariance to implement clone function as returning this object
1756 * rather than base */
1757 Union
*clone_item_impl () const override
{ return new Union (*this); }
1760 class ConstantItem
: public VisItem
, public ImplItem
1762 Identifier identifier
;
1763 std::unique_ptr
<Type
> type
;
1764 std::unique_ptr
<Expr
> const_expr
;
1768 std::string
as_string () const override
;
1770 ConstantItem (Analysis::NodeMapping mappings
, Identifier ident
,
1771 Visibility vis
, std::unique_ptr
<Type
> type
,
1772 std::unique_ptr
<Expr
> const_expr
, AST::AttrVec outer_attrs
,
1775 ConstantItem (ConstantItem
const &other
);
1777 // Overload assignment operator to clone
1778 ConstantItem
&operator= (ConstantItem
const &other
);
1780 // move constructors
1781 ConstantItem (ConstantItem
&&other
) = default;
1782 ConstantItem
&operator= (ConstantItem
&&other
) = default;
1784 // Returns whether constant item is an "unnamed" (wildcard underscore used
1785 // as identifier) constant.
1786 bool is_unnamed () const
1788 return identifier
.as_string () == std::string ("_");
1791 location_t
get_locus () const override final
{ return locus
; }
1793 void accept_vis (HIRFullVisitor
&vis
) override
;
1794 void accept_vis (HIRStmtVisitor
&vis
) override
;
1795 void accept_vis (HIRImplVisitor
&vis
) override
;
1796 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1804 Expr
&get_expr () { return *const_expr
; }
1806 Identifier
get_identifier () const { return identifier
; }
1808 Analysis::NodeMapping
get_impl_mappings () const override
1810 return get_mappings ();
1813 ImplItemType
get_impl_item_type () const override final
1815 return ImplItem::ImplItemType::CONSTANT
;
1818 ItemKind
get_item_kind () const override
{ return ItemKind::Constant
; }
1820 std::string
get_impl_item_name () const override final
1822 return get_identifier ().as_string ();
1826 /* Use covariance to implement clone function as returning this object
1827 * rather than base */
1828 ConstantItem
*clone_item_impl () const override
1830 return new ConstantItem (*this);
1833 /* Use covariance to implement clone function as returning this object
1834 * rather than base */
1835 ConstantItem
*clone_inherent_impl_item_impl () const override
1837 return new ConstantItem (*this);
1841 /* Static item HIR node - items within module scope with fixed storage
1843 class StaticItem
: public VisItem
1847 std::unique_ptr
<Type
> type
;
1848 std::unique_ptr
<Expr
> expr
;
1852 std::string
as_string () const override
;
1854 StaticItem (Analysis::NodeMapping mappings
, Identifier name
, Mutability mut
,
1855 std::unique_ptr
<Type
> type
, std::unique_ptr
<Expr
> expr
,
1856 Visibility vis
, AST::AttrVec outer_attrs
, location_t locus
);
1858 // Copy constructor with clone
1859 StaticItem (StaticItem
const &other
);
1861 // Overloaded assignment operator to clone
1862 StaticItem
&operator= (StaticItem
const &other
);
1864 // move constructors
1865 StaticItem (StaticItem
&&other
) = default;
1866 StaticItem
&operator= (StaticItem
&&other
) = default;
1868 location_t
get_locus () const override final
{ return locus
; }
1870 void accept_vis (HIRFullVisitor
&vis
) override
;
1871 void accept_vis (HIRStmtVisitor
&vis
) override
;
1872 void accept_vis (HIRVisItemVisitor
&vis
) override
;
1874 Identifier
get_identifier () const { return name
; }
1876 Mutability
get_mut () const { return mut
; }
1878 bool is_mut () const { return mut
== Mutability::Mut
; }
1892 ItemKind
get_item_kind () const override
{ return ItemKind::Static
; }
1895 StaticItem
*clone_item_impl () const override
1897 return new StaticItem (*this);
1901 // Function declaration in traits
1902 class TraitFunctionDecl
1905 FunctionQualifiers qualifiers
;
1906 Identifier function_name
;
1907 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
1908 std::vector
<FunctionParam
> function_params
;
1909 std::unique_ptr
<Type
> return_type
;
1910 WhereClause where_clause
;
1911 tl::optional
<SelfParam
> self
;
1915 TraitFunctionDecl (Identifier function_name
, FunctionQualifiers qualifiers
,
1916 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
1917 tl::optional
<SelfParam
> self
,
1918 std::vector
<FunctionParam
> function_params
,
1919 std::unique_ptr
<Type
> return_type
,
1920 WhereClause where_clause
);
1922 // Copy constructor with clone
1923 TraitFunctionDecl (TraitFunctionDecl
const &other
);
1925 ~TraitFunctionDecl () = default;
1927 // Overloaded assignment operator with clone
1928 TraitFunctionDecl
&operator= (TraitFunctionDecl
const &other
);
1930 // move constructors
1931 TraitFunctionDecl (TraitFunctionDecl
&&other
) = default;
1932 TraitFunctionDecl
&operator= (TraitFunctionDecl
&&other
) = default;
1934 std::string
as_string () const;
1936 // Returns whether function decl has generic parameters.
1937 bool has_generics () const { return !generic_params
.empty (); }
1939 // Returns whether function decl has regular parameters.
1940 bool has_params () const { return !function_params
.empty (); }
1942 // Returns whether function has return type (otherwise is void).
1943 bool has_return_type () const { return return_type
!= nullptr; }
1945 // Returns whether function has a where clause.
1946 bool has_where_clause () const { return !where_clause
.is_empty (); }
1948 WhereClause
&get_where_clause () { return where_clause
; }
1950 bool is_method () const { return self
.has_value (); }
1952 SelfParam
&get_self_unchecked () { return self
.value (); }
1953 const SelfParam
&get_self_unchecked () const { return self
.value (); }
1955 tl::optional
<SelfParam
> &get_self () { return self
; }
1956 const tl::optional
<SelfParam
> &get_self () const { return self
; }
1958 Identifier
get_function_name () const { return function_name
; }
1960 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
1962 return generic_params
;
1965 Type
&get_return_type () { return *return_type
; }
1967 std::vector
<FunctionParam
> &get_function_params () { return function_params
; }
1969 const FunctionQualifiers
&get_qualifiers () const { return qualifiers
; }
1972 // Actual trait item function declaration within traits
1973 class TraitItemFunc
: public TraitItem
1975 AST::AttrVec outer_attrs
;
1976 TraitFunctionDecl decl
;
1977 std::unique_ptr
<BlockExpr
> block_expr
;
1981 // Returns whether function has a definition or is just a declaration.
1982 bool has_definition () const { return block_expr
!= nullptr; }
1984 TraitItemFunc (Analysis::NodeMapping mappings
, TraitFunctionDecl decl
,
1985 std::unique_ptr
<BlockExpr
> block_expr
,
1986 AST::AttrVec outer_attrs
, location_t locus
);
1988 // Copy constructor with clone
1989 TraitItemFunc (TraitItemFunc
const &other
);
1991 // Overloaded assignment operator to clone
1992 TraitItemFunc
&operator= (TraitItemFunc
const &other
);
1994 // move constructors
1995 TraitItemFunc (TraitItemFunc
&&other
) = default;
1996 TraitItemFunc
&operator= (TraitItemFunc
&&other
) = default;
1998 std::string
as_string () const override
;
2000 location_t
get_locus () const { return locus
; }
2002 void accept_vis (HIRFullVisitor
&vis
) override
;
2003 void accept_vis (HIRTraitItemVisitor
&vis
) override
;
2005 TraitFunctionDecl
&get_decl () { return decl
; }
2007 const TraitFunctionDecl
&get_decl () const { return decl
; }
2009 BlockExpr
&get_block_expr () { return *block_expr
; }
2011 const std::string
trait_identifier () const override final
2013 return decl
.get_function_name ().as_string ();
2016 TraitItemKind
get_item_kind () const override final
2018 return TraitItemKind::FUNC
;
2021 AST::AttrVec
&get_outer_attrs () override final
{ return outer_attrs
; }
2022 const AST::AttrVec
&get_outer_attrs () const override final
2027 location_t
get_trait_locus () const override
{ return get_locus (); }
2030 // Clone function implementation as (not pure) virtual method
2031 TraitItemFunc
*clone_trait_item_impl () const override
2033 return new TraitItemFunc (*this);
2037 // Constant item within traits
2038 class TraitItemConst
: public TraitItem
2040 AST::AttrVec outer_attrs
;
2042 std::unique_ptr
<Type
> type
;
2043 std::unique_ptr
<Expr
> expr
;
2047 // Whether the constant item has an associated expression.
2048 bool has_expression () const { return expr
!= nullptr; }
2050 TraitItemConst (Analysis::NodeMapping mappings
, Identifier name
,
2051 std::unique_ptr
<Type
> type
, std::unique_ptr
<Expr
> expr
,
2052 AST::AttrVec outer_attrs
, location_t locus
);
2054 // Copy constructor with clones
2055 TraitItemConst (TraitItemConst
const &other
);
2057 // Overloaded assignment operator to clone
2058 TraitItemConst
&operator= (TraitItemConst
const &other
);
2060 // move constructors
2061 TraitItemConst (TraitItemConst
&&other
) = default;
2062 TraitItemConst
&operator= (TraitItemConst
&&other
) = default;
2064 std::string
as_string () const override
;
2066 location_t
get_locus () const { return locus
; }
2068 void accept_vis (HIRFullVisitor
&vis
) override
;
2069 void accept_vis (HIRTraitItemVisitor
&vis
) override
;
2071 Identifier
get_name () const { return name
; }
2073 bool has_type () const { return expr
!= nullptr; }
2075 bool has_expr () const { return expr
!= nullptr; }
2089 const std::string
trait_identifier () const override final
2091 return name
.as_string ();
2094 TraitItemKind
get_item_kind () const override final
2096 return TraitItemKind::CONST
;
2099 AST::AttrVec
&get_outer_attrs () override final
{ return outer_attrs
; }
2100 const AST::AttrVec
&get_outer_attrs () const override final
2105 location_t
get_trait_locus () const override
{ return get_locus (); }
2108 // Clone function implementation as (not pure) virtual method
2109 TraitItemConst
*clone_trait_item_impl () const override
2111 return new TraitItemConst (*this);
2115 // Type items within traits
2116 class TraitItemType
: public TraitItem
2118 AST::AttrVec outer_attrs
;
2121 std::vector
<std::unique_ptr
<TypeParamBound
>>
2122 type_param_bounds
; // inlined form
2126 // Returns whether trait item type has type param bounds.
2127 bool has_type_param_bounds () const { return !type_param_bounds
.empty (); }
2129 TraitItemType (Analysis::NodeMapping mappings
, Identifier name
,
2130 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
,
2131 AST::AttrVec outer_attrs
, location_t locus
);
2133 // Copy constructor with vector clone
2134 TraitItemType (TraitItemType
const &other
);
2136 // Overloaded assignment operator with vector clone
2137 TraitItemType
&operator= (TraitItemType
const &other
);
2139 // default move constructors
2140 TraitItemType (TraitItemType
&&other
) = default;
2141 TraitItemType
&operator= (TraitItemType
&&other
) = default;
2143 std::string
as_string () const override
;
2145 location_t
get_locus () const { return locus
; }
2147 void accept_vis (HIRFullVisitor
&vis
) override
;
2148 void accept_vis (HIRTraitItemVisitor
&vis
) override
;
2150 Identifier
get_name () const { return name
; }
2152 std::vector
<std::unique_ptr
<TypeParamBound
>> &get_type_param_bounds ()
2154 return type_param_bounds
;
2157 const std::string
trait_identifier () const override final
2159 return name
.as_string ();
2162 TraitItemKind
get_item_kind () const override final
2164 return TraitItemKind::TYPE
;
2167 AST::AttrVec
&get_outer_attrs () override final
{ return outer_attrs
; }
2168 const AST::AttrVec
&get_outer_attrs () const override final
2173 location_t
get_trait_locus () const override
{ return get_locus (); }
2176 // Clone function implementation as (not pure) virtual method
2177 TraitItemType
*clone_trait_item_impl () const override
2179 return new TraitItemType (*this);
2183 // Rust trait item declaration HIR node
2184 class Trait
: public VisItem
2188 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
2189 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
;
2190 WhereClause where_clause
;
2191 std::vector
<std::unique_ptr
<TraitItem
>> trait_items
;
2195 std::string
as_string () const override
;
2197 // Returns whether trait has generic parameters.
2198 bool has_generics () const { return !generic_params
.empty (); }
2200 // Returns whether trait has type parameter bounds.
2201 bool has_type_param_bounds () const { return !type_param_bounds
.empty (); }
2203 // Returns whether trait has where clause.
2204 bool has_where_clause () const { return !where_clause
.is_empty (); }
2206 // Returns whether trait has trait items.
2207 bool has_trait_items () const { return !trait_items
.empty (); }
2209 std::vector
<std::unique_ptr
<TraitItem
>> &get_trait_items ()
2214 WhereClause
&get_where_clause () { return where_clause
; }
2216 Identifier
get_name () const { return name
; }
2217 bool is_unsafe () const { return unsafety
== Unsafety::Unsafe
; }
2220 Trait (Analysis::NodeMapping mappings
, Identifier name
, Unsafety unsafety
,
2221 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
2222 std::vector
<std::unique_ptr
<TypeParamBound
>> type_param_bounds
,
2223 WhereClause where_clause
,
2224 std::vector
<std::unique_ptr
<TraitItem
>> trait_items
, Visibility vis
,
2225 AST::AttrVec outer_attrs
, location_t locus
);
2227 // Copy constructor with vector clone
2228 Trait (Trait
const &other
);
2230 // Overloaded assignment operator with vector clone
2231 Trait
&operator= (Trait
const &other
);
2233 // default move constructors
2234 Trait (Trait
&&other
) = default;
2235 Trait
&operator= (Trait
&&other
) = default;
2237 location_t
get_locus () const override final
{ return locus
; }
2239 void accept_vis (HIRFullVisitor
&vis
) override
;
2240 void accept_vis (HIRStmtVisitor
&vis
) override
;
2241 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2243 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2245 return generic_params
;
2248 const std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params () const
2250 return generic_params
;
2253 std::vector
<std::unique_ptr
<TypeParamBound
>> &get_type_param_bounds ()
2255 return type_param_bounds
;
2258 const std::vector
<std::unique_ptr
<TypeParamBound
>> &
2259 get_type_param_bounds () const
2261 return type_param_bounds
;
2264 ItemKind
get_item_kind () const override
{ return ItemKind::Trait
; }
2267 /* Use covariance to implement clone function as returning this object
2268 * rather than base */
2269 Trait
*clone_item_impl () const override
{ return new Trait (*this); }
2272 class ImplBlock
: public VisItem
, public WithInnerAttrs
2274 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
;
2275 std::unique_ptr
<Type
> impl_type
;
2276 std::unique_ptr
<TypePath
> trait_ref
;
2277 WhereClause where_clause
;
2278 BoundPolarity polarity
;
2280 std::vector
<std::unique_ptr
<ImplItem
>> impl_items
;
2284 ImplBlock (Analysis::NodeMapping mappings
,
2285 std::vector
<std::unique_ptr
<ImplItem
>> impl_items
,
2286 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
2287 std::unique_ptr
<Type
> impl_type
,
2288 std::unique_ptr
<TypePath
> trait_ref
, WhereClause where_clause
,
2289 BoundPolarity polarity
, Visibility vis
, AST::AttrVec inner_attrs
,
2290 AST::AttrVec outer_attrs
, location_t locus
, bool unsafe
= false);
2292 ImplBlock (ImplBlock
const &other
);
2294 ImplBlock
&operator= (ImplBlock
const &other
);
2296 ImplBlock (ImplBlock
&&other
) = default;
2297 ImplBlock
&operator= (ImplBlock
&&other
) = default;
2299 std::string
as_string () const override
;
2301 // Returns whether inherent impl block has inherent impl items.
2302 bool has_impl_items () const { return !impl_items
.empty (); }
2304 bool is_unsafe () const { return unsafe
; }
2306 void accept_vis (HIRFullVisitor
&vis
) override
;
2307 void accept_vis (HIRStmtVisitor
&vis
) override
;
2308 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2310 std::vector
<std::unique_ptr
<ImplItem
>> &get_impl_items ()
2315 const std::vector
<std::unique_ptr
<ImplItem
>> &get_impl_items () const
2320 // Returns whether impl has generic parameters.
2321 bool has_generics () const { return !generic_params
.empty (); }
2323 // Returns whether impl has where clause.
2324 bool has_where_clause () const { return !where_clause
.is_empty (); }
2326 // Returns the polarity of the impl.
2327 BoundPolarity
get_polarity () const { return polarity
; }
2329 location_t
get_locus () const override final
{ return locus
; }
2333 rust_assert (impl_type
);
2337 bool has_type () { return impl_type
!= nullptr; }
2339 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2341 return generic_params
;
2344 bool has_trait_ref () const { return trait_ref
!= nullptr; }
2346 TypePath
&get_trait_ref () { return *trait_ref
; }
2348 WhereClause
&get_where_clause () { return where_clause
; }
2350 ItemKind
get_item_kind () const override
{ return ItemKind::Impl
; }
2353 ImplBlock
*clone_item_impl () const override
{ return new ImplBlock (*this); }
2356 // Abstract base class for an item used inside an extern block
2357 class ExternalItem
: public Node
2359 Analysis::NodeMapping mappings
;
2360 AST::AttrVec outer_attrs
;
2361 Visibility visibility
;
2362 Identifier item_name
;
2366 enum class ExternKind
2373 virtual ~ExternalItem () {}
2375 BaseKind
get_hir_kind () override final
{ return EXTERNAL
; }
2377 virtual ExternKind
get_extern_kind () = 0;
2379 // Returns whether item has outer attributes.
2380 bool has_outer_attrs () const { return !outer_attrs
.empty (); }
2382 // Returns whether item has non-default visibility.
2383 bool has_visibility () const { return !visibility
.is_error (); }
2385 // Unique pointer custom clone function
2386 std::unique_ptr
<ExternalItem
> clone_external_item () const
2388 return std::unique_ptr
<ExternalItem
> (clone_external_item_impl ());
2391 virtual std::string
as_string () const;
2393 location_t
get_locus () const { return locus
; }
2395 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
2396 virtual void accept_vis (HIRExternalItemVisitor
&vis
) = 0;
2398 Visibility
&get_visibility () { return visibility
; }
2399 Analysis::NodeMapping
get_mappings () const { return mappings
; }
2401 Identifier
get_item_name () const { return item_name
; }
2403 AST::AttrVec
&get_outer_attrs () { return outer_attrs
; }
2406 ExternalItem (Analysis::NodeMapping mappings
, Identifier item_name
,
2407 Visibility vis
, AST::AttrVec outer_attrs
, location_t locus
);
2410 ExternalItem (ExternalItem
const &other
);
2412 // Overloaded assignment operator to clone
2413 ExternalItem
&operator= (ExternalItem
const &other
);
2415 // move constructors
2416 ExternalItem (ExternalItem
&&other
) = default;
2417 ExternalItem
&operator= (ExternalItem
&&other
) = default;
2419 // Clone function implementation as pure virtual method
2420 virtual ExternalItem
*clone_external_item_impl () const = 0;
2423 // A static item used in an extern block
2424 class ExternalStaticItem
: public ExternalItem
2427 std::unique_ptr
<Type
> item_type
;
2430 ExternalStaticItem (Analysis::NodeMapping mappings
, Identifier item_name
,
2431 std::unique_ptr
<Type
> item_type
, Mutability mut
,
2432 Visibility vis
, AST::AttrVec outer_attrs
,
2436 ExternalStaticItem (ExternalStaticItem
const &other
);
2438 // Overloaded assignment operator to clone
2439 ExternalStaticItem
&operator= (ExternalStaticItem
const &other
);
2441 // move constructors
2442 ExternalStaticItem (ExternalStaticItem
&&other
) = default;
2443 ExternalStaticItem
&operator= (ExternalStaticItem
&&other
) = default;
2445 std::string
as_string () const override
;
2447 void accept_vis (HIRFullVisitor
&vis
) override
;
2448 void accept_vis (HIRExternalItemVisitor
&vis
) override
;
2450 bool is_mut () const { return mut
== Mutability::Mut
; }
2452 Mutability
get_mut () { return mut
; }
2454 Type
&get_item_type () { return *item_type
; }
2456 ExternKind
get_extern_kind () override
{ return ExternKind::Static
; }
2459 /* Use covariance to implement clone function as returning this object
2460 * rather than base */
2461 ExternalStaticItem
*clone_external_item_impl () const override
2463 return new ExternalStaticItem (*this);
2467 // A named function parameter used in external functions
2468 struct NamedFunctionParam
2472 std::unique_ptr
<Type
> param_type
;
2473 Analysis::NodeMapping mappings
;
2476 bool has_name () const { return name
.as_string () != "_"; }
2478 NamedFunctionParam (Analysis::NodeMapping mappings
, Identifier name
,
2479 std::unique_ptr
<Type
> param_type
);
2482 NamedFunctionParam (NamedFunctionParam
const &other
);
2484 ~NamedFunctionParam () = default;
2486 // Overloaded assignment operator to clone
2487 NamedFunctionParam
&operator= (NamedFunctionParam
const &other
);
2489 // move constructors
2490 NamedFunctionParam (NamedFunctionParam
&&other
) = default;
2491 NamedFunctionParam
&operator= (NamedFunctionParam
&&other
) = default;
2493 std::string
as_string () const;
2495 Identifier
get_param_name () const { return name
; }
2499 rust_assert (param_type
);
2503 Analysis::NodeMapping
get_mappings () const { return mappings
; }
2506 // A function item used in an extern block
2507 class ExternalFunctionItem
: public ExternalItem
2509 // bool has_generics;
2510 // Generics generic_params;
2511 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
; // inlined
2513 // bool has_return_type;
2514 // FunctionReturnType return_type;
2515 std::unique_ptr
<Type
> return_type
; // inlined
2517 // bool has_where_clause;
2518 WhereClause where_clause
;
2520 std::vector
<NamedFunctionParam
> function_params
;
2524 // Returns whether item has generic parameters.
2525 bool has_generics () const { return !generic_params
.empty (); }
2527 // Returns whether item has a return type (otherwise void).
2528 bool has_return_type () const { return return_type
!= nullptr; }
2530 // Returns whether item has a where clause.
2531 bool has_where_clause () const { return !where_clause
.is_empty (); }
2533 WARN_UNUSED_RESULT
const WhereClause
&get_where_clause () const
2535 return where_clause
;
2538 ExternalFunctionItem (
2539 Analysis::NodeMapping mappings
, Identifier item_name
,
2540 std::vector
<std::unique_ptr
<GenericParam
>> generic_params
,
2541 std::unique_ptr
<Type
> return_type
, WhereClause where_clause
,
2542 std::vector
<NamedFunctionParam
> function_params
, bool has_variadics
,
2543 Visibility vis
, AST::AttrVec outer_attrs
, location_t locus
);
2545 // Copy constructor with clone
2546 ExternalFunctionItem (ExternalFunctionItem
const &other
);
2548 // Overloaded assignment operator with clone
2549 ExternalFunctionItem
&operator= (ExternalFunctionItem
const &other
);
2551 // move constructors
2552 ExternalFunctionItem (ExternalFunctionItem
&&other
) = default;
2553 ExternalFunctionItem
&operator= (ExternalFunctionItem
&&other
) = default;
2555 std::string
as_string () const override
;
2557 void accept_vis (HIRFullVisitor
&vis
) override
;
2558 void accept_vis (HIRExternalItemVisitor
&vis
) override
;
2560 std::vector
<std::unique_ptr
<GenericParam
>> &get_generic_params ()
2562 return generic_params
;
2565 Type
&get_return_type () { return *return_type
; }
2567 std::vector
<NamedFunctionParam
> &get_function_params ()
2569 return function_params
;
2572 bool is_variadic () const { return has_variadics
; }
2574 ExternKind
get_extern_kind () override
{ return ExternKind::Function
; }
2577 /* Use covariance to implement clone function as returning this object
2578 * rather than base */
2579 ExternalFunctionItem
*clone_external_item_impl () const override
2581 return new ExternalFunctionItem (*this);
2585 class ExternalTypeItem
: public ExternalItem
2588 ExternalTypeItem (Analysis::NodeMapping mappings
, Identifier item_name
,
2589 Visibility vis
, location_t locus
);
2591 ExternalTypeItem (ExternalTypeItem
const &other
);
2593 ExternalTypeItem (ExternalTypeItem
&&other
) = default;
2594 ExternalTypeItem
&operator= (ExternalTypeItem
&&other
) = default;
2595 ExternalTypeItem
&operator= (ExternalTypeItem
const &other
) = default;
2597 std::string
as_string () const override
;
2599 void accept_vis (HIRFullVisitor
&vis
) override
;
2600 void accept_vis (HIRExternalItemVisitor
&vis
) override
;
2602 ExternKind
get_extern_kind () override
{ return ExternKind::Type
; }
2605 /* Use covariance to implement clone function as returning this object
2606 * rather than base */
2607 ExternalTypeItem
*clone_external_item_impl () const override
2609 return new ExternalTypeItem (*this);
2613 // An extern block HIR node
2614 class ExternBlock
: public VisItem
, public WithInnerAttrs
2617 std::vector
<std::unique_ptr
<ExternalItem
>> extern_items
;
2621 std::string
as_string () const override
;
2623 // Returns whether extern block has extern items.
2624 bool has_extern_items () const { return !extern_items
.empty (); }
2626 ABI
get_abi () const { return abi
; }
2628 ExternBlock (Analysis::NodeMapping mappings
, ABI abi
,
2629 std::vector
<std::unique_ptr
<ExternalItem
>> extern_items
,
2630 Visibility vis
, AST::AttrVec inner_attrs
,
2631 AST::AttrVec outer_attrs
, location_t locus
);
2633 // Copy constructor with vector clone
2634 ExternBlock (ExternBlock
const &other
);
2636 // Overloaded assignment operator with vector clone
2637 ExternBlock
&operator= (ExternBlock
const &other
);
2639 // move constructors
2640 ExternBlock (ExternBlock
&&other
) = default;
2641 ExternBlock
&operator= (ExternBlock
&&other
) = default;
2643 location_t
get_locus () const override final
{ return locus
; }
2645 void accept_vis (HIRFullVisitor
&vis
) override
;
2646 void accept_vis (HIRStmtVisitor
&vis
) override
;
2647 void accept_vis (HIRVisItemVisitor
&vis
) override
;
2649 std::vector
<std::unique_ptr
<ExternalItem
>> &get_extern_items ()
2651 return extern_items
;
2654 ItemKind
get_item_kind () const override
{ return ItemKind::ExternBlock
; }
2657 /* Use covariance to implement clone function as returning this object
2658 * rather than base */
2659 ExternBlock
*clone_item_impl () const override
2661 return new ExternBlock (*this);
2664 /* Use covariance to implement clone function as returning this object
2665 * rather than base */
2666 /*virtual ExternBlock* clone_statement_impl() const override {
2667 return new ExternBlock(*this);