@@ -165,10 +165,58 @@ pub fn classify(e: &expr,
165
165
pub fn lookup_const( tcx : ty:: ctxt , e : & expr ) -> Option <@expr> {
166
166
match tcx. def_map . find ( & e. id ) {
167
167
Some ( & ast:: def_static( def_id, false ) ) => lookup_const_by_id ( tcx, def_id) ,
168
+ Some ( & ast:: def_variant( enum_def, variant_def) ) => lookup_variant_by_id ( tcx,
169
+ enum_def,
170
+ variant_def) ,
168
171
_ => None
169
172
}
170
173
}
171
174
175
+ pub fn lookup_variant_by_id( tcx : ty:: ctxt ,
176
+ enum_def : ast:: def_id ,
177
+ variant_def : ast:: def_id )
178
+ -> Option <@expr> {
179
+ fn variant_expr( variants : & [ ast:: variant ] , id : ast:: node_id ) -> Option <@expr> {
180
+ for variants . iter( ) . advance |variant| {
181
+ if variant. node. id == id {
182
+ return variant. node. disr_expr;
183
+ }
184
+ }
185
+ None
186
+ }
187
+
188
+ if ast_util:: is_local ( enum_def) {
189
+ match tcx. items . find ( & enum_def. node ) {
190
+ None => None ,
191
+ Some ( & ast_map:: node_item( it, _) ) => match it. node {
192
+ item_enum( ast:: enum_def { variants : ref variants } , _) => {
193
+ variant_expr ( * variants, variant_def. node )
194
+ }
195
+ _ => None
196
+ } ,
197
+ Some ( _) => None
198
+ }
199
+ } else {
200
+ let maps = astencode:: Maps {
201
+ root_map : @mut HashMap :: new ( ) ,
202
+ method_map : @mut HashMap :: new ( ) ,
203
+ vtable_map : @mut HashMap :: new ( ) ,
204
+ write_guard_map : @mut HashSet :: new ( ) ,
205
+ capture_map : @mut HashMap :: new ( )
206
+ } ;
207
+ match csearch:: maybe_get_item_ast ( tcx, enum_def,
208
+ |a, b, c, d| astencode:: decode_inlined_item ( a, b, maps, /*bar*/ copy c, d) ) {
209
+ csearch:: found( ast:: ii_item( item) ) => match item. node {
210
+ item_enum( ast:: enum_def { variants : ref variants } , _) => {
211
+ variant_expr ( * variants, variant_def. node )
212
+ }
213
+ _ => None
214
+ } ,
215
+ _ => None
216
+ }
217
+ }
218
+ }
219
+
172
220
pub fn lookup_const_by_id( tcx : ty:: ctxt ,
173
221
def_id : ast:: def_id )
174
222
-> Option <@expr> {
@@ -237,13 +285,13 @@ pub enum const_val {
237
285
}
238
286
239
287
pub fn eval_const_expr ( tcx : middle:: ty:: ctxt , e : & expr ) -> const_val {
240
- match eval_const_expr_partial ( tcx, e) {
288
+ match eval_const_expr_partial ( & tcx, e) {
241
289
Ok ( r) => r,
242
290
Err ( s) => tcx. sess . span_fatal ( e. span , s)
243
291
}
244
292
}
245
293
246
- pub fn eval_const_expr_partial( tcx : middle :: ty:: ctxt , e : & expr )
294
+ pub fn eval_const_expr_partial< T : ty:: ExprTyProvider > ( tcx : & T , e : & expr )
247
295
-> Result < const_val , ~str > {
248
296
use middle:: ty;
249
297
fn fromb ( b : bool ) -> Result < const_val , ~str > { Ok ( const_int ( b as i64 ) ) }
@@ -360,7 +408,7 @@ pub fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: &expr)
360
408
}
361
409
}
362
410
expr_cast( base, _) => {
363
- let ety = ty :: expr_ty ( tcx , e) ;
411
+ let ety = tcx . expr_ty ( e) ;
364
412
let base = eval_const_expr_partial ( tcx, base) ;
365
413
match /*bad*/ copy base {
366
414
Err ( _) => base,
@@ -390,8 +438,8 @@ pub fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: &expr)
390
438
}
391
439
}
392
440
expr_path( _) => {
393
- match lookup_const ( tcx, e) {
394
- Some ( actual_e) => eval_const_expr_partial ( tcx, actual_e) ,
441
+ match lookup_const ( tcx. ty_ctxt ( ) , e) {
442
+ Some ( actual_e) => eval_const_expr_partial ( & tcx. ty_ctxt ( ) , actual_e) ,
395
443
None => Err ( ~"Non -constant path in constant expr")
396
444
}
397
445
}
0 commit comments