Skip to content

Commit

Permalink
Refactor fn_ty, working towards #3320
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Sep 7, 2012
1 parent 6995058 commit fb8786f
Show file tree
Hide file tree
Showing 33 changed files with 699 additions and 569 deletions.
2 changes: 1 addition & 1 deletion src/rustc/metadata/decoder.rs
Expand Up @@ -585,7 +585,7 @@ fn get_enum_variants(intr: ident_interner, cdata: cmd, id: ast::node_id,
let mut arg_tys: ~[ty::t] = ~[];
match ty::get(ctor_ty).struct {
ty::ty_fn(f) => {
for f.inputs.each |a| { vec::push(arg_tys, a.ty); }
for f.sig.inputs.each |a| { vec::push(arg_tys, a.ty); }
}
_ => { /* Nullary enum variant. */ }
}
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/metadata/encoder.rs
Expand Up @@ -750,7 +750,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::Writer, item: @item,
encode_name(ecx, ebml_w, mty.ident);
encode_type_param_bounds(ebml_w, ecx, ty_m.tps);
encode_type(ecx, ebml_w, ty::mk_fn(tcx, mty.fty));
encode_family(ebml_w, purity_fn_family(mty.fty.purity));
encode_family(ebml_w, purity_fn_family(mty.fty.meta.purity));
encode_self_type(ebml_w, mty.self_ty);
ebml_w.end_tag();
}
Expand Down
15 changes: 11 additions & 4 deletions src/rustc/metadata/tydecode.rs
Expand Up @@ -9,6 +9,7 @@ use syntax::ast_util;
use syntax::ast_util::respan;
use middle::ty;
use std::map::hashmap;
use ty::{FnTyBase, FnMeta, FnSig};

export parse_ty_data, parse_def_id, parse_ident;
export parse_bounds_data;
Expand Down Expand Up @@ -274,7 +275,7 @@ fn parse_ty(st: @pstate, conv: conv_did) -> ty::t {
parse_ty_rust_fn(st, conv)
}
'X' => {
return ty::mk_var(st.tcx, ty::ty_vid(parse_int(st) as uint));
return ty::mk_var(st.tcx, ty::TyVid(parse_int(st) as uint));
}
'Y' => return ty::mk_type(st.tcx),
'C' => {
Expand Down Expand Up @@ -372,7 +373,7 @@ fn parse_purity(c: char) -> purity {
}
}

fn parse_ty_fn(st: @pstate, conv: conv_did) -> ty::fn_ty {
fn parse_ty_fn(st: @pstate, conv: conv_did) -> ty::FnTy {
let proto = parse_proto(st);
let purity = parse_purity(next(st));
let bounds = parse_bounds(st, conv);
Expand All @@ -392,8 +393,14 @@ fn parse_ty_fn(st: @pstate, conv: conv_did) -> ty::fn_ty {
}
st.pos += 1u; // eat the ']'
let (ret_style, ret_ty) = parse_ret_ty(st, conv);
return {purity: purity, proto: proto, bounds: bounds, inputs: inputs,
output: ret_ty, ret_style: ret_style};
return FnTyBase {
meta: FnMeta {purity: purity,
proto: proto,
bounds: bounds,
ret_style: ret_style},
sig: FnSig {inputs: inputs,
output: ret_ty}
};
}


Expand Down
14 changes: 7 additions & 7 deletions src/rustc/metadata/tyencode.rs
Expand Up @@ -342,19 +342,19 @@ fn enc_purity(w: io::Writer, p: purity) {
}
}

fn enc_ty_fn(w: io::Writer, cx: @ctxt, ft: ty::fn_ty) {
enc_proto(w, cx, ft.proto);
enc_purity(w, ft.purity);
enc_bounds(w, cx, ft.bounds);
fn enc_ty_fn(w: io::Writer, cx: @ctxt, ft: ty::FnTy) {
enc_proto(w, cx, ft.meta.proto);
enc_purity(w, ft.meta.purity);
enc_bounds(w, cx, ft.meta.bounds);
w.write_char('[');
for ft.inputs.each |arg| {
for ft.sig.inputs.each |arg| {
enc_mode(w, cx, arg.mode);
enc_ty(w, cx, arg.ty);
}
w.write_char(']');
match ft.ret_style {
match ft.meta.ret_style {
noreturn => w.write_char('!'),
_ => enc_ty(w, cx, ft.output)
_ => enc_ty(w, cx, ft.sig.output)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/rustc/middle/borrowck/check_loans.rs
Expand Up @@ -218,13 +218,13 @@ impl check_loan_ctxt {
let callee_ty = ty::node_id_to_type(tcx, callee_id);
match ty::get(callee_ty).struct {
ty::ty_fn(fn_ty) => {
match fn_ty.purity {
match fn_ty.meta.purity {
ast::pure_fn => return, // case (c) above
ast::impure_fn | ast::unsafe_fn | ast::extern_fn => {
self.report_purity_error(
pc, callee_span,
fmt!("access to %s function",
pprust::purity_to_str(fn_ty.purity)));
pprust::purity_to_str(fn_ty.meta.purity)));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/middle/lint.rs
Expand Up @@ -659,7 +659,7 @@ fn check_fn(tcx: ty::ctxt, fk: visit::fn_kind, decl: ast::fn_decl,
match ty::get(fn_ty).struct {
ty::ty_fn(fn_ty) => {
let mut counter = 0;
do vec::iter2(fn_ty.inputs, decl.inputs) |arg_ty, arg_ast| {
do vec::iter2(fn_ty.sig.inputs, decl.inputs) |arg_ty, arg_ast| {
counter += 1;
debug!("arg %d, ty=%s, mode=%s",
counter,
Expand Down
6 changes: 3 additions & 3 deletions src/rustc/middle/trans/base.rs
Expand Up @@ -529,10 +529,10 @@ fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
let ccx = cx.ccx();
let mut cx = cx;
match ty::get(fn_ty).struct {
ty::ty_fn({inputs: args, _}) => {
ty::ty_fn(ref fn_ty) => {
let mut j = 0u;
let v_id = variant.id;
for vec::each(args) |a| {
for vec::each(fn_ty.sig.inputs) |a| {
let llfldp_a = GEP_enum(cx, a_tup, tid, v_id, tps, j);
let ty_subst = ty::subst_tps(ccx.tcx, tps, a.ty);
cx = f(cx, llfldp_a, ty_subst);
Expand Down Expand Up @@ -1984,7 +1984,7 @@ fn create_main_wrapper(ccx: @crate_ctxt, sp: span, main_llfn: ValueRef,
let main_takes_argv =
// invariant!
match ty::get(main_node_type).struct {
ty::ty_fn({inputs, _}) => inputs.len() != 0u,
ty::ty_fn(ref fn_ty) => fn_ty.sig.inputs.len() != 0u,
_ => ccx.sess.span_fatal(sp, ~"main has a non-function type")
};

Expand Down
20 changes: 9 additions & 11 deletions src/rustc/middle/trans/closure.rs
Expand Up @@ -425,17 +425,15 @@ fn make_fn_glue(
}
};

return match ty::get(t).struct {
ty::ty_fn({proto: ty::proto_bare, _}) |
ty::ty_fn({proto: ty::proto_vstore(ty::vstore_slice(_)), _}) =>
bcx,
ty::ty_fn({proto: ty::proto_vstore(ty::vstore_uniq), _}) =>
fn_env(ty::ck_uniq),
ty::ty_fn({proto: ty::proto_vstore(ty::vstore_box), _}) =>
fn_env(ty::ck_box),
_ =>
fail ~"make_fn_glue invoked on non-function type"
};
let proto = ty::ty_fn_proto(t);
match proto {
ty::proto_bare | ty::proto_vstore(ty::vstore_slice(_)) => bcx,
ty::proto_vstore(ty::vstore_uniq) => fn_env(ty::ck_uniq),
ty::proto_vstore(ty::vstore_box) => fn_env(ty::ck_box),
ty::proto_vstore(ty::vstore_fixed(_)) => {
cx.sess().bug(~"Closure with fixed vstore");
}
}
}

fn make_opaque_cbox_take_glue(
Expand Down
13 changes: 7 additions & 6 deletions src/rustc/middle/trans/expr.rs
Expand Up @@ -447,13 +447,14 @@ fn trans_rvalue_dps(bcx: block, expr: @ast::expr, dest: Dest) -> block {
ast::expr_fn_block(decl, body, cap_clause) => {
let expr_ty = expr_ty(bcx, expr);
match ty::get(expr_ty).struct {
ty::ty_fn({proto, _}) => {
ty::ty_fn(ref fn_ty) => {
debug!("translating fn_block %s with type %s",
expr_to_str(expr, tcx.sess.intr()),
ty_to_str(tcx, expr_ty));
return closure::trans_expr_fn(bcx, proto, decl, body,
expr.id, cap_clause, None,
dest);
return closure::trans_expr_fn(
bcx, fn_ty.meta.proto, decl, body,
expr.id, cap_clause, None,
dest);
}
_ => {
bcx.sess().impossible_case(
Expand All @@ -463,11 +464,11 @@ fn trans_rvalue_dps(bcx: block, expr: @ast::expr, dest: Dest) -> block {
}
ast::expr_loop_body(blk) => {
match ty::get(expr_ty(bcx, expr)).struct {
ty::ty_fn({proto, _}) => {
ty::ty_fn(ref fn_ty) => {
match blk.node {
ast::expr_fn_block(decl, body, cap) => {
return closure::trans_expr_fn(
bcx, proto, decl, body, blk.id,
bcx, fn_ty.meta.proto, decl, body, blk.id,
cap, Some(None), dest);
}
_ => {
Expand Down
40 changes: 20 additions & 20 deletions src/rustc/middle/trans/foreign.rs
Expand Up @@ -20,6 +20,7 @@ use util::ppaux::ty_to_str;
use datum::*;
use callee::*;
use expr::{Dest, Ignore};
use ty::{FnTyBase, FnMeta, FnSig};

export link_name, trans_foreign_mod, register_foreign_fn, trans_foreign_fn,
trans_intrinsic;
Expand Down Expand Up @@ -439,12 +440,12 @@ type c_stack_tys = {
fn c_arg_and_ret_lltys(ccx: @crate_ctxt,
id: ast::node_id) -> (~[TypeRef], TypeRef, ty::t) {
match ty::get(ty::node_id_to_type(ccx.tcx, id)).struct {
ty::ty_fn({inputs: arg_tys, output: ret_ty, _}) => {
let llargtys = type_of_explicit_args(ccx, arg_tys);
let llretty = type_of::type_of(ccx, ret_ty);
(llargtys, llretty, ret_ty)
}
_ => ccx.sess.bug(~"c_arg_and_ret_lltys called on non-function type")
ty::ty_fn(ref fn_ty) => {
let llargtys = type_of_explicit_args(ccx, fn_ty.sig.inputs);
let llretty = type_of::type_of(ccx, fn_ty.sig.output);
(llargtys, llretty, fn_ty.sig.output)
}
_ => ccx.sess.bug(~"c_arg_and_ret_lltys called on non-function type")
}
}

Expand Down Expand Up @@ -953,20 +954,19 @@ fn trans_intrinsic(ccx: @crate_ctxt, decl: ValueRef, item: @ast::foreign_item,
~"frame_address" => {
let frameaddress = ccx.intrinsics.get(~"llvm.frameaddress");
let frameaddress_val = Call(bcx, frameaddress, ~[C_i32(0i32)]);
let fty = ty::mk_fn(bcx.tcx(), {
purity: ast::impure_fn,
proto:
ty::proto_vstore(ty::vstore_slice(
ty::re_bound(ty::br_anon(0)))),
bounds: @~[],
inputs: ~[{
mode: ast::expl(ast::by_val),
ty: ty::mk_imm_ptr(
bcx.tcx(),
ty::mk_mach_uint(bcx.tcx(), ast::ty_u8))
}],
output: ty::mk_nil(bcx.tcx()),
ret_style: ast::return_val
let star_u8 = ty::mk_imm_ptr(
bcx.tcx(),
ty::mk_mach_uint(bcx.tcx(), ast::ty_u8));
let fty = ty::mk_fn(bcx.tcx(), FnTyBase {
meta: FnMeta {purity: ast::impure_fn,
proto:
ty::proto_vstore(ty::vstore_slice(
ty::re_bound(ty::br_anon(0)))),
bounds: @~[],
ret_style: ast::return_val},
sig: FnSig {inputs: ~[{mode: ast::expl(ast::by_val),
ty: star_u8}],
output: ty::mk_nil(bcx.tcx())}
});
let datum = Datum {val: get_param(decl, first_real_arg),
mode: ByRef, ty: fty, source: FromLvalue};
Expand Down
52 changes: 31 additions & 21 deletions src/rustc/middle/trans/monomorphize.rs
Expand Up @@ -10,6 +10,7 @@ use base::{trans_item, get_item_val, no_self, self_arg, trans_fn,
use syntax::parse::token::special_idents;
use type_of::type_of_fn_from_ty;
use back::link::mangle_exported_name;
use middle::ty::{FnTyBase, FnMeta, FnSig};

fn monomorphic_fn(ccx: @crate_ctxt,
fn_id: ast::def_id,
Expand Down Expand Up @@ -198,27 +199,36 @@ fn monomorphic_fn(ccx: @crate_ctxt,
fn normalize_for_monomorphization(tcx: ty::ctxt, ty: ty::t) -> Option<ty::t> {
// FIXME[mono] could do this recursively. is that worthwhile? (#2529)
match ty::get(ty).struct {
ty::ty_box(*) => {
Some(ty::mk_opaque_box(tcx))
}
ty::ty_fn(ref fty) => {
Some(ty::mk_fn(tcx, {purity: ast::impure_fn,
proto: fty.proto,
bounds: @~[],
inputs: ~[],
output: ty::mk_nil(tcx),
ret_style: ast::return_val}))
}
ty::ty_trait(_, _, _) => {
Some(ty::mk_fn(tcx, {purity: ast::impure_fn,
proto: ty::proto_vstore(ty::vstore_box),
bounds: @~[],
inputs: ~[],
output: ty::mk_nil(tcx),
ret_style: ast::return_val}))
}
ty::ty_ptr(_) => Some(ty::mk_uint(tcx)),
_ => None
ty::ty_box(*) => {
Some(ty::mk_opaque_box(tcx))
}
ty::ty_fn(ref fty) => {
Some(ty::mk_fn(
tcx,
FnTyBase {meta: FnMeta {purity: ast::impure_fn,
proto: fty.meta.proto,
bounds: @~[],
ret_style: ast::return_val},
sig: FnSig {inputs: ~[],
output: ty::mk_nil(tcx)}}))
}
ty::ty_trait(_, _, _) => {
let box_proto = ty::proto_vstore(ty::vstore_box);
Some(ty::mk_fn(
tcx,
FnTyBase {meta: FnMeta {purity: ast::impure_fn,
proto: box_proto,
bounds: @~[],
ret_style: ast::return_val},
sig: FnSig {inputs: ~[],
output: ty::mk_nil(tcx)}}))
}
ty::ty_ptr(_) => {
Some(ty::mk_uint(tcx))
}
_ => {
None
}
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/rustc/middle/trans/reflect.rs
Expand Up @@ -178,31 +178,31 @@ impl reflector {

// FIXME (#2594): fetch constants out of intrinsic:: for the
// numbers.
ty::ty_fn(fty) => {
let pureval = match fty.purity {
ty::ty_fn(ref fty) => {
let pureval = match fty.meta.purity {
ast::pure_fn => 0u,
ast::unsafe_fn => 1u,
ast::impure_fn => 2u,
ast::extern_fn => 3u
};
let protoval = match fty.proto {
let protoval = match fty.meta.proto {
ty::proto_bare => 0u,
ty::proto_vstore(ty::vstore_uniq) => 2u,
ty::proto_vstore(ty::vstore_box) => 3u,
ty::proto_vstore(ty::vstore_slice(_)) => 4u,
ty::proto_vstore(ty::vstore_fixed(_)) =>
fail ~"fixed unexpected"
};
let retval = match fty.ret_style {
let retval = match fty.meta.ret_style {
ast::noreturn => 0u,
ast::return_val => 1u
};
let extra = ~[self.c_uint(pureval),
self.c_uint(protoval),
self.c_uint(vec::len(fty.inputs)),
self.c_uint(retval)];
self.c_uint(protoval),
self.c_uint(vec::len(fty.sig.inputs)),
self.c_uint(retval)];
self.visit(~"enter_fn", extra);
for fty.inputs.eachi |i, arg| {
for fty.sig.inputs.eachi |i, arg| {
let modeval = match arg.mode {
ast::infer(_) => 0u,
ast::expl(e) => match e {
Expand All @@ -220,7 +220,7 @@ impl reflector {
}
self.visit(~"fn_output",
~[self.c_uint(retval),
self.c_tydesc(fty.output)]);
self.c_tydesc(fty.sig.output)]);
self.visit(~"leave_fn", extra);
}

Expand Down

0 comments on commit fb8786f

Please sign in to comment.