From ffcb4613700c968041f891985927b77f70a51c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20=C3=81vila=20de=20Esp=C3=ADndola?= Date: Wed, 23 Feb 2011 14:06:37 -0500 Subject: [PATCH] Parse the abi in native modules. --- src/comp/front/ast.rs | 6 +++++ src/comp/front/parser.rs | 44 +++++++++++++++++++++++++++--------- src/comp/middle/fold.rs | 1 + src/test/run-pass/native2.rs | 9 ++++++++ 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index 01739d19f8f..30b4d324abd 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -241,7 +241,13 @@ type _mod = rec(vec[@view_item] view_items, vec[@item] items, mod_index index); +tag native_abi { + native_abi_rust; + native_abi_cdecl; +} + type native_mod = rec(str native_name, + native_abi abi, vec[@native_item] items, native_mod_index index); type native_mod_index = hashmap[ident,@native_item]; diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index bbac78d3ef8..03b24e4e361 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -1723,7 +1723,8 @@ impure fn parse_native_item(parser p) -> @ast.native_item { } impure fn parse_native_mod_items(parser p, - str native_name) -> ast.native_mod { + str native_name, + ast.native_abi abi) -> ast.native_mod { auto index = new_str_hash[@ast.native_item](); let vec[@ast.native_item] items = vec(); while (p.peek() != token.RBRACE) { @@ -1733,28 +1734,49 @@ impure fn parse_native_mod_items(parser p, // Index the item. ast.index_native_item(index, item); } - ret rec(native_name=native_name, items=items, index=index); + ret rec(native_name=native_name, abi=abi, + items=items, index=index); +} + +fn default_native_name(session.session sess, str id) -> str { + alt (sess.get_targ_cfg().os) { + case (session.os_win32) { + ret id + ".dll"; + } + case (session.os_macos) { + ret "lib" + id + ".dylib"; + } + case (session.os_linux) { + ret "lib" + id + ".so"; + } + } } impure fn parse_item_native_mod(parser p) -> @ast.item { auto lo = p.get_span(); expect(p, token.NATIVE); - auto has_eq; - auto native_name = ""; - if (p.peek() == token.MOD) { - has_eq = true; - } else { - native_name = parse_str_lit(p); - has_eq = false; + auto abi = ast.native_abi_cdecl; + if (p.peek() != token.MOD) { + auto t = parse_str_lit(p); + if (t == "cdecl") { + } else if (t == "rust") { + abi = ast.native_abi_rust; + } else { + p.err("unsupported abi: " + t); + fail; + } } expect(p, token.MOD); auto id = parse_ident(p); - if (has_eq) { + auto native_name; + if (p.peek() == token.EQ) { expect(p, token.EQ); native_name = parse_str_lit(p); + } else { + native_name = default_native_name(p.get_session(), id); } expect(p, token.LBRACE); - auto m = parse_native_mod_items(p, native_name); + auto m = parse_native_mod_items(p, native_name, ast.native_abi_cdecl); auto hi = p.get_span(); expect(p, token.RBRACE); auto item = ast.item_native_mod(id, m, p.next_def_id()); diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs index abe94b8919e..968d4737e66 100644 --- a/src/comp/middle/fold.rs +++ b/src/comp/middle/fold.rs @@ -944,6 +944,7 @@ fn fold_native_mod[ENV](&ENV e, ast_fold[ENV] fld, } ret fld.fold_native_mod(e, rec(native_name=m.native_name, + abi=m.abi, items=items, index=index)); } diff --git a/src/test/run-pass/native2.rs b/src/test/run-pass/native2.rs index 4d2f0ad94a8..4815345add6 100644 --- a/src/test/run-pass/native2.rs +++ b/src/test/run-pass/native2.rs @@ -3,9 +3,18 @@ native "rust" mod rustrt { fn vec_buf[T](vec[T] v, uint offset) -> vbuf; } +native "rust" mod bar = "foo" { +} + +native mod zed { +} + native mod libc = "libc.dylib" { fn write(int fd, rustrt.vbuf buf, uint count) -> int; } +native "cdecl" mod baz { +} + fn main(vec[str] args) { }