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));
297 template <
typename Elt>
300 create<json::Object>(std::move(Properties));
302 template <
typename Elt>
307 assert(
false &&
"Invalid UTF-8 in value used as JSON");
310 create<std::string>(std::move(V));
317 create<llvm::StringRef>(V);
319 assert(
false &&
"Invalid UTF-8 in value used as JSON");
329 typename =
typename std::enable_if<std::is_same<T, bool>::value>
::type,
337 typename =
typename std::enable_if<std::is_integral<T>::value>::type,
338 typename =
typename std::enable_if<!std::is_same<T, bool>::value>::type>
340 create<int64_t>(int64_t{I});
343 template <
typename T,
345 typename std::enable_if<std::is_floating_point<T>::value>::type,
348 create<double>(
double{D});
351 template <
typename T,
352 typename =
typename std::enable_if<std::is_same<
353 Value, decltype(
toJSON(*(
const T *)
nullptr))>::value>,
355 Value(
const T &V) : Value(toJSON(V)) {}
364 moveFrom(std::move(M));
404 return as<int64_t>();
410 return as<int64_t>();
412 double D = as<double>();
414 D >= double(std::numeric_limits<int64_t>::min()) &&
421 if (
Type == T_String)
424 return as<llvm::StringRef>();
447 void copyFrom(
const Value &M);
451 void moveFrom(
const Value &&M);
455 template <
typename T,
typename... U>
void create(U &&... V) {
456 new (
reinterpret_cast<T *
>(Union.buffer))
T(std::forward<U>(V)...);
458 template <
typename T> T &as()
const {
461 void *Storage =
static_cast<void *
>(Union.buffer);
462 return *
static_cast<T *
>(Storage);
465 template <
typename Indenter>
469 enum ValueType :
char {
480 mutable ValueType
Type;
484 friend bool operator==(
const Value &,
const Value &);
487 bool operator==(
const Value &,
const Value &);
488 inline bool operator!=(
const Value &L,
const Value &R) {
return !(L == R); }
500 assert(
false &&
"Invalid UTF-8 in value used as JSON");
501 *Owned =
fixUTF8(std::move(*Owned));
507 assert(
false &&
"Invalid UTF-8 in value used as JSON");
519 Owned.reset(
new std::string(*C.Owned));
529 std::string
str()
const {
return Data.str(); }
534 std::unique_ptr<std::string> Owned;
554 for (
const auto &
P : Properties) {
557 R.first->getSecond().moveFrom(std::move(
P.V));
566 inline bool fromJSON(
const Value &
E, std::string &Out) {
609 Out = std::move(Result);
612 template <
typename T>
bool fromJSON(
const Value &
E, std::vector<T> &Out) {
615 Out.resize(A->size());
616 for (
size_t I = 0;
I < A->size(); ++
I)
623 template <
typename T>
624 bool fromJSON(
const Value &
E, std::map<std::string, T> &Out) {
627 for (
const auto &KV : *
O)
637 return Opt ? Value(*Opt) : Value(
nullptr);
658 operator bool() {
return O; }
662 assert(*
this &&
"Must check this is an object before calling map()");
663 if (
const Value *
E =
O->get(Prop))
671 assert(*
this &&
"Must check this is an object before calling map()");
672 if (
const Value *
E =
O->get(Prop))
689 unsigned Line, Column,
Offset;
693 ParseError(
const char *Msg,
unsigned Line,
unsigned Column,
unsigned Offset)
694 : Msg(Msg), Line(Line), Column(Column), Offset(Offset) {}
696 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
This class represents lattice values for constants.
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)
#define LLVM_UNLIKELY(EXPR)
DenseMapIterator< ObjectKey, Value, llvm::DenseMapInfo< StringRef >, llvm::detail::DenseMapPair< ObjectKey, Value >, true > const_iterator
ObjectKey(llvm::StringRef S)
const json::Object * getAsObject() const
Value(const std::map< std::string, Elt > &C)
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
llvm::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)
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)
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 >, llvm::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...
#define LLVM_LIKELY(EXPR)
StringRef - Represent a constant reference to a string, i.e.
Value(const std::vector< Elt > &C)
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)