|
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Code for translating references to other items (DefIds). |
| 12 | +
|
| 13 | +use syntax::codemap::DUMMY_SP; |
| 14 | +use rustc::front::map; |
| 15 | +use rustc::middle::ty::{self, Ty, HasTypeFlags}; |
| 16 | +use rustc::middle::subst::Substs; |
| 17 | +use rustc::middle::const_eval; |
| 18 | +use rustc::middle::def_id::DefId; |
| 19 | +use rustc::middle::subst; |
| 20 | +use rustc::middle::traits; |
| 21 | +use rustc::mir::repr::ItemKind; |
| 22 | +use trans::common::{Block, fulfill_obligation}; |
| 23 | +use trans::base; |
| 24 | +use trans::expr; |
| 25 | +use trans::monomorphize; |
| 26 | +use trans::meth; |
| 27 | +use trans::inline; |
| 28 | + |
| 29 | +use super::MirContext; |
| 30 | +use super::operand::{OperandRef, OperandValue}; |
| 31 | + |
| 32 | +impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { |
| 33 | + /// Translate reference to item. |
| 34 | + pub fn trans_item_ref(&mut self, |
| 35 | + bcx: Block<'bcx, 'tcx>, |
| 36 | + ty: Ty<'tcx>, |
| 37 | + kind: ItemKind, |
| 38 | + substs: &'tcx Substs<'tcx>, |
| 39 | + did: DefId) |
| 40 | + -> OperandRef<'tcx> { |
| 41 | + match kind { |
| 42 | + ItemKind::Function | |
| 43 | + ItemKind::Struct | |
| 44 | + ItemKind::Variant => self.trans_fn_ref(bcx, ty, substs, did), |
| 45 | + ItemKind::Method => match bcx.tcx().impl_or_trait_item(did).container() { |
| 46 | + ty::ImplContainer(_) => self.trans_fn_ref(bcx, ty, substs, did), |
| 47 | + ty::TraitContainer(tdid) => self.trans_static_method(bcx, ty, did, tdid, substs) |
| 48 | + }, |
| 49 | + ItemKind::Constant => { |
| 50 | + let did = inline::maybe_instantiate_inline(bcx.ccx(), did); |
| 51 | + let expr = const_eval::lookup_const_by_id(bcx.tcx(), did, None) |
| 52 | + .expect("def was const, but lookup_const_by_id failed"); |
| 53 | + // FIXME: this is falling back to translating from HIR. This is not easy to fix, |
| 54 | + // because we would have somehow adapt const_eval to work on MIR rather than HIR. |
| 55 | + let d = expr::trans(bcx, expr); |
| 56 | + OperandRef::from_rvalue_datum(d.datum.to_rvalue_datum(d.bcx, "").datum) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Translates references to a function-like items. |
| 62 | + /// |
| 63 | + /// That includes regular functions, non-static methods, struct and enum variant constructors, |
| 64 | + /// closures and possibly more. |
| 65 | + /// |
| 66 | + /// This is an adaptation of callee::trans_fn_ref_with_substs. |
| 67 | + pub fn trans_fn_ref(&mut self, |
| 68 | + bcx: Block<'bcx, 'tcx>, |
| 69 | + ty: Ty<'tcx>, |
| 70 | + substs: &'tcx Substs<'tcx>, |
| 71 | + did: DefId) |
| 72 | + -> OperandRef<'tcx> { |
| 73 | + let did = inline::maybe_instantiate_inline(bcx.ccx(), did); |
| 74 | + |
| 75 | + if !substs.types.is_empty() || is_named_tuple_constructor(bcx.tcx(), did) { |
| 76 | + let (val, fn_ty, _) = monomorphize::monomorphic_fn(bcx.ccx(), did, substs, None); |
| 77 | + // FIXME: cast fnptr to proper type if necessary |
| 78 | + OperandRef { |
| 79 | + ty: fn_ty, |
| 80 | + val: OperandValue::Immediate(val) |
| 81 | + } |
| 82 | + } else { |
| 83 | + let val = if let Some(node_id) = bcx.tcx().map.as_local_node_id(did) { |
| 84 | + base::get_item_val(bcx.ccx(), node_id) |
| 85 | + } else { |
| 86 | + base::trans_external_path(bcx.ccx(), did, ty) |
| 87 | + }; |
| 88 | + // FIXME: cast fnptr to proper type if necessary |
| 89 | + OperandRef { |
| 90 | + ty: ty, |
| 91 | + val: OperandValue::Immediate(val) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// Translates references to static methods. |
| 97 | + /// |
| 98 | + /// This is an adaptation of meth::trans_static_method_callee |
| 99 | + pub fn trans_static_method(&mut self, |
| 100 | + bcx: Block<'bcx, 'tcx>, |
| 101 | + ty: Ty<'tcx>, |
| 102 | + method_id: DefId, |
| 103 | + trait_id: DefId, |
| 104 | + substs: &'tcx Substs<'tcx>) |
| 105 | + -> OperandRef<'tcx> { |
| 106 | + let ccx = bcx.ccx(); |
| 107 | + let tcx = bcx.tcx(); |
| 108 | + let mname = tcx.item_name(method_id); |
| 109 | + let subst::SeparateVecsPerParamSpace { |
| 110 | + types: rcvr_type, |
| 111 | + selfs: rcvr_self, |
| 112 | + fns: rcvr_method |
| 113 | + } = substs.clone().types.split(); |
| 114 | + let trait_substs = Substs::erased( |
| 115 | + subst::VecPerParamSpace::new(rcvr_type, rcvr_self, Vec::new()) |
| 116 | + ); |
| 117 | + let trait_substs = tcx.mk_substs(trait_substs); |
| 118 | + let trait_ref = ty::Binder(ty::TraitRef::new(trait_id, trait_substs)); |
| 119 | + let vtbl = fulfill_obligation(ccx, DUMMY_SP, trait_ref); |
| 120 | + match vtbl { |
| 121 | + traits::VtableImpl(traits::VtableImplData { impl_def_id, substs: imp_substs, .. }) => { |
| 122 | + assert!(!imp_substs.types.needs_infer()); |
| 123 | + let subst::SeparateVecsPerParamSpace { |
| 124 | + types: impl_type, |
| 125 | + selfs: impl_self, |
| 126 | + fns: _ |
| 127 | + } = imp_substs.types.split(); |
| 128 | + let callee_substs = Substs::erased( |
| 129 | + subst::VecPerParamSpace::new(impl_type, impl_self, rcvr_method) |
| 130 | + ); |
| 131 | + let mth = tcx.get_impl_method(impl_def_id, callee_substs, mname); |
| 132 | + let mthsubsts = tcx.mk_substs(mth.substs); |
| 133 | + self.trans_fn_ref(bcx, ty, mthsubsts, mth.method.def_id) |
| 134 | + }, |
| 135 | + traits::VtableObject(ref data) => { |
| 136 | + let idx = traits::get_vtable_index_of_object_method(tcx, data, method_id); |
| 137 | + OperandRef::from_rvalue_datum( |
| 138 | + meth::trans_object_shim(ccx, data.upcast_trait_ref.clone(), method_id, idx) |
| 139 | + ) |
| 140 | + } |
| 141 | + _ => { |
| 142 | + tcx.sess.bug(&format!("static call to invalid vtable: {:?}", vtbl)); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +fn is_named_tuple_constructor(tcx: &ty::ctxt, def_id: DefId) -> bool { |
| 149 | + let node_id = match tcx.map.as_local_node_id(def_id) { |
| 150 | + Some(n) => n, |
| 151 | + None => { return false; } |
| 152 | + }; |
| 153 | + match tcx.map.find(node_id).expect("local item should be in ast map") { |
| 154 | + map::NodeVariant(v) => { |
| 155 | + v.node.data.is_tuple() |
| 156 | + } |
| 157 | + map::NodeStructCtor(_) => true, |
| 158 | + _ => false |
| 159 | + } |
| 160 | +} |
0 commit comments