/src/librustc/middle/entry.rs

https://gitlab.com/pranith/rust · Rust · 148 lines · 107 code · 20 blank · 21 comment · 26 complexity · 479c51d949549a2ec4e9b50acd5d46a4 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 session::{config, Session};
  11. use syntax::ast::{Name, NodeId, Item, ItemFn};
  12. use syntax::ast_map;
  13. use syntax::attr;
  14. use syntax::codemap::Span;
  15. use syntax::parse::token;
  16. use syntax::visit;
  17. use syntax::visit::Visitor;
  18. struct EntryContext<'a, 'ast: 'a> {
  19. session: &'a Session,
  20. ast_map: &'a ast_map::Map<'ast>,
  21. // The interned Name for "main".
  22. main_name: Name,
  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, 'ast, 'v> Visitor<'v> for EntryContext<'a, 'ast> {
  34. fn visit_item(&mut self, item: &Item) {
  35. find_item(item, self);
  36. }
  37. }
  38. pub fn find_entry_point(session: &Session, ast_map: &ast_map::Map) {
  39. let any_exe = session.crate_types.borrow().iter().any(|ty| {
  40. *ty == config::CrateTypeExecutable
  41. });
  42. if !any_exe {
  43. // No need to find a main function
  44. return
  45. }
  46. // If the user wants no main function at all, then stop here.
  47. if attr::contains_name(&ast_map.krate().attrs, "no_main") {
  48. session.entry_type.set(Some(config::EntryNone));
  49. return
  50. }
  51. let mut ctxt = EntryContext {
  52. session: session,
  53. main_name: token::intern("main"),
  54. ast_map: ast_map,
  55. main_fn: None,
  56. attr_main_fn: None,
  57. start_fn: None,
  58. non_main_fns: Vec::new(),
  59. };
  60. visit::walk_crate(&mut ctxt, ast_map.krate());
  61. configure_main(&mut ctxt);
  62. }
  63. fn find_item(item: &Item, ctxt: &mut EntryContext) {
  64. match item.node {
  65. ItemFn(..) => {
  66. if item.ident.name == ctxt.main_name {
  67. ctxt.ast_map.with_path(item.id, |path| {
  68. if path.count() == 1 {
  69. // This is a top-level function so can be 'main'
  70. if ctxt.main_fn.is_none() {
  71. ctxt.main_fn = Some((item.id, item.span));
  72. } else {
  73. span_err!(ctxt.session, item.span, E0136,
  74. "multiple 'main' functions");
  75. }
  76. } else {
  77. // This isn't main
  78. ctxt.non_main_fns.push((item.id, item.span));
  79. }
  80. });
  81. }
  82. if attr::contains_name(&item.attrs, "main") {
  83. if ctxt.attr_main_fn.is_none() {
  84. ctxt.attr_main_fn = Some((item.id, item.span));
  85. } else {
  86. span_err!(ctxt.session, item.span, E0137,
  87. "multiple functions with a #[main] attribute");
  88. }
  89. }
  90. if attr::contains_name(&item.attrs, "start") {
  91. if ctxt.start_fn.is_none() {
  92. ctxt.start_fn = Some((item.id, item.span));
  93. } else {
  94. span_err!(ctxt.session, item.span, E0138,
  95. "multiple 'start' functions");
  96. }
  97. }
  98. }
  99. _ => ()
  100. }
  101. visit::walk_item(ctxt, item);
  102. }
  103. fn configure_main(this: &mut EntryContext) {
  104. if this.start_fn.is_some() {
  105. *this.session.entry_fn.borrow_mut() = this.start_fn;
  106. this.session.entry_type.set(Some(config::EntryStart));
  107. } else if this.attr_main_fn.is_some() {
  108. *this.session.entry_fn.borrow_mut() = this.attr_main_fn;
  109. this.session.entry_type.set(Some(config::EntryMain));
  110. } else if this.main_fn.is_some() {
  111. *this.session.entry_fn.borrow_mut() = this.main_fn;
  112. this.session.entry_type.set(Some(config::EntryMain));
  113. } else {
  114. // No main function
  115. this.session.err("main function not found");
  116. if !this.non_main_fns.is_empty() {
  117. // There were some functions named 'main' though. Try to give the user a hint.
  118. this.session.note("the main function must be defined at the crate level \
  119. but you have one or more functions named 'main' that are not \
  120. defined at the crate level. Either move the definition or \
  121. attach the `#[main]` attribute to override this behavior.");
  122. for &(_, span) in &this.non_main_fns {
  123. this.session.span_note(span, "here is a function named 'main'");
  124. }
  125. this.session.abort_if_errors();
  126. }
  127. }
  128. }