1 // Copyright 2012-2014 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.
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.
13 pub use self::Architecture::*;
14 pub use self::AbiArchitecture::*;
18 #[deriving(Copy, PartialEq)]
29 #[deriving(PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Clone, Copy)]
31 // NB: This ordering MUST match the AbiDatas array below.
32 // (This is ensured by the test indices_are_correct().)
34 // Single platform ABIs come first (`for_arch()` relies on this)
41 // Multiplatform ABIs second
49 #[allow(non_camel_case_types)]
50 #[deriving(Copy, PartialEq)]
51 pub enum Architecture {
63 // Name of this ABI as we like it called.
68 pub enum AbiArchitecture {
69 /// Not a real ABI (e.g., intrinsic)
71 /// An ABI that specifies cross-platform defaults (e.g., "C")
73 /// Multiple architectures (bitset)
77 #[allow(non_upper_case_globals)]
78 static AbiDatas: &'static [AbiData] = &[
79 // Platform-specific ABIs
80 AbiData {abi: Cdecl, name: "cdecl" },
81 AbiData {abi: Stdcall, name: "stdcall" },
82 AbiData {abi: Fastcall, name:"fastcall" },
83 AbiData {abi: Aapcs, name: "aapcs" },
84 AbiData {abi: Win64, name: "win64" },
86 // Cross-platform ABIs
88 // NB: Do not adjust this ordering without
89 // adjusting the indices below.
90 AbiData {abi: Rust, name: "Rust" },
91 AbiData {abi: C, name: "C" },
92 AbiData {abi: System, name: "system" },
93 AbiData {abi: RustIntrinsic, name: "rust-intrinsic" },
94 AbiData {abi: RustCall, name: "rust-call" },
97 /// Returns the ABI with the given name (if any).
98 pub fn lookup(name: &str) -> Option<Abi> {
99 AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
102 pub fn all_names() -> Vec<&'static str> {
103 AbiDatas.iter().map(|d| d.name).collect()
108 pub fn index(&self) -> uint {
113 pub fn data(&self) -> &'static AbiData {
114 &AbiDatas[self.index()]
117 pub fn name(&self) -> &'static str {
122 impl fmt::Show for Abi {
123 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
124 write!(f, "\"{}\"", self.name())
128 impl fmt::Show for Os {
129 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131 OsLinux => "linux".fmt(f),
132 OsWindows => "windows".fmt(f),
133 OsMacos => "macos".fmt(f),
134 OsiOS => "ios".fmt(f),
135 OsAndroid => "android".fmt(f),
136 OsFreebsd => "freebsd".fmt(f),
137 OsDragonfly => "dragonfly".fmt(f)
142 #[allow(non_snake_case)]
145 let abi = lookup("Rust");
146 assert!(abi.is_some() && abi.unwrap().data().name == "Rust");
151 let abi = lookup("cdecl");
152 assert!(abi.is_some() && abi.unwrap().data().name == "cdecl");
157 let abi = lookup("baz");
158 assert!(abi.is_none());
162 fn indices_are_correct() {
163 for (i, abi_data) in AbiDatas.iter().enumerate() {
164 assert_eq!(i, abi_data.abi.index());