2014-04-22 20:09:21 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2012-09-04 13:37:29 -05:00
|
|
|
use ast::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
|
|
|
use ast_util;
|
2014-02-15 00:26:51 -06:00
|
|
|
use codemap;
|
2013-11-30 16:00:39 -06:00
|
|
|
use codemap::Span;
|
2014-03-19 09:52:37 -05:00
|
|
|
use owned_slice::OwnedSlice;
|
2013-01-08 21:37:25 -06:00
|
|
|
use parse::token;
|
2014-02-13 23:07:09 -06:00
|
|
|
use print::pprust;
|
2014-09-13 11:06:01 -05:00
|
|
|
use ptr::P;
|
2013-09-14 00:10:48 -05:00
|
|
|
use visit::Visitor;
|
2012-12-23 16:41:37 -06:00
|
|
|
use visit;
|
|
|
|
|
2014-02-06 01:34:33 -06:00
|
|
|
use std::cmp;
|
2013-11-26 23:02:25 -06:00
|
|
|
use std::u32;
|
2013-04-03 12:28:14 -05:00
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn path_name_i(idents: &[Ident]) -> String {
|
2012-06-14 20:46:33 -05:00
|
|
|
// FIXME: Bad copies (#2543 -- same for everything else that says "bad")
|
2014-03-28 14:42:34 -05:00
|
|
|
idents.iter().map(|i| {
|
2015-02-03 16:31:06 -06:00
|
|
|
token::get_ident(*i).to_string()
|
2014-06-26 01:15:14 -05:00
|
|
|
}).collect::<Vec<String>>().connect("::")
|
2012-06-10 02:49:59 -05:00
|
|
|
}
|
2011-08-21 23:44:41 -05:00
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn local_def(id: NodeId) -> DefId {
|
2014-02-05 15:15:24 -06:00
|
|
|
ast::DefId { krate: LOCAL_CRATE, node: id }
|
2013-01-13 13:05:40 -06:00
|
|
|
}
|
2012-03-21 14:42:34 -05:00
|
|
|
|
2014-02-05 15:15:24 -06:00
|
|
|
pub fn is_local(did: ast::DefId) -> bool { did.krate == LOCAL_CRATE }
|
2011-08-21 23:44:41 -05:00
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn stmt_id(s: &Stmt) -> NodeId {
|
2012-08-06 14:34:08 -05:00
|
|
|
match s.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
StmtDecl(_, id) => id,
|
|
|
|
StmtExpr(_, id) => id,
|
|
|
|
StmtSemi(_, id) => id,
|
2014-10-09 14:17:22 -05:00
|
|
|
StmtMac(..) => panic!("attempted to analyze unexpanded stmt")
|
2012-02-14 17:21:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:24:37 -06:00
|
|
|
pub fn binop_to_string(op: BinOp_) -> &'static str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match op {
|
2014-02-24 02:33:50 -06:00
|
|
|
BiAdd => "+",
|
|
|
|
BiSub => "-",
|
|
|
|
BiMul => "*",
|
|
|
|
BiDiv => "/",
|
|
|
|
BiRem => "%",
|
|
|
|
BiAnd => "&&",
|
|
|
|
BiOr => "||",
|
|
|
|
BiBitXor => "^",
|
|
|
|
BiBitAnd => "&",
|
|
|
|
BiBitOr => "|",
|
|
|
|
BiShl => "<<",
|
|
|
|
BiShr => ">>",
|
|
|
|
BiEq => "==",
|
|
|
|
BiLt => "<",
|
|
|
|
BiLe => "<=",
|
|
|
|
BiNe => "!=",
|
|
|
|
BiGe => ">=",
|
|
|
|
BiGt => ">"
|
2013-09-01 20:45:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:24:37 -06:00
|
|
|
pub fn lazy_binop(b: BinOp_) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match b {
|
2013-09-01 20:45:37 -05:00
|
|
|
BiAnd => true,
|
|
|
|
BiOr => true,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
|
|
|
}
|
2011-08-21 23:44:41 -05:00
|
|
|
}
|
|
|
|
|
2015-01-12 21:24:37 -06:00
|
|
|
pub fn is_shift_binop(b: BinOp_) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match b {
|
2013-09-01 20:45:37 -05:00
|
|
|
BiShl => true,
|
|
|
|
BiShr => true,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
2012-02-21 23:01:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:24:37 -06:00
|
|
|
pub fn is_comparison_binop(b: BinOp_) -> bool {
|
2015-01-07 18:44:01 -06:00
|
|
|
match b {
|
2015-03-24 14:34:59 -05:00
|
|
|
BiEq | BiLt | BiLe | BiNe | BiGt | BiGe =>
|
|
|
|
true,
|
|
|
|
BiAnd | BiOr | BiAdd | BiSub | BiMul | BiDiv | BiRem |
|
|
|
|
BiBitXor | BiBitAnd | BiBitOr | BiShl | BiShr =>
|
|
|
|
false,
|
2015-01-07 18:44:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 14:49:41 -06:00
|
|
|
/// Returns `true` if the binary operator takes its arguments by value
|
2015-01-12 21:24:37 -06:00
|
|
|
pub fn is_by_value_binop(b: BinOp_) -> bool {
|
2015-03-24 14:34:59 -05:00
|
|
|
!is_comparison_binop(b)
|
2015-01-31 12:49:12 -06:00
|
|
|
}
|
|
|
|
|
2014-12-15 14:49:41 -06:00
|
|
|
/// Returns `true` if the unary operator takes its argument by value
|
|
|
|
pub fn is_by_value_unop(u: UnOp) -> bool {
|
|
|
|
match u {
|
|
|
|
UnNeg | UnNot => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 05:39:03 -05:00
|
|
|
pub fn unop_to_string(op: UnOp) -> &'static str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match op {
|
2014-05-05 20:56:44 -05:00
|
|
|
UnUniq => "box() ",
|
2013-12-31 14:55:39 -06:00
|
|
|
UnDeref => "*",
|
|
|
|
UnNot => "!",
|
|
|
|
UnNeg => "-",
|
2011-08-21 23:44:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn is_path(e: P<Expr>) -> bool {
|
2015-02-17 11:29:13 -06:00
|
|
|
match e.node { ExprPath(..) => true, _ => false }
|
2011-08-21 23:44:41 -05:00
|
|
|
}
|
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// Get a string representation of a signed int type, with its value.
|
|
|
|
/// We want to avoid "45int" and "-3int" in favor of "45" and "-3"
|
2014-06-21 05:39:03 -05:00
|
|
|
pub fn int_ty_to_string(t: IntTy, val: Option<i64>) -> String {
|
2014-04-10 19:21:01 -05:00
|
|
|
let s = match t {
|
2015-03-24 15:26:16 -05:00
|
|
|
TyIs => "isize",
|
2014-04-10 19:21:01 -05:00
|
|
|
TyI8 => "i8",
|
|
|
|
TyI16 => "i16",
|
|
|
|
TyI32 => "i32",
|
|
|
|
TyI64 => "i64"
|
|
|
|
};
|
|
|
|
|
|
|
|
match val {
|
2014-05-10 23:57:49 -05:00
|
|
|
// cast to a u64 so we can correctly print INT64_MIN. All integral types
|
|
|
|
// are parsed as u64, so we wouldn't want to print an extra negative
|
|
|
|
// sign.
|
2014-06-26 01:15:14 -05:00
|
|
|
Some(n) => format!("{}{}", n as u64, s),
|
2014-05-25 05:17:19 -05:00
|
|
|
None => s.to_string()
|
2011-08-21 23:44:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn int_ty_max(t: IntTy) -> u64 {
|
2012-08-06 14:34:08 -05:00
|
|
|
match t {
|
2015-03-03 02:42:26 -06:00
|
|
|
TyI8 => 0x80,
|
|
|
|
TyI16 => 0x8000,
|
2015-03-24 15:26:16 -05:00
|
|
|
TyIs | TyI32 => 0x80000000, // actually ni about TyIs
|
2015-03-03 02:42:26 -06:00
|
|
|
TyI64 => 0x8000000000000000
|
2011-12-07 14:53:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// Get a string representation of an unsigned int type, with its value.
|
2015-01-17 17:33:05 -06:00
|
|
|
/// We want to avoid "42u" in favor of "42us". "42uint" is right out.
|
2014-06-21 05:39:03 -05:00
|
|
|
pub fn uint_ty_to_string(t: UintTy, val: Option<u64>) -> String {
|
2014-04-10 19:21:01 -05:00
|
|
|
let s = match t {
|
2015-03-24 15:26:16 -05:00
|
|
|
TyUs => "usize",
|
2014-04-10 19:21:01 -05:00
|
|
|
TyU8 => "u8",
|
|
|
|
TyU16 => "u16",
|
|
|
|
TyU32 => "u32",
|
|
|
|
TyU64 => "u64"
|
|
|
|
};
|
|
|
|
|
|
|
|
match val {
|
2014-06-26 01:15:14 -05:00
|
|
|
Some(n) => format!("{}{}", n, s),
|
2014-05-25 05:17:19 -05:00
|
|
|
None => s.to_string()
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn uint_ty_max(t: UintTy) -> u64 {
|
2012-08-06 14:34:08 -05:00
|
|
|
match t {
|
2015-03-03 02:42:26 -06:00
|
|
|
TyU8 => 0xff,
|
|
|
|
TyU16 => 0xffff,
|
2015-03-24 15:26:16 -05:00
|
|
|
TyUs | TyU32 => 0xffffffff, // actually ni about TyUs
|
2015-03-03 02:42:26 -06:00
|
|
|
TyU64 => 0xffffffffffffffff
|
2011-12-07 14:53:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 05:39:03 -05:00
|
|
|
pub fn float_ty_to_string(t: FloatTy) -> String {
|
2014-05-07 18:33:43 -05:00
|
|
|
match t {
|
2014-05-25 05:17:19 -05:00
|
|
|
TyF32 => "f32".to_string(),
|
|
|
|
TyF64 => "f64".to_string(),
|
2014-05-07 18:33:43 -05:00
|
|
|
}
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
2011-08-21 23:44:41 -05:00
|
|
|
|
2014-06-30 20:02:14 -05:00
|
|
|
// convert a span and an identifier to the corresponding
|
|
|
|
// 1-segment path
|
2013-09-01 19:50:59 -05:00
|
|
|
pub fn ident_to_path(s: Span, identifier: Ident) -> Path {
|
2013-08-07 11:47:28 -05:00
|
|
|
ast::Path {
|
|
|
|
span: s,
|
|
|
|
global: false,
|
2014-02-28 15:09:09 -06:00
|
|
|
segments: vec!(
|
2013-08-07 11:47:28 -05:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2014-11-03 20:52:52 -06:00
|
|
|
parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
|
|
|
|
lifetimes: Vec::new(),
|
|
|
|
types: OwnedSlice::empty(),
|
2014-11-28 22:08:30 -06:00
|
|
|
bindings: OwnedSlice::empty(),
|
2014-11-03 20:52:52 -06:00
|
|
|
})
|
2013-08-07 11:47:28 -05:00
|
|
|
}
|
2014-02-28 15:09:09 -06:00
|
|
|
),
|
2013-08-07 11:47:28 -05:00
|
|
|
}
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|
|
|
|
|
2014-11-28 22:08:30 -06:00
|
|
|
// If path is a single segment ident path, return that ident. Otherwise, return
|
|
|
|
// None.
|
|
|
|
pub fn path_to_ident(path: &Path) -> Option<Ident> {
|
|
|
|
if path.segments.len() != 1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let segment = &path.segments[0];
|
|
|
|
if !segment.parameters.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(segment.identifier)
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> P<Pat> {
|
|
|
|
P(Pat {
|
|
|
|
id: id,
|
|
|
|
node: PatIdent(BindByValue(MutImmutable), codemap::Spanned{span:s, node:i}, None),
|
|
|
|
span: s
|
|
|
|
})
|
2012-11-06 20:41:06 -06:00
|
|
|
}
|
|
|
|
|
2014-02-15 00:26:51 -06:00
|
|
|
pub fn name_to_dummy_lifetime(name: Name) -> Lifetime {
|
|
|
|
Lifetime { id: DUMMY_NODE_ID,
|
|
|
|
span: codemap::DUMMY_SP,
|
|
|
|
name: name }
|
|
|
|
}
|
|
|
|
|
2014-02-13 23:07:09 -06:00
|
|
|
/// Generate a "pretty" name for an `impl` from its type and trait.
|
|
|
|
/// This is designed so that symbols of `impl`'d methods give some
|
|
|
|
/// hint of where they came from, (previously they would all just be
|
|
|
|
/// listed as `__extensions__::method_name::hash`, with no indication
|
|
|
|
/// of the type).
|
2015-01-22 15:14:52 -06:00
|
|
|
pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: Option<&Ty>) -> Ident {
|
|
|
|
let mut pretty = match ty {
|
|
|
|
Some(t) => pprust::ty_to_string(t),
|
|
|
|
None => String::from_str("..")
|
|
|
|
};
|
|
|
|
|
2014-02-13 23:07:09 -06:00
|
|
|
match *trait_ref {
|
|
|
|
Some(ref trait_ref) => {
|
2014-10-15 01:05:01 -05:00
|
|
|
pretty.push('.');
|
2015-02-18 13:48:57 -06:00
|
|
|
pretty.push_str(&pprust::path_to_string(&trait_ref.path));
|
2014-02-13 23:07:09 -06:00
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
2015-02-18 13:48:57 -06:00
|
|
|
token::gensym_ident(&pretty[..])
|
2014-02-13 23:07:09 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn struct_field_visibility(field: ast::StructField) -> Visibility {
|
2012-08-15 17:53:58 -05:00
|
|
|
match field.node.kind {
|
2014-03-25 18:53:52 -05:00
|
|
|
ast::NamedField(_, v) | ast::UnnamedField(v) => v
|
2012-08-15 17:53:58 -05:00
|
|
|
}
|
2012-03-28 20:50:33 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Maps a binary operator to its precedence
|
2015-01-12 21:24:37 -06:00
|
|
|
pub fn operator_prec(op: ast::BinOp_) -> usize {
|
2012-08-06 14:34:08 -05:00
|
|
|
match op {
|
2013-07-19 20:42:11 -05:00
|
|
|
// 'as' sits here with 12
|
2015-01-27 19:01:48 -06:00
|
|
|
BiMul | BiDiv | BiRem => 11,
|
|
|
|
BiAdd | BiSub => 10,
|
|
|
|
BiShl | BiShr => 9,
|
|
|
|
BiBitAnd => 8,
|
|
|
|
BiBitXor => 7,
|
|
|
|
BiBitOr => 6,
|
|
|
|
BiLt | BiLe | BiGe | BiGt | BiEq | BiNe => 3,
|
|
|
|
BiAnd => 2,
|
|
|
|
BiOr => 1
|
2012-04-26 18:13:59 -05:00
|
|
|
}
|
|
|
|
}
|
2012-05-14 16:13:32 -05:00
|
|
|
|
2013-03-29 12:04:48 -05:00
|
|
|
/// Precedence of the `as` operator, which is a binary operator
|
|
|
|
/// not appearing in the prior table.
|
2015-01-27 19:01:48 -06:00
|
|
|
pub const AS_PREC: usize = 12;
|
2013-03-29 12:04:48 -05:00
|
|
|
|
2013-02-14 23:50:03 -06:00
|
|
|
pub fn empty_generics() -> Generics {
|
2014-08-11 11:32:26 -05:00
|
|
|
Generics {
|
|
|
|
lifetimes: Vec::new(),
|
|
|
|
ty_params: OwnedSlice::empty(),
|
|
|
|
where_clause: WhereClause {
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
predicates: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
|
2012-05-16 15:21:04 -05:00
|
|
|
// ______________________________________________________________________
|
|
|
|
// Enumerating the IDs which appear in an AST
|
|
|
|
|
2015-03-30 08:38:59 -05:00
|
|
|
#[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
|
2014-01-09 07:05:33 -06:00
|
|
|
pub struct IdRange {
|
2014-03-27 17:39:48 -05:00
|
|
|
pub min: NodeId,
|
|
|
|
pub max: NodeId,
|
2013-01-17 10:55:28 -06:00
|
|
|
}
|
2012-05-16 15:21:04 -05:00
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
impl IdRange {
|
|
|
|
pub fn max() -> IdRange {
|
|
|
|
IdRange {
|
2014-01-25 01:37:51 -06:00
|
|
|
min: u32::MAX,
|
|
|
|
max: u32::MIN,
|
2013-05-31 17:17:22 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn empty(&self) -> bool {
|
2013-03-15 14:24:24 -05:00
|
|
|
self.min >= self.max
|
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn add(&mut self, id: NodeId) {
|
2014-02-06 01:34:33 -06:00
|
|
|
self.min = cmp::min(self.min, id);
|
|
|
|
self.max = cmp::max(self.max, id + 1);
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2012-05-16 15:21:04 -05:00
|
|
|
}
|
|
|
|
|
2013-08-29 21:01:19 -05:00
|
|
|
pub trait IdVisitingOperation {
|
2014-10-25 14:27:15 -05:00
|
|
|
fn visit_id(&mut self, node_id: NodeId);
|
2013-08-29 21:01:19 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 23:22:11 -05:00
|
|
|
/// A visitor that applies its operation to all of the node IDs
|
|
|
|
/// in a visitable thing.
|
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
pub struct IdVisitor<'a, O:'a> {
|
2014-10-25 14:27:15 -05:00
|
|
|
pub operation: &'a mut O,
|
2014-08-27 20:46:52 -05:00
|
|
|
pub pass_through_items: bool,
|
|
|
|
pub visited_outermost: bool,
|
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a, O: IdVisitingOperation> IdVisitor<'a, O> {
|
2014-10-25 14:27:15 -05:00
|
|
|
fn visit_generics_helper(&mut self, generics: &Generics) {
|
2015-01-31 11:20:46 -06:00
|
|
|
for type_parameter in &*generics.ty_params {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(type_parameter.id)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
2015-01-31 11:20:46 -06:00
|
|
|
for lifetime in &generics.lifetimes {
|
2014-08-05 21:59:24 -05:00
|
|
|
self.operation.visit_id(lifetime.lifetime.id)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 17:54:36 -05:00
|
|
|
impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_mod(&mut self,
|
2014-01-09 07:05:33 -06:00
|
|
|
module: &Mod,
|
2013-08-31 11:13:04 -05:00
|
|
|
_: Span,
|
2014-09-12 05:10:30 -05:00
|
|
|
node_id: NodeId) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(node_id);
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_mod(self, module)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(foreign_item.id);
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_foreign_item(self, foreign_item)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_item(&mut self, item: &Item) {
|
2013-07-19 20:42:11 -05:00
|
|
|
if !self.pass_through_items {
|
|
|
|
if self.visited_outermost {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
self.visited_outermost = true
|
2012-05-16 15:21:04 -05:00
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(item.id);
|
2015-01-13 09:30:17 -06:00
|
|
|
match item.node {
|
|
|
|
ItemUse(ref view_path) => {
|
|
|
|
match view_path.node {
|
|
|
|
ViewPathSimple(_, _) |
|
|
|
|
ViewPathGlob(_) => {}
|
|
|
|
ViewPathList(_, ref paths) => {
|
2015-01-31 11:20:46 -06:00
|
|
|
for path in paths {
|
2015-01-13 09:30:17 -06:00
|
|
|
self.operation.visit_id(path.node.id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ItemEnum(ref enum_definition, _) => {
|
2015-01-31 11:20:46 -06:00
|
|
|
for variant in &enum_definition.variants {
|
2015-01-13 09:30:17 -06:00
|
|
|
self.operation.visit_id(variant.node.id)
|
|
|
|
}
|
2013-06-01 17:31:56 -05:00
|
|
|
}
|
2015-01-13 09:30:17 -06:00
|
|
|
_ => {}
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_item(self, item);
|
2013-07-19 20:42:11 -05:00
|
|
|
|
|
|
|
self.visited_outermost = false
|
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_local(&mut self, local: &Local) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(local.id);
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_local(self, local)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_block(&mut self, block: &Block) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(block.id);
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_block(self, block)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_stmt(&mut self, statement: &Stmt) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(ast_util::stmt_id(statement));
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_stmt(self, statement)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_pat(&mut self, pattern: &Pat) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(pattern.id);
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_pat(self, pattern)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_expr(&mut self, expression: &Expr) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(expression.id);
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_expr(self, expression)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
2012-05-16 15:21:04 -05:00
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_ty(&mut self, typ: &Ty) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(typ.id);
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_ty(self, typ)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
2012-05-16 15:21:04 -05:00
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_generics(&mut self, generics: &Generics) {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.visit_generics_helper(generics);
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_generics(self, generics)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_fn(&mut self,
|
2014-09-09 17:54:36 -05:00
|
|
|
function_kind: visit::FnKind<'v>,
|
|
|
|
function_declaration: &'v FnDecl,
|
|
|
|
block: &'v Block,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2014-09-12 05:10:30 -05:00
|
|
|
node_id: NodeId) {
|
2013-07-19 20:42:11 -05:00
|
|
|
if !self.pass_through_items {
|
2014-09-09 17:54:36 -05:00
|
|
|
match function_kind {
|
2014-01-09 07:05:33 -06:00
|
|
|
visit::FkMethod(..) if self.visited_outermost => return,
|
|
|
|
visit::FkMethod(..) => self.visited_outermost = true,
|
2013-07-19 20:42:11 -05:00
|
|
|
_ => {}
|
2012-05-16 15:21:04 -05:00
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(node_id);
|
2012-05-16 15:21:04 -05:00
|
|
|
|
2014-09-09 17:54:36 -05:00
|
|
|
match function_kind {
|
2015-03-10 05:28:44 -05:00
|
|
|
visit::FkItemFn(_, generics, _, _) => {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.visit_generics_helper(generics)
|
2012-05-16 15:21:04 -05:00
|
|
|
}
|
2015-03-11 16:38:58 -05:00
|
|
|
visit::FkMethod(_, sig) => {
|
|
|
|
self.visit_generics_helper(&sig.generics)
|
2015-03-10 05:28:44 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
visit::FkFnBlock => {}
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2015-01-31 11:20:46 -06:00
|
|
|
for argument in &function_declaration.inputs {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(argument.id)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_fn(self,
|
2014-11-15 15:55:27 -06:00
|
|
|
function_kind,
|
|
|
|
function_declaration,
|
|
|
|
block,
|
|
|
|
span);
|
2013-07-19 20:42:11 -05:00
|
|
|
|
|
|
|
if !self.pass_through_items {
|
2014-11-29 15:41:21 -06:00
|
|
|
if let visit::FkMethod(..) = function_kind {
|
|
|
|
self.visited_outermost = false;
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_struct_field(&mut self, struct_field: &StructField) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(struct_field.node.id);
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_struct_field(self, struct_field)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
2013-09-26 23:53:40 -05:00
|
|
|
|
|
|
|
fn visit_struct_def(&mut self,
|
2014-01-09 07:05:33 -06:00
|
|
|
struct_def: &StructDef,
|
2014-05-14 01:20:25 -05:00
|
|
|
_: ast::Ident,
|
|
|
|
_: &ast::Generics,
|
2014-09-12 05:10:30 -05:00
|
|
|
id: NodeId) {
|
2013-09-26 23:53:40 -05:00
|
|
|
self.operation.visit_id(id);
|
2013-09-20 01:08:47 -05:00
|
|
|
struct_def.ctor_id.map(|ctor_id| self.operation.visit_id(ctor_id));
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_struct_def(self, struct_def);
|
2013-09-26 23:53:40 -05:00
|
|
|
}
|
2013-10-02 13:29:29 -05:00
|
|
|
|
2015-03-10 05:28:44 -05:00
|
|
|
fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
|
|
|
|
self.operation.visit_id(ti.id);
|
|
|
|
visit::walk_trait_item(self, ti);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
|
|
|
|
self.operation.visit_id(ii.id);
|
|
|
|
visit::walk_impl_item(self, ii);
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
2014-05-17 18:38:13 -05:00
|
|
|
|
2015-01-30 02:09:44 -06:00
|
|
|
fn visit_lifetime_ref(&mut self, lifetime: &Lifetime) {
|
2014-05-17 18:38:13 -05:00
|
|
|
self.operation.visit_id(lifetime.id);
|
|
|
|
}
|
|
|
|
|
2015-01-30 02:09:44 -06:00
|
|
|
fn visit_lifetime_def(&mut self, def: &LifetimeDef) {
|
2014-05-17 18:38:13 -05:00
|
|
|
self.visit_lifetime_ref(&def.lifetime);
|
|
|
|
}
|
2015-01-30 02:09:44 -06:00
|
|
|
|
|
|
|
fn visit_trait_ref(&mut self, trait_ref: &TraitRef) {
|
|
|
|
self.operation.visit_id(trait_ref.ref_id);
|
|
|
|
visit::walk_trait_ref(self, trait_ref);
|
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn visit_ids_for_inlined_item<O: IdVisitingOperation>(item: &InlinedItem,
|
2014-10-25 14:27:15 -05:00
|
|
|
operation: &mut O) {
|
2013-08-08 07:23:25 -05:00
|
|
|
let mut id_visitor = IdVisitor {
|
2013-08-29 21:01:19 -05:00
|
|
|
operation: operation,
|
2013-08-08 07:23:25 -05:00
|
|
|
pass_through_items: true,
|
2013-07-19 20:42:11 -05:00
|
|
|
visited_outermost: false,
|
|
|
|
};
|
2014-01-06 06:00:46 -06:00
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_inlined_item(&mut id_visitor, item);
|
2012-05-16 15:21:04 -05:00
|
|
|
}
|
|
|
|
|
2013-08-29 21:01:19 -05:00
|
|
|
struct IdRangeComputingVisitor {
|
2014-10-25 14:27:15 -05:00
|
|
|
result: IdRange,
|
2013-08-29 21:01:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IdVisitingOperation for IdRangeComputingVisitor {
|
2014-10-25 14:27:15 -05:00
|
|
|
fn visit_id(&mut self, id: NodeId) {
|
|
|
|
self.result.add(id);
|
2012-05-16 15:21:04 -05:00
|
|
|
}
|
2013-08-29 21:01:19 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn compute_id_range_for_inlined_item(item: &InlinedItem) -> IdRange {
|
2014-10-25 14:27:15 -05:00
|
|
|
let mut visitor = IdRangeComputingVisitor {
|
|
|
|
result: IdRange::max()
|
2013-12-27 20:03:28 -06:00
|
|
|
};
|
2014-10-25 14:27:15 -05:00
|
|
|
visit_ids_for_inlined_item(item, &mut visitor);
|
|
|
|
visitor.result
|
2012-05-16 15:21:04 -05:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Computes the id range for a single fn body, ignoring nested items.
|
2014-09-09 17:54:36 -05:00
|
|
|
pub fn compute_id_range_for_fn_body(fk: visit::FnKind,
|
2014-04-21 18:21:53 -05:00
|
|
|
decl: &FnDecl,
|
|
|
|
body: &Block,
|
|
|
|
sp: Span,
|
|
|
|
id: NodeId)
|
|
|
|
-> IdRange
|
|
|
|
{
|
2014-10-25 14:27:15 -05:00
|
|
|
let mut visitor = IdRangeComputingVisitor {
|
|
|
|
result: IdRange::max()
|
2014-04-21 18:21:53 -05:00
|
|
|
};
|
|
|
|
let mut id_visitor = IdVisitor {
|
2014-10-25 14:27:15 -05:00
|
|
|
operation: &mut visitor,
|
2014-04-21 18:21:53 -05:00
|
|
|
pass_through_items: false,
|
|
|
|
visited_outermost: false,
|
|
|
|
};
|
2014-09-12 05:10:30 -05:00
|
|
|
id_visitor.visit_fn(fk, decl, body, sp, id);
|
2014-10-25 14:27:15 -05:00
|
|
|
id_visitor.operation.result
|
2014-04-21 18:21:53 -05:00
|
|
|
}
|
|
|
|
|
2014-12-30 17:39:20 -06:00
|
|
|
pub fn walk_pat<F>(pat: &Pat, mut it: F) -> bool where F: FnMut(&Pat) -> bool {
|
|
|
|
// FIXME(#19596) this is a workaround, but there should be a better way
|
|
|
|
fn walk_pat_<G>(pat: &Pat, it: &mut G) -> bool where G: FnMut(&Pat) -> bool {
|
|
|
|
if !(*it)(pat) {
|
|
|
|
return false;
|
2012-09-19 18:55:01 -05:00
|
|
|
}
|
2014-12-30 17:39:20 -06:00
|
|
|
|
|
|
|
match pat.node {
|
|
|
|
PatIdent(_, _, Some(ref p)) => walk_pat_(&**p, it),
|
|
|
|
PatStruct(_, ref fields, _) => {
|
|
|
|
fields.iter().all(|field| walk_pat_(&*field.node.pat, it))
|
|
|
|
}
|
|
|
|
PatEnum(_, Some(ref s)) | PatTup(ref s) => {
|
|
|
|
s.iter().all(|p| walk_pat_(&**p, it))
|
|
|
|
}
|
2014-12-05 17:56:25 -06:00
|
|
|
PatBox(ref s) | PatRegion(ref s, _) => {
|
2014-12-30 17:39:20 -06:00
|
|
|
walk_pat_(&**s, it)
|
|
|
|
}
|
|
|
|
PatVec(ref before, ref slice, ref after) => {
|
|
|
|
before.iter().all(|p| walk_pat_(&**p, it)) &&
|
|
|
|
slice.iter().all(|p| walk_pat_(&**p, it)) &&
|
|
|
|
after.iter().all(|p| walk_pat_(&**p, it))
|
|
|
|
}
|
|
|
|
PatMac(_) => panic!("attempted to analyze unexpanded pattern"),
|
|
|
|
PatWild(_) | PatLit(_) | PatRange(_, _) | PatIdent(_, _, _) |
|
|
|
|
PatEnum(_, _) => {
|
|
|
|
true
|
|
|
|
}
|
2013-05-22 05:54:35 -05:00
|
|
|
}
|
2012-05-22 01:17:28 -05:00
|
|
|
}
|
2014-12-30 17:39:20 -06:00
|
|
|
|
|
|
|
walk_pat_(pat, &mut it)
|
2012-05-22 01:17:28 -05:00
|
|
|
}
|
|
|
|
|
2012-10-24 16:36:00 -05:00
|
|
|
/// Returns true if the given struct def is tuple-like; i.e. that its fields
|
|
|
|
/// are unnamed.
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn struct_def_is_tuple_like(struct_def: &ast::StructDef) -> bool {
|
2012-10-24 16:36:00 -05:00
|
|
|
struct_def.ctor_id.is_some()
|
|
|
|
}
|
|
|
|
|
2013-06-06 20:54:14 -05:00
|
|
|
/// Returns true if the given pattern consists solely of an identifier
|
|
|
|
/// and false otherwise.
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn pat_is_ident(pat: P<ast::Pat>) -> bool {
|
2013-06-06 20:54:14 -05:00
|
|
|
match pat.node {
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::PatIdent(..) => true,
|
2013-06-06 20:54:14 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 13:52:59 -05:00
|
|
|
// are two paths equal when compared unhygienically?
|
|
|
|
// since I'm using this to replace ==, it seems appropriate
|
|
|
|
// to compare the span, global, etc. fields as well.
|
|
|
|
pub fn path_name_eq(a : &ast::Path, b : &ast::Path) -> bool {
|
|
|
|
(a.span == b.span)
|
|
|
|
&& (a.global == b.global)
|
2015-02-18 13:48:57 -06:00
|
|
|
&& (segments_name_eq(&a.segments[..], &b.segments[..]))
|
2013-07-10 13:52:59 -05:00
|
|
|
}
|
|
|
|
|
2013-09-05 16:15:00 -05:00
|
|
|
// are two arrays of segments equal when compared unhygienically?
|
|
|
|
pub fn segments_name_eq(a : &[ast::PathSegment], b : &[ast::PathSegment]) -> bool {
|
2015-01-24 12:13:36 -06:00
|
|
|
a.len() == b.len() &&
|
|
|
|
a.iter().zip(b.iter()).all(|(s, t)| {
|
|
|
|
s.identifier.name == t.identifier.name &&
|
|
|
|
// FIXME #7743: ident -> name problems in lifetime comparison?
|
|
|
|
// can types contain idents?
|
|
|
|
s.parameters == t.parameters
|
|
|
|
})
|
2013-07-10 13:52:59 -05:00
|
|
|
}
|
2013-04-03 12:28:14 -05:00
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// Returns true if this literal is a string and false otherwise.
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn lit_is_str(lit: &Lit) -> bool {
|
2014-01-03 17:08:48 -06:00
|
|
|
match lit.node {
|
|
|
|
LitStr(..) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 12:28:14 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use ast::*;
|
|
|
|
use super::*;
|
2014-02-28 14:54:01 -06:00
|
|
|
|
2013-07-10 18:40:09 -05:00
|
|
|
fn ident_to_segment(id : &Ident) -> PathSegment {
|
2014-11-03 20:52:52 -06:00
|
|
|
PathSegment {identifier: id.clone(),
|
|
|
|
parameters: PathParameters::none()}
|
2013-09-05 16:15:00 -05:00
|
|
|
}
|
2013-04-03 12:28:14 -05:00
|
|
|
|
2013-07-10 13:52:59 -05:00
|
|
|
#[test] fn idents_name_eq_test() {
|
2014-03-28 14:42:34 -05:00
|
|
|
assert!(segments_name_eq(
|
2015-01-07 10:58:31 -06:00
|
|
|
&[Ident{name:Name(3),ctxt:4}, Ident{name:Name(78),ctxt:82}]
|
2015-02-25 00:20:34 -06:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>(),
|
2015-01-07 10:58:31 -06:00
|
|
|
&[Ident{name:Name(3),ctxt:104}, Ident{name:Name(78),ctxt:182}]
|
2015-02-25 00:20:34 -06:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>()));
|
2014-03-28 14:42:34 -05:00
|
|
|
assert!(!segments_name_eq(
|
2015-01-07 10:58:31 -06:00
|
|
|
&[Ident{name:Name(3),ctxt:4}, Ident{name:Name(78),ctxt:82}]
|
2015-02-25 00:20:34 -06:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>(),
|
2015-01-07 10:58:31 -06:00
|
|
|
&[Ident{name:Name(3),ctxt:104}, Ident{name:Name(77),ctxt:182}]
|
2015-02-25 00:20:34 -06:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>()));
|
2013-07-10 13:52:59 -05:00
|
|
|
}
|
2013-04-03 12:28:14 -05:00
|
|
|
}
|