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
|
|
|
|
2013-04-29 16:56:05 -05:00
|
|
|
use driver::session;
|
|
|
|
use driver::session::Session;
|
2014-01-09 07:05:33 -06:00
|
|
|
use syntax::ast::{Crate, NodeId, Item, ItemFn};
|
2013-07-19 20:42:11 -05:00
|
|
|
use syntax::ast_map;
|
2013-07-19 06:51:37 -05:00
|
|
|
use syntax::attr;
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2013-07-19 20:42:11 -05:00
|
|
|
use syntax::parse::token::special_idents;
|
2013-08-13 06:04:47 -05:00
|
|
|
use syntax::visit;
|
|
|
|
use syntax::visit::Visitor;
|
2013-04-29 16:56:05 -05:00
|
|
|
|
|
|
|
struct EntryContext {
|
|
|
|
session: Session,
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
ast_map: ast_map::Map,
|
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.
|
2013-08-31 11:13:04 -05:00
|
|
|
non_main_fns: ~[(NodeId, Span)],
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
|
2013-09-10 04:24:04 -05:00
|
|
|
impl Visitor<()> for EntryContext {
|
2014-01-09 07:05:33 -06: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-01-09 07:05:33 -06:00
|
|
|
pub fn find_entry_point(session: Session, crate: &Crate, ast_map: ast_map::Map) {
|
2013-12-22 16:40:03 -06:00
|
|
|
if session.building_library.get() {
|
2013-04-29 18:16:58 -05:00
|
|
|
// No need to find a main function
|
|
|
|
return;
|
|
|
|
}
|
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.
|
|
|
|
if attr::contains_name(crate.attrs, "no_main") {
|
2013-12-21 19:25:27 -06:00
|
|
|
session.entry_type.set(Some(session::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,
|
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,
|
2013-04-29 18:16:58 -05:00
|
|
|
non_main_fns: ~[],
|
2013-04-29 16:56:05 -05:00
|
|
|
};
|
|
|
|
|
2013-09-10 04:24:04 -05:00
|
|
|
visit::walk_crate(&mut ctxt, crate, ());
|
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(..) => {
|
2013-07-10 15:44:58 -05:00
|
|
|
if item.ident.name == special_idents::main.name {
|
2013-12-27 18:09:29 -06:00
|
|
|
{
|
|
|
|
let ast_map = ctxt.ast_map.borrow();
|
|
|
|
match ast_map.get().find(&item.id) {
|
2014-01-09 07:05:33 -06:00
|
|
|
Some(&ast_map::NodeItem(_, path)) => {
|
2013-12-27 18:09:29 -06:00
|
|
|
if path.len() == 0 {
|
|
|
|
// This is a top-level function so can be 'main'
|
|
|
|
if ctxt.main_fn.is_none() {
|
|
|
|
ctxt.main_fn = Some((item.id, item.span));
|
|
|
|
} else {
|
|
|
|
ctxt.session.span_err(
|
|
|
|
item.span,
|
|
|
|
"multiple 'main' functions");
|
|
|
|
}
|
2013-04-29 18:16:58 -05:00
|
|
|
} else {
|
2013-12-27 18:09:29 -06:00
|
|
|
// This isn't main
|
|
|
|
ctxt.non_main_fns.push((item.id, item.span));
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-27 18:09:29 -06:00
|
|
|
_ => unreachable!()
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2013-07-19 06:51:37 -05: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 {
|
|
|
|
ctxt.session.span_err(
|
|
|
|
item.span,
|
2013-05-19 00:07:44 -05:00
|
|
|
"multiple 'main' functions");
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2013-07-19 06:51:37 -05: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 {
|
|
|
|
ctxt.session.span_err(
|
|
|
|
item.span,
|
2013-05-19 00:07:44 -05:00
|
|
|
"multiple 'start' functions");
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
2013-09-10 04:24:04 -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() {
|
2013-12-21 19:25:27 -06:00
|
|
|
this.session.entry_fn.set(this.start_fn);
|
|
|
|
this.session.entry_type.set(Some(session::EntryStart));
|
2013-04-29 18:16:58 -05:00
|
|
|
} else if this.attr_main_fn.is_some() {
|
2013-12-21 19:25:27 -06:00
|
|
|
this.session.entry_fn.set(this.attr_main_fn);
|
|
|
|
this.session.entry_type.set(Some(session::EntryMain));
|
2013-04-29 18:16:58 -05:00
|
|
|
} else if this.main_fn.is_some() {
|
2013-12-21 19:25:27 -06:00
|
|
|
this.session.entry_fn.set(this.main_fn);
|
|
|
|
this.session.entry_type.set(Some(session::EntryMain));
|
2013-04-29 18:16:58 -05:00
|
|
|
} else {
|
2013-12-22 16:40:03 -06:00
|
|
|
if !this.session.building_library.get() {
|
2013-05-06 20:48:24 -05:00
|
|
|
// No main function
|
2013-05-19 00:07:44 -05:00
|
|
|
this.session.err("main function not found");
|
2013-05-06 20:48:24 -05:00
|
|
|
if !this.non_main_fns.is_empty() {
|
|
|
|
// There were some functions named 'main' though. Try to give the user a hint.
|
2013-05-19 00:07:44 -05:00
|
|
|
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.");
|
2013-08-03 11:45:23 -05:00
|
|
|
for &(_, span) in this.non_main_fns.iter() {
|
2013-05-19 00:07:44 -05:00
|
|
|
this.session.span_note(span, "here is a function named 'main'");
|
2013-05-06 20:48:24 -05:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|