Removing LLVM_DELETED_FUNCTION, as MSVC 2012 was the last reason for requiring the...
[oota-llvm.git] / include / llvm / ADT / Twine.h
index ab8d3653e33f2043829f48f8b03b32452d10d0e2..9e9a4e1ad2c351756352358b6c99924cf72ec4b5 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/DataTypes.h"
+#include "llvm/Support/ErrorHandling.h"
 #include <cassert>
 #include <string>
 
@@ -43,7 +44,7 @@ namespace llvm {
   /// itself, and renders as an empty string. This can be returned from APIs to
   /// effectively nullify any concatenations performed on the result.
   ///
-  /// \b Implementation \n
+  /// \b Implementation
   ///
   /// Given the nature of a Twine, it is not possible for the Twine's
   /// concatenation method to construct interior nodes; the result must be
@@ -66,7 +67,7 @@ namespace llvm {
   ///
   /// These invariants are check by \see isValid().
   ///
-  /// \b Efficiency Considerations \n
+  /// \b Efficiency Considerations
   ///
   /// The Twine is designed to yield efficient and small code for common
   /// situations. For this reason, the concat() method is inlined so that
@@ -79,7 +80,7 @@ namespace llvm {
   /// StringRef) codegen as desired.
   class Twine {
     /// NodeKind - Represent the type of an argument.
-    enum NodeKind {
+    enum NodeKind : unsigned char {
       /// An empty string; the result of concatenating anything with it is also
       /// empty.
       NullKind,
@@ -99,6 +100,9 @@ namespace llvm {
       /// A pointer to a StringRef instance.
       StringRefKind,
 
+      /// A char value reinterpreted as a pointer, to render as a character.
+      CharKind,
+
       /// An unsigned int value reinterpreted as a pointer, to render as an
       /// unsigned decimal integer.
       DecUIKind,
@@ -126,17 +130,33 @@ namespace llvm {
       UHexKind
     };
 
+    union Child
+    {
+      const Twine *twine;
+      const char *cString;
+      const std::string *stdString;
+      const StringRef *stringRef;
+      char character;
+      unsigned int decUI;
+      int decI;
+      const unsigned long *decUL;
+      const long *decL;
+      const unsigned long long *decULL;
+      const long long *decLL;
+      const uint64_t *uHex;
+    };
+
   private:
     /// LHS - The prefix in the concatenation, which may be uninitialized for
     /// Null or Empty kinds.
-    const void *LHS;
+    Child LHS;
     /// RHS - The suffix in the concatenation, which may be uninitialized for
     /// Null or Empty kinds.
-    const void *RHS;
+    Child RHS;
     /// LHSKind - The NodeKind of the left hand side, \see getLHSKind().
-    unsigned char LHSKind;
-    /// RHSKind - The NodeKind of the left hand side, \see getLHSKind().
-    unsigned char RHSKind;
+    NodeKind LHSKind;
+    /// RHSKind - The NodeKind of the right hand side, \see getRHSKind().
+    NodeKind RHSKind;
 
   private:
     /// Construct a nullary twine; the kind must be NullKind or EmptyKind.
@@ -147,17 +167,23 @@ namespace llvm {
 
     /// Construct a binary twine.
     explicit Twine(const Twine &_LHS, const Twine &_RHS)
-      : LHS(&_LHS), RHS(&_RHS), LHSKind(TwineKind), RHSKind(TwineKind) {
+      : LHSKind(TwineKind), RHSKind(TwineKind) {
+      LHS.twine = &_LHS;
+      RHS.twine = &_RHS;
       assert(isValid() && "Invalid twine!");
     }
 
     /// Construct a twine from explicit values.
-    explicit Twine(const void *_LHS, NodeKind _LHSKind,
-                   const void *_RHS, NodeKind _RHSKind)
+    explicit Twine(Child _LHS, NodeKind _LHSKind,
+                   Child _RHS, NodeKind _RHSKind)
       : LHS(_LHS), RHS(_RHS), LHSKind(_LHSKind), RHSKind(_RHSKind) {
       assert(isValid() && "Invalid twine!");
     }
 
+    /// Since the intended use of twines is as temporary objects, assignments
+    /// when concatenating might cause undefined behavior or stack corruptions
+    Twine &operator=(const Twine &Other) = delete;
+
     /// isNull - Check for the null twine.
     bool isNull() const {
       return getLHSKind() == NullKind;
@@ -200,26 +226,26 @@ namespace llvm {
 
       // A twine child should always be binary.
       if (getLHSKind() == TwineKind &&
-          !static_cast<const Twine*>(LHS)->isBinary())
+          !LHS.twine->isBinary())
         return false;
       if (getRHSKind() == TwineKind &&
-          !static_cast<const Twine*>(RHS)->isBinary())
+          !RHS.twine->isBinary())
         return false;
 
       return true;
     }
 
     /// getLHSKind - Get the NodeKind of the left-hand side.
-    NodeKind getLHSKind() const { return (NodeKind) LHSKind; }
+    NodeKind getLHSKind() const { return LHSKind; }
 
-    /// getRHSKind - Get the NodeKind of the left-hand side.
-    NodeKind getRHSKind() const { return (NodeKind) RHSKind; }
+    /// getRHSKind - Get the NodeKind of the right-hand side.
+    NodeKind getRHSKind() const { return RHSKind; }
 
     /// printOneChild - Print one child from a twine.
-    void printOneChild(raw_ostream &OS, const void *Ptr, NodeKind Kind) const;
+    void printOneChild(raw_ostream &OS, Child Ptr, NodeKind Kind) const;
 
     /// printOneChildRepr - Print the representation of one child from a twine.
-    void printOneChildRepr(raw_ostream &OS, const void *Ptr,
+    void printOneChildRepr(raw_ostream &OS, Child Ptr,
                            NodeKind Kind) const;
 
   public:
@@ -239,7 +265,7 @@ namespace llvm {
     /*implicit*/ Twine(const char *Str)
       : RHSKind(EmptyKind) {
       if (Str[0] != '\0') {
-        LHS = Str;
+        LHS.cString = Str;
         LHSKind = CStringKind;
       } else
         LHSKind = EmptyKind;
@@ -249,44 +275,70 @@ namespace llvm {
 
     /// Construct from an std::string.
     /*implicit*/ Twine(const std::string &Str)
-      : LHS(&Str), LHSKind(StdStringKind), RHSKind(EmptyKind) {
+      : LHSKind(StdStringKind), RHSKind(EmptyKind) {
+      LHS.stdString = &Str;
       assert(isValid() && "Invalid twine!");
     }
 
     /// Construct from a StringRef.
     /*implicit*/ Twine(const StringRef &Str)
-      : LHS(&Str), LHSKind(StringRefKind), RHSKind(EmptyKind) {
+      : LHSKind(StringRefKind), RHSKind(EmptyKind) {
+      LHS.stringRef = &Str;
       assert(isValid() && "Invalid twine!");
     }
 
-    /// Construct a twine to print \arg Val as an unsigned decimal integer.
+    /// Construct from a char.
+    explicit Twine(char Val)
+      : LHSKind(CharKind), RHSKind(EmptyKind) {
+      LHS.character = Val;
+    }
+
+    /// Construct from a signed char.
+    explicit Twine(signed char Val)
+      : LHSKind(CharKind), RHSKind(EmptyKind) {
+      LHS.character = static_cast<char>(Val);
+    }
+
+    /// Construct from an unsigned char.
+    explicit Twine(unsigned char Val)
+      : LHSKind(CharKind), RHSKind(EmptyKind) {
+      LHS.character = static_cast<char>(Val);
+    }
+
+    /// Construct a twine to print \p Val as an unsigned decimal integer.
     explicit Twine(unsigned Val)
-      : LHS((void*)(intptr_t)Val), LHSKind(DecUIKind), RHSKind(EmptyKind) {
+      : LHSKind(DecUIKind), RHSKind(EmptyKind) {
+      LHS.decUI = Val;
     }
 
-    /// Construct a twine to print \arg Val as a signed decimal integer.
+    /// Construct a twine to print \p Val as a signed decimal integer.
     explicit Twine(int Val)
-      : LHS((void*)(intptr_t)Val), LHSKind(DecIKind), RHSKind(EmptyKind) {
+      : LHSKind(DecIKind), RHSKind(EmptyKind) {
+      LHS.decI = Val;
     }
 
-    /// Construct a twine to print \arg Val as an unsigned decimal integer.
+    /// Construct a twine to print \p Val as an unsigned decimal integer.
     explicit Twine(const unsigned long &Val)
-      : LHS(&Val), LHSKind(DecULKind), RHSKind(EmptyKind) {
+      : LHSKind(DecULKind), RHSKind(EmptyKind) {
+      LHS.decUL = &Val;
     }
 
-    /// Construct a twine to print \arg Val as a signed decimal integer.
+    /// Construct a twine to print \p Val as a signed decimal integer.
     explicit Twine(const long &Val)
-      : LHS(&Val), LHSKind(DecLKind), RHSKind(EmptyKind) {
+      : LHSKind(DecLKind), RHSKind(EmptyKind) {
+      LHS.decL = &Val;
     }
 
-    /// Construct a twine to print \arg Val as an unsigned decimal integer.
+    /// Construct a twine to print \p Val as an unsigned decimal integer.
     explicit Twine(const unsigned long long &Val)
-      : LHS(&Val), LHSKind(DecULLKind), RHSKind(EmptyKind) {
+      : LHSKind(DecULLKind), RHSKind(EmptyKind) {
+      LHS.decULL = &Val;
     }
 
-    /// Construct a twine to print \arg Val as a signed decimal integer.
+    /// Construct a twine to print \p Val as a signed decimal integer.
     explicit Twine(const long long &Val)
-      : LHS(&Val), LHSKind(DecLLKind), RHSKind(EmptyKind) {
+      : LHSKind(DecLLKind), RHSKind(EmptyKind) {
+      LHS.decLL = &Val;
     }
 
     // FIXME: Unfortunately, to make sure this is as efficient as possible we
@@ -296,13 +348,17 @@ namespace llvm {
 
     /// Construct as the concatenation of a C string and a StringRef.
     /*implicit*/ Twine(const char *_LHS, const StringRef &_RHS)
-      : LHS(_LHS), RHS(&_RHS), LHSKind(CStringKind), RHSKind(StringRefKind) {
+      : LHSKind(CStringKind), RHSKind(StringRefKind) {
+      LHS.cString = _LHS;
+      RHS.stringRef = &_RHS;
       assert(isValid() && "Invalid twine!");
     }
 
     /// Construct as the concatenation of a StringRef and a C string.
     /*implicit*/ Twine(const StringRef &_LHS, const char *_RHS)
-      : LHS(&_LHS), RHS(_RHS), LHSKind(StringRefKind), RHSKind(CStringKind) {
+      : LHSKind(StringRefKind), RHSKind(CStringKind) {
+      LHS.stringRef = &_LHS;
+      RHS.cString = _RHS;
       assert(isValid() && "Invalid twine!");
     }
 
@@ -316,9 +372,12 @@ namespace llvm {
     /// @name Numeric Conversions
     /// @{
 
-    // Construct a twine to print \arg Val as an unsigned hexadecimal integer.
+    // Construct a twine to print \p Val as an unsigned hexadecimal integer.
     static Twine utohexstr(const uint64_t &Val) {
-      return Twine(&Val, UHexKind, 0, EmptyKind);
+      Child LHS, RHS;
+      LHS.uHex = &Val;
+      RHS.twine = nullptr;
+      return Twine(LHS, UHexKind, RHS, EmptyKind);
     }
 
     /// @}
@@ -369,11 +428,11 @@ namespace llvm {
     StringRef getSingleStringRef() const {
       assert(isSingleStringRef() &&"This cannot be had as a single stringref!");
       switch (getLHSKind()) {
-      default: assert(0 && "Out of sync with isSingleStringRef");
+      default: llvm_unreachable("Out of sync with isSingleStringRef");
       case EmptyKind:      return StringRef();
-      case CStringKind:    return StringRef((const char*)LHS);
-      case StdStringKind:  return StringRef(*(const std::string*)LHS);
-      case StringRefKind:  return *(const StringRef*)LHS;
+      case CStringKind:    return StringRef(LHS.cString);
+      case StdStringKind:  return StringRef(*LHS.stdString);
+      case StringRefKind:  return *LHS.stringRef;
       }
     }
 
@@ -390,17 +449,17 @@ namespace llvm {
     /// The returned StringRef's size does not include the null terminator.
     StringRef toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const;
 
-    /// print - Write the concatenated string represented by this twine to the
-    /// stream \arg OS.
+    /// Write the concatenated string represented by this twine to the
+    /// stream \p OS.
     void print(raw_ostream &OS) const;
 
-    /// dump - Dump the concatenated string represented by this twine to stderr.
+    /// Dump the concatenated string represented by this twine to stderr.
     void dump() const;
 
-    /// print - Write the representation of this twine to the stream \arg OS.
+    /// Write the representation of this twine to the stream \p OS.
     void printRepr(raw_ostream &OS) const;
 
-    /// dumpRepr - Dump the representation of this twine to stderr.
+    /// Dump the representation of this twine to stderr.
     void dumpRepr() const;
 
     /// @}
@@ -422,7 +481,9 @@ namespace llvm {
 
     // Otherwise we need to create a new node, taking care to fold in unary
     // twines.
-    const void *NewLHS = this, *NewRHS = &Suffix;
+    Child NewLHS, NewRHS;
+    NewLHS.twine = this;
+    NewRHS.twine = &Suffix;
     NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
     if (isUnary()) {
       NewLHS = LHS;