Skip to content

Commit cfcbec3

Browse files
committed
Implement an initial version of placement new.
1 parent ba39e27 commit cfcbec3

File tree

22 files changed

+266
-87
lines changed

22 files changed

+266
-87
lines changed

src/cargo/cargo.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ fn for_each_package(c: cargo, b: fn(source, package)) {
434434

435435
// Runs all programs in directory <buildpath>
436436
fn run_programs(buildpath: str) {
437-
let new = os::list_dir(buildpath);
438-
for ct: str in new {
437+
let newv = os::list_dir(buildpath);
438+
for ct: str in newv {
439439
run::run_program(ct, []);
440440
}
441441
}
@@ -471,9 +471,9 @@ fn install_one_crate(c: cargo, path: str, cf: str) {
471471
none { ret; }
472472
some(bp) { bp }
473473
};
474-
let new = os::list_dir(buildpath);
474+
let newv = os::list_dir(buildpath);
475475
let exec_suffix = os::exe_suffix();
476-
for ct: str in new {
476+
for ct: str in newv {
477477
if (exec_suffix != "" && str::ends_with(ct, exec_suffix)) ||
478478
(exec_suffix == "" && !str::starts_with(path::basename(ct),
479479
"lib")) {

src/fuzzer/fuzzer.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -488,23 +488,23 @@ fn file_might_not_converge(filename: str) -> bool {
488488
fn check_roundtrip_convergence(code: @str, maxIters: uint) {
489489

490490
let i = 0u;
491-
let new = code;
492-
let old = code;
491+
let newv = code;
492+
let oldv = code;
493493

494494
while i < maxIters {
495-
old = new;
496-
if content_might_not_converge(*old) { ret; }
497-
new = @parse_and_print(old);
498-
if old == new { break; }
495+
oldv = newv;
496+
if content_might_not_converge(*oldv) { ret; }
497+
newv = @parse_and_print(oldv);
498+
if oldv == newv { break; }
499499
i += 1u;
500500
}
501501

502-
if old == new {
502+
if oldv == newv {
503503
#error("Converged after %u iterations", i);
504504
} else {
505505
#error("Did not converge after %u iterations!", i);
506-
write_file("round-trip-a.rs", *old);
507-
write_file("round-trip-b.rs", *new);
506+
write_file("round-trip-a.rs", *oldv);
507+
write_file("round-trip-b.rs", *newv);
508508
run::run_program("diff",
509509
["-w", "-u", "round-trip-a.rs",
510510
"round-trip-b.rs"]);

src/libcore/path.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,30 +266,30 @@ fn normalize(p: path) -> path {
266266
#[cfg(target_os = "linux")]
267267
#[cfg(target_os = "macos")]
268268
#[cfg(target_os = "freebsd")]
269-
fn reabsolute(orig: path, new: path) -> path {
269+
fn reabsolute(orig: path, n: path) -> path {
270270
if path_is_absolute(orig) {
271-
path_sep() + new
271+
path_sep() + n
272272
} else {
273-
new
273+
n
274274
}
275275
}
276276

277277
#[cfg(target_os = "win32")]
278-
fn reabsolute(orig: path, new: path) -> path {
278+
fn reabsolute(orig: path, newp: path) -> path {
279279
if path_is_absolute(orig) && orig[0] == consts::path_sep as u8 {
280-
str::from_char(consts::path_sep) + new
280+
str::from_char(consts::path_sep) + newp
281281
} else {
282-
new
282+
newp
283283
}
284284
}
285285

286-
fn reterminate(orig: path, new: path) -> path {
286+
fn reterminate(orig: path, newp: path) -> path {
287287
let last = orig[str::len(orig) - 1u];
288288
if last == consts::path_sep as u8
289289
|| last == consts::path_sep as u8 {
290-
ret new + path_sep();
290+
ret newp + path_sep();
291291
} else {
292-
ret new;
292+
ret newp;
293293
}
294294
}
295295
}

src/libcore/vec.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,22 +468,22 @@ Concatenate a vector of vectors.
468468
Flattens a vector of vectors of T into a single vector of T.
469469
"]
470470
fn concat<T: copy>(v: [const [const T]]) -> [T] {
471-
let mut new: [T] = [];
472-
for inner: [T] in v { new += inner; }
473-
ret new;
471+
let mut r: [T] = [];
472+
for inner: [T] in v { r += inner; }
473+
ret r;
474474
}
475475

476476
#[doc = "
477477
Concatenate a vector of vectors, placing a given separator between each
478478
"]
479479
fn connect<T: copy>(v: [const [const T]], sep: T) -> [T] {
480-
let mut new: [T] = [];
480+
let mut r: [T] = [];
481481
let mut first = true;
482482
for inner: [T] in v {
483-
if first { first = false; } else { push(new, sep); }
484-
new += inner;
483+
if first { first = false; } else { push(r, sep); }
484+
r += inner;
485485
}
486-
ret new;
486+
ret r;
487487
}
488488

489489
#[doc = "Reduce a vector from left to right"]

src/rustc/middle/resolve.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,9 @@ fn resolve_impl_in_expr(e: @env, x: @ast::expr, sc: iscopes, v: vt<iscopes>) {
23082308
ast::expr_assign_op(_, _, _) | ast::expr_index(_, _) {
23092309
e.impl_map.insert(x.id, sc);
23102310
}
2311+
ast::expr_new(p, _, _) {
2312+
e.impl_map.insert(p.id, sc);
2313+
}
23112314
_ {}
23122315
}
23132316
visit::visit_expr(x, sc, v);

src/rustc/middle/trans/base.rs

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,9 +1261,9 @@ fn trans_unary(bcx: block, op: ast::unop, e: @ast::expr,
12611261
some(origin) {
12621262
let callee_id = ast_util::op_expr_callee_id(un_expr);
12631263
let fty = node_id_type(bcx, callee_id);
1264-
ret trans_call_inner(bcx, fty, {|bcx|
1264+
ret trans_call_inner(bcx, fty, expr_ty(bcx, un_expr), {|bcx|
12651265
impl::trans_method_callee(bcx, callee_id, e, origin)
1266-
}, [], un_expr.id, dest);
1266+
}, arg_exprs([]), dest);
12671267
}
12681268
_ {}
12691269
}
@@ -1450,10 +1450,10 @@ fn trans_assign_op(bcx: block, ex: @ast::expr, op: ast::binop,
14501450
some(origin) {
14511451
let callee_id = ast_util::op_expr_callee_id(ex);
14521452
let fty = node_id_type(bcx, callee_id);
1453-
ret trans_call_inner(bcx, fty, {|bcx|
1453+
ret trans_call_inner(bcx, fty, expr_ty(bcx, ex), {|bcx|
14541454
// FIXME provide the already-computed address, not the expr
14551455
impl::trans_method_callee(bcx, callee_id, dst, origin)
1456-
}, [src], ex.id, save_in(lhs_res.val));
1456+
}, arg_exprs([src]), save_in(lhs_res.val));
14571457
}
14581458
_ {}
14591459
}
@@ -1561,9 +1561,9 @@ fn trans_binary(bcx: block, op: ast::binop, lhs: @ast::expr,
15611561
some(origin) {
15621562
let callee_id = ast_util::op_expr_callee_id(ex);
15631563
let fty = node_id_type(bcx, callee_id);
1564-
ret trans_call_inner(bcx, fty, {|bcx|
1564+
ret trans_call_inner(bcx, fty, expr_ty(bcx, ex), {|bcx|
15651565
impl::trans_method_callee(bcx, callee_id, lhs, origin)
1566-
}, [rhs], ex.id, dest);
1566+
}, arg_exprs([rhs]), dest);
15671567
}
15681568
_ {}
15691569
}
@@ -2498,19 +2498,23 @@ fn trans_arg_expr(cx: block, arg: ty::arg, lldestty: TypeRef, e: @ast::expr,
24982498
ret rslt(bcx, val);
24992499
}
25002500

2501+
enum call_args {
2502+
arg_exprs([@ast::expr]),
2503+
arg_vals([ValueRef])
2504+
}
25012505

25022506
// NB: must keep 4 fns in sync:
25032507
//
25042508
// - type_of_fn
25052509
// - create_llargs_for_fn_args.
25062510
// - new_fn_ctxt
25072511
// - trans_args
2508-
fn trans_args(cx: block, llenv: ValueRef, es: [@ast::expr], fn_ty: ty::t,
2512+
fn trans_args(cx: block, llenv: ValueRef, args: call_args, fn_ty: ty::t,
25092513
dest: dest, generic_intrinsic: bool)
25102514
-> {bcx: block, args: [ValueRef], retslot: ValueRef} {
25112515

25122516
let temp_cleanups = [];
2513-
let args = ty::ty_fn_args(fn_ty);
2517+
let arg_tys = ty::ty_fn_args(fn_ty);
25142518
let llargs: [ValueRef] = [];
25152519

25162520
let ccx = cx.ccx();
@@ -2546,13 +2550,21 @@ fn trans_args(cx: block, llenv: ValueRef, es: [@ast::expr], fn_ty: ty::t,
25462550
// First we figure out the caller's view of the types of the arguments.
25472551
// This will be needed if this is a generic call, because the callee has
25482552
// to cast her view of the arguments to the caller's view.
2549-
let arg_tys = type_of_explicit_args(ccx, args);
2550-
let i = 0u;
2551-
for e: @ast::expr in es {
2552-
let r = trans_arg_expr(bcx, args[i], arg_tys[i], e, temp_cleanups);
2553-
bcx = r.bcx;
2554-
llargs += [r.val];
2555-
i += 1u;
2553+
alt args {
2554+
arg_exprs(es) {
2555+
let llarg_tys = type_of_explicit_args(ccx, arg_tys);
2556+
let i = 0u;
2557+
for e: @ast::expr in es {
2558+
let r = trans_arg_expr(bcx, arg_tys[i], llarg_tys[i],
2559+
e, temp_cleanups);
2560+
bcx = r.bcx;
2561+
llargs += [r.val];
2562+
i += 1u;
2563+
}
2564+
}
2565+
arg_vals(vs) {
2566+
llargs += vs;
2567+
}
25562568
}
25572569

25582570
// now that all arguments have been successfully built, we can revoke any
@@ -2568,15 +2580,15 @@ fn trans_args(cx: block, llenv: ValueRef, es: [@ast::expr], fn_ty: ty::t,
25682580
}
25692581

25702582
fn trans_call(in_cx: block, f: @ast::expr,
2571-
args: [@ast::expr], id: ast::node_id, dest: dest)
2583+
args: call_args, id: ast::node_id, dest: dest)
25722584
-> block {
2573-
trans_call_inner(in_cx, expr_ty(in_cx, f),
2574-
{|cx| trans_callee(cx, f)}, args, id, dest)
2585+
trans_call_inner(in_cx, expr_ty(in_cx, f), node_id_type(in_cx, id),
2586+
{|cx| trans_callee(cx, f)}, args, dest)
25752587
}
25762588

2577-
fn trans_call_inner(in_cx: block, fn_expr_ty: ty::t,
2589+
fn trans_call_inner(in_cx: block, fn_expr_ty: ty::t, ret_ty: ty::t,
25782590
get_callee: fn(block) -> lval_maybe_callee,
2579-
args: [@ast::expr], id: ast::node_id, dest: dest)
2591+
args: call_args, dest: dest)
25802592
-> block {
25812593
with_scope(in_cx, "call") {|cx|
25822594
let f_res = get_callee(cx);
@@ -2603,9 +2615,10 @@ fn trans_call_inner(in_cx: block, fn_expr_ty: ty::t,
26032615
}
26042616
};
26052617

2606-
let ret_ty = node_id_type(bcx, id);
2607-
let args_res = trans_args(bcx, llenv, args, fn_expr_ty, dest,
2608-
option::is_some(f_res.tds));
2618+
let args_res = {
2619+
trans_args(bcx, llenv, args, fn_expr_ty, dest,
2620+
option::is_some(f_res.tds))
2621+
};
26092622
bcx = args_res.bcx;
26102623
let llargs = args_res.args;
26112624
option::may(f_res.tds) {|vals|
@@ -2923,7 +2936,7 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
29232936
}
29242937
ast::expr_cast(val, _) { ret trans_cast(bcx, val, e.id, dest); }
29252938
ast::expr_call(f, args, _) {
2926-
ret trans_call(bcx, f, args, e.id, dest);
2939+
ret trans_call(bcx, f, arg_exprs(args), e.id, dest);
29272940
}
29282941
ast::expr_field(base, _, _) {
29292942
if dest == ignore { ret trans_expr(bcx, base, ignore); }
@@ -2937,9 +2950,9 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
29372950
let origin = bcx.ccx().maps.method_map.get(e.id);
29382951
let callee_id = ast_util::op_expr_callee_id(e);
29392952
let fty = node_id_type(bcx, callee_id);
2940-
ret trans_call_inner(bcx, fty, {|bcx|
2953+
ret trans_call_inner(bcx, fty, expr_ty(bcx, e), {|bcx|
29412954
impl::trans_method_callee(bcx, callee_id, base, origin)
2942-
}, [idx], e.id, dest);
2955+
}, arg_exprs([idx]), dest);
29432956
}
29442957

29452958
// These return nothing
@@ -3037,6 +3050,45 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
30373050
assert dest == ignore;
30383051
ret trans_assign_op(bcx, e, op, dst, src);
30393052
}
3053+
ast::expr_new(pool, alloc_id, val) {
3054+
// First, call pool->alloc(sz, align) to get back a void*. Then, cast
3055+
// this memory to the required type and evaluate value into it.
3056+
let ccx = bcx.ccx();
3057+
3058+
// Allocate space for the ptr that will be returned from
3059+
// `pool.alloc()`:
3060+
let ptr_ty = expr_ty(bcx, e);
3061+
let {bcx, val: ptr_ptr_val} = alloc_ty(bcx, ptr_ty);
3062+
3063+
#debug["ptr_ty = %s", ty_to_str(tcx, ptr_ty)];
3064+
#debug["ptr_ptr_val = %s", val_str(ccx.tn, ptr_ptr_val)];
3065+
3066+
let void_ty = ty::mk_ptr(tcx, {ty: ty::mk_nil(tcx),
3067+
mutbl: ast::m_imm});
3068+
let voidval = {
3069+
let llvoid_ty = type_of(ccx, void_ty);
3070+
PointerCast(bcx, ptr_ptr_val, T_ptr(llvoid_ty))
3071+
};
3072+
3073+
#debug["voidval = %s", val_str(ccx.tn, voidval)];
3074+
3075+
let llval_ty = type_of(ccx, expr_ty(bcx, val));
3076+
let args = [llsize_of(ccx, llval_ty), llalign_of(ccx, llval_ty)];
3077+
let origin = bcx.ccx().maps.method_map.get(alloc_id);
3078+
let bcx = trans_call_inner(
3079+
bcx,
3080+
node_id_type(bcx, alloc_id),
3081+
void_ty,
3082+
{|bcx| impl::trans_method_callee(bcx, alloc_id, pool, origin) },
3083+
arg_vals(args),
3084+
save_in(voidval));
3085+
3086+
#debug["dest = %s", dest_str(ccx, dest)];
3087+
let ptr_val = Load(bcx, ptr_ptr_val);
3088+
#debug["ptr_val = %s", val_str(ccx.tn, ptr_val)];
3089+
let bcx = trans_expr(bcx, val, save_in(ptr_val));
3090+
store_in_dest(bcx, ptr_val, dest)
3091+
}
30403092
_ { bcx.tcx().sess.span_bug(e.span, "trans_expr reached \
30413093
fall-through case"); }
30423094

src/rustc/middle/trans/native.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,27 +111,28 @@ fn classify_ty(ty: TypeRef) -> [x86_64_reg_class] {
111111
}
112112
}
113113

114-
fn unify(cls: [mut x86_64_reg_class], i: uint,
115-
new: x86_64_reg_class) {
116-
if cls[i] == new {
114+
fn unify(cls: [mut x86_64_reg_class],
115+
i: uint,
116+
newv: x86_64_reg_class) {
117+
if cls[i] == newv {
117118
ret;
118119
} else if cls[i] == no_class {
119-
cls[i] = new;
120-
} else if new == no_class {
120+
cls[i] = newv;
121+
} else if newv == no_class {
121122
ret;
122-
} else if cls[i] == memory_class || new == memory_class {
123+
} else if cls[i] == memory_class || newv == memory_class {
123124
cls[i] = memory_class;
124-
} else if cls[i] == integer_class || new == integer_class {
125+
} else if cls[i] == integer_class || newv == integer_class {
125126
cls[i] = integer_class;
126127
} else if cls[i] == x87_class ||
127128
cls[i] == x87up_class ||
128129
cls[i] == complex_x87_class ||
129-
new == x87_class ||
130-
new == x87up_class ||
131-
new == complex_x87_class {
130+
newv == x87_class ||
131+
newv == x87up_class ||
132+
newv == complex_x87_class {
132133
cls[i] = memory_class;
133134
} else {
134-
cls[i] = new;
135+
cls[i] = newv;
135136
}
136137
}
137138

src/rustc/middle/trans/type_use.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ fn mark_for_expr(cx: ctx, e: @expr) {
168168
expr_log(_, _, val) {
169169
node_type_needs(cx, use_tydesc, val.id);
170170
}
171+
expr_new(_, _, v) {
172+
node_type_needs(cx, use_repr, v.id);
173+
}
171174
expr_for(_, _, _) | expr_do_while(_, _) | expr_alt(_, _, _) |
172175
expr_block(_) | expr_if(_, _, _) | expr_while(_, _) |
173176
expr_fail(_) | expr_break | expr_cont | expr_unary(_, _) |

src/rustc/middle/tstate/ann.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ fn set_poststate(a: ts_ann, p: poststate) -> bool {
183183

184184

185185
// Set all the bits in p that are set in new
186-
fn extend_prestate(p: prestate, new: poststate) -> bool {
187-
ret tritv_union(p, new);
186+
fn extend_prestate(p: prestate, newv: poststate) -> bool {
187+
ret tritv_union(p, newv);
188188
}
189189

190190

191191
// Set all the bits in p that are set in new
192-
fn extend_poststate(p: poststate, new: poststate) -> bool {
193-
ret tritv_union(p, new);
192+
fn extend_poststate(p: poststate, newv: poststate) -> bool {
193+
ret tritv_union(p, newv);
194194
}
195195

196196
// Sets the given bit in p to "don't care"

0 commit comments

Comments
 (0)