|
| 1 | +//! Conversion of internal Rust compiler `rustc_target::abi` and `rustc_abi` items to stable ones. |
| 2 | +
|
| 3 | +#![allow(rustc::usage_of_qualified_ty)] |
| 4 | + |
| 5 | +use crate::rustc_smir::{Stable, Tables}; |
| 6 | +use rustc_middle::ty; |
| 7 | +use rustc_target::abi::call::Conv; |
| 8 | +use stable_mir::abi::{ |
| 9 | + ArgAbi, CallConvention, FieldsShape, FnAbi, Layout, LayoutShape, PassMode, TagEncoding, |
| 10 | + TyAndLayout, ValueAbi, VariantsShape, |
| 11 | +}; |
| 12 | +use stable_mir::ty::{Align, IndexedVal, Size, VariantIdx}; |
| 13 | +use stable_mir::{opaque, Opaque}; |
| 14 | + |
| 15 | +impl<'tcx> Stable<'tcx> for rustc_target::abi::VariantIdx { |
| 16 | + type T = VariantIdx; |
| 17 | + fn stable(&self, _: &mut Tables<'tcx>) -> Self::T { |
| 18 | + VariantIdx::to_val(self.as_usize()) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl<'tcx> Stable<'tcx> for rustc_abi::Endian { |
| 23 | + type T = stable_mir::target::Endian; |
| 24 | + |
| 25 | + fn stable(&self, _tables: &mut Tables<'tcx>) -> Self::T { |
| 26 | + match self { |
| 27 | + rustc_abi::Endian::Little => stable_mir::target::Endian::Little, |
| 28 | + rustc_abi::Endian::Big => stable_mir::target::Endian::Big, |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl<'tcx> Stable<'tcx> for rustc_target::abi::TyAndLayout<'tcx, ty::Ty<'tcx>> { |
| 34 | + type T = TyAndLayout; |
| 35 | + |
| 36 | + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { |
| 37 | + TyAndLayout { ty: self.ty.stable(tables), layout: self.layout.stable(tables) } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl<'tcx> Stable<'tcx> for rustc_target::abi::Layout<'tcx> { |
| 42 | + type T = Layout; |
| 43 | + |
| 44 | + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { |
| 45 | + tables.layout_id(*self) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<'tcx> Stable<'tcx> |
| 50 | + for rustc_abi::LayoutS<rustc_target::abi::FieldIdx, rustc_target::abi::VariantIdx> |
| 51 | +{ |
| 52 | + type T = LayoutShape; |
| 53 | + |
| 54 | + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { |
| 55 | + LayoutShape { |
| 56 | + fields: self.fields.stable(tables), |
| 57 | + variants: self.variants.stable(tables), |
| 58 | + abi: self.abi.stable(tables), |
| 59 | + abi_align: self.align.abi.stable(tables), |
| 60 | + size: self.size.stable(tables), |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<'tcx> Stable<'tcx> for rustc_target::abi::call::FnAbi<'tcx, ty::Ty<'tcx>> { |
| 66 | + type T = FnAbi; |
| 67 | + |
| 68 | + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { |
| 69 | + FnAbi { |
| 70 | + args: self.args.as_ref().stable(tables), |
| 71 | + ret: self.ret.stable(tables), |
| 72 | + fixed_count: self.fixed_count, |
| 73 | + conv: self.conv.stable(tables), |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl<'tcx> Stable<'tcx> for rustc_target::abi::call::ArgAbi<'tcx, ty::Ty<'tcx>> { |
| 79 | + type T = ArgAbi; |
| 80 | + |
| 81 | + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { |
| 82 | + ArgAbi { |
| 83 | + ty: self.layout.ty.stable(tables), |
| 84 | + layout: self.layout.layout.stable(tables), |
| 85 | + mode: self.mode.stable(tables), |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<'tcx> Stable<'tcx> for rustc_target::abi::call::Conv { |
| 91 | + type T = CallConvention; |
| 92 | + |
| 93 | + fn stable(&self, _tables: &mut Tables<'tcx>) -> Self::T { |
| 94 | + match self { |
| 95 | + Conv::C => CallConvention::C, |
| 96 | + Conv::Rust => CallConvention::Rust, |
| 97 | + Conv::Cold => CallConvention::Cold, |
| 98 | + Conv::PreserveMost => CallConvention::PreserveMost, |
| 99 | + Conv::PreserveAll => CallConvention::PreserveAll, |
| 100 | + Conv::ArmAapcs => CallConvention::ArmAapcs, |
| 101 | + Conv::CCmseNonSecureCall => CallConvention::CCmseNonSecureCall, |
| 102 | + Conv::Msp430Intr => CallConvention::Msp430Intr, |
| 103 | + Conv::PtxKernel => CallConvention::PtxKernel, |
| 104 | + Conv::X86Fastcall => CallConvention::X86Fastcall, |
| 105 | + Conv::X86Intr => CallConvention::X86Intr, |
| 106 | + Conv::X86Stdcall => CallConvention::X86Stdcall, |
| 107 | + Conv::X86ThisCall => CallConvention::X86ThisCall, |
| 108 | + Conv::X86VectorCall => CallConvention::X86VectorCall, |
| 109 | + Conv::X86_64SysV => CallConvention::X86_64SysV, |
| 110 | + Conv::X86_64Win64 => CallConvention::X86_64Win64, |
| 111 | + Conv::AmdGpuKernel => CallConvention::AmdGpuKernel, |
| 112 | + Conv::AvrInterrupt => CallConvention::AvrInterrupt, |
| 113 | + Conv::AvrNonBlockingInterrupt => CallConvention::AvrNonBlockingInterrupt, |
| 114 | + Conv::RiscvInterrupt { .. } => CallConvention::RiscvInterrupt, |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl<'tcx> Stable<'tcx> for rustc_target::abi::call::PassMode { |
| 120 | + type T = PassMode; |
| 121 | + |
| 122 | + fn stable(&self, _tables: &mut Tables<'tcx>) -> Self::T { |
| 123 | + match self { |
| 124 | + rustc_target::abi::call::PassMode::Ignore => PassMode::Ignore, |
| 125 | + rustc_target::abi::call::PassMode::Direct(_) => PassMode::Direct, |
| 126 | + rustc_target::abi::call::PassMode::Pair(_, _) => PassMode::Pair, |
| 127 | + rustc_target::abi::call::PassMode::Cast { .. } => PassMode::Cast, |
| 128 | + rustc_target::abi::call::PassMode::Indirect { .. } => PassMode::Indirect, |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl<'tcx> Stable<'tcx> for rustc_abi::FieldsShape<rustc_target::abi::FieldIdx> { |
| 134 | + type T = FieldsShape; |
| 135 | + |
| 136 | + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { |
| 137 | + match self { |
| 138 | + rustc_abi::FieldsShape::Primitive => FieldsShape::Primitive, |
| 139 | + rustc_abi::FieldsShape::Union(count) => FieldsShape::Union(*count), |
| 140 | + rustc_abi::FieldsShape::Array { stride, count } => { |
| 141 | + FieldsShape::Array { stride: stride.stable(tables), count: *count } |
| 142 | + } |
| 143 | + rustc_abi::FieldsShape::Arbitrary { offsets, .. } => { |
| 144 | + FieldsShape::Arbitrary { offsets: offsets.iter().as_slice().stable(tables) } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +impl<'tcx> Stable<'tcx> |
| 151 | + for rustc_abi::Variants<rustc_target::abi::FieldIdx, rustc_target::abi::VariantIdx> |
| 152 | +{ |
| 153 | + type T = VariantsShape; |
| 154 | + |
| 155 | + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { |
| 156 | + match self { |
| 157 | + rustc_abi::Variants::Single { index } => { |
| 158 | + VariantsShape::Single { index: index.stable(tables) } |
| 159 | + } |
| 160 | + rustc_abi::Variants::Multiple { tag, tag_encoding, tag_field, variants } => { |
| 161 | + VariantsShape::Multiple { |
| 162 | + tag: tag.stable(tables), |
| 163 | + tag_encoding: tag_encoding.stable(tables), |
| 164 | + tag_field: *tag_field, |
| 165 | + variants: variants.iter().as_slice().stable(tables), |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +impl<'tcx> Stable<'tcx> for rustc_abi::TagEncoding<rustc_target::abi::VariantIdx> { |
| 173 | + type T = TagEncoding; |
| 174 | + |
| 175 | + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { |
| 176 | + match self { |
| 177 | + rustc_abi::TagEncoding::Direct => TagEncoding::Direct, |
| 178 | + rustc_abi::TagEncoding::Niche { untagged_variant, niche_variants, niche_start } => { |
| 179 | + TagEncoding::Niche { |
| 180 | + untagged_variant: untagged_variant.stable(tables), |
| 181 | + niche_variants: niche_variants.stable(tables), |
| 182 | + niche_start: *niche_start, |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +impl<'tcx> Stable<'tcx> for rustc_abi::Abi { |
| 190 | + type T = ValueAbi; |
| 191 | + |
| 192 | + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { |
| 193 | + match *self { |
| 194 | + rustc_abi::Abi::Uninhabited => ValueAbi::Uninhabited, |
| 195 | + rustc_abi::Abi::Scalar(scalar) => ValueAbi::Scalar(scalar.stable(tables)), |
| 196 | + rustc_abi::Abi::ScalarPair(first, second) => { |
| 197 | + ValueAbi::ScalarPair(first.stable(tables), second.stable(tables)) |
| 198 | + } |
| 199 | + rustc_abi::Abi::Vector { element, count } => { |
| 200 | + ValueAbi::Vector { element: element.stable(tables), count } |
| 201 | + } |
| 202 | + rustc_abi::Abi::Aggregate { sized } => ValueAbi::Aggregate { sized }, |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +impl<'tcx> Stable<'tcx> for rustc_abi::Size { |
| 208 | + type T = Size; |
| 209 | + |
| 210 | + fn stable(&self, _tables: &mut Tables<'tcx>) -> Self::T { |
| 211 | + self.bytes_usize() |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +impl<'tcx> Stable<'tcx> for rustc_abi::Align { |
| 216 | + type T = Align; |
| 217 | + |
| 218 | + fn stable(&self, _tables: &mut Tables<'tcx>) -> Self::T { |
| 219 | + self.bytes() |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +impl<'tcx> Stable<'tcx> for rustc_abi::Scalar { |
| 224 | + type T = Opaque; |
| 225 | + |
| 226 | + fn stable(&self, _tables: &mut Tables<'tcx>) -> Self::T { |
| 227 | + opaque(self) |
| 228 | + } |
| 229 | +} |
0 commit comments