FLANG
character-value.h
1//===-- include/flang/Evaluate/character-value.h ----------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_EVALUATE_CHARACTER_VALUE_H_
10#define FORTRAN_EVALUATE_CHARACTER_VALUE_H_
11
12#include "flang/Evaluate/common.h"
13#include "flang/Evaluate/object-sizes.h"
14#include "flang/Evaluate/type.h"
15#include "llvm/Support/Compiler.h"
16#include "llvm/Support/raw_ostream.h"
17#include <cstddef>
18#include <iosfwd>
19#include <optional>
20#include <string>
21
22namespace Fortran::evaluate::value {
24
34class CharacterValue {
35public:
36 // rule-of-five
37 ~CharacterValue();
38 CharacterValue(const CharacterValue &);
39 CharacterValue(CharacterValue &&);
40 CharacterValue &operator=(const CharacterValue &);
41 CharacterValue &operator=(CharacterValue &&);
42
43 // ctors
44
49
50 explicit CharacterValue(int kind, std::string s);
51 explicit CharacterValue(int kind, std::u16string s);
52 explicit CharacterValue(int kind, std::u32string s);
53
55 CharacterValue(int kind, std::size_t n, char32_t c);
56
57 // Named ctors
58 static CharacterValue Zero(int kind);
59
60 static CharacterValue FromRawBytes(
61 int kind, const void *raw, size_t byteSize);
62
63 void print(llvm::raw_ostream &os) const;
64
65#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
66 LLVM_DUMP_METHOD void dump() const;
67#endif
68
71 bool IsNull() const;
72
74 int kind() const;
75
76 bool empty() const;
77 std::size_t size() const;
78 std::size_t length() const { return size(); }
79
81 std::size_t charSize() const { return kind(); }
82
84 size_t bytesStored() const { return length() * charSize(); }
85
86 // Casting to other representations
87 std::optional<llvm::StringRef> AsStringRef() const;
88 std::optional<std::string> AsStdString() const {
89 if (auto str{AsStringRef()}) {
90 return str->str();
91 }
92 return std::nullopt;
93 }
94 std::optional<std::u16string> AsU16String() const;
95 std::optional<std::u32string> AsU32String() const;
96
98 std::string ToStdString() const;
99
100 template <typename CharT, typename = std::void_t<std::basic_string<CharT>>>
101 std::optional<std::basic_string<CharT>> AsBasicString() const {
102 if constexpr (std::is_same_v<char, CharT>) {
103 return AsStdString();
104 } else if constexpr (std::is_same_v<char16_t, CharT>) {
105 return AsU16String();
106 } else if constexpr (std::is_same_v<char32_t, CharT>) {
107 return AsU32String();
108 } else {
109 static_assert(
110 !sizeof(CharT), "Must be one of the supported character types");
111 }
112 }
113
114 // Comparisons
115 Ordering Compare(const CharacterValue &y) const;
116 bool operator<(const CharacterValue &y) const;
117 bool operator<=(const CharacterValue &y) const { return !(y < *this); }
118 bool operator==(const CharacterValue &y) const;
119 bool operator!=(const CharacterValue &y) const { return !(*this == y); }
120 bool operator>=(const CharacterValue &y) const { return !(*this < y); }
121 bool operator>(const CharacterValue &y) const { return y < *this; }
122
125 std::optional<CharacterValue> ToAscii(int kind) const;
126
129 void assign(int kind, std::size_t n, char32_t c);
130
132 void assign(const char *p, std::size_t n);
133 void assign(const char16_t *p, std::size_t n);
134 void assign(const char32_t *p, std::size_t n);
135
137 void erase(std::size_t pos);
138
140 void append(std::size_t n, char32_t c);
141
143 CharacterValue &replace(
144 std::size_t pos, std::size_t len, const CharacterValue &other);
145
147 CharacterValue substr(std::size_t pos) const;
148
150 CharacterValue substr(std::size_t pos, std::size_t len) const;
151
153 void reserve(std::size_t n);
154
156 char32_t operator[](std::size_t i) const;
157
159 CharacterValue operator+(const CharacterValue &y) const;
160
162 CharacterValue &operator+=(const CharacterValue &y);
163
165 CharacterValue &operator+=(char32_t c);
166
168 static constexpr std::size_t npos{std::string::npos};
169
170 // Find-family methods; return npos when not found.
171 std::size_t find(const CharacterValue &pattern) const;
172 std::size_t rfind(const CharacterValue &pattern) const;
173 std::size_t find_first_of(const CharacterValue &set) const;
174 std::size_t find_last_of(const CharacterValue &set) const;
175 std::size_t find_first_not_of(char32_t c) const;
176 std::size_t find_last_not_of(char32_t c) const;
177 std::size_t find_first_not_of(const CharacterValue &set) const;
178 std::size_t find_last_not_of(const CharacterValue &set) const;
179
181 void *data();
182 const void *data() const;
183
185 char *charData() { return static_cast<char *>(data()); }
186 const char *charData() const { return static_cast<const char *>(data()); }
187
188 void *at(size_t pos) { return &charData()[pos * charSize()]; }
189 const void *at(size_t pos) const { return &charData()[pos * charSize()]; }
190
197 void StoreRawBytes(void *dst, size_t size, bool *changed = nullptr) const;
198
199 template <typename F>
200 static auto withCharProto(int kind, F &&f)
201 -> decltype(std::declval<F>()(std::declval<char>())) {
202 switch (kind) {
203 case 1:
204 return f(char{});
205 case 2:
206 return f(char16_t{});
207 case 4:
208 return f(char32_t{});
209 default:
210 DIE("unsupported character kind/null state");
211 }
212 }
213
214 template <typename F> decltype(auto) withStdString(F &&f) const {
215 switch (kind()) {
216 case 1:
217 return f(*AsStdString());
218 case 2:
219 return f(*AsU16String());
220 case 4:
221 return f(*AsU32String());
222 default:
223 DIE("unsupported kind/null state");
224 }
225 }
226
227private:
228 static CharacterValue FromImpl(const CharacterValueImpl &y);
229 static CharacterValue FromImpl(CharacterValueImpl &&y);
230
231 CharacterValueImpl &impl() {
232 return *reinterpret_cast<CharacterValueImpl *>(this->opaque_);
233 }
234 const CharacterValueImpl &impl() const {
235 return *reinterpret_cast<const CharacterValueImpl *>(this->opaque_);
236 }
237
238 alignas(
239 detail::kCharacterObjectAlign) char opaque_[detail::kCharacterObjectSize];
240};
241
242} // namespace Fortran::evaluate::value
243
244namespace llvm {
246inline raw_ostream &operator<<(
247 raw_ostream &os, const Fortran::evaluate::value::CharacterValue &v) {
248 v.print(os);
249 return os;
250}
251} // namespace llvm
252
253#endif // FORTRAN_EVALUATE_CHARACTER_VALUE_H_
Definition character-value-impl.h:22
Definition character-value.h:34
int kind() const
The kind of the value currently stored.
Definition character-value.cpp:83
std::string ToStdString() const
Force conversion to Ascii even if this means loss of information.
Definition character-value.cpp:97
void * data()
Raw byte pointer to the underlying character data.
Definition character-value.cpp:204
char * charData()
Like data(), but pre-casted to char.
Definition character-value.h:185
CharacterValue & operator+=(const CharacterValue &y)
Append another same-kind string.
Definition character-value.cpp:162
void append(std::size_t n, char32_t c)
Append n copies of the given character (widened to the stored type).
Definition character-value.cpp:136
CharacterValue()
Definition character-value.cpp:22
size_t bytesStored() const
Number of bytes accessed by FromRawBytes/StoreRawBytes.
Definition character-value.h:84
char32_t operator[](std::size_t i) const
Return the character at position i.
Definition character-value.cpp:154
std::size_t charSize() const
Byte size of one character unit (1, 2, or 4).
Definition character-value.h:81
void reserve(std::size_t n)
Reserve storage for at least n characters.
Definition character-value.cpp:152
CharacterValue operator+(const CharacterValue &y) const
Concatenate two same-kind strings.
Definition character-value.cpp:158
void assign(int kind, std::size_t n, char32_t c)
Definition character-value.cpp:118
static constexpr std::size_t npos
Sentinel value for "not found" positions (same as std::string::npos).
Definition character-value.h:168
CharacterValue substr(std::size_t pos) const
Return a suffix starting at pos.
Definition character-value.cpp:144
void erase(std::size_t pos)
Erase from position pos to end.
Definition character-value.cpp:134
bool IsNull() const
Definition character-value.cpp:77
CharacterValue & replace(std::size_t pos, std::size_t len, const CharacterValue &other)
Replace the substring [pos, pos+len) with characters from other.
Definition character-value.cpp:138
std::optional< CharacterValue > ToAscii(int kind) const
Definition character-value.cpp:111
void StoreRawBytes(void *dst, size_t size, bool *changed=nullptr) const
Definition character-value.cpp:207