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