/src/librustc/middle/entry.rs

https://gitlab.com/alx741/rust · Rust · 168 lines · 128 code · 19 blank · 21 comment · 31 complexity · 393a4091bbbd21900aabfb7cdca1fd12 MD5 · raw file

  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. use dep_graph::DepNode;
  11. use hir::map as ast_map;
  12. use hir::def_id::{CRATE_DEF_INDEX};
  13. use session::{config, Session};
  14. use syntax::ast::NodeId;
  15. use syntax::attr;
  16. use syntax::codemap::Span;
  17. use syntax::entry::EntryPointType;
  18. use hir::{Item, ItemFn};
  19. use hir::intravisit::Visitor;
  20. struct EntryContext<'a, 'tcx: 'a> {
  21. session: &'a Session,
  22. map: &'a ast_map::Map<'tcx>,
  23. // The top-level function called 'main'
  24. main_fn: Option<(NodeId, Span)>,
  25. // The function that has attribute named 'main'
  26. attr_main_fn: Option<(NodeId, Span)>,
  27. // The function that has the attribute 'start' on it
  28. start_fn: Option<(NodeId, Span)>,
  29. // The functions that one might think are 'main' but aren't, e.g.
  30. // main functions not defined at the top level. For diagnostics.
  31. non_main_fns: Vec<(NodeId, Span)> ,
  32. }
  33. impl<'a, 'tcx> Visitor<'tcx> for EntryContext<'a, 'tcx> {
  34. fn visit_item(&mut self, item: &'tcx Item) {
  35. let def_id = self.map.local_def_id(item.id);
  36. let def_key = self.map.def_key(def_id);
  37. let at_root = def_key.parent == Some(CRATE_DEF_INDEX);
  38. find_item(item, self, at_root);
  39. }
  40. }
  41. pub fn find_entry_point(session: &Session, ast_map: &ast_map::Map) {
  42. let _task = ast_map.dep_graph.in_task(DepNode::EntryPoint);
  43. let any_exe = session.crate_types.borrow().iter().any(|ty| {
  44. *ty == config::CrateTypeExecutable
  45. });
  46. if !any_exe {
  47. // No need to find a main function
  48. return
  49. }
  50. // If the user wants no main function at all, then stop here.
  51. if attr::contains_name(&ast_map.krate().attrs, "no_main") {
  52. session.entry_type.set(Some(config::EntryNone));
  53. return
  54. }
  55. let mut ctxt = EntryContext {
  56. session: session,
  57. map: ast_map,
  58. main_fn: None,
  59. attr_main_fn: None,
  60. start_fn: None,
  61. non_main_fns: Vec::new(),
  62. };
  63. ast_map.krate().visit_all_items(&mut ctxt);
  64. configure_main(&mut ctxt);
  65. }
  66. // Beware, this is duplicated in libsyntax/entry.rs, make sure to keep
  67. // them in sync.
  68. fn entry_point_type(item: &Item, at_root: bool) -> EntryPointType {
  69. match item.node {
  70. ItemFn(..) => {
  71. if attr::contains_name(&item.attrs, "start") {
  72. EntryPointType::Start
  73. } else if attr::contains_name(&item.attrs, "main") {
  74. EntryPointType::MainAttr
  75. } else if item.name.as_str() == "main" {
  76. if at_root {
  77. // This is a top-level function so can be 'main'
  78. EntryPointType::MainNamed
  79. } else {
  80. EntryPointType::OtherMain
  81. }
  82. } else {
  83. EntryPointType::None
  84. }
  85. }
  86. _ => EntryPointType::None,
  87. }
  88. }
  89. fn find_item(item: &Item, ctxt: &mut EntryContext, at_root: bool) {
  90. match entry_point_type(item, at_root) {
  91. EntryPointType::MainNamed => {
  92. if ctxt.main_fn.is_none() {
  93. ctxt.main_fn = Some((item.id, item.span));
  94. } else {
  95. span_err!(ctxt.session, item.span, E0136,
  96. "multiple 'main' functions");
  97. }
  98. },
  99. EntryPointType::OtherMain => {
  100. ctxt.non_main_fns.push((item.id, item.span));
  101. },
  102. EntryPointType::MainAttr => {
  103. if ctxt.attr_main_fn.is_none() {
  104. ctxt.attr_main_fn = Some((item.id, item.span));
  105. } else {
  106. span_err!(ctxt.session, item.span, E0137,
  107. "multiple functions with a #[main] attribute");
  108. }
  109. },
  110. EntryPointType::Start => {
  111. if ctxt.start_fn.is_none() {
  112. ctxt.start_fn = Some((item.id, item.span));
  113. } else {
  114. span_err!(ctxt.session, item.span, E0138,
  115. "multiple 'start' functions");
  116. }
  117. },
  118. EntryPointType::None => ()
  119. }
  120. }
  121. fn configure_main(this: &mut EntryContext) {
  122. if this.start_fn.is_some() {
  123. *this.session.entry_fn.borrow_mut() = this.start_fn;
  124. this.session.entry_type.set(Some(config::EntryStart));
  125. } else if this.attr_main_fn.is_some() {
  126. *this.session.entry_fn.borrow_mut() = this.attr_main_fn;
  127. this.session.entry_type.set(Some(config::EntryMain));
  128. } else if this.main_fn.is_some() {
  129. *this.session.entry_fn.borrow_mut() = this.main_fn;
  130. this.session.entry_type.set(Some(config::EntryMain));
  131. } else {
  132. // No main function
  133. let mut err = this.session.struct_err("main function not found");
  134. if !this.non_main_fns.is_empty() {
  135. // There were some functions named 'main' though. Try to give the user a hint.
  136. err.note("the main function must be defined at the crate level \
  137. but you have one or more functions named 'main' that are not \
  138. defined at the crate level. Either move the definition or \
  139. attach the `#[main]` attribute to override this behavior.");
  140. for &(_, span) in &this.non_main_fns {
  141. err.span_note(span, "here is a function named 'main'");
  142. }
  143. err.emit();
  144. this.session.abort_if_errors();
  145. } else {
  146. err.emit();
  147. }
  148. }
  149. }