blob: c09b0edca7b58762b0377ab3cac0b7c026d66aa2 [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
Fariborz Jahanian73da9e42008-12-20 20:56:12 +000075 case OriginalParmVar:
76 Dcl = ParmVarWithOriginalTypeDecl::CreateImpl(D, C);
77 break;
78
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000079 case Function:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000080 Dcl = FunctionDecl::CreateImpl(D, C);
81 break;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000082
83 case OverloadedFunction:
84 Dcl = OverloadedFunctionDecl::CreateImpl(D, C);
85 break;
86
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000087 case Record:
88 Dcl = RecordDecl::CreateImpl(D, C);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000089 break;
Ted Kremenekaad48b62007-11-14 08:06:37 +000090
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000091 case Typedef:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000092 Dcl = TypedefDecl::CreateImpl(D, C);
93 break;
Anders Carlssondfab6cb2008-02-08 00:33:21 +000094
Douglas Gregor72c3f312008-12-05 18:15:24 +000095 case TemplateTypeParm:
96 Dcl = TemplateTypeParmDecl::CreateImpl(D, C);
97 break;
98
Anders Carlssondfab6cb2008-02-08 00:33:21 +000099 case FileScopeAsm:
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000100 Dcl = FileScopeAsmDecl::CreateImpl(D, C);
101 break;
Ted Kremenek2ebc89f2007-11-06 19:51:47 +0000102 }
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000103
104 if (DeclContext *DC = dyn_cast<DeclContext>(Dcl))
105 DC->ReadOutRec(D, C);
106
107 return Dcl;
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +0000108}
Ted Kremenek04973312007-11-02 18:05:11 +0000109
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000110//===----------------------------------------------------------------------===//
111// Common serialization logic for subclasses of Decl.
112//===----------------------------------------------------------------------===//
113
114void Decl::EmitInRec(Serializer& S) const {
115 S.Emit(getLocation()); // From Decl.
Ted Kremenek04973312007-11-02 18:05:11 +0000116}
117
Sam Bishope2563ca2008-04-07 21:55:54 +0000118void Decl::ReadInRec(Deserializer& D, ASTContext& C) {
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000119 Loc = SourceLocation::ReadVal(D); // From Decl.
Ted Kremenek04973312007-11-02 18:05:11 +0000120}
121
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000122//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000123// Common serialization logic for subclasses of DeclContext.
124//===----------------------------------------------------------------------===//
125
126void DeclContext::EmitOutRec(Serializer& S) const {
Douglas Gregor44b43212008-12-11 16:49:14 +0000127 S.EmitInt(Decls.size());
128 for (decl_iterator D = decls_begin(); D != decls_end(); ++D) {
129 bool Owned = ((*D)->getLexicalDeclContext() == this &&
130 DeclKind != Decl::TranslationUnit &&
131 !isFunctionOrMethod());
132 S.EmitBool(Owned);
133 if (Owned)
134 S.EmitOwnedPtr(*D);
135 else
136 S.EmitPtr(*D);
137 }
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000138}
139
140void DeclContext::ReadOutRec(Deserializer& D, ASTContext& C) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000141 unsigned NumDecls = D.ReadInt();
142 Decls.resize(NumDecls);
143 for (unsigned Idx = 0; Idx < NumDecls; ++Idx) {
144 bool Owned = D.ReadBool();
145 if (Owned)
146 Decls[Idx] = cast_or_null<ScopedDecl>(D.ReadOwnedPtr<Decl>(C));
147 else
148 D.ReadPtr<ScopedDecl>(Decls[Idx]);
149 }
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000150}
151
152//===----------------------------------------------------------------------===//
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000153// Common serialization logic for subclasses of NamedDecl.
154//===----------------------------------------------------------------------===//
155
156void NamedDecl::EmitInRec(Serializer& S) const {
157 Decl::EmitInRec(S);
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000158 S.EmitInt(Name.getNameKind());
159
160 switch (Name.getNameKind()) {
161 case DeclarationName::Identifier:
162 S.EmitPtr(Name.getAsIdentifierInfo());
163 break;
164
165 case DeclarationName::ObjCZeroArgSelector:
166 case DeclarationName::ObjCOneArgSelector:
167 case DeclarationName::ObjCMultiArgSelector:
168 Name.getObjCSelector().Emit(S);
169 break;
170
171 case DeclarationName::CXXConstructorName:
172 case DeclarationName::CXXDestructorName:
173 case DeclarationName::CXXConversionFunctionName:
174 Name.getCXXNameType().Emit(S);
175 break;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000176
177 case DeclarationName::CXXOperatorName:
178 S.EmitInt(Name.getCXXOverloadedOperator());
179 break;
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000180 }
Ted Kremenek04973312007-11-02 18:05:11 +0000181}
182
Sam Bishope2563ca2008-04-07 21:55:54 +0000183void NamedDecl::ReadInRec(Deserializer& D, ASTContext& C) {
184 Decl::ReadInRec(D, C);
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000185
186 DeclarationName::NameKind Kind
187 = static_cast<DeclarationName::NameKind>(D.ReadInt());
188 switch (Kind) {
189 case DeclarationName::Identifier: {
190 IdentifierInfo *Identifier;
191 D.ReadPtr(Identifier);
192 Name = Identifier;
193 break;
194 }
195
196 case DeclarationName::ObjCZeroArgSelector:
197 case DeclarationName::ObjCOneArgSelector:
198 case DeclarationName::ObjCMultiArgSelector:
199 Name = Selector::ReadVal(D);
200 break;
201
202 case DeclarationName::CXXConstructorName:
203 Name = C.DeclarationNames.getCXXConstructorName(QualType::ReadVal(D));
204 break;
205
206 case DeclarationName::CXXDestructorName:
207 Name = C.DeclarationNames.getCXXDestructorName(QualType::ReadVal(D));
208 break;
209
210 case DeclarationName::CXXConversionFunctionName:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000211 Name
212 = C.DeclarationNames.getCXXConversionFunctionName(QualType::ReadVal(D));
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000213 break;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000214
215 case DeclarationName::CXXOperatorName: {
216 OverloadedOperatorKind Op
217 = static_cast<OverloadedOperatorKind>(D.ReadInt());
218 Name = C.DeclarationNames.getCXXOperatorName(Op);
219 break;
220 }
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000221 }
Ted Kremenek04973312007-11-02 18:05:11 +0000222}
223
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000224//===----------------------------------------------------------------------===//
225// Common serialization logic for subclasses of ScopedDecl.
226//===----------------------------------------------------------------------===//
227
228void ScopedDecl::EmitInRec(Serializer& S) const {
229 NamedDecl::EmitInRec(S);
Chris Lattnerb048c982008-04-06 04:47:34 +0000230 S.EmitPtr(cast_or_null<Decl>(getDeclContext())); // From ScopedDecl.
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000231 S.EmitPtr(cast_or_null<Decl>(getLexicalDeclContext())); // From ScopedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +0000232}
233
Sam Bishope2563ca2008-04-07 21:55:54 +0000234void ScopedDecl::ReadInRec(Deserializer& D, ASTContext& C) {
235 NamedDecl::ReadInRec(D, C);
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000236
237 assert(DeclCtx == 0);
238
239 const SerializedPtrID &SemaDCPtrID = D.ReadPtrID();
240 const SerializedPtrID &LexicalDCPtrID = D.ReadPtrID();
241
242 if (SemaDCPtrID == LexicalDCPtrID) {
243 // Allow back-patching. Observe that we register the variable of the
244 // *object* for back-patching. Its actual value will get filled in later.
245 D.ReadUIntPtr(DeclCtx, SemaDCPtrID);
246 }
247 else {
248 MultipleDC *MDC = new MultipleDC();
249 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
250 // Allow back-patching. Observe that we register the variable of the
251 // *object* for back-patching. Its actual value will get filled in later.
Argyrios Kyrtzidisb0b847e2008-11-14 23:32:45 +0000252 D.ReadPtr(MDC->SemanticDC, SemaDCPtrID);
253 D.ReadPtr(MDC->LexicalDC, LexicalDCPtrID);
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000254 }
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000255}
256
257 //===------------------------------------------------------------===//
258 // NOTE: Not all subclasses of ScopedDecl will use the "OutRec" //
259 // methods. This is because owned pointers are usually "batched" //
260 // together for efficiency. //
261 //===------------------------------------------------------------===//
262
263void ScopedDecl::EmitOutRec(Serializer& S) const {
264 S.EmitOwnedPtr(getNextDeclarator()); // From ScopedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +0000265}
266
Sam Bishope2563ca2008-04-07 21:55:54 +0000267void ScopedDecl::ReadOutRec(Deserializer& D, ASTContext& C) {
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000268 NextDeclarator =
Sam Bishope2563ca2008-04-07 21:55:54 +0000269 cast_or_null<ScopedDecl>(D.ReadOwnedPtr<Decl>(C)); // From ScopedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +0000270}
271
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000272//===----------------------------------------------------------------------===//
273// Common serialization logic for subclasses of ValueDecl.
274//===----------------------------------------------------------------------===//
275
276void ValueDecl::EmitInRec(Serializer& S) const {
277 ScopedDecl::EmitInRec(S);
278 S.Emit(getType()); // From ValueDecl.
279}
280
Sam Bishope2563ca2008-04-07 21:55:54 +0000281void ValueDecl::ReadInRec(Deserializer& D, ASTContext& C) {
282 ScopedDecl::ReadInRec(D, C);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000283 DeclType = QualType::ReadVal(D); // From ValueDecl.
284}
285
286//===----------------------------------------------------------------------===//
287// Common serialization logic for subclasses of VarDecl.
288//===----------------------------------------------------------------------===//
289
290void VarDecl::EmitInRec(Serializer& S) const {
291 ValueDecl::EmitInRec(S);
292 S.EmitInt(getStorageClass()); // From VarDecl.
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000293}
294
Sam Bishope2563ca2008-04-07 21:55:54 +0000295void VarDecl::ReadInRec(Deserializer& D, ASTContext& C) {
296 ValueDecl::ReadInRec(D, C);
Fariborz Jahaniande7b4cd2007-12-13 00:54:18 +0000297 SClass = static_cast<StorageClass>(D.ReadInt()); // From VarDecl.
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000298}
299
300 //===------------------------------------------------------------===//
301 // NOTE: VarDecl has its own "OutRec" methods that doesn't use //
302 // the one define in ScopedDecl. This is to batch emit the //
303 // owned pointers, which results in a smaller output.
304 //===------------------------------------------------------------===//
305
306void VarDecl::EmitOutRec(Serializer& S) const {
307 // Emit these last because they will create records of their own.
308 S.BatchEmitOwnedPtrs(getInit(), // From VarDecl.
309 getNextDeclarator()); // From ScopedDecl.
310}
311
Sam Bishope2563ca2008-04-07 21:55:54 +0000312void VarDecl::ReadOutRec(Deserializer& D, ASTContext& C) {
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000313 Decl* next_declarator;
314
Sam Bishope2563ca2008-04-07 21:55:54 +0000315 D.BatchReadOwnedPtrs(Init, // From VarDecl.
316 next_declarator, // From ScopedDecl.
317 C);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000318
319 setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
Ted Kremenek04973312007-11-02 18:05:11 +0000320}
321
322
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000323void VarDecl::EmitImpl(Serializer& S) const {
324 VarDecl::EmitInRec(S);
325 VarDecl::EmitOutRec(S);
Ted Kremenek04973312007-11-02 18:05:11 +0000326}
327
Sam Bishope2563ca2008-04-07 21:55:54 +0000328void VarDecl::ReadImpl(Deserializer& D, ASTContext& C) {
329 ReadInRec(D, C);
330 ReadOutRec(D, C);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000331}
332
333//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000334// TranslationUnitDecl Serialization.
335//===----------------------------------------------------------------------===//
336
337void TranslationUnitDecl::EmitImpl(llvm::Serializer& S) const
338{
339 Decl::EmitInRec(S);
340}
341
342TranslationUnitDecl* TranslationUnitDecl::CreateImpl(Deserializer& D,
343 ASTContext& C) {
344 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000345 TranslationUnitDecl* decl = new (Mem) TranslationUnitDecl();
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000346
347 decl->Decl::ReadInRec(D, C);
348
349 return decl;
350}
351
352//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000353// NamespaceDecl Serialization.
354//===----------------------------------------------------------------------===//
355
356void NamespaceDecl::EmitImpl(llvm::Serializer& S) const
357{
358 ScopedDecl::EmitInRec(S);
359 S.Emit(getLBracLoc());
360 S.Emit(getRBracLoc());
361 ScopedDecl::EmitOutRec(S);
362}
363
364NamespaceDecl* NamespaceDecl::CreateImpl(Deserializer& D, ASTContext& C) {
365 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
366 NamespaceDecl* decl = new (Mem) NamespaceDecl(0, SourceLocation(), 0);
367
368 decl->ScopedDecl::ReadInRec(D, C);
369 decl->LBracLoc = SourceLocation::ReadVal(D);
370 decl->RBracLoc = SourceLocation::ReadVal(D);
371 decl->ScopedDecl::ReadOutRec(D, C);
372
373 return decl;
374}
375
376//===----------------------------------------------------------------------===//
Steve Naroff248a7532008-04-15 22:42:06 +0000377// VarDecl Serialization.
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000378//===----------------------------------------------------------------------===//
379
Steve Naroff248a7532008-04-15 22:42:06 +0000380VarDecl* VarDecl::CreateImpl(Deserializer& D, ASTContext& C) {
381 void *Mem = C.getAllocator().Allocate<VarDecl>();
382 VarDecl* decl =
383 new (Mem) VarDecl(Var, 0, SourceLocation(), NULL, QualType(), None, NULL);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000384
Sam Bishope2563ca2008-04-07 21:55:54 +0000385 decl->VarDecl::ReadImpl(D, C);
Ted Kremenek04973312007-11-02 18:05:11 +0000386 return decl;
387}
388
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000389//===----------------------------------------------------------------------===//
Steve Naroff248a7532008-04-15 22:42:06 +0000390// ParmVarDecl Serialization.
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000391//===----------------------------------------------------------------------===//
Ted Kremenek04973312007-11-02 18:05:11 +0000392
Ted Kremenek137bd912007-12-13 06:28:13 +0000393void ParmVarDecl::EmitImpl(llvm::Serializer& S) const {
394 VarDecl::EmitImpl(S);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000395 S.EmitInt(getObjCDeclQualifier()); // From ParmVarDecl.
Chris Lattner04421082008-04-08 04:40:51 +0000396 S.EmitOwnedPtr(getDefaultArg()); // From ParmVarDecl.
Ted Kremenek137bd912007-12-13 06:28:13 +0000397}
398
Sam Bishope2563ca2008-04-07 21:55:54 +0000399ParmVarDecl* ParmVarDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000400 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
401 ParmVarDecl* decl = new (Mem)
402 ParmVarDecl(0, SourceLocation(), NULL, QualType(), None, NULL, NULL);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000403
Sam Bishope2563ca2008-04-07 21:55:54 +0000404 decl->VarDecl::ReadImpl(D, C);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000405 decl->objcDeclQualifier = static_cast<ObjCDeclQualifier>(D.ReadInt());
Chris Lattner04421082008-04-08 04:40:51 +0000406 decl->DefaultArg = D.ReadOwnedPtr<Expr>(C);
Ted Kremenek04973312007-11-02 18:05:11 +0000407 return decl;
408}
409
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000410//===----------------------------------------------------------------------===//
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000411// ParmVarWithOriginalTypeDecl Serialization.
412//===----------------------------------------------------------------------===//
413
414void ParmVarWithOriginalTypeDecl::EmitImpl(llvm::Serializer& S) const {
415 ParmVarDecl::EmitImpl(S);
416 S.Emit(OriginalType);
417}
418
419ParmVarWithOriginalTypeDecl* ParmVarWithOriginalTypeDecl::CreateImpl(
420 Deserializer& D, ASTContext& C) {
421 void *Mem = C.getAllocator().Allocate<ParmVarWithOriginalTypeDecl>();
422 ParmVarWithOriginalTypeDecl* decl = new (Mem)
423 ParmVarWithOriginalTypeDecl(0, SourceLocation(), NULL, QualType(),
424 QualType(), None, NULL, NULL);
425
426 decl->ParmVarDecl::ReadImpl(D, C);
427 decl->OriginalType = QualType::ReadVal(D);
428 return decl;
429}
430//===----------------------------------------------------------------------===//
Ted Kremenek583e0082007-11-14 18:12:19 +0000431// EnumDecl Serialization.
432//===----------------------------------------------------------------------===//
433
434void EnumDecl::EmitImpl(Serializer& S) const {
435 ScopedDecl::EmitInRec(S);
436 S.EmitBool(isDefinition());
Douglas Gregor44b43212008-12-11 16:49:14 +0000437 S.Emit(IntegerType);
438 S.EmitOwnedPtr(getNextDeclarator());
Ted Kremenek583e0082007-11-14 18:12:19 +0000439}
440
Sam Bishope2563ca2008-04-07 21:55:54 +0000441EnumDecl* EnumDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000442 void *Mem = C.getAllocator().Allocate<EnumDecl>();
443 EnumDecl* decl = new (Mem) EnumDecl(0, SourceLocation(), NULL, NULL);
Ted Kremenek583e0082007-11-14 18:12:19 +0000444
Sam Bishope2563ca2008-04-07 21:55:54 +0000445 decl->ScopedDecl::ReadInRec(D, C);
Ted Kremenek583e0082007-11-14 18:12:19 +0000446 decl->setDefinition(D.ReadBool());
447 decl->IntegerType = QualType::ReadVal(D);
448
Douglas Gregor44b43212008-12-11 16:49:14 +0000449 Decl* next_declarator = D.ReadOwnedPtr<Decl>(C);
Ted Kremenek583e0082007-11-14 18:12:19 +0000450 decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
451
452 return decl;
453}
454
455//===----------------------------------------------------------------------===//
456// EnumConstantDecl Serialization.
457//===----------------------------------------------------------------------===//
458
459void EnumConstantDecl::EmitImpl(Serializer& S) const {
460 S.Emit(Val);
461 ValueDecl::EmitInRec(S);
462 S.BatchEmitOwnedPtrs(getNextDeclarator(),Init);
463}
464
Sam Bishope2563ca2008-04-07 21:55:54 +0000465EnumConstantDecl* EnumConstantDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Ted Kremenek049b1682007-11-14 23:38:09 +0000466 llvm::APSInt val(1);
Ted Kremenek583e0082007-11-14 18:12:19 +0000467 D.Read(val);
468
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000469 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
470 EnumConstantDecl* decl = new (Mem)
471 EnumConstantDecl(0, SourceLocation(), NULL, QualType(), NULL, val, NULL);
Ted Kremenek583e0082007-11-14 18:12:19 +0000472
Sam Bishope2563ca2008-04-07 21:55:54 +0000473 decl->ValueDecl::ReadInRec(D, C);
Ted Kremenek583e0082007-11-14 18:12:19 +0000474
475 Decl* next_declarator;
476
Sam Bishope2563ca2008-04-07 21:55:54 +0000477 D.BatchReadOwnedPtrs(next_declarator, decl->Init, C);
Ted Kremenek583e0082007-11-14 18:12:19 +0000478
Ted Kremenek049b1682007-11-14 23:38:09 +0000479 decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
Ted Kremenek583e0082007-11-14 18:12:19 +0000480
481 return decl;
482}
483
484//===----------------------------------------------------------------------===//
Ted Kremenekf9d56c82007-11-14 17:47:01 +0000485// FieldDecl Serialization.
486//===----------------------------------------------------------------------===//
487
488void FieldDecl::EmitImpl(Serializer& S) const {
Douglas Gregor44b43212008-12-11 16:49:14 +0000489 S.EmitBool(Mutable);
Ted Kremenekf9d56c82007-11-14 17:47:01 +0000490 S.Emit(getType());
Douglas Gregor44b43212008-12-11 16:49:14 +0000491 ScopedDecl::EmitInRec(S);
Ted Kremenekf9d56c82007-11-14 17:47:01 +0000492 S.EmitOwnedPtr(BitWidth);
493}
494
Sam Bishope2563ca2008-04-07 21:55:54 +0000495FieldDecl* FieldDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000496 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Douglas Gregor44b43212008-12-11 16:49:14 +0000497 FieldDecl* decl = new (Mem) FieldDecl(Field, 0, SourceLocation(), NULL,
498 QualType(), 0, false, 0);
499 decl->Mutable = D.ReadBool();
Ted Kremenek21d50e12007-11-14 22:51:02 +0000500 decl->DeclType.ReadBackpatch(D);
Sam Bishope2563ca2008-04-07 21:55:54 +0000501 decl->ReadInRec(D, C);
502 decl->BitWidth = D.ReadOwnedPtr<Expr>(C);
Ted Kremenekf9d56c82007-11-14 17:47:01 +0000503 return decl;
504}
505
506//===----------------------------------------------------------------------===//
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000507// FunctionDecl Serialization.
508//===----------------------------------------------------------------------===//
Ted Kremenek04973312007-11-02 18:05:11 +0000509
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000510void FunctionDecl::EmitImpl(Serializer& S) const {
511 S.EmitInt(SClass); // From FunctionDecl.
512 S.EmitBool(IsInline); // From FunctionDecl.
513 ValueDecl::EmitInRec(S);
Ted Kremenek3bbc1982008-05-20 03:33:58 +0000514 S.EmitPtr(PreviousDeclaration);
Ted Kremenek04973312007-11-02 18:05:11 +0000515
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000516 // NOTE: We do not need to serialize out the number of parameters, because
517 // that is encoded in the type (accessed via getNumParams()).
Ted Kremenek04973312007-11-02 18:05:11 +0000518
Ted Kremenekd437f232007-11-13 22:51:08 +0000519 if (ParamInfo != NULL) {
520 S.EmitBool(true);
Argyrios Kyrtzidisdc5ddbf2008-11-07 14:22:23 +0000521 S.EmitInt(getNumParams());
Ted Kremenekd437f232007-11-13 22:51:08 +0000522 S.BatchEmitOwnedPtrs(getNumParams(),&ParamInfo[0], Body,
523 getNextDeclarator());
524 }
525 else {
526 S.EmitBool(false);
527 S.BatchEmitOwnedPtrs(Body,getNextDeclarator());
528 }
Ted Kremenek04973312007-11-02 18:05:11 +0000529}
530
Sam Bishope2563ca2008-04-07 21:55:54 +0000531FunctionDecl* FunctionDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Ted Kremenek04973312007-11-02 18:05:11 +0000532 StorageClass SClass = static_cast<StorageClass>(D.ReadInt());
533 bool IsInline = D.ReadBool();
Ted Kremenek04973312007-11-02 18:05:11 +0000534
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000535 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
536 FunctionDecl* decl = new (Mem)
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000537 FunctionDecl(Function, 0, SourceLocation(), DeclarationName(),
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000538 QualType(), SClass, IsInline, 0);
Ted Kremenek04973312007-11-02 18:05:11 +0000539
Sam Bishope2563ca2008-04-07 21:55:54 +0000540 decl->ValueDecl::ReadInRec(D, C);
Ted Kremenek3bbc1982008-05-20 03:33:58 +0000541 D.ReadPtr(decl->PreviousDeclaration);
Ted Kremenekda256852007-11-16 18:11:10 +0000542
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000543 Decl* next_declarator;
544
Argyrios Kyrtzidisdc5ddbf2008-11-07 14:22:23 +0000545 int numParams;
Ted Kremenekd437f232007-11-13 22:51:08 +0000546 bool hasParamDecls = D.ReadBool();
Argyrios Kyrtzidisdc5ddbf2008-11-07 14:22:23 +0000547 if (hasParamDecls)
548 numParams = D.ReadInt();
Ted Kremenekda256852007-11-16 18:11:10 +0000549
550 decl->ParamInfo = hasParamDecls
Argyrios Kyrtzidisdc5ddbf2008-11-07 14:22:23 +0000551 ? new ParmVarDecl*[numParams]
Ted Kremenekda256852007-11-16 18:11:10 +0000552 : NULL;
Ted Kremenekd437f232007-11-13 22:51:08 +0000553
554 if (hasParamDecls)
Argyrios Kyrtzidisdc5ddbf2008-11-07 14:22:23 +0000555 D.BatchReadOwnedPtrs(numParams,
Ted Kremenekd437f232007-11-13 22:51:08 +0000556 reinterpret_cast<Decl**>(&decl->ParamInfo[0]),
Sam Bishope2563ca2008-04-07 21:55:54 +0000557 decl->Body, next_declarator, C);
Ted Kremenekd437f232007-11-13 22:51:08 +0000558 else
Sam Bishope2563ca2008-04-07 21:55:54 +0000559 D.BatchReadOwnedPtrs(decl->Body, next_declarator, C);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000560
561 decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
Ted Kremenek04973312007-11-02 18:05:11 +0000562
563 return decl;
564}
Ted Kremenekf7bf4112007-11-05 21:49:34 +0000565
Steve Naroff56ee6892008-10-08 17:01:13 +0000566void BlockDecl::EmitImpl(Serializer& S) const {
567 // FIXME: what about arguments?
568 S.Emit(getCaretLocation());
569 S.EmitOwnedPtr(Body);
570}
571
572BlockDecl* BlockDecl::CreateImpl(Deserializer& D, ASTContext& C) {
573 QualType Q = QualType::ReadVal(D);
574 SourceLocation L = SourceLocation::ReadVal(D);
575 /*CompoundStmt* BodyStmt = cast<CompoundStmt>(*/D.ReadOwnedPtr<Stmt>(C)/*)*/;
576 assert(0 && "Cannot deserialize BlockBlockExpr yet");
577 // FIXME: need to handle parameters.
578 //return new BlockBlockExpr(L, Q, BodyStmt);
579 return 0;
580}
581
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000582//===----------------------------------------------------------------------===//
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000583// OverloadedFunctionDecl Serialization.
584//===----------------------------------------------------------------------===//
585
586void OverloadedFunctionDecl::EmitImpl(Serializer& S) const {
587 NamedDecl::EmitInRec(S);
588
589 S.EmitInt(getNumFunctions());
590 for (unsigned func = 0; func < getNumFunctions(); ++func)
591 S.EmitPtr(Functions[func]);
592}
593
594OverloadedFunctionDecl *
595OverloadedFunctionDecl::CreateImpl(Deserializer& D, ASTContext& C) {
596 void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
597 OverloadedFunctionDecl* decl = new (Mem)
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000598 OverloadedFunctionDecl(0, DeclarationName());
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000599
600 decl->NamedDecl::ReadInRec(D, C);
601
602 unsigned numFunctions = D.ReadInt();
603 decl->Functions.reserve(numFunctions);
604 for (unsigned func = 0; func < numFunctions; ++func)
605 D.ReadPtr(decl->Functions[func]);
606
607 return decl;
608}
609
610//===----------------------------------------------------------------------===//
Ted Kremenekaad48b62007-11-14 08:06:37 +0000611// RecordDecl Serialization.
612//===----------------------------------------------------------------------===//
613
Ted Kremenek583e0082007-11-14 18:12:19 +0000614void RecordDecl::EmitImpl(Serializer& S) const {
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000615 S.EmitInt(getTagKind());
616
Ted Kremenekaad48b62007-11-14 08:06:37 +0000617 ScopedDecl::EmitInRec(S);
Ted Kremenek583e0082007-11-14 18:12:19 +0000618 S.EmitBool(isDefinition());
Ted Kremenekaad48b62007-11-14 08:06:37 +0000619 S.EmitBool(hasFlexibleArrayMember());
Douglas Gregor44b43212008-12-11 16:49:14 +0000620 ScopedDecl::EmitOutRec(S);
Ted Kremenekaad48b62007-11-14 08:06:37 +0000621}
622
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000623RecordDecl* RecordDecl::CreateImpl(Deserializer& D, ASTContext& C) {
624 TagKind TK = TagKind(D.ReadInt());
Sam Bishope2563ca2008-04-07 21:55:54 +0000625
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000626 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000627 RecordDecl* decl = new (Mem) RecordDecl(Record, TK, 0, SourceLocation(), NULL);
Ted Kremenek583e0082007-11-14 18:12:19 +0000628
Sam Bishope2563ca2008-04-07 21:55:54 +0000629 decl->ScopedDecl::ReadInRec(D, C);
Ted Kremenek583e0082007-11-14 18:12:19 +0000630 decl->setDefinition(D.ReadBool());
Ted Kremenekaad48b62007-11-14 08:06:37 +0000631 decl->setHasFlexibleArrayMember(D.ReadBool());
Douglas Gregor44b43212008-12-11 16:49:14 +0000632 decl->ScopedDecl::ReadOutRec(D, C);
633
Ted Kremenekaad48b62007-11-14 08:06:37 +0000634 return decl;
635}
636
637//===----------------------------------------------------------------------===//
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000638// TypedefDecl Serialization.
639//===----------------------------------------------------------------------===//
640
641void TypedefDecl::EmitImpl(Serializer& S) const {
Ted Kremenek2ebc89f2007-11-06 19:51:47 +0000642 S.Emit(UnderlyingType);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000643 ScopedDecl::EmitInRec(S);
644 ScopedDecl::EmitOutRec(S);
Ted Kremenekf7bf4112007-11-05 21:49:34 +0000645}
646
Sam Bishope2563ca2008-04-07 21:55:54 +0000647TypedefDecl* TypedefDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000648 QualType T = QualType::ReadVal(D);
649
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000650 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
651 TypedefDecl* decl = new (Mem) TypedefDecl(0, SourceLocation(), NULL, T, NULL);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000652
Sam Bishope2563ca2008-04-07 21:55:54 +0000653 decl->ScopedDecl::ReadInRec(D, C);
654 decl->ScopedDecl::ReadOutRec(D, C);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000655
Ted Kremenekf7bf4112007-11-05 21:49:34 +0000656 return decl;
657}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000658
659//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +0000660// TemplateTypeParmDecl Serialization.
661//===----------------------------------------------------------------------===//
662
663void TemplateTypeParmDecl::EmitImpl(Serializer& S) const {
664 S.EmitBool(Typename);
665 ScopedDecl::EmitInRec(S);
666 ScopedDecl::EmitOutRec(S);
667}
668
669TemplateTypeParmDecl *
670TemplateTypeParmDecl::CreateImpl(Deserializer& D, ASTContext& C) {
671 bool Typename = D.ReadBool();
672 void *Mem = C.getAllocator().Allocate<TemplateTypeParmDecl>();
673 TemplateTypeParmDecl *decl
674 = new (Mem) TemplateTypeParmDecl(0, SourceLocation(), NULL, Typename);
675 decl->ScopedDecl::ReadInRec(D, C);
676 decl->ScopedDecl::ReadOutRec(D, C);
677 return decl;
678}
679
680//===----------------------------------------------------------------------===//
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000681// LinkageSpec Serialization.
682//===----------------------------------------------------------------------===//
683
684void LinkageSpecDecl::EmitInRec(Serializer& S) const {
685 Decl::EmitInRec(S);
686 S.EmitInt(getLanguage());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000687 S.EmitBool(HadBraces);
688 if (HadBraces) {
689 S.EmitInt(NumDecls);
690 for (decl_const_iterator D = decls_begin(), DEnd = decls_end();
691 D != DEnd; ++D)
692 S.EmitPtr(*D);
693 } else {
694 S.EmitPtr((Decl*)Decls);
695 }
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000696}
697
Sam Bishope2563ca2008-04-07 21:55:54 +0000698void LinkageSpecDecl::ReadInRec(Deserializer& D, ASTContext& C) {
699 Decl::ReadInRec(D, C);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000700 Language = static_cast<LanguageIDs>(D.ReadInt());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000701 HadBraces = D.ReadBool();
702 if (HadBraces) {
703 NumDecls = D.ReadInt();
704 Decl **NewDecls = new Decl*[NumDecls];
705 Decls = NewDecls;
706 for (unsigned I = 0; I < NumDecls; ++I)
707 D.ReadPtr(NewDecls[I]);
708 } else {
709 D.ReadPtr(this->Decls);
710 }
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000711}
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000712
713//===----------------------------------------------------------------------===//
714// FileScopeAsm Serialization.
715//===----------------------------------------------------------------------===//
716
717void FileScopeAsmDecl::EmitImpl(llvm::Serializer& S) const
718{
719 Decl::EmitInRec(S);
720 S.EmitOwnedPtr(AsmString);
721}
722
Sam Bishope2563ca2008-04-07 21:55:54 +0000723FileScopeAsmDecl* FileScopeAsmDecl::CreateImpl(Deserializer& D, ASTContext& C) {
Sam Bishopf3c63ae2008-04-11 14:49:10 +0000724 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
725 FileScopeAsmDecl* decl = new (Mem) FileScopeAsmDecl(SourceLocation(), 0);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000726
Sam Bishope2563ca2008-04-07 21:55:54 +0000727 decl->Decl::ReadInRec(D, C);
728 decl->AsmString = cast<StringLiteral>(D.ReadOwnedPtr<Expr>(C));
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000729// D.ReadOwnedPtr(D.ReadOwnedPtr<StringLiteral>())<#T * * Ptr#>, <#bool AutoRegister#>)(decl->AsmString);
730
731 return decl;
732}