blob: 2d6376c4a965c184a60a8790fa966adffcfe1f79 [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 Kremenek539a4182007-11-07 17:11:58 +000085 case PreDefinedExprClass:
86 return PreDefinedExpr::directMaterialize(D);
87
Ted Kremenek2bd6e652007-11-07 00:37:40 +000088 case ReturnStmtClass:
Ted Kremenek249d7cd2007-11-07 05:25:31 +000089 return ReturnStmt::directMaterialize(D);
90
91 case SwitchStmtClass:
92 return SwitchStmt::directMaterialize(D);
Ted Kremenek55e559a2007-11-07 07:50:10 +000093
94 case WhileStmtClass:
95 return WhileStmt::directMaterialize(D);
Ted Kremenek47281102007-11-07 00:17:35 +000096 }
Ted Kremenek215c2c82007-10-31 18:41:19 +000097}
98
Ted Kremenek2bd6e652007-11-07 00:37:40 +000099void BinaryOperator::directEmit(llvm::Serializer& S) const {
100 S.EmitInt(Opc);
101 S.Emit(OpLoc);;
102 S.Emit(getType());
103 S.EmitOwnedPtr(getLHS());
104 S.EmitOwnedPtr(getRHS());
105}
106
107BinaryOperator* BinaryOperator::directMaterialize(llvm::Deserializer& D) {
108 Opcode Opc = static_cast<Opcode>(D.ReadInt());
109 SourceLocation OpLoc = SourceLocation::ReadVal(D);
110 QualType Result = QualType::ReadVal(D);
111 Expr* LHS = D.ReadOwnedPtr<Expr>();
112 Expr* RHS = D.ReadOwnedPtr<Expr>();
113 return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc);
114}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000115
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000116void BreakStmt::directEmit(llvm::Serializer& S) const {
117 S.Emit(BreakLoc);
118}
119
120BreakStmt* BreakStmt::directMaterialize(llvm::Deserializer& D) {
121 SourceLocation Loc = SourceLocation::ReadVal(D);
122 return new BreakStmt(Loc);
123}
124
125void CaseStmt::directEmit(llvm::Serializer& S) const {
126 S.Emit(CaseLoc);
127 S.EmitOwnedPtr(getLHS());
128 S.EmitOwnedPtr(getRHS());
129 S.EmitOwnedPtr(getSubStmt());
130 S.EmitPtr(getNextSwitchCase());
131}
132
133CaseStmt* CaseStmt::directMaterialize(llvm::Deserializer& D) {
134 SourceLocation CaseLoc = SourceLocation::ReadVal(D);
135 Expr* LHS = D.ReadOwnedPtr<Expr>();
136 Expr* RHS = D.ReadOwnedPtr<Expr>();
137 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
138
139 CaseStmt* stmt = new CaseStmt(LHS,RHS,SubStmt,CaseLoc);
140 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
141
142 return stmt;
143}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000144
Ted Kremenek47281102007-11-07 00:17:35 +0000145void CompoundStmt::directEmit(llvm::Serializer& S) const {
146 S.Emit(LBracLoc);
147 S.Emit(RBracLoc);
148 S.Emit(Body.size());
Ted Kremenek215c2c82007-10-31 18:41:19 +0000149
Ted Kremenek47281102007-11-07 00:17:35 +0000150 for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I)
Ted Kremenek215c2c82007-10-31 18:41:19 +0000151 S.EmitOwnedPtr(*I);
152}
153
Ted Kremenek47281102007-11-07 00:17:35 +0000154CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) {
155 SourceLocation LB = SourceLocation::ReadVal(D);
156 SourceLocation RB = SourceLocation::ReadVal(D);
157 unsigned size = D.ReadInt();
Ted Kremenek215c2c82007-10-31 18:41:19 +0000158
Ted Kremenek47281102007-11-07 00:17:35 +0000159 CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB);
160
161 stmt->Body.reserve(size);
162
163 for (unsigned i = 0; i < size; ++i)
164 stmt->Body.push_back(D.ReadOwnedPtr<Stmt>());
165
166 return stmt;
Ted Kremenek215c2c82007-10-31 18:41:19 +0000167}
Ted Kremenek47281102007-11-07 00:17:35 +0000168
Ted Kremenek6c4dba72007-11-07 17:05:07 +0000169void ContinueStmt::directEmit(llvm::Serializer& S) const {
170 S.Emit(ContinueLoc);
171}
172
173ContinueStmt* ContinueStmt::directMaterialize(llvm::Deserializer& D) {
174 SourceLocation Loc = SourceLocation::ReadVal(D);
175 return new ContinueStmt(Loc);
176}
177
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000178void DeclStmt::directEmit(llvm::Serializer& S) const {
179 // FIXME: special handling for struct decls.
180 S.EmitOwnedPtr(getDecl());
Ted Kremenek47281102007-11-07 00:17:35 +0000181}
182
183void DeclRefExpr::directEmit(llvm::Serializer& S) const {
184 S.Emit(Loc);
185 S.Emit(getType());
186 S.EmitPtr(getDecl());
187}
188
189DeclRefExpr* DeclRefExpr::directMaterialize(llvm::Deserializer& D) {
190 SourceLocation Loc = SourceLocation::ReadVal(D);
191 QualType T = QualType::ReadVal(D);
192 DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc);
193 D.ReadPtr(dr->D,false);
194 return dr;
195}
196
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000197DeclStmt* DeclStmt::directMaterialize(llvm::Deserializer& D) {
198 ScopedDecl* decl = cast<ScopedDecl>(D.ReadOwnedPtr<Decl>());
199 return new DeclStmt(decl);
200}
201
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000202void DefaultStmt::directEmit(llvm::Serializer& S) const {
203 S.Emit(DefaultLoc);
204 S.EmitOwnedPtr(getSubStmt());
205 S.EmitPtr(getNextSwitchCase());
206}
207
208DefaultStmt* DefaultStmt::directMaterialize(llvm::Deserializer& D) {
209 SourceLocation Loc = SourceLocation::ReadVal(D);
210 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
211
212 DefaultStmt* stmt = new DefaultStmt(Loc,SubStmt);
213 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
214
215 return stmt;
216}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000217
Ted Kremenekab97f4d2007-11-07 07:53:55 +0000218void DoStmt::directEmit(llvm::Serializer& S) const {
219 S.Emit(DoLoc);
220 S.EmitOwnedPtr(getCond());
221 S.EmitOwnedPtr(getBody());
222}
223
224DoStmt* DoStmt::directMaterialize(llvm::Deserializer& D) {
225 SourceLocation DoLoc = SourceLocation::ReadVal(D);
226 Expr* Cond = D.ReadOwnedPtr<Expr>();
227 Stmt* Body = D.ReadOwnedPtr<Stmt>();
228 return new DoStmt(Body,Cond,DoLoc);
229}
230
Ted Kremenekf34da902007-11-07 08:02:55 +0000231void ForStmt::directEmit(llvm::Serializer& S) const {
232 S.Emit(ForLoc);
233 S.EmitOwnedPtr(getInit());
234 S.EmitOwnedPtr(getCond());
235 S.EmitOwnedPtr(getInc());
236 S.EmitOwnedPtr(getBody());
237}
238
239ForStmt* ForStmt::directMaterialize(llvm::Deserializer& D) {
240 SourceLocation ForLoc = SourceLocation::ReadVal(D);
241 Stmt* Init = D.ReadOwnedPtr<Stmt>();
242 Expr* Cond = D.ReadOwnedPtr<Expr>();
243 Expr* Inc = D.ReadOwnedPtr<Expr>();
244 Stmt* Body = D.ReadOwnedPtr<Stmt>();
245 return new ForStmt(Init,Cond,Inc,Body,ForLoc);
246}
247
Ted Kremenekaffd8be2007-11-07 08:07:46 +0000248void GotoStmt::directEmit(llvm::Serializer& S) const {
249 S.Emit(GotoLoc);
250 S.Emit(LabelLoc);
251 S.EmitPtr(Label);
252}
253
254GotoStmt* GotoStmt::directMaterialize(llvm::Deserializer& D) {
255 SourceLocation GotoLoc = SourceLocation::ReadVal(D);
256 SourceLocation LabelLoc = SourceLocation::ReadVal(D);
257 GotoStmt* stmt = new GotoStmt(NULL,GotoLoc,LabelLoc);
258 D.ReadPtr(stmt->Label); // This pointer may be backpatched later.
259 return stmt;
260}
261
Ted Kremenekca22d352007-11-07 07:19:30 +0000262void IfStmt::directEmit(llvm::Serializer& S) const {
263 S.Emit(IfLoc);
264 S.EmitOwnedPtr(getCond());
265 S.EmitOwnedPtr(getThen());
266 S.EmitOwnedPtr(getElse());
267}
268
269IfStmt* IfStmt::directMaterialize(llvm::Deserializer& D) {
270 SourceLocation L = SourceLocation::ReadVal(D);
271 Expr* Cond = D.ReadOwnedPtr<Expr>();
272 Stmt* Then = D.ReadOwnedPtr<Stmt>();
273 Stmt* Else = D.ReadOwnedPtr<Stmt>();
274 return new IfStmt(L,Cond,Then,Else);
275}
276
Ted Kremeneke7d27d52007-11-07 17:02:32 +0000277void IndirectGotoStmt::directEmit(llvm::Serializer& S) const {
278 S.EmitPtr(Target);
279}
280
281IndirectGotoStmt* IndirectGotoStmt::directMaterialize(llvm::Deserializer& D) {
282 IndirectGotoStmt* stmt = new IndirectGotoStmt(NULL);
283 D.ReadPtr(stmt->Target); // The target may be backpatched.
284 return stmt;
285}
286
Ted Kremenek47281102007-11-07 00:17:35 +0000287void IntegerLiteral::directEmit(llvm::Serializer& S) const {
288 S.Emit(Loc);
289 S.Emit(getType());
290 S.Emit(getValue());
291}
292
293IntegerLiteral* IntegerLiteral::directMaterialize(llvm::Deserializer& D) {
294 SourceLocation Loc = SourceLocation::ReadVal(D);
295 QualType T = QualType::ReadVal(D);
296
297 // Create a dummy APInt because it is more efficient to deserialize
298 // it in place with the deserialized IntegerLiteral. (fewer copies)
299 llvm::APInt temp;
300 IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc);
301 D.Read(expr->Value);
302
303 return expr;
304}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000305
Ted Kremenek56a74bb2007-11-07 00:48:04 +0000306void LabelStmt::directEmit(llvm::Serializer& S) const {
307 S.EmitPtr(Label);
308 S.Emit(IdentLoc);
309 S.EmitOwnedPtr(SubStmt);
310}
311
312LabelStmt* LabelStmt::directMaterialize(llvm::Deserializer& D) {
313 IdentifierInfo* Label = D.ReadPtr<IdentifierInfo>();
314 SourceLocation IdentLoc = SourceLocation::ReadVal(D);
315 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
316 return new LabelStmt(IdentLoc,Label,SubStmt);
317}
318
Ted Kremenek25aaa262007-11-07 00:40:53 +0000319void NullStmt::directEmit(llvm::Serializer& S) const {
320 S.Emit(SemiLoc);
321}
322
323NullStmt* NullStmt::directMaterialize(llvm::Deserializer& D) {
324 SourceLocation SemiLoc = SourceLocation::ReadVal(D);
325 return new NullStmt(SemiLoc);
326}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000327
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000328void ParenExpr::directEmit(llvm::Serializer& S) const {
329 S.Emit(L);
330 S.Emit(R);
331 S.EmitOwnedPtr(Val);
332}
333
334ParenExpr* ParenExpr::directMaterialize(llvm::Deserializer& D) {
335 SourceLocation L = SourceLocation::ReadVal(D);
336 SourceLocation R = SourceLocation::ReadVal(D);
337 Expr* val = D.ReadOwnedPtr<Expr>();
338 return new ParenExpr(L,R,val);
Ted Kremenek539a4182007-11-07 17:11:58 +0000339}
340
341void PreDefinedExpr::directEmit(llvm::Serializer& S) const {
342 S.Emit(Loc);
343 S.EmitInt(getIdentType());
344 S.Emit(getType());
345}
346
347PreDefinedExpr* PreDefinedExpr::directMaterialize(llvm::Deserializer& D) {
348 SourceLocation Loc = SourceLocation::ReadVal(D);
349 IdentType it = static_cast<IdentType>(D.ReadInt());
350 QualType Q = QualType::ReadVal(D);
351 return new PreDefinedExpr(Loc,Q,it);
352}
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000353
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000354void ReturnStmt::directEmit(llvm::Serializer& S) const {
355 S.Emit(RetLoc);
356 S.EmitOwnedPtr(RetExpr);
357}
358
359ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) {
360 SourceLocation RetLoc = SourceLocation::ReadVal(D);
361 Expr* RetExpr = D.ReadOwnedPtr<Expr>();
362 return new ReturnStmt(RetLoc,RetExpr);
363}
364
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000365void SwitchStmt::directEmit(llvm::Serializer& S) const {
366 S.Emit(SwitchLoc);
367 S.EmitOwnedPtr(getCond());
368 S.EmitOwnedPtr(getBody());
369 S.EmitPtr(FirstCase);
370}
371
372SwitchStmt* SwitchStmt::directMaterialize(llvm::Deserializer& D) {
373 SourceLocation Loc = SourceLocation::ReadVal(D);
374 Stmt* Cond = D.ReadOwnedPtr<Stmt>();
375 Stmt* Body = D.ReadOwnedPtr<Stmt>();
376 SwitchCase* FirstCase = cast<SwitchCase>(D.ReadPtr<Stmt>());
377
378 SwitchStmt* stmt = new SwitchStmt(cast<Expr>(Cond));
379 stmt->setBody(Body,Loc);
380 stmt->FirstCase = FirstCase;
381
382 return stmt;
383}
Ted Kremenek55e559a2007-11-07 07:50:10 +0000384
385void WhileStmt::directEmit(llvm::Serializer& S) const {
386 S.Emit(WhileLoc);
387 S.EmitOwnedPtr(getCond());
388 S.EmitOwnedPtr(getBody());
389}
390
391WhileStmt* WhileStmt::directMaterialize(llvm::Deserializer& D) {
392 SourceLocation WhileLoc = SourceLocation::ReadVal(D);
393 Expr* Cond = D.ReadOwnedPtr<Expr>();
394 Stmt* Body = D.ReadOwnedPtr<Stmt>();
395 return new WhileStmt(Cond,Body,WhileLoc);
396}