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