diff --git a/doc/tutorial/ffi.md b/doc/tutorial/ffi.md index 0a1ba128e513b..64146b183d30e 100644 --- a/doc/tutorial/ffi.md +++ b/doc/tutorial/ffi.md @@ -15,7 +15,8 @@ OpenSSL libraries installed, it should 'just work'. use std; import std::{vec, str}; - native "cdecl" mod crypto { + #[abi = "cdecl"] + native mod crypto { fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8; } @@ -41,7 +42,8 @@ OpenSSL libraries installed, it should 'just work'. Before we can call `SHA1`, we have to declare it. That is what this part of the program is responsible for: - native "cdecl" mod crypto { + #[abi = "cdecl"] + native mod crypto { fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8; } @@ -52,12 +54,17 @@ of functions are available in that library. In this case, it'll change the name `crypto` to a shared library name in a platform-specific way (`libcrypto.so` on Linux, for example), and link that in. If you want the module to have a different name from the -actual library, you can say `native "cdecl" mod something = "crypto" { -... }`. +actual library, you can use the `"link_name"` attribute, like: -The `"cdecl"` word indicates the calling convention to use for -functions in this module. Most C libraries use cdecl as their calling -convention. You can also specify `"x86stdcall"` to use stdcall + #[abi = "cdecl"] + #[link_name = "crypto"] + native mod something { + fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8; + } + +The `#[abi = "cdecl"]` attribute indicates the calling convention to +use for functions in this module. Most C libraries use cdecl as their +calling convention. You can also specify `"x86stdcall"` to use stdcall instead. FIXME: Mention c-stack variants? Are they going to change? @@ -164,7 +171,9 @@ microsecond-resolution timer. use std; type timeval = {mutable tv_sec: u32, mutable tv_usec: u32}; - native "cdecl" mod libc = "" { + #[abi = "cdecl"] + #[link_name = ""] + native mod libc { fn gettimeofday(tv: *timeval, tz: *()) -> i32; } fn unix_time_in_microseconds() -> u64 unsafe { @@ -173,9 +182,9 @@ microsecond-resolution timer. ret (x.tv_sec as u64) * 1000_000_u64 + (x.tv_usec as u64); } -The `libc = ""` sets the name of the native module to the empty string -to prevent the rust compiler from trying to link it. The standard C -library is already linked with Rust programs. +The `#[link_name = ""]` sets the name of the native module to the +empty string to prevent the rust compiler from trying to link it. +The standard C library is already linked with Rust programs. A `timeval`, in C, is a struct with two 32-bit integers. Thus, we define a record type with the same contents, and declare diff --git a/src/comp/front/attr.rs b/src/comp/front/attr.rs index 34b1e5c430b43..ff1633212f763 100644 --- a/src/comp/front/attr.rs +++ b/src/comp/front/attr.rs @@ -17,6 +17,7 @@ export require_unique_names; 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 { } } +fn get_meta_item_value_str_by_name(attrs: [ast::attribute], name: ast::ident) + -> option::t { + 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) } diff --git a/src/comp/front/config.rs b/src/comp/front/config.rs index f0eceb093b3b1..26ca6850e8af5 100644 --- a/src/comp/front/config.rs +++ b/src/comp/front/config.rs @@ -45,8 +45,7 @@ fn fold_native_mod(cfg: ast::crate_cfg, nm: ast::native_mod, fld: fold::ast_fold) -> ast::native_mod { let filter = bind filter_native_item(cfg, _); let filtered_items = vec::filter_map(filter, nm.items); - 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: filtered_items}; } diff --git a/src/comp/lib/llvm.rs b/src/comp/lib/llvm.rs index 83edf143f3351..68ba8624268cc 100644 --- a/src/comp/lib/llvm.rs +++ b/src/comp/lib/llvm.rs @@ -110,7 +110,9 @@ const LLVMRealULE: uint = 13u; const LLVMRealUNE: uint = 14u; #[link_args = "-Lrustllvm"] -native "cdecl" mod llvm = "rustllvm" { +#[link_name = "rustllvm"] +#[abi = "cdecl"] +native mod llvm { type ModuleRef; type ContextRef; diff --git a/src/comp/metadata/creader.rs b/src/comp/metadata/creader.rs index 95929b3449c88..f4abe903e13b6 100644 --- a/src/comp/metadata/creader.rs +++ b/src/comp/metadata/creader.rs @@ -54,7 +54,12 @@ fn visit_item(e: env, i: @ast::item) { ret; } let cstore = e.sess.get_cstore(); - if !cstore::add_used_library(cstore, m.native_name) { ret; } + let native_name = i.ident; + alt attr::get_meta_item_value_str_by_name(i.attrs, "link_name") { + some(nn) { native_name = nn; } + none. { } + } + if !cstore::add_used_library(cstore, native_name) { ret; } for a: ast::attribute in attr::find_attrs_by_name(i.attrs, "link_args") { diff --git a/src/comp/metadata/encoder.rs b/src/comp/metadata/encoder.rs index ec4e57ae56b51..73da789b50559 100644 --- a/src/comp/metadata/encoder.rs +++ b/src/comp/metadata/encoder.rs @@ -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' } diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index aa5d5ebaaa44f..ff9ee2a504f48 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -656,7 +656,7 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, 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), diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index e9e58cf52bbd4..823d9306ce235 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -17,6 +17,7 @@ import std::map::hashmap; 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::(tps); } } @@ -5805,14 +5806,13 @@ fn item_path(item: @ast::item) -> [str] { ret [item.ident]; } 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); } } diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index fabd2b069bdaa..8436d761882e7 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -668,7 +668,7 @@ mod collect { 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 @@ mod collect { 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); } } diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index ebac944d64a56..b2117810c6db6 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -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]}; @@ -501,7 +501,7 @@ type native_item = tag native_item_ { native_item_ty; - native_item_fn(option::t, fn_decl, [ty_param]); + native_item_fn(fn_decl, [ty_param]); } // diff --git a/src/comp/syntax/fold.rs b/src/comp/syntax/fold.rs index 126487aed983e..f9b6d5739c8fe 100644 --- a/src/comp/syntax/fold.rs +++ b/src/comp/syntax/fold.rs @@ -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, @@ -453,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)} } diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 4fa5baa7af4ca..05f508edfd005 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -7,6 +7,7 @@ import token::can_begin_expr; import codemap::span; import util::interner; import ast::{node_id, spanned}; +import front::attr; tag restriction { UNRESTRICTED; RESTRICT_NO_CALL_EXPRS; } @@ -1956,13 +1957,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)}; } @@ -1979,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 @@ -1994,40 +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); - } - } 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 = id; } 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); diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index d45c8a8a43a93..93a4293a5d159 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -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 @@ -399,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); diff --git a/src/comp/syntax/visit.rs b/src/comp/syntax/visit.rs index 0725611eb04dc..0ad6ea2fca28f 100644 --- a/src/comp/syntax/visit.rs +++ b/src/comp/syntax/visit.rs @@ -179,7 +179,7 @@ fn visit_pat(p: @pat, e: E, v: vt) { fn visit_native_item(ni: @native_item, e: E, v: vt) { 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. { } } } diff --git a/src/lib/comm.rs b/src/lib/comm.rs index 4aca724a01c3b..a01db908821c2 100644 --- a/src/lib/comm.rs +++ b/src/lib/comm.rs @@ -34,7 +34,8 @@ export recv; export chan; export port; -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { type void; type rust_port; @@ -48,7 +49,8 @@ native "cdecl" mod rustrt { fn rust_port_size(po: *rust_port) -> ctypes::size_t; } -native "rust-intrinsic" mod rusti { +#[abi = "rust-intrinsic"] +native mod rusti { fn recv(port: *rustrt::rust_port) -> T; } diff --git a/src/lib/dbg.rs b/src/lib/dbg.rs index 470341308f2f7..a6c039d581a80 100644 --- a/src/lib/dbg.rs +++ b/src/lib/dbg.rs @@ -8,7 +8,8 @@ * logging. */ -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn debug_tydesc(td: *sys::type_desc); fn debug_opaque(td: *sys::type_desc, x: T); fn debug_box(td: *sys::type_desc, x: @T); diff --git a/src/lib/fs.rs b/src/lib/fs.rs index 7c8c3ef112851..d5885a3baec1b 100644 --- a/src/lib/fs.rs +++ b/src/lib/fs.rs @@ -7,7 +7,8 @@ File system manipulation import os::getcwd; import os_fs; -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn rust_file_is_dir(path: str::sbuf) -> int; } diff --git a/src/lib/io.rs b/src/lib/io.rs index afba61423775b..78a5384db1377 100644 --- a/src/lib/io.rs +++ b/src/lib/io.rs @@ -1,5 +1,6 @@ -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn rust_get_stdin() -> os::libc::FILE; fn rust_get_stdout() -> os::libc::FILE; fn rust_get_stderr() -> os::libc::FILE; diff --git a/src/lib/linux_os.rs b/src/lib/linux_os.rs index 7a0e097696072..c8c4aeed84d2c 100644 --- a/src/lib/linux_os.rs +++ b/src/lib/linux_os.rs @@ -6,7 +6,9 @@ TODO: Restructure and document // FIXME Somehow merge stuff duplicated here and macosx_os.rs. Made difficult // by https://github.com/graydon/rust/issues#issue/268 -native "cdecl" mod libc = "" { +#[link_name = ""] +#[abi = "cdecl"] +native mod libc { fn read(fd: int, buf: *u8, count: uint) -> int; fn write(fd: int, buf: *u8, count: uint) -> int; fn fread(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint; @@ -81,7 +83,8 @@ fn waitpid(pid: int) -> int { ret status; } -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn rust_getcwd() -> str; } diff --git a/src/lib/macos_os.rs b/src/lib/macos_os.rs index 9bc1fab3c0e72..db93ac649fb87 100644 --- a/src/lib/macos_os.rs +++ b/src/lib/macos_os.rs @@ -1,5 +1,7 @@ -native "cdecl" mod libc = "" { +#[link_name = ""] +#[abi = "cdecl"] +native mod libc { fn read(fd: int, buf: *u8, count: uint) -> int; fn write(fd: int, buf: *u8, count: uint) -> int; fn fread(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint; @@ -74,7 +76,8 @@ fn waitpid(pid: int) -> int { ret status; } -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn rust_getcwd() -> str; } diff --git a/src/lib/math.rs b/src/lib/math.rs index fe33b133a86c0..6c6a30a0b8ef9 100644 --- a/src/lib/math.rs +++ b/src/lib/math.rs @@ -1,6 +1,8 @@ /* Module: math */ -native "cdecl" mod libc = "" { +#[link_name = ""] +#[abi = "cdecl"] +native mod libc { fn sqrt(n: float) -> float; fn sin(n: float) -> float; fn asin(n: float) -> float; diff --git a/src/lib/posix_fs.rs b/src/lib/posix_fs.rs index 0966b96d92411..fe8dbb4d4f13a 100644 --- a/src/lib/posix_fs.rs +++ b/src/lib/posix_fs.rs @@ -1,4 +1,5 @@ -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn rust_list_files(path: str) -> [str]; } diff --git a/src/lib/ptr.rs b/src/lib/ptr.rs index df30105b11d5b..1654f9188a08f 100644 --- a/src/lib/ptr.rs +++ b/src/lib/ptr.rs @@ -3,7 +3,8 @@ Module: ptr Unsafe pointer utility functions */ -native "rust-intrinsic" mod rusti { +#[abi = "rust-intrinsic"] +native mod rusti { fn addr_of(val: T) -> *T; fn ptr_offset(ptr: *T, count: uint) -> *T; } diff --git a/src/lib/rand.rs b/src/lib/rand.rs index 777fdb027bfa4..ae610ae860840 100644 --- a/src/lib/rand.rs +++ b/src/lib/rand.rs @@ -3,7 +3,8 @@ Module: rand Random number generation */ -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { type rctx; fn rand_new() -> rctx; fn rand_next(c: rctx) -> u32; diff --git a/src/lib/run_program.rs b/src/lib/run_program.rs index f8f84fa40e53e..915e6c0f32535 100644 --- a/src/lib/run_program.rs +++ b/src/lib/run_program.rs @@ -12,7 +12,8 @@ export program_output; export spawn_process; export waitpid; -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn rust_run_program(argv: *sbuf, in_fd: int, out_fd: int, err_fd: int) -> int; } diff --git a/src/lib/str.rs b/src/lib/str.rs index 2d76aa0017ddf..4184e8b2050e2 100644 --- a/src/lib/str.rs +++ b/src/lib/str.rs @@ -16,7 +16,8 @@ export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len, contains, iter_chars, loop_chars, loop_chars_sub, escape; -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn rust_str_push(&s: str, ch: u8); } diff --git a/src/lib/sys.rs b/src/lib/sys.rs index e7d04c4bcf444..3ebfae5253457 100644 --- a/src/lib/sys.rs +++ b/src/lib/sys.rs @@ -7,7 +7,8 @@ tag type_desc { type_desc(@type_desc); } -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { // Explicitly re-export native stuff we want to be made // available outside this crate. Otherwise it's // visible-in-crate, but not re-exported. @@ -19,7 +20,8 @@ native "cdecl" mod rustrt { fn unsupervise(); } -native "rust-intrinsic" mod rusti { +#[abi = "rust-intrinsic"] +native mod rusti { fn get_type_desc() -> *type_desc; } diff --git a/src/lib/task.rs b/src/lib/task.rs index 3dd4757656208..72a73763427c3 100644 --- a/src/lib/task.rs +++ b/src/lib/task.rs @@ -50,12 +50,15 @@ export spawn; export spawn_notify; export spawn_joinable; -native "rust-intrinsic" mod rusti { +#[abi = "rust-intrinsic"] +native mod rusti { // these must run on the Rust stack so that they can swap stacks etc: fn task_sleep(time_in_us: uint); } -native "cdecl" mod rustrt = "rustrt" { +#[link_name = "rustrt"] +#[abi = "cdecl"] +native mod rustrt { // these can run on the C stack: fn pin_task(); fn unpin_task(); diff --git a/src/lib/test.rs b/src/lib/test.rs index daa86bb8b40c7..db34b391f2ff1 100644 --- a/src/lib/test.rs +++ b/src/lib/test.rs @@ -25,7 +25,8 @@ export default_test_to_task; export configure_test_task; export joinable; -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn sched_threads() -> uint; } diff --git a/src/lib/time.rs b/src/lib/time.rs index 39ce8c59b6813..2865666fdc839 100644 --- a/src/lib/time.rs +++ b/src/lib/time.rs @@ -4,7 +4,8 @@ Module: time // FIXME: Document what these functions do -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn get_time(&sec: u32, &usec: u32); fn nano_time(&ns: u64); } diff --git a/src/lib/unicode.rs b/src/lib/unicode.rs index 3f4452ed7cc57..5038721d26e54 100644 --- a/src/lib/unicode.rs +++ b/src/lib/unicode.rs @@ -148,7 +148,9 @@ mod icu { // FIXME: should be -1, change when compiler supports negative // constants - native "cdecl" mod libicu = "icuuc" { + #[link_name = "icuuc"] + #[abi = "cdecl"] + native mod libicu { fn u_hasBinaryProperty(c: UChar32, which: UProperty) -> UBool; } } diff --git a/src/lib/unsafe.rs b/src/lib/unsafe.rs index 6db33a4aba2f7..0162c10ea22b1 100644 --- a/src/lib/unsafe.rs +++ b/src/lib/unsafe.rs @@ -4,11 +4,13 @@ Module: unsafe Unsafe operations */ -native "rust-intrinsic" mod rusti { +#[abi = "rust-intrinsic"] +native mod rusti { fn cast(src: T) -> U; } -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn leak(-thing: T); } diff --git a/src/lib/vec.rs b/src/lib/vec.rs index 5fc890b535935..3d51560741b22 100644 --- a/src/lib/vec.rs +++ b/src/lib/vec.rs @@ -6,11 +6,13 @@ import option::{some, none}; import uint::next_power_of_two; import ptr::addr_of; -native "rust-intrinsic" mod rusti { +#[abi = "rust-intrinsic"] +native mod rusti { fn vec_len(&&v: [mutable? T]) -> uint; } -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn vec_reserve_shared(t: *sys::type_desc, &v: [mutable? T], n: uint); diff --git a/src/lib/win32_fs.rs b/src/lib/win32_fs.rs index 12a33cdeeedb2..6960b836ca4f0 100644 --- a/src/lib/win32_fs.rs +++ b/src/lib/win32_fs.rs @@ -1,6 +1,7 @@ -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn rust_list_files(path: str) -> [str]; } diff --git a/src/lib/win32_os.rs b/src/lib/win32_os.rs index 3779058cd7b28..c7c5f323b11de 100644 --- a/src/lib/win32_os.rs +++ b/src/lib/win32_os.rs @@ -1,11 +1,15 @@ -native "cdecl" mod libc = "" { +#[abi = "cdecl"] +#[link_name = ""] +native mod libc { fn read(fd: int, buf: *u8, count: uint) -> int; 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; @@ -37,7 +41,8 @@ type DWORD = u32; type HMODULE = uint; type LPTSTR = str::sbuf; -native "stdcall" mod kernel32 { +#[abi = "stdcall"] +native mod kernel32 { fn GetEnvironmentVariableA(n: str::sbuf, v: str::sbuf, nsize: uint) -> uint; fn SetEnvironmentVariableA(n: str::sbuf, v: str::sbuf) -> int; @@ -81,7 +86,8 @@ fn fclose(file: libc::FILE) { libc::fclose(file) } -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn rust_process_wait(handle: int) -> int; fn rust_getcwd() -> str; } diff --git a/src/test/bench/shootout-nbody.rs b/src/test/bench/shootout-nbody.rs index 72f127976fff8..a560e6e1d7a9f 100644 --- a/src/test/bench/shootout-nbody.rs +++ b/src/test/bench/shootout-nbody.rs @@ -1,7 +1,9 @@ // based on: // http://shootout.alioth.debian.org/u32/benchmark.php?test=nbody&lang=java -native "cdecl" mod llvm = "" { +#[abi = "cdecl"] +#[link_name = ""] +native mod llvm { fn sqrt(n: float) -> float; } diff --git a/src/test/compile-fail/native-unsafe-fn-called.rs b/src/test/compile-fail/native-unsafe-fn-called.rs index 6e9d7ac18fccc..6459a423611b0 100644 --- a/src/test/compile-fail/native-unsafe-fn-called.rs +++ b/src/test/compile-fail/native-unsafe-fn-called.rs @@ -1,6 +1,7 @@ // -*- rust -*- // error-pattern: safe function calls function marked unsafe -native "cdecl" mod test { +#[abi = "cdecl"] +native mod test { unsafe fn free(); } diff --git a/src/test/compile-fail/native-unsafe-fn.rs b/src/test/compile-fail/native-unsafe-fn.rs index 2a2ce4b3246e0..3d2c0e1f02b03 100644 --- a/src/test/compile-fail/native-unsafe-fn.rs +++ b/src/test/compile-fail/native-unsafe-fn.rs @@ -1,7 +1,8 @@ // -*- rust -*- // error-pattern: unsafe functions can only be called -native "cdecl" mod test { +#[abi = "cdecl"] +native mod test { unsafe fn free(); } diff --git a/src/test/run-pass/bind-native.rs b/src/test/run-pass/bind-native.rs index ecd63a91eebe3..dddbf575d8075 100644 --- a/src/test/run-pass/bind-native.rs +++ b/src/test/run-pass/bind-native.rs @@ -2,7 +2,8 @@ Can we bind native things? */ -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn pin_task(); } diff --git a/src/test/run-pass/binops.rs b/src/test/run-pass/binops.rs index 2d421947eef29..c627d28e52bc7 100644 --- a/src/test/run-pass/binops.rs +++ b/src/test/run-pass/binops.rs @@ -117,7 +117,9 @@ fn test_fn() { assert (h1 >= h2); } -native "cdecl" mod native_mod = "" { +#[abi = "cdecl"] +#[link_name = ""] +native mod test { fn do_gc(); fn unsupervise(); } diff --git a/src/test/run-pass/c-stack-as-value.rs b/src/test/run-pass/c-stack-as-value.rs index 2a13e6d35638a..d06a6d1b5ea90 100644 --- a/src/test/run-pass/c-stack-as-value.rs +++ b/src/test/run-pass/c-stack-as-value.rs @@ -1,6 +1,7 @@ // xfail-test -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn unsupervise(); } diff --git a/src/test/run-pass/c-stack-returning-int64.rs b/src/test/run-pass/c-stack-returning-int64.rs index 278417050475f..9d1536c0ce79f 100644 --- a/src/test/run-pass/c-stack-returning-int64.rs +++ b/src/test/run-pass/c-stack-returning-int64.rs @@ -1,7 +1,9 @@ use std; import std::str; -native "cdecl" mod libc = "" { +#[abi = "cdecl"] +#[link_name = ""] +native mod libc { fn atol(x: str::sbuf) -> int; fn atoll(x: str::sbuf) -> i64; } diff --git a/src/test/run-pass/conditional-compile.rs b/src/test/run-pass/conditional-compile.rs index 10b9dcd98839d..921f7a2fae78e 100644 --- a/src/test/run-pass/conditional-compile.rs +++ b/src/test/run-pass/conditional-compile.rs @@ -4,13 +4,15 @@ const b: bool = false; const b: bool = true; #[cfg(bogus)] -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { // This symbol doesn't exist and would be a link error if this // module was translated fn bogus(); } -native "cdecl" mod rustrt { } +#[abi = "cdecl"] +native mod rustrt { } #[cfg(bogus)] type t = int; @@ -79,7 +81,8 @@ fn test_in_fn_ctxt() { } mod test_native_items { - native "cdecl" mod rustrt { + #[abi = "cdecl"] + native mod rustrt { #[cfg(bogus)] fn vec_from_buf_shared(ptr: *T, count: uint) -> [T]; fn vec_from_buf_shared(ptr: *T, count: uint) -> [T]; diff --git a/src/test/run-pass/import-from-native.rs b/src/test/run-pass/import-from-native.rs index 4167f89eaa109..af19142682054 100644 --- a/src/test/run-pass/import-from-native.rs +++ b/src/test/run-pass/import-from-native.rs @@ -3,7 +3,8 @@ mod spam { fn eggs() { } } -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { import spam::{ham, eggs}; export ham; export eggs; diff --git a/src/test/run-pass/import-glob-1.rs b/src/test/run-pass/import-glob-1.rs index d9c1b653217b0..e40d6cbb9cc23 100644 --- a/src/test/run-pass/import-glob-1.rs +++ b/src/test/run-pass/import-glob-1.rs @@ -20,7 +20,9 @@ mod a1 { // | | | mod a2 { // | | | - native "cdecl" mod b1 = "" { + #[abi = "cdecl"] + #[link_name = ""] + native mod b1 { // | | | import a1::b2::*; // | <-/ -/ diff --git a/src/test/run-pass/interior-vec.rs b/src/test/run-pass/interior-vec.rs index b28367790464c..988cea0042fef 100644 --- a/src/test/run-pass/interior-vec.rs +++ b/src/test/run-pass/interior-vec.rs @@ -1,6 +1,7 @@ import rusti::vec_len; -native "rust-intrinsic" mod rusti { +#[abi = "rust-intrinsic"] +native mod rusti { fn vec_len(&&v: [T]) -> uint; } diff --git a/src/test/run-pass/issue-506.rs b/src/test/run-pass/issue-506.rs index e8b2d64d7f210..28f1a92bbf8e9 100644 --- a/src/test/run-pass/issue-506.rs +++ b/src/test/run-pass/issue-506.rs @@ -10,7 +10,8 @@ use std; import std::task; -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn task_yield(); } diff --git a/src/test/run-pass/item-attributes.rs b/src/test/run-pass/item-attributes.rs index 033504e2e0d85..42874894ab83a 100644 --- a/src/test/run-pass/item-attributes.rs +++ b/src/test/run-pass/item-attributes.rs @@ -29,7 +29,8 @@ mod test_single_attr_outer { mod mod1 { } #[attr = "val"] - native "cdecl" mod rustrt { } + #[abi = "cdecl"] + native mod rustrt { } #[attr = "val"] type t = obj { }; @@ -55,7 +56,8 @@ mod test_multi_attr_outer { #[attr1 = "val"] #[attr2 = "val"] - native "cdecl" mod rustrt { } + #[abi = "cdecl"] + native mod rustrt { } #[attr1 = "val"] #[attr2 = "val"] @@ -83,7 +85,8 @@ mod test_stmt_single_attr_outer { } #[attr = "val"] - native "cdecl" mod rustrt { + #[abi = "cdecl"] + native mod rustrt { } */ @@ -116,7 +119,8 @@ mod test_stmt_multi_attr_outer { #[attr1 = "val"] #[attr2 = "val"] - native "cdecl" mod rustrt { + #[abi = "cdecl"] + native mod rustrt { } */ @@ -182,7 +186,8 @@ mod test_other_forms { } mod test_native_items { - native "cdecl" mod rustrt { + #[abi = "cdecl"] + native mod rustrt { #[attr]; #[attr] diff --git a/src/test/run-pass/native-dupe.rs b/src/test/run-pass/native-dupe.rs index fd0b941980aa6..22c53fda4f396 100644 --- a/src/test/run-pass/native-dupe.rs +++ b/src/test/run-pass/native-dupe.rs @@ -1,11 +1,15 @@ // xfail-fast - Somehow causes check-fast to livelock?? Probably because we're // calling pin_task and that's having wierd side-effects. -native "cdecl" mod rustrt1 = "rustrt" { +#[abi = "cdecl"] +#[link_name = "rustrt"] +native mod rustrt1 { fn pin_task(); } -native "cdecl" mod rustrt2 = "rustrt" { +#[abi = "cdecl"] +#[link_name = "rustrt"] +native mod rustrt2 { fn pin_task(); } diff --git a/src/test/run-pass/native-fn-linkname.rs b/src/test/run-pass/native-fn-linkname.rs index 7cd4a86bba26b..785ec5edda5e3 100644 --- a/src/test/run-pass/native-fn-linkname.rs +++ b/src/test/run-pass/native-fn-linkname.rs @@ -3,8 +3,11 @@ use std; import std::vec; import std::str; -native "cdecl" mod libc = "" { - fn my_strlen(str: *u8) -> uint = "strlen"; +#[link_name = ""] +#[abi = "cdecl"] +native mod libc { + #[link_name = "strlen"] + fn my_strlen(str: *u8) -> uint; } fn strlen(str: str) -> uint unsafe { diff --git a/src/test/run-pass/native-opaque-type.rs b/src/test/run-pass/native-opaque-type.rs index 6ec8b4dba6a73..e8df623d1b870 100644 --- a/src/test/run-pass/native-opaque-type.rs +++ b/src/test/run-pass/native-opaque-type.rs @@ -1,6 +1,8 @@ -native "cdecl" mod libc = "" { +#[abi = "cdecl"] +#[link_name = ""] +native mod libc { type file_handle; } diff --git a/src/test/run-pass/native2.rs b/src/test/run-pass/native2.rs index 03b0038c0d6a7..07cb455cf0fe0 100644 --- a/src/test/run-pass/native2.rs +++ b/src/test/run-pass/native2.rs @@ -1,17 +1,26 @@ -native "cdecl" mod rustrt { +#[abi = "cdecl"] +native mod rustrt { fn unsupervise(); } -native "cdecl" mod bar = "" { } +#[abi = "cdecl"] +#[link_name = ""] +native mod bar { } -native "cdecl" mod zed = "" { } +#[abi = "cdecl"] +#[link_name = ""] +native mod zed { } -native "cdecl" mod libc = "" { +#[abi = "cdecl"] +#[link_name = ""] +native mod libc { fn write(fd: int, buf: *u8, count: uint) -> int; } -native "cdecl" mod baz = "" { } +#[abi = "cdecl"] +#[link_name = ""] +native mod baz { } fn main(args: [str]) { } diff --git a/src/test/run-pass/x86stdcall2.rs b/src/test/run-pass/x86stdcall2.rs index 4edb8dc73e03b..6c5093a132996 100644 --- a/src/test/run-pass/x86stdcall2.rs +++ b/src/test/run-pass/x86stdcall2.rs @@ -5,7 +5,8 @@ type LPVOID = uint; type BOOL = u8; #[cfg(target_os = "win32")] -native "stdcall" mod kernel32 { +#[abi = "stdcall"] +native mod kernel32 { fn GetProcessHeap() -> HANDLE; fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID; fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;