mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-10 14:57:14 +00:00
Add view items to the ast map
This commit is contained in:
parent
055cc2ee74
commit
22a059ddd1
@ -274,6 +274,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
||||
ast_map::NodeArg(..) |
|
||||
ast_map::NodeBlock(..) |
|
||||
ast_map::NodePat(..) |
|
||||
ast_map::NodeViewItem(..) |
|
||||
ast_map::NodeLocal(..) => {
|
||||
ccx.sess().bug(&format!("can't monomorphize a {:?}",
|
||||
map_node)[])
|
||||
|
@ -1528,6 +1528,19 @@ pub struct ViewItem {
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
impl ViewItem {
|
||||
pub fn id(&self) -> NodeId {
|
||||
match self.node {
|
||||
ViewItemExternCrate(_, _, id) => id,
|
||||
ViewItemUse(ref vp) => match vp.node {
|
||||
ViewPathSimple(_, _, id) => id,
|
||||
ViewPathGlob(_, id) => id,
|
||||
ViewPathList(_, _, id) => id,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)]
|
||||
pub enum ViewItem_ {
|
||||
/// Ident: name used to refer to this crate in the code
|
||||
|
@ -107,6 +107,7 @@ pub fn path_to_string<PI: Iterator<Item=PathElem>>(path: PI) -> String {
|
||||
#[derive(Copy, Show)]
|
||||
pub enum Node<'ast> {
|
||||
NodeItem(&'ast Item),
|
||||
NodeViewItem(&'ast ViewItem),
|
||||
NodeForeignItem(&'ast ForeignItem),
|
||||
NodeTraitItem(&'ast TraitItem),
|
||||
NodeImplItem(&'ast ImplItem),
|
||||
@ -133,6 +134,7 @@ enum MapEntry<'ast> {
|
||||
|
||||
/// All the node types, with a parent ID.
|
||||
EntryItem(NodeId, &'ast Item),
|
||||
EntryViewItem(NodeId, &'ast ViewItem),
|
||||
EntryForeignItem(NodeId, &'ast ForeignItem),
|
||||
EntryTraitItem(NodeId, &'ast TraitItem),
|
||||
EntryImplItem(NodeId, &'ast ImplItem),
|
||||
@ -167,6 +169,7 @@ impl<'ast> MapEntry<'ast> {
|
||||
fn from_node(p: NodeId, node: Node<'ast>) -> MapEntry<'ast> {
|
||||
match node {
|
||||
NodeItem(n) => EntryItem(p, n),
|
||||
NodeViewItem(n) => EntryViewItem(p, n),
|
||||
NodeForeignItem(n) => EntryForeignItem(p, n),
|
||||
NodeTraitItem(n) => EntryTraitItem(p, n),
|
||||
NodeImplItem(n) => EntryImplItem(p, n),
|
||||
@ -185,6 +188,7 @@ impl<'ast> MapEntry<'ast> {
|
||||
fn parent(self) -> Option<NodeId> {
|
||||
Some(match self {
|
||||
EntryItem(id, _) => id,
|
||||
EntryViewItem(id, _) => id,
|
||||
EntryForeignItem(id, _) => id,
|
||||
EntryTraitItem(id, _) => id,
|
||||
EntryImplItem(id, _) => id,
|
||||
@ -204,6 +208,7 @@ impl<'ast> MapEntry<'ast> {
|
||||
fn to_node(self) -> Option<Node<'ast>> {
|
||||
Some(match self {
|
||||
EntryItem(_, n) => NodeItem(n),
|
||||
EntryViewItem(_, n) => NodeViewItem(n),
|
||||
EntryForeignItem(_, n) => NodeForeignItem(n),
|
||||
EntryTraitItem(_, n) => NodeTraitItem(n),
|
||||
EntryImplItem(_, n) => NodeImplItem(n),
|
||||
@ -336,6 +341,13 @@ impl<'ast> Map<'ast> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_view_item(&self, id: NodeId) -> &'ast ViewItem {
|
||||
match self.find(id) {
|
||||
Some(NodeViewItem(view_item)) => view_item,
|
||||
_ => panic!("expected view item, found {}", self.node_to_string(id))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_struct(&self, id: NodeId) -> &'ast StructDef {
|
||||
match self.find(id) {
|
||||
Some(NodeItem(i)) => {
|
||||
@ -521,6 +533,7 @@ impl<'ast> Map<'ast> {
|
||||
pub fn opt_span(&self, id: NodeId) -> Option<Span> {
|
||||
let sp = match self.find(id) {
|
||||
Some(NodeItem(item)) => item.span,
|
||||
Some(NodeViewItem(item)) => item.span,
|
||||
Some(NodeForeignItem(foreign_item)) => foreign_item.span,
|
||||
Some(NodeTraitItem(trait_method)) => {
|
||||
match *trait_method {
|
||||
@ -813,6 +826,11 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
|
||||
self.parent = parent;
|
||||
}
|
||||
|
||||
fn visit_view_item(&mut self, item: &'ast ViewItem) {
|
||||
self.insert(item.id(), NodeViewItem(item));
|
||||
visit::walk_view_item(self, item);
|
||||
}
|
||||
|
||||
fn visit_pat(&mut self, pat: &'ast Pat) {
|
||||
self.insert(pat.id, match pat.node {
|
||||
// Note: this is at least *potentially* a pattern...
|
||||
@ -1018,6 +1036,7 @@ impl<'a> NodePrinter for pprust::State<'a> {
|
||||
fn print_node(&mut self, node: &Node) -> IoResult<()> {
|
||||
match *node {
|
||||
NodeItem(a) => self.print_item(&*a),
|
||||
NodeViewItem(a) => self.print_view_item(&*a),
|
||||
NodeForeignItem(a) => self.print_foreign_item(&*a),
|
||||
NodeTraitItem(a) => self.print_trait_method(&*a),
|
||||
NodeImplItem(a) => self.print_impl_item(&*a),
|
||||
@ -1060,6 +1079,9 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
|
||||
};
|
||||
format!("{} {}{}", item_str, path_str, id_str)
|
||||
}
|
||||
Some(NodeViewItem(item)) => {
|
||||
format!("view item {}{}", pprust::view_item_to_string(&*item), id_str)
|
||||
}
|
||||
Some(NodeForeignItem(item)) => {
|
||||
let path_str = map.path_to_str_with_ident(id, item.ident);
|
||||
format!("foreign item {}{}", path_str, id_str)
|
||||
|
Loading…
Reference in New Issue
Block a user