rustc: use a TypedArena to allocate types in the type context.

This commit is contained in:
Eduard Burtescu 2014-04-22 14:33:15 +03:00
parent ab7b1c896d
commit 22f8b8462e
3 changed files with 37 additions and 25 deletions

View file

@ -32,6 +32,7 @@ use serialize::{json, Encodable};
use std::io;
use std::io::fs;
use arena::TypedArena;
use syntax::ast;
use syntax::attr;
use syntax::attr::{AttrMetaMethods};
@ -86,8 +87,9 @@ pub fn compile_input(sess: Session,
if stop_after_phase_2(&sess) { return; }
let type_arena = TypedArena::new();
let analysis = phase_3_run_analysis_passes(sess, &expanded_crate,
ast_map, id);
ast_map, &type_arena, id);
phase_save_analysis(&analysis.ty_cx.sess, &expanded_crate, &analysis, outdir);
if stop_after_phase_3(&analysis.ty_cx.sess) { return; }
let (tcx, trans) = phase_4_translate_to_llvm(expanded_crate, analysis);
@ -299,11 +301,11 @@ pub fn phase_2_configure_and_expand(sess: &Session,
Some((krate, map))
}
pub struct CrateAnalysis {
pub struct CrateAnalysis<'tcx> {
pub exp_map2: middle::resolve::ExportMap2,
pub exported_items: middle::privacy::ExportedItems,
pub public_items: middle::privacy::PublicItems,
pub ty_cx: ty::ctxt,
pub ty_cx: ty::ctxt<'tcx>,
pub reachable: NodeSet,
pub name: String,
}
@ -312,10 +314,11 @@ pub struct CrateAnalysis {
/// Run the resolution, typechecking, region checking and other
/// miscellaneous analysis passes on the crate. Return various
/// structures carrying the results of the analysis.
pub fn phase_3_run_analysis_passes(sess: Session,
krate: &ast::Crate,
ast_map: syntax::ast_map::Map,
name: String) -> CrateAnalysis {
pub fn phase_3_run_analysis_passes<'tcx>(sess: Session,
krate: &ast::Crate,
ast_map: syntax::ast_map::Map,
type_arena: &'tcx TypedArena<ty::t_box_>,
name: String) -> CrateAnalysis<'tcx> {
let time_passes = sess.time_passes();
time(time_passes, "external crate/lib resolution", (), |_|
@ -362,6 +365,7 @@ pub fn phase_3_run_analysis_passes(sess: Session,
stability::Index::build(krate));
let ty_cx = ty::mk_ctxt(sess,
type_arena,
def_map,
named_region_map,
ast_map,

View file

@ -33,7 +33,7 @@ use graphviz as dot;
use std::io::{mod, MemReader};
use std::from_str::FromStr;
use std::option;
use arena::TypedArena;
#[deriving(PartialEq, Show)]
pub enum PpSourceMode {
@ -114,7 +114,9 @@ impl PpSourceMode {
}
PpmTyped => {
let ast_map = ast_map.expect("--pretty=typed missing ast_map");
let analysis = driver::phase_3_run_analysis_passes(sess, krate, ast_map, id);
let type_arena = TypedArena::new();
let analysis = driver::phase_3_run_analysis_passes(sess, krate, ast_map,
&type_arena, id);
let annotation = TypedAnnotation { analysis: analysis };
f(&annotation, payload)
}
@ -531,8 +533,9 @@ pub fn pretty_print_input(sess: Session,
match code {
Some(code) => {
let variants = gather_flowgraph_variants(&sess);
let type_arena = TypedArena::new();
let analysis = driver::phase_3_run_analysis_passes(sess, &krate,
ast_map, id);
ast_map, &type_arena, id);
print_flowgraph(variants, analysis, code, out)
}
None => {

View file

@ -50,6 +50,7 @@ use std::mem;
use std::ops;
use std::rc::Rc;
use std::collections::{HashMap, HashSet};
use arena::TypedArena;
use syntax::abi;
use syntax::ast::{CrateNum, DefId, FnStyle, Ident, ItemTrait, LOCAL_CRATE};
use syntax::ast::{MutImmutable, MutMutable, Name, NamedField, NodeId};
@ -418,10 +419,13 @@ pub struct TransmuteRestriction {
/// 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.
pub struct ctxt {
pub struct ctxt<'tcx> {
/// The arena that types are allocated from.
type_arena: &'tcx TypedArena<t_box_>,
/// Specifically use a speedy hash algorithm for this hash map, it's used
/// quite often.
pub interner: RefCell<FnvHashMap<intern_key, Box<t_box_>>>,
interner: RefCell<FnvHashMap<intern_key, &'tcx t_box_>>,
pub next_id: Cell<uint>,
pub sess: Session,
pub def_map: resolve::DefMap,
@ -1373,21 +1377,22 @@ impl UnboxedClosureKind {
}
}
pub fn mk_ctxt(s: Session,
dm: resolve::DefMap,
named_region_map: resolve_lifetime::NamedRegionMap,
map: ast_map::Map,
freevars: freevars::freevar_map,
capture_modes: freevars::CaptureModeMap,
region_maps: middle::region::RegionMaps,
lang_items: middle::lang_items::LanguageItems,
stability: stability::Index)
-> ctxt {
pub fn mk_ctxt<'tcx>(s: Session,
type_arena: &'tcx TypedArena<t_box_>,
dm: resolve::DefMap,
named_region_map: resolve_lifetime::NamedRegionMap,
map: ast_map::Map,
freevars: freevars::freevar_map,
capture_modes: freevars::CaptureModeMap,
region_maps: middle::region::RegionMaps,
lang_items: middle::lang_items::LanguageItems,
stability: stability::Index) -> ctxt<'tcx> {
ctxt {
type_arena: type_arena,
interner: RefCell::new(FnvHashMap::new()),
named_region_map: named_region_map,
item_variance_map: RefCell::new(DefIdMap::new()),
variance_computed: Cell::new(false),
interner: RefCell::new(FnvHashMap::new()),
next_id: Cell::new(primitives::LAST_PRIMITIVE_ID),
sess: s,
def_map: dm,
@ -1554,11 +1559,11 @@ pub fn mk_t(cx: &ctxt, st: sty) -> t {
}
}
let t = box t_box_ {
let t = cx.type_arena.alloc(t_box_ {
sty: st,
id: cx.next_id.get(),
flags: flags,
};
});
let sty_ptr = &t.sty as *const sty;