mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Relocate Mach-O AST section parsing from SerializedModuleLoader/ to ASTSectionImporter/.
Swift SVN r7869
This commit is contained in:
42
include/swift/ASTSectionImporter/ASTSectionImporter.h
Normal file
42
include/swift/ASTSectionImporter/ASTSectionImporter.h
Normal file
@@ -0,0 +1,42 @@
|
||||
//===--- ASTSectionImporter.cpp - Import AST Section Modules ---*- C++ -*--===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See http://swift.org/LICENSE.txt for license information
|
||||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file implements support for loading modules serialized into a
|
||||
// Mach-O AST section into Swift.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#ifndef SWIFT_ASTSECTION_IMPORTER_H
|
||||
#define SWIFT_ASTSECTION_IMPORTER_H
|
||||
|
||||
#include "llvm/ADT/SmallVector.h"
|
||||
#include <memory>
|
||||
|
||||
namespace llvm {
|
||||
class MemoryBuffer;
|
||||
}
|
||||
namespace swift {
|
||||
class SerializedModuleLoader;
|
||||
|
||||
/// \brief Povided a memory buffer with an entire Mach-O __apple_ast
|
||||
/// section, this function makes memory buffer copies of all swift
|
||||
/// modules found in it and registers them using
|
||||
/// registerMemoryBuffer() so they can be found by loadModule(). The
|
||||
/// the access path of all modules found in the section is appended
|
||||
/// to the vector foundModules.
|
||||
/// \return true if successful.
|
||||
bool parseASTSection(SerializedModuleLoader* SML,
|
||||
const std::unique_ptr<llvm::MemoryBuffer> &data,
|
||||
SmallVectorImpl<std::string> &foundModules);
|
||||
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -13,6 +13,7 @@
|
||||
#ifndef SWIFT_SERIALIZATION_MODULELOADER_H
|
||||
#define SWIFT_SERIALIZATION_MODULELOADER_H
|
||||
|
||||
#include "swift/Basic/Dwarf.h"
|
||||
#include "swift/AST/ModuleLoader.h"
|
||||
#include "llvm/ADT/OwningPtr.h"
|
||||
#include "llvm/Support/MemoryBuffer.h"
|
||||
@@ -70,16 +71,6 @@ public:
|
||||
MemoryBuffers[AccessPath].reset(input.release());
|
||||
}
|
||||
|
||||
/// \brief Povided a memory buffer with an entire Mach-O __apple_ast
|
||||
/// section, this function makes memory buffer copies of all swift
|
||||
/// modules found in it and registers them using
|
||||
/// registerMemoryBuffer() so they can be found by loadModule(). The
|
||||
/// the access path of all modules found in the section is appended
|
||||
/// to the vector foundModules.
|
||||
/// \return true if successful.
|
||||
bool addASTSection(std::unique_ptr<llvm::MemoryBuffer> data,
|
||||
SmallVectorImpl<std::string> &foundModules);
|
||||
|
||||
/// \brief Look for declarations associated with the given name.
|
||||
///
|
||||
/// \param module The module to search.
|
||||
|
||||
83
lib/ASTSectionImporter/ASTSectionImporter.cpp
Normal file
83
lib/ASTSectionImporter/ASTSectionImporter.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
//===--- ASTSectionImporter.cpp - Import AST Section Modules --------------===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See http://swift.org/LICENSE.txt for license information
|
||||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file implements support for loading modules serialized into a
|
||||
// Mach-O AST section into Swift.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "swift/Serialization/SerializedModuleLoader.h"
|
||||
#include "llvm/Support/Debug.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
|
||||
namespace swift {
|
||||
|
||||
bool parseASTSection(SerializedModuleLoader* SML,
|
||||
const 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.
|
||||
SML->registerMemoryBuffer(AccessPath,
|
||||
std::unique_ptr<llvm::MemoryBuffer>(bitstream));
|
||||
foundModules.push_back(AccessPath);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
4
lib/ASTSectionImporter/CMakeLists.txt
Normal file
4
lib/ASTSectionImporter/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
add_swift_library(swiftASTSectionImporter
|
||||
ASTSectionImporter.cpp
|
||||
DEPENDS swiftBasic LLVMCore)
|
||||
|
||||
19
lib/ASTSectionImporter/Makefile
Normal file
19
lib/ASTSectionImporter/Makefile
Normal file
@@ -0,0 +1,19 @@
|
||||
##===- swift/lib/ASTSectionImporter/Makefile --------------*- Makefile -*-===##
|
||||
#
|
||||
# This source file is part of the Swift.org open source project
|
||||
#
|
||||
# Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
||||
# Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
#
|
||||
# See http://swift.org/LICENSE.txt for license information
|
||||
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
#
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
SWIFT_LEVEL := ../..
|
||||
include $(SWIFT_LEVEL)/../../Makefile.config
|
||||
|
||||
LIBRARYNAME := swiftASTSectionImporter
|
||||
|
||||
include $(SWIFT_LEVEL)/Makefile
|
||||
|
||||
@@ -8,6 +8,7 @@ add_subdirectory(Parse)
|
||||
add_subdirectory(Sema)
|
||||
add_subdirectory(ClangImporter)
|
||||
add_subdirectory(NullClangImporter)
|
||||
add_subdirectory(ASTSectionImporter)
|
||||
add_subdirectory(Serialization)
|
||||
add_subdirectory(IDE)
|
||||
add_subdirectory(Frontend)
|
||||
|
||||
@@ -12,6 +12,6 @@
|
||||
SWIFT_LEVEL := ..
|
||||
|
||||
PARALLEL_DIRS = Basic AST Sema Parse SIL SILGen SILPasses IRGen ClangImporter \
|
||||
NullClangImporter Serialization IDE Frontend
|
||||
NullClangImporter ASTSectionImporter Serialization IDE Frontend
|
||||
|
||||
include $(SWIFT_LEVEL)/Makefile
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
add_swift_executable(lldb-moduleimport-test
|
||||
lldb-moduleimport-test.cpp
|
||||
DEPENDS swiftParse swiftSema swiftClangImporter swiftAST swiftSerialization swiftFrontend
|
||||
DEPENDS swiftParse swiftSema swiftClangImporter swiftAST swiftASTSectionImporter swiftSerialization swiftFrontend
|
||||
COMPONENT_DEPENDS support ${LLVM_TARGETS_TO_BUILD})
|
||||
|
||||
target_link_libraries(swift)
|
||||
|
||||
@@ -17,7 +17,9 @@ TOOLNAME = lldb-moduleimport-test
|
||||
include $(SWIFT_LEVEL)/../../Makefile.config
|
||||
|
||||
LINK_COMPONENTS := $(TARGETS_TO_BUILD) bitreader mcjit option
|
||||
USEDLIBS = swiftFrontend.a swiftParse.a swiftClangImporter.a swiftSIL.a swiftSema.a swiftSerialization.a swiftAST.a swiftBasic.a
|
||||
USEDLIBS = swiftFrontend.a swiftParse.a \
|
||||
swiftClangImporter.a swiftASTSectionImporter.a \
|
||||
swiftSIL.a swiftSema.a swiftSerialization.a swiftAST.a swiftBasic.a
|
||||
|
||||
LLVMLibsOptions := \
|
||||
-lclangDriver \
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "swift/Frontend/Frontend.h"
|
||||
#include "swift/Serialization/SerializedModuleLoader.h"
|
||||
#include "swift/ASTSectionImporter/ASTSectionImporter.h"
|
||||
#include "llvm/ADT/OwningPtr.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/PrettyStackTrace.h"
|
||||
@@ -100,8 +100,9 @@ int main(int argc, char **argv) {
|
||||
// Pass the __apple_AST section to the module loader.
|
||||
auto data = llvm::MemoryBuffer::getNewMemBuffer(section.size, name);
|
||||
macho.read(const_cast<char *>(data->getBufferStart()), section.size);
|
||||
if (!CI.getSerializedModuleLoader()->addASTSection
|
||||
(std::unique_ptr<llvm::MemoryBuffer>(data), modules))
|
||||
if (!parseASTSection(CI.getSerializedModuleLoader(),
|
||||
std::unique_ptr<llvm::MemoryBuffer>(data),
|
||||
modules))
|
||||
exit(1);
|
||||
|
||||
for (auto path : modules)
|
||||
|
||||
Reference in New Issue
Block a user