]> git.lizzy.rs Git - rust.git/blob - src/librustc_driver/driver.rs
test fallout
[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         if control.$point.stop == Compilation::Stop {
73             $tsess.abort_if_errors();
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         sess.abort_if_new_errors(|| {
474             syntax::config::strip_unconfigured_items(sess.diagnostic(),
475                                                      krate,
476                                                      &mut feature_gated_cfgs)
477         })
478     });
479
480     *sess.crate_types.borrow_mut() = collect_crate_types(sess, &krate.attrs);
481     *sess.crate_metadata.borrow_mut() = collect_crate_metadata(sess, &krate.attrs);
482
483     time(time_passes, "recursion limit", || {
484         middle::recursion_limit::update_recursion_limit(sess, &krate);
485     });
486
487     time(time_passes, "gated macro checking", || {
488         sess.abort_if_new_errors(|| {
489             let features =
490               syntax::feature_gate::check_crate_macros(sess.codemap(),
491                                                        &sess.parse_sess.span_diagnostic,
492                                                        &krate);
493
494             // these need to be set "early" so that expansion sees `quote` if enabled.
495             *sess.features.borrow_mut() = features;
496         });
497     });
498
499
500     krate = time(time_passes, "crate injection", || {
501         syntax::std_inject::maybe_inject_crates_ref(krate, sess.opts.alt_std_name.clone())
502     });
503
504     let macros = time(time_passes,
505                       "macro loading",
506                       || macro_import::read_macro_defs(sess, &cstore, &krate));
507
508     let mut addl_plugins = Some(addl_plugins);
509     let registrars = time(time_passes, "plugin loading", || {
510         plugin::load::load_plugins(sess, &cstore, &krate, addl_plugins.take().unwrap())
511     });
512
513     let mut registry = Registry::new(sess, &krate);
514
515     time(time_passes, "plugin registration", || {
516         if sess.features.borrow().rustc_diagnostic_macros {
517             registry.register_macro("__diagnostic_used",
518                                     diagnostics::plugin::expand_diagnostic_used);
519             registry.register_macro("__register_diagnostic",
520                                     diagnostics::plugin::expand_register_diagnostic);
521             registry.register_macro("__build_diagnostic_array",
522                                     diagnostics::plugin::expand_build_diagnostic_array);
523         }
524
525         for registrar in registrars {
526             registry.args_hidden = Some(registrar.args);
527             (registrar.fun)(&mut registry);
528         }
529     });
530
531     let Registry { syntax_exts, early_lint_passes, late_lint_passes, lint_groups,
532                    llvm_passes, attributes, .. } = registry;
533
534     sess.abort_if_new_errors(|| {
535         let mut ls = sess.lint_store.borrow_mut();
536         for pass in early_lint_passes {
537             ls.register_early_pass(Some(sess), true, pass);
538         }
539         for pass in late_lint_passes {
540             ls.register_late_pass(Some(sess), true, pass);
541         }
542
543         for (name, to) in lint_groups {
544             ls.register_group(Some(sess), true, name, to);
545         }
546
547         *sess.plugin_llvm_passes.borrow_mut() = llvm_passes;
548         *sess.plugin_attributes.borrow_mut() = attributes.clone();
549     });
550
551     // Lint plugins are registered; now we can process command line flags.
552     if sess.opts.describe_lints {
553         super::describe_lints(&*sess.lint_store.borrow(), true);
554         return None;
555     }
556     sess.abort_if_new_errors(|| sess.lint_store.borrow_mut().process_command_line(sess));
557
558     krate = time(time_passes, "expansion", || {
559         // Windows dlls do not have rpaths, so they don't know how to find their
560         // dependencies. It's up to us to tell the system where to find all the
561         // dependent dlls. Note that this uses cfg!(windows) as opposed to
562         // targ_cfg because syntax extensions are always loaded for the host
563         // compiler, not for the target.
564         let mut _old_path = OsString::new();
565         if cfg!(windows) {
566             _old_path = env::var_os("PATH").unwrap_or(_old_path);
567             let mut new_path = sess.host_filesearch(PathKind::All)
568                                    .get_dylib_search_paths();
569             new_path.extend(env::split_paths(&_old_path));
570             env::set_var("PATH", &env::join_paths(new_path).unwrap());
571         }
572         let features = sess.features.borrow();
573         let cfg = syntax::ext::expand::ExpansionConfig {
574             crate_name: crate_name.to_string(),
575             features: Some(&features),
576             recursion_limit: sess.recursion_limit.get(),
577             trace_mac: sess.opts.debugging_opts.trace_macros,
578         };
579         let mut ecx = syntax::ext::base::ExtCtxt::new(&sess.parse_sess,
580                                                       krate.config.clone(),
581                                                       cfg,
582                                                       &mut feature_gated_cfgs);
583         syntax_ext::register_builtins(&mut ecx.syntax_env);
584         let (ret, macro_names) = syntax::ext::expand::expand_crate(ecx,
585                                                                    macros,
586                                                                    syntax_exts,
587                                                                    krate);
588         if cfg!(windows) {
589             env::set_var("PATH", &_old_path);
590         }
591         *sess.available_macros.borrow_mut() = macro_names;
592         ret
593     });
594
595     // Needs to go *after* expansion to be able to check the results
596     // of macro expansion.  This runs before #[cfg] to try to catch as
597     // much as possible (e.g. help the programmer avoid platform
598     // specific differences)
599     time(time_passes, "complete gated feature checking 1", || {
600         sess.abort_if_new_errors(|| {
601             let features = syntax::feature_gate::check_crate(sess.codemap(),
602                                                              &sess.parse_sess.span_diagnostic,
603                                                              &krate,
604                                                              &attributes,
605                                                              sess.opts.unstable_features);
606             *sess.features.borrow_mut() = features;
607         });
608     });
609
610     // JBC: make CFG processing part of expansion to avoid this problem:
611
612     // strip again, in case expansion added anything with a #[cfg].
613     krate = sess.abort_if_new_errors(|| {
614         let krate = time(time_passes, "configuration 2", || {
615             syntax::config::strip_unconfigured_items(sess.diagnostic(),
616                                                      krate,
617                                                      &mut feature_gated_cfgs)
618         });
619
620         time(time_passes, "gated configuration checking", || {
621             let features = sess.features.borrow();
622             feature_gated_cfgs.sort();
623             feature_gated_cfgs.dedup();
624             for cfg in &feature_gated_cfgs {
625                 cfg.check_and_emit(sess.diagnostic(), &features, sess.codemap());
626             }
627         });
628
629         krate
630     });
631
632     krate = time(time_passes, "maybe building test harness", || {
633         syntax::test::modify_for_testing(&sess.parse_sess, &sess.opts.cfg, krate, sess.diagnostic())
634     });
635
636     krate = time(time_passes,
637                  "prelude injection",
638                  || syntax::std_inject::maybe_inject_prelude(&sess.parse_sess, krate));
639
640     time(time_passes,
641          "checking that all macro invocations are gone",
642          || syntax::ext::expand::check_for_macros(&sess.parse_sess, &krate));
643
644     time(time_passes,
645          "checking for inline asm in case the target doesn't support it",
646          || no_asm::check_crate(sess, &krate));
647
648     // One final feature gating of the true AST that gets compiled
649     // later, to make sure we've got everything (e.g. configuration
650     // can insert new attributes via `cfg_attr`)
651     time(time_passes, "complete gated feature checking 2", || {
652         sess.abort_if_new_errors(|| {
653             let features = syntax::feature_gate::check_crate(sess.codemap(),
654                                                              &sess.parse_sess.span_diagnostic,
655                                                              &krate,
656                                                              &attributes,
657                                                              sess.opts.unstable_features);
658             *sess.features.borrow_mut() = features;
659         });
660     });
661
662     time(time_passes,
663          "const fn bodies and arguments",
664          || const_fn::check_crate(sess, &krate));
665
666     if sess.opts.debugging_opts.input_stats {
667         println!("Post-expansion node count: {}", count_nodes(&krate));
668     }
669
670     Some(krate)
671 }
672
673 pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate {
674     struct NodeIdAssigner<'a> {
675         sess: &'a Session,
676     }
677
678     impl<'a> Folder for NodeIdAssigner<'a> {
679         fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId {
680             assert_eq!(old_id, ast::DUMMY_NODE_ID);
681             self.sess.next_node_id()
682         }
683     }
684
685     let krate = time(sess.time_passes(),
686                      "assigning node ids",
687                      || NodeIdAssigner { sess: sess }.fold_crate(krate));
688
689     if sess.opts.debugging_opts.ast_json {
690         println!("{}", json::as_json(&krate));
691     }
692
693     krate
694 }
695
696 pub fn make_map<'ast>(sess: &Session,
697                       forest: &'ast mut hir_map::Forest)
698                       -> hir_map::Map<'ast> {
699     // Construct the HIR map
700     time(sess.time_passes(),
701          "indexing hir",
702          move || hir_map::map_crate(forest))
703 }
704
705 /// Run the resolution, typechecking, region checking and other
706 /// miscellaneous analysis passes on the crate. Return various
707 /// structures carrying the results of the analysis.
708 pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
709                                                cstore: &CStore,
710                                                hir_map: hir_map::Map<'tcx>,
711                                                arenas: &'tcx ty::CtxtArenas<'tcx>,
712                                                name: &str,
713                                                make_glob_map: resolve::MakeGlobMap,
714                                                f: F)
715                                                -> R
716     where F: for<'a> FnOnce(&'a ty::ctxt<'tcx>, MirMap<'tcx>, ty::CrateAnalysis) -> R
717 {
718     let time_passes = sess.time_passes();
719     let krate = hir_map.krate();
720
721     time(time_passes,
722          "external crate/lib resolution",
723          || LocalCrateReader::new(sess, cstore, &hir_map).read_crates(krate));
724
725     let lang_items = time(time_passes, "language item collection", || {
726         sess.abort_if_new_errors(|| {
727             middle::lang_items::collect_language_items(&sess, &hir_map)
728         })
729     });
730
731     let resolve::CrateMap {
732         def_map,
733         freevars,
734         export_map,
735         trait_map,
736         external_exports,
737         glob_map,
738     } = time(time_passes,
739              "resolution",
740              || resolve::resolve_crate(sess, &hir_map, make_glob_map));
741
742     let named_region_map = time(time_passes,
743                                 "lifetime resolution",
744                                 || middle::resolve_lifetime::krate(sess, krate, &def_map.borrow()));
745
746     time(time_passes,
747          "looking for entry point",
748          || middle::entry::find_entry_point(sess, &hir_map));
749
750     sess.plugin_registrar_fn.set(time(time_passes, "looking for plugin registrar", || {
751         plugin::build::find_plugin_registrar(sess.diagnostic(), krate)
752     }));
753
754     let region_map = time(time_passes,
755                           "region resolution",
756                           || middle::region::resolve_crate(sess, krate));
757
758     time(time_passes,
759          "loop checking",
760          || loops::check_crate(sess, krate));
761
762     time(time_passes,
763          "static item recursion checking",
764          || static_recursion::check_crate(sess, krate, &def_map.borrow(), &hir_map));
765
766     ty::ctxt::create_and_enter(sess,
767                                arenas,
768                                def_map,
769                                named_region_map,
770                                hir_map,
771                                freevars,
772                                region_map,
773                                lang_items,
774                                stability::Index::new(krate),
775                                |tcx| {
776                                    // passes are timed inside typeck
777                                    typeck::check_crate(tcx, trait_map);
778
779                                    time(time_passes,
780                                         "const checking",
781                                         || consts::check_crate(tcx));
782
783                                    let access_levels =
784                                        time(time_passes, "privacy checking", || {
785                                            rustc_privacy::check_crate(tcx,
786                                                                       &export_map,
787                                                                       external_exports)
788                                        });
789
790                                    // Do not move this check past lint
791                                    time(time_passes, "stability index", || {
792                                        tcx.stability.borrow_mut().build(tcx, krate, &access_levels)
793                                    });
794
795                                    time(time_passes,
796                                         "intrinsic checking",
797                                         || middle::intrinsicck::check_crate(tcx));
798
799                                    time(time_passes,
800                                         "effect checking",
801                                         || middle::effect::check_crate(tcx));
802
803                                    time(time_passes,
804                                         "match checking",
805                                         || middle::check_match::check_crate(tcx));
806
807                                    let mir_map =
808                                        time(time_passes,
809                                             "MIR dump",
810                                             || mir::mir_map::build_mir_for_crate(tcx));
811
812                                    time(time_passes,
813                                         "liveness checking",
814                                         || middle::liveness::check_crate(tcx));
815
816                                    time(time_passes,
817                                         "borrow checking",
818                                         || borrowck::check_crate(tcx));
819
820                                    time(time_passes,
821                                         "rvalue checking",
822                                         || rvalues::check_crate(tcx));
823
824                                    // Avoid overwhelming user with errors if type checking failed.
825                                    // I'm not sure how helpful this is, to be honest, but it avoids
826                                    // a
827                                    // lot of annoying errors in the compile-fail tests (basically,
828                                    // lint warnings and so on -- kindck used to do this abort, but
829                                    // kindck is gone now). -nmatsakis
830                                    tcx.sess.abort_if_errors();
831
832                                    let reachable_map =
833                                        time(time_passes,
834                                             "reachability checking",
835                                             || reachable::find_reachable(tcx, &access_levels));
836
837                                    time(time_passes, "death checking", || {
838                                        middle::dead::check_crate(tcx, &access_levels);
839                                    });
840
841                                    let ref lib_features_used =
842                                        time(time_passes,
843                                             "stability checking",
844                                             || stability::check_unstable_api_usage(tcx));
845
846                                    time(time_passes, "unused lib feature checking", || {
847                                        stability::check_unused_or_stable_features(&tcx.sess,
848                                                                                   lib_features_used)
849                                    });
850
851                                    time(time_passes,
852                                         "lint checking",
853                                         || lint::check_crate(tcx, &access_levels));
854
855                                    // The above three passes generate errors w/o aborting
856                                    tcx.sess.abort_if_errors();
857
858                                    f(tcx,
859                                      mir_map,
860                                      ty::CrateAnalysis {
861                                          export_map: export_map,
862                                          access_levels: access_levels,
863                                          reachable: reachable_map,
864                                          name: name,
865                                          glob_map: glob_map,
866                                      })
867                                })
868 }
869
870 /// Run the translation phase to LLVM, after which the AST and analysis can
871 /// be discarded.
872 pub fn phase_4_translate_to_llvm<'tcx>(tcx: &ty::ctxt<'tcx>,
873                                        mut mir_map: MirMap<'tcx>,
874                                        analysis: ty::CrateAnalysis)
875                                        -> trans::CrateTranslation {
876     let time_passes = tcx.sess.time_passes();
877
878     time(time_passes,
879          "resolving dependency formats",
880          || dependency_format::calculate(&tcx.sess));
881
882     time(time_passes,
883          "erasing regions from MIR",
884          || mir::transform::erase_regions::erase_regions(tcx, &mut mir_map));
885
886     // Option dance to work around the lack of stack once closures.
887     time(time_passes,
888          "translation",
889          move || trans::trans_crate(tcx, &mir_map, analysis))
890 }
891
892 /// Run LLVM itself, producing a bitcode file, assembly file or object file
893 /// as a side effect.
894 pub fn phase_5_run_llvm_passes(sess: &Session,
895                                trans: &trans::CrateTranslation,
896                                outputs: &OutputFilenames) {
897     if sess.opts.cg.no_integrated_as {
898         let mut map = HashMap::new();
899         map.insert(OutputType::Assembly, None);
900         time(sess.time_passes(),
901              "LLVM passes",
902              || write::run_passes(sess, trans, &map, outputs));
903
904         write::run_assembler(sess, outputs);
905
906         // Remove assembly source, unless --save-temps was specified
907         if !sess.opts.cg.save_temps {
908             fs::remove_file(&outputs.temp_path(OutputType::Assembly)).unwrap();
909         }
910     } else {
911         time(sess.time_passes(),
912              "LLVM passes",
913              || write::run_passes(sess, trans, &sess.opts.output_types, outputs));
914     }
915
916     sess.abort_if_errors();
917 }
918
919 /// Run the linker on any artifacts that resulted from the LLVM run.
920 /// This should produce either a finished executable or library.
921 pub fn phase_6_link_output(sess: &Session,
922                            trans: &trans::CrateTranslation,
923                            outputs: &OutputFilenames) {
924     time(sess.time_passes(),
925          "linking",
926          || link::link_binary(sess, trans, outputs, &trans.link.crate_name));
927 }
928
929 fn escape_dep_filename(filename: &str) -> String {
930     // Apparently clang and gcc *only* escape spaces:
931     // http://llvm.org/klaus/clang/commit/9d50634cfc268ecc9a7250226dd5ca0e945240d4
932     filename.replace(" ", "\\ ")
933 }
934
935 fn write_out_deps(sess: &Session, outputs: &OutputFilenames, id: &str) {
936     let mut out_filenames = Vec::new();
937     for output_type in sess.opts.output_types.keys() {
938         let file = outputs.path(*output_type);
939         match *output_type {
940             OutputType::Exe => {
941                 for output in sess.crate_types.borrow().iter() {
942                     let p = link::filename_for_input(sess, *output, id, outputs);
943                     out_filenames.push(p);
944                 }
945             }
946             _ => {
947                 out_filenames.push(file);
948             }
949         }
950     }
951
952     // Write out dependency rules to the dep-info file if requested
953     if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
954         return;
955     }
956     let deps_filename = outputs.path(OutputType::DepInfo);
957
958     let result =
959         (|| -> io::Result<()> {
960             // Build a list of files used to compile the output and
961             // write Makefile-compatible dependency rules
962             let files: Vec<String> = sess.codemap()
963                                          .files
964                                          .borrow()
965                                          .iter()
966                                          .filter(|fmap| fmap.is_real_file())
967                                          .filter(|fmap| !fmap.is_imported())
968                                          .map(|fmap| escape_dep_filename(&fmap.name))
969                                          .collect();
970             let mut file = try!(fs::File::create(&deps_filename));
971             for path in &out_filenames {
972                 try!(write!(file, "{}: {}\n\n", path.display(), files.join(" ")));
973             }
974
975             // Emit a fake target for each input file to the compilation. This
976             // prevents `make` from spitting out an error if a file is later
977             // deleted. For more info see #28735
978             for path in files {
979                 try!(writeln!(file, "{}:", path));
980             }
981             Ok(())
982         })();
983
984     match result {
985         Ok(()) => {}
986         Err(e) => {
987             sess.fatal(&format!("error writing dependencies to `{}`: {}",
988                                 deps_filename.display(),
989                                 e));
990         }
991     }
992 }
993
994 pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<config::CrateType> {
995     // Unconditionally collect crate types from attributes to make them used
996     let attr_types: Vec<config::CrateType> =
997         attrs.iter()
998              .filter_map(|a| {
999                  if a.check_name("crate_type") {
1000                      match a.value_str() {
1001                          Some(ref n) if *n == "rlib" => {
1002                              Some(config::CrateTypeRlib)
1003                          }
1004                          Some(ref n) if *n == "dylib" => {
1005                              Some(config::CrateTypeDylib)
1006                          }
1007                          Some(ref n) if *n == "lib" => {
1008                              Some(config::default_lib_output())
1009                          }
1010                          Some(ref n) if *n == "staticlib" => {
1011                              Some(config::CrateTypeStaticlib)
1012                          }
1013                          Some(ref n) if *n == "bin" => Some(config::CrateTypeExecutable),
1014                          Some(_) => {
1015                              session.add_lint(lint::builtin::UNKNOWN_CRATE_TYPES,
1016                                               ast::CRATE_NODE_ID,
1017                                               a.span,
1018                                               "invalid `crate_type` value".to_string());
1019                              None
1020                          }
1021                          _ => {
1022                              session.struct_span_err(a.span, "`crate_type` requires a value")
1023                                  .note("for example: `#![crate_type=\"lib\"]`")
1024                                  .emit();
1025                              None
1026                          }
1027                      }
1028                  } else {
1029                      None
1030                  }
1031              })
1032              .collect();
1033
1034     // If we're generating a test executable, then ignore all other output
1035     // styles at all other locations
1036     if session.opts.test {
1037         return vec![config::CrateTypeExecutable];
1038     }
1039
1040     // Only check command line flags if present. If no types are specified by
1041     // command line, then reuse the empty `base` Vec to hold the types that
1042     // will be found in crate attributes.
1043     let mut base = session.opts.crate_types.clone();
1044     if base.is_empty() {
1045         base.extend(attr_types);
1046         if base.is_empty() {
1047             base.push(link::default_output_for_target(session));
1048         }
1049         base.sort();
1050         base.dedup();
1051     }
1052
1053     base.into_iter()
1054         .filter(|crate_type| {
1055             let res = !link::invalid_output_for_target(session, *crate_type);
1056
1057             if !res {
1058                 session.warn(&format!("dropping unsupported crate type `{}` for target `{}`",
1059                                       *crate_type,
1060                                       session.opts.target_triple));
1061             }
1062
1063             res
1064         })
1065         .collect()
1066 }
1067
1068 pub fn collect_crate_metadata(session: &Session, _attrs: &[ast::Attribute]) -> Vec<String> {
1069     session.opts.cg.metadata.clone()
1070 }
1071
1072 pub fn build_output_filenames(input: &Input,
1073                               odir: &Option<PathBuf>,
1074                               ofile: &Option<PathBuf>,
1075                               attrs: &[ast::Attribute],
1076                               sess: &Session)
1077                               -> OutputFilenames {
1078     match *ofile {
1079         None => {
1080             // "-" as input file will cause the parser to read from stdin so we
1081             // have to make up a name
1082             // We want to toss everything after the final '.'
1083             let dirpath = match *odir {
1084                 Some(ref d) => d.clone(),
1085                 None => PathBuf::new(),
1086             };
1087
1088             // If a crate name is present, we use it as the link name
1089             let stem = sess.opts
1090                            .crate_name
1091                            .clone()
1092                            .or_else(|| attr::find_crate_name(attrs).map(|n| n.to_string()))
1093                            .unwrap_or(input.filestem());
1094
1095             OutputFilenames {
1096                 out_directory: dirpath,
1097                 out_filestem: stem,
1098                 single_output_file: None,
1099                 extra: sess.opts.cg.extra_filename.clone(),
1100                 outputs: sess.opts.output_types.clone(),
1101             }
1102         }
1103
1104         Some(ref out_file) => {
1105             let unnamed_output_types = sess.opts
1106                                            .output_types
1107                                            .values()
1108                                            .filter(|a| a.is_none())
1109                                            .count();
1110             let ofile = if unnamed_output_types > 1 {
1111                 sess.warn("ignoring specified output filename because multiple outputs were \
1112                            requested");
1113                 None
1114             } else {
1115                 Some(out_file.clone())
1116             };
1117             if *odir != None {
1118                 sess.warn("ignoring --out-dir flag due to -o flag.");
1119             }
1120
1121             let cur_dir = Path::new("");
1122
1123             OutputFilenames {
1124                 out_directory: out_file.parent().unwrap_or(cur_dir).to_path_buf(),
1125                 out_filestem: out_file.file_stem()
1126                                       .unwrap_or(OsStr::new(""))
1127                                       .to_str()
1128                                       .unwrap()
1129                                       .to_string(),
1130                 single_output_file: ofile,
1131                 extra: sess.opts.cg.extra_filename.clone(),
1132                 outputs: sess.opts.output_types.clone(),
1133             }
1134         }
1135     }
1136 }