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::{Crate, Name, NodeId, Item, ItemFn};
17 use syntax::codemap::Span;
18 use syntax::parse::token;
20 use syntax::visit::Visitor;
22 struct EntryContext<'a> {
25 ast_map: &'a ast_map::Map,
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> Visitor<()> for EntryContext<'a> {
45 fn visit_item(&mut self, item: &Item, _:()) {
46 find_item(item, self);
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
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));
62 let mut ctxt = EntryContext {
64 main_name: token::intern("main"),
69 non_main_fns: Vec::new(),
72 visit::walk_crate(&mut ctxt, krate, ());
74 configure_main(&mut ctxt);
77 fn find_item(item: &Item, ctxt: &mut EntryContext) {
80 if item.ident.name == ctxt.main_name {
81 ctxt.ast_map.with_path(item.id, |mut path| {
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));
87 ctxt.session.span_err(
89 "multiple 'main' functions");
93 ctxt.non_main_fns.push((item.id, item.span));
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));
102 ctxt.session.span_err(
104 "multiple 'main' functions");
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));
112 ctxt.session.span_err(
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.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));
135 if !this.session.building_library.get() {
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'");
148 this.session.abort_if_errors();