Importing rustc-1.48.0

Bug: 173721343
Change-Id: Ie8184d9a685086ca8a77266d6c608843f40dc9e1
diff --git a/compiler/rustc_save_analysis/Cargo.toml b/compiler/rustc_save_analysis/Cargo.toml
new file mode 100644
index 0000000..da1bed3
--- /dev/null
+++ b/compiler/rustc_save_analysis/Cargo.toml
@@ -0,0 +1,20 @@
+[package]
+authors = ["The Rust Project Developers"]
+name = "rustc_save_analysis"
+version = "0.0.0"
+edition = "2018"
+
+[dependencies]
+tracing = "0.1"
+rustc_middle = { path = "../rustc_middle" }
+rustc_ast = { path = "../rustc_ast" }
+rustc_ast_pretty = { path = "../rustc_ast_pretty" }
+rustc_data_structures = { path = "../rustc_data_structures" }
+rustc_hir = { path = "../rustc_hir" }
+rustc_hir_pretty = { path = "../rustc_hir_pretty" }
+rustc_lexer = { path = "../rustc_lexer" }
+serde_json = "1"
+rustc_session = { path = "../rustc_session" }
+rustc_span = { path = "../rustc_span" }
+rls-data = "0.19"
+rls-span = "0.5"
diff --git a/compiler/rustc_save_analysis/src/dump_visitor.rs b/compiler/rustc_save_analysis/src/dump_visitor.rs
new file mode 100644
index 0000000..ce48485
--- /dev/null
+++ b/compiler/rustc_save_analysis/src/dump_visitor.rs
@@ -0,0 +1,1517 @@
+//! Write the output of rustc's analysis to an implementor of Dump.
+//!
+//! Dumping the analysis is implemented by walking the AST and getting a bunch of
+//! info out from all over the place. We use `DefId`s to identify objects. The
+//! tricky part is getting syntactic (span, source text) and semantic (reference
+//! `DefId`s) information for parts of expressions which the compiler has discarded.
+//! E.g., in a path `foo::bar::baz`, the compiler only keeps a span for the whole
+//! path and a reference to `baz`, but we want spans and references for all three
+//! idents.
+//!
+//! SpanUtils is used to manipulate spans. In particular, to extract sub-spans
+//! from spans (e.g., the span for `bar` from the above example path).
+//! DumpVisitor walks the AST and processes it, and Dumper is used for
+//! recording the output.
+
+use rustc_ast as ast;
+use rustc_ast::walk_list;
+use rustc_data_structures::fx::FxHashSet;
+use rustc_hir as hir;
+use rustc_hir::def::{DefKind as HirDefKind, Res};
+use rustc_hir::def_id::{DefId, LocalDefId};
+use rustc_hir::intravisit::{self, Visitor};
+use rustc_hir_pretty::{bounds_to_string, fn_to_string, generic_params_to_string, ty_to_string};
+use rustc_middle::hir::map::Map;
+use rustc_middle::span_bug;
+use rustc_middle::ty::{self, DefIdTree, TyCtxt};
+use rustc_session::config::Input;
+use rustc_span::source_map::respan;
+use rustc_span::symbol::Ident;
+use rustc_span::*;
+
+use std::env;
+use std::path::Path;
+
+use crate::dumper::{Access, Dumper};
+use crate::sig;
+use crate::span_utils::SpanUtils;
+use crate::{
+    escape, generated_code, id_from_def_id, id_from_hir_id, lower_attributes, PathCollector,
+    SaveContext,
+};
+
+use rls_data::{
+    CompilationOptions, CratePreludeData, Def, DefKind, GlobalCrateId, Import, ImportKind, Ref,
+    RefKind, Relation, RelationKind, SpanData,
+};
+
+use tracing::{debug, error};
+
+macro_rules! down_cast_data {
+    ($id:ident, $kind:ident, $sp:expr) => {
+        let $id = if let super::Data::$kind(data) = $id {
+            data
+        } else {
+            span_bug!($sp, "unexpected data kind: {:?}", $id);
+        };
+    };
+}
+
+macro_rules! access_from {
+    ($save_ctxt:expr, $item:expr, $id:expr) => {
+        Access {
+            public: $item.vis.node.is_pub(),
+            reachable: $save_ctxt.access_levels.is_reachable($id),
+        }
+    };
+}
+
+macro_rules! access_from_vis {
+    ($save_ctxt:expr, $vis:expr, $id:expr) => {
+        Access { public: $vis.node.is_pub(), reachable: $save_ctxt.access_levels.is_reachable($id) }
+    };
+}
+
+pub struct DumpVisitor<'tcx> {
+    pub save_ctxt: SaveContext<'tcx>,
+    tcx: TyCtxt<'tcx>,
+    dumper: Dumper,
+
+    span: SpanUtils<'tcx>,
+    // Set of macro definition (callee) spans, and the set
+    // of macro use (callsite) spans. We store these to ensure
+    // we only write one macro def per unique macro definition, and
+    // one macro use per unique callsite span.
+    // mac_defs: FxHashSet<Span>,
+    // macro_calls: FxHashSet<Span>,
+}
+
+impl<'tcx> DumpVisitor<'tcx> {
+    pub fn new(save_ctxt: SaveContext<'tcx>) -> DumpVisitor<'tcx> {
+        let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
+        let dumper = Dumper::new(save_ctxt.config.clone());
+        DumpVisitor {
+            tcx: save_ctxt.tcx,
+            save_ctxt,
+            dumper,
+            span: span_utils,
+            // mac_defs: FxHashSet::default(),
+            // macro_calls: FxHashSet::default(),
+        }
+    }
+
+    pub fn analysis(&self) -> &rls_data::Analysis {
+        self.dumper.analysis()
+    }
+
+    fn nest_typeck_results<F>(&mut self, item_def_id: LocalDefId, f: F)
+    where
+        F: FnOnce(&mut Self),
+    {
+        let typeck_results = if self.tcx.has_typeck_results(item_def_id) {
+            Some(self.tcx.typeck(item_def_id))
+        } else {
+            None
+        };
+
+        let old_maybe_typeck_results = self.save_ctxt.maybe_typeck_results;
+        self.save_ctxt.maybe_typeck_results = typeck_results;
+        f(self);
+        self.save_ctxt.maybe_typeck_results = old_maybe_typeck_results;
+    }
+
+    fn span_from_span(&self, span: Span) -> SpanData {
+        self.save_ctxt.span_from_span(span)
+    }
+
+    fn lookup_def_id(&self, ref_id: hir::HirId) -> Option<DefId> {
+        self.save_ctxt.lookup_def_id(ref_id)
+    }
+
+    pub fn dump_crate_info(&mut self, name: &str, krate: &hir::Crate<'_>) {
+        let source_file = self.tcx.sess.local_crate_source_file.as_ref();
+        let crate_root = source_file.map(|source_file| {
+            let source_file = Path::new(source_file);
+            match source_file.file_name() {
+                Some(_) => source_file.parent().unwrap().display(),
+                None => source_file.display(),
+            }
+            .to_string()
+        });
+
+        let data = CratePreludeData {
+            crate_id: GlobalCrateId {
+                name: name.into(),
+                disambiguator: self
+                    .tcx
+                    .sess
+                    .local_crate_disambiguator()
+                    .to_fingerprint()
+                    .as_value(),
+            },
+            crate_root: crate_root.unwrap_or_else(|| "<no source>".to_owned()),
+            external_crates: self.save_ctxt.get_external_crates(),
+            span: self.span_from_span(krate.item.span),
+        };
+
+        self.dumper.crate_prelude(data);
+    }
+
+    pub fn dump_compilation_options(&mut self, input: &Input, crate_name: &str) {
+        // Apply possible `remap-path-prefix` remapping to the input source file
+        // (and don't include remapping args anymore)
+        let (program, arguments) = {
+            let remap_arg_indices = {
+                let mut indices = FxHashSet::default();
+                // Args are guaranteed to be valid UTF-8 (checked early)
+                for (i, e) in env::args().enumerate() {
+                    if e.starts_with("--remap-path-prefix=") {
+                        indices.insert(i);
+                    } else if e == "--remap-path-prefix" {
+                        indices.insert(i);
+                        indices.insert(i + 1);
+                    }
+                }
+                indices
+            };
+
+            let mut args = env::args()
+                .enumerate()
+                .filter(|(i, _)| !remap_arg_indices.contains(i))
+                .map(|(_, arg)| match input {
+                    Input::File(ref path) if path == Path::new(&arg) => {
+                        let mapped = &self.tcx.sess.local_crate_source_file;
+                        mapped.as_ref().unwrap().to_string_lossy().into()
+                    }
+                    _ => arg,
+                });
+
+            (args.next().unwrap(), args.collect())
+        };
+
+        let data = CompilationOptions {
+            directory: self.tcx.sess.working_dir.0.clone(),
+            program,
+            arguments,
+            output: self.save_ctxt.compilation_output(crate_name),
+        };
+
+        self.dumper.compilation_opts(data);
+    }
+
+    fn write_segments(&mut self, segments: impl IntoIterator<Item = &'tcx hir::PathSegment<'tcx>>) {
+        for seg in segments {
+            if let Some(data) = self.save_ctxt.get_path_segment_data(seg) {
+                self.dumper.dump_ref(data);
+            }
+        }
+    }
+
+    fn write_sub_paths(&mut self, path: &'tcx hir::Path<'tcx>) {
+        self.write_segments(path.segments)
+    }
+
+    // As write_sub_paths, but does not process the last ident in the path (assuming it
+    // will be processed elsewhere). See note on write_sub_paths about global.
+    fn write_sub_paths_truncated(&mut self, path: &'tcx hir::Path<'tcx>) {
+        if let [segments @ .., _] = path.segments {
+            self.write_segments(segments)
+        }
+    }
+
+    fn process_formals(&mut self, formals: &'tcx [hir::Param<'tcx>], qualname: &str) {
+        for arg in formals {
+            self.visit_pat(&arg.pat);
+            let mut collector = PathCollector::new(self.tcx);
+            collector.visit_pat(&arg.pat);
+
+            for (hir_id, ident, ..) in collector.collected_idents {
+                let typ = match self.save_ctxt.typeck_results().node_type_opt(hir_id) {
+                    Some(s) => s.to_string(),
+                    None => continue,
+                };
+                if !self.span.filter_generated(ident.span) {
+                    let id = id_from_hir_id(hir_id, &self.save_ctxt);
+                    let span = self.span_from_span(ident.span);
+
+                    self.dumper.dump_def(
+                        &Access { public: false, reachable: false },
+                        Def {
+                            kind: DefKind::Local,
+                            id,
+                            span,
+                            name: ident.to_string(),
+                            qualname: format!("{}::{}", qualname, ident.to_string()),
+                            value: typ,
+                            parent: None,
+                            children: vec![],
+                            decl_id: None,
+                            docs: String::new(),
+                            sig: None,
+                            attributes: vec![],
+                        },
+                    );
+                }
+            }
+        }
+    }
+
+    fn process_method(
+        &mut self,
+        sig: &'tcx hir::FnSig<'tcx>,
+        body: Option<hir::BodyId>,
+        hir_id: hir::HirId,
+        ident: Ident,
+        generics: &'tcx hir::Generics<'tcx>,
+        vis: &hir::Visibility<'tcx>,
+        span: Span,
+    ) {
+        debug!("process_method: {}:{}", hir_id, ident);
+
+        let map = &self.tcx.hir();
+        self.nest_typeck_results(map.local_def_id(hir_id), |v| {
+            if let Some(mut method_data) = v.save_ctxt.get_method_data(hir_id, ident, span) {
+                if let Some(body) = body {
+                    v.process_formals(map.body(body).params, &method_data.qualname);
+                }
+                v.process_generic_params(&generics, &method_data.qualname, hir_id);
+
+                method_data.value =
+                    fn_to_string(sig.decl, sig.header, Some(ident.name), generics, vis, &[], None);
+                method_data.sig = sig::method_signature(hir_id, ident, generics, sig, &v.save_ctxt);
+
+                v.dumper.dump_def(&access_from_vis!(v.save_ctxt, vis, hir_id), method_data);
+            }
+
+            // walk arg and return types
+            for arg in sig.decl.inputs {
+                v.visit_ty(arg);
+            }
+
+            if let hir::FnRetTy::Return(ref ret_ty) = sig.decl.output {
+                v.visit_ty(ret_ty)
+            }
+
+            // walk the fn body
+            if let Some(body) = body {
+                v.visit_expr(&map.body(body).value);
+            }
+        });
+    }
+
+    fn process_struct_field_def(
+        &mut self,
+        field: &'tcx hir::StructField<'tcx>,
+        parent_id: hir::HirId,
+    ) {
+        let field_data = self.save_ctxt.get_field_data(field, parent_id);
+        if let Some(field_data) = field_data {
+            self.dumper.dump_def(&access_from!(self.save_ctxt, field, field.hir_id), field_data);
+        }
+    }
+
+    // Dump generic params bindings, then visit_generics
+    fn process_generic_params(
+        &mut self,
+        generics: &'tcx hir::Generics<'tcx>,
+        prefix: &str,
+        id: hir::HirId,
+    ) {
+        for param in generics.params {
+            match param.kind {
+                hir::GenericParamKind::Lifetime { .. } => {}
+                hir::GenericParamKind::Type { .. } => {
+                    let param_ss = param.name.ident().span;
+                    let name = escape(self.span.snippet(param_ss));
+                    // Append $id to name to make sure each one is unique.
+                    let qualname = format!("{}::{}${}", prefix, name, id);
+                    if !self.span.filter_generated(param_ss) {
+                        let id = id_from_hir_id(param.hir_id, &self.save_ctxt);
+                        let span = self.span_from_span(param_ss);
+
+                        self.dumper.dump_def(
+                            &Access { public: false, reachable: false },
+                            Def {
+                                kind: DefKind::Type,
+                                id,
+                                span,
+                                name,
+                                qualname,
+                                value: String::new(),
+                                parent: None,
+                                children: vec![],
+                                decl_id: None,
+                                docs: String::new(),
+                                sig: None,
+                                attributes: vec![],
+                            },
+                        );
+                    }
+                }
+                hir::GenericParamKind::Const { .. } => {}
+            }
+        }
+        self.visit_generics(generics);
+    }
+
+    fn process_fn(
+        &mut self,
+        item: &'tcx hir::Item<'tcx>,
+        decl: &'tcx hir::FnDecl<'tcx>,
+        _header: &'tcx hir::FnHeader,
+        ty_params: &'tcx hir::Generics<'tcx>,
+        body: hir::BodyId,
+    ) {
+        let map = &self.tcx.hir();
+        self.nest_typeck_results(map.local_def_id(item.hir_id), |v| {
+            let body = map.body(body);
+            if let Some(fn_data) = v.save_ctxt.get_item_data(item) {
+                down_cast_data!(fn_data, DefData, item.span);
+                v.process_formals(body.params, &fn_data.qualname);
+                v.process_generic_params(ty_params, &fn_data.qualname, item.hir_id);
+
+                v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.hir_id), fn_data);
+            }
+
+            for arg in decl.inputs {
+                v.visit_ty(arg)
+            }
+
+            if let hir::FnRetTy::Return(ref ret_ty) = decl.output {
+                v.visit_ty(ret_ty)
+            }
+
+            v.visit_expr(&body.value);
+        });
+    }
+
+    fn process_static_or_const_item(
+        &mut self,
+        item: &'tcx hir::Item<'tcx>,
+        typ: &'tcx hir::Ty<'tcx>,
+        expr: &'tcx hir::Expr<'tcx>,
+    ) {
+        self.nest_typeck_results(self.tcx.hir().local_def_id(item.hir_id), |v| {
+            if let Some(var_data) = v.save_ctxt.get_item_data(item) {
+                down_cast_data!(var_data, DefData, item.span);
+                v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.hir_id), var_data);
+            }
+            v.visit_ty(&typ);
+            v.visit_expr(expr);
+        });
+    }
+
+    fn process_assoc_const(
+        &mut self,
+        hir_id: hir::HirId,
+        ident: Ident,
+        typ: &'tcx hir::Ty<'tcx>,
+        expr: Option<&'tcx hir::Expr<'tcx>>,
+        parent_id: DefId,
+        vis: &hir::Visibility<'tcx>,
+        attrs: &'tcx [ast::Attribute],
+    ) {
+        let qualname =
+            format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(hir_id).to_def_id()));
+
+        if !self.span.filter_generated(ident.span) {
+            let sig = sig::assoc_const_signature(hir_id, ident.name, typ, expr, &self.save_ctxt);
+            let span = self.span_from_span(ident.span);
+
+            self.dumper.dump_def(
+                &access_from_vis!(self.save_ctxt, vis, hir_id),
+                Def {
+                    kind: DefKind::Const,
+                    id: id_from_hir_id(hir_id, &self.save_ctxt),
+                    span,
+                    name: ident.name.to_string(),
+                    qualname,
+                    value: ty_to_string(&typ),
+                    parent: Some(id_from_def_id(parent_id)),
+                    children: vec![],
+                    decl_id: None,
+                    docs: self.save_ctxt.docs_for_attrs(attrs),
+                    sig,
+                    attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
+                },
+            );
+        }
+
+        // walk type and init value
+        self.nest_typeck_results(self.tcx.hir().local_def_id(hir_id), |v| {
+            v.visit_ty(typ);
+            if let Some(expr) = expr {
+                v.visit_expr(expr);
+            }
+        });
+    }
+
+    // FIXME tuple structs should generate tuple-specific data.
+    fn process_struct(
+        &mut self,
+        item: &'tcx hir::Item<'tcx>,
+        def: &'tcx hir::VariantData<'tcx>,
+        ty_params: &'tcx hir::Generics<'tcx>,
+    ) {
+        debug!("process_struct {:?} {:?}", item, item.span);
+        let name = item.ident.to_string();
+        let qualname = format!(
+            "::{}",
+            self.tcx.def_path_str(self.tcx.hir().local_def_id(item.hir_id).to_def_id())
+        );
+
+        let kind = match item.kind {
+            hir::ItemKind::Struct(_, _) => DefKind::Struct,
+            hir::ItemKind::Union(_, _) => DefKind::Union,
+            _ => unreachable!(),
+        };
+
+        let (value, fields) = match item.kind {
+            hir::ItemKind::Struct(hir::VariantData::Struct(ref fields, ..), ..)
+            | hir::ItemKind::Union(hir::VariantData::Struct(ref fields, ..), ..) => {
+                let include_priv_fields = !self.save_ctxt.config.pub_only;
+                let fields_str = fields
+                    .iter()
+                    .filter_map(|f| {
+                        if include_priv_fields || f.vis.node.is_pub() {
+                            Some(f.ident.to_string())
+                        } else {
+                            None
+                        }
+                    })
+                    .collect::<Vec<_>>()
+                    .join(", ");
+                let value = format!("{} {{ {} }}", name, fields_str);
+                (value, fields.iter().map(|f| id_from_hir_id(f.hir_id, &self.save_ctxt)).collect())
+            }
+            _ => (String::new(), vec![]),
+        };
+
+        if !self.span.filter_generated(item.ident.span) {
+            let span = self.span_from_span(item.ident.span);
+            self.dumper.dump_def(
+                &access_from!(self.save_ctxt, item, item.hir_id),
+                Def {
+                    kind,
+                    id: id_from_hir_id(item.hir_id, &self.save_ctxt),
+                    span,
+                    name,
+                    qualname: qualname.clone(),
+                    value,
+                    parent: None,
+                    children: fields,
+                    decl_id: None,
+                    docs: self.save_ctxt.docs_for_attrs(&item.attrs),
+                    sig: sig::item_signature(item, &self.save_ctxt),
+                    attributes: lower_attributes(item.attrs.to_vec(), &self.save_ctxt),
+                },
+            );
+        }
+
+        self.nest_typeck_results(self.tcx.hir().local_def_id(item.hir_id), |v| {
+            for field in def.fields() {
+                v.process_struct_field_def(field, item.hir_id);
+                v.visit_ty(&field.ty);
+            }
+
+            v.process_generic_params(ty_params, &qualname, item.hir_id);
+        });
+    }
+
+    fn process_enum(
+        &mut self,
+        item: &'tcx hir::Item<'tcx>,
+        enum_definition: &'tcx hir::EnumDef<'tcx>,
+        ty_params: &'tcx hir::Generics<'tcx>,
+    ) {
+        let enum_data = self.save_ctxt.get_item_data(item);
+        let enum_data = match enum_data {
+            None => return,
+            Some(data) => data,
+        };
+        down_cast_data!(enum_data, DefData, item.span);
+
+        let access = access_from!(self.save_ctxt, item, item.hir_id);
+
+        for variant in enum_definition.variants {
+            let name = variant.ident.name.to_string();
+            let qualname = format!("{}::{}", enum_data.qualname, name);
+            let name_span = variant.ident.span;
+
+            match variant.data {
+                hir::VariantData::Struct(ref fields, ..) => {
+                    let fields_str =
+                        fields.iter().map(|f| f.ident.to_string()).collect::<Vec<_>>().join(", ");
+                    let value = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str);
+                    if !self.span.filter_generated(name_span) {
+                        let span = self.span_from_span(name_span);
+                        let id = id_from_hir_id(variant.id, &self.save_ctxt);
+                        let parent = Some(id_from_hir_id(item.hir_id, &self.save_ctxt));
+
+                        self.dumper.dump_def(
+                            &access,
+                            Def {
+                                kind: DefKind::StructVariant,
+                                id,
+                                span,
+                                name,
+                                qualname,
+                                value,
+                                parent,
+                                children: vec![],
+                                decl_id: None,
+                                docs: self.save_ctxt.docs_for_attrs(&variant.attrs),
+                                sig: sig::variant_signature(variant, &self.save_ctxt),
+                                attributes: lower_attributes(
+                                    variant.attrs.to_vec(),
+                                    &self.save_ctxt,
+                                ),
+                            },
+                        );
+                    }
+                }
+                ref v => {
+                    let mut value = format!("{}::{}", enum_data.name, name);
+                    if let &hir::VariantData::Tuple(ref fields, _) = v {
+                        value.push('(');
+                        value.push_str(
+                            &fields
+                                .iter()
+                                .map(|f| ty_to_string(&f.ty))
+                                .collect::<Vec<_>>()
+                                .join(", "),
+                        );
+                        value.push(')');
+                    }
+                    if !self.span.filter_generated(name_span) {
+                        let span = self.span_from_span(name_span);
+                        let id = id_from_hir_id(variant.id, &self.save_ctxt);
+                        let parent = Some(id_from_hir_id(item.hir_id, &self.save_ctxt));
+
+                        self.dumper.dump_def(
+                            &access,
+                            Def {
+                                kind: DefKind::TupleVariant,
+                                id,
+                                span,
+                                name,
+                                qualname,
+                                value,
+                                parent,
+                                children: vec![],
+                                decl_id: None,
+                                docs: self.save_ctxt.docs_for_attrs(&variant.attrs),
+                                sig: sig::variant_signature(variant, &self.save_ctxt),
+                                attributes: lower_attributes(
+                                    variant.attrs.to_vec(),
+                                    &self.save_ctxt,
+                                ),
+                            },
+                        );
+                    }
+                }
+            }
+
+            for field in variant.data.fields() {
+                self.process_struct_field_def(field, variant.id);
+                self.visit_ty(field.ty);
+            }
+        }
+        self.process_generic_params(ty_params, &enum_data.qualname, item.hir_id);
+        self.dumper.dump_def(&access, enum_data);
+    }
+
+    fn process_impl(
+        &mut self,
+        item: &'tcx hir::Item<'tcx>,
+        generics: &'tcx hir::Generics<'tcx>,
+        trait_ref: &'tcx Option<hir::TraitRef<'tcx>>,
+        typ: &'tcx hir::Ty<'tcx>,
+        impl_items: &'tcx [hir::ImplItemRef<'tcx>],
+    ) {
+        if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
+            if !self.span.filter_generated(item.span) {
+                if let super::Data::RelationData(rel, imp) = impl_data {
+                    self.dumper.dump_relation(rel);
+                    self.dumper.dump_impl(imp);
+                } else {
+                    span_bug!(item.span, "unexpected data kind: {:?}", impl_data);
+                }
+            }
+        }
+
+        let map = &self.tcx.hir();
+        self.nest_typeck_results(map.local_def_id(item.hir_id), |v| {
+            v.visit_ty(&typ);
+            if let &Some(ref trait_ref) = trait_ref {
+                v.process_path(trait_ref.hir_ref_id, &hir::QPath::Resolved(None, &trait_ref.path));
+            }
+            v.process_generic_params(generics, "", item.hir_id);
+            for impl_item in impl_items {
+                v.process_impl_item(
+                    map.impl_item(impl_item.id),
+                    map.local_def_id(item.hir_id).to_def_id(),
+                );
+            }
+        });
+    }
+
+    fn process_trait(
+        &mut self,
+        item: &'tcx hir::Item<'tcx>,
+        generics: &'tcx hir::Generics<'tcx>,
+        trait_refs: hir::GenericBounds<'tcx>,
+        methods: &'tcx [hir::TraitItemRef],
+    ) {
+        let name = item.ident.to_string();
+        let qualname = format!(
+            "::{}",
+            self.tcx.def_path_str(self.tcx.hir().local_def_id(item.hir_id).to_def_id())
+        );
+        let mut val = name.clone();
+        if !generics.params.is_empty() {
+            val.push_str(&generic_params_to_string(generics.params));
+        }
+        if !trait_refs.is_empty() {
+            val.push_str(": ");
+            val.push_str(&bounds_to_string(trait_refs));
+        }
+        if !self.span.filter_generated(item.ident.span) {
+            let id = id_from_hir_id(item.hir_id, &self.save_ctxt);
+            let span = self.span_from_span(item.ident.span);
+            let children =
+                methods.iter().map(|i| id_from_hir_id(i.id.hir_id, &self.save_ctxt)).collect();
+            self.dumper.dump_def(
+                &access_from!(self.save_ctxt, item, item.hir_id),
+                Def {
+                    kind: DefKind::Trait,
+                    id,
+                    span,
+                    name,
+                    qualname: qualname.clone(),
+                    value: val,
+                    parent: None,
+                    children,
+                    decl_id: None,
+                    docs: self.save_ctxt.docs_for_attrs(&item.attrs),
+                    sig: sig::item_signature(item, &self.save_ctxt),
+                    attributes: lower_attributes(item.attrs.to_vec(), &self.save_ctxt),
+                },
+            );
+        }
+
+        // super-traits
+        for super_bound in trait_refs.iter() {
+            let (def_id, sub_span) = match *super_bound {
+                hir::GenericBound::Trait(ref trait_ref, _) => (
+                    self.lookup_def_id(trait_ref.trait_ref.hir_ref_id),
+                    trait_ref.trait_ref.path.segments.last().unwrap().ident.span,
+                ),
+                hir::GenericBound::LangItemTrait(lang_item, span, _, _) => {
+                    (Some(self.tcx.require_lang_item(lang_item, Some(span))), span)
+                }
+                hir::GenericBound::Outlives(..) => continue,
+            };
+
+            if let Some(id) = def_id {
+                if !self.span.filter_generated(sub_span) {
+                    let span = self.span_from_span(sub_span);
+                    self.dumper.dump_ref(Ref {
+                        kind: RefKind::Type,
+                        span: span.clone(),
+                        ref_id: id_from_def_id(id),
+                    });
+
+                    self.dumper.dump_relation(Relation {
+                        kind: RelationKind::SuperTrait,
+                        span,
+                        from: id_from_def_id(id),
+                        to: id_from_hir_id(item.hir_id, &self.save_ctxt),
+                    });
+                }
+            }
+        }
+
+        // walk generics and methods
+        self.process_generic_params(generics, &qualname, item.hir_id);
+        for method in methods {
+            let map = &self.tcx.hir();
+            self.process_trait_item(
+                map.trait_item(method.id),
+                map.local_def_id(item.hir_id).to_def_id(),
+            )
+        }
+    }
+
+    // `item` is the module in question, represented as an( item.
+    fn process_mod(&mut self, item: &'tcx hir::Item<'tcx>) {
+        if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
+            down_cast_data!(mod_data, DefData, item.span);
+            self.dumper.dump_def(&access_from!(self.save_ctxt, item, item.hir_id), mod_data);
+        }
+    }
+
+    fn dump_path_ref(&mut self, id: hir::HirId, path: &hir::QPath<'tcx>) {
+        let path_data = self.save_ctxt.get_path_data(id, path);
+        if let Some(path_data) = path_data {
+            self.dumper.dump_ref(path_data);
+        }
+    }
+
+    fn dump_path_segment_ref(&mut self, id: hir::HirId, segment: &hir::PathSegment<'tcx>) {
+        let segment_data = self.save_ctxt.get_path_segment_data_with_id(segment, id);
+        if let Some(segment_data) = segment_data {
+            self.dumper.dump_ref(segment_data);
+        }
+    }
+
+    fn process_path(&mut self, id: hir::HirId, path: &hir::QPath<'tcx>) {
+        if self.span.filter_generated(path.span()) {
+            return;
+        }
+        self.dump_path_ref(id, path);
+
+        // Type arguments
+        let segments = match path {
+            hir::QPath::Resolved(ty, path) => {
+                if let Some(ty) = ty {
+                    self.visit_ty(ty);
+                }
+                path.segments
+            }
+            hir::QPath::TypeRelative(ty, segment) => {
+                self.visit_ty(ty);
+                std::slice::from_ref(*segment)
+            }
+            hir::QPath::LangItem(..) => return,
+        };
+        for seg in segments {
+            if let Some(ref generic_args) = seg.args {
+                for arg in generic_args.args {
+                    if let hir::GenericArg::Type(ref ty) = arg {
+                        self.visit_ty(ty);
+                    }
+                }
+            }
+        }
+
+        if let hir::QPath::Resolved(_, path) = path {
+            self.write_sub_paths_truncated(path);
+        }
+    }
+
+    fn process_struct_lit(
+        &mut self,
+        ex: &'tcx hir::Expr<'tcx>,
+        path: &'tcx hir::QPath<'tcx>,
+        fields: &'tcx [hir::Field<'tcx>],
+        variant: &'tcx ty::VariantDef,
+        base: Option<&'tcx hir::Expr<'tcx>>,
+    ) {
+        if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
+            if let hir::QPath::Resolved(_, path) = path {
+                self.write_sub_paths_truncated(path);
+            }
+            down_cast_data!(struct_lit_data, RefData, ex.span);
+            if !generated_code(ex.span) {
+                self.dumper.dump_ref(struct_lit_data);
+            }
+
+            for field in fields {
+                if let Some(field_data) = self.save_ctxt.get_field_ref_data(field, variant) {
+                    self.dumper.dump_ref(field_data);
+                }
+
+                self.visit_expr(&field.expr)
+            }
+        }
+
+        walk_list!(self, visit_expr, base);
+    }
+
+    fn process_method_call(
+        &mut self,
+        ex: &'tcx hir::Expr<'tcx>,
+        seg: &'tcx hir::PathSegment<'tcx>,
+        args: &'tcx [hir::Expr<'tcx>],
+    ) {
+        debug!("process_method_call {:?} {:?}", ex, ex.span);
+        if let Some(mcd) = self.save_ctxt.get_expr_data(ex) {
+            down_cast_data!(mcd, RefData, ex.span);
+            if !generated_code(ex.span) {
+                self.dumper.dump_ref(mcd);
+            }
+        }
+
+        // Explicit types in the turbo-fish.
+        if let Some(generic_args) = seg.args {
+            for arg in generic_args.args {
+                if let hir::GenericArg::Type(ty) = arg {
+                    self.visit_ty(&ty)
+                };
+            }
+        }
+
+        // walk receiver and args
+        walk_list!(self, visit_expr, args);
+    }
+
+    fn process_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
+        match p.kind {
+            hir::PatKind::Struct(ref _path, fields, _) => {
+                // FIXME do something with _path?
+                let adt = match self.save_ctxt.typeck_results().node_type_opt(p.hir_id) {
+                    Some(ty) if ty.ty_adt_def().is_some() => ty.ty_adt_def().unwrap(),
+                    _ => {
+                        intravisit::walk_pat(self, p);
+                        return;
+                    }
+                };
+                let variant = adt.variant_of_res(self.save_ctxt.get_path_res(p.hir_id));
+
+                for field in fields {
+                    if let Some(index) = self.tcx.find_field_index(field.ident, variant) {
+                        if !self.span.filter_generated(field.ident.span) {
+                            let span = self.span_from_span(field.ident.span);
+                            self.dumper.dump_ref(Ref {
+                                kind: RefKind::Variable,
+                                span,
+                                ref_id: id_from_def_id(variant.fields[index].did),
+                            });
+                        }
+                    }
+                    self.visit_pat(&field.pat);
+                }
+            }
+            _ => intravisit::walk_pat(self, p),
+        }
+    }
+
+    fn process_var_decl(&mut self, pat: &'tcx hir::Pat<'tcx>) {
+        // The pattern could declare multiple new vars,
+        // we must walk the pattern and collect them all.
+        let mut collector = PathCollector::new(self.tcx);
+        collector.visit_pat(&pat);
+        self.visit_pat(&pat);
+
+        // Process collected paths.
+        for (id, ident, _) in collector.collected_idents {
+            let res = self.save_ctxt.get_path_res(id);
+            match res {
+                Res::Local(hir_id) => {
+                    let typ = self
+                        .save_ctxt
+                        .typeck_results()
+                        .node_type_opt(hir_id)
+                        .map(|t| t.to_string())
+                        .unwrap_or_default();
+
+                    // Rust uses the id of the pattern for var lookups, so we'll use it too.
+                    if !self.span.filter_generated(ident.span) {
+                        let qualname = format!("{}${}", ident.to_string(), hir_id);
+                        let id = id_from_hir_id(hir_id, &self.save_ctxt);
+                        let span = self.span_from_span(ident.span);
+
+                        self.dumper.dump_def(
+                            &Access { public: false, reachable: false },
+                            Def {
+                                kind: DefKind::Local,
+                                id,
+                                span,
+                                name: ident.to_string(),
+                                qualname,
+                                value: typ,
+                                parent: None,
+                                children: vec![],
+                                decl_id: None,
+                                docs: String::new(),
+                                sig: None,
+                                attributes: vec![],
+                            },
+                        );
+                    }
+                }
+                Res::Def(
+                    HirDefKind::Ctor(..)
+                    | HirDefKind::Const
+                    | HirDefKind::AssocConst
+                    | HirDefKind::Struct
+                    | HirDefKind::Variant
+                    | HirDefKind::TyAlias
+                    | HirDefKind::AssocTy,
+                    _,
+                )
+                | Res::SelfTy(..) => {
+                    self.dump_path_segment_ref(id, &hir::PathSegment::from_ident(ident));
+                }
+                def => {
+                    error!("unexpected definition kind when processing collected idents: {:?}", def)
+                }
+            }
+        }
+
+        for (id, ref path) in collector.collected_paths {
+            self.process_path(id, path);
+        }
+    }
+
+    /// Extracts macro use and definition information from the AST node defined
+    /// by the given NodeId, using the expansion information from the node's
+    /// span.
+    ///
+    /// If the span is not macro-generated, do nothing, else use callee and
+    /// callsite spans to record macro definition and use data, using the
+    /// mac_uses and mac_defs sets to prevent multiples.
+    fn process_macro_use(&mut self, _span: Span) {
+        // FIXME if we're not dumping the defs (see below), there is no point
+        // dumping refs either.
+        // let source_span = span.source_callsite();
+        // if !self.macro_calls.insert(source_span) {
+        //     return;
+        // }
+
+        // let data = match self.save_ctxt.get_macro_use_data(span) {
+        //     None => return,
+        //     Some(data) => data,
+        // };
+
+        // self.dumper.macro_use(data);
+
+        // FIXME write the macro def
+        // let mut hasher = DefaultHasher::new();
+        // data.callee_span.hash(&mut hasher);
+        // let hash = hasher.finish();
+        // let qualname = format!("{}::{}", data.name, hash);
+        // Don't write macro definition for imported macros
+        // if !self.mac_defs.contains(&data.callee_span)
+        //     && !data.imported {
+        //     self.mac_defs.insert(data.callee_span);
+        //     if let Some(sub_span) = self.span.span_for_macro_def_name(data.callee_span) {
+        //         self.dumper.macro_data(MacroData {
+        //             span: sub_span,
+        //             name: data.name.clone(),
+        //             qualname: qualname.clone(),
+        //             // FIXME where do macro docs come from?
+        //             docs: String::new(),
+        //         }.lower(self.tcx));
+        //     }
+        // }
+    }
+
+    fn process_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>, trait_id: DefId) {
+        self.process_macro_use(trait_item.span);
+        let vis_span = trait_item.span.shrink_to_lo();
+        match trait_item.kind {
+            hir::TraitItemKind::Const(ref ty, body) => {
+                let body = body.map(|b| &self.tcx.hir().body(b).value);
+                let respan = respan(vis_span, hir::VisibilityKind::Public);
+                self.process_assoc_const(
+                    trait_item.hir_id,
+                    trait_item.ident,
+                    &ty,
+                    body,
+                    trait_id,
+                    &respan,
+                    &trait_item.attrs,
+                );
+            }
+            hir::TraitItemKind::Fn(ref sig, ref trait_fn) => {
+                let body =
+                    if let hir::TraitFn::Provided(body) = trait_fn { Some(*body) } else { None };
+                let respan = respan(vis_span, hir::VisibilityKind::Public);
+                self.process_method(
+                    sig,
+                    body,
+                    trait_item.hir_id,
+                    trait_item.ident,
+                    &trait_item.generics,
+                    &respan,
+                    trait_item.span,
+                );
+            }
+            hir::TraitItemKind::Type(ref bounds, ref default_ty) => {
+                // FIXME do something with _bounds (for type refs)
+                let name = trait_item.ident.name.to_string();
+                let qualname = format!(
+                    "::{}",
+                    self.tcx
+                        .def_path_str(self.tcx.hir().local_def_id(trait_item.hir_id).to_def_id())
+                );
+
+                if !self.span.filter_generated(trait_item.ident.span) {
+                    let span = self.span_from_span(trait_item.ident.span);
+                    let id = id_from_hir_id(trait_item.hir_id, &self.save_ctxt);
+
+                    self.dumper.dump_def(
+                        &Access { public: true, reachable: true },
+                        Def {
+                            kind: DefKind::Type,
+                            id,
+                            span,
+                            name,
+                            qualname,
+                            value: self.span.snippet(trait_item.span),
+                            parent: Some(id_from_def_id(trait_id)),
+                            children: vec![],
+                            decl_id: None,
+                            docs: self.save_ctxt.docs_for_attrs(&trait_item.attrs),
+                            sig: sig::assoc_type_signature(
+                                trait_item.hir_id,
+                                trait_item.ident,
+                                Some(bounds),
+                                default_ty.as_ref().map(|ty| &**ty),
+                                &self.save_ctxt,
+                            ),
+                            attributes: lower_attributes(
+                                trait_item.attrs.to_vec(),
+                                &self.save_ctxt,
+                            ),
+                        },
+                    );
+                }
+
+                if let &Some(ref default_ty) = default_ty {
+                    self.visit_ty(default_ty)
+                }
+            }
+        }
+    }
+
+    fn process_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>, impl_id: DefId) {
+        self.process_macro_use(impl_item.span);
+        match impl_item.kind {
+            hir::ImplItemKind::Const(ref ty, body) => {
+                let body = self.tcx.hir().body(body);
+                self.process_assoc_const(
+                    impl_item.hir_id,
+                    impl_item.ident,
+                    &ty,
+                    Some(&body.value),
+                    impl_id,
+                    &impl_item.vis,
+                    &impl_item.attrs,
+                );
+            }
+            hir::ImplItemKind::Fn(ref sig, body) => {
+                self.process_method(
+                    sig,
+                    Some(body),
+                    impl_item.hir_id,
+                    impl_item.ident,
+                    &impl_item.generics,
+                    &impl_item.vis,
+                    impl_item.span,
+                );
+            }
+            hir::ImplItemKind::TyAlias(ref ty) => {
+                // FIXME: uses of the assoc type should ideally point to this
+                // 'def' and the name here should be a ref to the def in the
+                // trait.
+                self.visit_ty(ty)
+            }
+        }
+    }
+
+    pub(crate) fn process_crate(&mut self, krate: &'tcx hir::Crate<'tcx>) {
+        let id = hir::CRATE_HIR_ID;
+        let qualname =
+            format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id()));
+
+        let sm = self.tcx.sess.source_map();
+        let filename = sm.span_to_filename(krate.item.span);
+        let data_id = id_from_hir_id(id, &self.save_ctxt);
+        let children = krate
+            .item
+            .module
+            .item_ids
+            .iter()
+            .map(|i| id_from_hir_id(i.id, &self.save_ctxt))
+            .collect();
+        let span = self.span_from_span(krate.item.span);
+
+        self.dumper.dump_def(
+            &Access { public: true, reachable: true },
+            Def {
+                kind: DefKind::Mod,
+                id: data_id,
+                name: String::new(),
+                qualname,
+                span,
+                value: filename.to_string(),
+                children,
+                parent: None,
+                decl_id: None,
+                docs: self.save_ctxt.docs_for_attrs(krate.item.attrs),
+                sig: None,
+                attributes: lower_attributes(krate.item.attrs.to_owned(), &self.save_ctxt),
+            },
+        );
+        intravisit::walk_crate(self, krate);
+    }
+
+    fn process_bounds(&mut self, bounds: hir::GenericBounds<'tcx>) {
+        for bound in bounds {
+            if let hir::GenericBound::Trait(ref trait_ref, _) = *bound {
+                self.process_path(
+                    trait_ref.trait_ref.hir_ref_id,
+                    &hir::QPath::Resolved(None, &trait_ref.trait_ref.path),
+                )
+            }
+        }
+    }
+}
+
+impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
+    type Map = Map<'tcx>;
+
+    fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
+        intravisit::NestedVisitorMap::All(self.tcx.hir())
+    }
+
+    fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
+        self.process_macro_use(item.span);
+        match item.kind {
+            hir::ItemKind::Use(path, hir::UseKind::Single) => {
+                let sub_span = path.segments.last().unwrap().ident.span;
+                if !self.span.filter_generated(sub_span) {
+                    let access = access_from!(self.save_ctxt, item, item.hir_id);
+                    let ref_id = self.lookup_def_id(item.hir_id).map(id_from_def_id);
+                    let span = self.span_from_span(sub_span);
+                    let parent = self
+                        .save_ctxt
+                        .tcx
+                        .hir()
+                        .opt_local_def_id(item.hir_id)
+                        .and_then(|id| self.save_ctxt.tcx.parent(id.to_def_id()))
+                        .map(id_from_def_id);
+                    self.dumper.import(
+                        &access,
+                        Import {
+                            kind: ImportKind::Use,
+                            ref_id,
+                            span,
+                            alias_span: None,
+                            name: item.ident.to_string(),
+                            value: String::new(),
+                            parent,
+                        },
+                    );
+                    self.write_sub_paths_truncated(&path);
+                }
+            }
+            hir::ItemKind::Use(path, hir::UseKind::Glob) => {
+                // Make a comma-separated list of names of imported modules.
+                let def_id = self.tcx.hir().local_def_id(item.hir_id);
+                let names = self.tcx.names_imported_by_glob_use(def_id);
+                let names: Vec<_> = names.iter().map(|n| n.to_string()).collect();
+
+                // Otherwise it's a span with wrong macro expansion info, which
+                // we don't want to track anyway, since it's probably macro-internal `use`
+                if let Some(sub_span) = self.span.sub_span_of_star(item.span) {
+                    if !self.span.filter_generated(item.span) {
+                        let access = access_from!(self.save_ctxt, item, item.hir_id);
+                        let span = self.span_from_span(sub_span);
+                        let parent = self
+                            .save_ctxt
+                            .tcx
+                            .hir()
+                            .opt_local_def_id(item.hir_id)
+                            .and_then(|id| self.save_ctxt.tcx.parent(id.to_def_id()))
+                            .map(id_from_def_id);
+                        self.dumper.import(
+                            &access,
+                            Import {
+                                kind: ImportKind::GlobUse,
+                                ref_id: None,
+                                span,
+                                alias_span: None,
+                                name: "*".to_owned(),
+                                value: names.join(", "),
+                                parent,
+                            },
+                        );
+                        self.write_sub_paths(&path);
+                    }
+                }
+            }
+            hir::ItemKind::ExternCrate(_) => {
+                let name_span = item.ident.span;
+                if !self.span.filter_generated(name_span) {
+                    let span = self.span_from_span(name_span);
+                    let parent = self
+                        .save_ctxt
+                        .tcx
+                        .hir()
+                        .opt_local_def_id(item.hir_id)
+                        .and_then(|id| self.save_ctxt.tcx.parent(id.to_def_id()))
+                        .map(id_from_def_id);
+                    self.dumper.import(
+                        &Access { public: false, reachable: false },
+                        Import {
+                            kind: ImportKind::ExternCrate,
+                            ref_id: None,
+                            span,
+                            alias_span: None,
+                            name: item.ident.to_string(),
+                            value: String::new(),
+                            parent,
+                        },
+                    );
+                }
+            }
+            hir::ItemKind::Fn(ref sig, ref ty_params, body) => {
+                self.process_fn(item, sig.decl, &sig.header, ty_params, body)
+            }
+            hir::ItemKind::Static(ref typ, _, body) => {
+                let body = self.tcx.hir().body(body);
+                self.process_static_or_const_item(item, typ, &body.value)
+            }
+            hir::ItemKind::Const(ref typ, body) => {
+                let body = self.tcx.hir().body(body);
+                self.process_static_or_const_item(item, typ, &body.value)
+            }
+            hir::ItemKind::Struct(ref def, ref ty_params)
+            | hir::ItemKind::Union(ref def, ref ty_params) => {
+                self.process_struct(item, def, ty_params)
+            }
+            hir::ItemKind::Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
+            hir::ItemKind::Impl { ref generics, ref of_trait, ref self_ty, ref items, .. } => {
+                self.process_impl(item, generics, of_trait, &self_ty, items)
+            }
+            hir::ItemKind::Trait(_, _, ref generics, ref trait_refs, methods) => {
+                self.process_trait(item, generics, trait_refs, methods)
+            }
+            hir::ItemKind::Mod(ref m) => {
+                self.process_mod(item);
+                intravisit::walk_mod(self, m, item.hir_id);
+            }
+            hir::ItemKind::TyAlias(ty, ref generics) => {
+                let qualname = format!(
+                    "::{}",
+                    self.tcx.def_path_str(self.tcx.hir().local_def_id(item.hir_id).to_def_id())
+                );
+                let value = ty_to_string(&ty);
+                if !self.span.filter_generated(item.ident.span) {
+                    let span = self.span_from_span(item.ident.span);
+                    let id = id_from_hir_id(item.hir_id, &self.save_ctxt);
+
+                    self.dumper.dump_def(
+                        &access_from!(self.save_ctxt, item, item.hir_id),
+                        Def {
+                            kind: DefKind::Type,
+                            id,
+                            span,
+                            name: item.ident.to_string(),
+                            qualname: qualname.clone(),
+                            value,
+                            parent: None,
+                            children: vec![],
+                            decl_id: None,
+                            docs: self.save_ctxt.docs_for_attrs(&item.attrs),
+                            sig: sig::item_signature(item, &self.save_ctxt),
+                            attributes: lower_attributes(item.attrs.to_vec(), &self.save_ctxt),
+                        },
+                    );
+                }
+
+                self.visit_ty(ty);
+                self.process_generic_params(generics, &qualname, item.hir_id);
+            }
+            _ => intravisit::walk_item(self, item),
+        }
+    }
+
+    fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
+        for param in generics.params {
+            match param.kind {
+                hir::GenericParamKind::Lifetime { .. } => {}
+                hir::GenericParamKind::Type { ref default, .. } => {
+                    self.process_bounds(param.bounds);
+                    if let Some(ref ty) = default {
+                        self.visit_ty(ty);
+                    }
+                }
+                hir::GenericParamKind::Const { ref ty } => {
+                    self.process_bounds(param.bounds);
+                    self.visit_ty(ty);
+                }
+            }
+        }
+        for pred in generics.where_clause.predicates {
+            if let hir::WherePredicate::BoundPredicate(ref wbp) = *pred {
+                self.process_bounds(wbp.bounds);
+                self.visit_ty(wbp.bounded_ty);
+            }
+        }
+    }
+
+    fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) {
+        self.process_macro_use(t.span);
+        match t.kind {
+            hir::TyKind::Path(ref path) => {
+                if generated_code(t.span) {
+                    return;
+                }
+
+                if let Some(id) = self.lookup_def_id(t.hir_id) {
+                    let sub_span = path.last_segment_span();
+                    let span = self.span_from_span(sub_span);
+                    self.dumper.dump_ref(Ref {
+                        kind: RefKind::Type,
+                        span,
+                        ref_id: id_from_def_id(id),
+                    });
+                }
+
+                if let hir::QPath::Resolved(_, path) = path {
+                    self.write_sub_paths_truncated(path);
+                }
+                intravisit::walk_qpath(self, path, t.hir_id, t.span);
+            }
+            hir::TyKind::Array(ref ty, ref anon_const) => {
+                self.visit_ty(ty);
+                let map = self.tcx.hir();
+                self.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
+                    v.visit_expr(&map.body(anon_const.body).value)
+                });
+            }
+            hir::TyKind::OpaqueDef(item_id, _) => {
+                let item = self.tcx.hir().item(item_id.id);
+                self.nest_typeck_results(self.tcx.hir().local_def_id(item_id.id), |v| {
+                    v.visit_item(item)
+                });
+            }
+            _ => intravisit::walk_ty(self, t),
+        }
+    }
+
+    fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
+        debug!("visit_expr {:?}", ex.kind);
+        self.process_macro_use(ex.span);
+        match ex.kind {
+            hir::ExprKind::Struct(ref path, ref fields, ref base) => {
+                let hir_expr = self.save_ctxt.tcx.hir().expect_expr(ex.hir_id);
+                let adt = match self.save_ctxt.typeck_results().expr_ty_opt(&hir_expr) {
+                    Some(ty) if ty.ty_adt_def().is_some() => ty.ty_adt_def().unwrap(),
+                    _ => {
+                        intravisit::walk_expr(self, ex);
+                        return;
+                    }
+                };
+                let res = self.save_ctxt.get_path_res(hir_expr.hir_id);
+                self.process_struct_lit(ex, path, fields, adt.variant_of_res(res), *base)
+            }
+            hir::ExprKind::MethodCall(ref seg, _, args, _) => {
+                self.process_method_call(ex, seg, args)
+            }
+            hir::ExprKind::Field(ref sub_ex, _) => {
+                self.visit_expr(&sub_ex);
+
+                if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
+                    down_cast_data!(field_data, RefData, ex.span);
+                    if !generated_code(ex.span) {
+                        self.dumper.dump_ref(field_data);
+                    }
+                }
+            }
+            hir::ExprKind::Closure(_, ref decl, body, _fn_decl_span, _) => {
+                let id = format!("${}", ex.hir_id);
+
+                // walk arg and return types
+                for ty in decl.inputs {
+                    self.visit_ty(ty);
+                }
+
+                if let hir::FnRetTy::Return(ref ret_ty) = decl.output {
+                    self.visit_ty(ret_ty);
+                }
+
+                // walk the body
+                let map = self.tcx.hir();
+                self.nest_typeck_results(self.tcx.hir().local_def_id(ex.hir_id), |v| {
+                    let body = map.body(body);
+                    v.process_formals(body.params, &id);
+                    v.visit_expr(&body.value)
+                });
+            }
+            hir::ExprKind::Repeat(ref expr, ref anon_const) => {
+                self.visit_expr(expr);
+                let map = self.tcx.hir();
+                self.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
+                    v.visit_expr(&map.body(anon_const.body).value)
+                });
+            }
+            // In particular, we take this branch for call and path expressions,
+            // where we'll index the idents involved just by continuing to walk.
+            _ => intravisit::walk_expr(self, ex),
+        }
+    }
+
+    fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
+        self.process_macro_use(p.span);
+        self.process_pat(p);
+    }
+
+    fn visit_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) {
+        self.process_var_decl(&arm.pat);
+        if let Some(hir::Guard::If(expr)) = &arm.guard {
+            self.visit_expr(expr);
+        }
+        self.visit_expr(&arm.body);
+    }
+
+    fn visit_qpath(&mut self, path: &'tcx hir::QPath<'tcx>, id: hir::HirId, _: Span) {
+        self.process_path(id, path);
+    }
+
+    fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) {
+        self.process_macro_use(s.span);
+        intravisit::walk_stmt(self, s)
+    }
+
+    fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
+        self.process_macro_use(l.span);
+        self.process_var_decl(&l.pat);
+
+        // Just walk the initialiser and type (don't want to walk the pattern again).
+        walk_list!(self, visit_ty, &l.ty);
+        walk_list!(self, visit_expr, &l.init);
+    }
+
+    fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
+        let access = access_from!(self.save_ctxt, item, item.hir_id);
+
+        match item.kind {
+            hir::ForeignItemKind::Fn(decl, _, ref generics) => {
+                if let Some(fn_data) = self.save_ctxt.get_extern_item_data(item) {
+                    down_cast_data!(fn_data, DefData, item.span);
+
+                    self.process_generic_params(generics, &fn_data.qualname, item.hir_id);
+                    self.dumper.dump_def(&access, fn_data);
+                }
+
+                for ty in decl.inputs {
+                    self.visit_ty(ty);
+                }
+
+                if let hir::FnRetTy::Return(ref ret_ty) = decl.output {
+                    self.visit_ty(ret_ty);
+                }
+            }
+            hir::ForeignItemKind::Static(ref ty, _) => {
+                if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
+                    down_cast_data!(var_data, DefData, item.span);
+                    self.dumper.dump_def(&access, var_data);
+                }
+
+                self.visit_ty(ty);
+            }
+            hir::ForeignItemKind::Type => {
+                if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
+                    down_cast_data!(var_data, DefData, item.span);
+                    self.dumper.dump_def(&access, var_data);
+                }
+            }
+        }
+    }
+}
diff --git a/compiler/rustc_save_analysis/src/dumper.rs b/compiler/rustc_save_analysis/src/dumper.rs
new file mode 100644
index 0000000..5a26282
--- /dev/null
+++ b/compiler/rustc_save_analysis/src/dumper.rs
@@ -0,0 +1,91 @@
+use rls_data::config::Config;
+use rls_data::{
+    self, Analysis, CompilationOptions, CratePreludeData, Def, DefKind, Impl, Import, MacroRef,
+    Ref, RefKind, Relation,
+};
+use rls_span::{Column, Row};
+
+#[derive(Debug)]
+pub struct Access {
+    pub reachable: bool,
+    pub public: bool,
+}
+
+pub struct Dumper {
+    result: Analysis,
+    config: Config,
+}
+
+impl Dumper {
+    pub fn new(config: Config) -> Dumper {
+        Dumper { config: config.clone(), result: Analysis::new(config) }
+    }
+
+    pub fn analysis(&self) -> &Analysis {
+        &self.result
+    }
+}
+
+impl Dumper {
+    pub fn crate_prelude(&mut self, data: CratePreludeData) {
+        self.result.prelude = Some(data)
+    }
+
+    pub fn compilation_opts(&mut self, data: CompilationOptions) {
+        self.result.compilation = Some(data);
+    }
+
+    pub fn _macro_use(&mut self, data: MacroRef) {
+        if self.config.pub_only || self.config.reachable_only {
+            return;
+        }
+        self.result.macro_refs.push(data);
+    }
+
+    pub fn import(&mut self, access: &Access, import: Import) {
+        if !access.public && self.config.pub_only || !access.reachable && self.config.reachable_only
+        {
+            return;
+        }
+        self.result.imports.push(import);
+    }
+
+    pub fn dump_ref(&mut self, data: Ref) {
+        if self.config.pub_only || self.config.reachable_only {
+            return;
+        }
+        self.result.refs.push(data);
+    }
+
+    pub fn dump_def(&mut self, access: &Access, mut data: Def) {
+        if !access.public && self.config.pub_only || !access.reachable && self.config.reachable_only
+        {
+            return;
+        }
+        if data.kind == DefKind::Mod && data.span.file_name.to_str().unwrap() != data.value {
+            // If the module is an out-of-line definition, then we'll make the
+            // definition the first character in the module's file and turn
+            // the declaration into a reference to it.
+            let rf = Ref { kind: RefKind::Mod, span: data.span, ref_id: data.id };
+            self.result.refs.push(rf);
+            data.span = rls_data::SpanData {
+                file_name: data.value.clone().into(),
+                byte_start: 0,
+                byte_end: 0,
+                line_start: Row::new_one_indexed(1),
+                line_end: Row::new_one_indexed(1),
+                column_start: Column::new_one_indexed(1),
+                column_end: Column::new_one_indexed(1),
+            }
+        }
+        self.result.defs.push(data);
+    }
+
+    pub fn dump_relation(&mut self, data: Relation) {
+        self.result.relations.push(data);
+    }
+
+    pub fn dump_impl(&mut self, data: Impl) {
+        self.result.impls.push(data);
+    }
+}
diff --git a/compiler/rustc_save_analysis/src/lib.rs b/compiler/rustc_save_analysis/src/lib.rs
new file mode 100644
index 0000000..f643468
--- /dev/null
+++ b/compiler/rustc_save_analysis/src/lib.rs
@@ -0,0 +1,1097 @@
+#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
+#![feature(nll)]
+#![feature(or_patterns)]
+#![recursion_limit = "256"]
+
+mod dump_visitor;
+mod dumper;
+#[macro_use]
+mod span_utils;
+mod sig;
+
+use rustc_ast as ast;
+use rustc_ast::util::comments::beautify_doc_string;
+use rustc_ast_pretty::pprust::attribute_to_string;
+use rustc_hir as hir;
+use rustc_hir::def::{DefKind as HirDefKind, Res};
+use rustc_hir::def_id::{DefId, LOCAL_CRATE};
+use rustc_hir::intravisit::{self, Visitor};
+use rustc_hir::Node;
+use rustc_hir_pretty::{enum_def_to_string, fn_to_string, ty_to_string};
+use rustc_middle::hir::map::Map;
+use rustc_middle::middle::cstore::ExternCrate;
+use rustc_middle::middle::privacy::AccessLevels;
+use rustc_middle::ty::{self, print::with_no_trimmed_paths, DefIdTree, TyCtxt};
+use rustc_middle::{bug, span_bug};
+use rustc_session::config::{CrateType, Input, OutputType};
+use rustc_session::output::{filename_for_metadata, out_filename};
+use rustc_span::source_map::Spanned;
+use rustc_span::symbol::Ident;
+use rustc_span::*;
+
+use std::cell::Cell;
+use std::default::Default;
+use std::env;
+use std::fs::File;
+use std::io::BufWriter;
+use std::path::{Path, PathBuf};
+
+use dump_visitor::DumpVisitor;
+use span_utils::SpanUtils;
+
+use rls_data::config::Config;
+use rls_data::{
+    Analysis, Def, DefKind, ExternalCrateData, GlobalCrateId, Impl, ImplKind, MacroRef, Ref,
+    RefKind, Relation, RelationKind, SpanData,
+};
+
+use tracing::{debug, error, info};
+
+pub struct SaveContext<'tcx> {
+    tcx: TyCtxt<'tcx>,
+    maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
+    access_levels: &'tcx AccessLevels,
+    span_utils: SpanUtils<'tcx>,
+    config: Config,
+    impl_counter: Cell<u32>,
+}
+
+#[derive(Debug)]
+pub enum Data {
+    RefData(Ref),
+    DefData(Def),
+    RelationData(Relation, Impl),
+}
+
+impl<'tcx> SaveContext<'tcx> {
+    /// Gets the type-checking results for the current body.
+    /// As this will ICE if called outside bodies, only call when working with
+    /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
+    #[track_caller]
+    fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
+        self.maybe_typeck_results.expect("`SaveContext::typeck_results` called outside of body")
+    }
+
+    fn span_from_span(&self, span: Span) -> SpanData {
+        use rls_span::{Column, Row};
+
+        let sm = self.tcx.sess.source_map();
+        let start = sm.lookup_char_pos(span.lo());
+        let end = sm.lookup_char_pos(span.hi());
+
+        SpanData {
+            file_name: start.file.name.to_string().into(),
+            byte_start: span.lo().0,
+            byte_end: span.hi().0,
+            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),
+        }
+    }
+
+    // Returns path to the compilation output (e.g., libfoo-12345678.rmeta)
+    pub fn compilation_output(&self, crate_name: &str) -> PathBuf {
+        let sess = &self.tcx.sess;
+        // Save-analysis is emitted per whole session, not per each crate type
+        let crate_type = sess.crate_types()[0];
+        let outputs = &*self.tcx.output_filenames(LOCAL_CRATE);
+
+        if outputs.outputs.contains_key(&OutputType::Metadata) {
+            filename_for_metadata(sess, crate_name, outputs)
+        } else if outputs.outputs.should_codegen() {
+            out_filename(sess, crate_type, outputs, crate_name)
+        } else {
+            // Otherwise it's only a DepInfo, in which case we return early and
+            // not even reach the analysis stage.
+            unreachable!()
+        }
+    }
+
+    // List external crates used by the current crate.
+    pub fn get_external_crates(&self) -> Vec<ExternalCrateData> {
+        let mut result = Vec::with_capacity(self.tcx.crates().len());
+
+        for &n in self.tcx.crates().iter() {
+            let span = match self.tcx.extern_crate(n.as_def_id()) {
+                Some(&ExternCrate { span, .. }) => span,
+                None => {
+                    debug!("skipping crate {}, no data", n);
+                    continue;
+                }
+            };
+            let lo_loc = self.span_utils.sess.source_map().lookup_char_pos(span.lo());
+            result.push(ExternalCrateData {
+                // FIXME: change file_name field to PathBuf in rls-data
+                // https://github.com/nrc/rls-data/issues/7
+                file_name: self.span_utils.make_filename_string(&lo_loc.file),
+                num: n.as_u32(),
+                id: GlobalCrateId {
+                    name: self.tcx.crate_name(n).to_string(),
+                    disambiguator: self.tcx.crate_disambiguator(n).to_fingerprint().as_value(),
+                },
+            });
+        }
+
+        result
+    }
+
+    pub fn get_extern_item_data(&self, item: &hir::ForeignItem<'_>) -> Option<Data> {
+        let def_id = self.tcx.hir().local_def_id(item.hir_id).to_def_id();
+        let qualname = format!("::{}", self.tcx.def_path_str(def_id));
+        match item.kind {
+            hir::ForeignItemKind::Fn(ref decl, arg_names, ref generics) => {
+                filter!(self.span_utils, item.ident.span);
+
+                Some(Data::DefData(Def {
+                    kind: DefKind::ForeignFunction,
+                    id: id_from_def_id(def_id),
+                    span: self.span_from_span(item.ident.span),
+                    name: item.ident.to_string(),
+                    qualname,
+                    value: fn_to_string(
+                        decl,
+                        hir::FnHeader {
+                            // functions in extern block are implicitly unsafe
+                            unsafety: hir::Unsafety::Unsafe,
+                            // functions in extern block cannot be const
+                            constness: hir::Constness::NotConst,
+                            abi: self.tcx.hir().get_foreign_abi(item.hir_id),
+                            // functions in extern block cannot be async
+                            asyncness: hir::IsAsync::NotAsync,
+                        },
+                        Some(item.ident.name),
+                        generics,
+                        &item.vis,
+                        arg_names,
+                        None,
+                    ),
+                    parent: None,
+                    children: vec![],
+                    decl_id: None,
+                    docs: self.docs_for_attrs(&item.attrs),
+                    sig: sig::foreign_item_signature(item, self),
+                    attributes: lower_attributes(item.attrs.to_vec(), self),
+                }))
+            }
+            hir::ForeignItemKind::Static(ref ty, _) => {
+                filter!(self.span_utils, item.ident.span);
+
+                let id = id_from_def_id(def_id);
+                let span = self.span_from_span(item.ident.span);
+
+                Some(Data::DefData(Def {
+                    kind: DefKind::ForeignStatic,
+                    id,
+                    span,
+                    name: item.ident.to_string(),
+                    qualname,
+                    value: ty_to_string(ty),
+                    parent: None,
+                    children: vec![],
+                    decl_id: None,
+                    docs: self.docs_for_attrs(&item.attrs),
+                    sig: sig::foreign_item_signature(item, self),
+                    attributes: lower_attributes(item.attrs.to_vec(), self),
+                }))
+            }
+            // FIXME(plietar): needs a new DefKind in rls-data
+            hir::ForeignItemKind::Type => None,
+        }
+    }
+
+    pub fn get_item_data(&self, item: &hir::Item<'_>) -> Option<Data> {
+        let def_id = self.tcx.hir().local_def_id(item.hir_id).to_def_id();
+        match item.kind {
+            hir::ItemKind::Fn(ref sig, ref generics, _) => {
+                let qualname = format!("::{}", self.tcx.def_path_str(def_id));
+                filter!(self.span_utils, item.ident.span);
+                Some(Data::DefData(Def {
+                    kind: DefKind::Function,
+                    id: id_from_def_id(def_id),
+                    span: self.span_from_span(item.ident.span),
+                    name: item.ident.to_string(),
+                    qualname,
+                    value: fn_to_string(
+                        sig.decl,
+                        sig.header,
+                        Some(item.ident.name),
+                        generics,
+                        &item.vis,
+                        &[],
+                        None,
+                    ),
+                    parent: None,
+                    children: vec![],
+                    decl_id: None,
+                    docs: self.docs_for_attrs(&item.attrs),
+                    sig: sig::item_signature(item, self),
+                    attributes: lower_attributes(item.attrs.to_vec(), self),
+                }))
+            }
+            hir::ItemKind::Static(ref typ, ..) => {
+                let qualname = format!("::{}", self.tcx.def_path_str(def_id));
+
+                filter!(self.span_utils, item.ident.span);
+
+                let id = id_from_def_id(def_id);
+                let span = self.span_from_span(item.ident.span);
+
+                Some(Data::DefData(Def {
+                    kind: DefKind::Static,
+                    id,
+                    span,
+                    name: item.ident.to_string(),
+                    qualname,
+                    value: ty_to_string(&typ),
+                    parent: None,
+                    children: vec![],
+                    decl_id: None,
+                    docs: self.docs_for_attrs(&item.attrs),
+                    sig: sig::item_signature(item, self),
+                    attributes: lower_attributes(item.attrs.to_vec(), self),
+                }))
+            }
+            hir::ItemKind::Const(ref typ, _) => {
+                let qualname = format!("::{}", self.tcx.def_path_str(def_id));
+                filter!(self.span_utils, item.ident.span);
+
+                let id = id_from_def_id(def_id);
+                let span = self.span_from_span(item.ident.span);
+
+                Some(Data::DefData(Def {
+                    kind: DefKind::Const,
+                    id,
+                    span,
+                    name: item.ident.to_string(),
+                    qualname,
+                    value: ty_to_string(typ),
+                    parent: None,
+                    children: vec![],
+                    decl_id: None,
+                    docs: self.docs_for_attrs(&item.attrs),
+                    sig: sig::item_signature(item, self),
+                    attributes: lower_attributes(item.attrs.to_vec(), self),
+                }))
+            }
+            hir::ItemKind::Mod(ref m) => {
+                let qualname = format!("::{}", self.tcx.def_path_str(def_id));
+
+                let sm = self.tcx.sess.source_map();
+                let filename = sm.span_to_filename(m.inner);
+
+                filter!(self.span_utils, item.ident.span);
+
+                Some(Data::DefData(Def {
+                    kind: DefKind::Mod,
+                    id: id_from_def_id(def_id),
+                    name: item.ident.to_string(),
+                    qualname,
+                    span: self.span_from_span(item.ident.span),
+                    value: filename.to_string(),
+                    parent: None,
+                    children: m.item_ids.iter().map(|i| id_from_hir_id(i.id, self)).collect(),
+                    decl_id: None,
+                    docs: self.docs_for_attrs(&item.attrs),
+                    sig: sig::item_signature(item, self),
+                    attributes: lower_attributes(item.attrs.to_vec(), self),
+                }))
+            }
+            hir::ItemKind::Enum(ref def, ref generics) => {
+                let name = item.ident.to_string();
+                let qualname = format!("::{}", self.tcx.def_path_str(def_id));
+                filter!(self.span_utils, item.ident.span);
+                let value =
+                    enum_def_to_string(def, generics, item.ident.name, item.span, &item.vis);
+                Some(Data::DefData(Def {
+                    kind: DefKind::Enum,
+                    id: id_from_def_id(def_id),
+                    span: self.span_from_span(item.ident.span),
+                    name,
+                    qualname,
+                    value,
+                    parent: None,
+                    children: def.variants.iter().map(|v| id_from_hir_id(v.id, self)).collect(),
+                    decl_id: None,
+                    docs: self.docs_for_attrs(&item.attrs),
+                    sig: sig::item_signature(item, self),
+                    attributes: lower_attributes(item.attrs.to_vec(), self),
+                }))
+            }
+            hir::ItemKind::Impl { ref of_trait, ref self_ty, ref items, .. } => {
+                if let hir::TyKind::Path(hir::QPath::Resolved(_, ref path)) = self_ty.kind {
+                    // Common case impl for a struct or something basic.
+                    if generated_code(path.span) {
+                        return None;
+                    }
+                    let sub_span = path.segments.last().unwrap().ident.span;
+                    filter!(self.span_utils, sub_span);
+
+                    let impl_id = self.next_impl_id();
+                    let span = self.span_from_span(sub_span);
+
+                    let type_data = self.lookup_def_id(self_ty.hir_id);
+                    type_data.map(|type_data| {
+                        Data::RelationData(
+                            Relation {
+                                kind: RelationKind::Impl { id: impl_id },
+                                span: span.clone(),
+                                from: id_from_def_id(type_data),
+                                to: of_trait
+                                    .as_ref()
+                                    .and_then(|t| self.lookup_def_id(t.hir_ref_id))
+                                    .map(id_from_def_id)
+                                    .unwrap_or_else(null_id),
+                            },
+                            Impl {
+                                id: impl_id,
+                                kind: match *of_trait {
+                                    Some(_) => ImplKind::Direct,
+                                    None => ImplKind::Inherent,
+                                },
+                                span,
+                                value: String::new(),
+                                parent: None,
+                                children: items
+                                    .iter()
+                                    .map(|i| id_from_hir_id(i.id.hir_id, self))
+                                    .collect(),
+                                docs: String::new(),
+                                sig: None,
+                                attributes: vec![],
+                            },
+                        )
+                    })
+                } else {
+                    None
+                }
+            }
+            _ => {
+                // FIXME
+                bug!();
+            }
+        }
+    }
+
+    pub fn get_field_data(&self, field: &hir::StructField<'_>, scope: hir::HirId) -> Option<Def> {
+        let name = field.ident.to_string();
+        let scope_def_id = self.tcx.hir().local_def_id(scope).to_def_id();
+        let qualname = format!("::{}::{}", self.tcx.def_path_str(scope_def_id), field.ident);
+        filter!(self.span_utils, field.ident.span);
+        let field_def_id = self.tcx.hir().local_def_id(field.hir_id).to_def_id();
+        let typ = self.tcx.type_of(field_def_id).to_string();
+
+        let id = id_from_def_id(field_def_id);
+        let span = self.span_from_span(field.ident.span);
+
+        Some(Def {
+            kind: DefKind::Field,
+            id,
+            span,
+            name,
+            qualname,
+            value: typ,
+            parent: Some(id_from_def_id(scope_def_id)),
+            children: vec![],
+            decl_id: None,
+            docs: self.docs_for_attrs(&field.attrs),
+            sig: sig::field_signature(field, self),
+            attributes: lower_attributes(field.attrs.to_vec(), self),
+        })
+    }
+
+    // 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.
+    pub fn get_method_data(&self, hir_id: hir::HirId, ident: Ident, span: Span) -> Option<Def> {
+        // 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.
+        let def_id = self.tcx.hir().local_def_id(hir_id).to_def_id();
+        let (qualname, parent_scope, decl_id, docs, attributes) =
+            match self.tcx.impl_of_method(def_id) {
+                Some(impl_id) => match self.tcx.hir().get_if_local(impl_id) {
+                    Some(Node::Item(item)) => match item.kind {
+                        hir::ItemKind::Impl { ref self_ty, .. } => {
+                            let hir = self.tcx.hir();
+
+                            let mut qualname = String::from("<");
+                            qualname
+                                .push_str(&rustc_hir_pretty::id_to_string(&hir, self_ty.hir_id));
+
+                            let trait_id = self.tcx.trait_id_of_impl(impl_id);
+                            let mut docs = String::new();
+                            let mut attrs = vec![];
+                            if let Some(Node::ImplItem(item)) = hir.find(hir_id) {
+                                docs = self.docs_for_attrs(&item.attrs);
+                                attrs = item.attrs.to_vec();
+                            }
+
+                            let mut decl_id = None;
+                            if let Some(def_id) = trait_id {
+                                // A method in a trait impl.
+                                qualname.push_str(" as ");
+                                qualname.push_str(&self.tcx.def_path_str(def_id));
+
+                                decl_id = self
+                                    .tcx
+                                    .associated_items(def_id)
+                                    .filter_by_name_unhygienic(ident.name)
+                                    .next()
+                                    .map(|item| item.def_id);
+                            }
+                            qualname.push('>');
+
+                            (qualname, trait_id, decl_id, docs, attrs)
+                        }
+                        _ => {
+                            span_bug!(
+                                span,
+                                "Container {:?} for method {} not an impl?",
+                                impl_id,
+                                hir_id
+                            );
+                        }
+                    },
+                    r => {
+                        span_bug!(
+                            span,
+                            "Container {:?} for method {} is not a node item {:?}",
+                            impl_id,
+                            hir_id,
+                            r
+                        );
+                    }
+                },
+                None => match self.tcx.trait_of_item(def_id) {
+                    Some(def_id) => {
+                        let mut docs = String::new();
+                        let mut attrs = vec![];
+
+                        if let Some(Node::TraitItem(item)) = self.tcx.hir().find(hir_id) {
+                            docs = self.docs_for_attrs(&item.attrs);
+                            attrs = item.attrs.to_vec();
+                        }
+
+                        (
+                            format!("::{}", self.tcx.def_path_str(def_id)),
+                            Some(def_id),
+                            None,
+                            docs,
+                            attrs,
+                        )
+                    }
+                    None => {
+                        debug!("could not find container for method {} at {:?}", hir_id, span);
+                        // This is not necessarily a bug, if there was a compilation error,
+                        // the typeck results we need might not exist.
+                        return None;
+                    }
+                },
+            };
+
+        let qualname = format!("{}::{}", qualname, ident.name);
+
+        filter!(self.span_utils, ident.span);
+
+        Some(Def {
+            kind: DefKind::Method,
+            id: id_from_def_id(def_id),
+            span: self.span_from_span(ident.span),
+            name: ident.name.to_string(),
+            qualname,
+            // FIXME you get better data here by using the visitor.
+            value: String::new(),
+            parent: parent_scope.map(id_from_def_id),
+            children: vec![],
+            decl_id: decl_id.map(id_from_def_id),
+            docs,
+            sig: None,
+            attributes: lower_attributes(attributes, self),
+        })
+    }
+
+    pub fn get_trait_ref_data(&self, trait_ref: &hir::TraitRef<'_>) -> Option<Ref> {
+        self.lookup_def_id(trait_ref.hir_ref_id).and_then(|def_id| {
+            let span = trait_ref.path.span;
+            if generated_code(span) {
+                return None;
+            }
+            let sub_span = trait_ref.path.segments.last().unwrap().ident.span;
+            filter!(self.span_utils, sub_span);
+            let span = self.span_from_span(sub_span);
+            Some(Ref { kind: RefKind::Type, span, ref_id: id_from_def_id(def_id) })
+        })
+    }
+
+    pub fn get_expr_data(&self, expr: &hir::Expr<'_>) -> Option<Data> {
+        let ty = self.typeck_results().expr_ty_adjusted_opt(expr)?;
+        if matches!(ty.kind(), ty::Error(_)) {
+            return None;
+        }
+        match expr.kind {
+            hir::ExprKind::Field(ref sub_ex, ident) => {
+                match self.typeck_results().expr_ty_adjusted(&sub_ex).kind() {
+                    ty::Adt(def, _) if !def.is_enum() => {
+                        let variant = &def.non_enum_variant();
+                        filter!(self.span_utils, ident.span);
+                        let span = self.span_from_span(ident.span);
+                        Some(Data::RefData(Ref {
+                            kind: RefKind::Variable,
+                            span,
+                            ref_id: self
+                                .tcx
+                                .find_field_index(ident, variant)
+                                .map(|index| id_from_def_id(variant.fields[index].did))
+                                .unwrap_or_else(null_id),
+                        }))
+                    }
+                    ty::Tuple(..) => None,
+                    _ => {
+                        debug!("expected struct or union type, found {:?}", ty);
+                        None
+                    }
+                }
+            }
+            hir::ExprKind::Struct(qpath, ..) => match ty.kind() {
+                ty::Adt(def, _) => {
+                    let sub_span = qpath.last_segment_span();
+                    filter!(self.span_utils, sub_span);
+                    let span = self.span_from_span(sub_span);
+                    Some(Data::RefData(Ref {
+                        kind: RefKind::Type,
+                        span,
+                        ref_id: id_from_def_id(def.did),
+                    }))
+                }
+                _ => {
+                    debug!("expected adt, found {:?}", ty);
+                    None
+                }
+            },
+            hir::ExprKind::MethodCall(ref seg, ..) => {
+                let method_id = match self.typeck_results().type_dependent_def_id(expr.hir_id) {
+                    Some(id) => id,
+                    None => {
+                        debug!("could not resolve method id for {:?}", expr);
+                        return None;
+                    }
+                };
+                let (def_id, decl_id) = match self.tcx.associated_item(method_id).container {
+                    ty::ImplContainer(_) => (Some(method_id), None),
+                    ty::TraitContainer(_) => (None, Some(method_id)),
+                };
+                let sub_span = seg.ident.span;
+                filter!(self.span_utils, sub_span);
+                let span = self.span_from_span(sub_span);
+                Some(Data::RefData(Ref {
+                    kind: RefKind::Function,
+                    span,
+                    ref_id: def_id.or(decl_id).map(id_from_def_id).unwrap_or_else(null_id),
+                }))
+            }
+            hir::ExprKind::Path(ref path) => {
+                self.get_path_data(expr.hir_id, path).map(Data::RefData)
+            }
+            _ => {
+                // FIXME
+                bug!("invalid expression: {:?}", expr);
+            }
+        }
+    }
+
+    pub fn get_path_res(&self, hir_id: hir::HirId) -> Res {
+        match self.tcx.hir().get(hir_id) {
+            Node::TraitRef(tr) => tr.path.res,
+
+            Node::Item(&hir::Item { kind: hir::ItemKind::Use(path, _), .. }) => path.res,
+            Node::Visibility(&Spanned {
+                node: hir::VisibilityKind::Restricted { ref path, .. },
+                ..
+            }) => path.res,
+
+            Node::PathSegment(seg) => match seg.res {
+                Some(res) if res != Res::Err => res,
+                _ => {
+                    let parent_node = self.tcx.hir().get_parent_node(hir_id);
+                    self.get_path_res(parent_node)
+                }
+            },
+
+            Node::Expr(&hir::Expr { kind: hir::ExprKind::Struct(ref qpath, ..), .. }) => {
+                self.typeck_results().qpath_res(qpath, hir_id)
+            }
+
+            Node::Expr(&hir::Expr { kind: hir::ExprKind::Path(ref qpath), .. })
+            | Node::Pat(&hir::Pat {
+                kind:
+                    hir::PatKind::Path(ref qpath)
+                    | hir::PatKind::Struct(ref qpath, ..)
+                    | hir::PatKind::TupleStruct(ref qpath, ..),
+                ..
+            })
+            | Node::Ty(&hir::Ty { kind: hir::TyKind::Path(ref qpath), .. }) => match qpath {
+                hir::QPath::Resolved(_, path) => path.res,
+                hir::QPath::TypeRelative(..) | hir::QPath::LangItem(..) => self
+                    .maybe_typeck_results
+                    .map_or(Res::Err, |typeck_results| typeck_results.qpath_res(qpath, hir_id)),
+            },
+
+            Node::Binding(&hir::Pat {
+                kind: hir::PatKind::Binding(_, canonical_id, ..), ..
+            }) => Res::Local(canonical_id),
+
+            _ => Res::Err,
+        }
+    }
+
+    pub fn get_path_data(&self, id: hir::HirId, path: &hir::QPath<'_>) -> Option<Ref> {
+        let segment = match path {
+            hir::QPath::Resolved(_, path) => path.segments.last(),
+            hir::QPath::TypeRelative(_, segment) => Some(*segment),
+            hir::QPath::LangItem(..) => None,
+        };
+        segment.and_then(|seg| {
+            self.get_path_segment_data(seg).or_else(|| self.get_path_segment_data_with_id(seg, id))
+        })
+    }
+
+    pub fn get_path_segment_data(&self, path_seg: &hir::PathSegment<'_>) -> Option<Ref> {
+        self.get_path_segment_data_with_id(path_seg, path_seg.hir_id?)
+    }
+
+    pub fn get_path_segment_data_with_id(
+        &self,
+        path_seg: &hir::PathSegment<'_>,
+        id: hir::HirId,
+    ) -> Option<Ref> {
+        // Returns true if the path is function type sugar, e.g., `Fn(A) -> B`.
+        fn fn_type(seg: &hir::PathSegment<'_>) -> bool {
+            seg.args.map(|args| args.parenthesized).unwrap_or(false)
+        }
+
+        let res = self.get_path_res(id);
+        let span = path_seg.ident.span;
+        filter!(self.span_utils, span);
+        let span = self.span_from_span(span);
+
+        match res {
+            Res::Local(id) => {
+                Some(Ref { kind: RefKind::Variable, span, ref_id: id_from_hir_id(id, self) })
+            }
+            Res::Def(HirDefKind::Trait, def_id) if fn_type(path_seg) => {
+                Some(Ref { kind: RefKind::Type, span, ref_id: id_from_def_id(def_id) })
+            }
+            Res::Def(
+                HirDefKind::Struct
+                | HirDefKind::Variant
+                | HirDefKind::Union
+                | HirDefKind::Enum
+                | HirDefKind::TyAlias
+                | HirDefKind::ForeignTy
+                | HirDefKind::TraitAlias
+                | HirDefKind::AssocTy
+                | HirDefKind::Trait
+                | HirDefKind::OpaqueTy
+                | HirDefKind::TyParam,
+                def_id,
+            ) => Some(Ref { kind: RefKind::Type, span, ref_id: id_from_def_id(def_id) }),
+            Res::Def(HirDefKind::ConstParam, def_id) => {
+                Some(Ref { kind: RefKind::Variable, span, ref_id: id_from_def_id(def_id) })
+            }
+            Res::Def(HirDefKind::Ctor(..), def_id) => {
+                // This is a reference to a tuple struct or an enum variant 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 or enum variant itself.
+                let parent_def_id = self.tcx.parent(def_id).unwrap();
+                Some(Ref { kind: RefKind::Type, span, ref_id: id_from_def_id(parent_def_id) })
+            }
+            Res::Def(HirDefKind::Static | HirDefKind::Const | HirDefKind::AssocConst, _) => {
+                Some(Ref { kind: RefKind::Variable, span, ref_id: id_from_def_id(res.def_id()) })
+            }
+            Res::Def(HirDefKind::AssocFn, decl_id) => {
+                let def_id = if decl_id.is_local() {
+                    let ti = self.tcx.associated_item(decl_id);
+
+                    self.tcx
+                        .associated_items(ti.container.id())
+                        .filter_by_name_unhygienic(ti.ident.name)
+                        .find(|item| item.defaultness.has_value())
+                        .map(|item| item.def_id)
+                } else {
+                    None
+                };
+                Some(Ref {
+                    kind: RefKind::Function,
+                    span,
+                    ref_id: id_from_def_id(def_id.unwrap_or(decl_id)),
+                })
+            }
+            Res::Def(HirDefKind::Fn, def_id) => {
+                Some(Ref { kind: RefKind::Function, span, ref_id: id_from_def_id(def_id) })
+            }
+            Res::Def(HirDefKind::Mod, def_id) => {
+                Some(Ref { kind: RefKind::Mod, span, ref_id: id_from_def_id(def_id) })
+            }
+
+            Res::Def(
+                HirDefKind::Macro(..)
+                | HirDefKind::ExternCrate
+                | HirDefKind::ForeignMod
+                | HirDefKind::LifetimeParam
+                | HirDefKind::AnonConst
+                | HirDefKind::Use
+                | HirDefKind::Field
+                | HirDefKind::GlobalAsm
+                | HirDefKind::Impl
+                | HirDefKind::Closure
+                | HirDefKind::Generator,
+                _,
+            )
+            | Res::PrimTy(..)
+            | Res::SelfTy(..)
+            | Res::ToolMod
+            | Res::NonMacroAttr(..)
+            | Res::SelfCtor(..)
+            | Res::Err => None,
+        }
+    }
+
+    pub fn get_field_ref_data(
+        &self,
+        field_ref: &hir::Field<'_>,
+        variant: &ty::VariantDef,
+    ) -> Option<Ref> {
+        filter!(self.span_utils, field_ref.ident.span);
+        self.tcx.find_field_index(field_ref.ident, variant).map(|index| {
+            let span = self.span_from_span(field_ref.ident.span);
+            Ref { kind: RefKind::Variable, span, ref_id: id_from_def_id(variant.fields[index].did) }
+        })
+    }
+
+    /// Attempt to return MacroRef for any AST node.
+    ///
+    /// 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,
+    /// else uses the expansion callsite and callee to return some MacroRef.
+    pub fn get_macro_use_data(&self, span: Span) -> Option<MacroRef> {
+        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.
+        let callsite = span.source_callsite();
+        let callsite_span = self.span_from_span(callsite);
+        let callee = span.source_callee()?;
+
+        let mac_name = match callee.kind {
+            ExpnKind::Macro(kind, name) => match kind {
+                MacroKind::Bang => name,
+
+                // Ignore attribute macros, their spans are usually mangled
+                // FIXME(eddyb) is this really the case anymore?
+                MacroKind::Attr | MacroKind::Derive => return None,
+            },
+
+            // These are not macros.
+            // FIXME(eddyb) maybe there is a way to handle them usefully?
+            ExpnKind::Root | ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => return None,
+        };
+
+        let callee_span = self.span_from_span(callee.def_site);
+        Some(MacroRef {
+            span: callsite_span,
+            qualname: mac_name.to_string(), // FIXME: generate the real qualname
+            callee_span,
+        })
+    }
+
+    fn lookup_def_id(&self, ref_id: hir::HirId) -> Option<DefId> {
+        match self.get_path_res(ref_id) {
+            Res::PrimTy(_) | Res::SelfTy(..) | Res::Err => None,
+            def => def.opt_def_id(),
+        }
+    }
+
+    fn docs_for_attrs(&self, attrs: &[ast::Attribute]) -> String {
+        let mut result = String::new();
+
+        for attr in attrs {
+            if let Some(val) = attr.doc_str() {
+                // FIXME: Should save-analysis beautify doc strings itself or leave it to users?
+                result.push_str(&beautify_doc_string(val));
+                result.push('\n');
+            } else if self.tcx.sess.check_name(attr, sym::doc) {
+                if let Some(meta_list) = attr.meta_item_list() {
+                    meta_list
+                        .into_iter()
+                        .filter(|it| it.has_name(sym::include))
+                        .filter_map(|it| it.meta_item_list().map(|l| l.to_owned()))
+                        .flat_map(|it| it)
+                        .filter(|meta| meta.has_name(sym::contents))
+                        .filter_map(|meta| meta.value_str())
+                        .for_each(|val| {
+                            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
+    }
+
+    fn next_impl_id(&self) -> u32 {
+        let next = self.impl_counter.get();
+        self.impl_counter.set(next + 1);
+        next
+    }
+}
+
+// An AST visitor for collecting paths (e.g., the names of structs) and formal
+// variables (idents) from patterns.
+struct PathCollector<'l> {
+    tcx: TyCtxt<'l>,
+    collected_paths: Vec<(hir::HirId, &'l hir::QPath<'l>)>,
+    collected_idents: Vec<(hir::HirId, Ident, hir::Mutability)>,
+}
+
+impl<'l> PathCollector<'l> {
+    fn new(tcx: TyCtxt<'l>) -> PathCollector<'l> {
+        PathCollector { tcx, collected_paths: vec![], collected_idents: vec![] }
+    }
+}
+
+impl<'l> Visitor<'l> for PathCollector<'l> {
+    type Map = Map<'l>;
+
+    fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
+        intravisit::NestedVisitorMap::All(self.tcx.hir())
+    }
+
+    fn visit_pat(&mut self, p: &'l hir::Pat<'l>) {
+        match p.kind {
+            hir::PatKind::Struct(ref path, ..) => {
+                self.collected_paths.push((p.hir_id, path));
+            }
+            hir::PatKind::TupleStruct(ref path, ..) | hir::PatKind::Path(ref path) => {
+                self.collected_paths.push((p.hir_id, path));
+            }
+            hir::PatKind::Binding(bm, _, ident, _) => {
+                debug!(
+                    "PathCollector, visit ident in pat {}: {:?} {:?}",
+                    ident, p.span, ident.span
+                );
+                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.
+                    hir::BindingAnnotation::Unannotated | hir::BindingAnnotation::Ref => {
+                        hir::Mutability::Not
+                    }
+                    hir::BindingAnnotation::Mutable | hir::BindingAnnotation::RefMut => {
+                        hir::Mutability::Mut
+                    }
+                };
+                self.collected_idents.push((p.hir_id, ident, immut));
+            }
+            _ => {}
+        }
+        intravisit::walk_pat(self, p);
+    }
+}
+
+/// Defines what to do with the results of saving the analysis.
+pub trait SaveHandler {
+    fn save(&mut self, save_ctxt: &SaveContext<'_>, analysis: &Analysis);
+}
+
+/// Dump the save-analysis results to a file.
+pub struct DumpHandler<'a> {
+    odir: Option<&'a Path>,
+    cratename: String,
+}
+
+impl<'a> DumpHandler<'a> {
+    pub fn new(odir: Option<&'a Path>, cratename: &str) -> DumpHandler<'a> {
+        DumpHandler { odir, cratename: cratename.to_owned() }
+    }
+
+    fn output_file(&self, ctx: &SaveContext<'_>) -> (BufWriter<File>, PathBuf) {
+        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"),
+                };
+
+                if let Err(e) = std::fs::create_dir_all(&root_path) {
+                    error!("Could not create directory {}: {}", root_path.display(), e);
+                }
+
+                let executable = sess.crate_types().iter().any(|ct| *ct == CrateType::Executable);
+                let mut out_name = if executable { String::new() } else { "lib".to_owned() };
+                out_name.push_str(&self.cratename);
+                out_name.push_str(&sess.opts.cg.extra_filename);
+                out_name.push_str(".json");
+                root_path.push(&out_name);
+
+                root_path
+            }
+        };
+
+        info!("Writing output to {}", file_name.display());
+
+        let output_file = BufWriter::new(File::create(&file_name).unwrap_or_else(|e| {
+            sess.fatal(&format!("Could not open {}: {}", file_name.display(), e))
+        }));
+
+        (output_file, file_name)
+    }
+}
+
+impl SaveHandler for DumpHandler<'_> {
+    fn save(&mut self, save_ctxt: &SaveContext<'_>, analysis: &Analysis) {
+        let sess = &save_ctxt.tcx.sess;
+        let (output, file_name) = self.output_file(&save_ctxt);
+        if let Err(e) = serde_json::to_writer(output, &analysis) {
+            error!("Can't serialize save-analysis: {:?}", e);
+        }
+
+        if sess.opts.json_artifact_notifications {
+            sess.parse_sess.span_diagnostic.emit_artifact_notification(&file_name, "save-analysis");
+        }
+    }
+}
+
+/// Call a callback with the results of save-analysis.
+pub struct CallbackHandler<'b> {
+    pub callback: &'b mut dyn FnMut(&rls_data::Analysis),
+}
+
+impl SaveHandler for CallbackHandler<'_> {
+    fn save(&mut self, _: &SaveContext<'_>, analysis: &Analysis) {
+        (self.callback)(analysis)
+    }
+}
+
+pub fn process_crate<'l, 'tcx, H: SaveHandler>(
+    tcx: TyCtxt<'tcx>,
+    cratename: &str,
+    input: &'l Input,
+    config: Option<Config>,
+    mut handler: H,
+) {
+    with_no_trimmed_paths(|| {
+        tcx.dep_graph.with_ignore(|| {
+            info!("Dumping crate {}", cratename);
+
+            // Privacy checking requires and is done after type checking; use a
+            // fallback in case the access levels couldn't have been correctly computed.
+            let access_levels = match tcx.sess.compile_status() {
+                Ok(..) => tcx.privacy_access_levels(LOCAL_CRATE),
+                Err(..) => tcx.arena.alloc(AccessLevels::default()),
+            };
+
+            let save_ctxt = SaveContext {
+                tcx,
+                maybe_typeck_results: None,
+                access_levels: &access_levels,
+                span_utils: SpanUtils::new(&tcx.sess),
+                config: find_config(config),
+                impl_counter: Cell::new(0),
+            };
+
+            let mut visitor = DumpVisitor::new(save_ctxt);
+
+            visitor.dump_crate_info(cratename, tcx.hir().krate());
+            visitor.dump_compilation_options(input, cratename);
+            visitor.process_crate(tcx.hir().krate());
+
+            handler.save(&visitor.save_ctxt, &visitor.analysis())
+        })
+    })
+}
+
+fn find_config(supplied: Option<Config>) -> Config {
+    if let Some(config) = supplied {
+        return config;
+    }
+
+    match env::var_os("RUST_SAVE_ANALYSIS_CONFIG") {
+        None => Config::default(),
+        Some(config) => config
+            .to_str()
+            .ok_or(())
+            .map_err(|_| error!("`RUST_SAVE_ANALYSIS_CONFIG` isn't UTF-8"))
+            .and_then(|cfg| {
+                serde_json::from_str(cfg)
+                    .map_err(|_| error!("Could not deserialize save-analysis config"))
+            })
+            .unwrap_or_default(),
+    }
+}
+
+// Utility functions for the module.
+
+// Helper function to escape quotes in a string
+fn escape(s: String) -> String {
+    s.replace("\"", "\"\"")
+}
+
+// Helper function to determine if a span came from a
+// macro expansion or syntax extension.
+fn generated_code(span: Span) -> bool {
+    span.from_expansion() || span.is_dummy()
+}
+
+// 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_hir_id(id: hir::HirId, scx: &SaveContext<'_>) -> rls_data::Id {
+    let def_id = scx.tcx.hir().opt_local_def_id(id);
+    def_id.map(|id| id_from_def_id(id.to_def_id())).unwrap_or_else(|| {
+        // Create a *fake* `DefId` out of a `HirId` by combining the owner
+        // `local_def_index` and the `local_id`.
+        // 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.owner.local_def_index.as_u32() | id.local_id.as_u32().reverse_bits(),
+        }
+    })
+}
+
+fn null_id() -> rls_data::Id {
+    rls_data::Id { krate: u32::MAX, index: u32::MAX }
+}
+
+fn lower_attributes(attrs: Vec<ast::Attribute>, scx: &SaveContext<'_>) -> Vec<rls_data::Attribute> {
+    attrs
+        .into_iter()
+        // Only retain real attributes. Doc comments are lowered separately.
+        .filter(|attr| !attr.has_name(sym::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 = 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 { value, span: scx.span_from_span(attr.span) }
+        })
+        .collect()
+}
diff --git a/compiler/rustc_save_analysis/src/sig.rs b/compiler/rustc_save_analysis/src/sig.rs
new file mode 100644
index 0000000..747e198
--- /dev/null
+++ b/compiler/rustc_save_analysis/src/sig.rs
@@ -0,0 +1,930 @@
+// A signature is a string representation of an item's type signature, excluding
+// any body. It also includes ids for any defs or refs in the signature. For
+// example:
+//
+// ```
+// fn foo(x: String) {
+//     println!("{}", x);
+// }
+// ```
+// The signature string is something like "fn foo(x: String) {}" and the signature
+// will have defs for `foo` and `x` and a ref for `String`.
+//
+// All signature text should parse in the correct context (i.e., in a module or
+// impl, etc.). Clients may want to trim trailing `{}` or `;`. The text of a
+// signature is not guaranteed to be stable (it may improve or change as the
+// syntax changes, or whitespace or punctuation may change). It is also likely
+// not to be pretty - no attempt is made to prettify the text. It is recommended
+// that clients run the text through Rustfmt.
+//
+// This module generates Signatures for items by walking the AST and looking up
+// references.
+//
+// Signatures do not include visibility info. I'm not sure if this is a feature
+// or an ommission (FIXME).
+//
+// FIXME where clauses need implementing, defs/refs in generics are mostly missing.
+
+use crate::{id_from_def_id, id_from_hir_id, SaveContext};
+
+use rls_data::{SigElement, Signature};
+
+use rustc_ast::Mutability;
+use rustc_hir as hir;
+use rustc_hir::def::{DefKind, Res};
+use rustc_hir_pretty::id_to_string;
+use rustc_hir_pretty::{bounds_to_string, path_segment_to_string, path_to_string, ty_to_string};
+use rustc_span::symbol::{Ident, Symbol};
+
+pub fn item_signature(item: &hir::Item<'_>, scx: &SaveContext<'_>) -> Option<Signature> {
+    if !scx.config.signatures {
+        return None;
+    }
+    item.make(0, None, scx).ok()
+}
+
+pub fn foreign_item_signature(
+    item: &hir::ForeignItem<'_>,
+    scx: &SaveContext<'_>,
+) -> Option<Signature> {
+    if !scx.config.signatures {
+        return None;
+    }
+    item.make(0, None, scx).ok()
+}
+
+/// Signature for a struct or tuple field declaration.
+/// Does not include a trailing comma.
+pub fn field_signature(field: &hir::StructField<'_>, scx: &SaveContext<'_>) -> Option<Signature> {
+    if !scx.config.signatures {
+        return None;
+    }
+    field.make(0, None, scx).ok()
+}
+
+/// Does not include a trailing comma.
+pub fn variant_signature(variant: &hir::Variant<'_>, scx: &SaveContext<'_>) -> Option<Signature> {
+    if !scx.config.signatures {
+        return None;
+    }
+    variant.make(0, None, scx).ok()
+}
+
+pub fn method_signature(
+    id: hir::HirId,
+    ident: Ident,
+    generics: &hir::Generics<'_>,
+    m: &hir::FnSig<'_>,
+    scx: &SaveContext<'_>,
+) -> Option<Signature> {
+    if !scx.config.signatures {
+        return None;
+    }
+    make_method_signature(id, ident, generics, m, scx).ok()
+}
+
+pub fn assoc_const_signature(
+    id: hir::HirId,
+    ident: Symbol,
+    ty: &hir::Ty<'_>,
+    default: Option<&hir::Expr<'_>>,
+    scx: &SaveContext<'_>,
+) -> Option<Signature> {
+    if !scx.config.signatures {
+        return None;
+    }
+    make_assoc_const_signature(id, ident, ty, default, scx).ok()
+}
+
+pub fn assoc_type_signature(
+    id: hir::HirId,
+    ident: Ident,
+    bounds: Option<hir::GenericBounds<'_>>,
+    default: Option<&hir::Ty<'_>>,
+    scx: &SaveContext<'_>,
+) -> Option<Signature> {
+    if !scx.config.signatures {
+        return None;
+    }
+    make_assoc_type_signature(id, ident, bounds, default, scx).ok()
+}
+
+type Result = std::result::Result<Signature, &'static str>;
+
+trait Sig {
+    fn make(&self, offset: usize, id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result;
+}
+
+fn extend_sig(
+    mut sig: Signature,
+    text: String,
+    defs: Vec<SigElement>,
+    refs: Vec<SigElement>,
+) -> Signature {
+    sig.text = text;
+    sig.defs.extend(defs.into_iter());
+    sig.refs.extend(refs.into_iter());
+    sig
+}
+
+fn replace_text(mut sig: Signature, text: String) -> Signature {
+    sig.text = text;
+    sig
+}
+
+fn merge_sigs(text: String, sigs: Vec<Signature>) -> Signature {
+    let mut result = Signature { text, defs: vec![], refs: vec![] };
+
+    let (defs, refs): (Vec<_>, Vec<_>) = sigs.into_iter().map(|s| (s.defs, s.refs)).unzip();
+
+    result.defs.extend(defs.into_iter().flat_map(|ds| ds.into_iter()));
+    result.refs.extend(refs.into_iter().flat_map(|rs| rs.into_iter()));
+
+    result
+}
+
+fn text_sig(text: String) -> Signature {
+    Signature { text, defs: vec![], refs: vec![] }
+}
+
+impl<'hir> Sig for hir::Ty<'hir> {
+    fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
+        let id = Some(self.hir_id);
+        match self.kind {
+            hir::TyKind::Slice(ref ty) => {
+                let nested = ty.make(offset + 1, id, scx)?;
+                let text = format!("[{}]", nested.text);
+                Ok(replace_text(nested, text))
+            }
+            hir::TyKind::Ptr(ref mt) => {
+                let prefix = match mt.mutbl {
+                    hir::Mutability::Mut => "*mut ",
+                    hir::Mutability::Not => "*const ",
+                };
+                let nested = mt.ty.make(offset + prefix.len(), id, scx)?;
+                let text = format!("{}{}", prefix, nested.text);
+                Ok(replace_text(nested, text))
+            }
+            hir::TyKind::Rptr(ref lifetime, ref mt) => {
+                let mut prefix = "&".to_owned();
+                prefix.push_str(&lifetime.name.ident().to_string());
+                prefix.push(' ');
+                if let hir::Mutability::Mut = mt.mutbl {
+                    prefix.push_str("mut ");
+                };
+
+                let nested = mt.ty.make(offset + prefix.len(), id, scx)?;
+                let text = format!("{}{}", prefix, nested.text);
+                Ok(replace_text(nested, text))
+            }
+            hir::TyKind::Never => Ok(text_sig("!".to_owned())),
+            hir::TyKind::Tup(ts) => {
+                let mut text = "(".to_owned();
+                let mut defs = vec![];
+                let mut refs = vec![];
+                for t in ts {
+                    let nested = t.make(offset + text.len(), id, scx)?;
+                    text.push_str(&nested.text);
+                    text.push(',');
+                    defs.extend(nested.defs.into_iter());
+                    refs.extend(nested.refs.into_iter());
+                }
+                text.push(')');
+                Ok(Signature { text, defs, refs })
+            }
+            hir::TyKind::BareFn(ref f) => {
+                let mut text = String::new();
+                if !f.generic_params.is_empty() {
+                    // FIXME defs, bounds on lifetimes
+                    text.push_str("for<");
+                    text.push_str(
+                        &f.generic_params
+                            .iter()
+                            .filter_map(|param| match param.kind {
+                                hir::GenericParamKind::Lifetime { .. } => {
+                                    Some(param.name.ident().to_string())
+                                }
+                                _ => None,
+                            })
+                            .collect::<Vec<_>>()
+                            .join(", "),
+                    );
+                    text.push('>');
+                }
+
+                if let hir::Unsafety::Unsafe = f.unsafety {
+                    text.push_str("unsafe ");
+                }
+                text.push_str("fn(");
+
+                let mut defs = vec![];
+                let mut refs = vec![];
+                for i in f.decl.inputs {
+                    let nested = i.make(offset + text.len(), Some(i.hir_id), scx)?;
+                    text.push_str(&nested.text);
+                    text.push(',');
+                    defs.extend(nested.defs.into_iter());
+                    refs.extend(nested.refs.into_iter());
+                }
+                text.push(')');
+                if let hir::FnRetTy::Return(ref t) = f.decl.output {
+                    text.push_str(" -> ");
+                    let nested = t.make(offset + text.len(), None, scx)?;
+                    text.push_str(&nested.text);
+                    text.push(',');
+                    defs.extend(nested.defs.into_iter());
+                    refs.extend(nested.refs.into_iter());
+                }
+
+                Ok(Signature { text, defs, refs })
+            }
+            hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) => path.make(offset, id, scx),
+            hir::TyKind::Path(hir::QPath::Resolved(Some(ref qself), ref path)) => {
+                let nested_ty = qself.make(offset + 1, id, scx)?;
+                let prefix = format!(
+                    "<{} as {}>::",
+                    nested_ty.text,
+                    path_segment_to_string(&path.segments[0])
+                );
+
+                let name = path_segment_to_string(path.segments.last().ok_or("Bad path")?);
+                let res = scx.get_path_res(id.ok_or("Missing id for Path")?);
+                let id = id_from_def_id(res.def_id());
+                if path.segments.len() == 2 {
+                    let start = offset + prefix.len();
+                    let end = start + name.len();
+
+                    Ok(Signature {
+                        text: prefix + &name,
+                        defs: vec![],
+                        refs: vec![SigElement { id, start, end }],
+                    })
+                } else {
+                    let start = offset + prefix.len() + 5;
+                    let end = start + name.len();
+                    // FIXME should put the proper path in there, not elipses.
+                    Ok(Signature {
+                        text: prefix + "...::" + &name,
+                        defs: vec![],
+                        refs: vec![SigElement { id, start, end }],
+                    })
+                }
+            }
+            hir::TyKind::Path(hir::QPath::TypeRelative(ty, segment)) => {
+                let nested_ty = ty.make(offset + 1, id, scx)?;
+                let prefix = format!("<{}>::", nested_ty.text,);
+
+                let name = path_segment_to_string(segment);
+                let res = scx.get_path_res(id.ok_or("Missing id for Path")?);
+                let id = id_from_def_id(res.def_id());
+
+                let start = offset + prefix.len();
+                let end = start + name.len();
+                Ok(Signature {
+                    text: prefix + &name,
+                    defs: vec![],
+                    refs: vec![SigElement { id, start, end }],
+                })
+            }
+            hir::TyKind::Path(hir::QPath::LangItem(lang_item, _)) => {
+                Ok(text_sig(format!("#[lang = \"{}\"]", lang_item.name())))
+            }
+            hir::TyKind::TraitObject(bounds, ..) => {
+                // FIXME recurse into bounds
+                let bounds: Vec<hir::GenericBound<'_>> = bounds
+                    .iter()
+                    .map(|hir::PolyTraitRef { bound_generic_params, trait_ref, span }| {
+                        hir::GenericBound::Trait(
+                            hir::PolyTraitRef {
+                                bound_generic_params,
+                                trait_ref: hir::TraitRef {
+                                    path: trait_ref.path,
+                                    hir_ref_id: trait_ref.hir_ref_id,
+                                },
+                                span: *span,
+                            },
+                            hir::TraitBoundModifier::None,
+                        )
+                    })
+                    .collect();
+                let nested = bounds_to_string(&bounds);
+                Ok(text_sig(nested))
+            }
+            hir::TyKind::Array(ref ty, ref anon_const) => {
+                let nested_ty = ty.make(offset + 1, id, scx)?;
+                let expr = id_to_string(&scx.tcx.hir(), anon_const.body.hir_id).replace('\n', " ");
+                let text = format!("[{}; {}]", nested_ty.text, expr);
+                Ok(replace_text(nested_ty, text))
+            }
+            hir::TyKind::OpaqueDef(item_id, _) => {
+                let item = scx.tcx.hir().item(item_id.id);
+                item.make(offset, Some(item_id.id), scx)
+            }
+            hir::TyKind::Typeof(_) | hir::TyKind::Infer | hir::TyKind::Err => Err("Ty"),
+        }
+    }
+}
+
+impl<'hir> Sig for hir::Item<'hir> {
+    fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
+        let id = Some(self.hir_id);
+
+        match self.kind {
+            hir::ItemKind::Static(ref ty, m, ref body) => {
+                let mut text = "static ".to_owned();
+                if m == hir::Mutability::Mut {
+                    text.push_str("mut ");
+                }
+                let name = self.ident.to_string();
+                let defs = vec![SigElement {
+                    id: id_from_hir_id(self.hir_id, scx),
+                    start: offset + text.len(),
+                    end: offset + text.len() + name.len(),
+                }];
+                text.push_str(&name);
+                text.push_str(": ");
+
+                let ty = ty.make(offset + text.len(), id, scx)?;
+                text.push_str(&ty.text);
+
+                text.push_str(" = ");
+                let expr = id_to_string(&scx.tcx.hir(), body.hir_id).replace('\n', " ");
+                text.push_str(&expr);
+
+                text.push(';');
+
+                Ok(extend_sig(ty, text, defs, vec![]))
+            }
+            hir::ItemKind::Const(ref ty, ref body) => {
+                let mut text = "const ".to_owned();
+                let name = self.ident.to_string();
+                let defs = vec![SigElement {
+                    id: id_from_hir_id(self.hir_id, scx),
+                    start: offset + text.len(),
+                    end: offset + text.len() + name.len(),
+                }];
+                text.push_str(&name);
+                text.push_str(": ");
+
+                let ty = ty.make(offset + text.len(), id, scx)?;
+                text.push_str(&ty.text);
+
+                text.push_str(" = ");
+                let expr = id_to_string(&scx.tcx.hir(), body.hir_id).replace('\n', " ");
+                text.push_str(&expr);
+
+                text.push(';');
+
+                Ok(extend_sig(ty, text, defs, vec![]))
+            }
+            hir::ItemKind::Fn(hir::FnSig { ref decl, header, span: _ }, ref generics, _) => {
+                let mut text = String::new();
+                if let hir::Constness::Const = header.constness {
+                    text.push_str("const ");
+                }
+                if hir::IsAsync::Async == header.asyncness {
+                    text.push_str("async ");
+                }
+                if let hir::Unsafety::Unsafe = header.unsafety {
+                    text.push_str("unsafe ");
+                }
+                text.push_str("fn ");
+
+                let mut sig =
+                    name_and_generics(text, offset, generics, self.hir_id, self.ident, scx)?;
+
+                sig.text.push('(');
+                for i in decl.inputs {
+                    // FIXME should descend into patterns to add defs.
+                    sig.text.push_str(": ");
+                    let nested = i.make(offset + sig.text.len(), Some(i.hir_id), scx)?;
+                    sig.text.push_str(&nested.text);
+                    sig.text.push(',');
+                    sig.defs.extend(nested.defs.into_iter());
+                    sig.refs.extend(nested.refs.into_iter());
+                }
+                sig.text.push(')');
+
+                if let hir::FnRetTy::Return(ref t) = decl.output {
+                    sig.text.push_str(" -> ");
+                    let nested = t.make(offset + sig.text.len(), None, scx)?;
+                    sig.text.push_str(&nested.text);
+                    sig.defs.extend(nested.defs.into_iter());
+                    sig.refs.extend(nested.refs.into_iter());
+                }
+                sig.text.push_str(" {}");
+
+                Ok(sig)
+            }
+            hir::ItemKind::Mod(ref _mod) => {
+                let mut text = "mod ".to_owned();
+                let name = self.ident.to_string();
+                let defs = vec![SigElement {
+                    id: id_from_hir_id(self.hir_id, scx),
+                    start: offset + text.len(),
+                    end: offset + text.len() + name.len(),
+                }];
+                text.push_str(&name);
+                // Could be either `mod foo;` or `mod foo { ... }`, but we'll just pick one.
+                text.push(';');
+
+                Ok(Signature { text, defs, refs: vec![] })
+            }
+            hir::ItemKind::TyAlias(ref ty, ref generics) => {
+                let text = "type ".to_owned();
+                let mut sig =
+                    name_and_generics(text, offset, generics, self.hir_id, self.ident, scx)?;
+
+                sig.text.push_str(" = ");
+                let ty = ty.make(offset + sig.text.len(), id, scx)?;
+                sig.text.push_str(&ty.text);
+                sig.text.push(';');
+
+                Ok(merge_sigs(sig.text.clone(), vec![sig, ty]))
+            }
+            hir::ItemKind::Enum(_, ref generics) => {
+                let text = "enum ".to_owned();
+                let mut sig =
+                    name_and_generics(text, offset, generics, self.hir_id, self.ident, scx)?;
+                sig.text.push_str(" {}");
+                Ok(sig)
+            }
+            hir::ItemKind::Struct(_, ref generics) => {
+                let text = "struct ".to_owned();
+                let mut sig =
+                    name_and_generics(text, offset, generics, self.hir_id, self.ident, scx)?;
+                sig.text.push_str(" {}");
+                Ok(sig)
+            }
+            hir::ItemKind::Union(_, ref generics) => {
+                let text = "union ".to_owned();
+                let mut sig =
+                    name_and_generics(text, offset, generics, self.hir_id, self.ident, scx)?;
+                sig.text.push_str(" {}");
+                Ok(sig)
+            }
+            hir::ItemKind::Trait(is_auto, unsafety, ref generics, bounds, _) => {
+                let mut text = String::new();
+
+                if is_auto == hir::IsAuto::Yes {
+                    text.push_str("auto ");
+                }
+
+                if let hir::Unsafety::Unsafe = unsafety {
+                    text.push_str("unsafe ");
+                }
+                text.push_str("trait ");
+                let mut sig =
+                    name_and_generics(text, offset, generics, self.hir_id, self.ident, scx)?;
+
+                if !bounds.is_empty() {
+                    sig.text.push_str(": ");
+                    sig.text.push_str(&bounds_to_string(bounds));
+                }
+                // FIXME where clause
+                sig.text.push_str(" {}");
+
+                Ok(sig)
+            }
+            hir::ItemKind::TraitAlias(ref generics, bounds) => {
+                let mut text = String::new();
+                text.push_str("trait ");
+                let mut sig =
+                    name_and_generics(text, offset, generics, self.hir_id, self.ident, scx)?;
+
+                if !bounds.is_empty() {
+                    sig.text.push_str(" = ");
+                    sig.text.push_str(&bounds_to_string(bounds));
+                }
+                // FIXME where clause
+                sig.text.push(';');
+
+                Ok(sig)
+            }
+            hir::ItemKind::Impl {
+                unsafety,
+                polarity,
+                defaultness,
+                defaultness_span: _,
+                constness,
+                ref generics,
+                ref of_trait,
+                ref self_ty,
+                items: _,
+            } => {
+                let mut text = String::new();
+                if let hir::Defaultness::Default { .. } = defaultness {
+                    text.push_str("default ");
+                }
+                if let hir::Unsafety::Unsafe = unsafety {
+                    text.push_str("unsafe ");
+                }
+                text.push_str("impl");
+                if let hir::Constness::Const = constness {
+                    text.push_str(" const");
+                }
+
+                let generics_sig = generics.make(offset + text.len(), id, scx)?;
+                text.push_str(&generics_sig.text);
+
+                text.push(' ');
+
+                let trait_sig = if let Some(ref t) = *of_trait {
+                    if let hir::ImplPolarity::Negative(_) = polarity {
+                        text.push('!');
+                    }
+                    let trait_sig = t.path.make(offset + text.len(), id, scx)?;
+                    text.push_str(&trait_sig.text);
+                    text.push_str(" for ");
+                    trait_sig
+                } else {
+                    text_sig(String::new())
+                };
+
+                let ty_sig = self_ty.make(offset + text.len(), id, scx)?;
+                text.push_str(&ty_sig.text);
+
+                text.push_str(" {}");
+
+                Ok(merge_sigs(text, vec![generics_sig, trait_sig, ty_sig]))
+
+                // FIXME where clause
+            }
+            hir::ItemKind::ForeignMod(_) => Err("extern mod"),
+            hir::ItemKind::GlobalAsm(_) => Err("glboal asm"),
+            hir::ItemKind::ExternCrate(_) => Err("extern crate"),
+            hir::ItemKind::OpaqueTy(..) => Err("opaque type"),
+            // FIXME should implement this (e.g., pub use).
+            hir::ItemKind::Use(..) => Err("import"),
+        }
+    }
+}
+
+impl<'hir> Sig for hir::Path<'hir> {
+    fn make(&self, offset: usize, id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
+        let res = scx.get_path_res(id.ok_or("Missing id for Path")?);
+
+        let (name, start, end) = match res {
+            Res::PrimTy(..) | Res::SelfTy(..) | Res::Err => {
+                return Ok(Signature { text: path_to_string(self), defs: vec![], refs: vec![] });
+            }
+            Res::Def(DefKind::AssocConst | DefKind::Variant | DefKind::Ctor(..), _) => {
+                let len = self.segments.len();
+                if len < 2 {
+                    return Err("Bad path");
+                }
+                // FIXME: really we should descend into the generics here and add SigElements for
+                // them.
+                // FIXME: would be nice to have a def for the first path segment.
+                let seg1 = path_segment_to_string(&self.segments[len - 2]);
+                let seg2 = path_segment_to_string(&self.segments[len - 1]);
+                let start = offset + seg1.len() + 2;
+                (format!("{}::{}", seg1, seg2), start, start + seg2.len())
+            }
+            _ => {
+                let name = path_segment_to_string(self.segments.last().ok_or("Bad path")?);
+                let end = offset + name.len();
+                (name, offset, end)
+            }
+        };
+
+        let id = id_from_def_id(res.def_id());
+        Ok(Signature { text: name, defs: vec![], refs: vec![SigElement { id, start, end }] })
+    }
+}
+
+// This does not cover the where clause, which must be processed separately.
+impl<'hir> Sig for hir::Generics<'hir> {
+    fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
+        if self.params.is_empty() {
+            return Ok(text_sig(String::new()));
+        }
+
+        let mut text = "<".to_owned();
+
+        let mut defs = Vec::with_capacity(self.params.len());
+        for param in self.params {
+            let mut param_text = String::new();
+            if let hir::GenericParamKind::Const { .. } = param.kind {
+                param_text.push_str("const ");
+            }
+            param_text.push_str(&param.name.ident().as_str());
+            defs.push(SigElement {
+                id: id_from_hir_id(param.hir_id, scx),
+                start: offset + text.len(),
+                end: offset + text.len() + param_text.as_str().len(),
+            });
+            if let hir::GenericParamKind::Const { ref ty } = param.kind {
+                param_text.push_str(": ");
+                param_text.push_str(&ty_to_string(&ty));
+            }
+            if !param.bounds.is_empty() {
+                param_text.push_str(": ");
+                match param.kind {
+                    hir::GenericParamKind::Lifetime { .. } => {
+                        let bounds = param
+                            .bounds
+                            .iter()
+                            .map(|bound| match bound {
+                                hir::GenericBound::Outlives(lt) => lt.name.ident().to_string(),
+                                _ => panic!(),
+                            })
+                            .collect::<Vec<_>>()
+                            .join(" + ");
+                        param_text.push_str(&bounds);
+                        // FIXME add lifetime bounds refs.
+                    }
+                    hir::GenericParamKind::Type { .. } => {
+                        param_text.push_str(&bounds_to_string(param.bounds));
+                        // FIXME descend properly into bounds.
+                    }
+                    hir::GenericParamKind::Const { .. } => {
+                        // Const generics cannot contain bounds.
+                    }
+                }
+            }
+            text.push_str(&param_text);
+            text.push(',');
+        }
+
+        text.push('>');
+        Ok(Signature { text, defs, refs: vec![] })
+    }
+}
+
+impl<'hir> Sig for hir::StructField<'hir> {
+    fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
+        let mut text = String::new();
+
+        text.push_str(&self.ident.to_string());
+        let defs = Some(SigElement {
+            id: id_from_hir_id(self.hir_id, scx),
+            start: offset,
+            end: offset + text.len(),
+        });
+        text.push_str(": ");
+
+        let mut ty_sig = self.ty.make(offset + text.len(), Some(self.hir_id), scx)?;
+        text.push_str(&ty_sig.text);
+        ty_sig.text = text;
+        ty_sig.defs.extend(defs.into_iter());
+        Ok(ty_sig)
+    }
+}
+
+impl<'hir> Sig for hir::Variant<'hir> {
+    fn make(&self, offset: usize, parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
+        let mut text = self.ident.to_string();
+        match self.data {
+            hir::VariantData::Struct(fields, r) => {
+                let id = parent_id.unwrap();
+                let name_def = SigElement {
+                    id: id_from_hir_id(id, scx),
+                    start: offset,
+                    end: offset + text.len(),
+                };
+                text.push_str(" { ");
+                let mut defs = vec![name_def];
+                let mut refs = vec![];
+                if r {
+                    text.push_str("/* parse error */ ");
+                } else {
+                    for f in fields {
+                        let field_sig = f.make(offset + text.len(), Some(id), scx)?;
+                        text.push_str(&field_sig.text);
+                        text.push_str(", ");
+                        defs.extend(field_sig.defs.into_iter());
+                        refs.extend(field_sig.refs.into_iter());
+                    }
+                }
+                text.push('}');
+                Ok(Signature { text, defs, refs })
+            }
+            hir::VariantData::Tuple(fields, id) => {
+                let name_def = SigElement {
+                    id: id_from_hir_id(id, scx),
+                    start: offset,
+                    end: offset + text.len(),
+                };
+                text.push('(');
+                let mut defs = vec![name_def];
+                let mut refs = vec![];
+                for f in fields {
+                    let field_sig = f.make(offset + text.len(), Some(id), scx)?;
+                    text.push_str(&field_sig.text);
+                    text.push_str(", ");
+                    defs.extend(field_sig.defs.into_iter());
+                    refs.extend(field_sig.refs.into_iter());
+                }
+                text.push(')');
+                Ok(Signature { text, defs, refs })
+            }
+            hir::VariantData::Unit(id) => {
+                let name_def = SigElement {
+                    id: id_from_hir_id(id, scx),
+                    start: offset,
+                    end: offset + text.len(),
+                };
+                Ok(Signature { text, defs: vec![name_def], refs: vec![] })
+            }
+        }
+    }
+}
+
+impl<'hir> Sig for hir::ForeignItem<'hir> {
+    fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
+        let id = Some(self.hir_id);
+        match self.kind {
+            hir::ForeignItemKind::Fn(decl, _, ref generics) => {
+                let mut text = String::new();
+                text.push_str("fn ");
+
+                let mut sig =
+                    name_and_generics(text, offset, generics, self.hir_id, self.ident, scx)?;
+
+                sig.text.push('(');
+                for i in decl.inputs {
+                    sig.text.push_str(": ");
+                    let nested = i.make(offset + sig.text.len(), Some(i.hir_id), scx)?;
+                    sig.text.push_str(&nested.text);
+                    sig.text.push(',');
+                    sig.defs.extend(nested.defs.into_iter());
+                    sig.refs.extend(nested.refs.into_iter());
+                }
+                sig.text.push(')');
+
+                if let hir::FnRetTy::Return(ref t) = decl.output {
+                    sig.text.push_str(" -> ");
+                    let nested = t.make(offset + sig.text.len(), None, scx)?;
+                    sig.text.push_str(&nested.text);
+                    sig.defs.extend(nested.defs.into_iter());
+                    sig.refs.extend(nested.refs.into_iter());
+                }
+                sig.text.push(';');
+
+                Ok(sig)
+            }
+            hir::ForeignItemKind::Static(ref ty, m) => {
+                let mut text = "static ".to_owned();
+                if m == Mutability::Mut {
+                    text.push_str("mut ");
+                }
+                let name = self.ident.to_string();
+                let defs = vec![SigElement {
+                    id: id_from_hir_id(self.hir_id, scx),
+                    start: offset + text.len(),
+                    end: offset + text.len() + name.len(),
+                }];
+                text.push_str(&name);
+                text.push_str(": ");
+
+                let ty_sig = ty.make(offset + text.len(), id, scx)?;
+                text.push(';');
+
+                Ok(extend_sig(ty_sig, text, defs, vec![]))
+            }
+            hir::ForeignItemKind::Type => {
+                let mut text = "type ".to_owned();
+                let name = self.ident.to_string();
+                let defs = vec![SigElement {
+                    id: id_from_hir_id(self.hir_id, scx),
+                    start: offset + text.len(),
+                    end: offset + text.len() + name.len(),
+                }];
+                text.push_str(&name);
+                text.push(';');
+
+                Ok(Signature { text, defs, refs: vec![] })
+            }
+        }
+    }
+}
+
+fn name_and_generics(
+    mut text: String,
+    offset: usize,
+    generics: &hir::Generics<'_>,
+    id: hir::HirId,
+    name: Ident,
+    scx: &SaveContext<'_>,
+) -> Result {
+    let name = name.to_string();
+    let def = SigElement {
+        id: id_from_hir_id(id, scx),
+        start: offset + text.len(),
+        end: offset + text.len() + name.len(),
+    };
+    text.push_str(&name);
+    let generics: Signature = generics.make(offset + text.len(), Some(id), scx)?;
+    // FIXME where clause
+    let text = format!("{}{}", text, generics.text);
+    Ok(extend_sig(generics, text, vec![def], vec![]))
+}
+
+fn make_assoc_type_signature(
+    id: hir::HirId,
+    ident: Ident,
+    bounds: Option<hir::GenericBounds<'_>>,
+    default: Option<&hir::Ty<'_>>,
+    scx: &SaveContext<'_>,
+) -> Result {
+    let mut text = "type ".to_owned();
+    let name = ident.to_string();
+    let mut defs = vec![SigElement {
+        id: id_from_hir_id(id, scx),
+        start: text.len(),
+        end: text.len() + name.len(),
+    }];
+    let mut refs = vec![];
+    text.push_str(&name);
+    if let Some(bounds) = bounds {
+        text.push_str(": ");
+        // FIXME should descend into bounds
+        text.push_str(&bounds_to_string(bounds));
+    }
+    if let Some(default) = default {
+        text.push_str(" = ");
+        let ty_sig = default.make(text.len(), Some(id), scx)?;
+        text.push_str(&ty_sig.text);
+        defs.extend(ty_sig.defs.into_iter());
+        refs.extend(ty_sig.refs.into_iter());
+    }
+    text.push(';');
+    Ok(Signature { text, defs, refs })
+}
+
+fn make_assoc_const_signature(
+    id: hir::HirId,
+    ident: Symbol,
+    ty: &hir::Ty<'_>,
+    default: Option<&hir::Expr<'_>>,
+    scx: &SaveContext<'_>,
+) -> Result {
+    let mut text = "const ".to_owned();
+    let name = ident.to_string();
+    let mut defs = vec![SigElement {
+        id: id_from_hir_id(id, scx),
+        start: text.len(),
+        end: text.len() + name.len(),
+    }];
+    let mut refs = vec![];
+    text.push_str(&name);
+    text.push_str(": ");
+
+    let ty_sig = ty.make(text.len(), Some(id), scx)?;
+    text.push_str(&ty_sig.text);
+    defs.extend(ty_sig.defs.into_iter());
+    refs.extend(ty_sig.refs.into_iter());
+
+    if let Some(default) = default {
+        text.push_str(" = ");
+        text.push_str(&id_to_string(&scx.tcx.hir(), default.hir_id));
+    }
+    text.push(';');
+    Ok(Signature { text, defs, refs })
+}
+
+fn make_method_signature(
+    id: hir::HirId,
+    ident: Ident,
+    generics: &hir::Generics<'_>,
+    m: &hir::FnSig<'_>,
+    scx: &SaveContext<'_>,
+) -> Result {
+    // FIXME code dup with function signature
+    let mut text = String::new();
+    if let hir::Constness::Const = m.header.constness {
+        text.push_str("const ");
+    }
+    if hir::IsAsync::Async == m.header.asyncness {
+        text.push_str("async ");
+    }
+    if let hir::Unsafety::Unsafe = m.header.unsafety {
+        text.push_str("unsafe ");
+    }
+    text.push_str("fn ");
+
+    let mut sig = name_and_generics(text, 0, generics, id, ident, scx)?;
+
+    sig.text.push('(');
+    for i in m.decl.inputs {
+        sig.text.push_str(": ");
+        let nested = i.make(sig.text.len(), Some(i.hir_id), scx)?;
+        sig.text.push_str(&nested.text);
+        sig.text.push(',');
+        sig.defs.extend(nested.defs.into_iter());
+        sig.refs.extend(nested.refs.into_iter());
+    }
+    sig.text.push(')');
+
+    if let hir::FnRetTy::Return(ref t) = m.decl.output {
+        sig.text.push_str(" -> ");
+        let nested = t.make(sig.text.len(), None, scx)?;
+        sig.text.push_str(&nested.text);
+        sig.defs.extend(nested.defs.into_iter());
+        sig.refs.extend(nested.refs.into_iter());
+    }
+    sig.text.push_str(" {}");
+
+    Ok(sig)
+}
diff --git a/compiler/rustc_save_analysis/src/span_utils.rs b/compiler/rustc_save_analysis/src/span_utils.rs
new file mode 100644
index 0000000..edcd492
--- /dev/null
+++ b/compiler/rustc_save_analysis/src/span_utils.rs
@@ -0,0 +1,99 @@
+use crate::generated_code;
+use rustc_data_structures::sync::Lrc;
+use rustc_lexer::{tokenize, TokenKind};
+use rustc_session::Session;
+use rustc_span::*;
+
+#[derive(Clone)]
+pub struct SpanUtils<'a> {
+    pub sess: &'a Session,
+}
+
+impl<'a> SpanUtils<'a> {
+    pub fn new(sess: &'a Session) -> SpanUtils<'a> {
+        SpanUtils { sess }
+    }
+
+    pub fn make_filename_string(&self, file: &SourceFile) -> String {
+        match &file.name {
+            FileName::Real(name) if !file.name_was_remapped => {
+                let path = name.local_path();
+                if path.is_absolute() {
+                    self.sess
+                        .source_map()
+                        .path_mapping()
+                        .map_prefix(path.into())
+                        .0
+                        .display()
+                        .to_string()
+                } else {
+                    self.sess.working_dir.0.join(&path).display().to_string()
+                }
+            }
+            // If the file name is already remapped, we assume the user
+            // configured it the way they wanted to, so use that directly
+            filename => filename.to_string(),
+        }
+    }
+
+    pub fn snippet(&self, span: Span) -> String {
+        match self.sess.source_map().span_to_snippet(span) {
+            Ok(s) => s,
+            Err(_) => String::new(),
+        }
+    }
+
+    /// Finds the span of `*` token withing the larger `span`.
+    pub fn sub_span_of_star(&self, mut span: Span) -> Option<Span> {
+        let begin = self.sess.source_map().lookup_byte_offset(span.lo());
+        let end = self.sess.source_map().lookup_byte_offset(span.hi());
+        // Make the range zero-length if the span is invalid.
+        if begin.sf.start_pos != end.sf.start_pos {
+            span = span.shrink_to_lo();
+        }
+
+        let sf = Lrc::clone(&begin.sf);
+
+        self.sess.source_map().ensure_source_file_source_present(Lrc::clone(&sf));
+        let src =
+            sf.src.clone().or_else(|| sf.external_src.borrow().get_source().map(Lrc::clone))?;
+        let to_index = |pos: BytePos| -> usize { (pos - sf.start_pos).0 as usize };
+        let text = &src[to_index(span.lo())..to_index(span.hi())];
+        let start_pos = {
+            let mut pos = 0;
+            tokenize(text)
+                .map(|token| {
+                    let start = pos;
+                    pos += token.len;
+                    (start, token)
+                })
+                .find(|(_pos, token)| token.kind == TokenKind::Star)?
+                .0
+        };
+        let lo = span.lo() + BytePos(start_pos as u32);
+        let hi = lo + BytePos(1);
+        Some(span.with_lo(lo).with_hi(hi))
+    }
+
+    /// Return true if the span is generated code, and
+    /// it is not a subspan of the root callsite.
+    ///
+    /// Used to filter out spans of minimal value,
+    /// such as references to macro internal variables.
+    pub fn filter_generated(&self, span: Span) -> bool {
+        if generated_code(span) {
+            return true;
+        }
+
+        //If the span comes from a fake source_file, filter it.
+        !self.sess.source_map().lookup_char_pos(span.lo()).file.is_real_file()
+    }
+}
+
+macro_rules! filter {
+    ($util: expr, $parent: expr) => {
+        if $util.filter_generated($parent) {
+            return None;
+        }
+    };
+}