Move entry point identification logic to libsyntax
Identifying entry points will be useful in --test mode, which is handled in libsyntax.
This commit is contained in:
parent
4c996499a1
commit
45de9de1e9
3 changed files with 79 additions and 45 deletions
|
@ -11,20 +11,19 @@
|
||||||
|
|
||||||
use ast_map;
|
use ast_map;
|
||||||
use session::{config, Session};
|
use session::{config, Session};
|
||||||
use syntax::ast::{Name, NodeId, Item, ItemFn};
|
use syntax;
|
||||||
|
use syntax::ast::{NodeId, Item};
|
||||||
use syntax::attr;
|
use syntax::attr;
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::parse::token;
|
use syntax::entry::EntryPointType;
|
||||||
use syntax::visit;
|
use syntax::visit;
|
||||||
use syntax::visit::Visitor;
|
use syntax::visit::Visitor;
|
||||||
|
|
||||||
struct EntryContext<'a, 'ast: 'a> {
|
struct EntryContext<'a> {
|
||||||
session: &'a Session,
|
session: &'a Session,
|
||||||
|
|
||||||
ast_map: &'a ast_map::Map<'ast>,
|
// The current depth in the ast
|
||||||
|
depth: usize,
|
||||||
// The interned Name for "main".
|
|
||||||
main_name: Name,
|
|
||||||
|
|
||||||
// The top-level function called 'main'
|
// The top-level function called 'main'
|
||||||
main_fn: Option<(NodeId, Span)>,
|
main_fn: Option<(NodeId, Span)>,
|
||||||
|
@ -40,9 +39,11 @@ struct EntryContext<'a, 'ast: 'a> {
|
||||||
non_main_fns: Vec<(NodeId, Span)> ,
|
non_main_fns: Vec<(NodeId, Span)> ,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'ast, 'v> Visitor<'v> for EntryContext<'a, 'ast> {
|
impl<'a, 'v> Visitor<'v> for EntryContext<'a> {
|
||||||
fn visit_item(&mut self, item: &Item) {
|
fn visit_item(&mut self, item: &Item) {
|
||||||
|
self.depth += 1;
|
||||||
find_item(item, self);
|
find_item(item, self);
|
||||||
|
self.depth -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,8 +64,7 @@ pub fn find_entry_point(session: &Session, ast_map: &ast_map::Map) {
|
||||||
|
|
||||||
let mut ctxt = EntryContext {
|
let mut ctxt = EntryContext {
|
||||||
session: session,
|
session: session,
|
||||||
main_name: token::intern("main"),
|
depth: 0,
|
||||||
ast_map: ast_map,
|
|
||||||
main_fn: None,
|
main_fn: None,
|
||||||
attr_main_fn: None,
|
attr_main_fn: None,
|
||||||
start_fn: None,
|
start_fn: None,
|
||||||
|
@ -77,44 +77,35 @@ pub fn find_entry_point(session: &Session, ast_map: &ast_map::Map) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_item(item: &Item, ctxt: &mut EntryContext) {
|
fn find_item(item: &Item, ctxt: &mut EntryContext) {
|
||||||
match item.node {
|
match syntax::entry::entry_point_type(item, ctxt.depth) {
|
||||||
ItemFn(..) => {
|
EntryPointType::MainNamed => {
|
||||||
if item.ident.name == ctxt.main_name {
|
|
||||||
ctxt.ast_map.with_path(item.id, |path| {
|
|
||||||
if path.count() == 1 {
|
|
||||||
// This is a top-level function so can be 'main'
|
|
||||||
if ctxt.main_fn.is_none() {
|
if ctxt.main_fn.is_none() {
|
||||||
ctxt.main_fn = Some((item.id, item.span));
|
ctxt.main_fn = Some((item.id, item.span));
|
||||||
} else {
|
} else {
|
||||||
span_err!(ctxt.session, item.span, E0136,
|
span_err!(ctxt.session, item.span, E0136,
|
||||||
"multiple 'main' functions");
|
"multiple 'main' functions");
|
||||||
}
|
}
|
||||||
} else {
|
},
|
||||||
// This isn't main
|
EntryPointType::OtherMain => {
|
||||||
ctxt.non_main_fns.push((item.id, item.span));
|
ctxt.non_main_fns.push((item.id, item.span));
|
||||||
}
|
},
|
||||||
});
|
EntryPointType::MainAttr => {
|
||||||
}
|
|
||||||
|
|
||||||
if attr::contains_name(&item.attrs, "main") {
|
|
||||||
if ctxt.attr_main_fn.is_none() {
|
if ctxt.attr_main_fn.is_none() {
|
||||||
ctxt.attr_main_fn = Some((item.id, item.span));
|
ctxt.attr_main_fn = Some((item.id, item.span));
|
||||||
} else {
|
} else {
|
||||||
span_err!(ctxt.session, item.span, E0137,
|
span_err!(ctxt.session, item.span, E0137,
|
||||||
"multiple functions with a #[main] attribute");
|
"multiple functions with a #[main] attribute");
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
EntryPointType::Start => {
|
||||||
if attr::contains_name(&item.attrs, "start") {
|
|
||||||
if ctxt.start_fn.is_none() {
|
if ctxt.start_fn.is_none() {
|
||||||
ctxt.start_fn = Some((item.id, item.span));
|
ctxt.start_fn = Some((item.id, item.span));
|
||||||
} else {
|
} else {
|
||||||
span_err!(ctxt.session, item.span, E0138,
|
span_err!(ctxt.session, item.span, E0138,
|
||||||
"multiple 'start' functions");
|
"multiple 'start' functions");
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
EntryPointType::None => ()
|
||||||
_ => ()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
visit::walk_item(ctxt, item);
|
visit::walk_item(ctxt, item);
|
||||||
|
|
42
src/libsyntax/entry.rs
Normal file
42
src/libsyntax/entry.rs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
use attr;
|
||||||
|
use ast::{Item, ItemFn};
|
||||||
|
|
||||||
|
pub enum EntryPointType {
|
||||||
|
None,
|
||||||
|
MainNamed,
|
||||||
|
MainAttr,
|
||||||
|
Start,
|
||||||
|
OtherMain, // Not an entry point, but some other function named main
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn entry_point_type(item: &Item, depth: usize) -> EntryPointType {
|
||||||
|
match item.node {
|
||||||
|
ItemFn(..) => {
|
||||||
|
if attr::contains_name(&item.attrs, "start") {
|
||||||
|
EntryPointType::Start
|
||||||
|
} else if attr::contains_name(&item.attrs, "main") {
|
||||||
|
EntryPointType::MainAttr
|
||||||
|
} else if item.ident.name == "main" {
|
||||||
|
if depth == 1 {
|
||||||
|
// This is a top-level function so can be 'main'
|
||||||
|
EntryPointType::MainNamed
|
||||||
|
} else {
|
||||||
|
EntryPointType::OtherMain
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
EntryPointType::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => EntryPointType::None,
|
||||||
|
}
|
||||||
|
}
|
|
@ -90,6 +90,7 @@ pub mod attr;
|
||||||
pub mod codemap;
|
pub mod codemap;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod diagnostic;
|
pub mod diagnostic;
|
||||||
|
pub mod entry;
|
||||||
pub mod feature_gate;
|
pub mod feature_gate;
|
||||||
pub mod fold;
|
pub mod fold;
|
||||||
pub mod owned_slice;
|
pub mod owned_slice;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue