]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Use link_name attribute for native function
authorHaitao Li <lihaitao@gmail.com>
Mon, 14 Nov 2011 13:06:39 +0000 (21:06 +0800)
committerHaitao Li <lihaitao@gmail.com>
Wed, 16 Nov 2011 15:45:07 +0000 (23:45 +0800)
Fixes issue #906

12 files changed:
src/comp/front/attr.rs
src/comp/metadata/encoder.rs
src/comp/middle/resolve.rs
src/comp/middle/trans.rs
src/comp/middle/typeck.rs
src/comp/syntax/ast.rs
src/comp/syntax/fold.rs
src/comp/syntax/parse/parser.rs
src/comp/syntax/print/pprust.rs
src/comp/syntax/visit.rs
src/lib/win32_os.rs
src/test/run-pass/native-fn-linkname.rs

index 34b1e5c430b43bfbce8c00d6142f0373b7f45320..ff1633212f7634d87ddb9e50a0d1036b4ff3fe38 100644 (file)
@@ -17,6 +17,7 @@
 export get_attr_name;
 export get_meta_item_name;
 export get_meta_item_value_str;
+export get_meta_item_value_str_by_name;
 export get_meta_item_list;
 export mk_name_value_item_str;
 export mk_name_value_item;
@@ -85,6 +86,15 @@ fn get_meta_item_value_str(meta: @ast::meta_item) -> option::t<str> {
     }
 }
 
+fn get_meta_item_value_str_by_name(attrs: [ast::attribute], name: ast::ident)
+    -> option::t<str> {
+    let mattrs = find_attrs_by_name(attrs, name);
+    if vec::len(mattrs) > 0u {
+        ret get_meta_item_value_str(attr_meta(mattrs[0]));
+    }
+    ret option::none;
+}
+
 fn get_meta_item_list(meta: @ast::meta_item) -> option::t<[@ast::meta_item]> {
     alt meta.node {
       ast::meta_list(_, l) { option::some(l) }
index ec4e57ae56b51b2eae90860a48fa48b519b9fc8b..73da789b50559a6cb8ce501b0e34980e889b58a9 100644 (file)
@@ -348,7 +348,7 @@ fn encode_info_for_native_item(ecx: @encode_ctxt, ebml_w: ebml::writer,
         encode_type(ecx, ebml_w,
                     ty::mk_native(ecx.ccx.tcx, local_def(nitem.id)));
       }
-      native_item_fn(_, fn_decl, tps) {
+      native_item_fn(fn_decl, tps) {
         let letter =
             alt fn_decl.purity {
               unsafe_fn. { 'U' }
index aa5d5ebaaa44f97db116f56a882b6dba626a30d2..ff9ee2a504f4827c142b13e5776e349ec9fde574 100644 (file)
@@ -656,7 +656,7 @@ fn in_scope(e: env, sp: span, name: ident, s: scope, ns: namespace) ->
           }
           scope_native_item(it) {
             alt it.node {
-              ast::native_item_fn(_, decl, ty_params) {
+              ast::native_item_fn(decl, ty_params) {
                 ret lookup_in_fn(name, decl, ty_params, ns);
               }
             }
@@ -1077,7 +1077,7 @@ fn lookup_in_mie(e: env, mie: mod_index_entry, ns: namespace) ->
                 ret some(ast::def_native_ty(local_def(native_item.id)));
             }
           }
-          ast::native_item_fn(_, decl, _) {
+          ast::native_item_fn(decl, _) {
             if ns == ns_value {
                 ret some(ast::def_native_fn(
                     local_def(native_item.id),
index e9e58cf52bbd47f58712d18acb78f72f74118ad7..823d9306ce2357aab9fdbd5353c9a2feaf3c514c 100644 (file)
@@ -17,6 +17,7 @@
 import std::map::{new_int_hash, new_str_hash};
 import std::option::{some, none};
 import driver::session;
+import front::attr;
 import middle::{ty, gc};
 import middle::freevars::*;
 import back::{link, abi, upcall};
@@ -5585,7 +5586,7 @@ fn native_fn_ty_param_count(cx: @crate_ctxt, id: ast::node_id) -> uint {
         cx.sess.bug("register_native_fn(): native fn isn't \
                         actually a fn");
       }
-      ast::native_item_fn(_, _, tps) {
+      ast::native_item_fn(_, tps) {
         count = std::vec::len::<ast::ty_param>(tps);
       }
     }
@@ -5805,14 +5806,13 @@ fn trans_simple_native_abi(bcx: @block_ctxt, name: str,
 fn collect_native_item(ccx: @crate_ctxt, i: @ast::native_item, &&pt: [str],
                        _v: vt<[str]>) {
     alt i.node {
-      ast::native_item_fn(link_name, _, _) {
+      ast::native_item_fn(_, _) {
         if !ccx.obj_methods.contains_key(i.id) {
-            let name =
-              if option::is_some(link_name) {
-                option::get(link_name)
-              } else {
-                i.ident
-              };
+            let name = i.ident;
+            alt attr::get_meta_item_value_str_by_name(i.attrs, "link_name") {
+              none. { }
+              option::some(ln) { name = ln; }
+            }
             register_native_fn(ccx, i.span, pt, name, i.id);
         }
       }
index fabd2b069bdaad4f084273dde28e656e2cf32497..8436d761882e7f4b957b8ccbb660d99f59be22b2 100644 (file)
@@ -668,7 +668,7 @@ fn ty_of_native_item(cx: @ctxt, it: @ast::native_item,
                          abi: ast::native_abi) -> ty::ty_param_kinds_and_ty {
         let no_kinds: [ast::kind] = [];
         alt it.node {
-          ast::native_item_fn(_, fn_decl, params) {
+          ast::native_item_fn(fn_decl, params) {
             let get = bind getter(cx, _);
             let convert = bind ast_ty_to_ty(cx.tcx, get, _);
             let f = bind ty_of_arg(cx, _);
@@ -819,7 +819,7 @@ fn convert_native(cx: @ctxt, abi: @mutable option::t<ast::native_abi>,
           ast::native_item_ty. {
             // FIXME: Native types have no annotation. Should they? --pcw
           }
-          ast::native_item_fn(_, _, _) {
+          ast::native_item_fn(_, _) {
             write::ty_only(cx.tcx, i.id, tpt.ty);
           }
         }
index ebac944d64a56be96250f21109715398de45e7d6..16d0d0d53c890fc8d2e9caf2478cc5a3727248ff 100644 (file)
 
 tag native_item_ {
     native_item_ty;
-    native_item_fn(option::t<str>, fn_decl, [ty_param]);
+    native_item_fn(fn_decl, [ty_param]);
 }
 
 //
index 126487aed983e3083ebcf6eb91d60c50783c7981..56e6aecbab1fa2759a57f368400565f70f1af2a3 100644 (file)
@@ -187,9 +187,8 @@ fn noop_fold_native_item(&&ni: @native_item, fld: ast_fold) -> @native_item {
           node:
               alt ni.node {
                 native_item_ty. { native_item_ty }
-                native_item_fn(st, fdec, typms) {
-                  native_item_fn(st,
-                                 {inputs: vec::map(fold_arg, fdec.inputs),
+                native_item_fn(fdec, typms) {
+                  native_item_fn({inputs: vec::map(fold_arg, fdec.inputs),
                                   output: fld.fold_ty(fdec.output),
                                   purity: fdec.purity,
                                   il: fdec.il,
index 4fa5baa7af4ca986f100e2b3a79f2cb56bebae8f..bb5bf4f7faa9dc26baefd36f3bb45e3e6565a6ca 100644 (file)
@@ -1956,13 +1956,11 @@ fn parse_item_native_fn(p: parser, attrs: [ast::attribute],
     let lo = p.get_last_lo_pos();
     let t = parse_fn_header(p);
     let decl = parse_fn_decl(p, purity, ast::il_normal);
-    let link_name = none;
-    if p.peek() == token::EQ { p.bump(); link_name = some(parse_str(p)); }
     let hi = p.get_hi_pos();
     expect(p, token::SEMI);
     ret @{ident: t.ident,
           attrs: attrs,
-          node: ast::native_item_fn(link_name, decl, t.tps),
+          node: ast::native_item_fn(decl, t.tps),
           id: p.get_id(),
           span: ast_util::mk_sp(lo, hi)};
 }
index d45c8a8a43a93223f979af9079ed77133bac7974..21cfaa9e072f89cce2a4d497f89325242cfe93fb 100644 (file)
@@ -350,13 +350,9 @@ fn print_native_item(s: ps, item: @ast::native_item) {
 
 
 
-      ast::native_item_fn(lname, decl, typarams) {
+      ast::native_item_fn(decl, typarams) {
         print_fn(s, decl, ast::proto_bare, item.ident, typarams,
                  decl.constraints);
-        alt lname {
-          none. { }
-          some(ss) { space(s.s); word_space(s, "="); print_string(s, ss); }
-        }
         end(s); // end head-ibox
         word(s.s, ";");
         end(s); // end the outer fn box
index 0725611eb04dce795bb32f457487cb69ec8247e9..0ad6ea2fca28f62f1435870608890a2056e7dbb5 100644 (file)
@@ -179,7 +179,7 @@ fn visit_pat<E>(p: @pat, e: E, v: vt<E>) {
 
 fn visit_native_item<E>(ni: @native_item, e: E, v: vt<E>) {
     alt ni.node {
-      native_item_fn(_, fd, _) { visit_fn_decl(fd, e, v); }
+      native_item_fn(fd, _) { visit_fn_decl(fd, e, v); }
       native_item_ty. { }
     }
 }
index 3779058cd7b28801de0dd1b4cbc3558e6c82e833..006dd17fa23e42e8e418b6408491cc9bfd9112e5 100644 (file)
@@ -4,8 +4,10 @@
     fn write(fd: int, buf: *u8, count: uint) -> int;
     fn fread(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
     fn fwrite(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
-    fn open(s: str::sbuf, flags: int, mode: uint) -> int = "_open";
-    fn close(fd: int) -> int = "_close";
+    #[link_name = "_open"]
+    fn open(s: str::sbuf, flags: int, mode: uint) -> int;
+    #[link_name = "_close"]
+    fn close(fd: int) -> int;
     type FILE;
     fn fopen(path: str::sbuf, mode: str::sbuf) -> FILE;
     fn _fdopen(fd: int, mode: str::sbuf) -> FILE;
index 7cd4a86bba26b5ee33e128a605f5cbe39acb73da..5dc6faac8980f74ea2700e7a765033410ec10d27 100644 (file)
@@ -4,7 +4,8 @@
 import std::str;
 
 native "cdecl" mod libc = "" {
-    fn my_strlen(str: *u8) -> uint = "strlen";
+    #[link_name = "strlen"]
+    fn my_strlen(str: *u8) -> uint;
 }
 
 fn strlen(str: str) -> uint unsafe {