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.
13 use driver::session::Session;
14 use syntax::ast::{Name, NodeId, Item, ItemFn};
17 use syntax::codemap::Span;
18 use syntax::parse::token;
20 use syntax::visit::Visitor;
22 struct EntryContext<'a, 'ast: 'a> {
25 ast_map: &'a ast_map::Map<'ast>,
27 // The interned Name for "main".
30 // The top-level function called 'main'
31 main_fn: Option<(NodeId, Span)>,
33 // The function that has attribute named 'main'
34 attr_main_fn: Option<(NodeId, Span)>,
36 // The function that has the attribute 'start' on it
37 start_fn: Option<(NodeId, Span)>,
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)> ,
44 impl<'a, 'ast, 'v> Visitor<'v> for EntryContext<'a, 'ast> {
45 fn visit_item(&mut self, item: &Item) {
46 find_item(item, self);
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
55 // No need to find a main function
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));
65 let mut ctxt = EntryContext {
67 main_name: token::intern("main"),
72 non_main_fns: Vec::new(),
75 visit::walk_crate(&mut ctxt, ast_map.krate());
77 configure_main(&mut ctxt);
80 fn find_item(item: &Item, ctxt: &mut EntryContext) {
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));
90 span_err!(ctxt.session, item.span, E0136,
91 "multiple 'main' functions");
95 ctxt.non_main_fns.push((item.id, item.span));
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));
104 span_err!(ctxt.session, item.span, E0137,
105 "multiple functions with a #[main] attribute");
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));
113 span_err!(ctxt.session, item.span, E0138,
114 "multiple 'start' functions");
121 visit::walk_item(ctxt, item);
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));
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'");
146 this.session.abort_if_errors();