Skip to content

Commit 283c94c

Browse files
Clean up trans::trans_crate() after making things collector driven.
1 parent 37a10ec commit 283c94c

File tree

2 files changed

+69
-62
lines changed

2 files changed

+69
-62
lines changed

src/librustc_trans/base.rs

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,34 +2198,17 @@ pub fn set_link_section(ccx: &CrateContext,
21982198
}
21992199
}
22002200

2201-
pub fn trans_item(ccx: &CrateContext, item: &hir::Item) {
2201+
fn trans_item(ccx: &CrateContext, item: &hir::Item) {
22022202
let _icx = push_ctxt("trans_item");
22032203

2204-
let tcx = ccx.tcx();
22052204
match item.node {
2206-
hir::ItemFn(_, _, _, _, _, _) => {
2207-
let def_id = tcx.map.local_def_id(item.id);
2208-
// check for the #[rustc_error] annotation, which forces an
2209-
// error in trans. This is used to write compile-fail tests
2210-
// that actually test that compilation succeeds without
2211-
// reporting an error.
2212-
if is_entry_fn(ccx.sess(), item.id) {
2213-
let empty_substs = ccx.empty_substs_for_def_id(def_id);
2214-
let llfn = Callee::def(ccx, def_id, empty_substs).reify(ccx).val;
2215-
create_entry_wrapper(ccx, item.span, llfn);
2216-
if tcx.has_attr(def_id, "rustc_error") {
2217-
tcx.sess.span_fatal(item.span, "compilation successful");
2218-
}
2219-
}
2220-
2221-
// Function is actually translated in trans_instance
2222-
}
22232205
hir::ItemEnum(ref enum_definition, ref gens) => {
22242206
if gens.ty_params.is_empty() {
22252207
// sizes only make sense for non-generic types
22262208
enum_variant_size_lint(ccx, enum_definition, item.span, item.id);
22272209
}
22282210
}
2211+
hir::ItemFn(..) |
22292212
hir::ItemImpl(..) |
22302213
hir::ItemStatic(..) => {
22312214
// Don't do anything here. Translation has been moved to
@@ -2235,22 +2218,40 @@ pub fn trans_item(ccx: &CrateContext, item: &hir::Item) {
22352218
}
22362219
}
22372220

2238-
pub fn is_entry_fn(sess: &Session, node_id: ast::NodeId) -> bool {
2239-
match *sess.entry_fn.borrow() {
2240-
Some((entry_id, _)) => node_id == entry_id,
2241-
None => false,
2221+
/// Create the `main` function which will initialise the rust runtime and call
2222+
/// users’ main function.
2223+
pub fn maybe_create_entry_wrapper(ccx: &CrateContext) {
2224+
let (main_def_id, span) = match *ccx.sess().entry_fn.borrow() {
2225+
Some((id, span)) => {
2226+
(ccx.tcx().map.local_def_id(id), span)
2227+
}
2228+
None => return,
2229+
};
2230+
2231+
// check for the #[rustc_error] annotation, which forces an
2232+
// error in trans. This is used to write compile-fail tests
2233+
// that actually test that compilation succeeds without
2234+
// reporting an error.
2235+
if ccx.tcx().has_attr(main_def_id, "rustc_error") {
2236+
ccx.tcx().sess.span_fatal(span, "compilation successful");
2237+
}
2238+
2239+
let instance = Instance::mono(ccx.shared(), main_def_id);
2240+
2241+
if !ccx.codegen_unit().items.contains_key(&TransItem::Fn(instance)) {
2242+
// We want to create the wrapper in the same codegen unit as Rust's main
2243+
// function.
2244+
return;
22422245
}
2243-
}
22442246

2245-
/// Create the `main` function which will initialise the rust runtime and call users’ main
2246-
/// function.
2247-
pub fn create_entry_wrapper(ccx: &CrateContext, sp: Span, main_llfn: ValueRef) {
2247+
let main_llfn = Callee::def(ccx, main_def_id, instance.substs).reify(ccx).val;
2248+
22482249
let et = ccx.sess().entry_type.get().unwrap();
22492250
match et {
22502251
config::EntryMain => {
2251-
create_entry_fn(ccx, sp, main_llfn, true);
2252+
create_entry_fn(ccx, span, main_llfn, true);
22522253
}
2253-
config::EntryStart => create_entry_fn(ccx, sp, main_llfn, false),
2254+
config::EntryStart => create_entry_fn(ccx, span, main_llfn, false),
22542255
config::EntryNone => {} // Do nothing.
22552256
}
22562257

@@ -2590,13 +2591,11 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
25902591
};
25912592
let no_builtins = attr::contains_name(&krate.attrs, "no_builtins");
25922593

2593-
let (codegen_units, symbol_map) =
2594-
collect_and_partition_translation_items(&shared_ccx);
2594+
// Run the translation item collector and partition the collected items into
2595+
// codegen units.
2596+
let (codegen_units, symbol_map) = collect_and_partition_translation_items(&shared_ccx);
25952597
let codegen_unit_count = codegen_units.len();
25962598

2597-
assert!(tcx.sess.opts.cg.codegen_units == codegen_unit_count ||
2598-
tcx.sess.opts.debugging_opts.incremental.is_some());
2599-
26002599
let symbol_map = Rc::new(symbol_map);
26012600

26022601
let crate_context_list = CrateContextList::new(&shared_ccx,
@@ -2642,35 +2641,39 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
26422641
for (trans_item, _) in trans_items {
26432642
trans_item.define(&ccx);
26442643
}
2645-
}
2646-
2647-
{
2648-
let ccx = crate_context_list.get_ccx(0);
2649-
2650-
// Translate all items. See `TransModVisitor` for
2651-
// details on why we walk in this particular way.
2652-
{
2653-
let _icx = push_ctxt("text");
2654-
intravisit::walk_mod(&mut TransItemsWithinModVisitor { ccx: &ccx }, &krate.module);
2655-
krate.visit_all_items(&mut TransModVisitor { ccx: &ccx });
2656-
}
26572644

2658-
collector::print_collection_results(ccx.shared());
2645+
// If this codegen unit contains the main function, also create the
2646+
// wrapper here
2647+
maybe_create_entry_wrapper(&ccx);
26592648

2660-
symbol_names_test::report_symbol_names(&ccx);
2661-
}
2662-
2663-
for ccx in crate_context_list.iter() {
2664-
if ccx.sess().opts.debuginfo != NoDebugInfo {
2665-
debuginfo::finalize(&ccx);
2666-
}
2649+
// Run replace-all-uses-with for statics that need it
26672650
for &(old_g, new_g) in ccx.statics_to_rauw().borrow().iter() {
26682651
unsafe {
26692652
let bitcast = llvm::LLVMConstPointerCast(new_g, llvm::LLVMTypeOf(old_g));
26702653
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
26712654
llvm::LLVMDeleteGlobal(old_g);
26722655
}
26732656
}
2657+
2658+
// Finalize debuginfo
2659+
if ccx.sess().opts.debuginfo != NoDebugInfo {
2660+
debuginfo::finalize(&ccx);
2661+
}
2662+
}
2663+
2664+
collector::print_collection_results(&shared_ccx);
2665+
symbol_names_test::report_symbol_names(&shared_ccx);
2666+
2667+
{
2668+
let ccx = crate_context_list.get_ccx(0);
2669+
2670+
// At this point, we only walk the HIR for running
2671+
// enum_variant_size_lint(). This should arguably be moved somewhere
2672+
// else
2673+
{
2674+
intravisit::walk_mod(&mut TransItemsWithinModVisitor { ccx: &ccx }, &krate.module);
2675+
krate.visit_all_items(&mut TransModVisitor { ccx: &ccx });
2676+
}
26742677
}
26752678

26762679
if shared_ccx.sess().trans_stats() {
@@ -2696,6 +2699,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
26962699
}
26972700
}
26982701
}
2702+
26992703
if shared_ccx.sess().count_llvm_insns() {
27002704
for (k, v) in shared_ccx.stats().llvm_insns.borrow().iter() {
27012705
println!("{:7} {}", *v, *k);
@@ -2867,6 +2871,9 @@ fn collect_and_partition_translation_items<'a, 'tcx>(scx: &SharedCrateContext<'a
28672871
scx.reachable())
28682872
});
28692873

2874+
assert!(scx.tcx().sess.opts.cg.codegen_units == codegen_units.len() ||
2875+
scx.tcx().sess.opts.debugging_opts.incremental.is_some());
2876+
28702877
if scx.sess().opts.debugging_opts.print_trans_items.is_some() {
28712878
let mut item_to_cgus = HashMap::new();
28722879

src/librustc_trans/symbol_names_test.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,40 @@ use rustc::hir::intravisit::{self, Visitor};
1919
use syntax::ast;
2020
use syntax::attr::AttrMetaMethods;
2121

22-
use common::CrateContext;
22+
use common::SharedCrateContext;
2323
use monomorphize::Instance;
2424

2525
const SYMBOL_NAME: &'static str = "rustc_symbol_name";
2626
const ITEM_PATH: &'static str = "rustc_item_path";
2727

28-
pub fn report_symbol_names(ccx: &CrateContext) {
28+
pub fn report_symbol_names(scx: &SharedCrateContext) {
2929
// if the `rustc_attrs` feature is not enabled, then the
3030
// attributes we are interested in cannot be present anyway, so
3131
// skip the walk.
32-
let tcx = ccx.tcx();
32+
let tcx = scx.tcx();
3333
if !tcx.sess.features.borrow().rustc_attrs {
3434
return;
3535
}
3636

3737
let _ignore = tcx.dep_graph.in_ignore();
38-
let mut visitor = SymbolNamesTest { ccx: ccx };
38+
let mut visitor = SymbolNamesTest { scx: scx };
3939
tcx.map.krate().visit_all_items(&mut visitor);
4040
}
4141

4242
struct SymbolNamesTest<'a, 'tcx:'a> {
43-
ccx: &'a CrateContext<'a, 'tcx>,
43+
scx: &'a SharedCrateContext<'a, 'tcx>,
4444
}
4545

4646
impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
4747
fn process_attrs(&mut self,
4848
node_id: ast::NodeId) {
49-
let tcx = self.ccx.tcx();
49+
let tcx = self.scx.tcx();
5050
let def_id = tcx.map.local_def_id(node_id);
5151
for attr in tcx.get_attrs(def_id).iter() {
5252
if attr.check_name(SYMBOL_NAME) {
5353
// for now, can only use on monomorphic names
54-
let instance = Instance::mono(self.ccx.shared(), def_id);
55-
let name = instance.symbol_name(self.ccx.shared());
54+
let instance = Instance::mono(self.scx, def_id);
55+
let name = instance.symbol_name(self.scx);
5656
tcx.sess.span_err(attr.span, &format!("symbol-name({})", name));
5757
} else if attr.check_name(ITEM_PATH) {
5858
let path = tcx.item_path_str(def_id);

0 commit comments

Comments
 (0)