Skip to content

Commit 3b683f5

Browse files
committed
rustc: Use link_name attribute for native function
Fixes issue #906
1 parent 73cd032 commit 3b683f5

File tree

12 files changed

+35
-29
lines changed

12 files changed

+35
-29
lines changed

src/comp/front/attr.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export require_unique_names;
1717
export get_attr_name;
1818
export get_meta_item_name;
1919
export get_meta_item_value_str;
20+
export get_meta_item_value_str_by_name;
2021
export get_meta_item_list;
2122
export mk_name_value_item_str;
2223
export mk_name_value_item;
@@ -85,6 +86,15 @@ fn get_meta_item_value_str(meta: @ast::meta_item) -> option::t<str> {
8586
}
8687
}
8788

89+
fn get_meta_item_value_str_by_name(attrs: [ast::attribute], name: ast::ident)
90+
-> option::t<str> {
91+
let mattrs = find_attrs_by_name(attrs, name);
92+
if vec::len(mattrs) > 0u {
93+
ret get_meta_item_value_str(attr_meta(mattrs[0]));
94+
}
95+
ret option::none;
96+
}
97+
8898
fn get_meta_item_list(meta: @ast::meta_item) -> option::t<[@ast::meta_item]> {
8999
alt meta.node {
90100
ast::meta_list(_, l) { option::some(l) }

src/comp/metadata/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ fn encode_info_for_native_item(ecx: @encode_ctxt, ebml_w: ebml::writer,
348348
encode_type(ecx, ebml_w,
349349
ty::mk_native(ecx.ccx.tcx, local_def(nitem.id)));
350350
}
351-
native_item_fn(_, fn_decl, tps) {
351+
native_item_fn(fn_decl, tps) {
352352
let letter =
353353
alt fn_decl.purity {
354354
unsafe_fn. { 'U' }

src/comp/middle/resolve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
656656
}
657657
scope_native_item(it) {
658658
alt it.node {
659-
ast::native_item_fn(_, decl, ty_params) {
659+
ast::native_item_fn(decl, ty_params) {
660660
ret lookup_in_fn(name, decl, ty_params, ns);
661661
}
662662
}
@@ -1077,7 +1077,7 @@ fn lookup_in_mie(e: env, mie: mod_index_entry, ns: namespace) ->
10771077
ret some(ast::def_native_ty(local_def(native_item.id)));
10781078
}
10791079
}
1080-
ast::native_item_fn(_, decl, _) {
1080+
ast::native_item_fn(decl, _) {
10811081
if ns == ns_value {
10821082
ret some(ast::def_native_fn(
10831083
local_def(native_item.id),

src/comp/middle/trans.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import std::map::hashmap;
1717
import std::map::{new_int_hash, new_str_hash};
1818
import std::option::{some, none};
1919
import driver::session;
20+
import front::attr;
2021
import middle::{ty, gc};
2122
import middle::freevars::*;
2223
import back::{link, abi, upcall};
@@ -5585,7 +5586,7 @@ fn native_fn_ty_param_count(cx: @crate_ctxt, id: ast::node_id) -> uint {
55855586
cx.sess.bug("register_native_fn(): native fn isn't \
55865587
actually a fn");
55875588
}
5588-
ast::native_item_fn(_, _, tps) {
5589+
ast::native_item_fn(_, tps) {
55895590
count = std::vec::len::<ast::ty_param>(tps);
55905591
}
55915592
}
@@ -5805,14 +5806,13 @@ fn item_path(item: @ast::item) -> [str] { ret [item.ident]; }
58055806
fn collect_native_item(ccx: @crate_ctxt, i: @ast::native_item, &&pt: [str],
58065807
_v: vt<[str]>) {
58075808
alt i.node {
5808-
ast::native_item_fn(link_name, _, _) {
5809+
ast::native_item_fn(_, _) {
58095810
if !ccx.obj_methods.contains_key(i.id) {
5810-
let name =
5811-
if option::is_some(link_name) {
5812-
option::get(link_name)
5813-
} else {
5814-
i.ident
5815-
};
5811+
let name = i.ident;
5812+
alt attr::get_meta_item_value_str_by_name(i.attrs, "link_name") {
5813+
none. { }
5814+
option::some(ln) { name = ln; }
5815+
}
58165816
register_native_fn(ccx, i.span, pt, name, i.id);
58175817
}
58185818
}

src/comp/middle/typeck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ mod collect {
668668
abi: ast::native_abi) -> ty::ty_param_kinds_and_ty {
669669
let no_kinds: [ast::kind] = [];
670670
alt it.node {
671-
ast::native_item_fn(_, fn_decl, params) {
671+
ast::native_item_fn(fn_decl, params) {
672672
let get = bind getter(cx, _);
673673
let convert = bind ast_ty_to_ty(cx.tcx, get, _);
674674
let f = bind ty_of_arg(cx, _);
@@ -819,7 +819,7 @@ mod collect {
819819
ast::native_item_ty. {
820820
// FIXME: Native types have no annotation. Should they? --pcw
821821
}
822-
ast::native_item_fn(_, _, _) {
822+
ast::native_item_fn(_, _) {
823823
write::ty_only(cx.tcx, i.id, tpt.ty);
824824
}
825825
}

src/comp/syntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ type native_item =
501501

502502
tag native_item_ {
503503
native_item_ty;
504-
native_item_fn(option::t<str>, fn_decl, [ty_param]);
504+
native_item_fn(fn_decl, [ty_param]);
505505
}
506506

507507
//

src/comp/syntax/fold.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,8 @@ fn noop_fold_native_item(&&ni: @native_item, fld: ast_fold) -> @native_item {
187187
node:
188188
alt ni.node {
189189
native_item_ty. { native_item_ty }
190-
native_item_fn(st, fdec, typms) {
191-
native_item_fn(st,
192-
{inputs: vec::map(fold_arg, fdec.inputs),
190+
native_item_fn(fdec, typms) {
191+
native_item_fn({inputs: vec::map(fold_arg, fdec.inputs),
193192
output: fld.fold_ty(fdec.output),
194193
purity: fdec.purity,
195194
il: fdec.il,

src/comp/syntax/parse/parser.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,13 +1956,11 @@ fn parse_item_native_fn(p: parser, attrs: [ast::attribute],
19561956
let lo = p.get_last_lo_pos();
19571957
let t = parse_fn_header(p);
19581958
let decl = parse_fn_decl(p, purity, ast::il_normal);
1959-
let link_name = none;
1960-
if p.peek() == token::EQ { p.bump(); link_name = some(parse_str(p)); }
19611959
let hi = p.get_hi_pos();
19621960
expect(p, token::SEMI);
19631961
ret @{ident: t.ident,
19641962
attrs: attrs,
1965-
node: ast::native_item_fn(link_name, decl, t.tps),
1963+
node: ast::native_item_fn(decl, t.tps),
19661964
id: p.get_id(),
19671965
span: ast_util::mk_sp(lo, hi)};
19681966
}

src/comp/syntax/print/pprust.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,9 @@ fn print_native_item(s: ps, item: @ast::native_item) {
350350

351351

352352

353-
ast::native_item_fn(lname, decl, typarams) {
353+
ast::native_item_fn(decl, typarams) {
354354
print_fn(s, decl, ast::proto_bare, item.ident, typarams,
355355
decl.constraints);
356-
alt lname {
357-
none. { }
358-
some(ss) { space(s.s); word_space(s, "="); print_string(s, ss); }
359-
}
360356
end(s); // end head-ibox
361357
word(s.s, ";");
362358
end(s); // end the outer fn box

src/comp/syntax/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ fn visit_pat<E>(p: @pat, e: E, v: vt<E>) {
179179

180180
fn visit_native_item<E>(ni: @native_item, e: E, v: vt<E>) {
181181
alt ni.node {
182-
native_item_fn(_, fd, _) { visit_fn_decl(fd, e, v); }
182+
native_item_fn(fd, _) { visit_fn_decl(fd, e, v); }
183183
native_item_ty. { }
184184
}
185185
}

src/lib/win32_os.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ native "cdecl" mod libc = "" {
44
fn write(fd: int, buf: *u8, count: uint) -> int;
55
fn fread(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
66
fn fwrite(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
7-
fn open(s: str::sbuf, flags: int, mode: uint) -> int = "_open";
8-
fn close(fd: int) -> int = "_close";
7+
#[link_name = "_open"]
8+
fn open(s: str::sbuf, flags: int, mode: uint) -> int;
9+
#[link_name = "_close"]
10+
fn close(fd: int) -> int;
911
type FILE;
1012
fn fopen(path: str::sbuf, mode: str::sbuf) -> FILE;
1113
fn _fdopen(fd: int, mode: str::sbuf) -> FILE;

src/test/run-pass/native-fn-linkname.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import std::vec;
44
import std::str;
55

66
native "cdecl" mod libc = "" {
7-
fn my_strlen(str: *u8) -> uint = "strlen";
7+
#[link_name = "strlen"]
8+
fn my_strlen(str: *u8) -> uint;
89
}
910

1011
fn strlen(str: str) -> uint unsafe {

0 commit comments

Comments
 (0)