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