1#![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::RustTail => CallConvention::Tail,
129 CanonAbi::Custom => CallConvention::Custom,
130 CanonAbi::Swift => CallConvention::Swift,
131 CanonAbi::Arm(arm_call) => match arm_call {
132 ArmCall::Aapcs => CallConvention::ArmAapcs,
133 ArmCall::CCmseNonSecureCall => CallConvention::CCmseNonSecureCall,
134 ArmCall::CCmseNonSecureEntry => CallConvention::CCmseNonSecureEntry,
135 },
136 CanonAbi::GpuKernel => CallConvention::GpuKernel,
137 CanonAbi::Interrupt(interrupt_kind) => match interrupt_kind {
138 InterruptKind::Avr => CallConvention::AvrInterrupt,
139 InterruptKind::AvrNonBlocking => CallConvention::AvrNonBlockingInterrupt,
140 InterruptKind::Msp430 => CallConvention::Msp430Intr,
141 InterruptKind::RiscvMachine | InterruptKind::RiscvSupervisor => {
142 CallConvention::RiscvInterrupt
143 }
144 InterruptKind::X86 => CallConvention::X86Intr,
145 },
146 CanonAbi::X86(x86_call) => match x86_call {
147 X86Call::Fastcall => CallConvention::X86Fastcall,
148 X86Call::Stdcall => CallConvention::X86Stdcall,
149 X86Call::SysV64 => CallConvention::X86_64SysV,
150 X86Call::Thiscall => CallConvention::X86ThisCall,
151 X86Call::Vectorcall => CallConvention::X86VectorCall,
152 X86Call::Win64 => CallConvention::X86_64Win64,
153 },
154 }
155 }
156}
157
158impl<'tcx> Stable<'tcx> for callconv::PassMode {
159 type T = PassMode;
160
161 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
162 match self {
163 callconv::PassMode::Ignore => PassMode::Ignore,
164 callconv::PassMode::Direct(attr) => PassMode::Direct(opaque(attr)),
165 callconv::PassMode::Pair(first, second) => {
166 PassMode::Pair(opaque(first), opaque(second))
167 }
168 callconv::PassMode::Cast { pad_i32, cast } => {
169 PassMode::Cast { pad_i32: *pad_i32, cast: opaque(cast) }
170 }
171 callconv::PassMode::Indirect { attrs, meta_attrs, on_stack } => PassMode::Indirect {
172 attrs: opaque(attrs),
173 meta_attrs: opaque(meta_attrs),
174 on_stack: *on_stack,
175 },
176 }
177 }
178}
179
180impl<'tcx> Stable<'tcx> for rustc_abi::FieldsShape<rustc_abi::FieldIdx> {
181 type T = FieldsShape;
182
183 fn stable<'cx>(
184 &self,
185 tables: &mut Tables<'cx, BridgeTys>,
186 cx: &CompilerCtxt<'cx, BridgeTys>,
187 ) -> Self::T {
188 match self {
189 rustc_abi::FieldsShape::Primitive => FieldsShape::Primitive,
190 rustc_abi::FieldsShape::Union(count) => FieldsShape::Union(*count),
191 rustc_abi::FieldsShape::Array { stride, count } => {
192 FieldsShape::Array { stride: stride.stable(tables, cx), count: *count }
193 }
194 rustc_abi::FieldsShape::Arbitrary { offsets, .. } => {
195 FieldsShape::Arbitrary { offsets: offsets.iter().as_slice().stable(tables, cx) }
196 }
197 }
198 }
199}
200
201impl<'tcx> Stable<'tcx> for rustc_abi::Variants<rustc_abi::FieldIdx, rustc_abi::VariantIdx> {
202 type T = VariantsShape;
203
204 fn stable<'cx>(
205 &self,
206 tables: &mut Tables<'cx, BridgeTys>,
207 cx: &CompilerCtxt<'cx, BridgeTys>,
208 ) -> Self::T {
209 match self {
210 rustc_abi::Variants::Single { index } => {
211 VariantsShape::Single { index: index.stable(tables, cx) }
212 }
213 rustc_abi::Variants::Empty => VariantsShape::Empty,
214 rustc_abi::Variants::Multiple { tag, tag_encoding, tag_field, variants } => {
215 VariantsShape::Multiple {
216 tag: tag.stable(tables, cx),
217 tag_encoding: tag_encoding.stable(tables, cx),
218 tag_field: tag_field.stable(tables, cx),
219 variants: variants
220 .iter()
221 .map(|v| VariantFields {
222 offsets: v.field_offsets.iter().as_slice().stable(tables, cx),
223 })
224 .collect(),
225 }
226 }
227 }
228 }
229}
230
231impl<'tcx> Stable<'tcx> for rustc_abi::TagEncoding<rustc_abi::VariantIdx> {
232 type T = TagEncoding;
233
234 fn stable<'cx>(
235 &self,
236 tables: &mut Tables<'cx, BridgeTys>,
237 cx: &CompilerCtxt<'cx, BridgeTys>,
238 ) -> Self::T {
239 match self {
240 rustc_abi::TagEncoding::Direct => TagEncoding::Direct,
241 rustc_abi::TagEncoding::Niche { untagged_variant, niche_variants, niche_start } => {
242 TagEncoding::Niche {
243 untagged_variant: untagged_variant.stable(tables, cx),
244 niche_variants: niche_variants.stable(tables, cx),
245 niche_start: *niche_start,
246 }
247 }
248 }
249 }
250}
251
252impl<'tcx> Stable<'tcx> for rustc_abi::NumScalableVectors {
253 type T = NumScalableVectors;
254
255 fn stable<'cx>(
256 &self,
257 _tables: &mut Tables<'cx, BridgeTys>,
258 _cx: &CompilerCtxt<'cx, BridgeTys>,
259 ) -> Self::T {
260 NumScalableVectors(self.0)
261 }
262}
263
264impl<'tcx> Stable<'tcx> for rustc_abi::BackendRepr {
265 type T = ValueAbi;
266
267 fn stable<'cx>(
268 &self,
269 tables: &mut Tables<'cx, BridgeTys>,
270 cx: &CompilerCtxt<'cx, BridgeTys>,
271 ) -> Self::T {
272 match *self {
273 rustc_abi::BackendRepr::Scalar(scalar) => ValueAbi::Scalar(scalar.stable(tables, cx)),
274 rustc_abi::BackendRepr::ScalarPair { a: first, b: second, b_offset: second_offset } => {
275 ValueAbi::ScalarPair {
276 a: first.stable(tables, cx),
277 b: second.stable(tables, cx),
278 b_offset: second_offset.stable(tables, cx),
279 }
280 }
281 rustc_abi::BackendRepr::SimdVector { element, count } => {
282 ValueAbi::Vector { element: element.stable(tables, cx), count }
283 }
284 rustc_abi::BackendRepr::SimdScalableVector { element, count, number_of_vectors } => {
285 ValueAbi::ScalableVector {
286 element: element.stable(tables, cx),
287 count,
288 number_of_vectors: number_of_vectors.stable(tables, cx),
289 }
290 }
291 rustc_abi::BackendRepr::Memory { sized } => ValueAbi::Aggregate { sized },
292 }
293 }
294}
295
296impl<'tcx> Stable<'tcx> for rustc_abi::Size {
297 type T = Size;
298
299 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
300 Size::from_bits(self.bits_usize())
301 }
302}
303
304impl<'tcx> Stable<'tcx> for rustc_abi::Align {
305 type T = Align;
306
307 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
308 self.bytes()
309 }
310}
311
312impl<'tcx> Stable<'tcx> for rustc_abi::Scalar {
313 type T = Scalar;
314
315 fn stable<'cx>(
316 &self,
317 tables: &mut Tables<'cx, BridgeTys>,
318 cx: &CompilerCtxt<'cx, BridgeTys>,
319 ) -> Self::T {
320 match self {
321 rustc_abi::Scalar::Initialized { value, valid_range } => Scalar::Initialized {
322 value: value.stable(tables, cx),
323 valid_range: valid_range.stable(tables, cx),
324 },
325 rustc_abi::Scalar::Union { value } => Scalar::Union { value: value.stable(tables, cx) },
326 }
327 }
328}
329
330impl<'tcx> Stable<'tcx> for rustc_abi::Primitive {
331 type T = Primitive;
332
333 fn stable<'cx>(
334 &self,
335 tables: &mut Tables<'cx, BridgeTys>,
336 cx: &CompilerCtxt<'cx, BridgeTys>,
337 ) -> Self::T {
338 match self {
339 rustc_abi::Primitive::Int(length, signed) => {
340 Primitive::Int { length: length.stable(tables, cx), signed: *signed }
341 }
342 rustc_abi::Primitive::Float(length) => {
343 Primitive::Float { length: length.stable(tables, cx) }
344 }
345 rustc_abi::Primitive::Pointer(space) => Primitive::Pointer(space.stable(tables, cx)),
346 }
347 }
348}
349
350impl<'tcx> Stable<'tcx> for rustc_abi::AddressSpace {
351 type T = AddressSpace;
352
353 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
354 AddressSpace(self.0)
355 }
356}
357
358impl<'tcx> Stable<'tcx> for rustc_abi::Integer {
359 type T = IntegerLength;
360
361 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
362 match self {
363 rustc_abi::Integer::I8 => IntegerLength::I8,
364 rustc_abi::Integer::I16 => IntegerLength::I16,
365 rustc_abi::Integer::I32 => IntegerLength::I32,
366 rustc_abi::Integer::I64 => IntegerLength::I64,
367 rustc_abi::Integer::I128 => IntegerLength::I128,
368 }
369 }
370}
371
372impl<'tcx> Stable<'tcx> for rustc_abi::Float {
373 type T = FloatLength;
374
375 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
376 match self {
377 rustc_abi::Float::F16 => FloatLength::F16,
378 rustc_abi::Float::F32 => FloatLength::F32,
379 rustc_abi::Float::F64 => FloatLength::F64,
380 rustc_abi::Float::F128 => FloatLength::F128,
381 }
382 }
383}
384
385impl<'tcx> Stable<'tcx> for rustc_abi::WrappingRange {
386 type T = WrappingRange;
387
388 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
389 WrappingRange { start: self.start, end: self.end }
390 }
391}
392
393impl<'tcx> Stable<'tcx> for rustc_abi::ReprFlags {
394 type T = ReprFlags;
395
396 fn stable<'cx>(
397 &self,
398 _tables: &mut Tables<'cx, BridgeTys>,
399 _cx: &CompilerCtxt<'cx, BridgeTys>,
400 ) -> Self::T {
401 ReprFlags {
402 is_simd: self.intersects(Self::IS_SIMD),
403 is_c: self.intersects(Self::IS_C),
404 is_transparent: self.intersects(Self::IS_TRANSPARENT),
405 is_linear: self.intersects(Self::IS_LINEAR),
406 }
407 }
408}
409
410impl<'tcx> Stable<'tcx> for rustc_abi::IntegerType {
411 type T = IntegerType;
412
413 fn stable<'cx>(
414 &self,
415 tables: &mut Tables<'cx, BridgeTys>,
416 cx: &CompilerCtxt<'cx, BridgeTys>,
417 ) -> Self::T {
418 match self {
419 rustc_abi::IntegerType::Pointer(signed) => IntegerType::Pointer { is_signed: *signed },
420 rustc_abi::IntegerType::Fixed(integer, signed) => {
421 IntegerType::Fixed { length: integer.stable(tables, cx), is_signed: *signed }
422 }
423 }
424 }
425}
426
427impl<'tcx> Stable<'tcx> for rustc_abi::ReprOptions {
428 type T = ReprOptions;
429
430 fn stable<'cx>(
431 &self,
432 tables: &mut Tables<'cx, BridgeTys>,
433 cx: &CompilerCtxt<'cx, BridgeTys>,
434 ) -> Self::T {
435 ReprOptions {
436 int: self.int.map(|int| int.stable(tables, cx)),
437 align: self.align.map(|align| align.stable(tables, cx)),
438 pack: self.pack.map(|pack| pack.stable(tables, cx)),
439 flags: self.flags.stable(tables, cx),
440 }
441 }
442}