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); |
| 54 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 55 | case IntegerLiteralClass: |
Ted Kremenek | 25aaa26 | 2007-11-07 00:40:53 +0000 | [diff] [blame] | 56 | return IntegerLiteral::directMaterialize(D); |
| 57 | |
Ted Kremenek | 56a74bb | 2007-11-07 00:48:04 +0000 | [diff] [blame] | 58 | case LabelStmtClass: |
| 59 | return LabelStmt::directMaterialize(D); |
| 60 | |
Ted Kremenek | 25aaa26 | 2007-11-07 00:40:53 +0000 | [diff] [blame] | 61 | case NullStmtClass: |
| 62 | return NullStmt::directMaterialize(D); |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 64 | case ParenExprClass: |
| 65 | return ParenExpr::directMaterialize(D); |
| 66 | |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 67 | case ReturnStmtClass: |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 68 | return ReturnStmt::directMaterialize(D); |
| 69 | |
| 70 | case SwitchStmtClass: |
| 71 | return SwitchStmt::directMaterialize(D); |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 72 | } |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 75 | void BinaryOperator::directEmit(llvm::Serializer& S) const { |
| 76 | S.EmitInt(Opc); |
| 77 | S.Emit(OpLoc);; |
| 78 | S.Emit(getType()); |
| 79 | S.EmitOwnedPtr(getLHS()); |
| 80 | S.EmitOwnedPtr(getRHS()); |
| 81 | } |
| 82 | |
| 83 | BinaryOperator* BinaryOperator::directMaterialize(llvm::Deserializer& D) { |
| 84 | Opcode Opc = static_cast<Opcode>(D.ReadInt()); |
| 85 | SourceLocation OpLoc = SourceLocation::ReadVal(D); |
| 86 | QualType Result = QualType::ReadVal(D); |
| 87 | Expr* LHS = D.ReadOwnedPtr<Expr>(); |
| 88 | Expr* RHS = D.ReadOwnedPtr<Expr>(); |
| 89 | return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc); |
| 90 | } |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 92 | void BreakStmt::directEmit(llvm::Serializer& S) const { |
| 93 | S.Emit(BreakLoc); |
| 94 | } |
| 95 | |
| 96 | BreakStmt* BreakStmt::directMaterialize(llvm::Deserializer& D) { |
| 97 | SourceLocation Loc = SourceLocation::ReadVal(D); |
| 98 | return new BreakStmt(Loc); |
| 99 | } |
| 100 | |
| 101 | void CaseStmt::directEmit(llvm::Serializer& S) const { |
| 102 | S.Emit(CaseLoc); |
| 103 | S.EmitOwnedPtr(getLHS()); |
| 104 | S.EmitOwnedPtr(getRHS()); |
| 105 | S.EmitOwnedPtr(getSubStmt()); |
| 106 | S.EmitPtr(getNextSwitchCase()); |
| 107 | } |
| 108 | |
| 109 | CaseStmt* CaseStmt::directMaterialize(llvm::Deserializer& D) { |
| 110 | SourceLocation CaseLoc = SourceLocation::ReadVal(D); |
| 111 | Expr* LHS = D.ReadOwnedPtr<Expr>(); |
| 112 | Expr* RHS = D.ReadOwnedPtr<Expr>(); |
| 113 | Stmt* SubStmt = D.ReadOwnedPtr<Stmt>(); |
| 114 | |
| 115 | CaseStmt* stmt = new CaseStmt(LHS,RHS,SubStmt,CaseLoc); |
| 116 | stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>()); |
| 117 | |
| 118 | return stmt; |
| 119 | } |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 121 | void CompoundStmt::directEmit(llvm::Serializer& S) const { |
| 122 | S.Emit(LBracLoc); |
| 123 | S.Emit(RBracLoc); |
| 124 | S.Emit(Body.size()); |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 126 | 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] | 127 | S.EmitOwnedPtr(*I); |
| 128 | } |
| 129 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 130 | CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) { |
| 131 | SourceLocation LB = SourceLocation::ReadVal(D); |
| 132 | SourceLocation RB = SourceLocation::ReadVal(D); |
| 133 | unsigned size = D.ReadInt(); |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 135 | CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB); |
| 136 | |
| 137 | stmt->Body.reserve(size); |
| 138 | |
| 139 | for (unsigned i = 0; i < size; ++i) |
| 140 | stmt->Body.push_back(D.ReadOwnedPtr<Stmt>()); |
| 141 | |
| 142 | return stmt; |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 143 | } |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 145 | void DeclStmt::directEmit(llvm::Serializer& S) const { |
| 146 | // FIXME: special handling for struct decls. |
| 147 | S.EmitOwnedPtr(getDecl()); |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void DeclRefExpr::directEmit(llvm::Serializer& S) const { |
| 151 | S.Emit(Loc); |
| 152 | S.Emit(getType()); |
| 153 | S.EmitPtr(getDecl()); |
| 154 | } |
| 155 | |
| 156 | DeclRefExpr* DeclRefExpr::directMaterialize(llvm::Deserializer& D) { |
| 157 | SourceLocation Loc = SourceLocation::ReadVal(D); |
| 158 | QualType T = QualType::ReadVal(D); |
| 159 | DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc); |
| 160 | D.ReadPtr(dr->D,false); |
| 161 | return dr; |
| 162 | } |
| 163 | |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 164 | DeclStmt* DeclStmt::directMaterialize(llvm::Deserializer& D) { |
| 165 | ScopedDecl* decl = cast<ScopedDecl>(D.ReadOwnedPtr<Decl>()); |
| 166 | return new DeclStmt(decl); |
| 167 | } |
| 168 | |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 169 | void DefaultStmt::directEmit(llvm::Serializer& S) const { |
| 170 | S.Emit(DefaultLoc); |
| 171 | S.EmitOwnedPtr(getSubStmt()); |
| 172 | S.EmitPtr(getNextSwitchCase()); |
| 173 | } |
| 174 | |
| 175 | DefaultStmt* DefaultStmt::directMaterialize(llvm::Deserializer& D) { |
| 176 | SourceLocation Loc = SourceLocation::ReadVal(D); |
| 177 | Stmt* SubStmt = D.ReadOwnedPtr<Stmt>(); |
| 178 | |
| 179 | DefaultStmt* stmt = new DefaultStmt(Loc,SubStmt); |
| 180 | stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>()); |
| 181 | |
| 182 | return stmt; |
| 183 | } |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame] | 185 | void IntegerLiteral::directEmit(llvm::Serializer& S) const { |
| 186 | S.Emit(Loc); |
| 187 | S.Emit(getType()); |
| 188 | S.Emit(getValue()); |
| 189 | } |
| 190 | |
| 191 | IntegerLiteral* IntegerLiteral::directMaterialize(llvm::Deserializer& D) { |
| 192 | SourceLocation Loc = SourceLocation::ReadVal(D); |
| 193 | QualType T = QualType::ReadVal(D); |
| 194 | |
| 195 | // Create a dummy APInt because it is more efficient to deserialize |
| 196 | // it in place with the deserialized IntegerLiteral. (fewer copies) |
| 197 | llvm::APInt temp; |
| 198 | IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc); |
| 199 | D.Read(expr->Value); |
| 200 | |
| 201 | return expr; |
| 202 | } |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | 56a74bb | 2007-11-07 00:48:04 +0000 | [diff] [blame] | 204 | void LabelStmt::directEmit(llvm::Serializer& S) const { |
| 205 | S.EmitPtr(Label); |
| 206 | S.Emit(IdentLoc); |
| 207 | S.EmitOwnedPtr(SubStmt); |
| 208 | } |
| 209 | |
| 210 | LabelStmt* LabelStmt::directMaterialize(llvm::Deserializer& D) { |
| 211 | IdentifierInfo* Label = D.ReadPtr<IdentifierInfo>(); |
| 212 | SourceLocation IdentLoc = SourceLocation::ReadVal(D); |
| 213 | Stmt* SubStmt = D.ReadOwnedPtr<Stmt>(); |
| 214 | return new LabelStmt(IdentLoc,Label,SubStmt); |
| 215 | } |
| 216 | |
Ted Kremenek | 25aaa26 | 2007-11-07 00:40:53 +0000 | [diff] [blame] | 217 | void NullStmt::directEmit(llvm::Serializer& S) const { |
| 218 | S.Emit(SemiLoc); |
| 219 | } |
| 220 | |
| 221 | NullStmt* NullStmt::directMaterialize(llvm::Deserializer& D) { |
| 222 | SourceLocation SemiLoc = SourceLocation::ReadVal(D); |
| 223 | return new NullStmt(SemiLoc); |
| 224 | } |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 226 | void ParenExpr::directEmit(llvm::Serializer& S) const { |
| 227 | S.Emit(L); |
| 228 | S.Emit(R); |
| 229 | S.EmitOwnedPtr(Val); |
| 230 | } |
| 231 | |
| 232 | ParenExpr* ParenExpr::directMaterialize(llvm::Deserializer& D) { |
| 233 | SourceLocation L = SourceLocation::ReadVal(D); |
| 234 | SourceLocation R = SourceLocation::ReadVal(D); |
| 235 | Expr* val = D.ReadOwnedPtr<Expr>(); |
| 236 | return new ParenExpr(L,R,val); |
| 237 | } |
| 238 | |
Ted Kremenek | 2bd6e65 | 2007-11-07 00:37:40 +0000 | [diff] [blame] | 239 | void ReturnStmt::directEmit(llvm::Serializer& S) const { |
| 240 | S.Emit(RetLoc); |
| 241 | S.EmitOwnedPtr(RetExpr); |
| 242 | } |
| 243 | |
| 244 | ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) { |
| 245 | SourceLocation RetLoc = SourceLocation::ReadVal(D); |
| 246 | Expr* RetExpr = D.ReadOwnedPtr<Expr>(); |
| 247 | return new ReturnStmt(RetLoc,RetExpr); |
| 248 | } |
| 249 | |
Ted Kremenek | 249d7cd | 2007-11-07 05:25:31 +0000 | [diff] [blame] | 250 | void SwitchStmt::directEmit(llvm::Serializer& S) const { |
| 251 | S.Emit(SwitchLoc); |
| 252 | S.EmitOwnedPtr(getCond()); |
| 253 | S.EmitOwnedPtr(getBody()); |
| 254 | S.EmitPtr(FirstCase); |
| 255 | } |
| 256 | |
| 257 | SwitchStmt* SwitchStmt::directMaterialize(llvm::Deserializer& D) { |
| 258 | SourceLocation Loc = SourceLocation::ReadVal(D); |
| 259 | Stmt* Cond = D.ReadOwnedPtr<Stmt>(); |
| 260 | Stmt* Body = D.ReadOwnedPtr<Stmt>(); |
| 261 | SwitchCase* FirstCase = cast<SwitchCase>(D.ReadPtr<Stmt>()); |
| 262 | |
| 263 | SwitchStmt* stmt = new SwitchStmt(cast<Expr>(Cond)); |
| 264 | stmt->setBody(Body,Loc); |
| 265 | stmt->FirstCase = FirstCase; |
| 266 | |
| 267 | return stmt; |
| 268 | } |