X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibsyntax%2Fast.rs;h=4e6641f4c50ed88c76d6bf161e616f81b161f54f;hb=6f4ab9458a7ad06c8ce630604f533c8c0c0acef4;hp=f7d9d532062a99acf62ffcf15a3626a0d6e017ad;hpb=9475ae477a4d42c564eab9621ffb6aa7c160a3dc;p=rust.git diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index f7d9d532062..4e6641f4c50 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -120,12 +120,11 @@ pub struct PathSegment { pub span: Span, /// Type/lifetime parameters attached to this path. They come in - /// two flavors: `Path` and `Path(A,B) -> C`. Note that - /// this is more than just simple syntactic sugar; the use of - /// parens affects the region binding rules, so we preserve the - /// distinction. - /// The `Option>` wrapper is purely a size optimization; - /// `None` is used to represent both `Path` and `Path<>`. + /// two flavors: `Path` and `Path(A,B) -> C`. + /// `None` means that no parameter list is supplied (`Path`), + /// `Some` means that parameter list is supplied (`Path`) + /// but it can be empty (`Path<>`). + /// `P` is used as a size optimization for the common case with no parameters. pub parameters: Option>, } @@ -136,7 +135,7 @@ pub fn from_ident(ident: Ident, span: Span) -> Self { pub fn crate_root(span: Span) -> Self { PathSegment { identifier: Ident { ctxt: span.ctxt, ..keywords::CrateRoot.ident() }, - span: span, + span, parameters: None, } } @@ -153,9 +152,20 @@ pub enum PathParameters { Parenthesized(ParenthesizedParameterData), } +impl PathParameters { + pub fn span(&self) -> Span { + match *self { + AngleBracketed(ref data) => data.span, + Parenthesized(ref data) => data.span, + } + } +} + /// A path like `Foo<'a, T>` #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Default)] pub struct AngleBracketedParameterData { + /// Overall span + pub span: Span, /// The lifetime parameters for this path segment. pub lifetimes: Vec, /// The type parameters for this path segment, if present. @@ -168,8 +178,13 @@ pub struct AngleBracketedParameterData { impl Into>> for AngleBracketedParameterData { fn into(self) -> Option> { - let empty = self.lifetimes.is_empty() && self.types.is_empty() && self.bindings.is_empty(); - if empty { None } else { Some(P(PathParameters::AngleBracketed(self))) } + Some(P(PathParameters::AngleBracketed(self))) + } +} + +impl Into>> for ParenthesizedParameterData { + fn into(self) -> Option> { + Some(P(PathParameters::Parenthesized(self))) } } @@ -321,6 +336,7 @@ fn default() -> Generics { where_clause: WhereClause { id: DUMMY_NODE_ID, predicates: Vec::new(), + span: DUMMY_SP, }, span: DUMMY_SP, } @@ -332,6 +348,7 @@ fn default() -> Generics { pub struct WhereClause { pub id: NodeId, pub predicates: Vec, + pub span: Span, } /// A single predicate in a `where` clause @@ -546,8 +563,8 @@ pub enum PatKind { TupleStruct(Path, Vec>, Option), /// A possibly qualified path pattern. - /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants - /// or associated constants. Quailfied path patterns `::B::C`/`::B::C` can + /// Unqualified path patterns `A::B::C` can legally refer to variants, structs, constants + /// or associated constants. Qualified path patterns `::B::C`/`::B::C` can /// only legally refer to associated constants. Path(Option, Path), @@ -718,6 +735,13 @@ pub fn add_trailing_semicolon(mut self) -> Self { }; self } + + pub fn is_item(&self) -> bool { + match self.node { + StmtKind::Local(_) => true, + _ => false, + } + } } impl fmt::Debug for Stmt { @@ -1134,6 +1158,8 @@ pub struct TraitItem { pub attrs: Vec, pub node: TraitItemKind, pub span: Span, + /// See `Item::tokens` for what this is + pub tokens: Option, } #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] @@ -1153,6 +1179,8 @@ pub struct ImplItem { pub attrs: Vec, pub node: ImplItemKind, pub span: Span, + /// See `Item::tokens` for what this is + pub tokens: Option, } #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] @@ -1464,15 +1492,15 @@ pub fn from_self(eself: ExplicitSelf, eself_ident: SpannedIdent) -> Arg { let infer_ty = P(Ty { id: DUMMY_NODE_ID, node: TyKind::ImplicitSelf, - span: span, + span, }); let arg = |mutbl, ty| Arg { pat: P(Pat { id: DUMMY_NODE_ID, node: PatKind::Ident(BindingMode::ByValue(mutbl), eself_ident, None), - span: span, + span, }), - ty: ty, + ty, id: DUMMY_NODE_ID, }; match eself.node { @@ -1481,7 +1509,7 @@ pub fn from_self(eself: ExplicitSelf, eself_ident: SpannedIdent) -> Arg { SelfKind::Region(lt, mutbl) => arg(Mutability::Immutable, P(Ty { id: DUMMY_NODE_ID, node: TyKind::Rptr(lt, MutTy { ty: infer_ty, mutbl: mutbl }), - span: span, + span, })), } } @@ -1710,7 +1738,7 @@ pub fn new(lifetimes: Vec, path: Path, span: Span) -> Self { PolyTraitRef { bound_lifetimes: lifetimes, trait_ref: TraitRef { path: path, ref_id: DUMMY_NODE_ID }, - span: span, + span, } } } @@ -1797,11 +1825,20 @@ pub struct Item { pub node: ItemKind, pub vis: Visibility, pub span: Span, + + /// Original tokens this item was parsed from. This isn't necessarily + /// available for all items, although over time more and more items should + /// have this be `Some`. Right now this is primarily used for procedural + /// macros, notably custom attributes. + /// + /// Note that the tokens here do not include the outer attributes, but will + /// include inner attributes. + pub tokens: Option, } #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum ItemKind { - /// An`extern crate` item, with optional original crate name. + /// An `extern crate` item, with optional original crate name. /// /// E.g. `extern crate foo` or `extern crate foo_bar as foo` ExternCrate(Option),