|
| 1 | +// Copyright 2016 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 | +use ast; |
| 12 | +use codemap::{DUMMY_SP, dummy_spanned}; |
| 13 | +use ext::expand::{Expansion, ExpansionKind}; |
| 14 | +use fold::*; |
| 15 | +use parse::token::keywords; |
| 16 | +use ptr::P; |
| 17 | +use util::small_vector::SmallVector; |
| 18 | + |
| 19 | +use std::collections::HashMap; |
| 20 | + |
| 21 | +pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion { |
| 22 | + fn mac_placeholder() -> ast::Mac { |
| 23 | + dummy_spanned(ast::Mac_ { |
| 24 | + path: ast::Path { span: DUMMY_SP, global: false, segments: Vec::new() }, |
| 25 | + tts: Vec::new(), |
| 26 | + }) |
| 27 | + } |
| 28 | + |
| 29 | + let ident = keywords::Invalid.ident(); |
| 30 | + let attrs = Vec::new(); |
| 31 | + let vis = ast::Visibility::Inherited; |
| 32 | + let span = DUMMY_SP; |
| 33 | + let expr_placeholder = || P(ast::Expr { |
| 34 | + id: id, span: span, |
| 35 | + attrs: ast::ThinVec::new(), |
| 36 | + node: ast::ExprKind::Mac(mac_placeholder()), |
| 37 | + }); |
| 38 | + |
| 39 | + match kind { |
| 40 | + ExpansionKind::Expr => Expansion::Expr(expr_placeholder()), |
| 41 | + ExpansionKind::OptExpr => Expansion::OptExpr(Some(expr_placeholder())), |
| 42 | + ExpansionKind::Items => Expansion::Items(SmallVector::one(P(ast::Item { |
| 43 | + id: id, span: span, ident: ident, vis: vis, attrs: attrs, |
| 44 | + node: ast::ItemKind::Mac(mac_placeholder()), |
| 45 | + }))), |
| 46 | + ExpansionKind::TraitItems => Expansion::TraitItems(SmallVector::one(ast::TraitItem { |
| 47 | + id: id, span: span, ident: ident, attrs: attrs, |
| 48 | + node: ast::TraitItemKind::Macro(mac_placeholder()), |
| 49 | + })), |
| 50 | + ExpansionKind::ImplItems => Expansion::ImplItems(SmallVector::one(ast::ImplItem { |
| 51 | + id: id, span: span, ident: ident, vis: vis, attrs: attrs, |
| 52 | + node: ast::ImplItemKind::Macro(mac_placeholder()), |
| 53 | + defaultness: ast::Defaultness::Final, |
| 54 | + })), |
| 55 | + ExpansionKind::Pat => Expansion::Pat(P(ast::Pat { |
| 56 | + id: id, span: span, node: ast::PatKind::Mac(mac_placeholder()), |
| 57 | + })), |
| 58 | + ExpansionKind::Ty => Expansion::Ty(P(ast::Ty { |
| 59 | + id: id, span: span, node: ast::TyKind::Mac(mac_placeholder()), |
| 60 | + })), |
| 61 | + ExpansionKind::Stmts => Expansion::Stmts(SmallVector::one({ |
| 62 | + let mac = P((mac_placeholder(), ast::MacStmtStyle::Braces, ast::ThinVec::new())); |
| 63 | + ast::Stmt { id: id, span: span, node: ast::StmtKind::Mac(mac) } |
| 64 | + })), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +pub fn macro_scope_placeholder() -> Expansion { |
| 69 | + placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID) |
| 70 | +} |
| 71 | + |
| 72 | +pub struct PlaceholderExpander { |
| 73 | + expansions: HashMap<ast::NodeId, Expansion>, |
| 74 | +} |
| 75 | + |
| 76 | +impl PlaceholderExpander { |
| 77 | + pub fn new(expansions: HashMap<ast::NodeId, Expansion>) -> Self { |
| 78 | + PlaceholderExpander { |
| 79 | + expansions: expansions, |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + pub fn remove(&mut self, id: ast::NodeId) -> Expansion { |
| 84 | + let expansion = self.expansions.remove(&id).unwrap(); |
| 85 | + expansion.fold_with(self) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl Folder for PlaceholderExpander { |
| 90 | + fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> { |
| 91 | + match item.node { |
| 92 | + // Scope placeholder |
| 93 | + ast::ItemKind::Mac(_) if item.id == ast::DUMMY_NODE_ID => SmallVector::one(item), |
| 94 | + ast::ItemKind::Mac(_) => self.remove(item.id).make_items(), |
| 95 | + _ => noop_fold_item(item, self), |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> { |
| 100 | + match item.node { |
| 101 | + ast::TraitItemKind::Macro(_) => self.remove(item.id).make_trait_items(), |
| 102 | + _ => noop_fold_trait_item(item, self), |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> { |
| 107 | + match item.node { |
| 108 | + ast::ImplItemKind::Macro(_) => self.remove(item.id).make_impl_items(), |
| 109 | + _ => noop_fold_impl_item(item, self), |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> { |
| 114 | + match expr.node { |
| 115 | + ast::ExprKind::Mac(_) => self.remove(expr.id).make_expr(), |
| 116 | + _ => expr.map(|expr| noop_fold_expr(expr, self)), |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> { |
| 121 | + match expr.node { |
| 122 | + ast::ExprKind::Mac(_) => self.remove(expr.id).make_opt_expr(), |
| 123 | + _ => noop_fold_opt_expr(expr, self), |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> { |
| 128 | + let (style, mut expansion) = match stmt.node { |
| 129 | + ast::StmtKind::Mac(mac) => (mac.1, self.remove(stmt.id).make_stmts()), |
| 130 | + _ => return noop_fold_stmt(stmt, self), |
| 131 | + }; |
| 132 | + |
| 133 | + if style == ast::MacStmtStyle::Semicolon { |
| 134 | + if let Some(stmt) = expansion.pop() { |
| 135 | + expansion.push(stmt.add_trailing_semicolon()); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + expansion |
| 140 | + } |
| 141 | + |
| 142 | + fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> { |
| 143 | + match pat.node { |
| 144 | + ast::PatKind::Mac(_) => self.remove(pat.id).make_pat(), |
| 145 | + _ => noop_fold_pat(pat, self), |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> { |
| 150 | + match ty.node { |
| 151 | + ast::TyKind::Mac(_) => self.remove(ty.id).make_ty(), |
| 152 | + _ => noop_fold_ty(ty, self), |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +pub fn reconstructed_macro_rules(def: &ast::MacroDef, path: &ast::Path) -> Expansion { |
| 158 | + Expansion::Items(SmallVector::one(P(ast::Item { |
| 159 | + ident: def.ident, |
| 160 | + attrs: def.attrs.clone(), |
| 161 | + id: ast::DUMMY_NODE_ID, |
| 162 | + node: ast::ItemKind::Mac(ast::Mac { |
| 163 | + span: def.span, |
| 164 | + node: ast::Mac_ { |
| 165 | + path: path.clone(), |
| 166 | + tts: def.body.clone(), |
| 167 | + } |
| 168 | + }), |
| 169 | + vis: ast::Visibility::Inherited, |
| 170 | + span: def.span, |
| 171 | + }))) |
| 172 | +} |
0 commit comments