]> git.lizzy.rs Git - rust.git/blob - src/common.rs
Fix write_cvalue type assert for late bound regions in trait objects
[rust.git] / src / common.rs
1 use std::fmt;
2
3 use rustc_target::spec::{HasTargetSpec, Target};
4
5 use cranelift_module::Module;
6
7 use crate::prelude::*;
8
9 pub fn mir_var(loc: Local) -> Variable {
10     Variable::with_u32(loc.index() as u32)
11 }
12
13 pub fn pointer_ty(tcx: TyCtxt) -> types::Type {
14     match tcx.data_layout.pointer_size.bits() {
15         16 => types::I16,
16         32 => types::I32,
17         64 => types::I64,
18         bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits),
19     }
20 }
21
22 fn scalar_to_clif_type(tcx: TyCtxt, scalar: &Scalar) -> Type {
23     match scalar.value.size(&tcx).bits() {
24         8 => types::I8,
25         16 => types::I16,
26         32 => types::I32,
27         64 => types::I64,
28         size => bug!("Unsupported scalar size {}", size),
29     }
30 }
31
32 pub fn clif_type_from_ty<'a, 'tcx: 'a>(
33     tcx: TyCtxt<'a, 'tcx, 'tcx>,
34     ty: Ty<'tcx>,
35 ) -> Option<types::Type> {
36     Some(match ty.sty {
37         ty::Bool => types::I8,
38         ty::Uint(size) => match size {
39             UintTy::U8 => types::I8,
40             UintTy::U16 => types::I16,
41             UintTy::U32 => types::I32,
42             UintTy::U64 => types::I64,
43             UintTy::U128 => unimpl!("u128"),
44             UintTy::Usize => pointer_ty(tcx),
45         },
46         ty::Int(size) => match size {
47             IntTy::I8 => types::I8,
48             IntTy::I16 => types::I16,
49             IntTy::I32 => types::I32,
50             IntTy::I64 => types::I64,
51             IntTy::I128 => unimpl!("i128"),
52             IntTy::Isize => pointer_ty(tcx),
53         },
54         ty::Char => types::I32,
55         ty::Float(size) => match size {
56             FloatTy::F32 => types::F32,
57             FloatTy::F64 => types::F64,
58         },
59         ty::FnPtr(_) => pointer_ty(tcx),
60         ty::RawPtr(TypeAndMut { ty, mutbl: _ }) | ty::Ref(_, ty, _) => {
61             if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
62                 pointer_ty(tcx)
63             } else {
64                 return None;
65             }
66         }
67         ty::Param(_) => bug!("{:?}: {:?}", ty, ty.sty),
68         _ => return None,
69     })
70 }
71
72 pub fn codegen_select(bcx: &mut FunctionBuilder, cond: Value, lhs: Value, rhs: Value) -> Value {
73     let lhs_ty = bcx.func.dfg.value_type(lhs);
74     let rhs_ty = bcx.func.dfg.value_type(rhs);
75     assert_eq!(lhs_ty, rhs_ty);
76     if lhs_ty == types::I8 || lhs_ty == types::I16 {
77         // FIXME workaround for missing enocding for select.i8
78         let lhs = bcx.ins().uextend(types::I32, lhs);
79         let rhs = bcx.ins().uextend(types::I32, rhs);
80         let res = bcx.ins().select(cond, lhs, rhs);
81         bcx.ins().ireduce(lhs_ty, res)
82     } else {
83         bcx.ins().select(cond, lhs, rhs)
84     }
85 }
86
87 fn codegen_field<'a, 'tcx: 'a>(
88     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
89     base: Value,
90     layout: TyLayout<'tcx>,
91     field: mir::Field,
92 ) -> (Value, TyLayout<'tcx>) {
93     let field_offset = layout.fields.offset(field.index());
94     let field_ty = layout.field(&*fx, field.index());
95     if field_offset.bytes() > 0 {
96         (
97             fx.bcx.ins().iadd_imm(base, field_offset.bytes() as i64),
98             field_ty,
99         )
100     } else {
101         (base, field_ty)
102     }
103 }
104
105 /// A read-only value
106 #[derive(Debug, Copy, Clone)]
107 pub enum CValue<'tcx> {
108     ByRef(Value, TyLayout<'tcx>),
109     ByVal(Value, TyLayout<'tcx>),
110     ByValPair(Value, Value, TyLayout<'tcx>),
111 }
112
113 impl<'tcx> CValue<'tcx> {
114     pub fn layout(&self) -> TyLayout<'tcx> {
115         match *self {
116             CValue::ByRef(_, layout)
117             | CValue::ByVal(_, layout)
118             | CValue::ByValPair(_, _, layout) => layout,
119         }
120     }
121
122     pub fn force_stack<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value
123     where
124         'tcx: 'a,
125     {
126         match self {
127             CValue::ByRef(value, _layout) => value,
128             CValue::ByVal(value, layout) => {
129                 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
130                     kind: StackSlotKind::ExplicitSlot,
131                     size: layout.size.bytes() as u32,
132                     offset: None,
133                 });
134                 let addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0);
135                 fx.bcx.ins().store(MemFlags::new(), value, addr, 0);
136                 addr
137             }
138             CValue::ByValPair(value, extra, layout) => {
139                 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
140                     kind: StackSlotKind::ExplicitSlot,
141                     size: layout.size.bytes() as u32,
142                     offset: None,
143                 });
144                 let base = fx.bcx.ins().stack_addr(types::I64, stack_slot, 0);
145                 let a_addr = codegen_field(fx, base, layout, mir::Field::new(0)).0;
146                 let b_addr = codegen_field(fx, base, layout, mir::Field::new(1)).0;
147                 fx.bcx.ins().store(MemFlags::new(), value, a_addr, 0);
148                 fx.bcx.ins().store(MemFlags::new(), extra, b_addr, 0);
149                 base
150             }
151         }
152     }
153
154     /// Load a value with layout.abi of scalar
155     pub fn load_scalar<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value
156     where
157         'tcx: 'a,
158     {
159         match self {
160             CValue::ByRef(addr, layout) => {
161                 let scalar = match layout.abi {
162                     layout::Abi::Scalar(ref scalar) => scalar.clone(),
163                     _ => unreachable!(),
164                 };
165                 let clif_ty = crate::abi::scalar_to_clif_type(fx.tcx, scalar);
166                 fx.bcx.ins().load(clif_ty, MemFlags::new(), addr, 0)
167             }
168             CValue::ByVal(value, _layout) => value,
169             CValue::ByValPair(_, _, _layout) => bug!("Please use load_scalar_pair for ByValPair"),
170         }
171     }
172
173     /// Load a value pair with layout.abi of scalar pair
174     pub fn load_scalar_pair<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> (Value, Value)
175     where
176         'tcx: 'a,
177     {
178         match self {
179             CValue::ByRef(addr, layout) => {
180                 let (a, b) = match &layout.abi {
181                     layout::Abi::ScalarPair(a, b) => (a.clone(), b.clone()),
182                     _ => unreachable!(),
183                 };
184                 let clif_ty1 = crate::abi::scalar_to_clif_type(fx.tcx, a.clone());
185                 let clif_ty2 = crate::abi::scalar_to_clif_type(fx.tcx, b);
186                 let val1 = fx.bcx.ins().load(clif_ty1, MemFlags::new(), addr, 0);
187                 let val2 = fx.bcx.ins().load(
188                     clif_ty2,
189                     MemFlags::new(),
190                     addr,
191                     a.value.size(&fx.tcx).bytes() as i32,
192                 );
193                 (val1, val2)
194             }
195             CValue::ByVal(_, _layout) => bug!("Please use load_scalar for ByVal"),
196             CValue::ByValPair(val1, val2, _layout) => (val1, val2),
197         }
198     }
199
200     pub fn value_field<'a>(
201         self,
202         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
203         field: mir::Field,
204     ) -> CValue<'tcx>
205     where
206         'tcx: 'a,
207     {
208         let (base, layout) = match self {
209             CValue::ByRef(addr, layout) => (addr, layout),
210             _ => bug!("place_field for {:?}", self),
211         };
212
213         let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
214         CValue::ByRef(field_ptr, field_layout)
215     }
216
217     pub fn unsize_value<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, dest: CPlace<'tcx>) {
218         crate::unsize::coerce_unsized_into(fx, self, dest);
219     }
220
221     pub fn const_val<'a>(
222         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
223         ty: Ty<'tcx>,
224         const_val: i64,
225     ) -> CValue<'tcx>
226     where
227         'tcx: 'a,
228     {
229         let clif_ty = fx.clif_type(ty).unwrap();
230         let layout = fx.layout_of(ty);
231         CValue::ByVal(fx.bcx.ins().iconst(clif_ty, const_val), layout)
232     }
233
234     pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
235         match self {
236             CValue::ByRef(addr, _) => CValue::ByRef(addr, layout),
237             CValue::ByVal(val, _) => CValue::ByVal(val, layout),
238             CValue::ByValPair(val, extra, _) => CValue::ByValPair(val, extra, layout),
239         }
240     }
241 }
242
243 /// A place where you can write a value to or read a value from
244 #[derive(Debug, Copy, Clone)]
245 pub enum CPlace<'tcx> {
246     Var(Local, TyLayout<'tcx>),
247     Addr(Value, Option<Value>, TyLayout<'tcx>),
248     Stack(StackSlot, TyLayout<'tcx>),
249     NoPlace(TyLayout<'tcx>),
250 }
251
252 impl<'a, 'tcx: 'a> CPlace<'tcx> {
253     pub fn layout(&self) -> TyLayout<'tcx> {
254         match *self {
255             CPlace::Var(_, layout)
256             | CPlace::Addr(_, _, layout)
257             | CPlace::Stack(_, layout)
258             | CPlace::NoPlace(layout) => layout,
259         }
260     }
261
262     pub fn new_stack_slot(
263         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
264         ty: Ty<'tcx>,
265     ) -> CPlace<'tcx> {
266         let layout = fx.layout_of(ty);
267         assert!(!layout.is_unsized());
268         if layout.size.bytes() == 0 {
269             return CPlace::NoPlace(layout);
270         }
271
272         let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
273             kind: StackSlotKind::ExplicitSlot,
274             size: layout.size.bytes() as u32,
275             offset: None,
276         });
277         CPlace::Stack(stack_slot, layout)
278     }
279
280     pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CValue<'tcx> {
281         match self {
282             CPlace::Var(var, layout) => CValue::ByVal(fx.bcx.use_var(mir_var(var)), layout),
283             CPlace::Addr(addr, extra, layout) => {
284                 assert!(extra.is_none(), "unsized values are not yet supported");
285                 CValue::ByRef(addr, layout)
286             }
287             CPlace::Stack(stack_slot, layout) => CValue::ByRef(
288                 fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
289                 layout,
290             ),
291             CPlace::NoPlace(layout) => CValue::ByRef(
292                 fx.bcx
293                     .ins()
294                     .iconst(fx.pointer_type, fx.pointer_type.bytes() as i64),
295                 layout,
296             ),
297         }
298     }
299
300     pub fn to_addr(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value {
301         match self.to_addr_maybe_unsized(fx) {
302             (addr, None) => addr,
303             (_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
304         }
305     }
306
307     pub fn to_addr_maybe_unsized(
308         self,
309         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
310     ) -> (Value, Option<Value>) {
311         match self {
312             CPlace::Addr(addr, extra, _layout) => (addr, extra),
313             CPlace::Stack(stack_slot, _layout) => (
314                 fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
315                 None,
316             ),
317             CPlace::NoPlace(_) => (fx.bcx.ins().iconst(fx.pointer_type, 45), None),
318             CPlace::Var(_, _) => bug!("Expected CPlace::Addr, found CPlace::Var"),
319         }
320     }
321
322     pub fn write_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, from: CValue<'tcx>) {
323         use rustc::hir::Mutability::*;
324
325         let from_ty = from.layout().ty;
326         let to_ty = self.layout().ty;
327
328         fn assert_assignable<'a, 'tcx: 'a>(fx: &FunctionCx<'a, 'tcx, impl Backend>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) {
329             match (&from_ty.sty, &to_ty.sty) {
330                 (ty::Ref(_, t, MutImmutable), ty::Ref(_, u, MutImmutable))
331                 | (ty::Ref(_, t, MutMutable), ty::Ref(_, u, MutImmutable))
332                 | (ty::Ref(_, t, MutMutable), ty::Ref(_, u, MutMutable)) => {
333                     assert_assignable(fx, t, u);
334                     // &mut T -> &T is allowed
335                     // &'a T -> &'b T is allowed
336                 }
337                 (ty::Ref(_, _, MutImmutable), ty::Ref(_, _, MutMutable)) => {
338                     panic!("Cant assign value of type {} to place of type {}", from_ty.sty, to_ty.sty)
339                 }
340                 (ty::FnPtr(_), ty::FnPtr(_)) => {
341                     let from_sig = fx.tcx.normalize_erasing_late_bound_regions(
342                         ParamEnv::reveal_all(),
343                         &from_ty.fn_sig(fx.tcx),
344                     );
345                     let to_sig = fx.tcx.normalize_erasing_late_bound_regions(
346                         ParamEnv::reveal_all(),
347                         &to_ty.fn_sig(fx.tcx),
348                     );
349                     assert_eq!(
350                         from_sig, to_sig,
351                         "Can't write fn ptr with incompatible sig {:?} to place with sig {:?}\n\n{:#?}",
352                         from_sig, to_sig, fx,
353                     );
354                     // fn(&T) -> for<'l> fn(&'l T) is allowed
355                 }
356                 (ty::Dynamic(from_traits, _), ty::Dynamic(to_traits, _)) => {
357                     let from_traits = fx.tcx.normalize_erasing_late_bound_regions(
358                         ParamEnv::reveal_all(),
359                         from_traits,
360                     );
361                     let to_traits = fx.tcx.normalize_erasing_late_bound_regions(
362                         ParamEnv::reveal_all(),
363                         to_traits,
364                     );
365                     assert_eq!(
366                         from_traits, to_traits,
367                         "Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}",
368                         from_traits, to_traits, fx,
369                     );
370                     // dyn for<'r> Trait<'r> -> dyn Trait<'_> is allowed
371                 }
372                 _ => {
373                     assert_eq!(
374                         from_ty,
375                         to_ty,
376                         "Can't write value with incompatible type {:?} to place with type {:?}\n\n{:#?}",
377                         from_ty.sty,
378                         to_ty.sty,
379                         fx,
380                     );
381                 }
382             }
383         }
384
385         assert_assignable(fx, from_ty, to_ty);
386
387         let (addr, dst_layout) = match self {
388             CPlace::Var(var, _) => {
389                 let data = from.load_scalar(fx);
390                 fx.bcx.def_var(mir_var(var), data);
391                 return;
392             }
393             CPlace::Addr(addr, None, dst_layout) => (addr, dst_layout),
394             CPlace::Stack(stack_slot, dst_layout) => (
395                 fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
396                 dst_layout,
397             ),
398             CPlace::NoPlace(layout) => {
399                 assert!(layout.size.bytes() == 0);
400                 assert!(from.layout().size.bytes() == 0);
401                 return;
402             }
403             CPlace::Addr(_, _, _) => bug!("Can't write value to unsized place {:?}", self),
404         };
405
406         match from {
407             CValue::ByVal(val, _src_layout) => {
408                 fx.bcx.ins().store(MemFlags::new(), val, addr, 0);
409             }
410             CValue::ByValPair(val1, val2, _src_layout) => {
411                 let val1_offset = dst_layout.fields.offset(0).bytes() as i32;
412                 let val2_offset = dst_layout.fields.offset(1).bytes() as i32;
413                 fx.bcx.ins().store(MemFlags::new(), val1, addr, val1_offset);
414                 fx.bcx.ins().store(MemFlags::new(), val2, addr, val2_offset);
415             }
416             CValue::ByRef(from, src_layout) => {
417                 let size = dst_layout.size.bytes();
418                 let src_align = src_layout.align.abi.bytes() as u8;
419                 let dst_align = dst_layout.align.abi.bytes() as u8;
420                 fx.bcx.emit_small_memcpy(
421                     fx.module.target_config(),
422                     addr,
423                     from,
424                     size,
425                     dst_align,
426                     src_align,
427                 );
428             }
429         }
430     }
431
432     pub fn place_field(
433         self,
434         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
435         field: mir::Field,
436     ) -> CPlace<'tcx> {
437         let layout = self.layout();
438         let (base, extra) = self.to_addr_maybe_unsized(fx);
439
440         let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
441         let extra = if field_layout.is_unsized() {
442             assert!(extra.is_some());
443             extra
444         } else {
445             None
446         };
447         CPlace::Addr(field_ptr, extra, field_layout)
448     }
449
450     pub fn place_index(
451         self,
452         fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
453         index: Value,
454     ) -> CPlace<'tcx> {
455         let (elem_layout, addr) = match self.layout().ty.sty {
456             ty::Array(elem_ty, _) => (fx.layout_of(elem_ty), self.to_addr(fx)),
457             ty::Slice(elem_ty) => (fx.layout_of(elem_ty), self.to_addr_maybe_unsized(fx).0),
458             _ => bug!("place_index({:?})", self.layout().ty),
459         };
460
461         let offset = fx
462             .bcx
463             .ins()
464             .imul_imm(index, elem_layout.size.bytes() as i64);
465
466         CPlace::Addr(fx.bcx.ins().iadd(addr, offset), None, elem_layout)
467     }
468
469     pub fn place_deref(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CPlace<'tcx> {
470         let inner_layout = fx.layout_of(self.layout().ty.builtin_deref(true).unwrap().ty);
471         if !inner_layout.is_unsized() {
472             CPlace::Addr(self.to_cvalue(fx).load_scalar(fx), None, inner_layout)
473         } else {
474             match self.layout().abi {
475                 Abi::ScalarPair(ref a, ref b) => {
476                     let addr = self.to_addr(fx);
477                     let ptr =
478                         fx.bcx
479                             .ins()
480                             .load(scalar_to_clif_type(fx.tcx, a), MemFlags::new(), addr, 0);
481                     let extra = fx.bcx.ins().load(
482                         scalar_to_clif_type(fx.tcx, b),
483                         MemFlags::new(),
484                         addr,
485                         a.value.size(&fx.tcx).bytes() as u32 as i32,
486                     );
487                     CPlace::Addr(ptr, Some(extra), inner_layout)
488                 }
489                 _ => bug!(
490                     "Fat ptr doesn't have abi ScalarPair, but it has {:?}",
491                     self.layout().abi
492                 ),
493             }
494         }
495     }
496
497     pub fn write_place_ref(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, dest: CPlace<'tcx>) {
498         if !self.layout().is_unsized() {
499             let ptr = CValue::ByVal(self.to_addr(fx), dest.layout());
500             dest.write_cvalue(fx, ptr);
501         } else {
502             let (value, extra) = self.to_addr_maybe_unsized(fx);
503
504             match dest.layout().abi {
505                 Abi::ScalarPair(ref a, _) => {
506                     let dest_addr = dest.to_addr(fx);
507                     fx.bcx.ins().store(MemFlags::new(), value, dest_addr, 0);
508                     fx.bcx.ins().store(
509                         MemFlags::new(),
510                         extra.expect("unsized type without metadata"),
511                         dest_addr,
512                         a.value.size(&fx.tcx).bytes() as u32 as i32,
513                     );
514                 }
515                 _ => bug!(
516                     "Non ScalarPair abi {:?} in write_place_ref dest",
517                     dest.layout().abi
518                 ),
519             }
520         }
521     }
522
523     pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
524         assert!(!self.layout().is_unsized());
525         match self {
526             CPlace::Var(var, _) => CPlace::Var(var, layout),
527             CPlace::Addr(addr, extra, _) => CPlace::Addr(addr, extra, layout),
528             CPlace::Stack(stack_slot, _) => CPlace::Stack(stack_slot, layout),
529             CPlace::NoPlace(_) => {
530                 assert!(layout.size.bytes() == 0);
531                 CPlace::NoPlace(layout)
532             }
533         }
534     }
535
536     pub fn downcast_variant(
537         self,
538         fx: &FunctionCx<'a, 'tcx, impl Backend>,
539         variant: VariantIdx,
540     ) -> Self {
541         let layout = self.layout().for_variant(fx, variant);
542         self.unchecked_cast_to(layout)
543     }
544 }
545
546 pub fn clif_intcast<'a, 'tcx: 'a>(
547     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
548     val: Value,
549     to: Type,
550     signed: bool,
551 ) -> Value {
552     let from = fx.bcx.func.dfg.value_type(val);
553     if from == to {
554         return val;
555     }
556     if to.wider_or_equal(from) {
557         if signed {
558             fx.bcx.ins().sextend(to, val)
559         } else {
560             fx.bcx.ins().uextend(to, val)
561         }
562     } else {
563         fx.bcx.ins().ireduce(to, val)
564     }
565 }
566
567 pub struct FunctionCx<'a, 'tcx: 'a, B: Backend> {
568     // FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
569     pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
570     pub module: &'a mut Module<B>,
571     pub pointer_type: Type, // Cached from module
572
573     pub instance: Instance<'tcx>,
574     pub mir: &'tcx Mir<'tcx>,
575
576     pub bcx: FunctionBuilder<'a>,
577     pub ebb_map: HashMap<BasicBlock, Ebb>,
578     pub local_map: HashMap<Local, CPlace<'tcx>>,
579
580     pub clif_comments: crate::pretty_clif::CommentWriter,
581     pub constants: &'a mut crate::constant::ConstantCx,
582     pub caches: &'a mut Caches<'tcx>,
583     pub source_info_set: indexmap::IndexSet<SourceInfo>,
584 }
585
586 impl<'a, 'tcx: 'a, B: Backend + 'a> fmt::Debug for FunctionCx<'a, 'tcx, B> {
587     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
588         writeln!(f, "{:?}", self.instance.substs)?;
589         writeln!(f, "{:?}", self.local_map)?;
590
591         let mut clif = String::new();
592         ::cranelift::codegen::write::decorate_function(
593             &mut &self.clif_comments,
594             &mut clif,
595             &self.bcx.func,
596             None,
597         )
598         .unwrap();
599         writeln!(f, "\n{}", clif)
600     }
601 }
602
603 impl<'a, 'tcx: 'a, B: Backend> LayoutOf for FunctionCx<'a, 'tcx, B> {
604     type Ty = Ty<'tcx>;
605     type TyLayout = TyLayout<'tcx>;
606
607     fn layout_of(&self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
608         let ty = self.monomorphize(&ty);
609         self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
610     }
611 }
612
613 impl<'a, 'tcx, B: Backend + 'a> layout::HasTyCtxt<'tcx> for FunctionCx<'a, 'tcx, B> {
614     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
615         self.tcx
616     }
617 }
618
619 impl<'a, 'tcx, B: Backend + 'a> layout::HasDataLayout for FunctionCx<'a, 'tcx, B> {
620     fn data_layout(&self) -> &layout::TargetDataLayout {
621         &self.tcx.data_layout
622     }
623 }
624
625 impl<'a, 'tcx, B: Backend + 'a> HasTargetSpec for FunctionCx<'a, 'tcx, B> {
626     fn target_spec(&self) -> &Target {
627         &self.tcx.sess.target.target
628     }
629 }
630
631 impl<'a, 'tcx, B: Backend> BackendTypes for FunctionCx<'a, 'tcx, B> {
632     type Value = Value;
633     type BasicBlock = Ebb;
634     type Type = Type;
635     type Funclet = !;
636     type DIScope = !;
637 }
638
639 impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
640     pub fn monomorphize<T>(&self, value: &T) -> T
641     where
642         T: TypeFoldable<'tcx>,
643     {
644         self.tcx.subst_and_normalize_erasing_regions(
645             self.instance.substs,
646             ty::ParamEnv::reveal_all(),
647             value,
648         )
649     }
650
651     pub fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
652         clif_type_from_ty(self.tcx, self.monomorphize(&ty))
653     }
654
655     pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
656         *self.ebb_map.get(&bb).unwrap()
657     }
658
659     pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
660         *self.local_map.get(&local).unwrap()
661     }
662
663     pub fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
664         let (index, _) = self.source_info_set.insert_full(source_info);
665         self.bcx.set_srcloc(SourceLoc::new(index as u32));
666     }
667 }