about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2014-02-07 14:42:48 -0500
committerNiko Matsakis <niko@alum.mit.edu>2014-02-11 16:55:22 -0500
commitf7e5d8418c0e6dcd17efc61f7f5eb641dce8653d (patch)
tree30a59ca38f94bf3671fcea949d99458be3af9df0 /src
parent0e005ab84892aa201f720cc60d2aef002bfd7a4a (diff)
ty -- minor refactorings, helper methods
Diffstat (limited to 'src')
-rw-r--r--src/librustc/middle/ty.rs47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs
index 7b2aee9274a..5b58ec57c63 100644
--- a/src/librustc/middle/ty.rs
+++ b/src/librustc/middle/ty.rs
@@ -45,6 +45,7 @@ use syntax::attr;
 use syntax::attr::AttrMetaMethods;
 use syntax::codemap::Span;
 use syntax::parse::token;
+use syntax::parse::token::InternedString;
 use syntax::{ast, ast_map};
 use syntax::opt_vec::OptVec;
 use syntax::opt_vec;
@@ -2668,8 +2669,13 @@ pub fn node_id_to_trait_ref(cx: ctxt, id: ast::NodeId) -> @ty::TraitRef {
     }
 }
 
+pub fn try_node_id_to_type(cx: ctxt, id: ast::NodeId) -> Option<t> {
+    let node_types = cx.node_types.borrow();
+    node_types.get().find_copy(&(id as uint))
+}
+
 pub fn node_id_to_type(cx: ctxt, id: ast::NodeId) -> t {
-    match node_id_to_type_opt(cx, id) {
+    match try_node_id_to_type(cx, id) {
        Some(t) => t,
        None => cx.sess.bug(
            format!("node_id_to_type: no type for node `{}`",
@@ -2883,6 +2889,45 @@ pub fn expr_ty_adjusted(cx: ctxt, expr: &ast::Expr) -> t {
     adjust_ty(cx, expr.span, unadjusted_ty, adjustment)
 }
 
+pub fn expr_span(cx: ctxt, id: NodeId) -> Span {
+    match cx.items.find(id) {
+        Some(ast_map::NodeExpr(e)) => {
+            e.span
+        }
+        Some(f) => {
+            cx.sess.bug(format!("Node id {} is not an expr: {:?}",
+                                id, f));
+        }
+        None => {
+            cx.sess.bug(format!("Node id {} is not present \
+                                in the node map", id));
+        }
+    }
+}
+
+pub fn local_var_name_str(cx: ctxt, id: NodeId) -> InternedString {
+    match cx.items.find(id) {
+        Some(ast_map::NodeLocal(pat)) => {
+            match pat.node {
+                ast::PatIdent(_, ref path, _) => {
+                    let ident = ast_util::path_to_ident(path);
+                    token::get_ident(ident.name)
+                }
+                _ => {
+                    cx.sess.bug(
+                        format!("Variable id {} maps to {:?}, not local",
+                                id, pat));
+                }
+            }
+        }
+        r => {
+            cx.sess.bug(
+                format!("Variable id {} maps to {:?}, not local",
+                        id, r));
+        }
+    }
+}
+
 pub fn adjust_ty(cx: ctxt,
                  span: Span,
                  unadjusted_ty: ty::t,