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, NodeId, item, item_fn};
17 use syntax::codemap::span;
18 use syntax::oldvisit::{default_visitor, mk_vt, vt, Visitor, visit_crate};
19 use syntax::oldvisit::{visit_item};
20 use syntax::parse::token::special_idents;
26 ast_map: ast_map::map,
28 // The top-level function called 'main'
29 main_fn: Option<(NodeId, span)>,
31 // The function that has attribute named 'main'
32 attr_main_fn: Option<(NodeId, span)>,
34 // The function that has the attribute 'start' on it
35 start_fn: Option<(NodeId, span)>,
37 // The functions that one might think are 'main' but aren't, e.g.
38 // main functions not defined at the top level. For diagnostics.
39 non_main_fns: ~[(NodeId, span)],
42 type EntryVisitor = vt<@mut EntryContext>;
44 pub fn find_entry_point(session: Session, crate: &Crate, ast_map: ast_map::map) {
46 // FIXME #4404 android JNI hacks
47 if *session.building_library &&
48 session.targ_cfg.os != session::os_android {
49 // No need to find a main function
53 // If the user wants no main function at all, then stop here.
54 if attr::contains_name(crate.attrs, "no_main") {
55 *session.entry_type = Some(session::EntryNone);
59 let ctxt = @mut EntryContext {
68 visit_crate(crate, (ctxt, mk_vt(@Visitor {
69 visit_item: |item, (ctxt, visitor)| find_item(item, ctxt, visitor),
76 fn find_item(item: @item, ctxt: @mut EntryContext, visitor: EntryVisitor) {
79 if item.ident == special_idents::main {
80 match ctxt.ast_map.find(&item.id) {
81 Some(&ast_map::node_item(_, 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));
96 _ => util::unreachable()
100 if attr::contains_name(item.attrs, "main") {
101 if ctxt.attr_main_fn.is_none() {
102 ctxt.attr_main_fn = Some((item.id, item.span));
104 ctxt.session.span_err(
106 "multiple 'main' functions");
110 if attr::contains_name(item.attrs, "start") {
111 if ctxt.start_fn.is_none() {
112 ctxt.start_fn = Some((item.id, item.span));
114 ctxt.session.span_err(
116 "multiple 'start' functions");
123 visit_item(item, (ctxt, visitor));
126 fn configure_main(ctxt: @mut EntryContext) {
127 let this = &mut *ctxt;
128 if this.start_fn.is_some() {
129 *this.session.entry_fn = this.start_fn;
130 *this.session.entry_type = Some(session::EntryStart);
131 } else if this.attr_main_fn.is_some() {
132 *this.session.entry_fn = this.attr_main_fn;
133 *this.session.entry_type = Some(session::EntryMain);
134 } else if this.main_fn.is_some() {
135 *this.session.entry_fn = this.main_fn;
136 *this.session.entry_type = Some(session::EntryMain);
138 if !*this.session.building_library {
140 this.session.err("main function not found");
141 if !this.non_main_fns.is_empty() {
142 // There were some functions named 'main' though. Try to give the user a hint.
143 this.session.note("the main function must be defined at the crate level \
144 but you have one or more functions named 'main' that are not \
145 defined at the crate level. Either move the definition or \
146 attach the `#[main]` attribute to override this behavior.");
147 for &(_, span) in this.non_main_fns.iter() {
148 this.session.span_note(span, "here is a function named 'main'");
151 this.session.abort_if_errors();
153 // If we *are* building a library, then we're on android where we still might
154 // optionally want to translate main $4404
155 assert_eq!(this.session.targ_cfg.os, session::os_android);