syntax: move ast_map to librustc.

This commit is contained in:
Eduard Burtescu 2015-06-10 02:40:45 +03:00
parent 7b0f2af27f
commit 76eaed44d9
45 changed files with 75 additions and 63 deletions

View file

@ -0,0 +1,254 @@
// Copyright 2014 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.
//! This module provides a simplified abstraction for working with
//! code blocks identified by their integer node-id. In particular,
//! it captures a common set of attributes that all "function-like
//! things" (represented by `FnLike` instances) share. For example,
//! all `FnLike` instances have a type signature (be it explicit or
//! inferred). And all `FnLike` instances have a body, i.e. the code
//! that is run when the function-like thing it represents is invoked.
//!
//! With the above abstraction in place, one can treat the program
//! text as a collection of blocks of code (and most such blocks are
//! nested within a uniquely determined `FnLike`), and users can ask
//! for the `Code` associated with a particular NodeId.
pub use self::Code::*;
use ast_map::{self, Node};
use syntax::abi;
use syntax::ast::{Block, FnDecl, NodeId};
use syntax::ast;
use syntax::codemap::Span;
use syntax::visit;
/// An FnLikeNode is a Node that is like a fn, in that it has a decl
/// and a body (as well as a NodeId, a span, etc).
///
/// More specifically, it is one of either:
/// - A function item,
/// - A closure expr (i.e. an ExprClosure), or
/// - The default implementation for a trait method.
///
/// To construct one, use the `Code::from_node` function.
#[derive(Copy, Clone)]
pub struct FnLikeNode<'a> { node: ast_map::Node<'a> }
/// MaybeFnLike wraps a method that indicates if an object
/// corresponds to some FnLikeNode.
pub trait MaybeFnLike { fn is_fn_like(&self) -> bool; }
/// Components shared by fn-like things (fn items, methods, closures).
pub struct FnParts<'a> {
pub decl: &'a FnDecl,
pub body: &'a Block,
pub kind: visit::FnKind<'a>,
pub span: Span,
pub id: NodeId,
}
impl MaybeFnLike for ast::Item {
fn is_fn_like(&self) -> bool {
match self.node { ast::ItemFn(..) => true, _ => false, }
}
}
impl MaybeFnLike for ast::TraitItem {
fn is_fn_like(&self) -> bool {
match self.node { ast::MethodTraitItem(_, Some(_)) => true, _ => false, }
}
}
impl MaybeFnLike for ast::Expr {
fn is_fn_like(&self) -> bool {
match self.node {
ast::ExprClosure(..) => true,
_ => false,
}
}
}
/// Carries either an FnLikeNode or a Block, as these are the two
/// constructs that correspond to "code" (as in, something from which
/// we can construct a control-flow graph).
#[derive(Copy, Clone)]
pub enum Code<'a> {
FnLikeCode(FnLikeNode<'a>),
BlockCode(&'a Block),
}
impl<'a> Code<'a> {
pub fn id(&self) -> ast::NodeId {
match *self {
FnLikeCode(node) => node.id(),
BlockCode(block) => block.id,
}
}
/// Attempts to construct a Code from presumed FnLike or Block node input.
pub fn from_node(node: Node) -> Option<Code> {
if let ast_map::NodeBlock(block) = node {
Some(BlockCode(block))
} else {
FnLikeNode::from_node(node).map(|fn_like| FnLikeCode(fn_like))
}
}
}
/// These are all the components one can extract from a fn item for
/// use when implementing FnLikeNode operations.
struct ItemFnParts<'a> {
ident: ast::Ident,
decl: &'a ast::FnDecl,
unsafety: ast::Unsafety,
constness: ast::Constness,
abi: abi::Abi,
vis: ast::Visibility,
generics: &'a ast::Generics,
body: &'a Block,
id: ast::NodeId,
span: Span
}
/// These are all the components one can extract from a closure expr
/// for use when implementing FnLikeNode operations.
struct ClosureParts<'a> {
decl: &'a FnDecl,
body: &'a Block,
id: NodeId,
span: Span
}
impl<'a> ClosureParts<'a> {
fn new(d: &'a FnDecl, b: &'a Block, id: NodeId, s: Span) -> ClosureParts<'a> {
ClosureParts { decl: d, body: b, id: id, span: s }
}
}
impl<'a> FnLikeNode<'a> {
/// Attempts to construct a FnLikeNode from presumed FnLike node input.
pub fn from_node(node: Node) -> Option<FnLikeNode> {
let fn_like = match node {
ast_map::NodeItem(item) => item.is_fn_like(),
ast_map::NodeTraitItem(tm) => tm.is_fn_like(),
ast_map::NodeImplItem(_) => true,
ast_map::NodeExpr(e) => e.is_fn_like(),
_ => false
};
if fn_like {
Some(FnLikeNode {
node: node
})
} else {
None
}
}
pub fn to_fn_parts(self) -> FnParts<'a> {
FnParts {
decl: self.decl(),
body: self.body(),
kind: self.kind(),
span: self.span(),
id: self.id(),
}
}
pub fn body(self) -> &'a Block {
self.handle(|i: ItemFnParts<'a>| &*i.body,
|_, _, _: &'a ast::MethodSig, _, body: &'a ast::Block, _| body,
|c: ClosureParts<'a>| c.body)
}
pub fn decl(self) -> &'a FnDecl {
self.handle(|i: ItemFnParts<'a>| &*i.decl,
|_, _, sig: &'a ast::MethodSig, _, _, _| &sig.decl,
|c: ClosureParts<'a>| c.decl)
}
pub fn span(self) -> Span {
self.handle(|i: ItemFnParts| i.span,
|_, _, _: &'a ast::MethodSig, _, _, span| span,
|c: ClosureParts| c.span)
}
pub fn id(self) -> NodeId {
self.handle(|i: ItemFnParts| i.id,
|id, _, _: &'a ast::MethodSig, _, _, _| id,
|c: ClosureParts| c.id)
}
pub fn kind(self) -> visit::FnKind<'a> {
let item = |p: ItemFnParts<'a>| -> visit::FnKind<'a> {
visit::FkItemFn(p.ident, p.generics, p.unsafety, p.constness, p.abi, p.vis)
};
let closure = |_: ClosureParts| {
visit::FkFnBlock
};
let method = |_, ident, sig: &'a ast::MethodSig, vis, _, _| {
visit::FkMethod(ident, sig, vis)
};
self.handle(item, method, closure)
}
fn handle<A, I, M, C>(self, item_fn: I, method: M, closure: C) -> A where
I: FnOnce(ItemFnParts<'a>) -> A,
M: FnOnce(NodeId,
ast::Ident,
&'a ast::MethodSig,
Option<ast::Visibility>,
&'a ast::Block,
Span)
-> A,
C: FnOnce(ClosureParts<'a>) -> A,
{
match self.node {
ast_map::NodeItem(i) => match i.node {
ast::ItemFn(ref decl, unsafety, constness, abi, ref generics, ref block) =>
item_fn(ItemFnParts {
id: i.id,
ident: i.ident,
decl: &**decl,
unsafety: unsafety,
body: &**block,
generics: generics,
abi: abi,
vis: i.vis,
constness: constness,
span: i.span
}),
_ => panic!("item FnLikeNode that is not fn-like"),
},
ast_map::NodeTraitItem(ti) => match ti.node {
ast::MethodTraitItem(ref sig, Some(ref body)) => {
method(ti.id, ti.ident, sig, None, body, ti.span)
}
_ => panic!("trait method FnLikeNode that is not fn-like"),
},
ast_map::NodeImplItem(ii) => {
match ii.node {
ast::MethodImplItem(ref sig, ref body) => {
method(ii.id, ii.ident, sig, Some(ii.vis), body, ii.span)
}
_ => {
panic!("impl method FnLikeNode that is not fn-like")
}
}
}
ast_map::NodeExpr(e) => match e.node {
ast::ExprClosure(_, ref decl, ref block) =>
closure(ClosureParts::new(&**decl, &**block, e.id, e.span)),
_ => panic!("expr FnLikeNode that is not fn-like"),
},
_ => panic!("other FnLikeNode that is not fn-like"),
}
}
}

1023
src/librustc/ast_map/mod.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -92,6 +92,8 @@ pub mod back {
pub use rustc_back::x86_64;
}
pub mod ast_map;
pub mod middle {
pub mod astconv_util;
pub mod astencode;

View file

@ -10,6 +10,7 @@
// Searching for information from the cstore
use ast_map;
use metadata::common::*;
use metadata::cstore;
use metadata::decoder;
@ -20,7 +21,6 @@ use rbml;
use rbml::reader;
use std::rc::Rc;
use syntax::ast;
use syntax::ast_map;
use syntax::attr;
use syntax::attr::AttrMetaMethods;
use syntax::diagnostic::expect;

View file

@ -15,6 +15,7 @@
pub use self::DefLike::*;
use self::Family::*;
use ast_map;
use back::svh::Svh;
use metadata::cstore::crate_metadata;
use metadata::common::*;
@ -44,7 +45,6 @@ use std::str;
use rbml::reader;
use rbml;
use serialize::Decodable;
use syntax::ast_map;
use syntax::attr;
use syntax::parse::token::{IdentInterner, special_idents};
use syntax::parse::token;

View file

@ -15,6 +15,7 @@
pub use self::InlinedItemRef::*;
use ast_map::{self, LinkedPath, PathElem, PathElems};
use back::svh::Svh;
use session::config;
use metadata::common::*;
@ -34,7 +35,6 @@ use std::io::prelude::*;
use std::io::{Cursor, SeekFrom};
use syntax::abi;
use syntax::ast::{self, DefId, NodeId};
use syntax::ast_map::{self, LinkedPath, PathElem, PathElems};
use syntax::ast_util::*;
use syntax::ast_util;
use syntax::attr;

View file

@ -12,6 +12,7 @@
// FIXME: remove this after snapshot, and Results are handled
#![allow(unused_must_use)]
use ast_map;
use metadata::common as c;
use metadata::cstore as cstore;
use session::Session;
@ -32,7 +33,7 @@ use middle::subst::VecPerParamSpace;
use middle::ty::{self, Ty, MethodCall, MethodCallee, MethodOrigin};
use util::ppaux::ty_to_string;
use syntax::{ast, ast_map, ast_util, codemap, fold};
use syntax::{ast, ast_util, codemap, fold};
use syntax::codemap::Span;
use syntax::fold::Folder;
use syntax::parse::token;

View file

@ -17,8 +17,8 @@ use std::borrow::IntoCow;
use graphviz as dot;
use syntax::ast;
use syntax::ast_map;
use ast_map;
use middle::cfg;
pub type Node<'a> = (cfg::CFGIndex, &'a cfg::CFGNode);

View file

@ -11,11 +11,11 @@
// This compiler pass detects static items that refer to themselves
// recursively.
use ast_map;
use session::Session;
use middle::def::{DefStatic, DefConst, DefAssociatedConst, DefMap};
use syntax::ast;
use syntax::{ast_util, ast_map};
use syntax::{ast, ast_util};
use syntax::codemap::Span;
use syntax::visit::Visitor;
use syntax::visit;

View file

@ -15,6 +15,8 @@ pub use self::const_val::*;
use self::ErrKind::*;
use ast_map;
use ast_map::blocks::FnLikeNode;
use metadata::csearch;
use middle::{astencode, def, infer, subst, traits};
use middle::pat_util::def_to_path;
@ -24,13 +26,12 @@ use util::num::ToPrimitive;
use util::ppaux::Repr;
use syntax::ast::{self, Expr};
use syntax::ast_map::blocks::FnLikeNode;
use syntax::ast_util;
use syntax::codemap::Span;
use syntax::feature_gate;
use syntax::parse::token::InternedString;
use syntax::ptr::P;
use syntax::{ast_map, codemap, visit};
use syntax::{codemap, visit};
use std::borrow::{Cow, IntoCow};
use std::num::wrapping::OverflowingOps;

View file

@ -12,12 +12,13 @@
// closely. The idea is that all reachable symbols are live, codes called
// from live codes are live, and everything else is dead.
use ast_map;
use middle::{def, pat_util, privacy, ty};
use lint;
use util::nodemap::NodeSet;
use std::collections::HashSet;
use syntax::{ast, ast_map, codemap};
use syntax::{ast, codemap};
use syntax::ast_util::{local_def, is_local};
use syntax::attr::{self, AttrMetaMethods};
use syntax::visit::{self, Visitor};

View file

@ -9,9 +9,9 @@
// except according to those terms.
use ast_map;
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;

View file

@ -72,6 +72,7 @@ use super::region_inference::ProcessedErrors;
use super::region_inference::SameRegions;
use std::collections::HashSet;
use ast_map;
use middle::def;
use middle::infer;
use middle::subst;
@ -81,7 +82,6 @@ use std::cell::{Cell, RefCell};
use std::char::from_u32;
use std::string::String;
use syntax::ast;
use syntax::ast_map;
use syntax::ast_util::name_to_dummy_lifetime;
use syntax::owned_slice::OwnedSlice;
use syntax::codemap;

View file

@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use ast_map::NodeForeignItem;
use metadata::csearch;
use middle::def::DefFn;
use middle::subst::{Subst, Substs, EnumeratedItems};
@ -18,7 +19,6 @@ use util::ppaux::Repr;
use syntax::abi::RustIntrinsic;
use syntax::ast::DefId;
use syntax::ast;
use syntax::ast_map::NodeForeignItem;
use syntax::codemap::Span;
use syntax::parse::token;
use syntax::visit::Visitor;

View file

@ -73,6 +73,7 @@ pub use self::categorization::*;
use self::Aliasability::*;
use ast_map;
use middle::check_const;
use middle::def;
use middle::region;
@ -82,7 +83,6 @@ use util::ppaux::{Repr, UserString};
use syntax::ast::{MutImmutable, MutMutable};
use syntax::ast;
use syntax::ast_map;
use syntax::codemap::Span;
use syntax::print::pprust;
use syntax::parse::token;

View file

@ -15,6 +15,7 @@
// makes all other generics or inline functions that it references
// reachable as well.
use ast_map;
use middle::def;
use middle::ty;
use middle::privacy;
@ -24,7 +25,6 @@ use util::nodemap::NodeSet;
use std::collections::HashSet;
use syntax::abi;
use syntax::ast;
use syntax::ast_map;
use syntax::ast_util::is_local;
use syntax::attr;
use syntax::visit::Visitor;

View file

@ -16,6 +16,7 @@
//! Most of the documentation on regions can be found in
//! `middle/typeck/infer/region_inference.rs`
use ast_map;
use session::Session;
use middle::ty::{self, Ty};
use util::nodemap::{FnvHashMap, FnvHashSet, NodeMap};
@ -25,7 +26,6 @@ use syntax::codemap::{self, Span};
use syntax::{ast, visit};
use syntax::ast::{Block, Item, FnDecl, NodeId, Arm, Pat, Stmt, Expr, Local};
use syntax::ast_util::stmt_id;
use syntax::ast_map;
use syntax::ptr::P;
use syntax::visit::{Visitor, FnKind};

View file

@ -35,6 +35,7 @@ pub use self::IntVarValue::*;
pub use self::MethodOrigin::*;
pub use self::CopyImplementationError::*;
use ast_map::{self, LinkedPath};
use back::svh::Svh;
use session::Session;
use lint;
@ -91,7 +92,6 @@ use syntax::parse::token::{self, InternedString, special_idents};
use syntax::print::pprust;
use syntax::ptr::P;
use syntax::ast;
use syntax::ast_map::{self, LinkedPath};
pub type Disr = u64;

View file

@ -9,6 +9,7 @@
// except according to those terms.
use ast_map;
use middle::def;
use middle::region;
use middle::subst::{VecPerParamSpace,Subst};
@ -31,7 +32,6 @@ use std::collections::hash_state::HashState;
use std::hash::Hash;
use std::rc::Rc;
use syntax::abi;
use syntax::ast_map;
use syntax::codemap::{Span, Pos};
use syntax::parse::token;
use syntax::print::pprust;