Skip to content

Commit

Permalink
syntax: fix fallout from using ptr::P.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Sep 14, 2014
1 parent d6fb338 commit ccd8498
Show file tree
Hide file tree
Showing 45 changed files with 1,529 additions and 1,688 deletions.
82 changes: 41 additions & 41 deletions src/libsyntax/ast_map/blocks.rs
Expand Up @@ -22,7 +22,7 @@
//! for the `Code` associated with a particular NodeId.

use abi;
use ast::{P, Block, FnDecl, NodeId};
use ast::{Block, FnDecl, NodeId};
use ast;
use ast_map::{Node};
use ast_map;
Expand All @@ -39,16 +39,16 @@ use visit;
/// - The default implementation for a trait method.
///
/// To construct one, use the `Code::from_node` function.
pub struct FnLikeNode { node: ast_map::Node }
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: P<FnDecl>,
pub body: P<Block>,
pub decl: &'a FnDecl,
pub body: &'a Block,
pub kind: visit::FnKind<'a>,
pub span: Span,
pub id: NodeId,
Expand Down Expand Up @@ -78,12 +78,12 @@ impl MaybeFnLike for ast::Expr {
/// 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).
pub enum Code {
FnLikeCode(FnLikeNode),
BlockCode(P<Block>),
pub enum Code<'a> {
FnLikeCode(FnLikeNode<'a>),
BlockCode(&'a Block),
}

impl Code {
impl<'a> Code<'a> {
pub fn id(&self) -> ast::NodeId {
match *self {
FnLikeCode(node) => node.id(),
Expand Down Expand Up @@ -115,32 +115,32 @@ impl Code {
/// use when implementing FnLikeNode operations.
struct ItemFnParts<'a> {
ident: ast::Ident,
decl: P<ast::FnDecl>,
decl: &'a ast::FnDecl,
style: ast::FnStyle,
abi: abi::Abi,
generics: &'a ast::Generics,
body: P<Block>,
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 {
decl: P<FnDecl>,
body: P<Block>,
struct ClosureParts<'a> {
decl: &'a FnDecl,
body: &'a Block,
id: NodeId,
span: Span
}

impl ClosureParts {
fn new(d: P<FnDecl>, b: P<Block>, id: NodeId, s: Span) -> ClosureParts {
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 FnLikeNode {
pub fn to_fn_parts<'a>(&'a self) -> FnParts<'a> {
impl<'a> FnLikeNode<'a> {
pub fn to_fn_parts(self) -> FnParts<'a> {
FnParts {
decl: self.decl(),
body: self.body(),
Expand All @@ -150,31 +150,31 @@ impl FnLikeNode {
}
}

pub fn body<'a>(&'a self) -> P<Block> {
self.handle(|i: ItemFnParts| i.body,
|m: &'a ast::Method| m.pe_body(),
|c: ClosureParts| c.body)
pub fn body(self) -> &'a Block {
self.handle(|i: ItemFnParts<'a>| &*i.body,
|m: &'a ast::Method| m.pe_body(),
|c: ClosureParts<'a>| c.body)
}

pub fn decl<'a>(&'a self) -> P<FnDecl> {
self.handle(|i: ItemFnParts| i.decl,
|m: &'a ast::Method| m.pe_fn_decl(),
|c: ClosureParts| c.decl)
pub fn decl(self) -> &'a FnDecl {
self.handle(|i: ItemFnParts<'a>| &*i.decl,
|m: &'a ast::Method| m.pe_fn_decl(),
|c: ClosureParts<'a>| c.decl)
}

pub fn span<'a>(&'a self) -> Span {
pub fn span(self) -> Span {
self.handle(|i: ItemFnParts| i.span,
|m: &'a ast::Method| m.span,
|c: ClosureParts| c.span)
}

pub fn id<'a>(&'a self) -> NodeId {
pub fn id(self) -> NodeId {
self.handle(|i: ItemFnParts| i.id,
|m: &'a ast::Method| m.id,
|c: ClosureParts| c.id)
}

pub fn kind<'a>(&'a self) -> visit::FnKind<'a> {
pub fn kind(self) -> visit::FnKind<'a> {
let item = |p: ItemFnParts<'a>| -> visit::FnKind<'a> {
visit::FkItemFn(p.ident, p.generics, p.style, p.abi)
};
Expand All @@ -187,33 +187,33 @@ impl FnLikeNode {
self.handle(item, method, closure)
}

fn handle<'a, A>(&'a self,
item_fn: |ItemFnParts<'a>| -> A,
method: |&'a ast::Method| -> A,
closure: |ClosureParts| -> A) -> A {
fn handle<A>(self,
item_fn: |ItemFnParts<'a>| -> A,
method: |&'a ast::Method| -> A,
closure: |ClosureParts<'a>| -> A) -> A {
match self.node {
ast_map::NodeItem(ref i) => match i.node {
ast::ItemFn(decl, style, abi, ref generics, block) =>
ast_map::NodeItem(i) => match i.node {
ast::ItemFn(ref decl, style, abi, ref generics, ref block) =>
item_fn(ItemFnParts{
ident: i.ident, decl: decl, style: style, body: block,
ident: i.ident, decl: &**decl, style: style, body: &**block,
generics: generics, abi: abi, id: i.id, span: i.span
}),
_ => fail!("item FnLikeNode that is not fn-like"),
},
ast_map::NodeTraitItem(ref t) => match **t {
ast_map::NodeTraitItem(t) => match *t {
ast::ProvidedMethod(ref m) => method(&**m),
_ => fail!("trait method FnLikeNode that is not fn-like"),
},
ast_map::NodeImplItem(ref ii) => {
match **ii {
ast_map::NodeImplItem(ii) => {
match *ii {
ast::MethodImplItem(ref m) => method(&**m),
}
}
ast_map::NodeExpr(ref e) => match e.node {
ast_map::NodeExpr(e) => match e.node {
ast::ExprFnBlock(_, ref decl, ref block) =>
closure(ClosureParts::new(*decl, *block, e.id, e.span)),
closure(ClosureParts::new(&**decl, &**block, e.id, e.span)),
ast::ExprProc(ref decl, ref block) =>
closure(ClosureParts::new(*decl, *block, e.id, e.span)),
closure(ClosureParts::new(&**decl, &**block, e.id, e.span)),
_ => fail!("expr FnLikeNode that is not fn-like"),
},
_ => fail!("other FnLikeNode that is not fn-like"),
Expand Down
116 changes: 15 additions & 101 deletions src/libsyntax/ast_util.rs
Expand Up @@ -19,12 +19,12 @@ use codemap::Span;
use owned_slice::OwnedSlice;
use parse::token;
use print::pprust;
use ptr::P;
use visit::Visitor;
use visit;

use std::cell::Cell;
use std::cmp;
use std::gc::{Gc, GC};
use std::u32;

pub fn path_name_i(idents: &[Ident]) -> String {
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn unop_to_string(op: UnOp) -> &'static str {
}
}

pub fn is_path(e: Gc<Expr>) -> bool {
pub fn is_path(e: P<Expr>) -> bool {
return match e.node { ExprPath(_) => true, _ => false };
}

Expand Down Expand Up @@ -166,21 +166,6 @@ pub fn float_ty_to_string(t: FloatTy) -> String {
}
}

pub fn is_call_expr(e: Gc<Expr>) -> bool {
match e.node { ExprCall(..) => true, _ => false }
}

pub fn block_from_expr(e: Gc<Expr>) -> P<Block> {
P(Block {
view_items: Vec::new(),
stmts: Vec::new(),
expr: Some(e),
id: e.id,
rules: DefaultBlock,
span: e.span
})
}

// convert a span and an identifier to the corresponding
// 1-segment path
pub fn ident_to_path(s: Span, identifier: Ident) -> Path {
Expand All @@ -197,10 +182,12 @@ pub fn ident_to_path(s: Span, identifier: Ident) -> Path {
}
}

pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> Gc<Pat> {
box(GC) ast::Pat { id: id,
node: PatIdent(BindByValue(MutImmutable), codemap::Spanned{span:s, node:i}, None),
span: s }
pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> P<Pat> {
P(Pat {
id: id,
node: PatIdent(BindByValue(MutImmutable), codemap::Spanned{span:s, node:i}, None),
span: s
})
}

pub fn name_to_dummy_lifetime(name: Name) -> Lifetime {
Expand All @@ -226,57 +213,6 @@ pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: &Ty) -> Ident {
token::gensym_ident(pretty.as_slice())
}

pub fn trait_method_to_ty_method(method: &Method) -> TypeMethod {
match method.node {
MethDecl(ident,
ref generics,
abi,
explicit_self,
fn_style,
decl,
_,
vis) => {
TypeMethod {
ident: ident,
attrs: method.attrs.clone(),
fn_style: fn_style,
decl: decl,
generics: generics.clone(),
explicit_self: explicit_self,
id: method.id,
span: method.span,
vis: vis,
abi: abi,
}
},
MethMac(_) => fail!("expected non-macro method declaration")
}
}

/// extract a TypeMethod from a TraitItem. if the TraitItem is
/// a default, pull out the useful fields to make a TypeMethod
//
// NB: to be used only after expansion is complete, and macros are gone.
pub fn trait_item_to_ty_method(method: &TraitItem) -> TypeMethod {
match *method {
RequiredMethod(ref m) => (*m).clone(),
ProvidedMethod(ref m) => trait_method_to_ty_method(&**m),
}
}

pub fn split_trait_methods(trait_methods: &[TraitItem])
-> (Vec<TypeMethod> , Vec<Gc<Method>> ) {
let mut reqd = Vec::new();
let mut provd = Vec::new();
for trt_method in trait_methods.iter() {
match *trt_method {
RequiredMethod(ref tm) => reqd.push((*tm).clone()),
ProvidedMethod(m) => provd.push(m)
}
};
(reqd, provd)
}

pub fn struct_field_visibility(field: ast::StructField) -> Visibility {
match field.node.kind {
ast::NamedField(_, v) | ast::UnnamedField(v) => v
Expand Down Expand Up @@ -603,13 +539,6 @@ pub fn compute_id_range_for_fn_body(fk: visit::FnKind,
visitor.result.get()
}

pub fn is_item_impl(item: Gc<ast::Item>) -> bool {
match item.node {
ItemImpl(..) => true,
_ => false
}
}

pub fn walk_pat(pat: &Pat, it: |&Pat| -> bool) -> bool {
if !it(pat) {
return false;
Expand Down Expand Up @@ -678,7 +607,7 @@ pub fn struct_def_is_tuple_like(struct_def: &ast::StructDef) -> bool {

/// Returns true if the given pattern consists solely of an identifier
/// and false otherwise.
pub fn pat_is_ident(pat: Gc<ast::Pat>) -> bool {
pub fn pat_is_ident(pat: P<ast::Pat>) -> bool {
match pat.node {
ast::PatIdent(..) => true,
_ => false,
Expand Down Expand Up @@ -713,28 +642,13 @@ pub fn segments_name_eq(a : &[ast::PathSegment], b : &[ast::PathSegment]) -> boo
}

/// Returns true if this literal is a string and false otherwise.
pub fn lit_is_str(lit: Gc<Lit>) -> bool {
pub fn lit_is_str(lit: &Lit) -> bool {
match lit.node {
LitStr(..) => true,
_ => false,
}
}

pub fn get_inner_tys(ty: P<Ty>) -> Vec<P<Ty>> {
match ty.node {
ast::TyRptr(_, mut_ty) | ast::TyPtr(mut_ty) => {
vec!(mut_ty.ty)
}
ast::TyBox(ty)
| ast::TyVec(ty)
| ast::TyUniq(ty)
| ast::TyFixedLengthVec(ty, _) => vec!(ty),
ast::TyTup(ref tys) => tys.clone(),
ast::TyParen(ty) => get_inner_tys(ty),
_ => Vec::new()
}
}

/// Returns true if the static with the given mutability and attributes
/// has a significant address and false otherwise.
pub fn static_has_significant_address(mutbl: ast::Mutability,
Expand All @@ -757,13 +671,13 @@ pub trait PostExpansionMethod {
fn pe_abi(&self) -> Abi;
fn pe_explicit_self<'a>(&'a self) -> &'a ast::ExplicitSelf;
fn pe_fn_style(&self) -> ast::FnStyle;
fn pe_fn_decl(&self) -> P<ast::FnDecl>;
fn pe_body(&self) -> P<ast::Block>;
fn pe_fn_decl<'a>(&'a self) -> &'a ast::FnDecl;
fn pe_body<'a>(&'a self) -> &'a ast::Block;
fn pe_vis(&self) -> ast::Visibility;
}

macro_rules! mf_method{
($meth_name:ident, $field_ty:ty, $field_pat:pat, $result:ident) => {
($meth_name:ident, $field_ty:ty, $field_pat:pat, $result:expr) => {
fn $meth_name<'a>(&'a self) -> $field_ty {
match self.node {
$field_pat => $result,
Expand All @@ -784,8 +698,8 @@ impl PostExpansionMethod for Method {
mf_method!(pe_explicit_self,&'a ast::ExplicitSelf,
MethDecl(_,_,_,ref explicit_self,_,_,_,_),explicit_self)
mf_method!(pe_fn_style,ast::FnStyle,MethDecl(_,_,_,_,fn_style,_,_,_),fn_style)
mf_method!(pe_fn_decl,P<ast::FnDecl>,MethDecl(_,_,_,_,_,decl,_,_),decl)
mf_method!(pe_body,P<ast::Block>,MethDecl(_,_,_,_,_,_,body,_),body)
mf_method!(pe_fn_decl,&'a ast::FnDecl,MethDecl(_,_,_,_,_,ref decl,_,_),&**decl)
mf_method!(pe_body,&'a ast::Block,MethDecl(_,_,_,_,_,_,ref body,_),&**body)
mf_method!(pe_vis,ast::Visibility,MethDecl(_,_,_,_,_,_,_,vis),vis)
}

Expand Down

0 comments on commit ccd8498

Please sign in to comment.