Skip to content

Commit f24a537

Browse files
committed
Move subst data structures into subst.rs, fix capitalization
1 parent 61d7917 commit f24a537

31 files changed

+336
-313
lines changed

src/librustc/metadata/tydecode.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#![allow(non_camel_case_types)]
1818

19+
use middle::subst;
1920
use middle::ty;
2021

2122
use std::rc::Rc;
@@ -25,7 +26,6 @@ use std::uint;
2526
use syntax::abi;
2627
use syntax::ast;
2728
use syntax::ast::*;
28-
use syntax::owned_slice::OwnedSlice;
2929
use syntax::parse::token;
3030

3131
// Compact string representation for ty::t values. API ty_str &
@@ -133,7 +133,7 @@ pub fn parse_trait_ref_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tc
133133
}
134134

135135
pub fn parse_substs_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: &ty::ctxt,
136-
conv: conv_did) -> ty::substs {
136+
conv: conv_did) -> subst::Substs {
137137
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
138138
parse_substs(&mut st, conv)
139139
}
@@ -162,7 +162,7 @@ fn parse_trait_store(st: &mut PState, conv: conv_did) -> ty::TraitStore {
162162
}
163163
}
164164

165-
fn parse_substs(st: &mut PState, conv: conv_did) -> ty::substs {
165+
fn parse_substs(st: &mut PState, conv: conv_did) -> subst::Substs {
166166
let regions = parse_region_substs(st, |x,y| conv(x,y));
167167

168168
let self_ty = parse_opt(st, |st| parse_ty(st, |x,y| conv(x,y)) );
@@ -172,24 +172,24 @@ fn parse_substs(st: &mut PState, conv: conv_did) -> ty::substs {
172172
while peek(st) != ']' { params.push(parse_ty(st, |x,y| conv(x,y))); }
173173
st.pos = st.pos + 1u;
174174

175-
return ty::substs {
175+
return subst::Substs {
176176
regions: regions,
177177
self_ty: self_ty,
178178
tps: params
179179
};
180180
}
181181

182-
fn parse_region_substs(st: &mut PState, conv: conv_did) -> ty::RegionSubsts {
182+
fn parse_region_substs(st: &mut PState, conv: conv_did) -> subst::RegionSubsts {
183183
match next(st) {
184-
'e' => ty::ErasedRegions,
184+
'e' => subst::ErasedRegions,
185185
'n' => {
186186
let mut regions = vec!();
187187
while peek(st) != '.' {
188188
let r = parse_region(st, |x,y| conv(x,y));
189189
regions.push(r);
190190
}
191191
assert_eq!(next(st), '.');
192-
ty::NonerasedRegions(OwnedSlice::from_vec(regions))
192+
subst::NonerasedRegions(regions)
193193
}
194194
_ => fail!("parse_bound_region: bad input")
195195
}

src/librustc/metadata/tyencode.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::cell::RefCell;
1717
use std::collections::HashMap;
1818
use std::io::MemWriter;
1919

20+
use middle::subst;
2021
use middle::ty::param_ty;
2122
use middle::ty;
2223

@@ -96,20 +97,20 @@ fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
9697
}
9798
}
9899

99-
pub fn enc_substs(w: &mut MemWriter, cx: &ctxt, substs: &ty::substs) {
100+
pub fn enc_substs(w: &mut MemWriter, cx: &ctxt, substs: &subst::Substs) {
100101
enc_region_substs(w, cx, &substs.regions);
101102
enc_opt(w, substs.self_ty, |w, t| enc_ty(w, cx, t));
102103
mywrite!(w, "[");
103104
for t in substs.tps.iter() { enc_ty(w, cx, *t); }
104105
mywrite!(w, "]");
105106
}
106107

107-
fn enc_region_substs(w: &mut MemWriter, cx: &ctxt, substs: &ty::RegionSubsts) {
108+
fn enc_region_substs(w: &mut MemWriter, cx: &ctxt, substs: &subst::RegionSubsts) {
108109
match *substs {
109-
ty::ErasedRegions => {
110+
subst::ErasedRegions => {
110111
mywrite!(w, "e");
111112
}
112-
ty::NonerasedRegions(ref regions) => {
113+
subst::NonerasedRegions(ref regions) => {
113114
mywrite!(w, "n");
114115
for &r in regions.iter() {
115116
enc_region(w, cx, r);

src/librustc/middle/astencode.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use metadata::tydecode;
2323
use metadata::tydecode::{DefIdSource, NominalType, TypeWithId, TypeParameter,
2424
RegionParameter};
2525
use metadata::tyencode;
26+
use middle::subst;
2627
use middle::typeck::{MethodCall, MethodCallee, MethodOrigin};
2728
use middle::{ty, typeck};
2829
use util::ppaux::ty_to_str;
@@ -796,7 +797,7 @@ trait ebml_writer_helpers {
796797
fn emit_tpbt(&mut self,
797798
ecx: &e::EncodeContext,
798799
tpbt: ty::ty_param_bounds_and_ty);
799-
fn emit_substs(&mut self, ecx: &e::EncodeContext, substs: &ty::substs);
800+
fn emit_substs(&mut self, ecx: &e::EncodeContext, substs: &subst::Substs);
800801
fn emit_auto_adjustment(&mut self, ecx: &e::EncodeContext, adj: &ty::AutoAdjustment);
801802
}
802803

@@ -842,7 +843,7 @@ impl<'a> ebml_writer_helpers for Encoder<'a> {
842843
});
843844
}
844845

845-
fn emit_substs(&mut self, ecx: &e::EncodeContext, substs: &ty::substs) {
846+
fn emit_substs(&mut self, ecx: &e::EncodeContext, substs: &subst::Substs) {
846847
self.emit_opaque(|this| Ok(tyencode::enc_substs(this.writer,
847848
&ecx.ty_str_ctxt(),
848849
substs)));
@@ -1076,7 +1077,7 @@ trait ebml_decoder_decoder_helpers {
10761077
-> ty::TypeParameterDef;
10771078
fn read_ty_param_bounds_and_ty(&mut self, xcx: &ExtendedDecodeContext)
10781079
-> ty::ty_param_bounds_and_ty;
1079-
fn read_substs(&mut self, xcx: &ExtendedDecodeContext) -> ty::substs;
1080+
fn read_substs(&mut self, xcx: &ExtendedDecodeContext) -> subst::Substs;
10801081
fn read_auto_adjustment(&mut self, xcx: &ExtendedDecodeContext) -> ty::AutoAdjustment;
10811082
fn convert_def_id(&mut self,
10821083
xcx: &ExtendedDecodeContext,
@@ -1093,7 +1094,7 @@ trait ebml_decoder_decoder_helpers {
10931094
cdata: &cstore::crate_metadata) -> Vec<ty::t>;
10941095
fn read_substs_noxcx(&mut self, tcx: &ty::ctxt,
10951096
cdata: &cstore::crate_metadata)
1096-
-> ty::substs;
1097+
-> subst::Substs;
10971098
}
10981099

10991100
impl<'a> ebml_decoder_decoder_helpers for reader::Decoder<'a> {
@@ -1121,7 +1122,7 @@ impl<'a> ebml_decoder_decoder_helpers for reader::Decoder<'a> {
11211122
fn read_substs_noxcx(&mut self,
11221123
tcx: &ty::ctxt,
11231124
cdata: &cstore::crate_metadata)
1124-
-> ty::substs
1125+
-> subst::Substs
11251126
{
11261127
self.read_opaque(|_, doc| {
11271128
Ok(tydecode::parse_substs_data(
@@ -1210,7 +1211,7 @@ impl<'a> ebml_decoder_decoder_helpers for reader::Decoder<'a> {
12101211
}).unwrap()
12111212
}
12121213

1213-
fn read_substs(&mut self, xcx: &ExtendedDecodeContext) -> ty::substs {
1214+
fn read_substs(&mut self, xcx: &ExtendedDecodeContext) -> subst::Substs {
12141215
self.read_opaque(|this, doc| {
12151216
Ok(tydecode::parse_substs_data(doc.data,
12161217
xcx.dcx.cdata.cnum,

src/librustc/middle/kind.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use middle::freevars::freevar_entry;
1313
use middle::freevars;
14+
use middle::subst;
1415
use middle::ty;
1516
use middle::typeck;
1617
use util::ppaux::{Repr, ty_to_str};
@@ -19,7 +20,6 @@ use util::ppaux::UserString;
1920
use syntax::ast::*;
2021
use syntax::attr;
2122
use syntax::codemap::Span;
22-
use syntax::owned_slice::OwnedSlice;
2323
use syntax::print::pprust::{expr_to_str,path_to_str};
2424
use syntax::{visit,ast_util};
2525
use syntax::visit::Visitor;
@@ -87,8 +87,8 @@ fn check_struct_safe_for_destructor(cx: &mut Context,
8787
struct_did: DefId) {
8888
let struct_tpt = ty::lookup_item_type(cx.tcx, struct_did);
8989
if !struct_tpt.generics.has_type_params() {
90-
let struct_ty = ty::mk_struct(cx.tcx, struct_did, ty::substs {
91-
regions: ty::NonerasedRegions(OwnedSlice::empty()),
90+
let struct_ty = ty::mk_struct(cx.tcx, struct_did, subst::Substs {
91+
regions: subst::NonerasedRegions(Vec::new()),
9292
self_ty: None,
9393
tps: Vec::new()
9494
});

src/librustc/middle/subst.rs

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,70 @@ use middle::ty_fold;
1515
use middle::ty_fold::{TypeFoldable, TypeFolder};
1616
use util::ppaux::Repr;
1717

18+
use std::vec::Vec;
1819
use syntax::codemap::Span;
1920

21+
///////////////////////////////////////////////////////////////////////////
22+
23+
/**
24+
* Represents the values to use when substituting lifetime parameters.
25+
* If the value is `ErasedRegions`, then this subst is occurring during
26+
* trans, and all region parameters will be replaced with `ty::ReStatic`. */
27+
#[deriving(Clone, Eq, TotalEq, Hash)]
28+
pub enum RegionSubsts {
29+
ErasedRegions,
30+
NonerasedRegions(Vec<ty::Region>)
31+
}
32+
33+
/**
34+
* The type `Substs` represents the kinds of things that can be substituted to
35+
* convert a polytype into a monotype. Note however that substituting bound
36+
* regions other than `self` is done through a different mechanism:
37+
*
38+
* - `tps` represents the type parameters in scope. They are indexed
39+
* according to the order in which they were declared.
40+
*
41+
* - `self_r` indicates the region parameter `self` that is present on nominal
42+
* types (enums, structs) declared as having a region parameter. `self_r`
43+
* should always be none for types that are not region-parameterized and
44+
* Some(_) for types that are. The only bound region parameter that should
45+
* appear within a region-parameterized type is `self`.
46+
*
47+
* - `self_ty` is the type to which `self` should be remapped, if any. The
48+
* `self` type is rather funny in that it can only appear on traits and is
49+
* always substituted away to the implementing type for a trait. */
50+
#[deriving(Clone, Eq, TotalEq, Hash)]
51+
pub struct Substs {
52+
pub self_ty: Option<ty::t>,
53+
pub tps: Vec<ty::t>,
54+
pub regions: RegionSubsts,
55+
}
56+
57+
impl Substs {
58+
pub fn empty() -> Substs {
59+
Substs {
60+
self_ty: None,
61+
tps: Vec::new(),
62+
regions: NonerasedRegions(Vec::new())
63+
}
64+
}
65+
66+
pub fn is_noop(&self) -> bool {
67+
let regions_is_noop = match self.regions {
68+
ErasedRegions => false, // may be used to canonicalize
69+
NonerasedRegions(ref regions) => regions.is_empty()
70+
};
71+
72+
self.tps.len() == 0u &&
73+
regions_is_noop &&
74+
self.self_ty.is_none()
75+
}
76+
77+
pub fn self_ty(&self) -> ty::t {
78+
self.self_ty.unwrap()
79+
}
80+
}
81+
2082
///////////////////////////////////////////////////////////////////////////
2183
// Public trait `Subst`
2284
//
@@ -25,20 +87,20 @@ use syntax::codemap::Span;
2587
// there is more information available (for better errors).
2688

2789
pub trait Subst {
28-
fn subst(&self, tcx: &ty::ctxt, substs: &ty::substs) -> Self {
90+
fn subst(&self, tcx: &ty::ctxt, substs: &Substs) -> Self {
2991
self.subst_spanned(tcx, substs, None)
3092
}
3193

3294
fn subst_spanned(&self, tcx: &ty::ctxt,
33-
substs: &ty::substs,
95+
substs: &Substs,
3496
span: Option<Span>)
3597
-> Self;
3698
}
3799

38100
impl<T:TypeFoldable> Subst for T {
39101
fn subst_spanned(&self,
40102
tcx: &ty::ctxt,
41-
substs: &ty::substs,
103+
substs: &Substs,
42104
span: Option<Span>)
43105
-> T
44106
{
@@ -56,7 +118,7 @@ impl<T:TypeFoldable> Subst for T {
56118

57119
struct SubstFolder<'a> {
58120
tcx: &'a ty::ctxt,
59-
substs: &'a ty::substs,
121+
substs: &'a Substs,
60122

61123
// The location for which the substitution is performed, if available.
62124
span: Option<Span>,
@@ -81,8 +143,8 @@ impl<'a> TypeFolder for SubstFolder<'a> {
81143
match r {
82144
ty::ReEarlyBound(_, i, _) => {
83145
match self.substs.regions {
84-
ty::ErasedRegions => ty::ReStatic,
85-
ty::NonerasedRegions(ref regions) => *regions.get(i),
146+
ErasedRegions => ty::ReStatic,
147+
NonerasedRegions(ref regions) => *regions.get(i),
86148
}
87149
}
88150
_ => r

src/librustc/middle/trans/adt.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ use std::num::{Bitwise};
5151
use std::rc::Rc;
5252

5353
use lib::llvm::{ValueRef, True, IntEQ, IntNE};
54+
use middle::subst;
55+
use middle::subst::Subst;
5456
use middle::trans::_match;
5557
use middle::trans::build::*;
5658
use middle::trans::common::*;
@@ -304,10 +306,10 @@ impl Case {
304306
}
305307
}
306308

307-
fn get_cases(tcx: &ty::ctxt, def_id: ast::DefId, substs: &ty::substs) -> Vec<Case> {
309+
fn get_cases(tcx: &ty::ctxt, def_id: ast::DefId, substs: &subst::Substs) -> Vec<Case> {
308310
ty::enum_variants(tcx, def_id).iter().map(|vi| {
309311
let arg_tys = vi.args.iter().map(|&raw_ty| {
310-
ty::subst(tcx, substs, raw_ty)
312+
raw_ty.subst(tcx, substs)
311313
}).collect();
312314
Case { discr: vi.disr_val, tys: arg_tys }
313315
}).collect()

src/librustc/middle/trans/base.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ use middle::lint;
4040
use middle::astencode;
4141
use middle::lang_items::{LangItem, ExchangeMallocFnLangItem, StartFnLangItem};
4242
use middle::weak_lang_items;
43+
use middle::subst;
44+
use middle::subst::Subst;
4345
use middle::trans::_match;
4446
use middle::trans::adt;
4547
use middle::trans::build::*;
@@ -442,7 +444,7 @@ pub fn get_res_dtor(ccx: &CrateContext,
442444
did: ast::DefId,
443445
t: ty::t,
444446
parent_id: ast::DefId,
445-
substs: &ty::substs)
447+
substs: &subst::Substs)
446448
-> ValueRef {
447449
let _icx = push_ctxt("trans_res_dtor");
448450
let did = if did.krate != ast::LOCAL_CRATE {
@@ -463,8 +465,7 @@ pub fn get_res_dtor(ccx: &CrateContext,
463465
} else {
464466
let tcx = ccx.tcx();
465467
let name = csearch::get_symbol(&ccx.sess().cstore, did);
466-
let class_ty = ty::subst(tcx, substs,
467-
ty::lookup_item_type(tcx, parent_id).ty);
468+
let class_ty = ty::lookup_item_type(tcx, parent_id).ty.subst(tcx, substs);
468469
let llty = type_of_dtor(ccx, class_ty);
469470
let dtor_ty = ty::mk_ctor_fn(ccx.tcx(), ast::DUMMY_NODE_ID,
470471
[glue::get_drop_glue_type(ccx, t)], ty::mk_nil());
@@ -633,7 +634,7 @@ pub fn iter_structural_ty<'r,
633634
repr: &adt::Repr,
634635
av: ValueRef,
635636
variant: &ty::VariantInfo,
636-
substs: &ty::substs,
637+
substs: &subst::Substs,
637638
f: val_and_ty_fn<'r,'b>)
638639
-> &'b Block<'b> {
639640
let _icx = push_ctxt("iter_variant");
@@ -643,7 +644,7 @@ pub fn iter_structural_ty<'r,
643644
for (i, &arg) in variant.args.iter().enumerate() {
644645
cx = f(cx,
645646
adt::trans_field_ptr(cx, repr, av, variant.disr_val, i),
646-
ty::subst(tcx, substs, arg));
647+
arg.subst(tcx, substs));
647648
}
648649
return cx;
649650
}

0 commit comments

Comments
 (0)