xref: /llvm-project/llvm/lib/ObjectYAML/ArchiveYAML.cpp (revision 47369e194a48dbe3cf04a7845e23e25e43e973f0)
1*47369e19SGeorgii Rymar //===- ArchiveYAML.cpp - ELF YAMLIO implementation -------------------- ----===//
2*47369e19SGeorgii Rymar //
3*47369e19SGeorgii Rymar // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*47369e19SGeorgii Rymar // See https://llvm.org/LICENSE.txt for license information.
5*47369e19SGeorgii Rymar // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*47369e19SGeorgii Rymar //
7*47369e19SGeorgii Rymar //===----------------------------------------------------------------------===//
8*47369e19SGeorgii Rymar //
9*47369e19SGeorgii Rymar // This file defines classes for handling the YAML representation of archives.
10*47369e19SGeorgii Rymar //
11*47369e19SGeorgii Rymar //===----------------------------------------------------------------------===//
12*47369e19SGeorgii Rymar 
13*47369e19SGeorgii Rymar #include "llvm/ObjectYAML/ArchiveYAML.h"
14*47369e19SGeorgii Rymar 
15*47369e19SGeorgii Rymar namespace llvm {
16*47369e19SGeorgii Rymar 
17*47369e19SGeorgii Rymar namespace yaml {
18*47369e19SGeorgii Rymar 
mapping(IO & IO,ArchYAML::Archive & A)19*47369e19SGeorgii Rymar void MappingTraits<ArchYAML::Archive>::mapping(IO &IO, ArchYAML::Archive &A) {
20*47369e19SGeorgii Rymar   assert(!IO.getContext() && "The IO context is initialized already");
21*47369e19SGeorgii Rymar   IO.setContext(&A);
22*47369e19SGeorgii Rymar   IO.mapTag("!Arch", true);
23*47369e19SGeorgii Rymar   IO.mapOptional("Magic", A.Magic, "!<arch>\n");
24*47369e19SGeorgii Rymar   IO.mapOptional("Members", A.Members);
25*47369e19SGeorgii Rymar   IO.mapOptional("Content", A.Content);
26*47369e19SGeorgii Rymar   IO.setContext(nullptr);
27*47369e19SGeorgii Rymar }
28*47369e19SGeorgii Rymar 
validate(IO &,ArchYAML::Archive & A)29*47369e19SGeorgii Rymar std::string MappingTraits<ArchYAML::Archive>::validate(IO &,
30*47369e19SGeorgii Rymar                                                        ArchYAML::Archive &A) {
31*47369e19SGeorgii Rymar   if (A.Members && A.Content)
32*47369e19SGeorgii Rymar     return "\"Content\" and \"Members\" cannot be used together";
33*47369e19SGeorgii Rymar   return "";
34*47369e19SGeorgii Rymar }
35*47369e19SGeorgii Rymar 
mapping(IO & IO,ArchYAML::Archive::Child & E)36*47369e19SGeorgii Rymar void MappingTraits<ArchYAML::Archive::Child>::mapping(
37*47369e19SGeorgii Rymar     IO &IO, ArchYAML::Archive::Child &E) {
38*47369e19SGeorgii Rymar   assert(IO.getContext() && "The IO context is not initialized");
39*47369e19SGeorgii Rymar   for (auto &P : E.Fields)
40*47369e19SGeorgii Rymar     IO.mapOptional(P.first.data(), P.second.Value, P.second.DefaultValue);
41*47369e19SGeorgii Rymar   IO.mapOptional("Content", E.Content);
42*47369e19SGeorgii Rymar   IO.mapOptional("PaddingByte", E.PaddingByte);
43*47369e19SGeorgii Rymar }
44*47369e19SGeorgii Rymar 
45*47369e19SGeorgii Rymar std::string
validate(IO &,ArchYAML::Archive::Child & C)46*47369e19SGeorgii Rymar MappingTraits<ArchYAML::Archive::Child>::validate(IO &,
47*47369e19SGeorgii Rymar                                                   ArchYAML::Archive::Child &C) {
48*47369e19SGeorgii Rymar   for (auto &P : C.Fields)
49*47369e19SGeorgii Rymar     if (P.second.Value.size() > P.second.MaxLength)
50*47369e19SGeorgii Rymar       return ("the maximum length of \"" + P.first + "\" field is " +
51*47369e19SGeorgii Rymar               Twine(P.second.MaxLength))
52*47369e19SGeorgii Rymar           .str();
53*47369e19SGeorgii Rymar   return "";
54*47369e19SGeorgii Rymar }
55*47369e19SGeorgii Rymar 
56*47369e19SGeorgii Rymar } // end namespace yaml
57*47369e19SGeorgii Rymar 
58*47369e19SGeorgii Rymar } // end namespace llvm
59