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.

llvm-svn: 43793
This commit is contained in:
Ted Kremenek 2007-11-07 00:17:35 +00:00
parent abcfe90802
commit 148aa5edbc
3 changed files with 132 additions and 84 deletions

View File

@ -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<StmtSerializer> {
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<StmtClass>(D.ReadInt());
void VisitStmt(Stmt*) { assert("Not Implemented yet"); }
};
} // end anonymous namespace
void SerializeTrait<Stmt>::Emit(Serializer& S, const Stmt& stmt) {
S.EmitInt(stmt.getStmtClass());
StmtSerializer SS(S);
SS.Visit(const_cast<Stmt*>(&stmt));
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 StmtSerializer::VisitDeclStmt(DeclStmt* Stmt) {
// FIXME
// S.EmitOwnedPtr(Stmt->getDecl());
}
void StmtSerializer::VisitNullStmt(NullStmt* Stmt) {
S.Emit(Stmt->getSemiLoc());
}
void StmtSerializer::VisitCompoundStmt(CompoundStmt* Stmt) {
S.Emit(Stmt->getLBracLoc());
S.Emit(Stmt->getRBracLoc());
CompoundStmt::body_iterator I=Stmt->body_begin(), E=Stmt->body_end();
S.EmitInt(E-I);
void CompoundStmt::directEmit(llvm::Serializer& S) const {
S.Emit(LBracLoc);
S.Emit(RBracLoc);
S.Emit(Body.size());
for ( ; I != E; ++I )
for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I)
S.EmitOwnedPtr(*I);
}
Stmt* SerializeTrait<Stmt>::Materialize(Deserializer& D) {
unsigned sClass = D.ReadInt();
CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) {
SourceLocation LB = SourceLocation::ReadVal(D);
SourceLocation RB = SourceLocation::ReadVal(D);
unsigned size = D.ReadInt();
switch (sClass) {
default:
assert(false && "No matching statement class.");
return NULL;
case Stmt::DeclStmtClass:
return NULL; // FIXME
// return new DeclStmt(D.ReadOwnedPtr<ScopedDecl>());
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<Stmt*, 16> Body;
for (unsigned i = 0 ; i < NumStmts; ++i)
Body.push_back(D.ReadOwnedPtr<Stmt>());
return new CompoundStmt(&Body[0],NumStmts,LBracLoc,RBracLoc);
}
}
CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB);
stmt->Body.reserve(size);
for (unsigned i = 0; i < size; ++i)
stmt->Body.push_back(D.ReadOwnedPtr<Stmt>());
return stmt;
}
void ReturnStmt::directEmit(llvm::Serializer& S) const {
S.Emit(RetLoc);
S.EmitOwnedPtr(RetExpr);
}
ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) {
SourceLocation RetLoc = SourceLocation::ReadVal(D);
Expr* RetExpr = D.ReadOwnedPtr<Expr>();
return new ReturnStmt(RetLoc,RetExpr);
}
void BinaryOperator::directEmit(llvm::Serializer& S) const {
S.EmitInt(Opc);
S.Emit(OpLoc);;
S.Emit(getType());
S.EmitOwnedPtr(getLHS());
S.EmitOwnedPtr(getRHS());
}
BinaryOperator* BinaryOperator::directMaterialize(llvm::Deserializer& D) {
Opcode Opc = static_cast<Opcode>(D.ReadInt());
SourceLocation OpLoc = SourceLocation::ReadVal(D);
QualType Result = QualType::ReadVal(D);
Expr* LHS = D.ReadOwnedPtr<Expr>();
Expr* RHS = D.ReadOwnedPtr<Expr>();
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;
}
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);
// 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;
}

View File

@ -111,12 +111,8 @@ public:
}
static bool classof(const Expr *) { return true; }
void Emit(llvm::Serializer& S) const {
llvm::SerializeTrait<Stmt>::Emit(S,*this);
}
static inline Expr* Materialize(llvm::Deserializer& D) {
return cast<Expr>(llvm::SerializeTrait<Stmt>::Materialize(D));
return cast<Expr>(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,

View File

@ -110,6 +110,14 @@ public:
const_child_iterator child_end() const {
return const_child_iterator(const_cast<Stmt*>(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<clang::Stmt> {
static void Emit(Serializer& S, const clang::Stmt& stmt);
static clang::Stmt* Materialize(Deserializer& D);
};
} // end namespace llvm
#endif