diff options
author | Douglas Gregor <dgregor@apple.com> | 2011-08-26 23:56:07 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2011-08-26 23:56:07 +0000 |
commit | 6aa52ec6b969faabf3764baf79d89810b8249a7e (patch) | |
tree | d0921823c1cb331fe29e8cb31b78e9719e2126fb /lib/Lex/Preprocessor.cpp | |
parent | d37c67b8f26ffe31e7f7292f26ab39cee78151d8 (diff) |
Introduce support for a simple module import declaration, which
loads the named module. The syntax itself is intentionally hideous and
will be replaced at some later point with something more
palatable. For now, we're focusing on the semantics:
- Module imports are handled first by the preprocessor (to get macro
definitions) and then the same tokens are also handled by the parser
(to get declarations). If both happen (as in normal compilation),
the second one is redundant, because we currently have no way to
hide macros or declarations when loading a module. Chris gets credit
for this mad-but-workable scheme.
- The Preprocessor now holds on to a reference to a module loader,
which is responsible for loading named modules. CompilerInstance is
the only important module loader: it now knows how to create and
wire up an AST reader on demand to actually perform the module load.
- We search for modules in the include path, using the module name
with the suffix ".pcm" (precompiled module) for the file name. This
is a temporary hack; we hope to improve the situation in the
future.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@138679 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/Preprocessor.cpp')
-rw-r--r-- | lib/Lex/Preprocessor.cpp | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index 24d36cdb25..1d1687d84b 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -35,6 +35,7 @@ #include "clang/Lex/ScratchBuffer.h" #include "clang/Lex/LexDiagnostic.h" #include "clang/Lex/CodeCompletionHandler.h" +#include "clang/Lex/ModuleLoader.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/TargetInfo.h" @@ -50,12 +51,12 @@ ExternalPreprocessorSource::~ExternalPreprocessorSource() { } Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, const TargetInfo &target, SourceManager &SM, - HeaderSearch &Headers, + HeaderSearch &Headers, ModuleLoader &TheModuleLoader, IdentifierInfoLookup* IILookup, bool OwnsHeaders) : Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()), - SourceMgr(SM), - HeaderInfo(Headers), ExternalSource(0), + SourceMgr(SM), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader), + ExternalSource(0), Identifiers(opts, IILookup), BuiltinInfo(Target), CodeComplete(0), CodeCompletionFile(0), SkipMainFilePreamble(0, true), CurPPLexer(0), CurDirLookup(0), Callbacks(0), MacroArgCache(0), Record(0), MIChainHead(0), @@ -483,7 +484,7 @@ void Preprocessor::HandleIdentifier(Token &Identifier) { if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { if (MI->isEnabled()) { if (!HandleMacroExpandedIdentifier(Identifier, MI)) - return; + goto finish; } else { // C99 6.10.3.4p2 says that a disabled macro may never again be // expanded, even if it's in a context where it could be expanded in the @@ -505,6 +506,33 @@ void Preprocessor::HandleIdentifier(Token &Identifier) { // like "#define TY typeof", "TY(1) x". if (II.isExtensionToken() && !DisableMacroExpansion) Diag(Identifier, diag::ext_token_used); + +finish: + // If we have the start of a module import, handle it now. + if (Identifier.is(tok::kw___import__) && + !InMacroArgs && !DisableMacroExpansion) + HandleModuleImport(Identifier); +} + +void Preprocessor::HandleModuleImport(Token &Import) { + // The token sequence + // + // __import__ identifier + // + // indicates a module import directive. We load the module and then + // leave the token sequence for the parser. + Token ModuleNameTok = LookAhead(0); + if (ModuleNameTok.getKind() != tok::identifier) + return; + + (void)TheModuleLoader.loadModule(Import.getLocation(), + *ModuleNameTok.getIdentifierInfo(), + ModuleNameTok.getLocation()); + + // FIXME: Transmogrify __import__ into some kind of AST-only __import__ that + // is not recognized by the preprocessor but is recognized by the parser. + // It would also be useful to stash the ModuleKey somewhere, so we don't try + // to load the module twice. } void Preprocessor::AddCommentHandler(CommentHandler *Handler) { @@ -535,6 +563,8 @@ bool Preprocessor::HandleComment(Token &result, SourceRange Comment) { return true; } +ModuleLoader::~ModuleLoader() { } + CommentHandler::~CommentHandler() { } CodeCompletionHandler::~CodeCompletionHandler() { } |