-//===- llvm/Supporrt/YAMLTraits.h -------------------------------*- C++ -*-===//
+//===- llvm/Support/YAMLTraits.h --------------------------------*- C++ -*-===//
//
// The LLVM Linker
//
//
//===----------------------------------------------------------------------===//
-#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/Optional.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/Regex.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"
namespace llvm {
namespace yaml {
/// This class should be specialized by any type that needs to be converted
/// to/from a YAML mapping. For example:
///
-/// struct ScalarBitSetTraits<MyStruct> {
+/// struct MappingTraits<MyStruct> {
/// static void mapping(IO &io, MyStruct &s) {
/// io.mapRequired("name", s.name);
/// io.mapRequired("size", s.size);
struct MappingTraits {
// Must provide:
// static void mapping(IO &io, T &fields);
+ // Optionally may provide:
+ // static StringRef validate(IO &io, T &fields);
};
/// // return empty string on success, or error string
/// return StringRef();
/// }
+/// static bool mustQuote(StringRef) { return true; }
/// };
template<typename T>
struct ScalarTraits {
// Function to convert a string to a value. Returns the empty
// StringRef on success or an error string if string is malformed:
//static StringRef input(StringRef scalar, void *ctxt, T &value);
+ //
+ // Function to determine if the value should be quoted.
+ //static bool mustQuote(StringRef);
};
static double test(...);
public:
- static bool const value = (sizeof(test<ScalarEnumerationTraits<T> >(0)) == 1);
+ static bool const value =
+ (sizeof(test<ScalarEnumerationTraits<T> >(nullptr)) == 1);
};
static double test(...);
public:
- static bool const value = (sizeof(test<ScalarBitSetTraits<T> >(0)) == 1);
+ static bool const value = (sizeof(test<ScalarBitSetTraits<T> >(nullptr)) == 1);
};
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&);
+ typedef bool (*Signature_mustQuote)(StringRef);
template <typename U>
- static char test(SameType<Signature_input, &U::input>*,
- SameType<Signature_output, &U::output>*);
+ static char test(SameType<Signature_input, &U::input> *,
+ SameType<Signature_output, &U::output> *,
+ SameType<Signature_mustQuote, &U::mustQuote> *);
template <typename U>
static double test(...);
public:
- static bool const value = (sizeof(test<ScalarTraits<T> >(0,0)) == 1);
+ static bool const value =
+ (sizeof(test<ScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1);
};
static double test(...);
public:
- static bool const value = (sizeof(test<MappingTraits<T> >(0)) == 1);
+ static bool const value = (sizeof(test<MappingTraits<T> >(nullptr)) == 1);
};
-
-// Test if SequenceTraits<T> is defined on type T
-// and SequenceTraits<T>::flow is *not* defined.
+// Test if MappingTraits<T>::validate() is defined on type T.
template <class T>
-struct has_SequenceTraits
+struct has_MappingValidateTraits
{
- typedef size_t (*Signature_size)(class IO&, T&);
+ typedef StringRef (*Signature_validate)(class IO&, T&);
template <typename U>
- static char test(SameType<Signature_size, &U::size>*);
+ static char test(SameType<Signature_validate, &U::validate>*);
template <typename U>
static double test(...);
- template <typename U> static
- char flowtest( char[sizeof(&U::flow)] ) ;
-
- template <typename U>
- static double flowtest(...);
-
public:
- static bool const value = (sizeof(test<SequenceTraits<T> >(0)) == 1)
- && (sizeof(flowtest<T>(0)) != 1);
+ static bool const value = (sizeof(test<MappingTraits<T> >(nullptr)) == 1);
};
-// Test if SequenceTraits<T> is defined on type T
-// and SequenceTraits<T>::flow is defined.
+
+// Test if SequenceTraits<T> is defined on type T.
template <class T>
-struct has_FlowSequenceTraits
+struct has_SequenceMethodTraits
{
typedef size_t (*Signature_size)(class IO&, T&);
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> >(nullptr)) == 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 = std::is_class<T>::value>
+class has_FlowTraits
+{
+public:
+ static const bool value = false;
+};
+
+// 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_FlowTraits<T, true>
+{
+ struct Fallback { bool flow; };
+ struct Derived : T, Fallback { };
+
+ template<typename C>
+ static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];
+
+ 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>(nullptr)) == 2;
};
+
+// Test if SequenceTraits<T> is defined on type T
+template<typename T>
+struct has_SequenceTraits : public std::integral_constant<bool,
+ has_SequenceMethodTraits<T>::value > { };
+
+
// Test if DocumentListTraits<T> is defined on type T
template <class T>
struct has_DocumentListTraits
static double test(...);
public:
- static bool const value = (sizeof(test<DocumentListTraits<T> >(0)) == 1);
+ static bool const value = (sizeof(test<DocumentListTraits<T> >(nullptr))==1);
};
+inline bool isNumber(StringRef S) {
+ static const char OctalChars[] = "01234567";
+ if (S.startswith("0") &&
+ S.drop_front().find_first_not_of(OctalChars) == StringRef::npos)
+ return true;
+
+ if (S.startswith("0o") &&
+ S.drop_front(2).find_first_not_of(OctalChars) == StringRef::npos)
+ return true;
+
+ static const char HexChars[] = "0123456789abcdefABCDEF";
+ if (S.startswith("0x") &&
+ S.drop_front(2).find_first_not_of(HexChars) == StringRef::npos)
+ return true;
+
+ static const char DecChars[] = "0123456789";
+ if (S.find_first_not_of(DecChars) == StringRef::npos)
+ return true;
+
+ if (S.equals(".inf") || S.equals(".Inf") || S.equals(".INF"))
+ return true;
+
+ Regex FloatMatcher("^(\\.[0-9]+|[0-9]+(\\.[0-9]*)?)([eE][-+]?[0-9]+)?$");
+ if (FloatMatcher.match(S))
+ return true;
+
+ return false;
+}
+
+inline bool isNumeric(StringRef S) {
+ if ((S.front() == '-' || S.front() == '+') && isNumber(S.drop_front()))
+ return true;
+
+ if (isNumber(S))
+ return true;
+
+ if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN"))
+ return true;
+
+ return false;
+}
+inline bool isNull(StringRef S) {
+ return S.equals("null") || S.equals("Null") || S.equals("NULL") ||
+ S.equals("~");
+}
+
+inline bool isBool(StringRef S) {
+ return S.equals("true") || S.equals("True") || S.equals("TRUE") ||
+ S.equals("false") || S.equals("False") || S.equals("FALSE");
+}
+
+inline bool needsQuotes(StringRef S) {
+ if (S.empty())
+ return true;
+ if (isspace(S.front()) || isspace(S.back()))
+ return true;
+ if (S.front() == ',')
+ return true;
+
+ static const char ScalarSafeChars[] =
+ "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-/^., \t";
+ if (S.find_first_not_of(ScalarSafeChars) != StringRef::npos)
+ return true;
+
+ if (isNull(S))
+ return true;
+ if (isBool(S))
+ return true;
+ if (isNumeric(S))
+ return true;
+
+ return false;
+}
template<typename T>
-struct missingTraits : public llvm::integral_constant<bool,
+struct missingTraits : public std::integral_constant<bool,
!has_ScalarEnumerationTraits<T>::value
&& !has_ScalarBitSetTraits<T>::value
&& !has_ScalarTraits<T>::value
&& !has_MappingTraits<T>::value
&& !has_SequenceTraits<T>::value
- && !has_FlowSequenceTraits<T>::value
&& !has_DocumentListTraits<T>::value > {};
+template<typename T>
+struct validatedMappingTraits : public std::integral_constant<bool,
+ has_MappingTraits<T>::value
+ && has_MappingValidateTraits<T>::value> {};
+template<typename T>
+struct unvalidatedMappingTraits : public std::integral_constant<bool,
+ has_MappingTraits<T>::value
+ && !has_MappingValidateTraits<T>::value> {};
// Base class for Input and Output.
class IO {
public:
- IO(void *Ctxt=NULL);
+ IO(void *Ctxt=nullptr);
virtual ~IO();
virtual bool outputting() = 0;
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;
virtual bool bitSetMatch(const char*, bool) = 0;
virtual void endBitSetScalar() = 0;
- virtual void scalarString(StringRef &) = 0;
+ virtual void scalarString(StringRef &, bool) = 0;
virtual void setError(const Twine &) = 0;
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;
}
}
// 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;
}
}
// 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;
}
}
+ template <typename T>
+ void maskedBitSetCase(T &Val, const char *Str, T ConstVal, T Mask) {
+ if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
+ Val = Val | ConstVal;
+ }
+
+ template <typename T>
+ void maskedBitSetCase(T &Val, const char *Str, uint32_t ConstVal,
+ uint32_t Mask) {
+ if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
+ Val = Val | ConstVal;
+ }
+
void *getContext();
void setContext(void *);
}
template <typename T>
- typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
+ typename std::enable_if<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);
}
template <typename T>
- typename llvm::enable_if_c<!has_SequenceTraits<T>::value,void>::type
+ void mapOptional(const char* Key, Optional<T> &Val) {
+ processKeyWithDefault(Key, Val, Optional<T>(), /*Required=*/false);
+ }
+
+ template <typename T>
+ typename std::enable_if<!has_SequenceTraits<T>::value,void>::type
mapOptional(const char* Key, T& Val) {
this->processKey(Key, Val, false);
}
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, Optional<T> &Val,
+ const Optional<T> &DefaultValue, bool Required) {
+ assert(DefaultValue.hasValue() == false &&
+ "Optional<T> shouldn't have a value!");
+ void *SaveInfo;
+ bool UseDefault;
+ const bool sameAsDefault = outputting() && !Val.hasValue();
+ if (!outputting() && !Val.hasValue())
+ Val = T();
+ if (this->preflightKey(Key, Required, sameAsDefault, UseDefault,
+ SaveInfo)) {
+ yamlize(*this, Val.getValue(), Required);
+ this->postflightKey(SaveInfo);
+ } else {
+ if (UseDefault)
+ Val = DefaultValue;
+ }
+ }
+
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);
template<typename T>
-typename llvm::enable_if_c<has_ScalarEnumerationTraits<T>::value,void>::type
+typename std::enable_if<has_ScalarEnumerationTraits<T>::value,void>::type
yamlize(IO &io, T &Val, bool) {
io.beginEnumScalar();
ScalarEnumerationTraits<T>::enumeration(io, Val);
}
template<typename T>
-typename llvm::enable_if_c<has_ScalarBitSetTraits<T>::value,void>::type
+typename std::enable_if<has_ScalarBitSetTraits<T>::value,void>::type
yamlize(IO &io, T &Val, bool) {
bool DoClear;
if ( io.beginBitSetScalar(DoClear) ) {
template<typename T>
-typename llvm::enable_if_c<has_ScalarTraits<T>::value,void>::type
+typename std::enable_if<has_ScalarTraits<T>::value,void>::type
yamlize(IO &io, T &Val, bool) {
if ( io.outputting() ) {
std::string Storage;
llvm::raw_string_ostream Buffer(Storage);
ScalarTraits<T>::output(Val, io.getContext(), Buffer);
StringRef Str = Buffer.str();
- io.scalarString(Str);
+ io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
}
else {
StringRef Str;
- io.scalarString(Str);
+ io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
if ( !Result.empty() ) {
io.setError(llvm::Twine(Result));
template<typename T>
-typename llvm::enable_if_c<has_MappingTraits<T>::value, void>::type
+typename std::enable_if<validatedMappingTraits<T>::value, void>::type
+yamlize(IO &io, T &Val, bool) {
+ io.beginMapping();
+ if (io.outputting()) {
+ StringRef Err = MappingTraits<T>::validate(io, Val);
+ if (!Err.empty()) {
+ llvm::errs() << Err << "\n";
+ assert(Err.empty() && "invalid struct trying to be written as yaml");
+ }
+ }
+ MappingTraits<T>::mapping(io, Val);
+ if (!io.outputting()) {
+ StringRef Err = MappingTraits<T>::validate(io, Val);
+ if (!Err.empty())
+ io.setError(Err);
+ }
+ io.endMapping();
+}
+
+template<typename T>
+typename std::enable_if<unvalidatedMappingTraits<T>::value, void>::type
yamlize(IO &io, T &Val, bool) {
io.beginMapping();
MappingTraits<T>::mapping(io, Val);
io.endMapping();
}
-#ifndef BUILDING_YAMLIO
template<typename T>
-typename llvm::enable_if_c<missingTraits<T>::value, void>::type
+typename std::enable_if<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
+typename std::enable_if<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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef S) { return needsQuotes(S); }
+};
+
+template<>
+struct ScalarTraits<std::string> {
+ static void output(const std::string &, void*, llvm::raw_ostream &);
+ static StringRef input(StringRef, void*, std::string &);
+ static bool mustQuote(StringRef S) { return needsQuotes(S); }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
-#endif
template <typename TNorm, typename TFinal>
struct MappingNormalization {
MappingNormalization(IO &i_o, TFinal &Obj)
- : io(i_o), BufPtr(NULL), Result(Obj) {
+ : io(i_o), BufPtr(nullptr), Result(Obj) {
if ( io.outputting() ) {
BufPtr = new (&Buffer) TNorm(io, Obj);
}
TNorm* operator->() { return BufPtr; }
private:
- //typedef typename llvm::AlignedCharArrayUnion<TNorm> Storage;
- //Storage Buffer;
- char Buffer[sizeof(TNorm)];
+ typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
+
+ Storage Buffer;
IO &io;
TNorm *BufPtr;
TFinal &Result;
TNorm* operator->() { return BufPtr; }
private:
- //typedef typename llvm::AlignedCharArrayUnion<TNorm> Storage;
- //Storage Buffer;
- char Buffer[sizeof(TNorm)];
+ typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
+
+ Storage Buffer;
IO &io;
TNorm *BufPtr;
TFinal &Result;
///
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 = nullptr,
+ SourceMgr::DiagHandlerTy DiagHandler = nullptr,
+ void *DiagHandlerCtxt = nullptr);
+ ~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 void beginMapping();
- virtual void endMapping();
- virtual bool preflightKey(const char *, bool, bool, bool &, void *&);
- virtual void postflightKey(void *);
- virtual unsigned beginSequence();
- virtual void endSequence();
- virtual bool preflightElement(unsigned index, void *&);
- virtual void postflightElement(void *);
- virtual unsigned beginFlowSequence();
- virtual bool preflightFlowElement(unsigned , void *&);
- virtual void postflightFlowElement(void *);
- virtual void endFlowSequence();
- virtual void beginEnumScalar();
- virtual bool matchEnumScalar(const char*, bool);
- virtual void endEnumScalar();
- virtual bool beginBitSetScalar(bool &);
- virtual bool bitSetMatch(const char *, bool );
- virtual void endBitSetScalar();
- virtual void scalarString(StringRef &);
- virtual void setError(const Twine &message);
+ bool outputting() override;
+ bool mapTag(StringRef, bool) override;
+ void beginMapping() override;
+ void endMapping() override;
+ bool preflightKey(const char *, bool, bool, bool &, void *&) override;
+ void postflightKey(void *) override;
+ unsigned beginSequence() override;
+ void endSequence() override;
+ bool preflightElement(unsigned index, void *&) override;
+ void postflightElement(void *) override;
+ unsigned beginFlowSequence() override;
+ bool preflightFlowElement(unsigned , void *&) override;
+ void postflightFlowElement(void *) override;
+ void endFlowSequence() override;
+ void beginEnumScalar() override;
+ bool matchEnumScalar(const char*, bool) override;
+ void endEnumScalar() override;
+ bool beginBitSetScalar(bool &) override;
+ bool bitSetMatch(const char *, bool ) override;
+ void endBitSetScalar() override;
+ void scalarString(StringRef &, bool) override;
+ void setError(const Twine &message) override;
+ bool canElideEmptySequence() override;
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 {
+ void anchor() override;
public:
EmptyHNode(Node *n) : HNode(n) { }
static inline bool classof(const HNode *n) {
};
class ScalarHNode : public HNode {
+ void anchor() override;
public:
ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
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);
class SequenceHNode : public HNode {
public:
SequenceHNode(Node *n) : HNode(n) { }
+ virtual ~SequenceHNode();
static inline bool classof(const HNode *n) {
return SequenceNode::classof(n->_node);
// These are only used by operator>>. They could be private
// if those templated things could be made friends.
bool setCurrentDocument();
- void nextDocument();
+ bool nextDocument();
private:
- llvm::yaml::Stream *Strm;
- llvm::SourceMgr SrcMgr;
- llvm::error_code EC;
- llvm::BumpPtrAllocator Allocator;
- llvm::yaml::document_iterator DocIterator;
- std::vector<bool> BitValuesUsed;
- HNode *CurrentNode;
- bool ScalarMatchFound;
+ llvm::SourceMgr SrcMgr; // must be before Strm
+ std::unique_ptr<llvm::yaml::Stream> Strm;
+ std::unique_ptr<HNode> TopNode;
+ llvm::error_code EC;
+ llvm::BumpPtrAllocator StringAllocator;
+ llvm::yaml::document_iterator DocIterator;
+ std::vector<bool> BitValuesUsed;
+ HNode *CurrentNode;
+ bool ScalarMatchFound;
};
///
class Output : public IO {
public:
- Output(llvm::raw_ostream &, void *Ctxt=NULL);
+ Output(llvm::raw_ostream &, void *Ctxt=nullptr);
virtual ~Output();
- virtual bool outputting();
- virtual void beginMapping();
- virtual void endMapping();
- virtual bool preflightKey(const char *key, bool, bool, bool &, void *&);
- virtual void postflightKey(void *);
- virtual unsigned beginSequence();
- virtual void endSequence();
- virtual bool preflightElement(unsigned, void *&);
- virtual void postflightElement(void *);
- virtual unsigned beginFlowSequence();
- virtual bool preflightFlowElement(unsigned, void *&);
- virtual void postflightFlowElement(void *);
- virtual void endFlowSequence();
- virtual void beginEnumScalar();
- virtual bool matchEnumScalar(const char*, bool);
- virtual void endEnumScalar();
- virtual bool beginBitSetScalar(bool &);
- virtual bool bitSetMatch(const char *, bool );
- virtual void endBitSetScalar();
- virtual void scalarString(StringRef &);
- virtual void setError(const Twine &message);
-
+ bool outputting() override;
+ bool mapTag(StringRef, bool) override;
+ void beginMapping() override;
+ void endMapping() override;
+ bool preflightKey(const char *key, bool, bool, bool &, void *&) override;
+ void postflightKey(void *) override;
+ unsigned beginSequence() override;
+ void endSequence() override;
+ bool preflightElement(unsigned, void *&) override;
+ void postflightElement(void *) override;
+ unsigned beginFlowSequence() override;
+ bool preflightFlowElement(unsigned, void *&) override;
+ void postflightFlowElement(void *) override;
+ void endFlowSequence() override;
+ void beginEnumScalar() override;
+ bool matchEnumScalar(const char*, bool) override;
+ void endEnumScalar() override;
+ bool beginBitSetScalar(bool &) override;
+ bool bitSetMatch(const char *, bool ) override;
+ void endBitSetScalar() override;
+ void scalarString(StringRef &, bool) override;
+ void setError(const Twine &message) override;
+ bool canElideEmptySequence() override;
public:
// These are only used by operator<<. They could be private
// if that templated operator could be made a friend.
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
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 &);
+ static bool mustQuote(StringRef) { return false; }
};
-#endif
// Define non-member operator>> so that Input can stream in a document list.
template <typename T>
inline
-typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Input &>::type
+typename std::enable_if<has_DocumentListTraits<T>::value, Input &>::type
operator>>(Input &yin, T &docList) {
int i = 0;
while ( yin.setCurrentDocument() ) {
// Define non-member operator>> so that Input can stream in a map as a document.
template <typename T>
inline
-typename llvm::enable_if_c<has_MappingTraits<T>::value,Input &>::type
+typename std::enable_if<has_MappingTraits<T>::value, Input &>::type
operator>>(Input &yin, T &docMap) {
yin.setCurrentDocument();
yamlize(yin, docMap, true);
// a document.
template <typename T>
inline
-typename llvm::enable_if_c<has_SequenceTraits<T>::value,Input &>::type
+typename std::enable_if<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
-typename llvm::enable_if_c<missingTraits<T>::value,Input &>::type
+typename std::enable_if<missingTraits<T>::value, Input &>::type
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.
template <typename T>
inline
-typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Output &>::type
+typename std::enable_if<has_DocumentListTraits<T>::value, Output &>::type
operator<<(Output &yout, T &docList) {
yout.beginDocuments();
const size_t count = DocumentListTraits<T>::size(yout, docList);
// Define non-member operator<< so that Output can stream out a map.
template <typename T>
inline
-typename llvm::enable_if_c<has_MappingTraits<T>::value,Output &>::type
+typename std::enable_if<has_MappingTraits<T>::value, Output &>::type
operator<<(Output &yout, T &map) {
yout.beginDocuments();
if ( yout.preflightDocument(0) ) {
// Define non-member operator<< so that Output can stream out a sequence.
template <typename T>
inline
-typename llvm::enable_if_c<has_SequenceTraits<T>::value,Output &>::type
+typename std::enable_if<has_SequenceTraits<T>::value, Output &>::type
operator<<(Output &yout, T &seq) {
yout.beginDocuments();
if ( yout.preflightDocument(0) ) {
return yout;
}
-#ifndef BUILDING_YAMLIO
// Provide better error message about types missing a trait specialization
template <typename T>
inline
-typename llvm::enable_if_c<missingTraits<T>::value,Output &>::type
+typename std::enable_if<missingTraits<T>::value, Output &>::type
operator<<(Output &yout, T &seq) {
char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
return yout;
}
-#endif
} // namespace yaml
return seq.size(); \
} \
static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
+ (void)flow; /* Remove this workaround after PR17897 is fixed */ \
if ( index >= seq.size() ) \
seq.resize(index+1); \
return seq[index]; \
-#endif // LLVM_YAML_TRAITS_H_
+#endif // LLVM_SUPPORT_YAMLTRAITS_H