1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use session::{config, Session};
use syntax::ast::{Name, NodeId, Item, ItemFn};
use syntax::ast_map;
use syntax::attr;
use syntax::codemap::Span;
use syntax::parse::token;
use syntax::visit;
use syntax::visit::Visitor;
struct EntryContext<'a, 'ast: 'a> {
session: &'a Session,
ast_map: &'a ast_map::Map<'ast>,
main_name: Name,
main_fn: Option<(NodeId, Span)>,
attr_main_fn: Option<(NodeId, Span)>,
start_fn: Option<(NodeId, Span)>,
non_main_fns: Vec<(NodeId, Span)> ,
}
impl<'a, 'ast, 'v> Visitor<'v> for EntryContext<'a, 'ast> {
fn visit_item(&mut self, item: &Item) {
find_item(item, self);
}
}
pub fn find_entry_point(session: &Session, ast_map: &ast_map::Map) {
let any_exe = session.crate_types.borrow().iter().any(|ty| {
*ty == config::CrateTypeExecutable
});
if !any_exe {
return
}
if attr::contains_name(&ast_map.krate().attrs, "no_main") {
session.entry_type.set(Some(config::EntryNone));
return
}
let mut ctxt = EntryContext {
session: session,
main_name: token::intern("main"),
ast_map: ast_map,
main_fn: None,
attr_main_fn: None,
start_fn: None,
non_main_fns: Vec::new(),
};
visit::walk_crate(&mut ctxt, ast_map.krate());
configure_main(&mut ctxt);
}
fn find_item(item: &Item, ctxt: &mut EntryContext) {
match item.node {
ItemFn(..) => {
if item.ident.name == ctxt.main_name {
ctxt.ast_map.with_path(item.id, |path| {
if path.count() == 1 {
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");
}
} else {
ctxt.non_main_fns.push((item.id, item.span));
}
});
}
if attr::contains_name(&item.attrs, "main") {
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");
}
}
if attr::contains_name(&item.attrs, "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");
}
}
}
_ => ()
}
visit::walk_item(ctxt, item);
}
fn configure_main(this: &mut EntryContext) {
if this.start_fn.is_some() {
*this.session.entry_fn.borrow_mut() = this.start_fn;
this.session.entry_type.set(Some(config::EntryStart));
} else if this.attr_main_fn.is_some() {
*this.session.entry_fn.borrow_mut() = this.attr_main_fn;
this.session.entry_type.set(Some(config::EntryMain));
} else if this.main_fn.is_some() {
*this.session.entry_fn.borrow_mut() = this.main_fn;
this.session.entry_type.set(Some(config::EntryMain));
} else {
this.session.err("main function not found");
if !this.non_main_fns.is_empty() {
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.");
for &(_, span) in &this.non_main_fns {
this.session.span_note(span, "here is a function named 'main'");
}
this.session.abort_if_errors();
}
}
}