]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/creader.rs
11cb2b8a818f4b66eff2cd7b36d11cce116f4e23
[rust.git] / src / librustc_metadata / creader.rs
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.
4 //
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.
10
11 #![allow(non_camel_case_types)]
12
13 //! Validates all used crates and extern libraries and loads their metadata
14
15 use common::rustc_version;
16 use cstore::{self, CStore, CrateSource, MetadataBlob};
17 use decoder;
18 use loader::{self, CratePaths};
19
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;
27
28 use std::cell::{RefCell, Cell};
29 use std::path::PathBuf;
30 use std::rc::Rc;
31 use std::fs;
32
33 use syntax::ast;
34 use syntax::abi::Abi;
35 use syntax::codemap::{self, Span, mk_sp, Pos};
36 use syntax::parse;
37 use syntax::attr;
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;
43 use rustc_front::hir;
44 use log;
45
46 pub struct LocalCrateReader<'a, 'b:'a> {
47     sess: &'a Session,
48     cstore: &'a CStore,
49     creader: CrateReader<'a>,
50     ast_map: &'a hir_map::Map<'b>,
51 }
52
53 pub struct CrateReader<'a> {
54     sess: &'a Session,
55     cstore: &'a CStore,
56     next_crate_num: ast::CrateNum,
57     foreign_item_map: FnvHashMap<String, Vec<ast::NodeId>>,
58     local_crate_name: String,
59 }
60
61 impl<'a, 'b, 'hir> Visitor<'hir> for LocalCrateReader<'a, 'b> {
62     fn visit_item(&mut self, a: &'hir hir::Item) {
63         self.process_item(a);
64     }
65 }
66
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());
74         opt_source.map(|cs| {
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()));
78         });
79     })
80 }
81
82 fn should_link(i: &ast::Item) -> bool {
83     !attr::contains_name(&i.attrs, "no_link")
84 }
85 // Dup for the hir
86 fn should_link_hir(i: &hir::Item) -> bool {
87     !attr::contains_name(&i.attrs, "no_link")
88 }
89
90 struct CrateInfo {
91     ident: String,
92     name: String,
93     id: ast::NodeId,
94     should_link: bool,
95 }
96
97 fn register_native_lib(sess: &Session,
98                        cstore: &CStore,
99                        span: Option<Span>,
100                        name: String,
101                        kind: cstore::NativeLibraryKind) {
102     if name.is_empty() {
103         match span {
104             Some(span) => {
105                 span_err!(sess, span, E0454,
106                           "#[link(name = \"\")] given with empty name");
107             }
108             None => {
109                 sess.err("empty library name given via `-l`");
110             }
111         }
112         return
113     }
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";
117         match span {
118             Some(span) => {
119                 span_err!(sess, span, E0455,
120                           "{}", msg)
121             }
122             None => sess.err(msg),
123         }
124     }
125     cstore.add_used_library(name, kind);
126 }
127
128 // Extra info about a crate loaded for plugins or exported macros.
129 struct ExtensionCrate {
130     metadata: PMDSource,
131     dylib: Option<PathBuf>,
132     target_only: bool,
133 }
134
135 enum PMDSource {
136     Registered(Rc<cstore::crate_metadata>),
137     Owned(MetadataBlob),
138 }
139
140 impl PMDSource {
141     pub fn as_slice<'a>(&'a self) -> &'a [u8] {
142         match *self {
143             PMDSource::Registered(ref cmd) => cmd.data(),
144             PMDSource::Owned(ref mdb) => mdb.as_slice(),
145         }
146     }
147 }
148
149 impl<'a> CrateReader<'a> {
150     pub fn new(sess: &'a Session,
151                cstore: &'a CStore,
152                local_crate_name: &str) -> CrateReader<'a> {
153         CrateReader {
154             sess: sess,
155             cstore: cstore,
156             next_crate_num: cstore.next_crate_num(),
157             foreign_item_map: FnvHashMap(),
158             local_crate_name: local_crate_name.to_owned(),
159         }
160     }
161
162     fn extract_crate_info(&self, i: &ast::Item) -> Option<CrateInfo> {
163         match i.node {
164             ast::ItemKind::ExternCrate(ref path_opt) => {
165                 debug!("resolving extern crate stmt. ident: {} path_opt: {:?}",
166                        i.ident, path_opt);
167                 let name = match *path_opt {
168                     Some(name) => {
169                         validate_crate_name(Some(self.sess), &name.as_str(),
170                                             Some(i.span));
171                         name.to_string()
172                     }
173                     None => i.ident.to_string(),
174                 };
175                 Some(CrateInfo {
176                     ident: i.ident.to_string(),
177                     name: name,
178                     id: i.id,
179                     should_link: should_link(i),
180                 })
181             }
182             _ => None
183         }
184     }
185
186     // Dup of the above, but for the hir
187     fn extract_crate_info_hir(&self, i: &hir::Item) -> Option<CrateInfo> {
188         match i.node {
189             hir::ItemExternCrate(ref path_opt) => {
190                 debug!("resolving extern crate stmt. ident: {} path_opt: {:?}",
191                        i.name, path_opt);
192                 let name = match *path_opt {
193                     Some(name) => {
194                         validate_crate_name(Some(self.sess), &name.as_str(),
195                                             Some(i.span));
196                         name.to_string()
197                     }
198                     None => i.name.to_string(),
199                 };
200                 Some(CrateInfo {
201                     ident: i.name.to_string(),
202                     name: name,
203                     id: i.id,
204                     should_link: should_link_hir(i),
205                 })
206             }
207             _ => None
208         }
209     }
210
211     fn existing_match(&self, name: &str, hash: Option<&Svh>, kind: PathKind)
212                       -> Option<ast::CrateNum> {
213         let mut ret = None;
214         self.cstore.iter_crate_data(|cnum, data| {
215             if data.name != name { return }
216
217             match hash {
218                 Some(hash) if *hash == data.hash() => { ret = Some(cnum); return }
219                 Some(..) => return,
220                 None => {}
221             }
222
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.
228             //
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()
238                 });
239                 if found {
240                     ret = Some(cnum);
241                 }
242                 return
243             }
244
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())
252                                   .unwrap().1;
253             if ret.is_none() && (prev_kind == kind || prev_kind == PathKind::All) {
254                 ret = Some(cnum);
255             }
256         });
257         return ret;
258     }
259
260     fn verify_rustc_version(&self,
261                             name: &str,
262                             span: Span,
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",
269                                               name,
270                                               crate_rustc_version
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");
275             err.emit();
276         }
277     }
278
279     fn verify_no_symbol_conflicts(&self,
280                                   crate_name: &str,
281                                   span: Span,
282                                   metadata: &MetadataBlob) {
283         let disambiguator = decoder::get_crate_disambiguator(metadata.as_slice());
284
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.",
293                         crate_name)
294         }
295
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.",
306                         crate_name)
307             }
308         });
309     }
310
311     fn register_crate(&mut self,
312                       root: &Option<CratePaths>,
313                       ident: &str,
314                       name: &str,
315                       span: Span,
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);
322
323         // Claim this crate number and cache it
324         let cnum = self.next_crate_num;
325         self.next_crate_num += 1;
326
327         // Stash paths for top-most crate locally if necessary.
328         let crate_paths = if root.is_none() {
329             Some(CratePaths {
330                 ident: ident.to_string(),
331                 dylib: lib.dylib.clone().map(|p| p.0),
332                 rlib:  lib.rlib.clone().map(|p| p.0),
333             })
334         } else {
335             None
336         };
337         // Maintain a reference to the top most crate.
338         let root = if root.is_some() { root } else { &crate_paths };
339
340         let loader::Library { dylib, rlib, metadata } = lib;
341
342         let cnum_map = self.resolve_crate_deps(root, metadata.as_slice(), span);
343         let staged_api = self.is_staged_api(metadata.as_slice());
344
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()),
351             data: metadata,
352             cnum_map: RefCell::new(cnum_map),
353             cnum: cnum,
354             codemap_import_info: RefCell::new(vec![]),
355             span: span,
356             staged_api: staged_api,
357             explicitly_linked: Cell::new(explicitly_linked),
358         });
359
360         let source = cstore::CrateSource {
361             dylib: dylib,
362             rlib: rlib,
363             cnum: cnum,
364         };
365
366         self.cstore.set_crate_data(cnum, cmeta.clone());
367         self.cstore.add_used_crate_source(source.clone());
368         (cnum, cmeta, source)
369     }
370
371     fn is_staged_api(&self, data: &[u8]) -> bool {
372         let attrs = decoder::get_crate_attributes(data);
373         for attr in &attrs {
374             if attr.name() == "stable" || attr.name() == "unstable" {
375                 return true
376             }
377         }
378         false
379     }
380
381     fn resolve_crate(&mut self,
382                      root: &Option<CratePaths>,
383                      ident: &str,
384                      name: &str,
385                      hash: Option<&Svh>,
386                      span: Span,
387                      kind: PathKind,
388                      explicitly_linked: bool)
389                          -> (ast::CrateNum, Rc<cstore::crate_metadata>,
390                              cstore::CrateSource) {
391         enum LookupResult {
392             Previous(ast::CrateNum),
393             Loaded(loader::Library),
394         }
395         let result = match self.existing_match(name, hash, kind) {
396             Some(cnum) => LookupResult::Previous(cnum),
397             None => {
398                 let mut load_ctxt = loader::Context {
399                     sess: self.sess,
400                     span: span,
401                     ident: ident,
402                     crate_name: name,
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,
407                     root: root,
408                     rejected_via_hash: vec!(),
409                     rejected_via_triple: vec!(),
410                     rejected_via_kind: vec!(),
411                     should_match_name: true,
412                 };
413                 let library = load_ctxt.load_library_crate();
414
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
420                                                                .as_slice());
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);
426                     }
427                 });
428                 result
429             }
430         };
431
432         match result {
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);
437                 }
438                 (cnum, data, self.cstore.used_crate_source(cnum))
439             }
440             LookupResult::Loaded(library) => {
441                 self.register_crate(root, ident, name, span, library,
442                                     explicitly_linked)
443             }
444         }
445     }
446
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
454         // numbers
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,
458                                                    &dep.name,
459                                                    &dep.name,
460                                                    Some(&dep.hash),
461                                                    span,
462                                                    PathKind::Dependency,
463                                                    dep.explicitly_linked);
464             (dep.cnum, local_cnum)
465         }).collect()
466     }
467
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 {
476             sess: self.sess,
477             span: span,
478             ident: &ident[..],
479             crate_name: &name[..],
480             hash: None,
481             filesearch: self.sess.host_filesearch(PathKind::Crate),
482             target: &self.sess.host,
483             triple: config::host_triple(),
484             root: &None,
485             rejected_via_hash: vec!(),
486             rejected_via_triple: vec!(),
487             rejected_via_kind: vec!(),
488             should_match_name: true,
489         };
490         let library = match load_ctxt.maybe_load_library_crate() {
491             Some(l) => l,
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,
495                 target_only = true;
496                 should_link = info.should_link;
497
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()
502             }
503             None => { load_ctxt.report_load_errs(); unreachable!() },
504         };
505
506         let dylib = library.dylib.clone();
507         let register = should_link && self.existing_match(&info.name,
508                                                           None,
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,
514                                                   true);
515             PMDSource::Registered(cmd)
516         } else {
517             // Not registering the crate; just hold on to the metadata
518             PMDSource::Owned(library.metadata)
519         };
520
521         ExtensionCrate {
522             metadata: metadata,
523             dylib: dylib.map(|p| p.0),
524             target_only: target_only,
525         }
526     }
527
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);
532
533         let source_name = format!("<{} macros>", item.ident);
534         let mut macros = vec![];
535         decoder::each_exported_macro(ekrate.metadata.as_slice(),
536                                      &self.cstore.intr,
537             |name, attrs, span, body| {
538                 // NB: Don't use parse::parse_tts_from_source_str because it parses with
539                 // quote_depth > 0.
540                 let mut p = parse::new_parser_from_source_str(&self.sess.parse_sess,
541                                                               self.sess.opts.cfg.clone(),
542                                                               source_name.clone(),
543                                                               body);
544                 let lo = p.span.lo;
545                 let body = match p.parse_all_token_trees() {
546                     Ok(body) => body,
547                     Err(mut err) => {
548                         err.emit();
549                         panic!(FatalError);
550                     }
551                 };
552                 let local_span = mk_sp(lo, p.last_span.hi);
553
554                 // Mark the attrs as used
555                 for attr in &attrs {
556                     attr::mark_used(attr);
557                 }
558
559                 macros.push(ast::MacroDef {
560                     ident: ast::Ident::with_empty_ctxt(name),
561                     attrs: attrs,
562                     id: ast::DUMMY_NODE_ID,
563                     span: local_span,
564                     imported_from: Some(item.ident),
565                     // overridden in plugin/load.rs
566                     export: false,
567                     use_locally: false,
568                     allow_internal_unstable: false,
569
570                     body: body,
571                 });
572                 self.sess.imported_macro_spans.borrow_mut()
573                     .insert(local_span, (name.as_str().to_string(), span));
574                 true
575             }
576         );
577         macros
578     }
579
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,
587              should_link: false,
588         });
589
590         if ekrate.target_only {
591             // Need to abort before syntax expansion.
592             let message = format!("plugin `{}` is not available for triple `{}` \
593                                    (only found {})",
594                                   name,
595                                   config::host_triple(),
596                                   self.sess.opts.target_triple);
597             span_fatal!(self.sess, span, E0456, "{}", &message[..]);
598         }
599
600         let registrar =
601             decoder::get_plugin_registrar_fn(ekrate.metadata.as_slice())
602             .map(|id| decoder::get_symbol_from_buf(ekrate.metadata.as_slice(), id));
603
604         match (ekrate.dylib.as_ref(), registrar) {
605             (Some(dylib), Some(reg)) => Some((dylib.to_path_buf(), reg)),
606             (None, Some(_)) => {
607                 span_err!(self.sess, span, E0457,
608                           "plugin `{}` only found in rlib format, but must be available \
609                            in dylib format",
610                           name);
611                 // No need to abort because the loading code will just ignore this
612                 // empty dylib.
613                 None
614             }
615             _ => None,
616         }
617     }
618
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
624             });
625             if is_static {
626                 for id in list {
627                     self.cstore.add_statically_included_foreign_item(*id);
628                 }
629             }
630         }
631     }
632
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!
636         //
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();
648             }
649         });
650         if !needs_allocator || found_required_allocator { return }
651
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.
655         //
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() {
662             match *ct {
663                 config::CrateTypeExecutable => need_exe_alloc = true,
664                 config::CrateTypeDylib |
665                 config::CrateTypeStaticlib => need_lib_alloc = true,
666                 config::CrateTypeRlib => {}
667             }
668         }
669         if !need_lib_alloc && !need_exe_alloc { return }
670
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).
676         //
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.
680         //
681         // What this boils down to is:
682         //
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
688         } else {
689             &self.sess.target.target.options.exe_allocation_crate
690         };
691         let (cnum, data, _) = self.resolve_crate(&None, name, name, None,
692                                                  codemap::DUMMY_SP,
693                                                  PathKind::Crate, false);
694
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()));
701         }
702
703         self.sess.injected_allocator.set(Some(cnum));
704         self.inject_allocator_dependency(cnum);
705     }
706
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);
712
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).
717         //
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() {
722                 return
723             }
724
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());
730         });
731
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,
742                                       krate_name));
743             }
744
745             for (_, &dep) in data.cnum_map.borrow().iter() {
746                 validate(me, dep, allocator);
747             }
748         }
749     }
750 }
751
752 impl<'a, 'b> LocalCrateReader<'a, 'b> {
753     pub fn new(sess: &'a Session,
754                cstore: &'a CStore,
755                map: &'a hir_map::Map<'b>,
756                local_crate_name: &str)
757                -> LocalCrateReader<'a, 'b> {
758         LocalCrateReader {
759             sess: sess,
760             cstore: cstore,
761             creader: CrateReader::new(sess, cstore, local_crate_name),
762             ast_map: map,
763         }
764     }
765
766     // Traverses an AST, reading all the information about use'd crates and
767     // extern libraries necessary for later resolving, typechecking, linking,
768     // etc.
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();
772
773         self.process_crate(krate);
774         krate.visit_all_items(self);
775         self.creader.inject_allocator_crate();
776
777         if log_enabled!(log::INFO) {
778             dump_crates(&self.cstore);
779         }
780
781         for &(ref name, kind) in &self.sess.opts.libs {
782             register_native_lib(self.sess, self.cstore, None, name.clone(), kind);
783         }
784         self.creader.register_statically_included_foreign_items();
785     }
786
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 */ }
792             }
793         }
794     }
795
796     fn process_item(&mut self, i: &hir::Item) {
797         match i.node {
798             hir::ItemExternCrate(_) => {
799                 if !should_link_hir(i) {
800                     return;
801                 }
802
803                 match self.creader.extract_crate_info_hir(i) {
804                     Some(info) => {
805                         let (cnum, cmeta, _) = self.creader.resolve_crate(&None,
806                                                               &info.ident,
807                                                               &info.name,
808                                                               None,
809                                                               i.span,
810                                                               PathKind::Crate,
811                                                               true);
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)
817                         });
818                         self.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
819                     }
820                     None => ()
821                 }
822             }
823             hir::ItemForeignMod(ref fm) => self.process_foreign_mod(i, fm),
824             _ => { }
825         }
826     }
827
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 {
830             return;
831         }
832
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);
837             }
838         }
839
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() {
843                 Some(item) => item,
844                 None => continue,
845             };
846             let kind = items.iter().find(|k| {
847                 k.check_name("kind")
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,
853                 Some(k) => {
854                     span_err!(self.sess, m.span, E0458,
855                               "unknown kind: `{}`", k);
856                     cstore::NativeUnknown
857                 }
858                 None => cstore::NativeUnknown
859             };
860             let n = items.iter().find(|n| {
861                 n.check_name("name")
862             }).and_then(|a| a.value_str());
863             let n = match n {
864                 Some(n) => n,
865                 None => {
866                     span_err!(self.sess, m.span, E0459,
867                               "#[link(...)] specified without `name = \"foo\"`");
868                     InternedString::new("foo")
869                 }
870             };
871             register_native_lib(self.sess, self.cstore, Some(m.span), n.to_string(), kind);
872         }
873
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() {
877                 Some(name) => name,
878                 None => continue,
879             };
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));
883         }
884     }
885 }
886
887 /// Imports the codemap from an external crate into the codemap of the crate
888 /// currently being compiled (the "local crate").
889 ///
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()`).
899 ///
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.
904 ///
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());
913
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 = {
919             local_codemap.files
920                          .borrow()
921                          .iter()
922                          .find(|fm| are_equal_modulo_startpos(&fm, &filemap_to_import))
923                          .map(|rc| rc.clone())
924         };
925
926         match reusable_filemap {
927             Some(fm) => {
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
932                 }
933             }
934             None => {
935                 // We can't reuse an existing FileMap, so allocate a new one
936                 // containing the information we need.
937                 let codemap::FileMap {
938                     name,
939                     start_pos,
940                     end_pos,
941                     lines,
942                     multibyte_chars,
943                     ..
944                 } = filemap_to_import;
945
946                 let source_length = (end_pos - start_pos).to_usize();
947
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;
956                 }
957                 let mut multibyte_chars = multibyte_chars.into_inner();
958                 for mbc in &mut multibyte_chars {
959                     mbc.pos = mbc.pos - start_pos;
960                 }
961
962                 let local_version = local_codemap.new_imported_filemap(name,
963                                                                        source_length,
964                                                                        lines,
965                                                                        multibyte_chars);
966                 cstore::ImportedFileMap {
967                     original_start_pos: start_pos,
968                     original_end_pos: end_pos,
969                     translated_filemap: local_version
970                 }
971             }
972         }
973     }).collect();
974
975     return imported_filemaps;
976
977     fn are_equal_modulo_startpos(fm1: &codemap::FileMap,
978                                  fm2: &codemap::FileMap)
979                                  -> bool {
980         if fm1.name != fm2.name {
981             return false;
982         }
983
984         let lines1 = fm1.lines.borrow();
985         let lines2 = fm2.lines.borrow();
986
987         if lines1.len() != lines2.len() {
988             return false;
989         }
990
991         for (&line1, &line2) in lines1.iter().zip(lines2.iter()) {
992             if (line1 - fm1.start_pos) != (line2 - fm2.start_pos) {
993                 return false;
994             }
995         }
996
997         let multibytes1 = fm1.multibyte_chars.borrow();
998         let multibytes2 = fm2.multibyte_chars.borrow();
999
1000         if multibytes1.len() != multibytes2.len() {
1001             return false;
1002         }
1003
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)) {
1007                 return false;
1008             }
1009         }
1010
1011         true
1012     }
1013 }