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