|
7 | 7 | //!
|
8 | 8 | //! For now, we are developing everything inside `rustc`, thus, we keep this module private.
|
9 | 9 |
|
| 10 | +use crate::rustc_internal::{self, opaque}; |
10 | 11 | use crate::stable_mir::{self, ty::TyKind, Context};
|
11 | 12 | use rustc_middle::mir;
|
12 | 13 | use rustc_middle::ty::{self, Ty, TyCtxt};
|
13 | 14 | use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
|
| 15 | +use rustc_target::abi::FieldIdx; |
14 | 16 | use tracing::debug;
|
15 | 17 |
|
16 | 18 | impl<'tcx> Context for Tables<'tcx> {
|
@@ -119,11 +121,21 @@ fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
|
119 | 121 | stable_mir::Crate { id: crate_num.into(), name: crate_name, is_local }
|
120 | 122 | }
|
121 | 123 |
|
122 |
| -pub trait Stable { |
| 124 | +/// Trait used to convert between an internal MIR type to a Stable MIR type. |
| 125 | +pub(crate) trait Stable { |
| 126 | + /// The stable representation of the type implementing Stable. |
123 | 127 | type T;
|
| 128 | + /// Converts an object to the equivalent Stable MIR representation. |
124 | 129 | fn stable(&self) -> Self::T;
|
125 | 130 | }
|
126 | 131 |
|
| 132 | +impl Stable for DefId { |
| 133 | + type T = stable_mir::CrateItem; |
| 134 | + fn stable(&self) -> Self::T { |
| 135 | + rustc_internal::crate_item(*self) |
| 136 | + } |
| 137 | +} |
| 138 | + |
127 | 139 | impl<'tcx> Stable for mir::Statement<'tcx> {
|
128 | 140 | type T = stable_mir::mir::Statement;
|
129 | 141 | fn stable(&self) -> Self::T {
|
@@ -155,27 +167,127 @@ impl<'tcx> Stable for mir::Rvalue<'tcx> {
|
155 | 167 | match self {
|
156 | 168 | Use(op) => stable_mir::mir::Rvalue::Use(op.stable()),
|
157 | 169 | Repeat(_, _) => todo!(),
|
158 |
| - Ref(_, _, _) => todo!(), |
159 |
| - ThreadLocalRef(_) => todo!(), |
160 |
| - AddressOf(_, _) => todo!(), |
161 |
| - Len(_) => todo!(), |
| 170 | + Ref(region, kind, place) => { |
| 171 | + stable_mir::mir::Rvalue::Ref(opaque(region), kind.stable(), place.stable()) |
| 172 | + } |
| 173 | + ThreadLocalRef(def_id) => stable_mir::mir::Rvalue::ThreadLocalRef(def_id.stable()), |
| 174 | + AddressOf(mutability, place) => { |
| 175 | + stable_mir::mir::Rvalue::AddressOf(mutability.stable(), place.stable()) |
| 176 | + } |
| 177 | + Len(place) => stable_mir::mir::Rvalue::Len(place.stable()), |
162 | 178 | Cast(_, _, _) => todo!(),
|
163 |
| - BinaryOp(_, _) => todo!(), |
| 179 | + BinaryOp(bin_op, ops) => { |
| 180 | + stable_mir::mir::Rvalue::BinaryOp(bin_op.stable(), ops.0.stable(), ops.1.stable()) |
| 181 | + } |
164 | 182 | CheckedBinaryOp(bin_op, ops) => stable_mir::mir::Rvalue::CheckedBinaryOp(
|
165 | 183 | bin_op.stable(),
|
166 | 184 | ops.0.stable(),
|
167 | 185 | ops.1.stable(),
|
168 | 186 | ),
|
169 | 187 | NullaryOp(_, _) => todo!(),
|
170 | 188 | UnaryOp(un_op, op) => stable_mir::mir::Rvalue::UnaryOp(un_op.stable(), op.stable()),
|
171 |
| - Discriminant(_) => todo!(), |
| 189 | + Discriminant(place) => stable_mir::mir::Rvalue::Discriminant(place.stable()), |
172 | 190 | Aggregate(_, _) => todo!(),
|
173 | 191 | ShallowInitBox(_, _) => todo!(),
|
174 |
| - CopyForDeref(_) => todo!(), |
| 192 | + CopyForDeref(place) => stable_mir::mir::Rvalue::CopyForDeref(place.stable()), |
| 193 | + } |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +impl Stable for mir::Mutability { |
| 198 | + type T = stable_mir::mir::Mutability; |
| 199 | + fn stable(&self) -> Self::T { |
| 200 | + use mir::Mutability::*; |
| 201 | + match *self { |
| 202 | + Not => stable_mir::mir::Mutability::Not, |
| 203 | + Mut => stable_mir::mir::Mutability::Mut, |
| 204 | + } |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +impl Stable for mir::BorrowKind { |
| 209 | + type T = stable_mir::mir::BorrowKind; |
| 210 | + fn stable(&self) -> Self::T { |
| 211 | + use mir::BorrowKind::*; |
| 212 | + match *self { |
| 213 | + Shared => stable_mir::mir::BorrowKind::Shared, |
| 214 | + Shallow => stable_mir::mir::BorrowKind::Shallow, |
| 215 | + Unique => stable_mir::mir::BorrowKind::Unique, |
| 216 | + Mut { allow_two_phase_borrow } => { |
| 217 | + stable_mir::mir::BorrowKind::Mut { allow_two_phase_borrow } |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +impl<'tcx> Stable for mir::NullOp<'tcx> { |
| 224 | + type T = stable_mir::mir::NullOp; |
| 225 | + fn stable(&self) -> Self::T { |
| 226 | + use mir::NullOp::*; |
| 227 | + match self { |
| 228 | + SizeOf => stable_mir::mir::NullOp::SizeOf, |
| 229 | + AlignOf => stable_mir::mir::NullOp::AlignOf, |
| 230 | + OffsetOf(indices) => { |
| 231 | + stable_mir::mir::NullOp::OffsetOf(indices.iter().map(|idx| idx.stable()).collect()) |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +impl Stable for mir::CastKind { |
| 238 | + type T = stable_mir::mir::CastKind; |
| 239 | + fn stable(&self) -> Self::T { |
| 240 | + use mir::CastKind::*; |
| 241 | + match self { |
| 242 | + PointerExposeAddress => stable_mir::mir::CastKind::PointerExposeAddress, |
| 243 | + PointerFromExposedAddress => stable_mir::mir::CastKind::PointerFromExposedAddress, |
| 244 | + Pointer(cast) => stable_mir::mir::CastKind::Pointer(cast.stable()), |
| 245 | + DynStar => stable_mir::mir::CastKind::DynStar, |
| 246 | + IntToInt => stable_mir::mir::CastKind::IntToInt, |
| 247 | + FloatToInt => stable_mir::mir::CastKind::FloatToInt, |
| 248 | + FloatToFloat => stable_mir::mir::CastKind::FloatToFloat, |
| 249 | + IntToFloat => stable_mir::mir::CastKind::IntToFloat, |
| 250 | + PtrToPtr => stable_mir::mir::CastKind::PtrToPtr, |
| 251 | + FnPtrToPtr => stable_mir::mir::CastKind::FnPtrToPtr, |
| 252 | + Transmute => stable_mir::mir::CastKind::Transmute, |
| 253 | + } |
| 254 | + } |
| 255 | +} |
| 256 | + |
| 257 | +impl Stable for ty::adjustment::PointerCast { |
| 258 | + type T = stable_mir::mir::PointerCast; |
| 259 | + fn stable(&self) -> Self::T { |
| 260 | + use ty::adjustment::PointerCast; |
| 261 | + match self { |
| 262 | + PointerCast::ReifyFnPointer => stable_mir::mir::PointerCast::ReifyFnPointer, |
| 263 | + PointerCast::UnsafeFnPointer => stable_mir::mir::PointerCast::UnsafeFnPointer, |
| 264 | + PointerCast::ClosureFnPointer(unsafety) => { |
| 265 | + stable_mir::mir::PointerCast::ClosureFnPointer(unsafety.stable()) |
| 266 | + } |
| 267 | + PointerCast::MutToConstPointer => stable_mir::mir::PointerCast::MutToConstPointer, |
| 268 | + PointerCast::ArrayToPointer => stable_mir::mir::PointerCast::ArrayToPointer, |
| 269 | + PointerCast::Unsize => stable_mir::mir::PointerCast::Unsize, |
| 270 | + } |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +impl Stable for rustc_hir::Unsafety { |
| 275 | + type T = stable_mir::mir::Safety; |
| 276 | + fn stable(&self) -> Self::T { |
| 277 | + match self { |
| 278 | + rustc_hir::Unsafety::Unsafe => stable_mir::mir::Safety::Unsafe, |
| 279 | + rustc_hir::Unsafety::Normal => stable_mir::mir::Safety::Normal, |
175 | 280 | }
|
176 | 281 | }
|
177 | 282 | }
|
178 | 283 |
|
| 284 | +impl Stable for FieldIdx { |
| 285 | + type T = usize; |
| 286 | + fn stable(&self) -> Self::T { |
| 287 | + self.as_usize() |
| 288 | + } |
| 289 | +} |
| 290 | + |
179 | 291 | impl<'tcx> Stable for mir::Operand<'tcx> {
|
180 | 292 | type T = stable_mir::mir::Operand;
|
181 | 293 | fn stable(&self) -> Self::T {
|
@@ -211,34 +323,38 @@ impl Stable for mir::UnwindAction {
|
211 | 323 | }
|
212 | 324 | }
|
213 | 325 |
|
214 |
| -fn rustc_assert_msg_to_msg<'tcx>( |
215 |
| - assert_message: &rustc_middle::mir::AssertMessage<'tcx>, |
216 |
| -) -> stable_mir::mir::AssertMessage { |
217 |
| - use rustc_middle::mir::AssertKind; |
218 |
| - match assert_message { |
219 |
| - AssertKind::BoundsCheck { len, index } => { |
220 |
| - stable_mir::mir::AssertMessage::BoundsCheck { len: len.stable(), index: index.stable() } |
221 |
| - } |
222 |
| - AssertKind::Overflow(bin_op, op1, op2) => { |
223 |
| - stable_mir::mir::AssertMessage::Overflow(bin_op.stable(), op1.stable(), op2.stable()) |
224 |
| - } |
225 |
| - AssertKind::OverflowNeg(op) => stable_mir::mir::AssertMessage::OverflowNeg(op.stable()), |
226 |
| - AssertKind::DivisionByZero(op) => { |
227 |
| - stable_mir::mir::AssertMessage::DivisionByZero(op.stable()) |
228 |
| - } |
229 |
| - AssertKind::RemainderByZero(op) => { |
230 |
| - stable_mir::mir::AssertMessage::RemainderByZero(op.stable()) |
231 |
| - } |
232 |
| - AssertKind::ResumedAfterReturn(generator) => { |
233 |
| - stable_mir::mir::AssertMessage::ResumedAfterReturn(generator.stable()) |
234 |
| - } |
235 |
| - AssertKind::ResumedAfterPanic(generator) => { |
236 |
| - stable_mir::mir::AssertMessage::ResumedAfterPanic(generator.stable()) |
237 |
| - } |
238 |
| - AssertKind::MisalignedPointerDereference { required, found } => { |
239 |
| - stable_mir::mir::AssertMessage::MisalignedPointerDereference { |
240 |
| - required: required.stable(), |
241 |
| - found: found.stable(), |
| 326 | +impl<'tcx> Stable for mir::AssertMessage<'tcx> { |
| 327 | + type T = stable_mir::mir::AssertMessage; |
| 328 | + fn stable(&self) -> Self::T { |
| 329 | + use rustc_middle::mir::AssertKind; |
| 330 | + match self { |
| 331 | + AssertKind::BoundsCheck { len, index } => stable_mir::mir::AssertMessage::BoundsCheck { |
| 332 | + len: len.stable(), |
| 333 | + index: index.stable(), |
| 334 | + }, |
| 335 | + AssertKind::Overflow(bin_op, op1, op2) => stable_mir::mir::AssertMessage::Overflow( |
| 336 | + bin_op.stable(), |
| 337 | + op1.stable(), |
| 338 | + op2.stable(), |
| 339 | + ), |
| 340 | + AssertKind::OverflowNeg(op) => stable_mir::mir::AssertMessage::OverflowNeg(op.stable()), |
| 341 | + AssertKind::DivisionByZero(op) => { |
| 342 | + stable_mir::mir::AssertMessage::DivisionByZero(op.stable()) |
| 343 | + } |
| 344 | + AssertKind::RemainderByZero(op) => { |
| 345 | + stable_mir::mir::AssertMessage::RemainderByZero(op.stable()) |
| 346 | + } |
| 347 | + AssertKind::ResumedAfterReturn(generator) => { |
| 348 | + stable_mir::mir::AssertMessage::ResumedAfterReturn(generator.stable()) |
| 349 | + } |
| 350 | + AssertKind::ResumedAfterPanic(generator) => { |
| 351 | + stable_mir::mir::AssertMessage::ResumedAfterPanic(generator.stable()) |
| 352 | + } |
| 353 | + AssertKind::MisalignedPointerDereference { required, found } => { |
| 354 | + stable_mir::mir::AssertMessage::MisalignedPointerDereference { |
| 355 | + required: required.stable(), |
| 356 | + found: found.stable(), |
| 357 | + } |
242 | 358 | }
|
243 | 359 | }
|
244 | 360 | }
|
@@ -363,7 +479,7 @@ impl<'tcx> Stable for mir::Terminator<'tcx> {
|
363 | 479 | Assert { cond, expected, msg, target, unwind } => Terminator::Assert {
|
364 | 480 | cond: cond.stable(),
|
365 | 481 | expected: *expected,
|
366 |
| - msg: rustc_assert_msg_to_msg(msg), |
| 482 | + msg: msg.stable(), |
367 | 483 | target: target.as_usize(),
|
368 | 484 | unwind: unwind.stable(),
|
369 | 485 | },
|
|
0 commit comments