summary refs log tree commit diff
path: root/src/librustc/metadata/inline.rs
blob: a5ca68e4350ebb7193f48325ad45ff57f8e7b0f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// 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.

use middle::def_id::DefId;
use rustc_front::hir;
use rustc_front::util::IdVisitor;
use syntax::ast_util::{IdRange, IdRangeComputingVisitor, IdVisitingOperation};
use syntax::ptr::P;
use rustc_front::visit::Visitor;
use self::InlinedItem::*;

/// The data we save and restore about an inlined item or method.  This is not
/// part of the AST that we parse from a file, but it becomes part of the tree
/// that we trans.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum InlinedItem {
    Item(P<hir::Item>),
    TraitItem(DefId /* impl id */, P<hir::TraitItem>),
    ImplItem(DefId /* impl id */, P<hir::ImplItem>),
    Foreign(P<hir::ForeignItem>),
}

/// A borrowed version of `hir::InlinedItem`.
pub enum InlinedItemRef<'a> {
    Item(&'a hir::Item),
    TraitItem(DefId, &'a hir::TraitItem),
    ImplItem(DefId, &'a hir::ImplItem),
    Foreign(&'a hir::ForeignItem)
}

impl InlinedItem {
    pub fn visit<'ast,V>(&'ast self, visitor: &mut V)
        where V: Visitor<'ast>
    {
        match *self {
            Item(ref i) => visitor.visit_item(&**i),
            Foreign(ref i) => visitor.visit_foreign_item(&**i),
            TraitItem(_, ref ti) => visitor.visit_trait_item(ti),
            ImplItem(_, ref ii) => visitor.visit_impl_item(ii),
        }
    }

    pub fn visit_ids<O: IdVisitingOperation>(&self, operation: &mut O) {
        let mut id_visitor = IdVisitor {
            operation: operation,
            pass_through_items: true,
            visited_outermost: false,
        };
        self.visit(&mut id_visitor);
    }

    pub fn compute_id_range(&self) -> IdRange {
        let mut visitor = IdRangeComputingVisitor::new();
        self.visit_ids(&mut visitor);
        visitor.result()
    }
}