revert r194655
[oota-llvm.git] / include / llvm / Support / YAMLTraits.h
index 4376165e536751bf5b0a140cc2eaea31ac680b2c..27c1393b3ac6a936f5b2842d651eb3d0e2093b15 100644 (file)
@@ -7,23 +7,23 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_YAML_TRAITS_H_
-#define LLVM_YAML_TRAITS_H_
+#ifndef LLVM_SUPPORT_YAMLTRAITS_H
+#define LLVM_SUPPORT_YAMLTRAITS_H
 
 
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseMapInfo.h"
 #include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/SourceMgr.h"
-#include "llvm/Support/system_error.h"
-#include "llvm/Support/type_traits.h"
 #include "llvm/Support/YAMLParser.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/system_error.h"
+#include "llvm/Support/type_traits.h"
 
 
 namespace llvm {
@@ -195,7 +195,7 @@ public:
 template <class T>
 struct has_ScalarTraits
 {
-  typedef llvm::StringRef (*Signature_input)(llvm::StringRef, void*, T&);
+  typedef StringRef (*Signature_input)(StringRef, void*, T&);
   typedef void (*Signature_output)(const T&, void*, llvm::raw_ostream&);
 
   template <typename U>
@@ -227,10 +227,9 @@ public:
 };
 
 
-// Test if SequenceTraits<T> is defined on type T
-// and SequenceTraits<T>::flow is *not* defined.
+// Test if SequenceTraits<T> is defined on type T.
 template <class T>
-struct has_SequenceTraits
+struct has_SequenceMethodTraits
 {
   typedef size_t (*Signature_size)(class IO&, T&);
 
@@ -240,43 +239,48 @@ struct has_SequenceTraits
   template <typename U>
   static double test(...);
 
-  template <typename U> static
-  char flowtest( char[sizeof(&U::flow)] ) ;
+public:
+  static bool const value =  (sizeof(test<SequenceTraits<T> >(0)) == 1);
+};
 
-  template <typename U>
-  static double flowtest(...);
 
+// has_FlowTraits<int> will cause an error with some compilers because
+// it subclasses int.  Using this wrapper only instantiates the
+// real has_FlowTraits only if the template type is a class.
+template <typename T, bool Enabled = llvm::is_class<T>::value>
+class has_FlowTraits
+{
 public:
-  static bool const value =  (sizeof(test<SequenceTraits<T> >(0)) == 1)
-                          && (sizeof(flowtest<T>(0)) != 1);
+   static const bool value = false;
 };
 
-
-// Test if SequenceTraits<T> is defined on type T
-// and SequenceTraits<T>::flow is defined.
+// Some older gcc compilers don't support straight forward tests
+// for members, so test for ambiguity cause by the base and derived
+// classes both defining the member.
 template <class T>
-struct has_FlowSequenceTraits
+struct has_FlowTraits<T, true>
 {
-  typedef size_t (*Signature_size)(class IO&, T&);
-
-  template <typename U>
-  static char test(SameType<Signature_size, &U::size>*);
+  struct Fallback { bool flow; };
+  struct Derived : T, Fallback { };
 
-  template <typename U>
-  static double test(...);
+  template<typename C>
+  static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];
 
-  template <typename U> static
-  char flowtest( char[sizeof(&U::flow)] ) ;
-
-  template <typename U>
-  static double flowtest(...);
+  template<typename C>
+  static char (&f(...))[2];
 
 public:
-  static bool const value =  (sizeof(test<SequenceTraits<T> >(0)) == 1)
-                          && (sizeof(flowtest<T>(0)) == 1);
+  static bool const value = sizeof(f<Derived>(0)) == 2;
 };
 
 
+
+// Test if SequenceTraits<T> is defined on type T
+template<typename T>
+struct has_SequenceTraits : public  llvm::integral_constant<bool,
+                                      has_SequenceMethodTraits<T>::value > { };
+
+
 // Test if DocumentListTraits<T> is defined on type T
 template <class T>
 struct has_DocumentListTraits
@@ -303,7 +307,6 @@ struct missingTraits : public  llvm::integral_constant<bool,
                                       && !has_ScalarTraits<T>::value
                                       && !has_MappingTraits<T>::value
                                       && !has_SequenceTraits<T>::value
-                                      && !has_FlowSequenceTraits<T>::value
                                       && !has_DocumentListTraits<T>::value >  {};
 
 
@@ -320,12 +323,14 @@ public:
   virtual bool preflightElement(unsigned, void *&) = 0;
   virtual void postflightElement(void*) = 0;
   virtual void endSequence() = 0;
+  virtual bool canElideEmptySequence() = 0;
 
   virtual unsigned beginFlowSequence() = 0;
   virtual bool preflightFlowElement(unsigned, void *&) = 0;
   virtual void postflightFlowElement(void*) = 0;
   virtual void endFlowSequence() = 0;
 
+  virtual bool mapTag(StringRef Tag, bool Default=false) = 0;
   virtual void beginMapping() = 0;
   virtual void endMapping() = 0;
   virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0;
@@ -345,7 +350,7 @@ public:
 
   template <typename T>
   void enumCase(T &Val, const char* Str, const T ConstVal) {
-    if ( matchEnumScalar(Str, (Val == ConstVal)) ) {
+    if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) {
       Val = ConstVal;
     }
   }
@@ -353,14 +358,14 @@ public:
   // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
   template <typename T>
   void enumCase(T &Val, const char* Str, const uint32_t ConstVal) {
-    if ( matchEnumScalar(Str, (Val == static_cast<T>(ConstVal))) ) {
+    if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) {
       Val = ConstVal;
     }
   }
 
   template <typename T>
   void bitSetCase(T &Val, const char* Str, const T ConstVal) {
-    if ( bitSetMatch(Str, ((Val & ConstVal) == ConstVal)) ) {
+    if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
       Val = Val | ConstVal;
     }
   }
@@ -368,7 +373,7 @@ public:
   // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
   template <typename T>
   void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) {
-    if ( bitSetMatch(Str, ((Val & ConstVal) == ConstVal)) ) {
+    if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
       Val = Val | ConstVal;
     }
   }
@@ -385,7 +390,7 @@ public:
   typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
   mapOptional(const char* Key, T& Val) {
     // omit key/value instead of outputting empty sequence
-    if ( this->outputting() && !(Val.begin() != Val.end()) )
+    if ( this->canElideEmptySequence() && !(Val.begin() != Val.end()) )
       return;
     this->processKey(Key, Val, false);
   }
@@ -400,15 +405,14 @@ public:
   void mapOptional(const char* Key, T& Val, const T& Default) {
     this->processKeyWithDefault(Key, Val, Default, false);
   }
-
-
+  
 private:
   template <typename T>
   void processKeyWithDefault(const char *Key, T &Val, const T& DefaultValue,
                                                                 bool Required) {
     void *SaveInfo;
     bool UseDefault;
-    const bool sameAsDefault = (Val == DefaultValue);
+    const bool sameAsDefault = outputting() && Val == DefaultValue;
     if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
                                                                   SaveInfo) ) {
       yamlize(*this, Val, Required);
@@ -486,123 +490,119 @@ yamlize(IO &io, T &Val, bool) {
   io.endMapping();
 }
 
-#ifndef BUILDING_YAMLIO
 template<typename T>
 typename llvm::enable_if_c<missingTraits<T>::value, void>::type
 yamlize(IO &io, T &Val, bool) {
   char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
 }
-#endif
 
 template<typename T>
 typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
 yamlize(IO &io, T &Seq, bool) {
-  unsigned incount = io.beginSequence();
-  unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incount;
-  for(unsigned i=0; i < count; ++i) {
-    void *SaveInfo;
-    if ( io.preflightElement(i, SaveInfo) ) {
-      yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
-      io.postflightElement(SaveInfo);
+  if ( has_FlowTraits< SequenceTraits<T> >::value ) {
+    unsigned incnt = io.beginFlowSequence();
+    unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
+    for(unsigned i=0; i < count; ++i) {
+      void *SaveInfo;
+      if ( io.preflightFlowElement(i, SaveInfo) ) {
+        yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
+        io.postflightFlowElement(SaveInfo);
+      }
     }
+    io.endFlowSequence();
   }
-  io.endSequence();
-}
-
-template<typename T>
-typename llvm::enable_if_c<has_FlowSequenceTraits<T>::value,void>::type
-yamlize(IO &io, T &Seq, bool) {
-  unsigned incount = io.beginFlowSequence();
-  unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incount;
-  for(unsigned i=0; i < count; ++i) {
-    void *SaveInfo;
-    if ( io.preflightFlowElement(i, SaveInfo) ) {
-      yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
-      io.postflightFlowElement(SaveInfo);
+  else {
+    unsigned incnt = io.beginSequence();
+    unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
+    for(unsigned i=0; i < count; ++i) {
+      void *SaveInfo;
+      if ( io.preflightElement(i, SaveInfo) ) {
+        yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
+        io.postflightElement(SaveInfo);
+      }
     }
+    io.endSequence();
   }
-  io.endFlowSequence();
 }
 
 
-
-// Clients of YAML I/O only see declaration of the traits for built-in
-// types.  The implementation is in the LLVM Support library.  Without
-// this #ifdef, every client would get a copy of the implementation of
-// these traits.
-#ifndef BUILDING_YAMLIO
 template<>
 struct ScalarTraits<bool> {
   static void output(const bool &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, bool &);
+  static StringRef input(StringRef, void*, bool &);
 };
 
 template<>
 struct ScalarTraits<StringRef> {
   static void output(const StringRef &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, StringRef &);
+  static StringRef input(StringRef, void*, StringRef &);
+};
+template<>
+struct ScalarTraits<std::string> {
+  static void output(const std::string &, void*, llvm::raw_ostream &);
+  static StringRef input(StringRef, void*, std::string &);
 };
 
 template<>
 struct ScalarTraits<uint8_t> {
   static void output(const uint8_t &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, uint8_t &);
+  static StringRef input(StringRef, void*, uint8_t &);
 };
 
 template<>
 struct ScalarTraits<uint16_t> {
   static void output(const uint16_t &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, uint16_t &);
+  static StringRef input(StringRef, void*, uint16_t &);
 };
 
 template<>
 struct ScalarTraits<uint32_t> {
   static void output(const uint32_t &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, uint32_t &);
+  static StringRef input(StringRef, void*, uint32_t &);
 };
 
 template<>
 struct ScalarTraits<uint64_t> {
   static void output(const uint64_t &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, uint64_t &);
+  static StringRef input(StringRef, void*, uint64_t &);
 };
 
 template<>
 struct ScalarTraits<int8_t> {
   static void output(const int8_t &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, int8_t &);
+  static StringRef input(StringRef, void*, int8_t &);
 };
 
 template<>
 struct ScalarTraits<int16_t> {
   static void output(const int16_t &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, int16_t &);
+  static StringRef input(StringRef, void*, int16_t &);
 };
 
 template<>
 struct ScalarTraits<int32_t> {
   static void output(const int32_t &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, int32_t &);
+  static StringRef input(StringRef, void*, int32_t &);
 };
 
 template<>
 struct ScalarTraits<int64_t> {
   static void output(const int64_t &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, int64_t &);
+  static StringRef input(StringRef, void*, int64_t &);
 };
 
 template<>
 struct ScalarTraits<float> {
   static void output(const float &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, float &);
+  static StringRef input(StringRef, void*, float &);
 };
 
 template<>
 struct ScalarTraits<double> {
   static void output(const double &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, double &);
+  static StringRef input(StringRef, void*, double &);
 };
-#endif
 
 
 
@@ -630,7 +630,7 @@ struct MappingNormalization {
   TNorm* operator->() { return BufPtr; }
 
 private:
-  typedef typename llvm::AlignedCharArrayUnion<TNorm> Storage;
+  typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
 
   Storage       Buffer;
   IO           &io;
@@ -666,7 +666,7 @@ struct MappingNormalizationHeap {
   TNorm* operator->() { return BufPtr; }
 
 private:
-  typedef typename llvm::AlignedCharArrayUnion<TNorm> Storage;
+  typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
 
   Storage       Buffer;
   IO           &io;
@@ -690,17 +690,21 @@ private:
 ///
 class Input : public IO {
 public:
-  // Construct a yaml Input object from a StringRef and optional user-data.
-  Input(StringRef InputContent, void *Ctxt=NULL);
+  // Construct a yaml Input object from a StringRef and optional
+  // user-data. The DiagHandler can be specified to provide
+  // alternative error reporting.
+  Input(StringRef InputContent,
+        void *Ctxt = NULL,
+        SourceMgr::DiagHandlerTy DiagHandler = NULL,
+        void *DiagHandlerCtxt = NULL);
+  ~Input();
 
   // Check if there was an syntax or semantic error during parsing.
   llvm::error_code error();
 
-  // To set alternate error reporting.
-  void setDiagHandler(llvm::SourceMgr::DiagHandlerTy Handler, void *Ctxt = 0);
-
 private:
   virtual bool outputting();
+  virtual bool mapTag(StringRef, bool);
   virtual void beginMapping();
   virtual void endMapping();
   virtual bool preflightKey(const char *, bool, bool, bool &, void *&);
@@ -721,16 +725,20 @@ private:
   virtual void endBitSetScalar();
   virtual void scalarString(StringRef &);
   virtual void setError(const Twine &message);
+  virtual bool canElideEmptySequence();
 
   class HNode {
+    virtual void anchor();
   public:
     HNode(Node *n) : _node(n) { }
+    virtual ~HNode() { }
     static inline bool classof(const HNode *) { return true; }
 
     Node *_node;
   };
 
   class EmptyHNode : public HNode {
+    virtual void anchor();
   public:
     EmptyHNode(Node *n) : HNode(n) { }
     static inline bool classof(const HNode *n) {
@@ -740,6 +748,7 @@ private:
   };
 
   class ScalarHNode : public HNode {
+    virtual void anchor();
   public:
     ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
 
@@ -756,21 +765,14 @@ private:
   class MapHNode : public HNode {
   public:
     MapHNode(Node *n) : HNode(n) { }
+    virtual ~MapHNode();
 
     static inline bool classof(const HNode *n) {
       return MappingNode::classof(n->_node);
     }
     static inline bool classof(const MapHNode *) { return true; }
 
-    struct StrMappingInfo {
-      static StringRef getEmptyKey() { return StringRef(); }
-      static StringRef getTombstoneKey() { return StringRef(" ", 0); }
-      static unsigned getHashValue(StringRef const val) {
-                                                return llvm::HashString(val); }
-      static bool isEqual(StringRef const lhs,
-                          StringRef const rhs) { return lhs.equals(rhs); }
-    };
-    typedef llvm::DenseMap<StringRef, HNode*, StrMappingInfo> NameToNode;
+    typedef llvm::StringMap<HNode*> NameToNode;
 
     bool isValidKey(StringRef key);
 
@@ -781,6 +783,7 @@ private:
   class SequenceHNode : public HNode {
   public:
     SequenceHNode(Node *n) : HNode(n) { }
+    virtual ~SequenceHNode();
 
     static inline bool classof(const HNode *n) {
       return SequenceNode::classof(n->_node);
@@ -802,10 +805,11 @@ public:
   void nextDocument();
 
 private:
-  llvm::yaml::Stream              *Strm;
-  llvm::SourceMgr                  SrcMgr;
+  llvm::SourceMgr                  SrcMgr; // must be before Strm
+  OwningPtr<llvm::yaml::Stream>    Strm;
+  OwningPtr<HNode>                 TopNode;
   llvm::error_code                 EC;
-  llvm::BumpPtrAllocator           Allocator;
+  llvm::BumpPtrAllocator           StringAllocator;
   llvm::yaml::document_iterator    DocIterator;
   std::vector<bool>                BitValuesUsed;
   HNode                           *CurrentNode;
@@ -825,6 +829,7 @@ public:
   virtual ~Output();
 
   virtual bool outputting();
+  virtual bool mapTag(StringRef, bool);
   virtual void beginMapping();
   virtual void endMapping();
   virtual bool preflightKey(const char *key, bool, bool, bool &, void *&);
@@ -845,7 +850,7 @@ public:
   virtual void endBitSetScalar();
   virtual void scalarString(StringRef &);
   virtual void setError(const Twine &message);
-
+  virtual bool canElideEmptySequence();
 public:
   // These are only used by operator<<. They could be private
   // if that templated operator could be made a friend.
@@ -910,35 +915,29 @@ LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)
 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
 
 
-// Clients of YAML I/O only see declaration of the traits for Hex*
-// types.  The implementation is in the LLVM Support library.  Without
-// this #ifdef, every client would get a copy of the implementation of
-// these traits.
-#ifndef BUILDING_YAMLIO
 template<>
 struct ScalarTraits<Hex8> {
   static void output(const Hex8 &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, Hex8 &);
+  static StringRef input(StringRef, void*, Hex8 &);
 };
 
 template<>
 struct ScalarTraits<Hex16> {
   static void output(const Hex16 &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, Hex16 &);
+  static StringRef input(StringRef, void*, Hex16 &);
 };
 
 template<>
 struct ScalarTraits<Hex32> {
   static void output(const Hex32 &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, Hex32 &);
+  static StringRef input(StringRef, void*, Hex32 &);
 };
 
 template<>
 struct ScalarTraits<Hex64> {
   static void output(const Hex64 &, void*, llvm::raw_ostream &);
-  static llvm::StringRef input(llvm::StringRef , void*, Hex64 &);
+  static StringRef input(StringRef, void*, Hex64 &);
 };
-#endif
 
 
 // Define non-member operator>> so that Input can stream in a document list.
@@ -973,12 +972,11 @@ template <typename T>
 inline
 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Input &>::type
 operator>>(Input &yin, T &docSeq) {
-  yin.setCurrentDocument();
-  yamlize(yin, docSeq, true);
+  if (yin.setCurrentDocument())
+    yamlize(yin, docSeq, true);
   return yin;
 }
 
-#ifndef BUILDING_YAMLIO
 // Provide better error message about types missing a trait specialization
 template <typename T>
 inline
@@ -987,7 +985,6 @@ operator>>(Input &yin, T &docSeq) {
   char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
   return yin;
 }
-#endif
 
 
 // Define non-member operator<< so that Output can stream out document list.
@@ -1035,7 +1032,6 @@ operator<<(Output &yout, T &seq) {
   return yout;
 }
 
-#ifndef BUILDING_YAMLIO
 // Provide better error message about types missing a trait specialization
 template <typename T>
 inline
@@ -1044,7 +1040,6 @@ operator<<(Output &yout, T &seq) {
   char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
   return yout;
 }
-#endif
 
 
 } // namespace yaml
@@ -1111,4 +1106,4 @@ operator<<(Output &yout, T &seq) {
 
 
 
-#endif // LLVM_YAML_TRAITS_H_
+#endif // LLVM_SUPPORT_YAMLTRAITS_H