diff options
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 76 | ||||
| -rw-r--r-- | src/libsyntax/codemap.rs | 18 |
2 files changed, 83 insertions, 11 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 5b03b3bf038..e844b206cc0 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -63,8 +63,10 @@ use owned_slice::OwnedSlice; use parse::token::{InternedString, str_to_ident}; use parse::token; use parse::lexer; +use print::pprust; use ptr::P; +use std::cell::Cell; use std::fmt; use std::rc::Rc; use serialize::{Encodable, Decodable, Encoder, Decoder}; @@ -200,14 +202,19 @@ impl Decodable for Ident { /// Function name (not all functions have names) pub type FnIdent = Option<Ident>; -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, - Debug, Copy)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] pub struct Lifetime { pub id: NodeId, pub span: Span, pub name: Name } +impl fmt::Debug for Lifetime { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "lifetime({}: {})", self.id, pprust::lifetime_to_string(self)) + } +} + /// A lifetime definition, eg `'a: 'b+'c+'d` #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct LifetimeDef { @@ -218,7 +225,7 @@ pub struct LifetimeDef { /// A "Path" is essentially Rust's notion of a name; for instance: /// std::cmp::PartialEq . It's represented as a sequence of identifiers, /// along with a bunch of supporting information. -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] pub struct Path { pub span: Span, /// A `::foo` path, is relative to the crate root rather than current @@ -228,6 +235,18 @@ pub struct Path { pub segments: Vec<PathSegment>, } +impl fmt::Debug for Path { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "path({})", pprust::path_to_string(self)) + } +} + +impl fmt::Display for Path { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", pprust::path_to_string(self)) + } +} + /// A segment of a path: an identifier, an optional lifetime, and a set of /// types. #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] @@ -358,12 +377,25 @@ pub type CrateNum = u32; pub type NodeId = u32; #[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, - RustcDecodable, Hash, Debug, Copy)] + RustcDecodable, Hash, Copy)] pub struct DefId { pub krate: CrateNum, pub node: NodeId, } +fn default_def_id_debug(_: DefId, _: &mut fmt::Formatter) -> fmt::Result { Ok(()) } + +thread_local!(pub static DEF_ID_DEBUG: Cell<fn(DefId, &mut fmt::Formatter) -> fmt::Result> = + Cell::new(default_def_id_debug)); + +impl fmt::Debug for DefId { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + try!(write!(f, "DefId {{ krate: {}, node: {} }}", + self.krate, self.node)); + DEF_ID_DEBUG.with(|def_id_debug| def_id_debug.get()(*self, f)) + } +} + impl DefId { /// Read the node id, asserting that this def-id is krate-local. pub fn local_id(&self) -> NodeId { @@ -539,13 +571,19 @@ pub struct Block { pub span: Span, } -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] pub struct Pat { pub id: NodeId, pub node: Pat_, pub span: Span, } +impl fmt::Debug for Pat { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "pat({}: {})", self.id, pprust::pat_to_string(self)) + } +} + /// A single field in a struct pattern /// /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` @@ -682,7 +720,16 @@ pub enum UnOp { /// A statement pub type Stmt = Spanned<Stmt_>; -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] +impl fmt::Debug for Stmt { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "stmt({}: {})", + ast_util::stmt_id(self), + pprust::stmt_to_string(self)) + } +} + + +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] pub enum Stmt_ { /// Could be an item or a local (let) binding: StmtDecl(P<Decl>, NodeId), @@ -695,7 +742,6 @@ pub enum Stmt_ { StmtMac(P<Mac>, MacStmtStyle), } - #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum MacStmtStyle { /// The macro statement had a trailing semicolon, e.g. `foo! { ... };` @@ -772,13 +818,19 @@ pub enum UnsafeSource { } /// An expression -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash,)] pub struct Expr { pub id: NodeId, pub node: Expr_, pub span: Span, } +impl fmt::Debug for Expr { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self)) + } +} + #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum Expr_ { /// First expr is the place; second expr is the value. @@ -1357,13 +1409,19 @@ pub struct TypeBinding { // NB PartialEq method appears below. -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] pub struct Ty { pub id: NodeId, pub node: Ty_, pub span: Span, } +impl fmt::Debug for Ty { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "type({})", pprust::ty_to_string(self)) + } +} + /// Not represented directly in the AST, referred to by name through a ty_path. #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] pub enum PrimTy { diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index ea27ed6b360..5ddcfaef9ea 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -19,7 +19,7 @@ pub use self::ExpnFormat::*; -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::ops::{Add, Sub}; use std::path::Path; use std::rc::Rc; @@ -115,7 +115,7 @@ impl Sub for CharPos { /// are *absolute* positions from the beginning of the codemap, not positions /// relative to FileMaps. Methods on the CodeMap can be used to relate spans back /// to the original source. -#[derive(Clone, Copy, Debug, Hash)] +#[derive(Clone, Copy, Hash)] pub struct Span { pub lo: BytePos, pub hi: BytePos, @@ -164,6 +164,20 @@ impl Decodable for Span { } } +fn default_span_debug(span: Span, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Span {{ lo: {:?}, hi: {:?}, expn_id: {:?} }}", + span.lo, span.hi, span.expn_id) +} + +thread_local!(pub static SPAN_DEBUG: Cell<fn(Span, &mut fmt::Formatter) -> fmt::Result> = + Cell::new(default_span_debug)); + +impl fmt::Debug for Span { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + SPAN_DEBUG.with(|span_debug| span_debug.get()(*self, f)) + } +} + pub fn spanned<T>(lo: BytePos, hi: BytePos, t: T) -> Spanned<T> { respan(mk_sp(lo, hi), t) } |
