]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/extern-fn-struct-passing-abi/test.rs
Rollup merge of #41250 - kennytm:fix-41228, r=nikomatsakis
[rust.git] / src / test / run-make / extern-fn-struct-passing-abi / test.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Passing structs via FFI should work regardless of whether
12 // they get passed in multiple registers, byval pointers or the stack
13
14 #[derive(Clone, Copy, Debug, PartialEq)]
15 #[repr(C)]
16 struct Rect {
17     a: i32,
18     b: i32,
19     c: i32,
20     d: i32
21 }
22
23 #[derive(Clone, Copy, Debug, PartialEq)]
24 #[repr(C)]
25 struct BiggerRect {
26     s: Rect,
27     a: i32,
28     b: i32
29 }
30
31 #[derive(Clone, Copy, Debug, PartialEq)]
32 #[repr(C)]
33 struct FloatRect {
34     a: i32,
35     b: i32,
36     c: f64
37 }
38
39 #[derive(Clone, Copy, Debug, PartialEq)]
40 #[repr(C)]
41 struct Huge {
42     a: i32,
43     b: i32,
44     c: i32,
45     d: i32,
46     e: i32
47 }
48
49 #[derive(Clone, Copy, Debug, PartialEq)]
50 #[repr(C)]
51 struct FloatPoint {
52     x: f64,
53     y: f64
54 }
55
56 #[link(name = "test", kind = "static")]
57 extern {
58     fn byval_rect(a: i32, b: i32, c: i32, d: i32, e: i32, s: Rect);
59
60     fn byval_many_rect(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, s: Rect);
61
62     fn byval_rect_floats(a: f32, b: f32, c: f64, d: f32, e: f32,
63                          f: f32, g: f64, s: Rect, t: FloatRect);
64
65     fn byval_rect_with_float(a: i32, b: i32, c: f32, d: i32, e: i32, f: i32, s: Rect);
66
67     fn split_rect(a: i32, b: i32, s: Rect);
68
69     fn split_rect_floats(a: f32, b: f32, s: FloatRect);
70
71     fn split_rect_with_floats(a: i32, b: i32, c: f32, d: i32, e: f32, f: i32, s: Rect);
72
73     fn split_and_byval_rect(a: i32, b: i32, c: i32, s: Rect, t: Rect);
74
75     fn split_ret_byval_struct(a: i32, b: i32, s: Rect) -> Rect;
76
77     fn sret_byval_struct(a: i32, b: i32, c: i32, d: i32, s: Rect) -> BiggerRect;
78
79     fn sret_split_struct(a: i32, b: i32, s: Rect) -> BiggerRect;
80
81     fn huge_struct(s: Huge) -> Huge;
82
83     fn float_point(p: FloatPoint) -> FloatPoint;
84 }
85
86 fn main() {
87     let s = Rect { a: 553, b: 554, c: 555, d: 556 };
88     let t = BiggerRect { s: s, a: 27834, b: 7657 };
89     let u = FloatRect { a: 3489, b: 3490, c: 8. };
90     let v = Huge { a: 5647, b: 5648, c: 5649, d: 5650, e: 5651 };
91     let p = FloatPoint { x: 5., y: -3. };
92
93     unsafe {
94         byval_rect(1, 2, 3, 4, 5, s);
95         byval_many_rect(1, 2, 3, 4, 5, 6, s);
96         byval_rect_floats(1., 2., 3., 4., 5., 6., 7., s, u);
97         byval_rect_with_float(1, 2, 3.0, 4, 5, 6, s);
98         split_rect(1, 2, s);
99         split_rect_floats(1., 2., u);
100         split_rect_with_floats(1, 2, 3.0, 4, 5.0, 6, s);
101         split_and_byval_rect(1, 2, 3, s, s);
102         split_rect(1, 2, s);
103         assert_eq!(huge_struct(v), v);
104         assert_eq!(split_ret_byval_struct(1, 2, s), s);
105         assert_eq!(sret_byval_struct(1, 2, 3, 4, s), t);
106         assert_eq!(sret_split_struct(1, 2, s), t);
107         assert_eq!(float_point(p), p);
108     }
109 }