10 #ifndef LLVM_ADT_TWINE_H 11 #define LLVM_ADT_TWINE_H 13 #include "llvm/SmallVector.h" 14 #include "llvm/StringRef.h" 81 enum NodeKind :
unsigned char {
136 const std::string *stdString;
142 const unsigned long *decUL;
144 const unsigned long long *decULL;
145 const long long *decLL;
146 const uint64_t *uHex;
163 explicit Twine(NodeKind Kind)
164 : LHSKind(Kind), RHSKind(EmptyKind) {
165 assert(isNullary() &&
"Invalid kind!");
170 : LHSKind(TwineKind), RHSKind(TwineKind) {
171 this->LHS.twine = &LHS;
172 this->RHS.twine = &RHS;
173 assert(isValid() &&
"Invalid twine!");
177 explicit Twine(Child LHS, NodeKind LHSKind, Child RHS, NodeKind RHSKind)
178 : LHS(LHS), RHS(RHS), LHSKind(LHSKind), RHSKind(RHSKind) {
179 assert(isValid() &&
"Invalid twine!");
184 Twine &operator=(
const Twine &Other) =
delete;
187 bool isNull()
const {
188 return getLHSKind() == NullKind;
192 bool isEmpty()
const {
193 return getLHSKind() == EmptyKind;
197 bool isNullary()
const {
198 return isNull() || isEmpty();
202 bool isUnary()
const {
203 return getRHSKind() == EmptyKind && !isNullary();
207 bool isBinary()
const {
208 return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
213 bool isValid()
const {
215 if (isNullary() && getRHSKind() != EmptyKind)
219 if (getRHSKind() == NullKind)
223 if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
227 if (getLHSKind() == TwineKind &&
228 !LHS.twine->isBinary())
230 if (getRHSKind() == TwineKind &&
231 !RHS.twine->isBinary())
238 NodeKind getLHSKind()
const {
return LHSKind; }
241 NodeKind getRHSKind()
const {
return RHSKind; }
244 void printOneChild(
raw_ostream &OS, Child Ptr, NodeKind Kind)
const;
248 NodeKind Kind)
const;
255 Twine() : LHSKind(EmptyKind), RHSKind(EmptyKind) {
256 assert(isValid() &&
"Invalid twine!");
267 : RHSKind(EmptyKind) {
268 if (Str[0] !=
'\0') {
270 LHSKind = CStringKind;
274 assert(isValid() &&
"Invalid twine!");
279 : LHSKind(StdStringKind), RHSKind(EmptyKind) {
280 LHS.stdString = &Str;
281 assert(isValid() &&
"Invalid twine!");
286 : LHSKind(StringRefKind), RHSKind(EmptyKind) {
287 LHS.stringRef = &Str;
288 assert(isValid() &&
"Invalid twine!");
293 : LHSKind(SmallStringKind), RHSKind(EmptyKind) {
294 LHS.smallString = &Str;
295 assert(isValid() &&
"Invalid twine!");
300 : LHSKind(CharKind), RHSKind(EmptyKind) {
306 : LHSKind(CharKind), RHSKind(EmptyKind) {
307 LHS.character =
static_cast<char>(Val);
312 : LHSKind(CharKind), RHSKind(EmptyKind) {
313 LHS.character =
static_cast<char>(Val);
318 : LHSKind(DecUIKind), RHSKind(EmptyKind) {
324 : LHSKind(DecIKind), RHSKind(EmptyKind) {
329 explicit Twine(
const unsigned long &Val)
330 : LHSKind(DecULKind), RHSKind(EmptyKind) {
336 : LHSKind(DecLKind), RHSKind(EmptyKind) {
341 explicit Twine(
const unsigned long long &Val)
342 : LHSKind(DecULLKind), RHSKind(EmptyKind) {
347 explicit Twine(
const long long &Val)
348 : LHSKind(DecLLKind), RHSKind(EmptyKind) {
359 : LHSKind(CStringKind), RHSKind(StringRefKind) {
360 this->LHS.cString = LHS;
361 this->RHS.stringRef = &RHS;
362 assert(isValid() &&
"Invalid twine!");
367 : LHSKind(StringRefKind), RHSKind(CStringKind) {
368 this->LHS.stringRef = &LHS;
369 this->RHS.cString = RHS;
370 assert(isValid() &&
"Invalid twine!");
376 return Twine(NullKind);
384 static Twine utohexstr(
const uint64_t &Val) {
388 return Twine(LHS, UHexKind, RHS, EmptyKind);
404 if (getRHSKind() != EmptyKind)
return false;
406 switch (getLHSKind()) {
411 case SmallStringKind:
429 std::string
str()
const;
438 switch (getLHSKind()) {
443 case CStringKind:
return StringRef(LHS.cString);
444 case StdStringKind:
return StringRef(*LHS.stdString);
445 case StringRefKind:
return *LHS.stringRef;
446 case SmallStringKind:
447 return StringRef(LHS.smallString->data(), LHS.smallString->size());
487 inline Twine Twine::concat(
const Twine &Suffix)
const {
489 if (isNull() || Suffix.isNull())
490 return Twine(NullKind);
495 if (Suffix.isEmpty())
500 Child NewLHS, NewRHS;
502 NewRHS.twine = &Suffix;
503 NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
506 NewLHSKind = getLHSKind();
508 if (Suffix.isUnary()) {
510 NewRHSKind = Suffix.getLHSKind();
513 return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
517 return LHS.concat(RHS);
524 return Twine(LHS, RHS);
531 return Twine(LHS, RHS);
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:25
Twine(signed char Val)
Construct from a signed char.
Definition: Twine.h:305
Twine(const char *Str)
Construct from a C string.
Definition: Twine.h:266
bool isSingleStringRef() const
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStrin...
Definition: Twine.h:403
Twine(const unsigned long long &Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition: Twine.h:341
Twine(const StringRef &LHS, const char *RHS)
Construct as the concatenation of a StringRef and a C string.
Definition: Twine.h:366
Twine(const SmallVectorImpl< char > &Str)
Construct from a SmallString.
Definition: Twine.h:292
void dump() const
Dump the concatenated string represented by this twine to stderr.
Definition: Twine.cpp:163
void dumpRepr() const
Dump the representation of this twine to stderr.
Definition: Twine.cpp:167
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:15
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
Definition: Twine.h:436
Twine(char Val)
Construct from a char.
Definition: Twine.h:299
Twine()
Construct from an empty string.
Definition: Twine.h:255
Twine(const long long &Val)
Construct a twine to print Val as a signed decimal integer.
Definition: Twine.h:347
void printRepr(raw_ostream &OS) const
Write the representation of this twine to the stream OS.
Definition: Twine.cpp:155
static Twine createNull()
Create a 'null' string, which is an empty string that always concatenates to form another empty strin...
Definition: Twine.h:375
Twine(const StringRef &Str)
Construct from a StringRef.
Definition: Twine.h:285
Twine(unsigned char Val)
Construct from an unsigned char.
Definition: Twine.h:311
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:454
Twine(const std::string &Str)
Construct from an std::string.
Definition: Twine.h:278
Twine(int Val)
Construct a twine to print Val as a signed decimal integer.
Definition: Twine.h:323
Twine(unsigned Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition: Twine.h:317
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:134
Twine(const long &Val)
Construct a twine to print Val as a signed decimal integer.
Definition: Twine.h:335
Twine(const char *LHS, const StringRef &RHS)
Construct as the concatenation of a C string and a StringRef.
Definition: Twine.h:358
StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single null terminated StringRef if it can be represented as such...
Definition: Twine.cpp:30
void print(raw_ostream &OS) const
Write the concatenated string represented by this twine to the stream OS.
Definition: Twine.cpp:150
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:397
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:33
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
Twine(const unsigned long &Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition: Twine.h:329