2017-11-04 06:35:27 +08:00
|
|
|
//===- Module.cpp - Describe a module -------------------------------------===//
|
2011-12-01 07:21:26 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-12-01 07:21:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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),
|
2020-04-18 07:23:41 +08:00
|
|
|
VisibilityID(VisibilityID), IsUnimportable(false),
|
2017-11-04 06:35:27 +08:00
|
|
|
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),
|
2018-04-21 01:16:04 +08:00
|
|
|
NoUndeclaredIncludes(false), ModuleMapIsPrivate(false),
|
2020-10-30 03:14:30 +08:00
|
|
|
NameVisibility(Hidden) {
|
2012-01-05 07:32:19 +08:00
|
|
|
if (Parent) {
|
2020-04-18 07:23:41 +08:00
|
|
|
IsAvailable = Parent->isAvailable();
|
|
|
|
IsUnimportable = Parent->isUnimportable();
|
|
|
|
IsSystem = Parent->IsSystem;
|
|
|
|
IsExternC = Parent->IsExternC;
|
|
|
|
NoUndeclaredIncludes = Parent->NoUndeclaredIncludes;
|
|
|
|
ModuleMapIsPrivate = Parent->ModuleMapIsPrivate;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 01:11:13 +08:00
|
|
|
static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature) {
|
|
|
|
StringRef Platform = Target.getPlatformName();
|
|
|
|
StringRef Env = Target.getTriple().getEnvironmentName();
|
|
|
|
|
|
|
|
// Attempt to match platform and environment.
|
|
|
|
if (Platform == Feature || Target.getTriple().getOSName() == Feature ||
|
|
|
|
Env == Feature)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto CmpPlatformEnv = [](StringRef LHS, StringRef RHS) {
|
2020-12-17 15:28:32 +08:00
|
|
|
auto Pos = LHS.find('-');
|
2018-09-19 01:11:13 +08:00
|
|
|
if (Pos == StringRef::npos)
|
|
|
|
return false;
|
|
|
|
SmallString<128> NewLHS = LHS.slice(0, Pos);
|
|
|
|
NewLHS += LHS.slice(Pos+1, LHS.size());
|
|
|
|
return NewLHS == RHS;
|
|
|
|
};
|
|
|
|
|
|
|
|
SmallString<128> PlatformEnv = Target.getTriple().getOSAndEnvironmentName();
|
|
|
|
// Darwin has different but equivalent variants for simulators, example:
|
|
|
|
// 1. x86_64-apple-ios-simulator
|
|
|
|
// 2. x86_64-apple-iossimulator
|
|
|
|
// where both are valid examples of the same platform+environment but in the
|
|
|
|
// variant (2) the simulator is hardcoded as part of the platform name. Both
|
|
|
|
// forms above should match for "iossimulator" requirement.
|
|
|
|
if (Target.getTriple().isOSDarwin() && PlatformEnv.endswith("simulator"))
|
|
|
|
return PlatformEnv == Feature || CmpPlatformEnv(PlatformEnv, Feature);
|
|
|
|
|
|
|
|
return PlatformEnv == Feature;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determine whether a translation unit built using the current
|
2011-12-31 12:05:44 +08:00
|
|
|
/// 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)
|
2019-02-24 05:06:26 +08:00
|
|
|
.Case("coroutines", LangOpts.Coroutines)
|
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)
|
2018-10-31 04:31:30 +08:00
|
|
|
.Case("objc", LangOpts.ObjC)
|
2015-02-03 05:56:15 +08:00
|
|
|
.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)
|
2018-09-19 01:11:13 +08:00
|
|
|
.Default(Target.hasFeature(Feature) ||
|
|
|
|
isPlatformEnvironment(Target, Feature));
|
2015-02-03 05:56:15 +08:00
|
|
|
if (!HasFeature)
|
2021-10-10 23:52:14 +08:00
|
|
|
HasFeature = llvm::is_contained(LangOpts.ModuleFeatures, Feature);
|
2015-02-03 05:56:15 +08:00
|
|
|
return HasFeature;
|
2011-12-31 12:05:44 +08:00
|
|
|
}
|
|
|
|
|
2020-04-18 07:23:41 +08:00
|
|
|
bool Module::isUnimportable(const LangOptions &LangOpts,
|
|
|
|
const TargetInfo &Target, Requirement &Req,
|
|
|
|
Module *&ShadowingModule) const {
|
|
|
|
if (!IsUnimportable)
|
|
|
|
return false;
|
2011-12-31 12:05:44 +08:00
|
|
|
|
|
|
|
for (const Module *Current = this; Current; Current = Current->Parent) {
|
2018-01-05 10:33:18 +08:00
|
|
|
if (Current->ShadowingModule) {
|
|
|
|
ShadowingModule = Current->ShadowingModule;
|
2020-04-18 07:23:41 +08:00
|
|
|
return true;
|
2018-01-05 10:33:18 +08:00
|
|
|
}
|
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];
|
2020-04-18 07:23:41 +08:00
|
|
|
return true;
|
2011-12-31 12:05:44 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 07:23:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("could not find a reason why module is unimportable");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
|
|
|
|
Requirement &Req,
|
|
|
|
UnresolvedHeaderDirective &MissingHeader,
|
|
|
|
Module *&ShadowingModule) const {
|
|
|
|
if (IsAvailable)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (isUnimportable(LangOpts, Target, Req, ShadowingModule))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: All missing headers are listed on the top-level module. Should we
|
|
|
|
// just look there?
|
|
|
|
for (const Module *Current = this; Current; Current = Current->Parent) {
|
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 {
|
2020-07-22 07:03:56 +08:00
|
|
|
for (auto *Parent = this; Parent; Parent = Parent->Parent) {
|
|
|
|
if (Parent == Other)
|
2011-12-06 01:28:06 +08:00
|
|
|
return true;
|
2020-07-22 07:03:56 +08:00
|
|
|
}
|
2011-12-06 01:28:06 +08:00
|
|
|
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;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-12-06 06:27:44 +08:00
|
|
|
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);
|
2021-09-14 21:11:23 +08:00
|
|
|
if (!AllowStringLiterals || isValidAsciiIdentifier(Name))
|
2017-06-20 07:09:36 +08:00
|
|
|
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;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
std::string Result;
|
2017-06-20 07:09:36 +08:00
|
|
|
|
|
|
|
llvm::raw_string_ostream Out(Result);
|
|
|
|
printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
|
2018-07-31 03:24:48 +08:00
|
|
|
Out.flush();
|
2017-06-20 07:09:36 +08:00
|
|
|
|
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())
|
2021-05-17 15:40:29 +08:00
|
|
|
return {"", "", U.Entry->getDir()};
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2021-05-17 15:40:29 +08:00
|
|
|
return {UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory,
|
|
|
|
Umbrella.dyn_cast<const DirectoryEntry *>()};
|
2020-02-27 08:29:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::addTopHeader(const FileEntry *File) {
|
|
|
|
assert(File);
|
|
|
|
TopHeaders.insert(File);
|
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) {
|
2019-08-02 05:31:56 +08:00
|
|
|
if (auto FE = FileMgr.getFile(*I))
|
|
|
|
TopHeaders.insert(*FE);
|
2013-03-14 05:13:43 +08:00
|
|
|
}
|
|
|
|
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) {
|
2020-01-29 09:48:15 +08:00
|
|
|
Requirements.push_back(Requirement(std::string(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;
|
|
|
|
|
2020-04-18 07:23:41 +08:00
|
|
|
markUnavailable(/*Unimportable*/true);
|
2014-04-19 06:07:31 +08:00
|
|
|
}
|
|
|
|
|
2020-04-18 07:23:41 +08:00
|
|
|
void Module::markUnavailable(bool Unimportable) {
|
|
|
|
auto needUpdate = [Unimportable](Module *M) {
|
|
|
|
return M->IsAvailable || (!M->IsUnimportable && Unimportable);
|
2015-07-14 03:48:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2020-04-18 07:23:41 +08:00
|
|
|
Current->IsUnimportable |= Unimportable;
|
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()];
|
|
|
|
}
|
|
|
|
|
2019-05-08 05:38:51 +08:00
|
|
|
Module *Module::findOrInferSubmodule(StringRef Name) {
|
|
|
|
llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
|
|
|
|
if (Pos != SubModuleIndex.end())
|
|
|
|
return SubModules[Pos->getValue()];
|
|
|
|
if (!InferSubmodules)
|
|
|
|
return nullptr;
|
|
|
|
Module *Result = new Module(Name, SourceLocation(), this, false, InferExplicitSubmodules, 0);
|
|
|
|
Result->InferExplicitSubmodules = InferExplicitSubmodules;
|
|
|
|
Result->InferSubmodules = InferSubmodules;
|
|
|
|
Result->InferExportWildcard = InferExportWildcard;
|
|
|
|
if (Result->InferExportWildcard)
|
|
|
|
Result->Exports.push_back(Module::ExportDecl(nullptr, true));
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 08:45:39 +08:00
|
|
|
void Module::print(raw_ostream &OS, unsigned Indent, bool Dump) 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";
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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";
|
|
|
|
}
|
2018-07-31 03:24:48 +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);
|
2018-07-31 03:24: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";
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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)
|
2021-03-23 08:45:39 +08:00
|
|
|
(*MI)->print(OS, Indent + 2, Dump);
|
2018-07-31 03:24:48 +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";
|
|
|
|
}
|
|
|
|
|
2021-03-23 08:45:39 +08:00
|
|
|
if (Dump) {
|
|
|
|
for (Module *M : Imports) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
llvm::errs() << "import " << M->getFullModuleName() << "\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";
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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 {
|
2021-03-23 08:45:39 +08:00
|
|
|
print(llvm::errs(), 0, true);
|
2011-12-01 07:21:26 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
// 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);
|
2018-09-13 07:09:23 +08:00
|
|
|
for (Module *E : Exports) {
|
2020-04-18 11:25:15 +08:00
|
|
|
// Don't import non-importable modules.
|
|
|
|
if (!E->isUnimportable())
|
2018-09-13 07:09:23 +08:00
|
|
|
VisitModule({E, &V});
|
|
|
|
}
|
2015-05-01 09:53:09 +08:00
|
|
|
|
|
|
|
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});
|
|
|
|
}
|
2020-02-28 10:13:54 +08:00
|
|
|
|
2020-03-18 03:51:58 +08:00
|
|
|
ASTSourceDescriptor::ASTSourceDescriptor(Module &M)
|
2020-02-28 10:13:54 +08:00
|
|
|
: Signature(M.Signature), ClangModule(&M) {
|
|
|
|
if (M.Directory)
|
|
|
|
Path = M.Directory->getName();
|
2020-10-21 06:11:52 +08:00
|
|
|
if (auto File = M.getASTFile())
|
2020-02-28 10:13:54 +08:00
|
|
|
ASTFile = File->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ASTSourceDescriptor::getModuleName() const {
|
|
|
|
if (ClangModule)
|
|
|
|
return ClangModule->Name;
|
|
|
|
else
|
|
|
|
return std::string(PCHModuleName);
|
|
|
|
}
|