]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
Rollup merge of #63055 - Mark-Simulacrum:save-analysis-clean-2, r=Xanewok
[rust.git] / src / libsyntax / lib.rs
1 //! The Rust parser and macro expander.
2 //!
3 //! # Note
4 //!
5 //! This API is completely unstable and subject to change.
6
7 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
8        test(attr(deny(warnings))))]
9
10 #![feature(bind_by_move_pattern_guards)]
11 #![feature(box_syntax)]
12 #![feature(const_fn)]
13 #![feature(const_transmute)]
14 #![feature(crate_visibility_modifier)]
15 #![feature(label_break_value)]
16 #![feature(mem_take)]
17 #![feature(nll)]
18 #![feature(proc_macro_diagnostic)]
19 #![feature(proc_macro_internals)]
20 #![feature(proc_macro_span)]
21 #![feature(rustc_diagnostic_macros)]
22 #![feature(try_trait)]
23 #![feature(unicode_internals)]
24
25 #![recursion_limit="256"]
26
27 extern crate proc_macro;
28
29 pub use errors;
30 use rustc_data_structures::sync::Lock;
31 use rustc_data_structures::bit_set::GrowableBitSet;
32 pub use rustc_data_structures::thin_vec::ThinVec;
33 use ast::AttrId;
34 use syntax_pos::edition::Edition;
35
36 const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments");
37
38 // A variant of 'try!' that panics on an Err. This is used as a crutch on the
39 // way towards a non-panic!-prone parser. It should be used for fatal parsing
40 // errors; eventually we plan to convert all code using panictry to just use
41 // normal try.
42 #[macro_export]
43 macro_rules! panictry {
44     ($e:expr) => ({
45         use std::result::Result::{Ok, Err};
46         use errors::FatalError;
47         match $e {
48             Ok(e) => e,
49             Err(mut e) => {
50                 e.emit();
51                 FatalError.raise()
52             }
53         }
54     })
55 }
56
57 // A variant of 'panictry!' that works on a Vec<Diagnostic> instead of a single DiagnosticBuilder.
58 macro_rules! panictry_buffer {
59     ($handler:expr, $e:expr) => ({
60         use std::result::Result::{Ok, Err};
61         use errors::{FatalError, DiagnosticBuilder};
62         match $e {
63             Ok(e) => e,
64             Err(errs) => {
65                 for e in errs {
66                     DiagnosticBuilder::new_diagnostic($handler, e).emit();
67                 }
68                 FatalError.raise()
69             }
70         }
71     })
72 }
73
74 #[macro_export]
75 macro_rules! unwrap_or {
76     ($opt:expr, $default:expr) => {
77         match $opt {
78             Some(x) => x,
79             None => $default,
80         }
81     }
82 }
83
84 pub struct Globals {
85     used_attrs: Lock<GrowableBitSet<AttrId>>,
86     known_attrs: Lock<GrowableBitSet<AttrId>>,
87     syntax_pos_globals: syntax_pos::Globals,
88 }
89
90 impl Globals {
91     fn new(edition: Edition) -> Globals {
92         Globals {
93             // We have no idea how many attributes their will be, so just
94             // initiate the vectors with 0 bits. We'll grow them as necessary.
95             used_attrs: Lock::new(GrowableBitSet::new_empty()),
96             known_attrs: Lock::new(GrowableBitSet::new_empty()),
97             syntax_pos_globals: syntax_pos::Globals::new(edition),
98         }
99     }
100 }
101
102 pub fn with_globals<F, R>(edition: Edition, f: F) -> R
103     where F: FnOnce() -> R
104 {
105     let globals = Globals::new(edition);
106     GLOBALS.set(&globals, || {
107         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
108     })
109 }
110
111 pub fn with_default_globals<F, R>(f: F) -> R
112     where F: FnOnce() -> R
113 {
114     with_globals(edition::DEFAULT_EDITION, f)
115 }
116
117 scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals);
118
119 #[macro_use]
120 pub mod diagnostics {
121     #[macro_use]
122     pub mod macros;
123     pub mod plugin;
124     pub mod metadata;
125 }
126
127 // N.B., this module needs to be declared first so diagnostics are
128 // registered before they are used.
129 pub mod error_codes;
130
131 pub mod util {
132     pub mod lev_distance;
133     pub mod node_count;
134     pub mod parser;
135     #[cfg(test)]
136     pub mod parser_testing;
137     pub mod map_in_place;
138 }
139
140 pub mod json;
141
142 pub mod ast;
143 pub mod attr;
144 pub mod source_map;
145 #[macro_use]
146 pub mod config;
147 pub mod entry;
148 pub mod feature_gate;
149 pub mod mut_visit;
150 pub mod parse;
151 pub mod ptr;
152 pub mod show_span;
153 pub use syntax_pos::edition;
154 pub use syntax_pos::symbol;
155 pub mod tokenstream;
156 pub mod visit;
157
158 pub mod print {
159     pub mod pp;
160     pub mod pprust;
161     mod helpers;
162 }
163
164 pub mod ext {
165     mod placeholders;
166     mod proc_macro_server;
167
168     pub use syntax_pos::hygiene;
169     pub mod allocator;
170     pub mod base;
171     pub mod build;
172     pub mod expand;
173     pub mod proc_macro;
174
175     pub mod tt {
176         pub mod transcribe;
177         pub mod macro_check;
178         pub mod macro_parser;
179         pub mod macro_rules;
180         pub mod quoted;
181     }
182 }
183
184 pub mod early_buffered_lints;
185
186 #[cfg(test)]
187 mod test_snippet;
188
189 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }