blob: b881cbe5b50436ad1ecaae5d351939fe35533cd5 [file] [log] [blame]
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +00001//===--- DeclSerialization.cpp - Serialization of Decls ---------*- C++ -*-===//
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 files defines methods that implement bitcode serialization for Decls.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
15#include "clang/AST/Expr.h"
16#include "llvm/Bitcode/Serialize.h"
17#include "llvm/Bitcode/Deserialize.h"
18
Ted Kremenek928fd7f2007-11-13 00:15:39 +000019using llvm::Serializer;
20using llvm::Deserializer;
21using llvm::SerializedPtrID;
22
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +000023using namespace clang;
24
Ted Kremenek928fd7f2007-11-13 00:15:39 +000025//===----------------------------------------------------------------------===//
26// Decl Serialization: Dispatch code to handle specialized decl types.
27//===----------------------------------------------------------------------===//
Ted Kremenek8af8fe32007-11-05 21:38:00 +000028
Ted Kremenek928fd7f2007-11-13 00:15:39 +000029void Decl::Emit(Serializer& S) const {
30 S.EmitInt(getKind());
31 EmitImpl(S);
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +000032}
33
Ted Kremenek928fd7f2007-11-13 00:15:39 +000034Decl* Decl::Create(Deserializer& D) {
35
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000036 Kind k = static_cast<Kind>(D.ReadInt());
37
38 switch (k) {
39 default:
40 assert (false && "Not implemented.");
41 break;
42
43 case BlockVar:
Ted Kremenek928fd7f2007-11-13 00:15:39 +000044 return BlockVarDecl::CreateImpl(D);
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000045
46 case FileVar:
Ted Kremenek928fd7f2007-11-13 00:15:39 +000047 return FileVarDecl::CreateImpl(D);
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000048
49 case ParmVar:
Ted Kremenek928fd7f2007-11-13 00:15:39 +000050 return ParmVarDecl::CreateImpl(D);
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000051
52 case Function:
Ted Kremenek928fd7f2007-11-13 00:15:39 +000053 return FunctionDecl::CreateImpl(D);
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000054
55 case Typedef:
Ted Kremenek928fd7f2007-11-13 00:15:39 +000056 return TypedefDecl::CreateImpl(D);
Ted Kremenek2ebc89f2007-11-06 19:51:47 +000057 }
Ted Kremenek2f1f8cb2007-10-25 21:37:16 +000058}
Ted Kremenek04973312007-11-02 18:05:11 +000059
Ted Kremenek928fd7f2007-11-13 00:15:39 +000060//===----------------------------------------------------------------------===//
61// Common serialization logic for subclasses of Decl.
62//===----------------------------------------------------------------------===//
63
64void Decl::EmitInRec(Serializer& S) const {
65 S.Emit(getLocation()); // From Decl.
Ted Kremenek04973312007-11-02 18:05:11 +000066}
67
Ted Kremenek928fd7f2007-11-13 00:15:39 +000068void Decl::ReadInRec(Deserializer& D) {
69 Loc = SourceLocation::ReadVal(D); // From Decl.
Ted Kremenek04973312007-11-02 18:05:11 +000070}
71
Ted Kremenek928fd7f2007-11-13 00:15:39 +000072//===----------------------------------------------------------------------===//
73// Common serialization logic for subclasses of NamedDecl.
74//===----------------------------------------------------------------------===//
75
76void NamedDecl::EmitInRec(Serializer& S) const {
77 Decl::EmitInRec(S);
78 S.EmitPtr(getIdentifier()); // From NamedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +000079}
80
Ted Kremenek928fd7f2007-11-13 00:15:39 +000081void NamedDecl::ReadInRec(Deserializer& D) {
82 Decl::ReadInRec(D);
83 D.ReadPtr(Identifier); // From NamedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +000084}
85
Ted Kremenek928fd7f2007-11-13 00:15:39 +000086//===----------------------------------------------------------------------===//
87// Common serialization logic for subclasses of ScopedDecl.
88//===----------------------------------------------------------------------===//
89
90void ScopedDecl::EmitInRec(Serializer& S) const {
91 NamedDecl::EmitInRec(S);
92 S.EmitPtr(getNext()); // From ScopedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +000093}
94
Ted Kremenek928fd7f2007-11-13 00:15:39 +000095void ScopedDecl::ReadInRec(Deserializer& D) {
96 NamedDecl::ReadInRec(D);
97 D.ReadPtr(Next); // From ScopedDecl.
98}
99
100 //===------------------------------------------------------------===//
101 // NOTE: Not all subclasses of ScopedDecl will use the "OutRec" //
102 // methods. This is because owned pointers are usually "batched" //
103 // together for efficiency. //
104 //===------------------------------------------------------------===//
105
106void ScopedDecl::EmitOutRec(Serializer& S) const {
107 S.EmitOwnedPtr(getNextDeclarator()); // From ScopedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +0000108}
109
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000110void ScopedDecl::ReadOutRec(Deserializer& D) {
111 NextDeclarator =
112 cast_or_null<ScopedDecl>(D.ReadOwnedPtr<Decl>()); // From ScopedDecl.
Ted Kremenek04973312007-11-02 18:05:11 +0000113}
114
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000115//===----------------------------------------------------------------------===//
116// Common serialization logic for subclasses of ValueDecl.
117//===----------------------------------------------------------------------===//
118
119void ValueDecl::EmitInRec(Serializer& S) const {
120 ScopedDecl::EmitInRec(S);
121 S.Emit(getType()); // From ValueDecl.
122}
123
124void ValueDecl::ReadInRec(Deserializer& D) {
125 ScopedDecl::ReadInRec(D);
126 DeclType = QualType::ReadVal(D); // From ValueDecl.
127}
128
129//===----------------------------------------------------------------------===//
130// Common serialization logic for subclasses of VarDecl.
131//===----------------------------------------------------------------------===//
132
133void VarDecl::EmitInRec(Serializer& S) const {
134 ValueDecl::EmitInRec(S);
135 S.EmitInt(getStorageClass()); // From VarDecl.
136 S.EmitInt(getObjcDeclQualifier()); // From VarDecl.
137}
138
139void VarDecl::ReadInRec(Deserializer& D) {
140 ValueDecl::ReadInRec(D);
141 SClass = static_cast<StorageClass>(D.ReadInt()); // From VarDecl.
142 objcDeclQualifier = static_cast<ObjcDeclQualifier>(D.ReadInt()); // VarDecl.
143}
144
145 //===------------------------------------------------------------===//
146 // NOTE: VarDecl has its own "OutRec" methods that doesn't use //
147 // the one define in ScopedDecl. This is to batch emit the //
148 // owned pointers, which results in a smaller output.
149 //===------------------------------------------------------------===//
150
151void VarDecl::EmitOutRec(Serializer& S) const {
152 // Emit these last because they will create records of their own.
153 S.BatchEmitOwnedPtrs(getInit(), // From VarDecl.
154 getNextDeclarator()); // From ScopedDecl.
155}
156
157void VarDecl::ReadOutRec(Deserializer& D) {
158 Decl* next_declarator;
159
160 D.BatchReadOwnedPtrs(Init, // From VarDecl.
161 next_declarator); // From ScopedDecl.
162
163 setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
Ted Kremenek04973312007-11-02 18:05:11 +0000164}
165
166
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000167void VarDecl::EmitImpl(Serializer& S) const {
168 VarDecl::EmitInRec(S);
169 VarDecl::EmitOutRec(S);
Ted Kremenek04973312007-11-02 18:05:11 +0000170}
171
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000172void VarDecl::ReadImpl(Deserializer& D) {
173 ReadInRec(D);
174 ReadOutRec(D);
175}
176
177//===----------------------------------------------------------------------===//
178// BlockVarDecl Serialization.
179//===----------------------------------------------------------------------===//
180
181BlockVarDecl* BlockVarDecl::CreateImpl(Deserializer& D) {
182 BlockVarDecl* decl =
183 new BlockVarDecl(SourceLocation(),NULL,QualType(),None,NULL);
184
185 decl->VarDecl::ReadImpl(D);
186
Ted Kremenek04973312007-11-02 18:05:11 +0000187 return decl;
188}
189
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000190//===----------------------------------------------------------------------===//
191// FileVarDecl Serialization.
192//===----------------------------------------------------------------------===//
Ted Kremenek04973312007-11-02 18:05:11 +0000193
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000194FileVarDecl* FileVarDecl::CreateImpl(Deserializer& D) {
195 FileVarDecl* decl =
196 new FileVarDecl(SourceLocation(),NULL,QualType(),None,NULL);
197
198 decl->VarDecl::ReadImpl(D);
199
Ted Kremenek04973312007-11-02 18:05:11 +0000200 return decl;
201}
202
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000203//===----------------------------------------------------------------------===//
204// ParmDecl Serialization.
205//===----------------------------------------------------------------------===//
Ted Kremenek04973312007-11-02 18:05:11 +0000206
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000207ParmVarDecl* ParmVarDecl::CreateImpl(Deserializer& D) {
208 ParmVarDecl* decl =
209 new ParmVarDecl(SourceLocation(),NULL,QualType(),None,NULL);
210
211 decl->VarDecl::ReadImpl(D);
212
Ted Kremenek04973312007-11-02 18:05:11 +0000213 return decl;
214}
215
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000216//===----------------------------------------------------------------------===//
217// FunctionDecl Serialization.
218//===----------------------------------------------------------------------===//
Ted Kremenek04973312007-11-02 18:05:11 +0000219
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000220void FunctionDecl::EmitImpl(Serializer& S) const {
221 S.EmitInt(SClass); // From FunctionDecl.
222 S.EmitBool(IsInline); // From FunctionDecl.
223 ValueDecl::EmitInRec(S);
224 S.EmitPtr(DeclChain);
Ted Kremenek04973312007-11-02 18:05:11 +0000225
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000226 // NOTE: We do not need to serialize out the number of parameters, because
227 // that is encoded in the type (accessed via getNumParams()).
Ted Kremenek04973312007-11-02 18:05:11 +0000228
Ted Kremenekd437f232007-11-13 22:51:08 +0000229 if (ParamInfo != NULL) {
230 S.EmitBool(true);
231 S.BatchEmitOwnedPtrs(getNumParams(),&ParamInfo[0], Body,
232 getNextDeclarator());
233 }
234 else {
235 S.EmitBool(false);
236 S.BatchEmitOwnedPtrs(Body,getNextDeclarator());
237 }
Ted Kremenek04973312007-11-02 18:05:11 +0000238}
239
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000240FunctionDecl* FunctionDecl::CreateImpl(Deserializer& D) {
Ted Kremenek04973312007-11-02 18:05:11 +0000241 StorageClass SClass = static_cast<StorageClass>(D.ReadInt());
242 bool IsInline = D.ReadBool();
Ted Kremenek04973312007-11-02 18:05:11 +0000243
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000244 FunctionDecl* decl =
245 new FunctionDecl(SourceLocation(),NULL,QualType(),SClass,IsInline);
Ted Kremenek04973312007-11-02 18:05:11 +0000246
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000247 decl->ValueDecl::ReadInRec(D);
248 D.ReadPtr(decl->DeclChain);
249
250 decl->ParamInfo = decl->getNumParams()
251 ? new ParmVarDecl*[decl->getNumParams()]
252 : NULL;
253
254 Decl* next_declarator;
255
Ted Kremenekd437f232007-11-13 22:51:08 +0000256 bool hasParamDecls = D.ReadBool();
257
258 if (hasParamDecls)
259 D.BatchReadOwnedPtrs(decl->getNumParams(),
260 reinterpret_cast<Decl**>(&decl->ParamInfo[0]),
261 decl->Body, next_declarator);
262 else
263 D.BatchReadOwnedPtrs(decl->Body, next_declarator);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000264
265 decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
Ted Kremenek04973312007-11-02 18:05:11 +0000266
267 return decl;
268}
Ted Kremenekf7bf4112007-11-05 21:49:34 +0000269
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000270//===----------------------------------------------------------------------===//
271// TypedefDecl Serialization.
272//===----------------------------------------------------------------------===//
273
274void TypedefDecl::EmitImpl(Serializer& S) const {
Ted Kremenek2ebc89f2007-11-06 19:51:47 +0000275 S.Emit(UnderlyingType);
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000276 ScopedDecl::EmitInRec(S);
277 ScopedDecl::EmitOutRec(S);
Ted Kremenekf7bf4112007-11-05 21:49:34 +0000278}
279
Ted Kremenek928fd7f2007-11-13 00:15:39 +0000280TypedefDecl* TypedefDecl::CreateImpl(Deserializer& D) {
281 QualType T = QualType::ReadVal(D);
282
283 TypedefDecl* decl = new TypedefDecl(SourceLocation(),NULL,T,NULL);
284
285 decl->ScopedDecl::ReadInRec(D);
286 decl->ScopedDecl::ReadOutRec(D);
287
Ted Kremenekf7bf4112007-11-05 21:49:34 +0000288 return decl;
289}