44 #ifndef LLVM_SUPPORT_JSON_H 45 #define LLVM_SUPPORT_JSON_H 99 explicit Object() =
default;
103 explicit Object(std::initializer_list<KV> Properties);
115 template <
typename... Ts>
119 template <
typename... Ts>
147 return !(LHS == RHS);
153 std::vector<Value> V;
160 explicit Array() =
default;
161 explicit Array(std::initializer_list<Value> Elements);
162 template <
typename Collection>
explicit Array(
const Collection &
C) {
163 for (
const auto &V : C)
181 bool empty()
const {
return V.empty(); }
182 size_t size()
const {
return V.size(); }
188 V.emplace_back(std::forward<Args>(A)...);
194 return V.insert(P, std::move(
E));
197 return V.insert(P, A, Z);
200 return V.emplace(P, std::forward<Args>(A)...);
293 Value(std::initializer_list<Value> Elements);
295 create<json::Array>(std::move(Elements));
298 create<json::Object>(std::move(Properties));
303 assert(
false &&
"Invalid UTF-8 in value used as JSON");
306 create<std::string>(std::move(V));
313 create<llvm::StringRef>(V);
315 assert(
false &&
"Invalid UTF-8 in value used as JSON");
325 typename =
typename std::enable_if<std::is_same<T, bool>::value>
::type,
333 typename =
typename std::enable_if<std::is_integral<T>::value>::type,
334 typename =
typename std::enable_if<!std::is_same<T, bool>::value>::type>
336 create<int64_t>(int64_t{I});
339 template <
typename T,
341 typename std::enable_if<std::is_floating_point<T>::value>::type,
344 create<double>(
double{D});
347 template <
typename T,
348 typename =
typename std::enable_if<std::is_same<
349 Value, decltype(
toJSON(*(
const T *)
nullptr))>::value>,
351 Value(
const T &V) : Value(toJSON(V)) {}
360 moveFrom(std::move(M));
400 return as<int64_t>();
406 return as<int64_t>();
408 double D = as<double>();
410 D >= double(std::numeric_limits<int64_t>::min()) &&
417 if (
Type == T_String)
420 return as<llvm::StringRef>();
443 void copyFrom(
const Value &M);
447 void moveFrom(
const Value &&M);
451 template <
typename T,
typename... U>
void create(U &&... V) {
452 new (
reinterpret_cast<T *
>(
Union.buffer))
T(std::forward<U>(V)...);
454 template <
typename T> T &as()
const {
455 return *
reinterpret_cast<T *
>(
Union.buffer);
458 template <
typename Indenter>
462 enum ValueType :
char {
473 mutable ValueType
Type;
479 bool operator==(
const Value &,
const Value &);
480 inline bool operator!=(
const Value &L,
const Value &R) {
return !(L == R); }
492 assert(
false &&
"Invalid UTF-8 in value used as JSON");
493 *Owned =
fixUTF8(std::move(*Owned));
499 assert(
false &&
"Invalid UTF-8 in value used as JSON");
511 Owned.reset(
new std::string(*C.Owned));
521 std::string
str()
const {
return Data.str(); }
526 std::unique_ptr<std::string> Owned;
546 for (
const auto &
P : Properties) {
549 R.first->getSecond().moveFrom(std::move(
P.V));
558 inline bool fromJSON(
const Value &
E, std::string &Out) {
601 Out = std::move(Result);
604 template <
typename T>
bool fromJSON(
const Value &
E, std::vector<T> &Out) {
607 Out.resize(A->size());
608 for (
size_t I = 0;
I < A->size(); ++
I)
615 template <
typename T>
616 bool fromJSON(
const Value &
E, std::map<std::string, T> &Out) {
619 for (
const auto &KV : *
O)
629 return Opt ? Value(*Opt) : Value(
nullptr);
650 operator bool() {
return O; }
654 assert(*
this &&
"Must check this is an object before calling map()");
655 if (
const Value *
E =
O->get(Prop))
663 assert(*
this &&
"Must check this is an object before calling map()");
664 if (
const Value *
E =
O->get(Prop))
681 unsigned Line, Column,
Offset;
685 ParseError(
const char *Msg,
unsigned Line,
unsigned Column,
unsigned Offset)
686 : Msg(Msg), Line(Line), Column(Column), Offset(Offset) {}
688 OS <<
llvm::formatv(
"[{0}:{1}, byte={2}]: {3}", Line, Column, Offset, Msg);
An Object is a JSON object, which maps strings to heterogenous JSON values.
llvm::Optional< std::nullptr_t > getAsNull() const
const json::Array * getAsArray() const
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
const Value & back() const
void log(llvm::raw_ostream &OS) const override
Print an error message to an output stream.
bool map(StringRef Prop, T &Out)
Maps a property to a field, if it exists.
const_iterator begin() const
Compute iterated dominance frontiers using a linear time algorithm.
Value & operator=(Value &&M)
ObjectKey(const ObjectKey &C)
const json::Array * getArray(StringRef K) const
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
An Array is a JSON array, which contains heterogeneous JSON values.
const Value & front() const
llvm::Optional< bool > getAsBoolean() const
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Value(json::Array &&Elements)
void push_back(const Value &E)
bool isUTF8(llvm::StringRef S, size_t *ErrOffset=nullptr)
Returns true if S is valid UTF-8, which is required for use as JSON.
std::pair< iterator, bool > try_emplace(const ObjectKey &K, Ts &&... Args)
DenseMapIterator< ObjectKey, Value, llvm::DenseMapInfo< StringRef >, detail::DenseMapPair< ObjectKey, Value >, true > const_iterator
ObjectKey(llvm::StringRef S)
const json::Object * getAsObject() const
llvm::Optional< int64_t > getAsInteger() const
llvm::Optional< double > getNumber(StringRef K) const
ObjectKey(const llvm::formatv_object_base &V)
Tagged union holding either a T or a Error.
llvm::Optional< llvm::StringRef > getString(StringRef K) const
bool fromJSON(const Value &E, std::string &Out)
llvm::Optional< std::nullptr_t > getNull(StringRef K) const
detail::DenseMapPair< ObjectKey, Value > value_type
ObjectMapper(const Value &E)
void emplace_back(Args &&... A)
ObjectKey is a used to capture keys in Object.
Value(const llvm::formatv_object_base &V)
bool operator==(const Object &LHS, const Object &RHS)
Value toJSON(const llvm::Optional< T > &Opt)
const json::Object * getObject(StringRef K) const
const_iterator find(StringRef K) const
void push_back(Value &&E)
bool map(StringRef Prop, llvm::Optional< T > &Out)
Maps a property to a field, if it exists.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
The instances of the Type class are immutable: once they are created, they are never changed...
A Value is an JSON value of unknown type.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
ParseError(const char *Msg, unsigned Line, unsigned Column, unsigned Offset)
#define LLVM_LIKELY(EXPR)
Number values can store both int64s and doubles at full precision, depending on what they were constr...
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_UNLIKELY(EXPR)
llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
std::vector< Value >::const_iterator const_iterator
std::vector< Value >::iterator iterator
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
ObjectKey & operator=(const ObjectKey &C)
Array(const Collection &C)
llvm::Optional< llvm::StringRef > getAsString() const
std::string fixUTF8(llvm::StringRef S)
Replaces invalid UTF-8 sequences in S with the replacement character (U+FFFD).
Value(json::Object &&Properties)
const_iterator begin() const
iterator insert(iterator P, Value &&E)
const Value & operator[](size_t I) const
bool operator!=(const Object &LHS, const Object &RHS)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::Optional< int64_t > getInteger(StringRef K) const
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
iterator insert(iterator P, const Value &E)
llvm::Optional< double > getAsNumber() const
This union template exposes a suitably aligned and sized character array member which can hold elemen...
Base class for user error types.
Value(const llvm::SmallVectorImpl< char > &V)
ObjectKey(const llvm::SmallVectorImpl< char > &V)
iterator find(StringRef K)
const_iterator end() const
Value & operator[](const ObjectKey &K)
DenseMapIterator< ObjectKey, Value, llvm::DenseMapInfo< StringRef >, detail::DenseMapPair< ObjectKey, Value > > iterator
iterator insert(iterator P, It A, It Z)
iterator emplace(const_iterator P, Args &&... A)
std::pair< iterator, bool > try_emplace(ObjectKey &&K, Ts &&... Args)
json::Object * getAsObject()
Helper for mapping JSON objects onto protocol structs.
LLVM_NODISCARD bool empty() const
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive, key type.
llvm::Optional< bool > getBoolean(StringRef K) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value & operator=(const Value &M)
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
friend bool operator==(const Array &L, const Array &R)
Value & operator[](size_t I)
const_iterator end() const
llvm::raw_ostream & operator<<(llvm::raw_ostream &, const Value &)
const Value * data() const
bool operator<(const ObjectKey &L, const ObjectKey &R)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
json::Array * getAsArray()
std::pair< iterator, bool > insert(KV E)