1 //==- Serialization.h - Generic Object Serialization to Bitcode ---*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines traits for primitive types used for both object
11 // serialization and deserialization.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_BITCODE_SERIALIZE
16 #define LLVM_BITCODE_SERIALIZE
18 #include "llvm/Bitcode/SerializationFwd.h"
22 /// SerializeTrait - SerializeTrait bridges between the Serializer/Deserializer
23 /// and the functions that serialize objects of specific types. The default
24 /// behavior is to call static methods of the class for the object being
25 /// serialized, but this behavior can be changed by specializing this
26 /// template. Classes only need to implement the methods corresponding
27 /// to the serialization scheme they want to support. For example, "Read"
28 /// and "ReadVal" correspond to different deserialization schemes which make
29 /// sense for different types; a class need only implement one of them.
30 /// Serialization and deserialization of pointers are specially handled
31 /// by the Serializer and Deserializer using the EmitOwnedPtr, etc. methods.
32 /// To serialize the actual object referred to by a pointer, the class
33 /// of the object either must implement the methods called by the default
34 /// behavior of SerializeTrait, or specialize SerializeTrait. This latter
35 /// is useful when one cannot add methods to an existing class (for example).
37 struct SerializeTrait {
38 static inline void Emit(Serializer& S, const T& X) { X.Emit(S); }
39 static inline void Read(Deserializer& D, T& X) { X.Read(D); }
40 static inline T* Create(Deserializer& D) { return T::Create(D); }
42 template <typename Arg1>
43 static inline T* Create(Deserializer& D, Arg1& arg1) {
44 return T::Create(D, arg1);
48 #define SERIALIZE_INT_TRAIT(TYPE)\
49 template <> struct SerializeTrait<TYPE> {\
50 static void Emit(Serializer& S, TYPE X);\
51 static void Read(Deserializer& S, TYPE& X); };
53 SERIALIZE_INT_TRAIT(bool)
54 SERIALIZE_INT_TRAIT(unsigned char)
55 SERIALIZE_INT_TRAIT(unsigned short)
56 SERIALIZE_INT_TRAIT(unsigned int)
57 SERIALIZE_INT_TRAIT(unsigned long)
59 SERIALIZE_INT_TRAIT(signed char)
60 SERIALIZE_INT_TRAIT(signed short)
61 SERIALIZE_INT_TRAIT(signed int)
62 SERIALIZE_INT_TRAIT(signed long)
64 #undef SERIALIZE_INT_TRAIT
66 } // end namespace llvm