]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/entry.rs
rollup merge of #17306 : scialex/fix-zsh
[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 driver::config;
13 use driver::session::Session;
14 use syntax::ast::{Name, NodeId, Item, ItemFn};
15 use syntax::ast_map;
16 use syntax::attr;
17 use syntax::codemap::Span;
18 use syntax::parse::token;
19 use syntax::visit;
20 use syntax::visit::Visitor;
21
22 struct EntryContext<'a, 'ast: 'a> {
23     session: &'a Session,
24
25     ast_map: &'a ast_map::Map<'ast>,
26
27     // The interned Name for "main".
28     main_name: Name,
29
30     // The top-level function called 'main'
31     main_fn: Option<(NodeId, Span)>,
32
33     // The function that has attribute named 'main'
34     attr_main_fn: Option<(NodeId, Span)>,
35
36     // The function that has the attribute 'start' on it
37     start_fn: Option<(NodeId, Span)>,
38
39     // The functions that one might think are 'main' but aren't, e.g.
40     // main functions not defined at the top level. For diagnostics.
41     non_main_fns: Vec<(NodeId, Span)> ,
42 }
43
44 impl<'a, 'ast, 'v> Visitor<'v> for EntryContext<'a, 'ast> {
45     fn visit_item(&mut self, item: &Item) {
46         find_item(item, self);
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.as_slice(), "no_main") {
61         session.entry_type.set(Some(config::EntryNone));
62         return
63     }
64
65     let mut ctxt = EntryContext {
66         session: session,
67         main_name: token::intern("main"),
68         ast_map: ast_map,
69         main_fn: None,
70         attr_main_fn: None,
71         start_fn: None,
72         non_main_fns: Vec::new(),
73     };
74
75     visit::walk_crate(&mut ctxt, ast_map.krate());
76
77     configure_main(&mut ctxt);
78 }
79
80 fn find_item(item: &Item, ctxt: &mut EntryContext) {
81     match item.node {
82         ItemFn(..) => {
83             if item.ident.name == ctxt.main_name {
84                  ctxt.ast_map.with_path(item.id, |mut path| {
85                         if path.count() == 1 {
86                             // This is a top-level function so can be 'main'
87                             if ctxt.main_fn.is_none() {
88                                 ctxt.main_fn = Some((item.id, item.span));
89                             } else {
90                                 span_err!(ctxt.session, item.span, E0136,
91                                           "multiple 'main' functions");
92                             }
93                         } else {
94                             // This isn't main
95                             ctxt.non_main_fns.push((item.id, item.span));
96                         }
97                 });
98             }
99
100             if attr::contains_name(item.attrs.as_slice(), "main") {
101                 if ctxt.attr_main_fn.is_none() {
102                     ctxt.attr_main_fn = Some((item.id, item.span));
103                 } else {
104                     span_err!(ctxt.session, item.span, E0137,
105                               "multiple functions with a #[main] attribute");
106                 }
107             }
108
109             if attr::contains_name(item.attrs.as_slice(), "start") {
110                 if ctxt.start_fn.is_none() {
111                     ctxt.start_fn = Some((item.id, item.span));
112                 } else {
113                     span_err!(ctxt.session, item.span, E0138,
114                               "multiple 'start' functions");
115                 }
116             }
117         }
118         _ => ()
119     }
120
121     visit::walk_item(ctxt, item);
122 }
123
124 fn configure_main(this: &mut EntryContext) {
125     if this.start_fn.is_some() {
126         *this.session.entry_fn.borrow_mut() = this.start_fn;
127         this.session.entry_type.set(Some(config::EntryStart));
128     } else if this.attr_main_fn.is_some() {
129         *this.session.entry_fn.borrow_mut() = this.attr_main_fn;
130         this.session.entry_type.set(Some(config::EntryMain));
131     } else if this.main_fn.is_some() {
132         *this.session.entry_fn.borrow_mut() = this.main_fn;
133         this.session.entry_type.set(Some(config::EntryMain));
134     } else {
135         // No main function
136         this.session.err("main function not found");
137         if !this.non_main_fns.is_empty() {
138             // There were some functions named 'main' though. Try to give the user a hint.
139             this.session.note("the main function must be defined at the crate level \
140                                but you have one or more functions named 'main' that are not \
141                                defined at the crate level. Either move the definition or \
142                                attach the `#[main]` attribute to override this behavior.");
143             for &(_, span) in this.non_main_fns.iter() {
144                 this.session.span_note(span, "here is a function named 'main'");
145             }
146             this.session.abort_if_errors();
147         }
148     }
149 }