Skip to content

Commit

Permalink
Introduce VecPerParamSpace and use it to represent sets of types and
Browse files Browse the repository at this point in the history
parameters

This involves numerous substeps:

1. Treat Self same as any other parameter.
2. No longer compute offsets for method parameters.
3. Store all generic types (both trait/impl and method) with a method,
   eliminating odd discrepancies.
4. Stop doing unspeakable things to static methods and instead just use
   the natural types, now that we can easily add the type parameters from
   trait into the method's polytype.
5. No doubt some more. It was hard to separate these into distinct commits.

Fixes rust-lang#13564
  • Loading branch information
nikomatsakis committed Jun 7, 2014
1 parent bd6683c commit 1851550
Show file tree
Hide file tree
Showing 48 changed files with 2,557 additions and 2,215 deletions.
10 changes: 7 additions & 3 deletions src/librustc/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ pub static tag_impls_impl: uint = 0x81;
pub static tag_items_data_item_inherent_impl: uint = 0x82;
pub static tag_items_data_item_extension_impl: uint = 0x83;

pub static tag_region_param_def: uint = 0x84;
pub static tag_region_param_def_ident: uint = 0x85;
pub static tag_region_param_def_def_id: uint = 0x86;
// GAP 0x84, 0x85, 0x86

pub static tag_native_libraries: uint = 0x87;
pub static tag_native_libraries_lib: uint = 0x88;
Expand All @@ -214,3 +212,9 @@ pub struct LinkMeta {
pub crateid: CrateId,
pub crate_hash: Svh,
}

pub static tag_region_param_def: uint = 0x90;
pub static tag_region_param_def_ident: uint = 0x91;
pub static tag_region_param_def_def_id: uint = 0x92;
pub static tag_region_param_def_space: uint = 0x93;
pub static tag_region_param_def_index: uint = 0x94;
7 changes: 4 additions & 3 deletions src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ pub fn get_field_type(tcx: &ty::ctxt, class_id: ast::DefId,
});
let ty = decoder::item_type(def, the_field, tcx, &*cdata);
ty::ty_param_bounds_and_ty {
generics: ty::Generics {type_param_defs: Rc::new(Vec::new()),
region_param_defs: Rc::new(Vec::new())},
generics: ty::Generics {types: VecPerParamSpace::empty(),
regions: VecPerParamSpace::empty()},
ty: ty
}
}
Expand All @@ -240,7 +240,8 @@ pub fn get_impl_trait(tcx: &ty::ctxt,

// Given a def_id for an impl, return information about its vtables
pub fn get_impl_vtables(tcx: &ty::ctxt,
def: ast::DefId) -> typeck::impl_res {
def: ast::DefId)
-> typeck::vtable_res {
let cstore = &tcx.sess.cstore;
let cdata = cstore.get_crate_data(def.krate);
decoder::get_impl_vtables(&*cdata, def.node, tcx)
Expand Down
68 changes: 38 additions & 30 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use metadata::tydecode::{parse_ty_data, parse_def_id,
parse_bare_fn_ty_data, parse_trait_ref_data};
use middle::lang_items;
use middle::def;
use middle::subst;
use middle::ty::{ImplContainer, TraitContainer};
use middle::ty;
use middle::typeck;
Expand Down Expand Up @@ -257,34 +258,44 @@ fn item_ty_param_defs(item: ebml::Doc,
tcx: &ty::ctxt,
cdata: Cmd,
tag: uint)
-> Rc<Vec<ty::TypeParameterDef> > {
let mut bounds = Vec::new();
-> subst::VecPerParamSpace<ty::TypeParameterDef> {
let mut bounds = subst::VecPerParamSpace::empty();
reader::tagged_docs(item, tag, |p| {
let bd = parse_type_param_def_data(
p.data, p.start, cdata.cnum, tcx,
|_, did| translate_def_id(cdata, did));
bounds.push(bd);
bounds.push(bd.space, bd);
true
});
Rc::new(bounds)
bounds
}

fn item_region_param_defs(item_doc: ebml::Doc, cdata: Cmd)
-> Rc<Vec<ty::RegionParameterDef> > {
let mut v = Vec::new();
-> subst::VecPerParamSpace<ty::RegionParameterDef>
{
let mut v = subst::VecPerParamSpace::empty();
reader::tagged_docs(item_doc, tag_region_param_def, |rp_doc| {
let ident_str_doc = reader::get_doc(rp_doc,
tag_region_param_def_ident);
let ident = item_name(&*token::get_ident_interner(), ident_str_doc);
let def_id_doc = reader::get_doc(rp_doc,
tag_region_param_def_def_id);
let def_id = reader::with_doc_data(def_id_doc, parse_def_id);
let def_id = translate_def_id(cdata, def_id);
v.push(ty::RegionParameterDef { name: ident.name,
def_id: def_id });
true
});
Rc::new(v)
let ident_str_doc = reader::get_doc(rp_doc,
tag_region_param_def_ident);
let ident = item_name(&*token::get_ident_interner(), ident_str_doc);
let def_id_doc = reader::get_doc(rp_doc,
tag_region_param_def_def_id);
let def_id = reader::with_doc_data(def_id_doc, parse_def_id);
let def_id = translate_def_id(cdata, def_id);

let doc = reader::get_doc(rp_doc, tag_region_param_def_space);
let space = subst::ParamSpace::from_uint(reader::doc_as_u64(doc) as uint);

let doc = reader::get_doc(rp_doc, tag_region_param_def_index);
let index = reader::doc_as_u64(doc) as uint;

v.push(space, ty::RegionParameterDef { name: ident.name,
def_id: def_id,
space: space,
index: index });
true
});
v
}

fn enum_variant_ids(item: ebml::Doc, cdata: Cmd) -> Vec<ast::DefId> {
Expand Down Expand Up @@ -403,8 +414,8 @@ pub fn get_trait_def(cdata: Cmd,
}

ty::TraitDef {
generics: ty::Generics {type_param_defs: tp_defs,
region_param_defs: rp_defs},
generics: ty::Generics {types: tp_defs,
regions: rp_defs},
bounds: bounds,
trait_ref: Rc::new(item_trait_ref(item_doc, tcx, cdata))
}
Expand All @@ -422,8 +433,8 @@ pub fn get_type(cdata: Cmd, id: ast::NodeId, tcx: &ty::ctxt)
let rp_defs = item_region_param_defs(item, cdata);

ty::ty_param_bounds_and_ty {
generics: ty::Generics {type_param_defs: tp_defs,
region_param_defs: rp_defs},
generics: ty::Generics {types: tp_defs,
regions: rp_defs},
ty: t
}
}
Expand All @@ -440,16 +451,13 @@ pub fn get_impl_trait(cdata: Cmd,

pub fn get_impl_vtables(cdata: Cmd,
id: ast::NodeId,
tcx: &ty::ctxt) -> typeck::impl_res
tcx: &ty::ctxt)
-> typeck::vtable_res
{
let item_doc = lookup_item(id, cdata.data());
let vtables_doc = reader::get_doc(item_doc, tag_item_impl_vtables);
let mut decoder = reader::Decoder::new(vtables_doc);

typeck::impl_res {
trait_vtables: decoder.read_vtable_res(tcx, cdata),
self_vtables: decoder.read_vtable_param_res(tcx, cdata)
}
decoder.read_vtable_res(tcx, cdata)
}


Expand Down Expand Up @@ -802,8 +810,8 @@ pub fn get_method(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::NodeId,
ty::Method::new(
name,
ty::Generics {
type_param_defs: type_param_defs,
region_param_defs: rp_defs,
types: type_param_defs,
regions: rp_defs,
},
fty,
explicit_self,
Expand Down
33 changes: 18 additions & 15 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use metadata::common::*;
use metadata::cstore;
use metadata::decoder;
use metadata::tyencode;
use middle::subst::VecPerParamSpace;
use middle::ty::{node_id_to_type, lookup_item_type};
use middle::astencode;
use middle::ty;
Expand Down Expand Up @@ -125,10 +126,9 @@ fn encode_trait_ref(ebml_w: &mut Encoder,

fn encode_impl_vtables(ebml_w: &mut Encoder,
ecx: &EncodeContext,
vtables: &typeck::impl_res) {
vtables: &typeck::vtable_res) {
ebml_w.start_tag(tag_item_impl_vtables);
astencode::encode_vtable_res(ecx, ebml_w, &vtables.trait_vtables);
astencode::encode_vtable_param_res(ecx, ebml_w, &vtables.self_vtables);
astencode::encode_vtable_res(ecx, ebml_w, vtables);
ebml_w.end_tag();
}

Expand All @@ -145,7 +145,7 @@ pub fn def_to_str(did: DefId) -> String {

fn encode_ty_type_param_defs(ebml_w: &mut Encoder,
ecx: &EncodeContext,
params: &[ty::TypeParameterDef],
params: &VecPerParamSpace<ty::TypeParameterDef>,
tag: uint) {
let ty_str_ctxt = &tyencode::ctxt {
diag: ecx.diag,
Expand All @@ -161,7 +161,7 @@ fn encode_ty_type_param_defs(ebml_w: &mut Encoder,
}

fn encode_region_param_defs(ebml_w: &mut Encoder,
params: &[ty::RegionParameterDef]) {
params: &VecPerParamSpace<ty::RegionParameterDef>) {
for param in params.iter() {
ebml_w.start_tag(tag_region_param_def);

Expand All @@ -172,6 +172,12 @@ fn encode_region_param_defs(ebml_w: &mut Encoder,
ebml_w.wr_tagged_str(tag_region_param_def_def_id,
def_to_str(param.def_id).as_slice());

ebml_w.wr_tagged_u64(tag_region_param_def_space,
param.space.to_uint() as u64);

ebml_w.wr_tagged_u64(tag_region_param_def_index,
param.index as u64);

ebml_w.end_tag();
}
}
Expand All @@ -188,9 +194,9 @@ fn encode_item_variances(ebml_w: &mut Encoder,
fn encode_bounds_and_type(ebml_w: &mut Encoder,
ecx: &EncodeContext,
tpt: &ty::ty_param_bounds_and_ty) {
encode_ty_type_param_defs(ebml_w, ecx, tpt.generics.type_param_defs(),
encode_ty_type_param_defs(ebml_w, ecx, &tpt.generics.types,
tag_items_data_item_ty_param_bounds);
encode_region_param_defs(ebml_w, tpt.generics.region_param_defs());
encode_region_param_defs(ebml_w, &tpt.generics.regions);
encode_type(ecx, ebml_w, tpt.ty);
}

Expand Down Expand Up @@ -722,8 +728,7 @@ fn encode_method_ty_fields(ecx: &EncodeContext,
method_ty: &ty::Method) {
encode_def_id(ebml_w, method_ty.def_id);
encode_name(ebml_w, method_ty.ident.name);
encode_ty_type_param_defs(ebml_w, ecx,
method_ty.generics.type_param_defs(),
encode_ty_type_param_defs(ebml_w, ecx, &method_ty.generics.types,
tag_item_method_tps);
encode_method_fty(ecx, ebml_w, &method_ty.fty);
encode_visibility(ebml_w, method_ty.vis);
Expand Down Expand Up @@ -767,10 +772,8 @@ fn encode_info_for_method(ecx: &EncodeContext,
}

for &ast_method in ast_method_opt.iter() {
let num_params = tpt.generics.type_param_defs().len();
if num_params > 0u ||
is_default_impl ||
should_inline(ast_method.attrs.as_slice()) {
let any_types = !tpt.generics.types.is_empty();
if any_types || is_default_impl || should_inline(ast_method.attrs.as_slice()) {
encode_inlined_item(ecx, ebml_w,
IIMethodRef(local_def(parent_id), false, ast_method));
} else {
Expand Down Expand Up @@ -1121,9 +1124,9 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_item_variances(ebml_w, ecx, item.id);
let trait_def = ty::lookup_trait_def(tcx, def_id);
encode_ty_type_param_defs(ebml_w, ecx,
trait_def.generics.type_param_defs(),
&trait_def.generics.types,
tag_items_data_item_ty_param_bounds);
encode_region_param_defs(ebml_w, trait_def.generics.region_param_defs());
encode_region_param_defs(ebml_w, &trait_def.generics.regions);
encode_trait_ref(ebml_w, ecx, &*trait_def.trait_ref, tag_item_trait_ref);
encode_name(ebml_w, item.ident.name);
encode_attributes(ebml_w, item.attrs.as_slice());
Expand Down
Loading

0 comments on commit 1851550

Please sign in to comment.