1 //===-- LLVMSPSSerializers.h - SPS serialization for LLVM types -*- 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 // SPS Serialization for common LLVM types.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_LLVMSPSSERIALIZERS_H
14 #define LLVM_EXECUTIONENGINE_ORC_LLVMSPSSERIALIZERS_H
15 
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h"
18 
19 namespace llvm {
20 namespace orc {
21 namespace shared {
22 
23 template <typename SPSValueT, typename ValueT>
24 class SPSSerializationTraits<SPSSequence<SPSTuple<SPSString, SPSValueT>>,
25                              StringMap<ValueT>> {
26 public:
size(const StringMap<ValueT> & M)27   static size_t size(const StringMap<ValueT> &M) {
28     size_t Sz = SPSArgList<uint64_t>::size(static_cast<uint64_t>(M.size()));
29     for (auto &E : M)
30       Sz += SPSArgList<SPSString, SPSValueT>::size(E.first(), E.second);
31     return Sz;
32   }
33 
serialize(SPSOutputBuffer & OB,const StringMap<ValueT> & M)34   static bool serialize(SPSOutputBuffer &OB, const StringMap<ValueT> &M) {
35     if (!SPSArgList<uint64_t>::serialize(OB, static_cast<uint64_t>(M.size())))
36       return false;
37 
38     for (auto &E : M)
39       if (!SPSArgList<SPSString, SPSValueT>::serialize(OB, E.first(), E.second))
40         return false;
41 
42     return true;
43   }
44 
deserialize(SPSInputBuffer & IB,StringMap<ValueT> & M)45   static bool deserialize(SPSInputBuffer &IB, StringMap<ValueT> &M) {
46     uint64_t Size;
47     assert(M.empty() && "M already contains elements");
48 
49     if (!SPSArgList<uint64_t>::deserialize(IB, Size))
50       return false;
51 
52     while (Size--) {
53       StringRef S;
54       ValueT V;
55       if (!SPSArgList<SPSString, SPSValueT>::deserialize(IB, S, V))
56         return false;
57       if (!M.insert(std::make_pair(S, V)).second)
58         return false;
59     }
60 
61     return true;
62   }
63 };
64 
65 } // end namespace shared
66 } // end namespace orc
67 } // end namespace llvm
68 
69 #endif // LLVM_EXECUTIONENGINE_ORC_LLVMSPSSERIALIZERS_H
70