46 #ifndef LLVM_SUPPORT_JSON_H 47 #define LLVM_SUPPORT_JSON_H 105 explicit Object(std::initializer_list<KV> Properties);
117 template <
typename... Ts>
121 template <
typename... Ts>
151 return !(LHS == RHS);
157 std::vector<Value> V;
165 explicit Array(std::initializer_list<Value> Elements);
166 template <
typename Collection>
explicit Array(
const Collection &
C) {
167 for (
const auto &V : C)
185 bool empty()
const {
return V.empty(); }
186 size_t size()
const {
return V.size(); }
193 V.emplace_back(std::forward<Args>(A)...);
199 return V.insert(P, std::move(
E));
202 return V.insert(P, A, Z);
205 return V.emplace(P, std::forward<Args>(A)...);
298 Value(std::initializer_list<Value> Elements);
300 create<json::Array>(std::move(Elements));
302 template <
typename Elt>
305 create<json::Object>(std::move(Properties));
307 template <
typename Elt>
312 assert(
false &&
"Invalid UTF-8 in value used as JSON");
315 create<std::string>(std::move(V));
322 create<llvm::StringRef>(V);
324 assert(
false &&
"Invalid UTF-8 in value used as JSON");
334 typename =
typename std::enable_if<std::is_same<T, bool>::value>
::type,
342 typename =
typename std::enable_if<std::is_integral<T>::value>::type,
343 typename =
typename std::enable_if<!std::is_same<T, bool>::value>::type>
345 create<int64_t>(int64_t{I});
348 template <
typename T,
350 typename std::enable_if<std::is_floating_point<T>::value>::type,
353 create<double>(
double{D});
356 template <
typename T,
357 typename =
typename std::enable_if<std::is_same<
358 Value, decltype(
toJSON(*(
const T *)
nullptr))>::value>,
360 Value(
const T &V) : Value(toJSON(V)) {}
369 moveFrom(std::move(M));
409 return as<int64_t>();
415 return as<int64_t>();
417 double D = as<double>();
419 D >= double(std::numeric_limits<int64_t>::min()) &&
426 if (
Type == T_String)
429 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);
478 mutable ValueType
Type;
482 friend bool operator==(
const Value &,
const Value &);
485 bool operator==(
const Value &,
const Value &);
486 inline bool operator!=(
const Value &L,
const Value &
R) {
return !(L ==
R); }
497 assert(
false &&
"Invalid UTF-8 in value used as JSON");
498 *Owned =
fixUTF8(std::move(*Owned));
504 assert(
false &&
"Invalid UTF-8 in value used as JSON");
516 Owned.reset(
new std::string(*C.Owned));
526 std::string
str()
const {
return Data.str(); }
531 std::unique_ptr<std::string> Owned;
551 for (
const auto &
P : Properties) {
554 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)
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);
761 : OS(OS), IndentSize(IndentSize) {
762 Stack.emplace_back();
765 assert(Stack.size() == 1 &&
"Unmatched begin()/end()");
766 assert(Stack.back().Ctx == Singleton);
767 assert(Stack.back().HasValue &&
"Did not write top-level value");
778 void value(
const Value &V);
797 attributeImpl(Key, [&] { value(Contents); });
801 attributeImpl(Key, [&] { array(Contents); });
805 attributeImpl(Key, [&] {
object(Contents); });
834 Context Ctx = Singleton;
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
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
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)
#define LLVM_UNLIKELY(EXPR)
#define LLVM_LIKELY(EXPR)
void attributeObject(llvm::StringRef Key, Block Contents)
Emit an attribute whose value is an object with attributes from the Block.
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.
void array(Block Contents)
Emit an array whose elements are emitted in the provided Block.
detail::ValueMatchesPoly< M > HasValue(M Matcher)
An Array is a JSON array, which contains heterogeneous JSON values.
An efficient, type-erasing, non-owning reference to a callable.
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)
OStream(llvm::raw_ostream &OS, unsigned IndentSize=0)
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)
void attributeArray(llvm::StringRef Key, Block Contents)
Emit an attribute whose value is an array with elements from the Block.
DenseMapIterator< ObjectKey, Value, llvm::DenseMapInfo< StringRef >, llvm::detail::DenseMapPair< ObjectKey, Value >, true > const_iterator
ObjectKey(llvm::StringRef S)
const json::Object * getAsObject() const
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
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)
void flush()
Flushes the underlying ostream. OStream does not buffer internally.
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)
void attribute(llvm::StringRef Key, const Value &Contents)
Emit an attribute whose value is self-contained (number, vector<int> etc).
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
bool erase(const KeyT &Val)
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.
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Value &V)
Serializes this Value to JSON, writing it to the provided stream.
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...
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)
Align max(MaybeAlign Lhs, Align Rhs)
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
void object(Block Contents)
Emit an object whose elements are emitted in the provided Block.
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
A suitably aligned and sized character array member which can hold elements of any type...
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...
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
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()
void value(const Value &V)
Emit a self-contained value (number, string, vector<string> etc).
std::pair< iterator, bool > insert(KV E)