blob: e0b87c7cabd87d1e8b6754a4b8cd707c97bbacc0 [file] [log] [blame]
Ted Kremenek215c2c82007-10-31 18:41:19 +00001//===--- StmtSerialization.cpp - Serialization of Statements --------------===//
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 file defines the type-specific methods for serializing statements
11// and expressions.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek47281102007-11-07 00:17:35 +000015#include "clang/AST/Expr.h"
Ted Kremenek215c2c82007-10-31 18:41:19 +000016#include "llvm/Bitcode/Serialize.h"
17#include "llvm/Bitcode/Deserialize.h"
18
Ted Kremenek215c2c82007-10-31 18:41:19 +000019using namespace clang;
20
Ted Kremenek47281102007-11-07 00:17:35 +000021void Stmt::Emit(llvm::Serializer& S) const {
22 S.EmitInt(getStmtClass());
23 directEmit(S);
24}
Ted Kremenek215c2c82007-10-31 18:41:19 +000025
Ted Kremenek47281102007-11-07 00:17:35 +000026Stmt* Stmt::Materialize(llvm::Deserializer& D) {
27 StmtClass SC = static_cast<StmtClass>(D.ReadInt());
Ted Kremenek215c2c82007-10-31 18:41:19 +000028
Ted Kremenek47281102007-11-07 00:17:35 +000029 switch (SC) {
30 default:
31 assert (false && "Not implemented.");
32 return NULL;
Ted Kremenek2bd6e652007-11-07 00:37:40 +000033
34 case BinaryOperatorClass:
35 return BinaryOperator::directMaterialize(D);
Ted Kremenek47281102007-11-07 00:17:35 +000036
Ted Kremenek249d7cd2007-11-07 05:25:31 +000037 case BreakStmtClass:
38 return BreakStmt::directMaterialize(D);
39
40 case CaseStmtClass:
41 return CaseStmt::directMaterialize(D);
42
Ted Kremenek47281102007-11-07 00:17:35 +000043 case CompoundStmtClass:
44 return CompoundStmt::directMaterialize(D);
45
Ted Kremenek47281102007-11-07 00:17:35 +000046 case DeclRefExprClass:
47 return DeclRefExpr::directMaterialize(D);
48
Ted Kremenek2bd6e652007-11-07 00:37:40 +000049 case DeclStmtClass:
50 return DeclStmt::directMaterialize(D);
Ted Kremenek249d7cd2007-11-07 05:25:31 +000051
52 case DefaultStmtClass:
53 return DefaultStmt::directMaterialize(D);
Ted Kremenekab97f4d2007-11-07 07:53:55 +000054
55 case DoStmtClass:
56 return DoStmt::directMaterialize(D);
Ted Kremenekf34da902007-11-07 08:02:55 +000057
58 case ForStmtClass:
59 return ForStmt::directMaterialize(D);
Ted Kremenekaffd8be2007-11-07 08:07:46 +000060
61 case GotoStmtClass:
62 return GotoStmt::directMaterialize(D);
Ted Kremenek249d7cd2007-11-07 05:25:31 +000063
Ted Kremenekca22d352007-11-07 07:19:30 +000064 case IfStmtClass:
65 return IfStmt::directMaterialize(D);
66
Ted Kremeneke7d27d52007-11-07 17:02:32 +000067 case IndirectGotoStmtClass:
68 return IndirectGotoStmt::directMaterialize(D);
69
Ted Kremenek47281102007-11-07 00:17:35 +000070 case IntegerLiteralClass:
Ted Kremenek25aaa262007-11-07 00:40:53 +000071 return IntegerLiteral::directMaterialize(D);
72
Ted Kremenek56a74bb2007-11-07 00:48:04 +000073 case LabelStmtClass:
74 return LabelStmt::directMaterialize(D);
75
Ted Kremenek25aaa262007-11-07 00:40:53 +000076 case NullStmtClass:
77 return NullStmt::directMaterialize(D);
Ted Kremenek2bd6e652007-11-07 00:37:40 +000078
Ted Kremenek249d7cd2007-11-07 05:25:31 +000079 case ParenExprClass:
80 return ParenExpr::directMaterialize(D);
81
Ted Kremenek2bd6e652007-11-07 00:37:40 +000082 case ReturnStmtClass:
Ted Kremenek249d7cd2007-11-07 05:25:31 +000083 return ReturnStmt::directMaterialize(D);
84
85 case SwitchStmtClass:
86 return SwitchStmt::directMaterialize(D);
Ted Kremenek55e559a2007-11-07 07:50:10 +000087
88 case WhileStmtClass:
89 return WhileStmt::directMaterialize(D);
Ted Kremenek47281102007-11-07 00:17:35 +000090 }
Ted Kremenek215c2c82007-10-31 18:41:19 +000091}
92
Ted Kremenek2bd6e652007-11-07 00:37:40 +000093void BinaryOperator::directEmit(llvm::Serializer& S) const {
94 S.EmitInt(Opc);
95 S.Emit(OpLoc);;
96 S.Emit(getType());
97 S.EmitOwnedPtr(getLHS());
98 S.EmitOwnedPtr(getRHS());
99}
100
101BinaryOperator* BinaryOperator::directMaterialize(llvm::Deserializer& D) {
102 Opcode Opc = static_cast<Opcode>(D.ReadInt());
103 SourceLocation OpLoc = SourceLocation::ReadVal(D);
104 QualType Result = QualType::ReadVal(D);
105 Expr* LHS = D.ReadOwnedPtr<Expr>();
106 Expr* RHS = D.ReadOwnedPtr<Expr>();
107 return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc);
108}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000109
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000110void BreakStmt::directEmit(llvm::Serializer& S) const {
111 S.Emit(BreakLoc);
112}
113
114BreakStmt* BreakStmt::directMaterialize(llvm::Deserializer& D) {
115 SourceLocation Loc = SourceLocation::ReadVal(D);
116 return new BreakStmt(Loc);
117}
118
119void CaseStmt::directEmit(llvm::Serializer& S) const {
120 S.Emit(CaseLoc);
121 S.EmitOwnedPtr(getLHS());
122 S.EmitOwnedPtr(getRHS());
123 S.EmitOwnedPtr(getSubStmt());
124 S.EmitPtr(getNextSwitchCase());
125}
126
127CaseStmt* CaseStmt::directMaterialize(llvm::Deserializer& D) {
128 SourceLocation CaseLoc = SourceLocation::ReadVal(D);
129 Expr* LHS = D.ReadOwnedPtr<Expr>();
130 Expr* RHS = D.ReadOwnedPtr<Expr>();
131 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
132
133 CaseStmt* stmt = new CaseStmt(LHS,RHS,SubStmt,CaseLoc);
134 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
135
136 return stmt;
137}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000138
Ted Kremenek47281102007-11-07 00:17:35 +0000139void CompoundStmt::directEmit(llvm::Serializer& S) const {
140 S.Emit(LBracLoc);
141 S.Emit(RBracLoc);
142 S.Emit(Body.size());
Ted Kremenek215c2c82007-10-31 18:41:19 +0000143
Ted Kremenek47281102007-11-07 00:17:35 +0000144 for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I)
Ted Kremenek215c2c82007-10-31 18:41:19 +0000145 S.EmitOwnedPtr(*I);
146}
147
Ted Kremenek47281102007-11-07 00:17:35 +0000148CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) {
149 SourceLocation LB = SourceLocation::ReadVal(D);
150 SourceLocation RB = SourceLocation::ReadVal(D);
151 unsigned size = D.ReadInt();
Ted Kremenek215c2c82007-10-31 18:41:19 +0000152
Ted Kremenek47281102007-11-07 00:17:35 +0000153 CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB);
154
155 stmt->Body.reserve(size);
156
157 for (unsigned i = 0; i < size; ++i)
158 stmt->Body.push_back(D.ReadOwnedPtr<Stmt>());
159
160 return stmt;
Ted Kremenek215c2c82007-10-31 18:41:19 +0000161}
Ted Kremenek47281102007-11-07 00:17:35 +0000162
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000163void DeclStmt::directEmit(llvm::Serializer& S) const {
164 // FIXME: special handling for struct decls.
165 S.EmitOwnedPtr(getDecl());
Ted Kremenek47281102007-11-07 00:17:35 +0000166}
167
168void DeclRefExpr::directEmit(llvm::Serializer& S) const {
169 S.Emit(Loc);
170 S.Emit(getType());
171 S.EmitPtr(getDecl());
172}
173
174DeclRefExpr* DeclRefExpr::directMaterialize(llvm::Deserializer& D) {
175 SourceLocation Loc = SourceLocation::ReadVal(D);
176 QualType T = QualType::ReadVal(D);
177 DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc);
178 D.ReadPtr(dr->D,false);
179 return dr;
180}
181
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000182DeclStmt* DeclStmt::directMaterialize(llvm::Deserializer& D) {
183 ScopedDecl* decl = cast<ScopedDecl>(D.ReadOwnedPtr<Decl>());
184 return new DeclStmt(decl);
185}
186
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000187void DefaultStmt::directEmit(llvm::Serializer& S) const {
188 S.Emit(DefaultLoc);
189 S.EmitOwnedPtr(getSubStmt());
190 S.EmitPtr(getNextSwitchCase());
191}
192
193DefaultStmt* DefaultStmt::directMaterialize(llvm::Deserializer& D) {
194 SourceLocation Loc = SourceLocation::ReadVal(D);
195 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
196
197 DefaultStmt* stmt = new DefaultStmt(Loc,SubStmt);
198 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
199
200 return stmt;
201}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000202
Ted Kremenekab97f4d2007-11-07 07:53:55 +0000203void DoStmt::directEmit(llvm::Serializer& S) const {
204 S.Emit(DoLoc);
205 S.EmitOwnedPtr(getCond());
206 S.EmitOwnedPtr(getBody());
207}
208
209DoStmt* DoStmt::directMaterialize(llvm::Deserializer& D) {
210 SourceLocation DoLoc = SourceLocation::ReadVal(D);
211 Expr* Cond = D.ReadOwnedPtr<Expr>();
212 Stmt* Body = D.ReadOwnedPtr<Stmt>();
213 return new DoStmt(Body,Cond,DoLoc);
214}
215
Ted Kremenekf34da902007-11-07 08:02:55 +0000216void ForStmt::directEmit(llvm::Serializer& S) const {
217 S.Emit(ForLoc);
218 S.EmitOwnedPtr(getInit());
219 S.EmitOwnedPtr(getCond());
220 S.EmitOwnedPtr(getInc());
221 S.EmitOwnedPtr(getBody());
222}
223
224ForStmt* ForStmt::directMaterialize(llvm::Deserializer& D) {
225 SourceLocation ForLoc = SourceLocation::ReadVal(D);
226 Stmt* Init = D.ReadOwnedPtr<Stmt>();
227 Expr* Cond = D.ReadOwnedPtr<Expr>();
228 Expr* Inc = D.ReadOwnedPtr<Expr>();
229 Stmt* Body = D.ReadOwnedPtr<Stmt>();
230 return new ForStmt(Init,Cond,Inc,Body,ForLoc);
231}
232
Ted Kremenekaffd8be2007-11-07 08:07:46 +0000233void GotoStmt::directEmit(llvm::Serializer& S) const {
234 S.Emit(GotoLoc);
235 S.Emit(LabelLoc);
236 S.EmitPtr(Label);
237}
238
239GotoStmt* GotoStmt::directMaterialize(llvm::Deserializer& D) {
240 SourceLocation GotoLoc = SourceLocation::ReadVal(D);
241 SourceLocation LabelLoc = SourceLocation::ReadVal(D);
242 GotoStmt* stmt = new GotoStmt(NULL,GotoLoc,LabelLoc);
243 D.ReadPtr(stmt->Label); // This pointer may be backpatched later.
244 return stmt;
245}
246
Ted Kremenekca22d352007-11-07 07:19:30 +0000247void IfStmt::directEmit(llvm::Serializer& S) const {
248 S.Emit(IfLoc);
249 S.EmitOwnedPtr(getCond());
250 S.EmitOwnedPtr(getThen());
251 S.EmitOwnedPtr(getElse());
252}
253
254IfStmt* IfStmt::directMaterialize(llvm::Deserializer& D) {
255 SourceLocation L = SourceLocation::ReadVal(D);
256 Expr* Cond = D.ReadOwnedPtr<Expr>();
257 Stmt* Then = D.ReadOwnedPtr<Stmt>();
258 Stmt* Else = D.ReadOwnedPtr<Stmt>();
259 return new IfStmt(L,Cond,Then,Else);
260}
261
Ted Kremeneke7d27d52007-11-07 17:02:32 +0000262void IndirectGotoStmt::directEmit(llvm::Serializer& S) const {
263 S.EmitPtr(Target);
264}
265
266IndirectGotoStmt* IndirectGotoStmt::directMaterialize(llvm::Deserializer& D) {
267 IndirectGotoStmt* stmt = new IndirectGotoStmt(NULL);
268 D.ReadPtr(stmt->Target); // The target may be backpatched.
269 return stmt;
270}
271
Ted Kremenek47281102007-11-07 00:17:35 +0000272void IntegerLiteral::directEmit(llvm::Serializer& S) const {
273 S.Emit(Loc);
274 S.Emit(getType());
275 S.Emit(getValue());
276}
277
278IntegerLiteral* IntegerLiteral::directMaterialize(llvm::Deserializer& D) {
279 SourceLocation Loc = SourceLocation::ReadVal(D);
280 QualType T = QualType::ReadVal(D);
281
282 // Create a dummy APInt because it is more efficient to deserialize
283 // it in place with the deserialized IntegerLiteral. (fewer copies)
284 llvm::APInt temp;
285 IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc);
286 D.Read(expr->Value);
287
288 return expr;
289}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000290
Ted Kremenek56a74bb2007-11-07 00:48:04 +0000291void LabelStmt::directEmit(llvm::Serializer& S) const {
292 S.EmitPtr(Label);
293 S.Emit(IdentLoc);
294 S.EmitOwnedPtr(SubStmt);
295}
296
297LabelStmt* LabelStmt::directMaterialize(llvm::Deserializer& D) {
298 IdentifierInfo* Label = D.ReadPtr<IdentifierInfo>();
299 SourceLocation IdentLoc = SourceLocation::ReadVal(D);
300 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
301 return new LabelStmt(IdentLoc,Label,SubStmt);
302}
303
Ted Kremenek25aaa262007-11-07 00:40:53 +0000304void NullStmt::directEmit(llvm::Serializer& S) const {
305 S.Emit(SemiLoc);
306}
307
308NullStmt* NullStmt::directMaterialize(llvm::Deserializer& D) {
309 SourceLocation SemiLoc = SourceLocation::ReadVal(D);
310 return new NullStmt(SemiLoc);
311}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000312
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000313void ParenExpr::directEmit(llvm::Serializer& S) const {
314 S.Emit(L);
315 S.Emit(R);
316 S.EmitOwnedPtr(Val);
317}
318
319ParenExpr* ParenExpr::directMaterialize(llvm::Deserializer& D) {
320 SourceLocation L = SourceLocation::ReadVal(D);
321 SourceLocation R = SourceLocation::ReadVal(D);
322 Expr* val = D.ReadOwnedPtr<Expr>();
323 return new ParenExpr(L,R,val);
324}
325
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000326void ReturnStmt::directEmit(llvm::Serializer& S) const {
327 S.Emit(RetLoc);
328 S.EmitOwnedPtr(RetExpr);
329}
330
331ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) {
332 SourceLocation RetLoc = SourceLocation::ReadVal(D);
333 Expr* RetExpr = D.ReadOwnedPtr<Expr>();
334 return new ReturnStmt(RetLoc,RetExpr);
335}
336
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000337void SwitchStmt::directEmit(llvm::Serializer& S) const {
338 S.Emit(SwitchLoc);
339 S.EmitOwnedPtr(getCond());
340 S.EmitOwnedPtr(getBody());
341 S.EmitPtr(FirstCase);
342}
343
344SwitchStmt* SwitchStmt::directMaterialize(llvm::Deserializer& D) {
345 SourceLocation Loc = SourceLocation::ReadVal(D);
346 Stmt* Cond = D.ReadOwnedPtr<Stmt>();
347 Stmt* Body = D.ReadOwnedPtr<Stmt>();
348 SwitchCase* FirstCase = cast<SwitchCase>(D.ReadPtr<Stmt>());
349
350 SwitchStmt* stmt = new SwitchStmt(cast<Expr>(Cond));
351 stmt->setBody(Body,Loc);
352 stmt->FirstCase = FirstCase;
353
354 return stmt;
355}
Ted Kremenek55e559a2007-11-07 07:50:10 +0000356
357void WhileStmt::directEmit(llvm::Serializer& S) const {
358 S.Emit(WhileLoc);
359 S.EmitOwnedPtr(getCond());
360 S.EmitOwnedPtr(getBody());
361}
362
363WhileStmt* WhileStmt::directMaterialize(llvm::Deserializer& D) {
364 SourceLocation WhileLoc = SourceLocation::ReadVal(D);
365 Expr* Cond = D.ReadOwnedPtr<Expr>();
366 Stmt* Body = D.ReadOwnedPtr<Stmt>();
367 return new WhileStmt(Cond,Body,WhileLoc);
368}