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