Skip to content

Commit 97b20f8

Browse files
committed
rustc: Make type_names and named_types tables in trans use @str instead of ~str
Cuts down on bad copies (though there are still a few that can go away once there are impls of push / append / + for @str)
1 parent 9c1476e commit 97b20f8

File tree

4 files changed

+61
-58
lines changed

4 files changed

+61
-58
lines changed

src/librustc/lib/llvm.rs

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,20 +1251,19 @@ fn SetLinkage(Global: ValueRef, Link: Linkage) {
12511251

12521252
/* Memory-managed object interface to type handles. */
12531253

1254-
type type_names = @{type_names: HashMap<TypeRef, ~str>,
1255-
named_types: HashMap<~str, TypeRef>};
1254+
type type_names = @{type_names: HashMap<TypeRef, @str>,
1255+
named_types: HashMap<@str, TypeRef>};
12561256

1257-
fn associate_type(tn: type_names, +s: ~str, t: TypeRef) {
1258-
// XXX: Bad copy, use @str instead?
1259-
assert tn.type_names.insert(t, copy s);
1257+
fn associate_type(tn: type_names, s: @str, t: TypeRef) {
1258+
assert tn.type_names.insert(t, s);
12601259
assert tn.named_types.insert(s, t);
12611260
}
12621261

1263-
fn type_has_name(tn: type_names, t: TypeRef) -> Option<~str> {
1262+
fn type_has_name(tn: type_names, t: TypeRef) -> Option<@str> {
12641263
return tn.type_names.find(t);
12651264
}
12661265

1267-
fn name_has_type(tn: type_names, +s: ~str) -> Option<TypeRef> {
1266+
fn name_has_type(tn: type_names, s: @str) -> Option<TypeRef> {
12681267
return tn.named_types.find(s);
12691268
}
12701269

@@ -1273,101 +1272,104 @@ fn mk_type_names() -> type_names {
12731272
named_types: HashMap()}
12741273
}
12751274

1276-
fn type_to_str(names: type_names, ty: TypeRef) -> ~str {
1275+
fn type_to_str(names: type_names, ty: TypeRef) -> @str {
12771276
return type_to_str_inner(names, ~[], ty);
12781277
}
12791278

12801279
fn type_to_str_inner(names: type_names, +outer0: ~[TypeRef], ty: TypeRef) ->
1281-
~str {
1280+
@str {
12821281
unsafe {
12831282
match type_has_name(names, ty) {
1284-
option::Some(ref n) => return (/*bad*/copy *n),
1283+
option::Some(n) => return n,
12851284
_ => {}
12861285
}
12871286

1288-
// XXX: Bad copy.
1287+
// FIXME #2543: Bad copy.
12891288
let outer = vec::append_one(copy outer0, ty);
12901289

12911290
let kind = llvm::LLVMGetTypeKind(ty);
12921291

12931292
fn tys_str(names: type_names, outer: ~[TypeRef],
1294-
tys: ~[TypeRef]) -> ~str {
1295-
let mut s: ~str = ~"";
1293+
tys: ~[TypeRef]) -> @str {
1294+
let mut s = ~"";
12961295
let mut first: bool = true;
12971296
for tys.each |t| {
12981297
if first { first = false; } else { s += ~", "; }
1299-
s += type_to_str_inner(names, outer, *t);
1298+
s += type_to_str_inner(names, outer, *t).to_owned();
13001299
}
1301-
return s;
1300+
// [Note at-str] FIXME #2543: Could rewrite this without the copy,
1301+
// but need better @str support.
1302+
return s.to_managed();
13021303
}
13031304
13041305
match kind {
1305-
Void => return ~"Void",
1306-
Half => return ~"Half",
1307-
Float => return ~"Float",
1308-
Double => return ~"Double",
1309-
X86_FP80 => return ~"X86_FP80",
1310-
FP128 => return ~"FP128",
1311-
PPC_FP128 => return ~"PPC_FP128",
1312-
Label => return ~"Label",
1306+
Void => return @"Void",
1307+
Half => return @"Half",
1308+
Float => return @"Float",
1309+
Double => return @"Double",
1310+
X86_FP80 => return @"X86_FP80",
1311+
FP128 => return @"FP128",
1312+
PPC_FP128 => return @"PPC_FP128",
1313+
Label => return @"Label",
13131314
Integer => {
1314-
return ~"i" + int::str(llvm::LLVMGetIntTypeWidth(ty) as int);
1315+
// See [Note at-str]
1316+
return fmt!("i%d", llvm::LLVMGetIntTypeWidth(ty)
1317+
as int).to_managed();
13151318
}
13161319
Function => {
1317-
let mut s = ~"fn(";
13181320
let out_ty: TypeRef = llvm::LLVMGetReturnType(ty);
13191321
let n_args = llvm::LLVMCountParamTypes(ty) as uint;
13201322
let args = vec::from_elem(n_args, 0 as TypeRef);
13211323
unsafe {
13221324
llvm::LLVMGetParamTypes(ty, vec::raw::to_ptr(args));
13231325
}
1324-
s += tys_str(names, outer, args);
1325-
s += ~") -> ";
1326-
s += type_to_str_inner(names, outer, out_ty);
1327-
return s;
1326+
// See [Note at-str]
1327+
return fmt!("fn(%s) -> %s",
1328+
tys_str(names, outer, args),
1329+
type_to_str_inner(names, outer, out_ty)).to_managed();
13281330
}
13291331
Struct => {
1330-
let mut s: ~str = ~"{";
13311332
let n_elts = llvm::LLVMCountStructElementTypes(ty) as uint;
13321333
let mut elts = vec::from_elem(n_elts, 0 as TypeRef);
1333-
if elts.len() > 0 {
1334+
if elts.is_not_empty() {
13341335
llvm::LLVMGetStructElementTypes(
13351336
ty, ptr::to_mut_unsafe_ptr(&mut elts[0]));
13361337
}
1337-
s += tys_str(names, outer, elts);
1338-
s += ~"}";
1339-
return s;
1338+
// See [Note at-str]
1339+
return fmt!("{%s}", tys_str(names, outer, elts)).to_managed();
13401340
}
13411341
Array => {
13421342
let el_ty = llvm::LLVMGetElementType(ty);
1343-
return ~"[" + type_to_str_inner(names, outer, el_ty) + ~" x " +
1344-
uint::str(llvm::LLVMGetArrayLength(ty) as uint) + ~"]";
1343+
// See [Note at-str]
1344+
return fmt!("[%s@ x %u", type_to_str_inner(names, outer, el_ty),
1345+
llvm::LLVMGetArrayLength(ty) as uint).to_managed();
13451346
}
13461347
Pointer => {
1347-
let mut i: uint = 0u;
1348+
let mut i = 0;
13481349
for outer0.each |tout| {
1349-
i += 1u;
1350+
i += 1;
13501351
if *tout as int == ty as int {
1351-
let n: uint = vec::len::<TypeRef>(outer0) - i;
1352-
return ~"*\\" + int::str(n as int);
1352+
let n = outer0.len() - i;
1353+
// See [Note at-str]
1354+
return fmt!("*\\%d", n as int).to_managed();
13531355
}
13541356
}
13551357
let addrstr = {
13561358
let addrspace = llvm::LLVMGetPointerAddressSpace(ty) as uint;
1357-
if addrspace == 0u {
1359+
if addrspace == 0 {
13581360
~""
13591361
} else {
13601362
fmt!("addrspace(%u)", addrspace)
13611363
}
13621364
};
1363-
return addrstr + ~"*" +
1364-
type_to_str_inner(names,
1365-
outer,
1366-
llvm::LLVMGetElementType(ty));
1365+
// See [Note at-str]
1366+
return fmt!("%s*%s", addrstr, type_to_str_inner(names,
1367+
outer,
1368+
llvm::LLVMGetElementType(ty))).to_managed();
13671369
}
1368-
Vector => return ~"Vector",
1369-
Metadata => return ~"Metadata",
1370-
X86_MMX => return ~"X86_MMAX"
1370+
Vector => return @"Vector",
1371+
Metadata => return @"Metadata",
1372+
X86_MMX => return @"X86_MMAX"
13711373
}
13721374
}
13731375
}

src/librustc/middle/trans/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,9 +2979,9 @@ fn trans_crate(sess: session::Session,
29792979
let float_type = T_float(targ_cfg);
29802980
let task_type = T_task(targ_cfg);
29812981
let taskptr_type = T_ptr(task_type);
2982-
lib::llvm::associate_type(tn, ~"taskptr", taskptr_type);
2982+
lib::llvm::associate_type(tn, @"taskptr", taskptr_type);
29832983
let tydesc_type = T_tydesc(targ_cfg);
2984-
lib::llvm::associate_type(tn, ~"tydesc", tydesc_type);
2984+
lib::llvm::associate_type(tn, @"tydesc", tydesc_type);
29852985
let crate_map = decl_crate_map(sess, link_meta, llmod);
29862986
let dbg_cx = if sess.opts.debuginfo {
29872987
Some(debuginfo::mk_ctxt(copy llmod_id, sess.parse_sess.interner))

src/librustc/middle/trans/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ fn Invoke(cx: block, Fn: ValueRef, Args: ~[ValueRef],
188188
terminate(cx, "Invoke");
189189
debug!("Invoke(%s with arguments (%s))",
190190
val_str(cx.ccx().tn, Fn),
191-
str::connect(vec::map(Args, |a| val_str(cx.ccx().tn, *a)),
191+
str::connect(vec::map(Args, |a| val_str(cx.ccx().tn,
192+
*a).to_owned()),
192193
~", "));
193194
unsafe {
194195
count_insn(cx, "invoke");

src/librustc/middle/trans/common.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ impl Result {
645645
}
646646
}
647647

648-
fn ty_str(tn: type_names, t: TypeRef) -> ~str {
648+
fn ty_str(tn: type_names, t: TypeRef) -> @str {
649649
return lib::llvm::type_to_str(tn, t);
650650
}
651651

@@ -655,7 +655,7 @@ fn val_ty(v: ValueRef) -> TypeRef {
655655
}
656656
}
657657

658-
fn val_str(tn: type_names, v: ValueRef) -> ~str {
658+
fn val_str(tn: type_names, v: ValueRef) -> @str {
659659
return ty_str(tn, val_ty(v));
660660
}
661661

@@ -729,11 +729,11 @@ impl block {
729729
}
730730
}
731731

732-
fn val_str(val: ValueRef) -> ~str {
732+
fn val_str(val: ValueRef) -> @str {
733733
val_str(self.ccx().tn, val)
734734
}
735735

736-
fn llty_str(llty: TypeRef) -> ~str {
736+
fn llty_str(llty: TypeRef) -> @str {
737737
ty_str(self.ccx().tn, llty)
738738
}
739739

@@ -924,7 +924,7 @@ fn T_tydesc_field(cx: @crate_ctxt, field: uint) -> TypeRef unsafe {
924924
}
925925

926926
fn T_generic_glue_fn(cx: @crate_ctxt) -> TypeRef {
927-
let s = ~"glue_fn";
927+
let s = @"glue_fn";
928928
match name_has_type(cx.tn, s) {
929929
Some(t) => return t,
930930
_ => ()
@@ -1038,7 +1038,7 @@ fn T_taskptr(cx: @crate_ctxt) -> TypeRef { return T_ptr(cx.task_type); }
10381038

10391039
// This type must never be used directly; it must always be cast away.
10401040
fn T_typaram(tn: type_names) -> TypeRef {
1041-
let s = ~"typaram";
1041+
let s = @"typaram";
10421042
match name_has_type(tn, s) {
10431043
Some(t) => return t,
10441044
_ => ()
@@ -1061,7 +1061,7 @@ fn T_enum_discrim(cx: @crate_ctxt) -> TypeRef {
10611061
}
10621062

10631063
fn T_opaque_enum(cx: @crate_ctxt) -> TypeRef {
1064-
let s = ~"opaque_enum";
1064+
let s = @"opaque_enum";
10651065
match name_has_type(cx.tn, s) {
10661066
Some(t) => return t,
10671067
_ => ()

0 commit comments

Comments
 (0)