]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/abi.rs
Auto merge of #43648 - RalfJung:jemalloc-debug, r=alexcrichton
[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 use std::fmt;
12
13 #[derive(PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Clone, Copy, Debug)]
14 pub enum Abi {
15     // NB: This ordering MUST match the AbiDatas array below.
16     // (This is ensured by the test indices_are_correct().)
17
18     // Single platform ABIs
19     Cdecl,
20     Stdcall,
21     Fastcall,
22     Vectorcall,
23     Thiscall,
24     Aapcs,
25     Win64,
26     SysV64,
27     PtxKernel,
28     Msp430Interrupt,
29     X86Interrupt,
30
31     // Multiplatform / generic ABIs
32     Rust,
33     C,
34     System,
35     RustIntrinsic,
36     RustCall,
37     PlatformIntrinsic,
38     Unadjusted
39 }
40
41 #[derive(Copy, Clone)]
42 pub struct AbiData {
43     abi: Abi,
44
45     /// Name of this ABI as we like it called.
46     name: &'static str,
47
48     /// A generic ABI is supported on all platforms.
49     generic: bool,
50 }
51
52 #[allow(non_upper_case_globals)]
53 const AbiDatas: &'static [AbiData] = &[
54     // Platform-specific ABIs
55     AbiData {abi: Abi::Cdecl, name: "cdecl", generic: false },
56     AbiData {abi: Abi::Stdcall, name: "stdcall", generic: false },
57     AbiData {abi: Abi::Fastcall, name: "fastcall", generic: false },
58     AbiData {abi: Abi::Vectorcall, name: "vectorcall", generic: false},
59     AbiData {abi: Abi::Thiscall, name: "thiscall", generic: false},
60     AbiData {abi: Abi::Aapcs, name: "aapcs", generic: false },
61     AbiData {abi: Abi::Win64, name: "win64", generic: false },
62     AbiData {abi: Abi::SysV64, name: "sysv64", generic: false },
63     AbiData {abi: Abi::PtxKernel, name: "ptx-kernel", generic: false },
64     AbiData {abi: Abi::Msp430Interrupt, name: "msp430-interrupt", generic: false },
65     AbiData {abi: Abi::X86Interrupt, name: "x86-interrupt", generic: false },
66
67     // Cross-platform ABIs
68     AbiData {abi: Abi::Rust, name: "Rust", generic: true },
69     AbiData {abi: Abi::C, name: "C", generic: true },
70     AbiData {abi: Abi::System, name: "system", generic: true },
71     AbiData {abi: Abi::RustIntrinsic, name: "rust-intrinsic", generic: true },
72     AbiData {abi: Abi::RustCall, name: "rust-call", generic: true },
73     AbiData {abi: Abi::PlatformIntrinsic, name: "platform-intrinsic", generic: true },
74     AbiData {abi: Abi::Unadjusted, name: "unadjusted", generic: true },
75 ];
76
77 /// Returns the ABI with the given name (if any).
78 pub fn lookup(name: &str) -> Option<Abi> {
79     AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
80 }
81
82 pub fn all_names() -> Vec<&'static str> {
83     AbiDatas.iter().map(|d| d.name).collect()
84 }
85
86 impl Abi {
87     #[inline]
88     pub fn index(&self) -> usize {
89         *self as usize
90     }
91
92     #[inline]
93     pub fn data(&self) -> &'static AbiData {
94         &AbiDatas[self.index()]
95     }
96
97     pub fn name(&self) -> &'static str {
98         self.data().name
99     }
100
101     pub fn generic(&self) -> bool {
102         self.data().generic
103     }
104 }
105
106 impl fmt::Display for Abi {
107     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108         write!(f, "\"{}\"", self.name())
109     }
110 }
111
112 #[allow(non_snake_case)]
113 #[test]
114 fn lookup_Rust() {
115     let abi = lookup("Rust");
116     assert!(abi.is_some() && abi.unwrap().data().name == "Rust");
117 }
118
119 #[test]
120 fn lookup_cdecl() {
121     let abi = lookup("cdecl");
122     assert!(abi.is_some() && abi.unwrap().data().name == "cdecl");
123 }
124
125 #[test]
126 fn lookup_baz() {
127     let abi = lookup("baz");
128     assert!(abi.is_none());
129 }
130
131 #[test]
132 fn indices_are_correct() {
133     for (i, abi_data) in AbiDatas.iter().enumerate() {
134         assert_eq!(i, abi_data.abi.index());
135     }
136 }