]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgrust/libproc_macro/span.rs
Update copyright years.
[thirdparty/gcc.git] / libgrust / libproc_macro / span.rs
CommitLineData
767698ff 1// Copyright (C) 2023-2024 Free Software Foundation, Inc.
0a221966
PEP
2//
3// This file is part of the GNU Proc Macro Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// Under Section 7 of GPL version 3, you are granted additional
15// permissions described in the GCC Runtime Library Exception, version
16// 3.1, as published by the Free Software Foundation.
17
18// You should have received a copy of the GNU General Public License and
19// a copy of the GCC Runtime Library Exception along with this program;
20// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21// <http://www.gnu.org/licenses/>.
22
f7379085
PEP
23use bridge;
24use std::fmt;
25
26/// A region of source code along with macro expansion information.
27#[derive(Copy, Clone)]
28pub struct Span(pub(crate) bridge::span::Span);
29
30impl Span {
31 // TODO: Add experimental API functions for this type
32
33 /// Creates a new span that resolves at the macro call location.
34 pub fn call_site() -> Self {
35 Span(bridge::span::Span::call_site())
36 }
37
38 /// Creates a new span that resolved sometimes at macro call site, and
39 /// sometimes at macro definition site.
40 pub fn mixed_site() -> Self {
41 Span(bridge::span::Span::mixed_site())
42 }
43
44 /// Creates a new span with the same line/column informations but that
45 /// resolve symbols as though it were at `other`.
46 ///
47 /// # Arguments
48 ///
49 /// * `other` - Other span to resolve at.
50 pub fn resolved_at(&self, other: Span) -> Self {
51 Span(self.0.resolved_at(other.0))
52 }
53
54 /// Creates a new span with the same name resolution behavior as self, but
55 /// with the line/column information of `other`.
56 ///
57 /// # Arguments
58 ///
59 /// * `other` - Other span containing the line/column informations to use.
60 pub fn located_at(&self, other: Span) -> Self {
61 Span(self.0.located_at(other.0))
62 }
63
64 /// Return the source text behind a span.
65 pub fn source_text(&self) -> Option<String> {
66 self.0.source_text()
67 }
68}
69
70impl fmt::Debug for Span {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 self.0.fmt(f)
73 }
74}