]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/abi.rs
Auto merge of #31333 - lambda:31273-abort-on-stack-overflow, r=brson
[rust.git] / src / libsyntax / abi.rs
1 // Copyright 2012-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 pub use self::Os::*;
12 pub use self::Abi::*;
13 pub use self::Architecture::*;
14 pub use self::AbiArchitecture::*;
15
16 use std::fmt;
17
18 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
19 pub enum Os {
20     OsWindows,
21     OsMacos,
22     OsLinux,
23     OsAndroid,
24     OsFreebsd,
25     OsiOS,
26     OsDragonfly,
27     OsBitrig,
28     OsNetbsd,
29     OsOpenbsd,
30     OsNaCl,
31     OsSolaris,
32 }
33
34 #[derive(PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Clone, Copy, Debug)]
35 pub enum Abi {
36     // NB: This ordering MUST match the AbiDatas array below.
37     // (This is ensured by the test indices_are_correct().)
38
39     // Single platform ABIs come first (`for_arch()` relies on this)
40     Cdecl,
41     Stdcall,
42     Fastcall,
43     Vectorcall,
44     Aapcs,
45     Win64,
46
47     // Multiplatform ABIs second
48     Rust,
49     C,
50     System,
51     RustIntrinsic,
52     RustCall,
53     PlatformIntrinsic,
54 }
55
56 #[allow(non_camel_case_types)]
57 #[derive(Copy, Clone, PartialEq, Debug)]
58 pub enum Architecture {
59     X86,
60     X86_64,
61     Arm,
62     Mips,
63     Mipsel
64 }
65
66 #[derive(Copy, Clone)]
67 pub struct AbiData {
68     abi: Abi,
69
70     // Name of this ABI as we like it called.
71     name: &'static str,
72 }
73
74 #[derive(Copy, Clone)]
75 pub enum AbiArchitecture {
76     /// Not a real ABI (e.g., intrinsic)
77     RustArch,
78     /// An ABI that specifies cross-platform defaults (e.g., "C")
79     AllArch,
80     /// Multiple architectures (bitset)
81     Archs(u32)
82 }
83
84 #[allow(non_upper_case_globals)]
85 const AbiDatas: &'static [AbiData] = &[
86     // Platform-specific ABIs
87     AbiData {abi: Cdecl, name: "cdecl" },
88     AbiData {abi: Stdcall, name: "stdcall" },
89     AbiData {abi: Fastcall, name: "fastcall" },
90     AbiData {abi: Vectorcall, name: "vectorcall"},
91     AbiData {abi: Aapcs, name: "aapcs" },
92     AbiData {abi: Win64, name: "win64" },
93
94     // Cross-platform ABIs
95     //
96     // NB: Do not adjust this ordering without
97     // adjusting the indices below.
98     AbiData {abi: Rust, name: "Rust" },
99     AbiData {abi: C, name: "C" },
100     AbiData {abi: System, name: "system" },
101     AbiData {abi: RustIntrinsic, name: "rust-intrinsic" },
102     AbiData {abi: RustCall, name: "rust-call" },
103     AbiData {abi: PlatformIntrinsic, name: "platform-intrinsic" }
104 ];
105
106 /// Returns the ABI with the given name (if any).
107 pub fn lookup(name: &str) -> Option<Abi> {
108     AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
109 }
110
111 pub fn all_names() -> Vec<&'static str> {
112     AbiDatas.iter().map(|d| d.name).collect()
113 }
114
115 impl Abi {
116     #[inline]
117     pub fn index(&self) -> usize {
118         *self as usize
119     }
120
121     #[inline]
122     pub fn data(&self) -> &'static AbiData {
123         &AbiDatas[self.index()]
124     }
125
126     pub fn name(&self) -> &'static str {
127         self.data().name
128     }
129 }
130
131 impl fmt::Display for Abi {
132     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
133         write!(f, "\"{}\"", self.name())
134     }
135 }
136
137 impl fmt::Display for Os {
138     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139         match *self {
140             OsLinux => "linux".fmt(f),
141             OsWindows => "windows".fmt(f),
142             OsMacos => "macos".fmt(f),
143             OsiOS => "ios".fmt(f),
144             OsAndroid => "android".fmt(f),
145             OsFreebsd => "freebsd".fmt(f),
146             OsDragonfly => "dragonfly".fmt(f),
147             OsBitrig => "bitrig".fmt(f),
148             OsNetbsd => "netbsd".fmt(f),
149             OsOpenbsd => "openbsd".fmt(f),
150             OsNaCl => "nacl".fmt(f),
151             OsSolaris => "solaris".fmt(f),
152         }
153     }
154 }
155
156 #[allow(non_snake_case)]
157 #[test]
158 fn lookup_Rust() {
159     let abi = lookup("Rust");
160     assert!(abi.is_some() && abi.unwrap().data().name == "Rust");
161 }
162
163 #[test]
164 fn lookup_cdecl() {
165     let abi = lookup("cdecl");
166     assert!(abi.is_some() && abi.unwrap().data().name == "cdecl");
167 }
168
169 #[test]
170 fn lookup_baz() {
171     let abi = lookup("baz");
172     assert!(abi.is_none());
173 }
174
175 #[test]
176 fn indices_are_correct() {
177     for (i, abi_data) in AbiDatas.iter().enumerate() {
178         assert_eq!(i, abi_data.abi.index());
179     }
180 }