]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgrust/libproc_macro/ident.rs
Update copyright years.
[thirdparty/gcc.git] / libgrust / libproc_macro / ident.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;
25use Span;
26
27/// An identifier.
28#[derive(Clone)]
29pub struct Ident(pub(crate) bridge::ident::Ident);
30
31impl Ident {
32 /// Creates a new identifier.
33 ///
34 /// # Arguments
35 ///
36 /// * `string` - A valid identifier.
37 /// * `span` - The span of the identifier.
38 ///
39 /// # Panics
40 ///
41 /// The `string` argument must be a valid identifier permitted by the
42 /// language, otherwise the function will panic.
43 pub fn new(string: &str, span: Span) -> Self {
44 Ident(bridge::ident::Ident::new(string, span.0))
45 }
46
47 /// Creates a new raw identifier.
48 ///
49 /// # Arguments
50 ///
51 /// * `string` - A valid identifier.
52 /// * `span` - The span of the identifier.
53 ///
54 /// # Panics
55 ///
56 /// The `string` argument must be a valid identifier permitted by the
57 /// language. Furthermore, it should not be a keyword used in path
58 /// segments, otherwise this function will panic.
59 pub fn new_raw(string: &str, span: Span) -> Self {
60 Ident(bridge::ident::Ident::new_raw(string, span.0))
61 }
62
63 /// Return the span of the identifier
64 pub fn span(&self) -> Span {
65 Span(self.0.span())
66 }
67
68 /// Change the span of the identifier.
69 ///
70 /// # Arguments
71 ///
72 /// * `span` - The new span value.
73 pub fn set_span(&mut self, span: Span) {
74 self.0.set_span(span.0);
75 }
76}
77
78impl fmt::Display for Ident {
79 /// Display as lossless converted string.
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 self.0.fmt(f)
82 }
83}
84
85impl fmt::Debug for Ident {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 self.0.fmt(f)
88 }
89}