]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/entry.rs
Auto merge of #28668 - alexcrichton:crossing-with-jemalloc, r=nrc
[rust.git] / src / librustc / middle / entry.rs
1 // Copyright 2012 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
12 use front::map as ast_map;
13 use session::{config, Session};
14 use syntax::ast::NodeId;
15 use syntax::attr;
16 use syntax::codemap::Span;
17 use syntax::entry::EntryPointType;
18 use rustc_front::hir::{Item, ItemFn};
19 use rustc_front::visit;
20 use rustc_front::visit::Visitor;
21
22 struct EntryContext<'a> {
23     session: &'a Session,
24
25     // The current depth in the ast
26     depth: usize,
27
28     // The top-level function called 'main'
29     main_fn: Option<(NodeId, Span)>,
30
31     // The function that has attribute named 'main'
32     attr_main_fn: Option<(NodeId, Span)>,
33
34     // The function that has the attribute 'start' on it
35     start_fn: Option<(NodeId, Span)>,
36
37     // The functions that one might think are 'main' but aren't, e.g.
38     // main functions not defined at the top level. For diagnostics.
39     non_main_fns: Vec<(NodeId, Span)> ,
40 }
41
42 impl<'a, 'v> Visitor<'v> for EntryContext<'a> {
43     fn visit_item(&mut self, item: &Item) {
44         self.depth += 1;
45         find_item(item, self);
46         self.depth -= 1;
47     }
48 }
49
50 pub fn find_entry_point(session: &Session, ast_map: &ast_map::Map) {
51     let any_exe = session.crate_types.borrow().iter().any(|ty| {
52         *ty == config::CrateTypeExecutable
53     });
54     if !any_exe {
55         // No need to find a main function
56         return
57     }
58
59     // If the user wants no main function at all, then stop here.
60     if attr::contains_name(&ast_map.krate().attrs, "no_main") {
61         session.entry_type.set(Some(config::EntryNone));
62         return
63     }
64
65     let mut ctxt = EntryContext {
66         session: session,
67         depth: 0,
68         main_fn: None,
69         attr_main_fn: None,
70         start_fn: None,
71         non_main_fns: Vec::new(),
72     };
73
74     visit::walk_crate(&mut ctxt, ast_map.krate());
75
76     configure_main(&mut ctxt);
77 }
78
79 // Beware, this is duplicated in libsyntax/entry.rs, make sure to keep
80 // them in sync.
81 fn entry_point_type(item: &Item, depth: usize) -> EntryPointType {
82     match item.node {
83         ItemFn(..) => {
84             if attr::contains_name(&item.attrs, "start") {
85                 EntryPointType::Start
86             } else if attr::contains_name(&item.attrs, "main") {
87                 EntryPointType::MainAttr
88             } else if item.name.as_str() == "main" {
89                 if depth == 1 {
90                     // This is a top-level function so can be 'main'
91                     EntryPointType::MainNamed
92                 } else {
93                     EntryPointType::OtherMain
94                 }
95             } else {
96                 EntryPointType::None
97             }
98         }
99         _ => EntryPointType::None,
100     }
101 }
102
103
104 fn find_item(item: &Item, ctxt: &mut EntryContext) {
105     match entry_point_type(item, ctxt.depth) {
106         EntryPointType::MainNamed => {
107             if ctxt.main_fn.is_none() {
108                 ctxt.main_fn = Some((item.id, item.span));
109             } else {
110                 span_err!(ctxt.session, item.span, E0136,
111                           "multiple 'main' functions");
112             }
113         },
114         EntryPointType::OtherMain => {
115             ctxt.non_main_fns.push((item.id, item.span));
116         },
117         EntryPointType::MainAttr => {
118             if ctxt.attr_main_fn.is_none() {
119                 ctxt.attr_main_fn = Some((item.id, item.span));
120             } else {
121                 span_err!(ctxt.session, item.span, E0137,
122                           "multiple functions with a #[main] attribute");
123             }
124         },
125         EntryPointType::Start => {
126             if ctxt.start_fn.is_none() {
127                 ctxt.start_fn = Some((item.id, item.span));
128             } else {
129                 span_err!(ctxt.session, item.span, E0138,
130                           "multiple 'start' functions");
131             }
132         },
133         EntryPointType::None => ()
134     }
135
136     visit::walk_item(ctxt, item);
137 }
138
139 fn configure_main(this: &mut EntryContext) {
140     if this.start_fn.is_some() {
141         *this.session.entry_fn.borrow_mut() = this.start_fn;
142         this.session.entry_type.set(Some(config::EntryStart));
143     } else if this.attr_main_fn.is_some() {
144         *this.session.entry_fn.borrow_mut() = this.attr_main_fn;
145         this.session.entry_type.set(Some(config::EntryMain));
146     } else if this.main_fn.is_some() {
147         *this.session.entry_fn.borrow_mut() = this.main_fn;
148         this.session.entry_type.set(Some(config::EntryMain));
149     } else {
150         // No main function
151         this.session.err("main function not found");
152         if !this.non_main_fns.is_empty() {
153             // There were some functions named 'main' though. Try to give the user a hint.
154             this.session.note("the main function must be defined at the crate level \
155                                but you have one or more functions named 'main' that are not \
156                                defined at the crate level. Either move the definition or \
157                                attach the `#[main]` attribute to override this behavior.");
158             for &(_, span) in &this.non_main_fns {
159                 this.session.span_note(span, "here is a function named 'main'");
160             }
161             this.session.abort_if_errors();
162         }
163     }
164 }