Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 1 | //===--- SerializationTest.cpp - Experimental Object Serialization --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Ted Kremenek and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements prototype code for serialization of objects in clang. |
| 11 | // It is not intended yet for public use, but simply is a placeholder to |
| 12 | // experiment with new serialization features. Serialization will eventually |
| 13 | // be integrated as a proper component of the clang libraries. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "ASTConsumers.h" |
| 18 | #include "clang/AST/AST.h" |
| 19 | #include "clang/AST/ASTConsumer.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "llvm/System/Path.h" |
| 22 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 23 | #include <fstream> |
Anders Carlsson | adc9b13 | 2007-10-17 00:50:25 +0000 | [diff] [blame^] | 24 | #include <iostream> |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | using llvm::BitstreamWriter; |
| 28 | using std::cerr; |
| 29 | using std::cout; |
| 30 | using std::endl; |
| 31 | using std::flush; |
| 32 | |
| 33 | namespace llvm { |
| 34 | template<typename T> struct IntrospectionTrait { |
| 35 | struct Flags { |
| 36 | enum { isPod = false, UniqueInstances = false, UniqueRefs = false }; |
| 37 | }; |
| 38 | |
| 39 | template<typename Introspector> |
| 40 | struct Ops { |
| 41 | static inline void Introspect(T& X, Introspector& I) { |
| 42 | assert (false && "Introspect not implemented."); |
| 43 | } |
| 44 | }; |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | namespace { |
| 49 | class SerializationTest : public ASTConsumer { |
| 50 | IdentifierTable* IdTable; |
| 51 | unsigned MainFileID; |
| 52 | public: |
| 53 | void Initialize(ASTContext& Context, unsigned mainFileID) { |
| 54 | IdTable = &Context.Idents; |
| 55 | MainFileID = mainFileID; |
| 56 | RunSerializationTest(); |
| 57 | } |
| 58 | |
| 59 | void RunSerializationTest(); |
| 60 | bool WriteAll(llvm::sys::Path& Filename); |
| 61 | |
| 62 | virtual void HandleTopLevelDecl(Decl *D) {} |
| 63 | }; |
| 64 | |
| 65 | class Writer { |
| 66 | std::vector<unsigned char> Buffer; |
| 67 | BitstreamWriter Stream; |
| 68 | std::ostream& Out; |
| 69 | public: |
| 70 | |
| 71 | enum { IdentifierTableBID = 0x8 }; |
| 72 | |
| 73 | Writer(std::ostream& out) : Stream(Buffer), Out(out) { |
| 74 | Buffer.reserve(256*1024); |
| 75 | |
| 76 | // Emit the file header. |
| 77 | Stream.Emit((unsigned)'B', 8); |
| 78 | Stream.Emit((unsigned)'C', 8); |
| 79 | Stream.Emit(0xC, 4); |
| 80 | Stream.Emit(0xF, 4); |
| 81 | Stream.Emit(0xE, 4); |
| 82 | Stream.Emit(0x0, 4); |
| 83 | } |
| 84 | |
| 85 | ~Writer() { |
| 86 | Out.write((char*)&Buffer.front(), Buffer.size()); |
| 87 | Out.flush(); |
| 88 | } |
| 89 | |
| 90 | template <typename T> inline void operator()(T& x) { |
| 91 | llvm::IntrospectionTrait<T>::template Ops<Writer>::Introspect(x,*this); |
| 92 | } |
| 93 | |
| 94 | template <typename T> inline void operator()(T& x, unsigned bits) { |
| 95 | llvm::IntrospectionTrait<T>::template Ops<Writer>::Introspect(x,bits,*this); |
| 96 | } |
| 97 | |
| 98 | template <typename T> inline void operator()(const T& x) { |
| 99 | operator()(const_cast<T&>(x)); |
| 100 | } |
| 101 | |
| 102 | template <typename T> inline void operator()(const T& x, unsigned bits) { |
| 103 | operator()(const_cast<T&>(x),bits); |
| 104 | } |
| 105 | |
| 106 | inline void operator()(bool X) { Stream.Emit(X,1); } |
| 107 | inline void operator()(unsigned X) { Stream.Emit(X,32); } |
| 108 | inline void operator()(unsigned X, unsigned bits, bool VBR=false) { |
| 109 | if (VBR) Stream.Emit(X,bits); |
| 110 | else Stream.Emit(X,bits); |
| 111 | } |
| 112 | |
| 113 | inline BitstreamWriter& getStream() { |
| 114 | return Stream; |
| 115 | } |
| 116 | |
| 117 | template <typename T> inline void EnterSubblock(unsigned CodeLen) { |
| 118 | Stream.EnterSubblock(8,CodeLen); |
| 119 | } |
| 120 | |
| 121 | inline void ExitBlock() { Stream.ExitBlock(); } |
| 122 | |
| 123 | }; |
| 124 | |
| 125 | } // end anonymous namespace |
| 126 | |
| 127 | //===----------------------------------------------------------------------===// |
| 128 | // External Interface. |
| 129 | //===----------------------------------------------------------------------===// |
| 130 | |
| 131 | ASTConsumer* clang::CreateSerializationTest() { |
| 132 | return new SerializationTest(); |
| 133 | } |
| 134 | |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | // Serialization "Driver" code. |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | |
| 139 | void SerializationTest::RunSerializationTest() { |
| 140 | std::string ErrMsg; |
| 141 | llvm::sys::Path Filename = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
| 142 | |
| 143 | if (Filename.isEmpty()) { |
| 144 | cerr << "Error: " << ErrMsg << "\n"; |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | Filename.appendComponent("test.cfe_bc"); |
| 149 | |
| 150 | if (Filename.makeUnique(true,&ErrMsg)) { |
| 151 | cerr << "Error: " << ErrMsg << "\n"; |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | if (!WriteAll(Filename)) |
| 156 | return; |
| 157 | |
| 158 | cout << "Wrote file: " << Filename.c_str() << "\n"; |
| 159 | } |
| 160 | |
| 161 | bool SerializationTest::WriteAll(llvm::sys::Path& Filename) { |
| 162 | std::ofstream Out(Filename.c_str()); |
| 163 | |
| 164 | if (!Out) { |
| 165 | cerr << "Error: Cannot open " << Filename.c_str() << "\n"; |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | Writer W(Out); |
| 170 | W(*IdTable); |
| 171 | |
| 172 | W.getStream().FlushToWord(); |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | //===----------------------------------------------------------------------===// |
| 177 | // Serialization Methods. |
| 178 | //===----------------------------------------------------------------------===// |
| 179 | |
| 180 | namespace llvm { |
| 181 | |
| 182 | struct IntrospectionPrimitivesFlags { |
| 183 | enum { isPod = true, UniqueInstances = false, UniqueRefs = false }; |
| 184 | }; |
| 185 | |
| 186 | |
| 187 | template<> struct |
| 188 | IntrospectionTrait<bool>::Flags : public IntrospectionPrimitivesFlags {}; |
| 189 | |
| 190 | template<> struct |
| 191 | IntrospectionTrait<unsigned>::Flags : public IntrospectionPrimitivesFlags {}; |
| 192 | |
| 193 | template<> struct |
| 194 | IntrospectionTrait<short>::Flags : public IntrospectionPrimitivesFlags {}; |
| 195 | |
| 196 | |
| 197 | |
| 198 | template<> |
| 199 | struct IntrospectionTrait<clang::IdentifierInfo>::Flags { |
| 200 | enum { isPod = false, // Cannot copy via memcpy. Must use copy-ctor. |
| 201 | hasUniqueInstances = true, // Two pointers with different |
| 202 | // addreses point to objects |
| 203 | // that are not equal to each other. |
| 204 | hasUniqueReferences = true // Two (non-temporary) pointers |
| 205 | // will point to distinct instances. |
| 206 | }; |
| 207 | }; |
| 208 | |
| 209 | template<> template<typename Introspector> |
| 210 | struct IntrospectionTrait<clang::IdentifierInfo>::Ops<Introspector> { |
| 211 | static void Introspect(clang::IdentifierInfo& X, Introspector& I) { |
| 212 | // I(X.getTokenID()); |
| 213 | I(X.getBuiltinID(),9); // FIXME: do 9 bit specialization. |
| 214 | // I(X.getObjCKeywordID()); |
| 215 | I(X.hasMacroDefinition()); |
| 216 | I(X.isExtensionToken()); |
| 217 | I(X.isPoisoned()); |
| 218 | I(X.isOtherTargetMacro()); |
| 219 | I(X.isCPlusPlusOperatorKeyword()); |
| 220 | I(X.isNonPortableBuiltin()); |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | template<> template<> |
| 225 | struct IntrospectionTrait<clang::IdentifierTable>::Ops<Writer> { |
| 226 | static void Introspect(clang::IdentifierTable& X, Writer& W) { |
| 227 | W.EnterSubblock<clang::IdentifierTable>(1); |
| 228 | /* |
| 229 | for (clang::IdentifierTable::iterator I = X.begin(), E = X.end(); |
| 230 | I != E; ++I) |
| 231 | W(I->getValue()); |
| 232 | */ |
| 233 | W.ExitBlock(); |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | } // end namespace llvm |
| 241 | |