1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use syntax::source_map::Span;
15 use utils::{mk_sp, outer_attributes};
19 /// Spanned returns a span including attributes, if available.
21 fn span(&self) -> Span;
24 macro_rules! span_with_attrs_lo_hi {
25 ($this:ident, $lo:expr, $hi:expr) => {{
26 let attrs = outer_attributes(&$this.attrs);
30 mk_sp(attrs[0].span.lo(), $hi)
35 macro_rules! span_with_attrs {
37 span_with_attrs_lo_hi!($this, $this.span.lo(), $this.span.hi())
41 macro_rules! implement_spanned {
43 impl Spanned for $this {
44 fn span(&self) -> Span {
45 span_with_attrs!(self)
51 // Implement `Spanned` for structs with `attrs` field.
52 implement_spanned!(ast::Expr);
53 implement_spanned!(ast::Field);
54 implement_spanned!(ast::ForeignItem);
55 implement_spanned!(ast::Item);
56 implement_spanned!(ast::Local);
57 implement_spanned!(ast::TraitItem);
58 implement_spanned!(ast::ImplItem);
60 impl Spanned for ast::Stmt {
61 fn span(&self) -> Span {
63 ast::StmtKind::Local(ref local) => mk_sp(local.span().lo(), self.span.hi()),
64 ast::StmtKind::Item(ref item) => mk_sp(item.span().lo(), self.span.hi()),
65 ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
66 mk_sp(expr.span().lo(), self.span.hi())
68 ast::StmtKind::Mac(ref mac) => {
69 let (_, _, ref attrs) = **mac;
73 mk_sp(attrs[0].span.lo(), self.span.hi())
80 impl Spanned for ast::Pat {
81 fn span(&self) -> Span {
86 impl Spanned for ast::Ty {
87 fn span(&self) -> Span {
92 impl Spanned for ast::Arm {
93 fn span(&self) -> Span {
94 let lo = if self.attrs.is_empty() {
95 self.pats[0].span.lo()
97 self.attrs[0].span.lo()
99 span_with_attrs_lo_hi!(self, lo, self.body.span.hi())
103 impl Spanned for ast::Arg {
104 fn span(&self) -> Span {
105 if ::items::is_named_arg(self) {
106 mk_sp(self.pat.span.lo(), self.ty.span.hi())
113 impl Spanned for ast::GenericParam {
114 fn span(&self) -> Span {
115 let lo = if self.attrs.is_empty() {
118 self.attrs[0].span.lo()
120 let hi = if self.bounds.is_empty() {
123 self.bounds.last().unwrap().span().hi()
125 let ty_hi = if let ast::GenericParamKind::Type {
126 default: Some(ref ty),
133 mk_sp(lo, max(hi, ty_hi))
137 impl Spanned for ast::StructField {
138 fn span(&self) -> Span {
139 span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi())
143 impl Spanned for ast::WherePredicate {
144 fn span(&self) -> Span {
146 ast::WherePredicate::BoundPredicate(ref p) => p.span,
147 ast::WherePredicate::RegionPredicate(ref p) => p.span,
148 ast::WherePredicate::EqPredicate(ref p) => p.span,
153 impl Spanned for ast::FunctionRetTy {
154 fn span(&self) -> Span {
156 ast::FunctionRetTy::Default(span) => span,
157 ast::FunctionRetTy::Ty(ref ty) => ty.span,
162 impl Spanned for ast::GenericArg {
163 fn span(&self) -> Span {
165 ast::GenericArg::Lifetime(ref lt) => lt.ident.span,
166 ast::GenericArg::Type(ref ty) => ty.span(),
171 impl Spanned for ast::GenericBound {
172 fn span(&self) -> Span {
174 ast::GenericBound::Trait(ref ptr, _) => ptr.span,
175 ast::GenericBound::Outlives(ref l) => l.ident.span,
180 impl Spanned for MacroArg {
181 fn span(&self) -> Span {
183 MacroArg::Expr(ref expr) => expr.span(),
184 MacroArg::Ty(ref ty) => ty.span(),
185 MacroArg::Pat(ref pat) => pat.span(),
186 MacroArg::Item(ref item) => item.span(),