aboutsummaryrefslogtreecommitdiff
path: root/include/llvm/Support/YAMLTraits.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/llvm/Support/YAMLTraits.h')
-rw-r--r--include/llvm/Support/YAMLTraits.h83
1 files changed, 61 insertions, 22 deletions
diff --git a/include/llvm/Support/YAMLTraits.h b/include/llvm/Support/YAMLTraits.h
index 3d790e96fff7..5181dc56d81d 100644
--- a/include/llvm/Support/YAMLTraits.h
+++ b/include/llvm/Support/YAMLTraits.h
@@ -1,9 +1,8 @@
//===- llvm/Support/YAMLTraits.h --------------------------------*- C++ -*-===//
//
-// The LLVM Linker
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
@@ -102,8 +101,7 @@ template <class T, class Context> struct MappingContextTraits {
/// io.enumCase(value, "green", cGreen);
/// }
/// };
-template<typename T>
-struct ScalarEnumerationTraits {
+template <typename T, typename Enable = void> struct ScalarEnumerationTraits {
// Must provide:
// static void enumeration(IO &io, T &value);
};
@@ -119,8 +117,7 @@ struct ScalarEnumerationTraits {
/// io.bitSetCase(value, "round", flagRound);
/// }
/// };
-template<typename T>
-struct ScalarBitSetTraits {
+template <typename T, typename Enable = void> struct ScalarBitSetTraits {
// Must provide:
// static void bitset(IO &io, T &value);
};
@@ -146,8 +143,7 @@ enum class QuotingType { None, Single, Double };
/// }
/// static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
/// };
-template<typename T>
-struct ScalarTraits {
+template <typename T, typename Enable = void> struct ScalarTraits {
// Must provide:
//
// Function to write the value as a string:
@@ -864,8 +860,8 @@ public:
mapOptionalWithContext(Key, Val, Ctx);
}
- template <typename T>
- void mapOptional(const char *Key, T &Val, const T &Default) {
+ template <typename T, typename DefaultT>
+ void mapOptional(const char *Key, T &Val, const DefaultT &Default) {
EmptyContext Ctx;
mapOptionalWithContext(Key, Val, Default, Ctx);
}
@@ -891,10 +887,13 @@ public:
this->processKey(Key, Val, false, Ctx);
}
- template <typename T, typename Context>
- void mapOptionalWithContext(const char *Key, T &Val, const T &Default,
+ template <typename T, typename Context, typename DefaultT>
+ void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default,
Context &Ctx) {
- this->processKeyWithDefault(Key, Val, Default, false, Ctx);
+ static_assert(std::is_convertible<DefaultT, T>::value,
+ "Default type must be implicitly convertible to value type!");
+ this->processKeyWithDefault(Key, Val, static_cast<const T &>(Default),
+ false, Ctx);
}
private:
@@ -978,7 +977,7 @@ yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
bool DoClear;
if ( io.beginBitSetScalar(DoClear) ) {
if ( DoClear )
- Val = static_cast<T>(0);
+ Val = T();
ScalarBitSetTraits<T>::bitset(io, Val);
io.endBitSetScalar();
}
@@ -1243,12 +1242,14 @@ struct ScalarTraits<double> {
static QuotingType mustQuote(StringRef) { return QuotingType::None; }
};
-// For endian types, we just use the existing ScalarTraits for the underlying
-// type. This way endian aware types are supported whenever a ScalarTraits
-// is defined for the underlying type.
+// For endian types, we use existing scalar Traits class for the underlying
+// type. This way endian aware types are supported whenever the traits are
+// defined for the underlying type.
template <typename value_type, support::endianness endian, size_t alignment>
-struct ScalarTraits<support::detail::packed_endian_specific_integral<
- value_type, endian, alignment>> {
+struct ScalarTraits<
+ support::detail::packed_endian_specific_integral<value_type, endian,
+ alignment>,
+ typename std::enable_if<has_ScalarTraits<value_type>::value>::type> {
using endian_type =
support::detail::packed_endian_specific_integral<value_type, endian,
alignment>;
@@ -1269,6 +1270,38 @@ struct ScalarTraits<support::detail::packed_endian_specific_integral<
}
};
+template <typename value_type, support::endianness endian, size_t alignment>
+struct ScalarEnumerationTraits<
+ support::detail::packed_endian_specific_integral<value_type, endian,
+ alignment>,
+ typename std::enable_if<
+ has_ScalarEnumerationTraits<value_type>::value>::type> {
+ using endian_type =
+ support::detail::packed_endian_specific_integral<value_type, endian,
+ alignment>;
+
+ static void enumeration(IO &io, endian_type &E) {
+ value_type V = E;
+ ScalarEnumerationTraits<value_type>::enumeration(io, V);
+ E = V;
+ }
+};
+
+template <typename value_type, support::endianness endian, size_t alignment>
+struct ScalarBitSetTraits<
+ support::detail::packed_endian_specific_integral<value_type, endian,
+ alignment>,
+ typename std::enable_if<has_ScalarBitSetTraits<value_type>::value>::type> {
+ using endian_type =
+ support::detail::packed_endian_specific_integral<value_type, endian,
+ alignment>;
+ static void bitset(IO &io, endian_type &E) {
+ value_type V = E;
+ ScalarBitSetTraits<value_type>::bitset(io, V);
+ E = V;
+ }
+};
+
// Utility for use within MappingTraits<>::mapping() method
// to [de]normalize an object for use with YAML conversion.
template <typename TNorm, typename TFinal>
@@ -1587,8 +1620,9 @@ private:
bool NeedBitValueComma = false;
bool NeedFlowSequenceComma = false;
bool EnumerationMatchFound = false;
- bool NeedsNewLine = false;
bool WriteDefaultValues = false;
+ StringRef Padding;
+ StringRef PaddingBeforeContainer;
};
/// YAML I/O does conversion based on types. But often native data types
@@ -1872,6 +1906,11 @@ struct SequenceTraits<SmallVector<T, N>,
typename std::enable_if<CheckIsBool<
SequenceElementTraits<T>::flow>::value>::type>
: SequenceTraitsImpl<SmallVector<T, N>, SequenceElementTraits<T>::flow> {};
+template <typename T>
+struct SequenceTraits<SmallVectorImpl<T>,
+ typename std::enable_if<CheckIsBool<
+ SequenceElementTraits<T>::flow>::value>::type>
+ : SequenceTraitsImpl<SmallVectorImpl<T>, SequenceElementTraits<T>::flow> {};
// Sequences of fundamental types use flow formatting.
template <typename T>