]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
Auto merge of #50409 - KiChjang:issue-50343, r=nikomatsakis
[rust.git] / src / libsyntax / 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 //! The Rust parser and macro expander.
12 //!
13 //! # Note
14 //!
15 //! This API is completely unstable and subject to change.
16
17 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
18        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
19        html_root_url = "https://doc.rust-lang.org/nightly/",
20        test(attr(deny(warnings))))]
21
22 #![feature(unicode_internals)]
23 #![feature(rustc_diagnostic_macros)]
24 #![feature(slice_sort_by_cached_key)]
25 #![feature(non_exhaustive)]
26 #![feature(const_atomic_usize_new)]
27 #![feature(rustc_attrs)]
28 #![feature(str_escape)]
29
30 #![recursion_limit="256"]
31
32 #[macro_use] extern crate bitflags;
33 extern crate core;
34 extern crate serialize;
35 #[macro_use] extern crate log;
36 pub extern crate rustc_errors as errors;
37 extern crate syntax_pos;
38 extern crate rustc_data_structures;
39 extern crate rustc_target;
40 #[macro_use] extern crate scoped_tls;
41
42 extern crate serialize as rustc_serialize; // used by deriving
43
44 use rustc_data_structures::sync::Lock;
45
46 // A variant of 'try!' that panics on an Err. This is used as a crutch on the
47 // way towards a non-panic!-prone parser. It should be used for fatal parsing
48 // errors; eventually we plan to convert all code using panictry to just use
49 // normal try.
50 // Exported for syntax_ext, not meant for general use.
51 #[macro_export]
52 macro_rules! panictry {
53     ($e:expr) => ({
54         use std::result::Result::{Ok, Err};
55         use errors::FatalError;
56         match $e {
57             Ok(e) => e,
58             Err(mut e) => {
59                 e.emit();
60                 FatalError.raise()
61             }
62         }
63     })
64 }
65
66 #[macro_export]
67 macro_rules! unwrap_or {
68     ($opt:expr, $default:expr) => {
69         match $opt {
70             Some(x) => x,
71             None => $default,
72         }
73     }
74 }
75
76 struct Globals {
77     used_attrs: Lock<Vec<u64>>,
78     known_attrs: Lock<Vec<u64>>,
79     syntax_pos_globals: syntax_pos::Globals,
80 }
81
82 impl Globals {
83     fn new() -> Globals {
84         Globals {
85             used_attrs: Lock::new(Vec::new()),
86             known_attrs: Lock::new(Vec::new()),
87             syntax_pos_globals: syntax_pos::Globals::new(),
88         }
89     }
90 }
91
92 pub fn with_globals<F, R>(f: F) -> R
93     where F: FnOnce() -> R
94 {
95     let globals = Globals::new();
96     GLOBALS.set(&globals, || {
97         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
98     })
99 }
100
101 scoped_thread_local!(static GLOBALS: Globals);
102
103 #[macro_use]
104 pub mod diagnostics {
105     #[macro_use]
106     pub mod macros;
107     pub mod plugin;
108     pub mod metadata;
109 }
110
111 // NB: This module needs to be declared first so diagnostics are
112 // registered before they are used.
113 pub mod diagnostic_list;
114
115 pub mod util {
116     pub mod lev_distance;
117     pub mod node_count;
118     pub mod parser;
119     #[cfg(test)]
120     pub mod parser_testing;
121     pub mod small_vector;
122     pub mod move_map;
123
124     mod thin_vec;
125     pub use self::thin_vec::ThinVec;
126
127     mod rc_slice;
128     pub use self::rc_slice::RcSlice;
129 }
130
131 pub mod json;
132
133 pub mod syntax {
134     pub use ext;
135     pub use parse;
136     pub use ast;
137 }
138
139 pub mod ast;
140 pub mod attr;
141 pub mod codemap;
142 #[macro_use]
143 pub mod config;
144 pub mod entry;
145 pub mod edition;
146 pub mod feature_gate;
147 pub mod fold;
148 pub mod parse;
149 pub mod ptr;
150 pub mod show_span;
151 pub mod std_inject;
152 pub mod str;
153 pub use syntax_pos::symbol;
154 pub mod test;
155 pub mod tokenstream;
156 pub mod visit;
157
158 pub mod print {
159     pub mod pp;
160     pub mod pprust;
161 }
162
163 pub mod ext {
164     pub use syntax_pos::hygiene;
165     pub mod base;
166     pub mod build;
167     pub mod derive;
168     pub mod expand;
169     pub mod placeholders;
170     pub mod quote;
171     pub mod source_util;
172
173     pub mod tt {
174         pub mod transcribe;
175         pub mod macro_parser;
176         pub mod macro_rules;
177         pub mod quoted;
178     }
179 }
180
181 #[cfg(test)]
182 mod test_snippet;
183
184 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }