|
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::ty::{FloatTy, IntTy, RigidTy, TyKind, UintTy};
|
11 | 12 | use crate::stable_mir::{self, Context};
|
12 | 13 | use rustc_middle::mir;
|
13 | 14 | use rustc_middle::ty::{self, Ty, TyCtxt};
|
14 | 15 | use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
|
| 16 | +use rustc_target::abi::FieldIdx; |
15 | 17 | use tracing::debug;
|
16 | 18 |
|
17 | 19 | impl<'tcx> Context for Tables<'tcx> {
|
@@ -137,11 +139,21 @@ fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
|
137 | 139 | stable_mir::Crate { id: crate_num.into(), name: crate_name, is_local }
|
138 | 140 | }
|
139 | 141 |
|
140 |
| -pub trait Stable { |
| 142 | +/// Trait used to convert between an internal MIR type to a Stable MIR type. |
| 143 | +pub(crate) trait Stable { |
| 144 | + /// The stable representation of the type implementing Stable. |
141 | 145 | type T;
|
| 146 | + /// Converts an object to the equivalent Stable MIR representation. |
142 | 147 | fn stable(&self) -> Self::T;
|
143 | 148 | }
|
144 | 149 |
|
| 150 | +impl Stable for DefId { |
| 151 | + type T = stable_mir::CrateItem; |
| 152 | + fn stable(&self) -> Self::T { |
| 153 | + rustc_internal::crate_item(*self) |
| 154 | + } |
| 155 | +} |
| 156 | + |
145 | 157 | impl<'tcx> Stable for mir::Statement<'tcx> {
|
146 | 158 | type T = stable_mir::mir::Statement;
|
147 | 159 | fn stable(&self) -> Self::T {
|
@@ -173,27 +185,138 @@ impl<'tcx> Stable for mir::Rvalue<'tcx> {
|
173 | 185 | match self {
|
174 | 186 | Use(op) => stable_mir::mir::Rvalue::Use(op.stable()),
|
175 | 187 | Repeat(_, _) => todo!(),
|
176 |
| - Ref(_, _, _) => todo!(), |
177 |
| - ThreadLocalRef(_) => todo!(), |
178 |
| - AddressOf(_, _) => todo!(), |
179 |
| - Len(_) => todo!(), |
| 188 | + Ref(region, kind, place) => { |
| 189 | + stable_mir::mir::Rvalue::Ref(opaque(region), kind.stable(), place.stable()) |
| 190 | + } |
| 191 | + ThreadLocalRef(def_id) => stable_mir::mir::Rvalue::ThreadLocalRef(def_id.stable()), |
| 192 | + AddressOf(mutability, place) => { |
| 193 | + stable_mir::mir::Rvalue::AddressOf(mutability.stable(), place.stable()) |
| 194 | + } |
| 195 | + Len(place) => stable_mir::mir::Rvalue::Len(place.stable()), |
180 | 196 | Cast(_, _, _) => todo!(),
|
181 |
| - BinaryOp(_, _) => todo!(), |
| 197 | + BinaryOp(bin_op, ops) => { |
| 198 | + stable_mir::mir::Rvalue::BinaryOp(bin_op.stable(), ops.0.stable(), ops.1.stable()) |
| 199 | + } |
182 | 200 | CheckedBinaryOp(bin_op, ops) => stable_mir::mir::Rvalue::CheckedBinaryOp(
|
183 | 201 | bin_op.stable(),
|
184 | 202 | ops.0.stable(),
|
185 | 203 | ops.1.stable(),
|
186 | 204 | ),
|
187 | 205 | NullaryOp(_, _) => todo!(),
|
188 | 206 | UnaryOp(un_op, op) => stable_mir::mir::Rvalue::UnaryOp(un_op.stable(), op.stable()),
|
189 |
| - Discriminant(_) => todo!(), |
| 207 | + Discriminant(place) => stable_mir::mir::Rvalue::Discriminant(place.stable()), |
190 | 208 | Aggregate(_, _) => todo!(),
|
191 | 209 | ShallowInitBox(_, _) => todo!(),
|
192 |
| - CopyForDeref(_) => todo!(), |
| 210 | + CopyForDeref(place) => stable_mir::mir::Rvalue::CopyForDeref(place.stable()), |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +impl Stable for mir::Mutability { |
| 216 | + type T = stable_mir::mir::Mutability; |
| 217 | + fn stable(&self) -> Self::T { |
| 218 | + use mir::Mutability::*; |
| 219 | + match *self { |
| 220 | + Not => stable_mir::mir::Mutability::Not, |
| 221 | + Mut => stable_mir::mir::Mutability::Mut, |
| 222 | + } |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +impl Stable for mir::BorrowKind { |
| 227 | + type T = stable_mir::mir::BorrowKind; |
| 228 | + fn stable(&self) -> Self::T { |
| 229 | + use mir::BorrowKind::*; |
| 230 | + match *self { |
| 231 | + Shared => stable_mir::mir::BorrowKind::Shared, |
| 232 | + Shallow => stable_mir::mir::BorrowKind::Shallow, |
| 233 | + Mut { kind } => stable_mir::mir::BorrowKind::Mut { kind: kind.stable() }, |
| 234 | + } |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +impl Stable for mir::MutBorrowKind { |
| 239 | + type T = stable_mir::mir::MutBorrowKind; |
| 240 | + fn stable(&self) -> Self::T { |
| 241 | + use mir::MutBorrowKind::*; |
| 242 | + match *self { |
| 243 | + Default => stable_mir::mir::MutBorrowKind::Default, |
| 244 | + TwoPhaseBorrow => stable_mir::mir::MutBorrowKind::TwoPhaseBorrow, |
| 245 | + ClosureCapture => stable_mir::mir::MutBorrowKind::ClosureCapture, |
| 246 | + } |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +impl<'tcx> Stable for mir::NullOp<'tcx> { |
| 251 | + type T = stable_mir::mir::NullOp; |
| 252 | + fn stable(&self) -> Self::T { |
| 253 | + use mir::NullOp::*; |
| 254 | + match self { |
| 255 | + SizeOf => stable_mir::mir::NullOp::SizeOf, |
| 256 | + AlignOf => stable_mir::mir::NullOp::AlignOf, |
| 257 | + OffsetOf(indices) => { |
| 258 | + stable_mir::mir::NullOp::OffsetOf(indices.iter().map(|idx| idx.stable()).collect()) |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | +} |
| 263 | + |
| 264 | +impl Stable for mir::CastKind { |
| 265 | + type T = stable_mir::mir::CastKind; |
| 266 | + fn stable(&self) -> Self::T { |
| 267 | + use mir::CastKind::*; |
| 268 | + match self { |
| 269 | + PointerExposeAddress => stable_mir::mir::CastKind::PointerExposeAddress, |
| 270 | + PointerFromExposedAddress => stable_mir::mir::CastKind::PointerFromExposedAddress, |
| 271 | + PointerCoercion(c) => stable_mir::mir::CastKind::PointerCoercion(c.stable()), |
| 272 | + DynStar => stable_mir::mir::CastKind::DynStar, |
| 273 | + IntToInt => stable_mir::mir::CastKind::IntToInt, |
| 274 | + FloatToInt => stable_mir::mir::CastKind::FloatToInt, |
| 275 | + FloatToFloat => stable_mir::mir::CastKind::FloatToFloat, |
| 276 | + IntToFloat => stable_mir::mir::CastKind::IntToFloat, |
| 277 | + PtrToPtr => stable_mir::mir::CastKind::PtrToPtr, |
| 278 | + FnPtrToPtr => stable_mir::mir::CastKind::FnPtrToPtr, |
| 279 | + Transmute => stable_mir::mir::CastKind::Transmute, |
193 | 280 | }
|
194 | 281 | }
|
195 | 282 | }
|
196 | 283 |
|
| 284 | +impl Stable for ty::adjustment::PointerCoercion { |
| 285 | + type T = stable_mir::mir::PointerCoercion; |
| 286 | + fn stable(&self) -> Self::T { |
| 287 | + use ty::adjustment::PointerCoercion; |
| 288 | + match self { |
| 289 | + PointerCoercion::ReifyFnPointer => stable_mir::mir::PointerCoercion::ReifyFnPointer, |
| 290 | + PointerCoercion::UnsafeFnPointer => stable_mir::mir::PointerCoercion::UnsafeFnPointer, |
| 291 | + PointerCoercion::ClosureFnPointer(unsafety) => { |
| 292 | + stable_mir::mir::PointerCoercion::ClosureFnPointer(unsafety.stable()) |
| 293 | + } |
| 294 | + PointerCoercion::MutToConstPointer => { |
| 295 | + stable_mir::mir::PointerCoercion::MutToConstPointer |
| 296 | + } |
| 297 | + PointerCoercion::ArrayToPointer => stable_mir::mir::PointerCoercion::ArrayToPointer, |
| 298 | + PointerCoercion::Unsize => stable_mir::mir::PointerCoercion::Unsize, |
| 299 | + } |
| 300 | + } |
| 301 | +} |
| 302 | + |
| 303 | +impl Stable for rustc_hir::Unsafety { |
| 304 | + type T = stable_mir::mir::Safety; |
| 305 | + fn stable(&self) -> Self::T { |
| 306 | + match self { |
| 307 | + rustc_hir::Unsafety::Unsafe => stable_mir::mir::Safety::Unsafe, |
| 308 | + rustc_hir::Unsafety::Normal => stable_mir::mir::Safety::Normal, |
| 309 | + } |
| 310 | + } |
| 311 | +} |
| 312 | + |
| 313 | +impl Stable for FieldIdx { |
| 314 | + type T = usize; |
| 315 | + fn stable(&self) -> Self::T { |
| 316 | + self.as_usize() |
| 317 | + } |
| 318 | +} |
| 319 | + |
197 | 320 | impl<'tcx> Stable for mir::Operand<'tcx> {
|
198 | 321 | type T = stable_mir::mir::Operand;
|
199 | 322 | fn stable(&self) -> Self::T {
|
@@ -229,34 +352,38 @@ impl Stable for mir::UnwindAction {
|
229 | 352 | }
|
230 | 353 | }
|
231 | 354 |
|
232 |
| -fn rustc_assert_msg_to_msg<'tcx>( |
233 |
| - assert_message: &rustc_middle::mir::AssertMessage<'tcx>, |
234 |
| -) -> stable_mir::mir::AssertMessage { |
235 |
| - use rustc_middle::mir::AssertKind; |
236 |
| - match assert_message { |
237 |
| - AssertKind::BoundsCheck { len, index } => { |
238 |
| - stable_mir::mir::AssertMessage::BoundsCheck { len: len.stable(), index: index.stable() } |
239 |
| - } |
240 |
| - AssertKind::Overflow(bin_op, op1, op2) => { |
241 |
| - stable_mir::mir::AssertMessage::Overflow(bin_op.stable(), op1.stable(), op2.stable()) |
242 |
| - } |
243 |
| - AssertKind::OverflowNeg(op) => stable_mir::mir::AssertMessage::OverflowNeg(op.stable()), |
244 |
| - AssertKind::DivisionByZero(op) => { |
245 |
| - stable_mir::mir::AssertMessage::DivisionByZero(op.stable()) |
246 |
| - } |
247 |
| - AssertKind::RemainderByZero(op) => { |
248 |
| - stable_mir::mir::AssertMessage::RemainderByZero(op.stable()) |
249 |
| - } |
250 |
| - AssertKind::ResumedAfterReturn(generator) => { |
251 |
| - stable_mir::mir::AssertMessage::ResumedAfterReturn(generator.stable()) |
252 |
| - } |
253 |
| - AssertKind::ResumedAfterPanic(generator) => { |
254 |
| - stable_mir::mir::AssertMessage::ResumedAfterPanic(generator.stable()) |
255 |
| - } |
256 |
| - AssertKind::MisalignedPointerDereference { required, found } => { |
257 |
| - stable_mir::mir::AssertMessage::MisalignedPointerDereference { |
258 |
| - required: required.stable(), |
259 |
| - found: found.stable(), |
| 355 | +impl<'tcx> Stable for mir::AssertMessage<'tcx> { |
| 356 | + type T = stable_mir::mir::AssertMessage; |
| 357 | + fn stable(&self) -> Self::T { |
| 358 | + use rustc_middle::mir::AssertKind; |
| 359 | + match self { |
| 360 | + AssertKind::BoundsCheck { len, index } => stable_mir::mir::AssertMessage::BoundsCheck { |
| 361 | + len: len.stable(), |
| 362 | + index: index.stable(), |
| 363 | + }, |
| 364 | + AssertKind::Overflow(bin_op, op1, op2) => stable_mir::mir::AssertMessage::Overflow( |
| 365 | + bin_op.stable(), |
| 366 | + op1.stable(), |
| 367 | + op2.stable(), |
| 368 | + ), |
| 369 | + AssertKind::OverflowNeg(op) => stable_mir::mir::AssertMessage::OverflowNeg(op.stable()), |
| 370 | + AssertKind::DivisionByZero(op) => { |
| 371 | + stable_mir::mir::AssertMessage::DivisionByZero(op.stable()) |
| 372 | + } |
| 373 | + AssertKind::RemainderByZero(op) => { |
| 374 | + stable_mir::mir::AssertMessage::RemainderByZero(op.stable()) |
| 375 | + } |
| 376 | + AssertKind::ResumedAfterReturn(generator) => { |
| 377 | + stable_mir::mir::AssertMessage::ResumedAfterReturn(generator.stable()) |
| 378 | + } |
| 379 | + AssertKind::ResumedAfterPanic(generator) => { |
| 380 | + stable_mir::mir::AssertMessage::ResumedAfterPanic(generator.stable()) |
| 381 | + } |
| 382 | + AssertKind::MisalignedPointerDereference { required, found } => { |
| 383 | + stable_mir::mir::AssertMessage::MisalignedPointerDereference { |
| 384 | + required: required.stable(), |
| 385 | + found: found.stable(), |
| 386 | + } |
260 | 387 | }
|
261 | 388 | }
|
262 | 389 | }
|
@@ -381,7 +508,7 @@ impl<'tcx> Stable for mir::Terminator<'tcx> {
|
381 | 508 | Assert { cond, expected, msg, target, unwind } => Terminator::Assert {
|
382 | 509 | cond: cond.stable(),
|
383 | 510 | expected: *expected,
|
384 |
| - msg: rustc_assert_msg_to_msg(msg), |
| 511 | + msg: msg.stable(), |
385 | 512 | target: target.as_usize(),
|
386 | 513 | unwind: unwind.stable(),
|
387 | 514 | },
|
|
0 commit comments