]> git.lizzy.rs Git - rust.git/blob - src/librustc_driver/driver.rs
5db3ee59cae4f609c097fc54e3ac88be130e58dc
[rust.git] / src / librustc_driver / driver.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 use rustc::front;
12 use rustc::front::map as hir_map;
13 use rustc_mir as mir;
14 use rustc_mir::mir_map::MirMap;
15 use rustc::session::Session;
16 use rustc::session::config::{self, Input, OutputFilenames, OutputType};
17 use rustc::session::search_paths::PathKind;
18 use rustc::lint;
19 use rustc::metadata;
20 use rustc::metadata::creader::LocalCrateReader;
21 use rustc::middle::{stability, ty, reachable};
22 use rustc::middle::dependency_format;
23 use rustc::middle;
24 use rustc::plugin::registry::Registry;
25 use rustc::plugin;
26 use rustc::util::nodemap::NodeMap;
27 use rustc::util::common::time;
28 use rustc_borrowck as borrowck;
29 use rustc_resolve as resolve;
30 use rustc_trans::back::link;
31 use rustc_trans::back::write;
32 use rustc_trans::trans;
33 use rustc_typeck as typeck;
34 use rustc_privacy;
35 use rustc_front::hir;
36 use rustc_front::lowering::{lower_crate, LoweringContext};
37 use super::Compilation;
38
39 use serialize::json;
40
41 use std::collections::HashMap;
42 use std::env;
43 use std::ffi::{OsString, OsStr};
44 use std::fs;
45 use std::io::{self, Write};
46 use std::path::{Path, PathBuf};
47 use syntax::ast::{self, NodeIdAssigner};
48 use syntax::attr;
49 use syntax::attr::AttrMetaMethods;
50 use syntax::diagnostics;
51 use syntax::feature_gate::UnstableFeatures;
52 use syntax::fold::Folder;
53 use syntax::parse;
54 use syntax::parse::token;
55 use syntax::util::node_count::NodeCounter;
56 use syntax::visit;
57 use syntax;
58
59 pub fn compile_input(sess: Session,
60                      cfg: ast::CrateConfig,
61                      input: &Input,
62                      outdir: &Option<PathBuf>,
63                      output: &Option<PathBuf>,
64                      addl_plugins: Option<Vec<String>>,
65                      control: CompileController) {
66     macro_rules! controller_entry_point{($point: ident, $tsess: expr, $make_state: expr) => ({
67         let state = $make_state;
68         (control.$point.callback)(state);
69
70         $tsess.abort_if_errors();
71         if control.$point.stop == Compilation::Stop {
72             return;
73         }
74     })}
75
76     // We need nested scopes here, because the intermediate results can keep
77     // large chunks of memory alive and we want to free them as soon as
78     // possible to keep the peak memory usage low
79     let result = {
80         let (outputs, expanded_crate, id) = {
81             let krate = phase_1_parse_input(&sess, cfg, input);
82
83             controller_entry_point!(after_parse,
84                                     sess,
85                                     CompileState::state_after_parse(input, &sess, outdir, &krate));
86
87             let outputs = build_output_filenames(input, outdir, output, &krate.attrs, &sess);
88             let id = link::find_crate_name(Some(&sess), &krate.attrs, input);
89             let expanded_crate = match phase_2_configure_and_expand(&sess,
90                                                                     krate,
91                                                                     &id[..],
92                                                                     addl_plugins) {
93                 None => return,
94                 Some(k) => k,
95             };
96
97             (outputs, expanded_crate, id)
98         };
99
100         controller_entry_point!(after_expand,
101                                 sess,
102                                 CompileState::state_after_expand(input,
103                                                                  &sess,
104                                                                  outdir,
105                                                                  &expanded_crate,
106                                                                  &id[..]));
107
108         let expanded_crate = assign_node_ids(&sess, expanded_crate);
109         // Lower ast -> hir.
110         let lcx = LoweringContext::new(&sess, Some(&expanded_crate));
111         let mut hir_forest = time(sess.time_passes(),
112                                   "lowering ast -> hir",
113                                   || hir_map::Forest::new(lower_crate(&lcx, &expanded_crate)));
114         let arenas = ty::CtxtArenas::new();
115         let ast_map = make_map(&sess, &mut hir_forest);
116
117         write_out_deps(&sess, &outputs, &id);
118
119         controller_entry_point!(after_write_deps,
120                                 sess,
121                                 CompileState::state_after_write_deps(input,
122                                                                      &sess,
123                                                                      outdir,
124                                                                      &ast_map,
125                                                                      &expanded_crate,
126                                                                      &ast_map.krate(),
127                                                                      &id[..],
128                                                                      &lcx));
129
130         time(sess.time_passes(), "attribute checking", || {
131             front::check_attr::check_crate(&sess, &expanded_crate);
132         });
133
134         time(sess.time_passes(),
135              "early lint checks",
136              || lint::check_ast_crate(&sess, &expanded_crate));
137
138         phase_3_run_analysis_passes(&sess,
139                                     ast_map,
140                                     &arenas,
141                                     &id,
142                                     control.make_glob_map,
143                                     |tcx, mir_map, analysis| {
144
145                                         {
146                                             let state =
147                                                 CompileState::state_after_analysis(input,
148                                                                                    &tcx.sess,
149                                                                                    outdir,
150                                                                                    &expanded_crate,
151                                                                                    tcx.map.krate(),
152                                                                                    &analysis,
153                                                                                    &mir_map,
154                                                                                    tcx,
155                                                                                    &lcx,
156                                                                                    &id);
157                                             (control.after_analysis.callback)(state);
158
159                                             tcx.sess.abort_if_errors();
160                                             if control.after_analysis.stop == Compilation::Stop {
161                                                 return Err(());
162                                             }
163                                         }
164
165                                         if log_enabled!(::log::INFO) {
166                                             println!("Pre-trans");
167                                             tcx.print_debug_stats();
168                                         }
169                                         let trans = phase_4_translate_to_llvm(tcx,
170                                                                               mir_map,
171                                                                               analysis);
172
173                                         if log_enabled!(::log::INFO) {
174                                             println!("Post-trans");
175                                             tcx.print_debug_stats();
176                                         }
177
178                                         // Discard interned strings as they are no longer required.
179                                         token::get_ident_interner().clear();
180
181                                         Ok((outputs, trans))
182                                     })
183     };
184
185     let (outputs, trans) = if let Ok(out) = result {
186         out
187     } else {
188         return;
189     };
190
191     phase_5_run_llvm_passes(&sess, &trans, &outputs);
192
193     controller_entry_point!(after_llvm,
194                             sess,
195                             CompileState::state_after_llvm(input, &sess, outdir, &trans));
196
197     phase_6_link_output(&sess, &trans, &outputs);
198 }
199
200 /// The name used for source code that doesn't originate in a file
201 /// (e.g. source from stdin or a string)
202 pub fn anon_src() -> String {
203     "<anon>".to_string()
204 }
205
206 pub fn source_name(input: &Input) -> String {
207     match *input {
208         // FIXME (#9639): This needs to handle non-utf8 paths
209         Input::File(ref ifile) => ifile.to_str().unwrap().to_string(),
210         Input::Str(_) => anon_src(),
211     }
212 }
213
214 /// CompileController is used to customise compilation, it allows compilation to
215 /// be stopped and/or to call arbitrary code at various points in compilation.
216 /// It also allows for various flags to be set to influence what information gets
217 /// collected during compilation.
218 ///
219 /// This is a somewhat higher level controller than a Session - the Session
220 /// controls what happens in each phase, whereas the CompileController controls
221 /// whether a phase is run at all and whether other code (from outside the
222 /// the compiler) is run between phases.
223 ///
224 /// Note that if compilation is set to stop and a callback is provided for a
225 /// given entry point, the callback is called before compilation is stopped.
226 ///
227 /// Expect more entry points to be added in the future.
228 pub struct CompileController<'a> {
229     pub after_parse: PhaseController<'a>,
230     pub after_expand: PhaseController<'a>,
231     pub after_write_deps: PhaseController<'a>,
232     pub after_analysis: PhaseController<'a>,
233     pub after_llvm: PhaseController<'a>,
234
235     pub make_glob_map: resolve::MakeGlobMap,
236 }
237
238 impl<'a> CompileController<'a> {
239     pub fn basic() -> CompileController<'a> {
240         CompileController {
241             after_parse: PhaseController::basic(),
242             after_expand: PhaseController::basic(),
243             after_write_deps: PhaseController::basic(),
244             after_analysis: PhaseController::basic(),
245             after_llvm: PhaseController::basic(),
246             make_glob_map: resolve::MakeGlobMap::No,
247         }
248     }
249 }
250
251 pub struct PhaseController<'a> {
252     pub stop: Compilation,
253     pub callback: Box<Fn(CompileState) -> () + 'a>,
254 }
255
256 impl<'a> PhaseController<'a> {
257     pub fn basic() -> PhaseController<'a> {
258         PhaseController {
259             stop: Compilation::Continue,
260             callback: box |_| {},
261         }
262     }
263 }
264
265 /// State that is passed to a callback. What state is available depends on when
266 /// during compilation the callback is made. See the various constructor methods
267 /// (`state_*`) in the impl to see which data is provided for any given entry point.
268 pub struct CompileState<'a, 'ast: 'a, 'tcx: 'a> {
269     pub input: &'a Input,
270     pub session: &'a Session,
271     pub cfg: Option<&'a ast::CrateConfig>,
272     pub krate: Option<&'a ast::Crate>,
273     pub crate_name: Option<&'a str>,
274     pub output_filenames: Option<&'a OutputFilenames>,
275     pub out_dir: Option<&'a Path>,
276     pub expanded_crate: Option<&'a ast::Crate>,
277     pub hir_crate: Option<&'a hir::Crate>,
278     pub ast_map: Option<&'a hir_map::Map<'ast>>,
279     pub mir_map: Option<&'a MirMap<'tcx>>,
280     pub analysis: Option<&'a ty::CrateAnalysis<'a>>,
281     pub tcx: Option<&'a ty::ctxt<'tcx>>,
282     pub lcx: Option<&'a LoweringContext<'a>>,
283     pub trans: Option<&'a trans::CrateTranslation>,
284 }
285
286 impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
287     fn empty(input: &'a Input,
288              session: &'a Session,
289              out_dir: &'a Option<PathBuf>)
290              -> CompileState<'a, 'ast, 'tcx> {
291         CompileState {
292             input: input,
293             session: session,
294             out_dir: out_dir.as_ref().map(|s| &**s),
295             cfg: None,
296             krate: None,
297             crate_name: None,
298             output_filenames: None,
299             expanded_crate: None,
300             hir_crate: None,
301             ast_map: None,
302             analysis: None,
303             mir_map: None,
304             tcx: None,
305             lcx: None,
306             trans: None,
307         }
308     }
309
310     fn state_after_parse(input: &'a Input,
311                          session: &'a Session,
312                          out_dir: &'a Option<PathBuf>,
313                          krate: &'a ast::Crate)
314                          -> CompileState<'a, 'ast, 'tcx> {
315         CompileState { krate: Some(krate), ..CompileState::empty(input, session, out_dir) }
316     }
317
318     fn state_after_expand(input: &'a Input,
319                           session: &'a Session,
320                           out_dir: &'a Option<PathBuf>,
321                           expanded_crate: &'a ast::Crate,
322                           crate_name: &'a str)
323                           -> CompileState<'a, 'ast, 'tcx> {
324         CompileState {
325             crate_name: Some(crate_name),
326             expanded_crate: Some(expanded_crate),
327             ..CompileState::empty(input, session, out_dir)
328         }
329     }
330
331     fn state_after_write_deps(input: &'a Input,
332                               session: &'a Session,
333                               out_dir: &'a Option<PathBuf>,
334                               ast_map: &'a hir_map::Map<'ast>,
335                               krate: &'a ast::Crate,
336                               hir_crate: &'a hir::Crate,
337                               crate_name: &'a str,
338                               lcx: &'a LoweringContext<'a>)
339                               -> CompileState<'a, 'ast, 'tcx> {
340         CompileState {
341             crate_name: Some(crate_name),
342             ast_map: Some(ast_map),
343             krate: Some(krate),
344             hir_crate: Some(hir_crate),
345             lcx: Some(lcx),
346             ..CompileState::empty(input, session, out_dir)
347         }
348     }
349
350     fn state_after_analysis(input: &'a Input,
351                             session: &'a Session,
352                             out_dir: &'a Option<PathBuf>,
353                             krate: &'a ast::Crate,
354                             hir_crate: &'a hir::Crate,
355                             analysis: &'a ty::CrateAnalysis,
356                             mir_map: &'a MirMap<'tcx>,
357                             tcx: &'a ty::ctxt<'tcx>,
358                             lcx: &'a LoweringContext<'a>,
359                             crate_name: &'a str)
360                             -> CompileState<'a, 'ast, 'tcx> {
361         CompileState {
362             analysis: Some(analysis),
363             mir_map: Some(mir_map),
364             tcx: Some(tcx),
365             krate: Some(krate),
366             hir_crate: Some(hir_crate),
367             lcx: Some(lcx),
368             crate_name: Some(crate_name),
369             ..CompileState::empty(input, session, out_dir)
370         }
371     }
372
373
374     fn state_after_llvm(input: &'a Input,
375                         session: &'a Session,
376                         out_dir: &'a Option<PathBuf>,
377                         trans: &'a trans::CrateTranslation)
378                         -> CompileState<'a, 'ast, 'tcx> {
379         CompileState { trans: Some(trans), ..CompileState::empty(input, session, out_dir) }
380     }
381 }
382
383 pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input) -> ast::Crate {
384     // These may be left in an incoherent state after a previous compile.
385     // `clear_tables` and `get_ident_interner().clear()` can be used to free
386     // memory, but they do not restore the initial state.
387     syntax::ext::mtwt::reset_tables();
388     token::reset_ident_interner();
389
390     let krate = time(sess.time_passes(), "parsing", || {
391         match *input {
392             Input::File(ref file) => {
393                 parse::parse_crate_from_file(&(*file), cfg.clone(), &sess.parse_sess)
394             }
395             Input::Str(ref src) => {
396                 parse::parse_crate_from_source_str(anon_src().to_string(),
397                                                    src.to_string(),
398                                                    cfg.clone(),
399                                                    &sess.parse_sess)
400             }
401         }
402     });
403
404     if sess.opts.debugging_opts.ast_json_noexpand {
405         println!("{}", json::as_json(&krate));
406     }
407
408     if sess.opts.debugging_opts.input_stats {
409         println!("Lines of code:             {}", sess.codemap().count_lines());
410         println!("Pre-expansion node count:  {}", count_nodes(&krate));
411     }
412
413     if let Some(ref s) = sess.opts.show_span {
414         syntax::show_span::run(sess.diagnostic(), s, &krate);
415     }
416
417     krate
418 }
419
420 fn count_nodes(krate: &ast::Crate) -> usize {
421     let mut counter = NodeCounter::new();
422     visit::walk_crate(&mut counter, krate);
423     counter.count
424 }
425
426 // For continuing compilation after a parsed crate has been
427 // modified
428
429 /// Run the "early phases" of the compiler: initial `cfg` processing,
430 /// loading compiler plugins (including those from `addl_plugins`),
431 /// syntax expansion, secondary `cfg` expansion, synthesis of a test
432 /// harness if one is to be provided and injection of a dependency on the
433 /// standard library and prelude.
434 ///
435 /// Returns `None` if we're aborting after handling -W help.
436 pub fn phase_2_configure_and_expand(sess: &Session,
437                                     mut krate: ast::Crate,
438                                     crate_name: &str,
439                                     addl_plugins: Option<Vec<String>>)
440                                     -> Option<ast::Crate> {
441     let time_passes = sess.time_passes();
442
443     // strip before anything else because crate metadata may use #[cfg_attr]
444     // and so macros can depend on configuration variables, such as
445     //
446     //   #[macro_use] #[cfg(foo)]
447     //   mod bar { macro_rules! baz!(() => {{}}) }
448     //
449     // baz! should not use this definition unless foo is enabled.
450
451     let mut feature_gated_cfgs = vec![];
452     krate = time(time_passes, "configuration 1", || {
453         syntax::config::strip_unconfigured_items(sess.diagnostic(), krate, &mut feature_gated_cfgs)
454     });
455
456     *sess.crate_types.borrow_mut() = collect_crate_types(sess, &krate.attrs);
457     *sess.crate_metadata.borrow_mut() = collect_crate_metadata(sess, &krate.attrs);
458
459     time(time_passes, "recursion limit", || {
460         middle::recursion_limit::update_recursion_limit(sess, &krate);
461     });
462
463     time(time_passes, "gated macro checking", || {
464         let features = syntax::feature_gate::check_crate_macros(sess.codemap(),
465                                                                 &sess.parse_sess.span_diagnostic,
466                                                                 &krate);
467
468         // these need to be set "early" so that expansion sees `quote` if enabled.
469         *sess.features.borrow_mut() = features;
470         sess.abort_if_errors();
471     });
472
473
474     krate = time(time_passes, "crate injection", || {
475         syntax::std_inject::maybe_inject_crates_ref(krate, sess.opts.alt_std_name.clone())
476     });
477
478     let macros = time(time_passes,
479                       "macro loading",
480                       || metadata::macro_import::read_macro_defs(sess, &krate));
481
482     let mut addl_plugins = Some(addl_plugins);
483     let registrars = time(time_passes, "plugin loading", || {
484         plugin::load::load_plugins(sess, &krate, addl_plugins.take().unwrap())
485     });
486
487     let mut registry = Registry::new(sess, &krate);
488
489     time(time_passes, "plugin registration", || {
490         if sess.features.borrow().rustc_diagnostic_macros {
491             registry.register_macro("__diagnostic_used",
492                                     diagnostics::plugin::expand_diagnostic_used);
493             registry.register_macro("__register_diagnostic",
494                                     diagnostics::plugin::expand_register_diagnostic);
495             registry.register_macro("__build_diagnostic_array",
496                                     diagnostics::plugin::expand_build_diagnostic_array);
497         }
498
499         for registrar in registrars {
500             registry.args_hidden = Some(registrar.args);
501             (registrar.fun)(&mut registry);
502         }
503     });
504
505     let Registry { syntax_exts, early_lint_passes, late_lint_passes, lint_groups,
506                    llvm_passes, attributes, .. } = registry;
507
508     {
509         let mut ls = sess.lint_store.borrow_mut();
510         for pass in early_lint_passes {
511             ls.register_early_pass(Some(sess), true, pass);
512         }
513         for pass in late_lint_passes {
514             ls.register_late_pass(Some(sess), true, pass);
515         }
516
517         for (name, to) in lint_groups {
518             ls.register_group(Some(sess), true, name, to);
519         }
520
521         *sess.plugin_llvm_passes.borrow_mut() = llvm_passes;
522         *sess.plugin_attributes.borrow_mut() = attributes.clone();
523     }
524
525     // Lint plugins are registered; now we can process command line flags.
526     if sess.opts.describe_lints {
527         super::describe_lints(&*sess.lint_store.borrow(), true);
528         return None;
529     }
530     sess.lint_store.borrow_mut().process_command_line(sess);
531
532     // Abort if there are errors from lint processing or a plugin registrar.
533     sess.abort_if_errors();
534
535     krate = time(time_passes, "expansion", || {
536         // Windows dlls do not have rpaths, so they don't know how to find their
537         // dependencies. It's up to us to tell the system where to find all the
538         // dependent dlls. Note that this uses cfg!(windows) as opposed to
539         // targ_cfg because syntax extensions are always loaded for the host
540         // compiler, not for the target.
541         let mut _old_path = OsString::new();
542         if cfg!(windows) {
543             _old_path = env::var_os("PATH").unwrap_or(_old_path);
544             let mut new_path = sess.host_filesearch(PathKind::All)
545                                    .get_dylib_search_paths();
546             new_path.extend(env::split_paths(&_old_path));
547             env::set_var("PATH", &env::join_paths(new_path).unwrap());
548         }
549         let features = sess.features.borrow();
550         let cfg = syntax::ext::expand::ExpansionConfig {
551             crate_name: crate_name.to_string(),
552             features: Some(&features),
553             recursion_limit: sess.recursion_limit.get(),
554             trace_mac: sess.opts.debugging_opts.trace_macros,
555         };
556         let ret = syntax::ext::expand::expand_crate(&sess.parse_sess,
557                                                     cfg,
558                                                     macros,
559                                                     syntax_exts,
560                                                     &mut feature_gated_cfgs,
561                                                     krate);
562         if cfg!(windows) {
563             env::set_var("PATH", &_old_path);
564         }
565         ret
566     });
567
568     // Needs to go *after* expansion to be able to check the results
569     // of macro expansion.  This runs before #[cfg] to try to catch as
570     // much as possible (e.g. help the programmer avoid platform
571     // specific differences)
572     time(time_passes, "complete gated feature checking 1", || {
573         let features = syntax::feature_gate::check_crate(sess.codemap(),
574                                                          &sess.parse_sess.span_diagnostic,
575                                                          &krate,
576                                                          &attributes,
577                                                          sess.opts.unstable_features);
578         *sess.features.borrow_mut() = features;
579         sess.abort_if_errors();
580     });
581
582     // JBC: make CFG processing part of expansion to avoid this problem:
583
584     // strip again, in case expansion added anything with a #[cfg].
585     krate = time(time_passes, "configuration 2", || {
586         syntax::config::strip_unconfigured_items(sess.diagnostic(), krate, &mut feature_gated_cfgs)
587     });
588
589     time(time_passes, "gated configuration checking", || {
590         let features = sess.features.borrow();
591         feature_gated_cfgs.sort();
592         feature_gated_cfgs.dedup();
593         for cfg in &feature_gated_cfgs {
594             cfg.check_and_emit(sess.diagnostic(), &features);
595         }
596     });
597
598     krate = time(time_passes, "maybe building test harness", || {
599         syntax::test::modify_for_testing(&sess.parse_sess, &sess.opts.cfg, krate, sess.diagnostic())
600     });
601
602     krate = time(time_passes,
603                  "prelude injection",
604                  || syntax::std_inject::maybe_inject_prelude(&sess.parse_sess, krate));
605
606     time(time_passes,
607          "checking that all macro invocations are gone",
608          || syntax::ext::expand::check_for_macros(&sess.parse_sess, &krate));
609
610     time(time_passes,
611          "checking for inline asm in case the target doesn't support it",
612          || middle::check_no_asm::check_crate(sess, &krate));
613
614     // One final feature gating of the true AST that gets compiled
615     // later, to make sure we've got everything (e.g. configuration
616     // can insert new attributes via `cfg_attr`)
617     time(time_passes, "complete gated feature checking 2", || {
618         let features = syntax::feature_gate::check_crate(sess.codemap(),
619                                                          &sess.parse_sess.span_diagnostic,
620                                                          &krate,
621                                                          &attributes,
622                                                          sess.opts.unstable_features);
623         *sess.features.borrow_mut() = features;
624         sess.abort_if_errors();
625     });
626
627     if sess.opts.debugging_opts.input_stats {
628         println!("Post-expansion node count: {}", count_nodes(&krate));
629     }
630
631     Some(krate)
632 }
633
634 pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate {
635     struct NodeIdAssigner<'a> {
636         sess: &'a Session,
637     }
638
639     impl<'a> Folder for NodeIdAssigner<'a> {
640         fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId {
641             assert_eq!(old_id, ast::DUMMY_NODE_ID);
642             self.sess.next_node_id()
643         }
644     }
645
646     let krate = time(sess.time_passes(),
647                      "assigning node ids",
648                      || NodeIdAssigner { sess: sess }.fold_crate(krate));
649
650     if sess.opts.debugging_opts.ast_json {
651         println!("{}", json::as_json(&krate));
652     }
653
654     krate
655 }
656
657 pub fn make_map<'ast>(sess: &Session,
658                       forest: &'ast mut front::map::Forest)
659                       -> front::map::Map<'ast> {
660     // Construct the 'ast'-map
661     let map = time(sess.time_passes(),
662                    "indexing hir",
663                    move || front::map::map_crate(forest));
664
665     map
666 }
667
668 /// Run the resolution, typechecking, region checking and other
669 /// miscellaneous analysis passes on the crate. Return various
670 /// structures carrying the results of the analysis.
671 pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
672                                                ast_map: front::map::Map<'tcx>,
673                                                arenas: &'tcx ty::CtxtArenas<'tcx>,
674                                                name: &str,
675                                                make_glob_map: resolve::MakeGlobMap,
676                                                f: F)
677                                                -> R
678     where F: for<'a> FnOnce(&'a ty::ctxt<'tcx>, MirMap<'tcx>, ty::CrateAnalysis) -> R
679 {
680     let time_passes = sess.time_passes();
681     let krate = ast_map.krate();
682
683     time(time_passes,
684          "external crate/lib resolution",
685          || LocalCrateReader::new(sess, &ast_map).read_crates(krate));
686
687     let lang_items = time(time_passes,
688                           "language item collection",
689                           || middle::lang_items::collect_language_items(&sess, &ast_map));
690
691     let resolve::CrateMap {
692         def_map,
693         freevars,
694         export_map,
695         trait_map,
696         external_exports,
697         glob_map,
698     } = time(time_passes,
699              "resolution",
700              || resolve::resolve_crate(sess, &ast_map, make_glob_map));
701
702     // Discard MTWT tables that aren't required past resolution.
703     // FIXME: get rid of uses of MTWT tables in typeck, mir and trans and clear them
704     if !sess.opts.debugging_opts.keep_mtwt_tables {
705         // syntax::ext::mtwt::clear_tables();
706     }
707
708     let named_region_map = time(time_passes,
709                                 "lifetime resolution",
710                                 || middle::resolve_lifetime::krate(sess, krate, &def_map.borrow()));
711
712     time(time_passes,
713          "looking for entry point",
714          || middle::entry::find_entry_point(sess, &ast_map));
715
716     sess.plugin_registrar_fn.set(time(time_passes, "looking for plugin registrar", || {
717         plugin::build::find_plugin_registrar(sess.diagnostic(), krate)
718     }));
719
720     let region_map = time(time_passes,
721                           "region resolution",
722                           || middle::region::resolve_crate(sess, krate));
723
724     time(time_passes,
725          "loop checking",
726          || middle::check_loop::check_crate(sess, krate));
727
728     time(time_passes,
729          "static item recursion checking",
730          || middle::check_static_recursion::check_crate(sess, krate, &def_map.borrow(), &ast_map));
731
732     ty::ctxt::create_and_enter(sess,
733                                arenas,
734                                def_map,
735                                named_region_map,
736                                ast_map,
737                                freevars,
738                                region_map,
739                                lang_items,
740                                stability::Index::new(krate),
741                                |tcx| {
742                                    // passes are timed inside typeck
743                                    typeck::check_crate(tcx, trait_map);
744
745                                    time(time_passes,
746                                         "const checking",
747                                         || middle::check_const::check_crate(tcx));
748
749                                    let (exported_items, public_items) =
750                                        time(time_passes, "privacy checking", || {
751                                            rustc_privacy::check_crate(tcx,
752                                                                       &export_map,
753                                                                       external_exports)
754                                        });
755
756                                    // Do not move this check past lint
757                                    time(time_passes, "stability index", || {
758                                        tcx.stability.borrow_mut().build(tcx, krate, &exported_items)
759                                    });
760
761                                    time(time_passes,
762                                         "intrinsic checking",
763                                         || middle::intrinsicck::check_crate(tcx));
764
765                                    time(time_passes,
766                                         "effect checking",
767                                         || middle::effect::check_crate(tcx));
768
769                                    time(time_passes,
770                                         "match checking",
771                                         || middle::check_match::check_crate(tcx));
772
773                                    let mir_map = match tcx.sess.opts.unstable_features {
774                                        UnstableFeatures::Disallow => {
775                                            // use this as a shorthand for beta/stable, and skip
776                                            // MIR construction there until known regressions are
777                                            // addressed
778                                            NodeMap()
779                                        }
780                                        UnstableFeatures::Allow | UnstableFeatures::Cheat => {
781                                            time(time_passes,
782                                                 "MIR dump",
783                                                 || mir::mir_map::build_mir_for_crate(tcx))
784                                        }
785                                    };
786
787                                    time(time_passes,
788                                         "liveness checking",
789                                         || middle::liveness::check_crate(tcx));
790
791                                    time(time_passes,
792                                         "borrow checking",
793                                         || borrowck::check_crate(tcx));
794
795                                    time(time_passes,
796                                         "rvalue checking",
797                                         || middle::check_rvalues::check_crate(tcx, krate));
798
799                                    // Avoid overwhelming user with errors if type checking failed.
800                                    // I'm not sure how helpful this is, to be honest, but it avoids
801                                    // a
802                                    // lot of annoying errors in the compile-fail tests (basically,
803                                    // lint warnings and so on -- kindck used to do this abort, but
804                                    // kindck is gone now). -nmatsakis
805                                    tcx.sess.abort_if_errors();
806
807                                    let reachable_map =
808                                        time(time_passes,
809                                             "reachability checking",
810                                             || reachable::find_reachable(tcx, &exported_items));
811
812                                    time(time_passes, "death checking", || {
813                                        middle::dead::check_crate(tcx,
814                                                                  &exported_items,
815                                                                  &reachable_map)
816                                    });
817
818                                    let ref lib_features_used =
819                                        time(time_passes,
820                                             "stability checking",
821                                             || stability::check_unstable_api_usage(tcx));
822
823                                    time(time_passes, "unused lib feature checking", || {
824                                        stability::check_unused_or_stable_features(&tcx.sess,
825                                                                                   lib_features_used)
826                                    });
827
828                                    time(time_passes,
829                                         "lint checking",
830                                         || lint::check_crate(tcx, krate, &exported_items));
831
832                                    // The above three passes generate errors w/o aborting
833                                    tcx.sess.abort_if_errors();
834
835                                    f(tcx,
836                                      mir_map,
837                                      ty::CrateAnalysis {
838                                          export_map: export_map,
839                                          exported_items: exported_items,
840                                          public_items: public_items,
841                                          reachable: reachable_map,
842                                          name: name,
843                                          glob_map: glob_map,
844                                      })
845                                })
846 }
847
848 /// Run the translation phase to LLVM, after which the AST and analysis can
849 /// be discarded.
850 pub fn phase_4_translate_to_llvm<'tcx>(tcx: &ty::ctxt<'tcx>,
851                                        mut mir_map: MirMap<'tcx>,
852                                        analysis: ty::CrateAnalysis)
853                                        -> trans::CrateTranslation {
854     let time_passes = tcx.sess.time_passes();
855
856     time(time_passes,
857          "resolving dependency formats",
858          || dependency_format::calculate(&tcx.sess));
859
860     time(time_passes,
861          "erasing regions from MIR",
862          || mir::transform::erase_regions::erase_regions(tcx, &mut mir_map));
863
864     // Option dance to work around the lack of stack once closures.
865     time(time_passes,
866          "translation",
867          move || trans::trans_crate(tcx, &mir_map, analysis))
868 }
869
870 /// Run LLVM itself, producing a bitcode file, assembly file or object file
871 /// as a side effect.
872 pub fn phase_5_run_llvm_passes(sess: &Session,
873                                trans: &trans::CrateTranslation,
874                                outputs: &OutputFilenames) {
875     if sess.opts.cg.no_integrated_as {
876         let mut map = HashMap::new();
877         map.insert(OutputType::Assembly, None);
878         time(sess.time_passes(),
879              "LLVM passes",
880              || write::run_passes(sess, trans, &map, outputs));
881
882         write::run_assembler(sess, outputs);
883
884         // Remove assembly source, unless --save-temps was specified
885         if !sess.opts.cg.save_temps {
886             fs::remove_file(&outputs.temp_path(OutputType::Assembly)).unwrap();
887         }
888     } else {
889         time(sess.time_passes(),
890              "LLVM passes",
891              || write::run_passes(sess, trans, &sess.opts.output_types, outputs));
892     }
893
894     sess.abort_if_errors();
895 }
896
897 /// Run the linker on any artifacts that resulted from the LLVM run.
898 /// This should produce either a finished executable or library.
899 pub fn phase_6_link_output(sess: &Session,
900                            trans: &trans::CrateTranslation,
901                            outputs: &OutputFilenames) {
902     time(sess.time_passes(),
903          "linking",
904          || link::link_binary(sess, trans, outputs, &trans.link.crate_name));
905 }
906
907 fn escape_dep_filename(filename: &str) -> String {
908     // Apparently clang and gcc *only* escape spaces:
909     // http://llvm.org/klaus/clang/commit/9d50634cfc268ecc9a7250226dd5ca0e945240d4
910     filename.replace(" ", "\\ ")
911 }
912
913 fn write_out_deps(sess: &Session, outputs: &OutputFilenames, id: &str) {
914     let mut out_filenames = Vec::new();
915     for output_type in sess.opts.output_types.keys() {
916         let file = outputs.path(*output_type);
917         match *output_type {
918             OutputType::Exe => {
919                 for output in sess.crate_types.borrow().iter() {
920                     let p = link::filename_for_input(sess, *output, id, outputs);
921                     out_filenames.push(p);
922                 }
923             }
924             _ => {
925                 out_filenames.push(file);
926             }
927         }
928     }
929
930     // Write out dependency rules to the dep-info file if requested
931     if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
932         return;
933     }
934     let deps_filename = outputs.path(OutputType::DepInfo);
935
936     let result =
937         (|| -> io::Result<()> {
938             // Build a list of files used to compile the output and
939             // write Makefile-compatible dependency rules
940             let files: Vec<String> = sess.codemap()
941                                          .files
942                                          .borrow()
943                                          .iter()
944                                          .filter(|fmap| fmap.is_real_file())
945                                          .filter(|fmap| !fmap.is_imported())
946                                          .map(|fmap| escape_dep_filename(&fmap.name))
947                                          .collect();
948             let mut file = try!(fs::File::create(&deps_filename));
949             for path in &out_filenames {
950                 try!(write!(file, "{}: {}\n\n", path.display(), files.join(" ")));
951             }
952
953             // Emit a fake target for each input file to the compilation. This
954             // prevents `make` from spitting out an error if a file is later
955             // deleted. For more info see #28735
956             for path in files {
957                 try!(writeln!(file, "{}:", path));
958             }
959             Ok(())
960         })();
961
962     match result {
963         Ok(()) => {}
964         Err(e) => {
965             sess.fatal(&format!("error writing dependencies to `{}`: {}",
966                                 deps_filename.display(),
967                                 e));
968         }
969     }
970 }
971
972 pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<config::CrateType> {
973     // Unconditionally collect crate types from attributes to make them used
974     let attr_types: Vec<config::CrateType> =
975         attrs.iter()
976              .filter_map(|a| {
977                  if a.check_name("crate_type") {
978                      match a.value_str() {
979                          Some(ref n) if *n == "rlib" => {
980                              Some(config::CrateTypeRlib)
981                          }
982                          Some(ref n) if *n == "dylib" => {
983                              Some(config::CrateTypeDylib)
984                          }
985                          Some(ref n) if *n == "lib" => {
986                              Some(config::default_lib_output())
987                          }
988                          Some(ref n) if *n == "staticlib" => {
989                              Some(config::CrateTypeStaticlib)
990                          }
991                          Some(ref n) if *n == "bin" => Some(config::CrateTypeExecutable),
992                          Some(_) => {
993                              session.add_lint(lint::builtin::UNKNOWN_CRATE_TYPES,
994                                               ast::CRATE_NODE_ID,
995                                               a.span,
996                                               "invalid `crate_type` value".to_string());
997                              None
998                          }
999                          _ => {
1000                              session.span_err(a.span, "`crate_type` requires a value");
1001                              session.note("for example: `#![crate_type=\"lib\"]`");
1002                              None
1003                          }
1004                      }
1005                  } else {
1006                      None
1007                  }
1008              })
1009              .collect();
1010
1011     // If we're generating a test executable, then ignore all other output
1012     // styles at all other locations
1013     if session.opts.test {
1014         return vec![config::CrateTypeExecutable];
1015     }
1016
1017     // Only check command line flags if present. If no types are specified by
1018     // command line, then reuse the empty `base` Vec to hold the types that
1019     // will be found in crate attributes.
1020     let mut base = session.opts.crate_types.clone();
1021     if base.is_empty() {
1022         base.extend(attr_types);
1023         if base.is_empty() {
1024             base.push(link::default_output_for_target(session));
1025         }
1026         base.sort();
1027         base.dedup();
1028     }
1029
1030     base.into_iter()
1031         .filter(|crate_type| {
1032             let res = !link::invalid_output_for_target(session, *crate_type);
1033
1034             if !res {
1035                 session.warn(&format!("dropping unsupported crate type `{}` for target `{}`",
1036                                       *crate_type,
1037                                       session.opts.target_triple));
1038             }
1039
1040             res
1041         })
1042         .collect()
1043 }
1044
1045 pub fn collect_crate_metadata(session: &Session, _attrs: &[ast::Attribute]) -> Vec<String> {
1046     session.opts.cg.metadata.clone()
1047 }
1048
1049 pub fn build_output_filenames(input: &Input,
1050                               odir: &Option<PathBuf>,
1051                               ofile: &Option<PathBuf>,
1052                               attrs: &[ast::Attribute],
1053                               sess: &Session)
1054                               -> OutputFilenames {
1055     match *ofile {
1056         None => {
1057             // "-" as input file will cause the parser to read from stdin so we
1058             // have to make up a name
1059             // We want to toss everything after the final '.'
1060             let dirpath = match *odir {
1061                 Some(ref d) => d.clone(),
1062                 None => PathBuf::new(),
1063             };
1064
1065             // If a crate name is present, we use it as the link name
1066             let stem = sess.opts
1067                            .crate_name
1068                            .clone()
1069                            .or_else(|| attr::find_crate_name(attrs).map(|n| n.to_string()))
1070                            .unwrap_or(input.filestem());
1071
1072             OutputFilenames {
1073                 out_directory: dirpath,
1074                 out_filestem: stem,
1075                 single_output_file: None,
1076                 extra: sess.opts.cg.extra_filename.clone(),
1077                 outputs: sess.opts.output_types.clone(),
1078             }
1079         }
1080
1081         Some(ref out_file) => {
1082             let unnamed_output_types = sess.opts
1083                                            .output_types
1084                                            .values()
1085                                            .filter(|a| a.is_none())
1086                                            .count();
1087             let ofile = if unnamed_output_types > 1 {
1088                 sess.warn("ignoring specified output filename because multiple outputs were \
1089                            requested");
1090                 None
1091             } else {
1092                 Some(out_file.clone())
1093             };
1094             if *odir != None {
1095                 sess.warn("ignoring --out-dir flag due to -o flag.");
1096             }
1097
1098             let cur_dir = Path::new("");
1099
1100             OutputFilenames {
1101                 out_directory: out_file.parent().unwrap_or(cur_dir).to_path_buf(),
1102                 out_filestem: out_file.file_stem()
1103                                       .unwrap_or(OsStr::new(""))
1104                                       .to_str()
1105                                       .unwrap()
1106                                       .to_string(),
1107                 single_output_file: ofile,
1108                 extra: sess.opts.cg.extra_filename.clone(),
1109                 outputs: sess.opts.output_types.clone(),
1110             }
1111         }
1112     }
1113 }