Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 1 | //===--- 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 Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/Serialize.h" |
| 17 | #include "llvm/Bitcode/Deserialize.h" |
| 18 | |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 21 | void Stmt::Emit(llvm::Serializer& S) const { |
| 22 | S.EmitInt(getStmtClass()); |
| 23 | directEmit(S); |
| 24 | } |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 26 | Stmt* Stmt::Materialize(llvm::Deserializer& D) { |
| 27 | StmtClass SC = static_cast<StmtClass>(D.ReadInt()); |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 29 | switch (SC) { |
| 30 | default: |
| 31 | assert (false && "Not implemented."); |
| 32 | return NULL; |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 33 | |
| 34 | case BinaryOperatorClass: |
| 35 | return BinaryOperator::directMaterialize(D); |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 37 | case BreakStmtClass: |
| 38 | return BreakStmt::directMaterialize(D); |
| 39 | |
| 40 | case CaseStmtClass: |
| 41 | return CaseStmt::directMaterialize(D); |
| 42 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 43 | case CompoundStmtClass: |
| 44 | return CompoundStmt::directMaterialize(D); |
| 45 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 46 | case DeclRefExprClass: |
| 47 | return DeclRefExpr::directMaterialize(D); |
| 48 | |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 49 | case DeclStmtClass: |
| 50 | return DeclStmt::directMaterialize(D); |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 51 | |
| 52 | case DefaultStmtClass: |
| 53 | return DefaultStmt::directMaterialize(D); |
Ted Kremenek | ab97f4d | 2007-11-07 07:53:55 +0000 | [diff] [blame] | 54 | |
| 55 | case DoStmtClass: |
| 56 | return DoStmt::directMaterialize(D); |
Ted Kremenek | f34da90 | 2007-11-07 08:02:55 +0000 | [diff] [blame] | 57 | |
| 58 | case ForStmtClass: |
| 59 | return ForStmt::directMaterialize(D); |
Ted Kremenek | affd8be | 2007-11-07 08:07:46 +0000 | [diff] [blame^] | 60 | |
| 61 | case GotoStmtClass: |
| 62 | return GotoStmt::directMaterialize(D); |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | ca22d35 | 2007-11-07 07:19:30 +0000 | [diff] [blame] | 64 | case IfStmtClass: |
| 65 | return IfStmt::directMaterialize(D); |
| 66 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 67 | case IntegerLiteralClass: |
Ted Kremenek | 25aaa26 | 2007-11-07 00:40:53 +0000 | [diff] [blame] | 68 | return IntegerLiteral::directMaterialize(D); |
| 69 | |
Ted Kremenek | 56a74bb | 2007-11-07 00:48:04 +0000 | [diff] [blame] | 70 | case LabelStmtClass: |
| 71 | return LabelStmt::directMaterialize(D); |
| 72 | |
Ted Kremenek | 25aaa26 | 2007-11-07 00:40:53 +0000 | [diff] [blame] | 73 | case NullStmtClass: |
| 74 | return NullStmt::directMaterialize(D); |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 76 | case ParenExprClass: |
| 77 | return ParenExpr::directMaterialize(D); |
| 78 | |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 79 | case ReturnStmtClass: |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 80 | return ReturnStmt::directMaterialize(D); |
| 81 | |
| 82 | case SwitchStmtClass: |
| 83 | return SwitchStmt::directMaterialize(D); |
Ted Kremenek | 55e559a | 2007-11-07 07:50:10 +0000 | [diff] [blame] | 84 | |
| 85 | case WhileStmtClass: |
| 86 | return WhileStmt::directMaterialize(D); |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 87 | } |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 90 | void 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 | |
| 98 | BinaryOperator* 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 Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 107 | void BreakStmt::directEmit(llvm::Serializer& S) const { |
| 108 | S.Emit(BreakLoc); |
| 109 | } |
| 110 | |
| 111 | BreakStmt* BreakStmt::directMaterialize(llvm::Deserializer& D) { |
| 112 | SourceLocation Loc = SourceLocation::ReadVal(D); |
| 113 | return new BreakStmt(Loc); |
| 114 | } |
| 115 | |
| 116 | void 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 | |
| 124 | CaseStmt* 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 Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 136 | void CompoundStmt::directEmit(llvm::Serializer& S) const { |
| 137 | S.Emit(LBracLoc); |
| 138 | S.Emit(RBracLoc); |
| 139 | S.Emit(Body.size()); |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 141 | for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I) |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 142 | S.EmitOwnedPtr(*I); |
| 143 | } |
| 144 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 145 | CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) { |
| 146 | SourceLocation LB = SourceLocation::ReadVal(D); |
| 147 | SourceLocation RB = SourceLocation::ReadVal(D); |
| 148 | unsigned size = D.ReadInt(); |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 149 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 150 | 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 Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 158 | } |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 160 | void DeclStmt::directEmit(llvm::Serializer& S) const { |
| 161 | // FIXME: special handling for struct decls. |
| 162 | S.EmitOwnedPtr(getDecl()); |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void DeclRefExpr::directEmit(llvm::Serializer& S) const { |
| 166 | S.Emit(Loc); |
| 167 | S.Emit(getType()); |
| 168 | S.EmitPtr(getDecl()); |
| 169 | } |
| 170 | |
| 171 | DeclRefExpr* 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 Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 179 | DeclStmt* DeclStmt::directMaterialize(llvm::Deserializer& D) { |
| 180 | ScopedDecl* decl = cast<ScopedDecl>(D.ReadOwnedPtr<Decl>()); |
| 181 | return new DeclStmt(decl); |
| 182 | } |
| 183 | |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 184 | void DefaultStmt::directEmit(llvm::Serializer& S) const { |
| 185 | S.Emit(DefaultLoc); |
| 186 | S.EmitOwnedPtr(getSubStmt()); |
| 187 | S.EmitPtr(getNextSwitchCase()); |
| 188 | } |
| 189 | |
| 190 | DefaultStmt* 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 Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | ab97f4d | 2007-11-07 07:53:55 +0000 | [diff] [blame] | 200 | void DoStmt::directEmit(llvm::Serializer& S) const { |
| 201 | S.Emit(DoLoc); |
| 202 | S.EmitOwnedPtr(getCond()); |
| 203 | S.EmitOwnedPtr(getBody()); |
| 204 | } |
| 205 | |
| 206 | DoStmt* 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 Kremenek | f34da90 | 2007-11-07 08:02:55 +0000 | [diff] [blame] | 213 | void 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 | |
| 221 | ForStmt* 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 Kremenek | affd8be | 2007-11-07 08:07:46 +0000 | [diff] [blame^] | 230 | void GotoStmt::directEmit(llvm::Serializer& S) const { |
| 231 | S.Emit(GotoLoc); |
| 232 | S.Emit(LabelLoc); |
| 233 | S.EmitPtr(Label); |
| 234 | } |
| 235 | |
| 236 | GotoStmt* 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 Kremenek | ca22d35 | 2007-11-07 07:19:30 +0000 | [diff] [blame] | 244 | void IfStmt::directEmit(llvm::Serializer& S) const { |
| 245 | S.Emit(IfLoc); |
| 246 | S.EmitOwnedPtr(getCond()); |
| 247 | S.EmitOwnedPtr(getThen()); |
| 248 | S.EmitOwnedPtr(getElse()); |
| 249 | } |
| 250 | |
| 251 | IfStmt* 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 Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 259 | void IntegerLiteral::directEmit(llvm::Serializer& S) const { |
| 260 | S.Emit(Loc); |
| 261 | S.Emit(getType()); |
| 262 | S.Emit(getValue()); |
| 263 | } |
| 264 | |
| 265 | IntegerLiteral* 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 Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 277 | |
Ted Kremenek | 56a74bb | 2007-11-07 00:48:04 +0000 | [diff] [blame] | 278 | void LabelStmt::directEmit(llvm::Serializer& S) const { |
| 279 | S.EmitPtr(Label); |
| 280 | S.Emit(IdentLoc); |
| 281 | S.EmitOwnedPtr(SubStmt); |
| 282 | } |
| 283 | |
| 284 | LabelStmt* 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 Kremenek | 25aaa26 | 2007-11-07 00:40:53 +0000 | [diff] [blame] | 291 | void NullStmt::directEmit(llvm::Serializer& S) const { |
| 292 | S.Emit(SemiLoc); |
| 293 | } |
| 294 | |
| 295 | NullStmt* NullStmt::directMaterialize(llvm::Deserializer& D) { |
| 296 | SourceLocation SemiLoc = SourceLocation::ReadVal(D); |
| 297 | return new NullStmt(SemiLoc); |
| 298 | } |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 299 | |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 300 | void ParenExpr::directEmit(llvm::Serializer& S) const { |
| 301 | S.Emit(L); |
| 302 | S.Emit(R); |
| 303 | S.EmitOwnedPtr(Val); |
| 304 | } |
| 305 | |
| 306 | ParenExpr* 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 Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 313 | void ReturnStmt::directEmit(llvm::Serializer& S) const { |
| 314 | S.Emit(RetLoc); |
| 315 | S.EmitOwnedPtr(RetExpr); |
| 316 | } |
| 317 | |
| 318 | ReturnStmt* 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 Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 324 | void SwitchStmt::directEmit(llvm::Serializer& S) const { |
| 325 | S.Emit(SwitchLoc); |
| 326 | S.EmitOwnedPtr(getCond()); |
| 327 | S.EmitOwnedPtr(getBody()); |
| 328 | S.EmitPtr(FirstCase); |
| 329 | } |
| 330 | |
| 331 | SwitchStmt* 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 Kremenek | 55e559a | 2007-11-07 07:50:10 +0000 | [diff] [blame] | 343 | |
| 344 | void WhileStmt::directEmit(llvm::Serializer& S) const { |
| 345 | S.Emit(WhileLoc); |
| 346 | S.EmitOwnedPtr(getCond()); |
| 347 | S.EmitOwnedPtr(getBody()); |
| 348 | } |
| 349 | |
| 350 | WhileStmt* 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 | } |