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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
//===--- DeclSerialization.cpp - Serialization of Decls ---------*- 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 files defines methods that implement bitcode serialization for Decls.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"
#include "llvm/Bitcode/Serialize.h"
#include "llvm/Bitcode/Deserialize.h"
using llvm::Serializer;
using llvm::Deserializer;
using llvm::SerializedPtrID;
using namespace clang;
//===----------------------------------------------------------------------===//
// Decl Serialization: Dispatch code to handle specialized decl types.
//===----------------------------------------------------------------------===//
void Decl::Emit(Serializer& S) const {
S.EmitInt(getKind());
EmitImpl(S);
}
Decl* Decl::Create(Deserializer& D) {
Kind k = static_cast<Kind>(D.ReadInt());
switch (k) {
default:
assert (false && "Not implemented.");
break;
case BlockVar:
return BlockVarDecl::CreateImpl(D);
case FileVar:
return FileVarDecl::CreateImpl(D);
case ParmVar:
return ParmVarDecl::CreateImpl(D);
case Function:
return FunctionDecl::CreateImpl(D);
case Typedef:
return TypedefDecl::CreateImpl(D);
}
}
//===----------------------------------------------------------------------===//
// Common serialization logic for subclasses of Decl.
//===----------------------------------------------------------------------===//
void Decl::EmitInRec(Serializer& S) const {
S.Emit(getLocation()); // From Decl.
}
void Decl::ReadInRec(Deserializer& D) {
Loc = SourceLocation::ReadVal(D); // From Decl.
}
//===----------------------------------------------------------------------===//
// Common serialization logic for subclasses of NamedDecl.
//===----------------------------------------------------------------------===//
void NamedDecl::EmitInRec(Serializer& S) const {
Decl::EmitInRec(S);
S.EmitPtr(getIdentifier()); // From NamedDecl.
}
void NamedDecl::ReadInRec(Deserializer& D) {
Decl::ReadInRec(D);
D.ReadPtr(Identifier); // From NamedDecl.
}
//===----------------------------------------------------------------------===//
// Common serialization logic for subclasses of ScopedDecl.
//===----------------------------------------------------------------------===//
void ScopedDecl::EmitInRec(Serializer& S) const {
NamedDecl::EmitInRec(S);
S.EmitPtr(getNext()); // From ScopedDecl.
}
void ScopedDecl::ReadInRec(Deserializer& D) {
NamedDecl::ReadInRec(D);
D.ReadPtr(Next); // From ScopedDecl.
}
//===------------------------------------------------------------===//
// NOTE: Not all subclasses of ScopedDecl will use the "OutRec" //
// methods. This is because owned pointers are usually "batched" //
// together for efficiency. //
//===------------------------------------------------------------===//
void ScopedDecl::EmitOutRec(Serializer& S) const {
S.EmitOwnedPtr(getNextDeclarator()); // From ScopedDecl.
}
void ScopedDecl::ReadOutRec(Deserializer& D) {
NextDeclarator =
cast_or_null<ScopedDecl>(D.ReadOwnedPtr<Decl>()); // From ScopedDecl.
}
//===----------------------------------------------------------------------===//
// Common serialization logic for subclasses of ValueDecl.
//===----------------------------------------------------------------------===//
void ValueDecl::EmitInRec(Serializer& S) const {
ScopedDecl::EmitInRec(S);
S.Emit(getType()); // From ValueDecl.
}
void ValueDecl::ReadInRec(Deserializer& D) {
ScopedDecl::ReadInRec(D);
DeclType = QualType::ReadVal(D); // From ValueDecl.
}
//===----------------------------------------------------------------------===//
// Common serialization logic for subclasses of VarDecl.
//===----------------------------------------------------------------------===//
void VarDecl::EmitInRec(Serializer& S) const {
ValueDecl::EmitInRec(S);
S.EmitInt(getStorageClass()); // From VarDecl.
S.EmitInt(getObjcDeclQualifier()); // From VarDecl.
}
void VarDecl::ReadInRec(Deserializer& D) {
ValueDecl::ReadInRec(D);
SClass = static_cast<StorageClass>(D.ReadInt()); // From VarDecl.
objcDeclQualifier = static_cast<ObjcDeclQualifier>(D.ReadInt()); // VarDecl.
}
//===------------------------------------------------------------===//
// NOTE: VarDecl has its own "OutRec" methods that doesn't use //
// the one define in ScopedDecl. This is to batch emit the //
// owned pointers, which results in a smaller output.
//===------------------------------------------------------------===//
void VarDecl::EmitOutRec(Serializer& S) const {
// Emit these last because they will create records of their own.
S.BatchEmitOwnedPtrs(getInit(), // From VarDecl.
getNextDeclarator()); // From ScopedDecl.
}
void VarDecl::ReadOutRec(Deserializer& D) {
Decl* next_declarator;
D.BatchReadOwnedPtrs(Init, // From VarDecl.
next_declarator); // From ScopedDecl.
setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
}
void VarDecl::EmitImpl(Serializer& S) const {
VarDecl::EmitInRec(S);
VarDecl::EmitOutRec(S);
}
void VarDecl::ReadImpl(Deserializer& D) {
ReadInRec(D);
ReadOutRec(D);
}
//===----------------------------------------------------------------------===//
// BlockVarDecl Serialization.
//===----------------------------------------------------------------------===//
BlockVarDecl* BlockVarDecl::CreateImpl(Deserializer& D) {
BlockVarDecl* decl =
new BlockVarDecl(SourceLocation(),NULL,QualType(),None,NULL);
decl->VarDecl::ReadImpl(D);
return decl;
}
//===----------------------------------------------------------------------===//
// FileVarDecl Serialization.
//===----------------------------------------------------------------------===//
FileVarDecl* FileVarDecl::CreateImpl(Deserializer& D) {
FileVarDecl* decl =
new FileVarDecl(SourceLocation(),NULL,QualType(),None,NULL);
decl->VarDecl::ReadImpl(D);
return decl;
}
//===----------------------------------------------------------------------===//
// ParmDecl Serialization.
//===----------------------------------------------------------------------===//
ParmVarDecl* ParmVarDecl::CreateImpl(Deserializer& D) {
ParmVarDecl* decl =
new ParmVarDecl(SourceLocation(),NULL,QualType(),None,NULL);
decl->VarDecl::ReadImpl(D);
return decl;
}
//===----------------------------------------------------------------------===//
// FunctionDecl Serialization.
//===----------------------------------------------------------------------===//
void FunctionDecl::EmitImpl(Serializer& S) const {
S.EmitInt(SClass); // From FunctionDecl.
S.EmitBool(IsInline); // From FunctionDecl.
ValueDecl::EmitInRec(S);
S.EmitPtr(DeclChain);
// NOTE: We do not need to serialize out the number of parameters, because
// that is encoded in the type (accessed via getNumParams()).
if (ParamInfo != NULL) {
S.EmitBool(true);
S.BatchEmitOwnedPtrs(getNumParams(),&ParamInfo[0], Body,
getNextDeclarator());
}
else {
S.EmitBool(false);
S.BatchEmitOwnedPtrs(Body,getNextDeclarator());
}
}
FunctionDecl* FunctionDecl::CreateImpl(Deserializer& D) {
StorageClass SClass = static_cast<StorageClass>(D.ReadInt());
bool IsInline = D.ReadBool();
FunctionDecl* decl =
new FunctionDecl(SourceLocation(),NULL,QualType(),SClass,IsInline);
decl->ValueDecl::ReadInRec(D);
D.ReadPtr(decl->DeclChain);
decl->ParamInfo = decl->getNumParams()
? new ParmVarDecl*[decl->getNumParams()]
: NULL;
Decl* next_declarator;
bool hasParamDecls = D.ReadBool();
if (hasParamDecls)
D.BatchReadOwnedPtrs(decl->getNumParams(),
reinterpret_cast<Decl**>(&decl->ParamInfo[0]),
decl->Body, next_declarator);
else
D.BatchReadOwnedPtrs(decl->Body, next_declarator);
decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
return decl;
}
//===----------------------------------------------------------------------===//
// TypedefDecl Serialization.
//===----------------------------------------------------------------------===//
void TypedefDecl::EmitImpl(Serializer& S) const {
S.Emit(UnderlyingType);
ScopedDecl::EmitInRec(S);
ScopedDecl::EmitOutRec(S);
}
TypedefDecl* TypedefDecl::CreateImpl(Deserializer& D) {
QualType T = QualType::ReadVal(D);
TypedefDecl* decl = new TypedefDecl(SourceLocation(),NULL,T,NULL);
decl->ScopedDecl::ReadInRec(D);
decl->ScopedDecl::ReadOutRec(D);
return decl;
}
|