lib.rs 43.4 KB
Newer Older
1
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9 10
// 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.

11
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
N
Nick Cameron 已提交
12 13
       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
       html_root_url = "https://doc.rust-lang.org/nightly/")]
14
#![deny(warnings)]
15 16
#![feature(custom_attribute)]
#![allow(unused_attributes)]
17

N
Nick Cameron 已提交
18 19
#[macro_use]
extern crate rustc;
20

N
Nick Cameron 已提交
21 22
#[macro_use]
extern crate log;
N
Nick Cameron 已提交
23
extern crate rustc_data_structures;
N
Nick Cameron 已提交
24
extern crate rustc_serialize;
25
extern crate rustc_typeck;
N
Nick Cameron 已提交
26 27
#[macro_use]
extern crate syntax;
28
extern crate syntax_pos;
29

30
extern crate rls_data;
31
extern crate rls_span;
32

N
Nick Cameron 已提交
33

34 35 36
mod json_dumper;
mod dump_visitor;
#[macro_use]
37
mod span_utils;
N
Nick Cameron 已提交
38
mod sig;
39

40
use rustc::hir;
41 42
use rustc::hir::def::Def as HirDef;
use rustc::hir::map::{Node, NodeItem};
N
Nick Cameron 已提交
43
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
44
use rustc::session::config::CrateType::CrateTypeExecutable;
45
use rustc::ty::{self, TyCtxt};
46
use rustc_typeck::hir_ty_to_ty;
47

48
use std::default::Default;
49
use std::env;
50
use std::fs::File;
51
use std::path::{Path, PathBuf};
N
Nick Cameron 已提交
52

N
Nick Cameron 已提交
53
use syntax::ast::{self, Attribute, NodeId, PatKind};
54
use syntax::parse::lexer::comments::strip_doc_comment_decoration;
55
use syntax::parse::token;
56
use syntax::print::pprust;
57
use syntax::symbol::keywords;
N
Nick Cameron 已提交
58
use syntax::visit::{self, Visitor};
N
Nick Cameron 已提交
59
use syntax::print::pprust::{arg_to_string, ty_to_string};
60 61
use syntax::codemap::MacroAttribute;
use syntax_pos::*;
62

63
use json_dumper::JsonDumper;
64 65 66
use dump_visitor::DumpVisitor;
use span_utils::SpanUtils;

N
Nick Cameron 已提交
67 68
use rls_data::{Def, DefKind, ExternalCrateData, GlobalCrateId, MacroRef, Ref, RefKind, Relation,
               RelationKind, SpanData};
69
use rls_data::config::Config;
70

71

N
Nick Cameron 已提交
72
pub struct SaveContext<'l, 'tcx: 'l> {
73
    tcx: TyCtxt<'l, 'tcx, 'tcx>,
74
    tables: &'l ty::TypeckTables<'tcx>,
75
    analysis: &'l ty::CrateAnalysis,
N
Nick Cameron 已提交
76
    span_utils: SpanUtils<'tcx>,
77
    config: Config,
78 79
}

80 81 82 83 84 85 86
#[derive(Debug)]
pub enum Data {
    RefData(Ref),
    DefData(Def),
    RelationData(Relation),
}

N
Nick Cameron 已提交
87
impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
88
    fn span_from_span(&self, span: Span) -> SpanData {
N
Nick Cameron 已提交
89
        use rls_span::{Column, Row};
90 91

        let cm = self.tcx.sess.codemap();
92 93
        let start = cm.lookup_char_pos(span.lo());
        let end = cm.lookup_char_pos(span.hi());
94 95 96

        SpanData {
            file_name: start.file.name.clone().into(),
97 98
            byte_start: span.lo().0,
            byte_end: span.hi().0,
99 100 101 102 103 104 105
            line_start: Row::new_one_indexed(start.line as u32),
            line_end: Row::new_one_indexed(end.line as u32),
            column_start: Column::new_one_indexed(start.col.0 as u32 + 1),
            column_end: Column::new_one_indexed(end.col.0 as u32 + 1),
        }
    }

106
    // List external crates used by the current crate.
107
    pub fn get_external_crates(&self) -> Vec<ExternalCrateData> {
108 109
        let mut result = Vec::new();

110
        for &n in self.tcx.crates().iter() {
T
Taylor Cramer 已提交
111
            let span = match *self.tcx.extern_crate(n.as_def_id()) {
112 113 114 115 116 117
                Some(ref c) => c.span,
                None => {
                    debug!("Skipping crate {}, no data", n);
                    continue;
                }
            };
118
            let lo_loc = self.span_utils.sess.codemap().lookup_char_pos(span.lo());
119 120
            result.push(ExternalCrateData {
                file_name: SpanUtils::make_path_string(&lo_loc.file.name),
121 122 123
                num: n.as_u32(),
                id: GlobalCrateId {
                    name: self.tcx.crate_name(n).to_string(),
N
Nick Cameron 已提交
124
                    disambiguator: self.tcx.crate_disambiguator(n).to_fingerprint().as_value(),
125
                },
N
Nick Cameron 已提交
126
            });
A
Ariel Ben-Yehuda 已提交
127
        }
128 129 130

        result
    }
N
Nick Cameron 已提交
131

132 133 134 135
    pub fn get_extern_item_data(&self, item: &ast::ForeignItem) -> Option<Data> {
        let qualname = format!("::{}", self.tcx.node_path_str(item.id));
        match item.node {
            ast::ForeignItemKind::Fn(ref decl, ref generics) => {
N
Nick Cameron 已提交
136 137
                let sub_span = self.span_utils
                    .sub_span_after_keyword(item.span, keywords::Fn);
N
rebased  
Nick Cameron 已提交
138
                filter!(self.span_utils, sub_span, item.span, None);
139 140 141 142 143

                Some(Data::DefData(Def {
                    kind: DefKind::Function,
                    id: id_from_node_id(item.id, self),
                    span: self.span_from_span(sub_span.unwrap()),
144
                    name: item.ident.to_string(),
145
                    qualname,
146 147
                    value: make_signature(decl, generics),
                    parent: None,
148 149
                    children: vec![],
                    decl_id: None,
150
                    docs: self.docs_for_attrs(&item.attrs),
151
                    sig: sig::foreign_item_signature(item, self),
152
                    attributes: lower_attributes(item.attrs.clone(), self),
153 154 155 156 157
                }))
            }
            ast::ForeignItemKind::Static(ref ty, m) => {
                let keyword = if m { keywords::Mut } else { keywords::Static };
                let sub_span = self.span_utils.sub_span_after_keyword(item.span, keyword);
N
rebased  
Nick Cameron 已提交
158
                filter!(self.span_utils, sub_span, item.span, None);
159 160 161 162 163 164 165 166

                let id = ::id_from_node_id(item.id, self);
                let span = self.span_from_span(sub_span.unwrap());

                Some(Data::DefData(Def {
                    kind: DefKind::Static,
                    id,
                    span,
167
                    name: item.ident.to_string(),
168 169
                    qualname,
                    value: ty_to_string(ty),
170
                    parent: None,
171 172
                    children: vec![],
                    decl_id: None,
173
                    docs: self.docs_for_attrs(&item.attrs),
174
                    sig: sig::foreign_item_signature(item, self),
175
                    attributes: lower_attributes(item.attrs.clone(), self),
176 177
                }))
            }
P
Paul Lietar 已提交
178 179
            // FIXME(plietar): needs a new DefKind in rls-data
            ast::ForeignItemKind::Ty => None,
180 181 182
        }
    }

183
    pub fn get_item_data(&self, item: &ast::Item) -> Option<Data> {
N
Nick Cameron 已提交
184
        match item.node {
V
Vadim Petrochenkov 已提交
185
            ast::ItemKind::Fn(ref decl, .., ref generics, _) => {
186
                let qualname = format!("::{}", self.tcx.node_path_str(item.id));
N
Nick Cameron 已提交
187 188
                let sub_span = self.span_utils
                    .sub_span_after_keyword(item.span, keywords::Fn);
189
                filter!(self.span_utils, sub_span, item.span, None);
190 191 192 193
                Some(Data::DefData(Def {
                    kind: DefKind::Function,
                    id: id_from_node_id(item.id, self),
                    span: self.span_from_span(sub_span.unwrap()),
194
                    name: item.ident.to_string(),
195
                    qualname,
196
                    value: make_signature(decl, generics),
197
                    parent: None,
198 199
                    children: vec![],
                    decl_id: None,
200
                    docs: self.docs_for_attrs(&item.attrs),
N
Nick Cameron 已提交
201
                    sig: sig::item_signature(item, self),
202
                    attributes: lower_attributes(item.attrs.clone(), self),
203
                }))
N
Nick Cameron 已提交
204
            }
205
            ast::ItemKind::Static(ref typ, mt, _) => {
206
                let qualname = format!("::{}", self.tcx.node_path_str(item.id));
207

208 209 210
                let keyword = match mt {
                    ast::Mutability::Mutable => keywords::Mut,
                    ast::Mutability::Immutable => keywords::Static,
211 212
                };

213
                let sub_span = self.span_utils.sub_span_after_keyword(item.span, keyword);
214
                filter!(self.span_utils, sub_span, item.span, None);
215 216 217 218 219 220 221 222

                let id = id_from_node_id(item.id, self);
                let span = self.span_from_span(sub_span.unwrap());

                Some(Data::DefData(Def {
                    kind: DefKind::Static,
                    id,
                    span,
223
                    name: item.ident.to_string(),
224 225
                    qualname,
                    value: ty_to_string(&typ),
226
                    parent: None,
227 228
                    children: vec![],
                    decl_id: None,
229
                    docs: self.docs_for_attrs(&item.attrs),
N
Nick Cameron 已提交
230
                    sig: sig::item_signature(item, self),
231
                    attributes: lower_attributes(item.attrs.clone(), self),
232
                }))
233
            }
234
            ast::ItemKind::Const(ref typ, _) => {
235
                let qualname = format!("::{}", self.tcx.node_path_str(item.id));
N
Nick Cameron 已提交
236 237
                let sub_span = self.span_utils
                    .sub_span_after_keyword(item.span, keywords::Const);
238
                filter!(self.span_utils, sub_span, item.span, None);
239 240 241 242 243 244 245 246

                let id = id_from_node_id(item.id, self);
                let span = self.span_from_span(sub_span.unwrap());

                Some(Data::DefData(Def {
                    kind: DefKind::Const,
                    id,
                    span,
247
                    name: item.ident.to_string(),
248 249
                    qualname,
                    value: ty_to_string(typ),
250
                    parent: None,
251 252
                    children: vec![],
                    decl_id: None,
253
                    docs: self.docs_for_attrs(&item.attrs),
N
Nick Cameron 已提交
254
                    sig: sig::item_signature(item, self),
255
                    attributes: lower_attributes(item.attrs.clone(), self),
256
                }))
257
            }
258
            ast::ItemKind::Mod(ref m) => {
259
                let qualname = format!("::{}", self.tcx.node_path_str(item.id));
260

261
                let cm = self.tcx.sess.codemap();
262 263
                let filename = cm.span_to_filename(m.inner);

N
Nick Cameron 已提交
264 265
                let sub_span = self.span_utils
                    .sub_span_after_keyword(item.span, keywords::Mod);
266
                filter!(self.span_utils, sub_span, item.span, None);
267

268 269 270
                Some(Data::DefData(Def {
                    kind: DefKind::Mod,
                    id: id_from_node_id(item.id, self),
271
                    name: item.ident.to_string(),
272 273 274 275
                    qualname,
                    span: self.span_from_span(sub_span.unwrap()),
                    value: filename,
                    parent: None,
N
Nick Cameron 已提交
276 277 278 279
                    children: m.items
                        .iter()
                        .map(|i| id_from_node_id(i.id, self))
                        .collect(),
280
                    decl_id: None,
281
                    docs: self.docs_for_attrs(&item.attrs),
N
Nick Cameron 已提交
282
                    sig: sig::item_signature(item, self),
283
                    attributes: lower_attributes(item.attrs.clone(), self),
284
                }))
N
Nick Cameron 已提交
285
            }
286 287 288
            ast::ItemKind::Enum(ref def, _) => {
                let name = item.ident.to_string();
                let qualname = format!("::{}", self.tcx.node_path_str(item.id));
N
Nick Cameron 已提交
289 290
                let sub_span = self.span_utils
                    .sub_span_after_keyword(item.span, keywords::Enum);
291
                filter!(self.span_utils, sub_span, item.span, None);
N
Nick Cameron 已提交
292 293 294 295 296
                let variants_str = def.variants
                    .iter()
                    .map(|v| v.node.name.to_string())
                    .collect::<Vec<_>>()
                    .join(", ");
297 298 299 300 301 302 303 304 305 306
                let value = format!("{}::{{{}}}", name, variants_str);
                Some(Data::DefData(Def {
                    kind: DefKind::Enum,
                    id: id_from_node_id(item.id, self),
                    span: self.span_from_span(sub_span.unwrap()),
                    name,
                    qualname,
                    value,
                    parent: None,
                    children: def.variants
N
Nick Cameron 已提交
307 308 309
                        .iter()
                        .map(|v| id_from_node_id(v.node.data.id(), self))
                        .collect(),
310
                    decl_id: None,
311
                    docs: self.docs_for_attrs(&item.attrs),
N
Nick Cameron 已提交
312
                    sig: sig::item_signature(item, self),
313
                    attributes: lower_attributes(item.attrs.to_owned(), self),
314
                }))
N
Nick Cameron 已提交
315
            }
V
Vadim Petrochenkov 已提交
316
            ast::ItemKind::Impl(.., ref trait_ref, ref typ, _) => {
317
                if let ast::TyKind::Path(None, ref path) = typ.node {
N
Nick Cameron 已提交
318
                    // Common case impl for a struct or something basic.
319 320
                    if generated_code(path.span) {
                        return None;
N
Nick Cameron 已提交
321
                    }
322 323 324 325
                    let sub_span = self.span_utils.sub_span_for_type_name(path.span);
                    filter!(self.span_utils, sub_span, typ.span, None);

                    let type_data = self.lookup_ref_id(typ.id);
N
Nick Cameron 已提交
326 327 328 329 330 331 332 333 334 335 336 337
                    type_data.map(|type_data| {
                        Data::RelationData(Relation {
                            kind: RelationKind::Impl,
                            span: self.span_from_span(sub_span.unwrap()),
                            from: id_from_def_id(type_data),
                            to: trait_ref
                                .as_ref()
                                .and_then(|t| self.lookup_ref_id(t.ref_id))
                                .map(id_from_def_id)
                                .unwrap_or(null_id()),
                        })
                    })
338 339
                } else {
                    None
N
Nick Cameron 已提交
340 341
                }
            }
342 343
            _ => {
                // FIXME
344
                bug!();
345 346 347 348
            }
        }
    }

N
Nick Cameron 已提交
349
    pub fn get_field_data(&self, field: &ast::StructField, scope: NodeId) -> Option<Def> {
350
        if let Some(ident) = field.ident {
N
Nick Cameron 已提交
351
            let name = ident.to_string();
352
            let qualname = format!("::{}::{}", self.tcx.node_path_str(scope), ident);
N
Nick Cameron 已提交
353 354
            let sub_span = self.span_utils
                .sub_span_before_token(field.span, token::Colon);
355
            filter!(self.span_utils, sub_span, field.span, None);
356
            let def_id = self.tcx.hir.local_def_id(field.id);
357
            let typ = self.tcx.type_of(def_id).to_string();
N
Nick Cameron 已提交
358

359 360 361 362 363 364 365 366 367 368 369 370 371 372

            let id = id_from_node_id(field.id, self);
            let span = self.span_from_span(sub_span.unwrap());

            Some(Def {
                kind: DefKind::Field,
                id,
                span,
                name,
                qualname,
                value: typ,
                parent: Some(id_from_node_id(scope, self)),
                children: vec![],
                decl_id: None,
373
                docs: self.docs_for_attrs(&field.attrs),
374
                sig: sig::field_signature(field, self),
375
                attributes: lower_attributes(field.attrs.clone(), self),
376 377 378
            })
        } else {
            None
379 380 381
        }
    }

N
Nick Cameron 已提交
382 383
    // FIXME would be nice to take a MethodItem here, but the ast provides both
    // trait and impl flavours, so the caller must do the disassembly.
N
Nick Cameron 已提交
384
    pub fn get_method_data(&self, id: ast::NodeId, name: ast::Name, span: Span) -> Option<Def> {
N
Nick Cameron 已提交
385 386
        // The qualname for a method is the trait name or name of the struct in an impl in
        // which the method is declared in, followed by the method's name.
387
        let (qualname, parent_scope, decl_id, docs, attributes) =
N
Nick Cameron 已提交
388 389 390
            match self.tcx.impl_of_method(self.tcx.hir.local_def_id(id)) {
                Some(impl_id) => match self.tcx.hir.get_if_local(impl_id) {
                    Some(Node::NodeItem(item)) => match item.node {
V
Vadim Petrochenkov 已提交
391
                        hir::ItemImpl(.., ref ty, _) => {
N
Nick Cameron 已提交
392
                            let mut result = String::from("<");
393
                            result.push_str(&self.tcx.hir.node_to_pretty_string(ty.id));
N
Nick Cameron 已提交
394

395
                            let mut trait_id = self.tcx.trait_id_of_impl(impl_id);
396
                            let mut decl_id = None;
397
                            if let Some(def_id) = trait_id {
398 399
                                result.push_str(" as ");
                                result.push_str(&self.tcx.item_path_str(def_id));
N
Nick Cameron 已提交
400 401
                                self.tcx
                                    .associated_items(def_id)
402 403
                                    .find(|item| item.name == name)
                                    .map(|item| decl_id = Some(item.def_id));
404 405 406 407 408 409
                            } else {
                                if let Some(NodeItem(item)) = self.tcx.hir.find(id) {
                                    if let hir::ItemImpl(_, _, _, _, _, ref ty, _) = item.node {
                                        trait_id = self.lookup_ref_id(ty.id);
                                    }
                                }
N
Nick Cameron 已提交
410 411
                            }
                            result.push_str(">");
412

N
Nick Cameron 已提交
413 414 415 416 417 418 419
                            (
                                result,
                                trait_id,
                                decl_id,
                                self.docs_for_attrs(&item.attrs),
                                item.attrs.to_vec(),
                            )
N
Nick Cameron 已提交
420 421
                        }
                        _ => {
N
Nick Cameron 已提交
422 423 424 425 426 427
                            span_bug!(
                                span,
                                "Container {:?} for method {} not an impl?",
                                impl_id,
                                id
                            );
N
Nick Cameron 已提交
428
                        }
N
Nick Cameron 已提交
429 430 431 432 433 434 435 436 437
                    },
                    r => {
                        span_bug!(
                            span,
                            "Container {:?} for method {} is not a node item {:?}",
                            impl_id,
                            id,
                            r
                        );
N
Nick Cameron 已提交
438
                    }
N
Nick Cameron 已提交
439 440 441 442 443 444 445 446 447 448
                },
                None => match self.tcx.trait_of_item(self.tcx.hir.local_def_id(id)) {
                    Some(def_id) => match self.tcx.hir.get_if_local(def_id) {
                        Some(Node::NodeItem(item)) => (
                            format!("::{}", self.tcx.item_path_str(def_id)),
                            Some(def_id),
                            None,
                            self.docs_for_attrs(&item.attrs),
                            item.attrs.to_vec(),
                        ),
449
                        r => {
N
Nick Cameron 已提交
450 451 452 453 454 455 456 457
                            span_bug!(
                                span,
                                "Could not find container {:?} for \
                                 method {}, got {:?}",
                                def_id,
                                id,
                                r
                            );
N
Nick Cameron 已提交
458
                        }
N
Nick Cameron 已提交
459 460 461 462 463 464
                    },
                    None => {
                        debug!("Could not find container for method {} at {:?}", id, span);
                        // This is not necessarily a bug, if there was a compilation error,
                        // the tables we need might not exist.
                        return None;
N
Nick Cameron 已提交
465
                    }
N
Nick Cameron 已提交
466 467
                },
            };
N
Nick Cameron 已提交
468

469
        let qualname = format!("{}::{}", qualname, name);
N
Nick Cameron 已提交
470 471

        let sub_span = self.span_utils.sub_span_after_keyword(span, keywords::Fn);
472
        filter!(self.span_utils, sub_span, span, None);
473

474 475 476 477
        Some(Def {
            kind: DefKind::Method,
            id: id_from_node_id(id, self),
            span: self.span_from_span(sub_span.unwrap()),
N
Nick Cameron 已提交
478
            name: name.to_string(),
479
            qualname,
480 481
            // FIXME you get better data here by using the visitor.
            value: String::new(),
482 483 484 485
            parent: parent_scope.map(|id| id_from_def_id(id)),
            children: vec![],
            decl_id: decl_id.map(|id| id_from_def_id(id)),
            docs,
N
Nick Cameron 已提交
486
            sig: None,
487
            attributes: lower_attributes(attributes, self),
488
        })
N
Nick Cameron 已提交
489 490
    }

N
Nick Cameron 已提交
491
    pub fn get_trait_ref_data(&self, trait_ref: &ast::TraitRef) -> Option<Ref> {
492
        self.lookup_ref_id(trait_ref.ref_id).and_then(|def_id| {
493
            let span = trait_ref.path.span;
494 495 496
            if generated_code(span) {
                return None;
            }
497 498
            let sub_span = self.span_utils.sub_span_for_type_name(span).or(Some(span));
            filter!(self.span_utils, sub_span, span, None);
499 500 501 502 503
            let span = self.span_from_span(sub_span.unwrap());
            Some(Ref {
                kind: RefKind::Type,
                span,
                ref_id: id_from_def_id(def_id),
504
            })
N
Nick Cameron 已提交
505 506 507
        })
    }

N
Nick Cameron 已提交
508
    pub fn get_expr_data(&self, expr: &ast::Expr) -> Option<Data> {
509
        let hir_node = self.tcx.hir.expect_expr(expr.id);
510
        let ty = self.tables.expr_ty_adjusted_opt(&hir_node);
511 512 513
        if ty.is_none() || ty.unwrap().sty == ty::TyError {
            return None;
        }
514
        match expr.node {
515
            ast::ExprKind::Field(ref sub_ex, ident) => {
516
                let hir_node = match self.tcx.hir.find(sub_ex.id) {
517 518
                    Some(Node::NodeExpr(expr)) => expr,
                    _ => {
N
Nick Cameron 已提交
519 520 521 522 523
                        debug!(
                            "Missing or weird node for sub-expression {} in {:?}",
                            sub_ex.id,
                            expr
                        );
524 525 526
                        return None;
                    }
                };
527
                match self.tables.expr_ty_adjusted(&hir_node).sty {
528
                    ty::TyAdt(def, _) if !def.is_enum() => {
529 530
                        let f = def.struct_variant().field_named(ident.node.name);
                        let sub_span = self.span_utils.span_for_last_ident(expr.span);
531
                        filter!(self.span_utils, sub_span, expr.span, None);
532 533 534 535 536
                        let span = self.span_from_span(sub_span.unwrap());
                        return Some(Data::RefData(Ref {
                            kind: RefKind::Variable,
                            span,
                            ref_id: id_from_def_id(f.did),
537
                        }));
538
                    }
N
Nick Cameron 已提交
539
                    _ => {
540
                        debug!("Expected struct or union type, found {:?}", ty);
N
Nick Cameron 已提交
541 542
                        None
                    }
543 544
                }
            }
V
Vadim Petrochenkov 已提交
545
            ast::ExprKind::Struct(ref path, ..) => {
546
                match self.tables.expr_ty_adjusted(&hir_node).sty {
547
                    ty::TyAdt(def, _) if !def.is_enum() => {
548
                        let sub_span = self.span_utils.span_for_last_ident(path.span);
549
                        filter!(self.span_utils, sub_span, path.span, None);
550 551 552 553 554
                        let span = self.span_from_span(sub_span.unwrap());
                        Some(Data::RefData(Ref {
                            kind: RefKind::Type,
                            span,
                            ref_id: id_from_def_id(def.did),
N
Nick Cameron 已提交
555
                        }))
556 557
                    }
                    _ => {
558
                        // FIXME ty could legitimately be an enum, but then we will fail
N
Nick Cameron 已提交
559
                        // later if we try to look up the fields.
560
                        debug!("expected struct or union, found {:?}", ty);
N
Nick Cameron 已提交
561
                        None
562 563 564
                    }
                }
            }
565
            ast::ExprKind::MethodCall(ref seg, ..) => {
566
                let expr_hir_id = self.tcx.hir.definitions().node_to_hir_id(expr.id);
567 568 569 570 571 572 573
                let method_id = match self.tables.type_dependent_defs().get(expr_hir_id) {
                    Some(id) => id.def_id(),
                    None => {
                        debug!("Could not resolve method id for {:?}", expr);
                        return None;
                    }
                };
574
                let (def_id, decl_id) = match self.tcx.associated_item(method_id).container {
575
                    ty::ImplContainer(_) => (Some(method_id), None),
N
Nick Cameron 已提交
576
                    ty::TraitContainer(_) => (None, Some(method_id)),
577
                };
578 579 580
                let sub_span = seg.span;
                filter!(self.span_utils, Some(sub_span), expr.span, None);
                let span = self.span_from_span(sub_span);
581 582 583
                Some(Data::RefData(Ref {
                    kind: RefKind::Function,
                    span,
N
Nick Cameron 已提交
584 585 586 587
                    ref_id: def_id
                        .or(decl_id)
                        .map(|id| id_from_def_id(id))
                        .unwrap_or(null_id()),
588 589
                }))
            }
590
            ast::ExprKind::Path(_, ref path) => {
591
                self.get_path_data(expr.id, path).map(|d| Data::RefData(d))
N
Nick Cameron 已提交
592
            }
N
Nick Cameron 已提交
593
            _ => {
594
                // FIXME
595
                bug!();
N
Nick Cameron 已提交
596 597 598 599
            }
        }
    }

600
    pub fn get_path_def(&self, id: NodeId) -> HirDef {
601
        match self.tcx.hir.get(id) {
602 603
            Node::NodeTraitRef(tr) => tr.path.def,

N
Nick Cameron 已提交
604 605 606 607
            Node::NodeItem(&hir::Item {
                node: hir::ItemUse(ref path, _),
                ..
            }) |
608 609
            Node::NodeVisibility(&hir::Visibility::Restricted { ref path, .. }) => path.def,

N
Nick Cameron 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
            Node::NodeExpr(&hir::Expr {
                node: hir::ExprStruct(ref qpath, ..),
                ..
            }) |
            Node::NodeExpr(&hir::Expr {
                node: hir::ExprPath(ref qpath),
                ..
            }) |
            Node::NodePat(&hir::Pat {
                node: hir::PatKind::Path(ref qpath),
                ..
            }) |
            Node::NodePat(&hir::Pat {
                node: hir::PatKind::Struct(ref qpath, ..),
                ..
            }) |
            Node::NodePat(&hir::Pat {
                node: hir::PatKind::TupleStruct(ref qpath, ..),
                ..
            }) => {
630 631
                let hir_id = self.tcx.hir.node_to_hir_id(id);
                self.tables.qpath_def(qpath, hir_id)
632 633
            }

634
            Node::NodeBinding(&hir::Pat {
N
Nick Cameron 已提交
635 636
                node: hir::PatKind::Binding(_, canonical_id, ..),
                ..
637
            }) => HirDef::Local(canonical_id),
638

N
Nick Cameron 已提交
639 640 641 642 643 644 645 646 647 648 649
            Node::NodeTy(ty) => if let hir::Ty {
                node: hir::TyPath(ref qpath),
                ..
            } = *ty
            {
                match *qpath {
                    hir::QPath::Resolved(_, ref path) => path.def,
                    hir::QPath::TypeRelative(..) => {
                        let ty = hir_ty_to_ty(self.tcx, ty);
                        if let ty::TyProjection(proj) = ty.sty {
                            return HirDef::AssociatedTy(proj.item_def_id);
650
                        }
N
Nick Cameron 已提交
651
                        HirDef::Err
652 653
                    }
                }
N
Nick Cameron 已提交
654 655 656
            } else {
                HirDef::Err
            },
657

N
Nick Cameron 已提交
658
            _ => HirDef::Err,
659
        }
660
    }
661

662
    pub fn get_path_data(&self, id: NodeId, path: &ast::Path) -> Option<Ref> {
663 664 665 666 667 668 669 670 671 672 673 674 675
        // Returns true if the path is function type sugar, e.g., `Fn(A) -> B`.
        fn fn_type(path: &ast::Path) -> bool {
            if path.segments.len() != 1 {
                return false;
            }
            if let Some(ref params) = path.segments[0].parameters {
                if let ast::PathParameters::Parenthesized(_) = **params {
                    return true;
                }
            }
            false
        }

676 677 678 679
        if path.segments.is_empty() {
            return None;
        }

680
        let def = self.get_path_def(id);
681 682 683
        let last_seg = &path.segments[path.segments.len() - 1];
        let sub_span = last_seg.span;
        filter!(self.span_utils, Some(sub_span), path.span, None);
N
Nick Cameron 已提交
684
        match def {
N
Nick Cameron 已提交
685
            HirDef::Upvar(id, ..) | HirDef::Local(id) => {
686
                let span = self.span_from_span(sub_span);
687 688 689 690 691 692
                Some(Ref {
                    kind: RefKind::Variable,
                    span,
                    ref_id: id_from_node_id(id, self),
                })
            }
693 694 695 696
            HirDef::Static(..) |
            HirDef::Const(..) |
            HirDef::AssociatedConst(..) |
            HirDef::VariantCtor(..) => {
697
                let span = self.span_from_span(sub_span);
698 699 700 701 702
                Some(Ref {
                    kind: RefKind::Variable,
                    span,
                    ref_id: id_from_def_id(def.def_id()),
                })
N
Nick Cameron 已提交
703
            }
704 705 706 707
            HirDef::Trait(def_id) if fn_type(path) => {
                // Function type bounds are desugared in the parser, so we have to
                // special case them here.
                let fn_span = self.span_utils.span_for_first_ident(path.span);
N
Nick Cameron 已提交
708 709 710 711 712 713
                fn_span.map(|span| {
                    Ref {
                        kind: RefKind::Type,
                        span: self.span_from_span(span),
                        ref_id: id_from_def_id(def_id),
                    }
714 715
                })
            }
716 717 718 719 720
            HirDef::Struct(def_id) |
            HirDef::Variant(def_id, ..) |
            HirDef::Union(def_id) |
            HirDef::Enum(def_id) |
            HirDef::TyAlias(def_id) |
P
Paul Lietar 已提交
721
            HirDef::TyForeign(def_id) |
722 723 724
            HirDef::AssociatedTy(def_id) |
            HirDef::Trait(def_id) |
            HirDef::TyParam(def_id) => {
725
                let span = self.span_from_span(sub_span);
726 727 728 729 730
                Some(Ref {
                    kind: RefKind::Type,
                    span,
                    ref_id: id_from_def_id(def_id),
                })
N
Nick Cameron 已提交
731
            }
732 733 734 735
            HirDef::StructCtor(def_id, _) => {
                // This is a reference to a tuple struct where the def_id points
                // to an invisible constructor function. That is not a very useful
                // def, so adjust to point to the tuple struct itself.
736
                let span = self.span_from_span(sub_span);
737 738 739 740 741 742 743
                let parent_def_id = self.tcx.parent_def_id(def_id).unwrap();
                Some(Ref {
                    kind: RefKind::Type,
                    span,
                    ref_id: id_from_def_id(parent_def_id),
                })
            }
744
            HirDef::Method(decl_id) => {
745
                let def_id = if decl_id.is_local() {
746
                    let ti = self.tcx.associated_item(decl_id);
N
Nick Cameron 已提交
747 748
                    self.tcx
                        .associated_items(ti.container.id())
749
                        .find(|item| item.name == ti.name && item.defaultness.has_value())
750
                        .map(|item| item.def_id)
N
Nick Cameron 已提交
751 752 753
                } else {
                    None
                };
754
                let span = self.span_from_span(sub_span);
755 756 757 758 759
                Some(Ref {
                    kind: RefKind::Function,
                    span,
                    ref_id: id_from_def_id(def_id.unwrap_or(decl_id)),
                })
N
Nick Cameron 已提交
760
            }
761
            HirDef::Fn(def_id) => {
762
                let span = self.span_from_span(sub_span);
763 764 765 766 767
                Some(Ref {
                    kind: RefKind::Function,
                    span,
                    ref_id: id_from_def_id(def_id),
                })
N
Nick Cameron 已提交
768
            }
769
            HirDef::Mod(def_id) => {
770
                let span = self.span_from_span(sub_span);
771 772 773 774 775
                Some(Ref {
                    kind: RefKind::Mod,
                    span,
                    ref_id: id_from_def_id(def_id),
                })
776
            }
777 778 779 780 781 782
            HirDef::PrimTy(..) |
            HirDef::SelfTy(..) |
            HirDef::Label(..) |
            HirDef::Macro(..) |
            HirDef::GlobalAsm(..) |
            HirDef::Err => None,
N
Nick Cameron 已提交
783 784 785
        }
    }

N
Nick Cameron 已提交
786 787 788 789 790
    pub fn get_field_ref_data(
        &self,
        field_ref: &ast::Field,
        variant: &ty::VariantDef,
    ) -> Option<Ref> {
791 792 793
        let f = variant.field_named(field_ref.ident.node.name);
        // We don't really need a sub-span here, but no harm done
        let sub_span = self.span_utils.span_for_last_ident(field_ref.ident.span);
794
        filter!(self.span_utils, sub_span, field_ref.ident.span, None);
795 796 797 798 799
        let span = self.span_from_span(sub_span.unwrap());
        Some(Ref {
            kind: RefKind::Variable,
            span,
            ref_id: id_from_def_id(f.did),
800
        })
801 802
    }

803
    /// Attempt to return MacroRef for any AST node.
804 805 806
    ///
    /// For a given piece of AST defined by the supplied Span and NodeId,
    /// returns None if the node is not macro-generated or the span is malformed,
807 808
    /// else uses the expansion callsite and callee to return some MacroRef.
    pub fn get_macro_use_data(&self, span: Span) -> Option<MacroRef> {
809 810 811 812 813 814
        if !generated_code(span) {
            return None;
        }
        // Note we take care to use the source callsite/callee, to handle
        // nested expansions and ensure we only generate data for source-visible
        // macro uses.
815
        let callsite = span.source_callsite();
816
        let callsite_span = self.span_from_span(callsite);
817 818
        let callee = span.source_callee()?;
        let callee_span = callee.span?;
819 820 821 822 823 824 825 826 827

        // Ignore attribute macros, their spans are usually mangled
        if let MacroAttribute(_) = callee.format {
            return None;
        }

        // If the callee is an imported macro from an external crate, need to get
        // the source span and name from the session, as their spans are localized
        // when read in, and no longer correspond to the source.
N
Nick Cameron 已提交
828 829 830 831 832 833
        if let Some(mac) = self.tcx
            .sess
            .imported_macro_spans
            .borrow()
            .get(&callee_span)
        {
834
            let &(ref mac_name, mac_span) = mac;
835 836 837 838 839 840
            let mac_span = self.span_from_span(mac_span);
            return Some(MacroRef {
                span: callsite_span,
                qualname: mac_name.clone(), // FIXME: generate the real qualname
                callee_span: mac_span,
            });
841 842
        }

843 844 845 846 847
        let callee_span = self.span_from_span(callee_span);
        Some(MacroRef {
            span: callsite_span,
            qualname: callee.name().to_string(), // FIXME: generate the real qualname
            callee_span,
848 849 850
        })
    }

N
Nick Cameron 已提交
851
    fn lookup_ref_id(&self, ref_id: NodeId) -> Option<DefId> {
852
        match self.get_path_def(ref_id) {
853
            HirDef::PrimTy(_) | HirDef::SelfTy(..) | HirDef::Err => None,
854
            def => Some(def.def_id()),
N
Nick Cameron 已提交
855 856 857
        }
    }

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
    fn docs_for_attrs(&self, attrs: &[Attribute]) -> String {
        let mut result = String::new();

        for attr in attrs {
            if attr.check_name("doc") {
                if let Some(val) = attr.value_str() {
                    if attr.is_sugared_doc {
                        result.push_str(&strip_doc_comment_decoration(&val.as_str()));
                    } else {
                        result.push_str(&val.as_str());
                    }
                    result.push('\n');
                }
            }
        }

        if !self.config.full_docs {
            if let Some(index) = result.find("\n\n") {
                result.truncate(index);
            }
        }

        result
    }
882 883
}

884
fn make_signature(decl: &ast::FnDecl, generics: &ast::Generics) -> String {
885
    let mut sig = "fn ".to_owned();
886 887
    if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() {
        sig.push('<');
N
Nick Cameron 已提交
888 889 890 891 892 893
        sig.push_str(&generics
            .lifetimes
            .iter()
            .map(|l| l.lifetime.ident.name.to_string())
            .collect::<Vec<_>>()
            .join(", "));
894 895 896
        if !generics.lifetimes.is_empty() {
            sig.push_str(", ");
        }
N
Nick Cameron 已提交
897 898 899 900 901 902
        sig.push_str(&generics
            .ty_params
            .iter()
            .map(|l| l.ident.to_string())
            .collect::<Vec<_>>()
            .join(", "));
903 904 905
        sig.push_str("> ");
    }
    sig.push('(');
N
Nick Cameron 已提交
906 907 908 909 910
    sig.push_str(&decl.inputs
        .iter()
        .map(arg_to_string)
        .collect::<Vec<_>>()
        .join(", "));
911 912
    sig.push(')');
    match decl.output {
913
        ast::FunctionRetTy::Default(_) => sig.push_str(" -> ()"),
914 915 916 917 918 919
        ast::FunctionRetTy::Ty(ref t) => sig.push_str(&format!(" -> {}", ty_to_string(t))),
    }

    sig
}

920 921 922 923 924
// An AST visitor for collecting paths (e.g., the names of structs) and formal
// variables (idents) from patterns.
struct PathCollector<'l> {
    collected_paths: Vec<(NodeId, &'l ast::Path)>,
    collected_idents: Vec<(NodeId, ast::Ident, Span, ast::Mutability)>,
N
Nick Cameron 已提交
925 926
}

927 928 929 930 931 932
impl<'l> PathCollector<'l> {
    fn new() -> PathCollector<'l> {
        PathCollector {
            collected_paths: vec![],
            collected_idents: vec![],
        }
N
Nick Cameron 已提交
933 934 935
    }
}

936 937
impl<'l, 'a: 'l> Visitor<'a> for PathCollector<'l> {
    fn visit_pat(&mut self, p: &'a ast::Pat) {
N
Nick Cameron 已提交
938
        match p.node {
V
Vadim Petrochenkov 已提交
939
            PatKind::Struct(ref path, ..) => {
940
                self.collected_paths.push((p.id, path));
N
Nick Cameron 已提交
941
            }
N
Nick Cameron 已提交
942
            PatKind::TupleStruct(ref path, ..) | PatKind::Path(_, ref path) => {
943
                self.collected_paths.push((p.id, path));
N
Nick Cameron 已提交
944
            }
945
            PatKind::Ident(bm, ref path1, _) => {
N
Nick Cameron 已提交
946 947 948 949 950 951
                debug!(
                    "PathCollector, visit ident in pat {}: {:?} {:?}",
                    path1.node,
                    p.span,
                    path1.span
                );
N
Nick Cameron 已提交
952 953 954 955
                let immut = match bm {
                    // Even if the ref is mut, you can't change the ref, only
                    // the data pointed at, so showing the initialising expression
                    // is still worthwhile.
956
                    ast::BindingMode::ByRef(_) => ast::Mutability::Immutable,
957
                    ast::BindingMode::ByValue(mt) => mt,
N
Nick Cameron 已提交
958
                };
N
Nick Cameron 已提交
959 960
                self.collected_idents
                    .push((p.id, path1.node, path1.span, immut));
N
Nick Cameron 已提交
961 962 963 964 965 966 967
            }
            _ => {}
        }
        visit::walk_pat(self, p);
    }
}

968 969
/// Defines what to do with the results of saving the analysis.
pub trait SaveHandler {
N
Nick Cameron 已提交
970 971 972 973 974 975
    fn save<'l, 'tcx>(
        &mut self,
        save_ctxt: SaveContext<'l, 'tcx>,
        krate: &ast::Crate,
        cratename: &str,
    );
976
}
977

978 979 980
/// Dump the save-analysis results to a file.
pub struct DumpHandler<'a> {
    odir: Option<&'a Path>,
N
Nick Cameron 已提交
981
    cratename: String,
982
}
983

984
impl<'a> DumpHandler<'a> {
985
    pub fn new(odir: Option<&'a Path>, cratename: &str) -> DumpHandler<'a> {
986
        DumpHandler {
987
            odir,
N
Nick Cameron 已提交
988
            cratename: cratename.to_owned(),
989 990
        }
    }
991

992 993 994 995 996 997 998 999 1000
    fn output_file(&self, ctx: &SaveContext) -> File {
        let sess = &ctx.tcx.sess;
        let file_name = match ctx.config.output_file {
            Some(ref s) => PathBuf::from(s),
            None => {
                let mut root_path = match self.odir {
                    Some(val) => val.join("save-analysis"),
                    None => PathBuf::from("save-analysis-temp"),
                };
1001

1002 1003 1004
                if let Err(e) = std::fs::create_dir_all(&root_path) {
                    error!("Could not create directory {}: {}", root_path.display(), e);
                }
1005

N
Nick Cameron 已提交
1006 1007 1008 1009
                let executable = sess.crate_types
                    .borrow()
                    .iter()
                    .any(|ct| *ct == CrateTypeExecutable);
1010 1011 1012 1013 1014 1015 1016
                let mut out_name = if executable {
                    "".to_owned()
                } else {
                    "lib".to_owned()
                };
                out_name.push_str(&self.cratename);
                out_name.push_str(&sess.opts.cg.extra_filename);
1017
                out_name.push_str(".json");
1018
                root_path.push(&out_name);
1019

1020 1021
                root_path
            }
1022
        };
1023 1024 1025

        info!("Writing output to {}", file_name.display());

N
Nick Cameron 已提交
1026 1027 1028
        let output_file = File::create(&file_name).unwrap_or_else(
            |e| sess.fatal(&format!("Could not open {}: {}", file_name.display(), e)),
        );
1029

1030
        output_file
1031
    }
1032 1033 1034
}

impl<'a> SaveHandler for DumpHandler<'a> {
N
Nick Cameron 已提交
1035 1036 1037 1038 1039 1040
    fn save<'l, 'tcx>(
        &mut self,
        save_ctxt: SaveContext<'l, 'tcx>,
        krate: &ast::Crate,
        cratename: &str,
    ) {
1041
        let output = &mut self.output_file(&save_ctxt);
1042 1043
        let mut dumper = JsonDumper::new(output, save_ctxt.config.clone());
        let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
1044

1045 1046
        visitor.dump_crate_info(cratename, krate);
        visit::walk_crate(&mut visitor, krate);
1047
    }
1048
}
1049

1050 1051 1052 1053 1054 1055
/// Call a callback with the results of save-analysis.
pub struct CallbackHandler<'b> {
    pub callback: &'b mut FnMut(&rls_data::Analysis),
}

impl<'b> SaveHandler for CallbackHandler<'b> {
N
Nick Cameron 已提交
1056 1057 1058 1059 1060 1061
    fn save<'l, 'tcx>(
        &mut self,
        save_ctxt: SaveContext<'l, 'tcx>,
        krate: &ast::Crate,
        cratename: &str,
    ) {
1062 1063 1064 1065 1066
        // We're using the JsonDumper here because it has the format of the
        // save-analysis results that we will pass to the callback. IOW, we are
        // using the JsonDumper to collect the save-analysis results, but not
        // actually to dump them to a file. This is all a bit convoluted and
        // there is certainly a simpler design here trying to get out (FIXME).
1067 1068 1069 1070 1071
        let mut dumper = JsonDumper::with_callback(self.callback, save_ctxt.config.clone());
        let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);

        visitor.dump_crate_info(cratename, krate);
        visit::walk_crate(&mut visitor, krate);
1072 1073 1074
    }
}

N
Nick Cameron 已提交
1075 1076 1077 1078 1079 1080 1081 1082
pub fn process_crate<'l, 'tcx, H: SaveHandler>(
    tcx: TyCtxt<'l, 'tcx, 'tcx>,
    krate: &ast::Crate,
    analysis: &'l ty::CrateAnalysis,
    cratename: &str,
    config: Option<Config>,
    mut handler: H,
) {
1083 1084 1085 1086 1087
    let _ignore = tcx.dep_graph.in_ignore();

    assert!(analysis.glob_map.is_some());

    info!("Dumping crate {}", cratename);
1088

1089
    let save_ctxt = SaveContext {
1090
        tcx,
1091
        tables: &ty::TypeckTables::empty(None),
1092
        analysis,
1093
        span_utils: SpanUtils::new(&tcx.sess),
1094
        config: find_config(config),
1095
    };
1096

1097
    handler.save(save_ctxt, krate, cratename)
1098
}
1099

1100 1101 1102 1103 1104
fn find_config(supplied: Option<Config>) -> Config {
    if let Some(config) = supplied {
        return config;
    }
    match env::var_os("RUST_SAVE_ANALYSIS_CONFIG") {
N
Nick Cameron 已提交
1105 1106
        Some(config_string) => rustc_serialize::json::decode(config_string.to_str().unwrap())
            .expect("Could not deserialize save-analysis config"),
1107 1108 1109 1110
        None => Config::default(),
    }
}

1111 1112 1113 1114 1115 1116 1117
// Utility functions for the module.

// Helper function to escape quotes in a string
fn escape(s: String) -> String {
    s.replace("\"", "\"\"")
}

1118 1119
// Helper function to determine if a span came from a
// macro expansion or syntax extension.
N
Nick Cameron 已提交
1120
fn generated_code(span: Span) -> bool {
1121
    span.ctxt() != NO_EXPANSION || span == DUMMY_SP
1122
}
N
Nick Cameron 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

// DefId::index is a newtype and so the JSON serialisation is ugly. Therefore
// we use our own Id which is the same, but without the newtype.
fn id_from_def_id(id: DefId) -> rls_data::Id {
    rls_data::Id {
        krate: id.krate.as_u32(),
        index: id.index.as_u32(),
    }
}

fn id_from_node_id(id: NodeId, scx: &SaveContext) -> rls_data::Id {
1134
    let def_id = scx.tcx.hir.opt_local_def_id(id);
1135 1136 1137 1138 1139 1140 1141 1142 1143
    def_id.map(|id| id_from_def_id(id)).unwrap_or_else(|| {
        // Create a *fake* `DefId` out of a `NodeId` by subtracting the `NodeId`
        // out of the maximum u32 value. This will work unless you have *billions*
        // of definitions in a single crate (very unlikely to actually happen).
        rls_data::Id {
            krate: LOCAL_CRATE.as_u32(),
            index: !id.as_u32(),
        }
    })
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
}

fn null_id() -> rls_data::Id {
    rls_data::Id {
        krate: u32::max_value(),
        index: u32::max_value(),
    }
}

fn lower_attributes(attrs: Vec<Attribute>, scx: &SaveContext) -> Vec<rls_data::Attribute> {
    attrs.into_iter()
    // Only retain real attributes. Doc comments are lowered separately.
    .filter(|attr| attr.path != "doc")
    .map(|mut attr| {
        // Remove the surrounding '#[..]' or '#![..]' of the pretty printed
        // attribute. First normalize all inner attribute (#![..]) to outer
        // ones (#[..]), then remove the two leading and the one trailing character.
        attr.style = ast::AttrStyle::Outer;
        let value = pprust::attribute_to_string(&attr);
        // This str slicing works correctly, because the leading and trailing characters
        // are in the ASCII range and thus exactly one byte each.
        let value = value[2..value.len()-1].to_string();

        rls_data::Attribute {
1168
            value,
1169 1170 1171
            span: scx.span_from_span(attr.span),
        }
    }).collect()
N
Nick Cameron 已提交
1172
}