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-07-31 02:04:06 -05:00
|
|
|
use front::map as ast_map;
|
2015-11-17 17:54:21 -06:00
|
|
|
use middle::def_id::{CRATE_DEF_INDEX};
|
2014-11-15 19:30:33 -06:00
|
|
|
use session::{config, Session};
|
2015-07-31 02:04:06 -05:00
|
|
|
use syntax::ast::NodeId;
|
2015-09-14 04:58:20 -05:00
|
|
|
use syntax::attr;
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2015-08-23 13:12:39 -05:00
|
|
|
use syntax::entry::EntryPointType;
|
2015-09-14 04:58:20 -05:00
|
|
|
use rustc_front::hir::{Item, ItemFn};
|
2015-11-17 17:54:21 -06:00
|
|
|
use rustc_front::intravisit::Visitor;
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2015-11-17 17:54:21 -06:00
|
|
|
struct EntryContext<'a, 'tcx: 'a> {
|
2014-03-05 08:36:01 -06:00
|
|
|
session: &'a Session,
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2015-11-17 17:54:21 -06:00
|
|
|
map: &'a ast_map::Map<'tcx>,
|
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
|
|
|
}
|
|
|
|
|
2015-11-17 17:54:21 -06:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for EntryContext<'a, 'tcx> {
|
|
|
|
fn visit_item(&mut self, item: &'tcx Item) {
|
|
|
|
let def_id = self.map.local_def_id(item.id);
|
|
|
|
let def_key = self.map.def_key(def_id);
|
|
|
|
let at_root = def_key.parent == Some(CRATE_DEF_INDEX);
|
|
|
|
find_item(item, self, at_root);
|
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,
|
2015-11-17 17:54:21 -06:00
|
|
|
map: ast_map,
|
2013-04-29 18:16:58 -05:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-11-17 17:54:21 -06:00
|
|
|
ast_map.krate().visit_all_items(&mut ctxt);
|
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
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
// Beware, this is duplicated in libsyntax/entry.rs, make sure to keep
|
|
|
|
// them in sync.
|
2015-11-17 17:54:21 -06:00
|
|
|
fn entry_point_type(item: &Item, at_root: bool) -> EntryPointType {
|
2015-07-31 02:04:06 -05:00
|
|
|
match item.node {
|
|
|
|
ItemFn(..) => {
|
|
|
|
if attr::contains_name(&item.attrs, "start") {
|
|
|
|
EntryPointType::Start
|
|
|
|
} else if attr::contains_name(&item.attrs, "main") {
|
|
|
|
EntryPointType::MainAttr
|
2015-09-24 15:05:02 -05:00
|
|
|
} else if item.name.as_str() == "main" {
|
2015-11-17 17:54:21 -06:00
|
|
|
if at_root {
|
2015-07-31 02:04:06 -05:00
|
|
|
// This is a top-level function so can be 'main'
|
|
|
|
EntryPointType::MainNamed
|
|
|
|
} else {
|
|
|
|
EntryPointType::OtherMain
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EntryPointType::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => EntryPointType::None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-17 17:54:21 -06:00
|
|
|
fn find_item(item: &Item, ctxt: &mut EntryContext, at_root: bool) {
|
|
|
|
match entry_point_type(item, at_root) {
|
2015-08-23 13:12:39 -05:00
|
|
|
EntryPointType::MainNamed => {
|
|
|
|
if ctxt.main_fn.is_none() {
|
|
|
|
ctxt.main_fn = Some((item.id, item.span));
|
|
|
|
} else {
|
|
|
|
span_err!(ctxt.session, item.span, E0136,
|
|
|
|
"multiple 'main' functions");
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2015-08-23 13:12:39 -05:00
|
|
|
},
|
|
|
|
EntryPointType::OtherMain => {
|
|
|
|
ctxt.non_main_fns.push((item.id, item.span));
|
|
|
|
},
|
|
|
|
EntryPointType::MainAttr => {
|
|
|
|
if ctxt.attr_main_fn.is_none() {
|
|
|
|
ctxt.attr_main_fn = Some((item.id, item.span));
|
|
|
|
} else {
|
|
|
|
span_err!(ctxt.session, item.span, E0137,
|
|
|
|
"multiple functions with a #[main] attribute");
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2015-08-23 13:12:39 -05:00
|
|
|
},
|
|
|
|
EntryPointType::Start => {
|
|
|
|
if ctxt.start_fn.is_none() {
|
|
|
|
ctxt.start_fn = Some((item.id, item.span));
|
|
|
|
} else {
|
|
|
|
span_err!(ctxt.session, item.span, E0138,
|
|
|
|
"multiple 'start' functions");
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
2015-08-23 13:12:39 -05:00
|
|
|
},
|
|
|
|
EntryPointType::None => ()
|
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
|
|
|
}
|
|
|
|
}
|