@@ -18,6 +18,7 @@ import back.abi;
18
18
import middle. ty . pat_ty ;
19
19
20
20
import util. common ;
21
+ import util. common . append ;
21
22
import util. common . istr ;
22
23
import util. common . new_def_hash ;
23
24
import util. common . new_str_hash ;
@@ -566,15 +567,20 @@ fn align_of(TypeRef t) -> ValueRef {
566
567
ret llvm. LLVMConstIntCast ( lib. llvm . llvm . LLVMAlignOf ( t) , T_int ( ) , False ) ;
567
568
}
568
569
569
- fn trans_malloc ( @block_ctxt cx , @ty. t t ) -> result {
570
- auto scope_cx = find_scope_cx ( cx) ;
571
- auto ptr_ty = type_of ( cx. fcx . ccx , t) ;
572
- auto body_ty = lib. llvm . llvm . LLVMGetElementType ( ptr_ty) ;
570
+ fn trans_malloc_inner ( @block_ctxt cx , TypeRef llptr_ty ) -> result {
571
+ auto llbody_ty = lib. llvm . llvm . LLVMGetElementType ( llptr_ty) ;
573
572
// FIXME: need a table to collect tydesc globals.
574
573
auto tydesc = C_int ( 0 ) ;
575
- auto sz = size_of ( body_ty ) ;
574
+ auto sz = size_of ( llbody_ty ) ;
576
575
auto sub = trans_upcall ( cx, "upcall_malloc" , vec ( sz, tydesc) ) ;
577
- sub. val = sub. bcx . build . IntToPtr ( sub. val , ptr_ty) ;
576
+ sub. val = sub. bcx . build . IntToPtr ( sub. val , llptr_ty) ;
577
+ ret sub;
578
+ }
579
+
580
+ fn trans_malloc ( @block_ctxt cx , @ty. t t ) -> result {
581
+ auto scope_cx = find_scope_cx ( cx) ;
582
+ auto llptr_ty = type_of ( cx. fcx . ccx , t) ;
583
+ auto sub = trans_malloc_inner ( cx, llptr_ty) ;
578
584
scope_cx. cleanups += clean ( bind drop_ty ( _, sub. val , t) ) ;
579
585
ret sub;
580
586
}
@@ -2442,7 +2448,7 @@ impure fn trans_obj(@crate_ctxt cx, &ast._obj ob, ast.def_id oid,
2442
2448
auto llctor_decl = cx. item_ids. get( oid) ;
2443
2449
cx. item_names. insert( cx. path, llctor_decl) ;
2444
2450
2445
- // Translate obj ctor fields to function arguments.
2451
+ // Translate obj ctor args to function arguments.
2446
2452
let vec[ ast. arg] fn_args = vec( ) ;
2447
2453
for ( ast. obj_field f in ob. fields) {
2448
2454
fn_args += vec( rec( mode=ast. alias,
@@ -2456,7 +2462,8 @@ impure fn trans_obj(@crate_ctxt cx, &ast._obj ob, ast.def_id oid,
2456
2462
2457
2463
auto bcx = new_top_block_ctxt( fcx) ;
2458
2464
2459
- copy_args_to_allocas( bcx, fn_args, arg_tys_of_fn( ann) ) ;
2465
+ let vec[ ty. arg] arg_tys = arg_tys_of_fn( ann) ;
2466
+ copy_args_to_allocas( bcx, fn_args, arg_tys) ;
2460
2467
2461
2468
auto pair = bcx. build. Alloca ( type_of( cx, ret_ty_of_fn( ann) ) ) ;
2462
2469
auto vtbl = trans_vtbl( cx, ob, ty_params) ;
@@ -2468,9 +2475,37 @@ impure fn trans_obj(@crate_ctxt cx, &ast._obj ob, ast.def_id oid,
2468
2475
C_int ( abi. obj_field_box) ) ) ;
2469
2476
bcx. build. Store ( vtbl, pair_vtbl) ;
2470
2477
2471
- // FIXME: allocate the object body, copy the args in, etc.
2472
- bcx. build. Store ( C_null ( T_ptr ( T_box ( T_nil ( ) ) ) ) , pair_box) ;
2478
+ let TypeRef llbox_ty = T_ptr ( T_box ( T_nil ( ) ) ) ;
2479
+ if ( _vec. len[ ty. arg] ( arg_tys) == 0 u) {
2480
+ // Store null into pair, if no args.
2481
+ bcx. build. Store ( C_null ( llbox_ty) , pair_box) ;
2482
+ } else {
2483
+ // Malloc a box for the body and copy args in.
2484
+ let vec[ @ty. t] obj_fields = vec( ) ;
2485
+ for ( ty. arg a in arg_tys) {
2486
+ append[ @ty. t] ( obj_fields, a. ty) ;
2487
+ }
2488
+ // Synthesize an obj body:
2489
+ let @ty. t fields_ty = ty. plain_ty( ty. ty_tup( obj_fields) ) ;
2490
+ let TypeRef llfields_ty = type_of( bcx. fcx. ccx, fields_ty) ;
2491
+ let TypeRef llobj_body_ty =
2492
+ T_ptr ( T_box ( T_struct ( vec( T_tydesc ( ) ,
2493
+ llfields_ty) ) ) ) ;
2494
+ auto r = trans_malloc_inner( bcx, llobj_body_ty) ;
2495
+ auto box = r. val;
2496
+ auto rc = r. bcx. build. GEP ( box,
2497
+ vec( C_int ( 0 ) ,
2498
+ C_int ( abi. box_rc_field_refcnt) ) ) ;
2499
+ auto body = r. bcx. build. GEP ( box,
2500
+ vec( C_int ( 0 ) ,
2501
+ C_int ( abi. box_rc_field_body) ) ) ;
2502
+ r. bcx. build. Store ( C_int ( 1 ) , rc) ;
2503
+
2504
+ // FIXME: Copy args into body
2473
2505
2506
+ auto p = r. bcx. build. PointerCast ( box, llbox_ty) ;
2507
+ r. bcx. build. Store ( p, pair_box) ;
2508
+ }
2474
2509
bcx. build. Ret ( bcx. build. Load ( pair) ) ;
2475
2510
}
2476
2511
0 commit comments