]> git.lizzy.rs Git - rust.git/commitdiff
add unsafe tags into various points in the translation chains
authorNiko Matsakis <niko@alum.mit.edu>
Fri, 7 Oct 2011 19:05:38 +0000 (12:05 -0700)
committerBrian Anderson <banderson@mozilla.com>
Wed, 12 Oct 2011 23:33:06 +0000 (16:33 -0700)
and so forth

src/comp/back/link.rs
src/comp/lib/llvm.rs
src/comp/metadata/creader.rs
src/comp/middle/trans_build.rs
src/comp/middle/trans_common.rs
src/test/stdtest/str.rs
src/test/stdtest/vec.rs

index 52024d6f5b728418414f301847660977bcf69109..01f44cd2edbfaf1e9d77eaecdae617e3dc7b8ea2 100644 (file)
 }
 
 fn llvm_err(sess: session::session, msg: str) {
-    let buf = llvm::LLVMRustGetLastError();
-    if buf == std::ptr::null() {
-        sess.fatal(msg);
-    } else { sess.fatal(msg + ": " + str::str_from_cstr(buf)); }
+    unsafe {
+        let buf = llvm::LLVMRustGetLastError();
+        if buf == std::ptr::null() {
+            sess.fatal(msg);
+        } else { sess.fatal(msg + ": " + str::str_from_cstr(buf)); }
+    }
 }
 
 fn link_intrinsics(sess: session::session, llmod: ModuleRef) {
index c47f75135d7c17b579860c4e1b5eb97426d9159e..2cf85bcd218c668973edbaeb327697202f1ed34a 100644 (file)
@@ -961,7 +961,9 @@ fn tys_str(names: type_names, outer: [TypeRef], tys: [TypeRef]) -> str {
         let out_ty: TypeRef = llvm::LLVMGetReturnType(ty);
         let n_args: uint = llvm::LLVMCountParamTypes(ty);
         let args: [TypeRef] = vec::init_elt::<TypeRef>(0 as TypeRef, n_args);
-        llvm::LLVMGetParamTypes(ty, vec::to_ptr(args));
+        unsafe {
+            llvm::LLVMGetParamTypes(ty, vec::to_ptr(args));
+        }
         s += tys_str(names, outer, args);
         s += ") -> ";
         s += type_to_str_inner(names, outer, out_ty);
@@ -971,7 +973,9 @@ fn tys_str(names: type_names, outer: [TypeRef], tys: [TypeRef]) -> str {
         let s: str = "{";
         let n_elts: uint = llvm::LLVMCountStructElementTypes(ty);
         let elts: [TypeRef] = vec::init_elt::<TypeRef>(0 as TypeRef, n_elts);
-        llvm::LLVMGetStructElementTypes(ty, vec::to_ptr(elts));
+        unsafe {
+            llvm::LLVMGetStructElementTypes(ty, vec::to_ptr(elts));
+        }
         s += tys_str(names, outer, elts);
         s += "}";
         ret s;
@@ -1011,7 +1015,9 @@ fn float_width(llt: TypeRef) -> uint {
 
 fn fn_ty_param_tys(fn_ty: TypeRef) -> [TypeRef] {
     let args = vec::init_elt(0 as TypeRef, llvm::LLVMCountParamTypes(fn_ty));
-    llvm::LLVMGetParamTypes(fn_ty, vec::to_ptr(args));
+    unsafe {
+        llvm::LLVMGetParamTypes(fn_ty, vec::to_ptr(args));
+    }
     ret args;
 }
 
index 90da54f36b314aa6d7ab066a1b2c196cb8146ba6..1dd51a8b680abb7a5c6a4e60599c99666b9d8fe0 100644 (file)
@@ -170,24 +170,26 @@ fn find_library_crate_aux(nn: {prefix: str, suffix: str}, crate_name: str,
 }
 
 fn get_metadata_section(filename: str) -> option::t<@[u8]> {
-    let mb = str::as_buf(filename, {|buf|
-        llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
-    });
-    if mb as int == 0 { ret option::none::<@[u8]>; }
-    let of = mk_object_file(mb);
-    let si = mk_section_iter(of.llof);
-    while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
-        let name_buf = llvm::LLVMGetSectionName(si.llsi);
-        let name = str::str_from_cstr(name_buf);
-        if str::eq(name, x86::get_meta_sect_name()) {
-            let cbuf = llvm::LLVMGetSectionContents(si.llsi);
-            let csz = llvm::LLVMGetSectionSize(si.llsi);
-            let cvbuf: *u8 = std::unsafe::reinterpret_cast(cbuf);
-            ret option::some::<@[u8]>(@vec::unsafe::from_buf(cvbuf, csz));
+    unsafe {
+        let mb = str::as_buf(filename, {|buf|
+            llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
+                                       });
+        if mb as int == 0 { ret option::none::<@[u8]>; }
+        let of = mk_object_file(mb);
+        let si = mk_section_iter(of.llof);
+        while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
+            let name_buf = llvm::LLVMGetSectionName(si.llsi);
+            let name = str::str_from_cstr(name_buf);
+            if str::eq(name, x86::get_meta_sect_name()) {
+                let cbuf = llvm::LLVMGetSectionContents(si.llsi);
+                let csz = llvm::LLVMGetSectionSize(si.llsi);
+                let cvbuf: *u8 = std::unsafe::reinterpret_cast(cbuf);
+                ret option::some::<@[u8]>(@vec::unsafe::from_buf(cvbuf, csz));
+            }
+            llvm::LLVMMoveToNextSection(si.llsi);
         }
-        llvm::LLVMMoveToNextSection(si.llsi);
+        ret option::none::<@[u8]>;
     }
-    ret option::none::<@[u8]>;
 }
 
 fn load_library_crate(sess: session::session, span: span, ident: ast::ident,
index 384cbb49ff9593f6a9ac3958e901f95fd1f95dc2..7cf9a9c27b3f2ec089d3b883746ade752d31625f 100644 (file)
@@ -37,8 +37,10 @@ fn AggregateRet(cx: @block_ctxt, RetVals: [ValueRef]) {
     if cx.unreachable { ret; }
     assert (!cx.terminated);
     cx.terminated = true;
-    llvm::LLVMBuildAggregateRet(B(cx), vec::to_ptr(RetVals),
-                                vec::len(RetVals));
+    unsafe {
+        llvm::LLVMBuildAggregateRet(B(cx), vec::to_ptr(RetVals),
+                                    vec::len(RetVals));
+    }
 }
 
 fn Br(cx: @block_ctxt, Dest: BasicBlockRef) {
@@ -88,8 +90,10 @@ fn Invoke(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef],
     if cx.unreachable { ret; }
     assert (!cx.terminated);
     cx.terminated = true;
-    llvm::LLVMBuildInvoke(B(cx), Fn, vec::to_ptr(Args),
-                          vec::len(Args), Then, Catch, noname());
+    unsafe {
+        llvm::LLVMBuildInvoke(B(cx), Fn, vec::to_ptr(Args),
+                              vec::len(Args), Then, Catch, noname());
+    }
 }
 
 fn FastInvoke(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef],
@@ -97,9 +101,11 @@ fn FastInvoke(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef],
     if cx.unreachable { ret; }
     assert (!cx.terminated);
     cx.terminated = true;
-    let v = llvm::LLVMBuildInvoke(B(cx), Fn, vec::to_ptr(Args),
-                                  vec::len(Args), Then, Catch, noname());
-    llvm::LLVMSetInstructionCallConv(v, lib::llvm::LLVMFastCallConv);
+    unsafe {
+        let v = llvm::LLVMBuildInvoke(B(cx), Fn, vec::to_ptr(Args),
+                                      vec::len(Args), Then, Catch, noname());
+        llvm::LLVMSetInstructionCallConv(v, lib::llvm::LLVMFastCallConv);
+    }
 }
 
 fn Unreachable(cx: @block_ctxt) {
@@ -311,15 +317,19 @@ fn Store(cx: @block_ctxt, Val: ValueRef, Ptr: ValueRef) {
 
 fn GEP(cx: @block_ctxt, Pointer: ValueRef, Indices: [ValueRef]) -> ValueRef {
     if cx.unreachable { ret llvm::LLVMGetUndef(T_ptr(T_nil())); }
-    ret llvm::LLVMBuildGEP(B(cx), Pointer, vec::to_ptr(Indices),
-                           vec::len(Indices), noname());
+    unsafe {
+        ret llvm::LLVMBuildGEP(B(cx), Pointer, vec::to_ptr(Indices),
+                               vec::len(Indices), noname());
+    }
 }
 
 fn InBoundsGEP(cx: @block_ctxt, Pointer: ValueRef, Indices: [ValueRef]) ->
    ValueRef {
     if cx.unreachable { ret llvm::LLVMGetUndef(T_ptr(T_nil())); }
-    ret llvm::LLVMBuildInBoundsGEP(B(cx), Pointer, vec::to_ptr(Indices),
-                                   vec::len(Indices), noname());
+    unsafe {
+        ret llvm::LLVMBuildInBoundsGEP(B(cx), Pointer, vec::to_ptr(Indices),
+                                       vec::len(Indices), noname());
+    }
 }
 
 fn StructGEP(cx: @block_ctxt, Pointer: ValueRef, Idx: uint) -> ValueRef {
@@ -460,9 +470,11 @@ fn Phi(cx: @block_ctxt, Ty: TypeRef, vals: [ValueRef], bbs: [BasicBlockRef])
     if cx.unreachable { ret llvm::LLVMGetUndef(Ty); }
     assert (vec::len::<ValueRef>(vals) == vec::len::<BasicBlockRef>(bbs));
     let phi = EmptyPhi(cx, Ty);
-    llvm::LLVMAddIncoming(phi, vec::to_ptr(vals), vec::to_ptr(bbs),
-                          vec::len(vals));
-    ret phi;
+    unsafe {
+        llvm::LLVMAddIncoming(phi, vec::to_ptr(vals), vec::to_ptr(bbs),
+                              vec::len(vals));
+        ret phi;
+    }
 }
 
 fn AddIncomingToPhi(phi: ValueRef, val: ValueRef, bb: BasicBlockRef) {
@@ -480,26 +492,32 @@ fn _UndefReturn(Fn: ValueRef) -> ValueRef {
 }
 
 fn Call(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef]) -> ValueRef {
-    if cx.unreachable { ret _UndefReturn(Fn); }
-    ret llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
-                            vec::len(Args), noname());
+    unsafe {
+        if cx.unreachable { ret _UndefReturn(Fn); }
+        ret llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
+                                vec::len(Args), noname());
+    }
 }
 
 fn FastCall(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef]) -> ValueRef {
-    if cx.unreachable { ret _UndefReturn(Fn); }
-    let v = llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
-                                vec::len(Args), noname());
-    llvm::LLVMSetInstructionCallConv(v, lib::llvm::LLVMFastCallConv);
-    ret v;
+    unsafe {
+        if cx.unreachable { ret _UndefReturn(Fn); }
+        let v = llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
+                                    vec::len(Args), noname());
+        llvm::LLVMSetInstructionCallConv(v, lib::llvm::LLVMFastCallConv);
+        ret v;
+    }
 }
 
 fn CallWithConv(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef], Conv: uint)
    -> ValueRef {
-    if cx.unreachable { ret _UndefReturn(Fn); }
-    let v = llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
-                                vec::len(Args), noname());
-    llvm::LLVMSetInstructionCallConv(v, Conv);
-    ret v;
+    unsafe {
+        if cx.unreachable { ret _UndefReturn(Fn); }
+        let v = llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
+                                    vec::len(Args), noname());
+        llvm::LLVMSetInstructionCallConv(v, Conv);
+        ret v;
+    }
 }
 
 fn Select(cx: @block_ctxt, If: ValueRef, Then: ValueRef, Else: ValueRef) ->
@@ -568,7 +586,10 @@ fn Trap(cx: @block_ctxt) {
     });
     assert (T as int != 0);
     let Args: [ValueRef] = [];
-    llvm::LLVMBuildCall(b, T, vec::to_ptr(Args), vec::len(Args), noname());
+    unsafe {
+        llvm::LLVMBuildCall(b, T, vec::to_ptr(Args),
+                            vec::len(Args), noname());
+    }
 }
 
 fn LandingPad(cx: @block_ctxt, Ty: TypeRef, PersFn: ValueRef,
index 9e0f99685a5d77f83c8818a005322f930c0b80b6..945530d87d2af3ed1c69795228b1b453036ddc73 100644 (file)
@@ -518,8 +518,10 @@ fn T_size_t() -> TypeRef {
 }
 
 fn T_fn(inputs: [TypeRef], output: TypeRef) -> TypeRef {
-    ret llvm::LLVMFunctionType(output, to_ptr(inputs),
-                               std::vec::len::<TypeRef>(inputs), False);
+    unsafe {
+        ret llvm::LLVMFunctionType(output, to_ptr(inputs),
+                                   std::vec::len::<TypeRef>(inputs), False);
+    }
 }
 
 fn T_fn_pair(cx: crate_ctxt, tfn: TypeRef) -> TypeRef {
@@ -529,7 +531,9 @@ fn T_fn_pair(cx: crate_ctxt, tfn: TypeRef) -> TypeRef {
 fn T_ptr(t: TypeRef) -> TypeRef { ret llvm::LLVMPointerType(t, 0u); }
 
 fn T_struct(elts: [TypeRef]) -> TypeRef {
-    ret llvm::LLVMStructType(to_ptr(elts), std::vec::len(elts), False);
+    unsafe {
+        ret llvm::LLVMStructType(to_ptr(elts), std::vec::len(elts), False);
+    }
 }
 
 fn T_named_struct(name: str) -> TypeRef {
@@ -538,7 +542,9 @@ fn T_named_struct(name: str) -> TypeRef {
 }
 
 fn set_struct_body(t: TypeRef, elts: [TypeRef]) {
-    llvm::LLVMStructSetBody(t, to_ptr(elts), std::vec::len(elts), False);
+    unsafe {
+        llvm::LLVMStructSetBody(t, to_ptr(elts), std::vec::len(elts), False);
+    }
 }
 
 fn T_empty_struct() -> TypeRef { ret T_struct([]); }
@@ -578,12 +584,15 @@ fn T_task() -> TypeRef {
 fn T_tydesc_field(cx: crate_ctxt, field: int) -> TypeRef {
     // Bit of a kludge: pick the fn typeref out of the tydesc..
 
-    let tydesc_elts: [TypeRef] =
-        std::vec::init_elt::<TypeRef>(T_nil(), abi::n_tydesc_fields as uint);
-    llvm::LLVMGetStructElementTypes(cx.tydesc_type,
-                                    to_ptr::<TypeRef>(tydesc_elts));
-    let t = llvm::LLVMGetElementType(tydesc_elts[field]);
-    ret t;
+    unsafe {
+        let tydesc_elts: [TypeRef] =
+            std::vec::init_elt::<TypeRef>(T_nil(),
+                                          abi::n_tydesc_fields as uint);
+        llvm::LLVMGetStructElementTypes(cx.tydesc_type,
+                                        to_ptr::<TypeRef>(tydesc_elts));
+        let t = llvm::LLVMGetElementType(tydesc_elts[field]);
+        ret t;
+    }
 }
 
 fn T_glue_fn(cx: crate_ctxt) -> TypeRef {
@@ -790,30 +799,42 @@ fn C_postr(s: str) -> ValueRef {
 }
 
 fn C_zero_byte_arr(size: uint) -> ValueRef {
-    let i = 0u;
-    let elts: [ValueRef] = [];
-    while i < size { elts += [C_u8(0u)]; i += 1u; }
-    ret llvm::LLVMConstArray(T_i8(), std::vec::to_ptr(elts),
-                             std::vec::len(elts));
+    unsafe {
+        let i = 0u;
+        let elts: [ValueRef] = [];
+        while i < size { elts += [C_u8(0u)]; i += 1u; }
+        ret llvm::LLVMConstArray(T_i8(), std::vec::to_ptr(elts),
+                                 std::vec::len(elts));
+    }
 }
 
 fn C_struct(elts: [ValueRef]) -> ValueRef {
-    ret llvm::LLVMConstStruct(std::vec::to_ptr(elts), std::vec::len(elts),
-                              False);
+    unsafe {
+        ret llvm::LLVMConstStruct(std::vec::to_ptr(elts), std::vec::len(elts),
+                                  False);
+    }
 }
 
 fn C_named_struct(T: TypeRef, elts: [ValueRef]) -> ValueRef {
-    ret llvm::LLVMConstNamedStruct(T, std::vec::to_ptr(elts),
-                                   std::vec::len(elts));
+    unsafe {
+        ret llvm::LLVMConstNamedStruct(T, std::vec::to_ptr(elts),
+                                       std::vec::len(elts));
+    }
 }
 
 fn C_array(ty: TypeRef, elts: [ValueRef]) -> ValueRef {
-    ret llvm::LLVMConstArray(ty, std::vec::to_ptr(elts), std::vec::len(elts));
+    unsafe {
+        ret llvm::LLVMConstArray(ty, std::vec::to_ptr(elts),
+                                 std::vec::len(elts));
+    }
 }
 
 fn C_bytes(bytes: [u8]) -> ValueRef {
-    ret llvm::LLVMConstString(unsafe::reinterpret_cast(vec::to_ptr(bytes)),
-                              vec::len(bytes), False);
+    unsafe {
+        ret llvm::LLVMConstString(
+            unsafe::reinterpret_cast(vec::to_ptr(bytes)),
+            vec::len(bytes), False);
+    }
 }
 
 fn C_shape(ccx: @crate_ctxt, bytes: [u8]) -> ValueRef {
index f1839a0aa91dfa5ecc5e73e3c386447d3930339e..8cce515ec3e7d022f81d82daf4b74ca450072bda 100644 (file)
@@ -256,7 +256,7 @@ fn unsafe_from_bytes() {
 }
 
 #[test]
-fn str_from_cstr() {
+unsafe fn str_from_cstr() {
     let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
     let b = vec::to_ptr(a);
     let c = str::str_from_cstr(b);
@@ -278,7 +278,7 @@ fn as_buf_small() {
 }
 
 #[test]
-fn as_buf2() {
+unsafe fn as_buf2() {
     let s = "hello";
     let sb = str::as_buf(s, {|b| b });
     let s_cstr = str::str_from_cstr(sb);
index 5e5de913914aeed78ef260d52283b143b38c687b..313db7b71f107b600cfe62136fa5da8a1096182f 100644 (file)
@@ -21,7 +21,7 @@ fn square_if_odd(&&n: uint) -> option::t<uint> {
 fn add(&&x: uint, &&y: uint) -> uint { ret x + y; }
 
 #[test]
-fn test_unsafe_ptrs() {
+unsafe fn test_unsafe_ptrs() {
     // Test on-stack copy-from-buf.
     let a = [1, 2, 3];
     let ptr = vec::to_ptr(a);