Move DataTypes.h to include/llvm/System, update all users. This breaks the last
[oota-llvm.git] / include / llvm / ADT / Twine.h
1 //===-- Twine.h - Fast Temporary String Concatenation -----------*- C++ -*-===//
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 #ifndef LLVM_ADT_TWINE_H
11 #define LLVM_ADT_TWINE_H
12
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/System/DataTypes.h"
15 #include <cassert>
16 #include <string>
17
18 namespace llvm {
19   template <typename T>
20   class SmallVectorImpl;
21   class StringRef;
22   class raw_ostream;
23
24   /// Twine - A lightweight data structure for efficiently representing the
25   /// concatenation of temporary values as strings.
26   ///
27   /// A Twine is a kind of rope, it represents a concatenated string using a
28   /// binary-tree, where the string is the preorder of the nodes. Since the
29   /// Twine can be efficiently rendered into a buffer when its result is used,
30   /// it avoids the cost of generating temporary values for intermediate string
31   /// results -- particularly in cases when the Twine result is never
32   /// required. By explicitly tracking the type of leaf nodes, we can also avoid
33   /// the creation of temporary strings for conversions operations (such as
34   /// appending an integer to a string).
35   ///
36   /// A Twine is not intended for use directly and should not be stored, its
37   /// implementation relies on the ability to store pointers to temporary stack
38   /// objects which may be deallocated at the end of a statement. Twines should
39   /// only be used accepted as const references in arguments, when an API wishes
40   /// to accept possibly-concatenated strings.
41   ///
42   /// Twines support a special 'null' value, which always concatenates to form
43   /// itself, and renders as an empty string. This can be returned from APIs to
44   /// effectively nullify any concatenations performed on the result.
45   /// 
46   /// \b Implementation \n
47   ///
48   /// Given the nature of a Twine, it is not possible for the Twine's
49   /// concatenation method to construct interior nodes; the result must be
50   /// represented inside the returned value. For this reason a Twine object
51   /// actually holds two values, the left- and right-hand sides of a
52   /// concatenation. We also have nullary Twine objects, which are effectively
53   /// sentinel values that represent empty strings.
54   ///
55   /// Thus, a Twine can effectively have zero, one, or two children. The \see
56   /// isNullary(), \see isUnary(), and \see isBinary() predicates exist for
57   /// testing the number of children.
58   ///
59   /// We maintain a number of invariants on Twine objects (FIXME: Why):
60   ///  - Nullary twines are always represented with their Kind on the left-hand
61   ///    side, and the Empty kind on the right-hand side.
62   ///  - Unary twines are always represented with the value on the left-hand
63   ///    side, and the Empty kind on the right-hand side.
64   ///  - If a Twine has another Twine as a child, that child should always be
65   ///    binary (otherwise it could have been folded into the parent).
66   ///
67   /// These invariants are check by \see isValid().
68   ///
69   /// \b Efficiency Considerations \n
70   ///
71   /// The Twine is designed to yield efficient and small code for common
72   /// situations. For this reason, the concat() method is inlined so that
73   /// concatenations of leaf nodes can be optimized into stores directly into a
74   /// single stack allocated object.
75   ///
76   /// In practice, not all compilers can be trusted to optimize concat() fully,
77   /// so we provide two additional methods (and accompanying operator+
78   /// overloads) to guarantee that particularly important cases (cstring plus
79   /// StringRef) codegen as desired.
80   class Twine {
81     /// NodeKind - Represent the type of an argument.
82     enum NodeKind {
83       /// An empty string; the result of concatenating anything with it is also
84       /// empty.
85       NullKind,
86
87       /// The empty string.
88       EmptyKind,
89
90       /// A pointer to a Twine instance.
91       TwineKind,
92
93       /// A pointer to a C string instance.
94       CStringKind,
95
96       /// A pointer to an std::string instance.
97       StdStringKind,
98
99       /// A pointer to a StringRef instance.
100       StringRefKind,
101
102       /// A pointer to an unsigned int value, to render as an unsigned decimal
103       /// integer.
104       DecUIKind,
105
106       /// A pointer to an int value, to render as a signed decimal integer.
107       DecIKind,
108
109       /// A pointer to an unsigned long value, to render as an unsigned decimal
110       /// integer.
111       DecULKind,
112
113       /// A pointer to a long value, to render as a signed decimal integer.
114       DecLKind,
115
116       /// A pointer to an unsigned long long value, to render as an unsigned
117       /// decimal integer.
118       DecULLKind,
119
120       /// A pointer to a long long value, to render as a signed decimal integer.
121       DecLLKind,
122
123       /// A pointer to a uint64_t value, to render as an unsigned hexadecimal
124       /// integer.
125       UHexKind
126     };
127
128   private:
129     /// LHS - The prefix in the concatenation, which may be uninitialized for
130     /// Null or Empty kinds.
131     const void *LHS;
132     /// RHS - The suffix in the concatenation, which may be uninitialized for
133     /// Null or Empty kinds.
134     const void *RHS;
135     /// LHSKind - The NodeKind of the left hand side, \see getLHSKind().
136     NodeKind LHSKind : 8;
137     /// RHSKind - The NodeKind of the left hand side, \see getLHSKind().
138     NodeKind RHSKind : 8;
139
140   private:
141     /// Construct a nullary twine; the kind must be NullKind or EmptyKind.
142     explicit Twine(NodeKind Kind)
143       : LHSKind(Kind), RHSKind(EmptyKind) {
144       assert(isNullary() && "Invalid kind!");
145     }
146
147     /// Construct a binary twine.
148     explicit Twine(const Twine &_LHS, const Twine &_RHS)
149       : LHS(&_LHS), RHS(&_RHS), LHSKind(TwineKind), RHSKind(TwineKind) {
150       assert(isValid() && "Invalid twine!");
151     }
152
153     /// Construct a twine from explicit values.
154     explicit Twine(const void *_LHS, NodeKind _LHSKind,
155                    const void *_RHS, NodeKind _RHSKind)
156       : LHS(_LHS), RHS(_RHS), LHSKind(_LHSKind), RHSKind(_RHSKind) {
157       assert(isValid() && "Invalid twine!");
158     }
159
160     /// isNull - Check for the null twine.
161     bool isNull() const {
162       return getLHSKind() == NullKind;
163     }
164
165     /// isEmpty - Check for the empty twine.
166     bool isEmpty() const {
167       return getLHSKind() == EmptyKind;
168     }
169
170     /// isNullary - Check if this is a nullary twine (null or empty).
171     bool isNullary() const {
172       return isNull() || isEmpty();
173     }
174
175     /// isUnary - Check if this is a unary twine.
176     bool isUnary() const {
177       return getRHSKind() == EmptyKind && !isNullary();
178     }
179
180     /// isBinary - Check if this is a binary twine.
181     bool isBinary() const {
182       return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
183     }
184
185     /// isValid - Check if this is a valid twine (satisfying the invariants on
186     /// order and number of arguments).
187     bool isValid() const {
188       // Nullary twines always have Empty on the RHS.
189       if (isNullary() && getRHSKind() != EmptyKind)
190         return false;
191
192       // Null should never appear on the RHS.
193       if (getRHSKind() == NullKind)
194         return false;
195
196       // The RHS cannot be non-empty if the LHS is empty.
197       if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
198         return false;
199
200       // A twine child should always be binary.
201       if (getLHSKind() == TwineKind &&
202           !static_cast<const Twine*>(LHS)->isBinary())
203         return false;
204       if (getRHSKind() == TwineKind &&
205           !static_cast<const Twine*>(RHS)->isBinary())
206         return false;
207
208       return true;
209     }
210
211     /// getLHSKind - Get the NodeKind of the left-hand side.
212     NodeKind getLHSKind() const { return LHSKind; }
213
214     /// getRHSKind - Get the NodeKind of the left-hand side.
215     NodeKind getRHSKind() const { return RHSKind; }
216
217     /// printOneChild - Print one child from a twine.
218     void printOneChild(raw_ostream &OS, const void *Ptr, NodeKind Kind) const;
219
220     /// printOneChildRepr - Print the representation of one child from a twine.
221     void printOneChildRepr(raw_ostream &OS, const void *Ptr,
222                            NodeKind Kind) const;
223
224   public:
225     /// @name Constructors
226     /// @{
227
228     /// Construct from an empty string.
229     /*implicit*/ Twine() : LHSKind(EmptyKind), RHSKind(EmptyKind) {
230       assert(isValid() && "Invalid twine!");
231     }
232
233     /// Construct from a C string.
234     ///
235     /// We take care here to optimize "" into the empty twine -- this will be
236     /// optimized out for string constants. This allows Twine arguments have
237     /// default "" values, without introducing unnecessary string constants.
238     /*implicit*/ Twine(const char *Str)
239       : RHSKind(EmptyKind) {
240       if (Str[0] != '\0') {
241         LHS = Str;
242         LHSKind = CStringKind;
243       } else
244         LHSKind = EmptyKind;
245
246       assert(isValid() && "Invalid twine!");
247     }
248
249     /// Construct from an std::string.
250     /*implicit*/ Twine(const std::string &Str)
251       : LHS(&Str), LHSKind(StdStringKind), RHSKind(EmptyKind) {
252       assert(isValid() && "Invalid twine!");
253     }
254
255     /// Construct from a StringRef.
256     /*implicit*/ Twine(const StringRef &Str)
257       : LHS(&Str), LHSKind(StringRefKind), RHSKind(EmptyKind) {
258       assert(isValid() && "Invalid twine!");
259     }
260
261     /// Construct a twine to print \arg Val as an unsigned decimal integer.
262     explicit Twine(const unsigned int &Val) 
263       : LHS(&Val), LHSKind(DecUIKind), RHSKind(EmptyKind) {
264     }
265
266     /// Construct a twine to print \arg Val as a signed decimal integer.
267     explicit Twine(const int &Val) 
268       : LHS(&Val), LHSKind(DecIKind), RHSKind(EmptyKind) {
269     }
270
271     /// Construct a twine to print \arg Val as an unsigned decimal integer.
272     explicit Twine(const unsigned long &Val) 
273       : LHS(&Val), LHSKind(DecULKind), RHSKind(EmptyKind) {
274     }
275
276     /// Construct a twine to print \arg Val as a signed decimal integer.
277     explicit Twine(const long &Val) 
278       : LHS(&Val), LHSKind(DecLKind), RHSKind(EmptyKind) {
279     }
280
281     /// Construct a twine to print \arg Val as an unsigned decimal integer.
282     explicit Twine(const unsigned long long &Val) 
283       : LHS(&Val), LHSKind(DecULLKind), RHSKind(EmptyKind) {
284     }
285
286     /// Construct a twine to print \arg Val as a signed decimal integer.
287     explicit Twine(const long long &Val) 
288       : LHS(&Val), LHSKind(DecLLKind), RHSKind(EmptyKind) {
289     }
290
291     // FIXME: Unfortunately, to make sure this is as efficient as possible we
292     // need extra binary constructors from particular types. We can't rely on
293     // the compiler to be smart enough to fold operator+()/concat() down to the
294     // right thing. Yet.
295
296     /// Construct as the concatenation of a C string and a StringRef.
297     /*implicit*/ Twine(const char *_LHS, const StringRef &_RHS)
298       : LHS(_LHS), RHS(&_RHS), LHSKind(CStringKind), RHSKind(StringRefKind) {
299       assert(isValid() && "Invalid twine!");
300     }
301
302     /// Construct as the concatenation of a StringRef and a C string.
303     /*implicit*/ Twine(const StringRef &_LHS, const char *_RHS)
304       : LHS(&_LHS), RHS(_RHS), LHSKind(StringRefKind), RHSKind(CStringKind) {
305       assert(isValid() && "Invalid twine!");
306     }
307
308     /// Create a 'null' string, which is an empty string that always
309     /// concatenates to form another empty string.
310     static Twine createNull() {
311       return Twine(NullKind);
312     }
313
314     /// @}
315     /// @name Numeric Conversions
316     /// @{
317
318     // Construct a twine to print \arg Val as an unsigned hexadecimal integer.
319     static Twine utohexstr(const uint64_t &Val) {
320       return Twine(&Val, UHexKind, 0, EmptyKind);
321     }
322
323     /// @}
324     /// @name Predicate Operations
325     /// @{
326
327     /// isTriviallyEmpty - Check if this twine is trivially empty; a false
328     /// return value does not necessarily mean the twine is empty.
329     bool isTriviallyEmpty() const {
330       return isNullary();
331     }
332
333     /// @}
334     /// @name String Operations
335     /// @{
336
337     Twine concat(const Twine &Suffix) const;
338
339     /// @}
340     /// @name Output & Conversion.
341     /// @{
342
343     /// str - Return the twine contents as a std::string.
344     std::string str() const;
345
346     /// toVector - Write the concatenated string into the given SmallString or
347     /// SmallVector.
348     void toVector(SmallVectorImpl<char> &Out) const;
349
350     /// print - Write the concatenated string represented by this twine to the
351     /// stream \arg OS.
352     void print(raw_ostream &OS) const;
353
354     /// dump - Dump the concatenated string represented by this twine to stderr.
355     void dump() const;
356
357     /// print - Write the representation of this twine to the stream \arg OS.
358     void printRepr(raw_ostream &OS) const;
359
360     /// dumpRepr - Dump the representation of this twine to stderr.
361     void dumpRepr() const;
362
363     /// @}
364   };
365
366   /// @name Twine Inline Implementations
367   /// @{
368
369   inline Twine Twine::concat(const Twine &Suffix) const {
370     // Concatenation with null is null.
371     if (isNull() || Suffix.isNull())
372       return Twine(NullKind);
373
374     // Concatenation with empty yields the other side.
375     if (isEmpty())
376       return Suffix;
377     if (Suffix.isEmpty())
378       return *this;
379
380     // Otherwise we need to create a new node, taking care to fold in unary
381     // twines.
382     const void *NewLHS = this, *NewRHS = &Suffix;
383     NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
384     if (isUnary()) {
385       NewLHS = LHS;
386       NewLHSKind = getLHSKind();
387     }
388     if (Suffix.isUnary()) {
389       NewRHS = Suffix.LHS;
390       NewRHSKind = Suffix.getLHSKind();
391     }
392
393     return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
394   }
395
396   inline Twine operator+(const Twine &LHS, const Twine &RHS) {
397     return LHS.concat(RHS);
398   }
399
400   /// Additional overload to guarantee simplified codegen; this is equivalent to
401   /// concat().
402
403   inline Twine operator+(const char *LHS, const StringRef &RHS) {
404     return Twine(LHS, RHS);
405   }
406
407   /// Additional overload to guarantee simplified codegen; this is equivalent to
408   /// concat().
409
410   inline Twine operator+(const StringRef &LHS, const char *RHS) {
411     return Twine(LHS, RHS);
412   }
413
414   inline raw_ostream &operator<<(raw_ostream &OS, const Twine &RHS) {
415     RHS.print(OS);
416     return OS;
417   }
418
419   /// @}
420 }
421
422 #endif