1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use visit::{self, Visitor, FnKind};
15 use syntax::ast::{Ident, Name, NodeId, DUMMY_NODE_ID};
16 use syntax::codemap::Span;
18 use syntax::owned_slice::OwnedSlice;
20 pub fn walk_pat<F>(pat: &Pat, mut it: F) -> bool where F: FnMut(&Pat) -> bool {
21 // FIXME(#19596) this is a workaround, but there should be a better way
22 fn walk_pat_<G>(pat: &Pat, it: &mut G) -> bool where G: FnMut(&Pat) -> bool {
28 PatIdent(_, _, Some(ref p)) => walk_pat_(&**p, it),
29 PatStruct(_, ref fields, _) => {
30 fields.iter().all(|field| walk_pat_(&*field.node.pat, it))
32 PatEnum(_, Some(ref s)) | PatTup(ref s) => {
33 s.iter().all(|p| walk_pat_(&**p, it))
35 PatBox(ref s) | PatRegion(ref s, _) => {
38 PatVec(ref before, ref slice, ref after) => {
39 before.iter().all(|p| walk_pat_(&**p, it)) &&
40 slice.iter().all(|p| walk_pat_(&**p, it)) &&
41 after.iter().all(|p| walk_pat_(&**p, it))
43 PatWild(_) | PatLit(_) | PatRange(_, _) | PatIdent(_, _, _) |
44 PatEnum(_, _) | PatQPath(_, _) => {
50 walk_pat_(pat, &mut it)
53 pub fn binop_to_string(op: BinOp_) -> &'static str {
76 /// Returns true if the given struct def is tuple-like; i.e. that its fields
78 pub fn struct_def_is_tuple_like(struct_def: &hir::StructDef) -> bool {
79 struct_def.ctor_id.is_some()
82 pub fn stmt_id(s: &Stmt) -> NodeId {
84 StmtDecl(_, id) => id,
85 StmtExpr(_, id) => id,
86 StmtSemi(_, id) => id,
90 pub fn lazy_binop(b: BinOp_) -> bool {
98 pub fn is_shift_binop(b: BinOp_) -> bool {
106 pub fn is_comparison_binop(b: BinOp_) -> bool {
108 BiEq | BiLt | BiLe | BiNe | BiGt | BiGe =>
110 BiAnd | BiOr | BiAdd | BiSub | BiMul | BiDiv | BiRem |
111 BiBitXor | BiBitAnd | BiBitOr | BiShl | BiShr =>
116 /// Returns `true` if the binary operator takes its arguments by value
117 pub fn is_by_value_binop(b: BinOp_) -> bool {
118 !is_comparison_binop(b)
121 /// Returns `true` if the unary operator takes its argument by value
122 pub fn is_by_value_unop(u: UnOp) -> bool {
124 UnNeg | UnNot => true,
129 pub fn unop_to_string(op: UnOp) -> &'static str {
137 pub struct IdVisitor<'a, O:'a> {
138 pub operation: &'a mut O,
139 pub pass_through_items: bool,
140 pub visited_outermost: bool,
143 impl<'a, O: ast_util::IdVisitingOperation> IdVisitor<'a, O> {
144 fn visit_generics_helper(&mut self, generics: &Generics) {
145 for type_parameter in generics.ty_params.iter() {
146 self.operation.visit_id(type_parameter.id)
148 for lifetime in &generics.lifetimes {
149 self.operation.visit_id(lifetime.lifetime.id)
154 impl<'a, 'v, O: ast_util::IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
155 fn visit_mod(&mut self,
159 self.operation.visit_id(node_id);
160 visit::walk_mod(self, module)
163 fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
164 self.operation.visit_id(foreign_item.id);
165 visit::walk_foreign_item(self, foreign_item)
168 fn visit_item(&mut self, item: &Item) {
169 if !self.pass_through_items {
170 if self.visited_outermost {
173 self.visited_outermost = true
177 self.operation.visit_id(item.id);
179 ItemUse(ref view_path) => {
180 match view_path.node {
181 ViewPathSimple(_, _) |
182 ViewPathGlob(_) => {}
183 ViewPathList(_, ref paths) => {
185 self.operation.visit_id(path.node.id())
190 ItemEnum(ref enum_definition, _) => {
191 for variant in &enum_definition.variants {
192 self.operation.visit_id(variant.node.id)
198 visit::walk_item(self, item);
200 self.visited_outermost = false
203 fn visit_local(&mut self, local: &Local) {
204 self.operation.visit_id(local.id);
205 visit::walk_local(self, local)
208 fn visit_block(&mut self, block: &Block) {
209 self.operation.visit_id(block.id);
210 visit::walk_block(self, block)
213 fn visit_stmt(&mut self, statement: &Stmt) {
214 self.operation.visit_id(stmt_id(statement));
215 visit::walk_stmt(self, statement)
218 fn visit_pat(&mut self, pattern: &Pat) {
219 self.operation.visit_id(pattern.id);
220 visit::walk_pat(self, pattern)
223 fn visit_expr(&mut self, expression: &Expr) {
224 self.operation.visit_id(expression.id);
225 visit::walk_expr(self, expression)
228 fn visit_ty(&mut self, typ: &Ty) {
229 self.operation.visit_id(typ.id);
230 visit::walk_ty(self, typ)
233 fn visit_generics(&mut self, generics: &Generics) {
234 self.visit_generics_helper(generics);
235 visit::walk_generics(self, generics)
238 fn visit_fn(&mut self,
239 function_kind: FnKind<'v>,
240 function_declaration: &'v FnDecl,
244 if !self.pass_through_items {
245 match function_kind {
246 FnKind::Method(..) if self.visited_outermost => return,
247 FnKind::Method(..) => self.visited_outermost = true,
252 self.operation.visit_id(node_id);
254 match function_kind {
255 FnKind::ItemFn(_, generics, _, _, _, _) => {
256 self.visit_generics_helper(generics)
258 FnKind::Method(_, sig, _) => {
259 self.visit_generics_helper(&sig.generics)
261 FnKind::Closure => {}
264 for argument in &function_declaration.inputs {
265 self.operation.visit_id(argument.id)
270 function_declaration,
274 if !self.pass_through_items {
275 if let FnKind::Method(..) = function_kind {
276 self.visited_outermost = false;
281 fn visit_struct_field(&mut self, struct_field: &StructField) {
282 self.operation.visit_id(struct_field.node.id);
283 visit::walk_struct_field(self, struct_field)
286 fn visit_struct_def(&mut self,
287 struct_def: &StructDef,
291 self.operation.visit_id(id);
292 struct_def.ctor_id.map(|ctor_id| self.operation.visit_id(ctor_id));
293 visit::walk_struct_def(self, struct_def);
296 fn visit_trait_item(&mut self, ti: &hir::TraitItem) {
297 self.operation.visit_id(ti.id);
298 visit::walk_trait_item(self, ti);
301 fn visit_impl_item(&mut self, ii: &hir::ImplItem) {
302 self.operation.visit_id(ii.id);
303 visit::walk_impl_item(self, ii);
306 fn visit_lifetime(&mut self, lifetime: &Lifetime) {
307 self.operation.visit_id(lifetime.id);
310 fn visit_lifetime_def(&mut self, def: &LifetimeDef) {
311 self.visit_lifetime(&def.lifetime);
314 fn visit_trait_ref(&mut self, trait_ref: &TraitRef) {
315 self.operation.visit_id(trait_ref.ref_id);
316 visit::walk_trait_ref(self, trait_ref);
320 /// Computes the id range for a single fn body, ignoring nested items.
321 pub fn compute_id_range_for_fn_body(fk: FnKind,
328 let mut visitor = ast_util::IdRangeComputingVisitor {
329 result: ast_util::IdRange::max()
331 let mut id_visitor = IdVisitor {
332 operation: &mut visitor,
333 pass_through_items: false,
334 visited_outermost: false,
336 id_visitor.visit_fn(fk, decl, body, sp, id);
337 id_visitor.operation.result
340 pub fn is_path(e: P<Expr>) -> bool {
341 match e.node { ExprPath(..) => true, _ => false }
344 pub fn empty_generics() -> Generics {
346 lifetimes: Vec::new(),
347 ty_params: OwnedSlice::empty(),
348 where_clause: WhereClause {
350 predicates: Vec::new(),
355 // convert a span and an identifier to the corresponding
357 pub fn ident_to_path(s: Span, ident: Ident) -> Path {
364 parameters: hir::AngleBracketedParameters(hir::AngleBracketedParameterData {
365 lifetimes: Vec::new(),
366 types: OwnedSlice::empty(),
367 bindings: OwnedSlice::empty(),