]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/entry.rs
Rollup merge of #31007 - pra85:license, r=aturon
[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 middle::def_id::{CRATE_DEF_INDEX};
14 use session::{config, Session};
15 use syntax::ast::NodeId;
16 use syntax::attr;
17 use syntax::codemap::Span;
18 use syntax::entry::EntryPointType;
19 use rustc_front::hir::{Item, ItemFn};
20 use rustc_front::intravisit::Visitor;
21
22 struct EntryContext<'a, 'tcx: 'a> {
23     session: &'a Session,
24
25     map: &'a ast_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> Visitor<'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
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         map: ast_map,
68         main_fn: None,
69         attr_main_fn: None,
70         start_fn: None,
71         non_main_fns: Vec::new(),
72     };
73
74     ast_map.krate().visit_all_items(&mut ctxt);
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, at_root: bool) -> 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 at_root {
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, at_root: bool) {
105     match entry_point_type(item, at_root) {
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
137 fn configure_main(this: &mut EntryContext) {
138     if this.start_fn.is_some() {
139         *this.session.entry_fn.borrow_mut() = this.start_fn;
140         this.session.entry_type.set(Some(config::EntryStart));
141     } else if this.attr_main_fn.is_some() {
142         *this.session.entry_fn.borrow_mut() = this.attr_main_fn;
143         this.session.entry_type.set(Some(config::EntryMain));
144     } else if this.main_fn.is_some() {
145         *this.session.entry_fn.borrow_mut() = this.main_fn;
146         this.session.entry_type.set(Some(config::EntryMain));
147     } else {
148         // No main function
149         let mut err = this.session.struct_err("main function not found");
150         if !this.non_main_fns.is_empty() {
151             // There were some functions named 'main' though. Try to give the user a hint.
152             err.note("the main function must be defined at the crate level \
153                       but you have one or more functions named 'main' that are not \
154                       defined at the crate level. Either move the definition or \
155                       attach the `#[main]` attribute to override this behavior.");
156             for &(_, span) in &this.non_main_fns {
157                 err.span_note(span, "here is a function named 'main'");
158             }
159             err.emit();
160             this.session.abort_if_errors();
161         } else {
162             err.emit();
163         }
164     }
165 }