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