Skip to content

Commit 9292744

Browse files
committed
Use the same type of record in ty::ty_fn and ty::method
Removes some more code duplication.
1 parent 970f5cc commit 9292744

File tree

15 files changed

+249
-357
lines changed

15 files changed

+249
-357
lines changed

src/comp/metadata/decoder.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,10 @@ fn get_tag_variants(_data: @[u8], def: ast::def_id, tcx: ty::ctxt,
253253
let ctor_ty = item_type(item, external_crate_id, tcx, extres);
254254
let arg_tys: [ty::t] = [];
255255
alt ty::struct(tcx, ctor_ty) {
256-
ty::ty_fn(_, args, _, _, _) {
257-
for a: ty::arg in args { arg_tys += [a.ty]; }
258-
}
259-
_ {
260-
// Nullary tag variant.
261-
256+
ty::ty_fn(f) {
257+
for a: ty::arg in f.inputs { arg_tys += [a.ty]; }
262258
}
259+
_ { /* Nullary tag variant. */ }
263260
}
264261
infos += [@{args: arg_tys, ctor_ty: ctor_ty, id: did}];
265262
}

src/comp/metadata/tydecode.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,7 @@ fn parse_constr<copy T>(st: @pstate, sd: str_def, pser: arg_parser<T>) ->
165165
}
166166

167167
fn parse_ty_rust_fn(st: @pstate, sd: str_def, p: ast::proto) -> ty::t {
168-
let func = parse_ty_fn(st, sd);
169-
ret ty::mk_fn(st.tcx, p,
170-
func.args, func.ty, func.cf,
171-
func.cs);
168+
ret ty::mk_fn(st.tcx, {proto: p with parse_ty_fn(st, sd)});
172169
}
173170

174171
fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
@@ -256,7 +253,7 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
256253
}
257254
'N' {
258255
let func = parse_ty_fn(st, sd);
259-
ret ty::mk_native_fn(st.tcx, func.args, func.ty);
256+
ret ty::mk_native_fn(st.tcx, func.inputs, func.output);
260257
}
261258
'O' {
262259
assert (next(st) as char == '[');
@@ -270,14 +267,8 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
270267
while peek(st) as char != '[' {
271268
name += str::unsafe_from_byte(next(st));
272269
}
273-
let func = parse_ty_fn(st, sd);
274-
methods +=
275-
[{proto: proto,
276-
ident: name,
277-
inputs: func.args,
278-
output: func.ty,
279-
cf: func.cf,
280-
constrs: func.cs}];
270+
methods += [{ident: name,
271+
fty: {proto: proto with parse_ty_fn(st, sd)}}];
281272
}
282273
st.pos += 1u;
283274
ret ty::mk_obj(st.tcx, methods);
@@ -365,8 +356,7 @@ fn parse_hex(st: @pstate) -> uint {
365356
ret n;
366357
}
367358

368-
fn parse_ty_fn(st: @pstate, sd: str_def) ->
369-
{args: [ty::arg], ty: ty::t, cf: ast::ret_style, cs: [@ty::constr]} {
359+
fn parse_ty_fn(st: @pstate, sd: str_def) -> ty::fn_ty {
370360
assert (next(st) as char == '[');
371361
let inputs: [ty::arg] = [];
372362
while peek(st) as char != ']' {
@@ -383,7 +373,8 @@ fn parse_ty_fn(st: @pstate, sd: str_def) ->
383373
st.pos += 1u; // eat the ']'
384374
let cs = parse_constrs(st, sd);
385375
let (ret_style, ret_ty) = parse_ret_ty(st, sd);
386-
ret {args: inputs, ty: ret_ty, cf: ret_style, cs: cs};
376+
ret {proto: ast::proto_bare, inputs: inputs, output: ret_ty,
377+
ret_style: ret_style, constraints: cs};
387378
}
388379

389380

src/comp/metadata/tyencode.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,21 @@ fn enc_sty(w: io::writer, cx: @ctxt, st: ty::sty) {
143143
}
144144
w.write_char(']');
145145
}
146-
ty::ty_fn(proto, args, out, cf, constrs) {
147-
enc_proto(w, proto);
148-
enc_ty_fn(w, cx, args, out, cf, constrs);
146+
ty::ty_fn(f) {
147+
enc_proto(w, f.proto);
148+
enc_ty_fn(w, cx, f);
149149
}
150150
ty::ty_native_fn(args, out) {
151151
w.write_char('N');
152-
enc_ty_fn(w, cx, args, out, return_val, []);
152+
enc_ty_fn(w, cx, {proto: proto_bare, inputs: args, output: out,
153+
ret_style: return_val, constraints: []});
153154
}
154155
ty::ty_obj(methods) {
155156
w.write_str("O[");
156157
for m: ty::method in methods {
157-
enc_proto(w, m.proto);
158+
enc_proto(w, m.fty.proto);
158159
w.write_str(m.ident);
159-
enc_ty_fn(w, cx, m.inputs, m.output, m.cf, m.constrs);
160+
enc_ty_fn(w, cx, m.fty);
160161
}
161162
w.write_char(']');
162163
}
@@ -202,10 +203,9 @@ fn enc_proto(w: io::writer, proto: proto) {
202203
}
203204
}
204205

205-
fn enc_ty_fn(w: io::writer, cx: @ctxt, args: [ty::arg], out: ty::t,
206-
cf: ret_style, constrs: [@ty::constr]) {
206+
fn enc_ty_fn(w: io::writer, cx: @ctxt, ft: ty::fn_ty) {
207207
w.write_char('[');
208-
for arg: ty::arg in args {
208+
for arg: ty::arg in ft.inputs {
209209
alt arg.mode {
210210
by_mut_ref. { w.write_char('&'); }
211211
by_move. { w.write_char('-'); }
@@ -217,16 +217,16 @@ fn enc_ty_fn(w: io::writer, cx: @ctxt, args: [ty::arg], out: ty::t,
217217
}
218218
w.write_char(']');
219219
let colon = true;
220-
for c: @ty::constr in constrs {
220+
for c: @ty::constr in ft.constraints {
221221
if colon {
222222
w.write_char(':');
223223
colon = false;
224224
} else { w.write_char(';'); }
225225
enc_constr(w, cx, c);
226226
}
227-
alt cf {
227+
alt ft.ret_style {
228228
noreturn. { w.write_char('!'); }
229-
_ { enc_ty(w, cx, out); }
229+
_ { enc_ty(w, cx, ft.output); }
230230
}
231231
}
232232

src/comp/middle/alias.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,9 @@ fn ty_can_unsafely_include(cx: ctx, needle: unsafe_ty, haystack: ty::t,
518518
for t in ts { if helper(tcx, needle, t, mut) { ret true; } }
519519
ret false;
520520
}
521-
ty::ty_fn(ast::proto_bare., _, _, _, _) { ret false; }
521+
ty::ty_fn({proto: ast::proto_bare., _}) { ret false; }
522522
// These may contain anything.
523-
ty::ty_fn(_, _, _, _, _) | ty::ty_obj(_) { ret true; }
523+
ty::ty_fn(_) | ty::ty_obj(_) { ret true; }
524524
// A type param may include everything, but can only be
525525
// treated as opaque downstream, and is thus safe unless we
526526
// saw mutable fields, in which case the whole thing can be
@@ -558,7 +558,7 @@ fn copy_is_expensive(tcx: ty::ctxt, ty: ty::t) -> bool {
558558
ty::ty_ptr(_) { 1u }
559559
ty::ty_box(_) { 3u }
560560
ty::ty_constr(t, _) | ty::ty_res(_, t, _) { score_ty(tcx, t) }
561-
ty::ty_fn(_, _, _, _, _) | ty::ty_native_fn(_, _) |
561+
ty::ty_fn(_) | ty::ty_native_fn(_, _) |
562562
ty::ty_obj(_) { 4u }
563563
ty::ty_str. | ty::ty_vec(_) | ty::ty_param(_, _) { 50u }
564564
ty::ty_uniq(mt) { 1u + score_ty(tcx, mt.ty) }

src/comp/middle/block_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn check_crate(tcx: ty::ctxt, crate: @crate) {
1313
fn visit_expr(ex: @expr, cx: ctx, v: visit::vt<ctx>) {
1414
if !cx.allow_block {
1515
alt ty::struct(cx.tcx, ty::expr_ty(cx.tcx, ex)) {
16-
ty::ty_fn(proto_block., _, _, _, _) {
16+
ty::ty_fn({proto: proto_block., _}) {
1717
cx.tcx.sess.span_err(ex.span, "expressions with block type \
1818
can only appear in callee or (by-ref) argument position");
1919
}

src/comp/middle/fn_usage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn fn_usage_expr(expr: @ast::expr,
3232
if !ctx.generic_bare_fn_legal
3333
&& ty::expr_has_ty_params(ctx.tcx, expr) {
3434
alt ty::struct(ctx.tcx, ty::expr_ty(ctx.tcx, expr)) {
35-
ty::ty_fn(ast::proto_bare., _, _, _, _) {
35+
ty::ty_fn({proto: ast::proto_bare., _}) {
3636
ctx.tcx.sess.span_fatal(
3737
expr.span,
3838
"generic bare functions can only be called or bound");

src/comp/middle/gc.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,9 @@ fn type_is_gc_relevant(cx: ty::ctxt, ty: ty::t) -> bool {
145145
ret type_is_gc_relevant(cx, tm.ty);
146146
}
147147
ty::ty_constr(sub, _) { ret type_is_gc_relevant(cx, sub); }
148-
149-
150-
151-
152-
153-
ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_fn(_, _, _, _, _) |
148+
ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_fn(_) |
154149
ty::ty_native_fn(_, _) | ty::ty_obj(_) | ty::ty_param(_, _) |
155-
ty::ty_res(_, _, _) {
156-
ret true;
157-
}
158-
159-
160-
161-
162-
150+
ty::ty_res(_, _, _) { ret true; }
163151
ty::ty_var(_) {
164152
fail "ty_var in type_is_gc_relevant";
165153
}

src/comp/middle/shape.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ fn shape_of(ccx: @crate_ctxt, t: ty::t, ty_param_map: [uint],
416416
}
417417
}
418418
}
419-
ty::ty_fn(_, _, _, _, _) {
419+
ty::ty_fn(_) {
420420
s += [shape_fn];
421421
}
422422
ty::ty_opaque_closure. {

src/comp/middle/trans.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn type_of_inner(cx: @crate_ctxt, sp: span, t: ty::t)
168168
}
169169
T_struct(tys)
170170
}
171-
ty::ty_fn(_, _, _, _, _) {
171+
ty::ty_fn(_) {
172172
// FIXME: could be a constraint on ty_fn
173173
check returns_non_ty_var(cx, t);
174174
T_fn_pair(cx, type_of_fn_from_ty(cx, sp, t, 0u))
@@ -232,7 +232,7 @@ fn type_of_ty_param_kinds_and_ty(lcx: @local_ctxt, sp: span,
232232
let cx = lcx.ccx;
233233
let t = tpt.ty;
234234
alt ty::struct(cx.tcx, t) {
235-
ty::ty_fn(_, _, _, _, _) | ty::ty_native_fn(_, _) {
235+
ty::ty_fn(_) | ty::ty_native_fn(_, _) {
236236
check returns_non_ty_var(cx, t);
237237
ret type_of_fn_from_ty(cx, sp, t, vec::len(tpt.kinds));
238238
}
@@ -482,7 +482,7 @@ fn simplify_type(ccx: @crate_ctxt, typ: ty::t) -> ty::t {
482482
ty::ty_uniq(_) {
483483
ret ty::mk_imm_uniq(ccx.tcx, ty::mk_nil(ccx.tcx));
484484
}
485-
ty::ty_fn(_, _, _, _, _) {
485+
ty::ty_fn(_) {
486486
ret ty::mk_tup(ccx.tcx,
487487
[ty::mk_imm_box(ccx.tcx, ty::mk_nil(ccx.tcx)),
488488
ty::mk_imm_box(ccx.tcx, ty::mk_nil(ccx.tcx))]);
@@ -1321,7 +1321,7 @@ fn make_take_glue(cx: @block_ctxt, v: ValueRef, t: ty::t) {
13211321
Store(bcx, s, v);
13221322
bcx
13231323
}
1324-
ty::ty_native_fn(_, _) | ty::ty_fn(_, _, _, _, _) {
1324+
ty::ty_native_fn(_, _) | ty::ty_fn(_) {
13251325
trans_closure::make_fn_glue(bcx, v, t, take_ty)
13261326
}
13271327
ty::ty_opaque_closure. {
@@ -1396,7 +1396,7 @@ fn make_free_glue(bcx: @block_ctxt, v: ValueRef, t: ty::t) {
13961396
// they must be freed.
13971397
trans_shared_free(bcx, v)
13981398
}
1399-
ty::ty_native_fn(_, _) | ty::ty_fn(_, _, _, _, _) {
1399+
ty::ty_native_fn(_, _) | ty::ty_fn(_) {
14001400
trans_closure::make_fn_glue(bcx, v, t, free_ty)
14011401
}
14021402
ty::ty_opaque_closure. {
@@ -1425,7 +1425,7 @@ fn make_drop_glue(bcx: @block_ctxt, v0: ValueRef, t: ty::t) {
14251425
ty::ty_res(did, inner, tps) {
14261426
trans_res_drop(bcx, v0, did, inner, tps)
14271427
}
1428-
ty::ty_native_fn(_, _) | ty::ty_fn(_, _, _, _, _) {
1428+
ty::ty_native_fn(_, _) | ty::ty_fn(_) {
14291429
trans_closure::make_fn_glue(bcx, v0, t, drop_ty)
14301430
}
14311431
ty::ty_opaque_closure. {
@@ -1635,7 +1635,7 @@ fn iter_structural_ty(cx: @block_ctxt, av: ValueRef, t: ty::t,
16351635
let ccx = bcx_ccx(cx);
16361636
let cx = cx;
16371637
alt ty::struct(ccx.tcx, fn_ty) {
1638-
ty::ty_fn(_, args, _, _, _) {
1638+
ty::ty_fn({inputs: args, _}) {
16391639
let j = 0u;
16401640
let v_id = variant.id;
16411641
for a: ty::arg in args {
@@ -2762,7 +2762,7 @@ fn trans_object_field_inner(bcx: @block_ctxt, o: ValueRef,
27622762
vtbl = PointerCast(bcx, vtbl, vtbl_type);
27632763

27642764
let v = GEPi(bcx, vtbl, [0, ix as int]);
2765-
let fn_ty: ty::t = ty::method_ty_to_fn_ty(tcx, mths[ix]);
2765+
let fn_ty: ty::t = ty::mk_fn(tcx, mths[ix].fty);
27662766
let ret_ty = ty::ty_fn_ret(tcx, fn_ty);
27672767
// FIXME: constrain ty_obj?
27682768
check non_ty_var(ccx, ret_ty);
@@ -3558,7 +3558,7 @@ fn trans_expr(bcx: @block_ctxt, e: @ast::expr, dest: dest) -> @block_ctxt {
35583558
}
35593559
ast::expr_fn_block(decl, body) {
35603560
alt ty::struct(tcx, ty::expr_ty(tcx, e)) {
3561-
ty::ty_fn(proto, _, _, _, _) {
3561+
ty::ty_fn({proto, _}) {
35623562
let cap_clause = { copies: [], moves: [] };
35633563
ret trans_closure::trans_expr_fn(
35643564
bcx, decl, body, e.span, e.id, cap_clause, dest);
@@ -4443,7 +4443,7 @@ fn copy_args_to_allocas(fcx: @fn_ctxt, bcx: @block_ctxt, args: [ast::arg],
44434443

44444444
fn arg_tys_of_fn(ccx: @crate_ctxt, id: ast::node_id) -> [ty::arg] {
44454445
alt ty::struct(ccx.tcx, ty::node_id_to_type(ccx.tcx, id)) {
4446-
ty::ty_fn(_, arg_tys, _, _, _) { ret arg_tys; }
4446+
ty::ty_fn({inputs, _}) { inputs }
44474447
}
44484448
}
44494449

@@ -5107,7 +5107,7 @@ fn create_main_wrapper(ccx: @crate_ctxt, sp: span, main_llfn: ValueRef,
51075107

51085108
let main_takes_argv =
51095109
alt ty::struct(ccx.tcx, main_node_type) {
5110-
ty::ty_fn(_, args, _, _, _) { vec::len(args) != 0u }
5110+
ty::ty_fn({inputs, _}) { vec::len(inputs) != 0u }
51115111
};
51125112

51135113
let llfn = create_main(ccx, sp, main_llfn, main_takes_argv);

src/comp/middle/trans_closure.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,19 +516,19 @@ fn make_fn_glue(
516516
};
517517

518518
ret alt ty::struct(tcx, t) {
519-
ty::ty_native_fn(_, _) | ty::ty_fn(ast::proto_bare., _, _, _, _) {
519+
ty::ty_native_fn(_, _) | ty::ty_fn({proto: ast::proto_bare., _}) {
520520
bcx
521521
}
522-
ty::ty_fn(ast::proto_block., _, _, _, _) {
522+
ty::ty_fn({proto: ast::proto_block., _}) {
523523
bcx
524524
}
525-
ty::ty_fn(ast::proto_send., _, _, _, _) {
525+
ty::ty_fn({proto: ast::proto_send., _}) {
526526
fn_env({ |bcx, box_cell_v|
527527
let box_ty = trans_closure::send_opaque_closure_box_ty(tcx);
528528
glue_fn(bcx, box_cell_v, box_ty)
529529
})
530530
}
531-
ty::ty_fn(ast::proto_shared(_), _, _, _, _) {
531+
ty::ty_fn({proto: ast::proto_shared(_), _}) {
532532
fn_env({ |bcx, box_cell_v|
533533
let box_ty = trans_closure::shared_opaque_closure_box_ty(tcx);
534534
glue_fn(bcx, box_cell_v, box_ty)

src/comp/middle/trans_objects.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,8 @@ fn process_bkwding_mthd(cx: @local_ctxt, sp: span, m: @ty::method,
631631
// return type, if necessary.
632632
let llretptr = fcx.llretptr;
633633
let ccx = cx.ccx;
634-
if ty::type_contains_params(ccx.tcx, m.output) {
635-
let m_output = m.output;
634+
if ty::type_contains_params(ccx.tcx, m.fty.output) {
635+
let m_output = m.fty.output;
636636
check non_ty_var(ccx, m_output);
637637
let llretty = type_of_inner(ccx, sp, m_output);
638638
llretptr = PointerCast(bcx, llretptr, T_ptr(llretty));
@@ -676,7 +676,7 @@ fn process_bkwding_mthd(cx: @local_ctxt, sp: span, m: @ty::method,
676676
// function (they're in fcx.llargs) to llouter_mthd_args.
677677

678678
let a: uint = 2u; // retptr, env come first
679-
for arg: ty::arg in m.inputs {
679+
for arg: ty::arg in m.fty.inputs {
680680
llouter_mthd_args += [llvm::LLVMGetParam(llbackwarding_fn, a)];
681681
a += 1u;
682682
}
@@ -725,8 +725,8 @@ fn process_fwding_mthd(cx: @local_ctxt, sp: span, m: @ty::method,
725725
// return type, if necessary.
726726
let llretptr = fcx.llretptr;
727727
let ccx = cx.ccx;
728-
if ty::type_contains_params(ccx.tcx, m.output) {
729-
let m_output = m.output;
728+
if ty::type_contains_params(ccx.tcx, m.fty.output) {
729+
let m_output = m.fty.output;
730730
check non_ty_var(ccx, m_output);
731731
let llretty = type_of_inner(ccx, sp, m_output);
732732
llretptr = PointerCast(bcx, llretptr, T_ptr(llretty));
@@ -828,7 +828,7 @@ fn process_fwding_mthd(cx: @local_ctxt, sp: span, m: @ty::method,
828828
// function (they're in fcx.llargs) to llorig_mthd_args.
829829

830830
let a: uint = 2u; // retptr, env come first
831-
for arg: ty::arg in m.inputs {
831+
for arg: ty::arg in m.fty.inputs {
832832
llorig_mthd_args += [llvm::LLVMGetParam(llforwarding_fn, a)];
833833
a += 1u;
834834
}
@@ -877,9 +877,10 @@ fn process_normal_mthd(cx: @local_ctxt, m: @ast::method, self_ty: ty::t,
877877
let llfnty = T_nil();
878878
let ccx = cx.ccx;
879879
alt ty::struct(cx.ccx.tcx, node_id_type(cx.ccx, m.id)) {
880-
ty::ty_fn(_, inputs, output, rs, _) {
881-
check non_ty_var(ccx, output);
882-
llfnty = type_of_fn(ccx, m.span, true, inputs, output,
880+
ty::ty_fn(f) {
881+
let out = f.output;
882+
check non_ty_var(ccx, out);
883+
llfnty = type_of_fn(ccx, m.span, true, f.inputs, out,
883884
vec::len(ty_params));
884885
}
885886
}
@@ -930,9 +931,9 @@ fn populate_self_stack(bcx: @block_ctxt, self_stack: ValueRef,
930931

931932
fn type_of_meth(ccx: @crate_ctxt, sp: span, m: @ty::method,
932933
tps: [ast::ty_param]) -> TypeRef {
933-
let out_ty = m.output;
934+
let out_ty = m.fty.output;
934935
check non_ty_var(ccx, out_ty);
935-
type_of_fn(ccx, sp, true, m.inputs, out_ty, vec::len(tps))
936+
type_of_fn(ccx, sp, true, m.fty.inputs, out_ty, vec::len(tps))
936937
}
937938

938939
//

0 commit comments

Comments
 (0)