build_reduced_graph.rs 39.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Reduced graph building
//!
//! Here we build the "reduced graph": the graph of the module tree without
//! any imports resolved.

T
Fallout  
Tamir Duberstein 已提交
16
use DefModifiers;
17 18 19
use resolve_imports::ImportDirective;
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
use resolve_imports::ImportResolution;
20 21 22
use Module;
use Namespace::{TypeNS, ValueNS};
use NameBindings;
23
use {names_to_string, module_to_string};
24
use ParentLink::{self, ModuleParentLink, BlockParentLink};
25
use Resolver;
26
use resolve_imports::Shadowable;
27
use {resolve_error, ResolutionError};
28 29 30 31 32 33

use self::DuplicateCheckingMode::*;

use rustc::metadata::csearch;
use rustc::metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
use rustc::middle::def::*;
34
use rustc::middle::def_id::{CRATE_DEF_INDEX, DefId};
35

36
use syntax::ast::{Name, NodeId};
37
use syntax::attr::AttrMetaMethods;
38
use syntax::parse::token::special_idents;
39
use syntax::codemap::{Span, DUMMY_SP};
40 41 42 43 44 45 46 47 48 49 50 51 52

use rustc_front::hir;
use rustc_front::hir::{Block, Crate, DeclItem};
use rustc_front::hir::{ForeignItem, ForeignItemFn, ForeignItemStatic};
use rustc_front::hir::{Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn};
use rustc_front::hir::{ItemForeignMod, ItemImpl, ItemMod, ItemStatic, ItemDefaultImpl};
use rustc_front::hir::{ItemStruct, ItemTrait, ItemTy, ItemUse};
use rustc_front::hir::{NamedField, PathListIdent, PathListMod, Public};
use rustc_front::hir::StmtDecl;
use rustc_front::hir::UnnamedField;
use rustc_front::hir::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
use rustc_front::hir::Visibility;
use rustc_front::visit::{self, Visitor};
53 54

use std::mem::replace;
55 56
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
57 58 59

// Specifies how duplicates should be handled when adding a child item if
// another item exists with the same name in some namespace.
N
Niko Matsakis 已提交
60
#[derive(Copy, Clone, PartialEq)]
61
enum DuplicateCheckingMode {
62
    ForbidDuplicateTypes,
63 64
    ForbidDuplicateValues,
    ForbidDuplicateTypesAndValues,
C
corentih 已提交
65
    OverwriteDuplicates,
66 67
}

C
corentih 已提交
68 69
struct GraphBuilder<'a, 'b: 'a, 'tcx: 'b> {
    resolver: &'a mut Resolver<'b, 'tcx>,
70 71
}

72 73 74
impl<'a, 'b:'a, 'tcx:'b> Deref for GraphBuilder<'a, 'b, 'tcx> {
    type Target = Resolver<'b, 'tcx>;

75 76 77 78 79
    fn deref(&self) -> &Resolver<'b, 'tcx> {
        &*self.resolver
    }
}

80
impl<'a, 'b:'a, 'tcx:'b> DerefMut for GraphBuilder<'a, 'b, 'tcx> {
81 82 83 84 85 86 87
    fn deref_mut(&mut self) -> &mut Resolver<'b, 'tcx> {
        &mut *self.resolver
    }
}

impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
    /// Constructs the reduced graph for the entire crate.
88
    fn build_reduced_graph(self, krate: &hir::Crate) {
89
        let mut visitor = BuildReducedGraphVisitor {
90
            parent: self.graph_root.clone(),
91 92 93 94 95
            builder: self,
        };
        visit::walk_crate(&mut visitor, krate);
    }

96 97 98
    /// Adds a new child item to the module definition of the parent node,
    /// or if there is already a child, does duplicate checking on the child.
    /// Returns the child's corresponding name bindings.
99 100
    fn add_child(&self,
                 name: Name,
101
                 parent: &Rc<Module>,
102 103 104
                 duplicate_checking_mode: DuplicateCheckingMode,
                 // For printing errors
                 sp: Span)
105
                 -> NameBindings {
C
corentih 已提交
106
        self.check_for_conflicts_between_external_crates_and_items(&**parent, name, sp);
107 108

        // Add or reuse the child.
J
Jonathan S 已提交
109
        let child = parent.children.borrow().get(&name).cloned();
110 111
        match child {
            None => {
112
                let child = NameBindings::new();
J
Jonathan S 已提交
113
                parent.children.borrow_mut().insert(name, child.clone());
114 115 116 117 118 119
                child
            }
            Some(child) => {
                // Enforce the duplicate checking mode:
                //
                // * If we're requesting duplicate type checking, check that
120
                //   the name isn't defined in the type namespace.
121 122
                //
                // * If we're requesting duplicate value checking, check that
123
                //   the name isn't defined in the value namespace.
124
                //
125 126
                // * If we're requesting duplicate type and value checking,
                //   check that the name isn't defined in either namespace.
127 128 129 130 131
                //
                // * If no duplicate checking was requested at all, do
                //   nothing.

                let ns = match duplicate_checking_mode {
132 133 134 135 136
                    ForbidDuplicateTypes if child.type_ns.defined() => TypeNS,
                    ForbidDuplicateValues if child.value_ns.defined() => ValueNS,
                    ForbidDuplicateTypesAndValues if child.type_ns.defined() => TypeNS,
                    ForbidDuplicateTypesAndValues if child.value_ns.defined() => ValueNS,
                    _ => return child,
137
                };
138 139 140 141 142 143 144 145

                // Record an error here by looking up the namespace that had the duplicate
                let ns_str = match ns { TypeNS => "type or module", ValueNS => "value" };
                resolve_error(self, sp, ResolutionError::DuplicateDefinition(ns_str, name));

                if let Some(sp) = child[ns].span() {
                    let note = format!("first definition of {} `{}` here", ns_str, name);
                    self.session.span_note(sp, &note);
146 147 148 149 150 151 152 153
                }
                child
            }
        }
    }

    fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
        // Check each statement.
154
        for statement in &block.stmts {
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
            match statement.node {
                StmtDecl(ref declaration, _) => {
                    match declaration.node {
                        DeclItem(_) => {
                            return true;
                        }
                        _ => {
                            // Keep searching.
                        }
                    }
                }
                _ => {
                    // Keep searching.
                }
            }
        }

172
        // If we found no items, we don't need to create
173 174 175 176 177
        // an anonymous module.

        return false;
    }

178
    fn get_parent_link(&mut self, parent: &Rc<Module>, name: Name) -> ParentLink {
179
        ModuleParentLink(Rc::downgrade(parent), name)
180 181 182
    }

    /// Constructs the reduced graph for one item.
183
    fn build_reduced_graph_for_item(&mut self, item: &Item, parent: &Rc<Module>) -> Rc<Module> {
V
Vadim Petrochenkov 已提交
184
        let name = item.name;
185
        let sp = item.span;
186
        let is_public = item.vis == hir::Public;
T
Fallout  
Tamir Duberstein 已提交
187 188 189 190 191
        let modifiers = if is_public {
            DefModifiers::PUBLIC
        } else {
            DefModifiers::empty()
        } | DefModifiers::IMPORTABLE;
192 193

        match item.node {
194 195 196 197 198 199 200
            ItemUse(ref view_path) => {
                // Extract and intern the module part of the path. For
                // globs and lists, the path is found directly in the AST;
                // for simple paths we have to munge the path a little.
                let module_path = match view_path.node {
                    ViewPathSimple(_, ref full_path) => {
                        full_path.segments
C
corentih 已提交
201 202 203 204 205 206
                                 .split_last()
                                 .unwrap()
                                 .1
                                 .iter()
                                 .map(|seg| seg.identifier.name)
                                 .collect()
207 208 209 210 211
                    }

                    ViewPathGlob(ref module_ident_path) |
                    ViewPathList(ref module_ident_path, _) => {
                        module_ident_path.segments
C
corentih 已提交
212 213 214
                                         .iter()
                                         .map(|seg| seg.identifier.name)
                                         .collect()
215 216 217 218 219
                    }
                };

                // Build up the import directives.
                let shadowable = item.attrs.iter().any(|attr| {
220
                    attr.name() == special_idents::prelude_import.name.as_str()
221 222 223 224 225 226 227 228 229
                });
                let shadowable = if shadowable {
                    Shadowable::Always
                } else {
                    Shadowable::Never
                };

                match view_path.node {
                    ViewPathSimple(binding, ref full_path) => {
C
corentih 已提交
230
                        let source_name = full_path.segments.last().unwrap().identifier.name;
231
                        if source_name.as_str() == "mod" || source_name.as_str() == "self" {
232
                            resolve_error(self,
233 234
                                          view_path.span,
                                          ResolutionError::SelfImportsOnlyAllowedWithin);
235 236
                        }

237
                        let subclass = SingleImport(binding, source_name);
238 239 240 241 242 243 244 245 246 247
                        self.build_import_directive(&**parent,
                                                    module_path,
                                                    subclass,
                                                    view_path.span,
                                                    item.id,
                                                    is_public,
                                                    shadowable);
                    }
                    ViewPathList(_, ref source_items) => {
                        // Make sure there's at most one `mod` import in the list.
C
corentih 已提交
248 249 250 251 252 253 254 255
                        let mod_spans = source_items.iter()
                                                    .filter_map(|item| {
                                                        match item.node {
                                                            PathListMod { .. } => Some(item.span),
                                                            _ => None,
                                                        }
                                                    })
                                                    .collect::<Vec<Span>>();
256
                        if mod_spans.len() > 1 {
C
corentih 已提交
257 258 259
                            resolve_error(self,
                                          mod_spans[0],
                                          ResolutionError::SelfImportCanOnlyAppearOnceInTheList);
260
                            for other_span in mod_spans.iter().skip(1) {
C
corentih 已提交
261 262
                                self.session
                                    .span_note(*other_span, "another `self` import appears here");
263 264 265
                            }
                        }

266
                        for source_item in source_items {
267 268
                            let (module_path, name, rename) = match source_item.node {
                                PathListIdent { name, rename, .. } =>
269
                                    (module_path.clone(), name, rename.unwrap_or(name)),
270
                                PathListMod { rename, .. } => {
271 272 273
                                    let name = match module_path.last() {
                                        Some(name) => *name,
                                        None => {
274
                                            resolve_error(
275 276
                                                self,
                                                source_item.span,
277
                                                ResolutionError::
278
                                                SelfImportOnlyInImportListWithNonEmptyPrefix
279
                                            );
280 281 282
                                            continue;
                                        }
                                    };
S
Simonas Kazlauskas 已提交
283
                                    let module_path = module_path.split_last().unwrap().1;
284
                                    let rename = rename.unwrap_or(name);
285
                                    (module_path.to_vec(), name, rename)
286 287
                                }
                            };
C
corentih 已提交
288 289 290 291 292 293 294
                            self.build_import_directive(&**parent,
                                                        module_path,
                                                        SingleImport(rename, name),
                                                        source_item.span,
                                                        source_item.node.id(),
                                                        is_public,
                                                        shadowable);
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
                        }
                    }
                    ViewPathGlob(_) => {
                        self.build_import_directive(&**parent,
                                                    module_path,
                                                    GlobImport,
                                                    view_path.span,
                                                    item.id,
                                                    is_public,
                                                    shadowable);
                    }
                }
                parent.clone()
            }

            ItemExternCrate(_) => {
C
corentih 已提交
311 312
                // n.b. we don't need to look at the path option here, because cstore already
                // did
N
Nick Cameron 已提交
313
                if let Some(crate_id) = self.session.cstore.find_extern_mod_stmt_cnum(item.id) {
C
corentih 已提交
314 315 316 317
                    let def_id = DefId {
                        krate: crate_id,
                        index: CRATE_DEF_INDEX,
                    };
318
                    self.external_exports.insert(def_id);
319
                    let parent_link = ModuleParentLink(Rc::downgrade(parent), name);
320 321 322
                    let def = DefMod(def_id);
                    let external_module = Module::new(parent_link, Some(def), false, true);

323
                    debug!("(build reduced graph for item) found extern `{}`",
C
corentih 已提交
324
                           module_to_string(&*external_module));
325
                    self.check_for_conflicts_between_external_crates(&**parent, name, sp);
C
corentih 已提交
326 327
                    parent.external_module_children
                          .borrow_mut()
328 329 330 331 332 333
                          .insert(name, external_module.clone());
                    self.build_reduced_graph_for_external_crate(&external_module);
                }
                parent.clone()
            }

334
            ItemMod(..) => {
335
                let name_bindings = self.add_child(name, parent, ForbidDuplicateTypes, sp);
336 337

                let parent_link = self.get_parent_link(parent, name);
338
                let def = DefMod(self.ast_map.local_def_id(item.id));
339 340 341
                let module = Module::new(parent_link, Some(def), false, is_public);
                name_bindings.define_module(module.clone(), sp);
                module
342 343
            }

344
            ItemForeignMod(..) => parent.clone(),
345 346 347

            // These items live in the value namespace.
            ItemStatic(_, m, _) => {
348
                let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, sp);
349
                let mutbl = m == hir::MutMutable;
350

351 352 353
                name_bindings.define_value(DefStatic(self.ast_map.local_def_id(item.id), mutbl),
                                           sp,
                                           modifiers);
354
                parent.clone()
355 356
            }
            ItemConst(_, _) => {
357
                self.add_child(name, parent, ForbidDuplicateValues, sp)
358
                    .define_value(DefConst(self.ast_map.local_def_id(item.id)), sp, modifiers);
359
                parent.clone()
360
            }
361
            ItemFn(_, _, _, _, _, _) => {
362
                let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, sp);
363

364
                let def = DefFn(self.ast_map.local_def_id(item.id), false);
365
                name_bindings.define_value(def, sp, modifiers);
366
                parent.clone()
367 368 369 370
            }

            // These items live in the type namespace.
            ItemTy(..) => {
C
corentih 已提交
371 372
                let name_bindings = self.add_child(name,
                                                   parent,
373
                                                   ForbidDuplicateTypes,
C
corentih 已提交
374
                                                   sp);
375

376
                let parent_link = self.get_parent_link(parent, name);
377
                let def = DefTy(self.ast_map.local_def_id(item.id), false);
378 379
                let module = Module::new(parent_link, Some(def), false, is_public);
                name_bindings.define_module(module, sp);
380
                parent.clone()
381 382 383
            }

            ItemEnum(ref enum_definition, _) => {
C
corentih 已提交
384 385
                let name_bindings = self.add_child(name,
                                                   parent,
386
                                                   ForbidDuplicateTypes,
C
corentih 已提交
387
                                                   sp);
388

389
                let parent_link = self.get_parent_link(parent, name);
390
                let def = DefTy(self.ast_map.local_def_id(item.id), true);
391 392
                let module = Module::new(parent_link, Some(def), false, is_public);
                name_bindings.define_module(module.clone(), sp);
393

394
                for variant in &(*enum_definition).variants {
395
                    let item_def_id = self.ast_map.local_def_id(item.id);
C
corentih 已提交
396
                    self.build_reduced_graph_for_variant(&**variant, item_def_id, &module);
397
                }
398
                parent.clone()
399 400 401 402 403
            }

            // These items live in both the type and value namespaces.
            ItemStruct(ref struct_def, _) => {
                // Adding to both Type and Value namespaces or just Type?
404
                let (forbid, ctor_id) = if struct_def.is_struct() {
405
                    (ForbidDuplicateTypes, None)
406
                } else {
407
                    (ForbidDuplicateTypesAndValues, Some(struct_def.id()))
408 409
                };

410
                let name_bindings = self.add_child(name, parent, forbid, sp);
411 412

                // Define a name in the type namespace.
413 414 415
                name_bindings.define_type(DefTy(self.ast_map.local_def_id(item.id), false),
                                          sp,
                                          modifiers);
416 417 418

                // If this is a newtype or unit-like struct, define a name
                // in the value namespace as well
419
                if let Some(cid) = ctor_id {
420 421 422
                    name_bindings.define_value(DefStruct(self.ast_map.local_def_id(cid)),
                                               sp,
                                               modifiers);
423 424 425
                }

                // Record the def ID and fields of this struct.
C
corentih 已提交
426 427 428 429 430 431 432 433 434
                let named_fields = struct_def.fields()
                                             .iter()
                                             .filter_map(|f| {
                                                 match f.node.kind {
                                                     NamedField(name, _) => Some(name),
                                                     UnnamedField(_) => None,
                                                 }
                                             })
                                             .collect();
435 436
                let item_def_id = self.ast_map.local_def_id(item.id);
                self.structs.insert(item_def_id, named_fields);
437

438
                parent.clone()
439 440
            }

F
Flavio Percoco 已提交
441
            ItemDefaultImpl(_, _) |
E
Eduard Burtescu 已提交
442
            ItemImpl(..) => parent.clone(),
443 444

            ItemTrait(_, _, _, ref items) => {
C
corentih 已提交
445 446
                let name_bindings = self.add_child(name,
                                                   parent,
447
                                                   ForbidDuplicateTypes,
C
corentih 已提交
448
                                                   sp);
449

450 451
                let def_id = self.ast_map.local_def_id(item.id);

452
                // Add all the items within to a new module.
453
                let parent_link = self.get_parent_link(parent, name);
454
                let def = DefTrait(def_id);
455 456
                let module_parent = Module::new(parent_link, Some(def), false, is_public);
                name_bindings.define_module(module_parent.clone(), sp);
457 458

                // Add the names of all the items to the trait info.
459
                for trait_item in items {
V
Vadim Petrochenkov 已提交
460
                    let name_bindings = self.add_child(trait_item.name,
C
corentih 已提交
461 462 463
                                                       &module_parent,
                                                       ForbidDuplicateTypesAndValues,
                                                       trait_item.span);
464 465

                    match trait_item.node {
466
                        hir::ConstTraitItem(..) => {
467
                            let def = DefAssociatedConst(self.ast_map.local_def_id(trait_item.id));
T
Fallout  
Tamir Duberstein 已提交
468 469
                            // NB: not DefModifiers::IMPORTABLE
                            name_bindings.define_value(def, trait_item.span, DefModifiers::PUBLIC);
470
                        }
471
                        hir::MethodTraitItem(..) => {
472
                            let def = DefMethod(self.ast_map.local_def_id(trait_item.id));
T
Fallout  
Tamir Duberstein 已提交
473 474
                            // NB: not DefModifiers::IMPORTABLE
                            name_bindings.define_value(def, trait_item.span, DefModifiers::PUBLIC);
475
                        }
476
                        hir::TypeTraitItem(..) => {
477 478
                            let def = DefAssociatedTy(self.ast_map.local_def_id(item.id),
                                                      self.ast_map.local_def_id(trait_item.id));
T
Fallout  
Tamir Duberstein 已提交
479 480
                            // NB: not DefModifiers::IMPORTABLE
                            name_bindings.define_type(def, trait_item.span, DefModifiers::PUBLIC);
481
                        }
482
                    }
483

484 485
                    let trait_item_def_id = self.ast_map.local_def_id(trait_item.id);
                    self.trait_item_map.insert((trait_item.name, def_id), trait_item_def_id);
486 487
                }

488
                parent.clone()
489 490 491 492 493 494 495 496 497
            }
        }
    }

    // Constructs the reduced graph for one variant. Variants exist in the
    // type and value namespaces.
    fn build_reduced_graph_for_variant(&mut self,
                                       variant: &Variant,
                                       item_id: DefId,
498
                                       parent: &Rc<Module>) {
499
        let name = variant.node.name;
500 501
        let is_exported = if variant.node.data.is_struct() {
            // Not adding fields for variants as they are not accessed with a self receiver
502
            let variant_def_id = self.ast_map.local_def_id(variant.node.data.id());
503 504 505 506
            self.structs.insert(variant_def_id, Vec::new());
            true
        } else {
            false
507 508
        };

C
corentih 已提交
509
        let child = self.add_child(name, parent, ForbidDuplicateTypesAndValues, variant.span);
510 511 512
        // variants are always treated as importable to allow them to be glob
        // used
        child.define_value(DefVariant(item_id,
513 514
                                      self.ast_map.local_def_id(variant.node.data.id()),
                                      is_exported),
C
corentih 已提交
515 516
                           variant.span,
                           DefModifiers::PUBLIC | DefModifiers::IMPORTABLE);
517
        child.define_type(DefVariant(item_id,
518 519
                                     self.ast_map.local_def_id(variant.node.data.id()),
                                     is_exported),
C
corentih 已提交
520 521
                          variant.span,
                          DefModifiers::PUBLIC | DefModifiers::IMPORTABLE);
522 523 524
    }

    /// Constructs the reduced graph for one foreign item.
525 526 527
    fn build_reduced_graph_for_foreign_item(&mut self,
                                            foreign_item: &ForeignItem,
                                            parent: &Rc<Module>) {
V
Vadim Petrochenkov 已提交
528
        let name = foreign_item.name;
529
        let is_public = foreign_item.vis == hir::Public;
T
Fallout  
Tamir Duberstein 已提交
530 531 532 533 534
        let modifiers = if is_public {
            DefModifiers::PUBLIC
        } else {
            DefModifiers::empty()
        } | DefModifiers::IMPORTABLE;
C
corentih 已提交
535
        let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, foreign_item.span);
536

537 538
        let def = match foreign_item.node {
            ForeignItemFn(..) => {
539
                DefFn(self.ast_map.local_def_id(foreign_item.id), false)
540 541
            }
            ForeignItemStatic(_, m) => {
542
                DefStatic(self.ast_map.local_def_id(foreign_item.id), m)
543
            }
544 545
        };
        name_bindings.define_value(def, foreign_item.span, modifiers);
546 547
    }

548
    fn build_reduced_graph_for_block(&mut self, block: &Block, parent: &Rc<Module>) -> Rc<Module> {
549 550 551
        if self.block_needs_anonymous_module(block) {
            let block_id = block.id;

C
corentih 已提交
552 553
            debug!("(building reduced graph for block) creating a new anonymous module for block \
                    {}",
554 555
                   block_id);

556 557
            let parent_link = BlockParentLink(Rc::downgrade(parent), block_id);
            let new_module = Module::new(parent_link, None, false, false);
J
Jonathan S 已提交
558 559
            parent.anonymous_children.borrow_mut().insert(block_id, new_module.clone());
            new_module
560
        } else {
561
            parent.clone()
562 563 564 565 566 567 568 569 570
        }
    }

    fn handle_external_def(&mut self,
                           def: Def,
                           vis: Visibility,
                           child_name_bindings: &NameBindings,
                           final_ident: &str,
                           name: Name,
571
                           new_parent: &Rc<Module>) {
C
corentih 已提交
572 573 574
        debug!("(building reduced graph for external crate) building external def {}, priv {:?}",
               final_ident,
               vis);
575
        let is_public = vis == hir::Public;
T
Fallout  
Tamir Duberstein 已提交
576 577 578 579 580
        let modifiers = if is_public {
            DefModifiers::PUBLIC
        } else {
            DefModifiers::empty()
        } | DefModifiers::IMPORTABLE;
C
corentih 已提交
581
        let is_exported = is_public &&
582
                          match new_parent.def_id() {
J
Jonathan S 已提交
583
            None => true,
C
corentih 已提交
584
            Some(did) => self.external_exports.contains(&did),
585 586 587 588 589 590
        };
        if is_exported {
            self.external_exports.insert(def.def_id());
        }

        match def {
591 592 593 594
            DefMod(_) |
            DefForeignMod(_) |
            DefStruct(_) |
            DefTy(..) => {
595 596
                if let Some(module_def) = child_name_bindings.type_ns.module() {
                    debug!("(building reduced graph for external crate) already created module");
597
                    module_def.def.set(Some(def));
598 599 600 601 602
                } else {
                    debug!("(building reduced graph for external crate) building module {} {}",
                           final_ident,
                           is_public);
                    let parent_link = self.get_parent_link(new_parent, name);
603 604
                    let module = Module::new(parent_link, Some(def), true, is_public);
                    child_name_bindings.define_module(module, DUMMY_SP);
C
corentih 已提交
605
                }
606
            }
C
corentih 已提交
607
            _ => {}
608 609 610
        }

        match def {
C
corentih 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
            DefMod(_) | DefForeignMod(_) => {}
            DefVariant(_, variant_id, is_struct) => {
                debug!("(building reduced graph for external crate) building variant {}",
                       final_ident);
                // variants are always treated as importable to allow them to be
                // glob used
                let modifiers = DefModifiers::PUBLIC | DefModifiers::IMPORTABLE;
                if is_struct {
                    child_name_bindings.define_type(def, DUMMY_SP, modifiers);
                    // Not adding fields for variants as they are not accessed with a self receiver
                    self.structs.insert(variant_id, Vec::new());
                } else {
                    child_name_bindings.define_value(def, DUMMY_SP, modifiers);
                }
            }
            DefFn(ctor_id, true) => {
                child_name_bindings.define_value(
628 629 630
                csearch::get_tuple_struct_definition_if_ctor(&self.session.cstore, ctor_id)
                    .map_or(def, |_| DefStruct(ctor_id)), DUMMY_SP, modifiers);
            }
C
corentih 已提交
631 632 633 634 635 636 637 638 639
            DefFn(..) |
            DefStatic(..) |
            DefConst(..) |
            DefAssociatedConst(..) |
            DefMethod(..) => {
                debug!("(building reduced graph for external crate) building value (fn/static) {}",
                       final_ident);
                // impl methods have already been defined with the correct importability
                // modifier
640
                let mut modifiers = match *child_name_bindings.value_ns.borrow() {
C
corentih 已提交
641 642 643 644
                    Some(ref def) => (modifiers & !DefModifiers::IMPORTABLE) |
                                     (def.modifiers & DefModifiers::IMPORTABLE),
                    None => modifiers,
                };
645
                if !new_parent.is_normal() {
C
corentih 已提交
646 647
                    modifiers = modifiers & !DefModifiers::IMPORTABLE;
                }
648 649
                child_name_bindings.define_value(def, DUMMY_SP, modifiers);
            }
C
corentih 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
            DefTrait(def_id) => {
                debug!("(building reduced graph for external crate) building type {}",
                       final_ident);

                // If this is a trait, add all the trait item names to the trait
                // info.

                let trait_item_def_ids = csearch::get_trait_item_def_ids(&self.session.cstore,
                                                                         def_id);
                for trait_item_def in &trait_item_def_ids {
                    let trait_item_name = csearch::get_trait_name(&self.session.cstore,
                                                                  trait_item_def.def_id());

                    debug!("(building reduced graph for external crate) ... adding trait item \
                            '{}'",
                           trait_item_name);

                    self.trait_item_map.insert((trait_item_name, def_id), trait_item_def.def_id());

                    if is_exported {
                        self.external_exports.insert(trait_item_def.def_id());
                    }
                }

                // Define a module if necessary.
                let parent_link = self.get_parent_link(new_parent, name);
676 677
                let module = Module::new(parent_link, Some(def), true, is_public);
                child_name_bindings.define_module(module, DUMMY_SP);
C
corentih 已提交
678 679 680 681
            }
            DefTy(..) | DefAssociatedTy(..) => {
                debug!("(building reduced graph for external crate) building type {}",
                       final_ident);
682

683 684
                let modifiers = match new_parent.is_normal() {
                    true => modifiers,
C
corentih 已提交
685 686 687
                    _ => modifiers & !DefModifiers::IMPORTABLE,
                };

688 689 690 691 692
                if let DefTy(..) = def {
                    child_name_bindings.type_ns.set_modifiers(modifiers);
                } else {
                    child_name_bindings.define_type(def, DUMMY_SP, modifiers);
                }
C
corentih 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
            }
            DefStruct(def_id) => {
                debug!("(building reduced graph for external crate) building type and value for \
                        {}",
                       final_ident);
                let fields = csearch::get_struct_field_names(&self.session.cstore, def_id);

                if fields.is_empty() {
                    child_name_bindings.define_value(def, DUMMY_SP, modifiers);
                }

                // Record the def ID and fields of this struct.
                self.structs.insert(def_id, fields);
            }
            DefLocal(..) |
            DefPrimTy(..) |
            DefTyParam(..) |
            DefUse(..) |
            DefUpvar(..) |
            DefLabel(..) |
            DefSelfTy(..) => {
                panic!("didn't expect `{:?}`", def);
            }
716 717 718 719 720
        }
    }

    /// Builds the reduced graph for a single item in an external crate.
    fn build_reduced_graph_for_external_crate_def(&mut self,
721
                                                  root: &Rc<Module>,
722 723
                                                  def_like: DefLike,
                                                  name: Name,
724
                                                  def_visibility: Visibility) {
725 726 727 728 729 730 731 732 733 734 735 736 737
        match def_like {
            DlDef(def) => {
                // Add the new child item, if necessary.
                match def {
                    DefForeignMod(def_id) => {
                        // Foreign modules have no names. Recur and populate
                        // eagerly.
                        csearch::each_child_of_item(&self.session.cstore,
                                                    def_id,
                                                    |def_like,
                                                     child_name,
                                                     vis| {
                            self.build_reduced_graph_for_external_crate_def(
738
                                root,
739 740 741 742 743 744
                                def_like,
                                child_name,
                                vis)
                        });
                    }
                    _ => {
C
corentih 已提交
745 746 747 748
                        let child_name_bindings = self.add_child(name,
                                                                 root,
                                                                 OverwriteDuplicates,
                                                                 DUMMY_SP);
749 750

                        self.handle_external_def(def,
751
                                                 def_visibility,
752
                                                 &child_name_bindings,
753
                                                 &name.as_str(),
754
                                                 name,
J
Jonathan S 已提交
755
                                                 root);
756 757 758
                    }
                }
            }
759
            DlImpl(_) => {
C
corentih 已提交
760
                debug!("(building reduced graph for external crate) ignoring impl");
761 762
            }
            DlField => {
C
corentih 已提交
763
                debug!("(building reduced graph for external crate) ignoring field");
764 765 766 767 768
            }
        }
    }

    /// Builds the reduced graph rooted at the given external module.
769
    fn populate_external_module(&mut self, module: &Rc<Module>) {
770
        debug!("(populating external module) attempting to populate {}",
771
               module_to_string(&**module));
772

773
        let def_id = match module.def_id() {
774 775
            None => {
                debug!("(populating external module) ... no def ID!");
C
corentih 已提交
776
                return;
777 778 779 780 781 782 783
            }
            Some(def_id) => def_id,
        };

        csearch::each_child_of_item(&self.session.cstore,
                                    def_id,
                                    |def_like, child_name, visibility| {
C
corentih 已提交
784 785 786 787 788 789 790
                                        debug!("(populating external module) ... found ident: {}",
                                               child_name);
                                        self.build_reduced_graph_for_external_crate_def(module,
                                                                                        def_like,
                                                                                        child_name,
                                                                                        visibility)
                                    });
791 792 793 794 795 796 797
        module.populated.set(true)
    }

    /// Ensures that the reduced graph rooted at the given external module
    /// is built, building it if it is not.
    fn populate_module_if_necessary(&mut self, module: &Rc<Module>) {
        if !module.populated.get() {
798
            self.populate_external_module(module)
799 800 801 802 803 804
        }
        assert!(module.populated.get())
    }

    /// Builds the reduced graph rooted at the 'use' directive for an external
    /// crate.
805
    fn build_reduced_graph_for_external_crate(&mut self, root: &Rc<Module>) {
806
        csearch::each_top_level_item_of_crate(&self.session.cstore,
807
                                              root.def_id()
808 809 810
                                                  .unwrap()
                                                  .krate,
                                              |def_like, name, visibility| {
811
            self.build_reduced_graph_for_external_crate_def(root, def_like, name, visibility)
812 813 814 815 816 817 818 819 820 821 822 823
        });
    }

    /// Creates and adds an import directive to the given module.
    fn build_import_directive(&mut self,
                              module_: &Module,
                              module_path: Vec<Name>,
                              subclass: ImportDirectiveSubclass,
                              span: Span,
                              id: NodeId,
                              is_public: bool,
                              shadowable: Shadowable) {
C
corentih 已提交
824 825 826
        module_.imports
               .borrow_mut()
               .push(ImportDirective::new(module_path, subclass, span, id, is_public, shadowable));
827
        self.unresolved_imports += 1;
828 829

        if is_public {
V
Victor Berger 已提交
830
            module_.inc_pub_count();
831 832
        }

833 834 835 836 837
        // Bump the reference count on the name. Or, if this is a glob, set
        // the appropriate flag.

        match subclass {
            SingleImport(target, _) => {
N
Nick Cameron 已提交
838
                debug!("(building import directive) building import directive: {}::{}",
839
                       names_to_string(&module_.imports.borrow().last().unwrap().module_path),
840
                       target);
841

N
Nick Cameron 已提交
842
                let mut import_resolutions = module_.import_resolutions.borrow_mut();
843 844
                match import_resolutions.get_mut(&target) {
                    Some(resolution) => {
N
Nick Cameron 已提交
845
                        debug!("(building import directive) bumping reference");
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
                        resolution.outstanding_references += 1;

                        // the source of this name is different now
                        resolution.type_id = id;
                        resolution.value_id = id;
                        resolution.is_public = is_public;
                        return;
                    }
                    None => {}
                }
                debug!("(building import directive) creating new");
                let mut resolution = ImportResolution::new(id, is_public);
                resolution.outstanding_references = 1;
                import_resolutions.insert(target, resolution);
            }
            GlobImport => {
                // Set the glob flag. This tells us that we don't know the
                // module's exports ahead of time.

V
Victor Berger 已提交
865
                module_.inc_glob_count();
866
                if is_public {
V
Victor Berger 已提交
867
                    module_.inc_pub_glob_count();
868
                }
869 870 871 872 873
            }
        }
    }
}

C
corentih 已提交
874
struct BuildReducedGraphVisitor<'a, 'b: 'a, 'tcx: 'b> {
875
    builder: GraphBuilder<'a, 'b, 'tcx>,
C
corentih 已提交
876
    parent: Rc<Module>,
877 878 879 880
}

impl<'a, 'b, 'v, 'tcx> Visitor<'v> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
    fn visit_item(&mut self, item: &Item) {
881
        let p = self.builder.build_reduced_graph_for_item(item, &self.parent);
882 883 884 885 886 887
        let old_parent = replace(&mut self.parent, p);
        visit::walk_item(self, item);
        self.parent = old_parent;
    }

    fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
888
        self.builder.build_reduced_graph_for_foreign_item(foreign_item, &self.parent);
889 890 891
    }

    fn visit_block(&mut self, block: &Block) {
892
        let np = self.builder.build_reduced_graph_for_block(block, &self.parent);
893 894 895 896 897 898
        let old_parent = replace(&mut self.parent, np);
        visit::walk_block(self, block);
        self.parent = old_parent;
    }
}

899
pub fn build_reduced_graph(resolver: &mut Resolver, krate: &hir::Crate) {
C
corentih 已提交
900
    GraphBuilder { resolver: resolver }.build_reduced_graph(krate);
901 902 903
}

pub fn populate_module_if_necessary(resolver: &mut Resolver, module: &Rc<Module>) {
C
corentih 已提交
904
    GraphBuilder { resolver: resolver }.populate_module_if_necessary(module);
905
}