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