]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/entry.rs
librustc: Automatically change uses of `~[T]` to `Vec<T>` in rustc.
[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::session;
13 use driver::session::Session;
14 use syntax::ast::{Crate, 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> {
23     session: Session,
24
25     ast_map: &'a ast_map::Map,
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> Visitor<()> for EntryContext<'a> {
45     fn visit_item(&mut self, item: &Item, _:()) {
46         find_item(item, self);
47     }
48 }
49
50 pub fn find_entry_point(session: Session, krate: &Crate, ast_map: &ast_map::Map) {
51     if session.building_library.get() {
52         // No need to find a main function
53         return;
54     }
55
56     // If the user wants no main function at all, then stop here.
57     if attr::contains_name(krate.attrs.as_slice(), "no_main") {
58         session.entry_type.set(Some(session::EntryNone));
59         return
60     }
61
62     let mut ctxt = EntryContext {
63         session: session,
64         main_name: token::intern("main"),
65         ast_map: ast_map,
66         main_fn: None,
67         attr_main_fn: None,
68         start_fn: None,
69         non_main_fns: Vec::new(),
70     };
71
72     visit::walk_crate(&mut ctxt, krate, ());
73
74     configure_main(&mut ctxt);
75 }
76
77 fn find_item(item: &Item, ctxt: &mut EntryContext) {
78     match item.node {
79         ItemFn(..) => {
80             if item.ident.name == ctxt.main_name {
81                  ctxt.ast_map.with_path(item.id, |mut path| {
82                         if path.len() == 1 {
83                             // This is a top-level function so can be 'main'
84                             if ctxt.main_fn.is_none() {
85                                 ctxt.main_fn = Some((item.id, item.span));
86                             } else {
87                                 ctxt.session.span_err(
88                                     item.span,
89                                     "multiple 'main' functions");
90                             }
91                         } else {
92                             // This isn't main
93                             ctxt.non_main_fns.push((item.id, item.span));
94                         }
95                 });
96             }
97
98             if attr::contains_name(item.attrs.as_slice(), "main") {
99                 if ctxt.attr_main_fn.is_none() {
100                     ctxt.attr_main_fn = Some((item.id, item.span));
101                 } else {
102                     ctxt.session.span_err(
103                         item.span,
104                         "multiple 'main' functions");
105                 }
106             }
107
108             if attr::contains_name(item.attrs.as_slice(), "start") {
109                 if ctxt.start_fn.is_none() {
110                     ctxt.start_fn = Some((item.id, item.span));
111                 } else {
112                     ctxt.session.span_err(
113                         item.span,
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.set(this.start_fn);
127         this.session.entry_type.set(Some(session::EntryStart));
128     } else if this.attr_main_fn.is_some() {
129         this.session.entry_fn.set(this.attr_main_fn);
130         this.session.entry_type.set(Some(session::EntryMain));
131     } else if this.main_fn.is_some() {
132         this.session.entry_fn.set(this.main_fn);
133         this.session.entry_type.set(Some(session::EntryMain));
134     } else {
135         if !this.session.building_library.get() {
136             // No main function
137             this.session.err("main function not found");
138             if !this.non_main_fns.is_empty() {
139                 // There were some functions named 'main' though. Try to give the user a hint.
140                 this.session.note("the main function must be defined at the crate level \
141                                    but you have one or more functions named 'main' that are not \
142                                    defined at the crate level. Either move the definition or \
143                                    attach the `#[main]` attribute to override this behavior.");
144                 for &(_, span) in this.non_main_fns.iter() {
145                     this.session.span_note(span, "here is a function named 'main'");
146                 }
147             }
148             this.session.abort_if_errors();
149         }
150     }
151 }