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-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-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-03-22 19:23:36 +02:00
|
|
|
use common::*;
|
2016-02-23 22:04:27 +02:00
|
|
|
use rustc::util::ppaux;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
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-08-08 23:39:49 +03:00
|
|
|
ppaux::parameterized(f, &self.substs, self.def, ppaux::Ns::Value, &[])
|
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-05-03 05:23:22 +03:00
|
|
|
pub fn apply_param_substs<'a, 'tcx, T>(tcx: TyCtxt<'a, 'tcx, '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
|
|
|
{
|
|
|
|
let substituted = value.subst(tcx, param_substs);
|
2016-03-11 02:33:20 +02:00
|
|
|
tcx.normalize_associated_type(&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>,
|
|
|
|
f: ty::FieldDef<'tcx>)
|
|
|
|
-> 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
|
|
|
}
|