Use attributes for native module ABI and link name

This patch changes how to specify ABI and link name of a native module.

Before:
  native "cdecl" mod llvm = "rustllvm" {...}

After:
  #[abi = "cdecl"]
  #[link_name = "rustllvm"]
  native mod llvm {...}

The old optional syntax for ABI and link name is no longer supported.

Fixes issue #547
This commit is contained in:
Haitao Li 2011-11-16 22:49:38 -06:00 committed by Brian Anderson
parent 7a9b66db63
commit 88f29aab27
46 changed files with 168 additions and 128 deletions

View file

@ -432,7 +432,7 @@ tag native_abi {
}
type native_mod =
{native_name: str,
{// FIXME: Removing abi from AST. Depends on Issue #1179.
abi: native_abi,
view_items: [@view_item],
items: [@native_item]};

View file

@ -452,8 +452,7 @@ fn noop_fold_mod(m: _mod, fld: ast_fold) -> _mod {
}
fn noop_fold_native_mod(nm: native_mod, fld: ast_fold) -> native_mod {
ret {native_name: nm.native_name,
abi: nm.abi,
ret {abi: nm.abi,
view_items: vec::map(fld.fold_view_item, nm.view_items),
items: vec::map(fld.fold_native_item, nm.items)}
}

View file

@ -1978,7 +1978,7 @@ fn parse_native_item(p: parser, attrs: [ast::attribute]) ->
} else { unexpected(p, p.peek()); }
}
fn parse_native_mod_items(p: parser, native_name: str, abi: ast::native_abi,
fn parse_native_mod_items(p: parser, abi: ast::native_abi,
first_item_attrs: [ast::attribute]) ->
ast::native_mod {
// Shouldn't be any view items since we've already parsed an item attr
@ -1993,63 +1993,37 @@ fn parse_native_mod_items(p: parser, native_name: str, abi: ast::native_abi,
initial_attrs = [];
items += [parse_native_item(p, attrs)];
}
ret {native_name: native_name,
abi: abi,
ret {abi: abi,
view_items: view_items,
items: items};
}
fn parse_item_native_mod(p: parser, attrs: [ast::attribute]) -> @ast::item {
let lo = p.get_last_lo_pos();
let abi = ast::native_abi_cdecl;
if !is_word(p, "mod") {
let t = parse_str(p);
if str::eq(t, "rust-intrinsic") {
abi = ast::native_abi_rust_intrinsic;
} else if str::eq(t, "cdecl") {
abi = ast::native_abi_cdecl;
} else if str::eq(t, "stdcall") {
abi = ast::native_abi_stdcall;
} else {
p.fatal("unsupported abi: " + t);
}
} else {
abi =
alt attr::get_meta_item_value_str_by_name(attrs, "abi") {
none. { ast::native_abi_cdecl }
some("rust-intrinsic") {
ast::native_abi_rust_intrinsic
}
some("cdecl") {
ast::native_abi_cdecl
}
some("stdcall") {
ast::native_abi_stdcall
}
some(t) {
p.fatal("unsupported abi: " + t);
}
};
}
expect_word(p, "mod");
let id = parse_ident(p);
let native_name;
if p.peek() == token::EQ {
expect(p, token::EQ);
native_name = parse_str(p);
} else {
native_name =
alt attr::get_meta_item_value_str_by_name(attrs, "link_name") {
none. { id }
some(nn) { nn }
};
}
expect(p, token::LBRACE);
let more_attrs = parse_inner_attrs_and_next(p);
let inner_attrs = more_attrs.inner;
let first_item_outer_attrs = more_attrs.next;
let m =
parse_native_mod_items(p, native_name, abi, first_item_outer_attrs);
let abi =
alt attr::get_meta_item_value_str_by_name(
attrs + inner_attrs, "abi") {
none. { ast::native_abi_cdecl }
some("rust-intrinsic") {
ast::native_abi_rust_intrinsic
}
some("cdecl") {
ast::native_abi_cdecl
}
some("stdcall") {
ast::native_abi_stdcall
}
some(t) {
p.fatal("unsupported abi: " + t);
}
};
let m = parse_native_mod_items(p, abi, first_item_outer_attrs);
let hi = p.get_hi_pos();
expect(p, token::RBRACE);
ret mk_item(p, lo, hi, id, ast::item_native_mod(m), attrs + inner_attrs);

View file

@ -395,24 +395,8 @@ fn print_item(s: ps, &&item: @ast::item) {
}
ast::item_native_mod(nmod) {
head(s, "native");
alt nmod.abi {
ast::native_abi_rust_intrinsic. {
word_nbsp(s, "\"rust-intrinsic\"");
}
ast::native_abi_cdecl. {
word_nbsp(s, "\"cdecl\"");
}
ast::native_abi_stdcall. {
word_nbsp(s, "\"stdcall\"");
}
}
word_nbsp(s, "mod");
word_nbsp(s, item.ident);
if !str::eq(nmod.native_name, item.ident) {
word_space(s, "=");
print_string(s, nmod.native_name);
nbsp(s);
}
bopen(s);
print_native_mod(s, nmod, item.attrs);
bclose(s, item.span);