@@ -28,10 +28,6 @@ fn const_lit(cx: @crate_ctxt, e: @ast::expr, lit: ast::lit)
28
28
}
29
29
}
30
30
31
- // FIXME (#2530): this should do some structural hash-consing to avoid
32
- // duplicate constants. I think. Maybe LLVM has a magical mode that does so
33
- // later on?
34
-
35
31
fn const_ptrcast( cx: @crate_ctxt, a: ValueRef , t: TypeRef ) -> ValueRef {
36
32
let b = llvm : : LLVMConstPointerCast ( a, T_ptr ( t) ) ;
37
33
assert cx. const_globals. insert( b as int, a) ;
@@ -341,24 +337,29 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
341
337
~"bad const-slice expr")
342
338
}
343
339
}
344
- ast:: expr_path ( _) => {
340
+ ast:: expr_path ( pth) => {
341
+ assert pth. types . len ( ) == 0 ;
345
342
match cx. tcx . def_map . find ( e. id ) {
343
+ Some ( ast:: def_fn( def_id, _) ) => {
344
+ assert ast_util:: is_local ( def_id) ;
345
+ let f = base:: get_item_val ( cx, def_id. node ) ;
346
+ C_struct ( ~[ f, C_null ( T_opaque_box_ptr ( cx) ) ] )
347
+ }
346
348
Some ( ast:: def_const( def_id) ) => {
347
- // Don't know how to handle external consts
348
349
assert ast_util:: is_local ( def_id) ;
349
- match cx. tcx . items . get ( def_id. node ) {
350
- ast_map:: node_item( @{
351
- node: ast:: item_const( _, subexpr) , _
352
- } , _) => {
353
- // FIXME (#2530): Instead of recursing here to regenerate
354
- // the values for other constants, we should just look up
355
- // the already-defined value.
356
- const_expr ( cx, subexpr)
357
- }
358
- _ => cx. sess . span_bug ( e. span , ~"expected item")
350
+ if ! cx. const_values . contains_key ( def_id. node ) {
351
+ match cx. tcx . items . get ( def_id. node ) {
352
+ ast_map:: node_item( @{
353
+ node: ast:: item_const( _, subexpr) , _
354
+ } , _) => {
355
+ trans_const ( cx, subexpr, def_id. node ) ;
356
+ }
357
+ _ => cx. sess . span_bug ( e. span , ~"expected item")
358
+ }
359
359
}
360
+ cx. const_values . get ( def_id. node )
360
361
}
361
- _ => cx. sess . span_bug ( e. span , ~"expected to find a const def")
362
+ _ => cx. sess . span_bug ( e. span , ~"expected a const or fn def")
362
363
}
363
364
}
364
365
ast:: expr_paren ( e) => { return const_expr ( cx, e) ; }
@@ -369,11 +370,9 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
369
370
370
371
fn trans_const ( ccx : @crate_ctxt , e : @ast:: expr , id : ast:: node_id ) {
371
372
let _icx = ccx. insn_ctxt ( "trans_const" ) ;
372
- let v = const_expr ( ccx, e) ;
373
-
374
- // The scalars come back as 1st class LLVM vals
375
- // which we have to stick into global constants.
376
373
let g = base:: get_item_val ( ccx, id) ;
374
+ let v = const_expr ( ccx, e) ;
375
+ ccx. const_values . insert ( id, v) ;
377
376
llvm:: LLVMSetInitializer ( g, v) ;
378
377
llvm:: LLVMSetGlobalConstant ( g, True ) ;
379
378
}
0 commit comments