Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimal support for native modules #223

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ TEST_XFAILS_RUSTC := $(filter-out \
linear-for-loop.rs \
multiline-comment.rs \
mutual-recursion-group.rs \
native2.rs \
obj-drop.rs \
obj-recursion.rs \
obj-with-vec.rs \
Expand Down
6 changes: 6 additions & 0 deletions src/comp/front/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ type _mod = rec(vec[@view_item] view_items,
vec[@item] items,
mod_index index);

type native_mod = rec(str native_name);

type variant_arg = rec(@ty ty, def_id id);
type variant = rec(str name, vec[variant_arg] args, def_id id, ann ann);

Expand All @@ -240,6 +242,7 @@ tag item_ {
item_const(ident, @ty, @expr, def_id, ann);
item_fn(ident, _fn, vec[ty_param], def_id, ann);
item_mod(ident, _mod, def_id);
item_native_mod(ident, native_mod, def_id);
item_ty(ident, @ty, vec[ty_param], def_id, ann);
item_tag(ident, vec[variant], vec[ty_param], def_id);
item_obj(ident, _obj, vec[ty_param], def_id, ann);
Expand Down Expand Up @@ -267,6 +270,9 @@ fn index_item(mod_index index, @item it) {
case (ast.item_mod(?id, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_native_mod(?id, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_ty(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(it));
}
Expand Down
19 changes: 19 additions & 0 deletions src/comp/front/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,20 @@ impure fn parse_item_mod(parser p) -> @ast.item {
ret @spanned(lo, hi, item);
}

impure fn parse_item_native_mod(parser p) -> @ast.item {
auto lo = p.get_span();
expect(p, token.NATIVE);
auto native_name = parse_str_lit(p);
expect(p, token.MOD);
auto id = parse_ident(p);
expect(p, token.LBRACE);
auto m = rec(native_name = native_name);
auto hi = p.get_span();
expect(p, token.RBRACE);
auto item = ast.item_native_mod(id, m, p.next_def_id());
ret @spanned(lo, hi, item);
}

impure fn parse_item_type(parser p) -> @ast.item {
auto lo = p.get_span();
expect(p, token.TYPE);
Expand Down Expand Up @@ -1695,6 +1709,11 @@ impure fn parse_item(parser p) -> @ast.item {
check (lyr == ast.layer_value);
ret parse_item_mod(p);
}
case (token.NATIVE) {
check (eff == ast.eff_pure);
check (lyr == ast.layer_value);
ret parse_item_native_mod(p);
}
case (token.TYPE) {
check (eff == ast.eff_pure);
ret parse_item_type(p);
Expand Down
31 changes: 29 additions & 2 deletions src/comp/middle/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ type ast_fold[ENV] =
(fn(&ENV e, &span sp, ident ident,
&ast._mod m, def_id id) -> @item) fold_item_mod,

(fn(&ENV e, &span sp, ident ident,
&ast.native_mod m, def_id id) -> @item) fold_item_native_mod,

(fn(&ENV e, &span sp, ident ident,
@ty t, vec[ast.ty_param] ty_params,
def_id id, ann a) -> @item) fold_item_ty,
Expand Down Expand Up @@ -227,6 +230,8 @@ type ast_fold[ENV] =

(fn(&ENV e, &ast._mod m) -> ast._mod) fold_mod,

(fn(&ENV e, &ast.native_mod m) -> ast.native_mod) fold_native_mod,

(fn(&ENV e, &span sp,
&ast._mod m) -> @ast.crate) fold_crate,

Expand Down Expand Up @@ -768,6 +773,11 @@ fn fold_item[ENV](&ENV env, ast_fold[ENV] fld, @item i) -> @item {
ret fld.fold_item_mod(env_, i.span, ident, mm_, id);
}

case (ast.item_native_mod(?ident, ?mm, ?id)) {
let ast.native_mod mm_ = fold_native_mod[ENV](env_, fld, mm);
ret fld.fold_item_native_mod(env_, i.span, ident, mm_, id);
}

case (ast.item_ty(?ident, ?ty, ?params, ?id, ?ann)) {
let @ast.ty ty_ = fold_ty[ENV](env_, fld, ty);
ret fld.fold_item_ty(env_, i.span, ident, ty_, params, id, ann);
Expand Down Expand Up @@ -798,7 +808,6 @@ fn fold_item[ENV](&ENV env, ast_fold[ENV] fld, @item i) -> @item {
fail;
}


fn fold_mod[ENV](&ENV e, ast_fold[ENV] fld, &ast._mod m) -> ast._mod {

let vec[@view_item] view_items = vec();
Expand All @@ -818,7 +827,12 @@ fn fold_mod[ENV](&ENV e, ast_fold[ENV] fld, &ast._mod m) -> ast._mod {
}

ret fld.fold_mod(e, rec(view_items=view_items, items=items, index=index));
}
}

fn fold_native_mod[ENV](&ENV e, ast_fold[ENV] fld,
&ast.native_mod m) -> ast.native_mod {
ret fld.fold_native_mod(e, rec(native_name = m.native_name));
}

fn fold_crate[ENV](&ENV env, ast_fold[ENV] fld, @ast.crate c) -> @ast.crate {
let ENV env_ = fld.update_env_for_crate(env, c);
Expand Down Expand Up @@ -1092,6 +1106,11 @@ fn identity_fold_item_mod[ENV](&ENV e, &span sp, ident i,
ret @respan(sp, ast.item_mod(i, m, id));
}

fn identity_fold_item_native_mod[ENV](&ENV e, &span sp, ident i,
&ast.native_mod m, def_id id) -> @item {
ret @respan(sp, ast.item_native_mod(i, m, id));
}

fn identity_fold_item_ty[ENV](&ENV e, &span sp, ident i,
@ty t, vec[ast.ty_param] ty_params,
def_id id, ann a) -> @item {
Expand Down Expand Up @@ -1146,6 +1165,11 @@ fn identity_fold_mod[ENV](&ENV e, &ast._mod m) -> ast._mod {
ret m;
}

fn identity_fold_native_mod[ENV](&ENV e,
&ast.native_mod m) -> ast.native_mod {
ret m;
}

fn identity_fold_crate[ENV](&ENV e, &span sp, &ast._mod m) -> @ast.crate {
ret @respan(sp, rec(module=m));
}
Expand Down Expand Up @@ -1268,6 +1292,8 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
fold_item_const= bind identity_fold_item_const[ENV](_,_,_,_,_,_,_),
fold_item_fn = bind identity_fold_item_fn[ENV](_,_,_,_,_,_,_),
fold_item_mod = bind identity_fold_item_mod[ENV](_,_,_,_,_),
fold_item_native_mod =
bind identity_fold_item_native_mod[ENV](_,_,_,_,_),
fold_item_ty = bind identity_fold_item_ty[ENV](_,_,_,_,_,_,_),
fold_item_tag = bind identity_fold_item_tag[ENV](_,_,_,_,_,_),
fold_item_obj = bind identity_fold_item_obj[ENV](_,_,_,_,_,_,_),
Expand All @@ -1280,6 +1306,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
fold_block = bind identity_fold_block[ENV](_,_,_),
fold_fn = bind identity_fold_fn[ENV](_,_,_,_,_,_),
fold_mod = bind identity_fold_mod[ENV](_,_),
fold_native_mod = bind identity_fold_native_mod[ENV](_,_),
fold_crate = bind identity_fold_crate[ENV](_,_,_),
fold_obj = bind identity_fold_obj[ENV](_,_,_),

Expand Down
4 changes: 4 additions & 0 deletions src/comp/middle/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ fn collect_item_types(session.session sess, @ast.crate crate)
}

case (ast.item_mod(_, _, _)) { fail; }
case (ast.item_native_mod(_, _, _)) { fail; }
}
}

Expand Down Expand Up @@ -455,6 +456,9 @@ fn collect_item_types(session.session sess, @ast.crate crate)
case (ast.item_mod(_, _, _)) {
// ignore item_mod, it has no type.
}
case (ast.item_native_mod(_, _, _)) {
// ignore item_native_mod, it has no type.
}
case (_) {
// This call populates the ty_table with the converted type of
// the item in passing; we don't need to do anything else.
Expand Down
5 changes: 5 additions & 0 deletions src/test/run-pass/native2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
native "rust" mod rustrt {
}

fn main(vec[str] args) {
}