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;
|
2013-07-27 03:25:59 -05:00
|
|
|
use syntax::ast::{Crate, NodeId, item, item_fn};
|
2013-07-19 20:42:11 -05:00
|
|
|
use syntax::ast_map;
|
2013-07-19 06:51:37 -05:00
|
|
|
use syntax::attr;
|
2013-04-29 16:56:05 -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-06-28 17:32:26 -05:00
|
|
|
use std::util;
|
2013-04-29 16:56:05 -05:00
|
|
|
|
|
|
|
struct EntryContext {
|
|
|
|
session: Session,
|
|
|
|
|
2013-04-29 18:16:58 -05:00
|
|
|
ast_map: ast_map::map,
|
|
|
|
|
|
|
|
// The top-level function called 'main'
|
2013-07-27 03:25:59 -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-07-27 03:25:59 -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-07-27 03:25:59 -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-07-27 03:25:59 -05:00
|
|
|
non_main_fns: ~[(NodeId, span)],
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
|
2013-08-13 06:04:47 -05:00
|
|
|
struct EntryVisitor;
|
|
|
|
|
|
|
|
impl Visitor<@mut EntryContext> for EntryVisitor {
|
|
|
|
fn visit_item(&mut self, item:@item, ctxt:@mut EntryContext) {
|
|
|
|
find_item(item, ctxt, self);
|
|
|
|
}
|
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2013-07-19 00:38:55 -05:00
|
|
|
pub fn find_entry_point(session: Session, crate: &Crate, ast_map: ast_map::map) {
|
2013-04-29 18:16:58 -05:00
|
|
|
|
|
|
|
// FIXME #4404 android JNI hacks
|
2013-05-06 20:48:24 -05:00
|
|
|
if *session.building_library &&
|
|
|
|
session.targ_cfg.os != session::os_android {
|
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") {
|
|
|
|
*session.entry_type = Some(session::EntryNone);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-04-29 16:56:05 -05:00
|
|
|
let ctxt = @mut EntryContext {
|
|
|
|
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-08-13 06:04:47 -05:00
|
|
|
let mut v = EntryVisitor;
|
|
|
|
|
|
|
|
visit::walk_crate(&mut v, crate, ctxt);
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2013-04-29 18:16:58 -05:00
|
|
|
configure_main(ctxt);
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
|
2013-08-13 06:04:47 -05:00
|
|
|
fn find_item(item: @item, ctxt: @mut EntryContext, visitor: &mut EntryVisitor) {
|
2013-04-29 16:56:05 -05:00
|
|
|
match item.node {
|
|
|
|
item_fn(*) => {
|
2013-04-29 18:16:58 -05:00
|
|
|
if item.ident == special_idents::main {
|
|
|
|
match ctxt.ast_map.find(&item.id) {
|
|
|
|
Some(&ast_map::node_item(_, path)) => {
|
|
|
|
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,
|
2013-05-19 00:07:44 -05:00
|
|
|
"multiple 'main' functions");
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This isn't main
|
|
|
|
ctxt.non_main_fns.push((item.id, item.span));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => util::unreachable()
|
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-08-13 06:04:47 -05:00
|
|
|
visit::walk_item(visitor, item, ctxt);
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
|
2013-04-29 18:16:58 -05:00
|
|
|
fn configure_main(ctxt: @mut EntryContext) {
|
2013-04-29 16:56:05 -05:00
|
|
|
let this = &mut *ctxt;
|
2013-04-29 18:16:58 -05:00
|
|
|
if this.start_fn.is_some() {
|
2013-04-29 16:56:05 -05:00
|
|
|
*this.session.entry_fn = this.start_fn;
|
|
|
|
*this.session.entry_type = Some(session::EntryStart);
|
2013-04-29 18:16:58 -05:00
|
|
|
} else if this.attr_main_fn.is_some() {
|
2013-04-29 16:56:05 -05:00
|
|
|
*this.session.entry_fn = this.attr_main_fn;
|
|
|
|
*this.session.entry_type = Some(session::EntryMain);
|
2013-04-29 18:16:58 -05:00
|
|
|
} else if this.main_fn.is_some() {
|
|
|
|
*this.session.entry_fn = this.main_fn;
|
|
|
|
*this.session.entry_type = Some(session::EntryMain);
|
|
|
|
} else {
|
2013-05-06 20:48:24 -05:00
|
|
|
if !*this.session.building_library {
|
|
|
|
// 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();
|
|
|
|
} else {
|
|
|
|
// If we *are* building a library, then we're on android where we still might
|
|
|
|
// optionally want to translate main $4404
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(this.session.targ_cfg.os, session::os_android);
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
}
|