Skip to content

Commit d43a948

Browse files
committed
Document rustc::plugin
1 parent 7c674ac commit d43a948

File tree

5 files changed

+100
-12
lines changed

5 files changed

+100
-12
lines changed

src/librustc/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,7 @@ pub mod metadata;
114114

115115
pub mod driver;
116116

117-
pub mod plugin {
118-
pub use self::registry::Registry;
119-
120-
pub mod registry;
121-
pub mod load;
122-
pub mod build;
123-
}
117+
pub mod plugin;
124118

125119
pub mod util {
126120
pub mod common;

src/librustc/plugin/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Used by `rustc` when compiling a plugin crate.
12+
1113
use syntax::ast;
1214
use syntax::attr;
1315
use syntax::codemap::Span;
@@ -36,7 +38,6 @@ impl Visitor<()> for RegistrarFinder {
3638
}
3739

3840
/// Find the function marked with `#[plugin_registrar]`, if any.
39-
/// Used while compiling a crate which defines a registrar.
4041
pub fn find_plugin_registrar(diagnostic: &diagnostic::SpanHandler,
4142
krate: &ast::Crate) -> Option<ast::NodeId> {
4243
let mut finder = RegistrarFinder { registrars: Vec::new() };

src/librustc/plugin/load.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use plugin::registry::PluginRegistrarFun;
11+
//! Used by `rustc` when loading a plugin.
12+
1213
use driver::session::Session;
1314
use metadata::creader::PluginMetadataReader;
15+
use plugin::registry::Registry;
1416

1517
use std::mem;
1618
use std::os;
@@ -22,14 +24,25 @@ use syntax::visit::Visitor;
2224
use syntax::ext::expand::ExportedMacros;
2325
use syntax::attr::AttrMetaMethods;
2426

27+
/// Plugin-related crate metadata.
2528
pub struct PluginMetadata {
29+
/// Source code of macros exported by the crate.
2630
pub macros: Vec<String>,
31+
/// Path to the shared library file.
2732
pub lib: Option<Path>,
33+
/// Symbol name of the plugin registrar function.
2834
pub registrar_symbol: Option<String>,
2935
}
3036

37+
/// Pointer to a registrar function.
38+
pub type PluginRegistrarFun =
39+
fn(&mut Registry);
40+
41+
/// Information about loaded plugins.
3142
pub struct Plugins {
43+
/// Source code of exported macros.
3244
pub macros: Vec<ExportedMacros>,
45+
/// Registrars, as function pointers.
3346
pub registrars: Vec<PluginRegistrarFun>,
3447
}
3548

@@ -52,6 +65,7 @@ impl<'a> PluginLoader<'a> {
5265
}
5366
}
5467

68+
/// Read plugin metadata and dynamically load registrar functions.
5569
pub fn load_plugins(sess: &Session, krate: &ast::Crate) -> Plugins {
5670
let mut loader = PluginLoader::new(sess);
5771
visit::walk_crate(&mut loader, krate, ());

src/librustc/plugin/mod.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
/*!
12+
* Infrastructure for compiler plugins.
13+
*
14+
* Plugins are Rust libraries which extend the behavior of `rustc`
15+
* in various ways.
16+
*
17+
* Plugin authors will use the `Registry` type re-exported by
18+
* this module, along with its methods. The rest of the module
19+
* is for use by `rustc` itself.
20+
*
21+
* To define a plugin, build a dylib crate with a
22+
* `#[plugin_registrar]` function:
23+
*
24+
* ```rust,ignore
25+
* #![crate_id = "myplugin"]
26+
* #![crate_type = "dylib"]
27+
* #![feature(plugin_registrar)]
28+
*
29+
* extern crate rustc;
30+
*
31+
* use rustc::plugin::Registry;
32+
*
33+
* #[plugin_registrar]
34+
* pub fn plugin_registrar(reg: &mut Registry) {
35+
* reg.register_macro("mymacro", expand_mymacro);
36+
* }
37+
*
38+
* fn expand_mymacro(...) { // details elided
39+
* ```
40+
*
41+
* WARNING: We currently don't check that the registrar function
42+
* has the appropriate type!
43+
*
44+
* To use a plugin while compiling another crate:
45+
*
46+
* ```rust
47+
* #![feature(phase)]
48+
*
49+
* #[phase(plugin)]
50+
* extern crate myplugin;
51+
* ```
52+
*
53+
* If you also need the plugin crate available at runtime, use
54+
* `phase(plugin, link)`.
55+
*
56+
* See `src/test/auxiliary/macro_crate_test.rs` and `src/libfourcc`
57+
* for examples of syntax extension plugins.
58+
*/
59+
60+
pub use self::registry::Registry;
61+
62+
pub mod registry;
63+
pub mod load;
64+
pub mod build;

src/librustc/plugin/registry.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,23 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Used by plugin crates to tell `rustc` about the plugins they provide.
12+
1113
use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
1214
use syntax::ext::base::{IdentTT, ItemDecorator, ItemModifier, BasicMacroExpander};
1315
use syntax::ext::base::{MacroExpanderFn};
1416
use syntax::codemap::Span;
1517
use syntax::parse::token;
1618
use syntax::ast;
1719

20+
/// Structure used to register plugins.
21+
///
22+
/// A plugin registrar function takes an `&mut Registry` and should call
23+
/// methods to register its plugins.
24+
///
25+
/// This struct has public fields and other methods for use by `rustc`
26+
/// itself. They are not documented here, and plugin authors should
27+
/// not use them.
1828
pub struct Registry {
1929
#[doc(hidden)]
2030
pub krate_span: Span,
@@ -23,9 +33,6 @@ pub struct Registry {
2333
pub syntax_exts: Vec<NamedSyntaxExtension>,
2434
}
2535

26-
pub type PluginRegistrarFun =
27-
fn(&mut Registry);
28-
2936
impl Registry {
3037
#[doc(hidden)]
3138
pub fn new(krate: &ast::Crate) -> Registry {
@@ -35,6 +42,9 @@ impl Registry {
3542
}
3643
}
3744

45+
/// Register a syntax extension of any kind.
46+
///
47+
/// This is the most general hook into `libsyntax`'s expansion behavior.
3848
pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
3949
self.syntax_exts.push((name, match extension {
4050
NormalTT(ext, _) => NormalTT(ext, Some(self.krate_span)),
@@ -44,6 +54,11 @@ impl Registry {
4454
}));
4555
}
4656

57+
/// Register a macro of the usual kind.
58+
///
59+
/// This is a convenience wrapper for `register_syntax_extension`.
60+
/// It builds for you a `NormalTT` with a `BasicMacroExpander`,
61+
/// and also takes care of interning the macro's name.
4762
pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
4863
self.register_syntax_extension(
4964
token::intern(name),

0 commit comments

Comments
 (0)