2017-11-04 06:35:27 +08:00
|
|
|
//===- Module.cpp - Describe a module -------------------------------------===//
|
2011-12-01 07:21:26 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the Module class, which describes a module in the source
|
|
|
|
// code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-10-29 06:18:19 +08:00
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
#include "clang/Basic/Module.h"
|
2017-06-20 07:09:36 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2011-12-01 07:21:26 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2011-12-31 12:05:44 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2017-11-04 06:35:27 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2012-01-30 14:38:25 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2013-03-14 05:13:43 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2011-12-31 12:05:44 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2017-11-04 06:35:27 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2011-12-31 12:05:44 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2017-11-04 06:35:27 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-11-04 06:35:27 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2013-10-29 06:18:19 +08:00
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2014-03-02 13:58:18 +08:00
|
|
|
Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
|
2015-05-01 09:53:09 +08:00
|
|
|
bool IsFramework, bool IsExplicit, unsigned VisibilityID)
|
2017-11-04 06:35:27 +08:00
|
|
|
: Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
|
|
|
|
VisibilityID(VisibilityID), IsMissingRequirement(false),
|
|
|
|
HasIncompatibleModuleFile(false), IsAvailable(true),
|
|
|
|
IsFromModuleFile(false), IsFramework(IsFramework), IsExplicit(IsExplicit),
|
|
|
|
IsSystem(false), IsExternC(false), IsInferred(false),
|
|
|
|
InferSubmodules(false), InferExplicitSubmodules(false),
|
2015-11-05 08:54:55 +08:00
|
|
|
InferExportWildcard(false), ConfigMacrosExhaustive(false),
|
2017-04-12 04:46:34 +08:00
|
|
|
NoUndeclaredIncludes(false), NameVisibility(Hidden) {
|
2012-01-05 07:32:19 +08:00
|
|
|
if (Parent) {
|
|
|
|
if (!Parent->isAvailable())
|
|
|
|
IsAvailable = false;
|
2012-02-03 02:42:48 +08:00
|
|
|
if (Parent->IsSystem)
|
|
|
|
IsSystem = true;
|
2014-03-08 08:03:56 +08:00
|
|
|
if (Parent->IsExternC)
|
|
|
|
IsExternC = true;
|
2016-10-21 09:41:56 +08:00
|
|
|
if (Parent->NoUndeclaredIncludes)
|
|
|
|
NoUndeclaredIncludes = true;
|
2014-04-19 07:51:00 +08:00
|
|
|
IsMissingRequirement = Parent->IsMissingRequirement;
|
2012-01-05 07:32:19 +08:00
|
|
|
|
|
|
|
Parent->SubModuleIndex[Name] = Parent->SubModules.size();
|
|
|
|
Parent->SubModules.push_back(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
Module::~Module() {
|
2012-01-05 07:32:19 +08:00
|
|
|
for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
|
2011-12-01 07:21:26 +08:00
|
|
|
I != IEnd; ++I) {
|
2012-01-05 07:32:19 +08:00
|
|
|
delete *I;
|
2011-12-01 07:21:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-31 12:05:44 +08:00
|
|
|
/// \brief Determine whether a translation unit built using the current
|
|
|
|
/// language options has the given feature.
|
2012-01-30 14:01:29 +08:00
|
|
|
static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
|
|
|
|
const TargetInfo &Target) {
|
2015-02-03 05:56:15 +08:00
|
|
|
bool HasFeature = llvm::StringSwitch<bool>(Feature)
|
|
|
|
.Case("altivec", LangOpts.AltiVec)
|
|
|
|
.Case("blocks", LangOpts.Blocks)
|
2017-05-29 05:07:22 +08:00
|
|
|
.Case("coroutines", LangOpts.CoroutinesTS)
|
2015-02-03 05:56:15 +08:00
|
|
|
.Case("cplusplus", LangOpts.CPlusPlus)
|
|
|
|
.Case("cplusplus11", LangOpts.CPlusPlus11)
|
2018-02-15 03:01:03 +08:00
|
|
|
.Case("cplusplus14", LangOpts.CPlusPlus14)
|
|
|
|
.Case("cplusplus17", LangOpts.CPlusPlus17)
|
|
|
|
.Case("c99", LangOpts.C99)
|
|
|
|
.Case("c11", LangOpts.C11)
|
|
|
|
.Case("c17", LangOpts.C17)
|
2016-09-04 14:00:42 +08:00
|
|
|
.Case("freestanding", LangOpts.Freestanding)
|
2016-08-31 05:25:42 +08:00
|
|
|
.Case("gnuinlineasm", LangOpts.GNUAsm)
|
2015-02-03 05:56:15 +08:00
|
|
|
.Case("objc", LangOpts.ObjC1)
|
|
|
|
.Case("objc_arc", LangOpts.ObjCAutoRefCount)
|
|
|
|
.Case("opencl", LangOpts.OpenCL)
|
|
|
|
.Case("tls", Target.isTLSSupported())
|
2015-07-30 22:08:36 +08:00
|
|
|
.Case("zvector", LangOpts.ZVector)
|
2015-02-03 05:56:15 +08:00
|
|
|
.Default(Target.hasFeature(Feature));
|
|
|
|
if (!HasFeature)
|
|
|
|
HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
|
|
|
|
LangOpts.ModuleFeatures.end(),
|
|
|
|
Feature) != LangOpts.ModuleFeatures.end();
|
|
|
|
return HasFeature;
|
2011-12-31 12:05:44 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 08:08:08 +08:00
|
|
|
bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
|
|
|
|
Requirement &Req,
|
2018-01-05 10:33:18 +08:00
|
|
|
UnresolvedHeaderDirective &MissingHeader,
|
|
|
|
Module *&ShadowingModule) const {
|
2011-12-31 12:05:44 +08:00
|
|
|
if (IsAvailable)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (const Module *Current = this; Current; Current = Current->Parent) {
|
2018-01-05 10:33:18 +08:00
|
|
|
if (Current->ShadowingModule) {
|
|
|
|
ShadowingModule = Current->ShadowingModule;
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-29 06:18:19 +08:00
|
|
|
for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
|
|
|
|
if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
|
|
|
|
Current->Requirements[I].second) {
|
|
|
|
Req = Current->Requirements[I];
|
2011-12-31 12:05:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 03:48:52 +08:00
|
|
|
if (!Current->MissingHeaders.empty()) {
|
|
|
|
MissingHeader = Current->MissingHeaders.front();
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-31 12:05:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("could not find a reason why module is unavailable");
|
|
|
|
}
|
|
|
|
|
2014-04-18 22:36:51 +08:00
|
|
|
bool Module::isSubModuleOf(const Module *Other) const {
|
2011-12-06 01:28:06 +08:00
|
|
|
const Module *This = this;
|
|
|
|
do {
|
|
|
|
if (This == Other)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
This = This->Parent;
|
|
|
|
} while (This);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-06 06:27:44 +08:00
|
|
|
const Module *Module::getTopLevelModule() const {
|
|
|
|
const Module *Result = this;
|
|
|
|
while (Result->Parent)
|
|
|
|
Result = Result->Parent;
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:09:36 +08:00
|
|
|
static StringRef getModuleNameFromComponent(
|
|
|
|
const std::pair<std::string, SourceLocation> &IdComponent) {
|
|
|
|
return IdComponent.first;
|
|
|
|
}
|
2017-11-04 06:35:27 +08:00
|
|
|
|
2017-06-20 07:09:36 +08:00
|
|
|
static StringRef getModuleNameFromComponent(StringRef R) { return R; }
|
|
|
|
|
|
|
|
template<typename InputIter>
|
|
|
|
static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End,
|
|
|
|
bool AllowStringLiterals = true) {
|
|
|
|
for (InputIter It = Begin; It != End; ++It) {
|
|
|
|
if (It != Begin)
|
|
|
|
OS << ".";
|
|
|
|
|
|
|
|
StringRef Name = getModuleNameFromComponent(*It);
|
|
|
|
if (!AllowStringLiterals || isValidIdentifier(Name))
|
|
|
|
OS << Name;
|
|
|
|
else {
|
|
|
|
OS << '"';
|
|
|
|
OS.write_escaped(Name);
|
|
|
|
OS << '"';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
static void printModuleId(raw_ostream &OS, const Container &C) {
|
|
|
|
return printModuleId(OS, C.begin(), C.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Module::getFullModuleName(bool AllowStringLiterals) const {
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<StringRef, 2> Names;
|
2011-12-01 07:21:26 +08:00
|
|
|
|
|
|
|
// Build up the set of module names (from innermost to outermost).
|
|
|
|
for (const Module *M = this; M; M = M->Parent)
|
|
|
|
Names.push_back(M->Name);
|
|
|
|
|
|
|
|
std::string Result;
|
2017-06-20 07:09:36 +08:00
|
|
|
|
|
|
|
llvm::raw_string_ostream Out(Result);
|
|
|
|
printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
|
|
|
|
Out.flush();
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:13:33 +08:00
|
|
|
bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {
|
|
|
|
for (const Module *M = this; M; M = M->Parent) {
|
|
|
|
if (nameParts.empty() || M->Name != nameParts.back())
|
|
|
|
return false;
|
|
|
|
nameParts = nameParts.drop_back();
|
|
|
|
}
|
|
|
|
return nameParts.empty();
|
|
|
|
}
|
|
|
|
|
2015-05-16 10:28:53 +08:00
|
|
|
Module::DirectoryName Module::getUmbrellaDir() const {
|
|
|
|
if (Header U = getUmbrellaHeader())
|
|
|
|
return {"", U.Entry->getDir()};
|
2011-12-09 01:39:04 +08:00
|
|
|
|
2015-05-16 10:28:53 +08:00
|
|
|
return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()};
|
2011-12-09 01:39:04 +08:00
|
|
|
}
|
|
|
|
|
2013-03-14 05:13:43 +08:00
|
|
|
ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
|
|
|
|
if (!TopHeaderNames.empty()) {
|
|
|
|
for (std::vector<std::string>::iterator
|
|
|
|
I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
|
|
|
|
if (const FileEntry *FE = FileMgr.getFile(*I))
|
|
|
|
TopHeaders.insert(FE);
|
|
|
|
}
|
|
|
|
TopHeaderNames.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
|
|
|
|
}
|
|
|
|
|
2015-03-27 06:10:01 +08:00
|
|
|
bool Module::directlyUses(const Module *Requested) const {
|
|
|
|
auto *Top = getTopLevelModule();
|
|
|
|
|
|
|
|
// A top-level module implicitly uses itself.
|
|
|
|
if (Requested->isSubModuleOf(Top))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (auto *Use : Top->DirectUses)
|
|
|
|
if (Requested->isSubModuleOf(Use))
|
|
|
|
return true;
|
2016-10-21 09:41:56 +08:00
|
|
|
|
|
|
|
// Anyone is allowed to use our builtin stddef.h and its accompanying module.
|
|
|
|
if (!Requested->Parent && Requested->Name == "_Builtin_stddef_max_align_t")
|
|
|
|
return true;
|
|
|
|
|
2015-03-27 06:10:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-29 06:18:19 +08:00
|
|
|
void Module::addRequirement(StringRef Feature, bool RequiredState,
|
|
|
|
const LangOptions &LangOpts,
|
2012-01-30 14:01:29 +08:00
|
|
|
const TargetInfo &Target) {
|
2013-10-29 06:18:19 +08:00
|
|
|
Requirements.push_back(Requirement(Feature, RequiredState));
|
2011-12-31 12:05:44 +08:00
|
|
|
|
|
|
|
// If this feature is currently available, we're done.
|
2013-10-29 06:18:19 +08:00
|
|
|
if (hasFeature(Feature, LangOpts, Target) == RequiredState)
|
2011-12-31 12:05:44 +08:00
|
|
|
return;
|
|
|
|
|
2014-04-19 07:51:00 +08:00
|
|
|
markUnavailable(/*MissingRequirement*/true);
|
2014-04-19 06:07:31 +08:00
|
|
|
}
|
|
|
|
|
2014-04-19 07:51:00 +08:00
|
|
|
void Module::markUnavailable(bool MissingRequirement) {
|
2015-07-14 03:48:52 +08:00
|
|
|
auto needUpdate = [MissingRequirement](Module *M) {
|
|
|
|
return M->IsAvailable || (!M->IsMissingRequirement && MissingRequirement);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!needUpdate(this))
|
2011-12-31 12:05:44 +08:00
|
|
|
return;
|
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<Module *, 2> Stack;
|
2011-12-31 12:05:44 +08:00
|
|
|
Stack.push_back(this);
|
|
|
|
while (!Stack.empty()) {
|
|
|
|
Module *Current = Stack.back();
|
|
|
|
Stack.pop_back();
|
|
|
|
|
2015-07-14 03:48:52 +08:00
|
|
|
if (!needUpdate(Current))
|
2011-12-31 12:05:44 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Current->IsAvailable = false;
|
2014-04-19 07:51:00 +08:00
|
|
|
Current->IsMissingRequirement |= MissingRequirement;
|
2012-01-05 07:32:19 +08:00
|
|
|
for (submodule_iterator Sub = Current->submodule_begin(),
|
|
|
|
SubEnd = Current->submodule_end();
|
2011-12-31 12:05:44 +08:00
|
|
|
Sub != SubEnd; ++Sub) {
|
2015-07-14 03:48:52 +08:00
|
|
|
if (needUpdate(*Sub))
|
2012-01-05 07:32:19 +08:00
|
|
|
Stack.push_back(*Sub);
|
2011-12-31 12:05:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 07:32:19 +08:00
|
|
|
Module *Module::findSubmodule(StringRef Name) const {
|
|
|
|
llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
|
|
|
|
if (Pos == SubModuleIndex.end())
|
2014-05-08 14:41:40 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-01-05 07:32:19 +08:00
|
|
|
return SubModules[Pos->getValue()];
|
|
|
|
}
|
|
|
|
|
2013-02-20 03:34:40 +08:00
|
|
|
void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
|
2013-11-05 05:51:33 +08:00
|
|
|
// All non-explicit submodules are exported.
|
|
|
|
for (std::vector<Module *>::const_iterator I = SubModules.begin(),
|
|
|
|
E = SubModules.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Module *Mod = *I;
|
|
|
|
if (!Mod->IsExplicit)
|
|
|
|
Exported.push_back(Mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find re-exported modules by filtering the list of imported modules.
|
2013-02-20 03:34:40 +08:00
|
|
|
bool AnyWildcard = false;
|
|
|
|
bool UnrestrictedWildcard = false;
|
|
|
|
SmallVector<Module *, 4> WildcardRestrictions;
|
|
|
|
for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
|
|
|
|
Module *Mod = Exports[I].getPointer();
|
|
|
|
if (!Exports[I].getInt()) {
|
|
|
|
// Export a named module directly; no wildcards involved.
|
|
|
|
Exported.push_back(Mod);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wildcard export: export all of the imported modules that match
|
|
|
|
// the given pattern.
|
|
|
|
AnyWildcard = true;
|
|
|
|
if (UnrestrictedWildcard)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Module *Restriction = Exports[I].getPointer())
|
|
|
|
WildcardRestrictions.push_back(Restriction);
|
|
|
|
else {
|
|
|
|
WildcardRestrictions.clear();
|
|
|
|
UnrestrictedWildcard = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there were any wildcards, push any imported modules that were
|
|
|
|
// re-exported by the wildcard restriction.
|
|
|
|
if (!AnyWildcard)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
|
|
|
|
Module *Mod = Imports[I];
|
|
|
|
bool Acceptable = UnrestrictedWildcard;
|
|
|
|
if (!Acceptable) {
|
|
|
|
// Check whether this module meets one of the restrictions.
|
|
|
|
for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
|
|
|
|
Module *Restriction = WildcardRestrictions[R];
|
|
|
|
if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
|
|
|
|
Acceptable = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Acceptable)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Exported.push_back(Mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
void Module::buildVisibleModulesCache() const {
|
|
|
|
assert(VisibleModulesCache.empty() && "cache does not need building");
|
|
|
|
|
|
|
|
// This module is visible to itself.
|
|
|
|
VisibleModulesCache.insert(this);
|
|
|
|
|
2013-11-01 06:24:10 +08:00
|
|
|
// Every imported module is visible.
|
2013-11-01 10:19:14 +08:00
|
|
|
SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
|
2013-11-01 06:24:10 +08:00
|
|
|
while (!Stack.empty()) {
|
|
|
|
Module *CurrModule = Stack.pop_back_val();
|
2013-11-01 10:19:14 +08:00
|
|
|
|
|
|
|
// Every module transitively exported by an imported module is visible.
|
|
|
|
if (VisibleModulesCache.insert(CurrModule).second)
|
|
|
|
CurrModule->getExportedModules(Stack);
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
void Module::print(raw_ostream &OS, unsigned Indent) const {
|
2011-12-01 07:21:26 +08:00
|
|
|
OS.indent(Indent);
|
|
|
|
if (IsFramework)
|
|
|
|
OS << "framework ";
|
|
|
|
if (IsExplicit)
|
|
|
|
OS << "explicit ";
|
2017-06-20 07:09:36 +08:00
|
|
|
OS << "module ";
|
|
|
|
printModuleId(OS, &Name, &Name + 1);
|
2011-12-31 12:05:44 +08:00
|
|
|
|
2015-01-14 01:47:38 +08:00
|
|
|
if (IsSystem || IsExternC) {
|
2012-01-28 03:52:33 +08:00
|
|
|
OS.indent(Indent + 2);
|
2015-01-14 01:47:38 +08:00
|
|
|
if (IsSystem)
|
|
|
|
OS << " [system]";
|
|
|
|
if (IsExternC)
|
|
|
|
OS << " [extern_c]";
|
2012-01-28 03:52:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OS << " {\n";
|
|
|
|
|
2013-10-29 06:18:19 +08:00
|
|
|
if (!Requirements.empty()) {
|
2011-12-31 12:05:44 +08:00
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "requires ";
|
2013-10-29 06:18:19 +08:00
|
|
|
for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
|
2011-12-31 12:05:44 +08:00
|
|
|
if (I)
|
|
|
|
OS << ", ";
|
2013-10-29 06:18:19 +08:00
|
|
|
if (!Requirements[I].second)
|
|
|
|
OS << "!";
|
|
|
|
OS << Requirements[I].first;
|
2011-12-31 12:05:44 +08:00
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
2011-12-01 07:21:26 +08:00
|
|
|
|
2015-05-16 10:28:53 +08:00
|
|
|
if (Header H = getUmbrellaHeader()) {
|
2011-12-01 07:21:26 +08:00
|
|
|
OS.indent(Indent + 2);
|
2011-12-09 02:00:48 +08:00
|
|
|
OS << "umbrella header \"";
|
2015-05-16 10:28:53 +08:00
|
|
|
OS.write_escaped(H.NameAsWritten);
|
2011-12-01 07:21:26 +08:00
|
|
|
OS << "\"\n";
|
2015-05-16 10:28:53 +08:00
|
|
|
} else if (DirectoryName D = getUmbrellaDir()) {
|
2011-12-09 02:00:48 +08:00
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "umbrella \"";
|
2015-05-16 10:28:53 +08:00
|
|
|
OS.write_escaped(D.NameAsWritten);
|
2011-12-09 02:00:48 +08:00
|
|
|
OS << "\"\n";
|
2011-12-01 07:21:26 +08:00
|
|
|
}
|
2013-03-20 08:22:05 +08:00
|
|
|
|
|
|
|
if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "config_macros ";
|
|
|
|
if (ConfigMacrosExhaustive)
|
2013-03-20 11:59:18 +08:00
|
|
|
OS << "[exhaustive]";
|
2013-03-20 08:22:05 +08:00
|
|
|
for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
|
|
|
|
if (I)
|
|
|
|
OS << ", ";
|
|
|
|
OS << ConfigMacros[I];
|
|
|
|
}
|
2013-03-20 11:59:18 +08:00
|
|
|
OS << "\n";
|
2013-03-20 08:22:05 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 08:08:08 +08:00
|
|
|
struct {
|
2014-10-23 07:50:56 +08:00
|
|
|
StringRef Prefix;
|
2014-12-02 08:08:08 +08:00
|
|
|
HeaderKind Kind;
|
|
|
|
} Kinds[] = {{"", HK_Normal},
|
|
|
|
{"textual ", HK_Textual},
|
|
|
|
{"private ", HK_Private},
|
|
|
|
{"private textual ", HK_PrivateTextual},
|
|
|
|
{"exclude ", HK_Excluded}};
|
2014-10-23 07:50:56 +08:00
|
|
|
|
|
|
|
for (auto &K : Kinds) {
|
2017-06-02 09:55:39 +08:00
|
|
|
assert(&K == &Kinds[K.Kind] && "kinds in wrong order");
|
2014-12-02 08:08:08 +08:00
|
|
|
for (auto &H : Headers[K.Kind]) {
|
2014-10-23 07:50:56 +08:00
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << K.Prefix << "header \"";
|
2014-12-02 08:08:08 +08:00
|
|
|
OS.write_escaped(H.NameAsWritten);
|
2017-06-02 09:55:39 +08:00
|
|
|
OS << "\" { size " << H.Entry->getSize()
|
|
|
|
<< " mtime " << H.Entry->getModificationTime() << " }\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) {
|
|
|
|
for (auto &U : *Unresolved) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << Kinds[U.Kind].Prefix << "header \"";
|
|
|
|
OS.write_escaped(U.FileName);
|
|
|
|
OS << "\"";
|
|
|
|
if (U.Size || U.ModTime) {
|
|
|
|
OS << " {";
|
|
|
|
if (U.Size)
|
|
|
|
OS << " size " << *U.Size;
|
|
|
|
if (U.ModTime)
|
|
|
|
OS << " mtime " << *U.ModTime;
|
|
|
|
OS << " }";
|
|
|
|
}
|
|
|
|
OS << "\n";
|
2014-10-23 07:50:56 +08:00
|
|
|
}
|
2012-10-15 14:28:11 +08:00
|
|
|
}
|
2013-06-21 05:14:14 +08:00
|
|
|
|
2017-09-15 07:38:44 +08:00
|
|
|
if (!ExportAsModule.empty()) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "export_as" << ExportAsModule << "\n";
|
|
|
|
}
|
|
|
|
|
2012-01-05 07:32:19 +08:00
|
|
|
for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
|
2011-12-01 07:21:26 +08:00
|
|
|
MI != MIEnd; ++MI)
|
2014-08-09 08:57:23 +08:00
|
|
|
// Print inferred subframework modules so that we don't need to re-infer
|
|
|
|
// them (requires expensive directory iteration + stat calls) when we build
|
|
|
|
// the module. Regular inferred submodules are OK, as we need to look at all
|
|
|
|
// those header files anyway.
|
|
|
|
if (!(*MI)->IsInferred || (*MI)->IsFramework)
|
2014-04-24 05:10:46 +08:00
|
|
|
(*MI)->print(OS, Indent + 2);
|
2011-12-01 07:21:26 +08:00
|
|
|
|
2011-12-03 02:58:38 +08:00
|
|
|
for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
|
|
|
|
OS.indent(Indent + 2);
|
2011-12-06 01:34:59 +08:00
|
|
|
OS << "export ";
|
|
|
|
if (Module *Restriction = Exports[I].getPointer()) {
|
2017-06-20 07:09:36 +08:00
|
|
|
OS << Restriction->getFullModuleName(true);
|
2011-12-06 01:34:59 +08:00
|
|
|
if (Exports[I].getInt())
|
|
|
|
OS << ".*";
|
|
|
|
} else {
|
|
|
|
OS << "*";
|
|
|
|
}
|
2011-12-03 02:58:38 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "export ";
|
|
|
|
printModuleId(OS, UnresolvedExports[I].Id);
|
2016-03-10 05:09:51 +08:00
|
|
|
if (UnresolvedExports[I].Wildcard)
|
|
|
|
OS << (UnresolvedExports[I].Id.empty() ? "*" : ".*");
|
2011-12-03 02:58:38 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2013-09-24 17:14:14 +08:00
|
|
|
for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "use ";
|
2017-06-20 07:09:36 +08:00
|
|
|
OS << DirectUses[I]->getFullModuleName(true);
|
2013-09-24 17:14:14 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "use ";
|
|
|
|
printModuleId(OS, UnresolvedDirectUses[I]);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2013-01-15 01:21:00 +08:00
|
|
|
for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "link ";
|
|
|
|
if (LinkLibraries[I].IsFramework)
|
|
|
|
OS << "framework ";
|
|
|
|
OS << "\"";
|
|
|
|
OS.write_escaped(LinkLibraries[I].Library);
|
|
|
|
OS << "\"";
|
|
|
|
}
|
|
|
|
|
2013-03-21 05:10:35 +08:00
|
|
|
for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "conflict ";
|
|
|
|
printModuleId(OS, UnresolvedConflicts[I].Id);
|
|
|
|
OS << ", \"";
|
|
|
|
OS.write_escaped(UnresolvedConflicts[I].Message);
|
|
|
|
OS << "\"\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "conflict ";
|
2017-06-20 07:09:36 +08:00
|
|
|
OS << Conflicts[I].Other->getFullModuleName(true);
|
2013-03-21 05:10:35 +08:00
|
|
|
OS << ", \"";
|
|
|
|
OS.write_escaped(Conflicts[I].Message);
|
|
|
|
OS << "\"\n";
|
|
|
|
}
|
|
|
|
|
2011-12-06 06:27:44 +08:00
|
|
|
if (InferSubmodules) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
if (InferExplicitSubmodules)
|
|
|
|
OS << "explicit ";
|
|
|
|
OS << "module * {\n";
|
|
|
|
if (InferExportWildcard) {
|
|
|
|
OS.indent(Indent + 4);
|
|
|
|
OS << "export *\n";
|
|
|
|
}
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << "}\n";
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
OS.indent(Indent);
|
|
|
|
OS << "}\n";
|
|
|
|
}
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void Module::dump() const {
|
2011-12-01 07:21:26 +08:00
|
|
|
print(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2015-05-01 09:53:09 +08:00
|
|
|
void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
|
|
|
|
VisibleCallback Vis, ConflictCallback Cb) {
|
2016-02-12 01:04:42 +08:00
|
|
|
assert(Loc.isValid() && "setVisible expects a valid import location");
|
2015-05-01 09:53:09 +08:00
|
|
|
if (isVisible(M))
|
|
|
|
return;
|
2011-12-01 07:21:26 +08:00
|
|
|
|
2015-05-01 09:53:09 +08:00
|
|
|
++Generation;
|
|
|
|
|
|
|
|
struct Visiting {
|
|
|
|
Module *M;
|
|
|
|
Visiting *ExportedBy;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::function<void(Visiting)> VisitModule = [&](Visiting V) {
|
|
|
|
// Modules that aren't available cannot be made visible.
|
|
|
|
if (!V.M->isAvailable())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Nothing to do for a module that's already visible.
|
|
|
|
unsigned ID = V.M->getVisibilityID();
|
|
|
|
if (ImportLocs.size() <= ID)
|
|
|
|
ImportLocs.resize(ID + 1);
|
|
|
|
else if (ImportLocs[ID].isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ImportLocs[ID] = Loc;
|
|
|
|
Vis(M);
|
|
|
|
|
|
|
|
// Make any exported modules visible.
|
|
|
|
SmallVector<Module *, 16> Exports;
|
|
|
|
V.M->getExportedModules(Exports);
|
|
|
|
for (Module *E : Exports)
|
|
|
|
VisitModule({E, &V});
|
|
|
|
|
|
|
|
for (auto &C : V.M->Conflicts) {
|
|
|
|
if (isVisible(C.Other)) {
|
|
|
|
llvm::SmallVector<Module*, 8> Path;
|
|
|
|
for (Visiting *I = &V; I; I = I->ExportedBy)
|
|
|
|
Path.push_back(I->M);
|
|
|
|
Cb(Path, C.Other, C.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
VisitModule({M, nullptr});
|
|
|
|
}
|