blob: 61e70ecde82229b8c07330b206bace44666f1f1b [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 Kremenek249d7cd2007-11-07 05:25:31 +000060
Ted Kremenekca22d352007-11-07 07:19:30 +000061 case IfStmtClass:
62 return IfStmt::directMaterialize(D);
63
Ted Kremenek47281102007-11-07 00:17:35 +000064 case IntegerLiteralClass:
Ted Kremenek25aaa262007-11-07 00:40:53 +000065 return IntegerLiteral::directMaterialize(D);
66
Ted Kremenek56a74bb2007-11-07 00:48:04 +000067 case LabelStmtClass:
68 return LabelStmt::directMaterialize(D);
69
Ted Kremenek25aaa262007-11-07 00:40:53 +000070 case NullStmtClass:
71 return NullStmt::directMaterialize(D);
Ted Kremenek2bd6e652007-11-07 00:37:40 +000072
Ted Kremenek249d7cd2007-11-07 05:25:31 +000073 case ParenExprClass:
74 return ParenExpr::directMaterialize(D);
75
Ted Kremenek2bd6e652007-11-07 00:37:40 +000076 case ReturnStmtClass:
Ted Kremenek249d7cd2007-11-07 05:25:31 +000077 return ReturnStmt::directMaterialize(D);
78
79 case SwitchStmtClass:
80 return SwitchStmt::directMaterialize(D);
Ted Kremenek55e559a2007-11-07 07:50:10 +000081
82 case WhileStmtClass:
83 return WhileStmt::directMaterialize(D);
Ted Kremenek47281102007-11-07 00:17:35 +000084 }
Ted Kremenek215c2c82007-10-31 18:41:19 +000085}
86
Ted Kremenek2bd6e652007-11-07 00:37:40 +000087void BinaryOperator::directEmit(llvm::Serializer& S) const {
88 S.EmitInt(Opc);
89 S.Emit(OpLoc);;
90 S.Emit(getType());
91 S.EmitOwnedPtr(getLHS());
92 S.EmitOwnedPtr(getRHS());
93}
94
95BinaryOperator* BinaryOperator::directMaterialize(llvm::Deserializer& D) {
96 Opcode Opc = static_cast<Opcode>(D.ReadInt());
97 SourceLocation OpLoc = SourceLocation::ReadVal(D);
98 QualType Result = QualType::ReadVal(D);
99 Expr* LHS = D.ReadOwnedPtr<Expr>();
100 Expr* RHS = D.ReadOwnedPtr<Expr>();
101 return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc);
102}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000103
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000104void BreakStmt::directEmit(llvm::Serializer& S) const {
105 S.Emit(BreakLoc);
106}
107
108BreakStmt* BreakStmt::directMaterialize(llvm::Deserializer& D) {
109 SourceLocation Loc = SourceLocation::ReadVal(D);
110 return new BreakStmt(Loc);
111}
112
113void CaseStmt::directEmit(llvm::Serializer& S) const {
114 S.Emit(CaseLoc);
115 S.EmitOwnedPtr(getLHS());
116 S.EmitOwnedPtr(getRHS());
117 S.EmitOwnedPtr(getSubStmt());
118 S.EmitPtr(getNextSwitchCase());
119}
120
121CaseStmt* CaseStmt::directMaterialize(llvm::Deserializer& D) {
122 SourceLocation CaseLoc = SourceLocation::ReadVal(D);
123 Expr* LHS = D.ReadOwnedPtr<Expr>();
124 Expr* RHS = D.ReadOwnedPtr<Expr>();
125 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
126
127 CaseStmt* stmt = new CaseStmt(LHS,RHS,SubStmt,CaseLoc);
128 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
129
130 return stmt;
131}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000132
Ted Kremenek47281102007-11-07 00:17:35 +0000133void CompoundStmt::directEmit(llvm::Serializer& S) const {
134 S.Emit(LBracLoc);
135 S.Emit(RBracLoc);
136 S.Emit(Body.size());
Ted Kremenek215c2c82007-10-31 18:41:19 +0000137
Ted Kremenek47281102007-11-07 00:17:35 +0000138 for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I)
Ted Kremenek215c2c82007-10-31 18:41:19 +0000139 S.EmitOwnedPtr(*I);
140}
141
Ted Kremenek47281102007-11-07 00:17:35 +0000142CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) {
143 SourceLocation LB = SourceLocation::ReadVal(D);
144 SourceLocation RB = SourceLocation::ReadVal(D);
145 unsigned size = D.ReadInt();
Ted Kremenek215c2c82007-10-31 18:41:19 +0000146
Ted Kremenek47281102007-11-07 00:17:35 +0000147 CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB);
148
149 stmt->Body.reserve(size);
150
151 for (unsigned i = 0; i < size; ++i)
152 stmt->Body.push_back(D.ReadOwnedPtr<Stmt>());
153
154 return stmt;
Ted Kremenek215c2c82007-10-31 18:41:19 +0000155}
Ted Kremenek47281102007-11-07 00:17:35 +0000156
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000157void DeclStmt::directEmit(llvm::Serializer& S) const {
158 // FIXME: special handling for struct decls.
159 S.EmitOwnedPtr(getDecl());
Ted Kremenek47281102007-11-07 00:17:35 +0000160}
161
162void DeclRefExpr::directEmit(llvm::Serializer& S) const {
163 S.Emit(Loc);
164 S.Emit(getType());
165 S.EmitPtr(getDecl());
166}
167
168DeclRefExpr* DeclRefExpr::directMaterialize(llvm::Deserializer& D) {
169 SourceLocation Loc = SourceLocation::ReadVal(D);
170 QualType T = QualType::ReadVal(D);
171 DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc);
172 D.ReadPtr(dr->D,false);
173 return dr;
174}
175
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000176DeclStmt* DeclStmt::directMaterialize(llvm::Deserializer& D) {
177 ScopedDecl* decl = cast<ScopedDecl>(D.ReadOwnedPtr<Decl>());
178 return new DeclStmt(decl);
179}
180
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000181void DefaultStmt::directEmit(llvm::Serializer& S) const {
182 S.Emit(DefaultLoc);
183 S.EmitOwnedPtr(getSubStmt());
184 S.EmitPtr(getNextSwitchCase());
185}
186
187DefaultStmt* DefaultStmt::directMaterialize(llvm::Deserializer& D) {
188 SourceLocation Loc = SourceLocation::ReadVal(D);
189 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
190
191 DefaultStmt* stmt = new DefaultStmt(Loc,SubStmt);
192 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
193
194 return stmt;
195}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000196
Ted Kremenekab97f4d2007-11-07 07:53:55 +0000197void DoStmt::directEmit(llvm::Serializer& S) const {
198 S.Emit(DoLoc);
199 S.EmitOwnedPtr(getCond());
200 S.EmitOwnedPtr(getBody());
201}
202
203DoStmt* DoStmt::directMaterialize(llvm::Deserializer& D) {
204 SourceLocation DoLoc = SourceLocation::ReadVal(D);
205 Expr* Cond = D.ReadOwnedPtr<Expr>();
206 Stmt* Body = D.ReadOwnedPtr<Stmt>();
207 return new DoStmt(Body,Cond,DoLoc);
208}
209
Ted Kremenekf34da902007-11-07 08:02:55 +0000210void ForStmt::directEmit(llvm::Serializer& S) const {
211 S.Emit(ForLoc);
212 S.EmitOwnedPtr(getInit());
213 S.EmitOwnedPtr(getCond());
214 S.EmitOwnedPtr(getInc());
215 S.EmitOwnedPtr(getBody());
216}
217
218ForStmt* ForStmt::directMaterialize(llvm::Deserializer& D) {
219 SourceLocation ForLoc = SourceLocation::ReadVal(D);
220 Stmt* Init = D.ReadOwnedPtr<Stmt>();
221 Expr* Cond = D.ReadOwnedPtr<Expr>();
222 Expr* Inc = D.ReadOwnedPtr<Expr>();
223 Stmt* Body = D.ReadOwnedPtr<Stmt>();
224 return new ForStmt(Init,Cond,Inc,Body,ForLoc);
225}
226
Ted Kremenekca22d352007-11-07 07:19:30 +0000227void IfStmt::directEmit(llvm::Serializer& S) const {
228 S.Emit(IfLoc);
229 S.EmitOwnedPtr(getCond());
230 S.EmitOwnedPtr(getThen());
231 S.EmitOwnedPtr(getElse());
232}
233
234IfStmt* IfStmt::directMaterialize(llvm::Deserializer& D) {
235 SourceLocation L = SourceLocation::ReadVal(D);
236 Expr* Cond = D.ReadOwnedPtr<Expr>();
237 Stmt* Then = D.ReadOwnedPtr<Stmt>();
238 Stmt* Else = D.ReadOwnedPtr<Stmt>();
239 return new IfStmt(L,Cond,Then,Else);
240}
241
Ted Kremenek47281102007-11-07 00:17:35 +0000242void IntegerLiteral::directEmit(llvm::Serializer& S) const {
243 S.Emit(Loc);
244 S.Emit(getType());
245 S.Emit(getValue());
246}
247
248IntegerLiteral* IntegerLiteral::directMaterialize(llvm::Deserializer& D) {
249 SourceLocation Loc = SourceLocation::ReadVal(D);
250 QualType T = QualType::ReadVal(D);
251
252 // Create a dummy APInt because it is more efficient to deserialize
253 // it in place with the deserialized IntegerLiteral. (fewer copies)
254 llvm::APInt temp;
255 IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc);
256 D.Read(expr->Value);
257
258 return expr;
259}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000260
Ted Kremenek56a74bb2007-11-07 00:48:04 +0000261void LabelStmt::directEmit(llvm::Serializer& S) const {
262 S.EmitPtr(Label);
263 S.Emit(IdentLoc);
264 S.EmitOwnedPtr(SubStmt);
265}
266
267LabelStmt* LabelStmt::directMaterialize(llvm::Deserializer& D) {
268 IdentifierInfo* Label = D.ReadPtr<IdentifierInfo>();
269 SourceLocation IdentLoc = SourceLocation::ReadVal(D);
270 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
271 return new LabelStmt(IdentLoc,Label,SubStmt);
272}
273
Ted Kremenek25aaa262007-11-07 00:40:53 +0000274void NullStmt::directEmit(llvm::Serializer& S) const {
275 S.Emit(SemiLoc);
276}
277
278NullStmt* NullStmt::directMaterialize(llvm::Deserializer& D) {
279 SourceLocation SemiLoc = SourceLocation::ReadVal(D);
280 return new NullStmt(SemiLoc);
281}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000282
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000283void ParenExpr::directEmit(llvm::Serializer& S) const {
284 S.Emit(L);
285 S.Emit(R);
286 S.EmitOwnedPtr(Val);
287}
288
289ParenExpr* ParenExpr::directMaterialize(llvm::Deserializer& D) {
290 SourceLocation L = SourceLocation::ReadVal(D);
291 SourceLocation R = SourceLocation::ReadVal(D);
292 Expr* val = D.ReadOwnedPtr<Expr>();
293 return new ParenExpr(L,R,val);
294}
295
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000296void ReturnStmt::directEmit(llvm::Serializer& S) const {
297 S.Emit(RetLoc);
298 S.EmitOwnedPtr(RetExpr);
299}
300
301ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) {
302 SourceLocation RetLoc = SourceLocation::ReadVal(D);
303 Expr* RetExpr = D.ReadOwnedPtr<Expr>();
304 return new ReturnStmt(RetLoc,RetExpr);
305}
306
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000307void SwitchStmt::directEmit(llvm::Serializer& S) const {
308 S.Emit(SwitchLoc);
309 S.EmitOwnedPtr(getCond());
310 S.EmitOwnedPtr(getBody());
311 S.EmitPtr(FirstCase);
312}
313
314SwitchStmt* SwitchStmt::directMaterialize(llvm::Deserializer& D) {
315 SourceLocation Loc = SourceLocation::ReadVal(D);
316 Stmt* Cond = D.ReadOwnedPtr<Stmt>();
317 Stmt* Body = D.ReadOwnedPtr<Stmt>();
318 SwitchCase* FirstCase = cast<SwitchCase>(D.ReadPtr<Stmt>());
319
320 SwitchStmt* stmt = new SwitchStmt(cast<Expr>(Cond));
321 stmt->setBody(Body,Loc);
322 stmt->FirstCase = FirstCase;
323
324 return stmt;
325}
Ted Kremenek55e559a2007-11-07 07:50:10 +0000326
327void WhileStmt::directEmit(llvm::Serializer& S) const {
328 S.Emit(WhileLoc);
329 S.EmitOwnedPtr(getCond());
330 S.EmitOwnedPtr(getBody());
331}
332
333WhileStmt* WhileStmt::directMaterialize(llvm::Deserializer& D) {
334 SourceLocation WhileLoc = SourceLocation::ReadVal(D);
335 Expr* Cond = D.ReadOwnedPtr<Expr>();
336 Stmt* Body = D.ReadOwnedPtr<Stmt>();
337 return new WhileStmt(Cond,Body,WhileLoc);
338}