| 1 |
// Copyright (C) 2020-2024 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 |
struct Literal |
| 28 |
{ |
| 29 |
public: |
| 30 |
enum LitType |
| 31 |
{ |
| 32 |
CHAR, |
| 33 |
STRING, |
| 34 |
BYTE, |
| 35 |
BYTE_STRING, |
| 36 |
INT, |
| 37 |
FLOAT, |
| 38 |
BOOL |
| 39 |
}; |
| 40 |
|
| 41 |
private: |
| 42 |
std::string value_as_string; |
| 43 |
LitType type; |
| 44 |
PrimitiveCoreType type_hint; |
| 45 |
|
| 46 |
public: |
| 47 |
std::string as_string () const { return value_as_string; } |
| 48 |
|
| 49 |
LitType get_lit_type () const { return type; } |
| 50 |
|
| 51 |
PrimitiveCoreType get_type_hint () const { return type_hint; } |
| 52 |
|
| 53 |
Literal (std::string value_as_string, LitType type, |
| 54 |
PrimitiveCoreType type_hint) |
| 55 |
: value_as_string (std::move (value_as_string)), type (type), |
| 56 |
type_hint (type_hint) |
| 57 |
{} |
| 58 |
|
| 59 |
static Literal create_error () |
| 60 |
{ |
| 61 |
return Literal ("", CHAR, PrimitiveCoreType::CORETYPE_UNKNOWN); |
| 62 |
} |
| 63 |
|
| 64 |
void set_lit_type (LitType lt) { type = lt; } |
| 65 |
|
| 66 |
// Returns whether literal is in an invalid state. |
| 67 |
bool is_error () const { return value_as_string == ""; } |
| 68 |
|
| 69 |
bool is_equal (Literal &other) |
| 70 |
{ |
| 71 |
return value_as_string == other.value_as_string && type == other.type |
| 72 |
&& type_hint == other.type_hint; |
| 73 |
} |
| 74 |
}; |
| 75 |
} // namespace HIR |
| 76 |
} // namespace Rust |
| 77 |
|
| 78 |
#endif |