21 return try_emplace(std::move(K),
nullptr).first->getSecond();
37 return V->getAsNull();
42 return V->getAsBoolean();
47 return V->getAsNumber();
52 return V->getAsInteger();
57 return V->getAsString();
62 return V->getAsObject();
67 return V->getAsObject();
72 return V->getAsArray();
77 return V->getAsArray();
83 for (
const auto &L : LHS) {
84 auto R = RHS.
find(L.first);
85 if (R == RHS.
end() || L.second != R->second)
92 V.reserve(Elements.size());
93 for (
const Value &V : Elements) {
94 emplace_back(
nullptr);
95 back().moveFrom(std::move(V));
102 void Value::copyFrom(
const Value &M) {
109 memcpy(Union.buffer, M.Union.buffer,
sizeof(Union.buffer));
115 create<std::string>(M.as<std::string>());
126 void Value::moveFrom(
const Value &&M) {
133 memcpy(Union.buffer, M.Union.buffer,
sizeof(Union.buffer));
139 create<std::string>(std::move(M.as<std::string>()));
147 create<json::Array>(std::move(M.as<
json::Array>()));
153 void Value::destroy() {
167 as<json::Object>().~
Object();
170 as<json::Array>().~
Array();
188 if (L.Type == Value::T_Integer || R.Type == Value::T_Integer)
212 P = Start + ErrOffset;
213 return parseError(
"Invalid UTF-8 sequence");
216 bool parseValue(
Value &Out);
222 return parseError(
"Text after end of document");
227 return std::move(*Err);
231 void eatWhitespace() {
232 while (
P != End && (*
P ==
' ' || *
P ==
'\r' || *
P ==
'\n' || *
P ==
'\t'))
237 bool parseNumber(
char First,
Value &Out);
238 bool parseString(std::string &Out);
239 bool parseUnicode(std::string &Out);
240 bool parseError(
const char *Msg);
242 char next() {
return P == End ? 0 : *
P++; }
243 char peek() {
return P == End ? 0 : *
P; }
244 static bool isNumber(
char C) {
245 return C ==
'0' || C ==
'1' || C ==
'2' || C ==
'3' || C ==
'4' ||
246 C ==
'5' || C ==
'6' || C ==
'7' || C ==
'8' || C ==
'9' ||
247 C ==
'e' || C ==
'E' || C ==
'+' || C ==
'-' || C ==
'.';
251 const char *Start, *
P, *End;
254 bool Parser::parseValue(
Value &Out) {
257 return parseError(
"Unexpected EOF");
258 switch (
char C = next()) {
262 return (next() ==
'u' && next() ==
'l' && next() ==
'l') ||
263 parseError(
"Invalid JSON value (null?)");
266 return (next() ==
'r' && next() ==
'u' && next() ==
'e') ||
267 parseError(
"Invalid JSON value (true?)");
270 return (next() ==
'a' && next() ==
'l' && next() ==
's' && next() ==
'e') ||
271 parseError(
"Invalid JSON value (false?)");
274 if (parseString(S)) {
290 if (!parseValue(A.
back()))
300 return parseError(
"Expected , or ] after array element");
314 return parseError(
"Expected object key");
320 return parseError(
"Expected : after object key");
322 if (!parseValue(O[std::move(K)]))
332 return parseError(
"Expected , or } after object property");
338 return parseNumber(
C, Out);
339 return parseError(
"Invalid JSON value");
343 bool Parser::parseNumber(
char First,
Value &Out) {
347 while (isNumber(
peek()))
352 auto I = std::strtoll(S.
c_str(), &End, 10);
353 if (End == S.
end() &&
I >= std::numeric_limits<int64_t>::min() &&
359 Out = std::strtod(S.
c_str(), &End);
360 return End == S.
end() || parseError(
"Invalid JSON value (number?)");
363 bool Parser::parseString(std::string &Out) {
365 for (
char C = next();
C !=
'"';
C = next()) {
367 return parseError(
"Unterminated string");
369 return parseError(
"Control character in string");
375 switch (
C = next()) {
397 if (!parseUnicode(Out))
401 return parseError(
"Invalid escape sequence");
407 static void encodeUtf8(
uint32_t Rune, std::string &Out) {
409 Out.push_back(Rune & 0x7F);
410 }
else if (Rune < 0x800) {
411 uint8_t FirstByte = 0xC0 | ((Rune & 0x7C0) >> 6);
412 uint8_t SecondByte = 0x80 | (Rune & 0x3F);
413 Out.push_back(FirstByte);
414 Out.push_back(SecondByte);
415 }
else if (Rune < 0x10000) {
416 uint8_t FirstByte = 0xE0 | ((Rune & 0xF000) >> 12);
417 uint8_t SecondByte = 0x80 | ((Rune & 0xFC0) >> 6);
418 uint8_t ThirdByte = 0x80 | (Rune & 0x3F);
419 Out.push_back(FirstByte);
420 Out.push_back(SecondByte);
421 Out.push_back(ThirdByte);
422 }
else if (Rune < 0x110000) {
423 uint8_t FirstByte = 0xF0 | ((Rune & 0x1F0000) >> 18);
424 uint8_t SecondByte = 0x80 | ((Rune & 0x3F000) >> 12);
425 uint8_t ThirdByte = 0x80 | ((Rune & 0xFC0) >> 6);
426 uint8_t FourthByte = 0x80 | (Rune & 0x3F);
427 Out.push_back(FirstByte);
428 Out.push_back(SecondByte);
429 Out.push_back(ThirdByte);
430 Out.push_back(FourthByte);
440 bool Parser::parseUnicode(std::string &Out) {
442 auto Invalid = [&] { Out.append( {
'\xef',
'\xbf',
'\xbd'}); };
444 auto Parse4Hex = [
this](
uint16_t &Out) ->
bool {
446 char Bytes[] = {next(), next(), next(), next()};
447 for (
unsigned char C : Bytes) {
448 if (!std::isxdigit(
C))
449 return parseError(
"Invalid \\u escape sequence");
451 Out |= (
C >
'9') ? (
C & ~0x20) -
'A' + 10 : (
C -
'0');
456 if (!Parse4Hex(First))
462 if (
LLVM_LIKELY(First < 0xD800 || First >= 0xE000)) {
463 encodeUtf8(First, Out);
481 if (!Parse4Hex(Second))
490 encodeUtf8(0x10000 | ((First - 0xD800) << 10) | (Second - 0xDC00), Out);
495 bool Parser::parseError(
const char *Msg) {
497 const char *StartOfLine = Start;
498 for (
const char *
X = Start;
X <
P; ++
X) {
505 std::make_unique<ParseError>(Msg, Line, P - StartOfLine, P - Start));
517 return P.takeError();
522 std::vector<const Object::value_type *> Elements;
523 for (
const auto &
E : O)
524 Elements.push_back(&
E);
527 return L->first < R->first;
542 *ErrOffset = Rest - Data;
548 std::vector<UTF32> Codepoints(S.
size());
549 const UTF8 *In8 =
reinterpret_cast<const UTF8 *
>(S.
data());
550 UTF32 *Out32 = Codepoints.data();
553 Codepoints.resize(Out32 - Codepoints.data());
554 std::string Res(4 * Codepoints.size(), 0);
555 const UTF32 *In32 = Codepoints.data();
556 UTF8 *Out8 =
reinterpret_cast<UTF8 *
>(&Res[0]);
559 Res.resize(reinterpret_cast<char *>(Out8) - Res.data());
565 for (
unsigned char C : S) {
566 if (
C == 0x22 ||
C == 0x5C)
605 if (V.Type == Value::T_Integer)
608 OS <<
format(
"%.*g", std::numeric_limits<double>::max_digits10,
623 attribute(
E->first,
E->second);
628 void llvm::json::OStream::valueBegin() {
629 assert(Stack.back().Ctx !=
Object &&
"Only attributes allowed here");
630 if (Stack.back().HasValue) {
631 assert(Stack.back().Ctx != Singleton &&
"Only one value allowed here");
634 if (Stack.back().Ctx ==
Array)
636 Stack.back().HasValue =
true;
639 void llvm::json::OStream::newline() {
648 Stack.emplace_back();
649 Stack.back().Ctx =
Array;
650 Indent += IndentSize;
656 Indent -= IndentSize;
657 if (Stack.back().HasValue)
666 Stack.emplace_back();
667 Stack.back().Ctx =
Object;
668 Indent += IndentSize;
674 Indent -= IndentSize;
675 if (Stack.back().HasValue)
684 if (Stack.back().HasValue)
687 Stack.back().HasValue =
true;
688 Stack.emplace_back();
689 Stack.back().Ctx = Singleton;
693 assert(
false &&
"Invalid UTF-8 in attribute key");
702 assert(Stack.back().Ctx == Singleton);
703 assert(Stack.back().HasValue &&
"Attribute must have a value");
713 unsigned IndentAmount = 0;
An Object is a JSON object, which maps strings to heterogenous JSON values.
llvm::Optional< std::nullptr_t > getAsNull() const
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
const json::Array * getAsArray() const
This class represents lattice values for constants.
#define LLVM_UNLIKELY(EXPR)
friend bool operator==(const Value &, const Value &)
#define LLVM_LIKELY(EXPR)
void push_back(const T &Elt)
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.
bool isASCII(char C)
Checks whether character C is valid ASCII (high bit is zero).
llvm::Optional< bool > getAsBoolean() const
bool isUTF8(llvm::StringRef S, size_t *ErrOffset=nullptr)
Returns true if S is valid UTF-8, which is required for use as JSON.
ConversionResult ConvertUTF8toUTF32(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Convert a partial UTF8 sequence to UTF32.
std::pair< iterator, bool > try_emplace(const ObjectKey &K, Ts &&... Args)
const json::Object * getAsObject() const
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
llvm::Optional< int64_t > getAsInteger() const
llvm::Optional< double > getNumber(StringRef K) const
Tagged union holding either a T or a Error.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
llvm::Optional< llvm::StringRef > getString(StringRef K) const
llvm::Optional< std::nullptr_t > getNull(StringRef K) const
static std::vector< const Object::value_type * > sortedElements(const Object &O)
void emplace_back(Args &&... A)
ObjectKey is a used to capture keys in Object.
LLVM_NODISCARD size_t size() const
size - Get the string size.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
void attributeBegin(llvm::StringRef Key)
bool operator==(const Object &LHS, const Object &RHS)
const json::Object * getObject(StringRef K) const
ConversionResult ConvertUTF32toUTF8(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
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")
Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd)
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.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void sort(IteratorTy Start, IteratorTy End)
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
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).
Align max(MaybeAlign Lhs, Align Rhs)
llvm::Optional< int64_t > getInteger(StringRef K) const
llvm::Optional< double > getAsNumber() const
void write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, Optional< size_t > Width=None)
iterator find(StringRef K)
Value & operator[](const ObjectKey &K)
This file supports working with JSON data.
llvm::Optional< bool > getBoolean(StringRef K) const
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
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.
static void quote(llvm::raw_ostream &OS, llvm::StringRef S)
static bool peek(struct InternalInstruction *insn, uint8_t &byte)
void value(const Value &V)
Emit a self-contained value (number, string, vector<string> etc).