2014-08-01 22:25:41 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-08-11 15:46:54 -04:00
|
|
|
use common::*;
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-05-11 04:14:41 +03:00
|
|
|
use rustc::infer::TransNormalize;
|
2016-08-11 15:46:54 -04:00
|
|
|
use rustc::ty::fold::{TypeFolder, TypeFoldable};
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty::subst::{Subst, Substs};
|
2016-08-17 22:50:55 +03:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2016-02-23 22:04:27 +02:00
|
|
|
use rustc::util::ppaux;
|
2016-08-11 15:46:54 -04:00
|
|
|
use rustc::util::common::MemoizationMap;
|
2016-02-23 22:04:27 +02:00
|
|
|
use std::fmt;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-02-23 22:04:27 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub struct Instance<'tcx> {
|
2015-08-16 06:32:28 -04:00
|
|
|
pub def: DefId,
|
2016-03-09 18:22:05 -05:00
|
|
|
pub substs: &'tcx Substs<'tcx>,
|
2014-04-20 19:29:56 +03:00
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
|
2016-02-23 22:04:27 +02:00
|
|
|
impl<'tcx> fmt::Display for Instance<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-10-28 16:26:47 -06:00
|
|
|
ppaux::parameterized(f, &self.substs, self.def, &[])
|
2016-02-23 22:04:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Instance<'tcx> {
|
2016-03-09 18:22:05 -05:00
|
|
|
pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>)
|
|
|
|
-> Instance<'tcx> {
|
2016-08-18 08:32:50 +03:00
|
|
|
assert!(substs.regions().all(|&r| r == ty::ReErased));
|
2016-03-09 18:22:05 -05:00
|
|
|
Instance { def: def_id, substs: substs }
|
|
|
|
}
|
2016-05-11 17:11:20 -04:00
|
|
|
pub fn mono<'a>(scx: &SharedCrateContext<'a, 'tcx>, def_id: DefId) -> Instance<'tcx> {
|
|
|
|
Instance::new(def_id, scx.empty_substs_for_def_id(def_id))
|
2016-02-23 22:04:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
/// Monomorphizes a type from the AST by first applying the in-scope
|
|
|
|
/// substitutions and then normalizing any associated types.
|
2016-08-08 20:50:19 -04:00
|
|
|
pub fn apply_param_substs<'a, 'tcx, T>(scx: &SharedCrateContext<'a, 'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
param_substs: &Substs<'tcx>,
|
|
|
|
value: &T)
|
|
|
|
-> T
|
2016-05-11 04:14:41 +03:00
|
|
|
where T: TransNormalize<'tcx>
|
2014-12-17 14:16:28 -05:00
|
|
|
{
|
2016-08-08 20:50:19 -04:00
|
|
|
let tcx = scx.tcx();
|
2016-08-08 09:40:12 -04:00
|
|
|
debug!("apply_param_substs(param_substs={:?}, value={:?})", param_substs, value);
|
2014-12-17 14:16:28 -05:00
|
|
|
let substituted = value.subst(tcx, param_substs);
|
2016-08-11 15:46:54 -04:00
|
|
|
let substituted = scx.tcx().erase_regions(&substituted);
|
|
|
|
AssociatedTypeNormalizer::new(scx).fold(&substituted)
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
|
|
|
|
2015-08-02 22:52:50 +03:00
|
|
|
|
|
|
|
/// Returns the normalized type of a struct field
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn field_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
param_substs: &Substs<'tcx>,
|
2016-11-25 01:33:29 +02:00
|
|
|
f: &'tcx ty::FieldDef)
|
2016-05-03 04:56:42 +03:00
|
|
|
-> Ty<'tcx>
|
2015-08-02 22:52:50 +03:00
|
|
|
{
|
2016-03-11 02:33:20 +02:00
|
|
|
tcx.normalize_associated_type(&f.ty(tcx, param_substs))
|
2015-08-02 22:52:50 +03:00
|
|
|
}
|
2016-08-08 20:50:19 -04:00
|
|
|
|
2016-08-11 15:46:54 -04:00
|
|
|
struct AssociatedTypeNormalizer<'a, 'b: 'a, 'gcx: 'b> {
|
|
|
|
shared: &'a SharedCrateContext<'b, 'gcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'gcx> AssociatedTypeNormalizer<'a, 'b, 'gcx> {
|
|
|
|
fn new(shared: &'a SharedCrateContext<'b, 'gcx>) -> Self {
|
|
|
|
AssociatedTypeNormalizer {
|
|
|
|
shared: shared,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold<T:TypeFoldable<'gcx>>(&mut self, value: &T) -> T {
|
|
|
|
if !value.has_projection_types() {
|
|
|
|
value.clone()
|
|
|
|
} else {
|
|
|
|
value.fold_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizer<'a, 'b, 'gcx> {
|
|
|
|
fn tcx<'c>(&'c self) -> TyCtxt<'c, 'gcx, 'gcx> {
|
|
|
|
self.shared.tcx()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, ty: Ty<'gcx>) -> Ty<'gcx> {
|
|
|
|
if !ty.has_projection_types() {
|
|
|
|
ty
|
|
|
|
} else {
|
|
|
|
self.shared.project_cache().memoize(ty, || {
|
|
|
|
debug!("AssociatedTypeNormalizer: ty={:?}", ty);
|
|
|
|
self.shared.tcx().normalize_associated_type(&ty)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|