2015-09-06 21:51:58 +03:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! type context book-keeping
|
|
|
|
|
2016-01-06 10:16:58 -05:00
|
|
|
use dep_graph::{DepGraph, DepTrackingMap};
|
2015-09-06 21:51:58 +03:00
|
|
|
use session::Session;
|
|
|
|
use middle;
|
2016-04-12 22:58:55 +09:00
|
|
|
use middle::cstore::LOCAL_CRATE;
|
2016-07-27 17:26:55 -04:00
|
|
|
use hir::TraitMap;
|
2016-03-29 12:54:26 +03:00
|
|
|
use hir::def::DefMap;
|
2016-05-06 14:52:57 -04:00
|
|
|
use hir::def_id::{DefId, DefIndex};
|
|
|
|
use hir::map as ast_map;
|
|
|
|
use hir::map::{DefKey, DefPath, DefPathData, DisambiguatedDefPathData};
|
2015-09-06 21:51:58 +03:00
|
|
|
use middle::free_region::FreeRegionMap;
|
|
|
|
use middle::region::RegionMaps;
|
|
|
|
use middle::resolve_lifetime;
|
|
|
|
use middle::stability;
|
2016-05-11 04:14:41 +03:00
|
|
|
use ty::subst::{self, Substs};
|
2016-03-22 17:30:57 +02:00
|
|
|
use traits;
|
|
|
|
use ty::{self, TraitRef, Ty, TypeAndMut};
|
|
|
|
use ty::{TyS, TypeVariants};
|
|
|
|
use ty::{AdtDef, ClosureSubsts, ExistentialBounds, Region};
|
2016-03-29 13:14:01 +03:00
|
|
|
use hir::FreevarMap;
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::{BareFnTy, InferTy, ParamTy, ProjectionTy, TraitTy};
|
|
|
|
use ty::{TyVar, TyVid, IntVar, IntVid, FloatVar, FloatVid};
|
|
|
|
use ty::TypeVariants::*;
|
2016-04-19 09:11:46 +03:00
|
|
|
use ty::layout::{Layout, TargetDataLayout};
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::maps;
|
2015-12-22 16:39:33 -05:00
|
|
|
use util::common::MemoizationMap;
|
2015-09-06 21:51:58 +03:00
|
|
|
use util::nodemap::{NodeMap, NodeSet, DefIdMap, DefIdSet};
|
2016-03-23 04:56:49 +02:00
|
|
|
use util::nodemap::{FnvHashMap, FnvHashSet};
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
use arena::TypedArena;
|
|
|
|
use std::borrow::Borrow;
|
|
|
|
use std::cell::{Cell, RefCell, Ref};
|
|
|
|
use std::hash::{Hash, Hasher};
|
2016-05-11 04:14:41 +03:00
|
|
|
use std::mem;
|
2016-05-03 04:56:42 +03:00
|
|
|
use std::ops::Deref;
|
2015-09-06 21:51:58 +03:00
|
|
|
use std::rc::Rc;
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::ast::{self, Name, NodeId};
|
|
|
|
use syntax::attr;
|
2016-04-16 18:05:06 +03:00
|
|
|
use syntax::parse::token::{self, keywords};
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use hir;
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Internal storage
|
|
|
|
pub struct CtxtArenas<'tcx> {
|
|
|
|
// internings
|
|
|
|
type_: TypedArena<TyS<'tcx>>,
|
2016-04-29 08:30:54 +03:00
|
|
|
type_list: TypedArena<Vec<Ty<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
substs: TypedArena<Substs<'tcx>>,
|
|
|
|
bare_fn: TypedArena<BareFnTy<'tcx>>,
|
|
|
|
region: TypedArena<Region>,
|
|
|
|
stability: TypedArena<attr::Stability>,
|
2016-04-19 09:11:46 +03:00
|
|
|
layout: TypedArena<Layout>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
// references
|
|
|
|
trait_defs: TypedArena<ty::TraitDef<'tcx>>,
|
|
|
|
adt_defs: TypedArena<ty::AdtDefData<'tcx, 'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> CtxtArenas<'tcx> {
|
|
|
|
pub fn new() -> CtxtArenas<'tcx> {
|
|
|
|
CtxtArenas {
|
|
|
|
type_: TypedArena::new(),
|
2016-04-29 08:30:54 +03:00
|
|
|
type_list: TypedArena::new(),
|
2015-09-06 21:51:58 +03:00
|
|
|
substs: TypedArena::new(),
|
|
|
|
bare_fn: TypedArena::new(),
|
|
|
|
region: TypedArena::new(),
|
|
|
|
stability: TypedArena::new(),
|
2016-04-19 09:11:46 +03:00
|
|
|
layout: TypedArena::new(),
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
trait_defs: TypedArena::new(),
|
|
|
|
adt_defs: TypedArena::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub struct CtxtInterners<'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
/// The arenas that types etc are allocated from.
|
|
|
|
arenas: &'tcx CtxtArenas<'tcx>,
|
|
|
|
|
|
|
|
/// Specifically use a speedy hash algorithm for these hash sets,
|
|
|
|
/// they're accessed quite often.
|
2016-05-11 04:14:41 +03:00
|
|
|
type_: RefCell<FnvHashSet<Interned<'tcx, TyS<'tcx>>>>,
|
|
|
|
type_list: RefCell<FnvHashSet<Interned<'tcx, [Ty<'tcx>]>>>,
|
|
|
|
substs: RefCell<FnvHashSet<Interned<'tcx, Substs<'tcx>>>>,
|
|
|
|
bare_fn: RefCell<FnvHashSet<Interned<'tcx, BareFnTy<'tcx>>>>,
|
|
|
|
region: RefCell<FnvHashSet<Interned<'tcx, Region>>>,
|
2016-04-28 03:13:17 +03:00
|
|
|
stability: RefCell<FnvHashSet<&'tcx attr::Stability>>,
|
|
|
|
layout: RefCell<FnvHashSet<&'tcx Layout>>,
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
fn new(arenas: &'tcx CtxtArenas<'tcx>) -> CtxtInterners<'tcx> {
|
|
|
|
CtxtInterners {
|
|
|
|
arenas: arenas,
|
|
|
|
type_: RefCell::new(FnvHashSet()),
|
2016-04-29 08:30:54 +03:00
|
|
|
type_list: RefCell::new(FnvHashSet()),
|
2016-04-28 03:13:17 +03:00
|
|
|
substs: RefCell::new(FnvHashSet()),
|
|
|
|
bare_fn: RefCell::new(FnvHashSet()),
|
|
|
|
region: RefCell::new(FnvHashSet()),
|
|
|
|
stability: RefCell::new(FnvHashSet()),
|
|
|
|
layout: RefCell::new(FnvHashSet())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
/// Intern a type. global_interners is Some only if this is
|
|
|
|
/// a local interner and global_interners is its counterpart.
|
|
|
|
fn intern_ty(&self, st: TypeVariants<'tcx>,
|
|
|
|
global_interners: Option<&CtxtInterners<'gcx>>)
|
|
|
|
-> Ty<'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
let ty = {
|
|
|
|
let mut interner = self.type_.borrow_mut();
|
2016-05-11 04:14:41 +03:00
|
|
|
let global_interner = global_interners.map(|interners| {
|
|
|
|
interners.type_.borrow_mut()
|
|
|
|
});
|
|
|
|
if let Some(&Interned(ty)) = interner.get(&st) {
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
if let Some(ref interner) = global_interner {
|
|
|
|
if let Some(&Interned(ty)) = interner.get(&st) {
|
|
|
|
return ty;
|
|
|
|
}
|
2016-04-28 03:13:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let flags = super::flags::FlagComputation::for_sty(&st);
|
2016-05-11 04:14:41 +03:00
|
|
|
let ty_struct = TyS {
|
2016-04-28 03:13:17 +03:00
|
|
|
sty: st,
|
|
|
|
flags: Cell::new(flags.flags),
|
|
|
|
region_depth: flags.depth,
|
2016-05-11 04:14:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// HACK(eddyb) Depend on flags being accurate to
|
|
|
|
// determine that all contents are in the global tcx.
|
|
|
|
// See comments on Lift for why we can't use that.
|
|
|
|
if !flags.flags.intersects(ty::TypeFlags::KEEP_IN_LOCAL_TCX) {
|
|
|
|
if let Some(interner) = global_interners {
|
|
|
|
let ty_struct: TyS<'gcx> = unsafe {
|
|
|
|
mem::transmute(ty_struct)
|
|
|
|
};
|
|
|
|
let ty: Ty<'gcx> = interner.arenas.type_.alloc(ty_struct);
|
|
|
|
global_interner.unwrap().insert(Interned(ty));
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make sure we don't end up with inference
|
|
|
|
// types/regions in the global tcx.
|
|
|
|
if global_interners.is_none() {
|
|
|
|
drop(interner);
|
|
|
|
bug!("Attempted to intern `{:?}` which contains \
|
|
|
|
inference types/regions in the global type context",
|
|
|
|
&ty_struct);
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 03:13:17 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
// Don't be &mut TyS.
|
|
|
|
let ty: Ty<'tcx> = self.arenas.type_.alloc(ty_struct);
|
|
|
|
interner.insert(Interned(ty));
|
2016-04-28 03:13:17 +03:00
|
|
|
ty
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("Interned type: {:?} Pointer: {:?}",
|
|
|
|
ty, ty as *const TyS);
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
pub struct CommonTypes<'tcx> {
|
|
|
|
pub bool: Ty<'tcx>,
|
|
|
|
pub char: Ty<'tcx>,
|
|
|
|
pub isize: Ty<'tcx>,
|
|
|
|
pub i8: Ty<'tcx>,
|
|
|
|
pub i16: Ty<'tcx>,
|
|
|
|
pub i32: Ty<'tcx>,
|
|
|
|
pub i64: Ty<'tcx>,
|
|
|
|
pub usize: Ty<'tcx>,
|
|
|
|
pub u8: Ty<'tcx>,
|
|
|
|
pub u16: Ty<'tcx>,
|
|
|
|
pub u32: Ty<'tcx>,
|
|
|
|
pub u64: Ty<'tcx>,
|
|
|
|
pub f32: Ty<'tcx>,
|
|
|
|
pub f64: Ty<'tcx>,
|
|
|
|
pub err: Ty<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Tables<'tcx> {
|
|
|
|
/// Stores the types for various nodes in the AST. Note that this table
|
|
|
|
/// is not guaranteed to be populated until after typeck. See
|
|
|
|
/// typeck::check::fn_ctxt for details.
|
|
|
|
pub node_types: NodeMap<Ty<'tcx>>,
|
|
|
|
|
|
|
|
/// Stores the type parameters which were substituted to obtain the type
|
|
|
|
/// of this node. This only applies to nodes that refer to entities
|
|
|
|
/// parameterized by type parameters, such as generic fns, types, or
|
|
|
|
/// other items.
|
|
|
|
pub item_substs: NodeMap<ty::ItemSubsts<'tcx>>,
|
|
|
|
|
2015-09-14 14:55:56 +03:00
|
|
|
pub adjustments: NodeMap<ty::adjustment::AutoAdjustment<'tcx>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
pub method_map: ty::MethodMap<'tcx>,
|
|
|
|
|
|
|
|
/// Borrows
|
|
|
|
pub upvar_capture_map: ty::UpvarCaptureMap,
|
|
|
|
|
|
|
|
/// Records the type of each closure. The def ID is the ID of the
|
|
|
|
/// expression defining the closure.
|
|
|
|
pub closure_tys: DefIdMap<ty::ClosureTy<'tcx>>,
|
|
|
|
|
|
|
|
/// Records the type of each closure. The def ID is the ID of the
|
|
|
|
/// expression defining the closure.
|
|
|
|
pub closure_kinds: DefIdMap<ty::ClosureKind>,
|
2015-10-21 15:46:30 -04:00
|
|
|
|
|
|
|
/// For each fn, records the "liberated" types of its arguments
|
|
|
|
/// and return type. Liberated means that all bound regions
|
|
|
|
/// (including late-bound regions) are replaced with free
|
|
|
|
/// equivalents. This table is not used in trans (since regions
|
|
|
|
/// are erased there) and hence is not serialized to metadata.
|
|
|
|
pub liberated_fn_sigs: NodeMap<ty::FnSig<'tcx>>,
|
2016-02-11 18:31:42 +02:00
|
|
|
|
|
|
|
/// For each FRU expression, record the normalized types of the fields
|
|
|
|
/// of the struct - this is needed because it is non-trivial to
|
|
|
|
/// normalize while preserving regions. This table is used only in
|
|
|
|
/// MIR construction and hence is not serialized to metadata.
|
|
|
|
pub fru_field_types: NodeMap<Vec<Ty<'tcx>>>
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> Tables<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
pub fn empty() -> Tables<'tcx> {
|
|
|
|
Tables {
|
|
|
|
node_types: FnvHashMap(),
|
|
|
|
item_substs: NodeMap(),
|
|
|
|
adjustments: NodeMap(),
|
|
|
|
method_map: FnvHashMap(),
|
|
|
|
upvar_capture_map: FnvHashMap(),
|
|
|
|
closure_tys: DefIdMap(),
|
|
|
|
closure_kinds: DefIdMap(),
|
2015-10-21 15:46:30 -04:00
|
|
|
liberated_fn_sigs: NodeMap(),
|
2016-02-11 18:31:42 +02:00
|
|
|
fru_field_types: NodeMap()
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> CommonTypes<'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
|
2016-05-11 04:14:41 +03:00
|
|
|
let mk = |sty| interners.intern_ty(sty, None);
|
2015-09-06 21:51:58 +03:00
|
|
|
CommonTypes {
|
|
|
|
bool: mk(TyBool),
|
|
|
|
char: mk(TyChar),
|
|
|
|
err: mk(TyError),
|
2016-02-08 16:20:57 +01:00
|
|
|
isize: mk(TyInt(ast::IntTy::Is)),
|
|
|
|
i8: mk(TyInt(ast::IntTy::I8)),
|
|
|
|
i16: mk(TyInt(ast::IntTy::I16)),
|
|
|
|
i32: mk(TyInt(ast::IntTy::I32)),
|
|
|
|
i64: mk(TyInt(ast::IntTy::I64)),
|
|
|
|
usize: mk(TyUint(ast::UintTy::Us)),
|
|
|
|
u8: mk(TyUint(ast::UintTy::U8)),
|
|
|
|
u16: mk(TyUint(ast::UintTy::U16)),
|
|
|
|
u32: mk(TyUint(ast::UintTy::U32)),
|
|
|
|
u64: mk(TyUint(ast::UintTy::U64)),
|
2016-02-08 16:09:01 +01:00
|
|
|
f32: mk(TyFloat(ast::FloatTy::F32)),
|
|
|
|
f64: mk(TyFloat(ast::FloatTy::F64)),
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The data structure to keep track of all the information that typechecker
|
|
|
|
/// generates so that so that it can be reused and doesn't have to be redone
|
|
|
|
/// later on.
|
2016-05-03 04:56:42 +03:00
|
|
|
#[derive(Copy, Clone)]
|
2016-05-03 05:23:22 +03:00
|
|
|
pub struct TyCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
gcx: &'a GlobalCtxt<'gcx>,
|
2016-04-28 03:13:17 +03:00
|
|
|
interners: &'a CtxtInterners<'tcx>
|
2016-05-03 04:56:42 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> Deref for TyCtxt<'a, 'gcx, 'tcx> {
|
|
|
|
type Target = &'a GlobalCtxt<'gcx>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2016-05-03 04:56:42 +03:00
|
|
|
&self.gcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GlobalCtxt<'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
global_interners: CtxtInterners<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-05-17 12:05:02 -04:00
|
|
|
pub specializes_cache: RefCell<traits::SpecializesCache>,
|
|
|
|
|
2016-01-05 13:07:45 -05:00
|
|
|
pub dep_graph: DepGraph,
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Common types, pre-interned for your convenience.
|
|
|
|
pub types: CommonTypes<'tcx>,
|
|
|
|
|
2015-09-28 15:00:15 +13:00
|
|
|
pub sess: &'tcx Session,
|
2016-07-27 17:26:55 -04:00
|
|
|
|
|
|
|
/// Map from path id to the results from resolve; generated
|
|
|
|
/// initially by resolve and updated during typeck in some cases
|
|
|
|
/// (e.g., UFCS paths)
|
2015-11-04 00:02:22 -06:00
|
|
|
pub def_map: RefCell<DefMap>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-07-27 17:26:55 -04:00
|
|
|
/// Map indicating what traits are in scope for places where this
|
|
|
|
/// is relevant; generated by resolve.
|
|
|
|
pub trait_map: TraitMap,
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
pub named_region_map: resolve_lifetime::NamedRegionMap,
|
|
|
|
|
|
|
|
pub region_maps: RegionMaps,
|
|
|
|
|
|
|
|
// For each fn declared in the local crate, type check stores the
|
|
|
|
// free-region relationships that were deduced from its where
|
|
|
|
// clauses and parameter types. These are then read-again by
|
|
|
|
// borrowck. (They are not used during trans, and hence are not
|
|
|
|
// serialized or needed for cross-crate fns.)
|
|
|
|
free_region_maps: RefCell<NodeMap<FreeRegionMap>>,
|
|
|
|
// FIXME: jroesch make this a refcell
|
|
|
|
|
|
|
|
pub tables: RefCell<Tables<'tcx>>,
|
|
|
|
|
|
|
|
/// Maps from a trait item to the trait item "descriptor"
|
2015-12-22 16:39:33 -05:00
|
|
|
pub impl_or_trait_items: RefCell<DepTrackingMap<maps::ImplOrTraitItems<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Maps from a trait def-id to a list of the def-ids of its trait items
|
2015-12-22 16:39:33 -05:00
|
|
|
pub trait_item_def_ids: RefCell<DepTrackingMap<maps::TraitItemDefIds<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2015-12-22 16:39:33 -05:00
|
|
|
/// A cache for the trait_items() routine; note that the routine
|
2016-01-06 10:16:58 -05:00
|
|
|
/// itself pushes the `TraitItems` dependency node.
|
|
|
|
trait_items_cache: RefCell<DepTrackingMap<maps::TraitItems<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2015-12-22 16:39:33 -05:00
|
|
|
pub impl_trait_refs: RefCell<DepTrackingMap<maps::ImplTraitRefs<'tcx>>>,
|
|
|
|
pub trait_defs: RefCell<DepTrackingMap<maps::TraitDefs<'tcx>>>,
|
|
|
|
pub adt_defs: RefCell<DepTrackingMap<maps::AdtDefs<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Maps from the def-id of an item (trait/struct/enum/fn) to its
|
|
|
|
/// associated predicates.
|
2015-12-22 16:39:33 -05:00
|
|
|
pub predicates: RefCell<DepTrackingMap<maps::Predicates<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Maps from the def-id of a trait to the list of
|
|
|
|
/// super-predicates. This is a subset of the full list of
|
|
|
|
/// predicates. We store these in a separate map because we must
|
|
|
|
/// evaluate them even during type conversion, often before the
|
|
|
|
/// full predicates are available (note that supertraits have
|
|
|
|
/// additional acyclicity requirements).
|
2015-12-22 16:39:33 -05:00
|
|
|
pub super_predicates: RefCell<DepTrackingMap<maps::Predicates<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
pub map: ast_map::Map<'tcx>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
|
|
|
// Records the free variables refrenced by every closure
|
|
|
|
// expression. Do not track deps for this, just recompute it from
|
|
|
|
// scratch every time.
|
2015-09-06 21:51:58 +03:00
|
|
|
pub freevars: RefCell<FreevarMap>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
2016-04-19 22:43:10 +09:00
|
|
|
pub maybe_unused_trait_imports: NodeSet,
|
|
|
|
|
2015-12-22 16:39:33 -05:00
|
|
|
// Records the type of every item.
|
|
|
|
pub tcache: RefCell<DepTrackingMap<maps::Tcache<'tcx>>>,
|
|
|
|
|
|
|
|
// Internal cache for metadata decoding. No need to track deps on this.
|
2015-09-06 21:51:58 +03:00
|
|
|
pub rcache: RefCell<FnvHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
|
|
|
// Cache for the type-contents routine. FIXME -- track deps?
|
2015-09-06 21:51:58 +03:00
|
|
|
pub tc_cache: RefCell<FnvHashMap<Ty<'tcx>, ty::contents::TypeContents>>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
|
|
|
// FIXME no dep tracking, but we should be able to remove this
|
2015-09-06 21:51:58 +03:00
|
|
|
pub ty_param_defs: RefCell<NodeMap<ty::TypeParameterDef<'tcx>>>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
|
|
|
// FIXME dep tracking -- should be harmless enough
|
2015-09-06 21:51:58 +03:00
|
|
|
pub normalized_cache: RefCell<FnvHashMap<Ty<'tcx>, Ty<'tcx>>>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
pub lang_items: middle::lang_items::LanguageItems,
|
|
|
|
|
|
|
|
/// Maps from def-id of a type or region parameter to its
|
|
|
|
/// (inferred) variance.
|
2015-12-22 16:39:33 -05:00
|
|
|
pub item_variance_map: RefCell<DepTrackingMap<maps::ItemVariances<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// True if the variance has been computed yet; false otherwise.
|
|
|
|
pub variance_computed: Cell<bool>,
|
|
|
|
|
|
|
|
/// Maps a DefId of a type to a list of its inherent impls.
|
|
|
|
/// Contains implementations of methods that are inherent to a type.
|
|
|
|
/// Methods in these implementations don't need to be exported.
|
2015-12-22 16:39:33 -05:00
|
|
|
pub inherent_impls: RefCell<DepTrackingMap<maps::InherentImpls<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Maps a DefId of an impl to a list of its items.
|
|
|
|
/// Note that this contains all of the impls that we know about,
|
|
|
|
/// including ones in other crates. It's not clear that this is the best
|
|
|
|
/// way to do it.
|
2015-12-22 16:39:33 -05:00
|
|
|
pub impl_items: RefCell<DepTrackingMap<maps::ImplItems<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Set of used unsafe nodes (functions or blocks). Unsafe nodes not
|
|
|
|
/// present in this set can be warned about.
|
|
|
|
pub used_unsafe: RefCell<NodeSet>,
|
|
|
|
|
|
|
|
/// Set of nodes which mark locals as mutable which end up getting used at
|
|
|
|
/// some point. Local variable definitions not in this set can be warned
|
|
|
|
/// about.
|
|
|
|
pub used_mut_nodes: RefCell<NodeSet>,
|
|
|
|
|
2016-04-19 22:43:10 +09:00
|
|
|
/// Set of trait imports actually used in the method resolution.
|
|
|
|
/// This is used for warning unused imports.
|
|
|
|
pub used_trait_imports: RefCell<NodeSet>,
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// The set of external nominal types whose implementations have been read.
|
|
|
|
/// This is used for lazy resolution of methods.
|
|
|
|
pub populated_external_types: RefCell<DefIdSet>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// The set of external primitive types whose implementations have been read.
|
|
|
|
/// FIXME(arielb1): why is this separate from populated_external_types?
|
|
|
|
pub populated_external_primitive_impls: RefCell<DefIdSet>,
|
|
|
|
|
2016-03-03 14:48:08 +01:00
|
|
|
/// Cache used by const_eval when decoding external constants.
|
|
|
|
/// Contains `None` when the constant has been fetched but doesn't exist.
|
|
|
|
/// Constains `Some(expr_id, type)` otherwise.
|
|
|
|
/// `type` is `None` in case it's not a primitive type
|
|
|
|
pub extern_const_statics: RefCell<DefIdMap<Option<(NodeId, Option<Ty<'tcx>>)>>>,
|
|
|
|
/// Cache used by const_eval when decoding extern const fns
|
2015-09-06 21:51:58 +03:00
|
|
|
pub extern_const_fns: RefCell<DefIdMap<NodeId>>,
|
|
|
|
|
|
|
|
/// Maps any item's def-id to its stability index.
|
|
|
|
pub stability: RefCell<stability::Index<'tcx>>,
|
|
|
|
|
|
|
|
/// Caches the results of trait selection. This cache is used
|
|
|
|
/// for things that do not have to do with the parameters in scope.
|
|
|
|
pub selection_cache: traits::SelectionCache<'tcx>,
|
|
|
|
|
2015-10-21 14:50:38 +03:00
|
|
|
/// Caches the results of trait evaluation. This cache is used
|
|
|
|
/// for things that do not have to do with the parameters in scope.
|
|
|
|
/// Merge this with `selection_cache`?
|
|
|
|
pub evaluation_cache: traits::EvaluationCache<'tcx>,
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// A set of predicates that have been fulfilled *somewhere*.
|
|
|
|
/// This is used to avoid duplicate work. Predicates are only
|
|
|
|
/// added to this set when they mention only "global" names
|
|
|
|
/// (i.e., no type or lifetime parameters).
|
2016-01-20 16:56:29 -05:00
|
|
|
pub fulfilled_predicates: RefCell<traits::GlobalFulfilledPredicates<'tcx>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Caches the representation hints for struct definitions.
|
2016-01-06 10:16:58 -05:00
|
|
|
repr_hint_cache: RefCell<DepTrackingMap<maps::ReprHints<'tcx>>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Maps Expr NodeId's to their constant qualification.
|
2016-01-21 10:52:37 +01:00
|
|
|
pub const_qualif_map: RefCell<NodeMap<middle::const_qualif::ConstQualif>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Caches CoerceUnsized kinds for impls on custom types.
|
2015-09-14 14:55:56 +03:00
|
|
|
pub custom_coerce_unsized_kinds: RefCell<DefIdMap<ty::adjustment::CustomCoerceUnsized>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
/// Maps a cast expression to its kind. This is keyed on the
|
|
|
|
/// *from* expression of the cast, not the cast itself.
|
|
|
|
pub cast_kinds: RefCell<NodeMap<ty::cast::CastKind>>,
|
|
|
|
|
|
|
|
/// Maps Fn items to a collection of fragment infos.
|
|
|
|
///
|
|
|
|
/// The main goal is to identify data (each of which may be moved
|
|
|
|
/// or assigned) whose subparts are not moved nor assigned
|
|
|
|
/// (i.e. their state is *unfragmented*) and corresponding ast
|
|
|
|
/// nodes where the path to that data is moved or assigned.
|
|
|
|
///
|
|
|
|
/// In the long term, unfragmented values will have their
|
|
|
|
/// destructor entirely driven by a single stack-local drop-flag,
|
|
|
|
/// and their parents, the collections of the unfragmented values
|
|
|
|
/// (or more simply, "fragmented values"), are mapped to the
|
|
|
|
/// corresponding collections of stack-local drop-flags.
|
|
|
|
///
|
|
|
|
/// (However, in the short term that is not the case; e.g. some
|
|
|
|
/// unfragmented paths still need to be zeroed, namely when they
|
|
|
|
/// reference parent data from an outer scope that was not
|
|
|
|
/// entirely moved, and therefore that needs to be zeroed so that
|
|
|
|
/// we do not get double-drop when we hit the end of the parent
|
|
|
|
/// scope.)
|
|
|
|
///
|
|
|
|
/// Also: currently the table solely holds keys for node-ids of
|
|
|
|
/// unfragmented values (see `FragmentInfo` enum definition), but
|
|
|
|
/// longer-term we will need to also store mappings from
|
|
|
|
/// fragmented data to the set of unfragmented pieces that
|
|
|
|
/// constitute it.
|
|
|
|
pub fragment_infos: RefCell<DefIdMap<Vec<ty::FragmentInfo>>>,
|
2016-02-14 12:30:38 -05:00
|
|
|
|
|
|
|
/// The definite name of the current crate after taking into account
|
|
|
|
/// attributes, commandline parameters, etc.
|
|
|
|
pub crate_name: token::InternedString,
|
2016-04-18 16:03:16 +03:00
|
|
|
|
|
|
|
/// Data layout specification for the current target.
|
|
|
|
pub data_layout: TargetDataLayout,
|
2016-04-19 09:11:46 +03:00
|
|
|
|
|
|
|
/// Cache for layouts computed from types.
|
|
|
|
pub layout_cache: RefCell<FnvHashMap<Ty<'tcx>, &'tcx Layout>>,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 03:13:17 +03:00
|
|
|
impl<'tcx> GlobalCtxt<'tcx> {
|
|
|
|
/// Get the global TyCtxt.
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn global_tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
TyCtxt {
|
|
|
|
gcx: self,
|
|
|
|
interners: &self.global_interners
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn crate_name(self, cnum: ast::CrateNum) -> token::InternedString {
|
2016-03-16 05:40:14 -04:00
|
|
|
if cnum == LOCAL_CRATE {
|
|
|
|
self.crate_name.clone()
|
|
|
|
} else {
|
|
|
|
self.sess.cstore.crate_name(cnum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn crate_disambiguator(self, cnum: ast::CrateNum) -> token::InternedString {
|
2016-03-16 05:40:14 -04:00
|
|
|
if cnum == LOCAL_CRATE {
|
2016-07-21 12:41:29 -04:00
|
|
|
self.sess.local_crate_disambiguator()
|
2016-03-16 05:40:14 -04:00
|
|
|
} else {
|
2016-03-29 12:10:26 -04:00
|
|
|
self.sess.cstore.crate_disambiguator(cnum)
|
2016-03-16 05:40:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 14:52:57 -04:00
|
|
|
/// Given a def-key `key` and a crate `krate`, finds the def-index
|
|
|
|
/// that `krate` assigned to `key`. This `DefIndex` will always be
|
|
|
|
/// relative to `krate`.
|
|
|
|
///
|
|
|
|
/// Returns `None` if there is no `DefIndex` with that key.
|
|
|
|
pub fn def_index_for_def_key(self, krate: ast::CrateNum, key: DefKey)
|
|
|
|
-> Option<DefIndex> {
|
|
|
|
if krate == LOCAL_CRATE {
|
|
|
|
self.map.def_index_for_def_key(key)
|
|
|
|
} else {
|
|
|
|
self.sess.cstore.def_index_for_def_key(krate, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn retrace_path(self, path: &DefPath) -> Option<DefId> {
|
2016-07-29 16:49:26 -04:00
|
|
|
debug!("retrace_path(path={:?}, krate={:?})", path, self.crate_name(path.krate));
|
2016-05-06 14:52:57 -04:00
|
|
|
|
|
|
|
let root_key = DefKey {
|
|
|
|
parent: None,
|
|
|
|
disambiguated_data: DisambiguatedDefPathData {
|
|
|
|
data: DefPathData::CrateRoot,
|
|
|
|
disambiguator: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let root_index = self.def_index_for_def_key(path.krate, root_key)
|
|
|
|
.expect("no root key?");
|
|
|
|
|
|
|
|
debug!("retrace_path: root_index={:?}", root_index);
|
|
|
|
|
|
|
|
let mut index = root_index;
|
|
|
|
for data in &path.data {
|
|
|
|
let key = DefKey { parent: Some(index), disambiguated_data: data.clone() };
|
|
|
|
debug!("retrace_path: key={:?}", key);
|
|
|
|
match self.def_index_for_def_key(path.krate, key) {
|
|
|
|
Some(i) => index = i,
|
|
|
|
None => return None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(DefId { krate: path.krate, index: index })
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn type_parameter_def(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
node_id: NodeId)
|
|
|
|
-> ty::TypeParameterDef<'tcx>
|
|
|
|
{
|
|
|
|
self.ty_param_defs.borrow().get(&node_id).unwrap().clone()
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn node_types(self) -> Ref<'a, NodeMap<Ty<'tcx>>> {
|
2015-09-06 21:51:58 +03:00
|
|
|
fn projection<'a, 'tcx>(tables: &'a Tables<'tcx>) -> &'a NodeMap<Ty<'tcx>> {
|
|
|
|
&tables.node_types
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref::map(self.tables.borrow(), projection)
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn node_type_insert(self, id: NodeId, ty: Ty<'gcx>) {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.tables.borrow_mut().node_types.insert(id, ty);
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn intern_trait_def(self, def: ty::TraitDef<'gcx>)
|
|
|
|
-> &'gcx ty::TraitDef<'gcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
let did = def.trait_ref.def_id;
|
2016-04-28 03:13:17 +03:00
|
|
|
let interned = self.global_interners.arenas.trait_defs.alloc(def);
|
2015-12-01 09:50:07 +01:00
|
|
|
if let Some(prev) = self.trait_defs.borrow_mut().insert(did, interned) {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("Tried to overwrite interned TraitDef: {:?}", prev)
|
2015-12-01 09:50:07 +01:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
interned
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn alloc_trait_def(self, def: ty::TraitDef<'gcx>)
|
|
|
|
-> &'gcx ty::TraitDef<'gcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
self.global_interners.arenas.trait_defs.alloc(def)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-06-11 18:47:47 +03:00
|
|
|
pub fn insert_adt_def(self, did: DefId, adt_def: ty::AdtDefMaster<'gcx>) {
|
|
|
|
// this will need a transmute when reverse-variance is removed
|
|
|
|
if let Some(prev) = self.adt_defs.borrow_mut().insert(did, adt_def) {
|
|
|
|
bug!("Tried to overwrite interned AdtDef: {:?}", prev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn intern_adt_def(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
did: DefId,
|
|
|
|
kind: ty::AdtKind,
|
2016-04-29 06:00:23 +03:00
|
|
|
variants: Vec<ty::VariantDefData<'gcx, 'gcx>>)
|
|
|
|
-> ty::AdtDefMaster<'gcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
let def = ty::AdtDefData::new(self, did, kind, variants);
|
2016-04-28 03:13:17 +03:00
|
|
|
let interned = self.global_interners.arenas.adt_defs.alloc(def);
|
2016-06-11 18:47:47 +03:00
|
|
|
self.insert_adt_def(did, interned);
|
2015-09-06 21:51:58 +03:00
|
|
|
interned
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub fn intern_stability(self, stab: attr::Stability) -> &'gcx attr::Stability {
|
|
|
|
if let Some(st) = self.global_interners.stability.borrow().get(&stab) {
|
2015-09-06 21:51:58 +03:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
let interned = self.global_interners.arenas.stability.alloc(stab);
|
|
|
|
if let Some(prev) = self.global_interners.stability
|
2015-12-01 09:50:07 +01:00
|
|
|
.borrow_mut()
|
2016-03-23 04:56:49 +02:00
|
|
|
.replace(interned) {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("Tried to overwrite interned Stability: {:?}", prev)
|
2015-12-01 09:50:07 +01:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
interned
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub fn intern_layout(self, layout: Layout) -> &'gcx Layout {
|
|
|
|
if let Some(layout) = self.global_interners.layout.borrow().get(&layout) {
|
2016-04-19 09:11:46 +03:00
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
let interned = self.global_interners.arenas.layout.alloc(layout);
|
|
|
|
if let Some(prev) = self.global_interners.layout
|
2016-04-19 09:11:46 +03:00
|
|
|
.borrow_mut()
|
2016-03-23 04:56:49 +02:00
|
|
|
.replace(interned) {
|
2016-04-19 09:11:46 +03:00
|
|
|
bug!("Tried to overwrite interned Layout: {:?}", prev)
|
|
|
|
}
|
|
|
|
interned
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn store_free_region_map(self, id: NodeId, map: FreeRegionMap) {
|
2015-12-01 09:50:07 +01:00
|
|
|
if self.free_region_maps.borrow_mut().insert(id, map).is_some() {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("Tried to overwrite interned FreeRegionMap for NodeId {:?}", id)
|
2015-12-01 09:50:07 +01:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn free_region_map(self, id: NodeId) -> FreeRegionMap {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.free_region_maps.borrow()[&id].clone()
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn lift<T: ?Sized + Lift<'tcx>>(self, value: &T) -> Option<T::Lifted> {
|
2015-09-06 21:51:58 +03:00
|
|
|
value.lift_to_tcx(self)
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
/// Like lift, but only tries in the global tcx.
|
|
|
|
pub fn lift_to_global<T: ?Sized + Lift<'gcx>>(self, value: &T) -> Option<T::Lifted> {
|
|
|
|
value.lift_to_tcx(self.global_tcx())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if self is the same as self.global_tcx().
|
|
|
|
fn is_global(self) -> bool {
|
|
|
|
let local = self.interners as *const _;
|
|
|
|
let global = &self.global_interners as *const _;
|
|
|
|
local as usize == global as usize
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
/// Create a type context and call the closure with a `TyCtxt` reference
|
2015-09-06 21:51:58 +03:00
|
|
|
/// to the context. The closure enforces that the type context and any interned
|
|
|
|
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
|
|
|
|
/// reference to the context, to allow formatting values that need it.
|
2015-09-28 15:00:15 +13:00
|
|
|
pub fn create_and_enter<F, R>(s: &'tcx Session,
|
2016-05-03 04:56:42 +03:00
|
|
|
arenas: &'tcx CtxtArenas<'tcx>,
|
2016-05-31 09:21:49 +02:00
|
|
|
def_map: DefMap,
|
2016-07-27 17:26:55 -04:00
|
|
|
trait_map: TraitMap,
|
2016-05-03 04:56:42 +03:00
|
|
|
named_region_map: resolve_lifetime::NamedRegionMap,
|
|
|
|
map: ast_map::Map<'tcx>,
|
|
|
|
freevars: FreevarMap,
|
2016-04-19 22:43:10 +09:00
|
|
|
maybe_unused_trait_imports: NodeSet,
|
2016-05-03 04:56:42 +03:00
|
|
|
region_maps: RegionMaps,
|
|
|
|
lang_items: middle::lang_items::LanguageItems,
|
|
|
|
stability: stability::Index<'tcx>,
|
2016-02-14 12:30:38 -05:00
|
|
|
crate_name: &str,
|
2016-05-03 04:56:42 +03:00
|
|
|
f: F) -> R
|
2016-05-03 05:23:22 +03:00
|
|
|
where F: for<'b> FnOnce(TyCtxt<'b, 'tcx, 'tcx>) -> R
|
2015-09-06 21:51:58 +03:00
|
|
|
{
|
2016-04-18 16:03:16 +03:00
|
|
|
let data_layout = TargetDataLayout::parse(s);
|
2016-04-28 03:13:17 +03:00
|
|
|
let interners = CtxtInterners::new(arenas);
|
|
|
|
let common_types = CommonTypes::new(&interners);
|
2016-01-29 15:07:04 -05:00
|
|
|
let dep_graph = map.dep_graph.clone();
|
2016-01-20 16:56:29 -05:00
|
|
|
let fulfilled_predicates = traits::GlobalFulfilledPredicates::new(dep_graph.clone());
|
2016-05-11 04:14:41 +03:00
|
|
|
tls::enter_global(GlobalCtxt {
|
2016-05-17 12:05:02 -04:00
|
|
|
specializes_cache: RefCell::new(traits::SpecializesCache::new()),
|
2016-04-28 03:13:17 +03:00
|
|
|
global_interners: interners,
|
2016-01-05 13:07:45 -05:00
|
|
|
dep_graph: dep_graph.clone(),
|
2015-09-06 21:51:58 +03:00
|
|
|
types: common_types,
|
|
|
|
named_region_map: named_region_map,
|
|
|
|
region_maps: region_maps,
|
|
|
|
free_region_maps: RefCell::new(FnvHashMap()),
|
2015-12-22 16:39:33 -05:00
|
|
|
item_variance_map: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
2015-09-06 21:51:58 +03:00
|
|
|
variance_computed: Cell::new(false),
|
|
|
|
sess: s,
|
2016-05-31 09:21:49 +02:00
|
|
|
def_map: RefCell::new(def_map),
|
2016-07-27 17:26:55 -04:00
|
|
|
trait_map: trait_map,
|
2015-09-06 21:51:58 +03:00
|
|
|
tables: RefCell::new(Tables::empty()),
|
2015-12-22 16:39:33 -05:00
|
|
|
impl_trait_refs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
|
|
|
trait_defs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
|
|
|
adt_defs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
|
|
|
predicates: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
|
|
|
super_predicates: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
2016-01-20 16:56:29 -05:00
|
|
|
fulfilled_predicates: RefCell::new(fulfilled_predicates),
|
2015-09-06 21:51:58 +03:00
|
|
|
map: map,
|
2015-11-03 23:12:37 -06:00
|
|
|
freevars: RefCell::new(freevars),
|
2016-04-19 22:43:10 +09:00
|
|
|
maybe_unused_trait_imports: maybe_unused_trait_imports,
|
2015-12-22 16:39:33 -05:00
|
|
|
tcache: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
2015-09-06 21:51:58 +03:00
|
|
|
rcache: RefCell::new(FnvHashMap()),
|
|
|
|
tc_cache: RefCell::new(FnvHashMap()),
|
2015-12-22 16:39:33 -05:00
|
|
|
impl_or_trait_items: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
|
|
|
trait_item_def_ids: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
2016-01-06 10:16:58 -05:00
|
|
|
trait_items_cache: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
2015-09-06 21:51:58 +03:00
|
|
|
ty_param_defs: RefCell::new(NodeMap()),
|
|
|
|
normalized_cache: RefCell::new(FnvHashMap()),
|
|
|
|
lang_items: lang_items,
|
2015-12-22 16:39:33 -05:00
|
|
|
inherent_impls: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
|
|
|
impl_items: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
2015-09-06 21:51:58 +03:00
|
|
|
used_unsafe: RefCell::new(NodeSet()),
|
|
|
|
used_mut_nodes: RefCell::new(NodeSet()),
|
2016-04-19 22:43:10 +09:00
|
|
|
used_trait_imports: RefCell::new(NodeSet()),
|
2015-09-06 21:51:58 +03:00
|
|
|
populated_external_types: RefCell::new(DefIdSet()),
|
|
|
|
populated_external_primitive_impls: RefCell::new(DefIdSet()),
|
|
|
|
extern_const_statics: RefCell::new(DefIdMap()),
|
|
|
|
extern_const_fns: RefCell::new(DefIdMap()),
|
|
|
|
stability: RefCell::new(stability),
|
|
|
|
selection_cache: traits::SelectionCache::new(),
|
2015-10-21 14:50:38 +03:00
|
|
|
evaluation_cache: traits::EvaluationCache::new(),
|
2016-01-06 10:16:58 -05:00
|
|
|
repr_hint_cache: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
|
2015-09-06 21:51:58 +03:00
|
|
|
const_qualif_map: RefCell::new(NodeMap()),
|
|
|
|
custom_coerce_unsized_kinds: RefCell::new(DefIdMap()),
|
|
|
|
cast_kinds: RefCell::new(NodeMap()),
|
2016-02-14 12:30:38 -05:00
|
|
|
fragment_infos: RefCell::new(DefIdMap()),
|
|
|
|
crate_name: token::intern_and_get_ident(crate_name),
|
2016-04-18 16:03:16 +03:00
|
|
|
data_layout: data_layout,
|
2016-04-19 09:11:46 +03:00
|
|
|
layout_cache: RefCell::new(FnvHashMap()),
|
2015-09-06 21:51:58 +03:00
|
|
|
}, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
|
|
|
|
/// Call the closure with a local `TyCtxt` using the given arenas.
|
|
|
|
pub fn enter_local<F, R>(&self, arenas: &'tcx CtxtArenas<'tcx>, f: F) -> R
|
|
|
|
where F: for<'a> FnOnce(TyCtxt<'a, 'gcx, 'tcx>) -> R
|
|
|
|
{
|
|
|
|
let interners = CtxtInterners::new(arenas);
|
|
|
|
tls::enter(self, &interners, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// A trait implemented for all X<'a> types which can be safely and
|
|
|
|
/// efficiently converted to X<'tcx> as long as they are part of the
|
2016-02-29 23:36:51 +00:00
|
|
|
/// provided TyCtxt<'tcx>.
|
2015-09-06 21:51:58 +03:00
|
|
|
/// This can be done, for example, for Ty<'tcx> or &'tcx Substs<'tcx>
|
|
|
|
/// by looking them up in their respective interners.
|
2016-05-11 04:14:41 +03:00
|
|
|
///
|
|
|
|
/// However, this is still not the best implementation as it does
|
|
|
|
/// need to compare the components, even for interned values.
|
|
|
|
/// It would be more efficient if TypedArena provided a way to
|
|
|
|
/// determine whether the address is in the allocated range.
|
|
|
|
///
|
2015-09-06 21:51:58 +03:00
|
|
|
/// None is returned if the value or one of the components is not part
|
|
|
|
/// of the provided context.
|
|
|
|
/// For Ty, None can be returned if either the type interner doesn't
|
|
|
|
/// contain the TypeVariants key or if the address of the interned
|
|
|
|
/// pointer differs. The latter case is possible if a primitive type,
|
|
|
|
/// e.g. `()` or `u8`, was interned in a different context.
|
|
|
|
pub trait Lift<'tcx> {
|
|
|
|
type Lifted;
|
2016-04-29 06:00:23 +03:00
|
|
|
fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted>;
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Lift<'tcx> for Ty<'a> {
|
|
|
|
type Lifted = Ty<'tcx>;
|
2016-04-29 06:00:23 +03:00
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Ty<'tcx>> {
|
2016-05-11 04:14:41 +03:00
|
|
|
if let Some(&Interned(ty)) = tcx.interners.type_.borrow().get(&self.sty) {
|
2015-09-06 21:51:58 +03:00
|
|
|
if *self as *const _ == ty as *const _ {
|
|
|
|
return Some(ty);
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 06:00:23 +03:00
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Lift<'tcx> for &'a Substs<'a> {
|
|
|
|
type Lifted = &'tcx Substs<'tcx>;
|
2016-04-29 06:00:23 +03:00
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx Substs<'tcx>> {
|
2016-05-11 04:14:41 +03:00
|
|
|
if let Some(&Interned(substs)) = tcx.interners.substs.borrow().get(*self) {
|
2015-09-06 21:51:58 +03:00
|
|
|
if *self as *const _ == substs as *const _ {
|
|
|
|
return Some(substs);
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 06:00:23 +03:00
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:07:47 +03:00
|
|
|
impl<'a, 'tcx> Lift<'tcx> for &'a Region {
|
|
|
|
type Lifted = &'tcx Region;
|
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx Region> {
|
2016-05-11 04:14:41 +03:00
|
|
|
if let Some(&Interned(region)) = tcx.interners.region.borrow().get(*self) {
|
2016-05-02 18:07:47 +03:00
|
|
|
if *self as *const _ == region as *const _ {
|
|
|
|
return Some(region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 08:30:54 +03:00
|
|
|
impl<'a, 'tcx> Lift<'tcx> for &'a [Ty<'a>] {
|
|
|
|
type Lifted = &'tcx [Ty<'tcx>];
|
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx [Ty<'tcx>]> {
|
2016-05-11 04:14:41 +03:00
|
|
|
if let Some(&Interned(list)) = tcx.interners.type_list.borrow().get(*self) {
|
2016-04-29 08:30:54 +03:00
|
|
|
if *self as *const _ == list as *const _ {
|
|
|
|
return Some(list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'a, 'tcx> Lift<'tcx> for &'a BareFnTy<'a> {
|
|
|
|
type Lifted = &'tcx BareFnTy<'tcx>;
|
|
|
|
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
|
|
|
|
-> Option<&'tcx BareFnTy<'tcx>> {
|
|
|
|
if let Some(&Interned(fty)) = tcx.interners.bare_fn.borrow().get(*self) {
|
|
|
|
if *self as *const _ == fty as *const _ {
|
|
|
|
return Some(fty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Also try in the global tcx if we're not that.
|
|
|
|
if !tcx.is_global() {
|
|
|
|
self.lift_to_tcx(tcx.global_tcx())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
pub mod tls {
|
2016-05-11 04:14:41 +03:00
|
|
|
use super::{CtxtInterners, GlobalCtxt, TyCtxt};
|
2015-09-06 21:51:58 +03:00
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 15:52:29 -08:00
|
|
|
use std::cell::Cell;
|
2015-09-06 21:51:58 +03:00
|
|
|
use std::fmt;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos;
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
/// Marker types used for the scoped TLS slot.
|
2015-09-06 21:51:58 +03:00
|
|
|
/// The type context cannot be used directly because the scoped TLS
|
|
|
|
/// in libstd doesn't allow types generic over lifetimes.
|
2016-05-03 04:56:42 +03:00
|
|
|
enum ThreadLocalGlobalCtxt {}
|
2016-05-11 04:14:41 +03:00
|
|
|
enum ThreadLocalInterners {}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 15:52:29 -08:00
|
|
|
thread_local! {
|
2016-05-11 04:14:41 +03:00
|
|
|
static TLS_TCX: Cell<Option<(*const ThreadLocalGlobalCtxt,
|
|
|
|
*const ThreadLocalInterners)>> = Cell::new(None)
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 15:52:29 -08:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-06-21 18:08:13 -04:00
|
|
|
fn span_debug(span: syntax_pos::Span, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-09-06 21:51:58 +03:00
|
|
|
with(|tcx| {
|
|
|
|
write!(f, "{}", tcx.sess.codemap().span_to_string(span))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub fn enter_global<'gcx, F, R>(gcx: GlobalCtxt<'gcx>, f: F) -> R
|
|
|
|
where F: for<'a> FnOnce(TyCtxt<'a, 'gcx, 'gcx>) -> R
|
2016-04-28 03:13:17 +03:00
|
|
|
{
|
2016-06-21 18:08:13 -04:00
|
|
|
syntax_pos::SPAN_DEBUG.with(|span_dbg| {
|
2015-09-06 21:51:58 +03:00
|
|
|
let original_span_debug = span_dbg.get();
|
|
|
|
span_dbg.set(span_debug);
|
2016-05-11 04:14:41 +03:00
|
|
|
let result = enter(&gcx, &gcx.global_interners, f);
|
2015-09-06 21:51:58 +03:00
|
|
|
span_dbg.set(original_span_debug);
|
|
|
|
result
|
2015-09-28 15:00:15 +13:00
|
|
|
})
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub fn enter<'a, 'gcx: 'tcx, 'tcx, F, R>(gcx: &'a GlobalCtxt<'gcx>,
|
|
|
|
interners: &'a CtxtInterners<'tcx>,
|
|
|
|
f: F) -> R
|
|
|
|
where F: FnOnce(TyCtxt<'a, 'gcx, 'tcx>) -> R
|
|
|
|
{
|
|
|
|
let gcx_ptr = gcx as *const _ as *const ThreadLocalGlobalCtxt;
|
|
|
|
let interners_ptr = interners as *const _ as *const ThreadLocalInterners;
|
|
|
|
TLS_TCX.with(|tls| {
|
|
|
|
let prev = tls.get();
|
|
|
|
tls.set(Some((gcx_ptr, interners_ptr)));
|
|
|
|
let ret = f(TyCtxt {
|
|
|
|
gcx: gcx,
|
|
|
|
interners: interners
|
|
|
|
});
|
|
|
|
tls.set(prev);
|
|
|
|
ret
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn with<F, R>(f: F) -> R
|
2016-05-11 04:14:41 +03:00
|
|
|
where F: for<'a, 'gcx, 'tcx> FnOnce(TyCtxt<'a, 'gcx, 'tcx>) -> R
|
2016-05-03 05:23:22 +03:00
|
|
|
{
|
2016-05-11 04:14:41 +03:00
|
|
|
TLS_TCX.with(|tcx| {
|
|
|
|
let (gcx, interners) = tcx.get().unwrap();
|
2016-04-28 03:13:17 +03:00
|
|
|
let gcx = unsafe { &*(gcx as *const GlobalCtxt) };
|
2016-05-11 04:14:41 +03:00
|
|
|
let interners = unsafe { &*(interners as *const CtxtInterners) };
|
2016-05-03 04:56:42 +03:00
|
|
|
f(TyCtxt {
|
2016-04-28 03:13:17 +03:00
|
|
|
gcx: gcx,
|
2016-05-11 04:14:41 +03:00
|
|
|
interners: interners
|
2016-05-03 04:56:42 +03:00
|
|
|
})
|
std: Stabilize APIs for the 1.8 release
This commit is the result of the FCPs ending for the 1.8 release cycle for both
the libs and the lang suteams. The full list of changes are:
Stabilized
* `braced_empty_structs`
* `augmented_assignments`
* `str::encode_utf16` - renamed from `utf16_units`
* `str::EncodeUtf16` - renamed from `Utf16Units`
* `Ref::map`
* `RefMut::map`
* `ptr::drop_in_place`
* `time::Instant`
* `time::SystemTime`
* `{Instant,SystemTime}::now`
* `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier`
* `{Instant,SystemTime}::elapsed`
* Various `Add`/`Sub` impls for `Time` and `SystemTime`
* `SystemTimeError`
* `SystemTimeError::duration`
* Various impls for `SystemTimeError`
* `UNIX_EPOCH`
* `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign`
Deprecated
* Scoped TLS (the `scoped_thread_local!` macro)
* `Ref::filter_map`
* `RefMut::filter_map`
* `RwLockReadGuard::map`
* `RwLockWriteGuard::map`
* `Condvar::wait_timeout_with`
Closes #27714
Closes #27715
Closes #27746
Closes #27748
Closes #27908
Closes #29866
2016-02-25 15:52:29 -08:00
|
|
|
})
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn with_opt<F, R>(f: F) -> R
|
2016-05-11 04:14:41 +03:00
|
|
|
where F: for<'a, 'gcx, 'tcx> FnOnce(Option<TyCtxt<'a, 'gcx, 'tcx>>) -> R
|
2016-05-03 05:23:22 +03:00
|
|
|
{
|
2016-05-11 04:14:41 +03:00
|
|
|
if TLS_TCX.with(|tcx| tcx.get().is_some()) {
|
2015-09-06 21:51:58 +03:00
|
|
|
with(|v| f(Some(v)))
|
|
|
|
} else {
|
|
|
|
f(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! sty_debug_print {
|
|
|
|
($ctxt: expr, $($variant: ident),*) => {{
|
|
|
|
// curious inner module to allow variant names to be used as
|
|
|
|
// variable names.
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
mod inner {
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::{self, TyCtxt};
|
2016-05-11 04:14:41 +03:00
|
|
|
use ty::context::Interned;
|
2016-03-23 04:56:49 +02:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct DebugStat {
|
|
|
|
total: usize,
|
|
|
|
region_infer: usize,
|
|
|
|
ty_infer: usize,
|
|
|
|
both_infer: usize,
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn go(tcx: TyCtxt) {
|
2015-09-06 21:51:58 +03:00
|
|
|
let mut total = DebugStat {
|
|
|
|
total: 0,
|
|
|
|
region_infer: 0, ty_infer: 0, both_infer: 0,
|
|
|
|
};
|
|
|
|
$(let mut $variant = total;)*
|
|
|
|
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
for &Interned(t) in tcx.interners.type_.borrow().iter() {
|
2015-09-06 21:51:58 +03:00
|
|
|
let variant = match t.sty {
|
|
|
|
ty::TyBool | ty::TyChar | ty::TyInt(..) | ty::TyUint(..) |
|
|
|
|
ty::TyFloat(..) | ty::TyStr => continue,
|
|
|
|
ty::TyError => /* unimportant */ continue,
|
|
|
|
$(ty::$variant(..) => &mut $variant,)*
|
|
|
|
};
|
|
|
|
let region = t.flags.get().intersects(ty::TypeFlags::HAS_RE_INFER);
|
|
|
|
let ty = t.flags.get().intersects(ty::TypeFlags::HAS_TY_INFER);
|
|
|
|
|
|
|
|
variant.total += 1;
|
|
|
|
total.total += 1;
|
|
|
|
if region { total.region_infer += 1; variant.region_infer += 1 }
|
|
|
|
if ty { total.ty_infer += 1; variant.ty_infer += 1 }
|
|
|
|
if region && ty { total.both_infer += 1; variant.both_infer += 1 }
|
|
|
|
}
|
|
|
|
println!("Ty interner total ty region both");
|
|
|
|
$(println!(" {:18}: {uses:6} {usespc:4.1}%, \
|
|
|
|
{ty:4.1}% {region:5.1}% {both:4.1}%",
|
|
|
|
stringify!($variant),
|
|
|
|
uses = $variant.total,
|
|
|
|
usespc = $variant.total as f64 * 100.0 / total.total as f64,
|
|
|
|
ty = $variant.ty_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
region = $variant.region_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
both = $variant.both_infer as f64 * 100.0 / total.total as f64);
|
|
|
|
)*
|
|
|
|
println!(" total {uses:6} \
|
|
|
|
{ty:4.1}% {region:5.1}% {both:4.1}%",
|
|
|
|
uses = total.total,
|
|
|
|
ty = total.ty_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
region = total.region_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
both = total.both_infer as f64 * 100.0 / total.total as f64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inner::go($ctxt)
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn print_debug_stats(self) {
|
2015-09-06 21:51:58 +03:00
|
|
|
sty_debug_print!(
|
|
|
|
self,
|
2015-06-13 13:15:03 -07:00
|
|
|
TyEnum, TyBox, TyArray, TySlice, TyRawPtr, TyRef, TyFnDef, TyFnPtr,
|
|
|
|
TyTrait, TyStruct, TyClosure, TyTuple, TyParam, TyInfer, TyProjection);
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-04-28 03:13:17 +03:00
|
|
|
println!("Substs interner: #{}", self.interners.substs.borrow().len());
|
|
|
|
println!("BareFnTy interner: #{}", self.interners.bare_fn.borrow().len());
|
|
|
|
println!("Region interner: #{}", self.interners.region.borrow().len());
|
|
|
|
println!("Stability interner: #{}", self.interners.stability.borrow().len());
|
|
|
|
println!("Layout interner: #{}", self.interners.layout.borrow().len());
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
/// An entry in an interner.
|
|
|
|
struct Interned<'tcx, T: 'tcx+?Sized>(&'tcx T);
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
// NB: An Interned<Ty> compares and hashes as a sty.
|
|
|
|
impl<'tcx> PartialEq for Interned<'tcx, TyS<'tcx>> {
|
|
|
|
fn eq(&self, other: &Interned<'tcx, TyS<'tcx>>) -> bool {
|
|
|
|
self.0.sty == other.0.sty
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'tcx> Eq for Interned<'tcx, TyS<'tcx>> {}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'tcx> Hash for Interned<'tcx, TyS<'tcx>> {
|
2015-09-06 21:51:58 +03:00
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
2016-05-11 04:14:41 +03:00
|
|
|
self.0.sty.hash(s)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'tcx: 'lcx, 'lcx> Borrow<TypeVariants<'lcx>> for Interned<'tcx, TyS<'tcx>> {
|
2016-03-23 04:56:49 +02:00
|
|
|
fn borrow<'a>(&'a self) -> &'a TypeVariants<'lcx> {
|
2016-05-11 04:14:41 +03:00
|
|
|
&self.0.sty
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'tcx: 'lcx, 'lcx> Borrow<[Ty<'lcx>]> for Interned<'tcx, [Ty<'tcx>]> {
|
2016-04-29 08:30:54 +03:00
|
|
|
fn borrow<'a>(&'a self) -> &'a [Ty<'lcx>] {
|
2016-05-11 04:14:41 +03:00
|
|
|
self.0
|
2016-04-29 08:30:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'tcx: 'lcx, 'lcx> Borrow<Substs<'lcx>> for Interned<'tcx, Substs<'tcx>> {
|
2016-03-23 04:56:49 +02:00
|
|
|
fn borrow<'a>(&'a self) -> &'a Substs<'lcx> {
|
2016-05-11 04:14:41 +03:00
|
|
|
self.0
|
2016-03-23 04:56:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'tcx: 'lcx, 'lcx> Borrow<BareFnTy<'lcx>> for Interned<'tcx, BareFnTy<'tcx>> {
|
|
|
|
fn borrow<'a>(&'a self) -> &'a BareFnTy<'lcx> {
|
|
|
|
self.0
|
|
|
|
}
|
2016-05-02 18:07:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'tcx> Borrow<Region> for Interned<'tcx, Region> {
|
2016-05-02 18:07:47 +03:00
|
|
|
fn borrow<'a>(&'a self) -> &'a Region {
|
2016-05-11 04:14:41 +03:00
|
|
|
self.0
|
2016-05-02 18:07:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
macro_rules! items { ($($item:item)+) => ($($item)+) }
|
|
|
|
macro_rules! impl_interners {
|
|
|
|
($lt_tcx:tt, $($name:ident: $method:ident($alloc:ty, $needs_infer:expr)-> $ty:ty),+) => {
|
|
|
|
items!($(impl<$lt_tcx> PartialEq for Interned<$lt_tcx, $ty> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.0 == other.0
|
|
|
|
}
|
2016-04-29 08:30:54 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<$lt_tcx> Eq for Interned<$lt_tcx, $ty> {}
|
2016-04-29 08:30:54 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<$lt_tcx> Hash for Interned<$lt_tcx, $ty> {
|
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
|
|
|
self.0.hash(s)
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'a, 'gcx, $lt_tcx> TyCtxt<'a, 'gcx, $lt_tcx> {
|
|
|
|
pub fn $method(self, v: $alloc) -> &$lt_tcx $ty {
|
|
|
|
if let Some(i) = self.interners.$name.borrow().get::<$ty>(&v) {
|
|
|
|
return i.0;
|
|
|
|
}
|
|
|
|
if !self.is_global() {
|
|
|
|
if let Some(i) = self.global_interners.$name.borrow().get::<$ty>(&v) {
|
|
|
|
return i.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK(eddyb) Depend on flags being accurate to
|
|
|
|
// determine that all contents are in the global tcx.
|
|
|
|
// See comments on Lift for why we can't use that.
|
|
|
|
if !($needs_infer)(&v) {
|
|
|
|
if !self.is_global() {
|
|
|
|
let v = unsafe {
|
|
|
|
mem::transmute(v)
|
|
|
|
};
|
|
|
|
let i = self.global_interners.arenas.$name.alloc(v);
|
|
|
|
self.global_interners.$name.borrow_mut().insert(Interned(i));
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make sure we don't end up with inference
|
|
|
|
// types/regions in the global tcx.
|
|
|
|
if self.is_global() {
|
|
|
|
bug!("Attempted to intern `{:?}` which contains \
|
|
|
|
inference types/regions in the global type context",
|
|
|
|
v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let i = self.interners.arenas.$name.alloc(v);
|
|
|
|
self.interners.$name.borrow_mut().insert(Interned(i));
|
|
|
|
i
|
|
|
|
}
|
|
|
|
})+);
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
|
|
|
|
x.has_type_flags(ty::TypeFlags::KEEP_IN_LOCAL_TCX)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_interners!('tcx,
|
|
|
|
type_list: mk_type_list(Vec<Ty<'tcx>>, keep_local) -> [Ty<'tcx>],
|
|
|
|
substs: mk_substs(Substs<'tcx>, |substs: &Substs| {
|
|
|
|
keep_local(&substs.types) || keep_local(&substs.regions)
|
|
|
|
}) -> Substs<'tcx>,
|
|
|
|
bare_fn: mk_bare_fn(BareFnTy<'tcx>, |fty: &BareFnTy| {
|
|
|
|
keep_local(&fty.sig)
|
|
|
|
}) -> BareFnTy<'tcx>,
|
|
|
|
region: mk_region(Region, keep_local) -> Region
|
|
|
|
);
|
|
|
|
|
|
|
|
fn bound_list_is_sorted(bounds: &[ty::PolyProjectionPredicate]) -> bool {
|
|
|
|
bounds.is_empty() ||
|
|
|
|
bounds[1..].iter().enumerate().all(
|
|
|
|
|(index, bound)| bounds[index].sort_key() <= bound.sort_key())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Create an unsafe fn ty based on a safe fn ty.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn safe_to_unsafe_fn_ty(self, bare_fn: &BareFnTy<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
assert_eq!(bare_fn.unsafety, hir::Unsafety::Normal);
|
2016-04-29 08:30:54 +03:00
|
|
|
self.mk_fn_ptr(self.mk_bare_fn(ty::BareFnTy {
|
2015-09-06 21:51:58 +03:00
|
|
|
unsafety: hir::Unsafety::Unsafe,
|
|
|
|
abi: bare_fn.abi,
|
|
|
|
sig: bare_fn.sig.clone()
|
2016-04-29 08:30:54 +03:00
|
|
|
}))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 03:13:17 +03:00
|
|
|
// Interns a type/name combination, stores the resulting box in cx.interners,
|
2015-09-06 21:51:58 +03:00
|
|
|
// and returns the box as cast to an unsafe ptr (see comments for Ty above).
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_ty(self, st: TypeVariants<'tcx>) -> Ty<'tcx> {
|
2016-05-11 04:14:41 +03:00
|
|
|
let global_interners = if !self.is_global() {
|
|
|
|
Some(&self.global_interners)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
self.interners.intern_ty(st, global_interners)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_mach_int(self, tm: ast::IntTy) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
match tm {
|
2016-02-08 16:20:57 +01:00
|
|
|
ast::IntTy::Is => self.types.isize,
|
|
|
|
ast::IntTy::I8 => self.types.i8,
|
|
|
|
ast::IntTy::I16 => self.types.i16,
|
|
|
|
ast::IntTy::I32 => self.types.i32,
|
|
|
|
ast::IntTy::I64 => self.types.i64,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_mach_uint(self, tm: ast::UintTy) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
match tm {
|
2016-02-08 16:20:57 +01:00
|
|
|
ast::UintTy::Us => self.types.usize,
|
|
|
|
ast::UintTy::U8 => self.types.u8,
|
|
|
|
ast::UintTy::U16 => self.types.u16,
|
|
|
|
ast::UintTy::U32 => self.types.u32,
|
|
|
|
ast::UintTy::U64 => self.types.u64,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_mach_float(self, tm: ast::FloatTy) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
match tm {
|
2016-02-08 16:09:01 +01:00
|
|
|
ast::FloatTy::F32 => self.types.f32,
|
|
|
|
ast::FloatTy::F64 => self.types.f64,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_str(self) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyStr)
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_static_str(self) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_imm_ref(self.mk_region(ty::ReStatic), self.mk_str())
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_enum(self, def: AdtDef<'tcx>, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
// take a copy of substs so that we own the vectors inside
|
|
|
|
self.mk_ty(TyEnum(def, substs))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_box(self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyBox(ty))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_ptr(self, tm: TypeAndMut<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyRawPtr(tm))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_ref(self, r: &'tcx Region, tm: TypeAndMut<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyRef(r, tm))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_mut_ref(self, r: &'tcx Region, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ref(r, TypeAndMut {ty: ty, mutbl: hir::MutMutable})
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_imm_ref(self, r: &'tcx Region, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ref(r, TypeAndMut {ty: ty, mutbl: hir::MutImmutable})
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_mut_ptr(self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ptr(TypeAndMut {ty: ty, mutbl: hir::MutMutable})
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_imm_ptr(self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ptr(TypeAndMut {ty: ty, mutbl: hir::MutImmutable})
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_nil_ptr(self) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_imm_ptr(self.mk_nil())
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_array(self, ty: Ty<'tcx>, n: usize) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyArray(ty, n))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_slice(self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TySlice(ty))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_tup(self, ts: Vec<Ty<'tcx>>) -> Ty<'tcx> {
|
2016-04-29 08:30:54 +03:00
|
|
|
self.mk_ty(TyTuple(self.mk_type_list(ts)))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_nil(self) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_tup(Vec::new())
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_bool(self) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyBool)
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_fn_def(self, def_id: DefId,
|
2016-02-16 18:36:41 +02:00
|
|
|
substs: &'tcx Substs<'tcx>,
|
2016-04-29 08:30:54 +03:00
|
|
|
fty: &'tcx BareFnTy<'tcx>) -> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyFnDef(def_id, substs, fty))
|
2015-06-13 13:15:03 -07:00
|
|
|
}
|
|
|
|
|
2016-04-29 08:30:54 +03:00
|
|
|
pub fn mk_fn_ptr(self, fty: &'tcx BareFnTy<'tcx>) -> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyFnPtr(fty))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_trait(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
principal: ty::PolyTraitRef<'tcx>,
|
|
|
|
bounds: ExistentialBounds<'tcx>)
|
|
|
|
-> Ty<'tcx>
|
|
|
|
{
|
|
|
|
assert!(bound_list_is_sorted(&bounds.projection_bounds));
|
|
|
|
|
|
|
|
let inner = box TraitTy {
|
|
|
|
principal: principal,
|
|
|
|
bounds: bounds
|
|
|
|
};
|
|
|
|
self.mk_ty(TyTrait(inner))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_projection(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
trait_ref: TraitRef<'tcx>,
|
|
|
|
item_name: Name)
|
|
|
|
-> Ty<'tcx> {
|
|
|
|
// take a copy of substs so that we own the vectors inside
|
|
|
|
let inner = ProjectionTy { trait_ref: trait_ref, item_name: item_name };
|
|
|
|
self.mk_ty(TyProjection(inner))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_struct(self, def: AdtDef<'tcx>, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
// take a copy of substs so that we own the vectors inside
|
|
|
|
self.mk_ty(TyStruct(def, substs))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_closure(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
closure_id: DefId,
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
|
|
|
tys: Vec<Ty<'tcx>>)
|
|
|
|
-> Ty<'tcx> {
|
2016-04-29 08:30:54 +03:00
|
|
|
self.mk_closure_from_closure_substs(closure_id, ClosureSubsts {
|
2015-09-06 21:51:58 +03:00
|
|
|
func_substs: substs,
|
2016-04-29 08:30:54 +03:00
|
|
|
upvar_tys: self.mk_type_list(tys)
|
|
|
|
})
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_closure_from_closure_substs(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
closure_id: DefId,
|
2016-04-29 08:30:54 +03:00
|
|
|
closure_substs: ClosureSubsts<'tcx>)
|
2015-09-06 21:51:58 +03:00
|
|
|
-> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyClosure(closure_id, closure_substs))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_var(self, v: TyVid) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_infer(TyVar(v))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_int_var(self, v: IntVid) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_infer(IntVar(v))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_float_var(self, v: FloatVid) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_infer(FloatVar(v))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_infer(self, it: InferTy) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_ty(TyInfer(it))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_param(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
space: subst::ParamSpace,
|
|
|
|
index: u32,
|
|
|
|
name: Name) -> Ty<'tcx> {
|
|
|
|
self.mk_ty(TyParam(ParamTy { space: space, idx: index, name: name }))
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_self_type(self) -> Ty<'tcx> {
|
2016-04-18 22:53:50 +03:00
|
|
|
self.mk_param(subst::SelfSpace, 0, keywords::SelfType.name())
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn mk_param_from_def(self, def: &ty::TypeParameterDef) -> Ty<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.mk_param(def.space, def.index, def.name)
|
|
|
|
}
|
2015-12-22 16:39:33 -05:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn trait_items(self, trait_did: DefId) -> Rc<Vec<ty::ImplOrTraitItem<'gcx>>> {
|
2016-01-06 10:16:58 -05:00
|
|
|
self.trait_items_cache.memoize(trait_did, || {
|
|
|
|
let def_ids = self.trait_item_def_ids(trait_did);
|
|
|
|
Rc::new(def_ids.iter()
|
|
|
|
.map(|d| self.impl_or_trait_item(d.def_id()))
|
|
|
|
.collect())
|
|
|
|
})
|
2015-12-22 16:39:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Obtain the representation annotation for a struct definition.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn lookup_repr_hints(self, did: DefId) -> Rc<Vec<attr::ReprAttr>> {
|
2015-12-22 16:39:33 -05:00
|
|
|
self.repr_hint_cache.memoize(did, || {
|
|
|
|
Rc::new(if did.is_local() {
|
|
|
|
self.get_attrs(did).iter().flat_map(|meta| {
|
|
|
|
attr::find_repr_attrs(self.sess.diagnostic(), meta).into_iter()
|
|
|
|
}).collect()
|
|
|
|
} else {
|
|
|
|
self.sess.cstore.repr_attrs(did)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|