//
//===----------------------------------------------------------------------===//
-#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
+ 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<Stmt>::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*>(&stmt));
+ for (unsigned i = 0; i < size; ++i)
+ stmt->Body.push_back(D.ReadOwnedPtr<Stmt>());
+
+ 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<Expr>();
+ 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<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;
}
-Stmt* SerializeTrait<Stmt>::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<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);
- }
- }
+ // 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;
}
+
}
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));
}
};
// 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__.
// 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 {
// 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,
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
// 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,
// 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.
} // 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