Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 65 additions & 18 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ pub use self::ViewPath_::*;
pub use self::PathParameters::*;

use attr::ThinAttributes;
use codemap::{Span, Spanned, DUMMY_SP, ExpnId};
use codemap::{mk_sp, respan, Span, Spanned, DUMMY_SP, ExpnId};
use abi::Abi;
use errors;
use ext::base;
use ext::tt::macro_parser;
use parse::token::InternedString;
use parse::token;
use parse::token::{self, keywords, InternedString};
use parse::lexer;
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
use print::pprust;
Expand Down Expand Up @@ -1674,7 +1673,25 @@ pub struct Arg {
pub id: NodeId,
}

/// Represents the kind of 'self' associated with a method.
/// String representation of `Ident` here is always "self", but hygiene contexts may differ.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum SelfKind {
/// No self
Static,
/// `self`, `mut self`
Value(Ident),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the mutability here?

/// `&'lt self`, `&'lt mut self`
Region(Option<Lifetime>, Mutability, Ident),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Borrowed is better than Region, I think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nm, this is just moved code

/// `self: TYPE`, `mut self: TYPE`
Explicit(P<Ty>, Ident),
}

pub type ExplicitSelf = Spanned<SelfKind>;

impl Arg {
#[unstable(feature = "rustc_private", issue = "27812")]
#[rustc_deprecated(since = "1.10.0", reason = "use `from_self` instead")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually sort of cool 👍.

pub fn new_self(span: Span, mutability: Mutability, self_ident: Ident) -> Arg {
let path = Spanned{span:span,node:self_ident};
Arg {
Expand All @@ -1692,6 +1709,51 @@ impl Arg {
id: DUMMY_NODE_ID
}
}

pub fn to_self(&self) -> Option<ExplicitSelf> {
if let PatKind::Ident(_, ident, _) = self.pat.node {
if ident.node.name == keywords::SelfValue.name() {
return match self.ty.node {
TyKind::Infer => Some(respan(self.pat.span, SelfKind::Value(ident.node))),
TyKind::Rptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyKind::Infer => {
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl, ident.node)))
}
_ => Some(respan(mk_sp(self.pat.span.lo, self.ty.span.hi),
SelfKind::Explicit(self.ty.clone(), ident.node))),
}
}
}
None
}

pub fn from_self(eself: ExplicitSelf, ident_sp: Span, mutbl: Mutability) -> Arg {
let pat = |ident, span| P(Pat {
id: DUMMY_NODE_ID,
node: PatKind::Ident(BindingMode::ByValue(mutbl), respan(ident_sp, ident), None),
span: span,
});
let infer_ty = P(Ty {
id: DUMMY_NODE_ID,
node: TyKind::Infer,
span: DUMMY_SP,
});
let arg = |ident, ty, span| Arg {
pat: pat(ident, span),
ty: ty,
id: DUMMY_NODE_ID,
};
match eself.node {
SelfKind::Static => panic!("bug: `Arg::from_self` is called \
with `SelfKind::Static` argument"),
SelfKind::Explicit(ty, ident) => arg(ident, ty, mk_sp(eself.span.lo, ident_sp.hi)),
SelfKind::Value(ident) => arg(ident, infer_ty, eself.span),
SelfKind::Region(lt, mutbl, ident) => arg(ident, P(Ty {
id: DUMMY_NODE_ID,
node: TyKind::Rptr(lt, MutTy { ty: infer_ty, mutbl: mutbl }),
span: DUMMY_SP,
}), eself.span),
}
}
}

/// Represents the header (not the body) of a function declaration
Expand Down Expand Up @@ -1772,21 +1834,6 @@ impl FunctionRetTy {
}
}

/// Represents the kind of 'self' associated with a method
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum SelfKind {
/// No self
Static,
/// `self`
Value(Ident),
/// `&'lt self`, `&'lt mut self`
Region(Option<Lifetime>, Mutability, Ident),
/// `self: TYPE`
Explicit(P<Ty>, Ident),
}

pub type ExplicitSelf = Spanned<SelfKind>;

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Mod {
/// A span from the first token past `{` to the last token until `}`.
Expand Down
Loading