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; |
| 33 | |
| 34 | case CompoundStmtClass: |
| 35 | return CompoundStmt::directMaterialize(D); |
| 36 | |
| 37 | case ReturnStmtClass: |
| 38 | return ReturnStmt::directMaterialize(D); |
| 39 | |
| 40 | case BinaryOperatorClass: |
| 41 | return BinaryOperator::directMaterialize(D); |
| 42 | |
| 43 | case DeclRefExprClass: |
| 44 | return DeclRefExpr::directMaterialize(D); |
| 45 | |
| 46 | case IntegerLiteralClass: |
| 47 | return IntegerLiteral::directMaterialize(D); |
| 48 | } |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 52 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame^] | 53 | void CompoundStmt::directEmit(llvm::Serializer& S) const { |
| 54 | S.Emit(LBracLoc); |
| 55 | S.Emit(RBracLoc); |
| 56 | S.Emit(Body.size()); |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame^] | 58 | 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] | 59 | S.EmitOwnedPtr(*I); |
| 60 | } |
| 61 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame^] | 62 | CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) { |
| 63 | SourceLocation LB = SourceLocation::ReadVal(D); |
| 64 | SourceLocation RB = SourceLocation::ReadVal(D); |
| 65 | unsigned size = D.ReadInt(); |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame^] | 67 | CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB); |
| 68 | |
| 69 | stmt->Body.reserve(size); |
| 70 | |
| 71 | for (unsigned i = 0; i < size; ++i) |
| 72 | stmt->Body.push_back(D.ReadOwnedPtr<Stmt>()); |
| 73 | |
| 74 | return stmt; |
Ted Kremenek | 215c2c8 | 2007-10-31 18:41:19 +0000 | [diff] [blame] | 75 | } |
Ted Kremenek | 4728110 | 2007-11-07 00:17:35 +0000 | [diff] [blame^] | 76 | |
| 77 | |
| 78 | void ReturnStmt::directEmit(llvm::Serializer& S) const { |
| 79 | S.Emit(RetLoc); |
| 80 | S.EmitOwnedPtr(RetExpr); |
| 81 | } |
| 82 | |
| 83 | ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) { |
| 84 | SourceLocation RetLoc = SourceLocation::ReadVal(D); |
| 85 | Expr* RetExpr = D.ReadOwnedPtr<Expr>(); |
| 86 | return new ReturnStmt(RetLoc,RetExpr); |
| 87 | } |
| 88 | |
| 89 | |
| 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 | } |
| 106 | |
| 107 | void DeclRefExpr::directEmit(llvm::Serializer& S) const { |
| 108 | S.Emit(Loc); |
| 109 | S.Emit(getType()); |
| 110 | S.EmitPtr(getDecl()); |
| 111 | } |
| 112 | |
| 113 | DeclRefExpr* DeclRefExpr::directMaterialize(llvm::Deserializer& D) { |
| 114 | SourceLocation Loc = SourceLocation::ReadVal(D); |
| 115 | QualType T = QualType::ReadVal(D); |
| 116 | DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc); |
| 117 | D.ReadPtr(dr->D,false); |
| 118 | return dr; |
| 119 | } |
| 120 | |
| 121 | void IntegerLiteral::directEmit(llvm::Serializer& S) const { |
| 122 | S.Emit(Loc); |
| 123 | S.Emit(getType()); |
| 124 | S.Emit(getValue()); |
| 125 | } |
| 126 | |
| 127 | IntegerLiteral* IntegerLiteral::directMaterialize(llvm::Deserializer& D) { |
| 128 | SourceLocation Loc = SourceLocation::ReadVal(D); |
| 129 | QualType T = QualType::ReadVal(D); |
| 130 | |
| 131 | // Create a dummy APInt because it is more efficient to deserialize |
| 132 | // it in place with the deserialized IntegerLiteral. (fewer copies) |
| 133 | llvm::APInt temp; |
| 134 | IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc); |
| 135 | D.Read(expr->Value); |
| 136 | |
| 137 | return expr; |
| 138 | } |
| 139 | |