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