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.
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.
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;
17 use syntax::entry::EntryPointType;
19 use hir::{Item, ItemFn, ImplItem, TraitItem};
20 use hir::itemlikevisit::ItemLikeVisitor;
22 struct EntryContext<'a, 'tcx: 'a> {
25 map: &'a hir_map::Map<'tcx>,
27 // The top-level function called 'main'
28 main_fn: Option<(NodeId, Span)>,
30 // The function that has attribute named 'main'
31 attr_main_fn: Option<(NodeId, Span)>,
33 // The function that has the attribute 'start' on it
34 start_fn: Option<(NodeId, Span)>,
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)> ,
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);
49 fn visit_trait_item(&mut self, _trait_item: &'tcx TraitItem) {
50 // entry fn is never a trait item
53 fn visit_impl_item(&mut self, _impl_item: &'tcx ImplItem) {
54 // entry fn is never an impl item
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
63 // No need to find a main function
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));
73 let mut ctxt = EntryContext {
79 non_main_fns: Vec::new(),
82 hir_map.krate().visit_all_item_likes(&mut ctxt);
84 configure_main(&mut ctxt);
87 // Beware, this is duplicated in libsyntax/entry.rs, make sure to keep
89 fn entry_point_type(item: &Item, at_root: bool) -> EntryPointType {
92 if attr::contains_name(&item.attrs, "start") {
94 } else if attr::contains_name(&item.attrs, "main") {
95 EntryPointType::MainAttr
96 } else if item.name == "main" {
98 // This is a top-level function so can be 'main'
99 EntryPointType::MainNamed
101 EntryPointType::OtherMain
107 _ => EntryPointType::None,
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));
118 span_err!(ctxt.session, item.span, E0136,
119 "multiple 'main' functions");
122 EntryPointType::OtherMain => {
123 ctxt.non_main_fns.push((item.id, item.span));
125 EntryPointType::MainAttr => {
126 if ctxt.attr_main_fn.is_none() {
127 ctxt.attr_main_fn = Some((item.id, item.span));
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")
136 EntryPointType::Start => {
137 if ctxt.start_fn.is_none() {
138 ctxt.start_fn = Some((item.id, item.span));
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")
149 EntryPointType::None => ()
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));
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'");
176 this.session.abort_if_errors();