Skip to content

Commit 2a3a082

Browse files
Rafael Avila de Espindolagraydon
authored andcommitted
---
yaml --- r: 1366 b: refs/heads/master c: 0d27eb1 h: refs/heads/master v: v3
1 parent 6ae2e3f commit 2a3a082

File tree

3 files changed

+127
-35
lines changed

3 files changed

+127
-35
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: 2b669008a8bcfcf0d1ed41cecd243e59cded4ded
2+
refs/heads/master: 0d27eb13415d442a4f643c8cf4c0d839c0460744

trunk/src/comp/middle/ty.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ tag sty {
4343
ty_local(ast.def_id); // type of a local var
4444
ty_param(ast.def_id); // fn type param
4545
ty_type;
46+
ty_native;
4647
// TODO: ty_fn_arg(@t), for a possibly-aliased function argument
4748
}
4849

@@ -286,6 +287,7 @@ fn fold_ty(ty_fold fld, @t ty) -> @t {
286287
case (ty_str) { ret fld.fold_simple_ty(ty); }
287288
case (ty_tag(_)) { ret fld.fold_simple_ty(ty); }
288289
case (ty_type) { ret fld.fold_simple_ty(ty); }
290+
case (ty_native) { ret fld.fold_simple_ty(ty); }
289291
case (ty_box(?subty)) {
290292
ret rewrap(ty, ty_box(fold_ty(fld, subty)));
291293
}

trunk/src/comp/middle/typeck.rs

Lines changed: 124 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ import std.option.none;
3131
import std.option.some;
3232

3333
type ty_table = hashmap[ast.def_id, @ty.t];
34-
type ty_item_table = hashmap[ast.def_id,@ast.item];
34+
35+
tag any_item {
36+
any_item_rust(@ast.item);
37+
any_item_native(@ast.native_item);
38+
}
39+
40+
type ty_item_table = hashmap[ast.def_id,any_item];
3541

3642
type crate_ctxt = rec(session.session sess,
3743
@ty_table item_types,
@@ -161,6 +167,9 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
161167
case (ast.def_ty(?id)) {
162168
sty = instantiate(getter, id, path.node.types).struct;
163169
}
170+
case (ast.def_native_ty(?id)) {
171+
sty = instantiate(getter, id, path.node.types).struct;
172+
}
164173
case (ast.def_obj(?id)) {
165174
sty = instantiate(getter, id, path.node.types).struct;
166175
}
@@ -196,23 +205,36 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
196205
ret @rec(struct=sty, mut=mut, cname=cname);
197206
}
198207

208+
fn actual_type(@ty.t t, @ast.item item) -> @ty.t {
209+
alt (item.node) {
210+
case (ast.item_obj(_,_,_,_,_)) {
211+
// An obj used as a type name refers to the output type of the
212+
// item (constructor).
213+
ret middle.ty.ty_fn_ret(t);
214+
}
215+
case (_) { }
216+
}
217+
218+
ret t;
219+
}
220+
199221
// A convenience function to use a crate_ctxt to resolve names for
200222
// ast_ty_to_ty.
201223
fn ast_ty_to_ty_crate(@crate_ctxt ccx, &@ast.ty ast_ty) -> @ty.t {
202224
fn getter(@crate_ctxt ccx, ast.def_id id) -> ty_and_params {
203225
check (ccx.item_items.contains_key(id));
204226
check (ccx.item_types.contains_key(id));
205-
auto item = ccx.item_items.get(id);
227+
auto it = ccx.item_items.get(id);
206228
auto ty = ccx.item_types.get(id);
207-
auto params = ty_params_of_item(item);
208-
209-
alt (item.node) {
210-
case (ast.item_obj(_,_,_,_,_)) {
211-
// An obj used as a type name refers to the output type of the
212-
// item (constructor).
213-
ty = middle.ty.ty_fn_ret(ty);
229+
auto params;
230+
alt (it) {
231+
case (any_item_rust(?item)) {
232+
ty = actual_type(ty, item);
233+
params = ty_params_of_item(item);
214234
}
215-
case (_) { }
235+
case (any_item_native(?native_item)) {
236+
params = ty_params_of_native_item(native_item);
237+
}
216238
}
217239

218240
ret rec(params = params, ty = ty);
@@ -242,6 +264,18 @@ fn ty_params_of_item(@ast.item item) -> vec[ast.ty_param] {
242264
}
243265
}
244266

267+
fn ty_params_of_native_item(@ast.native_item item) -> vec[ast.ty_param] {
268+
alt (item.node) {
269+
case (ast.native_item_fn(_, _, ?p, _)) {
270+
ret p;
271+
}
272+
case (_) {
273+
let vec[ast.ty_param] r = vec();
274+
ret r;
275+
}
276+
}
277+
}
278+
245279
// Item collection - a pair of bootstrap passes:
246280
//
247281
// 1. Collect the IDs of all type items (typedefs) and store them in a table.
@@ -253,24 +287,40 @@ fn ty_params_of_item(@ast.item item) -> vec[ast.ty_param] {
253287
// We then annotate the AST with the resulting types and return the annotated
254288
// AST, along with a table mapping item IDs to their types.
255289

290+
fn ty_of_fn_decl(@ty_item_table id_to_ty_item,
291+
@ty_table item_to_ty,
292+
fn(&@ast.ty ast_ty) -> @ty.t convert,
293+
fn(&ast.arg a) -> arg ty_of_arg,
294+
&ast.fn_decl decl,
295+
ast.def_id def_id) -> @ty.t {
296+
auto input_tys = _vec.map[ast.arg,arg](ty_of_arg, decl.inputs);
297+
auto output_ty = convert(decl.output);
298+
auto t_fn = plain_ty(ty.ty_fn(input_tys, output_ty));
299+
item_to_ty.insert(def_id, t_fn);
300+
ret t_fn;
301+
}
302+
256303
fn collect_item_types(session.session sess, @ast.crate crate)
257304
-> tup(@ast.crate, @ty_table, @ty_item_table) {
258305

259306
fn getter(@ty_item_table id_to_ty_item,
260307
@ty_table item_to_ty,
261308
ast.def_id id) -> ty_and_params {
262309
check (id_to_ty_item.contains_key(id));
263-
auto item = id_to_ty_item.get(id);
264-
auto ty = ty_of_item(id_to_ty_item, item_to_ty, item);
265-
auto params = ty_params_of_item(item);
266-
267-
alt (item.node) {
268-
case (ast.item_obj(_,_,_,_,_)) {
269-
// An obj used as a type name refers to the output type of the
270-
// item (constructor).
271-
ty = middle.ty.ty_fn_ret(ty);
310+
auto it = id_to_ty_item.get(id);
311+
auto ty;
312+
auto params;
313+
alt (it) {
314+
case (any_item_rust(?item)) {
315+
ty = ty_of_item(id_to_ty_item, item_to_ty, item);
316+
ty = actual_type(ty, item);
317+
params = ty_params_of_item(item);
318+
}
319+
case (any_item_native(?native_item)) {
320+
ty = ty_of_native_item(id_to_ty_item, item_to_ty,
321+
native_item);
322+
params = ty_params_of_native_item(native_item);
272323
}
273-
case (_) { }
274324
}
275325

276326
ret rec(params = params, ty = ty);
@@ -340,16 +390,9 @@ fn collect_item_types(session.session sess, @ast.crate crate)
340390
}
341391

342392
case (ast.item_fn(?ident, ?fn_info, _, ?def_id, _)) {
343-
// TODO: handle ty-params
344-
345393
auto f = bind ty_of_arg(id_to_ty_item, item_to_ty, _);
346-
auto input_tys = _vec.map[ast.arg,arg](f,
347-
fn_info.decl.inputs);
348-
auto output_ty = convert(fn_info.decl.output);
349-
350-
auto t_fn = plain_ty(ty.ty_fn(input_tys, output_ty));
351-
item_to_ty.insert(def_id, t_fn);
352-
ret t_fn;
394+
ret ty_of_fn_decl(id_to_ty_item, item_to_ty, convert, f,
395+
fn_info.decl, def_id);
353396
}
354397

355398
case (ast.item_obj(?ident, ?obj_info, _, ?def_id, _)) {
@@ -385,6 +428,30 @@ fn collect_item_types(session.session sess, @ast.crate crate)
385428
}
386429
}
387430

431+
fn ty_of_native_item(@ty_item_table id_to_ty_item,
432+
@ty_table item_to_ty,
433+
@ast.native_item it) -> @ty.t {
434+
alt (it.node) {
435+
case (ast.native_item_fn(?ident, ?fn_decl, ?params, ?def_id)) {
436+
auto get = bind getter(id_to_ty_item, item_to_ty, _);
437+
auto convert = bind ast_ty_to_ty(get, _);
438+
auto f = bind ty_of_arg(id_to_ty_item, item_to_ty, _);
439+
ret ty_of_fn_decl(id_to_ty_item, item_to_ty, convert, f,
440+
fn_decl, def_id);
441+
}
442+
case (ast.native_item_ty(_, ?def_id)) {
443+
if (item_to_ty.contains_key(def_id)) {
444+
// Avoid repeating work.
445+
ret item_to_ty.get(def_id);
446+
}
447+
auto x =
448+
@rec(struct=ty.ty_native, mut=ast.imm, cname=none[str]);
449+
item_to_ty.insert(def_id, x);
450+
ret x;
451+
}
452+
}
453+
}
454+
388455
fn get_tag_variant_types(@ty_item_table id_to_ty_item,
389456
@ty_table item_to_ty,
390457
&ast.def_id tag_id,
@@ -422,25 +489,38 @@ fn collect_item_types(session.session sess, @ast.crate crate)
422489

423490
// First pass: collect all type item IDs.
424491
auto module = crate.node.module;
425-
auto id_to_ty_item = @common.new_def_hash[@ast.item]();
492+
auto id_to_ty_item = @common.new_def_hash[any_item]();
426493
fn collect(&@ty_item_table id_to_ty_item, @ast.item i)
427494
-> @ty_item_table {
428495
alt (i.node) {
429496
case (ast.item_ty(_, _, _, ?def_id, _)) {
430-
id_to_ty_item.insert(def_id, i);
497+
id_to_ty_item.insert(def_id, any_item_rust(i));
431498
}
432499
case (ast.item_tag(_, _, _, ?def_id)) {
433-
id_to_ty_item.insert(def_id, i);
500+
id_to_ty_item.insert(def_id, any_item_rust(i));
434501
}
435502
case (ast.item_obj(_, _, _, ?def_id, _)) {
436-
id_to_ty_item.insert(def_id, i);
503+
id_to_ty_item.insert(def_id, any_item_rust(i));
437504
}
438505
case (_) { /* empty */ }
439506
}
440507
ret id_to_ty_item;
441508
}
509+
fn collect_native(&@ty_item_table id_to_ty_item, @ast.native_item i)
510+
-> @ty_item_table {
511+
alt (i.node) {
512+
case (ast.native_item_ty(_, ?def_id)) {
513+
id_to_ty_item.insert(def_id, any_item_native(i));
514+
}
515+
case (ast.native_item_fn(_, _, _, ?def_id)) {
516+
id_to_ty_item.insert(def_id, any_item_native(i));
517+
}
518+
}
519+
ret id_to_ty_item;
520+
}
442521
auto fld_1 = fold.new_identity_fold[@ty_item_table]();
443-
fld_1 = @rec(update_env_for_item = bind collect(_, _)
522+
fld_1 = @rec(update_env_for_item = bind collect(_, _),
523+
update_env_for_native_item = bind collect_native(_, _)
444524
with *fld_1);
445525
fold.fold_crate[@ty_item_table](id_to_ty_item, fld_1, crate);
446526

@@ -473,6 +553,11 @@ fn collect_item_types(session.session sess, @ast.crate crate)
473553
ret e;
474554
}
475555

556+
fn convert_native(&@env e, @ast.native_item i) -> @env {
557+
ty_of_native_item(e.id_to_ty_item, e.item_to_ty, i);
558+
ret e;
559+
}
560+
476561
fn fold_item_const(&@env e, &span sp, ast.ident i,
477562
@ast.ty t, @ast.expr ex,
478563
ast.def_id id, ast.ann a) -> @ast.item {
@@ -575,6 +660,7 @@ fn collect_item_types(session.session sess, @ast.crate crate)
575660
auto fld_2 = fold.new_identity_fold[@env]();
576661
fld_2 =
577662
@rec(update_env_for_item = bind convert(_,_),
663+
update_env_for_native_item = bind convert_native(_,_),
578664
fold_item_const = bind fold_item_const(_,_,_,_,_,_,_),
579665
fold_item_fn = bind fold_item_fn(_,_,_,_,_,_,_),
580666
fold_item_obj = bind fold_item_obj(_,_,_,_,_,_,_),
@@ -1150,6 +1236,10 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
11501236
check (fcx.ccx.item_types.contains_key(id));
11511237
t = generalize_ty(fcx.ccx, fcx.ccx.item_types.get(id));
11521238
}
1239+
case (ast.def_native_fn(?id)) {
1240+
check (fcx.ccx.item_types.contains_key(id));
1241+
t = generalize_ty(fcx.ccx, fcx.ccx.item_types.get(id));
1242+
}
11531243
case (ast.def_const(?id)) {
11541244
check (fcx.ccx.item_types.contains_key(id));
11551245
t = fcx.ccx.item_types.get(id);

0 commit comments

Comments
 (0)