/// FunctionDecl object to save an allocation like FunctionType does.
ParmVarDecl **ParamInfo;
- Stmt *Body;
+ LazyStmtPtr Body;
/// PreviousDeclaration - A link to the previous declaration of this
/// same function, NULL if this is the first declaration. For
bool isThisDeclarationADefinition() const { return Body; }
void setBody(CompoundStmt *B) { Body = (Stmt*) B; }
+ void setLazyBody(uint64_t Offset) { Body = Offset; }
/// Whether this function is virtual, either by explicit marking, or by
/// overriding a virtual function. Only valid on C++ member functions.
/// building a new declaration.
virtual Decl *GetDecl(unsigned ID) = 0;
+ /// \brief Resolve the offset of a statement into a statement.
+ ///
+ /// This operation will read a new statement from the external
+ /// source each time it is called, and is meant to be used via a
+ /// LazyOffsetPtr.
+ virtual Stmt *GetStmt(uint64_t Offset) = 0;
+
/// \brief Read all of the declarations lexically stored in a
/// declaration context.
///
virtual void PrintStats();
};
+/// \brief A lazy pointer to an AST node (of base type T) that resides
+/// within an external AST source.
+///
+/// The AST node is identified within the external AST source by a
+/// 63-bit offset, and can be retrieved via an operation on the
+/// external AST source itself.
+template<typename T, T* (ExternalASTSource::*Get)(uint64_t Offset)>
+struct LazyOffsetPtr {
+ /// \brief Either a pointer to an AST node or the offset within the
+ /// external AST source where the AST node can be found.
+ ///
+ /// If the low bit is clear, a pointer to the AST node. If the low
+ /// bit is set, the upper 63 bits are the offset.
+ mutable uint64_t Ptr;
+
+public:
+ LazyOffsetPtr() : Ptr(0) { }
+
+ explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
+ explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
+ assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
+ if (Offset == 0)
+ Ptr = 0;
+ }
+
+ LazyOffsetPtr &operator=(T *Ptr) {
+ this->Ptr = reinterpret_cast<uint64_t>(Ptr);
+ return *this;
+ }
+
+ LazyOffsetPtr &operator=(uint64_t Offset) {
+ assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
+ if (Offset == 0)
+ Ptr = 0;
+ else
+ Ptr = (Offset << 1) | 0x01;
+
+ return *this;
+ }
+
+ /// \brief Whether this pointer is non-NULL.
+ ///
+ /// This operation does not require the AST node to be deserialized.
+ operator bool() const { return Ptr != 0; }
+
+ /// \brief Whether this pointer is currently stored as an offset.
+ bool isOffset() const { return Ptr & 0x01; }
+
+ /// \brief Retrieve the pointer to the AST node that this lazy pointer
+ ///
+ /// \param Source the external AST source.
+ ///
+ /// \returns a pointer to the AST node.
+ T* get(ExternalASTSource *Source) const {
+ if (isOffset()) {
+ assert(Source &&
+ "Cannot deserialize a lazy pointer without an AST source");
+ Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
+ }
+ return reinterpret_cast<T*>(Ptr);
+ }
+};
+
+/// \brief A lazy pointer to a statement.
+typedef LazyOffsetPtr<Stmt, &ExternalASTSource::GetStmt> LazyStmtPtr;
+
} // end namespace clang
#endif // LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H
//===----------------------------------------------------------------------===//
void FunctionDecl::Destroy(ASTContext& C) {
- if (Body)
- Body->Destroy(C);
+ if (Body && Body.isOffset())
+ Body.get(C.getExternalSource())->Destroy(C);
for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
(*I)->Destroy(C);
for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
if (FD->Body) {
Definition = FD;
- return cast<CompoundStmt>(FD->Body);
+ return cast<CompoundStmt>(FD->Body.get(Context.getExternalSource()));
}
}
CompoundStmt *FunctionDecl::getBodyIfAvailable() const {
for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
- if (FD->Body)
- return cast<CompoundStmt>(FD->Body);
+ if (FD->Body && !FD->Body.isOffset()) {
+ return cast<CompoundStmt>(FD->Body.get(0));
+ }
}
return 0;
void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
VisitValueDecl(FD);
if (Record[Idx++])
- FD->setBody(cast<CompoundStmt>(Reader.ReadStmt()));
+ FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
FD->setPreviousDeclaration(
cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
return ReadDeclRecord(DeclOffsets[Index], Index);
}
+Stmt *PCHReader::GetStmt(uint64_t Offset) {
+ // Keep track of where we are in the stream, then jump back there
+ // after reading this declaration.
+ SavedStreamPosition SavedPosition(Stream);
+
+ Stream.JumpToBit(Offset);
+ return ReadStmt();
+}
+
bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
llvm::SmallVectorImpl<pch::DeclID> &Decls) {
assert(DC->hasExternalLexicalStorage() &&