From 1f1e7564834577ae031120d8988c26cb5fdd0ced Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Thu, 25 Oct 2007 21:37:16 +0000 Subject: [PATCH] Added skeleton for Decl serialization. llvm-svn: 43361 --- clang/AST/DeclSerialization.cpp | 62 +++++++++++++++++++++++++++++++++ clang/include/clang/AST/Decl.h | 22 +++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 clang/AST/DeclSerialization.cpp diff --git a/clang/AST/DeclSerialization.cpp b/clang/AST/DeclSerialization.cpp new file mode 100644 index 000000000000..9b2278a4bebb --- /dev/null +++ b/clang/AST/DeclSerialization.cpp @@ -0,0 +1,62 @@ +//===--- DeclSerialization.cpp - Serialization of Decls ---------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Ted Kremenek and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This files defines methods that implement bitcode serialization for Decls. +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/Decl.h" +#include "clang/AST/Expr.h" +#include "llvm/Bitcode/Serialize.h" +#include "llvm/Bitcode/Deserialize.h" + +using llvm::SerializeTrait; +using llvm::Deserializer; +using llvm::Serializer; +using namespace clang; + + +static void EmitEnumConstantDecl(Serializer& S, EnumConstantDecl& decl) { + S.Emit(decl.getLocation()); + S.EmitPtr(decl.getIdentifier()); +// S.Emit(decl.getType()); FIXME + S.EmitOwnedPtr(decl.getInitExpr()); + // S.Emit(decl.getInitVal()); FIXME + S.EmitOwnedPtr(decl.getNextDeclarator()); +} + +static void EmitFunctionDecl(Serializer& S, FunctionDecl& decl) { + S.Emit(decl.getLocation()); + S.EmitPtr(decl.getIdentifier()); +// S.Emit(decl.getType()); FIXME +// S.Emit(decl.getStorageClass()); FIXME + S.EmitBool(decl.isInline()); + S.EmitOwnedPtr(decl.getNextDeclarator()); +} + + +void SerializeTrait::Emit(Serializer& S, Decl& decl) { + assert (!decl.isInvalidDecl() && "Can only serialize valid decls."); + + S.EmitInt((unsigned) decl.getKind()); + + switch (decl.getKind()) { + default: + assert (false && "Serialization not implemented for decl type."); + return; + + case Decl::EnumConstant: + EmitEnumConstantDecl(S,cast(decl)); + return; + + case Decl::Function: + EmitFunctionDecl(S,cast(decl)); + return; + } +} diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index d06481fbdd39..5788dc40f55b 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -17,6 +17,18 @@ #include "clang/Basic/SourceLocation.h" #include "clang/AST/Type.h" #include "llvm/ADT/APSInt.h" +#include "llvm/Bitcode/Serialization.h" + +namespace clang { +class Decl; +} + +namespace llvm { +template <> struct SerializeTrait { + static void Emit(Serializer& S, clang::Decl& D); + static clang::Decl* Materialize(Deserializer& D); +}; +} // end namespace llvm namespace clang { class Expr; @@ -113,7 +125,7 @@ public: /// setInvalidDecl - Indicates the Decl had a semantic error. This /// allows for graceful error recovery. void setInvalidDecl() { InvalidDecl = 1; } - int isInvalidDecl() const { return InvalidDecl; } + bool isInvalidDecl() const { return (bool) InvalidDecl; } IdentifierNamespace getIdentifierNamespace() const { switch (DeclKind) { @@ -138,6 +150,13 @@ public: static void addDeclKind(const Kind k); static bool CollectingStats(bool enable=false); static void PrintStats(); + + // Deserialization of Decls. + template + static inline DeclType* DeserializeDecl(llvm::Deserializer& D) { + Decl* decl = llvm::SerializeTrait::Materialize(D); + return cast(decl); + } // Implement isa/cast/dyncast/etc. static bool classof(const Decl *) { return true; } @@ -575,4 +594,5 @@ public: }; } // end namespace clang + #endif