Initial support for loading Clang modules into Swift.

From a user's perspective, one imports Clang modules using the normal
Swift syntax for module imports, e.g.,

  import Cocoa

However, to enable importing Clang modules, one needs to point Swift
at a particular SDK with the -sdk= argument, e.g.,

  swift -sdk=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9M.sdk

and, of course, that SDK needs to provide support for modules.

There are a number of moving parts here. The major pieces are:

CMake support for linking Clang into Swift: CMake users will now need
to set the SWIFT_PATH_TO_CLANG_SOURCE and SWIFT_PATH_TO_CLANG_BUILD
to the locations of the Clang source tree (which defaults to
tools/clang under your LLVM source tree) and the Clang build tree.

Makefile support for linking Clang into Swift: Makefile users will
need to have Clang located in tools/clang and Swift located in
tools/swift, and builds should just work.

Module loader abstraction: similar to Clang's module loader,
a module loader is responsible for resolving a module name to an
actual module, loading that module in the process. It will also be
responsible for performing name lookup into that module.

Clang importer: the only implementation of the module loader
abstraction, the importer creates a Clang compiler instance capable of
building and loading Clang modules. The approach we take here is to
parse a dummy .m file in Objective-C ARC mode with modules enabled,
but never tear down that compilation unit. Then, when we get a request
to import a Clang module, we turn that into a module-load request to
Clang's module loader, which will build an appropriate module
on-the-fly or used a cached module file.

Note that name lookup into Clang modules is not yet
implemented. That's the next major step.



Swift SVN r3199
This commit is contained in:
Doug Gregor
2012-11-16 18:17:05 +00:00
parent b1c3995fdc
commit bb26f52585
25 changed files with 664 additions and 31 deletions

View File

@@ -0,0 +1,91 @@
//===--- ModuleLoader.h - Module Loader Interface -------------------------===//
//
// 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 an abstract interface for loading modules.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_AST_MODULE_LOADER_H
#define SWIFT_AST_MODULE_LOADER_H
#include "swift/AST/Identifier.h"
#include "swift/AST/Module.h"
#include "swift/AST/Type.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/SourceLoc.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
namespace swift {
class Module;
/// \brief Abstract interface that loads named modules into the AST.
class ModuleLoader : public llvm::RefCountedBaseVPTR {
public:
virtual ~ModuleLoader();
/// \brief Import a module with the given module path.
///
/// \param importLoc The location of the 'import' keyword.
///
/// \param path A sequence of (identifier, location) pairs that denote
/// the dotted module name to load, e.g., AppKit.NSWindow.
///
/// \returns the module referenced, if it could be loaded. Otherwise,
/// emits a diagnostic and returns NULL.
virtual
Module *loadModule(SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path) = 0;
/// \brief Look for declarations associated with the given name.
///
/// \param module The module to search.
///
/// \param accessPath The access path used to refer to the name within this
/// (top-level) module.
///
/// \param name The name we're searching for.
///
/// \param lookupKind Whether we're performing qualified vs. unqualified
/// lookup.
///
/// \param result Will be populated with the results of name lookup.
virtual void lookupValue(ClangModule *module,
Module::AccessPathTy accessPath, Identifier name,
NLKind lookupKind,
SmallVectorImpl<ValueDecl*> &result) { }
/// \brief Look for extensions associated with the given type.
///
/// \param module The module to search.
///
/// \param type The type for which we are looking for extensions.
virtual ArrayRef<ExtensionDecl*> lookupExtensions(ClangModule *module,
Type type) {
return {};
}
/// \brief Look for members of the given type.
///
/// \param module The module to search.
///
/// \param base The type into which we will look to find members.
///
/// \param name The name of the members we are looking for.
///
/// \param result Will be populated with the results of name lookup.
virtual void lookupMembers(ClangModule *module, Type base, Identifier name,
SmallVectorImpl<ValueDecl*> &result) { }
};
}
#endif