2013-04-29 16:56:05 -05:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2015-06-09 18:40:45 -05:00
|
|
|
use ast_map;
|
2014-11-15 19:30:33 -06:00
|
|
|
use session::{config, Session};
|
2014-09-07 12:09:06 -05:00
|
|
|
use syntax::ast::{Name, NodeId, Item, ItemFn};
|
2013-07-19 06:51:37 -05:00
|
|
|
use syntax::attr;
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2014-02-13 23:07:09 -06:00
|
|
|
use syntax::parse::token;
|
2013-08-13 06:04:47 -05:00
|
|
|
use syntax::visit;
|
|
|
|
use syntax::visit::Visitor;
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
struct EntryContext<'a, 'ast: 'a> {
|
2014-03-05 08:36:01 -06:00
|
|
|
session: &'a Session,
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
ast_map: &'a ast_map::Map<'ast>,
|
2014-02-13 23:07:09 -06:00
|
|
|
|
|
|
|
// The interned Name for "main".
|
|
|
|
main_name: Name,
|
2013-04-29 18:16:58 -05:00
|
|
|
|
|
|
|
// The top-level function called 'main'
|
2013-08-31 11:13:04 -05:00
|
|
|
main_fn: Option<(NodeId, Span)>,
|
2013-04-29 18:16:58 -05:00
|
|
|
|
2013-04-29 16:56:05 -05:00
|
|
|
// The function that has attribute named 'main'
|
2013-08-31 11:13:04 -05:00
|
|
|
attr_main_fn: Option<(NodeId, Span)>,
|
2013-04-29 16:56:05 -05:00
|
|
|
|
|
|
|
// The function that has the attribute 'start' on it
|
2013-08-31 11:13:04 -05:00
|
|
|
start_fn: Option<(NodeId, Span)>,
|
2013-04-29 18:16:58 -05:00
|
|
|
|
|
|
|
// The functions that one might think are 'main' but aren't, e.g.
|
|
|
|
// main functions not defined at the top level. For diagnostics.
|
2014-03-04 12:02:49 -06:00
|
|
|
non_main_fns: Vec<(NodeId, Span)> ,
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
impl<'a, 'ast, 'v> Visitor<'v> for EntryContext<'a, 'ast> {
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_item(&mut self, item: &Item) {
|
2013-09-10 04:24:04 -05:00
|
|
|
find_item(item, self);
|
2013-08-13 06:04:47 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
pub fn find_entry_point(session: &Session, ast_map: &ast_map::Map) {
|
2014-05-02 17:26:45 -05:00
|
|
|
let any_exe = session.crate_types.borrow().iter().any(|ty| {
|
2014-05-06 06:38:01 -05:00
|
|
|
*ty == config::CrateTypeExecutable
|
2014-05-02 17:26:45 -05:00
|
|
|
});
|
2015-02-11 11:23:06 -06:00
|
|
|
if !any_exe {
|
2013-04-29 18:16:58 -05:00
|
|
|
// No need to find a main function
|
2014-05-02 17:26:45 -05:00
|
|
|
return
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2013-08-03 21:59:46 -05:00
|
|
|
// If the user wants no main function at all, then stop here.
|
2015-02-01 20:53:25 -06:00
|
|
|
if attr::contains_name(&ast_map.krate().attrs, "no_main") {
|
2014-05-06 06:38:01 -05:00
|
|
|
session.entry_type.set(Some(config::EntryNone));
|
2013-08-03 21:59:46 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-10 04:24:04 -05:00
|
|
|
let mut ctxt = EntryContext {
|
2013-04-29 16:56:05 -05:00
|
|
|
session: session,
|
2014-02-13 23:07:09 -06:00
|
|
|
main_name: token::intern("main"),
|
2013-04-29 18:16:58 -05:00
|
|
|
ast_map: ast_map,
|
|
|
|
main_fn: None,
|
2013-04-29 16:56:05 -05:00
|
|
|
attr_main_fn: None,
|
|
|
|
start_fn: None,
|
2014-03-04 12:02:49 -06:00
|
|
|
non_main_fns: Vec::new(),
|
2013-04-29 16:56:05 -05:00
|
|
|
};
|
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
visit::walk_crate(&mut ctxt, ast_map.krate());
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2013-09-10 04:24:04 -05:00
|
|
|
configure_main(&mut ctxt);
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn find_item(item: &Item, ctxt: &mut EntryContext) {
|
2013-04-29 16:56:05 -05:00
|
|
|
match item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ItemFn(..) => {
|
2014-02-13 23:07:09 -06:00
|
|
|
if item.ident.name == ctxt.main_name {
|
2014-11-06 11:32:37 -06:00
|
|
|
ctxt.ast_map.with_path(item.id, |path| {
|
2014-06-06 01:18:51 -05:00
|
|
|
if path.count() == 1 {
|
2014-02-13 23:07:09 -06:00
|
|
|
// This is a top-level function so can be 'main'
|
|
|
|
if ctxt.main_fn.is_none() {
|
|
|
|
ctxt.main_fn = Some((item.id, item.span));
|
2013-04-29 18:16:58 -05:00
|
|
|
} else {
|
2014-07-17 12:56:37 -05:00
|
|
|
span_err!(ctxt.session, item.span, E0136,
|
|
|
|
"multiple 'main' functions");
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
} else {
|
|
|
|
// This isn't main
|
|
|
|
ctxt.non_main_fns.push((item.id, item.span));
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
});
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2015-02-01 20:53:25 -06:00
|
|
|
if attr::contains_name(&item.attrs, "main") {
|
2013-04-29 18:16:58 -05:00
|
|
|
if ctxt.attr_main_fn.is_none() {
|
|
|
|
ctxt.attr_main_fn = Some((item.id, item.span));
|
|
|
|
} else {
|
2014-07-17 12:56:37 -05:00
|
|
|
span_err!(ctxt.session, item.span, E0137,
|
|
|
|
"multiple functions with a #[main] attribute");
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2015-02-01 20:53:25 -06:00
|
|
|
if attr::contains_name(&item.attrs, "start") {
|
2013-04-29 18:16:58 -05:00
|
|
|
if ctxt.start_fn.is_none() {
|
|
|
|
ctxt.start_fn = Some((item.id, item.span));
|
|
|
|
} else {
|
2014-07-17 12:56:37 -05:00
|
|
|
span_err!(ctxt.session, item.span, E0138,
|
|
|
|
"multiple 'start' functions");
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_item(ctxt, item);
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
|
2013-09-10 04:24:04 -05:00
|
|
|
fn configure_main(this: &mut EntryContext) {
|
2013-04-29 18:16:58 -05:00
|
|
|
if this.start_fn.is_some() {
|
2014-04-02 08:55:33 -05:00
|
|
|
*this.session.entry_fn.borrow_mut() = this.start_fn;
|
2014-05-06 06:38:01 -05:00
|
|
|
this.session.entry_type.set(Some(config::EntryStart));
|
2013-04-29 18:16:58 -05:00
|
|
|
} else if this.attr_main_fn.is_some() {
|
2014-04-02 08:55:33 -05:00
|
|
|
*this.session.entry_fn.borrow_mut() = this.attr_main_fn;
|
2014-05-06 06:38:01 -05:00
|
|
|
this.session.entry_type.set(Some(config::EntryMain));
|
2013-04-29 18:16:58 -05:00
|
|
|
} else if this.main_fn.is_some() {
|
2014-04-02 08:55:33 -05:00
|
|
|
*this.session.entry_fn.borrow_mut() = this.main_fn;
|
2014-05-06 06:38:01 -05:00
|
|
|
this.session.entry_type.set(Some(config::EntryMain));
|
2013-04-29 18:16:58 -05:00
|
|
|
} else {
|
2014-05-02 17:26:45 -05:00
|
|
|
// No main function
|
|
|
|
this.session.err("main function not found");
|
|
|
|
if !this.non_main_fns.is_empty() {
|
|
|
|
// There were some functions named 'main' though. Try to give the user a hint.
|
|
|
|
this.session.note("the main function must be defined at the crate level \
|
|
|
|
but you have one or more functions named 'main' that are not \
|
|
|
|
defined at the crate level. Either move the definition or \
|
|
|
|
attach the `#[main]` attribute to override this behavior.");
|
2015-01-31 11:20:46 -06:00
|
|
|
for &(_, span) in &this.non_main_fns {
|
2014-05-02 17:26:45 -05:00
|
|
|
this.session.span_note(span, "here is a function named 'main'");
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2013-05-06 20:48:24 -05:00
|
|
|
this.session.abort_if_errors();
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
}
|