blob: c0ffb0c42342834d89f2eec30b48774cb289dea4 [file] [log] [blame]
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +00001//===--- DeclSerialization.cpp - Serialization of Decls ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Gabor Greif843e9342008-03-06 10:40:09 +000010// This file defines methods that implement bitcode serialization for Decls.
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +000011//
12//===----------------------------------------------------------------------===//
13
Sam Bishopf3c63ae2008-04-11 14:49:10 +000014#include "clang/AST/ASTContext.h"
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +000015#include "clang/AST/Decl.h"
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000016#include "clang/AST/DeclCXX.h"
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +000017#include "clang/AST/Expr.h"
18#include "llvm/Bitcode/Serialize.h"
19#include "llvm/Bitcode/Deserialize.h"
20
Ted Kremenek928fd7f2007-11-13 00:15:39 +000021using llvm::Serializer;
22using llvm::Deserializer;
23using llvm::SerializedPtrID;
24
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +000025using namespace clang;
26
Ted Kremenek928fd7f2007-11-13 00:15:39 +000027//===----------------------------------------------------------------------===//
28// Decl Serialization: Dispatch code to handle specialized decl types.
29//===----------------------------------------------------------------------===//
Ted Kremenek8af8fe32007-11-05 21:38:00 +000030
Ted Kremenek928fd7f2007-11-13 00:15:39 +000031void Decl::Emit(Serializer& S) const {
32 S.EmitInt(getKind());
33 EmitImpl(S);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000034 if (const DeclContext *DC = dyn_cast<const DeclContext>(this))
35 DC->EmitOutRec(S);
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +000036}
37
Sam Bishope2563ca2008-04-07 21:55:54 +000038Decl* Decl::Create(Deserializer& D, ASTContext& C) {
Ted Kremenek928fd7f2007-11-13 00:15:39 +000039
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000040 Decl *Dcl;
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000041 Kind k = static_cast<Kind>(D.ReadInt());
Sam Bishope2563ca2008-04-07 21:55:54 +000042
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000043 switch (k) {
44 default:
45 assert (false && "Not implemented.");
Chris Lattner0ed844b2008-04-04 06:12:32 +000046
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000047 case TranslationUnit:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000048 Dcl = TranslationUnitDecl::CreateImpl(D, C);
49 break;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000050
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000051 case Namespace:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000052 Dcl = NamespaceDecl::CreateImpl(D, C);
53 break;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000054
Steve Naroff248a7532008-04-15 22:42:06 +000055 case Var:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000056 Dcl = VarDecl::CreateImpl(D, C);
57 break;
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000058
Ted Kremenek583e0082007-11-14 18:12:19 +000059 case Enum:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000060 Dcl = EnumDecl::CreateImpl(D, C);
61 break;
Ted Kremenek583e0082007-11-14 18:12:19 +000062
63 case EnumConstant:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000064 Dcl = EnumConstantDecl::CreateImpl(D, C);
65 break;
Ted Kremenek583e0082007-11-14 18:12:19 +000066
Ted Kremenekf9d56c82007-11-14 17:47:01 +000067 case Field:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000068 Dcl = FieldDecl::CreateImpl(D, C);
69 break;
Ted Kremenekf9d56c82007-11-14 17:47:01 +000070
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000071 case ParmVar:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000072 Dcl = ParmVarDecl::CreateImpl(D, C);
73 break;
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000074
75 case Function:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000076 Dcl = FunctionDecl::CreateImpl(D, C);
77 break;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000078
79 case OverloadedFunction:
80 Dcl = OverloadedFunctionDecl::CreateImpl(D, C);
81 break;
82
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000083 case Record:
84 Dcl = RecordDecl::CreateImpl(D, C);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000085 break;
Ted Kremenekaad48b62007-11-14 08:06:37 +000086
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000087 case Typedef:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000088 Dcl = TypedefDecl::CreateImpl(D, C);
89 break;
Anders Carlssondfab6cb2008-02-08 00:33:21 +000090
Douglas Gregor72c3f312008-12-05 18:15:24 +000091 case TemplateTypeParm:
92 Dcl = TemplateTypeParmDecl::CreateImpl(D, C);
93 break;
94
Anders Carlssondfab6cb2008-02-08 00:33:21 +000095 case FileScopeAsm:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000096 Dcl = FileScopeAsmDecl::CreateImpl(D, C);
97 break;
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000098 }
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000099
100 if (DeclContext *DC = dyn_cast<DeclContext>(Dcl))
101 DC->ReadOutRec(D, C);
102
103 return Dcl;
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +0000104}
Ted Kremenek04973312007-11-02 18:05:11 +0000105
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000106//===----------------------------------------------------------------------===//
107// Common serialization logic for subclasses of Decl.
108//===----------------------------------------------------------------------===//
109
110void Decl::EmitInRec(Serializer& S) const {
111 S.Emit(getLocation()); // From Decl.
Ted Kremenek04973312007-11-02 18:05:11 +0000112}
113
Sam Bishope2563ca2008-04-07 21:55:54 +0000114void Decl::ReadInRec(Deserializer& D, ASTContext& C) {
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000115 Loc = SourceLocation::ReadVal(D); // From Decl.
Ted Kremenek04973312007-11-02 18:05:11 +0000116}
117
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000118//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000119// Common serialization logic for subclasses of DeclContext.
120//===----------------------------------------------------------------------===//
121
122void DeclContext::EmitOutRec(Serializer& S) const {
Douglas Gregor44b43212008-12-11 16:49:14 +0000123 S.EmitInt(Decls.size());
124 for (decl_iterator D = decls_begin(); D != decls_end(); ++D) {
125 bool Owned = ((*D)->getLexicalDeclContext() == this &&
126 DeclKind != Decl::TranslationUnit &&
127 !isFunctionOrMethod());
128 S.EmitBool(Owned);
129 if (Owned)
130 S.EmitOwnedPtr(*D);
131 else
132 S.EmitPtr(*D);
133 }
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000134}
135
136void DeclContext::ReadOutRec(Deserializer& D, ASTContext& C) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000137 unsigned NumDecls = D.ReadInt();
138 Decls.resize(NumDecls);
139 for (unsigned Idx = 0; Idx < NumDecls; ++Idx) {
140 bool Owned = D.ReadBool();
141 if (Owned)
142 Decls[Idx] = cast_or_null<ScopedDecl>(D.ReadOwnedPtr<Decl>(C));
143 else
144 D.ReadPtr<ScopedDecl>(Decls[Idx]);
145 }
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000149// Common serialization logic for subclasses of NamedDecl.
150//===----------------------------------------------------------------------===//
151
152void NamedDecl::EmitInRec(Serializer& S) const {
153 Decl::EmitInRec(S);
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000154 S.EmitInt(Name.getNameKind());
155
156 switch (Name.getNameKind()) {
157 case DeclarationName::Identifier:
158 S.EmitPtr(Name.getAsIdentifierInfo());
159 break;
160
161 case DeclarationName::ObjCZeroArgSelector:
162 case DeclarationName::ObjCOneArgSelector:
163 case DeclarationName::ObjCMultiArgSelector:
164 Name.getObjCSelector().Emit(S);
165 break;
166
167 case DeclarationName::CXXConstructorName:
168 case DeclarationName::CXXDestructorName:
169 case DeclarationName::CXXConversionFunctionName:
170 Name.getCXXNameType().Emit(S);
171 break;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000172
173 case DeclarationName::CXXOperatorName:
174 S.EmitInt(Name.getCXXOverloadedOperator());
175 break;
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000176 }
Ted Kremenek04973312007-11-02 18:05:11 +0000177}
178
Sam Bishope2563ca2008-04-07 21:55:54 +0000179void NamedDecl::ReadInRec(Deserializer& D, ASTContext& C) {
180 Decl::ReadInRec(D, C);
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000181
182 DeclarationName::NameKind Kind
183 = static_cast<DeclarationName::NameKind>(D.ReadInt());
184 switch (Kind) {
185 case DeclarationName::Identifier: {
186 IdentifierInfo *Identifier;
187 D.ReadPtr(Identifier);
188 Name = Identifier;
189 break;
190 }
191
192 case DeclarationName::ObjCZeroArgSelector:
193 case DeclarationName::ObjCOneArgSelector:
194 case DeclarationName::ObjCMultiArgSelector:
195 Name = Selector::ReadVal(D);
196 break;
197
198 case DeclarationName::CXXConstructorName:
199 Name = C.DeclarationNames.getCXXConstructorName(QualType::ReadVal(D));
200 break;
201
202 case DeclarationName::CXXDestructorName:
203 Name = C.DeclarationNames.getCXXDestructorName(QualType::ReadVal(D));
204 break;
205
206 case DeclarationName::CXXConversionFunctionName:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000207 Name
208 = C.DeclarationNames.getCXXConversionFunctionName(QualType::ReadVal(D));
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000209 break;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000210
211 case DeclarationName::CXXOperatorName: {
212 OverloadedOperatorKind Op
213 = static_cast<OverloadedOperatorKind>(D.ReadInt());
214 Name = C.DeclarationNames.getCXXOperatorName(Op);
215 break;
216 }
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000217 }
Ted Kremenek04973312007-11-02 18:05:11 +0000218}
219
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000220//===----------------------------------------------------------------------===//
221// Common serialization logic for subclasses of ScopedDecl.
222//===----------------------------------------------------------------------===//
223
224void ScopedDecl::EmitInRec(Serializer& S) const {
225 NamedDecl::EmitInRec(S);
Chris Lattnerb048c982008-04-06 04:47:34 +0000226 S.EmitPtr(cast_or_null<Decl>(getDeclContext())); // From ScopedDecl.
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000227 S.EmitPtr(cast_or_null<Decl>(getLexicalDeclContext())); // From ScopedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +0000228}
229
Sam Bishope2563ca2008-04-07 21:55:54 +0000230void ScopedDecl::ReadInRec(Deserializer& D, ASTContext& C) {
231 NamedDecl::ReadInRec(D, C);
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000232
233 assert(DeclCtx == 0);
234
235 const SerializedPtrID &SemaDCPtrID = D.ReadPtrID();
236 const SerializedPtrID &LexicalDCPtrID = D.ReadPtrID();
237
238 if (SemaDCPtrID == LexicalDCPtrID) {
239 // Allow back-patching. Observe that we register the variable of the
240 // *object* for back-patching. Its actual value will get filled in later.
241 D.ReadUIntPtr(DeclCtx, SemaDCPtrID);
242 }
243 else {
244 MultipleDC *MDC = new MultipleDC();
245 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
246 // Allow back-patching. Observe that we register the variable of the
247 // *object* for back-patching. Its actual value will get filled in later.
Argyrios Kyrtzidisb0b847e2008-11-14 23:32:45 +0000248 D.ReadPtr(MDC->SemanticDC, SemaDCPtrID);
249 D.ReadPtr(MDC->LexicalDC, LexicalDCPtrID);
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000250 }
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000251}
252
253 //===------------------------------------------------------------===//
254 // NOTE: Not all subclasses of ScopedDecl will use the "OutRec" //
255 // methods. This is because owned pointers are usually "batched" //
256 // together for efficiency. //
257 //===------------------------------------------------------------===//
258
259void ScopedDecl::EmitOutRec(Serializer& S) const {
260 S.EmitOwnedPtr(getNextDeclarator()); // From ScopedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +0000261}
262
Sam Bishope2563ca2008-04-07 21:55:54 +0000263void ScopedDecl::ReadOutRec(Deserializer& D, ASTContext& C) {
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000264 NextDeclarator =
Sam Bishope2563ca2008-04-07 21:55:54 +0000265 cast_or_null<ScopedDecl>(D.ReadOwnedPtr<Decl>(C)); // From ScopedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +0000266}
267
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000268//===----------------------------------------------------------------------===//
269// Common serialization logic for subclasses of ValueDecl.
270//===----------------------------------------------------------------------===//
271
272void ValueDecl::EmitInRec(Serializer& S) const {
273 ScopedDecl::EmitInRec(S);
274 S.Emit(getType()); // From ValueDecl.
275}
276
Sam Bishope2563ca2008-04-07 21:55:54 +0000277void ValueDecl::ReadInRec(Deserializer& D, ASTContext& C) {
278 ScopedDecl::ReadInRec(D, C);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000279 DeclType = QualType::ReadVal(D); // From ValueDecl.
280}
281
282//===----------------------------------------------------------------------===//
283// Common serialization logic for subclasses of VarDecl.
284//===----------------------------------------------------------------------===//
285
286void VarDecl::EmitInRec(Serializer& S) const {
287 ValueDecl::EmitInRec(S);
288 S.EmitInt(getStorageClass()); // From VarDecl.
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000289}
290
Sam Bishope2563ca2008-04-07 21:55:54 +0000291void VarDecl::ReadInRec(Deserializer& D, ASTContext& C) {
292 ValueDecl::ReadInRec(D, C);
Fariborz Jahaniande7b4cd2007-12-13 00:54:18 +0000293 SClass = static_cast<StorageClass>(D.ReadInt()); // From VarDecl.
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000294}
295
296 //===------------------------------------------------------------===//
297 // NOTE: VarDecl has its own "OutRec" methods that doesn't use //
298 // the one define in ScopedDecl. This is to batch emit the //
299 // owned pointers, which results in a smaller output.
300 //===------------------------------------------------------------===//
301
302void VarDecl::EmitOutRec(Serializer& S) const {
303 // Emit these last because they will create records of their own.
304 S.BatchEmitOwnedPtrs(getInit(), // From VarDecl.
305 getNextDeclarator()); // From ScopedDecl.
306}
307
Sam Bishope2563ca2008-04-07 21:55:54 +0000308void VarDecl::ReadOutRec(Deserializer& D, ASTContext& C) {
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000309 Decl* next_declarator;
310
Sam Bishope2563ca2008-04-07 21:55:54 +0000311 D.BatchReadOwnedPtrs(Init, // From VarDecl.
312 next_declarator, // From ScopedDecl.
313 C);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000314
315 setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
Ted Kremenek04973312007-11-02 18:05:11 +0000316}
317
318
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000319void VarDecl::EmitImpl(Serializer& S) const {
320 VarDecl::EmitInRec(S);
321 VarDecl::EmitOutRec(S);
Ted Kremenek04973312007-11-02 18:05:11 +0000322}
323
Sam Bishope2563ca2008-04-07 21:55:54 +0000324void VarDecl::ReadImpl(Deserializer& D, ASTContext& C) {
325 ReadInRec(D, C);
326 ReadOutRec(D, C);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000327}
328
329//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000330// TranslationUnitDecl Serialization.
331//===----------------------------------------------------------------------===//
332
333void TranslationUnitDecl::EmitImpl(llvm::Serializer& S) const
334{
335 Decl::EmitInRec(S);
336}
337
338TranslationUnitDecl* TranslationUnitDecl::CreateImpl(Deserializer& D,
339 ASTContext& C) {
340 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000341 TranslationUnitDecl* decl = new (Mem) TranslationUnitDecl();
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000342
343 decl->Decl::ReadInRec(D, C);
344
345 return decl;
346}
347
348//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000349// NamespaceDecl Serialization.
350//===----------------------------------------------------------------------===//
351
352void NamespaceDecl::EmitImpl(llvm::Serializer& S) const
353{
354 ScopedDecl::EmitInRec(S);
355 S.Emit(getLBracLoc());
356 S.Emit(getRBracLoc());
357 ScopedDecl::EmitOutRec(S);
358}
359
360NamespaceDecl* NamespaceDecl::CreateImpl(Deserializer& D, ASTContext& C) {
361 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
362 NamespaceDecl* decl = new (Mem) NamespaceDecl(0, SourceLocation(), 0);
363
364 decl->ScopedDecl::ReadInRec(D, C);
365 decl->LBracLoc = SourceLocation::ReadVal(D);
366 decl->RBracLoc = SourceLocation::ReadVal(D);
367 decl->ScopedDecl::ReadOutRec(D, C);
368
369 return decl;
370}
371
372//===----------------------------------------------------------------------===//
Steve Naroff248a7532008-04-15 22:42:06 +0000373// VarDecl Serialization.
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000374//===----------------------------------------------------------------------===//
375
Steve Naroff248a7532008-04-15 22:42:06 +0000376VarDecl* VarDecl::CreateImpl(Deserializer& D, ASTContext& C) {
377 void *Mem = C.getAllocator().Allocate<VarDecl>();
378 VarDecl* decl =
379 new (Mem) VarDecl(Var, 0, SourceLocation(), NULL, QualType(), None, NULL);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000380
Sam Bishope2563ca2008-04-07 21:55:54 +0000381 decl->VarDecl::ReadImpl(D, C);
Ted Kremenek04973312007-11-02 18:05:11 +0000382 return decl;
383}
384
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000385//===----------------------------------------------------------------------===//
Steve Naroff248a7532008-04-15 22:42:06 +0000386// ParmVarDecl Serialization.
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000387//===----------------------------------------------------------------------===//
Ted Kremenek04973312007-11-02 18:05:11 +0000388
Ted Kremenek137bd912007-12-13 06:28:13 +0000389void ParmVarDecl::EmitImpl(llvm::Serializer& S) const {
390 VarDecl::EmitImpl(S);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000391 S.EmitInt(getObjCDeclQualifier()); // From ParmVarDecl.
Chris Lattner04421082008-04-08 04:40:51 +0000392 S.EmitOwnedPtr(getDefaultArg()); // From ParmVarDecl.
Ted Kremenek137bd912007-12-13 06:28:13 +0000393}
394
Sam Bishope2563ca2008-04-07 21:55:54 +0000395ParmVarDecl* ParmVarDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000396 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
397 ParmVarDecl* decl = new (Mem)
398 ParmVarDecl(0, SourceLocation(), NULL, QualType(), None, NULL, NULL);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000399
Sam Bishope2563ca2008-04-07 21:55:54 +0000400 decl->VarDecl::ReadImpl(D, C);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000401 decl->objcDeclQualifier = static_cast<ObjCDeclQualifier>(D.ReadInt());
Chris Lattner04421082008-04-08 04:40:51 +0000402 decl->DefaultArg = D.ReadOwnedPtr<Expr>(C);
Ted Kremenek04973312007-11-02 18:05:11 +0000403 return decl;
404}
405
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000406//===----------------------------------------------------------------------===//
Ted Kremenek583e0082007-11-14 18:12:19 +0000407// EnumDecl Serialization.
408//===----------------------------------------------------------------------===//
409
410void EnumDecl::EmitImpl(Serializer& S) const {
411 ScopedDecl::EmitInRec(S);
412 S.EmitBool(isDefinition());
Douglas Gregor44b43212008-12-11 16:49:14 +0000413 S.Emit(IntegerType);
414 S.EmitOwnedPtr(getNextDeclarator());
Ted Kremenek583e0082007-11-14 18:12:19 +0000415}
416
Sam Bishope2563ca2008-04-07 21:55:54 +0000417EnumDecl* EnumDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000418 void *Mem = C.getAllocator().Allocate<EnumDecl>();
419 EnumDecl* decl = new (Mem) EnumDecl(0, SourceLocation(), NULL, NULL);
Ted Kremenek583e0082007-11-14 18:12:19 +0000420
Sam Bishope2563ca2008-04-07 21:55:54 +0000421 decl->ScopedDecl::ReadInRec(D, C);
Ted Kremenek583e0082007-11-14 18:12:19 +0000422 decl->setDefinition(D.ReadBool());
423 decl->IntegerType = QualType::ReadVal(D);
424
Douglas Gregor44b43212008-12-11 16:49:14 +0000425 Decl* next_declarator = D.ReadOwnedPtr<Decl>(C);
Ted Kremenek583e0082007-11-14 18:12:19 +0000426 decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
427
428 return decl;
429}
430
431//===----------------------------------------------------------------------===//
432// EnumConstantDecl Serialization.
433//===----------------------------------------------------------------------===//
434
435void EnumConstantDecl::EmitImpl(Serializer& S) const {
436 S.Emit(Val);
437 ValueDecl::EmitInRec(S);
438 S.BatchEmitOwnedPtrs(getNextDeclarator(),Init);
439}
440
Sam Bishope2563ca2008-04-07 21:55:54 +0000441EnumConstantDecl* EnumConstantDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Ted Kremenek049b1682007-11-14 23:38:09 +0000442 llvm::APSInt val(1);
Ted Kremenek583e0082007-11-14 18:12:19 +0000443 D.Read(val);
444
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000445 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
446 EnumConstantDecl* decl = new (Mem)
447 EnumConstantDecl(0, SourceLocation(), NULL, QualType(), NULL, val, NULL);
Ted Kremenek583e0082007-11-14 18:12:19 +0000448
Sam Bishope2563ca2008-04-07 21:55:54 +0000449 decl->ValueDecl::ReadInRec(D, C);
Ted Kremenek583e0082007-11-14 18:12:19 +0000450
451 Decl* next_declarator;
452
Sam Bishope2563ca2008-04-07 21:55:54 +0000453 D.BatchReadOwnedPtrs(next_declarator, decl->Init, C);
Ted Kremenek583e0082007-11-14 18:12:19 +0000454
Ted Kremenek049b1682007-11-14 23:38:09 +0000455 decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
Ted Kremenek583e0082007-11-14 18:12:19 +0000456
457 return decl;
458}
459
460//===----------------------------------------------------------------------===//
Ted Kremenekf9d56c82007-11-14 17:47:01 +0000461// FieldDecl Serialization.
462//===----------------------------------------------------------------------===//
463
464void FieldDecl::EmitImpl(Serializer& S) const {
Douglas Gregor44b43212008-12-11 16:49:14 +0000465 S.EmitBool(Mutable);
Ted Kremenekf9d56c82007-11-14 17:47:01 +0000466 S.Emit(getType());
Douglas Gregor44b43212008-12-11 16:49:14 +0000467 ScopedDecl::EmitInRec(S);
Ted Kremenekf9d56c82007-11-14 17:47:01 +0000468 S.EmitOwnedPtr(BitWidth);
469}
470
Sam Bishope2563ca2008-04-07 21:55:54 +0000471FieldDecl* FieldDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000472 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Douglas Gregor44b43212008-12-11 16:49:14 +0000473 FieldDecl* decl = new (Mem) FieldDecl(Field, 0, SourceLocation(), NULL,
474 QualType(), 0, false, 0);
475 decl->Mutable = D.ReadBool();
Ted Kremenek21d50e12007-11-14 22:51:02 +0000476 decl->DeclType.ReadBackpatch(D);
Sam Bishope2563ca2008-04-07 21:55:54 +0000477 decl->ReadInRec(D, C);
478 decl->BitWidth = D.ReadOwnedPtr<Expr>(C);
Ted Kremenekf9d56c82007-11-14 17:47:01 +0000479 return decl;
480}
481
482//===----------------------------------------------------------------------===//
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000483// FunctionDecl Serialization.
484//===----------------------------------------------------------------------===//
Ted Kremenek04973312007-11-02 18:05:11 +0000485
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000486void FunctionDecl::EmitImpl(Serializer& S) const {
487 S.EmitInt(SClass); // From FunctionDecl.
488 S.EmitBool(IsInline); // From FunctionDecl.
489 ValueDecl::EmitInRec(S);
Ted Kremenek3bbc1982008-05-20 03:33:58 +0000490 S.EmitPtr(PreviousDeclaration);
Ted Kremenek04973312007-11-02 18:05:11 +0000491
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000492 // NOTE: We do not need to serialize out the number of parameters, because
493 // that is encoded in the type (accessed via getNumParams()).
Ted Kremenek04973312007-11-02 18:05:11 +0000494
Ted Kremenekd437f232007-11-13 22:51:08 +0000495 if (ParamInfo != NULL) {
496 S.EmitBool(true);
Argyrios Kyrtzidisdc5ddbf2008-11-07 14:22:23 +0000497 S.EmitInt(getNumParams());
Ted Kremenekd437f232007-11-13 22:51:08 +0000498 S.BatchEmitOwnedPtrs(getNumParams(),&ParamInfo[0], Body,
499 getNextDeclarator());
500 }
501 else {
502 S.EmitBool(false);
503 S.BatchEmitOwnedPtrs(Body,getNextDeclarator());
504 }
Ted Kremenek04973312007-11-02 18:05:11 +0000505}
506
Sam Bishope2563ca2008-04-07 21:55:54 +0000507FunctionDecl* FunctionDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Ted Kremenek04973312007-11-02 18:05:11 +0000508 StorageClass SClass = static_cast<StorageClass>(D.ReadInt());
509 bool IsInline = D.ReadBool();
Ted Kremenek04973312007-11-02 18:05:11 +0000510
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000511 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
512 FunctionDecl* decl = new (Mem)
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000513 FunctionDecl(Function, 0, SourceLocation(), DeclarationName(),
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000514 QualType(), SClass, IsInline, 0);
Ted Kremenek04973312007-11-02 18:05:11 +0000515
Sam Bishope2563ca2008-04-07 21:55:54 +0000516 decl->ValueDecl::ReadInRec(D, C);
Ted Kremenek3bbc1982008-05-20 03:33:58 +0000517 D.ReadPtr(decl->PreviousDeclaration);
Ted Kremenekda256852007-11-16 18:11:10 +0000518
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000519 Decl* next_declarator;
520
Argyrios Kyrtzidisdc5ddbf2008-11-07 14:22:23 +0000521 int numParams;
Ted Kremenekd437f232007-11-13 22:51:08 +0000522 bool hasParamDecls = D.ReadBool();
Argyrios Kyrtzidisdc5ddbf2008-11-07 14:22:23 +0000523 if (hasParamDecls)
524 numParams = D.ReadInt();
Ted Kremenekda256852007-11-16 18:11:10 +0000525
526 decl->ParamInfo = hasParamDecls
Argyrios Kyrtzidisdc5ddbf2008-11-07 14:22:23 +0000527 ? new ParmVarDecl*[numParams]
Ted Kremenekda256852007-11-16 18:11:10 +0000528 : NULL;
Ted Kremenekd437f232007-11-13 22:51:08 +0000529
530 if (hasParamDecls)
Argyrios Kyrtzidisdc5ddbf2008-11-07 14:22:23 +0000531 D.BatchReadOwnedPtrs(numParams,
Ted Kremenekd437f232007-11-13 22:51:08 +0000532 reinterpret_cast<Decl**>(&decl->ParamInfo[0]),
Sam Bishope2563ca2008-04-07 21:55:54 +0000533 decl->Body, next_declarator, C);
Ted Kremenekd437f232007-11-13 22:51:08 +0000534 else
Sam Bishope2563ca2008-04-07 21:55:54 +0000535 D.BatchReadOwnedPtrs(decl->Body, next_declarator, C);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000536
537 decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
Ted Kremenek04973312007-11-02 18:05:11 +0000538
539 return decl;
540}
Ted Kremenekf7bf4112007-11-05 21:49:34 +0000541
Steve Naroff56ee6892008-10-08 17:01:13 +0000542void BlockDecl::EmitImpl(Serializer& S) const {
543 // FIXME: what about arguments?
544 S.Emit(getCaretLocation());
545 S.EmitOwnedPtr(Body);
546}
547
548BlockDecl* BlockDecl::CreateImpl(Deserializer& D, ASTContext& C) {
549 QualType Q = QualType::ReadVal(D);
550 SourceLocation L = SourceLocation::ReadVal(D);
551 /*CompoundStmt* BodyStmt = cast<CompoundStmt>(*/D.ReadOwnedPtr<Stmt>(C)/*)*/;
552 assert(0 && "Cannot deserialize BlockBlockExpr yet");
553 // FIXME: need to handle parameters.
554 //return new BlockBlockExpr(L, Q, BodyStmt);
555 return 0;
556}
557
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000558//===----------------------------------------------------------------------===//
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000559// OverloadedFunctionDecl Serialization.
560//===----------------------------------------------------------------------===//
561
562void OverloadedFunctionDecl::EmitImpl(Serializer& S) const {
563 NamedDecl::EmitInRec(S);
564
565 S.EmitInt(getNumFunctions());
566 for (unsigned func = 0; func < getNumFunctions(); ++func)
567 S.EmitPtr(Functions[func]);
568}
569
570OverloadedFunctionDecl *
571OverloadedFunctionDecl::CreateImpl(Deserializer& D, ASTContext& C) {
572 void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
573 OverloadedFunctionDecl* decl = new (Mem)
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000574 OverloadedFunctionDecl(0, DeclarationName());
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000575
576 decl->NamedDecl::ReadInRec(D, C);
577
578 unsigned numFunctions = D.ReadInt();
579 decl->Functions.reserve(numFunctions);
580 for (unsigned func = 0; func < numFunctions; ++func)
581 D.ReadPtr(decl->Functions[func]);
582
583 return decl;
584}
585
586//===----------------------------------------------------------------------===//
Ted Kremenekaad48b62007-11-14 08:06:37 +0000587// RecordDecl Serialization.
588//===----------------------------------------------------------------------===//
589
Ted Kremenek583e0082007-11-14 18:12:19 +0000590void RecordDecl::EmitImpl(Serializer& S) const {
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000591 S.EmitInt(getTagKind());
592
Ted Kremenekaad48b62007-11-14 08:06:37 +0000593 ScopedDecl::EmitInRec(S);
Ted Kremenek583e0082007-11-14 18:12:19 +0000594 S.EmitBool(isDefinition());
Ted Kremenekaad48b62007-11-14 08:06:37 +0000595 S.EmitBool(hasFlexibleArrayMember());
Douglas Gregor44b43212008-12-11 16:49:14 +0000596 ScopedDecl::EmitOutRec(S);
Ted Kremenekaad48b62007-11-14 08:06:37 +0000597}
598
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000599RecordDecl* RecordDecl::CreateImpl(Deserializer& D, ASTContext& C) {
600 TagKind TK = TagKind(D.ReadInt());
Sam Bishope2563ca2008-04-07 21:55:54 +0000601
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000602 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000603 RecordDecl* decl = new (Mem) RecordDecl(Record, TK, 0, SourceLocation(), NULL);
Ted Kremenek583e0082007-11-14 18:12:19 +0000604
Sam Bishope2563ca2008-04-07 21:55:54 +0000605 decl->ScopedDecl::ReadInRec(D, C);
Ted Kremenek583e0082007-11-14 18:12:19 +0000606 decl->setDefinition(D.ReadBool());
Ted Kremenekaad48b62007-11-14 08:06:37 +0000607 decl->setHasFlexibleArrayMember(D.ReadBool());
Douglas Gregor44b43212008-12-11 16:49:14 +0000608 decl->ScopedDecl::ReadOutRec(D, C);
609
Ted Kremenekaad48b62007-11-14 08:06:37 +0000610 return decl;
611}
612
613//===----------------------------------------------------------------------===//
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000614// TypedefDecl Serialization.
615//===----------------------------------------------------------------------===//
616
617void TypedefDecl::EmitImpl(Serializer& S) const {
Ted Kremenek2ebc89f2007-11-06 19:51:47 +0000618 S.Emit(UnderlyingType);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000619 ScopedDecl::EmitInRec(S);
620 ScopedDecl::EmitOutRec(S);
Ted Kremenekf7bf4112007-11-05 21:49:34 +0000621}
622
Sam Bishope2563ca2008-04-07 21:55:54 +0000623TypedefDecl* TypedefDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000624 QualType T = QualType::ReadVal(D);
625
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000626 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
627 TypedefDecl* decl = new (Mem) TypedefDecl(0, SourceLocation(), NULL, T, NULL);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000628
Sam Bishope2563ca2008-04-07 21:55:54 +0000629 decl->ScopedDecl::ReadInRec(D, C);
630 decl->ScopedDecl::ReadOutRec(D, C);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000631
Ted Kremenekf7bf4112007-11-05 21:49:34 +0000632 return decl;
633}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000634
635//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +0000636// TemplateTypeParmDecl Serialization.
637//===----------------------------------------------------------------------===//
638
639void TemplateTypeParmDecl::EmitImpl(Serializer& S) const {
640 S.EmitBool(Typename);
641 ScopedDecl::EmitInRec(S);
642 ScopedDecl::EmitOutRec(S);
643}
644
645TemplateTypeParmDecl *
646TemplateTypeParmDecl::CreateImpl(Deserializer& D, ASTContext& C) {
647 bool Typename = D.ReadBool();
648 void *Mem = C.getAllocator().Allocate<TemplateTypeParmDecl>();
649 TemplateTypeParmDecl *decl
650 = new (Mem) TemplateTypeParmDecl(0, SourceLocation(), NULL, Typename);
651 decl->ScopedDecl::ReadInRec(D, C);
652 decl->ScopedDecl::ReadOutRec(D, C);
653 return decl;
654}
655
656//===----------------------------------------------------------------------===//
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000657// LinkageSpec Serialization.
658//===----------------------------------------------------------------------===//
659
660void LinkageSpecDecl::EmitInRec(Serializer& S) const {
661 Decl::EmitInRec(S);
662 S.EmitInt(getLanguage());
663 S.EmitPtr(D);
664}
665
Sam Bishope2563ca2008-04-07 21:55:54 +0000666void LinkageSpecDecl::ReadInRec(Deserializer& D, ASTContext& C) {
667 Decl::ReadInRec(D, C);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000668 Language = static_cast<LanguageIDs>(D.ReadInt());
669 D.ReadPtr(this->D);
670}
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000671
672//===----------------------------------------------------------------------===//
673// FileScopeAsm Serialization.
674//===----------------------------------------------------------------------===//
675
676void FileScopeAsmDecl::EmitImpl(llvm::Serializer& S) const
677{
678 Decl::EmitInRec(S);
679 S.EmitOwnedPtr(AsmString);
680}
681
Sam Bishope2563ca2008-04-07 21:55:54 +0000682FileScopeAsmDecl* FileScopeAsmDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000683 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
684 FileScopeAsmDecl* decl = new (Mem) FileScopeAsmDecl(SourceLocation(), 0);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000685
Sam Bishope2563ca2008-04-07 21:55:54 +0000686 decl->Decl::ReadInRec(D, C);
687 decl->AsmString = cast<StringLiteral>(D.ReadOwnedPtr<Expr>(C));
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000688// D.ReadOwnedPtr(D.ReadOwnedPtr<StringLiteral>())<#T * * Ptr#>, <#bool AutoRegister#>)(decl->AsmString);
689
690 return decl;
691}