Skip to content

Commit 1912524

Browse files
committed
---
yaml --- r: 1896 b: refs/heads/master c: ace2c92 h: refs/heads/master v: v3
1 parent 82bda26 commit 1912524

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 621ab344101b3733566a1367f6fa87ab50385d2e
2+
refs/heads/master: ace2c92a96eb4ac46913305854235592aa9207ae

trunk/src/comp/front/creader.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import lib.llvm.mk_section_iter;
99
import middle.fold;
1010
import middle.metadata;
1111
import middle.ty;
12-
import middle.typeck;
1312
import back.x86;
1413
import util.common;
1514
import util.common.span;
@@ -522,7 +521,7 @@ fn lookup_def(session.session sess, &span sp, int cnum, vec[ast.ident] path)
522521
fail;
523522
}
524523

525-
fn get_type(session.session sess, ast.def_id def) -> typeck.ty_and_params {
524+
fn get_type(session.session sess, ast.def_id def) -> ty.ty_params_and_ty {
526525
// FIXME: fill in.
527526
fail;
528527
}

trunk/src/comp/middle/typeck.rs

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ type fn_ctxt = rec(@ty.t ret_ty,
5353
@crate_ctxt ccx);
5454

5555
// Used for ast_ty_to_ty() below.
56-
type ty_and_params = rec(vec[ast.ty_param] params, @ty.t ty);
57-
type ty_getter = fn(ast.def_id) -> ty_and_params;
56+
type ty_getter = fn(ast.def_id) -> ty.ty_params_and_ty;
5857

5958
// Replaces parameter types inside a type with type variables.
6059
fn generalize_ty(@crate_ctxt cx, @ty.t t) -> @ty.t {
@@ -128,6 +127,23 @@ fn substitute_ty_params(&@crate_ctxt ccx,
128127
ret ty.fold_ty(substituter, typ);
129128
}
130129

130+
131+
// Looks up the type of the given item in an external crate.
132+
fn lookup_item_type_if_necessary(@crate_ctxt ccx, ast.def_id did) {
133+
if (did._0 == ccx.sess.get_targ_crate_num()) {
134+
ret; // Nothing to do; it should already be in the tables.
135+
}
136+
137+
if (ccx.item_types.contains_key(did)) {
138+
ret; // Nothing to do; we already looked up this item's type.
139+
}
140+
141+
auto tyt = creader.get_type(ccx.sess, did);
142+
ccx.item_types.insert(did, tyt._1);
143+
ccx.item_ty_params.insert(did, tyt._0);
144+
}
145+
146+
131147
type ty_params_opt_and_ty = tup(option.t[vec[ast.def_id]], @ty.t);
132148

133149
// Returns the type parameters and the type for the given definition.
@@ -151,20 +167,25 @@ fn ty_params_and_ty_for_def(@fn_ctxt fcx, &ast.def defn)
151167
ret tup(none[vec[ast.def_id]], fcx.locals.get(id));
152168
}
153169
case (ast.def_fn(?id)) {
170+
lookup_item_type_if_necessary(fcx.ccx, id);
154171
check (fcx.ccx.item_types.contains_key(id));
155172
ret tup(some(fcx.ccx.item_ty_params.get(id)),
156173
fcx.ccx.item_types.get(id));
157174
}
158175
case (ast.def_native_fn(?id)) {
176+
lookup_item_type_if_necessary(fcx.ccx, id);
159177
check (fcx.ccx.item_types.contains_key(id));
160178
ret tup(some(fcx.ccx.item_ty_params.get(id)),
161179
fcx.ccx.item_types.get(id));
162180
}
163181
case (ast.def_const(?id)) {
182+
lookup_item_type_if_necessary(fcx.ccx, id);
164183
check (fcx.ccx.item_types.contains_key(id));
165184
ret tup(none[vec[ast.def_id]], fcx.ccx.item_types.get(id));
166185
}
167186
case (ast.def_variant(?tag_id, ?variant_id)) {
187+
lookup_item_type_if_necessary(fcx.ccx, tag_id);
188+
lookup_item_type_if_necessary(fcx.ccx, variant_id);
168189
check (fcx.ccx.item_types.contains_key(variant_id));
169190
ret tup(some(fcx.ccx.item_ty_params.get(tag_id)),
170191
fcx.ccx.item_types.get(variant_id));
@@ -174,6 +195,7 @@ fn ty_params_and_ty_for_def(@fn_ctxt fcx, &ast.def defn)
174195
ret tup(none[vec[ast.def_id]], fcx.locals.get(id));
175196
}
176197
case (ast.def_obj(?id)) {
198+
lookup_item_type_if_necessary(fcx.ccx, id);
177199
check (fcx.ccx.item_types.contains_key(id));
178200
ret tup(some(fcx.ccx.item_ty_params.get(id)),
179201
fcx.ccx.item_types.get(id));
@@ -291,17 +313,17 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
291313
// TODO: maybe record cname chains so we can do
292314
// "foo = int" like OCaml?
293315
auto ty_and_params = getter(id);
294-
auto params = ty_and_params.params;
316+
auto params = ty_and_params._0;
295317
auto num_type_args = _vec.len[@ast.ty](args);
296-
check(num_type_args == _vec.len[ast.ty_param](params));
318+
check(num_type_args == _vec.len[ast.def_id](params));
297319

298320
auto param_map = common.new_def_hash[@ty.t]();
299321
for each (uint i in _uint.range(0u, num_type_args)) {
300322
auto arg = args.(i);
301323
auto param = params.(i);
302-
param_map.insert(param.id, ast_ty_to_ty(getter, arg));
324+
param_map.insert(param, ast_ty_to_ty(getter, arg));
303325
}
304-
ret ty.replace_type_params(ty_and_params.ty, param_map);
326+
ret ty.replace_type_params(ty_and_params._1, param_map);
305327
}
306328

307329
auto mut = ast.imm;
@@ -404,7 +426,7 @@ fn actual_type(@ty.t t, @ast.item item) -> @ty.t {
404426
// A convenience function to use a crate_ctxt to resolve names for
405427
// ast_ty_to_ty.
406428
fn ast_ty_to_ty_crate(@crate_ctxt ccx, &@ast.ty ast_ty) -> @ty.t {
407-
fn getter(@crate_ctxt ccx, ast.def_id id) -> ty_and_params {
429+
fn getter(@crate_ctxt ccx, ast.def_id id) -> ty.ty_params_and_ty {
408430

409431
if (id._0 != ccx.sess.get_targ_crate_num()) {
410432
// This is a type we need to load in from the crate reader.
@@ -426,7 +448,12 @@ fn ast_ty_to_ty_crate(@crate_ctxt ccx, &@ast.ty ast_ty) -> @ty.t {
426448
}
427449
}
428450

429-
ret rec(params = params, ty = ty);
451+
let vec[ast.def_id] param_ids = vec();
452+
for (ast.ty_param tp in params) {
453+
param_ids += vec(tp.id);
454+
}
455+
456+
ret tup(param_ids, ty);
430457
}
431458
auto f = bind getter(ccx, _);
432459
ret ast_ty_to_ty(f, ast_ty);
@@ -510,7 +537,7 @@ fn collect_item_types(session.session sess, @ast.crate crate)
510537
fn getter(session.session sess,
511538
@ty_item_table id_to_ty_item,
512539
@ty_table item_to_ty,
513-
ast.def_id id) -> ty_and_params {
540+
ast.def_id id) -> ty.ty_params_and_ty {
514541

515542
if (id._0 != sess.get_targ_crate_num()) {
516543
// This is a type we need to load in from the crate reader.
@@ -535,7 +562,12 @@ fn collect_item_types(session.session sess, @ast.crate crate)
535562
}
536563
}
537564

538-
ret rec(params = params, ty = ty);
565+
let vec[ast.def_id] param_ids = vec();
566+
for (ast.ty_param tp in params) {
567+
param_ids += vec(tp.id);
568+
}
569+
570+
ret tup(param_ids, ty);
539571
}
540572

541573
fn ty_of_arg(session.session sess,

0 commit comments

Comments
 (0)