2014-04-22 20:09:21 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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-28 22:26:56 -07:00
|
|
|
use abi::Abi;
|
2012-09-04 11:37:29 -07:00
|
|
|
use ast::*;
|
2012-12-23 17:41:37 -05:00
|
|
|
use ast;
|
|
|
|
use ast_util;
|
2014-02-15 01:26:51 -05:00
|
|
|
use codemap;
|
2013-12-01 00:00:39 +02:00
|
|
|
use codemap::Span;
|
2014-03-20 01:52:37 +11:00
|
|
|
use owned_slice::OwnedSlice;
|
2013-01-08 19:37:25 -08:00
|
|
|
use parse::token;
|
2014-02-14 07:07:09 +02:00
|
|
|
use print::pprust;
|
2014-09-13 19:06:01 +03:00
|
|
|
use ptr::P;
|
2013-09-14 01:10:48 -04:00
|
|
|
use visit::Visitor;
|
2012-12-23 17:41:37 -05:00
|
|
|
use visit;
|
|
|
|
|
2014-02-06 02:34:33 -05:00
|
|
|
use std::cmp;
|
2013-11-27 07:02:25 +02:00
|
|
|
use std::u32;
|
2013-04-03 10:28:14 -07:00
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn path_name_i(idents: &[Ident]) -> String {
|
2012-06-14 18:46:33 -07:00
|
|
|
// FIXME: Bad copies (#2543 -- same for everything else that says "bad")
|
2014-03-28 20:42:34 +01:00
|
|
|
idents.iter().map(|i| {
|
2014-05-25 03:17:19 -07:00
|
|
|
token::get_ident(*i).get().to_string()
|
2014-06-26 08:15:14 +02:00
|
|
|
}).collect::<Vec<String>>().connect("::")
|
2012-06-10 00:49:59 -07:00
|
|
|
}
|
2011-08-21 21:44:41 -07:00
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
pub fn local_def(id: NodeId) -> DefId {
|
2014-02-05 22:15:24 +01:00
|
|
|
ast::DefId { krate: LOCAL_CRATE, node: id }
|
2013-01-13 11:05:40 -08:00
|
|
|
}
|
2012-03-21 12:42:34 -07:00
|
|
|
|
2014-02-05 22:15:24 +01:00
|
|
|
pub fn is_local(did: ast::DefId) -> bool { did.krate == LOCAL_CRATE }
|
2011-08-21 21:44:41 -07:00
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
pub fn stmt_id(s: &Stmt) -> NodeId {
|
2012-08-06 12:34:08 -07:00
|
|
|
match s.node {
|
2013-09-02 03:45:37 +02:00
|
|
|
StmtDecl(_, id) => id,
|
|
|
|
StmtExpr(_, id) => id,
|
|
|
|
StmtSemi(_, id) => id,
|
2014-10-09 15:17:22 -04:00
|
|
|
StmtMac(..) => panic!("attempted to analyze unexpanded stmt")
|
2012-02-14 15:21:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 03:39:03 -07:00
|
|
|
pub fn binop_to_string(op: BinOp) -> &'static str {
|
2012-08-06 12:34:08 -07:00
|
|
|
match op {
|
2014-02-24 10:33:50 +02:00
|
|
|
BiAdd => "+",
|
|
|
|
BiSub => "-",
|
|
|
|
BiMul => "*",
|
|
|
|
BiDiv => "/",
|
|
|
|
BiRem => "%",
|
|
|
|
BiAnd => "&&",
|
|
|
|
BiOr => "||",
|
|
|
|
BiBitXor => "^",
|
|
|
|
BiBitAnd => "&",
|
|
|
|
BiBitOr => "|",
|
|
|
|
BiShl => "<<",
|
|
|
|
BiShr => ">>",
|
|
|
|
BiEq => "==",
|
|
|
|
BiLt => "<",
|
|
|
|
BiLe => "<=",
|
|
|
|
BiNe => "!=",
|
|
|
|
BiGe => ">=",
|
|
|
|
BiGt => ">"
|
2013-09-02 03:45:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lazy_binop(b: BinOp) -> bool {
|
2012-08-06 12:34:08 -07:00
|
|
|
match b {
|
2013-09-02 03:45:37 +02:00
|
|
|
BiAnd => true,
|
|
|
|
BiOr => true,
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => false
|
|
|
|
}
|
2011-08-21 21:44:41 -07:00
|
|
|
}
|
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
pub fn is_shift_binop(b: BinOp) -> bool {
|
2012-08-06 12:34:08 -07:00
|
|
|
match b {
|
2013-09-02 03:45:37 +02:00
|
|
|
BiShl => true,
|
|
|
|
BiShr => true,
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => false
|
2012-02-21 21:01:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 03:39:03 -07:00
|
|
|
pub fn unop_to_string(op: UnOp) -> &'static str {
|
2012-08-06 12:34:08 -07:00
|
|
|
match op {
|
2014-05-05 18:56:44 -07:00
|
|
|
UnUniq => "box() ",
|
2013-12-31 12:55:39 -08:00
|
|
|
UnDeref => "*",
|
|
|
|
UnNot => "!",
|
|
|
|
UnNeg => "-",
|
2011-08-21 21:44:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn is_path(e: P<Expr>) -> bool {
|
2013-09-02 03:45:37 +02:00
|
|
|
return match e.node { ExprPath(_) => true, _ => false };
|
2011-08-21 21:44:41 -07:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:12:30 -07: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 03:39:03 -07:00
|
|
|
pub fn int_ty_to_string(t: IntTy, val: Option<i64>) -> String {
|
2014-04-11 01:21:01 +01:00
|
|
|
let s = match t {
|
2014-04-21 17:58:52 -04:00
|
|
|
TyI if val.is_some() => "i",
|
2014-04-11 01:21:01 +01:00
|
|
|
TyI => "int",
|
|
|
|
TyI8 => "i8",
|
|
|
|
TyI16 => "i16",
|
|
|
|
TyI32 => "i32",
|
|
|
|
TyI64 => "i64"
|
|
|
|
};
|
|
|
|
|
|
|
|
match val {
|
2014-05-10 21:57:49 -07: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 08:15:14 +02:00
|
|
|
Some(n) => format!("{}{}", n as u64, s),
|
2014-05-25 03:17:19 -07:00
|
|
|
None => s.to_string()
|
2011-08-21 21:44:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn int_ty_max(t: IntTy) -> u64 {
|
2012-08-06 12:34:08 -07:00
|
|
|
match t {
|
2014-01-09 15:05:33 +02:00
|
|
|
TyI8 => 0x80u64,
|
|
|
|
TyI16 => 0x8000u64,
|
|
|
|
TyI | TyI32 => 0x80000000u64, // actually ni about TyI
|
|
|
|
TyI64 => 0x8000000000000000u64
|
2011-12-07 21:53:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 13:12:30 -07: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 03:39:03 -07:00
|
|
|
pub fn uint_ty_to_string(t: UintTy, val: Option<u64>) -> String {
|
2014-04-11 01:21:01 +01:00
|
|
|
let s = match t {
|
2014-04-21 17:58:52 -04:00
|
|
|
TyU if val.is_some() => "u",
|
2014-04-11 01:21:01 +01:00
|
|
|
TyU => "uint",
|
|
|
|
TyU8 => "u8",
|
|
|
|
TyU16 => "u16",
|
|
|
|
TyU32 => "u32",
|
|
|
|
TyU64 => "u64"
|
|
|
|
};
|
|
|
|
|
|
|
|
match val {
|
2014-06-26 08:15:14 +02:00
|
|
|
Some(n) => format!("{}{}", n, s),
|
2014-05-25 03:17:19 -07:00
|
|
|
None => s.to_string()
|
2011-12-07 21:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn uint_ty_max(t: UintTy) -> u64 {
|
2012-08-06 12:34:08 -07:00
|
|
|
match t {
|
2014-01-09 15:05:33 +02:00
|
|
|
TyU8 => 0xffu64,
|
|
|
|
TyU16 => 0xffffu64,
|
|
|
|
TyU | TyU32 => 0xffffffffu64, // actually ni about TyU
|
|
|
|
TyU64 => 0xffffffffffffffffu64
|
2011-12-07 21:53:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 03:39:03 -07:00
|
|
|
pub fn float_ty_to_string(t: FloatTy) -> String {
|
2014-05-07 16:33:43 -07:00
|
|
|
match t {
|
2014-05-25 03:17:19 -07:00
|
|
|
TyF32 => "f32".to_string(),
|
|
|
|
TyF64 => "f64".to_string(),
|
2014-05-07 16:33:43 -07:00
|
|
|
}
|
2011-12-07 21:06:12 +01:00
|
|
|
}
|
2011-08-21 21:44:41 -07:00
|
|
|
|
2014-06-30 18:02:14 -07:00
|
|
|
// convert a span and an identifier to the corresponding
|
|
|
|
// 1-segment path
|
2013-09-02 02:50:59 +02:00
|
|
|
pub fn ident_to_path(s: Span, identifier: Ident) -> Path {
|
2013-08-07 09:47:28 -07:00
|
|
|
ast::Path {
|
|
|
|
span: s,
|
|
|
|
global: false,
|
2014-02-28 13:09:09 -08:00
|
|
|
segments: vec!(
|
2013-08-07 09:47:28 -07:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2014-11-03 21:52:52 -05:00
|
|
|
parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
|
|
|
|
lifetimes: Vec::new(),
|
|
|
|
types: OwnedSlice::empty(),
|
2014-11-29 17:08:30 +13:00
|
|
|
bindings: OwnedSlice::empty(),
|
2014-11-03 21:52:52 -05:00
|
|
|
})
|
2013-08-07 09:47:28 -07:00
|
|
|
}
|
2014-02-28 13:09:09 -08:00
|
|
|
),
|
2013-08-07 09:47:28 -07:00
|
|
|
}
|
2012-01-14 16:05:07 -08:00
|
|
|
}
|
|
|
|
|
2014-11-29 17:08:30 +13: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 19:06:01 +03: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 18:41:06 -08:00
|
|
|
}
|
|
|
|
|
2014-02-15 01:26:51 -05:00
|
|
|
pub fn name_to_dummy_lifetime(name: Name) -> Lifetime {
|
|
|
|
Lifetime { id: DUMMY_NODE_ID,
|
|
|
|
span: codemap::DUMMY_SP,
|
|
|
|
name: name }
|
|
|
|
}
|
|
|
|
|
2014-02-14 07:07:09 +02: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 03:39:03 -07:00
|
|
|
let mut pretty = pprust::ty_to_string(ty);
|
2014-02-14 07:07:09 +02:00
|
|
|
match *trait_ref {
|
|
|
|
Some(ref trait_ref) => {
|
2014-10-14 23:05:01 -07:00
|
|
|
pretty.push('.');
|
2014-06-21 03:39:03 -07:00
|
|
|
pretty.push_str(pprust::path_to_string(&trait_ref.path).as_slice());
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
2014-04-02 16:54:22 -07:00
|
|
|
token::gensym_ident(pretty.as_slice())
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2014-08-05 19:44:21 -07:00
|
|
|
pub fn trait_method_to_ty_method(method: &Method) -> TypeMethod {
|
|
|
|
match method.node {
|
|
|
|
MethDecl(ident,
|
|
|
|
ref generics,
|
|
|
|
abi,
|
|
|
|
ref explicit_self,
|
|
|
|
fn_style,
|
|
|
|
ref decl,
|
|
|
|
_,
|
|
|
|
vis) => {
|
|
|
|
TypeMethod {
|
|
|
|
ident: ident,
|
|
|
|
attrs: method.attrs.clone(),
|
|
|
|
fn_style: fn_style,
|
|
|
|
decl: (*decl).clone(),
|
|
|
|
generics: generics.clone(),
|
|
|
|
explicit_self: (*explicit_self).clone(),
|
|
|
|
id: method.id,
|
|
|
|
span: method.span,
|
|
|
|
vis: vis,
|
|
|
|
abi: abi,
|
|
|
|
}
|
|
|
|
},
|
2014-10-09 15:17:22 -04:00
|
|
|
MethMac(_) => panic!("expected non-macro method declaration")
|
2014-08-05 19:44:21 -07: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 15:17:22 -04:00
|
|
|
panic!("trait_method_to_ty_method(): expected method but found \
|
2014-08-05 19:44:21 -07: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 15:05:33 +02:00
|
|
|
pub fn struct_field_visibility(field: ast::StructField) -> Visibility {
|
2012-08-15 15:53:58 -07:00
|
|
|
match field.node.kind {
|
2014-03-25 16:53:52 -07:00
|
|
|
ast::NamedField(_, v) | ast::UnnamedField(v) => v
|
2012-08-15 15:53:58 -07:00
|
|
|
}
|
2012-03-28 18:50:33 -07:00
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Maps a binary operator to its precedence
|
2013-09-02 03:45:37 +02:00
|
|
|
pub fn operator_prec(op: ast::BinOp) -> uint {
|
2012-08-06 12:34:08 -07:00
|
|
|
match op {
|
2013-07-19 18:42:11 -07:00
|
|
|
// 'as' sits here with 12
|
2013-09-02 03:45:37 +02: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 16:13:59 -07:00
|
|
|
}
|
|
|
|
}
|
2012-05-14 14:13:32 -07:00
|
|
|
|
2013-03-29 10:04:48 -07:00
|
|
|
/// Precedence of the `as` operator, which is a binary operator
|
|
|
|
/// not appearing in the prior table.
|
2014-10-27 15:37:07 -07:00
|
|
|
#[allow(non_upper_case_globals)]
|
2013-07-19 18:42:11 -07:00
|
|
|
pub static as_prec: uint = 12u;
|
2013-03-29 10:04:48 -07:00
|
|
|
|
2013-02-14 21:50:03 -08:00
|
|
|
pub fn empty_generics() -> Generics {
|
2014-08-11 09:32:26 -07:00
|
|
|
Generics {
|
|
|
|
lifetimes: Vec::new(),
|
|
|
|
ty_params: OwnedSlice::empty(),
|
|
|
|
where_clause: WhereClause {
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
predicates: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
2013-02-14 21:50:03 -08:00
|
|
|
}
|
|
|
|
|
2012-05-16 13:21:04 -07:00
|
|
|
// ______________________________________________________________________
|
|
|
|
// Enumerating the IDs which appear in an AST
|
|
|
|
|
2014-10-14 21:59:41 -04:00
|
|
|
#[deriving(Encodable, Decodable, Show)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct IdRange {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub min: NodeId,
|
|
|
|
pub max: NodeId,
|
2013-01-17 08:55:28 -08:00
|
|
|
}
|
2012-05-16 13:21:04 -07:00
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
impl Copy for IdRange {}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
impl IdRange {
|
|
|
|
pub fn max() -> IdRange {
|
|
|
|
IdRange {
|
2014-01-25 20:37:51 +13:00
|
|
|
min: u32::MAX,
|
|
|
|
max: u32::MIN,
|
2013-05-31 15:17:22 -07:00
|
|
|
}
|
2013-03-15 15:24:24 -04:00
|
|
|
}
|
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn empty(&self) -> bool {
|
2013-03-15 15:24:24 -04:00
|
|
|
self.min >= self.max
|
|
|
|
}
|
|
|
|
|
2013-07-27 10:25:59 +02:00
|
|
|
pub fn add(&mut self, id: NodeId) {
|
2014-02-06 02:34:33 -05:00
|
|
|
self.min = cmp::min(self.min, id);
|
|
|
|
self.max = cmp::max(self.max, id + 1);
|
2013-03-15 15:24:24 -04:00
|
|
|
}
|
2012-05-16 13:21:04 -07:00
|
|
|
}
|
|
|
|
|
2013-08-29 19:01:19 -07:00
|
|
|
pub trait IdVisitingOperation {
|
2014-10-25 22:27:15 +03:00
|
|
|
fn visit_id(&mut self, node_id: NodeId);
|
2013-08-29 19:01:19 -07:00
|
|
|
}
|
|
|
|
|
2014-07-11 21:22:11 -07:00
|
|
|
/// A visitor that applies its operation to all of the node IDs
|
|
|
|
/// in a visitable thing.
|
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
pub struct IdVisitor<'a, O:'a> {
|
2014-10-25 22:27:15 +03:00
|
|
|
pub operation: &'a mut O,
|
2014-08-27 21:46:52 -04:00
|
|
|
pub pass_through_items: bool,
|
|
|
|
pub visited_outermost: bool,
|
|
|
|
}
|
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a, O: IdVisitingOperation> IdVisitor<'a, O> {
|
2014-10-25 22:27:15 +03:00
|
|
|
fn visit_generics_helper(&mut self, generics: &Generics) {
|
2013-08-03 12:45:23 -04:00
|
|
|
for type_parameter in generics.ty_params.iter() {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(type_parameter.id)
|
2013-02-14 21:50:03 -08:00
|
|
|
}
|
2013-08-03 12:45:23 -04:00
|
|
|
for lifetime in generics.lifetimes.iter() {
|
2014-08-05 22:59:24 -04:00
|
|
|
self.operation.visit_id(lifetime.lifetime.id)
|
2013-02-14 21:50:03 -08:00
|
|
|
}
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 01:54:36 +03:00
|
|
|
impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
|
2013-08-08 14:23:25 +02:00
|
|
|
fn visit_mod(&mut self,
|
2014-01-09 15:05:33 +02:00
|
|
|
module: &Mod,
|
2013-08-31 18:13:04 +02:00
|
|
|
_: Span,
|
2014-09-12 13:10:30 +03:00
|
|
|
node_id: NodeId) {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(node_id);
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_mod(self, module)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-12 13:10:30 +03: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 18:42:11 -07:00
|
|
|
match view_item.node {
|
2014-03-07 15:57:45 +08:00
|
|
|
ViewItemExternCrate(_, _, node_id) => {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(node_id)
|
2012-05-16 13:21:04 -07:00
|
|
|
}
|
2014-04-26 22:33:45 +09: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-18 00:56:56 +02:00
|
|
|
self.operation.visit_id(path.node.id())
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_view_item(self, view_item);
|
2014-04-22 20:09:21 -05:00
|
|
|
self.visited_outermost = false;
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(foreign_item.id);
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_foreign_item(self, foreign_item)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_item(&mut self, item: &Item) {
|
2013-07-19 18:42:11 -07:00
|
|
|
if !self.pass_through_items {
|
|
|
|
if self.visited_outermost {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
self.visited_outermost = true
|
2012-05-16 13:21:04 -07:00
|
|
|
}
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(item.id);
|
2014-11-29 16:41:21 -05: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 15:31:56 -07:00
|
|
|
}
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_item(self, item);
|
2013-07-19 18:42:11 -07:00
|
|
|
|
|
|
|
self.visited_outermost = false
|
|
|
|
}
|
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_local(&mut self, local: &Local) {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(local.id);
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_local(self, local)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_block(&mut self, block: &Block) {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(block.id);
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_block(self, block)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_stmt(&mut self, statement: &Stmt) {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(ast_util::stmt_id(statement));
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_stmt(self, statement)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_pat(&mut self, pattern: &Pat) {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(pattern.id);
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_pat(self, pattern)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_expr(&mut self, expression: &Expr) {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(expression.id);
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_expr(self, expression)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
2012-05-16 13:21:04 -07:00
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_ty(&mut self, typ: &Ty) {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(typ.id);
|
2014-11-29 16:41:21 -05:00
|
|
|
if let TyPath(_, id) = typ.node {
|
|
|
|
self.operation.visit_id(id);
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_ty(self, typ)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
2012-05-16 13:21:04 -07:00
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_generics(&mut self, generics: &Generics) {
|
2013-07-19 18:42:11 -07:00
|
|
|
self.visit_generics_helper(generics);
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_generics(self, generics)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 14:23:25 +02:00
|
|
|
fn visit_fn(&mut self,
|
2014-09-10 01:54:36 +03:00
|
|
|
function_kind: visit::FnKind<'v>,
|
|
|
|
function_declaration: &'v FnDecl,
|
|
|
|
block: &'v Block,
|
2013-08-31 18:13:04 +02:00
|
|
|
span: Span,
|
2014-09-12 13:10:30 +03:00
|
|
|
node_id: NodeId) {
|
2013-07-19 18:42:11 -07:00
|
|
|
if !self.pass_through_items {
|
2014-09-10 01:54:36 +03:00
|
|
|
match function_kind {
|
2014-01-09 15:05:33 +02:00
|
|
|
visit::FkMethod(..) if self.visited_outermost => return,
|
|
|
|
visit::FkMethod(..) => self.visited_outermost = true,
|
2013-07-19 18:42:11 -07:00
|
|
|
_ => {}
|
2012-05-16 13:21:04 -07:00
|
|
|
}
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(node_id);
|
2012-05-16 13:21:04 -07:00
|
|
|
|
2014-09-10 01:54:36 +03:00
|
|
|
match function_kind {
|
2014-01-27 14:18:36 +02:00
|
|
|
visit::FkItemFn(_, generics, _, _) |
|
|
|
|
visit::FkMethod(_, generics, _) => {
|
2013-07-19 18:42:11 -07:00
|
|
|
self.visit_generics_helper(generics)
|
2012-05-16 13:21:04 -07:00
|
|
|
}
|
2014-01-09 15:05:33 +02:00
|
|
|
visit::FkFnBlock => {}
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2013-08-03 12:45:23 -04:00
|
|
|
for argument in function_declaration.inputs.iter() {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(argument.id)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 14:23:25 +02:00
|
|
|
visit::walk_fn(self,
|
2014-11-15 16:55:27 -05:00
|
|
|
function_kind,
|
|
|
|
function_declaration,
|
|
|
|
block,
|
|
|
|
span);
|
2013-07-19 18:42:11 -07:00
|
|
|
|
|
|
|
if !self.pass_through_items {
|
2014-11-29 16:41:21 -05:00
|
|
|
if let visit::FkMethod(..) = function_kind {
|
|
|
|
self.visited_outermost = false;
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_struct_field(&mut self, struct_field: &StructField) {
|
2013-08-29 19:01:19 -07:00
|
|
|
self.operation.visit_id(struct_field.node.id);
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_struct_field(self, struct_field)
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
2013-09-26 21:53:40 -07:00
|
|
|
|
|
|
|
fn visit_struct_def(&mut self,
|
2014-01-09 15:05:33 +02:00
|
|
|
struct_def: &StructDef,
|
2014-05-14 02:20:25 -04:00
|
|
|
_: ast::Ident,
|
|
|
|
_: &ast::Generics,
|
2014-09-12 13:10:30 +03:00
|
|
|
id: NodeId) {
|
2013-09-26 21:53:40 -07:00
|
|
|
self.operation.visit_id(id);
|
2013-09-20 02:08:47 -04:00
|
|
|
struct_def.ctor_id.map(|ctor_id| self.operation.visit_id(ctor_id));
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_struct_def(self, struct_def);
|
2013-09-26 21:53:40 -07:00
|
|
|
}
|
2013-10-02 11:29:29 -07:00
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_trait_item(&mut self, tm: &ast::TraitItem) {
|
2013-10-02 11:29:29 -07:00
|
|
|
match *tm {
|
2014-08-04 13:56:56 -07:00
|
|
|
ast::RequiredMethod(ref m) => self.operation.visit_id(m.id),
|
|
|
|
ast::ProvidedMethod(ref m) => self.operation.visit_id(m.id),
|
2014-10-28 14:48:52 -04:00
|
|
|
ast::TypeTraitItem(ref typ) => self.operation.visit_id(typ.ty_param.id),
|
2013-10-02 11:29:29 -07:00
|
|
|
}
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_trait_item(self, tm);
|
2013-10-02 11:29:29 -07:00
|
|
|
}
|
2014-05-18 02:38:13 +03:00
|
|
|
|
|
|
|
fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
|
|
|
|
self.operation.visit_id(lifetime.id);
|
|
|
|
}
|
|
|
|
|
2014-11-18 17:39:16 +01:00
|
|
|
fn visit_lifetime_def(&mut self, def: &'v LifetimeDef) {
|
2014-05-18 02:38:13 +03:00
|
|
|
self.visit_lifetime_ref(&def.lifetime);
|
|
|
|
}
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn visit_ids_for_inlined_item<O: IdVisitingOperation>(item: &InlinedItem,
|
2014-10-25 22:27:15 +03:00
|
|
|
operation: &mut O) {
|
2013-08-08 14:23:25 +02:00
|
|
|
let mut id_visitor = IdVisitor {
|
2013-08-29 19:01:19 -07:00
|
|
|
operation: operation,
|
2013-08-08 14:23:25 +02:00
|
|
|
pass_through_items: true,
|
2013-07-19 18:42:11 -07:00
|
|
|
visited_outermost: false,
|
|
|
|
};
|
2014-01-06 14:00:46 +02:00
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_inlined_item(&mut id_visitor, item);
|
2012-05-16 13:21:04 -07:00
|
|
|
}
|
|
|
|
|
2013-08-29 19:01:19 -07:00
|
|
|
struct IdRangeComputingVisitor {
|
2014-10-25 22:27:15 +03:00
|
|
|
result: IdRange,
|
2013-08-29 19:01:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IdVisitingOperation for IdRangeComputingVisitor {
|
2014-10-25 22:27:15 +03:00
|
|
|
fn visit_id(&mut self, id: NodeId) {
|
|
|
|
self.result.add(id);
|
2012-05-16 13:21:04 -07:00
|
|
|
}
|
2013-08-29 19:01:19 -07:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn compute_id_range_for_inlined_item(item: &InlinedItem) -> IdRange {
|
2014-10-25 22:27:15 +03:00
|
|
|
let mut visitor = IdRangeComputingVisitor {
|
|
|
|
result: IdRange::max()
|
2013-12-27 18:03:28 -08:00
|
|
|
};
|
2014-10-25 22:27:15 +03:00
|
|
|
visit_ids_for_inlined_item(item, &mut visitor);
|
|
|
|
visitor.result
|
2012-05-16 13:21:04 -07:00
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Computes the id range for a single fn body, ignoring nested items.
|
2014-09-10 01:54:36 +03:00
|
|
|
pub fn compute_id_range_for_fn_body(fk: visit::FnKind,
|
2014-04-21 19:21:53 -04:00
|
|
|
decl: &FnDecl,
|
|
|
|
body: &Block,
|
|
|
|
sp: Span,
|
|
|
|
id: NodeId)
|
|
|
|
-> IdRange
|
|
|
|
{
|
2014-10-25 22:27:15 +03:00
|
|
|
let mut visitor = IdRangeComputingVisitor {
|
|
|
|
result: IdRange::max()
|
2014-04-21 19:21:53 -04:00
|
|
|
};
|
|
|
|
let mut id_visitor = IdVisitor {
|
2014-10-25 22:27:15 +03:00
|
|
|
operation: &mut visitor,
|
2014-04-21 19:21:53 -04:00
|
|
|
pass_through_items: false,
|
|
|
|
visited_outermost: false,
|
|
|
|
};
|
2014-09-12 13:10:30 +03:00
|
|
|
id_visitor.visit_fn(fk, decl, body, sp, id);
|
2014-10-25 22:27:15 +03:00
|
|
|
id_visitor.operation.result
|
2014-04-21 19:21:53 -04:00
|
|
|
}
|
|
|
|
|
2014-12-08 13:28:32 -05:00
|
|
|
// FIXME(#19596) unbox `it`
|
2013-11-25 23:37:03 +09:00
|
|
|
pub fn walk_pat(pat: &Pat, it: |&Pat| -> bool) -> bool {
|
2013-05-22 06:54:35 -04:00
|
|
|
if !it(pat) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:34:08 -07:00
|
|
|
match pat.node {
|
2014-05-16 00:16:13 -07:00
|
|
|
PatIdent(_, _, Some(ref p)) => walk_pat(&**p, it),
|
2013-09-02 03:45:37 +02:00
|
|
|
PatStruct(_, ref fields, _) => {
|
2014-10-06 13:36:53 +13:00
|
|
|
fields.iter().all(|field| walk_pat(&*field.node.pat, |p| it(p)))
|
2012-09-19 16:55:01 -07:00
|
|
|
}
|
2013-09-02 03:45:37 +02:00
|
|
|
PatEnum(_, Some(ref s)) | PatTup(ref s) => {
|
2014-06-12 06:13:25 -04:00
|
|
|
s.iter().all(|p| walk_pat(&**p, |p| it(p)))
|
2012-09-19 16:55:01 -07:00
|
|
|
}
|
2014-05-16 00:16:13 -07:00
|
|
|
PatBox(ref s) | PatRegion(ref s) => {
|
|
|
|
walk_pat(&**s, it)
|
2012-09-19 16:55:01 -07:00
|
|
|
}
|
2013-09-02 03:45:37 +02:00
|
|
|
PatVec(ref before, ref slice, ref after) => {
|
2014-06-12 06:13:25 -04: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 20:22:43 +00:00
|
|
|
}
|
2014-10-09 15:17:22 -04:00
|
|
|
PatMac(_) => panic!("attempted to analyze unexpanded pattern"),
|
2014-08-06 17:04:44 +02:00
|
|
|
PatWild(_) | PatLit(_) | PatRange(_, _) | PatIdent(_, _, _) |
|
2013-09-02 03:45:37 +02:00
|
|
|
PatEnum(_, _) => {
|
2013-05-22 06:54:35 -04:00
|
|
|
true
|
|
|
|
}
|
2012-05-21 23:17:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 17:03:04 -07:00
|
|
|
pub trait EachViewItem {
|
2014-12-08 13:28:32 -05:00
|
|
|
fn each_view_item<F>(&self, f: F) -> bool where F: FnMut(&ast::ViewItem) -> bool;
|
2013-05-27 17:03:04 -07:00
|
|
|
}
|
|
|
|
|
2014-12-08 13:28:32 -05:00
|
|
|
struct EachViewItemData<F> where F: FnMut(&ast::ViewItem) -> bool {
|
|
|
|
callback: F,
|
2013-07-19 18:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-12-08 13:28:32 -05:00
|
|
|
impl<'v, F> Visitor<'v> for EachViewItemData<F> where F: FnMut(&ast::ViewItem) -> bool {
|
2014-09-12 13:10:30 +03:00
|
|
|
fn visit_view_item(&mut self, view_item: &ast::ViewItem) {
|
2013-07-19 18:42:11 -07:00
|
|
|
let _ = (self.callback)(view_item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 07:38:55 +02:00
|
|
|
impl EachViewItem for ast::Crate {
|
2014-12-08 13:28:32 -05:00
|
|
|
fn each_view_item<F>(&self, f: F) -> bool where F: FnMut(&ast::ViewItem) -> bool {
|
2013-09-06 20:29:16 -07:00
|
|
|
let mut visit = EachViewItemData {
|
2013-07-19 18:42:11 -07:00
|
|
|
callback: f,
|
|
|
|
};
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_crate(&mut visit, self);
|
2013-05-27 17:03:04 -07:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn view_path_id(p: &ViewPath) -> NodeId {
|
2012-08-06 12:34:08 -07:00
|
|
|
match p.node {
|
2014-01-09 15:05:33 +02:00
|
|
|
ViewPathSimple(_, _, id) | ViewPathGlob(_, id)
|
|
|
|
| ViewPathList(_, _, id) => id
|
2012-05-22 10:54:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 14:36:00 -07:00
|
|
|
/// Returns true if the given struct def is tuple-like; i.e. that its fields
|
|
|
|
/// are unnamed.
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn struct_def_is_tuple_like(struct_def: &ast::StructDef) -> bool {
|
2012-10-24 14:36:00 -07:00
|
|
|
struct_def.ctor_id.is_some()
|
|
|
|
}
|
|
|
|
|
2013-06-06 18:54:14 -07:00
|
|
|
/// Returns true if the given pattern consists solely of an identifier
|
|
|
|
/// and false otherwise.
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn pat_is_ident(pat: P<ast::Pat>) -> bool {
|
2013-06-06 18:54:14 -07:00
|
|
|
match pat.node {
|
2013-11-28 12:22:53 -08:00
|
|
|
ast::PatIdent(..) => true,
|
2013-06-06 18:54:14 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:52:59 -07: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 12:54:01 -08:00
|
|
|
&& (segments_name_eq(a.segments.as_slice(), b.segments.as_slice()))
|
2013-07-10 11:52:59 -07:00
|
|
|
}
|
|
|
|
|
2013-09-05 14:15:00 -07: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 19:21:14 +11:00
|
|
|
if a.len() != b.len() {
|
2013-07-10 11:52:59 -07:00
|
|
|
false
|
|
|
|
} else {
|
2013-09-05 14:15:00 -07:00
|
|
|
for (idx,seg) in a.iter().enumerate() {
|
2014-11-03 21:52:52 -05:00
|
|
|
if seg.identifier.name != b[idx].identifier.name
|
2013-07-11 22:58:14 -07:00
|
|
|
// FIXME #7743: ident -> name problems in lifetime comparison?
|
|
|
|
// can types contain idents?
|
2014-11-03 21:52:52 -05:00
|
|
|
|| seg.parameters != b[idx].parameters
|
|
|
|
{
|
2013-07-10 11:52:59 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2013-04-03 10:28:14 -07:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Returns true if this literal is a string and false otherwise.
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn lit_is_str(lit: &Lit) -> bool {
|
2014-01-03 15:08:48 -08:00
|
|
|
match lit.node {
|
|
|
|
LitStr(..) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 21:22:11 -07:00
|
|
|
/// Macro invocations are guaranteed not to occur after expansion is complete.
|
2014-07-14 16:27:54 -07: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 21:22:11 -07:00
|
|
|
/// that the invocations are indeed gone.
|
2014-07-14 16:27:54 -07:00
|
|
|
pub trait PostExpansionMethod {
|
|
|
|
fn pe_ident(&self) -> ast::Ident;
|
|
|
|
fn pe_generics<'a>(&'a self) -> &'a ast::Generics;
|
2014-05-28 22:26:56 -07:00
|
|
|
fn pe_abi(&self) -> Abi;
|
2014-07-14 16:27:54 -07:00
|
|
|
fn pe_explicit_self<'a>(&'a self) -> &'a ast::ExplicitSelf;
|
|
|
|
fn pe_fn_style(&self) -> ast::FnStyle;
|
2014-09-13 19:06:01 +03:00
|
|
|
fn pe_fn_decl<'a>(&'a self) -> &'a ast::FnDecl;
|
|
|
|
fn pe_body<'a>(&'a self) -> &'a ast::Block;
|
2014-07-14 16:27:54 -07:00
|
|
|
fn pe_vis(&self) -> ast::Visibility;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! mf_method{
|
2014-09-13 19:06:01 +03:00
|
|
|
($meth_name:ident, $field_ty:ty, $field_pat:pat, $result:expr) => {
|
2014-07-14 16:27:54 -07:00
|
|
|
fn $meth_name<'a>(&'a self) -> $field_ty {
|
|
|
|
match self.node {
|
2014-07-11 21:22:11 -07:00
|
|
|
$field_pat => $result,
|
|
|
|
MethMac(_) => {
|
2014-10-09 15:17:22 -04:00
|
|
|
panic!("expected an AST without macro invocations");
|
2014-07-11 21:22:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 10:28:42 -07:00
|
|
|
|
2014-07-14 16:27:54 -07:00
|
|
|
impl PostExpansionMethod for Method {
|
2014-07-16 10:28:42 -07:00
|
|
|
mf_method!(pe_ident,ast::Ident,MethDecl(ident,_,_,_,_,_,_,_),ident)
|
2014-07-14 16:27:54 -07:00
|
|
|
mf_method!(pe_generics,&'a ast::Generics,
|
2014-07-16 10:28:42 -07:00
|
|
|
MethDecl(_,ref generics,_,_,_,_,_,_),generics)
|
|
|
|
mf_method!(pe_abi,Abi,MethDecl(_,_,abi,_,_,_,_,_),abi)
|
2014-07-14 16:27:54 -07:00
|
|
|
mf_method!(pe_explicit_self,&'a ast::ExplicitSelf,
|
2014-07-16 10:28:42 -07:00
|
|
|
MethDecl(_,_,_,ref explicit_self,_,_,_,_),explicit_self)
|
|
|
|
mf_method!(pe_fn_style,ast::FnStyle,MethDecl(_,_,_,_,fn_style,_,_,_),fn_style)
|
2014-09-13 19:06:01 +03:00
|
|
|
mf_method!(pe_fn_decl,&'a ast::FnDecl,MethDecl(_,_,_,_,_,ref decl,_,_),&**decl)
|
|
|
|
mf_method!(pe_body,&'a ast::Block,MethDecl(_,_,_,_,_,_,ref body,_),&**body)
|
2014-07-16 10:28:42 -07:00
|
|
|
mf_method!(pe_vis,ast::Visibility,MethDecl(_,_,_,_,_,_,_,vis),vis)
|
2014-07-14 16:27:54 -07:00
|
|
|
}
|
2014-07-11 21:22:11 -07:00
|
|
|
|
2013-04-03 10:28:14 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use ast::*;
|
|
|
|
use super::*;
|
2014-02-28 12:54:01 -08:00
|
|
|
|
2013-07-10 16:40:09 -07:00
|
|
|
fn ident_to_segment(id : &Ident) -> PathSegment {
|
2014-11-03 21:52:52 -05:00
|
|
|
PathSegment {identifier: id.clone(),
|
|
|
|
parameters: PathParameters::none()}
|
2013-09-05 14:15:00 -07:00
|
|
|
}
|
2013-04-03 10:28:14 -07:00
|
|
|
|
2013-07-10 11:52:59 -07:00
|
|
|
#[test] fn idents_name_eq_test() {
|
2014-03-28 20:42:34 +01:00
|
|
|
assert!(segments_name_eq(
|
2014-07-08 22:28:52 -07:00
|
|
|
[Ident{name:Name(3),ctxt:4}, Ident{name:Name(78),ctxt:82}]
|
2014-03-28 20:42:34 +01:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice(),
|
2014-07-08 22:28:52 -07:00
|
|
|
[Ident{name:Name(3),ctxt:104}, Ident{name:Name(78),ctxt:182}]
|
2014-03-28 20:42:34 +01:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice()));
|
|
|
|
assert!(!segments_name_eq(
|
2014-07-08 22:28:52 -07:00
|
|
|
[Ident{name:Name(3),ctxt:4}, Ident{name:Name(78),ctxt:82}]
|
2014-03-28 20:42:34 +01:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice(),
|
2014-07-08 22:28:52 -07:00
|
|
|
[Ident{name:Name(3),ctxt:104}, Ident{name:Name(77),ctxt:182}]
|
2014-03-28 20:42:34 +01:00
|
|
|
.iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice()));
|
2013-07-10 11:52:59 -07:00
|
|
|
}
|
2013-04-03 10:28:14 -07:00
|
|
|
}
|