Add most of the plumbing for native items and add support for parsing native type declarations.

上级 f22dc4d5
......@@ -215,7 +215,6 @@
type _obj = rec(vec[obj_field] fields,
vec[@method] methods);
tag mod_index_entry {
mie_view_item(@view_item);
mie_item(@item);
......@@ -227,7 +226,10 @@
vec[@item] items,
mod_index index);
type native_mod = rec(str native_name);
type native_mod = rec(str native_name,
vec[@native_item] items,
native_mod_index index);
type native_mod_index = hashmap[ident,@native_item];
type variant_arg = rec(@ty ty, def_id id);
type variant = rec(str name, vec[variant_arg] args, def_id id, ann ann);
......@@ -249,6 +251,11 @@
item_obj(ident, _obj, vec[ty_param], def_id, ann);
}
type native_item = spanned[native_item_];
tag native_item_ {
native_item_ty(ident, def_id);
}
fn index_view_item(mod_index index, @view_item it) {
alt (it.node) {
case(ast.view_item_use(?id, _, _)) {
......@@ -292,6 +299,14 @@ fn index_item(mod_index index, @item it) {
}
}
fn index_native_item(native_mod_index index, @native_item it) {
alt (it.node) {
case (ast.native_item_ty(?id, _)) {
index.insert(id, it);
}
}
}
//
// Local Variables:
// mode: rust
......
......@@ -1577,6 +1577,39 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool {
ret @spanned(lo, hi, item);
}
impure fn parse_item_native_type(parser p) -> @ast.native_item {
auto lo = p.get_span();
expect(p, token.TYPE);
auto id = parse_ident(p);
auto hi = p.get_span();
expect(p, token.SEMI);
auto item = ast.native_item_ty(id, p.next_def_id());
ret @spanned(lo, hi, item);
}
impure fn parse_native_item(parser p) -> @ast.native_item {
alt (p.peek()) {
case (token.TYPE) {
ret parse_item_native_type(p);
}
}
}
impure fn parse_native_mod_items(parser p,
str native_name) -> ast.native_mod {
auto index = new_str_hash[@ast.native_item]();
let vec[@ast.native_item] items = vec();
while (p.peek() != token.RBRACE) {
auto item = parse_native_item(p);
items += vec(item);
// Index the item.
ast.index_native_item(index, item);
}
ret rec(native_name=native_name, items=items, index=index);
}
impure fn parse_item_native_mod(parser p) -> @ast.item {
auto lo = p.get_span();
expect(p, token.NATIVE);
......@@ -1584,7 +1617,7 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool {
expect(p, token.MOD);
auto id = parse_ident(p);
expect(p, token.LBRACE);
auto m = rec(native_name = native_name);
auto m = parse_native_mod_items(p, native_name);
auto hi = p.get_span();
expect(p, token.RBRACE);
auto item = ast.item_native_mod(id, m, p.next_def_id());
......
......@@ -20,6 +20,7 @@
import front.ast.item;
import front.ast.view_item;
import front.ast.meta_item;
import front.ast.native_item;
import front.ast.arg;
import front.ast.pat;
import front.ast.decl;
......@@ -203,6 +204,9 @@
@ty t, vec[ast.ty_param] ty_params,
def_id id, ann a) -> @item) fold_item_ty,
(fn(&ENV e, &span sp, ident ident,
def_id id) -> @native_item) fold_native_item_ty,
(fn(&ENV e, &span sp, ident ident,
vec[ast.variant] variants,
vec[ast.ty_param] ty_params,
......@@ -244,6 +248,7 @@
// Env updates.
(fn(&ENV e, @ast.crate c) -> ENV) update_env_for_crate,
(fn(&ENV e, @item i) -> ENV) update_env_for_item,
(fn(&ENV e, @native_item i) -> ENV) update_env_for_native_item,
(fn(&ENV e, @view_item i) -> ENV) update_env_for_view_item,
(fn(&ENV e, &block b) -> ENV) update_env_for_block,
(fn(&ENV e, @stmt s) -> ENV) update_env_for_stmt,
......@@ -841,9 +846,34 @@ 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_item[ENV](&ENV env, ast_fold[ENV] fld,
@native_item i) -> @native_item {
let ENV env_ = fld.update_env_for_native_item(env, i);
if (!fld.keep_going(env_)) {
ret i;
}
alt (i.node) {
case (ast.native_item_ty(?ident, ?id)) {
ret fld.fold_native_item_ty(env_, i.span, ident, id);
}
}
}
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));
let vec[@native_item] items = vec();
auto index = new_str_hash[@ast.native_item]();
for (@native_item i in m.items) {
auto new_item = fold_native_item[ENV](e, fld, i);
append[@native_item](items, new_item);
ast.index_native_item(index, new_item);
}
ret fld.fold_native_mod(e, rec(native_name=m.native_name,
items=items,
index=index));
}
fn fold_crate[ENV](&ENV env, ast_fold[ENV] fld, @ast.crate c) -> @ast.crate {
......@@ -1130,6 +1160,11 @@ fn identity_fold_item_ty[ENV](&ENV e, &span sp, ident i,
ret @respan(sp, ast.item_ty(i, t, ty_params, id, a));
}
fn identity_fold_native_item_ty[ENV](&ENV e, &span sp, ident i,
def_id id) -> @native_item {
ret @respan(sp, ast.native_item_ty(i, id));
}
fn identity_fold_item_tag[ENV](&ENV e, &span sp, ident i,
vec[ast.variant] variants,
vec[ast.ty_param] ty_params,
......@@ -1204,6 +1239,10 @@ fn identity_update_env_for_item[ENV](&ENV e, @item i) -> ENV {
ret e;
}
fn identity_update_env_for_native_item[ENV](&ENV e, @native_item i) -> ENV {
ret e;
}
fn identity_update_env_for_view_item[ENV](&ENV e, @view_item i) -> ENV {
ret e;
}
......@@ -1308,6 +1347,8 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
fold_item_native_mod =
bind identity_fold_item_native_mod[ENV](_,_,_,_,_),
fold_item_ty = bind identity_fold_item_ty[ENV](_,_,_,_,_,_,_),
fold_native_item_ty =
bind identity_fold_native_item_ty[ENV](_,_,_,_),
fold_item_tag = bind identity_fold_item_tag[ENV](_,_,_,_,_,_),
fold_item_obj = bind identity_fold_item_obj[ENV](_,_,_,_,_,_,_),
......@@ -1325,6 +1366,8 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
update_env_for_crate = bind identity_update_env_for_crate[ENV](_,_),
update_env_for_item = bind identity_update_env_for_item[ENV](_,_),
update_env_for_native_item =
bind identity_update_env_for_native_item[ENV](_,_),
update_env_for_view_item =
bind identity_update_env_for_view_item[ENV](_,_),
update_env_for_block = bind identity_update_env_for_block[ENV](_,_),
......
native "rust" mod rustrt {
type vbuf;
}
fn main(vec[str] args) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册