]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/entry.rs
auto merge of #8279 : pcwalton/rust/no-main, r=brson
[rust.git] / src / librustc / middle / entry.rs
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.
4 //
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.
10
11
12 use driver::session;
13 use driver::session::Session;
14 use syntax::ast::{Crate, NodeId, item, item_fn};
15 use syntax::ast_map;
16 use syntax::attr;
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;
21 use std::util;
22
23 struct EntryContext {
24     session: Session,
25
26     ast_map: ast_map::map,
27
28     // The top-level function called 'main'
29     main_fn: Option<(NodeId, span)>,
30
31     // The function that has attribute named 'main'
32     attr_main_fn: Option<(NodeId, span)>,
33
34     // The function that has the attribute 'start' on it
35     start_fn: Option<(NodeId, span)>,
36
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)],
40 }
41
42 type EntryVisitor = vt<@mut EntryContext>;
43
44 pub fn find_entry_point(session: Session, crate: &Crate, ast_map: ast_map::map) {
45
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
50         return;
51     }
52
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);
56         return
57     }
58
59     let ctxt = @mut EntryContext {
60         session: session,
61         ast_map: ast_map,
62         main_fn: None,
63         attr_main_fn: None,
64         start_fn: None,
65         non_main_fns: ~[],
66     };
67
68     visit_crate(crate, (ctxt, mk_vt(@Visitor {
69         visit_item: |item, (ctxt, visitor)| find_item(item, ctxt, visitor),
70         .. *default_visitor()
71     })));
72
73     configure_main(ctxt);
74 }
75
76 fn find_item(item: @item, ctxt: @mut EntryContext, visitor: EntryVisitor) {
77     match item.node {
78         item_fn(*) => {
79             if item.ident == special_idents::main {
80                 match ctxt.ast_map.find(&item.id) {
81                     Some(&ast_map::node_item(_, path)) => {
82                         if path.len() == 0 {
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));
86                             } else {
87                                 ctxt.session.span_err(
88                                     item.span,
89                                     "multiple 'main' functions");
90                             }
91                         } else {
92                             // This isn't main
93                             ctxt.non_main_fns.push((item.id, item.span));
94                         }
95                     }
96                     _ => util::unreachable()
97                 }
98             }
99
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));
103                 } else {
104                     ctxt.session.span_err(
105                         item.span,
106                         "multiple 'main' functions");
107                 }
108             }
109
110             if attr::contains_name(item.attrs, "start") {
111                 if ctxt.start_fn.is_none() {
112                     ctxt.start_fn = Some((item.id, item.span));
113                 } else {
114                     ctxt.session.span_err(
115                         item.span,
116                         "multiple 'start' functions");
117                 }
118             }
119         }
120         _ => ()
121     }
122
123     visit_item(item, (ctxt, visitor));
124 }
125
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);
137     } else {
138         if !*this.session.building_library {
139             // No main function
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'");
149                 }
150             }
151             this.session.abort_if_errors();
152         } else {
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);
156         }
157     }
158 }