Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
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 : #ifndef RUST_HIR_LITERAL_H
20 : #define RUST_HIR_LITERAL_H
21 :
22 : #include "rust-token.h"
23 :
24 : namespace Rust {
25 : namespace HIR {
26 : // A literal - value with a type. Used in LiteralExpr and LiteralPattern.
27 48053 : struct Literal
28 : {
29 : public:
30 : enum LitType
31 : {
32 : CHAR,
33 : STRING,
34 : BYTE,
35 : BYTE_STRING,
36 : C_STRING,
37 : INT,
38 : FLOAT,
39 : BOOL
40 : };
41 :
42 : private:
43 : std::string value_as_string;
44 : LitType type;
45 : PrimitiveCoreType type_hint;
46 :
47 : public:
48 70340 : std::string as_string () const { return value_as_string; }
49 :
50 70877 : LitType get_lit_type () const { return type; }
51 :
52 16744 : PrimitiveCoreType get_type_hint () const { return type_hint; }
53 :
54 22434 : Literal (std::string value_as_string, LitType type,
55 : PrimitiveCoreType type_hint)
56 22434 : : value_as_string (std::move (value_as_string)), type (type),
57 22434 : type_hint (type_hint)
58 : {}
59 :
60 : static Literal create_error ()
61 : {
62 : return Literal ("", CHAR, PrimitiveCoreType::CORETYPE_UNKNOWN);
63 : }
64 :
65 671 : void set_lit_type (LitType lt) { type = lt; }
66 :
67 : // Returns whether literal is in an invalid state.
68 : bool is_error () const { return value_as_string == ""; }
69 :
70 : bool is_equal (Literal &other)
71 : {
72 : return value_as_string == other.value_as_string && type == other.type
73 : && type_hint == other.type_hint;
74 : }
75 : };
76 : } // namespace HIR
77 : } // namespace Rust
78 :
79 : #endif
|