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
15#include
16#include
17#include
18#include <string_view>
19
20namespace llvm {
21
22 class formatv_object_base;
23 class raw_ostream;
24
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
82
83 enum NodeKind : unsigned char {
84
85
86 NullKind,
87
88
89 EmptyKind,
90
91
92 TwineKind,
93
94
95 CStringKind,
96
97
98 StdStringKind,
99
100
101
102
103 PtrAndLengthKind,
104
105
106
107 StringLiteralKind,
108
109
110 FormatvObjectKind,
111
112
113 CharKind,
114
115
116 DecUIKind,
117
118
119 DecIKind,
120
121
122
123 DecULKind,
124
125
126 DecLKind,
127
128
129
130 DecULLKind,
131
132
133 DecLLKind,
134
135
136
137 UHexKind
138 };
139
140 union Child
141 {
142 const Twine *twine;
143 const char *cString;
144 const std::string *stdString;
145 struct {
146 const char *ptr;
147 size_t length;
148 } ptrAndLength;
150 char character;
151 unsigned int decUI;
152 int decI;
153 const unsigned long *decUL;
154 const long *decL;
155 const unsigned long long *decULL;
156 const long long *decLL;
158 };
159
160
161
162 Child LHS;
163
164
165
166 Child RHS;
167
168
169 NodeKind LHSKind = EmptyKind;
170
171
172 NodeKind RHSKind = EmptyKind;
173
174
176 assert(isNullary() && "Invalid kind!");
177 }
178
179
181 : LHSKind(TwineKind), RHSKind(TwineKind) {
182 this->LHS.twine = &LHS;
183 this->RHS.twine = &RHS;
184 assert(isValid() && "Invalid twine!");
185 }
186
187
189 : LHS(LHS), RHS(RHS), LHSKind(LHSKind), RHSKind(RHSKind) {
190 assert(isValid() && "Invalid twine!");
191 }
192
193
194 bool isNull() const {
195 return getLHSKind() == NullKind;
196 }
197
198
199 bool isEmpty() const {
200 return getLHSKind() == EmptyKind;
201 }
202
203
204 bool isNullary() const {
205 return isNull() || isEmpty();
206 }
207
208
209 bool isUnary() const {
210 return getRHSKind() == EmptyKind && !isNullary();
211 }
212
213
214 bool isBinary() const {
215 return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
216 }
217
218
219
220 bool isValid() const {
221
222 if (isNullary() && getRHSKind() != EmptyKind)
223 return false;
224
225
226 if (getRHSKind() == NullKind)
227 return false;
228
229
230 if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
231 return false;
232
233
234 if (getLHSKind() == TwineKind &&
235 .twine->isBinary())
236 return false;
237 if (getRHSKind() == TwineKind &&
238 .twine->isBinary())
239 return false;
240
241 return true;
242 }
243
244
245 NodeKind getLHSKind() const { return LHSKind; }
246
247
248 NodeKind getRHSKind() const { return RHSKind; }
249
250
252
253
256
257 public:
258
259
260
261
263 assert(isValid() && "Invalid twine!");
264 }
265
267
268
269
270
271
272
274 if (Str[0] != '\0') {
275 LHS.cString = Str;
276 LHSKind = CStringKind;
277 } else
278 LHSKind = EmptyKind;
279
280 assert(isValid() && "Invalid twine!");
281 }
282
283
284 Twine(std::nullptr_t) = delete;
285
286
287 Twine(const std::string &Str) : LHSKind(StdStringKind) {
288 LHS.stdString = &Str;
289 assert(isValid() && "Invalid twine!");
290 }
291
292
293
294
295
296 Twine(const std::string_view &Str)
297 : LHSKind(PtrAndLengthKind) {
298 LHS.ptrAndLength.ptr = Str.data();
299 LHS.ptrAndLength.length = Str.length();
300 assert(isValid() && "Invalid twine!");
301 }
302
303
304 Twine(const StringRef &Str) : LHSKind(PtrAndLengthKind) {
305 LHS.ptrAndLength.ptr = Str.data();
306 LHS.ptrAndLength.length = Str.size();
307 assert(isValid() && "Invalid twine!");
308 }
309
310
312 : LHSKind(StringLiteralKind) {
313 LHS.ptrAndLength.ptr = Str.data();
314 LHS.ptrAndLength.length = Str.size();
315 assert(isValid() && "Invalid twine!");
316 }
317
318
320 : LHSKind(PtrAndLengthKind) {
321 LHS.ptrAndLength.ptr = Str.data();
322 LHS.ptrAndLength.length = Str.size();
323 assert(isValid() && "Invalid twine!");
324 }
325
326
328 : LHSKind(FormatvObjectKind) {
329 LHS.formatvObject = &Fmt;
330 assert(isValid() && "Invalid twine!");
331 }
332
333
334 explicit Twine(char Val) : LHSKind(CharKind) {
335 LHS.character = Val;
336 }
337
338
339 explicit Twine(signed char Val) : LHSKind(CharKind) {
340 LHS.character = static_cast<char>(Val);
341 }
342
343
344 explicit Twine(unsigned char Val) : LHSKind(CharKind) {
345 LHS.character = static_cast<char>(Val);
346 }
347
348
349 explicit Twine(unsigned Val) : LHSKind(DecUIKind) {
350 LHS.decUI = Val;
351 }
352
353
354 explicit Twine(int Val) : LHSKind(DecIKind) {
355 LHS.decI = Val;
356 }
357
358
359 explicit Twine(const unsigned long &Val) : LHSKind(DecULKind) {
360 LHS.decUL = &Val;
361 }
362
363
364 explicit Twine(const long &Val) : LHSKind(DecLKind) {
365 LHS.decL = &Val;
366 }
367
368
369 explicit Twine(const unsigned long long &Val) : LHSKind(DecULLKind) {
370 LHS.decULL = &Val;
371 }
372
373
374 explicit Twine(const long long &Val) : LHSKind(DecLLKind) {
375 LHS.decLL = &Val;
376 }
377
378
379
380
381
382
383
385 : LHSKind(CStringKind), RHSKind(PtrAndLengthKind) {
386 this->LHS.cString = LHS;
387 this->RHS.ptrAndLength.ptr = RHS.data();
388 this->RHS.ptrAndLength.length = RHS.size();
389 assert(isValid() && "Invalid twine!");
390 }
391
392
394 : LHSKind(PtrAndLengthKind), RHSKind(CStringKind) {
395 this->LHS.ptrAndLength.ptr = LHS.data();
396 this->LHS.ptrAndLength.length = LHS.size();
397 this->RHS.cString = RHS;
398 assert(isValid() && "Invalid twine!");
399 }
400
401
402
404
405
406
408 return Twine(NullKind);
409 }
410
411
412
413
414
415
418 LHS.uHex = &Val;
419 RHS.twine = nullptr;
420 return Twine(LHS, UHexKind, RHS, EmptyKind);
421 }
422
423
424
425
426
427
428
430 return isNullary();
431 }
432
433
435 return isUnary() && getLHSKind() == StringLiteralKind;
436 }
437
438
439
441 if (getRHSKind() != EmptyKind) return false;
442
443 switch (getLHSKind()) {
444 case EmptyKind:
445 case CStringKind:
446 case StdStringKind:
447 case PtrAndLengthKind:
448 case StringLiteralKind:
449 return true;
450 default:
451 return false;
452 }
453 }
454
455
456
457
458
460
461
462
463
464
465
466 std::string str() const;
467
468
470
471
472
475 switch (getLHSKind()) {
476 default: llvm_unreachable("Out of sync with isSingleStringRef");
477 case EmptyKind:
479 case CStringKind:
481 case StdStringKind:
483 case PtrAndLengthKind:
484 case StringLiteralKind:
485 return StringRef(LHS.ptrAndLength.ptr, LHS.ptrAndLength.length);
486 }
487 }
488
489
490
491
497 }
498
499
500
501
502
503
505
506
507
509
510
511 void dump() const;
512
513
515
516
518
519
520 };
521
522
523
524
526
527 if (isNull() || Suffix.isNull())
528 return Twine(NullKind);
529
530
531 if (isEmpty())
532 return Suffix;
533 if (Suffix.isEmpty())
534 return *this;
535
536
537
538 Child NewLHS, NewRHS;
539 NewLHS.twine = this;
540 NewRHS.twine = &Suffix;
541 NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
542 if (isUnary()) {
543 NewLHS = LHS;
544 NewLHSKind = getLHSKind();
545 }
546 if (Suffix.isUnary()) {
547 NewRHS = Suffix.LHS;
548 NewRHSKind = Suffix.getLHSKind();
549 }
550
551 return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
552 }
553
556 }
557
558
559
560
563 }
564
565
566
567
570 }
571
574 return OS;
575 }
576
577
578
579}
580
581#endif
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Twine()
Construct from an empty string.
Twine(const formatv_object_base &Fmt)
Construct from a formatv_object_base.
Twine & operator=(const Twine &)=delete
Since the intended use of twines is as temporary objects, assignments when concatenating might cause ...
Twine(const long long &Val)
Construct a twine to print Val as a signed decimal integer.
bool isSingleStringRef() const
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStrin...
Twine(const unsigned long &Val)
Construct a twine to print Val as an unsigned decimal integer.
Twine(const SmallVectorImpl< char > &Str)
Construct from a SmallString.
std::string str() const
Return the twine contents as a std::string.
Twine concat(const Twine &Suffix) const
Twine(unsigned Val)
Construct a twine to print Val as an unsigned decimal integer.
void print(raw_ostream &OS) const
Write the concatenated string represented by this twine to the stream OS.
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.
Twine(unsigned char Val)
Construct from an unsigned char.
void dump() const
Dump the concatenated string represented by this twine to stderr.
Twine(const char *LHS, const StringRef &RHS)
Construct as the concatenation of a C string and a StringRef.
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...
bool isSingleStringLiteral() const
Check if this twine is guaranteed to refer to single string literal.
static Twine createNull()
Create a 'null' string, which is an empty string that always concatenates to form another empty strin...
Twine(const long &Val)
Construct a twine to print Val as a signed decimal integer.
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.
Twine(const Twine &)=default
Twine(const std::string_view &Str)
Construct from an std::string_view by converting it to a pointer and length.
Twine(const StringLiteral &Str)
Construct from a StringLiteral.
Twine(const StringRef &LHS, const char *RHS)
Construct as the concatenation of a StringRef and a C string.
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
static Twine utohexstr(const uint64_t &Val)
Twine(int Val)
Construct a twine to print Val as a signed decimal integer.
Twine(const StringRef &Str)
Construct from a StringRef.
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Twine(char Val)
Construct from a char.
void dumpRepr() const
Dump the representation of this twine to stderr.
Twine(const unsigned long long &Val)
Construct a twine to print Val as an unsigned decimal integer.
Twine(const char *Str)
Construct from a C string.
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
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)
Determine the kind of a node from its type.