Skip to content

Commit 503dec1

Browse files
committed
Further WIP on classes
Changes to resolve and typeck. Still nothning working yet.
1 parent 9effae8 commit 503dec1

File tree

6 files changed

+75
-6
lines changed

6 files changed

+75
-6
lines changed

src/comp/metadata/tyencode.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ fn enc_sty(w: io::writer, cx: @ctxt, st: ty::sty) {
192192
w.write_char(']');
193193
}
194194
ty::ty_opaque_box { w.write_char('B'); }
195+
ty::ty_class(def, tys) {
196+
w.write_str("c[");
197+
w.write_str(cx.ds(def));
198+
w.write_char('|');
199+
for t: ty::t in tys { enc_ty(w, cx, t); }
200+
w.write_char(']');
201+
}
195202
}
196203
}
197204
fn enc_proto(w: io::writer, proto: proto) {

src/comp/middle/resolve.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,16 @@ fn visit_item_with_scope(e: @env, i: @ast::item, sc: scopes, v: vt<scopes>) {
472472
v.visit_ty(m.decl.output, msc, v);
473473
}
474474
}
475+
ast::item_class(tps, members, ctor_id, ctor_decl, ctor_block) {
476+
visit::visit_ty_params(tps, sc, v);
477+
let ctor_scope = cons(scope_fn_expr(ctor_decl, ctor_id, tps), @sc);
478+
for cm in members {
479+
alt cm.node.decl {
480+
class_method(i) { visit_item_with_scope(e, i, ctor_scope, v); }
481+
_ { } // instance var -- nothing to do
482+
}
483+
}
484+
}
475485
_ { visit::visit_item(i, sc, v); }
476486
}
477487

@@ -1209,7 +1219,9 @@ fn found_def_item(i: @ast::item, ns: namespace) -> option<def> {
12091219
}
12101220
}
12111221
ast::item_class(_, _, _, _, _) {
1212-
fail "class! don't know what to do";
1222+
if ns == ns_type {
1223+
ret some(ast::def_class(local_def(i.id)));
1224+
}
12131225
}
12141226
ast::item_impl(_,_,_,_) { /* ??? */ }
12151227
}

src/comp/middle/shape.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const shape_stack_fn: u8 = 26u8;
6363
const shape_bare_fn: u8 = 27u8;
6464
const shape_tydesc: u8 = 28u8;
6565
const shape_send_tydesc: u8 = 29u8;
66+
const shape_class: u8 = 30u8;
6667

6768
// FIXME: This is a bad API in trans_common.
6869
fn C_u8(n: u8) -> ValueRef { ret trans::common::C_u8(n as uint); }
@@ -404,6 +405,7 @@ fn shape_of(ccx: @crate_ctxt, t: ty::t, ty_param_map: [uint]) -> [u8] {
404405
add_substr(s, sub);
405406
}
406407
ty::ty_iface(_, _) { s += [shape_iface]; }
408+
ty::ty_class(_, _) { s += [shape_class]; }
407409
ty::ty_res(did, raw_subt, tps) {
408410
let subt = ty::substitute_type_params(ccx.tcx, tps, raw_subt);
409411
let ri = {did: did, t: subt};

src/comp/middle/ty.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export is_pred_ty;
4040
export lookup_item_type;
4141
export method;
4242
export method_idx;
43+
export mk_class;
4344
export mk_ctxt;
4445
export mk_named, type_name;
4546
export mt;
@@ -225,6 +226,7 @@ enum sty {
225226
ty_rec([field]),
226227
ty_fn(fn_ty),
227228
ty_iface(def_id, [t]),
229+
ty_class(def_id, [t]),
228230
ty_res(def_id, t, [t]),
229231
ty_tup([t]),
230232

@@ -354,7 +356,7 @@ fn mk_t_named(cx: ctxt, st: sty, name: option<str>) -> t {
354356
ty_opaque_box {}
355357
ty_param(_, _) { has_params = true; }
356358
ty_var(_) | ty_self(_) { has_vars = true; }
357-
ty_enum(_, tys) | ty_iface(_, tys) {
359+
ty_enum(_, tys) | ty_iface(_, tys) | ty_class(_, tys) {
358360
for tt in tys { derive_flags(has_params, has_vars, tt); }
359361
}
360362
ty_box(m) | ty_uniq(m) | ty_vec(m) | ty_ptr(m) {
@@ -442,6 +444,10 @@ fn mk_iface(cx: ctxt, did: ast::def_id, tys: [t]) -> t {
442444
mk_t(cx, ty_iface(did, tys))
443445
}
444446

447+
fn mk_class(cx: ctxt, class_id: ast::def_id, tys: [t]) -> t {
448+
mk_t(cx, ty_class(class_id, tys))
449+
}
450+
445451
fn mk_res(cx: ctxt, did: ast::def_id, inner: t, tps: [t]) -> t {
446452
mk_t(cx, ty_res(did, inner, tps))
447453
}
@@ -487,7 +493,8 @@ fn walk_ty(cx: ctxt, ty: t, f: fn(t)) {
487493
ty_str | ty_send_type | ty_type | ty_opaque_box |
488494
ty_opaque_closure_ptr(_) | ty_var(_) | ty_param(_, _) {}
489495
ty_box(tm) | ty_vec(tm) | ty_ptr(tm) { walk_ty(cx, tm.ty, f); }
490-
ty_enum(_, subtys) | ty_iface(_, subtys) | ty_self(subtys) {
496+
ty_enum(_, subtys) | ty_iface(_, subtys) | ty_class(_, subtys)
497+
| ty_self(subtys) {
491498
for subty: t in subtys { walk_ty(cx, subty, f); }
492499
}
493500
ty_rec(fields) {
@@ -1155,6 +1162,11 @@ fn hash_type_structure(st: sty) -> uint {
11551162
ty_opaque_closure_ptr(ck_box) { 42u }
11561163
ty_opaque_closure_ptr(ck_uniq) { 43u }
11571164
ty_opaque_box { 44u }
1165+
ty_class(did, tys) {
1166+
let h = hash_def(45u, did);
1167+
for typ: t in tys { h = hash_subty(h, typ); }
1168+
h
1169+
}
11581170
}
11591171
}
11601172

@@ -2410,6 +2422,9 @@ fn lookup_item_type(cx: ctxt, did: ast::def_id) -> ty_param_bounds_and_ty {
24102422
alt cx.tcache.find(did) {
24112423
some(tpt) { ret tpt; }
24122424
none {
2425+
/* where do things get added to the cache?
2426+
Have to add class members */
2427+
24132428
// The item is in this crate. The caller should have added it to the
24142429
// type cache already
24152430
assert did.crate != ast::local_crate;

src/comp/middle/typeck.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ fn ty_param_bounds_and_ty_for_def(fcx: @fn_ctxt, sp: span, defn: ast::def) ->
108108
}
109109
}
110110
ast::def_fn(id, _) | ast::def_const(id) |
111-
ast::def_variant(_, id) { ret ty::lookup_item_type(fcx.ccx.tcx, id); }
111+
ast::def_variant(_, id) | ast::def_class(id)
112+
| ast::def_class_method(_, id) | ast::def_class_field(_, id)
113+
{ ret ty::lookup_item_type(fcx.ccx.tcx, id); }
112114
ast::def_binding(id) {
113115
assert (fcx.locals.contains_key(id.node));
114116
let typ = ty::mk_var(fcx.ccx.tcx, lookup_local(fcx, sp, id.node));
@@ -422,8 +424,12 @@ fn ty_of_item(tcx: ty::ctxt, mode: mode, it: @ast::item)
422424
tcx.tcache.insert(local_def(it.id), tpt);
423425
ret tpt;
424426
}
425-
ast::item_class(_,_,_,_,_) {
426-
fail "ty_of_item: implement item_class";
427+
ast::item_class(tps,_,_,_,_) {
428+
let {bounds,params} = mk_ty_params(tcx, tps);
429+
let t = ty::mk_class(tcx, local_def(it.id), params);
430+
let tpt = {bounds: bounds, ty: t};
431+
tcx.tcache.insert(local_def(it.id), tpt);
432+
ret tpt;
427433
}
428434
ast::item_impl(_, _, _, _) | ast::item_mod(_) |
429435
ast::item_native_mod(_) { fail; }
@@ -755,6 +761,10 @@ mod collect {
755761
_ { fail; }
756762
}
757763
}
764+
fn convert_class_item(cx: @ctxt, parent_ty: ty::t,
765+
ci: ast::class_member) {
766+
/* TODO */
767+
}
758768
fn convert(cx: @ctxt, it: @ast::item) {
759769
alt it.node {
760770
// These don't define types.
@@ -847,6 +857,19 @@ mod collect {
847857
write_ty(cx.tcx, it.id, tpt.ty);
848858
ensure_iface_methods(cx.tcx, it.id);
849859
}
860+
ast::item_class(tps, members, ctor_id, ctor_decl, ctor_block) {
861+
let parent_ty = ty::lookup_item_type(cx.tcx, local_def(it.id));
862+
// Write the ctor type
863+
let t_ctor = ty::mk_fn(cx.tcx,
864+
ty_of_fn_decl(cx.tcx, m_collect,
865+
ast::proto_any, ctor_decl));
866+
write_ty(cx.tcx, ctor_id, t_ctor);
867+
/* FIXME: check for proper public/privateness */
868+
// Write the type of each of the members
869+
for m in members {
870+
convert_class_item(cx, parent_ty.ty, m.node.decl);
871+
}
872+
}
850873
_ {
851874
// This call populates the type cache with the converted type
852875
// of the item in passing. All we have to do here is to write

src/test/run-pass/classes-simple.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// xfail-test
2+
class cat {
3+
priv {
4+
let mutable meows : uint;
5+
}
6+
7+
let how_hungry : int;
8+
9+
new(in_x : uint, in_y : int) { meows = in_x; how_hungry = in_y; }
10+
}

0 commit comments

Comments
 (0)