Skip to content

Commit 168398b

Browse files
committed
Stop generating generic versions of generic functions
Monomorphic instances are generated on demand.
1 parent 47e6540 commit 168398b

File tree

5 files changed

+61
-62
lines changed

5 files changed

+61
-62
lines changed

src/rustc/metadata/encoder.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ fn encode_enum_variant_info(ecx: @encode_ctxt, ebml_w: ebml::writer,
263263
encode_enum_id(ebml_w, local_def(id));
264264
encode_type(ecx, ebml_w,
265265
node_id_to_type(ecx.ccx.tcx, variant.node.id));
266-
if vec::len::<variant_arg>(variant.node.args) > 0u {
266+
if vec::len(variant.node.args) > 0u && ty_params.len() == 0u {
267267
encode_symbol(ecx, ebml_w, variant.node.id);
268268
}
269269
encode_discriminant(ecx, ebml_w, variant.node.id);
@@ -361,10 +361,11 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
361361
encode_family(ebml_w, purity_fn_family(decl.purity));
362362
encode_type_param_bounds(ebml_w, ecx, tps);
363363
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
364-
encode_symbol(ecx, ebml_w, item.id);
365364
encode_path(ebml_w, path, ast_map::path_name(item.ident));
366365
if tps.len() > 0u || should_inline(item.attrs) {
367366
astencode::encode_inlined_item(ecx, ebml_w, path, ii_item(item));
367+
} else {
368+
encode_symbol(ecx, ebml_w, item.id);
368369
}
369370
ebml_w.end_tag();
370371
}
@@ -416,7 +417,9 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
416417
encode_type_param_bounds(ebml_w, ecx, tps);
417418
encode_type(ecx, ebml_w, ty::ty_fn_ret(fn_ty));
418419
encode_name(ebml_w, item.ident);
419-
encode_symbol(ecx, ebml_w, item.id);
420+
if tps.len() == 0u {
421+
encode_symbol(ecx, ebml_w, item.id);
422+
}
420423
encode_path(ebml_w, path, ast_map::path_name(item.ident));
421424
ebml_w.end_tag();
422425

@@ -466,12 +469,13 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
466469
encode_type_param_bounds(ebml_w, ecx, tps + m.tps);
467470
encode_type(ecx, ebml_w, node_id_to_type(tcx, m.id));
468471
encode_name(ebml_w, m.ident);
469-
encode_symbol(ecx, ebml_w, m.id);
470472
encode_path(ebml_w, impl_path, ast_map::path_name(m.ident));
471473
if tps.len() > 0u || m.tps.len() > 0u || should_inline(m.attrs) {
472474
astencode::encode_inlined_item(
473475
ecx, ebml_w, impl_path,
474476
ii_method(local_def(item.id), m));
477+
} else {
478+
encode_symbol(ecx, ebml_w, m.id);
475479
}
476480
ebml_w.end_tag();
477481
}

src/rustc/middle/trans/base.rs

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,29 +2153,29 @@ fn monomorphic_fn(ccx: @crate_ctxt, fn_id: ast::def_id, substs: [ty::t],
21532153
alt check map_node {
21542154
ast_map::node_item(i@@{node: ast::item_fn(decl, _, body), _}, _) {
21552155
set_inline_hint_if_appr(i.attrs, lldecl);
2156-
trans_fn(ccx, pt, decl, body, lldecl, no_self, [],
2157-
psubsts, fn_id.node, none);
2156+
trans_fn(ccx, pt, decl, body, lldecl, no_self, psubsts, fn_id.node,
2157+
none);
21582158
}
21592159
ast_map::node_variant(v, enum_item, _) {
21602160
let tvs = ty::enum_variants(ccx.tcx, local_def(enum_item.id));
21612161
let this_tv = option::get(vec::find(*tvs, {|tv|
21622162
tv.id.node == fn_id.node}));
21632163
set_inline_hint(lldecl);
21642164
trans_enum_variant(ccx, enum_item.id, v, this_tv.disr_val,
2165-
(*tvs).len() == 1u, [], psubsts, lldecl);
2165+
(*tvs).len() == 1u, psubsts, lldecl);
21662166
}
21672167
ast_map::node_method(mth, impl_def_id, _) {
21682168
set_inline_hint_if_appr(mth.attrs, lldecl);
21692169
let selfty = ty::node_id_to_type(ccx.tcx, mth.self_id);
21702170
let selfty = ty::substitute_type_params(ccx.tcx, substs, selfty);
21712171
trans_fn(ccx, pt, mth.decl, mth.body, lldecl,
2172-
impl_self(selfty), [], psubsts, fn_id.node, none);
2172+
impl_self(selfty), psubsts, fn_id.node, none);
21732173
}
21742174
ast_map::node_ctor(i) {
21752175
alt check ccx.tcx.items.get(i.id) {
21762176
ast_map::node_item(@{node: ast::item_res(decl, _, _, _, _), _}, _) {
21772177
set_inline_hint(lldecl);
2178-
trans_res_ctor(ccx, pt, decl, fn_id.node, [], psubsts, lldecl);
2178+
trans_res_ctor(ccx, pt, decl, fn_id.node, psubsts, lldecl);
21792179
}
21802180
ast_map::node_item(@{node: ast::item_class(_, _, ctor), _}, _) {
21812181
ccx.sess.unimpl("monomorphic class constructor");
@@ -2216,10 +2216,11 @@ fn maybe_instantiate_inline(ccx: @crate_ctxt, fn_id: ast::def_id)
22162216
mth.id];
22172217
ccx.external.insert(fn_id, some(mth.id));
22182218
compute_ii_method_info(ccx, impl_did, mth) {|ty, bounds, path|
2219-
let llfn = get_item_val(ccx, mth.id);
2220-
trans_fn(ccx, path, mth.decl, mth.body,
2221-
llfn, impl_self(ty), bounds,
2222-
none, mth.id, none);
2219+
if bounds.len() == 0u {
2220+
let llfn = get_item_val(ccx, mth.id);
2221+
trans_fn(ccx, path, mth.decl, mth.body,
2222+
llfn, impl_self(ty), none, mth.id, none);
2223+
}
22232224
}
22242225
local_def(mth.id)
22252226
}
@@ -4116,7 +4117,6 @@ enum self_arg { impl_self(ty::t), no_self, }
41164117
fn trans_closure(ccx: @crate_ctxt, path: path, decl: ast::fn_decl,
41174118
body: ast::blk, llfndecl: ValueRef,
41184119
ty_self: self_arg,
4119-
tps_bounds: [ty::param_bounds],
41204120
param_substs: option<param_substs>,
41214121
id: ast::node_id, maybe_self_id: option<@ast::expr>,
41224122
maybe_load_env: fn(fn_ctxt)) {
@@ -4125,7 +4125,7 @@ fn trans_closure(ccx: @crate_ctxt, path: path, decl: ast::fn_decl,
41254125
// Set up arguments to the function.
41264126
let fcx = new_fn_ctxt_w_id(ccx, path, llfndecl, id, maybe_self_id,
41274127
param_substs, some(body.span));
4128-
create_llargs_for_fn_args(fcx, ty_self, decl.inputs, tps_bounds);
4128+
create_llargs_for_fn_args(fcx, ty_self, decl.inputs, []);
41294129

41304130
// Create the first basic block in the function and keep a handle on it to
41314131
// pass to finish_fn later.
@@ -4168,15 +4168,14 @@ fn trans_fn(ccx: @crate_ctxt,
41684168
body: ast::blk,
41694169
llfndecl: ValueRef,
41704170
ty_self: self_arg,
4171-
tps_bounds: [ty::param_bounds],
41724171
param_substs: option<param_substs>,
41734172
id: ast::node_id,
41744173
maybe_self_id: option<@ast::expr>) {
41754174
let do_time = ccx.sess.opts.stats;
41764175
let start = if do_time { time::get_time() }
41774176
else { {sec: 0u32, usec: 0u32} };
41784177
trans_closure(ccx, path, decl, body, llfndecl, ty_self,
4179-
tps_bounds, param_substs, id, maybe_self_id, {|fcx|
4178+
param_substs, id, maybe_self_id, {|fcx|
41804179
if ccx.sess.opts.extra_debuginfo {
41814180
debuginfo::create_function(fcx);
41824181
}
@@ -4188,12 +4187,12 @@ fn trans_fn(ccx: @crate_ctxt,
41884187
}
41894188

41904189
fn trans_res_ctor(ccx: @crate_ctxt, path: path, dtor: ast::fn_decl,
4191-
ctor_id: ast::node_id, tps_bounds: [ty::param_bounds],
4190+
ctor_id: ast::node_id,
41924191
param_substs: option<param_substs>, llfndecl: ValueRef) {
41934192
// Create a function for the constructor
41944193
let fcx = new_fn_ctxt_w_id(ccx, path, llfndecl, ctor_id,
41954194
none, param_substs, none);
4196-
create_llargs_for_fn_args(fcx, no_self, dtor.inputs, tps_bounds);
4195+
create_llargs_for_fn_args(fcx, no_self, dtor.inputs, []);
41974196
let bcx = top_scope_block(fcx, none), lltop = bcx.llbb;
41984197
let fty = node_id_type(bcx, ctor_id);
41994198
let arg_t = ty::ty_fn_args(fty)[0].ty;
@@ -4223,7 +4222,6 @@ fn trans_res_ctor(ccx: @crate_ctxt, path: path, dtor: ast::fn_decl,
42234222

42244223
fn trans_enum_variant(ccx: @crate_ctxt, enum_id: ast::node_id,
42254224
variant: ast::variant, disr: int, is_degen: bool,
4226-
ty_params: [ast::ty_param],
42274225
param_substs: option<param_substs>,
42284226
llfndecl: ValueRef) {
42294227
// Translate variant arguments to function arguments.
@@ -4236,17 +4234,10 @@ fn trans_enum_variant(ccx: @crate_ctxt, enum_id: ast::node_id,
42364234
}
42374235
let fcx = new_fn_ctxt_w_id(ccx, [], llfndecl, variant.node.id, none,
42384236
param_substs, none);
4239-
create_llargs_for_fn_args(fcx, no_self, fn_args,
4240-
param_bounds(ccx, ty_params));
4237+
create_llargs_for_fn_args(fcx, no_self, fn_args, []);
42414238
let ty_param_substs = alt param_substs {
42424239
some(substs) { substs.tys }
4243-
none {
4244-
let i = 0u;
4245-
vec::map(ty_params, {|tp|
4246-
i += 1u;
4247-
ty::mk_param(ccx.tcx, i - 1u, local_def(tp.id))
4248-
})
4249-
}
4240+
none { [] }
42504241
};
42514242
let bcx = top_scope_block(fcx, none), lltop = bcx.llbb;
42524243
let arg_tys = ty::ty_fn_args(node_id_type(bcx, variant.node.id));
@@ -4400,44 +4391,46 @@ fn trans_item(ccx: @crate_ctxt, item: ast::item) {
44004391
};
44014392
alt item.node {
44024393
ast::item_fn(decl, tps, body) {
4403-
let llfndecl = get_item_val(ccx, item.id);
4404-
if decl.purity != ast::crust_fn {
4405-
trans_fn(ccx, *path + [path_name(item.ident)], decl, body,
4406-
llfndecl, no_self, param_bounds(ccx, tps),
4407-
none, item.id, none);
4408-
} else {
4394+
if decl.purity == ast::crust_fn {
4395+
let llfndecl = get_item_val(ccx, item.id);
44094396
native::trans_crust_fn(ccx, *path + [path_name(item.ident)],
44104397
decl, body, llfndecl, item.id);
4398+
} else if tps.len() == 0u {
4399+
let llfndecl = get_item_val(ccx, item.id);
4400+
trans_fn(ccx, *path + [path_name(item.ident)], decl, body,
4401+
llfndecl, no_self, none, item.id, none);
44114402
}
44124403
}
44134404
ast::item_impl(tps, _, _, ms) {
44144405
impl::trans_impl(ccx, *path, item.ident, ms, tps);
44154406
}
44164407
ast::item_res(decl, tps, body, dtor_id, ctor_id) {
4417-
let llctor_decl = get_item_val(ccx, ctor_id);
4418-
trans_res_ctor(ccx, *path, decl, ctor_id,
4419-
param_bounds(ccx, tps), none, llctor_decl);
4408+
if tps.len() == 0u {
4409+
let llctor_decl = get_item_val(ccx, ctor_id);
4410+
trans_res_ctor(ccx, *path, decl, ctor_id, none, llctor_decl);
44204411

4421-
// Create a function for the destructor
4422-
let lldtor_decl = get_item_val(ccx, item.id);
4423-
trans_fn(ccx, *path + [path_name(item.ident)], decl, body,
4424-
lldtor_decl, no_self, param_bounds(ccx, tps),
4425-
none, dtor_id, none);
4412+
let lldtor_decl = get_item_val(ccx, item.id);
4413+
trans_fn(ccx, *path + [path_name(item.ident)], decl, body,
4414+
lldtor_decl, no_self, none, dtor_id, none);
4415+
}
44264416
}
44274417
ast::item_mod(m) {
44284418
trans_mod(ccx, m);
44294419
}
44304420
ast::item_enum(variants, tps) {
4431-
let degen = variants.len() == 1u;
4432-
let vi = ty::enum_variants(ccx.tcx, local_def(item.id));
4433-
let i = 0;
4434-
for variant: ast::variant in variants {
4435-
if variant.node.args.len() > 0u {
4436-
trans_enum_variant(ccx, item.id, variant,
4437-
vi[i].disr_val, degen, tps,
4438-
none, get_item_val(ccx, variant.node.id));
4421+
if tps.len() == 0u {
4422+
let degen = variants.len() == 1u;
4423+
let vi = ty::enum_variants(ccx.tcx, local_def(item.id));
4424+
let i = 0;
4425+
for variant: ast::variant in variants {
4426+
let llfn = get_item_val(ccx, variant.node.id);
4427+
if variant.node.args.len() > 0u {
4428+
trans_enum_variant(ccx, item.id, variant,
4429+
vi[i].disr_val, degen,
4430+
none, llfn);
4431+
}
4432+
i += 1;
44394433
}
4440-
i += 1;
44414434
}
44424435
}
44434436
ast::item_const(_, expr) { trans_const(ccx, expr, item.id); }
@@ -4516,8 +4509,7 @@ fn trans_item(ccx: @crate_ctxt, item: ast::item) {
45164509
with ctor.node.body};
45174510
trans_fn(ccx, *path + [path_name(item.ident)], ctor.node.dec,
45184511
ctor_body__, llctor_decl, no_self,
4519-
param_bounds(ccx, tps), none, ctor.node.id,
4520-
some(rslt_expr));
4512+
none, ctor.node.id, some(rslt_expr));
45214513
// TODO: translate methods!
45224514
}
45234515
_ {/* fall through */ }

src/rustc/middle/trans/closure.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ fn trans_expr_fn(bcx: block,
470470
let cap_vars = capture::compute_capture_vars(
471471
ccx.tcx, id, proto, cap_clause);
472472
let {llbox, cdata_ty, bcx} = build_closure(bcx, cap_vars, ck, id);
473-
trans_closure(ccx, sub_path, decl, body, llfn, no_self, [],
473+
trans_closure(ccx, sub_path, decl, body, llfn, no_self,
474474
bcx.fcx.param_substs, id, none, {|fcx|
475475
load_environment(bcx, fcx, cdata_ty, cap_vars, ck);
476476
});
@@ -482,7 +482,7 @@ fn trans_expr_fn(bcx: block,
482482
ast::proto_box { trans_closure_env(ty::ck_box) }
483483
ast::proto_uniq { trans_closure_env(ty::ck_uniq) }
484484
ast::proto_bare {
485-
trans_closure(ccx, sub_path, decl, body, llfn, no_self, [], none,
485+
trans_closure(ccx, sub_path, decl, body, llfn, no_self, none,
486486
id, none, {|_fcx|});
487487
C_null(T_opaque_box_ptr(ccx))
488488
}

src/rustc/middle/trans/impl.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ import std::map::hashmap;
4646

4747
fn trans_impl(ccx: @crate_ctxt, path: path, name: ast::ident,
4848
methods: [@ast::method], tps: [ast::ty_param]) {
49+
if tps.len() > 0u { ret; }
4950
let sub_path = path + [path_name(name)];
5051
for m in methods {
51-
let llfn = get_item_val(ccx, m.id);
52-
let m_bounds = param_bounds(ccx, tps + m.tps);
53-
trans_fn(ccx, sub_path + [path_name(m.ident)], m.decl, m.body,
54-
llfn, impl_self(ty::node_id_to_type(ccx.tcx, m.self_id)),
55-
m_bounds, none, m.id, none);
52+
if m.tps.len() == 0u {
53+
let llfn = get_item_val(ccx, m.id);
54+
trans_fn(ccx, sub_path + [path_name(m.ident)], m.decl, m.body,
55+
llfn, impl_self(ty::node_id_to_type(ccx.tcx, m.self_id)),
56+
none, m.id, none);
57+
}
5658
}
5759
}
5860

@@ -335,6 +337,7 @@ fn trans_impl_wrapper(ccx: @crate_ctxt, pt: path,
335337
fn trans_impl_vtable(ccx: @crate_ctxt, pt: path,
336338
iface_id: ast::def_id, ms: [@ast::method],
337339
tps: [ast::ty_param], it: @ast::item) {
340+
if tps.len() > 0u { ret; }
338341
let new_pt = pt + [path_name(it.ident), path_name(int::str(it.id)),
339342
path_name("wrap")];
340343
let extra_tps = param_bounds(ccx, tps);

src/rustc/middle/trans/native.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn trans_crust_fn(ccx: @crate_ctxt, path: ast_map::path, decl: ast::fn_decl,
271271
ccx, path + [ast_map::path_name("__rust_abi")]);
272272
let llty = type_of_fn_from_ty(ccx, t, []);
273273
let llfndecl = decl_internal_cdecl_fn(ccx.llmod, ps, llty);
274-
trans_fn(ccx, path, decl, body, llfndecl, no_self, [], none, id,
274+
trans_fn(ccx, path, decl, body, llfndecl, no_self, none, id,
275275
none);
276276
ret llfndecl;
277277
}

0 commit comments

Comments
 (0)