Skip to content

Commit 12e0a83

Browse files
committed
---
yaml --- r: 45011 b: refs/heads/master c: 04ecab9 h: refs/heads/master i: 45009: fa19cf7 45007: 8222f9f v: v3
1 parent 0cdb6e9 commit 12e0a83

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 8a1706610b2cf458fe6e5ed12e227e3ca9dcaa6d
2+
refs/heads/master: 04ecab909a38a8c19405ea577e826facfe4d7da8
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d9689399d091c3265f00434a69c551a61c28dc
55
refs/heads/try: ef355f6332f83371e4acf04fc4eb940ab41d78d3

trunk/src/librustc/middle/trans/adt.rs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use core::libc::c_ulonglong;
1112
use core::option::{Option, Some, None};
1213
use core::vec;
13-
use lib::llvm::{ValueRef, TypeRef};
14+
use lib::llvm::{ValueRef, TypeRef, True, False};
1415
use middle::trans::_match;
1516
use middle::trans::build::*;
1617
use middle::trans::common::*;
@@ -23,7 +24,7 @@ use util::ppaux::ty_to_str;
2324

2425
// XXX: should this be done with boxed traits instead of ML-style?
2526
pub enum Repr {
26-
CEnum,
27+
CEnum(int, int), /* discriminant range */
2728
Univariant(Struct, Destructor),
2829
General(~[Struct])
2930
}
@@ -65,13 +66,13 @@ pub fn represent_type(cx: @CrateContext, t: ty::t) -> Repr {
6566
})), if dt { DtorPresent } else { DtorAbsent })
6667
}
6768
ty::ty_enum(def_id, ref substs) => {
68-
struct Case { discr: i64, tys: ~[ty::t] };
69+
struct Case { discr: int, tys: ~[ty::t] };
6970

7071
let cases = do ty::enum_variants(cx.tcx, def_id).map |vi| {
7172
let arg_tys = do vi.args.map |&raw_ty| {
7273
ty::subst(cx.tcx, substs, raw_ty)
7374
};
74-
Case { discr: vi.disr_val /*bad*/as i64, tys: arg_tys }
75+
Case { discr: vi.disr_val, tys: arg_tys }
7576
};
7677
if cases.len() == 0 {
7778
// Uninhabitable; represent as unit
@@ -80,9 +81,10 @@ pub fn represent_type(cx: @CrateContext, t: ty::t) -> Repr {
8081
// struct, tuple, newtype, etc.
8182
Univariant(mk_struct(cx, cases[0].tys), NoDtor)
8283
} else if cases.all(|c| c.tys.len() == 0) {
83-
CEnum
84+
let discrs = cases.map(|c| c.discr);
85+
CEnum(discrs.min(), discrs.max())
8486
} else {
85-
if !cases.alli(|i,c| c.discr == (i as i64)) {
87+
if !cases.alli(|i,c| c.discr == (i as int)) {
8688
cx.sess.bug(fmt!("non-C-like enum %s with specified \
8789
discriminants",
8890
ty::item_path_str(cx.tcx, def_id)))
@@ -114,7 +116,7 @@ pub fn fields_of(cx: @CrateContext, r: &Repr) -> ~[TypeRef] {
114116
fn generic_fields_of(cx: @CrateContext, r: &Repr, sizing: bool)
115117
-> ~[TypeRef] {
116118
match *r {
117-
CEnum => ~[T_enum_discrim(cx)],
119+
CEnum(*) => ~[T_enum_discrim(cx)],
118120
Univariant(ref st, dt) => {
119121
let f = if sizing {
120122
st.fields.map(|&ty| type_of::sizing_type_of(cx, ty))
@@ -134,25 +136,44 @@ fn generic_fields_of(cx: @CrateContext, r: &Repr, sizing: bool)
134136
}
135137
}
136138

139+
fn load_discr(bcx: block, scrutinee: ValueRef, min: int, max: int)
140+
-> ValueRef {
141+
let ptr = GEPi(bcx, scrutinee, [0, 0]);
142+
// XXX: write tests for the edge cases here
143+
if max + 1 == min {
144+
// i.e., if the range is everything. The lo==hi case would be
145+
// rejected by the LLVM verifier (it would mean either an
146+
// empty set, which is impossible, or the entire range of the
147+
// type, which is pointless).
148+
Load(bcx, ptr)
149+
} else {
150+
// llvm::ConstantRange can deal with ranges that wrap around,
151+
// so an overflow on (max + 1) is fine.
152+
LoadRangeAssert(bcx, ptr, min as c_ulonglong,
153+
(max + 1) as c_ulonglong,
154+
/* signed: */ True)
155+
}
156+
}
157+
137158
pub fn trans_switch(bcx: block, r: &Repr, scrutinee: ValueRef) ->
138159
(_match::branch_kind, Option<ValueRef>) {
139-
// XXX: LoadRangeAssert
140160
match *r {
141-
CEnum => {
142-
(_match::switch, Some(Load(bcx, GEPi(bcx, scrutinee, [0, 0]))))
161+
CEnum(min, max) => {
162+
(_match::switch, Some(load_discr(bcx, scrutinee, min, max)))
143163
}
144164
Univariant(*) => {
145165
(_match::single, None)
146166
}
147-
General(*) => {
148-
(_match::switch, Some(Load(bcx, GEPi(bcx, scrutinee, [0, 0]))))
167+
General(ref cases) => {
168+
(_match::switch, Some(load_discr(bcx, scrutinee, 0,
169+
(cases.len() - 1) as int)))
149170
}
150171
}
151172
}
152173

153174
pub fn trans_case(bcx: block, r: &Repr, discr: int) -> _match::opt_result {
154175
match *r {
155-
CEnum => {
176+
CEnum(*) => {
156177
_match::single_result(rslt(bcx, C_int(bcx.ccx(), discr)))
157178
}
158179
Univariant(*) => {
@@ -166,7 +187,8 @@ pub fn trans_case(bcx: block, r: &Repr, discr: int) -> _match::opt_result {
166187

167188
pub fn trans_set_discr(bcx: block, r: &Repr, val: ValueRef, discr: int) {
168189
match *r {
169-
CEnum => {
190+
CEnum(min, max) => {
191+
assert min <= discr && discr <= max;
170192
Store(bcx, C_int(bcx.ccx(), discr), GEPi(bcx, val, [0, 0]))
171193
}
172194
Univariant(_, DtorPresent) => {
@@ -184,7 +206,7 @@ pub fn trans_set_discr(bcx: block, r: &Repr, val: ValueRef, discr: int) {
184206

185207
pub fn num_args(r: &Repr, discr: int) -> uint {
186208
match *r {
187-
CEnum => 0,
209+
CEnum(*) => 0,
188210
Univariant(ref st, _dt) => { assert discr == 0; st.fields.len() }
189211
General(ref cases) => cases[discr as uint].fields.len()
190212
}
@@ -196,7 +218,7 @@ pub fn trans_GEP(bcx: block, r: &Repr, val: ValueRef, discr: int, ix: uint)
196218
// decide to do some kind of cdr-coding-like non-unique repr
197219
// someday), it'll need to return a possibly-new bcx as well.
198220
match *r {
199-
CEnum => {
221+
CEnum(*) => {
200222
bcx.ccx().sess.bug(~"element access in C-like enum")
201223
}
202224
Univariant(ref st, dt) => {
@@ -232,8 +254,9 @@ fn struct_GEP(bcx: block, st: &Struct, val: ValueRef, ix: uint,
232254
pub fn trans_const(ccx: @CrateContext, r: &Repr, discr: int,
233255
vals: &[ValueRef]) -> ValueRef {
234256
match *r {
235-
CEnum => {
257+
CEnum(min, max) => {
236258
assert vals.len() == 0;
259+
assert min <= discr && discr <= max;
237260
C_int(ccx, discr)
238261
}
239262
Univariant(ref st, _dt) => {

0 commit comments

Comments
 (0)