LLVM: include/llvm/ADT/Twine.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9#ifndef LLVM_ADT_TWINE_H
10#define LLVM_ADT_TWINE_H
11
16#include
17#include
18#include
19#include <string_view>
20
21namespace llvm {
22
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82class Twine {
83
84 enum NodeKind : unsigned char {
85
86
87 NullKind,
88
89
90 EmptyKind,
91
92
93 TwineKind,
94
95
96 CStringKind,
97
98
99 StdStringKind,
100
101
102
103
104 PtrAndLengthKind,
105
106
107
108 StringLiteralKind,
109
110
111 FormatvObjectKind,
112
113
114 CharKind,
115
116
117 DecUIKind,
118
119
120 DecIKind,
121
122
123 DecULKind,
124
125
126 DecLKind,
127
128
129 DecULLKind,
130
131
132 DecLLKind,
133
134
135 UHexKind
136 };
137
138 union Child {
139 const Twine *twine;
140 const char *cString;
141 const std::string *stdString;
142 struct {
143 const char *ptr;
144 size_t length;
145 } ptrAndLength;
147 char character;
148 unsigned int decUI;
149 int decI;
150 unsigned long decUL;
151 long decL;
152 unsigned long long decULL;
153 long long decLL;
155 };
156
157
158
159 Child LHS;
160
161
162
163 Child RHS;
164
165
166 NodeKind LHSKind = EmptyKind;
167
168
169 NodeKind RHSKind = EmptyKind;
170
171
172 explicit Twine(NodeKind Kind) : LHSKind(Kind) {
173 assert(isNullary() && "Invalid kind!");
174 }
175
176
177 explicit Twine(const Twine &LHS, const Twine &RHS)
178 : LHSKind(TwineKind), RHSKind(TwineKind) {
179 this->LHS.twine = &LHS;
180 this->RHS.twine = &RHS;
181 assert(isValid() && "Invalid twine!");
182 }
183
184
185 explicit Twine(Child LHS, NodeKind LHSKind, Child RHS, NodeKind RHSKind)
186 : LHS(LHS), RHS(RHS), LHSKind(LHSKind), RHSKind(RHSKind) {
187 assert(isValid() && "Invalid twine!");
188 }
189
190
191 bool isNull() const { return getLHSKind() == NullKind; }
192
193
194 bool isEmpty() const { return getLHSKind() == EmptyKind; }
195
196
197 bool isNullary() const { return isNull() || isEmpty(); }
198
199
200 bool isUnary() const { return getRHSKind() == EmptyKind && !isNullary(); }
201
202
203 bool isBinary() const {
204 return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
205 }
206
207
208
209 bool isValid() const {
210
211 if (isNullary() && getRHSKind() != EmptyKind)
212 return false;
213
214
215 if (getRHSKind() == NullKind)
216 return false;
217
218
219 if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
220 return false;
221
222
223 if (getLHSKind() == TwineKind && !LHS.twine->isBinary())
224 return false;
225 if (getRHSKind() == TwineKind && !RHS.twine->isBinary())
226 return false;
227
228 return true;
229 }
230
231
232 NodeKind getLHSKind() const { return LHSKind; }
233
234
235 NodeKind getRHSKind() const { return RHSKind; }
236
237
238 void printOneChild(raw_ostream &OS, Child Ptr, NodeKind Kind) const;
239
240
241 void printOneChildRepr(raw_ostream &OS, Child Ptr, NodeKind Kind) const;
242
243public:
244
245
246
247
248 Twine() { assert(isValid() && "Invalid twine!"); }
249
250 Twine(const Twine &) = default;
251
252
253
254
255
256
258 if (Str[0] != '\0') {
259 LHS.cString = Str;
260 LHSKind = CStringKind;
261 } else {
262 LHSKind = EmptyKind;
263 }
264
265 assert(isValid() && "Invalid twine!");
266 }
267
268
269 Twine(std::nullptr_t) = delete;
270
271
272 Twine(const std::string &Str) : LHSKind(StdStringKind) {
273 LHS.stdString = &Str;
274 assert(isValid() && "Invalid twine!");
275 }
276
277
278
279
280
281 Twine(const std::string_view &Str) : LHSKind(PtrAndLengthKind) {
282 LHS.ptrAndLength.ptr = Str.data();
283 LHS.ptrAndLength.length = Str.length();
284 assert(isValid() && "Invalid twine!");
285 }
286
287
289 LHS.ptrAndLength.ptr = Str.data();
290 LHS.ptrAndLength.length = Str.size();
291 assert(isValid() && "Invalid twine!");
292 }
293
294
296 LHS.ptrAndLength.ptr = Str.data();
297 LHS.ptrAndLength.length = Str.size();
298 assert(isValid() && "Invalid twine!");
299 }
300
301
303 : LHSKind(PtrAndLengthKind) {
304 LHS.ptrAndLength.ptr = Str.data();
305 LHS.ptrAndLength.length = Str.size();
306 assert(isValid() && "Invalid twine!");
307 }
308
309
311 : LHSKind(FormatvObjectKind) {
312 LHS.formatvObject = &Fmt;
313 assert(isValid() && "Invalid twine!");
314 }
315
316
317 explicit Twine(char Val) : LHSKind(CharKind) { LHS.character = Val; }
318
319
320 explicit Twine(signed char Val) : LHSKind(CharKind) {
321 LHS.character = static_cast<char>(Val);
322 }
323
324
325 explicit Twine(unsigned char Val) : LHSKind(CharKind) {
326 LHS.character = static_cast<char>(Val);
327 }
328
329
330 explicit Twine(unsigned Val) : LHSKind(DecUIKind) { LHS.decUI = Val; }
331
332
333 explicit Twine(int Val) : LHSKind(DecIKind) { LHS.decI = Val; }
334
335
336 explicit Twine(unsigned long Val) : LHSKind(DecULKind) { LHS.decUL = Val; }
337
338
339 explicit Twine(long Val) : LHSKind(DecLKind) { LHS.decL = Val; }
340
341
342 explicit Twine(unsigned long long Val) : LHSKind(DecULLKind) {
343 LHS.decULL = Val;
344 }
345
346
347 explicit Twine(long long Val) : LHSKind(DecLLKind) { LHS.decLL = Val; }
348
349
350
351
352
353
354
356 : LHSKind(CStringKind), RHSKind(PtrAndLengthKind) {
357 this->LHS.cString = LHS;
358 this->RHS.ptrAndLength.ptr = RHS.data();
359 this->RHS.ptrAndLength.length = RHS.size();
360 assert(isValid() && "Invalid twine!");
361 }
362
363
365 : LHSKind(PtrAndLengthKind), RHSKind(CStringKind) {
366 this->LHS.ptrAndLength.ptr = LHS.data();
367 this->LHS.ptrAndLength.length = LHS.size();
368 this->RHS.cString = RHS;
369 assert(isValid() && "Invalid twine!");
370 }
371
372
373
375
376
377
379
380
381
382
383
384
386 Child LHS, RHS;
387 LHS.uHex = Val;
388 RHS.twine = nullptr;
389 return Twine(LHS, UHexKind, RHS, EmptyKind);
390 }
391
392
393
394
395
396
397
399
400
402 return isUnary() && getLHSKind() == StringLiteralKind;
403 }
404
405
406
408 if (getRHSKind() != EmptyKind)
409 return false;
410
411 switch (getLHSKind()) {
412 case EmptyKind:
413 case CStringKind:
414 case StdStringKind:
415 case PtrAndLengthKind:
416 case StringLiteralKind:
417 return true;
418 default:
419 return false;
420 }
421 }
422
423
424
425
426
428
429
430
431
432
433
435
436
438
439
440
443 switch (getLHSKind()) {
444 default:
446 case EmptyKind:
448 case CStringKind:
450 case StdStringKind:
451 return StringRef(*LHS.stdString);
452 case PtrAndLengthKind:
453 case StringLiteralKind:
454 return StringRef(LHS.ptrAndLength.ptr, LHS.ptrAndLength.length);
455 }
456 }
457
458
459
460
467
468
469
470
471
472
475
476
477
479
480
482
483#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
484
486
487
489#endif
490
491
492};
493
494
495
496
498
499 if (isNull() || Suffix.isNull())
500 return Twine(NullKind);
501
502
503 if (isEmpty())
504 return Suffix;
505 if (Suffix.isEmpty())
506 return *this;
507
508
509
510 Child NewLHS, NewRHS;
511 NewLHS.twine = this;
512 NewRHS.twine = &Suffix;
513 NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
514 if (isUnary()) {
515 NewLHS = LHS;
516 NewLHSKind = getLHSKind();
517 }
518 if (Suffix.isUnary()) {
519 NewRHS = Suffix.LHS;
520 NewRHSKind = Suffix.getLHSKind();
521 }
522
523 return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
524}
525
528}
529
530
531
532
536
537
538
539
543
545 RHS.print(OS);
546 return OS;
547}
548
549
550
551}
552
553#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
This file defines the SmallVector class.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
pointer data()
Return a pointer to the vector's buffer, even if empty().
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
Twine()
Construct from an empty string.
Definition Twine.h:248
Twine(const formatv_object_base &Fmt)
Construct from a formatv_object_base.
Definition Twine.h:310
Twine & operator=(const Twine &)=delete
Since the intended use of twines is as temporary objects, assignments when concatenating might cause ...
bool isSingleStringRef() const
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStrin...
Definition Twine.h:407
Twine(long Val)
Construct a twine to print Val as a signed decimal integer.
Definition Twine.h:339
Twine(const SmallVectorImpl< char > &Str)
Construct from a SmallString.
Definition Twine.h:302
Twine(StringRef Str)
Construct from a StringRef.
Definition Twine.h:288
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Twine(unsigned long long Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition Twine.h:342
Twine concat(const Twine &Suffix) const
Definition Twine.h:497
Twine(unsigned Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition Twine.h:330
LLVM_ABI void print(raw_ostream &OS) const
Write the concatenated string represented by this twine to the stream OS.
LLVM_ABI StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single null terminated StringRef if it can be represented as such.
Twine(const std::string &Str)
Construct from an std::string.
Definition Twine.h:272
Twine(unsigned char Val)
Construct from an unsigned char.
Definition Twine.h:325
LLVM_DUMP_METHOD void dump() const
Dump the concatenated string represented by this twine to stderr.
Twine(std::nullptr_t)=delete
Delete the implicit conversion from nullptr as Twine(const char *) cannot take nullptr.
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
Definition Twine.h:398
bool isSingleStringLiteral() const
Check if this twine is guaranteed to refer to single string literal.
Definition Twine.h:401
static Twine createNull()
Create a 'null' string, which is an empty string that always concatenates to form another empty strin...
Definition Twine.h:378
LLVM_ABI void printRepr(raw_ostream &OS) const
Write the representation of this twine to the stream OS.
Twine(signed char Val)
Construct from a signed char.
Definition Twine.h:320
Twine(const Twine &)=default
Twine(const std::string_view &Str)
Construct from an std::string_view by converting it to a pointer and length.
Definition Twine.h:281
Twine(const StringLiteral &Str)
Construct from a StringLiteral.
Definition Twine.h:295
Twine(long long Val)
Construct a twine to print Val as a signed decimal integer.
Definition Twine.h:347
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition Twine.h:461
Twine(int Val)
Construct a twine to print Val as a signed decimal integer.
Definition Twine.h:333
Twine(StringRef LHS, const char *RHS)
Construct as the concatenation of a StringRef and a C string.
Definition Twine.h:364
Twine(const char *LHS, StringRef RHS)
Construct as the concatenation of a C string and a StringRef.
Definition Twine.h:355
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
Definition Twine.h:441
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
Twine(unsigned long Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition Twine.h:336
LLVM_ABI void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Twine(char Val)
Construct from a char.
Definition Twine.h:317
LLVM_DUMP_METHOD void dumpRepr() const
Dump the representation of this twine to stderr.
Twine(const char *Str)
Construct from a C string.
Definition Twine.h:257
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
APInt operator+(APInt a, const APInt &b)