Early exit if we don't have invokes. The 'Unwinds' vector isn't modified unless
[oota-llvm.git] / lib / Support / Twine.cpp
1 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
15
16 std::string Twine::str() const {
17   SmallString<256> Vec;
18   return toStringRef(Vec).str();
19 }
20
21 void Twine::toVector(SmallVectorImpl<char> &Out) const {
22   raw_svector_ostream OS(Out);
23   print(OS);
24 }
25
26 StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
27   if (isSingleStringRef())
28     return getSingleStringRef();
29   toVector(Out);
30   return StringRef(Out.data(), Out.size());
31 }
32
33 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
34   if (isUnary()) {
35     switch (getLHSKind()) {
36     case CStringKind:
37       // Already null terminated, yay!
38       return StringRef(static_cast<const char*>(LHS));
39     case StdStringKind: {
40         const std::string *str = static_cast<const std::string*>(LHS);
41         return StringRef(str->c_str(), str->size());
42       }
43     default:
44       break;
45     }
46   }
47   toVector(Out);
48   Out.push_back(0);
49   Out.pop_back();
50   return StringRef(Out.data(), Out.size());
51 }
52
53 void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
54                           NodeKind Kind) const {
55   switch (Kind) {
56   case Twine::NullKind: break;
57   case Twine::EmptyKind: break;
58   case Twine::TwineKind:
59     static_cast<const Twine*>(Ptr)->print(OS);
60     break;
61   case Twine::CStringKind:
62     OS << static_cast<const char*>(Ptr);
63     break;
64   case Twine::StdStringKind:
65     OS << *static_cast<const std::string*>(Ptr);
66     break;
67   case Twine::StringRefKind:
68     OS << *static_cast<const StringRef*>(Ptr);
69     break;
70   case Twine::DecUIKind:
71     OS << (unsigned)(uintptr_t)Ptr;
72     break;
73   case Twine::DecIKind:
74     OS << (int)(intptr_t)Ptr;
75     break;
76   case Twine::DecULKind:
77     OS << *static_cast<const unsigned long*>(Ptr);
78     break;
79   case Twine::DecLKind:
80     OS << *static_cast<const long*>(Ptr);
81     break;
82   case Twine::DecULLKind:
83     OS << *static_cast<const unsigned long long*>(Ptr);
84     break;
85   case Twine::DecLLKind:
86     OS << *static_cast<const long long*>(Ptr);
87     break;
88   case Twine::UHexKind:
89     OS.write_hex(*static_cast<const uint64_t*>(Ptr));
90     break;
91   }
92 }
93
94 void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
95                               NodeKind Kind) const {
96   switch (Kind) {
97   case Twine::NullKind:
98     OS << "null"; break;
99   case Twine::EmptyKind:
100     OS << "empty"; break;
101   case Twine::TwineKind:
102     OS << "rope:";
103     static_cast<const Twine*>(Ptr)->printRepr(OS);
104     break;
105   case Twine::CStringKind:
106     OS << "cstring:\""
107        << static_cast<const char*>(Ptr) << "\"";
108     break;
109   case Twine::StdStringKind:
110     OS << "std::string:\""
111        << static_cast<const std::string*>(Ptr) << "\"";
112     break;
113   case Twine::StringRefKind:
114     OS << "stringref:\""
115        << static_cast<const StringRef*>(Ptr) << "\"";
116     break;
117   case Twine::DecUIKind:
118     OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\"";
119     break;
120   case Twine::DecIKind:
121     OS << "decI:\"" << (int)(intptr_t)Ptr << "\"";
122     break;
123   case Twine::DecULKind:
124     OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
125     break;
126   case Twine::DecLKind:
127     OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
128     break;
129   case Twine::DecULLKind:
130     OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
131     break;
132   case Twine::DecLLKind:
133     OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
134     break;
135   case Twine::UHexKind:
136     OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
137     break;
138   }
139 }
140
141 void Twine::print(raw_ostream &OS) const {
142   printOneChild(OS, LHS, getLHSKind());
143   printOneChild(OS, RHS, getRHSKind());
144 }
145
146 void Twine::printRepr(raw_ostream &OS) const {
147   OS << "(Twine ";
148   printOneChildRepr(OS, LHS, getLHSKind());
149   OS << " ";
150   printOneChildRepr(OS, RHS, getRHSKind());
151   OS << ")";
152 }
153
154 void Twine::dump() const {
155   print(llvm::dbgs());
156 }
157
158 void Twine::dumpRepr() const {
159   printRepr(llvm::dbgs());
160 }