2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-09-04 13:37:29 -05:00
|
|
|
use ast::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
2013-01-25 18:57:39 -06:00
|
|
|
use ast_util::{inlined_item_utils, path_to_ident, stmt_id};
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast_util;
|
|
|
|
use attr;
|
|
|
|
use codemap;
|
2012-09-04 13:37:29 -05:00
|
|
|
use diagnostic::span_handler;
|
|
|
|
use parse::token::ident_interner;
|
2012-12-23 16:41:37 -06:00
|
|
|
use print::pprust;
|
|
|
|
use visit;
|
|
|
|
|
|
|
|
use core::cmp;
|
|
|
|
use core::either;
|
|
|
|
use core::str;
|
|
|
|
use core::vec;
|
2013-02-01 01:13:36 -06:00
|
|
|
use std::oldmap::HashMap;
|
|
|
|
use std::oldmap;
|
2012-12-23 16:41:37 -06:00
|
|
|
use std;
|
2011-06-19 15:41:21 -05:00
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub enum path_elt {
|
2012-08-27 18:26:35 -05:00
|
|
|
path_mod(ident),
|
|
|
|
path_name(ident)
|
|
|
|
}
|
|
|
|
|
2013-02-14 23:17:26 -06:00
|
|
|
pub impl cmp::Eq for path_elt {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &path_elt) -> bool {
|
|
|
|
match (*self) {
|
|
|
|
path_mod(e0a) => {
|
|
|
|
match (*other) {
|
|
|
|
path_mod(e0b) => e0a == e0b,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path_name(e0a) => {
|
|
|
|
match (*other) {
|
|
|
|
path_name(e0b) => e0a == e0b,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pure fn ne(&self, other: &path_elt) -> bool { !(*self).eq(other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub type path = ~[path_elt];
|
2012-02-03 02:53:37 -06:00
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn path_to_str_with_sep(p: &[path_elt], sep: ~str, itr: @ident_interner)
|
|
|
|
-> ~str {
|
2013-01-22 13:57:39 -06:00
|
|
|
let strs = do p.map |e| {
|
2012-09-21 20:43:30 -05:00
|
|
|
match *e {
|
2012-07-18 18:18:02 -05:00
|
|
|
path_mod(s) => *itr.get(s),
|
|
|
|
path_name(s) => *itr.get(s)
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
str::connect(strs, sep)
|
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn path_ident_to_str(p: path, i: ident, itr: @ident_interner) -> ~str {
|
2012-03-28 14:54:06 -05:00
|
|
|
if vec::is_empty(p) {
|
2012-07-18 18:18:02 -05:00
|
|
|
//FIXME /* FIXME (#2543) */ copy *i
|
|
|
|
*itr.get(i)
|
2012-03-28 14:54:06 -05:00
|
|
|
} else {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("%s::%s", path_to_str(p, itr), *itr.get(i))
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn path_to_str(p: &[path_elt], itr: @ident_interner) -> ~str {
|
2012-07-18 18:18:02 -05:00
|
|
|
path_to_str_with_sep(p, ~"::", itr)
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn path_elt_to_str(pe: path_elt, itr: @ident_interner) -> ~str {
|
2013-01-10 01:17:57 -06:00
|
|
|
match pe {
|
|
|
|
path_mod(s) => *itr.get(s),
|
|
|
|
path_name(s) => *itr.get(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub enum ast_node {
|
2012-02-03 02:53:37 -06:00
|
|
|
node_item(@item, @path),
|
2012-06-26 18:18:37 -05:00
|
|
|
node_foreign_item(@foreign_item, foreign_abi, @path),
|
2012-08-02 17:52:25 -05:00
|
|
|
node_trait_method(@trait_method, def_id /* trait did */,
|
|
|
|
@path /* path to the trait */),
|
2012-03-02 15:14:10 -06:00
|
|
|
node_method(@method, def_id /* impl did */, @path /* path to the impl */),
|
2012-03-07 05:21:08 -06:00
|
|
|
node_variant(variant, @item, @path),
|
2012-01-19 19:56:05 -06:00
|
|
|
node_expr(@expr),
|
2012-08-17 16:09:20 -05:00
|
|
|
node_stmt(@stmt),
|
2011-09-15 06:18:20 -05:00
|
|
|
// Locals are numbered, because the alias analysis needs to know in which
|
|
|
|
// order they are introduced.
|
2012-01-19 19:56:05 -06:00
|
|
|
node_arg(arg, uint),
|
|
|
|
node_local(uint),
|
2012-12-10 15:47:54 -06:00
|
|
|
// Destructor for a struct
|
|
|
|
node_dtor(~[ty_param], @struct_dtor, def_id, @path),
|
2012-03-15 20:46:18 -05:00
|
|
|
node_block(blk),
|
2012-10-26 20:23:45 -05:00
|
|
|
node_struct_ctor(@struct_def, @item, @path),
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
|
|
|
|
2013-02-01 01:13:36 -06:00
|
|
|
pub type map = std::oldmap::HashMap<node_id, ast_node>;
|
2013-02-04 16:02:01 -06:00
|
|
|
|
|
|
|
pub struct Ctx {
|
|
|
|
map: @map,
|
|
|
|
path: path,
|
|
|
|
local_id: uint,
|
2013-01-17 10:55:28 -06:00
|
|
|
diag: span_handler,
|
|
|
|
}
|
2011-06-19 15:41:21 -05:00
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub type vt = visit::vt<@mut Ctx>;
|
|
|
|
|
|
|
|
pub fn extend(cx: @mut Ctx, +elt: ident) -> @path {
|
2012-06-29 18:26:56 -05:00
|
|
|
@(vec::append(cx.path, ~[path_name(elt)]))
|
2012-03-06 06:57:07 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn mk_ast_map_visitor() -> vt {
|
2013-01-08 16:00:45 -06:00
|
|
|
return visit::mk_vt(@visit::Visitor {
|
2012-02-03 02:53:37 -06:00
|
|
|
visit_item: map_item,
|
|
|
|
visit_expr: map_expr,
|
2012-08-17 16:09:20 -05:00
|
|
|
visit_stmt: map_stmt,
|
2012-02-03 02:53:37 -06:00
|
|
|
visit_fn: map_fn,
|
|
|
|
visit_local: map_local,
|
2012-03-06 06:57:07 -06:00
|
|
|
visit_arm: map_arm,
|
2012-09-04 15:29:32 -05:00
|
|
|
visit_block: map_block,
|
|
|
|
.. *visit::default_visitor()
|
2012-02-14 17:21:53 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn map_crate(diag: span_handler, c: crate) -> map {
|
2013-02-04 16:02:01 -06:00
|
|
|
let cx = @mut Ctx {
|
|
|
|
map: @std::oldmap::HashMap(),
|
|
|
|
path: ~[],
|
|
|
|
local_id: 0u,
|
2013-01-17 10:55:28 -06:00
|
|
|
diag: diag,
|
|
|
|
};
|
2012-02-14 17:21:53 -06:00
|
|
|
visit::visit_crate(c, cx, mk_ast_map_visitor());
|
2013-02-04 16:02:01 -06:00
|
|
|
*cx.map
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
// Used for items loaded from external crate that are being inlined into this
|
2012-03-02 15:14:10 -06:00
|
|
|
// crate. The `path` should be the path to the item but should not include
|
|
|
|
// the item itself.
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn map_decoded_item(diag: span_handler,
|
2013-02-04 16:02:01 -06:00
|
|
|
map: map,
|
|
|
|
path: path,
|
|
|
|
ii: inlined_item) {
|
2012-02-14 17:21:53 -06:00
|
|
|
// I believe it is ok for the local IDs of inlined items from other crates
|
|
|
|
// to overlap with the local ids from this crate, so just generate the ids
|
|
|
|
// starting from 0. (In particular, I think these ids are only used in
|
|
|
|
// alias analysis, which we will not be running on the inlined items, and
|
|
|
|
// even if we did I think it only needs an ordering between local
|
|
|
|
// variables that are simultaneously in scope).
|
2013-02-04 16:02:01 -06:00
|
|
|
let cx = @mut Ctx {
|
|
|
|
map: @map,
|
|
|
|
path: path,
|
|
|
|
local_id: 0,
|
2013-01-17 10:55:28 -06:00
|
|
|
diag: diag,
|
|
|
|
};
|
2012-02-14 17:21:53 -06:00
|
|
|
let v = mk_ast_map_visitor();
|
2012-03-02 15:14:10 -06:00
|
|
|
|
|
|
|
// methods get added to the AST map when their impl is visited. Since we
|
|
|
|
// don't decode and instantiate the impl, but just the method, we have to
|
|
|
|
// add it to the table now:
|
2012-08-06 14:34:08 -05:00
|
|
|
match ii {
|
2012-10-08 13:49:01 -05:00
|
|
|
ii_item(*) | ii_dtor(*) => { /* fallthrough */ }
|
2012-08-03 21:59:04 -05:00
|
|
|
ii_foreign(i) => {
|
2012-06-26 18:18:37 -05:00
|
|
|
cx.map.insert(i.id, node_foreign_item(i, foreign_abi_rust_intrinsic,
|
2012-03-21 09:42:20 -05:00
|
|
|
@path));
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ii_method(impl_did, m) => {
|
2012-03-07 05:21:08 -06:00
|
|
|
map_method(impl_did, @path, m, cx);
|
2012-03-02 15:14:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// visit the item / method contents and add those to the map:
|
2012-03-01 21:37:52 -06:00
|
|
|
ii.accept(cx, v);
|
2012-03-01 12:36:22 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn map_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,
|
2013-02-04 16:02:01 -06:00
|
|
|
sp: codemap::span, id: node_id, &&cx: @mut Ctx, v: vt) {
|
2012-06-30 18:19:07 -05:00
|
|
|
for decl.inputs.each |a| {
|
2012-06-21 18:44:10 -05:00
|
|
|
cx.map.insert(a.id,
|
|
|
|
node_arg(/* FIXME (#2543) */
|
2012-09-19 18:55:01 -05:00
|
|
|
copy *a, cx.local_id));
|
2011-09-15 06:18:20 -05:00
|
|
|
cx.local_id += 1u;
|
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match fk {
|
2013-02-17 12:59:09 -06:00
|
|
|
visit::fk_dtor(ref tps, ref attrs, self_id, parent_id) => {
|
2012-12-27 13:36:00 -06:00
|
|
|
let dt = @spanned {
|
2013-01-13 14:29:36 -06:00
|
|
|
node: ast::struct_dtor_ {
|
|
|
|
id: id,
|
|
|
|
attrs: (*attrs),
|
|
|
|
self_id: self_id,
|
|
|
|
body: /* FIXME (#2543) */ copy body,
|
|
|
|
},
|
2012-12-27 13:36:00 -06:00
|
|
|
span: sp,
|
|
|
|
};
|
2013-02-17 12:59:09 -06:00
|
|
|
cx.map.insert(id, node_dtor(/* FIXME (#2543) */ copy *tps, dt,
|
2012-12-27 13:36:00 -06:00
|
|
|
parent_id,
|
|
|
|
@/* FIXME (#2543) */ copy cx.path));
|
2012-07-16 21:16:19 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2012-04-10 12:52:06 -05:00
|
|
|
}
|
2012-02-03 02:53:37 -06:00
|
|
|
visit::visit_fn(fk, decl, body, sp, id, cx, v);
|
2011-09-15 06:18:20 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn map_block(b: blk, &&cx: @mut Ctx, v: vt) {
|
2012-06-21 18:44:10 -05:00
|
|
|
cx.map.insert(b.node.id, node_block(/* FIXME (#2543) */ copy b));
|
2012-03-15 20:46:18 -05:00
|
|
|
visit::visit_block(b, cx, v);
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn number_pat(cx: @mut Ctx, pat: @pat) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do ast_util::walk_pat(pat) |p| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
pat_ident(*) => {
|
2012-02-22 09:57:23 -06:00
|
|
|
cx.map.insert(p.id, node_local(cx.local_id));
|
|
|
|
cx.local_id += 1u;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
2011-10-21 05:41:42 -05:00
|
|
|
};
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn map_local(loc: @local, &&cx: @mut Ctx, v: vt) {
|
2012-02-22 09:57:23 -06:00
|
|
|
number_pat(cx, loc.node.pat);
|
2012-02-03 02:53:37 -06:00
|
|
|
visit::visit_local(loc, cx, v);
|
2011-09-15 06:18:20 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn map_arm(arm: arm, &&cx: @mut Ctx, v: vt) {
|
2012-02-22 09:57:23 -06:00
|
|
|
number_pat(cx, arm.pats[0]);
|
2012-02-03 02:53:37 -06:00
|
|
|
visit::visit_arm(arm, cx, v);
|
2011-09-14 08:30:59 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn map_method(impl_did: def_id, impl_path: @path,
|
2013-02-04 16:02:01 -06:00
|
|
|
m: @method, &&cx: @mut Ctx) {
|
2012-03-02 15:14:10 -06:00
|
|
|
cx.map.insert(m.id, node_method(m, impl_did, impl_path));
|
2012-03-07 05:54:00 -06:00
|
|
|
cx.map.insert(m.self_id, node_local(cx.local_id));
|
|
|
|
cx.local_id += 1u;
|
2012-03-02 15:14:10 -06:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn map_item(i: @item, &&cx: @mut Ctx, v: vt) {
|
2012-06-21 18:44:10 -05:00
|
|
|
let item_path = @/* FIXME (#2543) */ copy cx.path;
|
2012-03-02 15:14:10 -06:00
|
|
|
cx.map.insert(i.id, node_item(i, item_path));
|
2012-08-06 14:34:08 -05:00
|
|
|
match i.node {
|
2012-11-13 21:08:01 -06:00
|
|
|
item_impl(_, _, _, ms) => {
|
2012-03-02 15:14:10 -06:00
|
|
|
let impl_did = ast_util::local_def(i.id);
|
2012-11-13 21:08:01 -06:00
|
|
|
for ms.each |m| {
|
|
|
|
map_method(impl_did, extend(cx, i.ident), *m, cx);
|
2012-03-02 15:14:10 -06:00
|
|
|
}
|
2011-12-19 00:36:37 -06:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
item_enum(ref enum_definition, _) => {
|
|
|
|
for (*enum_definition).variants.each |v| {
|
2012-02-08 03:05:44 -06:00
|
|
|
cx.map.insert(v.node.id, node_variant(
|
2012-09-19 18:55:01 -05:00
|
|
|
/* FIXME (#2543) */ copy *v, i,
|
2012-06-10 02:49:59 -05:00
|
|
|
extend(cx, i.ident)));
|
2012-02-08 03:05:44 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
item_foreign_mod(nm) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
let abi = match attr::foreign_abi(i.attrs) {
|
2012-12-04 12:50:00 -06:00
|
|
|
either::Left(ref msg) => cx.diag.span_fatal(i.span, (*msg)),
|
2012-08-14 18:54:13 -05:00
|
|
|
either::Right(abi) => abi
|
2012-03-07 05:21:08 -06:00
|
|
|
};
|
2012-06-30 18:19:07 -05:00
|
|
|
for nm.items.each |nitem| {
|
2012-06-07 23:53:47 -05:00
|
|
|
cx.map.insert(nitem.id,
|
2012-09-19 18:55:01 -05:00
|
|
|
node_foreign_item(*nitem, abi,
|
2012-06-21 18:44:10 -05:00
|
|
|
/* FIXME (#2543) */
|
2012-09-04 18:40:11 -05:00
|
|
|
if nm.sort == ast::named {
|
|
|
|
extend(cx, i.ident)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Anonymous extern mods go
|
|
|
|
in the parent scope */
|
|
|
|
@copy cx.path
|
|
|
|
}));
|
2012-03-07 05:21:08 -06:00
|
|
|
}
|
|
|
|
}
|
2012-12-10 15:47:54 -06:00
|
|
|
item_struct(struct_def, _) => {
|
|
|
|
map_struct_def(struct_def, node_item(i, item_path), i.ident, cx,
|
2012-08-07 18:08:09 -05:00
|
|
|
v);
|
2012-03-07 05:21:08 -06:00
|
|
|
}
|
2013-02-17 12:59:09 -06:00
|
|
|
item_trait(_, ref traits, ref methods) => {
|
2012-08-03 17:02:01 -05:00
|
|
|
for traits.each |p| {
|
|
|
|
cx.map.insert(p.ref_id, node_item(i, item_path));
|
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
for (*methods).each |tm| {
|
2012-09-19 18:55:01 -05:00
|
|
|
let id = ast_util::trait_method_to_ty_method(*tm).id;
|
2012-08-02 17:52:25 -05:00
|
|
|
let d_id = ast_util::local_def(i.id);
|
2012-09-19 18:55:01 -05:00
|
|
|
cx.map.insert(id, node_trait_method(@*tm, d_id, item_path));
|
2012-08-02 17:52:25 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match i.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
item_mod(_) | item_foreign_mod(_) => {
|
2012-09-26 19:33:34 -05:00
|
|
|
cx.path.push(path_mod(i.ident));
|
2012-03-06 06:57:07 -06:00
|
|
|
}
|
2012-09-26 19:33:34 -05:00
|
|
|
_ => cx.path.push(path_name(i.ident))
|
2012-02-03 02:53:37 -06:00
|
|
|
}
|
|
|
|
visit::visit_item(i, cx, v);
|
2012-09-28 00:20:47 -05:00
|
|
|
cx.path.pop();
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn map_struct_def(struct_def: @ast::struct_def, parent_node: ast_node,
|
2013-02-04 16:02:01 -06:00
|
|
|
ident: ast::ident, cx: @mut Ctx, _v: vt) {
|
2012-08-07 18:08:09 -05:00
|
|
|
let p = extend(cx, ident);
|
2012-10-24 16:36:00 -05:00
|
|
|
// If this is a tuple-like struct, register the constructor.
|
|
|
|
match struct_def.ctor_id {
|
|
|
|
None => {}
|
|
|
|
Some(ctor_id) => {
|
|
|
|
match parent_node {
|
|
|
|
node_item(item, _) => {
|
|
|
|
cx.map.insert(ctor_id,
|
|
|
|
node_struct_ctor(struct_def, item, p));
|
|
|
|
}
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"struct def parent wasn't an item")
|
2012-10-24 16:36:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-07 18:08:09 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn map_expr(ex: @expr, &&cx: @mut Ctx, v: vt) {
|
2011-09-15 06:18:20 -05:00
|
|
|
cx.map.insert(ex.id, node_expr(ex));
|
2012-02-03 02:53:37 -06:00
|
|
|
visit::visit_expr(ex, cx, v);
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn map_stmt(stmt: @stmt, &&cx: @mut Ctx, v: vt) {
|
2012-08-17 16:09:20 -05:00
|
|
|
cx.map.insert(stmt_id(*stmt), node_stmt(stmt));
|
|
|
|
visit::visit_stmt(stmt, cx, v);
|
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn node_id_to_str(map: map, id: node_id, itr: @ident_interner) -> ~str {
|
2013-02-05 21:41:45 -06:00
|
|
|
match map.find(&id) {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("unknown node (id=%d)", id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node_item(item, path)) => {
|
2012-08-09 11:59:50 -05:00
|
|
|
let path_str = path_ident_to_str(*path, item.ident, itr);
|
|
|
|
let item_str = match item.node {
|
|
|
|
item_const(*) => ~"const",
|
|
|
|
item_fn(*) => ~"fn",
|
|
|
|
item_mod(*) => ~"mod",
|
|
|
|
item_foreign_mod(*) => ~"foreign mod",
|
|
|
|
item_ty(*) => ~"ty",
|
|
|
|
item_enum(*) => ~"enum",
|
2012-12-10 15:47:54 -06:00
|
|
|
item_struct(*) => ~"struct",
|
2012-08-09 11:59:50 -05:00
|
|
|
item_trait(*) => ~"trait",
|
|
|
|
item_impl(*) => ~"impl",
|
|
|
|
item_mac(*) => ~"macro"
|
|
|
|
};
|
|
|
|
fmt!("%s %s (id=%?)", item_str, path_str, id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node_foreign_item(item, abi, path)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("foreign item %s with abi %? (id=%?)",
|
|
|
|
path_ident_to_str(*path, item.ident, itr), abi, id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2012-08-26 14:12:05 -05:00
|
|
|
Some(node_method(m, _, path)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("method %s in %s (id=%?)",
|
|
|
|
*itr.get(m.ident), path_to_str(*path, itr), id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2012-08-26 14:12:05 -05:00
|
|
|
Some(node_trait_method(tm, _, path)) => {
|
2012-08-02 17:52:25 -05:00
|
|
|
let m = ast_util::trait_method_to_ty_method(*tm);
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("method %s in %s (id=%?)",
|
|
|
|
*itr.get(m.ident), path_to_str(*path, itr), id)
|
2012-08-02 17:52:25 -05:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
Some(node_variant(ref variant, _, path)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("variant %s in %s (id=%?)",
|
2012-12-04 12:50:00 -06:00
|
|
|
*itr.get((*variant).node.name), path_to_str(*path, itr), id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node_expr(expr)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("expr %s (id=%?)", pprust::expr_to_str(expr, itr), id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node_stmt(stmt)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("stmt %s (id=%?)",
|
|
|
|
pprust::stmt_to_str(*stmt, itr), id)
|
2012-08-17 16:09:20 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node_arg(_, _)) => { // add more info here
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("arg (id=%?)", id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node_local(_)) => { // add more info here
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("local (id=%?)", id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node_dtor(*)) => { // add more info here
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("node_dtor (id=%?)", id)
|
2012-05-14 16:13:32 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node_block(_)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("block")
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2012-10-24 16:36:00 -05:00
|
|
|
Some(node_struct_ctor(*)) => {
|
|
|
|
fmt!("struct_ctor")
|
|
|
|
}
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
|
|
|
}
|
2012-11-30 13:24:16 -06:00
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn node_item_query<Result>(items: map, id: node_id,
|
2012-11-30 13:24:16 -06:00
|
|
|
query: fn(@item) -> Result,
|
|
|
|
error_msg: ~str) -> Result {
|
2013-02-05 21:41:45 -06:00
|
|
|
match items.find(&id) {
|
2012-11-30 13:24:16 -06:00
|
|
|
Some(node_item(it, _)) => query(it),
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(error_msg)
|
2012-11-30 13:24:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-19 15:41:21 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|