rustc: Make object methods into interior vectors

This commit is contained in:
Patrick Walton 2011-07-04 19:06:27 -07:00
parent 75c5f9bdba
commit 52a7c2b78e
4 changed files with 49 additions and 45 deletions

View File

@ -227,7 +227,7 @@ fn parse_ty(@pstate st, str_def sd) -> ty::t {
} }
case ('O') { case ('O') {
assert (next(st) as char == '['); assert (next(st) as char == '[');
let vec[ty::method] methods = []; let ty::method[] methods = ~[];
while (peek(st) as char != ']') { while (peek(st) as char != ']') {
auto proto; auto proto;
alt (next(st) as char) { alt (next(st) as char) {
@ -240,12 +240,12 @@ fn parse_ty(@pstate st, str_def sd) -> ty::t {
} }
auto func = parse_ty_fn(st, sd); auto func = parse_ty_fn(st, sd);
methods += methods +=
[rec(proto=proto, ~[rec(proto=proto,
ident=name, ident=name,
inputs=func._0, inputs=func._0,
output=func._1, output=func._1,
cf=func._2, cf=func._2,
constrs=func._3)]; constrs=func._3)];
} }
st.pos += 1u; st.pos += 1u;
ret ty::mk_obj(st.tcx, methods); ret ty::mk_obj(st.tcx, methods);

View File

@ -269,7 +269,7 @@ tag sty {
ty_rec(field[]); ty_rec(field[]);
ty_fn(ast::proto, arg[], t, controlflow, (@constr_def)[]); ty_fn(ast::proto, arg[], t, controlflow, (@constr_def)[]);
ty_native_fn(ast::native_abi, arg[], t); ty_native_fn(ast::native_abi, arg[], t);
ty_obj(vec[method]); ty_obj(method[]);
ty_res(def_id, t, t[]); ty_res(def_id, t, t[]);
ty_var(int); // type variable ty_var(int); // type variable
ty_param(uint); // fn/tag type param ty_param(uint); // fn/tag type param
@ -604,7 +604,7 @@ fn mk_native_fn(&ctxt cx, &ast::native_abi abi, &arg[] args, &t ty) -> t {
ret gen_ty(cx, ty_native_fn(abi, args, ty)); ret gen_ty(cx, ty_native_fn(abi, args, ty));
} }
fn mk_obj(&ctxt cx, &vec[method] meths) -> t { fn mk_obj(&ctxt cx, &method[] meths) -> t {
ret gen_ty(cx, ty_obj(meths)); ret gen_ty(cx, ty_obj(meths));
} }
@ -799,7 +799,7 @@ fn fold_ty(&ctxt cx, fold_mode fld, t ty_0) -> t {
fold_ty(cx, fld, ret_ty)), ty); fold_ty(cx, fld, ret_ty)), ty);
} }
case (ty_obj(?methods)) { case (ty_obj(?methods)) {
let vec[method] new_methods = []; let method[] new_methods = ~[];
for (method m in methods) { for (method m in methods) {
let arg[] new_args = ~[]; let arg[] new_args = ~[];
for (arg a in m.inputs) { for (arg a in m.inputs) {
@ -807,12 +807,12 @@ fn fold_ty(&ctxt cx, fold_mode fld, t ty_0) -> t {
ty=fold_ty(cx, fld, a.ty))]; ty=fold_ty(cx, fld, a.ty))];
} }
new_methods += new_methods +=
[rec(proto=m.proto, ~[rec(proto=m.proto,
ident=m.ident, ident=m.ident,
inputs=new_args, inputs=new_args,
output=fold_ty(cx, fld, m.output), output=fold_ty(cx, fld, m.output),
cf=m.cf, cf=m.cf,
constrs=m.constrs)]; constrs=m.constrs)];
} }
ty = copy_cname(cx, mk_obj(cx, new_methods), ty); ty = copy_cname(cx, mk_obj(cx, new_methods), ty);
} }
@ -1634,8 +1634,8 @@ fn equal_type_structures(&sty a, &sty b) -> bool {
case (ty_obj(?methods_a)) { case (ty_obj(?methods_a)) {
alt (b) { alt (b) {
case (ty_obj(?methods_b)) { case (ty_obj(?methods_b)) {
auto len = vec::len[method](methods_a); auto len = ivec::len[method](methods_a);
if (len != vec::len[method](methods_b)) { ret false; } if (len != ivec::len[method](methods_b)) { ret false; }
auto i = 0u; auto i = 0u;
while (i < len) { while (i < len) {
auto m_a = methods_a.(i); auto m_a = methods_a.(i);
@ -1941,17 +1941,17 @@ fn field_idx(&session::session sess, &span sp, &ast::ident id,
} }
fn method_idx(&session::session sess, &span sp, &ast::ident id, fn method_idx(&session::session sess, &span sp, &ast::ident id,
&vec[method] meths) -> uint { &method[] meths) -> uint {
let uint i = 0u; let uint i = 0u;
for (method m in meths) { if (str::eq(m.ident, id)) { ret i; } i += 1u; } for (method m in meths) { if (str::eq(m.ident, id)) { ret i; } i += 1u; }
sess.span_fatal(sp, "unknown method '" + id + "' of obj"); sess.span_fatal(sp, "unknown method '" + id + "' of obj");
} }
fn sort_methods(&vec[method] meths) -> vec[method] { fn sort_methods(&method[] meths) -> method[] {
fn method_lteq(&method a, &method b) -> bool { fn method_lteq(&method a, &method b) -> bool {
ret str::lteq(a.ident, b.ident); ret str::lteq(a.ident, b.ident);
} }
ret std::sort::merge_sort[method](bind method_lteq(_, _), meths); ret std::sort::ivector::merge_sort[method](bind method_lteq(_, _), meths);
} }
fn is_lval(&@ast::expr expr) -> bool { fn is_lval(&@ast::expr expr) -> bool {
@ -2191,12 +2191,12 @@ mod unify {
} }
} }
fn unify_obj(&@ctxt cx, &t expected, &t actual, fn unify_obj(&@ctxt cx, &t expected, &t actual,
&vec[method] expected_meths, &vec[method] actual_meths) -> &method[] expected_meths, &method[] actual_meths) ->
result { result {
let vec[method] result_meths = []; let method[] result_meths = ~[];
let uint i = 0u; let uint i = 0u;
let uint expected_len = vec::len[method](expected_meths); let uint expected_len = ivec::len[method](expected_meths);
let uint actual_len = vec::len[method](actual_meths); let uint actual_len = ivec::len[method](actual_meths);
if (expected_len != actual_len) { ret ures_err(terr_meth_count); } if (expected_len != actual_len) { ret ures_err(terr_meth_count); }
while (i < expected_len) { while (i < expected_len) {
auto e_meth = expected_meths.(i); auto e_meth = expected_meths.(i);
@ -2214,10 +2214,10 @@ mod unify {
alt (struct(cx.tcx, tfn)) { alt (struct(cx.tcx, tfn)) {
case (ty_fn(?proto, ?ins, ?out, ?cf, ?constrs)) { case (ty_fn(?proto, ?ins, ?out, ?cf, ?constrs)) {
result_meths += result_meths +=
[rec(inputs=ins, ~[rec(inputs=ins,
output=out, output=out,
cf=cf, cf=cf,
constrs=constrs with e_meth)]; constrs=constrs with e_meth)];
} }
} }
} }

View File

@ -364,7 +364,7 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
cname = some(path_to_str(path)); cname = some(path_to_str(path));
} }
case (ast::ty_obj(?meths)) { case (ast::ty_obj(?meths)) {
let vec[ty::method] tmeths = []; let ty::method[] tmeths = ~[];
for (ast::ty_method m in meths) { for (ast::ty_method m in meths) {
auto ins = ~[]; auto ins = ~[];
for (ast::ty_arg ta in m.node.inputs) { for (ast::ty_arg ta in m.node.inputs) {
@ -383,7 +383,7 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
output=out, output=out,
cf=m.node.cf, cf=m.node.cf,
constrs=out_constrs); constrs=out_constrs);
vec::push[ty::method](tmeths, new_m); tmeths += ~[new_m];
} }
typ = ty::mk_obj(tcx, ty::sort_methods(tmeths)); typ = ty::mk_obj(tcx, ty::sort_methods(tmeths));
} }
@ -717,9 +717,12 @@ mod collect {
write::ty_only(cx.tcx, variant.node.id, result_ty); write::ty_only(cx.tcx, variant.node.id, result_ty);
} }
} }
fn get_obj_method_types(&@ctxt cx, &ast::_obj object) -> vec[ty::method] { fn get_obj_method_types(&@ctxt cx, &ast::_obj object) -> ty::method[] {
ret vec::map[@ast::method, auto meths = ~[];
method](bind ty_of_method(cx, _), object.methods); for (@ast::method m in object.methods) {
meths += ~[ty_of_method(cx, m)];
}
ret meths;
} }
fn convert(@ctxt cx, @mutable option::t[ast::native_abi] abi, fn convert(@ctxt cx, @mutable option::t[ast::native_abi] abi,
&@ast::item it) { &@ast::item it) {
@ -2133,7 +2136,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
let uint ix = let uint ix =
ty::method_idx(fcx.ccx.tcx.sess, expr.span, field, ty::method_idx(fcx.ccx.tcx.sess, expr.span, field,
methods); methods);
if (ix >= vec::len[ty::method](methods)) { if (ix >= ivec::len[ty::method](methods)) {
fcx.ccx.tcx.sess.span_fatal(expr.span, fcx.ccx.tcx.sess.span_fatal(expr.span,
"bad index on obj"); "bad index on obj");
} }
@ -2267,21 +2270,21 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
constrs=out_constrs); constrs=out_constrs);
} }
fn get_anon_obj_method_types(@fn_ctxt fcx, fn get_anon_obj_method_types(@fn_ctxt fcx,
&ast::anon_obj anon_obj) -> &ast::anon_obj anon_obj)
vec[ty::method] { -> ty::method[] {
let vec[ty::method] methods = []; let ty::method[] methods = ~[];
// Outer methods. // Outer methods.
methods += vec::map[@ast::method, for (@ast::method m in anon_obj.methods) {
method](bind ty_of_method(fcx.ccx, _), methods += ~[ty_of_method(fcx.ccx, m)];
anon_obj.methods); }
// Inner methods. // Inner methods.
// Typecheck 'with_obj'. If it exists, it had better have // Typecheck 'with_obj'. If it exists, it had better have
// object type. // object type.
let vec[ty::method] with_obj_methods = []; let ty::method[] with_obj_methods = ~[];
alt (anon_obj.with_obj) { alt (anon_obj.with_obj) {
case (none) { } case (none) { }
case (some(?e)) { case (some(?e)) {

View File

@ -133,9 +133,10 @@ fn ty_to_str(&ctxt cx, &t typ) -> str {
ast::return, ~[]); ast::return, ~[]);
} }
case (ty_obj(?meths)) { case (ty_obj(?meths)) {
auto f = bind method_to_str(cx, _); // TODO: Remove this ivec->vec conversion.
auto m = vec::map[method, str](f, meths); auto strs = [];
s += "obj {\n\t" + str::connect(m, "\n\t") + "\n}"; for (method m in meths) { strs += [method_to_str(cx, m)]; }
s += "obj {\n\t" + str::connect(strs, "\n\t") + "\n}";
} }
case (ty_res(?id, _, _)) { case (ty_res(?id, _, _)) {
s += "<resource#" + int::str(id._0) + ":" + int::str(id._1) + ">"; s += "<resource#" + int::str(id._0) + ":" + int::str(id._1) + ">";