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