1 // Copyright (C) 2021-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_TRAIT_REF_H
20 #define RUST_HIR_TRAIT_REF_H
22 #include "rust-hir-full.h"
23 #include "rust-tyty-visitor.h"
28 // Data Objects for the associated trait items in a structure we can work with
29 // https://doc.rust-lang.org/edition-guide/rust-2018/trait-system/associated-constants.html
30 class TypeCheckContext
;
31 class TraitItemReference
42 TraitItemReference (std::string identifier
, bool optional
, TraitItemType type
,
43 HIR::TraitItem
*hir_trait_item
, TyTy::BaseType
*self
,
44 std::vector
<TyTy::SubstitutionParamMapping
> substitutions
,
47 TraitItemReference (TraitItemReference
const &other
);
49 TraitItemReference
&operator= (TraitItemReference
const &other
);
51 static TraitItemReference
error ()
53 return TraitItemReference ("", false, ERROR
, nullptr, nullptr, {},
57 static TraitItemReference
&error_node ()
59 static TraitItemReference error
= TraitItemReference::error ();
63 bool is_error () const;
65 std::string
as_string () const;
67 static std::string
trait_item_type_as_string (TraitItemType ty
)
83 bool is_optional () const;
85 std::string
get_identifier () const;
87 TraitItemType
get_trait_item_type () const;
89 HIR::TraitItem
*get_hir_trait_item () const;
91 location_t
get_locus () const;
93 const Analysis::NodeMapping
get_mappings () const;
95 TyTy::BaseType
*get_tyty () const;
97 Analysis::NodeMapping
get_parent_trait_mappings () const;
99 // this is called when the trait is completed resolution and gives the items
100 // a chance to run their specific type resolution passes. If we call their
101 // resolution on construction it can lead to a case where the trait being
102 // resolved recursively trying to resolve the trait itself infinitely since
103 // the trait will not be stored in its own map yet
106 void associated_type_set (TyTy::BaseType
*ty
) const;
108 void associated_type_reset (bool only_projections
) const;
110 bool is_object_safe () const;
113 TyTy::ErrorType
*get_error () const;
115 TyTy::BaseType
*get_type_from_typealias (/*const*/
116 HIR::TraitItemType
&type
) const;
119 get_type_from_constant (/*const*/ HIR::TraitItemConst
&constant
) const;
121 TyTy::BaseType
*get_type_from_fn (/*const*/ HIR::TraitItemFunc
&fn
) const;
123 bool is_item_resolved () const;
124 void resolve_item (HIR::TraitItemType
&type
);
125 void resolve_item (HIR::TraitItemConst
&constant
);
126 void resolve_item (HIR::TraitItemFunc
&func
);
128 std::string identifier
;
131 HIR::TraitItem
*hir_trait_item
;
132 std::vector
<TyTy::SubstitutionParamMapping
> inherited_substitutions
;
136 *self
; // this is the implict Self TypeParam required for methods
137 Resolver::TypeCheckContext
*context
;
140 // this wraps up the HIR::Trait so we can do analysis on it
145 TraitReference (const HIR::Trait
*hir_trait_ref
,
146 std::vector
<TraitItemReference
> item_refs
,
147 std::vector
<const TraitReference
*> super_traits
,
148 std::vector
<TyTy::SubstitutionParamMapping
> substs
);
150 TraitReference (TraitReference
const &other
);
152 TraitReference
&operator= (TraitReference
const &other
);
154 TraitReference (TraitReference
&&other
) = default;
155 TraitReference
&operator= (TraitReference
&&other
) = default;
157 static TraitReference
error ()
159 return TraitReference (nullptr, {}, {}, {});
162 bool is_error () const;
164 static TraitReference
&error_node ()
166 static TraitReference trait_error_node
= TraitReference::error ();
167 return trait_error_node
;
170 location_t
get_locus () const;
172 std::string
get_name () const;
174 std::string
as_string () const;
176 const HIR::Trait
*get_hir_trait_ref () const;
178 const Analysis::NodeMapping
&get_mappings () const;
180 DefId
get_defid () const;
182 bool lookup_hir_trait_item (const HIR::TraitItem
&item
,
183 TraitItemReference
**ref
);
185 bool lookup_trait_item (const std::string
&ident
, TraitItemReference
**ref
);
187 bool lookup_trait_item_by_type (const std::string
&ident
,
188 TraitItemReference::TraitItemType type
,
189 TraitItemReference
**ref
);
191 bool lookup_trait_item_by_type (const std::string
&ident
,
192 TraitItemReference::TraitItemType type
,
193 const TraitItemReference
**ref
) const;
195 bool lookup_hir_trait_item (const HIR::TraitItem
&item
,
196 const TraitItemReference
**ref
) const;
198 bool lookup_trait_item (const std::string
&ident
,
199 const TraitItemReference
**ref
) const;
201 const TraitItemReference
*
202 lookup_trait_item (const std::string
&ident
,
203 TraitItemReference::TraitItemType type
) const;
205 size_t size () const;
207 const std::vector
<TraitItemReference
> &get_trait_items () const;
209 void get_trait_items_and_supers (
210 std::vector
<const TraitItemReference
*> &result
) const;
214 void clear_associated_types () const;
216 void clear_associated_type_projections () const;
218 bool is_equal (const TraitReference
&other
) const;
220 const std::vector
<const TraitReference
*> get_super_traits () const;
222 bool is_object_safe (bool emit_error
, location_t locus
) const;
224 bool trait_has_generics () const;
226 std::vector
<TyTy::SubstitutionParamMapping
> get_trait_substs () const;
228 bool satisfies_bound (const TraitReference
&reference
) const;
231 const HIR::Trait
*hir_trait_ref
;
232 std::vector
<TraitItemReference
> item_refs
;
233 std::vector
<const TraitReference
*> super_traits
;
234 std::vector
<TyTy::SubstitutionParamMapping
> trait_substs
;
237 class AssociatedImplTrait
240 AssociatedImplTrait (TraitReference
*trait
,
241 TyTy::TypeBoundPredicate predicate
, HIR::ImplBlock
*impl
,
242 TyTy::BaseType
*self
,
243 Resolver::TypeCheckContext
*context
);
245 TyTy::TypeBoundPredicate
&get_predicate ();
247 HIR::ImplBlock
*get_impl_block ();
249 TyTy::BaseType
*get_self ();
250 const TyTy::BaseType
*get_self () const;
252 void setup_raw_associated_types ();
255 setup_associated_types (const TyTy::BaseType
*self
,
256 const TyTy::TypeBoundPredicate
&bound
,
257 TyTy::SubstitutionArgumentMappings
*args
= nullptr);
259 void reset_associated_types ();
262 TraitReference
*trait
;
263 TyTy::TypeBoundPredicate predicate
;
264 HIR::ImplBlock
*impl
;
265 TyTy::BaseType
*self
;
266 Resolver::TypeCheckContext
*context
;
269 } // namespace Resolver
272 #endif // RUST_HIR_TRAIT_REF_H