| File: | build/source/clang-tools-extra/clangd/index/YAMLSerialization.cpp |
| Warning: | line 144, column 5 1st function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | //===-- YAMLSerialization.cpp ------------------------------------*- C++-*-===// | |||
| 2 | // | |||
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
| 4 | // See https://llvm.org/LICENSE.txt for license information. | |||
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
| 6 | // | |||
| 7 | //===----------------------------------------------------------------------===// | |||
| 8 | // | |||
| 9 | // A YAML index file is a sequence of tagged entries. | |||
| 10 | // Each entry either encodes a Symbol or the list of references to a symbol | |||
| 11 | // (a "ref bundle"). | |||
| 12 | // | |||
| 13 | //===----------------------------------------------------------------------===// | |||
| 14 | ||||
| 15 | #include "Headers.h" | |||
| 16 | #include "index/Ref.h" | |||
| 17 | #include "index/Relation.h" | |||
| 18 | #include "index/Serialization.h" | |||
| 19 | #include "index/Symbol.h" | |||
| 20 | #include "index/SymbolLocation.h" | |||
| 21 | #include "index/SymbolOrigin.h" | |||
| 22 | #include "clang/Tooling/CompilationDatabase.h" | |||
| 23 | #include "llvm/ADT/StringRef.h" | |||
| 24 | #include "llvm/Support/Allocator.h" | |||
| 25 | #include "llvm/Support/StringSaver.h" | |||
| 26 | #include "llvm/Support/YAMLTraits.h" | |||
| 27 | #include "llvm/Support/raw_ostream.h" | |||
| 28 | #include <cstdint> | |||
| 29 | #include <optional> | |||
| 30 | ||||
| 31 | namespace { | |||
| 32 | struct YIncludeHeaderWithReferences; | |||
| 33 | } | |||
| 34 | ||||
| 35 | LLVM_YAML_IS_SEQUENCE_VECTOR(clang::clangd::Symbol::IncludeHeaderWithReferences)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <clang::clangd::Symbol::IncludeHeaderWithReferences> && !std::is_same_v<clang::clangd::Symbol::IncludeHeaderWithReferences , std::string> && !std::is_same_v<clang::clangd ::Symbol::IncludeHeaderWithReferences, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<clang::clangd ::Symbol::IncludeHeaderWithReferences> { static const bool flow = false; }; } } | |||
| 36 | LLVM_YAML_IS_SEQUENCE_VECTOR(clang::clangd::Ref)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <clang::clangd::Ref> && !std::is_same_v<clang ::clangd::Ref, std::string> && !std::is_same_v< clang::clangd::Ref, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<clang::clangd ::Ref> { static const bool flow = false; }; } } | |||
| 37 | LLVM_YAML_IS_SEQUENCE_VECTOR(YIncludeHeaderWithReferences)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <YIncludeHeaderWithReferences> && !std::is_same_v <YIncludeHeaderWithReferences, std::string> && ! std::is_same_v<YIncludeHeaderWithReferences, llvm::StringRef >, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<YIncludeHeaderWithReferences > { static const bool flow = false; }; } } | |||
| 38 | ||||
| 39 | namespace { | |||
| 40 | using RefBundle = | |||
| 41 | std::pair<clang::clangd::SymbolID, std::vector<clang::clangd::Ref>>; | |||
| 42 | // This is a pale imitation of std::variant<Symbol, RefBundle, Relation> | |||
| 43 | struct VariantEntry { | |||
| 44 | std::optional<clang::clangd::Symbol> Symbol; | |||
| 45 | std::optional<RefBundle> Refs; | |||
| 46 | std::optional<clang::clangd::Relation> Relation; | |||
| 47 | std::optional<clang::clangd::IncludeGraphNode> Source; | |||
| 48 | std::optional<clang::tooling::CompileCommand> Cmd; | |||
| 49 | }; | |||
| 50 | // A class helps YAML to serialize the 32-bit encoded position (Line&Column), | |||
| 51 | // as YAMLIO can't directly map bitfields. | |||
| 52 | struct YPosition { | |||
| 53 | uint32_t Line; | |||
| 54 | uint32_t Column; | |||
| 55 | }; | |||
| 56 | // A class helps YAML to serialize the IncludeHeaderWithReferences as YAMLIO | |||
| 57 | // can't directly map bitfields. | |||
| 58 | struct YIncludeHeaderWithReferences { | |||
| 59 | llvm::StringRef IncludeHeader; | |||
| 60 | uint32_t References; | |||
| 61 | clang::clangd::Symbol::IncludeDirective SupportedDirectives; | |||
| 62 | ||||
| 63 | YIncludeHeaderWithReferences() = default; | |||
| 64 | ||||
| 65 | YIncludeHeaderWithReferences( | |||
| 66 | llvm::StringRef IncludeHeader, uint32_t References, | |||
| 67 | clang::clangd::Symbol::IncludeDirective SupportedDirectives) | |||
| 68 | : IncludeHeader(IncludeHeader), References(References), | |||
| 69 | SupportedDirectives(SupportedDirectives) {} | |||
| 70 | }; | |||
| 71 | ||||
| 72 | // avoid ODR violation of specialization for non-owned CompileCommand | |||
| 73 | struct CompileCommandYAML : clang::tooling::CompileCommand {}; | |||
| 74 | ||||
| 75 | } // namespace | |||
| 76 | namespace llvm { | |||
| 77 | namespace yaml { | |||
| 78 | ||||
| 79 | using clang::clangd::FileDigest; | |||
| 80 | using clang::clangd::IncludeGraph; | |||
| 81 | using clang::clangd::IncludeGraphNode; | |||
| 82 | using clang::clangd::Ref; | |||
| 83 | using clang::clangd::RefKind; | |||
| 84 | using clang::clangd::Relation; | |||
| 85 | using clang::clangd::RelationKind; | |||
| 86 | using clang::clangd::Symbol; | |||
| 87 | using clang::clangd::SymbolID; | |||
| 88 | using clang::clangd::SymbolLocation; | |||
| 89 | using clang::index::SymbolInfo; | |||
| 90 | using clang::index::SymbolKind; | |||
| 91 | using clang::index::SymbolLanguage; | |||
| 92 | using clang::tooling::CompileCommand; | |||
| 93 | ||||
| 94 | // Helper to (de)serialize the SymbolID. We serialize it as a hex string. | |||
| 95 | struct NormalizedSymbolID { | |||
| 96 | NormalizedSymbolID(IO &) {} | |||
| 97 | NormalizedSymbolID(IO &, const SymbolID &ID) { | |||
| 98 | llvm::raw_string_ostream OS(HexString); | |||
| 99 | OS << ID; | |||
| 100 | } | |||
| 101 | ||||
| 102 | SymbolID denormalize(IO &I) { | |||
| 103 | auto ID = SymbolID::fromStr(HexString); | |||
| 104 | if (!ID) { | |||
| 105 | I.setError(llvm::toString(ID.takeError())); | |||
| 106 | return SymbolID(); | |||
| 107 | } | |||
| 108 | return *ID; | |||
| 109 | } | |||
| 110 | ||||
| 111 | std::string HexString; | |||
| 112 | }; | |||
| 113 | ||||
| 114 | struct NormalizedSymbolFlag { | |||
| 115 | NormalizedSymbolFlag(IO &) {} | |||
| 116 | NormalizedSymbolFlag(IO &, Symbol::SymbolFlag F) { | |||
| 117 | Flag = static_cast<uint8_t>(F); | |||
| 118 | } | |||
| 119 | ||||
| 120 | Symbol::SymbolFlag denormalize(IO &) { | |||
| 121 | return static_cast<Symbol::SymbolFlag>(Flag); | |||
| 122 | } | |||
| 123 | ||||
| 124 | uint8_t Flag = 0; | |||
| 125 | }; | |||
| 126 | ||||
| 127 | template <> struct MappingTraits<YPosition> { | |||
| 128 | static void mapping(IO &IO, YPosition &Value) { | |||
| 129 | IO.mapRequired("Line", Value.Line); | |||
| 130 | IO.mapRequired("Column", Value.Column); | |||
| 131 | } | |||
| 132 | }; | |||
| 133 | ||||
| 134 | struct NormalizedPosition { | |||
| 135 | using Position = clang::clangd::SymbolLocation::Position; | |||
| 136 | NormalizedPosition(IO &) {} | |||
| 137 | NormalizedPosition(IO &, const Position &Pos) { | |||
| 138 | P.Line = Pos.line(); | |||
| 139 | P.Column = Pos.column(); | |||
| 140 | } | |||
| 141 | ||||
| 142 | Position denormalize(IO &) { | |||
| 143 | Position Pos; | |||
| 144 | Pos.setLine(P.Line); | |||
| ||||
| 145 | Pos.setColumn(P.Column); | |||
| 146 | return Pos; | |||
| 147 | } | |||
| 148 | YPosition P; | |||
| 149 | }; | |||
| 150 | ||||
| 151 | struct NormalizedFileURI { | |||
| 152 | NormalizedFileURI(IO &) {} | |||
| 153 | NormalizedFileURI(IO &, const char *FileURI) { URI = FileURI; } | |||
| 154 | ||||
| 155 | const char *denormalize(IO &IO) { | |||
| 156 | assert(IO.getContext() &&(static_cast <bool> (IO.getContext() && "Expecting an UniqueStringSaver to allocate data" ) ? void (0) : __assert_fail ("IO.getContext() && \"Expecting an UniqueStringSaver to allocate data\"" , "clang-tools-extra/clangd/index/YAMLSerialization.cpp", 157 , __extension__ __PRETTY_FUNCTION__)) | |||
| 157 | "Expecting an UniqueStringSaver to allocate data")(static_cast <bool> (IO.getContext() && "Expecting an UniqueStringSaver to allocate data" ) ? void (0) : __assert_fail ("IO.getContext() && \"Expecting an UniqueStringSaver to allocate data\"" , "clang-tools-extra/clangd/index/YAMLSerialization.cpp", 157 , __extension__ __PRETTY_FUNCTION__)); | |||
| 158 | return static_cast<llvm::UniqueStringSaver *>(IO.getContext()) | |||
| 159 | ->save(URI) | |||
| 160 | .data(); | |||
| 161 | } | |||
| 162 | ||||
| 163 | std::string URI; | |||
| 164 | }; | |||
| 165 | ||||
| 166 | template <> struct MappingTraits<SymbolLocation> { | |||
| 167 | static void mapping(IO &IO, SymbolLocation &Value) { | |||
| 168 | MappingNormalization<NormalizedFileURI, const char *> NFile(IO, | |||
| 169 | Value.FileURI); | |||
| 170 | IO.mapRequired("FileURI", NFile->URI); | |||
| 171 | MappingNormalization<NormalizedPosition, SymbolLocation::Position> NStart( | |||
| 172 | IO, Value.Start); | |||
| 173 | IO.mapRequired("Start", NStart->P); | |||
| 174 | MappingNormalization<NormalizedPosition, SymbolLocation::Position> NEnd( | |||
| 175 | IO, Value.End); | |||
| 176 | IO.mapRequired("End", NEnd->P); | |||
| 177 | } | |||
| 178 | }; | |||
| 179 | ||||
| 180 | template <> struct MappingTraits<SymbolInfo> { | |||
| 181 | static void mapping(IO &IO, SymbolInfo &SymInfo) { | |||
| 182 | // FIXME: expose other fields? | |||
| 183 | IO.mapRequired("Kind", SymInfo.Kind); | |||
| 184 | IO.mapRequired("Lang", SymInfo.Lang); | |||
| 185 | } | |||
| 186 | }; | |||
| 187 | ||||
| 188 | template <> struct ScalarBitSetTraits<clang::clangd::Symbol::IncludeDirective> { | |||
| 189 | static void bitset(IO &IO, clang::clangd::Symbol::IncludeDirective &Value) { | |||
| 190 | IO.bitSetCase(Value, "Include", clang::clangd::Symbol::Include); | |||
| 191 | IO.bitSetCase(Value, "Import", clang::clangd::Symbol::Import); | |||
| 192 | } | |||
| 193 | }; | |||
| 194 | ||||
| 195 | template <> struct MappingTraits<YIncludeHeaderWithReferences> { | |||
| 196 | static void mapping(IO &IO, YIncludeHeaderWithReferences &Inc) { | |||
| 197 | IO.mapRequired("Header", Inc.IncludeHeader); | |||
| 198 | IO.mapRequired("References", Inc.References); | |||
| 199 | IO.mapOptional("Directives", Inc.SupportedDirectives, | |||
| 200 | clang::clangd::Symbol::Include); | |||
| 201 | } | |||
| 202 | }; | |||
| 203 | ||||
| 204 | struct NormalizedIncludeHeaders { | |||
| 205 | using IncludeHeader = clang::clangd::Symbol::IncludeHeaderWithReferences; | |||
| 206 | NormalizedIncludeHeaders(IO &) {} | |||
| 207 | NormalizedIncludeHeaders( | |||
| 208 | IO &, const llvm::SmallVector<IncludeHeader, 1> &IncludeHeaders) { | |||
| 209 | for (auto &I : IncludeHeaders) { | |||
| 210 | Headers.emplace_back(I.IncludeHeader, I.References, | |||
| 211 | I.supportedDirectives()); | |||
| 212 | } | |||
| 213 | } | |||
| 214 | ||||
| 215 | llvm::SmallVector<IncludeHeader, 1> denormalize(IO &) { | |||
| 216 | llvm::SmallVector<IncludeHeader, 1> Result; | |||
| 217 | for (auto &H : Headers) | |||
| 218 | Result.emplace_back(H.IncludeHeader, H.References, H.SupportedDirectives); | |||
| 219 | return Result; | |||
| 220 | } | |||
| 221 | llvm::SmallVector<YIncludeHeaderWithReferences, 1> Headers; | |||
| 222 | }; | |||
| 223 | ||||
| 224 | template <> struct MappingTraits<Symbol> { | |||
| 225 | static void mapping(IO &IO, Symbol &Sym) { | |||
| 226 | MappingNormalization<NormalizedSymbolID, SymbolID> NSymbolID(IO, Sym.ID); | |||
| 227 | MappingNormalization<NormalizedSymbolFlag, Symbol::SymbolFlag> NSymbolFlag( | |||
| 228 | IO, Sym.Flags); | |||
| 229 | MappingNormalization< | |||
| 230 | NormalizedIncludeHeaders, | |||
| 231 | llvm::SmallVector<Symbol::IncludeHeaderWithReferences, 1>> | |||
| 232 | NIncludeHeaders(IO, Sym.IncludeHeaders); | |||
| 233 | IO.mapRequired("ID", NSymbolID->HexString); | |||
| 234 | IO.mapRequired("Name", Sym.Name); | |||
| 235 | IO.mapRequired("Scope", Sym.Scope); | |||
| 236 | IO.mapRequired("SymInfo", Sym.SymInfo); | |||
| 237 | IO.mapOptional("CanonicalDeclaration", Sym.CanonicalDeclaration, | |||
| 238 | SymbolLocation()); | |||
| 239 | IO.mapOptional("Definition", Sym.Definition, SymbolLocation()); | |||
| 240 | IO.mapOptional("References", Sym.References, 0u); | |||
| 241 | IO.mapOptional("Flags", NSymbolFlag->Flag); | |||
| 242 | IO.mapOptional("Signature", Sym.Signature); | |||
| 243 | IO.mapOptional("TemplateSpecializationArgs", | |||
| 244 | Sym.TemplateSpecializationArgs); | |||
| 245 | IO.mapOptional("CompletionSnippetSuffix", Sym.CompletionSnippetSuffix); | |||
| 246 | IO.mapOptional("Documentation", Sym.Documentation); | |||
| 247 | IO.mapOptional("ReturnType", Sym.ReturnType); | |||
| 248 | IO.mapOptional("Type", Sym.Type); | |||
| 249 | IO.mapOptional("IncludeHeaders", NIncludeHeaders->Headers); | |||
| 250 | } | |||
| 251 | }; | |||
| 252 | ||||
| 253 | template <> struct ScalarEnumerationTraits<SymbolLanguage> { | |||
| 254 | static void enumeration(IO &IO, SymbolLanguage &Value) { | |||
| 255 | IO.enumCase(Value, "C", SymbolLanguage::C); | |||
| 256 | IO.enumCase(Value, "Cpp", SymbolLanguage::CXX); | |||
| 257 | IO.enumCase(Value, "ObjC", SymbolLanguage::ObjC); | |||
| 258 | IO.enumCase(Value, "Swift", SymbolLanguage::Swift); | |||
| 259 | } | |||
| 260 | }; | |||
| 261 | ||||
| 262 | template <> struct ScalarEnumerationTraits<SymbolKind> { | |||
| 263 | static void enumeration(IO &IO, SymbolKind &Value) { | |||
| 264 | #define DEFINE_ENUM(name) IO.enumCase(Value, #name, SymbolKind::name) | |||
| 265 | ||||
| 266 | DEFINE_ENUM(Unknown); | |||
| 267 | DEFINE_ENUM(Function); | |||
| 268 | DEFINE_ENUM(Module); | |||
| 269 | DEFINE_ENUM(Namespace); | |||
| 270 | DEFINE_ENUM(NamespaceAlias); | |||
| 271 | DEFINE_ENUM(Macro); | |||
| 272 | DEFINE_ENUM(Enum); | |||
| 273 | DEFINE_ENUM(Struct); | |||
| 274 | DEFINE_ENUM(Class); | |||
| 275 | DEFINE_ENUM(Protocol); | |||
| 276 | DEFINE_ENUM(Extension); | |||
| 277 | DEFINE_ENUM(Union); | |||
| 278 | DEFINE_ENUM(TypeAlias); | |||
| 279 | DEFINE_ENUM(Function); | |||
| 280 | DEFINE_ENUM(Variable); | |||
| 281 | DEFINE_ENUM(Field); | |||
| 282 | DEFINE_ENUM(EnumConstant); | |||
| 283 | DEFINE_ENUM(InstanceMethod); | |||
| 284 | DEFINE_ENUM(ClassMethod); | |||
| 285 | DEFINE_ENUM(StaticMethod); | |||
| 286 | DEFINE_ENUM(InstanceProperty); | |||
| 287 | DEFINE_ENUM(ClassProperty); | |||
| 288 | DEFINE_ENUM(StaticProperty); | |||
| 289 | DEFINE_ENUM(Constructor); | |||
| 290 | DEFINE_ENUM(Destructor); | |||
| 291 | DEFINE_ENUM(ConversionFunction); | |||
| 292 | DEFINE_ENUM(Parameter); | |||
| 293 | DEFINE_ENUM(Using); | |||
| 294 | ||||
| 295 | #undef DEFINE_ENUM | |||
| 296 | } | |||
| 297 | }; | |||
| 298 | ||||
| 299 | template <> struct MappingTraits<RefBundle> { | |||
| 300 | static void mapping(IO &IO, RefBundle &Refs) { | |||
| 301 | MappingNormalization<NormalizedSymbolID, SymbolID> NSymbolID(IO, | |||
| 302 | Refs.first); | |||
| 303 | IO.mapRequired("ID", NSymbolID->HexString); | |||
| 304 | IO.mapRequired("References", Refs.second); | |||
| 305 | } | |||
| 306 | }; | |||
| 307 | ||||
| 308 | struct NormalizedRefKind { | |||
| 309 | NormalizedRefKind(IO &) {} | |||
| 310 | NormalizedRefKind(IO &, RefKind O) { Kind = static_cast<uint8_t>(O); } | |||
| 311 | ||||
| 312 | RefKind denormalize(IO &) { return static_cast<RefKind>(Kind); } | |||
| 313 | ||||
| 314 | uint8_t Kind = 0; | |||
| 315 | }; | |||
| 316 | ||||
| 317 | template <> struct MappingTraits<Ref> { | |||
| 318 | static void mapping(IO &IO, Ref &R) { | |||
| 319 | MappingNormalization<NormalizedRefKind, RefKind> NKind(IO, R.Kind); | |||
| 320 | IO.mapRequired("Kind", NKind->Kind); | |||
| 321 | IO.mapRequired("Location", R.Location); | |||
| 322 | } | |||
| 323 | }; | |||
| 324 | ||||
| 325 | struct NormalizedSymbolRole { | |||
| 326 | NormalizedSymbolRole(IO &) {} | |||
| 327 | NormalizedSymbolRole(IO &IO, RelationKind R) { | |||
| 328 | Kind = static_cast<uint8_t>(R); | |||
| 329 | } | |||
| 330 | ||||
| 331 | RelationKind denormalize(IO &IO) { return static_cast<RelationKind>(Kind); } | |||
| 332 | ||||
| 333 | uint8_t Kind = 0; | |||
| 334 | }; | |||
| 335 | ||||
| 336 | template <> struct MappingTraits<SymbolID> { | |||
| 337 | static void mapping(IO &IO, SymbolID &ID) { | |||
| 338 | MappingNormalization<NormalizedSymbolID, SymbolID> NSymbolID(IO, ID); | |||
| 339 | IO.mapRequired("ID", NSymbolID->HexString); | |||
| 340 | } | |||
| 341 | }; | |||
| 342 | ||||
| 343 | template <> struct MappingTraits<Relation> { | |||
| 344 | static void mapping(IO &IO, Relation &Relation) { | |||
| 345 | MappingNormalization<NormalizedSymbolRole, RelationKind> NRole( | |||
| 346 | IO, Relation.Predicate); | |||
| 347 | IO.mapRequired("Subject", Relation.Subject); | |||
| 348 | IO.mapRequired("Predicate", NRole->Kind); | |||
| 349 | IO.mapRequired("Object", Relation.Object); | |||
| 350 | } | |||
| 351 | }; | |||
| 352 | ||||
| 353 | struct NormalizedSourceFlag { | |||
| 354 | NormalizedSourceFlag(IO &) {} | |||
| 355 | NormalizedSourceFlag(IO &, IncludeGraphNode::SourceFlag O) { | |||
| 356 | Flag = static_cast<uint8_t>(O); | |||
| 357 | } | |||
| 358 | ||||
| 359 | IncludeGraphNode::SourceFlag denormalize(IO &) { | |||
| 360 | return static_cast<IncludeGraphNode::SourceFlag>(Flag); | |||
| 361 | } | |||
| 362 | ||||
| 363 | uint8_t Flag = 0; | |||
| 364 | }; | |||
| 365 | ||||
| 366 | struct NormalizedFileDigest { | |||
| 367 | NormalizedFileDigest(IO &) {} | |||
| 368 | NormalizedFileDigest(IO &, const FileDigest &Digest) { | |||
| 369 | HexString = llvm::toHex(Digest); | |||
| 370 | } | |||
| 371 | ||||
| 372 | FileDigest denormalize(IO &I) { | |||
| 373 | FileDigest Digest; | |||
| 374 | if (HexString.size() == Digest.size() * 2 && | |||
| 375 | llvm::all_of(HexString, llvm::isHexDigit)) { | |||
| 376 | memcpy(Digest.data(), llvm::fromHex(HexString).data(), Digest.size()); | |||
| 377 | } else { | |||
| 378 | I.setError(std::string("Bad hex file digest: ") + HexString); | |||
| 379 | } | |||
| 380 | return Digest; | |||
| 381 | } | |||
| 382 | ||||
| 383 | std::string HexString; | |||
| 384 | }; | |||
| 385 | ||||
| 386 | template <> struct MappingTraits<IncludeGraphNode> { | |||
| 387 | static void mapping(IO &IO, IncludeGraphNode &Node) { | |||
| 388 | IO.mapRequired("URI", Node.URI); | |||
| 389 | MappingNormalization<NormalizedSourceFlag, IncludeGraphNode::SourceFlag> | |||
| 390 | NSourceFlag(IO, Node.Flags); | |||
| 391 | IO.mapRequired("Flags", NSourceFlag->Flag); | |||
| 392 | MappingNormalization<NormalizedFileDigest, FileDigest> NDigest(IO, | |||
| 393 | Node.Digest); | |||
| 394 | IO.mapRequired("Digest", NDigest->HexString); | |||
| 395 | IO.mapRequired("DirectIncludes", Node.DirectIncludes); | |||
| 396 | } | |||
| 397 | }; | |||
| 398 | ||||
| 399 | template <> struct MappingTraits<CompileCommandYAML> { | |||
| 400 | static void mapping(IO &IO, CompileCommandYAML &Cmd) { | |||
| 401 | IO.mapRequired("Directory", Cmd.Directory); | |||
| 402 | IO.mapRequired("CommandLine", Cmd.CommandLine); | |||
| 403 | } | |||
| 404 | }; | |||
| 405 | ||||
| 406 | template <> struct MappingTraits<VariantEntry> { | |||
| 407 | static void mapping(IO &IO, VariantEntry &Variant) { | |||
| 408 | if (IO.mapTag("!Symbol", Variant.Symbol.has_value())) { | |||
| 409 | if (!IO.outputting()) | |||
| 410 | Variant.Symbol.emplace(); | |||
| 411 | MappingTraits<Symbol>::mapping(IO, *Variant.Symbol); | |||
| 412 | } else if (IO.mapTag("!Refs", Variant.Refs.has_value())) { | |||
| 413 | if (!IO.outputting()) | |||
| 414 | Variant.Refs.emplace(); | |||
| 415 | MappingTraits<RefBundle>::mapping(IO, *Variant.Refs); | |||
| 416 | } else if (IO.mapTag("!Relations", Variant.Relation.has_value())) { | |||
| 417 | if (!IO.outputting()) | |||
| 418 | Variant.Relation.emplace(); | |||
| 419 | MappingTraits<Relation>::mapping(IO, *Variant.Relation); | |||
| 420 | } else if (IO.mapTag("!Source", Variant.Source.has_value())) { | |||
| 421 | if (!IO.outputting()) | |||
| 422 | Variant.Source.emplace(); | |||
| 423 | MappingTraits<IncludeGraphNode>::mapping(IO, *Variant.Source); | |||
| 424 | } else if (IO.mapTag("!Cmd", Variant.Cmd.has_value())) { | |||
| 425 | if (!IO.outputting()) | |||
| 426 | Variant.Cmd.emplace(); | |||
| 427 | MappingTraits<CompileCommandYAML>::mapping( | |||
| 428 | IO, static_cast<CompileCommandYAML &>(*Variant.Cmd)); | |||
| 429 | } | |||
| 430 | } | |||
| 431 | }; | |||
| 432 | ||||
| 433 | } // namespace yaml | |||
| 434 | } // namespace llvm | |||
| 435 | ||||
| 436 | namespace clang { | |||
| 437 | namespace clangd { | |||
| 438 | ||||
| 439 | void writeYAML(const IndexFileOut &O, llvm::raw_ostream &OS) { | |||
| 440 | llvm::yaml::Output Yout(OS); | |||
| 441 | for (const auto &Sym : *O.Symbols) { | |||
| 442 | VariantEntry Entry; | |||
| 443 | Entry.Symbol = Sym; | |||
| 444 | Yout << Entry; | |||
| 445 | } | |||
| 446 | if (O.Refs) | |||
| 447 | for (auto &Sym : *O.Refs) { | |||
| 448 | VariantEntry Entry; | |||
| 449 | Entry.Refs = Sym; | |||
| 450 | Yout << Entry; | |||
| 451 | } | |||
| 452 | if (O.Relations) | |||
| 453 | for (auto &R : *O.Relations) { | |||
| 454 | VariantEntry Entry; | |||
| 455 | Entry.Relation = R; | |||
| 456 | Yout << Entry; | |||
| 457 | } | |||
| 458 | if (O.Sources) { | |||
| 459 | for (const auto &Source : *O.Sources) { | |||
| 460 | VariantEntry Entry; | |||
| 461 | Entry.Source = Source.getValue(); | |||
| 462 | Yout << Entry; | |||
| 463 | } | |||
| 464 | } | |||
| 465 | if (O.Cmd) { | |||
| 466 | VariantEntry Entry; | |||
| 467 | Entry.Cmd = *O.Cmd; | |||
| 468 | Yout << Entry; | |||
| 469 | } | |||
| 470 | } | |||
| 471 | ||||
| 472 | llvm::Expected<IndexFileIn> readYAML(llvm::StringRef Data, | |||
| 473 | SymbolOrigin Origin) { | |||
| 474 | SymbolSlab::Builder Symbols; | |||
| 475 | RefSlab::Builder Refs; | |||
| 476 | RelationSlab::Builder Relations; | |||
| 477 | llvm::BumpPtrAllocator | |||
| 478 | Arena; // store the underlying data of Position::FileURI. | |||
| 479 | llvm::UniqueStringSaver Strings(Arena); | |||
| 480 | llvm::yaml::Input Yin(Data, &Strings); | |||
| 481 | IncludeGraph Sources; | |||
| 482 | std::optional<tooling::CompileCommand> Cmd; | |||
| 483 | while (Yin.setCurrentDocument()) { | |||
| 484 | llvm::yaml::EmptyContext Ctx; | |||
| 485 | VariantEntry Variant; | |||
| 486 | yamlize(Yin, Variant, true, Ctx); | |||
| 487 | if (Yin.error()) | |||
| 488 | return llvm::errorCodeToError(Yin.error()); | |||
| 489 | ||||
| 490 | if (Variant.Symbol) { | |||
| 491 | Variant.Symbol->Origin = Origin; | |||
| 492 | Symbols.insert(*Variant.Symbol); | |||
| 493 | } | |||
| 494 | if (Variant.Refs) | |||
| 495 | for (const auto &Ref : Variant.Refs->second) | |||
| 496 | Refs.insert(Variant.Refs->first, Ref); | |||
| 497 | if (Variant.Relation) | |||
| 498 | Relations.insert(*Variant.Relation); | |||
| 499 | if (Variant.Source) { | |||
| 500 | auto &IGN = *Variant.Source; | |||
| 501 | auto Entry = Sources.try_emplace(IGN.URI).first; | |||
| 502 | Entry->getValue() = std::move(IGN); | |||
| 503 | // Fixup refs to refer to map keys which will live on | |||
| 504 | Entry->getValue().URI = Entry->getKey(); | |||
| 505 | for (auto &Include : Entry->getValue().DirectIncludes) | |||
| 506 | Include = Sources.try_emplace(Include).first->getKey(); | |||
| 507 | } | |||
| 508 | if (Variant.Cmd) | |||
| 509 | Cmd = *Variant.Cmd; | |||
| 510 | Yin.nextDocument(); | |||
| 511 | } | |||
| 512 | ||||
| 513 | IndexFileIn Result; | |||
| 514 | Result.Symbols.emplace(std::move(Symbols).build()); | |||
| 515 | Result.Refs.emplace(std::move(Refs).build()); | |||
| 516 | Result.Relations.emplace(std::move(Relations).build()); | |||
| 517 | if (Sources.size()) | |||
| 518 | Result.Sources = std::move(Sources); | |||
| 519 | Result.Cmd = std::move(Cmd); | |||
| 520 | return std::move(Result); | |||
| 521 | } | |||
| 522 | ||||
| 523 | std::string toYAML(const Symbol &S) { | |||
| 524 | std::string Buf; | |||
| 525 | { | |||
| 526 | llvm::raw_string_ostream OS(Buf); | |||
| 527 | llvm::yaml::Output Yout(OS); | |||
| 528 | Symbol Sym = S; // copy: Yout<< requires mutability. | |||
| 529 | Yout << Sym; | |||
| 530 | } | |||
| 531 | return Buf; | |||
| 532 | } | |||
| 533 | ||||
| 534 | std::string toYAML(const std::pair<SymbolID, llvm::ArrayRef<Ref>> &Data) { | |||
| 535 | RefBundle Refs = {Data.first, Data.second}; | |||
| 536 | std::string Buf; | |||
| 537 | { | |||
| 538 | llvm::raw_string_ostream OS(Buf); | |||
| 539 | llvm::yaml::Output Yout(OS); | |||
| 540 | Yout << Refs; | |||
| 541 | } | |||
| 542 | return Buf; | |||
| 543 | } | |||
| 544 | ||||
| 545 | std::string toYAML(const Relation &R) { | |||
| 546 | std::string Buf; | |||
| 547 | { | |||
| 548 | llvm::raw_string_ostream OS(Buf); | |||
| 549 | llvm::yaml::Output Yout(OS); | |||
| 550 | Relation Rel = R; // copy: Yout<< requires mutability. | |||
| 551 | Yout << Rel; | |||
| 552 | } | |||
| 553 | return Buf; | |||
| 554 | } | |||
| 555 | ||||
| 556 | std::string toYAML(const Ref &R) { | |||
| 557 | std::string Buf; | |||
| 558 | { | |||
| 559 | llvm::raw_string_ostream OS(Buf); | |||
| 560 | llvm::yaml::Output Yout(OS); | |||
| 561 | Ref Reference = R; // copy: Yout<< requires mutability. | |||
| 562 | Yout << Reference; | |||
| ||||
| 563 | } | |||
| 564 | return Buf; | |||
| 565 | } | |||
| 566 | ||||
| 567 | } // namespace clangd | |||
| 568 | } // namespace clang |
| 1 | //===- llvm/Support/YAMLTraits.h --------------------------------*- C++ -*-===// | ||||||||||||
| 2 | // | ||||||||||||
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||||
| 4 | // See https://llvm.org/LICENSE.txt for license information. | ||||||||||||
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||||
| 6 | // | ||||||||||||
| 7 | //===----------------------------------------------------------------------===// | ||||||||||||
| 8 | |||||||||||||
| 9 | #ifndef LLVM_SUPPORT_YAMLTRAITS_H | ||||||||||||
| 10 | #define LLVM_SUPPORT_YAMLTRAITS_H | ||||||||||||
| 11 | |||||||||||||
| 12 | #include "llvm/ADT/ArrayRef.h" | ||||||||||||
| 13 | #include "llvm/ADT/BitVector.h" | ||||||||||||
| 14 | #include "llvm/ADT/SmallVector.h" | ||||||||||||
| 15 | #include "llvm/ADT/StringExtras.h" | ||||||||||||
| 16 | #include "llvm/ADT/StringMap.h" | ||||||||||||
| 17 | #include "llvm/ADT/StringRef.h" | ||||||||||||
| 18 | #include "llvm/ADT/Twine.h" | ||||||||||||
| 19 | #include "llvm/Support/AlignOf.h" | ||||||||||||
| 20 | #include "llvm/Support/Allocator.h" | ||||||||||||
| 21 | #include "llvm/Support/Endian.h" | ||||||||||||
| 22 | #include "llvm/Support/SMLoc.h" | ||||||||||||
| 23 | #include "llvm/Support/SourceMgr.h" | ||||||||||||
| 24 | #include "llvm/Support/YAMLParser.h" | ||||||||||||
| 25 | #include "llvm/Support/raw_ostream.h" | ||||||||||||
| 26 | #include <cassert> | ||||||||||||
| 27 | #include <map> | ||||||||||||
| 28 | #include <memory> | ||||||||||||
| 29 | #include <new> | ||||||||||||
| 30 | #include <optional> | ||||||||||||
| 31 | #include <string> | ||||||||||||
| 32 | #include <system_error> | ||||||||||||
| 33 | #include <type_traits> | ||||||||||||
| 34 | #include <vector> | ||||||||||||
| 35 | |||||||||||||
| 36 | namespace llvm { | ||||||||||||
| 37 | |||||||||||||
| 38 | class VersionTuple; | ||||||||||||
| 39 | |||||||||||||
| 40 | namespace yaml { | ||||||||||||
| 41 | |||||||||||||
| 42 | enum class NodeKind : uint8_t { | ||||||||||||
| 43 | Scalar, | ||||||||||||
| 44 | Map, | ||||||||||||
| 45 | Sequence, | ||||||||||||
| 46 | }; | ||||||||||||
| 47 | |||||||||||||
| 48 | struct EmptyContext {}; | ||||||||||||
| 49 | |||||||||||||
| 50 | /// This class should be specialized by any type that needs to be converted | ||||||||||||
| 51 | /// to/from a YAML mapping. For example: | ||||||||||||
| 52 | /// | ||||||||||||
| 53 | /// struct MappingTraits<MyStruct> { | ||||||||||||
| 54 | /// static void mapping(IO &io, MyStruct &s) { | ||||||||||||
| 55 | /// io.mapRequired("name", s.name); | ||||||||||||
| 56 | /// io.mapRequired("size", s.size); | ||||||||||||
| 57 | /// io.mapOptional("age", s.age); | ||||||||||||
| 58 | /// } | ||||||||||||
| 59 | /// }; | ||||||||||||
| 60 | template<class T> | ||||||||||||
| 61 | struct MappingTraits { | ||||||||||||
| 62 | // Must provide: | ||||||||||||
| 63 | // static void mapping(IO &io, T &fields); | ||||||||||||
| 64 | // Optionally may provide: | ||||||||||||
| 65 | // static std::string validate(IO &io, T &fields); | ||||||||||||
| 66 | // static void enumInput(IO &io, T &value); | ||||||||||||
| 67 | // | ||||||||||||
| 68 | // The optional flow flag will cause generated YAML to use a flow mapping | ||||||||||||
| 69 | // (e.g. { a: 0, b: 1 }): | ||||||||||||
| 70 | // static const bool flow = true; | ||||||||||||
| 71 | }; | ||||||||||||
| 72 | |||||||||||||
| 73 | /// This class is similar to MappingTraits<T> but allows you to pass in | ||||||||||||
| 74 | /// additional context for each map operation. For example: | ||||||||||||
| 75 | /// | ||||||||||||
| 76 | /// struct MappingContextTraits<MyStruct, MyContext> { | ||||||||||||
| 77 | /// static void mapping(IO &io, MyStruct &s, MyContext &c) { | ||||||||||||
| 78 | /// io.mapRequired("name", s.name); | ||||||||||||
| 79 | /// io.mapRequired("size", s.size); | ||||||||||||
| 80 | /// io.mapOptional("age", s.age); | ||||||||||||
| 81 | /// ++c.TimesMapped; | ||||||||||||
| 82 | /// } | ||||||||||||
| 83 | /// }; | ||||||||||||
| 84 | template <class T, class Context> struct MappingContextTraits { | ||||||||||||
| 85 | // Must provide: | ||||||||||||
| 86 | // static void mapping(IO &io, T &fields, Context &Ctx); | ||||||||||||
| 87 | // Optionally may provide: | ||||||||||||
| 88 | // static std::string validate(IO &io, T &fields, Context &Ctx); | ||||||||||||
| 89 | // | ||||||||||||
| 90 | // The optional flow flag will cause generated YAML to use a flow mapping | ||||||||||||
| 91 | // (e.g. { a: 0, b: 1 }): | ||||||||||||
| 92 | // static const bool flow = true; | ||||||||||||
| 93 | }; | ||||||||||||
| 94 | |||||||||||||
| 95 | /// This class should be specialized by any integral type that converts | ||||||||||||
| 96 | /// to/from a YAML scalar where there is a one-to-one mapping between | ||||||||||||
| 97 | /// in-memory values and a string in YAML. For example: | ||||||||||||
| 98 | /// | ||||||||||||
| 99 | /// struct ScalarEnumerationTraits<Colors> { | ||||||||||||
| 100 | /// static void enumeration(IO &io, Colors &value) { | ||||||||||||
| 101 | /// io.enumCase(value, "red", cRed); | ||||||||||||
| 102 | /// io.enumCase(value, "blue", cBlue); | ||||||||||||
| 103 | /// io.enumCase(value, "green", cGreen); | ||||||||||||
| 104 | /// } | ||||||||||||
| 105 | /// }; | ||||||||||||
| 106 | template <typename T, typename Enable = void> struct ScalarEnumerationTraits { | ||||||||||||
| 107 | // Must provide: | ||||||||||||
| 108 | // static void enumeration(IO &io, T &value); | ||||||||||||
| 109 | }; | ||||||||||||
| 110 | |||||||||||||
| 111 | /// This class should be specialized by any integer type that is a union | ||||||||||||
| 112 | /// of bit values and the YAML representation is a flow sequence of | ||||||||||||
| 113 | /// strings. For example: | ||||||||||||
| 114 | /// | ||||||||||||
| 115 | /// struct ScalarBitSetTraits<MyFlags> { | ||||||||||||
| 116 | /// static void bitset(IO &io, MyFlags &value) { | ||||||||||||
| 117 | /// io.bitSetCase(value, "big", flagBig); | ||||||||||||
| 118 | /// io.bitSetCase(value, "flat", flagFlat); | ||||||||||||
| 119 | /// io.bitSetCase(value, "round", flagRound); | ||||||||||||
| 120 | /// } | ||||||||||||
| 121 | /// }; | ||||||||||||
| 122 | template <typename T, typename Enable = void> struct ScalarBitSetTraits { | ||||||||||||
| 123 | // Must provide: | ||||||||||||
| 124 | // static void bitset(IO &io, T &value); | ||||||||||||
| 125 | }; | ||||||||||||
| 126 | |||||||||||||
| 127 | /// Describe which type of quotes should be used when quoting is necessary. | ||||||||||||
| 128 | /// Some non-printable characters need to be double-quoted, while some others | ||||||||||||
| 129 | /// are fine with simple-quoting, and some don't need any quoting. | ||||||||||||
| 130 | enum class QuotingType { None, Single, Double }; | ||||||||||||
| 131 | |||||||||||||
| 132 | /// This class should be specialized by type that requires custom conversion | ||||||||||||
| 133 | /// to/from a yaml scalar. For example: | ||||||||||||
| 134 | /// | ||||||||||||
| 135 | /// template<> | ||||||||||||
| 136 | /// struct ScalarTraits<MyType> { | ||||||||||||
| 137 | /// static void output(const MyType &val, void*, llvm::raw_ostream &out) { | ||||||||||||
| 138 | /// // stream out custom formatting | ||||||||||||
| 139 | /// out << llvm::format("%x", val); | ||||||||||||
| 140 | /// } | ||||||||||||
| 141 | /// static StringRef input(StringRef scalar, void*, MyType &value) { | ||||||||||||
| 142 | /// // parse scalar and set `value` | ||||||||||||
| 143 | /// // return empty string on success, or error string | ||||||||||||
| 144 | /// return StringRef(); | ||||||||||||
| 145 | /// } | ||||||||||||
| 146 | /// static QuotingType mustQuote(StringRef) { return QuotingType::Single; } | ||||||||||||
| 147 | /// }; | ||||||||||||
| 148 | template <typename T, typename Enable = void> struct ScalarTraits { | ||||||||||||
| 149 | // Must provide: | ||||||||||||
| 150 | // | ||||||||||||
| 151 | // Function to write the value as a string: | ||||||||||||
| 152 | // static void output(const T &value, void *ctxt, llvm::raw_ostream &out); | ||||||||||||
| 153 | // | ||||||||||||
| 154 | // Function to convert a string to a value. Returns the empty | ||||||||||||
| 155 | // StringRef on success or an error string if string is malformed: | ||||||||||||
| 156 | // static StringRef input(StringRef scalar, void *ctxt, T &value); | ||||||||||||
| 157 | // | ||||||||||||
| 158 | // Function to determine if the value should be quoted. | ||||||||||||
| 159 | // static QuotingType mustQuote(StringRef); | ||||||||||||
| 160 | }; | ||||||||||||
| 161 | |||||||||||||
| 162 | /// This class should be specialized by type that requires custom conversion | ||||||||||||
| 163 | /// to/from a YAML literal block scalar. For example: | ||||||||||||
| 164 | /// | ||||||||||||
| 165 | /// template <> | ||||||||||||
| 166 | /// struct BlockScalarTraits<MyType> { | ||||||||||||
| 167 | /// static void output(const MyType &Value, void*, llvm::raw_ostream &Out) | ||||||||||||
| 168 | /// { | ||||||||||||
| 169 | /// // stream out custom formatting | ||||||||||||
| 170 | /// Out << Value; | ||||||||||||
| 171 | /// } | ||||||||||||
| 172 | /// static StringRef input(StringRef Scalar, void*, MyType &Value) { | ||||||||||||
| 173 | /// // parse scalar and set `value` | ||||||||||||
| 174 | /// // return empty string on success, or error string | ||||||||||||
| 175 | /// return StringRef(); | ||||||||||||
| 176 | /// } | ||||||||||||
| 177 | /// }; | ||||||||||||
| 178 | template <typename T> | ||||||||||||
| 179 | struct BlockScalarTraits { | ||||||||||||
| 180 | // Must provide: | ||||||||||||
| 181 | // | ||||||||||||
| 182 | // Function to write the value as a string: | ||||||||||||
| 183 | // static void output(const T &Value, void *ctx, llvm::raw_ostream &Out); | ||||||||||||
| 184 | // | ||||||||||||
| 185 | // Function to convert a string to a value. Returns the empty | ||||||||||||
| 186 | // StringRef on success or an error string if string is malformed: | ||||||||||||
| 187 | // static StringRef input(StringRef Scalar, void *ctxt, T &Value); | ||||||||||||
| 188 | // | ||||||||||||
| 189 | // Optional: | ||||||||||||
| 190 | // static StringRef inputTag(T &Val, std::string Tag) | ||||||||||||
| 191 | // static void outputTag(const T &Val, raw_ostream &Out) | ||||||||||||
| 192 | }; | ||||||||||||
| 193 | |||||||||||||
| 194 | /// This class should be specialized by type that requires custom conversion | ||||||||||||
| 195 | /// to/from a YAML scalar with optional tags. For example: | ||||||||||||
| 196 | /// | ||||||||||||
| 197 | /// template <> | ||||||||||||
| 198 | /// struct TaggedScalarTraits<MyType> { | ||||||||||||
| 199 | /// static void output(const MyType &Value, void*, llvm::raw_ostream | ||||||||||||
| 200 | /// &ScalarOut, llvm::raw_ostream &TagOut) | ||||||||||||
| 201 | /// { | ||||||||||||
| 202 | /// // stream out custom formatting including optional Tag | ||||||||||||
| 203 | /// Out << Value; | ||||||||||||
| 204 | /// } | ||||||||||||
| 205 | /// static StringRef input(StringRef Scalar, StringRef Tag, void*, MyType | ||||||||||||
| 206 | /// &Value) { | ||||||||||||
| 207 | /// // parse scalar and set `value` | ||||||||||||
| 208 | /// // return empty string on success, or error string | ||||||||||||
| 209 | /// return StringRef(); | ||||||||||||
| 210 | /// } | ||||||||||||
| 211 | /// static QuotingType mustQuote(const MyType &Value, StringRef) { | ||||||||||||
| 212 | /// return QuotingType::Single; | ||||||||||||
| 213 | /// } | ||||||||||||
| 214 | /// }; | ||||||||||||
| 215 | template <typename T> struct TaggedScalarTraits { | ||||||||||||
| 216 | // Must provide: | ||||||||||||
| 217 | // | ||||||||||||
| 218 | // Function to write the value and tag as strings: | ||||||||||||
| 219 | // static void output(const T &Value, void *ctx, llvm::raw_ostream &ScalarOut, | ||||||||||||
| 220 | // llvm::raw_ostream &TagOut); | ||||||||||||
| 221 | // | ||||||||||||
| 222 | // Function to convert a string to a value. Returns the empty | ||||||||||||
| 223 | // StringRef on success or an error string if string is malformed: | ||||||||||||
| 224 | // static StringRef input(StringRef Scalar, StringRef Tag, void *ctxt, T | ||||||||||||
| 225 | // &Value); | ||||||||||||
| 226 | // | ||||||||||||
| 227 | // Function to determine if the value should be quoted. | ||||||||||||
| 228 | // static QuotingType mustQuote(const T &Value, StringRef Scalar); | ||||||||||||
| 229 | }; | ||||||||||||
| 230 | |||||||||||||
| 231 | /// This class should be specialized by any type that needs to be converted | ||||||||||||
| 232 | /// to/from a YAML sequence. For example: | ||||||||||||
| 233 | /// | ||||||||||||
| 234 | /// template<> | ||||||||||||
| 235 | /// struct SequenceTraits<MyContainer> { | ||||||||||||
| 236 | /// static size_t size(IO &io, MyContainer &seq) { | ||||||||||||
| 237 | /// return seq.size(); | ||||||||||||
| 238 | /// } | ||||||||||||
| 239 | /// static MyType& element(IO &, MyContainer &seq, size_t index) { | ||||||||||||
| 240 | /// if ( index >= seq.size() ) | ||||||||||||
| 241 | /// seq.resize(index+1); | ||||||||||||
| 242 | /// return seq[index]; | ||||||||||||
| 243 | /// } | ||||||||||||
| 244 | /// }; | ||||||||||||
| 245 | template<typename T, typename EnableIf = void> | ||||||||||||
| 246 | struct SequenceTraits { | ||||||||||||
| 247 | // Must provide: | ||||||||||||
| 248 | // static size_t size(IO &io, T &seq); | ||||||||||||
| 249 | // static T::value_type& element(IO &io, T &seq, size_t index); | ||||||||||||
| 250 | // | ||||||||||||
| 251 | // The following is option and will cause generated YAML to use | ||||||||||||
| 252 | // a flow sequence (e.g. [a,b,c]). | ||||||||||||
| 253 | // static const bool flow = true; | ||||||||||||
| 254 | }; | ||||||||||||
| 255 | |||||||||||||
| 256 | /// This class should be specialized by any type for which vectors of that | ||||||||||||
| 257 | /// type need to be converted to/from a YAML sequence. | ||||||||||||
| 258 | template<typename T, typename EnableIf = void> | ||||||||||||
| 259 | struct SequenceElementTraits { | ||||||||||||
| 260 | // Must provide: | ||||||||||||
| 261 | // static const bool flow; | ||||||||||||
| 262 | }; | ||||||||||||
| 263 | |||||||||||||
| 264 | /// This class should be specialized by any type that needs to be converted | ||||||||||||
| 265 | /// to/from a list of YAML documents. | ||||||||||||
| 266 | template<typename T> | ||||||||||||
| 267 | struct DocumentListTraits { | ||||||||||||
| 268 | // Must provide: | ||||||||||||
| 269 | // static size_t size(IO &io, T &seq); | ||||||||||||
| 270 | // static T::value_type& element(IO &io, T &seq, size_t index); | ||||||||||||
| 271 | }; | ||||||||||||
| 272 | |||||||||||||
| 273 | /// This class should be specialized by any type that needs to be converted | ||||||||||||
| 274 | /// to/from a YAML mapping in the case where the names of the keys are not known | ||||||||||||
| 275 | /// in advance, e.g. a string map. | ||||||||||||
| 276 | template <typename T> | ||||||||||||
| 277 | struct CustomMappingTraits { | ||||||||||||
| 278 | // static void inputOne(IO &io, StringRef key, T &elem); | ||||||||||||
| 279 | // static void output(IO &io, T &elem); | ||||||||||||
| 280 | }; | ||||||||||||
| 281 | |||||||||||||
| 282 | /// This class should be specialized by any type that can be represented as | ||||||||||||
| 283 | /// a scalar, map, or sequence, decided dynamically. For example: | ||||||||||||
| 284 | /// | ||||||||||||
| 285 | /// typedef std::unique_ptr<MyBase> MyPoly; | ||||||||||||
| 286 | /// | ||||||||||||
| 287 | /// template<> | ||||||||||||
| 288 | /// struct PolymorphicTraits<MyPoly> { | ||||||||||||
| 289 | /// static NodeKind getKind(const MyPoly &poly) { | ||||||||||||
| 290 | /// return poly->getKind(); | ||||||||||||
| 291 | /// } | ||||||||||||
| 292 | /// static MyScalar& getAsScalar(MyPoly &poly) { | ||||||||||||
| 293 | /// if (!poly || !isa<MyScalar>(poly)) | ||||||||||||
| 294 | /// poly.reset(new MyScalar()); | ||||||||||||
| 295 | /// return *cast<MyScalar>(poly.get()); | ||||||||||||
| 296 | /// } | ||||||||||||
| 297 | /// // ... | ||||||||||||
| 298 | /// }; | ||||||||||||
| 299 | template <typename T> struct PolymorphicTraits { | ||||||||||||
| 300 | // Must provide: | ||||||||||||
| 301 | // static NodeKind getKind(const T &poly); | ||||||||||||
| 302 | // static scalar_type &getAsScalar(T &poly); | ||||||||||||
| 303 | // static map_type &getAsMap(T &poly); | ||||||||||||
| 304 | // static sequence_type &getAsSequence(T &poly); | ||||||||||||
| 305 | }; | ||||||||||||
| 306 | |||||||||||||
| 307 | // Only used for better diagnostics of missing traits | ||||||||||||
| 308 | template <typename T> | ||||||||||||
| 309 | struct MissingTrait; | ||||||||||||
| 310 | |||||||||||||
| 311 | // Test if ScalarEnumerationTraits<T> is defined on type T. | ||||||||||||
| 312 | template <class T> | ||||||||||||
| 313 | struct has_ScalarEnumerationTraits | ||||||||||||
| 314 | { | ||||||||||||
| 315 | using Signature_enumeration = void (*)(class IO&, T&); | ||||||||||||
| 316 | |||||||||||||
| 317 | template <typename U> | ||||||||||||
| 318 | static char test(SameType<Signature_enumeration, &U::enumeration>*); | ||||||||||||
| 319 | |||||||||||||
| 320 | template <typename U> | ||||||||||||
| 321 | static double test(...); | ||||||||||||
| 322 | |||||||||||||
| 323 | static bool const value = | ||||||||||||
| 324 | (sizeof(test<ScalarEnumerationTraits<T>>(nullptr)) == 1); | ||||||||||||
| 325 | }; | ||||||||||||
| 326 | |||||||||||||
| 327 | // Test if ScalarBitSetTraits<T> is defined on type T. | ||||||||||||
| 328 | template <class T> | ||||||||||||
| 329 | struct has_ScalarBitSetTraits | ||||||||||||
| 330 | { | ||||||||||||
| 331 | using Signature_bitset = void (*)(class IO&, T&); | ||||||||||||
| 332 | |||||||||||||
| 333 | template <typename U> | ||||||||||||
| 334 | static char test(SameType<Signature_bitset, &U::bitset>*); | ||||||||||||
| 335 | |||||||||||||
| 336 | template <typename U> | ||||||||||||
| 337 | static double test(...); | ||||||||||||
| 338 | |||||||||||||
| 339 | static bool const value = (sizeof(test<ScalarBitSetTraits<T>>(nullptr)) == 1); | ||||||||||||
| 340 | }; | ||||||||||||
| 341 | |||||||||||||
| 342 | // Test if ScalarTraits<T> is defined on type T. | ||||||||||||
| 343 | template <class T> | ||||||||||||
| 344 | struct has_ScalarTraits | ||||||||||||
| 345 | { | ||||||||||||
| 346 | using Signature_input = StringRef (*)(StringRef, void*, T&); | ||||||||||||
| 347 | using Signature_output = void (*)(const T&, void*, raw_ostream&); | ||||||||||||
| 348 | using Signature_mustQuote = QuotingType (*)(StringRef); | ||||||||||||
| 349 | |||||||||||||
| 350 | template <typename U> | ||||||||||||
| 351 | static char test(SameType<Signature_input, &U::input> *, | ||||||||||||
| 352 | SameType<Signature_output, &U::output> *, | ||||||||||||
| 353 | SameType<Signature_mustQuote, &U::mustQuote> *); | ||||||||||||
| 354 | |||||||||||||
| 355 | template <typename U> | ||||||||||||
| 356 | static double test(...); | ||||||||||||
| 357 | |||||||||||||
| 358 | static bool const value = | ||||||||||||
| 359 | (sizeof(test<ScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1); | ||||||||||||
| 360 | }; | ||||||||||||
| 361 | |||||||||||||
| 362 | // Test if BlockScalarTraits<T> is defined on type T. | ||||||||||||
| 363 | template <class T> | ||||||||||||
| 364 | struct has_BlockScalarTraits | ||||||||||||
| 365 | { | ||||||||||||
| 366 | using Signature_input = StringRef (*)(StringRef, void *, T &); | ||||||||||||
| 367 | using Signature_output = void (*)(const T &, void *, raw_ostream &); | ||||||||||||
| 368 | |||||||||||||
| 369 | template <typename U> | ||||||||||||
| 370 | static char test(SameType<Signature_input, &U::input> *, | ||||||||||||
| 371 | SameType<Signature_output, &U::output> *); | ||||||||||||
| 372 | |||||||||||||
| 373 | template <typename U> | ||||||||||||
| 374 | static double test(...); | ||||||||||||
| 375 | |||||||||||||
| 376 | static bool const value = | ||||||||||||
| 377 | (sizeof(test<BlockScalarTraits<T>>(nullptr, nullptr)) == 1); | ||||||||||||
| 378 | }; | ||||||||||||
| 379 | |||||||||||||
| 380 | // Test if TaggedScalarTraits<T> is defined on type T. | ||||||||||||
| 381 | template <class T> struct has_TaggedScalarTraits { | ||||||||||||
| 382 | using Signature_input = StringRef (*)(StringRef, StringRef, void *, T &); | ||||||||||||
| 383 | using Signature_output = void (*)(const T &, void *, raw_ostream &, | ||||||||||||
| 384 | raw_ostream &); | ||||||||||||
| 385 | using Signature_mustQuote = QuotingType (*)(const T &, StringRef); | ||||||||||||
| 386 | |||||||||||||
| 387 | template <typename U> | ||||||||||||
| 388 | static char test(SameType<Signature_input, &U::input> *, | ||||||||||||
| 389 | SameType<Signature_output, &U::output> *, | ||||||||||||
| 390 | SameType<Signature_mustQuote, &U::mustQuote> *); | ||||||||||||
| 391 | |||||||||||||
| 392 | template <typename U> static double test(...); | ||||||||||||
| 393 | |||||||||||||
| 394 | static bool const value = | ||||||||||||
| 395 | (sizeof(test<TaggedScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1); | ||||||||||||
| 396 | }; | ||||||||||||
| 397 | |||||||||||||
| 398 | // Test if MappingContextTraits<T> is defined on type T. | ||||||||||||
| 399 | template <class T, class Context> struct has_MappingTraits { | ||||||||||||
| 400 | using Signature_mapping = void (*)(class IO &, T &, Context &); | ||||||||||||
| 401 | |||||||||||||
| 402 | template <typename U> | ||||||||||||
| 403 | static char test(SameType<Signature_mapping, &U::mapping>*); | ||||||||||||
| 404 | |||||||||||||
| 405 | template <typename U> | ||||||||||||
| 406 | static double test(...); | ||||||||||||
| 407 | |||||||||||||
| 408 | static bool const value = | ||||||||||||
| 409 | (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1); | ||||||||||||
| 410 | }; | ||||||||||||
| 411 | |||||||||||||
| 412 | // Test if MappingTraits<T> is defined on type T. | ||||||||||||
| 413 | template <class T> struct has_MappingTraits<T, EmptyContext> { | ||||||||||||
| 414 | using Signature_mapping = void (*)(class IO &, T &); | ||||||||||||
| 415 | |||||||||||||
| 416 | template <typename U> | ||||||||||||
| 417 | static char test(SameType<Signature_mapping, &U::mapping> *); | ||||||||||||
| 418 | |||||||||||||
| 419 | template <typename U> static double test(...); | ||||||||||||
| 420 | |||||||||||||
| 421 | static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1); | ||||||||||||
| 422 | }; | ||||||||||||
| 423 | |||||||||||||
| 424 | // Test if MappingContextTraits<T>::validate() is defined on type T. | ||||||||||||
| 425 | template <class T, class Context> struct has_MappingValidateTraits { | ||||||||||||
| 426 | using Signature_validate = std::string (*)(class IO &, T &, Context &); | ||||||||||||
| 427 | |||||||||||||
| 428 | template <typename U> | ||||||||||||
| 429 | static char test(SameType<Signature_validate, &U::validate>*); | ||||||||||||
| 430 | |||||||||||||
| 431 | template <typename U> | ||||||||||||
| 432 | static double test(...); | ||||||||||||
| 433 | |||||||||||||
| 434 | static bool const value = | ||||||||||||
| 435 | (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1); | ||||||||||||
| 436 | }; | ||||||||||||
| 437 | |||||||||||||
| 438 | // Test if MappingTraits<T>::validate() is defined on type T. | ||||||||||||
| 439 | template <class T> struct has_MappingValidateTraits<T, EmptyContext> { | ||||||||||||
| 440 | using Signature_validate = std::string (*)(class IO &, T &); | ||||||||||||
| 441 | |||||||||||||
| 442 | template <typename U> | ||||||||||||
| 443 | static char test(SameType<Signature_validate, &U::validate> *); | ||||||||||||
| 444 | |||||||||||||
| 445 | template <typename U> static double test(...); | ||||||||||||
| 446 | |||||||||||||
| 447 | static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1); | ||||||||||||
| 448 | }; | ||||||||||||
| 449 | |||||||||||||
| 450 | // Test if MappingContextTraits<T>::enumInput() is defined on type T. | ||||||||||||
| 451 | template <class T, class Context> struct has_MappingEnumInputTraits { | ||||||||||||
| 452 | using Signature_validate = void (*)(class IO &, T &); | ||||||||||||
| 453 | |||||||||||||
| 454 | template <typename U> | ||||||||||||
| 455 | static char test(SameType<Signature_validate, &U::enumInput> *); | ||||||||||||
| 456 | |||||||||||||
| 457 | template <typename U> static double test(...); | ||||||||||||
| 458 | |||||||||||||
| 459 | static bool const value = | ||||||||||||
| 460 | (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1); | ||||||||||||
| 461 | }; | ||||||||||||
| 462 | |||||||||||||
| 463 | // Test if MappingTraits<T>::enumInput() is defined on type T. | ||||||||||||
| 464 | template <class T> struct has_MappingEnumInputTraits<T, EmptyContext> { | ||||||||||||
| 465 | using Signature_validate = void (*)(class IO &, T &); | ||||||||||||
| 466 | |||||||||||||
| 467 | template <typename U> | ||||||||||||
| 468 | static char test(SameType<Signature_validate, &U::enumInput> *); | ||||||||||||
| 469 | |||||||||||||
| 470 | template <typename U> static double test(...); | ||||||||||||
| 471 | |||||||||||||
| 472 | static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1); | ||||||||||||
| 473 | }; | ||||||||||||
| 474 | |||||||||||||
| 475 | // Test if SequenceTraits<T> is defined on type T. | ||||||||||||
| 476 | template <class T> | ||||||||||||
| 477 | struct has_SequenceMethodTraits | ||||||||||||
| 478 | { | ||||||||||||
| 479 | using Signature_size = size_t (*)(class IO&, T&); | ||||||||||||
| 480 | |||||||||||||
| 481 | template <typename U> | ||||||||||||
| 482 | static char test(SameType<Signature_size, &U::size>*); | ||||||||||||
| 483 | |||||||||||||
| 484 | template <typename U> | ||||||||||||
| 485 | static double test(...); | ||||||||||||
| 486 | |||||||||||||
| 487 | static bool const value = (sizeof(test<SequenceTraits<T>>(nullptr)) == 1); | ||||||||||||
| 488 | }; | ||||||||||||
| 489 | |||||||||||||
| 490 | // Test if CustomMappingTraits<T> is defined on type T. | ||||||||||||
| 491 | template <class T> | ||||||||||||
| 492 | struct has_CustomMappingTraits | ||||||||||||
| 493 | { | ||||||||||||
| 494 | using Signature_input = void (*)(IO &io, StringRef key, T &v); | ||||||||||||
| 495 | |||||||||||||
| 496 | template <typename U> | ||||||||||||
| 497 | static char test(SameType<Signature_input, &U::inputOne>*); | ||||||||||||
| 498 | |||||||||||||
| 499 | template <typename U> | ||||||||||||
| 500 | static double test(...); | ||||||||||||
| 501 | |||||||||||||
| 502 | static bool const value = | ||||||||||||
| 503 | (sizeof(test<CustomMappingTraits<T>>(nullptr)) == 1); | ||||||||||||
| 504 | }; | ||||||||||||
| 505 | |||||||||||||
| 506 | // has_FlowTraits<int> will cause an error with some compilers because | ||||||||||||
| 507 | // it subclasses int. Using this wrapper only instantiates the | ||||||||||||
| 508 | // real has_FlowTraits only if the template type is a class. | ||||||||||||
| 509 | template <typename T, bool Enabled = std::is_class_v<T>> class has_FlowTraits { | ||||||||||||
| 510 | public: | ||||||||||||
| 511 | static const bool value = false; | ||||||||||||
| 512 | }; | ||||||||||||
| 513 | |||||||||||||
| 514 | // Some older gcc compilers don't support straight forward tests | ||||||||||||
| 515 | // for members, so test for ambiguity cause by the base and derived | ||||||||||||
| 516 | // classes both defining the member. | ||||||||||||
| 517 | template <class T> | ||||||||||||
| 518 | struct has_FlowTraits<T, true> | ||||||||||||
| 519 | { | ||||||||||||
| 520 | struct Fallback { bool flow; }; | ||||||||||||
| 521 | struct Derived : T, Fallback { }; | ||||||||||||
| 522 | |||||||||||||
| 523 | template<typename C> | ||||||||||||
| 524 | static char (&f(SameType<bool Fallback::*, &C::flow>*))[1]; | ||||||||||||
| 525 | |||||||||||||
| 526 | template<typename C> | ||||||||||||
| 527 | static char (&f(...))[2]; | ||||||||||||
| 528 | |||||||||||||
| 529 | static bool const value = sizeof(f<Derived>(nullptr)) == 2; | ||||||||||||
| 530 | }; | ||||||||||||
| 531 | |||||||||||||
| 532 | // Test if SequenceTraits<T> is defined on type T | ||||||||||||
| 533 | template<typename T> | ||||||||||||
| 534 | struct has_SequenceTraits : public std::integral_constant<bool, | ||||||||||||
| 535 | has_SequenceMethodTraits<T>::value > { }; | ||||||||||||
| 536 | |||||||||||||
| 537 | // Test if DocumentListTraits<T> is defined on type T | ||||||||||||
| 538 | template <class T> | ||||||||||||
| 539 | struct has_DocumentListTraits | ||||||||||||
| 540 | { | ||||||||||||
| 541 | using Signature_size = size_t (*)(class IO &, T &); | ||||||||||||
| 542 | |||||||||||||
| 543 | template <typename U> | ||||||||||||
| 544 | static char test(SameType<Signature_size, &U::size>*); | ||||||||||||
| 545 | |||||||||||||
| 546 | template <typename U> | ||||||||||||
| 547 | static double test(...); | ||||||||||||
| 548 | |||||||||||||
| 549 | static bool const value = (sizeof(test<DocumentListTraits<T>>(nullptr))==1); | ||||||||||||
| 550 | }; | ||||||||||||
| 551 | |||||||||||||
| 552 | template <class T> struct has_PolymorphicTraits { | ||||||||||||
| 553 | using Signature_getKind = NodeKind (*)(const T &); | ||||||||||||
| 554 | |||||||||||||
| 555 | template <typename U> | ||||||||||||
| 556 | static char test(SameType<Signature_getKind, &U::getKind> *); | ||||||||||||
| 557 | |||||||||||||
| 558 | template <typename U> static double test(...); | ||||||||||||
| 559 | |||||||||||||
| 560 | static bool const value = (sizeof(test<PolymorphicTraits<T>>(nullptr)) == 1); | ||||||||||||
| 561 | }; | ||||||||||||
| 562 | |||||||||||||
| 563 | inline bool isNumeric(StringRef S) { | ||||||||||||
| 564 | const auto skipDigits = [](StringRef Input) { | ||||||||||||
| 565 | return Input.ltrim("0123456789"); | ||||||||||||
| 566 | }; | ||||||||||||
| 567 | |||||||||||||
| 568 | // Make S.front() and S.drop_front().front() (if S.front() is [+-]) calls | ||||||||||||
| 569 | // safe. | ||||||||||||
| 570 | if (S.empty() || S.equals("+") || S.equals("-")) | ||||||||||||
| 571 | return false; | ||||||||||||
| 572 | |||||||||||||
| 573 | if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN")) | ||||||||||||
| 574 | return true; | ||||||||||||
| 575 | |||||||||||||
| 576 | // Infinity and decimal numbers can be prefixed with sign. | ||||||||||||
| 577 | StringRef Tail = (S.front() == '-' || S.front() == '+') ? S.drop_front() : S; | ||||||||||||
| 578 | |||||||||||||
| 579 | // Check for infinity first, because checking for hex and oct numbers is more | ||||||||||||
| 580 | // expensive. | ||||||||||||
| 581 | if (Tail.equals(".inf") || Tail.equals(".Inf") || Tail.equals(".INF")) | ||||||||||||
| 582 | return true; | ||||||||||||
| 583 | |||||||||||||
| 584 | // Section 10.3.2 Tag Resolution | ||||||||||||
| 585 | // YAML 1.2 Specification prohibits Base 8 and Base 16 numbers prefixed with | ||||||||||||
| 586 | // [-+], so S should be used instead of Tail. | ||||||||||||
| 587 | if (S.startswith("0o")) | ||||||||||||
| 588 | return S.size() > 2 && | ||||||||||||
| 589 | S.drop_front(2).find_first_not_of("01234567") == StringRef::npos; | ||||||||||||
| 590 | |||||||||||||
| 591 | if (S.startswith("0x")) | ||||||||||||
| 592 | return S.size() > 2 && S.drop_front(2).find_first_not_of( | ||||||||||||
| 593 | "0123456789abcdefABCDEF") == StringRef::npos; | ||||||||||||
| 594 | |||||||||||||
| 595 | // Parse float: [-+]? (\. [0-9]+ | [0-9]+ (\. [0-9]* )?) ([eE] [-+]? [0-9]+)? | ||||||||||||
| 596 | S = Tail; | ||||||||||||
| 597 | |||||||||||||
| 598 | // Handle cases when the number starts with '.' and hence needs at least one | ||||||||||||
| 599 | // digit after dot (as opposed by number which has digits before the dot), but | ||||||||||||
| 600 | // doesn't have one. | ||||||||||||
| 601 | if (S.startswith(".") && | ||||||||||||
| 602 | (S.equals(".") || | ||||||||||||
| 603 | (S.size() > 1 && std::strchr("0123456789", S[1]) == nullptr))) | ||||||||||||
| 604 | return false; | ||||||||||||
| 605 | |||||||||||||
| 606 | if (S.startswith("E") || S.startswith("e")) | ||||||||||||
| 607 | return false; | ||||||||||||
| 608 | |||||||||||||
| 609 | enum ParseState { | ||||||||||||
| 610 | Default, | ||||||||||||
| 611 | FoundDot, | ||||||||||||
| 612 | FoundExponent, | ||||||||||||
| 613 | }; | ||||||||||||
| 614 | ParseState State = Default; | ||||||||||||
| 615 | |||||||||||||
| 616 | S = skipDigits(S); | ||||||||||||
| 617 | |||||||||||||
| 618 | // Accept decimal integer. | ||||||||||||
| 619 | if (S.empty()) | ||||||||||||
| 620 | return true; | ||||||||||||
| 621 | |||||||||||||
| 622 | if (S.front() == '.') { | ||||||||||||
| 623 | State = FoundDot; | ||||||||||||
| 624 | S = S.drop_front(); | ||||||||||||
| 625 | } else if (S.front() == 'e' || S.front() == 'E') { | ||||||||||||
| 626 | State = FoundExponent; | ||||||||||||
| 627 | S = S.drop_front(); | ||||||||||||
| 628 | } else { | ||||||||||||
| 629 | return false; | ||||||||||||
| 630 | } | ||||||||||||
| 631 | |||||||||||||
| 632 | if (State == FoundDot) { | ||||||||||||
| 633 | S = skipDigits(S); | ||||||||||||
| 634 | if (S.empty()) | ||||||||||||
| 635 | return true; | ||||||||||||
| 636 | |||||||||||||
| 637 | if (S.front() == 'e' || S.front() == 'E') { | ||||||||||||
| 638 | State = FoundExponent; | ||||||||||||
| 639 | S = S.drop_front(); | ||||||||||||
| 640 | } else { | ||||||||||||
| 641 | return false; | ||||||||||||
| 642 | } | ||||||||||||
| 643 | } | ||||||||||||
| 644 | |||||||||||||
| 645 | assert(State == FoundExponent && "Should have found exponent at this point.")(static_cast <bool> (State == FoundExponent && "Should have found exponent at this point." ) ? void (0) : __assert_fail ("State == FoundExponent && \"Should have found exponent at this point.\"" , "llvm/include/llvm/Support/YAMLTraits.h", 645, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
| 646 | if (S.empty()) | ||||||||||||
| 647 | return false; | ||||||||||||
| 648 | |||||||||||||
| 649 | if (S.front() == '+' || S.front() == '-') { | ||||||||||||
| 650 | S = S.drop_front(); | ||||||||||||
| 651 | if (S.empty()) | ||||||||||||
| 652 | return false; | ||||||||||||
| 653 | } | ||||||||||||
| 654 | |||||||||||||
| 655 | return skipDigits(S).empty(); | ||||||||||||
| 656 | } | ||||||||||||
| 657 | |||||||||||||
| 658 | inline bool isNull(StringRef S) { | ||||||||||||
| 659 | return S.equals("null") || S.equals("Null") || S.equals("NULL") || | ||||||||||||
| 660 | S.equals("~"); | ||||||||||||
| 661 | } | ||||||||||||
| 662 | |||||||||||||
| 663 | inline bool isBool(StringRef S) { | ||||||||||||
| 664 | // FIXME: using parseBool is causing multiple tests to fail. | ||||||||||||
| 665 | return S.equals("true") || S.equals("True") || S.equals("TRUE") || | ||||||||||||
| 666 | S.equals("false") || S.equals("False") || S.equals("FALSE"); | ||||||||||||
| 667 | } | ||||||||||||
| 668 | |||||||||||||
| 669 | // 5.1. Character Set | ||||||||||||
| 670 | // The allowed character range explicitly excludes the C0 control block #x0-#x1F | ||||||||||||
| 671 | // (except for TAB #x9, LF #xA, and CR #xD which are allowed), DEL #x7F, the C1 | ||||||||||||
| 672 | // control block #x80-#x9F (except for NEL #x85 which is allowed), the surrogate | ||||||||||||
| 673 | // block #xD800-#xDFFF, #xFFFE, and #xFFFF. | ||||||||||||
| 674 | inline QuotingType needsQuotes(StringRef S) { | ||||||||||||
| 675 | if (S.empty()) | ||||||||||||
| 676 | return QuotingType::Single; | ||||||||||||
| 677 | |||||||||||||
| 678 | QuotingType MaxQuotingNeeded = QuotingType::None; | ||||||||||||
| 679 | if (isSpace(static_cast<unsigned char>(S.front())) || | ||||||||||||
| 680 | isSpace(static_cast<unsigned char>(S.back()))) | ||||||||||||
| 681 | MaxQuotingNeeded = QuotingType::Single; | ||||||||||||
| 682 | if (isNull(S)) | ||||||||||||
| 683 | MaxQuotingNeeded = QuotingType::Single; | ||||||||||||
| 684 | if (isBool(S)) | ||||||||||||
| 685 | MaxQuotingNeeded = QuotingType::Single; | ||||||||||||
| 686 | if (isNumeric(S)) | ||||||||||||
| 687 | MaxQuotingNeeded = QuotingType::Single; | ||||||||||||
| 688 | |||||||||||||
| 689 | // 7.3.3 Plain Style | ||||||||||||
| 690 | // Plain scalars must not begin with most indicators, as this would cause | ||||||||||||
| 691 | // ambiguity with other YAML constructs. | ||||||||||||
| 692 | if (std::strchr(R"(-?:\,[]{}#&*!|>'"%@`)", S[0]) != nullptr) | ||||||||||||
| 693 | MaxQuotingNeeded = QuotingType::Single; | ||||||||||||
| 694 | |||||||||||||
| 695 | for (unsigned char C : S) { | ||||||||||||
| 696 | // Alphanum is safe. | ||||||||||||
| 697 | if (isAlnum(C)) | ||||||||||||
| 698 | continue; | ||||||||||||
| 699 | |||||||||||||
| 700 | switch (C) { | ||||||||||||
| 701 | // Safe scalar characters. | ||||||||||||
| 702 | case '_': | ||||||||||||
| 703 | case '-': | ||||||||||||
| 704 | case '^': | ||||||||||||
| 705 | case '.': | ||||||||||||
| 706 | case ',': | ||||||||||||
| 707 | case ' ': | ||||||||||||
| 708 | // TAB (0x9) is allowed in unquoted strings. | ||||||||||||
| 709 | case 0x9: | ||||||||||||
| 710 | continue; | ||||||||||||
| 711 | // LF(0xA) and CR(0xD) may delimit values and so require at least single | ||||||||||||
| 712 | // quotes. LLVM YAML parser cannot handle single quoted multiline so use | ||||||||||||
| 713 | // double quoting to produce valid YAML. | ||||||||||||
| 714 | case 0xA: | ||||||||||||
| 715 | case 0xD: | ||||||||||||
| 716 | return QuotingType::Double; | ||||||||||||
| 717 | // DEL (0x7F) are excluded from the allowed character range. | ||||||||||||
| 718 | case 0x7F: | ||||||||||||
| 719 | return QuotingType::Double; | ||||||||||||
| 720 | // Forward slash is allowed to be unquoted, but we quote it anyway. We have | ||||||||||||
| 721 | // many tests that use FileCheck against YAML output, and this output often | ||||||||||||
| 722 | // contains paths. If we quote backslashes but not forward slashes then | ||||||||||||
| 723 | // paths will come out either quoted or unquoted depending on which platform | ||||||||||||
| 724 | // the test is run on, making FileCheck comparisons difficult. | ||||||||||||
| 725 | case '/': | ||||||||||||
| 726 | default: { | ||||||||||||
| 727 | // C0 control block (0x0 - 0x1F) is excluded from the allowed character | ||||||||||||
| 728 | // range. | ||||||||||||
| 729 | if (C <= 0x1F) | ||||||||||||
| 730 | return QuotingType::Double; | ||||||||||||
| 731 | |||||||||||||
| 732 | // Always double quote UTF-8. | ||||||||||||
| 733 | if ((C & 0x80) != 0) | ||||||||||||
| 734 | return QuotingType::Double; | ||||||||||||
| 735 | |||||||||||||
| 736 | // The character is not safe, at least simple quoting needed. | ||||||||||||
| 737 | MaxQuotingNeeded = QuotingType::Single; | ||||||||||||
| 738 | } | ||||||||||||
| 739 | } | ||||||||||||
| 740 | } | ||||||||||||
| 741 | |||||||||||||
| 742 | return MaxQuotingNeeded; | ||||||||||||
| 743 | } | ||||||||||||
| 744 | |||||||||||||
| 745 | template <typename T, typename Context> | ||||||||||||
| 746 | struct missingTraits | ||||||||||||
| 747 | : public std::integral_constant<bool, | ||||||||||||
| 748 | !has_ScalarEnumerationTraits<T>::value && | ||||||||||||
| 749 | !has_ScalarBitSetTraits<T>::value && | ||||||||||||
| 750 | !has_ScalarTraits<T>::value && | ||||||||||||
| 751 | !has_BlockScalarTraits<T>::value && | ||||||||||||
| 752 | !has_TaggedScalarTraits<T>::value && | ||||||||||||
| 753 | !has_MappingTraits<T, Context>::value && | ||||||||||||
| 754 | !has_SequenceTraits<T>::value && | ||||||||||||
| 755 | !has_CustomMappingTraits<T>::value && | ||||||||||||
| 756 | !has_DocumentListTraits<T>::value && | ||||||||||||
| 757 | !has_PolymorphicTraits<T>::value> {}; | ||||||||||||
| 758 | |||||||||||||
| 759 | template <typename T, typename Context> | ||||||||||||
| 760 | struct validatedMappingTraits | ||||||||||||
| 761 | : public std::integral_constant< | ||||||||||||
| 762 | bool, has_MappingTraits<T, Context>::value && | ||||||||||||
| 763 | has_MappingValidateTraits<T, Context>::value> {}; | ||||||||||||
| 764 | |||||||||||||
| 765 | template <typename T, typename Context> | ||||||||||||
| 766 | struct unvalidatedMappingTraits | ||||||||||||
| 767 | : public std::integral_constant< | ||||||||||||
| 768 | bool, has_MappingTraits<T, Context>::value && | ||||||||||||
| 769 | !has_MappingValidateTraits<T, Context>::value> {}; | ||||||||||||
| 770 | |||||||||||||
| 771 | // Base class for Input and Output. | ||||||||||||
| 772 | class IO { | ||||||||||||
| 773 | public: | ||||||||||||
| 774 | IO(void *Ctxt = nullptr); | ||||||||||||
| 775 | virtual ~IO(); | ||||||||||||
| 776 | |||||||||||||
| 777 | virtual bool outputting() const = 0; | ||||||||||||
| 778 | |||||||||||||
| 779 | virtual unsigned beginSequence() = 0; | ||||||||||||
| 780 | virtual bool preflightElement(unsigned, void *&) = 0; | ||||||||||||
| 781 | virtual void postflightElement(void*) = 0; | ||||||||||||
| 782 | virtual void endSequence() = 0; | ||||||||||||
| 783 | virtual bool canElideEmptySequence() = 0; | ||||||||||||
| 784 | |||||||||||||
| 785 | virtual unsigned beginFlowSequence() = 0; | ||||||||||||
| 786 | virtual bool preflightFlowElement(unsigned, void *&) = 0; | ||||||||||||
| 787 | virtual void postflightFlowElement(void*) = 0; | ||||||||||||
| 788 | virtual void endFlowSequence() = 0; | ||||||||||||
| 789 | |||||||||||||
| 790 | virtual bool mapTag(StringRef Tag, bool Default=false) = 0; | ||||||||||||
| 791 | virtual void beginMapping() = 0; | ||||||||||||
| 792 | virtual void endMapping() = 0; | ||||||||||||
| 793 | virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0; | ||||||||||||
| 794 | virtual void postflightKey(void*) = 0; | ||||||||||||
| 795 | virtual std::vector<StringRef> keys() = 0; | ||||||||||||
| 796 | |||||||||||||
| 797 | virtual void beginFlowMapping() = 0; | ||||||||||||
| 798 | virtual void endFlowMapping() = 0; | ||||||||||||
| 799 | |||||||||||||
| 800 | virtual void beginEnumScalar() = 0; | ||||||||||||
| 801 | virtual bool matchEnumScalar(const char*, bool) = 0; | ||||||||||||
| 802 | virtual bool matchEnumFallback() = 0; | ||||||||||||
| 803 | virtual void endEnumScalar() = 0; | ||||||||||||
| 804 | |||||||||||||
| 805 | virtual bool beginBitSetScalar(bool &) = 0; | ||||||||||||
| 806 | virtual bool bitSetMatch(const char*, bool) = 0; | ||||||||||||
| 807 | virtual void endBitSetScalar() = 0; | ||||||||||||
| 808 | |||||||||||||
| 809 | virtual void scalarString(StringRef &, QuotingType) = 0; | ||||||||||||
| 810 | virtual void blockScalarString(StringRef &) = 0; | ||||||||||||
| 811 | virtual void scalarTag(std::string &) = 0; | ||||||||||||
| 812 | |||||||||||||
| 813 | virtual NodeKind getNodeKind() = 0; | ||||||||||||
| 814 | |||||||||||||
| 815 | virtual void setError(const Twine &) = 0; | ||||||||||||
| 816 | virtual void setAllowUnknownKeys(bool Allow); | ||||||||||||
| 817 | |||||||||||||
| 818 | template <typename T> | ||||||||||||
| 819 | void enumCase(T &Val, const char* Str, const T ConstVal) { | ||||||||||||
| 820 | if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) { | ||||||||||||
| 821 | Val = ConstVal; | ||||||||||||
| 822 | } | ||||||||||||
| 823 | } | ||||||||||||
| 824 | |||||||||||||
| 825 | // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF | ||||||||||||
| 826 | template <typename T> | ||||||||||||
| 827 | void enumCase(T &Val, const char* Str, const uint32_t ConstVal) { | ||||||||||||
| 828 | if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) { | ||||||||||||
| 829 | Val = ConstVal; | ||||||||||||
| 830 | } | ||||||||||||
| 831 | } | ||||||||||||
| 832 | |||||||||||||
| 833 | template <typename FBT, typename T> | ||||||||||||
| 834 | void enumFallback(T &Val) { | ||||||||||||
| 835 | if (matchEnumFallback()) { | ||||||||||||
| 836 | EmptyContext Context; | ||||||||||||
| 837 | // FIXME: Force integral conversion to allow strong typedefs to convert. | ||||||||||||
| 838 | FBT Res = static_cast<typename FBT::BaseType>(Val); | ||||||||||||
| 839 | yamlize(*this, Res, true, Context); | ||||||||||||
| 840 | Val = static_cast<T>(static_cast<typename FBT::BaseType>(Res)); | ||||||||||||
| 841 | } | ||||||||||||
| 842 | } | ||||||||||||
| 843 | |||||||||||||
| 844 | template <typename T> | ||||||||||||
| 845 | void bitSetCase(T &Val, const char* Str, const T ConstVal) { | ||||||||||||
| 846 | if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) { | ||||||||||||
| 847 | Val = static_cast<T>(Val | ConstVal); | ||||||||||||
| 848 | } | ||||||||||||
| 849 | } | ||||||||||||
| 850 | |||||||||||||
| 851 | // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF | ||||||||||||
| 852 | template <typename T> | ||||||||||||
| 853 | void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) { | ||||||||||||
| 854 | if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) { | ||||||||||||
| 855 | Val = static_cast<T>(Val | ConstVal); | ||||||||||||
| 856 | } | ||||||||||||
| 857 | } | ||||||||||||
| 858 | |||||||||||||
| 859 | template <typename T> | ||||||||||||
| 860 | void maskedBitSetCase(T &Val, const char *Str, T ConstVal, T Mask) { | ||||||||||||
| 861 | if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal)) | ||||||||||||
| 862 | Val = Val | ConstVal; | ||||||||||||
| 863 | } | ||||||||||||
| 864 | |||||||||||||
| 865 | template <typename T> | ||||||||||||
| 866 | void maskedBitSetCase(T &Val, const char *Str, uint32_t ConstVal, | ||||||||||||
| 867 | uint32_t Mask) { | ||||||||||||
| 868 | if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal)) | ||||||||||||
| 869 | Val = Val | ConstVal; | ||||||||||||
| 870 | } | ||||||||||||
| 871 | |||||||||||||
| 872 | void *getContext() const; | ||||||||||||
| 873 | void setContext(void *); | ||||||||||||
| 874 | |||||||||||||
| 875 | template <typename T> void mapRequired(const char *Key, T &Val) { | ||||||||||||
| 876 | EmptyContext Ctx; | ||||||||||||
| 877 | this->processKey(Key, Val, true, Ctx); | ||||||||||||
| 878 | } | ||||||||||||
| 879 | |||||||||||||
| 880 | template <typename T, typename Context> | ||||||||||||
| 881 | void mapRequired(const char *Key, T &Val, Context &Ctx) { | ||||||||||||
| 882 | this->processKey(Key, Val, true, Ctx); | ||||||||||||
| 883 | } | ||||||||||||
| 884 | |||||||||||||
| 885 | template <typename T> void mapOptional(const char *Key, T &Val) { | ||||||||||||
| 886 | EmptyContext Ctx; | ||||||||||||
| 887 | mapOptionalWithContext(Key, Val, Ctx); | ||||||||||||
| 888 | } | ||||||||||||
| 889 | |||||||||||||
| 890 | template <typename T, typename DefaultT> | ||||||||||||
| 891 | void mapOptional(const char *Key, T &Val, const DefaultT &Default) { | ||||||||||||
| 892 | EmptyContext Ctx; | ||||||||||||
| 893 | mapOptionalWithContext(Key, Val, Default, Ctx); | ||||||||||||
| 894 | } | ||||||||||||
| 895 | |||||||||||||
| 896 | template <typename T, typename Context> | ||||||||||||
| 897 | std::enable_if_t<has_SequenceTraits<T>::value, void> | ||||||||||||
| 898 | mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) { | ||||||||||||
| 899 | // omit key/value instead of outputting empty sequence | ||||||||||||
| 900 | if (this->canElideEmptySequence() && !(Val.begin() != Val.end())) | ||||||||||||
| 901 | return; | ||||||||||||
| 902 | this->processKey(Key, Val, false, Ctx); | ||||||||||||
| 903 | } | ||||||||||||
| 904 | |||||||||||||
| 905 | template <typename T, typename Context> | ||||||||||||
| 906 | void mapOptionalWithContext(const char *Key, std::optional<T> &Val, | ||||||||||||
| 907 | Context &Ctx) { | ||||||||||||
| 908 | this->processKeyWithDefault(Key, Val, std::optional<T>(), | ||||||||||||
| 909 | /*Required=*/false, Ctx); | ||||||||||||
| 910 | } | ||||||||||||
| 911 | |||||||||||||
| 912 | template <typename T, typename Context> | ||||||||||||
| 913 | std::enable_if_t<!has_SequenceTraits<T>::value, void> | ||||||||||||
| 914 | mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) { | ||||||||||||
| 915 | this->processKey(Key, Val, false, Ctx); | ||||||||||||
| 916 | } | ||||||||||||
| 917 | |||||||||||||
| 918 | template <typename T, typename Context, typename DefaultT> | ||||||||||||
| 919 | void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default, | ||||||||||||
| 920 | Context &Ctx) { | ||||||||||||
| 921 | static_assert(std::is_convertible<DefaultT, T>::value, | ||||||||||||
| 922 | "Default type must be implicitly convertible to value type!"); | ||||||||||||
| 923 | this->processKeyWithDefault(Key, Val, static_cast<const T &>(Default), | ||||||||||||
| 924 | false, Ctx); | ||||||||||||
| 925 | } | ||||||||||||
| 926 | |||||||||||||
| 927 | private: | ||||||||||||
| 928 | template <typename T, typename Context> | ||||||||||||
| 929 | void processKeyWithDefault(const char *Key, std::optional<T> &Val, | ||||||||||||
| 930 | const std::optional<T> &DefaultValue, | ||||||||||||
| 931 | bool Required, Context &Ctx); | ||||||||||||
| 932 | |||||||||||||
| 933 | template <typename T, typename Context> | ||||||||||||
| 934 | void processKeyWithDefault(const char *Key, T &Val, const T &DefaultValue, | ||||||||||||
| 935 | bool Required, Context &Ctx) { | ||||||||||||
| 936 | void *SaveInfo; | ||||||||||||
| 937 | bool UseDefault; | ||||||||||||
| 938 | const bool sameAsDefault = outputting() && Val == DefaultValue; | ||||||||||||
| 939 | if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault, | ||||||||||||
| 940 | SaveInfo) ) { | ||||||||||||
| 941 | yamlize(*this, Val, Required, Ctx); | ||||||||||||
| 942 | this->postflightKey(SaveInfo); | ||||||||||||
| 943 | } | ||||||||||||
| 944 | else { | ||||||||||||
| 945 | if ( UseDefault ) | ||||||||||||
| 946 | Val = DefaultValue; | ||||||||||||
| 947 | } | ||||||||||||
| 948 | } | ||||||||||||
| 949 | |||||||||||||
| 950 | template <typename T, typename Context> | ||||||||||||
| 951 | void processKey(const char *Key, T &Val, bool Required, Context &Ctx) { | ||||||||||||
| 952 | void *SaveInfo; | ||||||||||||
| 953 | bool UseDefault; | ||||||||||||
| 954 | if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) { | ||||||||||||
| 955 | yamlize(*this, Val, Required, Ctx); | ||||||||||||
| 956 | this->postflightKey(SaveInfo); | ||||||||||||
| 957 | } | ||||||||||||
| 958 | } | ||||||||||||
| 959 | |||||||||||||
| 960 | private: | ||||||||||||
| 961 | void *Ctxt; | ||||||||||||
| 962 | }; | ||||||||||||
| 963 | |||||||||||||
| 964 | namespace detail { | ||||||||||||
| 965 | |||||||||||||
| 966 | template <typename T, typename Context> | ||||||||||||
| 967 | void doMapping(IO &io, T &Val, Context &Ctx) { | ||||||||||||
| 968 | MappingContextTraits<T, Context>::mapping(io, Val, Ctx); | ||||||||||||
| 969 | } | ||||||||||||
| 970 | |||||||||||||
| 971 | template <typename T> void doMapping(IO &io, T &Val, EmptyContext &Ctx) { | ||||||||||||
| 972 | MappingTraits<T>::mapping(io, Val); | ||||||||||||
| 973 | } | ||||||||||||
| 974 | |||||||||||||
| 975 | } // end namespace detail | ||||||||||||
| 976 | |||||||||||||
| 977 | template <typename T> | ||||||||||||
| 978 | std::enable_if_t<has_ScalarEnumerationTraits<T>::value, void> | ||||||||||||
| 979 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { | ||||||||||||
| 980 | io.beginEnumScalar(); | ||||||||||||
| 981 | ScalarEnumerationTraits<T>::enumeration(io, Val); | ||||||||||||
| 982 | io.endEnumScalar(); | ||||||||||||
| 983 | } | ||||||||||||
| 984 | |||||||||||||
| 985 | template <typename T> | ||||||||||||
| 986 | std::enable_if_t<has_ScalarBitSetTraits<T>::value, void> | ||||||||||||
| 987 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { | ||||||||||||
| 988 | bool DoClear; | ||||||||||||
| 989 | if ( io.beginBitSetScalar(DoClear) ) { | ||||||||||||
| 990 | if ( DoClear ) | ||||||||||||
| 991 | Val = T(); | ||||||||||||
| 992 | ScalarBitSetTraits<T>::bitset(io, Val); | ||||||||||||
| 993 | io.endBitSetScalar(); | ||||||||||||
| 994 | } | ||||||||||||
| 995 | } | ||||||||||||
| 996 | |||||||||||||
| 997 | template <typename T> | ||||||||||||
| 998 | std::enable_if_t<has_ScalarTraits<T>::value, void> yamlize(IO &io, T &Val, bool, | ||||||||||||
| 999 | EmptyContext &Ctx) { | ||||||||||||
| 1000 | if ( io.outputting() ) { | ||||||||||||
| 1001 | SmallString<128> Storage; | ||||||||||||
| 1002 | raw_svector_ostream Buffer(Storage); | ||||||||||||
| 1003 | ScalarTraits<T>::output(Val, io.getContext(), Buffer); | ||||||||||||
| 1004 | StringRef Str = Buffer.str(); | ||||||||||||
| 1005 | io.scalarString(Str, ScalarTraits<T>::mustQuote(Str)); | ||||||||||||
| 1006 | } | ||||||||||||
| 1007 | else { | ||||||||||||
| 1008 | StringRef Str; | ||||||||||||
| 1009 | io.scalarString(Str, ScalarTraits<T>::mustQuote(Str)); | ||||||||||||
| 1010 | StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val); | ||||||||||||
| 1011 | if ( !Result.empty() ) { | ||||||||||||
| 1012 | io.setError(Twine(Result)); | ||||||||||||
| 1013 | } | ||||||||||||
| 1014 | } | ||||||||||||
| 1015 | } | ||||||||||||
| 1016 | |||||||||||||
| 1017 | template <typename T> | ||||||||||||
| 1018 | std::enable_if_t<has_BlockScalarTraits<T>::value, void> | ||||||||||||
| 1019 | yamlize(IO &YamlIO, T &Val, bool, EmptyContext &Ctx) { | ||||||||||||
| 1020 | if (YamlIO.outputting()) { | ||||||||||||
| 1021 | std::string Storage; | ||||||||||||
| 1022 | raw_string_ostream Buffer(Storage); | ||||||||||||
| 1023 | BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer); | ||||||||||||
| 1024 | StringRef Str = Buffer.str(); | ||||||||||||
| 1025 | YamlIO.blockScalarString(Str); | ||||||||||||
| 1026 | } else { | ||||||||||||
| 1027 | StringRef Str; | ||||||||||||
| 1028 | YamlIO.blockScalarString(Str); | ||||||||||||
| 1029 | StringRef Result = | ||||||||||||
| 1030 | BlockScalarTraits<T>::input(Str, YamlIO.getContext(), Val); | ||||||||||||
| 1031 | if (!Result.empty()) | ||||||||||||
| 1032 | YamlIO.setError(Twine(Result)); | ||||||||||||
| 1033 | } | ||||||||||||
| 1034 | } | ||||||||||||
| 1035 | |||||||||||||
| 1036 | template <typename T> | ||||||||||||
| 1037 | std::enable_if_t<has_TaggedScalarTraits<T>::value, void> | ||||||||||||
| 1038 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { | ||||||||||||
| 1039 | if (io.outputting()) { | ||||||||||||
| 1040 | std::string ScalarStorage, TagStorage; | ||||||||||||
| 1041 | raw_string_ostream ScalarBuffer(ScalarStorage), TagBuffer(TagStorage); | ||||||||||||
| 1042 | TaggedScalarTraits<T>::output(Val, io.getContext(), ScalarBuffer, | ||||||||||||
| 1043 | TagBuffer); | ||||||||||||
| 1044 | io.scalarTag(TagBuffer.str()); | ||||||||||||
| 1045 | StringRef ScalarStr = ScalarBuffer.str(); | ||||||||||||
| 1046 | io.scalarString(ScalarStr, | ||||||||||||
| 1047 | TaggedScalarTraits<T>::mustQuote(Val, ScalarStr)); | ||||||||||||
| 1048 | } else { | ||||||||||||
| 1049 | std::string Tag; | ||||||||||||
| 1050 | io.scalarTag(Tag); | ||||||||||||
| 1051 | StringRef Str; | ||||||||||||
| 1052 | io.scalarString(Str, QuotingType::None); | ||||||||||||
| 1053 | StringRef Result = | ||||||||||||
| 1054 | TaggedScalarTraits<T>::input(Str, Tag, io.getContext(), Val); | ||||||||||||
| 1055 | if (!Result.empty()) { | ||||||||||||
| 1056 | io.setError(Twine(Result)); | ||||||||||||
| 1057 | } | ||||||||||||
| 1058 | } | ||||||||||||
| 1059 | } | ||||||||||||
| 1060 | |||||||||||||
| 1061 | template <typename T, typename Context> | ||||||||||||
| 1062 | std::enable_if_t<validatedMappingTraits<T, Context>::value, void> | ||||||||||||
| 1063 | yamlize(IO &io, T &Val, bool, Context &Ctx) { | ||||||||||||
| 1064 | if (has_FlowTraits<MappingTraits<T>>::value) | ||||||||||||
| 1065 | io.beginFlowMapping(); | ||||||||||||
| 1066 | else | ||||||||||||
| 1067 | io.beginMapping(); | ||||||||||||
| 1068 | if (io.outputting()) { | ||||||||||||
| 1069 | std::string Err = MappingTraits<T>::validate(io, Val); | ||||||||||||
| 1070 | if (!Err.empty()) { | ||||||||||||
| 1071 | errs() << Err << "\n"; | ||||||||||||
| 1072 | assert(Err.empty() && "invalid struct trying to be written as yaml")(static_cast <bool> (Err.empty() && "invalid struct trying to be written as yaml" ) ? void (0) : __assert_fail ("Err.empty() && \"invalid struct trying to be written as yaml\"" , "llvm/include/llvm/Support/YAMLTraits.h", 1072, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
| 1073 | } | ||||||||||||
| 1074 | } | ||||||||||||
| 1075 | detail::doMapping(io, Val, Ctx); | ||||||||||||
| 1076 | if (!io.outputting()) { | ||||||||||||
| 1077 | std::string Err = MappingTraits<T>::validate(io, Val); | ||||||||||||
| 1078 | if (!Err.empty()) | ||||||||||||
| 1079 | io.setError(Err); | ||||||||||||
| 1080 | } | ||||||||||||
| 1081 | if (has_FlowTraits<MappingTraits<T>>::value) | ||||||||||||
| 1082 | io.endFlowMapping(); | ||||||||||||
| 1083 | else | ||||||||||||
| 1084 | io.endMapping(); | ||||||||||||
| 1085 | } | ||||||||||||
| 1086 | |||||||||||||
| 1087 | template <typename T, typename Context> | ||||||||||||
| 1088 | std::enable_if_t<!has_MappingEnumInputTraits<T, Context>::value, bool> | ||||||||||||
| 1089 | yamlizeMappingEnumInput(IO &io, T &Val) { | ||||||||||||
| 1090 | return false; | ||||||||||||
| 1091 | } | ||||||||||||
| 1092 | |||||||||||||
| 1093 | template <typename T, typename Context> | ||||||||||||
| 1094 | std::enable_if_t<has_MappingEnumInputTraits<T, Context>::value, bool> | ||||||||||||
| 1095 | yamlizeMappingEnumInput(IO &io, T &Val) { | ||||||||||||
| 1096 | if (io.outputting()) | ||||||||||||
| 1097 | return false; | ||||||||||||
| 1098 | |||||||||||||
| 1099 | io.beginEnumScalar(); | ||||||||||||
| 1100 | MappingTraits<T>::enumInput(io, Val); | ||||||||||||
| 1101 | bool Matched = !io.matchEnumFallback(); | ||||||||||||
| 1102 | io.endEnumScalar(); | ||||||||||||
| 1103 | return Matched; | ||||||||||||
| 1104 | } | ||||||||||||
| 1105 | |||||||||||||
| 1106 | template <typename T, typename Context> | ||||||||||||
| 1107 | std::enable_if_t<unvalidatedMappingTraits<T, Context>::value, void> | ||||||||||||
| 1108 | yamlize(IO &io, T &Val, bool, Context &Ctx) { | ||||||||||||
| 1109 | if (yamlizeMappingEnumInput<T, Context>(io, Val)) | ||||||||||||
| 1110 | return; | ||||||||||||
| 1111 | if (has_FlowTraits<MappingTraits<T>>::value
| ||||||||||||
| 1112 | io.beginFlowMapping(); | ||||||||||||
| 1113 | detail::doMapping(io, Val, Ctx); | ||||||||||||
| 1114 | io.endFlowMapping(); | ||||||||||||
| 1115 | } else { | ||||||||||||
| 1116 | io.beginMapping(); | ||||||||||||
| 1117 | detail::doMapping(io, Val, Ctx); | ||||||||||||
| 1118 | io.endMapping(); | ||||||||||||
| 1119 | } | ||||||||||||
| 1120 | } | ||||||||||||
| 1121 | |||||||||||||
| 1122 | template <typename T> | ||||||||||||
| 1123 | std::enable_if_t<has_CustomMappingTraits<T>::value, void> | ||||||||||||
| 1124 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { | ||||||||||||
| 1125 | if ( io.outputting() ) { | ||||||||||||
| 1126 | io.beginMapping(); | ||||||||||||
| 1127 | CustomMappingTraits<T>::output(io, Val); | ||||||||||||
| 1128 | io.endMapping(); | ||||||||||||
| 1129 | } else { | ||||||||||||
| 1130 | io.beginMapping(); | ||||||||||||
| 1131 | for (StringRef key : io.keys()) | ||||||||||||
| 1132 | CustomMappingTraits<T>::inputOne(io, key, Val); | ||||||||||||
| 1133 | io.endMapping(); | ||||||||||||
| 1134 | } | ||||||||||||
| 1135 | } | ||||||||||||
| 1136 | |||||||||||||
| 1137 | template <typename T> | ||||||||||||
| 1138 | std::enable_if_t<has_PolymorphicTraits<T>::value, void> | ||||||||||||
| 1139 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { | ||||||||||||
| 1140 | switch (io.outputting() ? PolymorphicTraits<T>::getKind(Val) | ||||||||||||
| 1141 | : io.getNodeKind()) { | ||||||||||||
| 1142 | case NodeKind::Scalar: | ||||||||||||
| 1143 | return yamlize(io, PolymorphicTraits<T>::getAsScalar(Val), true, Ctx); | ||||||||||||
| 1144 | case NodeKind::Map: | ||||||||||||
| 1145 | return yamlize(io, PolymorphicTraits<T>::getAsMap(Val), true, Ctx); | ||||||||||||
| 1146 | case NodeKind::Sequence: | ||||||||||||
| 1147 | return yamlize(io, PolymorphicTraits<T>::getAsSequence(Val), true, Ctx); | ||||||||||||
| 1148 | } | ||||||||||||
| 1149 | } | ||||||||||||
| 1150 | |||||||||||||
| 1151 | template <typename T> | ||||||||||||
| 1152 | std::enable_if_t<missingTraits<T, EmptyContext>::value, void> | ||||||||||||
| 1153 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { | ||||||||||||
| 1154 | char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)]; | ||||||||||||
| 1155 | } | ||||||||||||
| 1156 | |||||||||||||
| 1157 | template <typename T, typename Context> | ||||||||||||
| 1158 | std::enable_if_t<has_SequenceTraits<T>::value, void> | ||||||||||||
| 1159 | yamlize(IO &io, T &Seq, bool, Context &Ctx) { | ||||||||||||
| 1160 | if ( has_FlowTraits< SequenceTraits<T>>::value ) { | ||||||||||||
| 1161 | unsigned incnt = io.beginFlowSequence(); | ||||||||||||
| 1162 | unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt; | ||||||||||||
| 1163 | for(unsigned i=0; i < count; ++i) { | ||||||||||||
| 1164 | void *SaveInfo; | ||||||||||||
| 1165 | if ( io.preflightFlowElement(i, SaveInfo) ) { | ||||||||||||
| 1166 | yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx); | ||||||||||||
| 1167 | io.postflightFlowElement(SaveInfo); | ||||||||||||
| 1168 | } | ||||||||||||
| 1169 | } | ||||||||||||
| 1170 | io.endFlowSequence(); | ||||||||||||
| 1171 | } | ||||||||||||
| 1172 | else { | ||||||||||||
| 1173 | unsigned incnt = io.beginSequence(); | ||||||||||||
| 1174 | unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt; | ||||||||||||
| 1175 | for(unsigned i=0; i < count; ++i) { | ||||||||||||
| 1176 | void *SaveInfo; | ||||||||||||
| 1177 | if ( io.preflightElement(i, SaveInfo) ) { | ||||||||||||
| 1178 | yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx); | ||||||||||||
| 1179 | io.postflightElement(SaveInfo); | ||||||||||||
| 1180 | } | ||||||||||||
| 1181 | } | ||||||||||||
| 1182 | io.endSequence(); | ||||||||||||
| 1183 | } | ||||||||||||
| 1184 | } | ||||||||||||
| 1185 | |||||||||||||
| 1186 | template<> | ||||||||||||
| 1187 | struct ScalarTraits<bool> { | ||||||||||||
| 1188 | static void output(const bool &, void* , raw_ostream &); | ||||||||||||
| 1189 | static StringRef input(StringRef, void *, bool &); | ||||||||||||
| 1190 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1191 | }; | ||||||||||||
| 1192 | |||||||||||||
| 1193 | template<> | ||||||||||||
| 1194 | struct ScalarTraits<StringRef> { | ||||||||||||
| 1195 | static void output(const StringRef &, void *, raw_ostream &); | ||||||||||||
| 1196 | static StringRef input(StringRef, void *, StringRef &); | ||||||||||||
| 1197 | static QuotingType mustQuote(StringRef S) { return needsQuotes(S); } | ||||||||||||
| 1198 | }; | ||||||||||||
| 1199 | |||||||||||||
| 1200 | template<> | ||||||||||||
| 1201 | struct ScalarTraits<std::string> { | ||||||||||||
| 1202 | static void output(const std::string &, void *, raw_ostream &); | ||||||||||||
| 1203 | static StringRef input(StringRef, void *, std::string &); | ||||||||||||
| 1204 | static QuotingType mustQuote(StringRef S) { return needsQuotes(S); } | ||||||||||||
| 1205 | }; | ||||||||||||
| 1206 | |||||||||||||
| 1207 | template<> | ||||||||||||
| 1208 | struct ScalarTraits<uint8_t> { | ||||||||||||
| 1209 | static void output(const uint8_t &, void *, raw_ostream &); | ||||||||||||
| 1210 | static StringRef input(StringRef, void *, uint8_t &); | ||||||||||||
| 1211 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1212 | }; | ||||||||||||
| 1213 | |||||||||||||
| 1214 | template<> | ||||||||||||
| 1215 | struct ScalarTraits<uint16_t> { | ||||||||||||
| 1216 | static void output(const uint16_t &, void *, raw_ostream &); | ||||||||||||
| 1217 | static StringRef input(StringRef, void *, uint16_t &); | ||||||||||||
| 1218 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1219 | }; | ||||||||||||
| 1220 | |||||||||||||
| 1221 | template<> | ||||||||||||
| 1222 | struct ScalarTraits<uint32_t> { | ||||||||||||
| 1223 | static void output(const uint32_t &, void *, raw_ostream &); | ||||||||||||
| 1224 | static StringRef input(StringRef, void *, uint32_t &); | ||||||||||||
| 1225 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1226 | }; | ||||||||||||
| 1227 | |||||||||||||
| 1228 | template<> | ||||||||||||
| 1229 | struct ScalarTraits<uint64_t> { | ||||||||||||
| 1230 | static void output(const uint64_t &, void *, raw_ostream &); | ||||||||||||
| 1231 | static StringRef input(StringRef, void *, uint64_t &); | ||||||||||||
| 1232 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1233 | }; | ||||||||||||
| 1234 | |||||||||||||
| 1235 | template<> | ||||||||||||
| 1236 | struct ScalarTraits<int8_t> { | ||||||||||||
| 1237 | static void output(const int8_t &, void *, raw_ostream &); | ||||||||||||
| 1238 | static StringRef input(StringRef, void *, int8_t &); | ||||||||||||
| 1239 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1240 | }; | ||||||||||||
| 1241 | |||||||||||||
| 1242 | template<> | ||||||||||||
| 1243 | struct ScalarTraits<int16_t> { | ||||||||||||
| 1244 | static void output(const int16_t &, void *, raw_ostream &); | ||||||||||||
| 1245 | static StringRef input(StringRef, void *, int16_t &); | ||||||||||||
| 1246 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1247 | }; | ||||||||||||
| 1248 | |||||||||||||
| 1249 | template<> | ||||||||||||
| 1250 | struct ScalarTraits<int32_t> { | ||||||||||||
| 1251 | static void output(const int32_t &, void *, raw_ostream &); | ||||||||||||
| 1252 | static StringRef input(StringRef, void *, int32_t &); | ||||||||||||
| 1253 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1254 | }; | ||||||||||||
| 1255 | |||||||||||||
| 1256 | template<> | ||||||||||||
| 1257 | struct ScalarTraits<int64_t> { | ||||||||||||
| 1258 | static void output(const int64_t &, void *, raw_ostream &); | ||||||||||||
| 1259 | static StringRef input(StringRef, void *, int64_t &); | ||||||||||||
| 1260 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1261 | }; | ||||||||||||
| 1262 | |||||||||||||
| 1263 | template<> | ||||||||||||
| 1264 | struct ScalarTraits<float> { | ||||||||||||
| 1265 | static void output(const float &, void *, raw_ostream &); | ||||||||||||
| 1266 | static StringRef input(StringRef, void *, float &); | ||||||||||||
| 1267 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1268 | }; | ||||||||||||
| 1269 | |||||||||||||
| 1270 | template<> | ||||||||||||
| 1271 | struct ScalarTraits<double> { | ||||||||||||
| 1272 | static void output(const double &, void *, raw_ostream &); | ||||||||||||
| 1273 | static StringRef input(StringRef, void *, double &); | ||||||||||||
| 1274 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1275 | }; | ||||||||||||
| 1276 | |||||||||||||
| 1277 | // For endian types, we use existing scalar Traits class for the underlying | ||||||||||||
| 1278 | // type. This way endian aware types are supported whenever the traits are | ||||||||||||
| 1279 | // defined for the underlying type. | ||||||||||||
| 1280 | template <typename value_type, support::endianness endian, size_t alignment> | ||||||||||||
| 1281 | struct ScalarTraits<support::detail::packed_endian_specific_integral< | ||||||||||||
| 1282 | value_type, endian, alignment>, | ||||||||||||
| 1283 | std::enable_if_t<has_ScalarTraits<value_type>::value>> { | ||||||||||||
| 1284 | using endian_type = | ||||||||||||
| 1285 | support::detail::packed_endian_specific_integral<value_type, endian, | ||||||||||||
| 1286 | alignment>; | ||||||||||||
| 1287 | |||||||||||||
| 1288 | static void output(const endian_type &E, void *Ctx, raw_ostream &Stream) { | ||||||||||||
| 1289 | ScalarTraits<value_type>::output(static_cast<value_type>(E), Ctx, Stream); | ||||||||||||
| 1290 | } | ||||||||||||
| 1291 | |||||||||||||
| 1292 | static StringRef input(StringRef Str, void *Ctx, endian_type &E) { | ||||||||||||
| 1293 | value_type V; | ||||||||||||
| 1294 | auto R = ScalarTraits<value_type>::input(Str, Ctx, V); | ||||||||||||
| 1295 | E = static_cast<endian_type>(V); | ||||||||||||
| 1296 | return R; | ||||||||||||
| 1297 | } | ||||||||||||
| 1298 | |||||||||||||
| 1299 | static QuotingType mustQuote(StringRef Str) { | ||||||||||||
| 1300 | return ScalarTraits<value_type>::mustQuote(Str); | ||||||||||||
| 1301 | } | ||||||||||||
| 1302 | }; | ||||||||||||
| 1303 | |||||||||||||
| 1304 | template <typename value_type, support::endianness endian, size_t alignment> | ||||||||||||
| 1305 | struct ScalarEnumerationTraits< | ||||||||||||
| 1306 | support::detail::packed_endian_specific_integral<value_type, endian, | ||||||||||||
| 1307 | alignment>, | ||||||||||||
| 1308 | std::enable_if_t<has_ScalarEnumerationTraits<value_type>::value>> { | ||||||||||||
| 1309 | using endian_type = | ||||||||||||
| 1310 | support::detail::packed_endian_specific_integral<value_type, endian, | ||||||||||||
| 1311 | alignment>; | ||||||||||||
| 1312 | |||||||||||||
| 1313 | static void enumeration(IO &io, endian_type &E) { | ||||||||||||
| 1314 | value_type V = E; | ||||||||||||
| 1315 | ScalarEnumerationTraits<value_type>::enumeration(io, V); | ||||||||||||
| 1316 | E = V; | ||||||||||||
| 1317 | } | ||||||||||||
| 1318 | }; | ||||||||||||
| 1319 | |||||||||||||
| 1320 | template <typename value_type, support::endianness endian, size_t alignment> | ||||||||||||
| 1321 | struct ScalarBitSetTraits< | ||||||||||||
| 1322 | support::detail::packed_endian_specific_integral<value_type, endian, | ||||||||||||
| 1323 | alignment>, | ||||||||||||
| 1324 | std::enable_if_t<has_ScalarBitSetTraits<value_type>::value>> { | ||||||||||||
| 1325 | using endian_type = | ||||||||||||
| 1326 | support::detail::packed_endian_specific_integral<value_type, endian, | ||||||||||||
| 1327 | alignment>; | ||||||||||||
| 1328 | static void bitset(IO &io, endian_type &E) { | ||||||||||||
| 1329 | value_type V = E; | ||||||||||||
| 1330 | ScalarBitSetTraits<value_type>::bitset(io, V); | ||||||||||||
| 1331 | E = V; | ||||||||||||
| 1332 | } | ||||||||||||
| 1333 | }; | ||||||||||||
| 1334 | |||||||||||||
| 1335 | // Utility for use within MappingTraits<>::mapping() method | ||||||||||||
| 1336 | // to [de]normalize an object for use with YAML conversion. | ||||||||||||
| 1337 | template <typename TNorm, typename TFinal> | ||||||||||||
| 1338 | struct MappingNormalization { | ||||||||||||
| 1339 | MappingNormalization(IO &i_o, TFinal &Obj) | ||||||||||||
| 1340 | : io(i_o), BufPtr(nullptr), Result(Obj) { | ||||||||||||
| 1341 | if ( io.outputting() ) { | ||||||||||||
| 1342 | BufPtr = new (&Buffer) TNorm(io, Obj); | ||||||||||||
| 1343 | } | ||||||||||||
| 1344 | else { | ||||||||||||
| 1345 | BufPtr = new (&Buffer) TNorm(io); | ||||||||||||
| 1346 | } | ||||||||||||
| 1347 | } | ||||||||||||
| 1348 | |||||||||||||
| 1349 | ~MappingNormalization() { | ||||||||||||
| 1350 | if ( ! io.outputting() ) { | ||||||||||||
| 1351 | Result = BufPtr->denormalize(io); | ||||||||||||
| 1352 | } | ||||||||||||
| 1353 | BufPtr->~TNorm(); | ||||||||||||
| 1354 | } | ||||||||||||
| 1355 | |||||||||||||
| 1356 | TNorm* operator->() { return BufPtr; } | ||||||||||||
| 1357 | |||||||||||||
| 1358 | private: | ||||||||||||
| 1359 | using Storage = AlignedCharArrayUnion<TNorm>; | ||||||||||||
| 1360 | |||||||||||||
| 1361 | Storage Buffer; | ||||||||||||
| 1362 | IO &io; | ||||||||||||
| 1363 | TNorm *BufPtr; | ||||||||||||
| 1364 | TFinal &Result; | ||||||||||||
| 1365 | }; | ||||||||||||
| 1366 | |||||||||||||
| 1367 | // Utility for use within MappingTraits<>::mapping() method | ||||||||||||
| 1368 | // to [de]normalize an object for use with YAML conversion. | ||||||||||||
| 1369 | template <typename TNorm, typename TFinal> | ||||||||||||
| 1370 | struct MappingNormalizationHeap { | ||||||||||||
| 1371 | MappingNormalizationHeap(IO &i_o, TFinal &Obj, BumpPtrAllocator *allocator) | ||||||||||||
| 1372 | : io(i_o), Result(Obj) { | ||||||||||||
| 1373 | if ( io.outputting() ) { | ||||||||||||
| 1374 | BufPtr = new (&Buffer) TNorm(io, Obj); | ||||||||||||
| 1375 | } | ||||||||||||
| 1376 | else if (allocator) { | ||||||||||||
| 1377 | BufPtr = allocator->Allocate<TNorm>(); | ||||||||||||
| 1378 | new (BufPtr) TNorm(io); | ||||||||||||
| 1379 | } else { | ||||||||||||
| 1380 | BufPtr = new TNorm(io); | ||||||||||||
| 1381 | } | ||||||||||||
| 1382 | } | ||||||||||||
| 1383 | |||||||||||||
| 1384 | ~MappingNormalizationHeap() { | ||||||||||||
| 1385 | if ( io.outputting() ) { | ||||||||||||
| 1386 | BufPtr->~TNorm(); | ||||||||||||
| 1387 | } | ||||||||||||
| 1388 | else { | ||||||||||||
| 1389 | Result = BufPtr->denormalize(io); | ||||||||||||
| 1390 | } | ||||||||||||
| 1391 | } | ||||||||||||
| 1392 | |||||||||||||
| 1393 | TNorm* operator->() { return BufPtr; } | ||||||||||||
| 1394 | |||||||||||||
| 1395 | private: | ||||||||||||
| 1396 | using Storage = AlignedCharArrayUnion<TNorm>; | ||||||||||||
| 1397 | |||||||||||||
| 1398 | Storage Buffer; | ||||||||||||
| 1399 | IO &io; | ||||||||||||
| 1400 | TNorm *BufPtr = nullptr; | ||||||||||||
| 1401 | TFinal &Result; | ||||||||||||
| 1402 | }; | ||||||||||||
| 1403 | |||||||||||||
| 1404 | /// | ||||||||||||
| 1405 | /// The Input class is used to parse a yaml document into in-memory structs | ||||||||||||
| 1406 | /// and vectors. | ||||||||||||
| 1407 | /// | ||||||||||||
| 1408 | /// It works by using YAMLParser to do a syntax parse of the entire yaml | ||||||||||||
| 1409 | /// document, then the Input class builds a graph of HNodes which wraps | ||||||||||||
| 1410 | /// each yaml Node. The extra layer is buffering. The low level yaml | ||||||||||||
| 1411 | /// parser only lets you look at each node once. The buffering layer lets | ||||||||||||
| 1412 | /// you search and interate multiple times. This is necessary because | ||||||||||||
| 1413 | /// the mapRequired() method calls may not be in the same order | ||||||||||||
| 1414 | /// as the keys in the document. | ||||||||||||
| 1415 | /// | ||||||||||||
| 1416 | class Input : public IO { | ||||||||||||
| 1417 | public: | ||||||||||||
| 1418 | // Construct a yaml Input object from a StringRef and optional | ||||||||||||
| 1419 | // user-data. The DiagHandler can be specified to provide | ||||||||||||
| 1420 | // alternative error reporting. | ||||||||||||
| 1421 | Input(StringRef InputContent, | ||||||||||||
| 1422 | void *Ctxt = nullptr, | ||||||||||||
| 1423 | SourceMgr::DiagHandlerTy DiagHandler = nullptr, | ||||||||||||
| 1424 | void *DiagHandlerCtxt = nullptr); | ||||||||||||
| 1425 | Input(MemoryBufferRef Input, | ||||||||||||
| 1426 | void *Ctxt = nullptr, | ||||||||||||
| 1427 | SourceMgr::DiagHandlerTy DiagHandler = nullptr, | ||||||||||||
| 1428 | void *DiagHandlerCtxt = nullptr); | ||||||||||||
| 1429 | ~Input() override; | ||||||||||||
| 1430 | |||||||||||||
| 1431 | // Check if there was an syntax or semantic error during parsing. | ||||||||||||
| 1432 | std::error_code error(); | ||||||||||||
| 1433 | |||||||||||||
| 1434 | private: | ||||||||||||
| 1435 | bool outputting() const override; | ||||||||||||
| 1436 | bool mapTag(StringRef, bool) override; | ||||||||||||
| 1437 | void beginMapping() override; | ||||||||||||
| 1438 | void endMapping() override; | ||||||||||||
| 1439 | bool preflightKey(const char *, bool, bool, bool &, void *&) override; | ||||||||||||
| 1440 | void postflightKey(void *) override; | ||||||||||||
| 1441 | std::vector<StringRef> keys() override; | ||||||||||||
| 1442 | void beginFlowMapping() override; | ||||||||||||
| 1443 | void endFlowMapping() override; | ||||||||||||
| 1444 | unsigned beginSequence() override; | ||||||||||||
| 1445 | void endSequence() override; | ||||||||||||
| 1446 | bool preflightElement(unsigned index, void *&) override; | ||||||||||||
| 1447 | void postflightElement(void *) override; | ||||||||||||
| 1448 | unsigned beginFlowSequence() override; | ||||||||||||
| 1449 | bool preflightFlowElement(unsigned , void *&) override; | ||||||||||||
| 1450 | void postflightFlowElement(void *) override; | ||||||||||||
| 1451 | void endFlowSequence() override; | ||||||||||||
| 1452 | void beginEnumScalar() override; | ||||||||||||
| 1453 | bool matchEnumScalar(const char*, bool) override; | ||||||||||||
| 1454 | bool matchEnumFallback() override; | ||||||||||||
| 1455 | void endEnumScalar() override; | ||||||||||||
| 1456 | bool beginBitSetScalar(bool &) override; | ||||||||||||
| 1457 | bool bitSetMatch(const char *, bool ) override; | ||||||||||||
| 1458 | void endBitSetScalar() override; | ||||||||||||
| 1459 | void scalarString(StringRef &, QuotingType) override; | ||||||||||||
| 1460 | void blockScalarString(StringRef &) override; | ||||||||||||
| 1461 | void scalarTag(std::string &) override; | ||||||||||||
| 1462 | NodeKind getNodeKind() override; | ||||||||||||
| 1463 | void setError(const Twine &message) override; | ||||||||||||
| 1464 | bool canElideEmptySequence() override; | ||||||||||||
| 1465 | |||||||||||||
| 1466 | class HNode { | ||||||||||||
| 1467 | virtual void anchor(); | ||||||||||||
| 1468 | |||||||||||||
| 1469 | public: | ||||||||||||
| 1470 | HNode(Node *n) : _node(n) { } | ||||||||||||
| 1471 | virtual ~HNode() = default; | ||||||||||||
| 1472 | |||||||||||||
| 1473 | static bool classof(const HNode *) { return true; } | ||||||||||||
| 1474 | |||||||||||||
| 1475 | Node *_node; | ||||||||||||
| 1476 | }; | ||||||||||||
| 1477 | |||||||||||||
| 1478 | class EmptyHNode : public HNode { | ||||||||||||
| 1479 | void anchor() override; | ||||||||||||
| 1480 | |||||||||||||
| 1481 | public: | ||||||||||||
| 1482 | EmptyHNode(Node *n) : HNode(n) { } | ||||||||||||
| 1483 | |||||||||||||
| 1484 | static bool classof(const HNode *n) { return NullNode::classof(n->_node); } | ||||||||||||
| 1485 | |||||||||||||
| 1486 | static bool classof(const EmptyHNode *) { return true; } | ||||||||||||
| 1487 | }; | ||||||||||||
| 1488 | |||||||||||||
| 1489 | class ScalarHNode : public HNode { | ||||||||||||
| 1490 | void anchor() override; | ||||||||||||
| 1491 | |||||||||||||
| 1492 | public: | ||||||||||||
| 1493 | ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { } | ||||||||||||
| 1494 | |||||||||||||
| 1495 | StringRef value() const { return _value; } | ||||||||||||
| 1496 | |||||||||||||
| 1497 | static bool classof(const HNode *n) { | ||||||||||||
| 1498 | return ScalarNode::classof(n->_node) || | ||||||||||||
| 1499 | BlockScalarNode::classof(n->_node); | ||||||||||||
| 1500 | } | ||||||||||||
| 1501 | |||||||||||||
| 1502 | static bool classof(const ScalarHNode *) { return true; } | ||||||||||||
| 1503 | |||||||||||||
| 1504 | protected: | ||||||||||||
| 1505 | StringRef _value; | ||||||||||||
| 1506 | }; | ||||||||||||
| 1507 | |||||||||||||
| 1508 | class MapHNode : public HNode { | ||||||||||||
| 1509 | void anchor() override; | ||||||||||||
| 1510 | |||||||||||||
| 1511 | public: | ||||||||||||
| 1512 | MapHNode(Node *n) : HNode(n) { } | ||||||||||||
| 1513 | |||||||||||||
| 1514 | static bool classof(const HNode *n) { | ||||||||||||
| 1515 | return MappingNode::classof(n->_node); | ||||||||||||
| 1516 | } | ||||||||||||
| 1517 | |||||||||||||
| 1518 | static bool classof(const MapHNode *) { return true; } | ||||||||||||
| 1519 | |||||||||||||
| 1520 | using NameToNodeAndLoc = | ||||||||||||
| 1521 | StringMap<std::pair<std::unique_ptr<HNode>, SMRange>>; | ||||||||||||
| 1522 | |||||||||||||
| 1523 | NameToNodeAndLoc Mapping; | ||||||||||||
| 1524 | SmallVector<std::string, 6> ValidKeys; | ||||||||||||
| 1525 | }; | ||||||||||||
| 1526 | |||||||||||||
| 1527 | class SequenceHNode : public HNode { | ||||||||||||
| 1528 | void anchor() override; | ||||||||||||
| 1529 | |||||||||||||
| 1530 | public: | ||||||||||||
| 1531 | SequenceHNode(Node *n) : HNode(n) { } | ||||||||||||
| 1532 | |||||||||||||
| 1533 | static bool classof(const HNode *n) { | ||||||||||||
| 1534 | return SequenceNode::classof(n->_node); | ||||||||||||
| 1535 | } | ||||||||||||
| 1536 | |||||||||||||
| 1537 | static bool classof(const SequenceHNode *) { return true; } | ||||||||||||
| 1538 | |||||||||||||
| 1539 | std::vector<std::unique_ptr<HNode>> Entries; | ||||||||||||
| 1540 | }; | ||||||||||||
| 1541 | |||||||||||||
| 1542 | std::unique_ptr<Input::HNode> createHNodes(Node *node); | ||||||||||||
| 1543 | void setError(HNode *hnode, const Twine &message); | ||||||||||||
| 1544 | void setError(Node *node, const Twine &message); | ||||||||||||
| 1545 | void setError(const SMRange &Range, const Twine &message); | ||||||||||||
| 1546 | |||||||||||||
| 1547 | void reportWarning(HNode *hnode, const Twine &message); | ||||||||||||
| 1548 | void reportWarning(Node *hnode, const Twine &message); | ||||||||||||
| 1549 | void reportWarning(const SMRange &Range, const Twine &message); | ||||||||||||
| 1550 | |||||||||||||
| 1551 | public: | ||||||||||||
| 1552 | // These are only used by operator>>. They could be private | ||||||||||||
| 1553 | // if those templated things could be made friends. | ||||||||||||
| 1554 | bool setCurrentDocument(); | ||||||||||||
| 1555 | bool nextDocument(); | ||||||||||||
| 1556 | |||||||||||||
| 1557 | /// Returns the current node that's being parsed by the YAML Parser. | ||||||||||||
| 1558 | const Node *getCurrentNode() const; | ||||||||||||
| 1559 | |||||||||||||
| 1560 | void setAllowUnknownKeys(bool Allow) override; | ||||||||||||
| 1561 | |||||||||||||
| 1562 | private: | ||||||||||||
| 1563 | SourceMgr SrcMgr; // must be before Strm | ||||||||||||
| 1564 | std::unique_ptr<llvm::yaml::Stream> Strm; | ||||||||||||
| 1565 | std::unique_ptr<HNode> TopNode; | ||||||||||||
| 1566 | std::error_code EC; | ||||||||||||
| 1567 | BumpPtrAllocator StringAllocator; | ||||||||||||
| 1568 | document_iterator DocIterator; | ||||||||||||
| 1569 | llvm::BitVector BitValuesUsed; | ||||||||||||
| 1570 | HNode *CurrentNode = nullptr; | ||||||||||||
| 1571 | bool ScalarMatchFound = false; | ||||||||||||
| 1572 | bool AllowUnknownKeys = false; | ||||||||||||
| 1573 | }; | ||||||||||||
| 1574 | |||||||||||||
| 1575 | /// | ||||||||||||
| 1576 | /// The Output class is used to generate a yaml document from in-memory structs | ||||||||||||
| 1577 | /// and vectors. | ||||||||||||
| 1578 | /// | ||||||||||||
| 1579 | class Output : public IO { | ||||||||||||
| 1580 | public: | ||||||||||||
| 1581 | Output(raw_ostream &, void *Ctxt = nullptr, int WrapColumn = 70); | ||||||||||||
| 1582 | ~Output() override; | ||||||||||||
| 1583 | |||||||||||||
| 1584 | /// Set whether or not to output optional values which are equal | ||||||||||||
| 1585 | /// to the default value. By default, when outputting if you attempt | ||||||||||||
| 1586 | /// to write a value that is equal to the default, the value gets ignored. | ||||||||||||
| 1587 | /// Sometimes, it is useful to be able to see these in the resulting YAML | ||||||||||||
| 1588 | /// anyway. | ||||||||||||
| 1589 | void setWriteDefaultValues(bool Write) { WriteDefaultValues = Write; } | ||||||||||||
| 1590 | |||||||||||||
| 1591 | bool outputting() const override; | ||||||||||||
| 1592 | bool mapTag(StringRef, bool) override; | ||||||||||||
| 1593 | void beginMapping() override; | ||||||||||||
| 1594 | void endMapping() override; | ||||||||||||
| 1595 | bool preflightKey(const char *key, bool, bool, bool &, void *&) override; | ||||||||||||
| 1596 | void postflightKey(void *) override; | ||||||||||||
| 1597 | std::vector<StringRef> keys() override; | ||||||||||||
| 1598 | void beginFlowMapping() override; | ||||||||||||
| 1599 | void endFlowMapping() override; | ||||||||||||
| 1600 | unsigned beginSequence() override; | ||||||||||||
| 1601 | void endSequence() override; | ||||||||||||
| 1602 | bool preflightElement(unsigned, void *&) override; | ||||||||||||
| 1603 | void postflightElement(void *) override; | ||||||||||||
| 1604 | unsigned beginFlowSequence() override; | ||||||||||||
| 1605 | bool preflightFlowElement(unsigned, void *&) override; | ||||||||||||
| 1606 | void postflightFlowElement(void *) override; | ||||||||||||
| 1607 | void endFlowSequence() override; | ||||||||||||
| 1608 | void beginEnumScalar() override; | ||||||||||||
| 1609 | bool matchEnumScalar(const char*, bool) override; | ||||||||||||
| 1610 | bool matchEnumFallback() override; | ||||||||||||
| 1611 | void endEnumScalar() override; | ||||||||||||
| 1612 | bool beginBitSetScalar(bool &) override; | ||||||||||||
| 1613 | bool bitSetMatch(const char *, bool ) override; | ||||||||||||
| 1614 | void endBitSetScalar() override; | ||||||||||||
| 1615 | void scalarString(StringRef &, QuotingType) override; | ||||||||||||
| 1616 | void blockScalarString(StringRef &) override; | ||||||||||||
| 1617 | void scalarTag(std::string &) override; | ||||||||||||
| 1618 | NodeKind getNodeKind() override; | ||||||||||||
| 1619 | void setError(const Twine &message) override; | ||||||||||||
| 1620 | bool canElideEmptySequence() override; | ||||||||||||
| 1621 | |||||||||||||
| 1622 | // These are only used by operator<<. They could be private | ||||||||||||
| 1623 | // if that templated operator could be made a friend. | ||||||||||||
| 1624 | void beginDocuments(); | ||||||||||||
| 1625 | bool preflightDocument(unsigned); | ||||||||||||
| 1626 | void postflightDocument(); | ||||||||||||
| 1627 | void endDocuments(); | ||||||||||||
| 1628 | |||||||||||||
| 1629 | private: | ||||||||||||
| 1630 | void output(StringRef s); | ||||||||||||
| 1631 | void outputUpToEndOfLine(StringRef s); | ||||||||||||
| 1632 | void newLineCheck(bool EmptySequence = false); | ||||||||||||
| 1633 | void outputNewLine(); | ||||||||||||
| 1634 | void paddedKey(StringRef key); | ||||||||||||
| 1635 | void flowKey(StringRef Key); | ||||||||||||
| 1636 | |||||||||||||
| 1637 | enum InState { | ||||||||||||
| 1638 | inSeqFirstElement, | ||||||||||||
| 1639 | inSeqOtherElement, | ||||||||||||
| 1640 | inFlowSeqFirstElement, | ||||||||||||
| 1641 | inFlowSeqOtherElement, | ||||||||||||
| 1642 | inMapFirstKey, | ||||||||||||
| 1643 | inMapOtherKey, | ||||||||||||
| 1644 | inFlowMapFirstKey, | ||||||||||||
| 1645 | inFlowMapOtherKey | ||||||||||||
| 1646 | }; | ||||||||||||
| 1647 | |||||||||||||
| 1648 | static bool inSeqAnyElement(InState State); | ||||||||||||
| 1649 | static bool inFlowSeqAnyElement(InState State); | ||||||||||||
| 1650 | static bool inMapAnyKey(InState State); | ||||||||||||
| 1651 | static bool inFlowMapAnyKey(InState State); | ||||||||||||
| 1652 | |||||||||||||
| 1653 | raw_ostream &Out; | ||||||||||||
| 1654 | int WrapColumn; | ||||||||||||
| 1655 | SmallVector<InState, 8> StateStack; | ||||||||||||
| 1656 | int Column = 0; | ||||||||||||
| 1657 | int ColumnAtFlowStart = 0; | ||||||||||||
| 1658 | int ColumnAtMapFlowStart = 0; | ||||||||||||
| 1659 | bool NeedBitValueComma = false; | ||||||||||||
| 1660 | bool NeedFlowSequenceComma = false; | ||||||||||||
| 1661 | bool EnumerationMatchFound = false; | ||||||||||||
| 1662 | bool WriteDefaultValues = false; | ||||||||||||
| 1663 | StringRef Padding; | ||||||||||||
| 1664 | StringRef PaddingBeforeContainer; | ||||||||||||
| 1665 | }; | ||||||||||||
| 1666 | |||||||||||||
| 1667 | template <typename T, typename Context> | ||||||||||||
| 1668 | void IO::processKeyWithDefault(const char *Key, std::optional<T> &Val, | ||||||||||||
| 1669 | const std::optional<T> &DefaultValue, | ||||||||||||
| 1670 | bool Required, Context &Ctx) { | ||||||||||||
| 1671 | assert(!DefaultValue && "std::optional<T> shouldn't have a value!")(static_cast <bool> (!DefaultValue && "std::optional<T> shouldn't have a value!" ) ? void (0) : __assert_fail ("!DefaultValue && \"std::optional<T> shouldn't have a value!\"" , "llvm/include/llvm/Support/YAMLTraits.h", 1671, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
| 1672 | void *SaveInfo; | ||||||||||||
| 1673 | bool UseDefault = true; | ||||||||||||
| 1674 | const bool sameAsDefault = outputting() && !Val; | ||||||||||||
| 1675 | if (!outputting() && !Val) | ||||||||||||
| 1676 | Val = T(); | ||||||||||||
| 1677 | if (Val && | ||||||||||||
| 1678 | this->preflightKey(Key, Required, sameAsDefault, UseDefault, SaveInfo)) { | ||||||||||||
| 1679 | |||||||||||||
| 1680 | // When reading an std::optional<X> key from a YAML description, we allow | ||||||||||||
| 1681 | // the special "<none>" value, which can be used to specify that no value | ||||||||||||
| 1682 | // was requested, i.e. the DefaultValue will be assigned. The DefaultValue | ||||||||||||
| 1683 | // is usually None. | ||||||||||||
| 1684 | bool IsNone = false; | ||||||||||||
| 1685 | if (!outputting()) | ||||||||||||
| 1686 | if (const auto *Node = | ||||||||||||
| 1687 | dyn_cast<ScalarNode>(((Input *)this)->getCurrentNode())) | ||||||||||||
| 1688 | // We use rtrim to ignore possible white spaces that might exist when a | ||||||||||||
| 1689 | // comment is present on the same line. | ||||||||||||
| 1690 | IsNone = Node->getRawValue().rtrim(' ') == "<none>"; | ||||||||||||
| 1691 | |||||||||||||
| 1692 | if (IsNone) | ||||||||||||
| 1693 | Val = DefaultValue; | ||||||||||||
| 1694 | else | ||||||||||||
| 1695 | yamlize(*this, *Val, Required, Ctx); | ||||||||||||
| 1696 | this->postflightKey(SaveInfo); | ||||||||||||
| 1697 | } else { | ||||||||||||
| 1698 | if (UseDefault) | ||||||||||||
| 1699 | Val = DefaultValue; | ||||||||||||
| 1700 | } | ||||||||||||
| 1701 | } | ||||||||||||
| 1702 | |||||||||||||
| 1703 | /// YAML I/O does conversion based on types. But often native data types | ||||||||||||
| 1704 | /// are just a typedef of built in intergral types (e.g. int). But the C++ | ||||||||||||
| 1705 | /// type matching system sees through the typedef and all the typedefed types | ||||||||||||
| 1706 | /// look like a built in type. This will cause the generic YAML I/O conversion | ||||||||||||
| 1707 | /// to be used. To provide better control over the YAML conversion, you can | ||||||||||||
| 1708 | /// use this macro instead of typedef. It will create a class with one field | ||||||||||||
| 1709 | /// and automatic conversion operators to and from the base type. | ||||||||||||
| 1710 | /// Based on BOOST_STRONG_TYPEDEF | ||||||||||||
| 1711 | #define LLVM_YAML_STRONG_TYPEDEF(_base, _type)struct _type { _type() = default; _type(const _base v) : value (v) {} _type(const _type &v) = default; _type &operator =(const _type &rhs) = default; _type &operator=(const _base &rhs) { value = rhs; return *this; } operator const _base & () const { return value; } bool operator==(const _type &rhs) const { return value == rhs.value; } bool operator ==(const _base &rhs) const { return value == rhs; } bool operator <(const _type &rhs) const { return value < rhs.value ; } _base value; using BaseType = _base; }; \ | ||||||||||||
| 1712 | struct _type { \ | ||||||||||||
| 1713 | _type() = default; \ | ||||||||||||
| 1714 | _type(const _base v) : value(v) {} \ | ||||||||||||
| 1715 | _type(const _type &v) = default; \ | ||||||||||||
| 1716 | _type &operator=(const _type &rhs) = default; \ | ||||||||||||
| 1717 | _type &operator=(const _base &rhs) { value = rhs; return *this; } \ | ||||||||||||
| 1718 | operator const _base & () const { return value; } \ | ||||||||||||
| 1719 | bool operator==(const _type &rhs) const { return value == rhs.value; } \ | ||||||||||||
| 1720 | bool operator==(const _base &rhs) const { return value == rhs; } \ | ||||||||||||
| 1721 | bool operator<(const _type &rhs) const { return value < rhs.value; } \ | ||||||||||||
| 1722 | _base value; \ | ||||||||||||
| 1723 | using BaseType = _base; \ | ||||||||||||
| 1724 | }; | ||||||||||||
| 1725 | |||||||||||||
| 1726 | /// | ||||||||||||
| 1727 | /// Use these types instead of uintXX_t in any mapping to have | ||||||||||||
| 1728 | /// its yaml output formatted as hexadecimal. | ||||||||||||
| 1729 | /// | ||||||||||||
| 1730 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)struct Hex8 { Hex8() = default; Hex8(const uint8_t v) : value (v) {} Hex8(const Hex8 &v) = default; Hex8 &operator= (const Hex8 &rhs) = default; Hex8 &operator=(const uint8_t &rhs) { value = rhs; return *this; } operator const uint8_t & () const { return value; } bool operator==(const Hex8 & rhs) const { return value == rhs.value; } bool operator==(const uint8_t &rhs) const { return value == rhs; } bool operator <(const Hex8 &rhs) const { return value < rhs.value ; } uint8_t value; using BaseType = uint8_t; }; | ||||||||||||
| 1731 | LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)struct Hex16 { Hex16() = default; Hex16(const uint16_t v) : value (v) {} Hex16(const Hex16 &v) = default; Hex16 &operator =(const Hex16 &rhs) = default; Hex16 &operator=(const uint16_t &rhs) { value = rhs; return *this; } operator const uint16_t & () const { return value; } bool operator==(const Hex16 &rhs) const { return value == rhs.value; } bool operator ==(const uint16_t &rhs) const { return value == rhs; } bool operator<(const Hex16 &rhs) const { return value < rhs.value; } uint16_t value; using BaseType = uint16_t; }; | ||||||||||||
| 1732 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)struct Hex32 { Hex32() = default; Hex32(const uint32_t v) : value (v) {} Hex32(const Hex32 &v) = default; Hex32 &operator =(const Hex32 &rhs) = default; Hex32 &operator=(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator==(const Hex32 &rhs) const { return value == rhs.value; } bool operator ==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const Hex32 &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; | ||||||||||||
| 1733 | LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)struct Hex64 { Hex64() = default; Hex64(const uint64_t v) : value (v) {} Hex64(const Hex64 &v) = default; Hex64 &operator =(const Hex64 &rhs) = default; Hex64 &operator=(const uint64_t &rhs) { value = rhs; return *this; } operator const uint64_t & () const { return value; } bool operator==(const Hex64 &rhs) const { return value == rhs.value; } bool operator ==(const uint64_t &rhs) const { return value == rhs; } bool operator<(const Hex64 &rhs) const { return value < rhs.value; } uint64_t value; using BaseType = uint64_t; }; | ||||||||||||
| 1734 | |||||||||||||
| 1735 | template<> | ||||||||||||
| 1736 | struct ScalarTraits<Hex8> { | ||||||||||||
| 1737 | static void output(const Hex8 &, void *, raw_ostream &); | ||||||||||||
| 1738 | static StringRef input(StringRef, void *, Hex8 &); | ||||||||||||
| 1739 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1740 | }; | ||||||||||||
| 1741 | |||||||||||||
| 1742 | template<> | ||||||||||||
| 1743 | struct ScalarTraits<Hex16> { | ||||||||||||
| 1744 | static void output(const Hex16 &, void *, raw_ostream &); | ||||||||||||
| 1745 | static StringRef input(StringRef, void *, Hex16 &); | ||||||||||||
| 1746 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1747 | }; | ||||||||||||
| 1748 | |||||||||||||
| 1749 | template<> | ||||||||||||
| 1750 | struct ScalarTraits<Hex32> { | ||||||||||||
| 1751 | static void output(const Hex32 &, void *, raw_ostream &); | ||||||||||||
| 1752 | static StringRef input(StringRef, void *, Hex32 &); | ||||||||||||
| 1753 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1754 | }; | ||||||||||||
| 1755 | |||||||||||||
| 1756 | template<> | ||||||||||||
| 1757 | struct ScalarTraits<Hex64> { | ||||||||||||
| 1758 | static void output(const Hex64 &, void *, raw_ostream &); | ||||||||||||
| 1759 | static StringRef input(StringRef, void *, Hex64 &); | ||||||||||||
| 1760 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1761 | }; | ||||||||||||
| 1762 | |||||||||||||
| 1763 | template <> struct ScalarTraits<VersionTuple> { | ||||||||||||
| 1764 | static void output(const VersionTuple &Value, void *, llvm::raw_ostream &Out); | ||||||||||||
| 1765 | static StringRef input(StringRef, void *, VersionTuple &); | ||||||||||||
| 1766 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | ||||||||||||
| 1767 | }; | ||||||||||||
| 1768 | |||||||||||||
| 1769 | // Define non-member operator>> so that Input can stream in a document list. | ||||||||||||
| 1770 | template <typename T> | ||||||||||||
| 1771 | inline std::enable_if_t<has_DocumentListTraits<T>::value, Input &> | ||||||||||||
| 1772 | operator>>(Input &yin, T &docList) { | ||||||||||||
| 1773 | int i = 0; | ||||||||||||
| 1774 | EmptyContext Ctx; | ||||||||||||
| 1775 | while ( yin.setCurrentDocument() ) { | ||||||||||||
| 1776 | yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true, Ctx); | ||||||||||||
| 1777 | if ( yin.error() ) | ||||||||||||
| 1778 | return yin; | ||||||||||||
| 1779 | yin.nextDocument(); | ||||||||||||
| 1780 | ++i; | ||||||||||||
| 1781 | } | ||||||||||||
| 1782 | return yin; | ||||||||||||
| 1783 | } | ||||||||||||
| 1784 | |||||||||||||
| 1785 | // Define non-member operator>> so that Input can stream in a map as a document. | ||||||||||||
| 1786 | template <typename T> | ||||||||||||
| 1787 | inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Input &> | ||||||||||||
| 1788 | operator>>(Input &yin, T &docMap) { | ||||||||||||
| 1789 | EmptyContext Ctx; | ||||||||||||
| 1790 | yin.setCurrentDocument(); | ||||||||||||
| 1791 | yamlize(yin, docMap, true, Ctx); | ||||||||||||
| 1792 | return yin; | ||||||||||||
| 1793 | } | ||||||||||||
| 1794 | |||||||||||||
| 1795 | // Define non-member operator>> so that Input can stream in a sequence as | ||||||||||||
| 1796 | // a document. | ||||||||||||
| 1797 | template <typename T> | ||||||||||||
| 1798 | inline std::enable_if_t<has_SequenceTraits<T>::value, Input &> | ||||||||||||
| 1799 | operator>>(Input &yin, T &docSeq) { | ||||||||||||
| 1800 | EmptyContext Ctx; | ||||||||||||
| 1801 | if (yin.setCurrentDocument()) | ||||||||||||
| 1802 | yamlize(yin, docSeq, true, Ctx); | ||||||||||||
| 1803 | return yin; | ||||||||||||
| 1804 | } | ||||||||||||
| 1805 | |||||||||||||
| 1806 | // Define non-member operator>> so that Input can stream in a block scalar. | ||||||||||||
| 1807 | template <typename T> | ||||||||||||
| 1808 | inline std::enable_if_t<has_BlockScalarTraits<T>::value, Input &> | ||||||||||||
| 1809 | operator>>(Input &In, T &Val) { | ||||||||||||
| 1810 | EmptyContext Ctx; | ||||||||||||
| 1811 | if (In.setCurrentDocument()) | ||||||||||||
| 1812 | yamlize(In, Val, true, Ctx); | ||||||||||||
| 1813 | return In; | ||||||||||||
| 1814 | } | ||||||||||||
| 1815 | |||||||||||||
| 1816 | // Define non-member operator>> so that Input can stream in a string map. | ||||||||||||
| 1817 | template <typename T> | ||||||||||||
| 1818 | inline std::enable_if_t<has_CustomMappingTraits<T>::value, Input &> | ||||||||||||
| 1819 | operator>>(Input &In, T &Val) { | ||||||||||||
| 1820 | EmptyContext Ctx; | ||||||||||||
| 1821 | if (In.setCurrentDocument()) | ||||||||||||
| 1822 | yamlize(In, Val, true, Ctx); | ||||||||||||
| 1823 | return In; | ||||||||||||
| 1824 | } | ||||||||||||
| 1825 | |||||||||||||
| 1826 | // Define non-member operator>> so that Input can stream in a polymorphic type. | ||||||||||||
| 1827 | template <typename T> | ||||||||||||
| 1828 | inline std::enable_if_t<has_PolymorphicTraits<T>::value, Input &> | ||||||||||||
| 1829 | operator>>(Input &In, T &Val) { | ||||||||||||
| 1830 | EmptyContext Ctx; | ||||||||||||
| 1831 | if (In.setCurrentDocument()) | ||||||||||||
| 1832 | yamlize(In, Val, true, Ctx); | ||||||||||||
| 1833 | return In; | ||||||||||||
| 1834 | } | ||||||||||||
| 1835 | |||||||||||||
| 1836 | // Provide better error message about types missing a trait specialization | ||||||||||||
| 1837 | template <typename T> | ||||||||||||
| 1838 | inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Input &> | ||||||||||||
| 1839 | operator>>(Input &yin, T &docSeq) { | ||||||||||||
| 1840 | char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)]; | ||||||||||||
| 1841 | return yin; | ||||||||||||
| 1842 | } | ||||||||||||
| 1843 | |||||||||||||
| 1844 | // Define non-member operator<< so that Output can stream out document list. | ||||||||||||
| 1845 | template <typename T> | ||||||||||||
| 1846 | inline std::enable_if_t<has_DocumentListTraits<T>::value, Output &> | ||||||||||||
| 1847 | operator<<(Output &yout, T &docList) { | ||||||||||||
| 1848 | EmptyContext Ctx; | ||||||||||||
| 1849 | yout.beginDocuments(); | ||||||||||||
| 1850 | const size_t count = DocumentListTraits<T>::size(yout, docList); | ||||||||||||
| 1851 | for(size_t i=0; i < count; ++i) { | ||||||||||||
| 1852 | if ( yout.preflightDocument(i) ) { | ||||||||||||
| 1853 | yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true, | ||||||||||||
| 1854 | Ctx); | ||||||||||||
| 1855 | yout.postflightDocument(); | ||||||||||||
| 1856 | } | ||||||||||||
| 1857 | } | ||||||||||||
| 1858 | yout.endDocuments(); | ||||||||||||
| 1859 | return yout; | ||||||||||||
| 1860 | } | ||||||||||||
| 1861 | |||||||||||||
| 1862 | // Define non-member operator<< so that Output can stream out a map. | ||||||||||||
| 1863 | template <typename T> | ||||||||||||
| 1864 | inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Output &> | ||||||||||||
| 1865 | operator<<(Output &yout, T &map) { | ||||||||||||
| 1866 | EmptyContext Ctx; | ||||||||||||
| 1867 | yout.beginDocuments(); | ||||||||||||
| 1868 | if ( yout.preflightDocument(0) ) { | ||||||||||||
| 1869 | yamlize(yout, map, true, Ctx); | ||||||||||||
| 1870 | yout.postflightDocument(); | ||||||||||||
| 1871 | } | ||||||||||||
| 1872 | yout.endDocuments(); | ||||||||||||
| 1873 | return yout; | ||||||||||||
| 1874 | } | ||||||||||||
| 1875 | |||||||||||||
| 1876 | // Define non-member operator<< so that Output can stream out a sequence. | ||||||||||||
| 1877 | template <typename T> | ||||||||||||
| 1878 | inline std::enable_if_t<has_SequenceTraits<T>::value, Output &> | ||||||||||||
| 1879 | operator<<(Output &yout, T &seq) { | ||||||||||||
| 1880 | EmptyContext Ctx; | ||||||||||||
| 1881 | yout.beginDocuments(); | ||||||||||||
| 1882 | if ( yout.preflightDocument(0) ) { | ||||||||||||
| 1883 | yamlize(yout, seq, true, Ctx); | ||||||||||||
| 1884 | yout.postflightDocument(); | ||||||||||||
| 1885 | } | ||||||||||||
| 1886 | yout.endDocuments(); | ||||||||||||
| 1887 | return yout; | ||||||||||||
| 1888 | } | ||||||||||||
| 1889 | |||||||||||||
| 1890 | // Define non-member operator<< so that Output can stream out a block scalar. | ||||||||||||
| 1891 | template <typename T> | ||||||||||||
| 1892 | inline std::enable_if_t<has_BlockScalarTraits<T>::value, Output &> | ||||||||||||
| 1893 | operator<<(Output &Out, T &Val) { | ||||||||||||
| 1894 | EmptyContext Ctx; | ||||||||||||
| 1895 | Out.beginDocuments(); | ||||||||||||
| 1896 | if (Out.preflightDocument(0)) { | ||||||||||||
| 1897 | yamlize(Out, Val, true, Ctx); | ||||||||||||
| 1898 | Out.postflightDocument(); | ||||||||||||
| 1899 | } | ||||||||||||
| 1900 | Out.endDocuments(); | ||||||||||||
| 1901 | return Out; | ||||||||||||
| 1902 | } | ||||||||||||
| 1903 | |||||||||||||
| 1904 | // Define non-member operator<< so that Output can stream out a string map. | ||||||||||||
| 1905 | template <typename T> | ||||||||||||
| 1906 | inline std::enable_if_t<has_CustomMappingTraits<T>::value, Output &> | ||||||||||||
| 1907 | operator<<(Output &Out, T &Val) { | ||||||||||||
| 1908 | EmptyContext Ctx; | ||||||||||||
| 1909 | Out.beginDocuments(); | ||||||||||||
| 1910 | if (Out.preflightDocument(0)) { | ||||||||||||
| 1911 | yamlize(Out, Val, true, Ctx); | ||||||||||||
| 1912 | Out.postflightDocument(); | ||||||||||||
| 1913 | } | ||||||||||||
| 1914 | Out.endDocuments(); | ||||||||||||
| 1915 | return Out; | ||||||||||||
| 1916 | } | ||||||||||||
| 1917 | |||||||||||||
| 1918 | // Define non-member operator<< so that Output can stream out a polymorphic | ||||||||||||
| 1919 | // type. | ||||||||||||
| 1920 | template <typename T> | ||||||||||||
| 1921 | inline std::enable_if_t<has_PolymorphicTraits<T>::value, Output &> | ||||||||||||
| 1922 | operator<<(Output &Out, T &Val) { | ||||||||||||
| 1923 | EmptyContext Ctx; | ||||||||||||
| 1924 | Out.beginDocuments(); | ||||||||||||
| 1925 | if (Out.preflightDocument(0)) { | ||||||||||||
| 1926 | // FIXME: The parser does not support explicit documents terminated with a | ||||||||||||
| 1927 | // plain scalar; the end-marker is included as part of the scalar token. | ||||||||||||
| 1928 | assert(PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar && "plain scalar documents are not supported")(static_cast <bool> (PolymorphicTraits<T>::getKind (Val) != NodeKind::Scalar && "plain scalar documents are not supported" ) ? void (0) : __assert_fail ("PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar && \"plain scalar documents are not supported\"" , "llvm/include/llvm/Support/YAMLTraits.h", 1928, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
| 1929 | yamlize(Out, Val, true, Ctx); | ||||||||||||
| 1930 | Out.postflightDocument(); | ||||||||||||
| 1931 | } | ||||||||||||
| 1932 | Out.endDocuments(); | ||||||||||||
| 1933 | return Out; | ||||||||||||
| 1934 | } | ||||||||||||
| 1935 | |||||||||||||
| 1936 | // Provide better error message about types missing a trait specialization | ||||||||||||
| 1937 | template <typename T> | ||||||||||||
| 1938 | inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Output &> | ||||||||||||
| 1939 | operator<<(Output &yout, T &seq) { | ||||||||||||
| 1940 | char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)]; | ||||||||||||
| 1941 | return yout; | ||||||||||||
| 1942 | } | ||||||||||||
| 1943 | |||||||||||||
| 1944 | template <bool B> struct IsFlowSequenceBase {}; | ||||||||||||
| 1945 | template <> struct IsFlowSequenceBase<true> { static const bool flow = true; }; | ||||||||||||
| 1946 | |||||||||||||
| 1947 | template <typename T, typename U = void> | ||||||||||||
| 1948 | struct IsResizable : std::false_type {}; | ||||||||||||
| 1949 | |||||||||||||
| 1950 | template <typename T> | ||||||||||||
| 1951 | struct IsResizable<T, std::void_t<decltype(std::declval<T>().resize(0))>> | ||||||||||||
| 1952 | : public std::true_type {}; | ||||||||||||
| 1953 | |||||||||||||
| 1954 | template <typename T, bool B> struct IsResizableBase { | ||||||||||||
| 1955 | using type = typename T::value_type; | ||||||||||||
| 1956 | |||||||||||||
| 1957 | static type &element(IO &io, T &seq, size_t index) { | ||||||||||||
| 1958 | if (index >= seq.size()) | ||||||||||||
| 1959 | seq.resize(index + 1); | ||||||||||||
| 1960 | return seq[index]; | ||||||||||||
| 1961 | } | ||||||||||||
| 1962 | }; | ||||||||||||
| 1963 | |||||||||||||
| 1964 | template <typename T> struct IsResizableBase<T, false> { | ||||||||||||
| 1965 | using type = typename T::value_type; | ||||||||||||
| 1966 | |||||||||||||
| 1967 | static type &element(IO &io, T &seq, size_t index) { | ||||||||||||
| 1968 | if (index >= seq.size()) { | ||||||||||||
| 1969 | io.setError(Twine("value sequence extends beyond static size (") + | ||||||||||||
| 1970 | Twine(seq.size()) + ")"); | ||||||||||||
| 1971 | return seq[0]; | ||||||||||||
| 1972 | } | ||||||||||||
| 1973 | return seq[index]; | ||||||||||||
| 1974 | } | ||||||||||||
| 1975 | }; | ||||||||||||
| 1976 | |||||||||||||
| 1977 | template <typename T, bool Flow> | ||||||||||||
| 1978 | struct SequenceTraitsImpl | ||||||||||||
| 1979 | : IsFlowSequenceBase<Flow>, IsResizableBase<T, IsResizable<T>::value> { | ||||||||||||
| 1980 | static size_t size(IO &io, T &seq) { return seq.size(); } | ||||||||||||
| 1981 | }; | ||||||||||||
| 1982 | |||||||||||||
| 1983 | // Simple helper to check an expression can be used as a bool-valued template | ||||||||||||
| 1984 | // argument. | ||||||||||||
| 1985 | template <bool> struct CheckIsBool { static const bool value = true; }; | ||||||||||||
| 1986 | |||||||||||||
| 1987 | // If T has SequenceElementTraits, then vector<T> and SmallVector<T, N> have | ||||||||||||
| 1988 | // SequenceTraits that do the obvious thing. | ||||||||||||
| 1989 | template <typename T> | ||||||||||||
| 1990 | struct SequenceTraits< | ||||||||||||
| 1991 | std::vector<T>, | ||||||||||||
| 1992 | std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>> | ||||||||||||
| 1993 | : SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {}; | ||||||||||||
| 1994 | template <typename T, unsigned N> | ||||||||||||
| 1995 | struct SequenceTraits< | ||||||||||||
| 1996 | SmallVector<T, N>, | ||||||||||||
| 1997 | std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>> | ||||||||||||
| 1998 | : SequenceTraitsImpl<SmallVector<T, N>, SequenceElementTraits<T>::flow> {}; | ||||||||||||
| 1999 | template <typename T> | ||||||||||||
| 2000 | struct SequenceTraits< | ||||||||||||
| 2001 | SmallVectorImpl<T>, | ||||||||||||
| 2002 | std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>> | ||||||||||||
| 2003 | : SequenceTraitsImpl<SmallVectorImpl<T>, SequenceElementTraits<T>::flow> {}; | ||||||||||||
| 2004 | template <typename T> | ||||||||||||
| 2005 | struct SequenceTraits< | ||||||||||||
| 2006 | MutableArrayRef<T>, | ||||||||||||
| 2007 | std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>> | ||||||||||||
| 2008 | : SequenceTraitsImpl<MutableArrayRef<T>, SequenceElementTraits<T>::flow> {}; | ||||||||||||
| 2009 | |||||||||||||
| 2010 | // Sequences of fundamental types use flow formatting. | ||||||||||||
| 2011 | template <typename T> | ||||||||||||
| 2012 | struct SequenceElementTraits<T, std::enable_if_t<std::is_fundamental_v<T>>> { | ||||||||||||
| 2013 | static const bool flow = true; | ||||||||||||
| 2014 | }; | ||||||||||||
| 2015 | |||||||||||||
| 2016 | // Sequences of strings use block formatting. | ||||||||||||
| 2017 | template<> struct SequenceElementTraits<std::string> { | ||||||||||||
| 2018 | static const bool flow = false; | ||||||||||||
| 2019 | }; | ||||||||||||
| 2020 | template<> struct SequenceElementTraits<StringRef> { | ||||||||||||
| 2021 | static const bool flow = false; | ||||||||||||
| 2022 | }; | ||||||||||||
| 2023 | template<> struct SequenceElementTraits<std::pair<std::string, std::string>> { | ||||||||||||
| 2024 | static const bool flow = false; | ||||||||||||
| 2025 | }; | ||||||||||||
| 2026 | |||||||||||||
| 2027 | /// Implementation of CustomMappingTraits for std::map<std::string, T>. | ||||||||||||
| 2028 | template <typename T> struct StdMapStringCustomMappingTraitsImpl { | ||||||||||||
| 2029 | using map_type = std::map<std::string, T>; | ||||||||||||
| 2030 | |||||||||||||
| 2031 | static void inputOne(IO &io, StringRef key, map_type &v) { | ||||||||||||
| 2032 | io.mapRequired(key.str().c_str(), v[std::string(key)]); | ||||||||||||
| 2033 | } | ||||||||||||
| 2034 | |||||||||||||
| 2035 | static void output(IO &io, map_type &v) { | ||||||||||||
| 2036 | for (auto &p : v) | ||||||||||||
| 2037 | io.mapRequired(p.first.c_str(), p.second); | ||||||||||||
| 2038 | } | ||||||||||||
| 2039 | }; | ||||||||||||
| 2040 | |||||||||||||
| 2041 | } // end namespace yaml | ||||||||||||
| 2042 | } // end namespace llvm | ||||||||||||
| 2043 | |||||||||||||
| 2044 | #define LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(TYPE, FLOW)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <TYPE> && !std::is_same_v<TYPE, std::string> && !std::is_same_v<TYPE, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<TYPE> { static const bool flow = FLOW; }; } } \ | ||||||||||||
| 2045 | namespace llvm { \ | ||||||||||||
| 2046 | namespace yaml { \ | ||||||||||||
| 2047 | static_assert( \ | ||||||||||||
| 2048 | !std::is_fundamental_v<TYPE> && !std::is_same_v<TYPE, std::string> && \ | ||||||||||||
| 2049 | !std::is_same_v<TYPE, llvm::StringRef>, \ | ||||||||||||
| 2050 | "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"); \ | ||||||||||||
| 2051 | template <> struct SequenceElementTraits<TYPE> { \ | ||||||||||||
| 2052 | static const bool flow = FLOW; \ | ||||||||||||
| 2053 | }; \ | ||||||||||||
| 2054 | } \ | ||||||||||||
| 2055 | } | ||||||||||||
| 2056 | |||||||||||||
| 2057 | /// Utility for declaring that a std::vector of a particular type | ||||||||||||
| 2058 | /// should be considered a YAML sequence. | ||||||||||||
| 2059 | #define LLVM_YAML_IS_SEQUENCE_VECTOR(type)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <type> && !std::is_same_v<type, std::string> && !std::is_same_v<type, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<type> { static const bool flow = false; }; } } \ | ||||||||||||
| 2060 | LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, false)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <type> && !std::is_same_v<type, std::string> && !std::is_same_v<type, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<type> { static const bool flow = false; }; } } | ||||||||||||
| 2061 | |||||||||||||
| 2062 | /// Utility for declaring that a std::vector of a particular type | ||||||||||||
| 2063 | /// should be considered a YAML flow sequence. | ||||||||||||
| 2064 | #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(type)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <type> && !std::is_same_v<type, std::string> && !std::is_same_v<type, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<type> { static const bool flow = true; }; } } \ | ||||||||||||
| 2065 | LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, true)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <type> && !std::is_same_v<type, std::string> && !std::is_same_v<type, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<type> { static const bool flow = true; }; } } | ||||||||||||
| 2066 | |||||||||||||
| 2067 | #define LLVM_YAML_DECLARE_MAPPING_TRAITS(Type)namespace llvm { namespace yaml { template <> struct MappingTraits <Type> { static void mapping(IO &IO, Type &Obj) ; }; } } \ | ||||||||||||
| 2068 | namespace llvm { \ | ||||||||||||
| 2069 | namespace yaml { \ | ||||||||||||
| 2070 | template <> struct MappingTraits<Type> { \ | ||||||||||||
| 2071 | static void mapping(IO &IO, Type &Obj); \ | ||||||||||||
| 2072 | }; \ | ||||||||||||
| 2073 | } \ | ||||||||||||
| 2074 | } | ||||||||||||
| 2075 | |||||||||||||
| 2076 | #define LLVM_YAML_DECLARE_ENUM_TRAITS(Type)namespace llvm { namespace yaml { template <> struct ScalarEnumerationTraits <Type> { static void enumeration(IO &io, Type & Value); }; } } \ | ||||||||||||
| 2077 | namespace llvm { \ | ||||||||||||
| 2078 | namespace yaml { \ | ||||||||||||
| 2079 | template <> struct ScalarEnumerationTraits<Type> { \ | ||||||||||||
| 2080 | static void enumeration(IO &io, Type &Value); \ | ||||||||||||
| 2081 | }; \ | ||||||||||||
| 2082 | } \ | ||||||||||||
| 2083 | } | ||||||||||||
| 2084 | |||||||||||||
| 2085 | #define LLVM_YAML_DECLARE_BITSET_TRAITS(Type)namespace llvm { namespace yaml { template <> struct ScalarBitSetTraits <Type> { static void bitset(IO &IO, Type &Options ); }; } } \ | ||||||||||||
| 2086 | namespace llvm { \ | ||||||||||||
| 2087 | namespace yaml { \ | ||||||||||||
| 2088 | template <> struct ScalarBitSetTraits<Type> { \ | ||||||||||||
| 2089 | static void bitset(IO &IO, Type &Options); \ | ||||||||||||
| 2090 | }; \ | ||||||||||||
| 2091 | } \ | ||||||||||||
| 2092 | } | ||||||||||||
| 2093 | |||||||||||||
| 2094 | #define LLVM_YAML_DECLARE_SCALAR_TRAITS(Type, MustQuote)namespace llvm { namespace yaml { template <> struct ScalarTraits <Type> { static void output(const Type &Value, void *ctx, raw_ostream &Out); static StringRef input(StringRef Scalar, void *ctxt, Type &Value); static QuotingType mustQuote (StringRef) { return MustQuote; } }; } } \ | ||||||||||||
| 2095 | namespace llvm { \ | ||||||||||||
| 2096 | namespace yaml { \ | ||||||||||||
| 2097 | template <> struct ScalarTraits<Type> { \ | ||||||||||||
| 2098 | static void output(const Type &Value, void *ctx, raw_ostream &Out); \ | ||||||||||||
| 2099 | static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \ | ||||||||||||
| 2100 | static QuotingType mustQuote(StringRef) { return MustQuote; } \ | ||||||||||||
| 2101 | }; \ | ||||||||||||
| 2102 | } \ | ||||||||||||
| 2103 | } | ||||||||||||
| 2104 | |||||||||||||
| 2105 | /// Utility for declaring that a std::vector of a particular type | ||||||||||||
| 2106 | /// should be considered a YAML document list. | ||||||||||||
| 2107 | #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type)namespace llvm { namespace yaml { template <unsigned N> struct DocumentListTraits<SmallVector<_type, N>> : public SequenceTraitsImpl<SmallVector<_type, N>, false > {}; template <> struct DocumentListTraits<std:: vector<_type>> : public SequenceTraitsImpl<std::vector <_type>, false> {}; } } \ | ||||||||||||
| 2108 | namespace llvm { \ | ||||||||||||
| 2109 | namespace yaml { \ | ||||||||||||
| 2110 | template <unsigned N> \ | ||||||||||||
| 2111 | struct DocumentListTraits<SmallVector<_type, N>> \ | ||||||||||||
| 2112 | : public SequenceTraitsImpl<SmallVector<_type, N>, false> {}; \ | ||||||||||||
| 2113 | template <> \ | ||||||||||||
| 2114 | struct DocumentListTraits<std::vector<_type>> \ | ||||||||||||
| 2115 | : public SequenceTraitsImpl<std::vector<_type>, false> {}; \ | ||||||||||||
| 2116 | } \ | ||||||||||||
| 2117 | } | ||||||||||||
| 2118 | |||||||||||||
| 2119 | /// Utility for declaring that std::map<std::string, _type> should be considered | ||||||||||||
| 2120 | /// a YAML map. | ||||||||||||
| 2121 | #define LLVM_YAML_IS_STRING_MAP(_type)namespace llvm { namespace yaml { template <> struct CustomMappingTraits <std::map<std::string, _type>> : public StdMapStringCustomMappingTraitsImpl <_type> {}; } } \ | ||||||||||||
| 2122 | namespace llvm { \ | ||||||||||||
| 2123 | namespace yaml { \ | ||||||||||||
| 2124 | template <> \ | ||||||||||||
| 2125 | struct CustomMappingTraits<std::map<std::string, _type>> \ | ||||||||||||
| 2126 | : public StdMapStringCustomMappingTraitsImpl<_type> {}; \ | ||||||||||||
| 2127 | } \ | ||||||||||||
| 2128 | } | ||||||||||||
| 2129 | |||||||||||||
| 2130 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex64)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <llvm::yaml::Hex64> && !std::is_same_v<llvm:: yaml::Hex64, std::string> && !std::is_same_v<llvm ::yaml::Hex64, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::yaml ::Hex64> { static const bool flow = true; }; } } | ||||||||||||
| 2131 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex32)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <llvm::yaml::Hex32> && !std::is_same_v<llvm:: yaml::Hex32, std::string> && !std::is_same_v<llvm ::yaml::Hex32, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::yaml ::Hex32> { static const bool flow = true; }; } } | ||||||||||||
| 2132 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex16)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <llvm::yaml::Hex16> && !std::is_same_v<llvm:: yaml::Hex16, std::string> && !std::is_same_v<llvm ::yaml::Hex16, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::yaml ::Hex16> { static const bool flow = true; }; } } | ||||||||||||
| 2133 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex8)namespace llvm { namespace yaml { static_assert( !std::is_fundamental_v <llvm::yaml::Hex8> && !std::is_same_v<llvm:: yaml::Hex8, std::string> && !std::is_same_v<llvm ::yaml::Hex8, llvm::StringRef>, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::yaml ::Hex8> { static const bool flow = true; }; } } | ||||||||||||
| 2134 | |||||||||||||
| 2135 | #endif // LLVM_SUPPORT_YAMLTRAITS_H |
| 1 | // The -*- C++ -*- dynamic memory management header. |
| 2 | |
| 3 | // Copyright (C) 1994-2020 Free Software Foundation, Inc. |
| 4 | |
| 5 | // This file is part of GCC. |
| 6 | // |
| 7 | // GCC is free software; you can redistribute it and/or modify |
| 8 | // it under the terms of the GNU General Public License as published by |
| 9 | // the Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | // |
| 12 | // GCC is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | // |
| 17 | // Under Section 7 of GPL version 3, you are granted additional |
| 18 | // permissions described in the GCC Runtime Library Exception, version |
| 19 | // 3.1, as published by the Free Software Foundation. |
| 20 | |
| 21 | // You should have received a copy of the GNU General Public License and |
| 22 | // a copy of the GCC Runtime Library Exception along with this program; |
| 23 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 24 | // <http://www.gnu.org/licenses/>. |
| 25 | |
| 26 | /** @file new |
| 27 | * This is a Standard C++ Library header. |
| 28 | * |
| 29 | * The header @c new defines several functions to manage dynamic memory and |
| 30 | * handling memory allocation errors; see |
| 31 | * https://gcc.gnu.org/onlinedocs/libstdc++/manual/dynamic_memory.html |
| 32 | * for more. |
| 33 | */ |
| 34 | |
| 35 | #ifndef _NEW |
| 36 | #define _NEW |
| 37 | |
| 38 | #pragma GCC system_header |
| 39 | |
| 40 | #include <bits/c++config.h> |
| 41 | #include <exception> |
| 42 | |
| 43 | #pragma GCC visibility push(default) |
| 44 | |
| 45 | extern "C++" { |
| 46 | |
| 47 | namespace std |
| 48 | { |
| 49 | /** |
| 50 | * @brief Exception possibly thrown by @c new. |
| 51 | * @ingroup exceptions |
| 52 | * |
| 53 | * @c bad_alloc (or classes derived from it) is used to report allocation |
| 54 | * errors from the throwing forms of @c new. */ |
| 55 | class bad_alloc : public exception |
| 56 | { |
| 57 | public: |
| 58 | bad_alloc() throw() { } |
| 59 | |
| 60 | #if __cplusplus201703L >= 201103L |
| 61 | bad_alloc(const bad_alloc&) = default; |
| 62 | bad_alloc& operator=(const bad_alloc&) = default; |
| 63 | #endif |
| 64 | |
| 65 | // This declaration is not useless: |
| 66 | // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 |
| 67 | virtual ~bad_alloc() throw(); |
| 68 | |
| 69 | // See comment in eh_exception.cc. |
| 70 | virtual const char* what() const throw(); |
| 71 | }; |
| 72 | |
| 73 | #if __cplusplus201703L >= 201103L |
| 74 | class bad_array_new_length : public bad_alloc |
| 75 | { |
| 76 | public: |
| 77 | bad_array_new_length() throw() { } |
| 78 | |
| 79 | // This declaration is not useless: |
| 80 | // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 |
| 81 | virtual ~bad_array_new_length() throw(); |
| 82 | |
| 83 | // See comment in eh_exception.cc. |
| 84 | virtual const char* what() const throw(); |
| 85 | }; |
| 86 | #endif |
| 87 | |
| 88 | #if __cpp_aligned_new201606L |
| 89 | enum class align_val_t: size_t {}; |
| 90 | #endif |
| 91 | |
| 92 | struct nothrow_t |
| 93 | { |
| 94 | #if __cplusplus201703L >= 201103L |
| 95 | explicit nothrow_t() = default; |
| 96 | #endif |
| 97 | }; |
| 98 | |
| 99 | extern const nothrow_t nothrow; |
| 100 | |
| 101 | /** If you write your own error handler to be called by @c new, it must |
| 102 | * be of this type. */ |
| 103 | typedef void (*new_handler)(); |
| 104 | |
| 105 | /// Takes a replacement handler as the argument, returns the |
| 106 | /// previous handler. |
| 107 | new_handler set_new_handler(new_handler) throw(); |
| 108 | |
| 109 | #if __cplusplus201703L >= 201103L |
| 110 | /// Return the current new handler. |
| 111 | new_handler get_new_handler() noexcept; |
| 112 | #endif |
| 113 | } // namespace std |
| 114 | |
| 115 | //@{ |
| 116 | /** These are replaceable signatures: |
| 117 | * - normal single new and delete (no arguments, throw @c bad_alloc on error) |
| 118 | * - normal array new and delete (same) |
| 119 | * - @c nothrow single new and delete (take a @c nothrow argument, return |
| 120 | * @c NULL on error) |
| 121 | * - @c nothrow array new and delete (same) |
| 122 | * |
| 123 | * Placement new and delete signatures (take a memory address argument, |
| 124 | * does nothing) may not be replaced by a user's program. |
| 125 | */ |
| 126 | _GLIBCXX_NODISCARD[[__nodiscard__]] void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc) |
| 127 | __attribute__((__externally_visible__)); |
| 128 | _GLIBCXX_NODISCARD[[__nodiscard__]] void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc) |
| 129 | __attribute__((__externally_visible__)); |
| 130 | void operator delete(void*) _GLIBCXX_USE_NOEXCEPTnoexcept |
| 131 | __attribute__((__externally_visible__)); |
| 132 | void operator delete[](void*) _GLIBCXX_USE_NOEXCEPTnoexcept |
| 133 | __attribute__((__externally_visible__)); |
| 134 | #if __cpp_sized_deallocation |
| 135 | void operator delete(void*, std::size_t) _GLIBCXX_USE_NOEXCEPTnoexcept |
| 136 | __attribute__((__externally_visible__)); |
| 137 | void operator delete[](void*, std::size_t) _GLIBCXX_USE_NOEXCEPTnoexcept |
| 138 | __attribute__((__externally_visible__)); |
| 139 | #endif |
| 140 | _GLIBCXX_NODISCARD[[__nodiscard__]] void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPTnoexcept |
| 141 | __attribute__((__externally_visible__, __malloc__)); |
| 142 | _GLIBCXX_NODISCARD[[__nodiscard__]] void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPTnoexcept |
| 143 | __attribute__((__externally_visible__, __malloc__)); |
| 144 | void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPTnoexcept |
| 145 | __attribute__((__externally_visible__)); |
| 146 | void operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPTnoexcept |
| 147 | __attribute__((__externally_visible__)); |
| 148 | #if __cpp_aligned_new201606L |
| 149 | _GLIBCXX_NODISCARD[[__nodiscard__]] void* operator new(std::size_t, std::align_val_t) |
| 150 | __attribute__((__externally_visible__)); |
| 151 | _GLIBCXX_NODISCARD[[__nodiscard__]] void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&) |
| 152 | _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__, __malloc__)); |
| 153 | void operator delete(void*, std::align_val_t) |
| 154 | _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__)); |
| 155 | void operator delete(void*, std::align_val_t, const std::nothrow_t&) |
| 156 | _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__)); |
| 157 | _GLIBCXX_NODISCARD[[__nodiscard__]] void* operator new[](std::size_t, std::align_val_t) |
| 158 | __attribute__((__externally_visible__)); |
| 159 | _GLIBCXX_NODISCARD[[__nodiscard__]] void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&) |
| 160 | _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__, __malloc__)); |
| 161 | void operator delete[](void*, std::align_val_t) |
| 162 | _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__)); |
| 163 | void operator delete[](void*, std::align_val_t, const std::nothrow_t&) |
| 164 | _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__)); |
| 165 | #if __cpp_sized_deallocation |
| 166 | void operator delete(void*, std::size_t, std::align_val_t) |
| 167 | _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__)); |
| 168 | void operator delete[](void*, std::size_t, std::align_val_t) |
| 169 | _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__)); |
| 170 | #endif // __cpp_sized_deallocation |
| 171 | #endif // __cpp_aligned_new |
| 172 | |
| 173 | // Default placement versions of operator new. |
| 174 | _GLIBCXX_NODISCARD[[__nodiscard__]] inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPTnoexcept |
| 175 | { return __p; } |
| 176 | _GLIBCXX_NODISCARD[[__nodiscard__]] inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPTnoexcept |
| 177 | { return __p; } |
| 178 | |
| 179 | // Default placement versions of operator delete. |
| 180 | inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPTnoexcept { } |
| 181 | inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPTnoexcept { } |
| 182 | //@} |
| 183 | } // extern "C++" |
| 184 | |
| 185 | #if __cplusplus201703L >= 201703L |
| 186 | #ifdef _GLIBCXX_HAVE_BUILTIN_LAUNDER1 |
| 187 | namespace std |
| 188 | { |
| 189 | #define __cpp_lib_launder201606 201606 |
| 190 | /// Pointer optimization barrier [ptr.launder] |
| 191 | template<typename _Tp> |
| 192 | [[nodiscard]] constexpr _Tp* |
| 193 | launder(_Tp* __p) noexcept |
| 194 | { return __builtin_launder(__p); } |
| 195 | |
| 196 | // The program is ill-formed if T is a function type or |
| 197 | // (possibly cv-qualified) void. |
| 198 | |
| 199 | template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM, bool _NE> |
| 200 | void launder(_Ret (*)(_Args...) _GLIBCXX_NOEXCEPT_QUALnoexcept (_NE)) = delete; |
| 201 | template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM, bool _NE> |
| 202 | void launder(_Ret (*)(_Args......) _GLIBCXX_NOEXCEPT_QUALnoexcept (_NE)) = delete; |
| 203 | |
| 204 | void launder(void*) = delete; |
| 205 | void launder(const void*) = delete; |
| 206 | void launder(volatile void*) = delete; |
| 207 | void launder(const volatile void*) = delete; |
| 208 | } |
| 209 | #endif // _GLIBCXX_HAVE_BUILTIN_LAUNDER |
| 210 | #endif // C++17 |
| 211 | |
| 212 | #if __cplusplus201703L > 201703L |
| 213 | namespace std |
| 214 | { |
| 215 | /// Tag type used to declare a class-specific operator delete that can |
| 216 | /// invoke the destructor before deallocating the memory. |
| 217 | struct destroying_delete_t |
| 218 | { |
| 219 | explicit destroying_delete_t() = default; |
| 220 | }; |
| 221 | /// Tag variable of type destroying_delete_t. |
| 222 | inline constexpr destroying_delete_t destroying_delete{}; |
| 223 | } |
| 224 | // Only define the feature test macro if the compiler supports the feature: |
| 225 | #if __cpp_impl_destroying_delete201806L |
| 226 | # define __cpp_lib_destroying_delete 201806L |
| 227 | #endif |
| 228 | #endif // C++20 |
| 229 | |
| 230 | #pragma GCC visibility pop |
| 231 | |
| 232 | #endif |