Debian Codegraph @

libgrust/libproc_macro_internal/literal.h

raw
1 // Copyright (C) 2023-2024 Free Software Foundation, Inc.
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
23 #ifndef LITERAL_H
24 #define LITERAL_H
25
26 #include <cstdint>
27 #include <string>
28 #include <vector>
29 #include "span.h"
30 #include "ffistring.h"
31
32 namespace ProcMacro {
33
34 enum LitKindTag
35 {
36 BYTE,
37 CHAR,
38 INTEGER,
39 FLOAT,
40 STR,
41 STR_RAW,
42 BYTE_STR,
43 BYTE_STR_RAW,
44 };
45
46 union LitKindPayload
47 {
48 std::uint8_t str_raw;
49 std::uint8_t byte_str_raw;
50 };
51
52 struct LitKind
53 {
54 LitKindTag tag;
55 LitKindPayload payload;
56
57 private:
58 public:
59 static LitKind make_byte ();
60 static LitKind make_char ();
61 static LitKind make_integer ();
62 static LitKind make_float ();
63 static LitKind make_str ();
64 static LitKind make_str_raw (std::uint8_t val);
65 static LitKind make_byte_str ();
66 static LitKind make_byte_str_raw (std::uint8_t val);
67 };
68
69 struct Literal
70 {
71 LitKind kind;
72 FFIString text;
73 FFIString suffix;
74 Span span;
75
76 public:
77 Literal clone () const;
78 bool has_suffix () const { return suffix.len != 0; };
79
80 static Literal make_literal (const std::string &text, bool &has_error);
81 static Literal make_literal (const LitKind kind, Span span,
82 const std::string &text,
83 const std::string &suffix = "");
84 static Literal make_u8 (std::uint8_t value, bool suffixed = true);
85 static Literal make_u16 (std::uint16_t value, bool suffixed = true);
86 static Literal make_u32 (std::uint32_t value, bool suffixed = true);
87 static Literal make_u64 (std::uint64_t value, bool suffixed = true);
88
89 static Literal make_i8 (std::int8_t value, bool suffixed = true);
90 static Literal make_i16 (std::int16_t value, bool suffixed = true);
91 static Literal make_i32 (std::int32_t value, bool suffixed = true);
92 static Literal make_i64 (std::int64_t value, bool suffixed = true);
93
94 static Literal make_string (const std::string &str);
95 static Literal make_byte_string (const std::vector<std::uint8_t> &vec);
96
97 static Literal make_f32 (float value, bool suffixed = false);
98 static Literal make_f64 (double value, bool suffixed = false);
99
100 static Literal make_char (std::uint32_t ch);
101 static Literal make_usize (std::uint64_t value, bool suffixed = true);
102 static Literal make_isize (std::int64_t value, bool suffixed = true);
103
104 static void drop (Literal *lit);
105 };
106
107 extern "C" {
108 bool
109 Literal__from_string (FFIString str, Literal *lit);
110 }
111 } // namespace ProcMacro
112
113 #endif /* ! LITERAL_H */