rust/src/libsyntax/abi.rs

179 lines
4.3 KiB
Rust
Raw Normal View History

2015-01-29 01:19:28 -06:00
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::fmt;
2015-03-30 08:38:59 -05:00
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[allow(non_camel_case_types)]
pub enum Os {
Windows,
Macos,
Linux,
Android,
Freebsd,
iOS,
Dragonfly,
Bitrig,
Netbsd,
Openbsd,
NaCl,
Solaris,
}
2015-01-28 07:34:18 -06:00
#[derive(PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Clone, Copy, Debug)]
pub enum Abi {
// NB: This ordering MUST match the AbiDatas array below.
// (This is ensured by the test indices_are_correct().)
// Single platform ABIs come first (`for_arch()` relies on this)
Cdecl,
Stdcall,
Fastcall,
Vectorcall,
Aapcs,
2013-11-16 17:25:56 -06:00
Win64,
SysV64,
// Multiplatform ABIs second
Rust,
C,
System,
RustIntrinsic,
RustCall,
PlatformIntrinsic,
}
#[allow(non_camel_case_types)]
2015-03-30 08:38:59 -05:00
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Architecture {
X86,
X86_64,
Arm,
2014-06-17 02:16:03 -05:00
Mips,
Mipsel
}
2015-03-30 08:38:59 -05:00
#[derive(Copy, Clone)]
pub struct AbiData {
abi: Abi,
// Name of this ABI as we like it called.
name: &'static str,
}
2015-03-30 08:38:59 -05:00
#[derive(Copy, Clone)]
pub enum AbiArchitecture {
2014-06-09 15:12:30 -05:00
/// Not a real ABI (e.g., intrinsic)
Rust,
2014-06-09 15:12:30 -05:00
/// An ABI that specifies cross-platform defaults (e.g., "C")
All,
2014-06-09 15:12:30 -05:00
/// Multiple architectures (bitset)
Archs(u32)
}
2014-10-27 17:37:07 -05:00
#[allow(non_upper_case_globals)]
const AbiDatas: &'static [AbiData] = &[
// Platform-specific ABIs
AbiData {abi: Abi::Cdecl, name: "cdecl" },
AbiData {abi: Abi::Stdcall, name: "stdcall" },
AbiData {abi: Abi::Fastcall, name: "fastcall" },
AbiData {abi: Abi::Vectorcall, name: "vectorcall"},
AbiData {abi: Abi::Aapcs, name: "aapcs" },
AbiData {abi: Abi::Win64, name: "win64" },
AbiData {abi: Abi::SysV64, name: "sysv64" },
// Cross-platform ABIs
//
// NB: Do not adjust this ordering without
// adjusting the indices below.
AbiData {abi: Abi::Rust, name: "Rust" },
AbiData {abi: Abi::C, name: "C" },
AbiData {abi: Abi::System, name: "system" },
AbiData {abi: Abi::RustIntrinsic, name: "rust-intrinsic" },
AbiData {abi: Abi::RustCall, name: "rust-call" },
AbiData {abi: Abi::PlatformIntrinsic, name: "platform-intrinsic" }
];
2014-06-09 15:12:30 -05:00
/// Returns the ABI with the given name (if any).
pub fn lookup(name: &str) -> Option<Abi> {
2014-06-12 05:13:25 -05:00
AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
}
pub fn all_names() -> Vec<&'static str> {
AbiDatas.iter().map(|d| d.name).collect()
}
impl Abi {
#[inline]
2015-01-17 17:33:05 -06:00
pub fn index(&self) -> usize {
*self as usize
}
#[inline]
pub fn data(&self) -> &'static AbiData {
&AbiDatas[self.index()]
}
pub fn name(&self) -> &'static str {
self.data().name
}
}
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 17:45:07 -06:00
impl fmt::Display for Abi {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\"{}\"", self.name())
}
}
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 17:45:07 -06:00
impl fmt::Display for Os {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Os::Linux => "linux".fmt(f),
Os::Windows => "windows".fmt(f),
Os::Macos => "macos".fmt(f),
Os::iOS => "ios".fmt(f),
Os::Android => "android".fmt(f),
Os::Freebsd => "freebsd".fmt(f),
Os::Dragonfly => "dragonfly".fmt(f),
Os::Bitrig => "bitrig".fmt(f),
Os::Netbsd => "netbsd".fmt(f),
Os::Openbsd => "openbsd".fmt(f),
Os::NaCl => "nacl".fmt(f),
Os::Solaris => "solaris".fmt(f),
}
}
}
#[allow(non_snake_case)]
#[test]
fn lookup_Rust() {
let abi = lookup("Rust");
assert!(abi.is_some() && abi.unwrap().data().name == "Rust");
}
#[test]
fn lookup_cdecl() {
let abi = lookup("cdecl");
assert!(abi.is_some() && abi.unwrap().data().name == "cdecl");
}
#[test]
fn lookup_baz() {
let abi = lookup("baz");
assert!(abi.is_none());
}
#[test]
fn indices_are_correct() {
for (i, abi_data) in AbiDatas.iter().enumerate() {
assert_eq!(i, abi_data.abi.index());
}
}