blob: e4a43acd33e5aa8b99042fcbc0eef421683c25f9 [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 Kremenek47281102007-11-07 00:17:35 +000067 case IntegerLiteralClass:
Ted Kremenek25aaa262007-11-07 00:40:53 +000068 return IntegerLiteral::directMaterialize(D);
69
Ted Kremenek56a74bb2007-11-07 00:48:04 +000070 case LabelStmtClass:
71 return LabelStmt::directMaterialize(D);
72
Ted Kremenek25aaa262007-11-07 00:40:53 +000073 case NullStmtClass:
74 return NullStmt::directMaterialize(D);
Ted Kremenek2bd6e652007-11-07 00:37:40 +000075
Ted Kremenek249d7cd2007-11-07 05:25:31 +000076 case ParenExprClass:
77 return ParenExpr::directMaterialize(D);
78
Ted Kremenek2bd6e652007-11-07 00:37:40 +000079 case ReturnStmtClass:
Ted Kremenek249d7cd2007-11-07 05:25:31 +000080 return ReturnStmt::directMaterialize(D);
81
82 case SwitchStmtClass:
83 return SwitchStmt::directMaterialize(D);
Ted Kremenek55e559a2007-11-07 07:50:10 +000084
85 case WhileStmtClass:
86 return WhileStmt::directMaterialize(D);
Ted Kremenek47281102007-11-07 00:17:35 +000087 }
Ted Kremenek215c2c82007-10-31 18:41:19 +000088}
89
Ted Kremenek2bd6e652007-11-07 00:37:40 +000090void BinaryOperator::directEmit(llvm::Serializer& S) const {
91 S.EmitInt(Opc);
92 S.Emit(OpLoc);;
93 S.Emit(getType());
94 S.EmitOwnedPtr(getLHS());
95 S.EmitOwnedPtr(getRHS());
96}
97
98BinaryOperator* BinaryOperator::directMaterialize(llvm::Deserializer& D) {
99 Opcode Opc = static_cast<Opcode>(D.ReadInt());
100 SourceLocation OpLoc = SourceLocation::ReadVal(D);
101 QualType Result = QualType::ReadVal(D);
102 Expr* LHS = D.ReadOwnedPtr<Expr>();
103 Expr* RHS = D.ReadOwnedPtr<Expr>();
104 return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc);
105}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000106
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000107void BreakStmt::directEmit(llvm::Serializer& S) const {
108 S.Emit(BreakLoc);
109}
110
111BreakStmt* BreakStmt::directMaterialize(llvm::Deserializer& D) {
112 SourceLocation Loc = SourceLocation::ReadVal(D);
113 return new BreakStmt(Loc);
114}
115
116void CaseStmt::directEmit(llvm::Serializer& S) const {
117 S.Emit(CaseLoc);
118 S.EmitOwnedPtr(getLHS());
119 S.EmitOwnedPtr(getRHS());
120 S.EmitOwnedPtr(getSubStmt());
121 S.EmitPtr(getNextSwitchCase());
122}
123
124CaseStmt* CaseStmt::directMaterialize(llvm::Deserializer& D) {
125 SourceLocation CaseLoc = SourceLocation::ReadVal(D);
126 Expr* LHS = D.ReadOwnedPtr<Expr>();
127 Expr* RHS = D.ReadOwnedPtr<Expr>();
128 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
129
130 CaseStmt* stmt = new CaseStmt(LHS,RHS,SubStmt,CaseLoc);
131 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
132
133 return stmt;
134}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000135
Ted Kremenek47281102007-11-07 00:17:35 +0000136void CompoundStmt::directEmit(llvm::Serializer& S) const {
137 S.Emit(LBracLoc);
138 S.Emit(RBracLoc);
139 S.Emit(Body.size());
Ted Kremenek215c2c82007-10-31 18:41:19 +0000140
Ted Kremenek47281102007-11-07 00:17:35 +0000141 for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I)
Ted Kremenek215c2c82007-10-31 18:41:19 +0000142 S.EmitOwnedPtr(*I);
143}
144
Ted Kremenek47281102007-11-07 00:17:35 +0000145CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) {
146 SourceLocation LB = SourceLocation::ReadVal(D);
147 SourceLocation RB = SourceLocation::ReadVal(D);
148 unsigned size = D.ReadInt();
Ted Kremenek215c2c82007-10-31 18:41:19 +0000149
Ted Kremenek47281102007-11-07 00:17:35 +0000150 CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB);
151
152 stmt->Body.reserve(size);
153
154 for (unsigned i = 0; i < size; ++i)
155 stmt->Body.push_back(D.ReadOwnedPtr<Stmt>());
156
157 return stmt;
Ted Kremenek215c2c82007-10-31 18:41:19 +0000158}
Ted Kremenek47281102007-11-07 00:17:35 +0000159
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000160void DeclStmt::directEmit(llvm::Serializer& S) const {
161 // FIXME: special handling for struct decls.
162 S.EmitOwnedPtr(getDecl());
Ted Kremenek47281102007-11-07 00:17:35 +0000163}
164
165void DeclRefExpr::directEmit(llvm::Serializer& S) const {
166 S.Emit(Loc);
167 S.Emit(getType());
168 S.EmitPtr(getDecl());
169}
170
171DeclRefExpr* DeclRefExpr::directMaterialize(llvm::Deserializer& D) {
172 SourceLocation Loc = SourceLocation::ReadVal(D);
173 QualType T = QualType::ReadVal(D);
174 DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc);
175 D.ReadPtr(dr->D,false);
176 return dr;
177}
178
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000179DeclStmt* DeclStmt::directMaterialize(llvm::Deserializer& D) {
180 ScopedDecl* decl = cast<ScopedDecl>(D.ReadOwnedPtr<Decl>());
181 return new DeclStmt(decl);
182}
183
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000184void DefaultStmt::directEmit(llvm::Serializer& S) const {
185 S.Emit(DefaultLoc);
186 S.EmitOwnedPtr(getSubStmt());
187 S.EmitPtr(getNextSwitchCase());
188}
189
190DefaultStmt* DefaultStmt::directMaterialize(llvm::Deserializer& D) {
191 SourceLocation Loc = SourceLocation::ReadVal(D);
192 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
193
194 DefaultStmt* stmt = new DefaultStmt(Loc,SubStmt);
195 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
196
197 return stmt;
198}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000199
Ted Kremenekab97f4d2007-11-07 07:53:55 +0000200void DoStmt::directEmit(llvm::Serializer& S) const {
201 S.Emit(DoLoc);
202 S.EmitOwnedPtr(getCond());
203 S.EmitOwnedPtr(getBody());
204}
205
206DoStmt* DoStmt::directMaterialize(llvm::Deserializer& D) {
207 SourceLocation DoLoc = SourceLocation::ReadVal(D);
208 Expr* Cond = D.ReadOwnedPtr<Expr>();
209 Stmt* Body = D.ReadOwnedPtr<Stmt>();
210 return new DoStmt(Body,Cond,DoLoc);
211}
212
Ted Kremenekf34da902007-11-07 08:02:55 +0000213void ForStmt::directEmit(llvm::Serializer& S) const {
214 S.Emit(ForLoc);
215 S.EmitOwnedPtr(getInit());
216 S.EmitOwnedPtr(getCond());
217 S.EmitOwnedPtr(getInc());
218 S.EmitOwnedPtr(getBody());
219}
220
221ForStmt* ForStmt::directMaterialize(llvm::Deserializer& D) {
222 SourceLocation ForLoc = SourceLocation::ReadVal(D);
223 Stmt* Init = D.ReadOwnedPtr<Stmt>();
224 Expr* Cond = D.ReadOwnedPtr<Expr>();
225 Expr* Inc = D.ReadOwnedPtr<Expr>();
226 Stmt* Body = D.ReadOwnedPtr<Stmt>();
227 return new ForStmt(Init,Cond,Inc,Body,ForLoc);
228}
229
Ted Kremenekaffd8be2007-11-07 08:07:46 +0000230void GotoStmt::directEmit(llvm::Serializer& S) const {
231 S.Emit(GotoLoc);
232 S.Emit(LabelLoc);
233 S.EmitPtr(Label);
234}
235
236GotoStmt* GotoStmt::directMaterialize(llvm::Deserializer& D) {
237 SourceLocation GotoLoc = SourceLocation::ReadVal(D);
238 SourceLocation LabelLoc = SourceLocation::ReadVal(D);
239 GotoStmt* stmt = new GotoStmt(NULL,GotoLoc,LabelLoc);
240 D.ReadPtr(stmt->Label); // This pointer may be backpatched later.
241 return stmt;
242}
243
Ted Kremenekca22d352007-11-07 07:19:30 +0000244void IfStmt::directEmit(llvm::Serializer& S) const {
245 S.Emit(IfLoc);
246 S.EmitOwnedPtr(getCond());
247 S.EmitOwnedPtr(getThen());
248 S.EmitOwnedPtr(getElse());
249}
250
251IfStmt* IfStmt::directMaterialize(llvm::Deserializer& D) {
252 SourceLocation L = SourceLocation::ReadVal(D);
253 Expr* Cond = D.ReadOwnedPtr<Expr>();
254 Stmt* Then = D.ReadOwnedPtr<Stmt>();
255 Stmt* Else = D.ReadOwnedPtr<Stmt>();
256 return new IfStmt(L,Cond,Then,Else);
257}
258
Ted Kremenek47281102007-11-07 00:17:35 +0000259void IntegerLiteral::directEmit(llvm::Serializer& S) const {
260 S.Emit(Loc);
261 S.Emit(getType());
262 S.Emit(getValue());
263}
264
265IntegerLiteral* IntegerLiteral::directMaterialize(llvm::Deserializer& D) {
266 SourceLocation Loc = SourceLocation::ReadVal(D);
267 QualType T = QualType::ReadVal(D);
268
269 // Create a dummy APInt because it is more efficient to deserialize
270 // it in place with the deserialized IntegerLiteral. (fewer copies)
271 llvm::APInt temp;
272 IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc);
273 D.Read(expr->Value);
274
275 return expr;
276}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000277
Ted Kremenek56a74bb2007-11-07 00:48:04 +0000278void LabelStmt::directEmit(llvm::Serializer& S) const {
279 S.EmitPtr(Label);
280 S.Emit(IdentLoc);
281 S.EmitOwnedPtr(SubStmt);
282}
283
284LabelStmt* LabelStmt::directMaterialize(llvm::Deserializer& D) {
285 IdentifierInfo* Label = D.ReadPtr<IdentifierInfo>();
286 SourceLocation IdentLoc = SourceLocation::ReadVal(D);
287 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
288 return new LabelStmt(IdentLoc,Label,SubStmt);
289}
290
Ted Kremenek25aaa262007-11-07 00:40:53 +0000291void NullStmt::directEmit(llvm::Serializer& S) const {
292 S.Emit(SemiLoc);
293}
294
295NullStmt* NullStmt::directMaterialize(llvm::Deserializer& D) {
296 SourceLocation SemiLoc = SourceLocation::ReadVal(D);
297 return new NullStmt(SemiLoc);
298}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000299
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000300void ParenExpr::directEmit(llvm::Serializer& S) const {
301 S.Emit(L);
302 S.Emit(R);
303 S.EmitOwnedPtr(Val);
304}
305
306ParenExpr* ParenExpr::directMaterialize(llvm::Deserializer& D) {
307 SourceLocation L = SourceLocation::ReadVal(D);
308 SourceLocation R = SourceLocation::ReadVal(D);
309 Expr* val = D.ReadOwnedPtr<Expr>();
310 return new ParenExpr(L,R,val);
311}
312
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000313void ReturnStmt::directEmit(llvm::Serializer& S) const {
314 S.Emit(RetLoc);
315 S.EmitOwnedPtr(RetExpr);
316}
317
318ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) {
319 SourceLocation RetLoc = SourceLocation::ReadVal(D);
320 Expr* RetExpr = D.ReadOwnedPtr<Expr>();
321 return new ReturnStmt(RetLoc,RetExpr);
322}
323
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000324void SwitchStmt::directEmit(llvm::Serializer& S) const {
325 S.Emit(SwitchLoc);
326 S.EmitOwnedPtr(getCond());
327 S.EmitOwnedPtr(getBody());
328 S.EmitPtr(FirstCase);
329}
330
331SwitchStmt* SwitchStmt::directMaterialize(llvm::Deserializer& D) {
332 SourceLocation Loc = SourceLocation::ReadVal(D);
333 Stmt* Cond = D.ReadOwnedPtr<Stmt>();
334 Stmt* Body = D.ReadOwnedPtr<Stmt>();
335 SwitchCase* FirstCase = cast<SwitchCase>(D.ReadPtr<Stmt>());
336
337 SwitchStmt* stmt = new SwitchStmt(cast<Expr>(Cond));
338 stmt->setBody(Body,Loc);
339 stmt->FirstCase = FirstCase;
340
341 return stmt;
342}
Ted Kremenek55e559a2007-11-07 07:50:10 +0000343
344void WhileStmt::directEmit(llvm::Serializer& S) const {
345 S.Emit(WhileLoc);
346 S.EmitOwnedPtr(getCond());
347 S.EmitOwnedPtr(getBody());
348}
349
350WhileStmt* WhileStmt::directMaterialize(llvm::Deserializer& D) {
351 SourceLocation WhileLoc = SourceLocation::ReadVal(D);
352 Expr* Cond = D.ReadOwnedPtr<Expr>();
353 Stmt* Body = D.ReadOwnedPtr<Stmt>();
354 return new WhileStmt(Cond,Body,WhileLoc);
355}