]> git.lizzy.rs Git - rust.git/blob - src/librustc_back/lib.rs
Rollup merge of #45117 - johnthagen:fix-str-raise, r=alexcrichton
[rust.git] / src / librustc_back / lib.rs
1 // Copyright 2012-2013 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 //! Some stuff used by rustc that doesn't have many dependencies
12 //!
13 //! Originally extracted from rustc::back, which was nominally the
14 //! compiler 'backend', though LLVM is rustc's backend, so rustc_back
15 //! is really just odds-and-ends relating to code gen and linking.
16 //! This crate mostly exists to make rustc smaller, so we might put
17 //! more 'stuff' here in the future.  It does not have a dependency on
18 //! rustc_llvm.
19 //!
20 //! FIXME: Split this into two crates: one that has deps on syntax, and
21 //! one that doesn't; the one that doesn't might get decent parallel
22 //! build speedups.
23
24 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
25       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
26       html_root_url = "https://doc.rust-lang.org/nightly/")]
27 #![deny(warnings)]
28
29 #![feature(box_syntax)]
30 #![feature(const_fn)]
31 #![feature(libc)]
32 #![feature(rand)]
33 #![cfg_attr(test, feature(rand))]
34
35 extern crate syntax;
36 extern crate libc;
37 extern crate serialize;
38 #[macro_use] extern crate log;
39
40 extern crate serialize as rustc_serialize; // used by deriving
41
42 pub mod tempdir;
43 pub mod target;
44 pub mod slice;
45 pub mod dynamic_lib;
46
47 use std::str::FromStr;
48
49 use serialize::json::{Json, ToJson};
50
51 macro_rules! linker_flavor {
52     ($(($variant:ident, $string:expr),)+) => {
53         #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash,
54                  RustcEncodable, RustcDecodable)]
55         pub enum LinkerFlavor {
56             $($variant,)+
57         }
58
59         impl LinkerFlavor {
60             pub const fn one_of() -> &'static str {
61                 concat!("one of: ", $($string, " ",)+)
62             }
63
64             pub fn from_str(s: &str) -> Option<Self> {
65                 Some(match s {
66                     $($string => LinkerFlavor::$variant,)+
67                     _ => return None,
68                 })
69             }
70
71             pub fn desc(&self) -> &str {
72                 match *self {
73                     $(LinkerFlavor::$variant => $string,)+
74                 }
75             }
76         }
77
78         impl ToJson for LinkerFlavor {
79             fn to_json(&self) -> Json {
80                 self.desc().to_json()
81             }
82         }
83     }
84 }
85
86 linker_flavor! {
87     (Em, "em"),
88     (Gcc, "gcc"),
89     (Ld, "ld"),
90     (Msvc, "msvc"),
91 }
92
93 #[derive(Clone, Copy, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
94 pub enum PanicStrategy {
95     Unwind,
96     Abort,
97 }
98
99 impl PanicStrategy {
100     pub fn desc(&self) -> &str {
101         match *self {
102             PanicStrategy::Unwind => "unwind",
103             PanicStrategy::Abort => "abort",
104         }
105     }
106 }
107
108 impl ToJson for PanicStrategy {
109     fn to_json(&self) -> Json {
110         match *self {
111             PanicStrategy::Abort => "abort".to_json(),
112             PanicStrategy::Unwind => "unwind".to_json(),
113         }
114     }
115 }
116
117 #[derive(Clone, Copy, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
118 pub enum RelroLevel {
119     Full,
120     Partial,
121     Off,
122 }
123
124 impl RelroLevel {
125     pub fn desc(&self) -> &str {
126         match *self {
127             RelroLevel::Full => "full",
128             RelroLevel::Partial => "partial",
129             RelroLevel::Off => "off",
130         }
131     }
132 }
133
134 impl FromStr for RelroLevel {
135     type Err = ();
136
137     fn from_str(s: &str) -> Result<RelroLevel, ()> {
138         match s {
139             "full" => Ok(RelroLevel::Full),
140             "partial" => Ok(RelroLevel::Partial),
141             "off" => Ok(RelroLevel::Off),
142             _ => Err(()),
143         }
144     }
145 }
146
147 impl ToJson for RelroLevel {
148     fn to_json(&self) -> Json {
149         match *self {
150             RelroLevel::Full => "full".to_json(),
151             RelroLevel::Partial => "partial".to_json(),
152             RelroLevel::Off => "off".to_json(),
153         }
154     }
155 }