]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
Rollup merge of #68438 - Aaron1011:fix/tait-non-defining, 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/", test(attr(deny(warnings))))]
8 #![feature(bool_to_option)]
9 #![feature(box_syntax)]
10 #![feature(const_fn)] // For the `transmute` in `P::new`
11 #![feature(const_transmute)]
12 #![feature(crate_visibility_modifier)]
13 #![feature(label_break_value)]
14 #![feature(nll)]
15 #![feature(try_trait)]
16 #![cfg_attr(bootstrap, feature(slice_patterns))]
17 #![feature(unicode_internals)]
18 #![recursion_limit = "256"]
19
20 use ast::AttrId;
21 use rustc_data_structures::sync::Lock;
22 use rustc_index::bit_set::GrowableBitSet;
23 use rustc_span::edition::{Edition, DEFAULT_EDITION};
24
25 #[macro_export]
26 macro_rules! unwrap_or {
27     ($opt:expr, $default:expr) => {
28         match $opt {
29             Some(x) => x,
30             None => $default,
31         }
32     };
33 }
34
35 pub struct Globals {
36     used_attrs: Lock<GrowableBitSet<AttrId>>,
37     known_attrs: Lock<GrowableBitSet<AttrId>>,
38     rustc_span_globals: rustc_span::Globals,
39 }
40
41 impl Globals {
42     fn new(edition: Edition) -> Globals {
43         Globals {
44             // We have no idea how many attributes there will be, so just
45             // initiate the vectors with 0 bits. We'll grow them as necessary.
46             used_attrs: Lock::new(GrowableBitSet::new_empty()),
47             known_attrs: Lock::new(GrowableBitSet::new_empty()),
48             rustc_span_globals: rustc_span::Globals::new(edition),
49         }
50     }
51 }
52
53 pub fn with_globals<R>(edition: Edition, f: impl FnOnce() -> R) -> R {
54     let globals = Globals::new(edition);
55     GLOBALS.set(&globals, || rustc_span::GLOBALS.set(&globals.rustc_span_globals, f))
56 }
57
58 pub fn with_default_globals<R>(f: impl FnOnce() -> R) -> R {
59     with_globals(DEFAULT_EDITION, f)
60 }
61
62 scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals);
63
64 pub mod util {
65     pub mod classify;
66     pub mod comments;
67     pub mod lev_distance;
68     pub mod literal;
69     pub mod map_in_place;
70     pub mod node_count;
71     pub mod parser;
72 }
73
74 pub mod ast;
75 pub mod attr;
76 pub mod entry;
77 pub mod expand;
78 pub mod mut_visit;
79 pub mod ptr;
80 pub use rustc_session::parse as sess;
81 pub mod token;
82 pub mod tokenstream;
83 pub mod visit;
84
85 pub mod print {
86     mod helpers;
87     pub mod pp;
88     pub mod pprust;
89 }
90
91 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
92
93 /// Requirements for a `StableHashingContext` to be used in this crate.
94 /// This is a hack to allow using the `HashStable_Generic` derive macro
95 /// instead of implementing everything in librustc.
96 pub trait HashStableContext: rustc_span::HashStableContext {
97     fn hash_attr(&mut self, _: &ast::Attribute, hasher: &mut StableHasher);
98 }
99
100 impl<AstCtx: crate::HashStableContext> HashStable<AstCtx> for ast::Attribute {
101     fn hash_stable(&self, hcx: &mut AstCtx, hasher: &mut StableHasher) {
102         hcx.hash_attr(self, hasher)
103     }
104 }