blob: 4595acc226b508b26c56950a0777db8a0a0610e1 [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 Kremenek6c4dba72007-11-07 17:05:07 +000046 case ContinueStmtClass:
47 return ContinueStmt::directMaterialize(D);
48
Ted Kremenek47281102007-11-07 00:17:35 +000049 case DeclRefExprClass:
50 return DeclRefExpr::directMaterialize(D);
51
Ted Kremenek2bd6e652007-11-07 00:37:40 +000052 case DeclStmtClass:
53 return DeclStmt::directMaterialize(D);
Ted Kremenek249d7cd2007-11-07 05:25:31 +000054
55 case DefaultStmtClass:
56 return DefaultStmt::directMaterialize(D);
Ted Kremenekab97f4d2007-11-07 07:53:55 +000057
58 case DoStmtClass:
59 return DoStmt::directMaterialize(D);
Ted Kremenekf34da902007-11-07 08:02:55 +000060
61 case ForStmtClass:
62 return ForStmt::directMaterialize(D);
Ted Kremenekaffd8be2007-11-07 08:07:46 +000063
64 case GotoStmtClass:
65 return GotoStmt::directMaterialize(D);
Ted Kremenek249d7cd2007-11-07 05:25:31 +000066
Ted Kremenekca22d352007-11-07 07:19:30 +000067 case IfStmtClass:
68 return IfStmt::directMaterialize(D);
69
Ted Kremeneke7d27d52007-11-07 17:02:32 +000070 case IndirectGotoStmtClass:
71 return IndirectGotoStmt::directMaterialize(D);
72
Ted Kremenek47281102007-11-07 00:17:35 +000073 case IntegerLiteralClass:
Ted Kremenek25aaa262007-11-07 00:40:53 +000074 return IntegerLiteral::directMaterialize(D);
75
Ted Kremenek56a74bb2007-11-07 00:48:04 +000076 case LabelStmtClass:
77 return LabelStmt::directMaterialize(D);
78
Ted Kremenek25aaa262007-11-07 00:40:53 +000079 case NullStmtClass:
80 return NullStmt::directMaterialize(D);
Ted Kremenek2bd6e652007-11-07 00:37:40 +000081
Ted Kremenek249d7cd2007-11-07 05:25:31 +000082 case ParenExprClass:
83 return ParenExpr::directMaterialize(D);
84
Ted Kremenek2bd6e652007-11-07 00:37:40 +000085 case ReturnStmtClass:
Ted Kremenek249d7cd2007-11-07 05:25:31 +000086 return ReturnStmt::directMaterialize(D);
87
88 case SwitchStmtClass:
89 return SwitchStmt::directMaterialize(D);
Ted Kremenek55e559a2007-11-07 07:50:10 +000090
91 case WhileStmtClass:
92 return WhileStmt::directMaterialize(D);
Ted Kremenek47281102007-11-07 00:17:35 +000093 }
Ted Kremenek215c2c82007-10-31 18:41:19 +000094}
95
Ted Kremenek2bd6e652007-11-07 00:37:40 +000096void BinaryOperator::directEmit(llvm::Serializer& S) const {
97 S.EmitInt(Opc);
98 S.Emit(OpLoc);;
99 S.Emit(getType());
100 S.EmitOwnedPtr(getLHS());
101 S.EmitOwnedPtr(getRHS());
102}
103
104BinaryOperator* BinaryOperator::directMaterialize(llvm::Deserializer& D) {
105 Opcode Opc = static_cast<Opcode>(D.ReadInt());
106 SourceLocation OpLoc = SourceLocation::ReadVal(D);
107 QualType Result = QualType::ReadVal(D);
108 Expr* LHS = D.ReadOwnedPtr<Expr>();
109 Expr* RHS = D.ReadOwnedPtr<Expr>();
110 return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc);
111}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000112
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000113void BreakStmt::directEmit(llvm::Serializer& S) const {
114 S.Emit(BreakLoc);
115}
116
117BreakStmt* BreakStmt::directMaterialize(llvm::Deserializer& D) {
118 SourceLocation Loc = SourceLocation::ReadVal(D);
119 return new BreakStmt(Loc);
120}
121
122void CaseStmt::directEmit(llvm::Serializer& S) const {
123 S.Emit(CaseLoc);
124 S.EmitOwnedPtr(getLHS());
125 S.EmitOwnedPtr(getRHS());
126 S.EmitOwnedPtr(getSubStmt());
127 S.EmitPtr(getNextSwitchCase());
128}
129
130CaseStmt* CaseStmt::directMaterialize(llvm::Deserializer& D) {
131 SourceLocation CaseLoc = SourceLocation::ReadVal(D);
132 Expr* LHS = D.ReadOwnedPtr<Expr>();
133 Expr* RHS = D.ReadOwnedPtr<Expr>();
134 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
135
136 CaseStmt* stmt = new CaseStmt(LHS,RHS,SubStmt,CaseLoc);
137 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
138
139 return stmt;
140}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000141
Ted Kremenek47281102007-11-07 00:17:35 +0000142void CompoundStmt::directEmit(llvm::Serializer& S) const {
143 S.Emit(LBracLoc);
144 S.Emit(RBracLoc);
145 S.Emit(Body.size());
Ted Kremenek215c2c82007-10-31 18:41:19 +0000146
Ted Kremenek47281102007-11-07 00:17:35 +0000147 for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I)
Ted Kremenek215c2c82007-10-31 18:41:19 +0000148 S.EmitOwnedPtr(*I);
149}
150
Ted Kremenek47281102007-11-07 00:17:35 +0000151CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) {
152 SourceLocation LB = SourceLocation::ReadVal(D);
153 SourceLocation RB = SourceLocation::ReadVal(D);
154 unsigned size = D.ReadInt();
Ted Kremenek215c2c82007-10-31 18:41:19 +0000155
Ted Kremenek47281102007-11-07 00:17:35 +0000156 CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB);
157
158 stmt->Body.reserve(size);
159
160 for (unsigned i = 0; i < size; ++i)
161 stmt->Body.push_back(D.ReadOwnedPtr<Stmt>());
162
163 return stmt;
Ted Kremenek215c2c82007-10-31 18:41:19 +0000164}
Ted Kremenek47281102007-11-07 00:17:35 +0000165
Ted Kremenek6c4dba72007-11-07 17:05:07 +0000166void ContinueStmt::directEmit(llvm::Serializer& S) const {
167 S.Emit(ContinueLoc);
168}
169
170ContinueStmt* ContinueStmt::directMaterialize(llvm::Deserializer& D) {
171 SourceLocation Loc = SourceLocation::ReadVal(D);
172 return new ContinueStmt(Loc);
173}
174
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000175void DeclStmt::directEmit(llvm::Serializer& S) const {
176 // FIXME: special handling for struct decls.
177 S.EmitOwnedPtr(getDecl());
Ted Kremenek47281102007-11-07 00:17:35 +0000178}
179
180void DeclRefExpr::directEmit(llvm::Serializer& S) const {
181 S.Emit(Loc);
182 S.Emit(getType());
183 S.EmitPtr(getDecl());
184}
185
186DeclRefExpr* DeclRefExpr::directMaterialize(llvm::Deserializer& D) {
187 SourceLocation Loc = SourceLocation::ReadVal(D);
188 QualType T = QualType::ReadVal(D);
189 DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc);
190 D.ReadPtr(dr->D,false);
191 return dr;
192}
193
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000194DeclStmt* DeclStmt::directMaterialize(llvm::Deserializer& D) {
195 ScopedDecl* decl = cast<ScopedDecl>(D.ReadOwnedPtr<Decl>());
196 return new DeclStmt(decl);
197}
198
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000199void DefaultStmt::directEmit(llvm::Serializer& S) const {
200 S.Emit(DefaultLoc);
201 S.EmitOwnedPtr(getSubStmt());
202 S.EmitPtr(getNextSwitchCase());
203}
204
205DefaultStmt* DefaultStmt::directMaterialize(llvm::Deserializer& D) {
206 SourceLocation Loc = SourceLocation::ReadVal(D);
207 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
208
209 DefaultStmt* stmt = new DefaultStmt(Loc,SubStmt);
210 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
211
212 return stmt;
213}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000214
Ted Kremenekab97f4d2007-11-07 07:53:55 +0000215void DoStmt::directEmit(llvm::Serializer& S) const {
216 S.Emit(DoLoc);
217 S.EmitOwnedPtr(getCond());
218 S.EmitOwnedPtr(getBody());
219}
220
221DoStmt* DoStmt::directMaterialize(llvm::Deserializer& D) {
222 SourceLocation DoLoc = SourceLocation::ReadVal(D);
223 Expr* Cond = D.ReadOwnedPtr<Expr>();
224 Stmt* Body = D.ReadOwnedPtr<Stmt>();
225 return new DoStmt(Body,Cond,DoLoc);
226}
227
Ted Kremenekf34da902007-11-07 08:02:55 +0000228void ForStmt::directEmit(llvm::Serializer& S) const {
229 S.Emit(ForLoc);
230 S.EmitOwnedPtr(getInit());
231 S.EmitOwnedPtr(getCond());
232 S.EmitOwnedPtr(getInc());
233 S.EmitOwnedPtr(getBody());
234}
235
236ForStmt* ForStmt::directMaterialize(llvm::Deserializer& D) {
237 SourceLocation ForLoc = SourceLocation::ReadVal(D);
238 Stmt* Init = D.ReadOwnedPtr<Stmt>();
239 Expr* Cond = D.ReadOwnedPtr<Expr>();
240 Expr* Inc = D.ReadOwnedPtr<Expr>();
241 Stmt* Body = D.ReadOwnedPtr<Stmt>();
242 return new ForStmt(Init,Cond,Inc,Body,ForLoc);
243}
244
Ted Kremenekaffd8be2007-11-07 08:07:46 +0000245void GotoStmt::directEmit(llvm::Serializer& S) const {
246 S.Emit(GotoLoc);
247 S.Emit(LabelLoc);
248 S.EmitPtr(Label);
249}
250
251GotoStmt* GotoStmt::directMaterialize(llvm::Deserializer& D) {
252 SourceLocation GotoLoc = SourceLocation::ReadVal(D);
253 SourceLocation LabelLoc = SourceLocation::ReadVal(D);
254 GotoStmt* stmt = new GotoStmt(NULL,GotoLoc,LabelLoc);
255 D.ReadPtr(stmt->Label); // This pointer may be backpatched later.
256 return stmt;
257}
258
Ted Kremenekca22d352007-11-07 07:19:30 +0000259void IfStmt::directEmit(llvm::Serializer& S) const {
260 S.Emit(IfLoc);
261 S.EmitOwnedPtr(getCond());
262 S.EmitOwnedPtr(getThen());
263 S.EmitOwnedPtr(getElse());
264}
265
266IfStmt* IfStmt::directMaterialize(llvm::Deserializer& D) {
267 SourceLocation L = SourceLocation::ReadVal(D);
268 Expr* Cond = D.ReadOwnedPtr<Expr>();
269 Stmt* Then = D.ReadOwnedPtr<Stmt>();
270 Stmt* Else = D.ReadOwnedPtr<Stmt>();
271 return new IfStmt(L,Cond,Then,Else);
272}
273
Ted Kremeneke7d27d52007-11-07 17:02:32 +0000274void IndirectGotoStmt::directEmit(llvm::Serializer& S) const {
275 S.EmitPtr(Target);
276}
277
278IndirectGotoStmt* IndirectGotoStmt::directMaterialize(llvm::Deserializer& D) {
279 IndirectGotoStmt* stmt = new IndirectGotoStmt(NULL);
280 D.ReadPtr(stmt->Target); // The target may be backpatched.
281 return stmt;
282}
283
Ted Kremenek47281102007-11-07 00:17:35 +0000284void IntegerLiteral::directEmit(llvm::Serializer& S) const {
285 S.Emit(Loc);
286 S.Emit(getType());
287 S.Emit(getValue());
288}
289
290IntegerLiteral* IntegerLiteral::directMaterialize(llvm::Deserializer& D) {
291 SourceLocation Loc = SourceLocation::ReadVal(D);
292 QualType T = QualType::ReadVal(D);
293
294 // Create a dummy APInt because it is more efficient to deserialize
295 // it in place with the deserialized IntegerLiteral. (fewer copies)
296 llvm::APInt temp;
297 IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc);
298 D.Read(expr->Value);
299
300 return expr;
301}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000302
Ted Kremenek56a74bb2007-11-07 00:48:04 +0000303void LabelStmt::directEmit(llvm::Serializer& S) const {
304 S.EmitPtr(Label);
305 S.Emit(IdentLoc);
306 S.EmitOwnedPtr(SubStmt);
307}
308
309LabelStmt* LabelStmt::directMaterialize(llvm::Deserializer& D) {
310 IdentifierInfo* Label = D.ReadPtr<IdentifierInfo>();
311 SourceLocation IdentLoc = SourceLocation::ReadVal(D);
312 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
313 return new LabelStmt(IdentLoc,Label,SubStmt);
314}
315
Ted Kremenek25aaa262007-11-07 00:40:53 +0000316void NullStmt::directEmit(llvm::Serializer& S) const {
317 S.Emit(SemiLoc);
318}
319
320NullStmt* NullStmt::directMaterialize(llvm::Deserializer& D) {
321 SourceLocation SemiLoc = SourceLocation::ReadVal(D);
322 return new NullStmt(SemiLoc);
323}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000324
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000325void ParenExpr::directEmit(llvm::Serializer& S) const {
326 S.Emit(L);
327 S.Emit(R);
328 S.EmitOwnedPtr(Val);
329}
330
331ParenExpr* ParenExpr::directMaterialize(llvm::Deserializer& D) {
332 SourceLocation L = SourceLocation::ReadVal(D);
333 SourceLocation R = SourceLocation::ReadVal(D);
334 Expr* val = D.ReadOwnedPtr<Expr>();
335 return new ParenExpr(L,R,val);
336}
337
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000338void ReturnStmt::directEmit(llvm::Serializer& S) const {
339 S.Emit(RetLoc);
340 S.EmitOwnedPtr(RetExpr);
341}
342
343ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) {
344 SourceLocation RetLoc = SourceLocation::ReadVal(D);
345 Expr* RetExpr = D.ReadOwnedPtr<Expr>();
346 return new ReturnStmt(RetLoc,RetExpr);
347}
348
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000349void SwitchStmt::directEmit(llvm::Serializer& S) const {
350 S.Emit(SwitchLoc);
351 S.EmitOwnedPtr(getCond());
352 S.EmitOwnedPtr(getBody());
353 S.EmitPtr(FirstCase);
354}
355
356SwitchStmt* SwitchStmt::directMaterialize(llvm::Deserializer& D) {
357 SourceLocation Loc = SourceLocation::ReadVal(D);
358 Stmt* Cond = D.ReadOwnedPtr<Stmt>();
359 Stmt* Body = D.ReadOwnedPtr<Stmt>();
360 SwitchCase* FirstCase = cast<SwitchCase>(D.ReadPtr<Stmt>());
361
362 SwitchStmt* stmt = new SwitchStmt(cast<Expr>(Cond));
363 stmt->setBody(Body,Loc);
364 stmt->FirstCase = FirstCase;
365
366 return stmt;
367}
Ted Kremenek55e559a2007-11-07 07:50:10 +0000368
369void WhileStmt::directEmit(llvm::Serializer& S) const {
370 S.Emit(WhileLoc);
371 S.EmitOwnedPtr(getCond());
372 S.EmitOwnedPtr(getBody());
373}
374
375WhileStmt* WhileStmt::directMaterialize(llvm::Deserializer& D) {
376 SourceLocation WhileLoc = SourceLocation::ReadVal(D);
377 Expr* Cond = D.ReadOwnedPtr<Expr>();
378 Stmt* Body = D.ReadOwnedPtr<Stmt>();
379 return new WhileStmt(Cond,Body,WhileLoc);
380}