blob: d7b497155a40755340328cadd7c318bfe399efe6 [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 Kremenek249d7cd2007-11-07 05:25:31 +000057
Ted Kremenekca22d352007-11-07 07:19:30 +000058 case IfStmtClass:
59 return IfStmt::directMaterialize(D);
60
Ted Kremenek47281102007-11-07 00:17:35 +000061 case IntegerLiteralClass:
Ted Kremenek25aaa262007-11-07 00:40:53 +000062 return IntegerLiteral::directMaterialize(D);
63
Ted Kremenek56a74bb2007-11-07 00:48:04 +000064 case LabelStmtClass:
65 return LabelStmt::directMaterialize(D);
66
Ted Kremenek25aaa262007-11-07 00:40:53 +000067 case NullStmtClass:
68 return NullStmt::directMaterialize(D);
Ted Kremenek2bd6e652007-11-07 00:37:40 +000069
Ted Kremenek249d7cd2007-11-07 05:25:31 +000070 case ParenExprClass:
71 return ParenExpr::directMaterialize(D);
72
Ted Kremenek2bd6e652007-11-07 00:37:40 +000073 case ReturnStmtClass:
Ted Kremenek249d7cd2007-11-07 05:25:31 +000074 return ReturnStmt::directMaterialize(D);
75
76 case SwitchStmtClass:
77 return SwitchStmt::directMaterialize(D);
Ted Kremenek55e559a2007-11-07 07:50:10 +000078
79 case WhileStmtClass:
80 return WhileStmt::directMaterialize(D);
Ted Kremenek47281102007-11-07 00:17:35 +000081 }
Ted Kremenek215c2c82007-10-31 18:41:19 +000082}
83
Ted Kremenek2bd6e652007-11-07 00:37:40 +000084void BinaryOperator::directEmit(llvm::Serializer& S) const {
85 S.EmitInt(Opc);
86 S.Emit(OpLoc);;
87 S.Emit(getType());
88 S.EmitOwnedPtr(getLHS());
89 S.EmitOwnedPtr(getRHS());
90}
91
92BinaryOperator* BinaryOperator::directMaterialize(llvm::Deserializer& D) {
93 Opcode Opc = static_cast<Opcode>(D.ReadInt());
94 SourceLocation OpLoc = SourceLocation::ReadVal(D);
95 QualType Result = QualType::ReadVal(D);
96 Expr* LHS = D.ReadOwnedPtr<Expr>();
97 Expr* RHS = D.ReadOwnedPtr<Expr>();
98 return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc);
99}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000100
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000101void BreakStmt::directEmit(llvm::Serializer& S) const {
102 S.Emit(BreakLoc);
103}
104
105BreakStmt* BreakStmt::directMaterialize(llvm::Deserializer& D) {
106 SourceLocation Loc = SourceLocation::ReadVal(D);
107 return new BreakStmt(Loc);
108}
109
110void CaseStmt::directEmit(llvm::Serializer& S) const {
111 S.Emit(CaseLoc);
112 S.EmitOwnedPtr(getLHS());
113 S.EmitOwnedPtr(getRHS());
114 S.EmitOwnedPtr(getSubStmt());
115 S.EmitPtr(getNextSwitchCase());
116}
117
118CaseStmt* CaseStmt::directMaterialize(llvm::Deserializer& D) {
119 SourceLocation CaseLoc = SourceLocation::ReadVal(D);
120 Expr* LHS = D.ReadOwnedPtr<Expr>();
121 Expr* RHS = D.ReadOwnedPtr<Expr>();
122 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
123
124 CaseStmt* stmt = new CaseStmt(LHS,RHS,SubStmt,CaseLoc);
125 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
126
127 return stmt;
128}
Ted Kremenek215c2c82007-10-31 18:41:19 +0000129
Ted Kremenek47281102007-11-07 00:17:35 +0000130void CompoundStmt::directEmit(llvm::Serializer& S) const {
131 S.Emit(LBracLoc);
132 S.Emit(RBracLoc);
133 S.Emit(Body.size());
Ted Kremenek215c2c82007-10-31 18:41:19 +0000134
Ted Kremenek47281102007-11-07 00:17:35 +0000135 for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I)
Ted Kremenek215c2c82007-10-31 18:41:19 +0000136 S.EmitOwnedPtr(*I);
137}
138
Ted Kremenek47281102007-11-07 00:17:35 +0000139CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) {
140 SourceLocation LB = SourceLocation::ReadVal(D);
141 SourceLocation RB = SourceLocation::ReadVal(D);
142 unsigned size = D.ReadInt();
Ted Kremenek215c2c82007-10-31 18:41:19 +0000143
Ted Kremenek47281102007-11-07 00:17:35 +0000144 CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB);
145
146 stmt->Body.reserve(size);
147
148 for (unsigned i = 0; i < size; ++i)
149 stmt->Body.push_back(D.ReadOwnedPtr<Stmt>());
150
151 return stmt;
Ted Kremenek215c2c82007-10-31 18:41:19 +0000152}
Ted Kremenek47281102007-11-07 00:17:35 +0000153
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000154void DeclStmt::directEmit(llvm::Serializer& S) const {
155 // FIXME: special handling for struct decls.
156 S.EmitOwnedPtr(getDecl());
Ted Kremenek47281102007-11-07 00:17:35 +0000157}
158
159void DeclRefExpr::directEmit(llvm::Serializer& S) const {
160 S.Emit(Loc);
161 S.Emit(getType());
162 S.EmitPtr(getDecl());
163}
164
165DeclRefExpr* DeclRefExpr::directMaterialize(llvm::Deserializer& D) {
166 SourceLocation Loc = SourceLocation::ReadVal(D);
167 QualType T = QualType::ReadVal(D);
168 DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc);
169 D.ReadPtr(dr->D,false);
170 return dr;
171}
172
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000173DeclStmt* DeclStmt::directMaterialize(llvm::Deserializer& D) {
174 ScopedDecl* decl = cast<ScopedDecl>(D.ReadOwnedPtr<Decl>());
175 return new DeclStmt(decl);
176}
177
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000178void DefaultStmt::directEmit(llvm::Serializer& S) const {
179 S.Emit(DefaultLoc);
180 S.EmitOwnedPtr(getSubStmt());
181 S.EmitPtr(getNextSwitchCase());
182}
183
184DefaultStmt* DefaultStmt::directMaterialize(llvm::Deserializer& D) {
185 SourceLocation Loc = SourceLocation::ReadVal(D);
186 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
187
188 DefaultStmt* stmt = new DefaultStmt(Loc,SubStmt);
189 stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
190
191 return stmt;
192}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000193
Ted Kremenekab97f4d2007-11-07 07:53:55 +0000194void DoStmt::directEmit(llvm::Serializer& S) const {
195 S.Emit(DoLoc);
196 S.EmitOwnedPtr(getCond());
197 S.EmitOwnedPtr(getBody());
198}
199
200DoStmt* DoStmt::directMaterialize(llvm::Deserializer& D) {
201 SourceLocation DoLoc = SourceLocation::ReadVal(D);
202 Expr* Cond = D.ReadOwnedPtr<Expr>();
203 Stmt* Body = D.ReadOwnedPtr<Stmt>();
204 return new DoStmt(Body,Cond,DoLoc);
205}
206
Ted Kremenekca22d352007-11-07 07:19:30 +0000207void IfStmt::directEmit(llvm::Serializer& S) const {
208 S.Emit(IfLoc);
209 S.EmitOwnedPtr(getCond());
210 S.EmitOwnedPtr(getThen());
211 S.EmitOwnedPtr(getElse());
212}
213
214IfStmt* IfStmt::directMaterialize(llvm::Deserializer& D) {
215 SourceLocation L = SourceLocation::ReadVal(D);
216 Expr* Cond = D.ReadOwnedPtr<Expr>();
217 Stmt* Then = D.ReadOwnedPtr<Stmt>();
218 Stmt* Else = D.ReadOwnedPtr<Stmt>();
219 return new IfStmt(L,Cond,Then,Else);
220}
221
Ted Kremenek47281102007-11-07 00:17:35 +0000222void IntegerLiteral::directEmit(llvm::Serializer& S) const {
223 S.Emit(Loc);
224 S.Emit(getType());
225 S.Emit(getValue());
226}
227
228IntegerLiteral* IntegerLiteral::directMaterialize(llvm::Deserializer& D) {
229 SourceLocation Loc = SourceLocation::ReadVal(D);
230 QualType T = QualType::ReadVal(D);
231
232 // Create a dummy APInt because it is more efficient to deserialize
233 // it in place with the deserialized IntegerLiteral. (fewer copies)
234 llvm::APInt temp;
235 IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc);
236 D.Read(expr->Value);
237
238 return expr;
239}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000240
Ted Kremenek56a74bb2007-11-07 00:48:04 +0000241void LabelStmt::directEmit(llvm::Serializer& S) const {
242 S.EmitPtr(Label);
243 S.Emit(IdentLoc);
244 S.EmitOwnedPtr(SubStmt);
245}
246
247LabelStmt* LabelStmt::directMaterialize(llvm::Deserializer& D) {
248 IdentifierInfo* Label = D.ReadPtr<IdentifierInfo>();
249 SourceLocation IdentLoc = SourceLocation::ReadVal(D);
250 Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
251 return new LabelStmt(IdentLoc,Label,SubStmt);
252}
253
Ted Kremenek25aaa262007-11-07 00:40:53 +0000254void NullStmt::directEmit(llvm::Serializer& S) const {
255 S.Emit(SemiLoc);
256}
257
258NullStmt* NullStmt::directMaterialize(llvm::Deserializer& D) {
259 SourceLocation SemiLoc = SourceLocation::ReadVal(D);
260 return new NullStmt(SemiLoc);
261}
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000262
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000263void ParenExpr::directEmit(llvm::Serializer& S) const {
264 S.Emit(L);
265 S.Emit(R);
266 S.EmitOwnedPtr(Val);
267}
268
269ParenExpr* ParenExpr::directMaterialize(llvm::Deserializer& D) {
270 SourceLocation L = SourceLocation::ReadVal(D);
271 SourceLocation R = SourceLocation::ReadVal(D);
272 Expr* val = D.ReadOwnedPtr<Expr>();
273 return new ParenExpr(L,R,val);
274}
275
Ted Kremenek2bd6e652007-11-07 00:37:40 +0000276void ReturnStmt::directEmit(llvm::Serializer& S) const {
277 S.Emit(RetLoc);
278 S.EmitOwnedPtr(RetExpr);
279}
280
281ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) {
282 SourceLocation RetLoc = SourceLocation::ReadVal(D);
283 Expr* RetExpr = D.ReadOwnedPtr<Expr>();
284 return new ReturnStmt(RetLoc,RetExpr);
285}
286
Ted Kremenek249d7cd2007-11-07 05:25:31 +0000287void SwitchStmt::directEmit(llvm::Serializer& S) const {
288 S.Emit(SwitchLoc);
289 S.EmitOwnedPtr(getCond());
290 S.EmitOwnedPtr(getBody());
291 S.EmitPtr(FirstCase);
292}
293
294SwitchStmt* SwitchStmt::directMaterialize(llvm::Deserializer& D) {
295 SourceLocation Loc = SourceLocation::ReadVal(D);
296 Stmt* Cond = D.ReadOwnedPtr<Stmt>();
297 Stmt* Body = D.ReadOwnedPtr<Stmt>();
298 SwitchCase* FirstCase = cast<SwitchCase>(D.ReadPtr<Stmt>());
299
300 SwitchStmt* stmt = new SwitchStmt(cast<Expr>(Cond));
301 stmt->setBody(Body,Loc);
302 stmt->FirstCase = FirstCase;
303
304 return stmt;
305}
Ted Kremenek55e559a2007-11-07 07:50:10 +0000306
307void WhileStmt::directEmit(llvm::Serializer& S) const {
308 S.Emit(WhileLoc);
309 S.EmitOwnedPtr(getCond());
310 S.EmitOwnedPtr(getBody());
311}
312
313WhileStmt* WhileStmt::directMaterialize(llvm::Deserializer& D) {
314 SourceLocation WhileLoc = SourceLocation::ReadVal(D);
315 Expr* Cond = D.ReadOwnedPtr<Expr>();
316 Stmt* Body = D.ReadOwnedPtr<Stmt>();
317 return new WhileStmt(Cond,Body,WhileLoc);
318}