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.
|
|
|
|
|
2014-05-29 00:26:56 -05:00
|
|
|
use abi::Abi;
|
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| {
|
2014-05-25 05:17:19 -05:00
|
|
|
token::get_ident(*i).get().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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 05:39:03 -05: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 14:49:41 -06:00
|
|
|
/// Returns `true` if the binary operator takes its arguments by value
|
2014-12-01 10:34:28 -06:00
|
|
|
pub fn is_by_value_binop(b: BinOp) -> bool {
|
|
|
|
match b {
|
|
|
|
BiAdd | BiSub | BiMul | BiDiv | BiRem | BiBitXor | BiBitAnd | BiBitOr | BiShl | BiShr => {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-09-01 20:45:37 -05:00
|
|
|
return 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 {
|
2014-04-21 16:58:52 -05:00
|
|
|
TyI if val.is_some() => "i",
|
2014-04-10 19:21:01 -05:00
|
|
|
TyI => "int",
|
|
|
|
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 {
|
2014-01-09 07:05:33 -06:00
|
|
|
TyI8 => 0x80u64,
|
|
|
|
TyI16 => 0x8000u64,
|
|
|
|
TyI | TyI32 => 0x80000000u64, // actually ni about TyI
|
|
|
|
TyI64 => 0x8000000000000000u64
|
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.
|
|
|
|
/// We want to avoid "42uint" in favor of "42u"
|
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 {
|
2014-04-21 16:58:52 -05:00
|
|
|
TyU if val.is_some() => "u",
|
2014-04-10 19:21:01 -05:00
|
|
|
TyU => "uint",
|
|
|
|
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 {
|
2014-01-09 07:05:33 -06:00
|
|
|
TyU8 => 0xffu64,
|
|
|
|
TyU16 => 0xffffu64,
|
|
|
|
TyU | TyU32 => 0xffffffffu64, // actually ni about TyU
|
|
|
|
TyU64 => 0xffffffffffffffffu64
|
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).
|
|
|
|
pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: &Ty) -> Ident {
|
2014-06-21 05:39:03 -05:00
|
|
|
let mut pretty = pprust::ty_to_string(ty);
|
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('.');
|
2014-06-21 05:39:03 -05:00
|
|
|
pretty.push_str(pprust::path_to_string(&trait_ref.path).as_slice());
|
2014-02-13 23:07:09 -06:00
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
2014-04-02 18:54:22 -05:00
|
|
|
token::gensym_ident(pretty.as_slice())
|
2014-02-13 23:07:09 -06:00
|
|
|
}
|
|
|
|
|
2014-08-05 21:44:21 -05:00
|
|
|
pub fn trait_method_to_ty_method(method: &Method) -> TypeMethod {
|
|
|
|
match method.node {
|
|
|
|
MethDecl(ident,
|
|
|
|
ref generics,
|
|
|
|
abi,
|
|
|
|
ref explicit_self,
|
2014-12-09 09:36:46 -06:00
|
|
|
unsafety,
|
2014-08-05 21:44:21 -05:00
|
|
|
ref decl,
|
|
|
|
_,
|
|
|
|
vis) => {
|
|
|
|
TypeMethod {
|
|
|
|
ident: ident,
|
|
|
|
attrs: method.attrs.clone(),
|
2014-12-09 09:36:46 -06:00
|
|
|
unsafety: unsafety,
|
2014-08-05 21:44:21 -05:00
|
|
|
decl: (*decl).clone(),
|
|
|
|
generics: generics.clone(),
|
|
|
|
explicit_self: (*explicit_self).clone(),
|
|
|
|
id: method.id,
|
|
|
|
span: method.span,
|
|
|
|
vis: vis,
|
|
|
|
abi: abi,
|
|
|
|
}
|
|
|
|
},
|
2014-10-09 14:17:22 -05:00
|
|
|
MethMac(_) => panic!("expected non-macro method declaration")
|
2014-08-05 21:44:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// extract a TypeMethod from a TraitItem. if the TraitItem is
|
|
|
|
/// a default, pull out the useful fields to make a TypeMethod
|
|
|
|
//
|
|
|
|
// NB: to be used only after expansion is complete, and macros are gone.
|
|
|
|
pub fn trait_item_to_ty_method(method: &TraitItem) -> TypeMethod {
|
|
|
|
match *method {
|
|
|
|
RequiredMethod(ref m) => (*m).clone(),
|
|
|
|
ProvidedMethod(ref m) => trait_method_to_ty_method(&**m),
|
|
|
|
TypeTraitItem(_) => {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("trait_method_to_ty_method(): expected method but found \
|
2014-08-05 21:44:21 -05:00
|
|
|
typedef")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn split_trait_methods(trait_methods: &[TraitItem])
|
|
|
|
-> (Vec<TypeMethod>, Vec<P<Method>> ) {
|
|
|
|
let mut reqd = Vec::new();
|
|
|
|
let mut provd = Vec::new();
|
|
|
|
for trt_method in trait_methods.iter() {
|
|
|
|
match *trt_method {
|
|
|
|
RequiredMethod(ref tm) => reqd.push((*tm).clone()),
|
|
|
|
ProvidedMethod(ref m) => provd.push((*m).clone()),
|
|
|
|
TypeTraitItem(_) => {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(reqd, provd)
|
|
|
|
}
|
|
|
|
|
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
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn operator_prec(op: ast::BinOp) -> uint {
|
2012-08-06 14:34:08 -05:00
|
|
|
match op {
|
2013-07-19 20:42:11 -05:00
|
|
|
// 'as' sits here with 12
|
2013-09-01 20:45:37 -05:00
|
|
|
BiMul | BiDiv | BiRem => 11u,
|
|
|
|
BiAdd | BiSub => 10u,
|
|
|
|
BiShl | BiShr => 9u,
|
|
|
|
BiBitAnd => 8u,
|
|
|
|
BiBitXor => 7u,
|
|
|
|
BiBitOr => 6u,
|
|
|
|
BiLt | BiLe | BiGe | BiGt => 4u,
|
|
|
|
BiEq | BiNe => 3u,
|
|
|
|
BiAnd => 2u,
|
|
|
|
BiOr => 1u
|
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.
|
2014-10-27 17:37:07 -05:00
|
|
|
#[allow(non_upper_case_globals)]
|
2013-07-19 20:42:11 -05:00
|
|
|
pub static as_prec: uint = 12u;
|
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
|
|
|
|
|
2014-12-14 22:32:24 -06:00
|
|
|
#[deriving(Copy, Encodable, Decodable, Show)]
|
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) {
|
2013-08-03 11:45:23 -05:00
|
|
|
for type_parameter in generics.ty_params.iter() {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(type_parameter.id)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
2013-08-03 11:45:23 -05:00
|
|
|
for lifetime in generics.lifetimes.iter() {
|
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_view_item(&mut self, view_item: &ViewItem) {
|
2014-04-22 20:09:21 -05:00
|
|
|
if !self.pass_through_items {
|
|
|
|
if self.visited_outermost {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
self.visited_outermost = true;
|
|
|
|
}
|
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
match view_item.node {
|
2014-03-07 01:57:45 -06:00
|
|
|
ViewItemExternCrate(_, _, node_id) => {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(node_id)
|
2012-05-16 15:21:04 -05:00
|
|
|
}
|
2014-04-26 08:33:45 -05:00
|
|
|
ViewItemUse(ref view_path) => {
|
|
|
|
match view_path.node {
|
|
|
|
ViewPathSimple(_, _, node_id) |
|
|
|
|
ViewPathGlob(_, node_id) => {
|
|
|
|
self.operation.visit_id(node_id)
|
|
|
|
}
|
|
|
|
ViewPathList(_, ref paths, node_id) => {
|
|
|
|
self.operation.visit_id(node_id);
|
|
|
|
for path in paths.iter() {
|
2014-07-17 17:56:56 -05:00
|
|
|
self.operation.visit_id(path.node.id())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_view_item(self, view_item);
|
2014-04-22 20:09:21 -05:00
|
|
|
self.visited_outermost = false;
|
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);
|
2014-11-29 15:41:21 -06:00
|
|
|
if let ItemEnum(ref enum_definition, _) = item.node {
|
|
|
|
for variant in enum_definition.variants.iter() {
|
|
|
|
self.operation.visit_id(variant.node.id)
|
2013-06-01 17:31:56 -05: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-11-29 15:41:21 -06:00
|
|
|
if let TyPath(_, id) = typ.node {
|
|
|
|
self.operation.visit_id(id);
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
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 {
|
2014-01-27 06:18:36 -06:00
|
|
|
visit::FkItemFn(_, generics, _, _) |
|
|
|
|
visit::FkMethod(_, generics, _) => {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.visit_generics_helper(generics)
|
2012-05-16 15:21:04 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
visit::FkFnBlock => {}
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for argument in function_declaration.inputs.iter() {
|
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
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_trait_item(&mut self, tm: &ast::TraitItem) {
|
2013-10-02 13:29:29 -05:00
|
|
|
match *tm {
|
2014-08-04 15:56:56 -05:00
|
|
|
ast::RequiredMethod(ref m) => self.operation.visit_id(m.id),
|
|
|
|
ast::ProvidedMethod(ref m) => self.operation.visit_id(m.id),
|
2014-10-28 13:48:52 -05:00
|
|
|
ast::TypeTraitItem(ref typ) => self.operation.visit_id(typ.ty_param.id),
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_trait_item(self, tm);
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
2014-05-17 18:38:13 -05:00
|
|
|
|
|
|
|
fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
|
|
|
|
self.operation.visit_id(lifetime.id);
|
|
|
|
}
|
|
|
|
|
2014-11-18 10:39:16 -06:00
|
|
|
fn visit_lifetime_def(&mut self, def: &'v LifetimeDef) {
|
2014-05-17 18:38:13 -05:00
|
|
|
self.visit_lifetime_ref(&def.lifetime);
|
|
|
|
}
|
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-08 12:28:32 -06:00
|
|
|
// FIXME(#19596) unbox `it`
|
2013-11-25 08:37:03 -06:00
|
|
|
pub fn walk_pat(pat: &Pat, it: |&Pat| -> bool) -> bool {
|
2013-05-22 05:54:35 -05:00
|
|
|
if !it(pat) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match pat.node {
|
2014-05-16 02:16:13 -05:00
|
|
|
PatIdent(_, _, Some(ref p)) => walk_pat(&**p, it),
|
2013-09-01 20:45:37 -05:00
|
|
|
PatStruct(_, ref fields, _) => {
|
2014-10-05 19:36:53 -05:00
|
|
|
fields.iter().all(|field| walk_pat(&*field.node.pat, |p| it(p)))
|
2012-09-19 18:55:01 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
PatEnum(_, Some(ref s)) | PatTup(ref s) => {
|
2014-06-12 05:13:25 -05:00
|
|
|
s.iter().all(|p| walk_pat(&**p, |p| it(p)))
|
2012-09-19 18:55:01 -05:00
|
|
|
}
|
2014-05-16 02:16:13 -05:00
|
|
|
PatBox(ref s) | PatRegion(ref s) => {
|
|
|
|
walk_pat(&**s, it)
|
2012-09-19 18:55:01 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
PatVec(ref before, ref slice, ref after) => {
|
2014-06-12 05:13:25 -05:00
|
|
|
before.iter().all(|p| walk_pat(&**p, |p| it(p))) &&
|
|
|
|
slice.iter().all(|p| walk_pat(&**p, |p| it(p))) &&
|
|
|
|
after.iter().all(|p| walk_pat(&**p, |p| it(p)))
|
2012-12-08 14:22:43 -06:00
|
|
|
}
|
2014-10-09 14:17:22 -05:00
|
|
|
PatMac(_) => panic!("attempted to analyze unexpanded pattern"),
|
2014-08-06 10:04:44 -05:00
|
|
|
PatWild(_) | PatLit(_) | PatRange(_, _) | PatIdent(_, _, _) |
|
2013-09-01 20:45:37 -05:00
|
|
|
PatEnum(_, _) => {
|
2013-05-22 05:54:35 -05:00
|
|
|
true
|
|
|
|
}
|
2012-05-22 01:17:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 19:03:04 -05:00
|
|
|
pub trait EachViewItem {
|
2014-12-08 12:28:32 -06:00
|
|
|
fn each_view_item<F>(&self, f: F) -> bool where F: FnMut(&ast::ViewItem) -> bool;
|
2013-05-27 19:03:04 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
struct EachViewItemData<F> where F: FnMut(&ast::ViewItem) -> bool {
|
|
|
|
callback: F,
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
impl<'v, F> Visitor<'v> for EachViewItemData<F> where F: FnMut(&ast::ViewItem) -> bool {
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_view_item(&mut self, view_item: &ast::ViewItem) {
|
2013-07-19 20:42:11 -05:00
|
|
|
let _ = (self.callback)(view_item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 00:38:55 -05:00
|
|
|
impl EachViewItem for ast::Crate {
|
2014-12-08 12:28:32 -06:00
|
|
|
fn each_view_item<F>(&self, f: F) -> bool where F: FnMut(&ast::ViewItem) -> bool {
|
2013-09-06 22:29:16 -05:00
|
|
|
let mut visit = EachViewItemData {
|
2013-07-19 20:42:11 -05:00
|
|
|
callback: f,
|
|
|
|
};
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_crate(&mut visit, self);
|
2013-05-27 19:03:04 -05:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn view_path_id(p: &ViewPath) -> NodeId {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ViewPathSimple(_, _, id) | ViewPathGlob(_, id)
|
|
|
|
| ViewPathList(_, _, id) => id
|
2012-05-22 12:54:12 -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)
|
2014-02-28 14:54:01 -06:00
|
|
|
&& (segments_name_eq(a.segments.as_slice(), b.segments.as_slice()))
|
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 {
|
2014-01-19 02:21:14 -06:00
|
|
|
if a.len() != b.len() {
|
2013-07-10 13:52:59 -05:00
|
|
|
false
|
|
|
|
} else {
|
2013-09-05 16:15:00 -05:00
|
|
|
for (idx,seg) in a.iter().enumerate() {
|
2014-11-03 20:52:52 -06:00
|
|
|
if seg.identifier.name != b[idx].identifier.name
|
2013-07-12 00:58:14 -05:00
|
|
|
// FIXME #7743: ident -> name problems in lifetime comparison?
|
|
|
|
// can types contain idents?
|
2014-11-03 20:52:52 -06:00
|
|
|
|| seg.parameters != b[idx].parameters
|
|
|
|
{
|
2013-07-10 13:52:59 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 23:22:11 -05:00
|
|
|
/// Macro invocations are guaranteed not to occur after expansion is complete.
|
2014-07-14 18:27:54 -05:00
|
|
|
/// Extracting fields of a method requires a dynamic check to make sure that it's
|
|
|
|
/// not a macro invocation. This check is guaranteed to succeed, assuming
|
2014-07-11 23:22:11 -05:00
|
|
|
/// that the invocations are indeed gone.
|
2014-07-14 18:27:54 -05:00
|
|
|
pub trait PostExpansionMethod {
|
|
|
|
fn pe_ident(&self) -> ast::Ident;
|
|
|
|
fn pe_generics<'a>(&'a self) -> &'a ast::Generics;
|
2014-05-29 00:26:56 -05:00
|
|
|
fn pe_abi(&self) -> Abi;
|
2014-07-14 18:27:54 -05:00
|
|
|
fn pe_explicit_self<'a>(&'a self) -> &'a ast::ExplicitSelf;
|
2014-12-09 09:36:46 -06:00
|
|
|
fn pe_unsafety(&self) -> ast::Unsafety;
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pe_fn_decl<'a>(&'a self) -> &'a ast::FnDecl;
|
|
|
|
fn pe_body<'a>(&'a self) -> &'a ast::Block;
|
2014-07-14 18:27:54 -05:00
|
|
|
fn pe_vis(&self) -> ast::Visibility;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! mf_method{
|
2014-09-13 11:06:01 -05:00
|
|
|
($meth_name:ident, $field_ty:ty, $field_pat:pat, $result:expr) => {
|
2014-07-14 18:27:54 -05:00
|
|
|
fn $meth_name<'a>(&'a self) -> $field_ty {
|
|
|
|
match self.node {
|
2014-07-11 23:22:11 -05:00
|
|
|
$field_pat => $result,
|
|
|
|
MethMac(_) => {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("expected an AST without macro invocations");
|
2014-07-11 23:22:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 12:28:42 -05:00
|
|
|
|
2014-07-14 18:27:54 -05:00
|
|
|
impl PostExpansionMethod for Method {
|
2014-11-14 11:18:10 -06:00
|
|
|
mf_method! { pe_ident,ast::Ident,MethDecl(ident,_,_,_,_,_,_,_),ident }
|
|
|
|
mf_method! {
|
|
|
|
pe_generics,&'a ast::Generics,
|
|
|
|
MethDecl(_,ref generics,_,_,_,_,_,_),generics
|
|
|
|
}
|
|
|
|
mf_method! { pe_abi,Abi,MethDecl(_,_,abi,_,_,_,_,_),abi }
|
|
|
|
mf_method! {
|
|
|
|
pe_explicit_self,&'a ast::ExplicitSelf,
|
|
|
|
MethDecl(_,_,_,ref explicit_self,_,_,_,_),explicit_self
|
|
|
|
}
|
|
|
|
mf_method! { pe_unsafety,ast::Unsafety,MethDecl(_,_,_,_,unsafety,_,_,_),unsafety }
|
|
|
|
mf_method! { pe_fn_decl,&'a ast::FnDecl,MethDecl(_,_,_,_,_,ref decl,_,_),&**decl }
|
|
|
|
mf_method! { pe_body,&'a ast::Block,MethDecl(_,_,_,_,_,_,ref body,_),&**body }
|
|
|
|
mf_method! { pe_vis,ast::Visibility,MethDecl(_,_,_,_,_,_,_,vis),vis }
|
2014-07-14 18:27:54 -05:00
|
|
|
}
|
2014-07-11 23:22:11 -05:00
|
|
|
|
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(
|
2014-07-09 00:28:52 -05:00
|
|
|
[Ident{name:Name(3),ctxt:4}, Ident{name:Name(78),ctxt:82}]
|
2014-03-28 14:42:34 -05:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice(),
|
2014-07-09 00:28:52 -05:00
|
|
|
[Ident{name:Name(3),ctxt:104}, Ident{name:Name(78),ctxt:182}]
|
2014-03-28 14:42:34 -05:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice()));
|
|
|
|
assert!(!segments_name_eq(
|
2014-07-09 00:28:52 -05:00
|
|
|
[Ident{name:Name(3),ctxt:4}, Ident{name:Name(78),ctxt:82}]
|
2014-03-28 14:42:34 -05:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice(),
|
2014-07-09 00:28:52 -05:00
|
|
|
[Ident{name:Name(3),ctxt:104}, Ident{name:Name(77),ctxt:182}]
|
2014-03-28 14:42:34 -05:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice()));
|
2013-07-10 13:52:59 -05:00
|
|
|
}
|
2013-04-03 12:28:14 -05:00
|
|
|
}
|