From 2dc9ac73a8a34cabf261a81a1653d7379065ac61 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Wed, 7 Nov 2007 00:17:35 +0000 Subject: [PATCH] Overhauled serialization of statements. We no longer use specialized SerializeTrait<> classes, but methods in Stmt (like other objects we are serializing). Full serialization of all statements is not yet complete. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43793 91177308-0d34-0410-b5e6-96231b3b80d8 --- AST/StmtSerialization.cpp | 164 ++++++++++++++++++++++++-------------- include/clang/AST/Expr.h | 15 ++-- include/clang/AST/Stmt.h | 27 ++++--- 3 files changed, 127 insertions(+), 79 deletions(-) diff --git a/AST/StmtSerialization.cpp b/AST/StmtSerialization.cpp index 5972c8cf80..95897fd975 100644 --- a/AST/StmtSerialization.cpp +++ b/AST/StmtSerialization.cpp @@ -12,86 +12,128 @@ // //===----------------------------------------------------------------------===// -#include "clang/AST/Stmt.h" -#include "clang/AST/StmtVisitor.h" +#include "clang/AST/Expr.h" #include "llvm/Bitcode/Serialize.h" #include "llvm/Bitcode/Deserialize.h" -using llvm::Serializer; -using llvm::Deserializer; -using llvm::SerializeTrait; - using namespace clang; +void Stmt::Emit(llvm::Serializer& S) const { + S.EmitInt(getStmtClass()); + directEmit(S); +} -namespace { -class StmtSerializer : public StmtVisitor { - Serializer& S; -public: - StmtSerializer(Serializer& S) : S(S) {} - - void VisitDeclStmt(DeclStmt* Stmt); - void VisitNullStmt(NullStmt* Stmt); - void VisitCompoundStmt(CompoundStmt* Stmt); +Stmt* Stmt::Materialize(llvm::Deserializer& D) { + StmtClass SC = static_cast(D.ReadInt()); - void VisitStmt(Stmt*) { assert("Not Implemented yet"); } -}; -} // end anonymous namespace + switch (SC) { + default: + assert (false && "Not implemented."); + return NULL; + + case CompoundStmtClass: + return CompoundStmt::directMaterialize(D); + + case ReturnStmtClass: + return ReturnStmt::directMaterialize(D); + + case BinaryOperatorClass: + return BinaryOperator::directMaterialize(D); + + case DeclRefExprClass: + return DeclRefExpr::directMaterialize(D); + + case IntegerLiteralClass: + return IntegerLiteral::directMaterialize(D); + } +} + -void SerializeTrait::Emit(Serializer& S, const Stmt& stmt) { - S.EmitInt(stmt.getStmtClass()); +void CompoundStmt::directEmit(llvm::Serializer& S) const { + S.Emit(LBracLoc); + S.Emit(RBracLoc); + S.Emit(Body.size()); + + for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I) + S.EmitOwnedPtr(*I); +} + +CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) { + SourceLocation LB = SourceLocation::ReadVal(D); + SourceLocation RB = SourceLocation::ReadVal(D); + unsigned size = D.ReadInt(); + + CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB); + + stmt->Body.reserve(size); - StmtSerializer SS(S); - SS.Visit(const_cast(&stmt)); + for (unsigned i = 0; i < size; ++i) + stmt->Body.push_back(D.ReadOwnedPtr()); + + return stmt; } -void StmtSerializer::VisitDeclStmt(DeclStmt* Stmt) { - // FIXME - // S.EmitOwnedPtr(Stmt->getDecl()); + +void ReturnStmt::directEmit(llvm::Serializer& S) const { + S.Emit(RetLoc); + S.EmitOwnedPtr(RetExpr); } -void StmtSerializer::VisitNullStmt(NullStmt* Stmt) { - S.Emit(Stmt->getSemiLoc()); +ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) { + SourceLocation RetLoc = SourceLocation::ReadVal(D); + Expr* RetExpr = D.ReadOwnedPtr(); + return new ReturnStmt(RetLoc,RetExpr); } -void StmtSerializer::VisitCompoundStmt(CompoundStmt* Stmt) { - S.Emit(Stmt->getLBracLoc()); - S.Emit(Stmt->getRBracLoc()); - CompoundStmt::body_iterator I=Stmt->body_begin(), E=Stmt->body_end(); +void BinaryOperator::directEmit(llvm::Serializer& S) const { + S.EmitInt(Opc); + S.Emit(OpLoc);; + S.Emit(getType()); + S.EmitOwnedPtr(getLHS()); + S.EmitOwnedPtr(getRHS()); +} - S.EmitInt(E-I); - - for ( ; I != E; ++I ) - S.EmitOwnedPtr(*I); +BinaryOperator* BinaryOperator::directMaterialize(llvm::Deserializer& D) { + Opcode Opc = static_cast(D.ReadInt()); + SourceLocation OpLoc = SourceLocation::ReadVal(D); + QualType Result = QualType::ReadVal(D); + Expr* LHS = D.ReadOwnedPtr(); + Expr* RHS = D.ReadOwnedPtr(); + return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc); +} + +void DeclRefExpr::directEmit(llvm::Serializer& S) const { + S.Emit(Loc); + S.Emit(getType()); + S.EmitPtr(getDecl()); +} + +DeclRefExpr* DeclRefExpr::directMaterialize(llvm::Deserializer& D) { + SourceLocation Loc = SourceLocation::ReadVal(D); + QualType T = QualType::ReadVal(D); + DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc); + D.ReadPtr(dr->D,false); + return dr; } -Stmt* SerializeTrait::Materialize(Deserializer& D) { - unsigned sClass = D.ReadInt(); +void IntegerLiteral::directEmit(llvm::Serializer& S) const { + S.Emit(Loc); + S.Emit(getType()); + S.Emit(getValue()); +} + +IntegerLiteral* IntegerLiteral::directMaterialize(llvm::Deserializer& D) { + SourceLocation Loc = SourceLocation::ReadVal(D); + QualType T = QualType::ReadVal(D); - switch (sClass) { - default: - assert(false && "No matching statement class."); - return NULL; - - case Stmt::DeclStmtClass: - return NULL; // FIXME -// return new DeclStmt(D.ReadOwnedPtr()); - - case Stmt::NullStmtClass: - return new NullStmt(SourceLocation::ReadVal(D)); - - case Stmt::CompoundStmtClass: { - SourceLocation LBracLoc = SourceLocation::ReadVal(D); - SourceLocation RBracLoc = SourceLocation::ReadVal(D); - unsigned NumStmts = D.ReadInt(); - llvm::SmallVector Body; - - for (unsigned i = 0 ; i < NumStmts; ++i) - Body.push_back(D.ReadOwnedPtr()); - - return new CompoundStmt(&Body[0],NumStmts,LBracLoc,RBracLoc); - } - } + // Create a dummy APInt because it is more efficient to deserialize + // it in place with the deserialized IntegerLiteral. (fewer copies) + llvm::APInt temp; + IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc); + D.Read(expr->Value); + + return expr; } + diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 3b8a1c0138..ce77dafdf7 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -111,12 +111,8 @@ public: } static bool classof(const Expr *) { return true; } - void Emit(llvm::Serializer& S) const { - llvm::SerializeTrait::Emit(S,*this); - } - static inline Expr* Materialize(llvm::Deserializer& D) { - return cast(llvm::SerializeTrait::Materialize(D)); + return cast(Stmt::Materialize(D)); } }; @@ -146,6 +142,9 @@ public: // Iterators virtual child_iterator child_begin(); virtual child_iterator child_end(); + + virtual void directEmit(llvm::Serializer& S) const; + static DeclRefExpr* directMaterialize(llvm::Deserializer& D); }; /// PreDefinedExpr - [C99 6.4.2.2] - A pre-defined identifier such as __func__. @@ -199,6 +198,9 @@ public: // Iterators virtual child_iterator child_begin(); virtual child_iterator child_end(); + + virtual void directEmit(llvm::Serializer& S) const; + static IntegerLiteral* directMaterialize(llvm::Deserializer& D); }; class CharacterLiteral : public Expr { @@ -799,6 +801,9 @@ public: // Iterators virtual child_iterator child_begin(); virtual child_iterator child_end(); + + virtual void directEmit(llvm::Serializer& S) const; + static BinaryOperator* directMaterialize(llvm::Deserializer& D); protected: BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index 5a7c0524cd..9f79fdcd91 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -110,6 +110,14 @@ public: const_child_iterator child_end() const { return const_child_iterator(const_cast(this)->child_end()); } + + void Emit(llvm::Serializer& S) const; + static Stmt* Materialize(llvm::Deserializer& D); + + virtual void directEmit(llvm::Serializer& S) const { + // This method will eventually be a pure-virtual function. + assert (false && "Not implemented."); + } }; /// DeclStmt - Adaptor class for mixing declarations with statements and @@ -207,6 +215,9 @@ public: // Iterators virtual child_iterator child_begin(); virtual child_iterator child_end(); + + virtual void directEmit(llvm::Serializer& S) const; + static CompoundStmt* directMaterialize(llvm::Deserializer& D); }; // SwitchCase is the base class for CaseStmt and DefaultStmt, @@ -636,6 +647,9 @@ public: // Iterators virtual child_iterator child_begin(); virtual child_iterator child_end(); + + virtual void directEmit(llvm::Serializer& S) const; + static ReturnStmt* directMaterialize(llvm::Deserializer& D); }; /// AsmStmt - This represents a GNU inline-assembly statement extension. @@ -779,17 +793,4 @@ public: } // end namespace clang -//===----------------------------------------------------------------------===// -// For Stmt serialization. -//===----------------------------------------------------------------------===// - -namespace llvm { - -template<> struct SerializeTrait { - static void Emit(Serializer& S, const clang::Stmt& stmt); - static clang::Stmt* Materialize(Deserializer& D); -}; - -} // end namespace llvm - #endif -- 2.40.0