LLVM API Documentation
00001 //==- Serialization.h - Generic Object Serialization to Bitcode ---*- C++ -*-=// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines traits for primitive types used for both object 00011 // serialization and deserialization. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_BITCODE_SERIALIZE 00016 #define LLVM_BITCODE_SERIALIZE 00017 00018 #include "llvm/Bitcode/SerializationFwd.h" 00019 00020 namespace llvm { 00021 00022 /// SerializeTrait - SerializeTrait bridges between the Serializer/Deserializer 00023 /// and the functions that serialize objects of specific types. The default 00024 /// behavior is to call static methods of the class for the object being 00025 /// serialized, but this behavior can be changed by specializing this 00026 /// template. Classes only need to implement the methods corresponding 00027 /// to the serialization scheme they want to support. For example, "Read" 00028 /// and "ReadVal" correspond to different deserialization schemes which make 00029 /// sense for different types; a class need only implement one of them. 00030 /// Serialization and deserialization of pointers are specially handled 00031 /// by the Serializer and Deserializer using the EmitOwnedPtr, etc. methods. 00032 /// To serialize the actual object referred to by a pointer, the class 00033 /// of the object either must implement the methods called by the default 00034 /// behavior of SerializeTrait, or specialize SerializeTrait. This latter 00035 /// is useful when one cannot add methods to an existing class (for example). 00036 template <typename T> 00037 struct SerializeTrait { 00038 static inline void Emit(Serializer& S, const T& X) { X.Emit(S); } 00039 static inline void Read(Deserializer& D, T& X) { X.Read(D); } 00040 static inline T* Create(Deserializer& D) { return T::Create(D); } 00041 00042 template <typename Arg1> 00043 static inline T* Create(Deserializer& D, Arg1& arg1) { 00044 return T::Create(D, arg1); 00045 } 00046 }; 00047 00048 #define SERIALIZE_INT_TRAIT(TYPE)\ 00049 template <> struct SerializeTrait<TYPE> {\ 00050 static void Emit(Serializer& S, TYPE X);\ 00051 static void Read(Deserializer& S, TYPE& X); }; 00052 00053 SERIALIZE_INT_TRAIT(bool) 00054 SERIALIZE_INT_TRAIT(unsigned char) 00055 SERIALIZE_INT_TRAIT(unsigned short) 00056 SERIALIZE_INT_TRAIT(unsigned int) 00057 SERIALIZE_INT_TRAIT(unsigned long) 00058 00059 SERIALIZE_INT_TRAIT(signed char) 00060 SERIALIZE_INT_TRAIT(signed short) 00061 SERIALIZE_INT_TRAIT(signed int) 00062 SERIALIZE_INT_TRAIT(signed long) 00063 00064 #undef SERIALIZE_INT_TRAIT 00065 00066 } // end namespace llvm 00067 00068 #endif