]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/rust/lex/rust-token.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / rust / lex / rust-token.cc
CommitLineData
83ffe9cd 1// Copyright (C) 2020-2023 Free Software Foundation, Inc.
18f6990f
JP
2
3// This file is part of GCC.
4
5// GCC is free software; you can redistribute it and/or modify it under
6// the terms of the GNU General Public License as published by the Free
7// Software Foundation; either version 3, or (at your option) any later
8// version.
9
10// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13// for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with GCC; see the file COPYING3. If not see
17// <http://www.gnu.org/licenses/>.
18
19#include "rust-token.h"
20#include "rust-diagnostics.h"
21
22namespace Rust {
23// Hackily defined way to get token description for enum value using x-macros
24const char *
25get_token_description (TokenId id)
26{
27 switch (id)
28 {
29#define RS_TOKEN(name, descr) \
30 case name: \
31 return descr;
32#define RS_TOKEN_KEYWORD(x, y) RS_TOKEN (x, y)
33 RS_TOKEN_LIST
34#undef RS_TOKEN_KEYWORD
35#undef RS_TOKEN
36 default:
37 gcc_unreachable ();
38 }
39}
40
41/* Hackily defined way to get token description as a string for enum value using
42 * x-macros */
43const char *
44token_id_to_str (TokenId id)
45{
46 switch (id)
47 {
48#define RS_TOKEN(name, _) \
49 case name: \
50 return #name;
51#define RS_TOKEN_KEYWORD(x, y) RS_TOKEN (x, y)
52 RS_TOKEN_LIST
53#undef RS_TOKEN_KEYWORD
54#undef RS_TOKEN
55 default:
56 gcc_unreachable ();
57 }
58}
59
60const char *
61get_type_hint_string (PrimitiveCoreType type)
62{
63 switch (type)
64 {
65 case CORETYPE_BOOL:
66 return "bool";
67 case CORETYPE_CHAR:
68 return "char";
69 case CORETYPE_STR:
70 return "str";
71 // case CORETYPE_INT:
72 case CORETYPE_ISIZE:
73 return "isize";
74 // case CORETYPE_UINT:
75 case CORETYPE_USIZE:
76 return "usize";
77 case CORETYPE_F32:
78 return "f32";
79 case CORETYPE_F64:
80 return "f64";
81 case CORETYPE_I8:
82 return "i8";
83 case CORETYPE_I16:
84 return "i16";
85 case CORETYPE_I32:
86 return "i32";
87 case CORETYPE_I64:
88 return "i64";
89 case CORETYPE_I128:
90 return "i128";
91 case CORETYPE_U8:
92 return "u8";
93 case CORETYPE_U16:
94 return "u16";
95 case CORETYPE_U32:
96 return "u32";
97 case CORETYPE_U64:
98 return "u64";
99 case CORETYPE_U128:
100 return "u128";
101 case CORETYPE_PURE_DECIMAL:
102 return "pure_decimal";
103 case CORETYPE_UNKNOWN:
104 default:
105 return "unknown";
106 }
107}
108
109const char *
110Token::get_type_hint_str () const
111{
112 return get_type_hint_string (type_hint);
113}
114
115const std::string &
116Token::get_str () const
117{
118 // FIXME: attempt to return null again
119 // gcc_assert(str != NULL);
120
121 // HACK: allow referencing an empty string
122 static const std::string empty = "";
123
124 if (str == NULL)
125 {
126 rust_error_at (get_locus (),
127 "attempted to get string for %<%s%>, which has no string. "
128 "returning empty string instead",
129 get_token_description ());
130 return empty;
131 }
132 return *str;
133}
134} // namespace Rust