mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Relocate Mach-O AST section parsing from SerializedModuleLoader/ to ASTSectionImporter/.
Swift SVN r7869
This commit is contained in:
@@ -16,7 +16,6 @@
|
||||
#include "swift/AST/AST.h"
|
||||
#include "swift/AST/Component.h"
|
||||
#include "swift/AST/Diagnostics.h"
|
||||
#include "swift/Basic/Dwarf.h"
|
||||
#include "swift/Basic/STLExtras.h"
|
||||
#include "swift/Basic/SourceManager.h"
|
||||
#include "llvm/ADT/SmallString.h"
|
||||
@@ -81,67 +80,6 @@ static llvm::error_code findModule(ASTContext &ctx, AccessPathElem moduleID,
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
bool SerializedModuleLoader::
|
||||
addASTSection(std::unique_ptr<llvm::MemoryBuffer> MemoryBuffer,
|
||||
SmallVectorImpl<std::string> &foundModules) {
|
||||
struct apple_ast_hdr {
|
||||
uint32_t version;
|
||||
uint32_t nmods;
|
||||
};
|
||||
|
||||
struct module_header {
|
||||
uint64_t bitstream_ofs;
|
||||
uint64_t bitstream_size;
|
||||
uint64_t name_ofs;
|
||||
uint32_t language;
|
||||
uint32_t flags;
|
||||
};
|
||||
|
||||
if (MemoryBuffer->getBufferSize() < sizeof(struct apple_ast_hdr)
|
||||
+ sizeof(struct module_header)) {
|
||||
llvm::dbgs() << "__apple_ast section is too small.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t size = MemoryBuffer->getBufferSize();
|
||||
const char *data = MemoryBuffer->getBufferStart();
|
||||
auto apple_ast_hdr = reinterpret_cast<const struct apple_ast_hdr *>(data);
|
||||
if (apple_ast_hdr->version != 1) {
|
||||
llvm::dbgs() << "Unsupported __apple_ast section version.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Iterate over all AST modules.
|
||||
for (uint32_t i = 0; i < apple_ast_hdr->nmods; ++i) {
|
||||
auto mh = reinterpret_cast<const struct module_header *>
|
||||
(data+sizeof(apple_ast_hdr));
|
||||
|
||||
if (mh->language != dwarf::DW_LANG_Swift)
|
||||
continue;
|
||||
|
||||
// Get the access path.
|
||||
if (mh->name_ofs + 4 > size) return false;
|
||||
auto nchars = *reinterpret_cast<const uint32_t *>(data + mh->name_ofs);
|
||||
if (mh->name_ofs+sizeof(nchars) > size) return false;
|
||||
assert(nchars < (2 << 10) && "path failed sanity check");
|
||||
llvm::StringRef AccessPath(data+mh->name_ofs+sizeof(nchars), nchars);
|
||||
|
||||
// loadModule() wants to take ownership of the input memory buffer.
|
||||
// Copy the bitstream into a new memory buffer.
|
||||
if (mh->bitstream_ofs + mh->bitstream_size > size) return false;
|
||||
auto mem = llvm::StringRef(data+mh->bitstream_ofs, mh->bitstream_size);
|
||||
auto bitstream = llvm::MemoryBuffer::getMemBufferCopy(mem, AccessPath);
|
||||
|
||||
// Register the memory buffer.
|
||||
registerMemoryBuffer(AccessPath,
|
||||
std::unique_ptr<llvm::MemoryBuffer>(bitstream));
|
||||
foundModules.push_back(AccessPath);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Module *SerializedModuleLoader::loadModule(SourceLoc importLoc,
|
||||
Module::AccessPathTy path) {
|
||||
// FIXME: Swift submodules?
|
||||
|
||||
Reference in New Issue
Block a user