Skip to content

Redo exported macro serialization #11720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl CrateLoader for Loader {
}
}

fn get_exported_macros(&mut self, cnum: ast::CrateNum) -> ~[@ast::Item] {
fn get_exported_macros(&mut self, cnum: ast::CrateNum) -> ~[~str] {
csearch::get_exported_macros(self.env.sess.cstore, cnum)
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub fn get_macro_registrar_fn(cstore: @cstore::CStore,

pub fn get_exported_macros(cstore: @cstore::CStore,
crate_num: ast::CrateNum)
-> ~[@ast::Item] {
-> ~[~str] {
let cdata = cstore.get_crate_data(crate_num);
decoder::get_exported_macros(cdata)
}
5 changes: 2 additions & 3 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use metadata::tydecode::{parse_ty_data, parse_def_id,
use middle::ty::{ImplContainer, TraitContainer};
use middle::ty;
use middle::typeck;
use middle::astencode;
use middle::astencode::vtable_decoder_helpers;

use std::at_vec;
Expand Down Expand Up @@ -1282,12 +1281,12 @@ pub fn get_macro_registrar_fn(cdata: Cmd) -> Option<ast::DefId> {
.map(|doc| item_def_id(doc, cdata))
}

pub fn get_exported_macros(cdata: Cmd) -> ~[@ast::Item] {
pub fn get_exported_macros(cdata: Cmd) -> ~[~str] {
let macros = reader::get_doc(reader::Doc(cdata.data()),
tag_exported_macros);
let mut result = ~[];
reader::tagged_docs(macros, tag_macro_def, |macro_doc| {
result.push(astencode::decode_exported_macro(macro_doc));
result.push(macro_doc.as_str());
true
});
result
Expand Down
9 changes: 8 additions & 1 deletion src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use syntax::ast_map;
use syntax::ast_util::*;
use syntax::attr;
use syntax::attr::AttrMetaMethods;
use syntax::codemap;
use syntax::diagnostic::SpanHandler;
use syntax::parse::token::special_idents;
use syntax::ast_util;
Expand Down Expand Up @@ -71,6 +72,7 @@ pub struct EncodeParams<'a> {
cstore: @cstore::CStore,
encode_inlined_item: encode_inlined_item<'a>,
reachable: @RefCell<HashSet<ast::NodeId>>,
codemap: @codemap::CodeMap,
}

struct Stats {
Expand Down Expand Up @@ -101,6 +103,7 @@ pub struct EncodeContext<'a> {
encode_inlined_item: encode_inlined_item<'a>,
type_abbrevs: abbrev_map,
reachable: @RefCell<HashSet<ast::NodeId>>,
codemap: @codemap::CodeMap,
}

pub fn reachable(ecx: &EncodeContext, id: NodeId) -> bool {
Expand Down Expand Up @@ -1714,8 +1717,10 @@ impl<'a, 'b> Visitor<()> for MacroDefVisitor<'a, 'b> {
fn visit_item(&mut self, item: &Item, _: ()) {
match item.node {
ItemMac(..) => {
let def = self.ecx.codemap.span_to_snippet(item.span)
.expect("Unable to find source for macro");
self.ebml_w.start_tag(tag_macro_def);
astencode::encode_exported_macro(self.ebml_w, item);
self.ebml_w.wr_str(def);
self.ebml_w.end_tag();
}
_ => {}
Expand Down Expand Up @@ -1881,6 +1886,7 @@ fn encode_metadata_inner(wr: &mut MemWriter, parms: EncodeParams, crate: &Crate)
link_meta,
reachable,
non_inlineable_statics,
codemap,
..
} = parms;
let type_abbrevs = @RefCell::new(HashMap::new());
Expand All @@ -1897,6 +1903,7 @@ fn encode_metadata_inner(wr: &mut MemWriter, parms: EncodeParams, crate: &Crate)
encode_inlined_item: encode_inlined_item,
type_abbrevs: type_abbrevs,
reachable: reachable,
codemap: codemap,
};

let mut ebml_w = writer::Encoder(wr);
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2678,6 +2678,7 @@ pub fn crate_ctxt_to_encode_parms<'r>(cx: &'r CrateContext, ie: encoder::encode_
cstore: cx.sess.cstore,
encode_inlined_item: ie,
reachable: cx.reachable,
codemap: cx.sess.codemap,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustpkg/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ impl<'a> base::CrateLoader for CrateLoader<'a> {
self.loader.load_crate(crate)
}

fn get_exported_macros(&mut self, cnum: ast::CrateNum) -> ~[@ast::Item] {
fn get_exported_macros(&mut self, cnum: ast::CrateNum) -> ~[~str] {
self.loader.get_exported_macros(cnum)
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub struct MacroCrate {

pub trait CrateLoader {
fn load_crate(&mut self, crate: &ast::ViewItem) -> MacroCrate;
fn get_exported_macros(&mut self, crate_num: ast::CrateNum) -> ~[@ast::Item];
fn get_exported_macros(&mut self, crate_num: ast::CrateNum) -> ~[~str];
fn get_registrar_symbol(&mut self, crate_num: ast::CrateNum) -> Option<~str>;
}

Expand Down
20 changes: 15 additions & 5 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,20 @@ pub fn expand_view_item(vi: &ast::ViewItem,
fn load_extern_macros(crate: &ast::ViewItem, fld: &mut MacroExpander) {
let MacroCrate { lib, cnum } = fld.cx.loader.load_crate(crate);

let crate_name = match crate.node {
ast::ViewItemExternMod(ref name, _, _) => token::ident_to_str(name),
_ => unreachable!(),
};
let name = format!("<{} macros>", crate_name).to_managed();

let exported_macros = fld.cx.loader.get_exported_macros(cnum);
for &it in exported_macros.iter() {
expand_item_mac(it, fld);
for source in exported_macros.iter() {
let item = parse::parse_item_from_source_str(name,
source.to_managed(),
fld.cx.cfg(),
fld.cx.parse_sess())
.expect("expected a serialized item");
expand_item_mac(item, fld);
}

let path = match lib {
Expand Down Expand Up @@ -944,7 +955,6 @@ pub fn inject_std_macros(parse_sess: @parse::ParseSess,
let sm = match parse_item_from_source_str(@"<std-macros>",
std_macros(),
cfg.clone(),
~[],
parse_sess) {
Some(item) => item,
None => fail!("expected core macros to parse correctly")
Expand Down Expand Up @@ -1212,7 +1222,7 @@ mod test {
fail!("lolwut")
}

fn get_exported_macros(&mut self, _: ast::CrateNum) -> ~[@ast::Item] {
fn get_exported_macros(&mut self, _: ast::CrateNum) -> ~[~str] {
fail!("lolwut")
}

Expand Down Expand Up @@ -1289,7 +1299,7 @@ mod test {
let item_ast = parse::parse_item_from_source_str(
@"<test>",
src,
cfg,~[],sess);
cfg,sess);
match item_ast {
Some(_) => (), // success
None => fail!("expected this to parse")
Expand Down
1 change: 0 additions & 1 deletion src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ pub mod rt {
@"<quote expansion>",
s,
self.cfg(),
~[],
self.parse_sess());
match res {
Some(ast) => ast,
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ pub fn parse_item_from_source_str(
name: @str,
source: @str,
cfg: ast::CrateConfig,
attrs: ~[ast::Attribute],
sess: @ParseSess
) -> Option<@ast::Item> {
let mut p = new_parser_from_source_str(sess, cfg, name, source);
let attrs = p.parse_outer_attributes();
maybe_aborted(p.parse_item(attrs),p)
}

Expand Down