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-06-12 00:31:02 -05:00
|
|
|
use attr::{InlineNever, InlineNone};
|
|
|
|
use attr;
|
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;
|
2013-09-14 00:10:48 -05:00
|
|
|
use visit::Visitor;
|
2012-12-23 16:41:37 -06:00
|
|
|
use visit;
|
|
|
|
|
2014-02-24 14:47:19 -06:00
|
|
|
use std::cell::Cell;
|
2014-02-06 01:34:33 -06:00
|
|
|
use std::cmp;
|
2014-06-11 21:33:52 -05:00
|
|
|
use std::gc::{Gc, GC};
|
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,
|
2013-11-28 14:22:53 -06:00
|
|
|
StmtMac(..) => fail!("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-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-16 02:16:13 -05:00
|
|
|
UnBox => "box(GC) ",
|
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-05-16 02:16:13 -05:00
|
|
|
pub fn is_path(e: Gc<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-05-16 02:16:13 -05:00
|
|
|
pub fn is_call_expr(e: Gc<Expr>) -> bool {
|
2013-11-28 14:22:53 -06:00
|
|
|
match e.node { ExprCall(..) => true, _ => false }
|
2011-08-21 23:44:41 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 02:16:13 -05:00
|
|
|
pub fn block_from_expr(e: Gc<Expr>) -> P<Block> {
|
2013-11-30 16:00:39 -06:00
|
|
|
P(Block {
|
2014-02-28 15:09:09 -06:00
|
|
|
view_items: Vec::new(),
|
|
|
|
stmts: Vec::new(),
|
2013-11-30 16:00:39 -06:00
|
|
|
expr: Some(e),
|
|
|
|
id: e.id,
|
2013-07-27 03:25:59 -05:00
|
|
|
rules: DefaultBlock,
|
2013-11-30 16:00:39 -06:00
|
|
|
span: e.span
|
|
|
|
})
|
2011-08-25 19:42:38 -05: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-03-07 09:50:40 -06:00
|
|
|
lifetimes: Vec::new(),
|
2014-03-19 09:52:37 -05:00
|
|
|
types: OwnedSlice::empty(),
|
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-05-16 02:16:13 -05:00
|
|
|
pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> Gc<Pat> {
|
|
|
|
box(GC) ast::Pat { id: id,
|
2014-06-30 20:02:14 -05:00
|
|
|
node: PatIdent(BindByValue(MutImmutable), codemap::Spanned{span:s, node:i}, None),
|
2013-01-14 22:52:28 -06:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn is_unguarded(a: &Arm) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match a.guard {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => true,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 02:16:13 -05:00
|
|
|
pub fn unguarded_pat(a: &Arm) -> Option<Vec<Gc<Pat>>> {
|
2013-07-02 14:47:32 -05:00
|
|
|
if is_unguarded(a) {
|
|
|
|
Some(/* FIXME (#2543) */ a.pats.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
|
|
|
|
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) => {
|
|
|
|
pretty.push_char('.');
|
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-05-16 02:16:13 -05:00
|
|
|
pub fn public_methods(ms: Vec<Gc<Method>> ) -> Vec<Gc<Method>> {
|
2013-11-20 18:23:04 -06:00
|
|
|
ms.move_iter().filter(|m| {
|
2013-01-07 23:15:25 -06:00
|
|
|
match m.vis {
|
2014-01-09 07:05:33 -06:00
|
|
|
Public => true,
|
2013-01-07 23:15:25 -06:00
|
|
|
_ => false
|
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
}).collect()
|
2012-03-26 11:59:59 -05:00
|
|
|
}
|
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// extract a TypeMethod from a TraitMethod. if the TraitMethod is
|
|
|
|
/// a default, pull out the useful fields to make a TypeMethod
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn trait_method_to_ty_method(method: &TraitMethod) -> TypeMethod {
|
2013-02-18 00:20:36 -06:00
|
|
|
match *method {
|
2014-01-09 07:05:33 -06:00
|
|
|
Required(ref m) => (*m).clone(),
|
|
|
|
Provided(ref m) => {
|
2013-07-27 03:25:59 -05:00
|
|
|
TypeMethod {
|
2013-01-15 17:03:49 -06:00
|
|
|
ident: m.ident,
|
2013-07-02 14:47:32 -05:00
|
|
|
attrs: m.attrs.clone(),
|
2014-04-06 20:04:40 -05:00
|
|
|
fn_style: m.fn_style,
|
2013-11-30 16:00:39 -06:00
|
|
|
decl: m.decl,
|
2013-07-02 14:47:32 -05:00
|
|
|
generics: m.generics.clone(),
|
2013-04-30 10:49:48 -05:00
|
|
|
explicit_self: m.explicit_self,
|
2013-01-15 17:03:49 -06:00
|
|
|
id: m.id,
|
|
|
|
span: m.span,
|
2014-05-22 12:49:26 -05:00
|
|
|
vis: m.vis,
|
2013-01-15 17:03:49 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-02 17:52:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn split_trait_methods(trait_methods: &[TraitMethod])
|
2014-05-16 02:16:13 -05:00
|
|
|
-> (Vec<TypeMethod> , Vec<Gc<Method>> ) {
|
2014-02-28 15:09:09 -06:00
|
|
|
let mut reqd = Vec::new();
|
|
|
|
let mut provd = Vec::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for trt_method in trait_methods.iter() {
|
2012-09-19 18:55:01 -05:00
|
|
|
match *trt_method {
|
2014-01-09 07:05:33 -06:00
|
|
|
Required(ref tm) => reqd.push((*tm).clone()),
|
|
|
|
Provided(m) => provd.push(m)
|
2012-08-02 17:52:30 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
(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.
|
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-03-07 09:50:40 -06:00
|
|
|
Generics {lifetimes: Vec::new(),
|
2014-03-19 09:52:37 -05:00
|
|
|
ty_params: OwnedSlice::empty()}
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
|
2012-05-16 15:21:04 -05:00
|
|
|
// ______________________________________________________________________
|
|
|
|
// Enumerating the IDs which appear in an AST
|
|
|
|
|
2013-05-15 17:55:57 -05:00
|
|
|
#[deriving(Encodable, Decodable)]
|
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 {
|
|
|
|
fn visit_id(&self, node_id: NodeId);
|
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
pub struct IdVisitor<'a, O> {
|
2014-03-27 17:39:48 -05:00
|
|
|
pub operation: &'a O,
|
|
|
|
pub pass_through_items: bool,
|
|
|
|
pub visited_outermost: bool,
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a, O: IdVisitingOperation> IdVisitor<'a, O> {
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_generics_helper(&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() {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(lifetime.id)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a, O: IdVisitingOperation> Visitor<()> 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,
|
2013-07-19 20:42:11 -05:00
|
|
|
node_id: NodeId,
|
|
|
|
env: ()) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(node_id);
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_mod(self, module, env)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn visit_view_item(&mut self, view_item: &ViewItem, env: ()) {
|
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() {
|
|
|
|
self.operation.visit_id(path.node.id)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 20:09:21 -05:00
|
|
|
visit::walk_view_item(self, view_item, env);
|
|
|
|
self.visited_outermost = false;
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &ForeignItem, env: ()) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(foreign_item.id);
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_foreign_item(self, foreign_item, env)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn visit_item(&mut self, item: &Item, env: ()) {
|
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);
|
2013-07-19 20:42:11 -05:00
|
|
|
match item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemEnum(ref enum_definition, _) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for variant in enum_definition.variants.iter() {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(variant.node.id)
|
2013-06-10 16:50:12 -05:00
|
|
|
}
|
2013-06-01 17:31:56 -05:00
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_item(self, item, env);
|
2013-07-19 20:42:11 -05:00
|
|
|
|
|
|
|
self.visited_outermost = false
|
|
|
|
}
|
|
|
|
|
2014-01-06 06:00:46 -06:00
|
|
|
fn visit_local(&mut self, local: &Local, env: ()) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(local.id);
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_local(self, local, env)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-01-06 06:00:46 -06:00
|
|
|
fn visit_block(&mut self, block: &Block, env: ()) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(block.id);
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_block(self, block, env)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-01-06 06:00:46 -06:00
|
|
|
fn visit_stmt(&mut self, statement: &Stmt, env: ()) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(ast_util::stmt_id(statement));
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_stmt(self, statement, env)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-11-25 12:22:21 -06:00
|
|
|
fn visit_pat(&mut self, pattern: &Pat, env: ()) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(pattern.id);
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_pat(self, pattern, env)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-01-06 06:00:46 -06:00
|
|
|
fn visit_expr(&mut self, expression: &Expr, env: ()) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(expression.id);
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_expr(self, expression, env)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
2012-05-16 15:21:04 -05:00
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_ty(&mut self, typ: &Ty, env: ()) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(typ.id);
|
2013-07-19 20:42:11 -05:00
|
|
|
match typ.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
TyPath(_, _, id) => self.operation.visit_id(id),
|
2013-07-19 20:42:11 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_ty(self, typ, env)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
2012-05-16 15:21:04 -05:00
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_generics(&mut self, generics: &Generics, env: ()) {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.visit_generics_helper(generics);
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_generics(self, generics, env)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_fn(&mut self,
|
2014-01-09 07:05:33 -06:00
|
|
|
function_kind: &visit::FnKind,
|
|
|
|
function_declaration: &FnDecl,
|
2014-01-06 06:00:46 -06:00
|
|
|
block: &Block,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-07-19 20:42:11 -05:00
|
|
|
node_id: NodeId,
|
|
|
|
env: ()) {
|
|
|
|
if !self.pass_through_items {
|
|
|
|
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
|
|
|
|
2013-07-19 20:42:11 -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,
|
2013-07-19 20:42:11 -05:00
|
|
|
function_kind,
|
|
|
|
function_declaration,
|
|
|
|
block,
|
|
|
|
span,
|
|
|
|
env);
|
|
|
|
|
|
|
|
if !self.pass_through_items {
|
|
|
|
match *function_kind {
|
2014-01-09 07:05:33 -06:00
|
|
|
visit::FkMethod(..) => self.visited_outermost = false,
|
2013-07-19 20:42:11 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn visit_struct_field(&mut self, struct_field: &StructField, env: ()) {
|
2013-08-29 21:01:19 -05:00
|
|
|
self.operation.visit_id(struct_field.node.id);
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_struct_field(self, struct_field, env)
|
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,
|
2013-09-26 23:53:40 -05:00
|
|
|
id: NodeId,
|
|
|
|
_: ()) {
|
|
|
|
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-05-14 01:20:25 -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-01-09 07:05:33 -06:00
|
|
|
fn visit_trait_method(&mut self, tm: &ast::TraitMethod, _: ()) {
|
2013-10-02 13:29:29 -05:00
|
|
|
match *tm {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::Required(ref m) => self.operation.visit_id(m.id),
|
|
|
|
ast::Provided(ref m) => self.operation.visit_id(m.id),
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
|
|
|
visit::walk_trait_method(self, tm, ());
|
|
|
|
}
|
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,
|
2013-10-02 13:29:29 -05:00
|
|
|
operation: &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-01-15 13:39:08 -06: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-01-09 07:05:33 -06:00
|
|
|
result: Cell<IdRange>,
|
2013-08-29 21:01:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IdVisitingOperation for IdRangeComputingVisitor {
|
|
|
|
fn visit_id(&self, id: NodeId) {
|
2013-12-27 20:03:28 -06:00
|
|
|
let mut id_range = self.result.get();
|
|
|
|
id_range.add(id);
|
|
|
|
self.result.set(id_range)
|
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 {
|
2013-12-27 20:03:28 -06:00
|
|
|
let visitor = IdRangeComputingVisitor {
|
2014-01-09 07:05:33 -06:00
|
|
|
result: Cell::new(IdRange::max())
|
2013-12-27 20:03:28 -06:00
|
|
|
};
|
|
|
|
visit_ids_for_inlined_item(item, &visitor);
|
|
|
|
visitor.result.get()
|
2012-05-16 15:21:04 -05:00
|
|
|
}
|
|
|
|
|
2014-04-21 18:21:53 -05:00
|
|
|
pub fn compute_id_range_for_fn_body(fk: &visit::FnKind,
|
|
|
|
decl: &FnDecl,
|
|
|
|
body: &Block,
|
|
|
|
sp: Span,
|
|
|
|
id: NodeId)
|
|
|
|
-> IdRange
|
|
|
|
{
|
|
|
|
/*!
|
|
|
|
* Computes the id range for a single fn body,
|
|
|
|
* ignoring nested items.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let visitor = IdRangeComputingVisitor {
|
|
|
|
result: Cell::new(IdRange::max())
|
|
|
|
};
|
|
|
|
let mut id_visitor = IdVisitor {
|
|
|
|
operation: &visitor,
|
|
|
|
pass_through_items: false,
|
|
|
|
visited_outermost: false,
|
|
|
|
};
|
|
|
|
id_visitor.visit_fn(fk, decl, body, sp, id, ());
|
|
|
|
visitor.result.get()
|
|
|
|
}
|
|
|
|
|
2014-05-16 02:16:13 -05:00
|
|
|
pub fn is_item_impl(item: Gc<ast::Item>) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemImpl(..) => true,
|
|
|
|
_ => false
|
2012-05-17 22:37:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-16 02:16:13 -05:00
|
|
|
fields.iter().advance(|f| walk_pat(&*f.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-05-16 02:16:13 -05:00
|
|
|
s.iter().advance(|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-05-16 02:16:13 -05:00
|
|
|
before.iter().advance(|p| walk_pat(&**p, |p| it(p))) &&
|
|
|
|
slice.iter().advance(|p| walk_pat(&**p, |p| it(p))) &&
|
|
|
|
after.iter().advance(|p| walk_pat(&**p, |p| it(p)))
|
2012-12-08 14:22:43 -06:00
|
|
|
}
|
2014-05-19 15:29:41 -05:00
|
|
|
PatMac(_) => fail!("attempted to analyze unexpanded pattern"),
|
2013-11-07 21:25:39 -06:00
|
|
|
PatWild | PatWildMulti | 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-01-09 07:05:33 -06:00
|
|
|
fn each_view_item(&self, f: |&ast::ViewItem| -> bool) -> bool;
|
2013-05-27 19:03:04 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
struct EachViewItemData<'a> {
|
2014-04-07 15:30:48 -05:00
|
|
|
callback: |&ast::ViewItem|: 'a -> bool,
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Visitor<()> for EachViewItemData<'a> {
|
2014-01-09 07:05:33 -06: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-01-09 07:05:33 -06:00
|
|
|
fn each_view_item(&self, f: |&ast::ViewItem| -> bool) -> bool {
|
2013-09-06 22:29:16 -05:00
|
|
|
let mut visit = EachViewItemData {
|
2013-07-19 20:42:11 -05:00
|
|
|
callback: f,
|
|
|
|
};
|
2013-09-06 22:29:16 -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-05-16 02:16:13 -05:00
|
|
|
pub fn pat_is_ident(pat: Gc<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() {
|
|
|
|
if (seg.identifier.name != b[idx].identifier.name)
|
2013-07-12 00:58:14 -05:00
|
|
|
// FIXME #7743: ident -> name problems in lifetime comparison?
|
2013-10-29 05:03:32 -05:00
|
|
|
|| (seg.lifetimes != b[idx].lifetimes)
|
2013-07-12 00:58:14 -05:00
|
|
|
// can types contain idents?
|
2013-09-05 16:15:00 -05:00
|
|
|
|| (seg.types != b[idx].types) {
|
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-05-16 02:16:13 -05:00
|
|
|
pub fn lit_is_str(lit: Gc<Lit>) -> bool {
|
2014-01-03 17:08:48 -06:00
|
|
|
match lit.node {
|
|
|
|
LitStr(..) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 00:26:51 -06:00
|
|
|
pub fn get_inner_tys(ty: P<Ty>) -> Vec<P<Ty>> {
|
|
|
|
match ty.node {
|
|
|
|
ast::TyRptr(_, mut_ty) | ast::TyPtr(mut_ty) => {
|
|
|
|
vec!(mut_ty.ty)
|
|
|
|
}
|
|
|
|
ast::TyBox(ty)
|
|
|
|
| ast::TyVec(ty)
|
|
|
|
| ast::TyUniq(ty)
|
|
|
|
| ast::TyFixedLengthVec(ty, _) => vec!(ty),
|
|
|
|
ast::TyTup(ref tys) => tys.clone(),
|
2014-06-11 14:14:38 -05:00
|
|
|
ast::TyParen(ty) => get_inner_tys(ty),
|
2014-02-15 00:26:51 -06:00
|
|
|
_ => Vec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:31:02 -05:00
|
|
|
/// Returns true if the static with the given mutability and attributes
|
|
|
|
/// has a significant address and false otherwise.
|
|
|
|
pub fn static_has_significant_address(mutbl: ast::Mutability,
|
|
|
|
attrs: &[ast::Attribute])
|
|
|
|
-> bool {
|
|
|
|
if mutbl == ast::MutMutable {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
let inline = attr::find_inline_attr(attrs);
|
|
|
|
inline == InlineNever || inline == InlineNone
|
|
|
|
}
|
2014-01-03 17:08:48 -06:00
|
|
|
|
2013-04-03 12:28:14 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use ast::*;
|
|
|
|
use super::*;
|
2014-03-19 09:52:37 -05:00
|
|
|
use owned_slice::OwnedSlice;
|
2014-02-28 14:54:01 -06:00
|
|
|
|
2013-07-10 18:40:09 -05:00
|
|
|
fn ident_to_segment(id : &Ident) -> PathSegment {
|
2013-10-29 05:03:32 -05:00
|
|
|
PathSegment {identifier:id.clone(),
|
2014-03-07 09:50:40 -06:00
|
|
|
lifetimes: Vec::new(),
|
2014-03-19 09:52:37 -05:00
|
|
|
types: OwnedSlice::empty()}
|
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
|
|
|
}
|