From: Jeffrey Seyfried Date: Fri, 17 Mar 2017 21:58:48 +0000 (+0000) Subject: Refactor out `ast::MacroDef`. X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=9c6430b3257a96d587349d85aa7596d3f4704c28;p=rust.git Refactor out `ast::MacroDef`. --- diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index d359c69d3a0..77bcde22ef7 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -1505,7 +1505,7 @@ pub fn lower_item(&mut self, i: &Item) -> Option { if let ItemKind::MacroDef(ref tts) = i.node { if i.attrs.iter().any(|attr| attr.path == "macro_export") { self.exported_macros.push(hir::MacroDef { - name: name, attrs: attrs, id: i.id, span: i.span, body: tts.clone().into(), + name: name, attrs: attrs, id: i.id, span: i.span, body: tts.stream(), }); } return None; diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index 9d5ba2c8f95..a265a84114a 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -49,7 +49,6 @@ use hir::def_id::LOCAL_CRATE; use hir::intravisit as hir_visit; use syntax::visit as ast_visit; -use syntax::tokenstream::ThinTokenStream; /// Information about the registered lints. /// @@ -1127,7 +1126,7 @@ fn visit_attribute(&mut self, attr: &'a ast::Attribute) { run_lints!(self, check_attribute, early_passes, attr); } - fn visit_mac_def(&mut self, _mac: &'a ThinTokenStream, id: ast::NodeId) { + fn visit_mac_def(&mut self, _mac: &'a ast::MacroDef, id: ast::NodeId) { let lints = self.sess.lints.borrow_mut().take(id); for early_lint in lints { self.early_lint(&early_lint); diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index 7478f902e06..06472ed7fd1 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -386,7 +386,9 @@ fn load_macro(&self, id: DefId, sess: &Session) -> LoadedMacro { id: ast::DUMMY_NODE_ID, span: local_span, attrs: attrs.iter().cloned().collect(), - node: ast::ItemKind::MacroDef(body.into()), + node: ast::ItemKind::MacroDef(ast::MacroDef { + tokens: body.into(), + }), vis: ast::Visibility::Inherited, }) } diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index d463e41c58a..39ebe490d0e 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -16,7 +16,6 @@ use syntax::abi; use syntax::ast; use syntax::attr; -use syntax::tokenstream::TokenStream; use syntax_pos::Span; use rustc::hir::map as hir_map; @@ -214,8 +213,8 @@ pub fn visit_mod_contents(&mut self, span: Span, attrs: hir::HirVec continue, }; - let matchers = if let ast::ItemKind::MacroDef(ref tokens) = def.node { - let tts: Vec<_> = TokenStream::from(tokens.clone()).into_trees().collect(); + let matchers = if let ast::ItemKind::MacroDef(ref def) = def.node { + let tts: Vec<_> = def.stream().into_trees().collect(); tts.chunks(4).map(|arm| arm[0].span()).collect() } else { unreachable!() diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 24ce99208ed..6a30072c835 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1019,6 +1019,17 @@ pub fn stream(&self) -> TokenStream { } } +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] +pub struct MacroDef { + pub tokens: ThinTokenStream, +} + +impl MacroDef { + pub fn stream(&self) -> TokenStream { + self.tokens.clone().into() + } +} + #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] pub enum StrStyle { /// A regular string, like `"foo"` @@ -1863,7 +1874,7 @@ pub enum ItemKind { Mac(Mac), /// A macro definition. - MacroDef(ThinTokenStream), + MacroDef(MacroDef), } impl ItemKind { diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 73494d47fee..7ac3990def4 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -189,7 +189,7 @@ pub fn compile(sess: &ParseSess, features: &RefCell, def: &ast::Item) // Parse the macro_rules! invocation let body = match def.node { - ast::ItemKind::MacroDef(ref body) => body.clone().into(), + ast::ItemKind::MacroDef(ref body) => body.stream(), _ => unreachable!(), }; let argument_map = match parse(sess, body, &argument_gram, None, true) { diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 58cf50cdc00..9aeb9ecca5a 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -189,6 +189,10 @@ fn fold_mac(&mut self, _mac: Mac) -> Mac { // fold::noop_fold_mac(_mac, self) } + fn fold_macro_def(&mut self, def: MacroDef) -> MacroDef { + noop_fold_macro_def(def, self) + } + fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime { noop_fold_lifetime(l, self) } @@ -515,6 +519,12 @@ pub fn noop_fold_mac(Spanned {node, span}: Mac, fld: &mut T) -> Mac { } } +pub fn noop_fold_macro_def(def: MacroDef, fld: &mut T) -> MacroDef { + MacroDef { + tokens: fld.fold_tts(def.tokens.into()).into(), + } +} + pub fn noop_fold_meta_list_item(li: NestedMetaItem, fld: &mut T) -> NestedMetaItem { Spanned { @@ -919,7 +929,7 @@ pub fn noop_fold_item_kind(i: ItemKind, folder: &mut T) -> ItemKind { items.move_flat_map(|item| folder.fold_trait_item(item)), ), ItemKind::Mac(m) => ItemKind::Mac(folder.fold_mac(m)), - ItemKind::MacroDef(tts) => ItemKind::MacroDef(folder.fold_tts(tts.into()).into()), + ItemKind::MacroDef(def) => ItemKind::MacroDef(folder.fold_macro_def(def)), } } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index c28f678cb51..3c9ad8ca9c0 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3781,7 +3781,9 @@ fn eat_macro_def(&mut self, attrs: &[Attribute], vis: &Visibility) } let span = lo.to(self.prev_span); - let kind = ItemKind::MacroDef(tts); + let kind = ItemKind::MacroDef(ast::MacroDef { + tokens: tts, + }); Ok(Some(self.mk_item(span, id, kind, Visibility::Inherited, attrs.to_owned()))) } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 83c289ff80b..6c5bf56070e 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1392,7 +1392,7 @@ pub fn print_item(&mut self, item: &ast::Item) -> io::Result<()> { self.print_ident(item.ident)?; self.cbox(INDENT_UNIT)?; self.popen()?; - self.print_tts(tts.clone().into())?; + self.print_tts(tts.stream())?; self.pclose()?; word(&mut self.s, ";")?; self.end()?; diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 0fa0753b22c..d29d2497afe 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -27,7 +27,6 @@ use ast::*; use syntax_pos::Span; use codemap::Spanned; -use tokenstream::ThinTokenStream; #[derive(Copy, Clone, PartialEq, Eq)] pub enum FnKind<'a> { @@ -113,7 +112,7 @@ fn visit_mac(&mut self, _mac: &'ast Mac) { // definition in your trait impl: // visit::walk_mac(self, _mac) } - fn visit_mac_def(&mut self, _mac: &'ast ThinTokenStream, _id: NodeId) { + fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) { // Nothing to do } fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {