Skip to main content

rustc_public/unstable/convert/stable/
abi.rs

1//! Conversion of internal Rust compiler `rustc_target` and `rustc_abi` items to stable ones.
2
3#![allow(rustc::usage_of_qualified_ty)]
4
5use rustc_abi::{ArmCall, CanonAbi, InterruptKind, X86Call};
6use rustc_middle::ty;
7use rustc_public_bridge::Tables;
8use rustc_public_bridge::context::CompilerCtxt;
9use rustc_target::callconv;
10
11use crate::abi::{
12    AddressSpace, ArgAbi, CallConvention, FieldsShape, FloatLength, FnAbi, IntegerLength,
13    IntegerType, Layout, LayoutShape, NumScalableVectors, PassMode, Primitive, ReprFlags,
14    ReprOptions, Scalar, TagEncoding, TyAndLayout, ValueAbi, VariantFields, VariantsShape,
15    WrappingRange,
16};
17use crate::compiler_interface::BridgeTys;
18use crate::target::MachineSize as Size;
19use crate::ty::{Align, VariantIdx};
20use crate::unstable::Stable;
21use crate::{IndexedVal, opaque};
22
23impl<'tcx> Stable<'tcx> for rustc_abi::VariantIdx {
24    type T = VariantIdx;
25    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
26        VariantIdx::to_val(self.as_usize())
27    }
28}
29
30impl<'tcx> Stable<'tcx> for rustc_abi::Endian {
31    type T = crate::target::Endian;
32
33    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
34        match self {
35            rustc_abi::Endian::Little => crate::target::Endian::Little,
36            rustc_abi::Endian::Big => crate::target::Endian::Big,
37        }
38    }
39}
40
41impl<'tcx> Stable<'tcx> for rustc_abi::TyAndLayout<'tcx, ty::Ty<'tcx>> {
42    type T = TyAndLayout;
43
44    fn stable<'cx>(
45        &self,
46        tables: &mut Tables<'cx, BridgeTys>,
47        cx: &CompilerCtxt<'cx, BridgeTys>,
48    ) -> Self::T {
49        TyAndLayout { ty: self.ty.stable(tables, cx), layout: self.layout.stable(tables, cx) }
50    }
51}
52
53impl<'tcx> Stable<'tcx> for rustc_abi::Layout<'tcx> {
54    type T = Layout;
55
56    fn stable<'cx>(
57        &self,
58        tables: &mut Tables<'cx, BridgeTys>,
59        cx: &CompilerCtxt<'cx, BridgeTys>,
60    ) -> Self::T {
61        tables.layout_id(cx.lift(*self))
62    }
63}
64
65impl<'tcx> Stable<'tcx> for rustc_abi::LayoutData<rustc_abi::FieldIdx, rustc_abi::VariantIdx> {
66    type T = LayoutShape;
67
68    fn stable<'cx>(
69        &self,
70        tables: &mut Tables<'cx, BridgeTys>,
71        cx: &CompilerCtxt<'cx, BridgeTys>,
72    ) -> Self::T {
73        LayoutShape {
74            fields: self.fields.stable(tables, cx),
75            variants: self.variants.stable(tables, cx),
76            abi: self.backend_repr.stable(tables, cx),
77            abi_align: self.align.abi.stable(tables, cx),
78            size: self.size.stable(tables, cx),
79        }
80    }
81}
82
83impl<'tcx> Stable<'tcx> for callconv::FnAbi<'tcx, ty::Ty<'tcx>> {
84    type T = FnAbi;
85
86    fn stable<'cx>(
87        &self,
88        tables: &mut Tables<'cx, BridgeTys>,
89        cx: &CompilerCtxt<'cx, BridgeTys>,
90    ) -> Self::T {
91        if !(self.args.len() >= self.fixed_count as usize) {
    ::core::panicking::panic("assertion failed: self.args.len() >= self.fixed_count as usize")
};assert!(self.args.len() >= self.fixed_count as usize);
92        if !(!self.c_variadic ||
            #[allow(non_exhaustive_omitted_patterns)] match self.conv {
                CanonAbi::C => true,
                _ => false,
            }) {
    ::core::panicking::panic("assertion failed: !self.c_variadic || matches!(self.conv, CanonAbi::C)")
};assert!(!self.c_variadic || matches!(self.conv, CanonAbi::C));
93        FnAbi {
94            args: self.args.as_ref().stable(tables, cx),
95            ret: self.ret.stable(tables, cx),
96            fixed_count: self.fixed_count,
97            conv: self.conv.stable(tables, cx),
98            c_variadic: self.c_variadic,
99        }
100    }
101}
102
103impl<'tcx> Stable<'tcx> for callconv::ArgAbi<'tcx, ty::Ty<'tcx>> {
104    type T = ArgAbi;
105
106    fn stable<'cx>(
107        &self,
108        tables: &mut Tables<'cx, BridgeTys>,
109        cx: &CompilerCtxt<'cx, BridgeTys>,
110    ) -> Self::T {
111        ArgAbi {
112            ty: self.layout.ty.stable(tables, cx),
113            layout: self.layout.layout.stable(tables, cx),
114            mode: self.mode.stable(tables, cx),
115        }
116    }
117}
118
119impl<'tcx> Stable<'tcx> for CanonAbi {
120    type T = CallConvention;
121
122    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
123        match self {
124            CanonAbi::C => CallConvention::C,
125            CanonAbi::Rust => CallConvention::Rust,
126            CanonAbi::RustCold => CallConvention::Cold,
127            CanonAbi::RustPreserveNone => CallConvention::PreserveNone,
128            CanonAbi::Custom => CallConvention::Custom,
129            CanonAbi::Swift => CallConvention::Swift,
130            CanonAbi::Arm(arm_call) => match arm_call {
131                ArmCall::Aapcs => CallConvention::ArmAapcs,
132                ArmCall::CCmseNonSecureCall => CallConvention::CCmseNonSecureCall,
133                ArmCall::CCmseNonSecureEntry => CallConvention::CCmseNonSecureEntry,
134            },
135            CanonAbi::GpuKernel => CallConvention::GpuKernel,
136            CanonAbi::Interrupt(interrupt_kind) => match interrupt_kind {
137                InterruptKind::Avr => CallConvention::AvrInterrupt,
138                InterruptKind::AvrNonBlocking => CallConvention::AvrNonBlockingInterrupt,
139                InterruptKind::Msp430 => CallConvention::Msp430Intr,
140                InterruptKind::RiscvMachine | InterruptKind::RiscvSupervisor => {
141                    CallConvention::RiscvInterrupt
142                }
143                InterruptKind::X86 => CallConvention::X86Intr,
144            },
145            CanonAbi::X86(x86_call) => match x86_call {
146                X86Call::Fastcall => CallConvention::X86Fastcall,
147                X86Call::Stdcall => CallConvention::X86Stdcall,
148                X86Call::SysV64 => CallConvention::X86_64SysV,
149                X86Call::Thiscall => CallConvention::X86ThisCall,
150                X86Call::Vectorcall => CallConvention::X86VectorCall,
151                X86Call::Win64 => CallConvention::X86_64Win64,
152            },
153        }
154    }
155}
156
157impl<'tcx> Stable<'tcx> for callconv::PassMode {
158    type T = PassMode;
159
160    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
161        match self {
162            callconv::PassMode::Ignore => PassMode::Ignore,
163            callconv::PassMode::Direct(attr) => PassMode::Direct(opaque(attr)),
164            callconv::PassMode::Pair(first, second) => {
165                PassMode::Pair(opaque(first), opaque(second))
166            }
167            callconv::PassMode::Cast { pad_i32, cast } => {
168                PassMode::Cast { pad_i32: *pad_i32, cast: opaque(cast) }
169            }
170            callconv::PassMode::Indirect { attrs, meta_attrs, on_stack } => PassMode::Indirect {
171                attrs: opaque(attrs),
172                meta_attrs: opaque(meta_attrs),
173                on_stack: *on_stack,
174            },
175        }
176    }
177}
178
179impl<'tcx> Stable<'tcx> for rustc_abi::FieldsShape<rustc_abi::FieldIdx> {
180    type T = FieldsShape;
181
182    fn stable<'cx>(
183        &self,
184        tables: &mut Tables<'cx, BridgeTys>,
185        cx: &CompilerCtxt<'cx, BridgeTys>,
186    ) -> Self::T {
187        match self {
188            rustc_abi::FieldsShape::Primitive => FieldsShape::Primitive,
189            rustc_abi::FieldsShape::Union(count) => FieldsShape::Union(*count),
190            rustc_abi::FieldsShape::Array { stride, count } => {
191                FieldsShape::Array { stride: stride.stable(tables, cx), count: *count }
192            }
193            rustc_abi::FieldsShape::Arbitrary { offsets, .. } => {
194                FieldsShape::Arbitrary { offsets: offsets.iter().as_slice().stable(tables, cx) }
195            }
196        }
197    }
198}
199
200impl<'tcx> Stable<'tcx> for rustc_abi::Variants<rustc_abi::FieldIdx, rustc_abi::VariantIdx> {
201    type T = VariantsShape;
202
203    fn stable<'cx>(
204        &self,
205        tables: &mut Tables<'cx, BridgeTys>,
206        cx: &CompilerCtxt<'cx, BridgeTys>,
207    ) -> Self::T {
208        match self {
209            rustc_abi::Variants::Single { index } => {
210                VariantsShape::Single { index: index.stable(tables, cx) }
211            }
212            rustc_abi::Variants::Empty => VariantsShape::Empty,
213            rustc_abi::Variants::Multiple { tag, tag_encoding, tag_field, variants } => {
214                VariantsShape::Multiple {
215                    tag: tag.stable(tables, cx),
216                    tag_encoding: tag_encoding.stable(tables, cx),
217                    tag_field: tag_field.stable(tables, cx),
218                    variants: variants
219                        .iter()
220                        .map(|v| VariantFields {
221                            offsets: v.field_offsets.iter().as_slice().stable(tables, cx),
222                        })
223                        .collect(),
224                }
225            }
226        }
227    }
228}
229
230impl<'tcx> Stable<'tcx> for rustc_abi::TagEncoding<rustc_abi::VariantIdx> {
231    type T = TagEncoding;
232
233    fn stable<'cx>(
234        &self,
235        tables: &mut Tables<'cx, BridgeTys>,
236        cx: &CompilerCtxt<'cx, BridgeTys>,
237    ) -> Self::T {
238        match self {
239            rustc_abi::TagEncoding::Direct => TagEncoding::Direct,
240            rustc_abi::TagEncoding::Niche { untagged_variant, niche_variants, niche_start } => {
241                TagEncoding::Niche {
242                    untagged_variant: untagged_variant.stable(tables, cx),
243                    niche_variants: niche_variants.stable(tables, cx),
244                    niche_start: *niche_start,
245                }
246            }
247        }
248    }
249}
250
251impl<'tcx> Stable<'tcx> for rustc_abi::NumScalableVectors {
252    type T = NumScalableVectors;
253
254    fn stable<'cx>(
255        &self,
256        _tables: &mut Tables<'cx, BridgeTys>,
257        _cx: &CompilerCtxt<'cx, BridgeTys>,
258    ) -> Self::T {
259        NumScalableVectors(self.0)
260    }
261}
262
263impl<'tcx> Stable<'tcx> for rustc_abi::BackendRepr {
264    type T = ValueAbi;
265
266    fn stable<'cx>(
267        &self,
268        tables: &mut Tables<'cx, BridgeTys>,
269        cx: &CompilerCtxt<'cx, BridgeTys>,
270    ) -> Self::T {
271        match *self {
272            rustc_abi::BackendRepr::Scalar(scalar) => ValueAbi::Scalar(scalar.stable(tables, cx)),
273            rustc_abi::BackendRepr::ScalarPair(first, second) => {
274                ValueAbi::ScalarPair(first.stable(tables, cx), second.stable(tables, cx))
275            }
276            rustc_abi::BackendRepr::SimdVector { element, count } => {
277                ValueAbi::Vector { element: element.stable(tables, cx), count }
278            }
279            rustc_abi::BackendRepr::SimdScalableVector { element, count, number_of_vectors } => {
280                ValueAbi::ScalableVector {
281                    element: element.stable(tables, cx),
282                    count,
283                    number_of_vectors: number_of_vectors.stable(tables, cx),
284                }
285            }
286            rustc_abi::BackendRepr::Memory { sized } => ValueAbi::Aggregate { sized },
287        }
288    }
289}
290
291impl<'tcx> Stable<'tcx> for rustc_abi::Size {
292    type T = Size;
293
294    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
295        Size::from_bits(self.bits_usize())
296    }
297}
298
299impl<'tcx> Stable<'tcx> for rustc_abi::Align {
300    type T = Align;
301
302    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
303        self.bytes()
304    }
305}
306
307impl<'tcx> Stable<'tcx> for rustc_abi::Scalar {
308    type T = Scalar;
309
310    fn stable<'cx>(
311        &self,
312        tables: &mut Tables<'cx, BridgeTys>,
313        cx: &CompilerCtxt<'cx, BridgeTys>,
314    ) -> Self::T {
315        match self {
316            rustc_abi::Scalar::Initialized { value, valid_range } => Scalar::Initialized {
317                value: value.stable(tables, cx),
318                valid_range: valid_range.stable(tables, cx),
319            },
320            rustc_abi::Scalar::Union { value } => Scalar::Union { value: value.stable(tables, cx) },
321        }
322    }
323}
324
325impl<'tcx> Stable<'tcx> for rustc_abi::Primitive {
326    type T = Primitive;
327
328    fn stable<'cx>(
329        &self,
330        tables: &mut Tables<'cx, BridgeTys>,
331        cx: &CompilerCtxt<'cx, BridgeTys>,
332    ) -> Self::T {
333        match self {
334            rustc_abi::Primitive::Int(length, signed) => {
335                Primitive::Int { length: length.stable(tables, cx), signed: *signed }
336            }
337            rustc_abi::Primitive::Float(length) => {
338                Primitive::Float { length: length.stable(tables, cx) }
339            }
340            rustc_abi::Primitive::Pointer(space) => Primitive::Pointer(space.stable(tables, cx)),
341        }
342    }
343}
344
345impl<'tcx> Stable<'tcx> for rustc_abi::AddressSpace {
346    type T = AddressSpace;
347
348    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
349        AddressSpace(self.0)
350    }
351}
352
353impl<'tcx> Stable<'tcx> for rustc_abi::Integer {
354    type T = IntegerLength;
355
356    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
357        match self {
358            rustc_abi::Integer::I8 => IntegerLength::I8,
359            rustc_abi::Integer::I16 => IntegerLength::I16,
360            rustc_abi::Integer::I32 => IntegerLength::I32,
361            rustc_abi::Integer::I64 => IntegerLength::I64,
362            rustc_abi::Integer::I128 => IntegerLength::I128,
363        }
364    }
365}
366
367impl<'tcx> Stable<'tcx> for rustc_abi::Float {
368    type T = FloatLength;
369
370    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
371        match self {
372            rustc_abi::Float::F16 => FloatLength::F16,
373            rustc_abi::Float::F32 => FloatLength::F32,
374            rustc_abi::Float::F64 => FloatLength::F64,
375            rustc_abi::Float::F128 => FloatLength::F128,
376        }
377    }
378}
379
380impl<'tcx> Stable<'tcx> for rustc_abi::WrappingRange {
381    type T = WrappingRange;
382
383    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
384        WrappingRange { start: self.start, end: self.end }
385    }
386}
387
388impl<'tcx> Stable<'tcx> for rustc_abi::ReprFlags {
389    type T = ReprFlags;
390
391    fn stable<'cx>(
392        &self,
393        _tables: &mut Tables<'cx, BridgeTys>,
394        _cx: &CompilerCtxt<'cx, BridgeTys>,
395    ) -> Self::T {
396        ReprFlags {
397            is_simd: self.intersects(Self::IS_SIMD),
398            is_c: self.intersects(Self::IS_C),
399            is_transparent: self.intersects(Self::IS_TRANSPARENT),
400            is_linear: self.intersects(Self::IS_LINEAR),
401        }
402    }
403}
404
405impl<'tcx> Stable<'tcx> for rustc_abi::IntegerType {
406    type T = IntegerType;
407
408    fn stable<'cx>(
409        &self,
410        tables: &mut Tables<'cx, BridgeTys>,
411        cx: &CompilerCtxt<'cx, BridgeTys>,
412    ) -> Self::T {
413        match self {
414            rustc_abi::IntegerType::Pointer(signed) => IntegerType::Pointer { is_signed: *signed },
415            rustc_abi::IntegerType::Fixed(integer, signed) => {
416                IntegerType::Fixed { length: integer.stable(tables, cx), is_signed: *signed }
417            }
418        }
419    }
420}
421
422impl<'tcx> Stable<'tcx> for rustc_abi::ReprOptions {
423    type T = ReprOptions;
424
425    fn stable<'cx>(
426        &self,
427        tables: &mut Tables<'cx, BridgeTys>,
428        cx: &CompilerCtxt<'cx, BridgeTys>,
429    ) -> Self::T {
430        ReprOptions {
431            int: self.int.map(|int| int.stable(tables, cx)),
432            align: self.align.map(|align| align.stable(tables, cx)),
433            pack: self.pack.map(|pack| pack.stable(tables, cx)),
434            flags: self.flags.stable(tables, cx),
435        }
436    }
437}