- //=- Serialization.h - Generic Object Serialization to Bitcode ---*- C++ -*-=//
+//==- Serialization.h - Generic Object Serialization to Bitcode ---*- C++ -*-=//
//
// The LLVM Compiler Infrastructure
//
-// This file was developed by Ted Kremenek and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
-// This file defines the interface for generic object serialization to
-// LLVM bitcode.
+// This file defines traits for primitive types used for both object
+// serialization and deserialization.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_BITCODE_SERIALIZE
#define LLVM_BITCODE_SERIALIZE
-#include "llvm/Bitcode/BitstreamWriter.h"
-#include "llvm/Bitcode/BitstreamReader.h"
-#include "llvm/ADT/SmallVector.h"
-#include <vector>
+#include "llvm/Bitcode/SerializationFwd.h"
namespace llvm {
-template <typename T> struct SerializeTrait;
-
-class Serializer {
- BitstreamWriter& Stream;
- SmallVector<uint64_t,10> Record;
- bool inBlock;
-public:
- Serializer(BitstreamWriter& stream, unsigned BlockID = 0);
- ~Serializer();
-
- template <typename T>
- inline void Emit(const T& X) { SerializeTrait<T>::Serialize(*this,X); }
-
- void EmitInt(unsigned X, unsigned bits);
- void EmitCString(const char* cstr);
+/// SerializeTrait - SerializeTrait bridges between the Serializer/Deserializer
+/// and the functions that serialize objects of specific types. The default
+/// behavior is to call static methods of the class for the object being
+/// serialized, but this behavior can be changed by specializing this
+/// template. Classes only need to implement the methods corresponding
+/// to the serialization scheme they want to support. For example, "Read"
+/// and "ReadVal" correspond to different deserialization schemes which make
+/// sense for different types; a class need only implement one of them.
+/// Serialization and deserialization of pointers are specially handled
+/// by the Serializer and Deserializer using the EmitOwnedPtr, etc. methods.
+/// To serialize the actual object referred to by a pointer, the class
+/// of the object either must implement the methods called by the default
+/// behavior of SerializeTrait, or specialize SerializeTrait. This latter
+/// is useful when one cannot add methods to an existing class (for example).
+template <typename T>
+struct SerializeTrait {
+ static inline void Emit(Serializer& S, const T& X) { X.Emit(S); }
+ static inline void Read(Deserializer& D, T& X) { X.Read(D); }
+ static inline T* Create(Deserializer& D) { return T::Create(D); }
- void Flush() { if (inRecord()) EmitRecord(); }
-
-private:
- void EmitRecord();
- inline bool inRecord() { return Record.size() > 0; }
-};
-
-
-class Deserializer {
- BitstreamReader& Stream;
- SmallVector<uint64_t,10> Record;
- unsigned RecIdx;
-public:
- Deserializer(BitstreamReader& stream);
- ~Deserializer();
-
- template <typename T>
- inline void Read(T& X) { SerializeTrait<T>::Deserialize(*this,X); }
-
- template <typename T>
- inline T* Materialize() {
- T* X = SerializeTrait<T>::Instantiate();
- Read(*X);
- return X;
- }
-
- uint64_t ReadInt(unsigned bits = 32);
- bool ReadBool() { return ReadInt(1); }
-
- char* ReadCString(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
- void ReadCString(std::vector<char>& buff, bool isNullTerm=false);
-
-private:
- void ReadRecord();
-
- inline bool inRecord() {
- if (Record.size() > 0) {
- if (RecIdx >= Record.size()) {
- RecIdx = 0;
- Record.clear();
- return false;
- }
- else return true;
- }
- else return false;
+ template <typename Arg1>
+ static inline T* Create(Deserializer& D, Arg1& arg1) {
+ return T::Create(D, arg1);
}
};
-
-template <typename uintty, unsigned Bits>
-struct SerializeIntTrait {
- static inline void Serialize(Serializer& S, uintty X) {
- S.EmitInt(X,Bits);
- }
-
- static inline void Deserialize(Deserializer& S, uintty& X) {
- X = (uintty) S.ReadInt(Bits);
- }
-};
+#define SERIALIZE_INT_TRAIT(TYPE)\
+template <> struct SerializeTrait<TYPE> {\
+ static void Emit(Serializer& S, TYPE X);\
+ static void Read(Deserializer& S, TYPE& X); };
-
-template <> struct SerializeTrait<bool>
- : public SerializeIntTrait<bool,1> {};
+SERIALIZE_INT_TRAIT(bool)
+SERIALIZE_INT_TRAIT(unsigned char)
+SERIALIZE_INT_TRAIT(unsigned short)
+SERIALIZE_INT_TRAIT(unsigned int)
+SERIALIZE_INT_TRAIT(unsigned long)
-template <> struct SerializeTrait<char>
- : public SerializeIntTrait<char,8> {};
-
-template <> struct SerializeTrait<short>
- : public SerializeIntTrait<short,16> {};
+SERIALIZE_INT_TRAIT(signed char)
+SERIALIZE_INT_TRAIT(signed short)
+SERIALIZE_INT_TRAIT(signed int)
+SERIALIZE_INT_TRAIT(signed long)
-template <> struct SerializeTrait<unsigned>
- : public SerializeIntTrait<unsigned,32> {};
+#undef SERIALIZE_INT_TRAIT
} // end namespace llvm
+
#endif