Skip to main content

rustc_public/ty/
def.rs

1use serde::Serialize;
2
3use crate::abi::ReprOptions;
4use crate::crate_def::{CrateDef, CrateDefType};
5use crate::mir::Body;
6use crate::ty::tys::*;
7use crate::{AssocItems, DefId, IndexedVal, Symbol, with};
8
9#[automatically_derived]
impl ::core::clone::Clone for AssocDef {
    #[inline]
    fn clone(&self) -> AssocDef {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        *self
    }
}
#[automatically_derived]
impl ::core::marker::Copy for AssocDef { }
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for AssocDef { }
#[automatically_derived]
impl ::core::cmp::PartialEq for AssocDef {
    #[inline]
    fn eq(&self, other: &AssocDef) -> bool { self.0 == other.0 }
}
#[automatically_derived]
impl ::core::cmp::Eq for AssocDef {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<DefId>;
    }
}
#[automatically_derived]
impl ::core::fmt::Debug for AssocDef {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "AssocDef",
            &&self.0)
    }
}
#[automatically_derived]
impl ::core::hash::Hash for AssocDef {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.0, state)
    }
}
#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl _serde::Serialize for AssocDef {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                _serde::Serializer::serialize_newtype_struct(__serializer,
                    "AssocDef", &self.0)
            }
        }
    };
impl CrateDef for AssocDef {
    fn def_id(&self) -> DefId { self.0 }
}crate_def! {
10    #[derive(Serialize)]
11    pub ForeignModuleDef;
12
13    #[derive(Serialize)]
14    pub ClosureDef;
15
16    #[derive(Serialize)]
17    pub CoroutineDef;
18
19    #[derive(Serialize)]
20    pub CoroutineClosureDef;
21
22    #[derive(Serialize)]
23    pub ParamDef;
24
25    #[derive(Serialize)]
26    pub BrNamedDef;
27
28    #[derive(Serialize)]
29    pub AdtDef;
30
31    #[derive(Serialize)]
32    pub AliasDef;
33
34    /// A trait's definition.
35    #[derive(Serialize)]
36    pub TraitDef;
37
38    #[derive(Serialize)]
39    pub GenericDef;
40
41    #[derive(Serialize)]
42    pub RegionDef;
43
44    #[derive(Serialize)]
45    pub CoroutineWitnessDef;
46
47    /// Hold information about an Opaque definition, particularly useful in `RPITIT`.
48    #[derive(Serialize)]
49    pub OpaqueDef;
50
51    #[derive(Serialize)]
52    pub AssocDef;
53}
54
55#[automatically_derived]
impl ::core::clone::Clone for ImplDef {
    #[inline]
    fn clone(&self) -> ImplDef {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        *self
    }
}
#[automatically_derived]
impl ::core::marker::Copy for ImplDef { }
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for ImplDef { }
#[automatically_derived]
impl ::core::cmp::PartialEq for ImplDef {
    #[inline]
    fn eq(&self, other: &ImplDef) -> bool { self.0 == other.0 }
}
#[automatically_derived]
impl ::core::cmp::Eq for ImplDef {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<DefId>;
    }
}
#[automatically_derived]
impl ::core::fmt::Debug for ImplDef {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "ImplDef",
            &&self.0)
    }
}
#[automatically_derived]
impl ::core::hash::Hash for ImplDef {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.0, state)
    }
}
#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl _serde::Serialize for ImplDef {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                _serde::Serializer::serialize_newtype_struct(__serializer,
                    "ImplDef", &self.0)
            }
        }
    };
impl CrateDef for ImplDef {
    fn def_id(&self) -> DefId { self.0 }
}
impl CrateDefType for ImplDef {}crate_def_with_ty! {
56    /// Hold information about a ForeignItem in a crate.
57    #[derive(Serialize)]
58    pub ForeignDef;
59
60    /// Hold information about a function definition in a crate.
61    #[derive(Serialize)]
62    pub FnDef;
63
64    #[derive(Serialize)]
65    pub IntrinsicDef;
66
67    #[derive(Serialize)]
68    pub FieldDef {
69        /// The field name.
70        pub name: Symbol,
71    }
72
73    #[derive(Serialize)]
74    pub ConstDef;
75
76    /// A trait impl definition.
77    #[derive(Serialize)]
78    pub ImplDef;
79}
80
81impl ForeignModuleDef {
82    pub fn module(&self) -> ForeignModule {
83        with(|cx| cx.foreign_module(*self))
84    }
85}
86
87impl ForeignDef {
88    pub fn kind(&self) -> ForeignItemKind {
89        with(|cx| cx.foreign_item_kind(*self))
90    }
91}
92
93impl FnDef {
94    // Get the function body if available.
95    pub fn body(&self) -> Option<Body> {
96        with(|ctx| ctx.has_body(self.0).then(|| ctx.mir_body(self.0)))
97    }
98
99    // Check if the function body is available.
100    pub fn has_body(&self) -> bool {
101        with(|ctx| ctx.has_body(self.0))
102    }
103
104    /// Get the information of the intrinsic if this function is a definition of one.
105    pub fn as_intrinsic(&self) -> Option<IntrinsicDef> {
106        with(|cx| cx.intrinsic(self.def_id()))
107    }
108
109    /// Check if the function is an intrinsic.
110    #[inline]
111    pub fn is_intrinsic(&self) -> bool {
112        self.as_intrinsic().is_some()
113    }
114
115    /// Get the constness of this function definition.
116    pub fn constness(&self) -> Constness {
117        with(|cx| cx.constness(*self))
118    }
119
120    /// Get the asyncness of this function definition.
121    pub fn asyncness(&self) -> Asyncness {
122        with(|cx| cx.asyncness(*self))
123    }
124
125    /// Get the function signature for this function definition.
126    pub fn fn_sig(&self) -> PolyFnSig {
127        let kind = self.ty().kind();
128        kind.fn_sig().unwrap()
129    }
130
131    /// Get the generics of this function definition.
132    pub fn generics_of(&self) -> Generics {
133        with(|cx| cx.generics_of(self.0))
134    }
135
136    /// Get the associated item information if this function is one.
137    pub fn associated_item(&self) -> Option<AssocItem> {
138        with(|cx| cx.associated_item(self.0))
139    }
140}
141
142impl IntrinsicDef {
143    /// Returns the plain name of the intrinsic.
144    /// e.g., `transmute` for `core::intrinsics::transmute`.
145    pub fn fn_name(&self) -> Symbol {
146        with(|cx| cx.intrinsic_name(*self))
147    }
148
149    /// Returns whether the intrinsic has no meaningful body and all backends
150    /// need to shim all calls to it.
151    pub fn must_be_overridden(&self) -> bool {
152        with(|cx| !cx.has_body(self.0))
153    }
154}
155
156impl From<IntrinsicDef> for FnDef {
157    fn from(def: IntrinsicDef) -> Self {
158        FnDef(def.0)
159    }
160}
161
162impl ClosureDef {
163    /// Retrieves the body of the closure definition. Returns None if the body
164    /// isn't available.
165    pub fn body(&self) -> Option<Body> {
166        with(|ctx| ctx.has_body(self.0).then(|| ctx.mir_body(self.0)))
167    }
168}
169
170impl CoroutineDef {
171    /// Retrieves the body of the coroutine definition. Returns None if the body
172    /// isn't available.
173    pub fn body(&self) -> Option<Body> {
174        with(|cx| cx.has_body(self.0).then(|| cx.mir_body(self.0)))
175    }
176
177    pub fn discriminant_for_variant(&self, args: &GenericArgs, idx: VariantIdx) -> Discr {
178        with(|cx| cx.coroutine_discr_for_variant(*self, args, idx))
179    }
180}
181
182impl AdtDef {
183    pub fn kind(&self) -> AdtKind {
184        with(|cx| cx.adt_kind(*self))
185    }
186
187    /// Retrieve the type of this Adt.
188    pub fn ty(&self) -> Ty {
189        with(|cx| cx.def_ty(self.0))
190    }
191
192    /// Retrieve the type of this Adt by instantiating and normalizing it with the given arguments.
193    ///
194    /// This will assume the type can be instantiated with these arguments.
195    pub fn ty_with_args(&self, args: &GenericArgs) -> Ty {
196        with(|cx| cx.def_ty_with_args(self.0, args))
197    }
198
199    pub fn is_box(&self) -> bool {
200        with(|cx| cx.adt_is_box(*self))
201    }
202
203    pub fn is_simd(&self) -> bool {
204        with(|cx| cx.adt_is_simd(*self))
205    }
206
207    /// The number of variants in this ADT.
208    pub fn num_variants(&self) -> usize {
209        with(|cx| cx.adt_variants_len(*self))
210    }
211
212    /// Retrieve the variants in this ADT.
213    pub fn variants(&self) -> Vec<VariantDef> {
214        self.variants_iter().collect()
215    }
216
217    /// Iterate over the variants in this ADT.
218    pub fn variants_iter(&self) -> impl Iterator<Item = VariantDef> {
219        (0..self.num_variants())
220            .map(|idx| VariantDef { idx: VariantIdx::to_val(idx), adt_def: *self })
221    }
222
223    pub fn variant(&self, idx: VariantIdx) -> Option<VariantDef> {
224        (idx.to_index() < self.num_variants()).then_some(VariantDef { idx, adt_def: *self })
225    }
226
227    pub fn repr(&self) -> ReprOptions {
228        with(|cx| cx.adt_repr(*self))
229    }
230
231    pub fn discriminant_for_variant(&self, idx: VariantIdx) -> Discr {
232        with(|cx| cx.adt_discr_for_variant(*self, idx))
233    }
234
235    /// Get the generics of this ADT definition.
236    pub fn generics_of(&self) -> Generics {
237        with(|cx| cx.generics_of(self.0))
238    }
239
240    /// Retrieve the inherent implementations for this ADT.
241    pub fn inherent_impls(&self) -> Vec<ImplDef> {
242        with(|cx| cx.inherent_impls(*self))
243    }
244}
245
246/// Definition of a variant, which can be either a struct / union field or an enum variant.
247#[derive(#[automatically_derived]
impl ::core::clone::Clone for VariantDef {
    #[inline]
    fn clone(&self) -> VariantDef {
        let _: ::core::clone::AssertParamIsClone<VariantIdx>;
        let _: ::core::clone::AssertParamIsClone<AdtDef>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for VariantDef { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for VariantDef {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "VariantDef",
            "idx", &self.idx, "adt_def", &&self.adt_def)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for VariantDef {
    #[inline]
    fn eq(&self, other: &VariantDef) -> bool {
        self.idx == other.idx && self.adt_def == other.adt_def
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for VariantDef {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<VariantIdx>;
        let _: ::core::cmp::AssertParamIsEq<AdtDef>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for VariantDef {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.idx, state);
        ::core::hash::Hash::hash(&self.adt_def, state)
    }
}Hash, #[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl _serde::Serialize for VariantDef {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "VariantDef", false as usize + 1 + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "idx", &self.idx)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "adt_def", &self.adt_def)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
248pub struct VariantDef {
249    /// The variant index.
250    pub(crate) idx: VariantIdx,
251    /// The data type where this variant comes from.
252    /// For now, we use this to retrieve information about the variant itself so we don't need to
253    /// cache more information.
254    pub(crate) adt_def: AdtDef,
255}
256
257impl VariantDef {
258    /// The name of the variant, struct or union.
259    ///
260    /// This will not include the name of the enum or qualified path.
261    pub fn name(&self) -> Symbol {
262        with(|cx| cx.variant_name(*self))
263    }
264
265    /// Retrieve all the fields in this variant.
266    // We expect user to cache this and use it directly since today it is expensive to generate all
267    // fields name.
268    pub fn fields(&self) -> Vec<FieldDef> {
269        with(|cx| cx.variant_fields(*self))
270    }
271
272    /// Returns the variant index.
273    pub fn idx(&self) -> VariantIdx {
274        self.idx
275    }
276
277    /// Returns the `AdtDef` which this variant comes from.
278    pub fn adt_def(&self) -> AdtDef {
279        self.adt_def
280    }
281}
282
283impl TraitDef {
284    pub fn declaration(trait_def: &TraitDef) -> TraitDecl {
285        with(|cx| cx.trait_decl(trait_def))
286    }
287
288    pub fn associated_items(&self) -> AssocItems {
289        with(|cx| cx.associated_items(self.def_id()))
290    }
291}
292
293impl ImplDef {
294    /// Retrieve information about this implementation.
295    pub fn trait_impl(&self) -> ImplTrait {
296        with(|cx| cx.trait_impl(self))
297    }
298
299    pub fn associated_items(&self) -> AssocItems {
300        with(|cx| cx.associated_items(self.def_id()))
301    }
302
303    /// Get the generics of this implementation.
304    pub fn generics_of(&self) -> Generics {
305        with(|cx| cx.generics_of(self.0))
306    }
307}
308
309#[derive(#[automatically_derived]
impl ::core::clone::Clone for GenericParamDef {
    #[inline]
    fn clone(&self) -> GenericParamDef {
        GenericParamDef {
            name: ::core::clone::Clone::clone(&self.name),
            def_id: ::core::clone::Clone::clone(&self.def_id),
            index: ::core::clone::Clone::clone(&self.index),
            pure_wrt_drop: ::core::clone::Clone::clone(&self.pure_wrt_drop),
            kind: ::core::clone::Clone::clone(&self.kind),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for GenericParamDef {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field5_finish(f,
            "GenericParamDef", "name", &self.name, "def_id", &self.def_id,
            "index", &self.index, "pure_wrt_drop", &self.pure_wrt_drop,
            "kind", &&self.kind)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for GenericParamDef {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Symbol>;
        let _: ::core::cmp::AssertParamIsEq<GenericDef>;
        let _: ::core::cmp::AssertParamIsEq<u32>;
        let _: ::core::cmp::AssertParamIsEq<bool>;
        let _: ::core::cmp::AssertParamIsEq<GenericParamDefKind>;
    }
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for GenericParamDef {
    #[inline]
    fn eq(&self, other: &GenericParamDef) -> bool {
        self.index == other.index && self.pure_wrt_drop == other.pure_wrt_drop
                    && self.name == other.name && self.def_id == other.def_id &&
            self.kind == other.kind
    }
}PartialEq, #[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl _serde::Serialize for GenericParamDef {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "GenericParamDef", false as usize + 1 + 1 + 1 + 1 + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "name", &self.name)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "def_id", &self.def_id)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "index", &self.index)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "pure_wrt_drop", &self.pure_wrt_drop)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "kind", &self.kind)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
310pub struct GenericParamDef {
311    pub name: Symbol,
312    pub def_id: GenericDef,
313    pub index: u32,
314    pub pure_wrt_drop: bool,
315    pub kind: GenericParamDefKind,
316}