      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198

    // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
    // file at the top-level directory of this distribution and at
    // http://rust-lang.org/COPYRIGHT.
    //
    // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. This file may not be copied, modified, or distributed
    // except according to those terms.

    //! A support library for macro authors when defining new macros.
    //!
    //! This library, provided by the standard distribution, provides the types
    //! consumed in the interfaces of procedurally defined macro definitions.
    //! Currently the primary use of this crate is to provide the ability to define
    //! new custom derive modes through `#[proc_macro_derive]`.
    //!
    //! Note that this crate is intentionally very bare-bones currently. The main
    //! type, `TokenStream`, only supports `fmt::Display` and `FromStr`
    //! implementations, indicating that it can only go to and come from a string.
    //! This functionality is intended to be expanded over time as more surface
    //! area for macro authors is stabilized.
    //!
    //! See [the book](../book/procedural-macros.html) for more.

    #![crate_name = "proc_macro"]
    #![stable(feature = "proc_macro_lib", since = "1.15.0")]
    #![crate_type = "rlib"]
    #![crate_type = "dylib"]
    #![deny(warnings)]
    #![deny(missing_docs)]
    #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
           html_favicon_url = "../../rust-logo-32x32-blk.png",
           html_root_url = "https://doc.rust-lang.org/nightly/",
           html_playground_url = "https://play.rust-lang.org/",
           issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
           test(no_crate_inject, attr(deny(warnings))),
           test(attr(allow(dead_code, deprecated, unused_variables, unused_mut))))]

    #![feature(rustc_private)]
    #![feature(staged_api)]
    #![feature(lang_items)]

    extern crate syntax;

    use std::fmt;
    use std::str::FromStr;

    use syntax::errors::DiagnosticBuilder;
    use syntax::parse;
    use syntax::tokenstream::TokenStream as TokenStream_;

    /// The main type provided by this crate, representing an abstract stream of
    /// tokens.
    ///
    /// This is both the input and output of `#[proc_macro_derive]` definitions.
    /// Currently it's required to be a list of valid Rust items, but this
    /// restriction may be lifted in the future.
    ///
    /// The API of this type is intentionally bare-bones, but it'll be expanded over
    /// time!
    #[stable(feature = "proc_macro_lib", since = "1.15.0")]
    pub struct TokenStream {
        inner: TokenStream_,
    }

    /// Error returned from `TokenStream::from_str`.
    #[derive(Debug)]
    #[stable(feature = "proc_macro_lib", since = "1.15.0")]
    pub struct LexError {
        _inner: (),
    }

    /// Permanently unstable internal implementation details of this crate. This
    /// should not be used.
    ///
    /// These methods are used by the rest of the compiler to generate instances of
    /// `TokenStream` to hand to macro definitions, as well as consume the output.
    ///
    /// Note that this module is also intentionally separate from the rest of the
    /// crate. This allows the `#[unstable]` directive below to naturally apply to
    /// all of the contents.
    #[unstable(feature = "proc_macro_internals", issue = "27812")]
    #[doc(hidden)]
    pub mod __internal {
        use std::cell::Cell;
        use std::rc::Rc;

        use syntax::ast;
        use syntax::ptr::P;
        use syntax::parse::{self, token, ParseSess};
        use syntax::tokenstream::{TokenTree, TokenStream as TokenStream_};

        use super::{TokenStream, LexError};

        pub fn new_token_stream(item: P<ast::Item>) -> TokenStream {
            TokenStream {
                inner: TokenTree::Token(item.span, token::Interpolated(Rc::new(token::NtItem(item))))
                    .into()
            }
        }

        pub fn token_stream_wrap(inner: TokenStream_) -> TokenStream {
            TokenStream {
                inner: inner
            }
        }

        pub fn token_stream_parse_items(stream: TokenStream) -> Result<Vec<P<ast::Item>>, LexError> {
            with_parse_sess(move |sess| {
                let mut parser = parse::stream_to_parser(sess, stream.inner);
                let mut items = Vec::new();

                while let Some(item) = try!(parser.parse_item().map_err(super::parse_to_lex_err)) {
                    items.push(item)
                }

                Ok(items)
            })
        }

        pub fn token_stream_inner(stream: TokenStream) -> TokenStream_ {
            stream.inner
        }

        pub trait Registry {
            fn register_custom_derive(&mut self,
                                      trait_name: &str,
                                      expand: fn(TokenStream) -> TokenStream,
                                      attributes: &[&'static str]);

            fn register_attr_proc_macro(&mut self,
                                        name: &str,
                                        expand: fn(TokenStream, TokenStream) -> TokenStream);

            fn register_bang_proc_macro(&mut self,
                                        name: &str,
                                        expand: fn(TokenStream) -> TokenStream);
        }

        // Emulate scoped_thread_local!() here essentially
        thread_local! {
            static CURRENT_SESS: Cell<*const ParseSess> = Cell::new(0 as *const _);
        }

        pub fn set_parse_sess<F, R>(sess: &ParseSess, f: F) -> R
            where F: FnOnce() -> R
        {
            struct Reset { prev: *const ParseSess }

            impl Drop for Reset {
                fn drop(&mut self) {
                    CURRENT_SESS.with(|p| p.set(self.prev));
                }
            }

            CURRENT_SESS.with(|p| {
                let _reset = Reset { prev: p.get() };
                p.set(sess);
                f()
            })
        }

        pub fn with_parse_sess<F, R>(f: F) -> R
            where F: FnOnce(&ParseSess) -> R
        {
            let p = CURRENT_SESS.with(|p| p.get());
            assert!(!p.is_null(), "proc_macro::__internal::with_parse_sess() called \
                                   before set_parse_sess()!");
            f(unsafe { &*p })
        }
    }

    fn parse_to_lex_err(mut err: DiagnosticBuilder) -> LexError {
        err.cancel();
        LexError { _inner: () }
    }

    #[stable(feature = "proc_macro_lib", since = "1.15.0")]
    impl FromStr for TokenStream {
        type Err = LexError;

        fn from_str(src: &str) -> Result<TokenStream, LexError> {
            __internal::with_parse_sess(|sess| {
                let src = src.to_string();
                let name = "<proc-macro source code>".to_string();
                let stream = parse::parse_stream_from_source_str(name, src, sess);
                Ok(__internal::token_stream_wrap(stream))
            })
        }
    }

    #[stable(feature = "proc_macro_lib", since = "1.15.0")]
    impl fmt::Display for TokenStream {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            self.inner.fmt(f)
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)