1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(non_camel_case_types)]
13 //! Validates all used crates and extern libraries and loads their metadata
15 use common::rustc_version;
16 use cstore::{self, CStore, CrateSource, MetadataBlob};
18 use loader::{self, CratePaths};
20 use rustc::back::svh::Svh;
21 use rustc::dep_graph::DepNode;
22 use rustc::session::{config, Session};
23 use rustc::session::search_paths::PathKind;
24 use rustc::middle::cstore::{CrateStore, validate_crate_name};
25 use rustc::util::nodemap::FnvHashMap;
26 use rustc::front::map as hir_map;
28 use std::cell::{RefCell, Cell};
29 use std::path::PathBuf;
35 use syntax::codemap::{self, Span, mk_sp, Pos};
38 use syntax::attr::AttrMetaMethods;
39 use syntax::errors::FatalError;
40 use syntax::parse::token::InternedString;
41 use syntax::util::small_vector::SmallVector;
42 use rustc_front::intravisit::Visitor;
46 pub struct LocalCrateReader<'a, 'b:'a> {
49 creader: CrateReader<'a>,
50 ast_map: &'a hir_map::Map<'b>,
53 pub struct CrateReader<'a> {
56 next_crate_num: ast::CrateNum,
57 foreign_item_map: FnvHashMap<String, Vec<ast::NodeId>>,
58 local_crate_name: String,
61 impl<'a, 'b, 'hir> Visitor<'hir> for LocalCrateReader<'a, 'b> {
62 fn visit_item(&mut self, a: &'hir hir::Item) {
67 fn dump_crates(cstore: &CStore) {
68 info!("resolved crates:");
69 cstore.iter_crate_data_origins(|_, data, opt_source| {
70 info!(" name: {}", data.name());
71 info!(" cnum: {}", data.cnum);
72 info!(" hash: {}", data.hash());
73 info!(" reqd: {}", data.explicitly_linked.get());
75 let CrateSource { dylib, rlib, cnum: _ } = cs;
76 dylib.map(|dl| info!(" dylib: {}", dl.0.display()));
77 rlib.map(|rl| info!(" rlib: {}", rl.0.display()));
82 fn should_link(i: &ast::Item) -> bool {
83 !attr::contains_name(&i.attrs, "no_link")
86 fn should_link_hir(i: &hir::Item) -> bool {
87 !attr::contains_name(&i.attrs, "no_link")
97 fn register_native_lib(sess: &Session,
101 kind: cstore::NativeLibraryKind) {
105 span_err!(sess, span, E0454,
106 "#[link(name = \"\")] given with empty name");
109 sess.err("empty library name given via `-l`");
114 let is_osx = sess.target.target.options.is_like_osx;
115 if kind == cstore::NativeFramework && !is_osx {
116 let msg = "native frameworks are only available on OSX targets";
119 span_err!(sess, span, E0455,
122 None => sess.err(msg),
125 cstore.add_used_library(name, kind);
128 // Extra info about a crate loaded for plugins or exported macros.
129 struct ExtensionCrate {
131 dylib: Option<PathBuf>,
136 Registered(Rc<cstore::crate_metadata>),
141 pub fn as_slice<'a>(&'a self) -> &'a [u8] {
143 PMDSource::Registered(ref cmd) => cmd.data(),
144 PMDSource::Owned(ref mdb) => mdb.as_slice(),
149 impl<'a> CrateReader<'a> {
150 pub fn new(sess: &'a Session,
152 local_crate_name: &str) -> CrateReader<'a> {
156 next_crate_num: cstore.next_crate_num(),
157 foreign_item_map: FnvHashMap(),
158 local_crate_name: local_crate_name.to_owned(),
162 fn extract_crate_info(&self, i: &ast::Item) -> Option<CrateInfo> {
164 ast::ItemKind::ExternCrate(ref path_opt) => {
165 debug!("resolving extern crate stmt. ident: {} path_opt: {:?}",
167 let name = match *path_opt {
169 validate_crate_name(Some(self.sess), &name.as_str(),
173 None => i.ident.to_string(),
176 ident: i.ident.to_string(),
179 should_link: should_link(i),
186 // Dup of the above, but for the hir
187 fn extract_crate_info_hir(&self, i: &hir::Item) -> Option<CrateInfo> {
189 hir::ItemExternCrate(ref path_opt) => {
190 debug!("resolving extern crate stmt. ident: {} path_opt: {:?}",
192 let name = match *path_opt {
194 validate_crate_name(Some(self.sess), &name.as_str(),
198 None => i.name.to_string(),
201 ident: i.name.to_string(),
204 should_link: should_link_hir(i),
211 fn existing_match(&self, name: &str, hash: Option<&Svh>, kind: PathKind)
212 -> Option<ast::CrateNum> {
214 self.cstore.iter_crate_data(|cnum, data| {
215 if data.name != name { return }
218 Some(hash) if *hash == data.hash() => { ret = Some(cnum); return }
223 // When the hash is None we're dealing with a top-level dependency
224 // in which case we may have a specification on the command line for
225 // this library. Even though an upstream library may have loaded
226 // something of the same name, we have to make sure it was loaded
227 // from the exact same location as well.
229 // We're also sure to compare *paths*, not actual byte slices. The
230 // `source` stores paths which are normalized which may be different
231 // from the strings on the command line.
232 let source = self.cstore.used_crate_source(cnum);
233 if let Some(locs) = self.sess.opts.externs.get(name) {
234 let found = locs.iter().any(|l| {
235 let l = fs::canonicalize(l).ok();
236 source.dylib.as_ref().map(|p| &p.0) == l.as_ref() ||
237 source.rlib.as_ref().map(|p| &p.0) == l.as_ref()
245 // Alright, so we've gotten this far which means that `data` has the
246 // right name, we don't have a hash, and we don't have a --extern
247 // pointing for ourselves. We're still not quite yet done because we
248 // have to make sure that this crate was found in the crate lookup
249 // path (this is a top-level dependency) as we don't want to
250 // implicitly load anything inside the dependency lookup path.
251 let prev_kind = source.dylib.as_ref().or(source.rlib.as_ref())
253 if ret.is_none() && (prev_kind == kind || prev_kind == PathKind::All) {
260 fn verify_rustc_version(&self,
263 metadata: &MetadataBlob) {
264 let crate_rustc_version = decoder::crate_rustc_version(metadata.as_slice());
265 if crate_rustc_version != Some(rustc_version()) {
266 let mut err = struct_span_fatal!(self.sess, span, E0514,
267 "the crate `{}` has been compiled with {}, which is \
268 incompatible with this version of rustc",
271 .as_ref().map(|s| &**s)
272 .unwrap_or("an old version of rustc"));
273 err.fileline_help(span, "consider removing the compiled binaries and recompiling \
274 with your current version of rustc");
279 fn verify_no_symbol_conflicts(&self,
282 metadata: &MetadataBlob) {
283 let disambiguator = decoder::get_crate_disambiguator(metadata.as_slice());
285 // Check for (potential) conflicts with the local crate
286 if self.local_crate_name == crate_name &&
287 &self.sess.crate_disambiguator.borrow()[..] == disambiguator {
288 span_fatal!(self.sess, span, E0519,
289 "the current crate is indistinguishable from one of its \
290 dependencies: it has the same crate-name `{}` and was \
291 compiled with the same `-C metadata` arguments. This \
292 will result in symbol conflicts between the two.",
296 let svh = decoder::get_crate_hash(metadata.as_slice());
297 // Check for conflicts with any crate loaded so far
298 self.cstore.iter_crate_data(|_, other| {
299 if other.name() == crate_name && // same crate-name
300 other.disambiguator() == disambiguator && // same crate-disambiguator
301 other.hash() != svh { // but different SVH
302 span_fatal!(self.sess, span, E0520,
303 "found two different crates with name `{}` that are \
304 not distinguished by differing `-C metadata`. This \
305 will result in symbol conflicts between the two.",
311 fn register_crate(&mut self,
312 root: &Option<CratePaths>,
316 lib: loader::Library,
317 explicitly_linked: bool)
318 -> (ast::CrateNum, Rc<cstore::crate_metadata>,
319 cstore::CrateSource) {
320 self.verify_rustc_version(name, span, &lib.metadata);
321 self.verify_no_symbol_conflicts(name, span, &lib.metadata);
323 // Claim this crate number and cache it
324 let cnum = self.next_crate_num;
325 self.next_crate_num += 1;
327 // Stash paths for top-most crate locally if necessary.
328 let crate_paths = if root.is_none() {
330 ident: ident.to_string(),
331 dylib: lib.dylib.clone().map(|p| p.0),
332 rlib: lib.rlib.clone().map(|p| p.0),
337 // Maintain a reference to the top most crate.
338 let root = if root.is_some() { root } else { &crate_paths };
340 let loader::Library { dylib, rlib, metadata } = lib;
342 let cnum_map = self.resolve_crate_deps(root, metadata.as_slice(), span);
343 let staged_api = self.is_staged_api(metadata.as_slice());
345 let cmeta = Rc::new(cstore::crate_metadata {
346 name: name.to_string(),
347 local_path: RefCell::new(SmallVector::zero()),
348 local_def_path: RefCell::new(vec![]),
349 index: decoder::load_index(metadata.as_slice()),
350 xref_index: decoder::load_xrefs(metadata.as_slice()),
352 cnum_map: RefCell::new(cnum_map),
354 codemap_import_info: RefCell::new(vec![]),
356 staged_api: staged_api,
357 explicitly_linked: Cell::new(explicitly_linked),
360 let source = cstore::CrateSource {
366 self.cstore.set_crate_data(cnum, cmeta.clone());
367 self.cstore.add_used_crate_source(source.clone());
368 (cnum, cmeta, source)
371 fn is_staged_api(&self, data: &[u8]) -> bool {
372 let attrs = decoder::get_crate_attributes(data);
374 if attr.name() == "stable" || attr.name() == "unstable" {
381 fn resolve_crate(&mut self,
382 root: &Option<CratePaths>,
388 explicitly_linked: bool)
389 -> (ast::CrateNum, Rc<cstore::crate_metadata>,
390 cstore::CrateSource) {
392 Previous(ast::CrateNum),
393 Loaded(loader::Library),
395 let result = match self.existing_match(name, hash, kind) {
396 Some(cnum) => LookupResult::Previous(cnum),
398 let mut load_ctxt = loader::Context {
403 hash: hash.map(|a| &*a),
404 filesearch: self.sess.target_filesearch(kind),
405 target: &self.sess.target.target,
406 triple: &self.sess.opts.target_triple,
408 rejected_via_hash: vec!(),
409 rejected_via_triple: vec!(),
410 rejected_via_kind: vec!(),
411 should_match_name: true,
413 let library = load_ctxt.load_library_crate();
415 // In the case that we're loading a crate, but not matching
416 // against a hash, we could load a crate which has the same hash
417 // as an already loaded crate. If this is the case prevent
418 // duplicates by just using the first crate.
419 let meta_hash = decoder::get_crate_hash(library.metadata
421 let mut result = LookupResult::Loaded(library);
422 self.cstore.iter_crate_data(|cnum, data| {
423 if data.name() == name && meta_hash == data.hash() {
424 assert!(hash.is_none());
425 result = LookupResult::Previous(cnum);
433 LookupResult::Previous(cnum) => {
434 let data = self.cstore.get_crate_data(cnum);
435 if explicitly_linked && !data.explicitly_linked.get() {
436 data.explicitly_linked.set(explicitly_linked);
438 (cnum, data, self.cstore.used_crate_source(cnum))
440 LookupResult::Loaded(library) => {
441 self.register_crate(root, ident, name, span, library,
447 // Go through the crate metadata and load any crates that it references
448 fn resolve_crate_deps(&mut self,
449 root: &Option<CratePaths>,
450 cdata: &[u8], span : Span)
451 -> cstore::cnum_map {
452 debug!("resolving deps of external crate");
453 // The map from crate numbers in the crate we're resolving to local crate
455 decoder::get_crate_deps(cdata).iter().map(|dep| {
456 debug!("resolving dep crate {} hash: `{}`", dep.name, dep.hash);
457 let (local_cnum, _, _) = self.resolve_crate(root,
462 PathKind::Dependency,
463 dep.explicitly_linked);
464 (dep.cnum, local_cnum)
468 fn read_extension_crate(&mut self, span: Span, info: &CrateInfo) -> ExtensionCrate {
469 let target_triple = &self.sess.opts.target_triple[..];
470 let is_cross = target_triple != config::host_triple();
471 let mut should_link = info.should_link && !is_cross;
472 let mut target_only = false;
473 let ident = info.ident.clone();
474 let name = info.name.clone();
475 let mut load_ctxt = loader::Context {
479 crate_name: &name[..],
481 filesearch: self.sess.host_filesearch(PathKind::Crate),
482 target: &self.sess.host,
483 triple: config::host_triple(),
485 rejected_via_hash: vec!(),
486 rejected_via_triple: vec!(),
487 rejected_via_kind: vec!(),
488 should_match_name: true,
490 let library = match load_ctxt.maybe_load_library_crate() {
492 None if is_cross => {
493 // Try loading from target crates. This will abort later if we
494 // try to load a plugin registrar function,
496 should_link = info.should_link;
498 load_ctxt.target = &self.sess.target.target;
499 load_ctxt.triple = target_triple;
500 load_ctxt.filesearch = self.sess.target_filesearch(PathKind::Crate);
501 load_ctxt.load_library_crate()
503 None => { load_ctxt.report_load_errs(); unreachable!() },
506 let dylib = library.dylib.clone();
507 let register = should_link && self.existing_match(&info.name,
509 PathKind::Crate).is_none();
510 let metadata = if register {
511 // Register crate now to avoid double-reading metadata
512 let (_, cmd, _) = self.register_crate(&None, &info.ident,
513 &info.name, span, library,
515 PMDSource::Registered(cmd)
517 // Not registering the crate; just hold on to the metadata
518 PMDSource::Owned(library.metadata)
523 dylib: dylib.map(|p| p.0),
524 target_only: target_only,
528 /// Read exported macros.
529 pub fn read_exported_macros(&mut self, item: &ast::Item) -> Vec<ast::MacroDef> {
530 let ci = self.extract_crate_info(item).unwrap();
531 let ekrate = self.read_extension_crate(item.span, &ci);
533 let source_name = format!("<{} macros>", item.ident);
534 let mut macros = vec![];
535 decoder::each_exported_macro(ekrate.metadata.as_slice(),
537 |name, attrs, span, body| {
538 // NB: Don't use parse::parse_tts_from_source_str because it parses with
540 let mut p = parse::new_parser_from_source_str(&self.sess.parse_sess,
541 self.sess.opts.cfg.clone(),
545 let body = match p.parse_all_token_trees() {
552 let local_span = mk_sp(lo, p.last_span.hi);
554 // Mark the attrs as used
556 attr::mark_used(attr);
559 macros.push(ast::MacroDef {
560 ident: ast::Ident::with_empty_ctxt(name),
562 id: ast::DUMMY_NODE_ID,
564 imported_from: Some(item.ident),
565 // overridden in plugin/load.rs
568 allow_internal_unstable: false,
572 self.sess.imported_macro_spans.borrow_mut()
573 .insert(local_span, (name.as_str().to_string(), span));
580 /// Look for a plugin registrar. Returns library path and symbol name.
581 pub fn find_plugin_registrar(&mut self, span: Span, name: &str)
582 -> Option<(PathBuf, String)> {
583 let ekrate = self.read_extension_crate(span, &CrateInfo {
584 name: name.to_string(),
585 ident: name.to_string(),
586 id: ast::DUMMY_NODE_ID,
590 if ekrate.target_only {
591 // Need to abort before syntax expansion.
592 let message = format!("plugin `{}` is not available for triple `{}` \
595 config::host_triple(),
596 self.sess.opts.target_triple);
597 span_fatal!(self.sess, span, E0456, "{}", &message[..]);
601 decoder::get_plugin_registrar_fn(ekrate.metadata.as_slice())
602 .map(|id| decoder::get_symbol_from_buf(ekrate.metadata.as_slice(), id));
604 match (ekrate.dylib.as_ref(), registrar) {
605 (Some(dylib), Some(reg)) => Some((dylib.to_path_buf(), reg)),
607 span_err!(self.sess, span, E0457,
608 "plugin `{}` only found in rlib format, but must be available \
611 // No need to abort because the loading code will just ignore this
619 fn register_statically_included_foreign_items(&mut self) {
620 let libs = self.cstore.get_used_libraries();
621 for (lib, list) in self.foreign_item_map.iter() {
622 let is_static = libs.borrow().iter().any(|&(ref name, kind)| {
623 lib == name && kind == cstore::NativeStatic
627 self.cstore.add_statically_included_foreign_item(*id);
633 fn inject_allocator_crate(&mut self) {
634 // Make sure that we actually need an allocator, if none of our
635 // dependencies need one then we definitely don't!
637 // Also, if one of our dependencies has an explicit allocator, then we
638 // also bail out as we don't need to implicitly inject one.
639 let mut needs_allocator = false;
640 let mut found_required_allocator = false;
641 self.cstore.iter_crate_data(|cnum, data| {
642 needs_allocator = needs_allocator || data.needs_allocator();
643 if data.is_allocator() {
644 debug!("{} required by rlib and is an allocator", data.name());
645 self.inject_allocator_dependency(cnum);
646 found_required_allocator = found_required_allocator ||
647 data.explicitly_linked.get();
650 if !needs_allocator || found_required_allocator { return }
652 // At this point we've determined that we need an allocator and no
653 // previous allocator has been activated. We look through our outputs of
654 // crate types to see what kind of allocator types we may need.
656 // The main special output type here is that rlibs do **not** need an
657 // allocator linked in (they're just object files), only final products
658 // (exes, dylibs, staticlibs) need allocators.
659 let mut need_lib_alloc = false;
660 let mut need_exe_alloc = false;
661 for ct in self.sess.crate_types.borrow().iter() {
663 config::CrateTypeExecutable => need_exe_alloc = true,
664 config::CrateTypeDylib |
665 config::CrateTypeStaticlib => need_lib_alloc = true,
666 config::CrateTypeRlib => {}
669 if !need_lib_alloc && !need_exe_alloc { return }
671 // The default allocator crate comes from the custom target spec, and we
672 // choose between the standard library allocator or exe allocator. This
673 // distinction exists because the default allocator for binaries (where
674 // the world is Rust) is different than library (where the world is
675 // likely *not* Rust).
677 // If a library is being produced, but we're also flagged with `-C
678 // prefer-dynamic`, then we interpret this as a *Rust* dynamic library
679 // is being produced so we use the exe allocator instead.
681 // What this boils down to is:
683 // * Binaries use jemalloc
684 // * Staticlibs and Rust dylibs use system malloc
685 // * Rust dylibs used as dependencies to rust use jemalloc
686 let name = if need_lib_alloc && !self.sess.opts.cg.prefer_dynamic {
687 &self.sess.target.target.options.lib_allocation_crate
689 &self.sess.target.target.options.exe_allocation_crate
691 let (cnum, data, _) = self.resolve_crate(&None, name, name, None,
693 PathKind::Crate, false);
695 // To ensure that the `-Z allocation-crate=foo` option isn't abused, and
696 // to ensure that the allocator is indeed an allocator, we verify that
697 // the crate loaded here is indeed tagged #![allocator].
698 if !data.is_allocator() {
699 self.sess.err(&format!("the allocator crate `{}` is not tagged \
700 with #![allocator]", data.name()));
703 self.sess.injected_allocator.set(Some(cnum));
704 self.inject_allocator_dependency(cnum);
707 fn inject_allocator_dependency(&self, allocator: ast::CrateNum) {
708 // Before we inject any dependencies, make sure we don't inject a
709 // circular dependency by validating that this allocator crate doesn't
710 // transitively depend on any `#![needs_allocator]` crates.
711 validate(self, allocator, allocator);
713 // All crates tagged with `needs_allocator` do not explicitly depend on
714 // the allocator selected for this compile, but in order for this
715 // compilation to be successfully linked we need to inject a dependency
716 // (to order the crates on the command line correctly).
718 // Here we inject a dependency from all crates with #![needs_allocator]
719 // to the crate tagged with #![allocator] for this compilation unit.
720 self.cstore.iter_crate_data(|cnum, data| {
721 if !data.needs_allocator() {
725 info!("injecting a dep from {} to {}", cnum, allocator);
726 let mut cnum_map = data.cnum_map.borrow_mut();
727 let remote_cnum = cnum_map.len() + 1;
728 let prev = cnum_map.insert(remote_cnum as ast::CrateNum, allocator);
729 assert!(prev.is_none());
732 fn validate(me: &CrateReader, krate: ast::CrateNum,
733 allocator: ast::CrateNum) {
734 let data = me.cstore.get_crate_data(krate);
735 if data.needs_allocator() {
736 let krate_name = data.name();
737 let data = me.cstore.get_crate_data(allocator);
738 let alloc_name = data.name();
739 me.sess.err(&format!("the allocator crate `{}` cannot depend \
740 on a crate that needs an allocator, but \
741 it depends on `{}`", alloc_name,
745 for (_, &dep) in data.cnum_map.borrow().iter() {
746 validate(me, dep, allocator);
752 impl<'a, 'b> LocalCrateReader<'a, 'b> {
753 pub fn new(sess: &'a Session,
755 map: &'a hir_map::Map<'b>,
756 local_crate_name: &str)
757 -> LocalCrateReader<'a, 'b> {
761 creader: CrateReader::new(sess, cstore, local_crate_name),
766 // Traverses an AST, reading all the information about use'd crates and
767 // extern libraries necessary for later resolving, typechecking, linking,
769 pub fn read_crates(&mut self) {
770 let _task = self.ast_map.dep_graph.in_task(DepNode::CrateReader);
771 let krate = self.ast_map.krate();
773 self.process_crate(krate);
774 krate.visit_all_items(self);
775 self.creader.inject_allocator_crate();
777 if log_enabled!(log::INFO) {
778 dump_crates(&self.cstore);
781 for &(ref name, kind) in &self.sess.opts.libs {
782 register_native_lib(self.sess, self.cstore, None, name.clone(), kind);
784 self.creader.register_statically_included_foreign_items();
787 fn process_crate(&self, c: &hir::Crate) {
788 for a in c.attrs.iter().filter(|m| m.name() == "link_args") {
789 match a.value_str() {
790 Some(ref linkarg) => self.cstore.add_used_link_args(&linkarg),
791 None => { /* fallthrough */ }
796 fn process_item(&mut self, i: &hir::Item) {
798 hir::ItemExternCrate(_) => {
799 if !should_link_hir(i) {
803 match self.creader.extract_crate_info_hir(i) {
805 let (cnum, cmeta, _) = self.creader.resolve_crate(&None,
812 let def_id = self.ast_map.local_def_id(i.id);
813 let def_path = self.ast_map.def_path(def_id);
814 cmeta.update_local_def_path(def_path);
815 self.ast_map.with_path(i.id, |path| {
816 cmeta.update_local_path(path)
818 self.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
823 hir::ItemForeignMod(ref fm) => self.process_foreign_mod(i, fm),
828 fn process_foreign_mod(&mut self, i: &hir::Item, fm: &hir::ForeignMod) {
829 if fm.abi == Abi::Rust || fm.abi == Abi::RustIntrinsic || fm.abi == Abi::PlatformIntrinsic {
833 // First, add all of the custom #[link_args] attributes
834 for m in i.attrs.iter().filter(|a| a.check_name("link_args")) {
835 if let Some(linkarg) = m.value_str() {
836 self.cstore.add_used_link_args(&linkarg);
840 // Next, process all of the #[link(..)]-style arguments
841 for m in i.attrs.iter().filter(|a| a.check_name("link")) {
842 let items = match m.meta_item_list() {
846 let kind = items.iter().find(|k| {
848 }).and_then(|a| a.value_str());
849 let kind = match kind.as_ref().map(|s| &s[..]) {
850 Some("static") => cstore::NativeStatic,
851 Some("dylib") => cstore::NativeUnknown,
852 Some("framework") => cstore::NativeFramework,
854 span_err!(self.sess, m.span, E0458,
855 "unknown kind: `{}`", k);
856 cstore::NativeUnknown
858 None => cstore::NativeUnknown
860 let n = items.iter().find(|n| {
862 }).and_then(|a| a.value_str());
866 span_err!(self.sess, m.span, E0459,
867 "#[link(...)] specified without `name = \"foo\"`");
868 InternedString::new("foo")
871 register_native_lib(self.sess, self.cstore, Some(m.span), n.to_string(), kind);
874 // Finally, process the #[linked_from = "..."] attribute
875 for m in i.attrs.iter().filter(|a| a.check_name("linked_from")) {
876 let lib_name = match m.value_str() {
880 let list = self.creader.foreign_item_map.entry(lib_name.to_string())
881 .or_insert(Vec::new());
882 list.extend(fm.items.iter().map(|it| it.id));
887 /// Imports the codemap from an external crate into the codemap of the crate
888 /// currently being compiled (the "local crate").
890 /// The import algorithm works analogous to how AST items are inlined from an
891 /// external crate's metadata:
892 /// For every FileMap in the external codemap an 'inline' copy is created in the
893 /// local codemap. The correspondence relation between external and local
894 /// FileMaps is recorded in the `ImportedFileMap` objects returned from this
895 /// function. When an item from an external crate is later inlined into this
896 /// crate, this correspondence information is used to translate the span
897 /// information of the inlined item so that it refers the correct positions in
898 /// the local codemap (see `astencode::DecodeContext::tr_span()`).
900 /// The import algorithm in the function below will reuse FileMaps already
901 /// existing in the local codemap. For example, even if the FileMap of some
902 /// source file of libstd gets imported many times, there will only ever be
903 /// one FileMap object for the corresponding file in the local codemap.
905 /// Note that imported FileMaps do not actually contain the source code of the
906 /// file they represent, just information about length, line breaks, and
907 /// multibyte characters. This information is enough to generate valid debuginfo
908 /// for items inlined from other crates.
909 pub fn import_codemap(local_codemap: &codemap::CodeMap,
910 metadata: &MetadataBlob)
911 -> Vec<cstore::ImportedFileMap> {
912 let external_codemap = decoder::get_imported_filemaps(metadata.as_slice());
914 let imported_filemaps = external_codemap.into_iter().map(|filemap_to_import| {
915 // Try to find an existing FileMap that can be reused for the filemap to
916 // be imported. A FileMap is reusable if it is exactly the same, just
917 // positioned at a different offset within the codemap.
918 let reusable_filemap = {
922 .find(|fm| are_equal_modulo_startpos(&fm, &filemap_to_import))
923 .map(|rc| rc.clone())
926 match reusable_filemap {
928 cstore::ImportedFileMap {
929 original_start_pos: filemap_to_import.start_pos,
930 original_end_pos: filemap_to_import.end_pos,
931 translated_filemap: fm
935 // We can't reuse an existing FileMap, so allocate a new one
936 // containing the information we need.
937 let codemap::FileMap {
944 } = filemap_to_import;
946 let source_length = (end_pos - start_pos).to_usize();
948 // Translate line-start positions and multibyte character
949 // position into frame of reference local to file.
950 // `CodeMap::new_imported_filemap()` will then translate those
951 // coordinates to their new global frame of reference when the
952 // offset of the FileMap is known.
953 let mut lines = lines.into_inner();
954 for pos in &mut lines {
955 *pos = *pos - start_pos;
957 let mut multibyte_chars = multibyte_chars.into_inner();
958 for mbc in &mut multibyte_chars {
959 mbc.pos = mbc.pos - start_pos;
962 let local_version = local_codemap.new_imported_filemap(name,
966 cstore::ImportedFileMap {
967 original_start_pos: start_pos,
968 original_end_pos: end_pos,
969 translated_filemap: local_version
975 return imported_filemaps;
977 fn are_equal_modulo_startpos(fm1: &codemap::FileMap,
978 fm2: &codemap::FileMap)
980 if fm1.name != fm2.name {
984 let lines1 = fm1.lines.borrow();
985 let lines2 = fm2.lines.borrow();
987 if lines1.len() != lines2.len() {
991 for (&line1, &line2) in lines1.iter().zip(lines2.iter()) {
992 if (line1 - fm1.start_pos) != (line2 - fm2.start_pos) {
997 let multibytes1 = fm1.multibyte_chars.borrow();
998 let multibytes2 = fm2.multibyte_chars.borrow();
1000 if multibytes1.len() != multibytes2.len() {
1004 for (mb1, mb2) in multibytes1.iter().zip(multibytes2.iter()) {
1005 if (mb1.bytes != mb2.bytes) ||
1006 ((mb1.pos - fm1.start_pos) != (mb2.pos - fm2.start_pos)) {