1 // Copyright (C) 2021-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 #ifndef RUST_HIR_TRAIT_REF_H
20 #define RUST_HIR_TRAIT_REF_H
22 #include "rust-hir-full.h"
23 #include "rust-hir-type-check-util.h"
24 #include "rust-tyty-visitor.h"
29 // Data Objects for the associated trait items in a structure we can work with
30 // https://doc.rust-lang.org/edition-guide/rust-2018/trait-system/associated-constants.html
31 class TypeCheckContext
;
32 class TraitItemReference
43 TraitItemReference (std::string identifier
, bool optional
, TraitItemType type
,
44 HIR::TraitItem
*hir_trait_item
, TyTy::BaseType
*self
,
45 std::vector
<TyTy::SubstitutionParamMapping
> substitutions
,
48 TraitItemReference (TraitItemReference
const &other
);
50 TraitItemReference
&operator= (TraitItemReference
const &other
);
52 static TraitItemReference
error ()
54 return TraitItemReference ("", false, ERROR
, nullptr, nullptr, {},
58 static TraitItemReference
&error_node ()
60 static TraitItemReference error
= TraitItemReference::error ();
64 bool is_error () const;
66 std::string
as_string () const;
68 static std::string
trait_item_type_as_string (TraitItemType ty
)
84 bool is_optional () const;
86 std::string
get_identifier () const;
88 TraitItemType
get_trait_item_type () const;
90 HIR::TraitItem
*get_hir_trait_item () const;
92 Location
get_locus () const;
94 const Analysis::NodeMapping
get_mappings () const;
96 TyTy::BaseType
*get_tyty () const;
98 Analysis::NodeMapping
get_parent_trait_mappings () const;
100 // this is called when the trait is completed resolution and gives the items
101 // a chance to run their specific type resolution passes. If we call their
102 // resolution on construction it can lead to a case where the trait being
103 // resolved recursively trying to resolve the trait itself infinitely since
104 // the trait will not be stored in its own map yet
107 void associated_type_set (TyTy::BaseType
*ty
) const;
109 void associated_type_reset (bool only_projections
) const;
111 bool is_object_safe () const;
114 TyTy::ErrorType
*get_error () const;
116 TyTy::BaseType
*get_type_from_typealias (/*const*/
117 HIR::TraitItemType
&type
) const;
120 get_type_from_constant (/*const*/ HIR::TraitItemConst
&constant
) const;
122 TyTy::BaseType
*get_type_from_fn (/*const*/ HIR::TraitItemFunc
&fn
) const;
124 bool is_item_resolved () const;
125 void resolve_item (HIR::TraitItemType
&type
);
126 void resolve_item (HIR::TraitItemConst
&constant
);
127 void resolve_item (HIR::TraitItemFunc
&func
);
129 std::string identifier
;
132 HIR::TraitItem
*hir_trait_item
;
133 std::vector
<TyTy::SubstitutionParamMapping
> inherited_substitutions
;
137 *self
; // this is the implict Self TypeParam required for methods
138 Resolver::TypeCheckContext
*context
;
141 // this wraps up the HIR::Trait so we can do analysis on it
146 TraitReference (const HIR::Trait
*hir_trait_ref
,
147 std::vector
<TraitItemReference
> item_refs
,
148 std::vector
<const TraitReference
*> super_traits
,
149 std::vector
<TyTy::SubstitutionParamMapping
> substs
);
151 TraitReference (TraitReference
const &other
);
153 TraitReference
&operator= (TraitReference
const &other
);
155 TraitReference (TraitReference
&&other
) = default;
156 TraitReference
&operator= (TraitReference
&&other
) = default;
158 static TraitReference
error ()
160 return TraitReference (nullptr, {}, {}, {});
163 bool is_error () const;
165 static TraitReference
&error_node ()
167 static TraitReference trait_error_node
= TraitReference::error ();
168 return trait_error_node
;
171 Location
get_locus () const;
173 std::string
get_name () const;
175 std::string
as_string () const;
177 const HIR::Trait
*get_hir_trait_ref () const;
179 const Analysis::NodeMapping
&get_mappings () const;
181 DefId
get_defid () const;
183 bool lookup_hir_trait_item (const HIR::TraitItem
&item
,
184 TraitItemReference
**ref
);
186 bool lookup_trait_item (const std::string
&ident
, TraitItemReference
**ref
);
188 bool lookup_trait_item_by_type (const std::string
&ident
,
189 TraitItemReference::TraitItemType type
,
190 TraitItemReference
**ref
);
192 bool lookup_trait_item_by_type (const std::string
&ident
,
193 TraitItemReference::TraitItemType type
,
194 const TraitItemReference
**ref
) const;
196 bool lookup_hir_trait_item (const HIR::TraitItem
&item
,
197 const TraitItemReference
**ref
) const;
199 bool lookup_trait_item (const std::string
&ident
,
200 const TraitItemReference
**ref
) const;
202 const TraitItemReference
*
203 lookup_trait_item (const std::string
&ident
,
204 TraitItemReference::TraitItemType type
) const;
206 size_t size () const;
208 const std::vector
<TraitItemReference
> &get_trait_items () const;
210 void get_trait_items_and_supers (
211 std::vector
<const TraitItemReference
*> &result
) const;
215 void clear_associated_types () const;
217 void clear_associated_type_projections () const;
219 bool is_equal (const TraitReference
&other
) const;
221 const std::vector
<const TraitReference
*> get_super_traits () const;
223 bool is_object_safe (bool emit_error
, Location locus
) const;
225 bool trait_has_generics () const;
227 std::vector
<TyTy::SubstitutionParamMapping
> get_trait_substs () const;
229 bool satisfies_bound (const TraitReference
&reference
) const;
232 const HIR::Trait
*hir_trait_ref
;
233 std::vector
<TraitItemReference
> item_refs
;
234 std::vector
<const TraitReference
*> super_traits
;
235 std::vector
<TyTy::SubstitutionParamMapping
> trait_substs
;
238 class AssociatedImplTrait
241 AssociatedImplTrait (TraitReference
*trait
, HIR::ImplBlock
*impl
,
242 TyTy::BaseType
*self
,
243 Resolver::TypeCheckContext
*context
);
245 TraitReference
*get_trait ();
247 HIR::ImplBlock
*get_impl_block ();
249 TyTy::BaseType
*get_self ();
250 const TyTy::BaseType
*get_self () const;
253 setup_associated_types (const TyTy::BaseType
*self
,
254 const TyTy::TypeBoundPredicate
&bound
);
256 void reset_associated_types ();
259 TraitReference
*trait
;
260 HIR::ImplBlock
*impl
;
261 TyTy::BaseType
*self
;
262 Resolver::TypeCheckContext
*context
;
265 } // namespace Resolver
268 #endif // RUST_HIR_TRAIT_REF_H