Skip to content

Commit

Permalink
Add view items to the ast map
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Jan 12, 2015
1 parent 055cc2e commit 22a059d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/librustc_trans/trans/monomorphize.rs
Expand Up @@ -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)[])
Expand Down
13 changes: 13 additions & 0 deletions src/libsyntax/ast.rs
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions src/libsyntax/ast_map/mod.rs
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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)) => {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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...
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 22a059d

Please sign in to comment.