| 1 |
//===--- SemaModule.cpp - Semantic Analysis for Modules -------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// This file implements semantic analysis for modules (C++ modules syntax, |
| 10 |
// Objective-C modules syntax, and Clang header modules). |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#include "clang/AST/ASTConsumer.h" |
| 15 |
#include "clang/AST/ASTMutationListener.h" |
| 16 |
#include "clang/AST/DynamicRecursiveASTVisitor.h" |
| 17 |
#include "clang/Lex/HeaderSearch.h" |
| 18 |
#include "clang/Lex/Preprocessor.h" |
| 19 |
#include "clang/Sema/ParsedAttr.h" |
| 20 |
#include "clang/Sema/SemaInternal.h" |
| 21 |
#include "llvm/ADT/ScopeExit.h" |
| 22 |
#include "llvm/ADT/StringExtras.h" |
| 23 |
|
| 24 |
using namespace clang; |
| 25 |
using namespace sema; |
| 26 |
|
| 27 |
static void checkModuleImportContext(Sema &S, Module *M, |
| 28 |
SourceLocation ImportLoc, DeclContext *DC, |
| 29 |
bool FromInclude = false) { |
| 30 |
SourceLocation ExternCLoc; |
| 31 |
|
| 32 |
if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) { |
| 33 |
switch (LSD->getLanguage()) { |
| 34 |
case LinkageSpecLanguageIDs::C: |
| 35 |
if (ExternCLoc.isInvalid()) |
| 36 |
ExternCLoc = LSD->getBeginLoc(); |
| 37 |
break; |
| 38 |
case LinkageSpecLanguageIDs::CXX: |
| 39 |
break; |
| 40 |
} |
| 41 |
DC = LSD->getParent(); |
| 42 |
} |
| 43 |
|
| 44 |
while (isa<LinkageSpecDecl>(DC) || isa<ExportDecl>(DC)) |
| 45 |
DC = DC->getParent(); |
| 46 |
|
| 47 |
if (!isa<TranslationUnitDecl>(DC)) { |
| 48 |
S.Diag(ImportLoc, (FromInclude && S.isModuleVisible(M)) |
| 49 |
? diag::ext_module_import_not_at_top_level_noop |
| 50 |
: diag::err_module_import_not_at_top_level_fatal) |
| 51 |
<< M->getFullModuleName() << DC; |
| 52 |
S.Diag(cast<Decl>(DC)->getBeginLoc(), |
| 53 |
diag::note_module_import_not_at_top_level) |
| 54 |
<< DC; |
| 55 |
} else if (!M->IsExternC && ExternCLoc.isValid()) { |
| 56 |
S.Diag(ImportLoc, diag::ext_module_import_in_extern_c) |
| 57 |
<< M->getFullModuleName(); |
| 58 |
S.Diag(ExternCLoc, diag::note_extern_c_begins_here); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// We represent the primary and partition names as 'Paths' which are sections |
| 63 |
// of the hierarchical access path for a clang module. However for C++20 |
| 64 |
// the periods in a name are just another character, and we will need to |
| 65 |
// flatten them into a string. |
| 66 |
static std::string stringFromPath(ModuleIdPath Path) { |
| 67 |
std::string Name; |
| 68 |
if (Path.empty()) |
| 69 |
return Name; |
| 70 |
|
| 71 |
for (auto &Piece : Path) { |
| 72 |
if (!Name.empty()) |
| 73 |
Name += "."; |
| 74 |
Name += Piece.getIdentifierInfo()->getName(); |
| 75 |
} |
| 76 |
return Name; |
| 77 |
} |
| 78 |
|
| 79 |
/// Helper function for makeTransitiveImportsVisible to decide whether |
| 80 |
/// the \param Imported module unit is in the same module with the \param |
| 81 |
/// CurrentModule. |
| 82 |
/// \param FoundPrimaryModuleInterface is a helper parameter to record the |
| 83 |
/// primary module interface unit corresponding to the module \param |
| 84 |
/// CurrentModule. Since currently it is expensive to decide whether two module |
| 85 |
/// units come from the same module by comparing the module name. |
| 86 |
static bool |
| 87 |
isImportingModuleUnitFromSameModule(ASTContext &Ctx, Module *Imported, |
| 88 |
Module *CurrentModule, |
| 89 |
Module *&FoundPrimaryModuleInterface) { |
| 90 |
if (!Imported->isNamedModule()) |
| 91 |
return false; |
| 92 |
|
| 93 |
// The a partition unit we're importing must be in the same module of the |
| 94 |
// current module. |
| 95 |
if (Imported->isModulePartition()) |
| 96 |
return true; |
| 97 |
|
| 98 |
// If we found the primary module interface during the search process, we can |
| 99 |
// return quickly to avoid expensive string comparison. |
| 100 |
if (FoundPrimaryModuleInterface) |
| 101 |
return Imported == FoundPrimaryModuleInterface; |
| 102 |
|
| 103 |
if (!CurrentModule) |
| 104 |
return false; |
| 105 |
|
| 106 |
// Then the imported module must be a primary module interface unit. It |
| 107 |
// is only allowed to import the primary module interface unit from the same |
| 108 |
// module in the implementation unit and the implementation partition unit. |
| 109 |
|
| 110 |
// Since we'll handle implementation unit above. We can only care |
| 111 |
// about the implementation partition unit here. |
| 112 |
if (!CurrentModule->isModulePartitionImplementation()) |
| 113 |
return false; |
| 114 |
|
| 115 |
if (Ctx.isInSameModule(Imported, CurrentModule)) { |
| 116 |
assert(!FoundPrimaryModuleInterface || |
| 117 |
FoundPrimaryModuleInterface == Imported); |
| 118 |
FoundPrimaryModuleInterface = Imported; |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
/// [module.import]p7: |
| 126 |
/// Additionally, when a module-import-declaration in a module unit of some |
| 127 |
/// module M imports another module unit U of M, it also imports all |
| 128 |
/// translation units imported by non-exported module-import-declarations in |
| 129 |
/// the module unit purview of U. These rules can in turn lead to the |
| 130 |
/// importation of yet more translation units. |
| 131 |
static void |
| 132 |
makeTransitiveImportsVisible(ASTContext &Ctx, VisibleModuleSet &VisibleModules, |
| 133 |
Module *Imported, Module *CurrentModule, |
| 134 |
SourceLocation ImportLoc, |
| 135 |
bool IsImportingPrimaryModuleInterface = false) { |
| 136 |
assert(Imported->isNamedModule() && |
| 137 |
"'makeTransitiveImportsVisible()' is intended for standard C++ named " |
| 138 |
"modules only."); |
| 139 |
|
| 140 |
llvm::SmallVector<Module *, 4> Worklist; |
| 141 |
llvm::SmallPtrSet<Module *, 16> Visited; |
| 142 |
Worklist.push_back(Imported); |
| 143 |
|
| 144 |
Module *FoundPrimaryModuleInterface = |
| 145 |
IsImportingPrimaryModuleInterface ? Imported : nullptr; |
| 146 |
|
| 147 |
while (!Worklist.empty()) { |
| 148 |
Module *Importing = Worklist.pop_back_val(); |
| 149 |
|
| 150 |
if (Visited.count(Importing)) |
| 151 |
continue; |
| 152 |
Visited.insert(Importing); |
| 153 |
|
| 154 |
// FIXME: The ImportLoc here is not meaningful. It may be problematic if we |
| 155 |
// use the sourcelocation loaded from the visible modules. |
| 156 |
VisibleModules.setVisible(Importing, ImportLoc); |
| 157 |
|
| 158 |
if (isImportingModuleUnitFromSameModule(Ctx, Importing, CurrentModule, |
| 159 |
FoundPrimaryModuleInterface)) { |
| 160 |
for (Module *TransImported : Importing->Imports) |
| 161 |
Worklist.push_back(TransImported); |
| 162 |
|
| 163 |
for (auto [Exports, _] : Importing->Exports) |
| 164 |
Worklist.push_back(Exports); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
Sema::DeclGroupPtrTy |
| 170 |
Sema::ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc) { |
| 171 |
// We start in the global module; |
| 172 |
Module *GlobalModule = |
| 173 |
PushGlobalModuleFragment(ModuleLoc); |
| 174 |
|
| 175 |
// All declarations created from now on are owned by the global module. |
| 176 |
auto *TU = Context.getTranslationUnitDecl(); |
| 177 |
// [module.global.frag]p2 |
| 178 |
// A global-module-fragment specifies the contents of the global module |
| 179 |
// fragment for a module unit. The global module fragment can be used to |
| 180 |
// provide declarations that are attached to the global module and usable |
| 181 |
// within the module unit. |
| 182 |
// |
| 183 |
// So the declations in the global module shouldn't be visible by default. |
| 184 |
TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported); |
| 185 |
TU->setLocalOwningModule(GlobalModule); |
| 186 |
|
| 187 |
// FIXME: Consider creating an explicit representation of this declaration. |
| 188 |
return nullptr; |
| 189 |
} |
| 190 |
|
| 191 |
void Sema::HandleStartOfHeaderUnit() { |
| 192 |
assert(getLangOpts().CPlusPlusModules && |
| 193 |
"Header units are only valid for C++20 modules"); |
| 194 |
SourceLocation StartOfTU = |
| 195 |
SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
| 196 |
|
| 197 |
StringRef HUName = getLangOpts().CurrentModule; |
| 198 |
if (HUName.empty()) { |
| 199 |
HUName = |
| 200 |
SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID())->getName(); |
| 201 |
const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str(); |
| 202 |
} |
| 203 |
|
| 204 |
// TODO: Make the C++20 header lookup independent. |
| 205 |
// When the input is pre-processed source, we need a file ref to the original |
| 206 |
// file for the header map. |
| 207 |
auto F = SourceMgr.getFileManager().getOptionalFileRef(HUName); |
| 208 |
// For the sake of error recovery (if someone has moved the original header |
| 209 |
// after creating the pre-processed output) fall back to obtaining the file |
| 210 |
// ref for the input file, which must be present. |
| 211 |
if (!F) |
| 212 |
F = SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID()); |
| 213 |
assert(F && "failed to find the header unit source?"); |
| 214 |
Module::Header H{HUName.str(), HUName.str(), *F}; |
| 215 |
auto &Map = PP.getHeaderSearchInfo().getModuleMap(); |
| 216 |
Module *Mod = Map.createHeaderUnit(StartOfTU, HUName, H); |
| 217 |
assert(Mod && "module creation should not fail"); |
| 218 |
ModuleScopes.push_back({}); // No GMF |
| 219 |
ModuleScopes.back().BeginLoc = StartOfTU; |
| 220 |
ModuleScopes.back().Module = Mod; |
| 221 |
VisibleModules.setVisible(Mod, StartOfTU); |
| 222 |
|
| 223 |
// From now on, we have an owning module for all declarations we see. |
| 224 |
// All of these are implicitly exported. |
| 225 |
auto *TU = Context.getTranslationUnitDecl(); |
| 226 |
TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::Visible); |
| 227 |
TU->setLocalOwningModule(Mod); |
| 228 |
} |
| 229 |
|
| 230 |
/// Tests whether the given identifier is reserved as a module name and |
| 231 |
/// diagnoses if it is. Returns true if a diagnostic is emitted and false |
| 232 |
/// otherwise. |
| 233 |
static bool DiagReservedModuleName(Sema &S, const IdentifierInfo *II, |
| 234 |
SourceLocation Loc) { |
| 235 |
enum { |
| 236 |
Valid = -1, |
| 237 |
Invalid = 0, |
| 238 |
Reserved = 1, |
| 239 |
} Reason = Valid; |
| 240 |
|
| 241 |
if (II->isStr("module") || II->isStr("import")) |
| 242 |
Reason = Invalid; |
| 243 |
else if (II->isReserved(S.getLangOpts()) != |
| 244 |
ReservedIdentifierStatus::NotReserved) |
| 245 |
Reason = Reserved; |
| 246 |
|
| 247 |
// If the identifier is reserved (not invalid) but is in a system header, |
| 248 |
// we do not diagnose (because we expect system headers to use reserved |
| 249 |
// identifiers). |
| 250 |
if (Reason == Reserved && S.getSourceManager().isInSystemHeader(Loc)) |
| 251 |
Reason = Valid; |
| 252 |
|
| 253 |
switch (Reason) { |
| 254 |
case Valid: |
| 255 |
return false; |
| 256 |
case Invalid: |
| 257 |
return S.Diag(Loc, diag::err_invalid_module_name) << II; |
| 258 |
case Reserved: |
| 259 |
S.Diag(Loc, diag::warn_reserved_module_name) << II; |
| 260 |
return false; |
| 261 |
} |
| 262 |
llvm_unreachable("fell off a fully covered switch"); |
| 263 |
} |
| 264 |
|
| 265 |
Sema::DeclGroupPtrTy |
| 266 |
Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc, |
| 267 |
ModuleDeclKind MDK, ModuleIdPath Path, |
| 268 |
ModuleIdPath Partition, ModuleImportState &ImportState, |
| 269 |
bool SeenNoTrivialPPDirective) { |
| 270 |
assert(getLangOpts().CPlusPlusModules && |
| 271 |
"should only have module decl in standard C++ modules"); |
| 272 |
|
| 273 |
bool IsFirstDecl = ImportState == ModuleImportState::FirstDecl; |
| 274 |
bool SeenGMF = ImportState == ModuleImportState::GlobalFragment; |
| 275 |
// If any of the steps here fail, we count that as invalidating C++20 |
| 276 |
// module state; |
| 277 |
ImportState = ModuleImportState::NotACXX20Module; |
| 278 |
|
| 279 |
bool IsPartition = !Partition.empty(); |
| 280 |
if (IsPartition) |
| 281 |
switch (MDK) { |
| 282 |
case ModuleDeclKind::Implementation: |
| 283 |
MDK = ModuleDeclKind::PartitionImplementation; |
| 284 |
break; |
| 285 |
case ModuleDeclKind::Interface: |
| 286 |
MDK = ModuleDeclKind::PartitionInterface; |
| 287 |
break; |
| 288 |
default: |
| 289 |
llvm_unreachable("how did we get a partition type set?"); |
| 290 |
} |
| 291 |
|
| 292 |
// A (non-partition) module implementation unit requires that we are not |
| 293 |
// compiling a module of any kind. A partition implementation emits an |
| 294 |
// interface (and the AST for the implementation), which will subsequently |
| 295 |
// be consumed to emit a binary. |
| 296 |
// A module interface unit requires that we are not compiling a module map. |
| 297 |
switch (getLangOpts().getCompilingModule()) { |
| 298 |
case LangOptions::CMK_None: |
| 299 |
// It's OK to compile a module interface as a normal translation unit. |
| 300 |
break; |
| 301 |
|
| 302 |
case LangOptions::CMK_ModuleInterface: |
| 303 |
if (MDK != ModuleDeclKind::Implementation) |
| 304 |
break; |
| 305 |
|
| 306 |
// We were asked to compile a module interface unit but this is a module |
| 307 |
// implementation unit. |
| 308 |
Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch) |
| 309 |
<< FixItHint::CreateInsertion(ModuleLoc, "export "); |
| 310 |
MDK = ModuleDeclKind::Interface; |
| 311 |
break; |
| 312 |
|
| 313 |
case LangOptions::CMK_ModuleMap: |
| 314 |
Diag(ModuleLoc, diag::err_module_decl_in_module_map_module); |
| 315 |
return nullptr; |
| 316 |
|
| 317 |
case LangOptions::CMK_HeaderUnit: |
| 318 |
Diag(ModuleLoc, diag::err_module_decl_in_header_unit); |
| 319 |
return nullptr; |
| 320 |
} |
| 321 |
|
| 322 |
assert(ModuleScopes.size() <= 1 && "expected to be at global module scope"); |
| 323 |
|
| 324 |
// FIXME: Most of this work should be done by the preprocessor rather than |
| 325 |
// here, in order to support macro import. |
| 326 |
|
| 327 |
// Only one module-declaration is permitted per source file. |
| 328 |
if (isCurrentModulePurview()) { |
| 329 |
Diag(ModuleLoc, diag::err_module_redeclaration); |
| 330 |
Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module), |
| 331 |
diag::note_prev_module_declaration); |
| 332 |
return nullptr; |
| 333 |
} |
| 334 |
|
| 335 |
assert((!getLangOpts().CPlusPlusModules || |
| 336 |
SeenGMF == (bool)this->TheGlobalModuleFragment) && |
| 337 |
"mismatched global module state"); |
| 338 |
|
| 339 |
// In C++20, A module directive may only appear as the first preprocessing |
| 340 |
// tokens in a file (excluding the global module fragment.). |
| 341 |
if (getLangOpts().CPlusPlusModules && |
| 342 |
(!IsFirstDecl || SeenNoTrivialPPDirective) && !SeenGMF) { |
| 343 |
Diag(ModuleLoc, diag::err_module_decl_not_at_start); |
| 344 |
SourceLocation BeginLoc = PP.getMainFileFirstPPTokenLoc(); |
| 345 |
Diag(BeginLoc, diag::note_global_module_introducer_missing) |
| 346 |
<< FixItHint::CreateInsertion(BeginLoc, "module;\n"); |
| 347 |
} |
| 348 |
|
| 349 |
// C++23 [module.unit]p1: ... The identifiers module and import shall not |
| 350 |
// appear as identifiers in a module-name or module-partition. All |
| 351 |
// module-names either beginning with an identifier consisting of std |
| 352 |
// followed by zero or more digits or containing a reserved identifier |
| 353 |
// ([lex.name]) are reserved and shall not be specified in a |
| 354 |
// module-declaration; no diagnostic is required. |
| 355 |
|
| 356 |
// Test the first part of the path to see if it's std[0-9]+ but allow the |
| 357 |
// name in a system header. |
| 358 |
StringRef FirstComponentName = Path[0].getIdentifierInfo()->getName(); |
| 359 |
if (!getSourceManager().isInSystemHeader(Path[0].getLoc()) && |
| 360 |
(FirstComponentName == "std" || |
| 361 |
(FirstComponentName.starts_with("std") && |
| 362 |
llvm::all_of(FirstComponentName.drop_front(3), &llvm::isDigit)))) |
| 363 |
Diag(Path[0].getLoc(), diag::warn_reserved_module_name) |
| 364 |
<< Path[0].getIdentifierInfo(); |
| 365 |
|
| 366 |
// Then test all of the components in the path to see if any of them are |
| 367 |
// using another kind of reserved or invalid identifier. |
| 368 |
for (auto Part : Path) { |
| 369 |
if (DiagReservedModuleName(*this, Part.getIdentifierInfo(), Part.getLoc())) |
| 370 |
return nullptr; |
| 371 |
} |
| 372 |
|
| 373 |
// Flatten the dots in a module name. Unlike Clang's hierarchical module map |
| 374 |
// modules, the dots here are just another character that can appear in a |
| 375 |
// module name. |
| 376 |
std::string ModuleName = stringFromPath(Path); |
| 377 |
if (IsPartition) { |
| 378 |
ModuleName += ":"; |
| 379 |
ModuleName += stringFromPath(Partition); |
| 380 |
} |
| 381 |
// If a module name was explicitly specified on the command line, it must be |
| 382 |
// correct. |
| 383 |
if (!getLangOpts().CurrentModule.empty() && |
| 384 |
getLangOpts().CurrentModule != ModuleName) { |
| 385 |
Diag(Path.front().getLoc(), diag::err_current_module_name_mismatch) |
| 386 |
<< SourceRange(Path.front().getLoc(), IsPartition |
| 387 |
? Partition.back().getLoc() |
| 388 |
: Path.back().getLoc()) |
| 389 |
<< getLangOpts().CurrentModule; |
| 390 |
return nullptr; |
| 391 |
} |
| 392 |
const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName; |
| 393 |
|
| 394 |
auto &Map = PP.getHeaderSearchInfo().getModuleMap(); |
| 395 |
Module *Mod; // The module we are creating. |
| 396 |
Module *Interface = nullptr; // The interface for an implementation. |
| 397 |
switch (MDK) { |
| 398 |
case ModuleDeclKind::Interface: |
| 399 |
case ModuleDeclKind::PartitionInterface: { |
| 400 |
// We can't have parsed or imported a definition of this module or parsed a |
| 401 |
// module map defining it already. |
| 402 |
if (auto *M = Map.findOrLoadModule(ModuleName)) { |
| 403 |
Diag(Path[0].getLoc(), diag::err_module_redefinition) << ModuleName; |
| 404 |
if (M->DefinitionLoc.isValid()) |
| 405 |
Diag(M->DefinitionLoc, diag::note_prev_module_definition); |
| 406 |
else if (OptionalFileEntryRef FE = M->getASTFile()) |
| 407 |
Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file) |
| 408 |
<< FE->getName(); |
| 409 |
Mod = M; |
| 410 |
break; |
| 411 |
} |
| 412 |
|
| 413 |
// Create a Module for the module that we're defining. |
| 414 |
Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName); |
| 415 |
if (MDK == ModuleDeclKind::PartitionInterface) |
| 416 |
Mod->Kind = Module::ModulePartitionInterface; |
| 417 |
assert(Mod && "module creation should not fail"); |
| 418 |
break; |
| 419 |
} |
| 420 |
|
| 421 |
case ModuleDeclKind::Implementation: { |
| 422 |
// C++20 A module-declaration that contains neither an export- |
| 423 |
// keyword nor a module-partition implicitly imports the primary |
| 424 |
// module interface unit of the module as if by a module-import- |
| 425 |
// declaration. |
| 426 |
IdentifierLoc ModuleNameLoc(Path[0].getLoc(), |
| 427 |
PP.getIdentifierInfo(ModuleName)); |
| 428 |
|
| 429 |
// The module loader will assume we're trying to import the module that |
| 430 |
// we're building if `LangOpts.CurrentModule` equals to 'ModuleName'. |
| 431 |
// Change the value for `LangOpts.CurrentModule` temporarily to make the |
| 432 |
// module loader work properly. |
| 433 |
const_cast<LangOptions &>(getLangOpts()).CurrentModule = ""; |
| 434 |
Interface = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc}, |
| 435 |
Module::AllVisible, |
| 436 |
/*IsInclusionDirective=*/false); |
| 437 |
const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName; |
| 438 |
|
| 439 |
if (!Interface) { |
| 440 |
Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName; |
| 441 |
// Create an empty module interface unit for error recovery. |
| 442 |
Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName); |
| 443 |
} else { |
| 444 |
Mod = Map.createModuleForImplementationUnit(ModuleLoc, ModuleName); |
| 445 |
} |
| 446 |
} break; |
| 447 |
|
| 448 |
case ModuleDeclKind::PartitionImplementation: |
| 449 |
// Create an interface, but note that it is an implementation |
| 450 |
// unit. |
| 451 |
Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName); |
| 452 |
Mod->Kind = Module::ModulePartitionImplementation; |
| 453 |
break; |
| 454 |
} |
| 455 |
|
| 456 |
if (!this->TheGlobalModuleFragment) { |
| 457 |
ModuleScopes.push_back({}); |
| 458 |
if (getLangOpts().ModulesLocalVisibility) |
| 459 |
ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules); |
| 460 |
} else { |
| 461 |
// We're done with the global module fragment now. |
| 462 |
ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global); |
| 463 |
} |
| 464 |
|
| 465 |
// Switch from the global module fragment (if any) to the named module. |
| 466 |
ModuleScopes.back().BeginLoc = StartLoc; |
| 467 |
ModuleScopes.back().Module = Mod; |
| 468 |
VisibleModules.setVisible(Mod, ModuleLoc); |
| 469 |
|
| 470 |
// From now on, we have an owning module for all declarations we see. |
| 471 |
// In C++20 modules, those declaration would be reachable when imported |
| 472 |
// unless explicitily exported. |
| 473 |
// Otherwise, those declarations are module-private unless explicitly |
| 474 |
// exported. |
| 475 |
auto *TU = Context.getTranslationUnitDecl(); |
| 476 |
TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported); |
| 477 |
TU->setLocalOwningModule(Mod); |
| 478 |
|
| 479 |
// We are in the module purview, but before any other (non import) |
| 480 |
// statements, so imports are allowed. |
| 481 |
ImportState = ModuleImportState::ImportAllowed; |
| 482 |
|
| 483 |
getASTContext().setCurrentNamedModule(Mod); |
| 484 |
|
| 485 |
if (auto *Listener = getASTMutationListener()) |
| 486 |
Listener->EnteringModulePurview(); |
| 487 |
|
| 488 |
// We already potentially made an implicit import (in the case of a module |
| 489 |
// implementation unit importing its interface). Make this module visible |
| 490 |
// and return the import decl to be added to the current TU. |
| 491 |
if (Interface) { |
| 492 |
HadImportedNamedModules = true; |
| 493 |
|
| 494 |
makeTransitiveImportsVisible(getASTContext(), VisibleModules, Interface, |
| 495 |
Mod, ModuleLoc, |
| 496 |
/*IsImportingPrimaryModuleInterface=*/true); |
| 497 |
|
| 498 |
// Make the import decl for the interface in the impl module. |
| 499 |
ImportDecl *Import = ImportDecl::Create(Context, CurContext, ModuleLoc, |
| 500 |
Interface, Path[0].getLoc()); |
| 501 |
CurContext->addDecl(Import); |
| 502 |
|
| 503 |
// Sequence initialization of the imported module before that of the current |
| 504 |
// module, if any. |
| 505 |
Context.addModuleInitializer(ModuleScopes.back().Module, Import); |
| 506 |
Mod->Imports.insert(Interface); // As if we imported it. |
| 507 |
// Also save this as a shortcut to checking for decls in the interface |
| 508 |
ThePrimaryInterface = Interface; |
| 509 |
// If we made an implicit import of the module interface, then return the |
| 510 |
// imported module decl. |
| 511 |
return ConvertDeclToDeclGroup(Import); |
| 512 |
} |
| 513 |
|
| 514 |
return nullptr; |
| 515 |
} |
| 516 |
|
| 517 |
Sema::DeclGroupPtrTy |
| 518 |
Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc, |
| 519 |
SourceLocation PrivateLoc) { |
| 520 |
// C++20 [basic.link]/2: |
| 521 |
// A private-module-fragment shall appear only in a primary module |
| 522 |
// interface unit. |
| 523 |
switch (ModuleScopes.empty() ? Module::ExplicitGlobalModuleFragment |
| 524 |
: ModuleScopes.back().Module->Kind) { |
| 525 |
case Module::ModuleMapModule: |
| 526 |
case Module::ExplicitGlobalModuleFragment: |
| 527 |
case Module::ImplicitGlobalModuleFragment: |
| 528 |
case Module::ModulePartitionImplementation: |
| 529 |
case Module::ModulePartitionInterface: |
| 530 |
case Module::ModuleHeaderUnit: |
| 531 |
Diag(PrivateLoc, diag::err_private_module_fragment_not_module); |
| 532 |
return nullptr; |
| 533 |
|
| 534 |
case Module::PrivateModuleFragment: |
| 535 |
Diag(PrivateLoc, diag::err_private_module_fragment_redefined); |
| 536 |
Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition); |
| 537 |
return nullptr; |
| 538 |
|
| 539 |
case Module::ModuleImplementationUnit: |
| 540 |
Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface); |
| 541 |
Diag(ModuleScopes.back().BeginLoc, |
| 542 |
diag::note_not_module_interface_add_export) |
| 543 |
<< FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export "); |
| 544 |
return nullptr; |
| 545 |
|
| 546 |
case Module::ModuleInterfaceUnit: |
| 547 |
break; |
| 548 |
} |
| 549 |
|
| 550 |
// FIXME: Check that this translation unit does not import any partitions; |
| 551 |
// such imports would violate [basic.link]/2's "shall be the only module unit" |
| 552 |
// restriction. |
| 553 |
|
| 554 |
// We've finished the public fragment of the translation unit. |
| 555 |
ActOnEndOfTranslationUnitFragment(TUFragmentKind::Normal); |
| 556 |
|
| 557 |
auto &Map = PP.getHeaderSearchInfo().getModuleMap(); |
| 558 |
Module *PrivateModuleFragment = |
| 559 |
Map.createPrivateModuleFragmentForInterfaceUnit( |
| 560 |
ModuleScopes.back().Module, PrivateLoc); |
| 561 |
assert(PrivateModuleFragment && "module creation should not fail"); |
| 562 |
|
| 563 |
// Enter the scope of the private module fragment. |
| 564 |
ModuleScopes.push_back({}); |
| 565 |
ModuleScopes.back().BeginLoc = ModuleLoc; |
| 566 |
ModuleScopes.back().Module = PrivateModuleFragment; |
| 567 |
VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc); |
| 568 |
|
| 569 |
// All declarations created from now on are scoped to the private module |
| 570 |
// fragment (and are neither visible nor reachable in importers of the module |
| 571 |
// interface). |
| 572 |
auto *TU = Context.getTranslationUnitDecl(); |
| 573 |
TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate); |
| 574 |
TU->setLocalOwningModule(PrivateModuleFragment); |
| 575 |
|
| 576 |
// FIXME: Consider creating an explicit representation of this declaration. |
| 577 |
return nullptr; |
| 578 |
} |
| 579 |
|
| 580 |
DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc, |
| 581 |
SourceLocation ExportLoc, |
| 582 |
SourceLocation ImportLoc, ModuleIdPath Path, |
| 583 |
bool IsPartition) { |
| 584 |
assert((!IsPartition || getLangOpts().CPlusPlusModules) && |
| 585 |
"partition seen in non-C++20 code?"); |
| 586 |
|
| 587 |
// For a C++20 module name, flatten into a single identifier with the source |
| 588 |
// location of the first component. |
| 589 |
IdentifierLoc ModuleNameLoc; |
| 590 |
|
| 591 |
std::string ModuleName; |
| 592 |
if (IsPartition) { |
| 593 |
// We already checked that we are in a module purview in the parser. |
| 594 |
assert(!ModuleScopes.empty() && "in a module purview, but no module?"); |
| 595 |
Module *NamedMod = ModuleScopes.back().Module; |
| 596 |
// If we are importing into a partition, find the owning named module, |
| 597 |
// otherwise, the name of the importing named module. |
| 598 |
ModuleName = NamedMod->getPrimaryModuleInterfaceName().str(); |
| 599 |
ModuleName += ":"; |
| 600 |
ModuleName += stringFromPath(Path); |
| 601 |
ModuleNameLoc = |
| 602 |
IdentifierLoc(Path[0].getLoc(), PP.getIdentifierInfo(ModuleName)); |
| 603 |
Path = ModuleIdPath(ModuleNameLoc); |
| 604 |
} else if (getLangOpts().CPlusPlusModules) { |
| 605 |
ModuleName = stringFromPath(Path); |
| 606 |
ModuleNameLoc = |
| 607 |
IdentifierLoc(Path[0].getLoc(), PP.getIdentifierInfo(ModuleName)); |
| 608 |
Path = ModuleIdPath(ModuleNameLoc); |
| 609 |
} |
| 610 |
|
| 611 |
// Diagnose self-import before attempting a load. |
| 612 |
// [module.import]/9 |
| 613 |
// A module implementation unit of a module M that is not a module partition |
| 614 |
// shall not contain a module-import-declaration nominating M. |
| 615 |
// (for an implementation, the module interface is imported implicitly, |
| 616 |
// but that's handled in the module decl code). |
| 617 |
|
| 618 |
if (getLangOpts().CPlusPlusModules && isCurrentModulePurview() && |
| 619 |
getCurrentModule()->Name == ModuleName) { |
| 620 |
Diag(ImportLoc, diag::err_module_self_import_cxx20) |
| 621 |
<< ModuleName << currentModuleIsImplementation(); |
| 622 |
return true; |
| 623 |
} |
| 624 |
|
| 625 |
Module *Mod = getModuleLoader().loadModule( |
| 626 |
ImportLoc, Path, Module::AllVisible, /*IsInclusionDirective=*/false); |
| 627 |
if (!Mod) |
| 628 |
return true; |
| 629 |
|
| 630 |
if (!Mod->isInterfaceOrPartition() && !ModuleName.empty() && |
| 631 |
!getLangOpts().ObjC) { |
| 632 |
Diag(ImportLoc, diag::err_module_import_non_interface_nor_parition) |
| 633 |
<< ModuleName; |
| 634 |
return true; |
| 635 |
} |
| 636 |
|
| 637 |
return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path); |
| 638 |
} |
| 639 |
|
| 640 |
/// Determine whether \p D is lexically within an export-declaration. |
| 641 |
static const ExportDecl *getEnclosingExportDecl(const Decl *D) { |
| 642 |
for (auto *DC = D->getLexicalDeclContext(); DC; DC = DC->getLexicalParent()) |
| 643 |
if (auto *ED = dyn_cast<ExportDecl>(DC)) |
| 644 |
return ED; |
| 645 |
return nullptr; |
| 646 |
} |
| 647 |
|
| 648 |
DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc, |
| 649 |
SourceLocation ExportLoc, |
| 650 |
SourceLocation ImportLoc, Module *Mod, |
| 651 |
ModuleIdPath Path) { |
| 652 |
if (Mod->isHeaderUnit()) |
| 653 |
Diag(ImportLoc, diag::warn_experimental_header_unit); |
| 654 |
|
| 655 |
if (Mod->isNamedModule()) |
| 656 |
makeTransitiveImportsVisible(getASTContext(), VisibleModules, Mod, |
| 657 |
getCurrentModule(), ImportLoc); |
| 658 |
else |
| 659 |
VisibleModules.setVisible(Mod, ImportLoc); |
| 660 |
|
| 661 |
assert((!Mod->isModulePartitionImplementation() || getCurrentModule()) && |
| 662 |
"We can only import a partition unit in a named module."); |
| 663 |
if (Mod->isModulePartitionImplementation() && |
| 664 |
getCurrentModule()->isModuleInterfaceUnit()) |
| 665 |
Diag(ImportLoc, |
| 666 |
diag::warn_import_implementation_partition_unit_in_interface_unit) |
| 667 |
<< Mod->Name; |
| 668 |
|
| 669 |
checkModuleImportContext(*this, Mod, ImportLoc, CurContext); |
| 670 |
|
| 671 |
// FIXME: we should support importing a submodule within a different submodule |
| 672 |
// of the same top-level module. Until we do, make it an error rather than |
| 673 |
// silently ignoring the import. |
| 674 |
// FIXME: Should we warn on a redundant import of the current module? |
| 675 |
if (Mod->isForBuilding(getLangOpts())) { |
| 676 |
Diag(ImportLoc, getLangOpts().isCompilingModule() |
| 677 |
? diag::err_module_self_import |
| 678 |
: diag::err_module_import_in_implementation) |
| 679 |
<< Mod->getFullModuleName() << getLangOpts().CurrentModule; |
| 680 |
} |
| 681 |
|
| 682 |
SmallVector<SourceLocation, 2> IdentifierLocs; |
| 683 |
|
| 684 |
if (Path.empty()) { |
| 685 |
// If this was a header import, pad out with dummy locations. |
| 686 |
// FIXME: Pass in and use the location of the header-name token in this |
| 687 |
// case. |
| 688 |
for (Module *ModCheck = Mod; ModCheck; ModCheck = ModCheck->Parent) |
| 689 |
IdentifierLocs.push_back(SourceLocation()); |
| 690 |
} else if (getLangOpts().CPlusPlusModules && !Mod->Parent) { |
| 691 |
// A single identifier for the whole name. |
| 692 |
IdentifierLocs.push_back(Path[0].getLoc()); |
| 693 |
} else { |
| 694 |
Module *ModCheck = Mod; |
| 695 |
for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 696 |
// If we've run out of module parents, just drop the remaining |
| 697 |
// identifiers. We need the length to be consistent. |
| 698 |
if (!ModCheck) |
| 699 |
break; |
| 700 |
ModCheck = ModCheck->Parent; |
| 701 |
|
| 702 |
IdentifierLocs.push_back(Path[I].getLoc()); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
ImportDecl *Import = ImportDecl::Create(Context, CurContext, StartLoc, |
| 707 |
Mod, IdentifierLocs); |
| 708 |
CurContext->addDecl(Import); |
| 709 |
|
| 710 |
// Sequence initialization of the imported module before that of the current |
| 711 |
// module, if any. |
| 712 |
if (!ModuleScopes.empty()) |
| 713 |
Context.addModuleInitializer(ModuleScopes.back().Module, Import); |
| 714 |
|
| 715 |
// A module (partition) implementation unit shall not be exported. |
| 716 |
if (getLangOpts().CPlusPlusModules && ExportLoc.isValid() && |
| 717 |
Mod->Kind == Module::ModuleKind::ModulePartitionImplementation) { |
| 718 |
Diag(ExportLoc, diag::err_export_partition_impl) |
| 719 |
<< SourceRange(ExportLoc, Path.back().getLoc()); |
| 720 |
} else if (ExportLoc.isValid() && |
| 721 |
(ModuleScopes.empty() || currentModuleIsImplementation())) { |
| 722 |
// [module.interface]p1: |
| 723 |
// An export-declaration shall inhabit a namespace scope and appear in the |
| 724 |
// purview of a module interface unit. |
| 725 |
Diag(ExportLoc, diag::err_export_not_in_module_interface); |
| 726 |
} else if (!ModuleScopes.empty()) { |
| 727 |
// Re-export the module if the imported module is exported. |
| 728 |
// Note that we don't need to add re-exported module to Imports field |
| 729 |
// since `Exports` implies the module is imported already. |
| 730 |
if (ExportLoc.isValid() || getEnclosingExportDecl(Import)) |
| 731 |
getCurrentModule()->Exports.emplace_back(Mod, false); |
| 732 |
else |
| 733 |
getCurrentModule()->Imports.insert(Mod); |
| 734 |
} |
| 735 |
|
| 736 |
HadImportedNamedModules = true; |
| 737 |
|
| 738 |
return Import; |
| 739 |
} |
| 740 |
|
| 741 |
void Sema::ActOnAnnotModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { |
| 742 |
checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true); |
| 743 |
BuildModuleInclude(DirectiveLoc, Mod); |
| 744 |
} |
| 745 |
|
| 746 |
void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { |
| 747 |
// Determine whether we're in the #include buffer for a module. The #includes |
| 748 |
// in that buffer do not qualify as module imports; they're just an |
| 749 |
// implementation detail of us building the module. |
| 750 |
// |
| 751 |
// FIXME: Should we even get ActOnAnnotModuleInclude calls for those? |
| 752 |
bool IsInModuleIncludes = |
| 753 |
TUKind == TU_ClangModule && |
| 754 |
getSourceManager().isWrittenInMainFile(DirectiveLoc); |
| 755 |
|
| 756 |
// If we are really importing a module (not just checking layering) due to an |
| 757 |
// #include in the main file, synthesize an ImportDecl. |
| 758 |
if (getLangOpts().Modules && !IsInModuleIncludes) { |
| 759 |
TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); |
| 760 |
ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, |
| 761 |
DirectiveLoc, Mod, |
| 762 |
DirectiveLoc); |
| 763 |
if (!ModuleScopes.empty()) |
| 764 |
Context.addModuleInitializer(ModuleScopes.back().Module, ImportD); |
| 765 |
TU->addDecl(ImportD); |
| 766 |
Consumer.HandleImplicitImportDecl(ImportD); |
| 767 |
} |
| 768 |
|
| 769 |
getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc); |
| 770 |
VisibleModules.setVisible(Mod, DirectiveLoc); |
| 771 |
|
| 772 |
if (getLangOpts().isCompilingModule()) { |
| 773 |
Module *ThisModule = PP.getHeaderSearchInfo().lookupModule( |
| 774 |
getLangOpts().CurrentModule, DirectiveLoc, false, false); |
| 775 |
(void)ThisModule; |
| 776 |
// For named modules, the current module name is not known while parsing the |
| 777 |
// global module fragment and lookupModule may return null. |
| 778 |
assert((getLangOpts().getCompilingModule() == |
| 779 |
LangOptionsBase::CMK_ModuleInterface || |
| 780 |
ThisModule) && |
| 781 |
"was expecting a module if building a Clang module"); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
void Sema::ActOnAnnotModuleBegin(SourceLocation DirectiveLoc, Module *Mod) { |
| 786 |
checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true); |
| 787 |
|
| 788 |
ModuleScopes.push_back({}); |
| 789 |
ModuleScopes.back().Module = Mod; |
| 790 |
if (getLangOpts().ModulesLocalVisibility) |
| 791 |
ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules); |
| 792 |
|
| 793 |
VisibleModules.setVisible(Mod, DirectiveLoc); |
| 794 |
|
| 795 |
// The enclosing context is now part of this module. |
| 796 |
// FIXME: Consider creating a child DeclContext to hold the entities |
| 797 |
// lexically within the module. |
| 798 |
if (getLangOpts().trackLocalOwningModule()) { |
| 799 |
for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) { |
| 800 |
cast<Decl>(DC)->setModuleOwnershipKind( |
| 801 |
getLangOpts().ModulesLocalVisibility |
| 802 |
? Decl::ModuleOwnershipKind::VisibleWhenImported |
| 803 |
: Decl::ModuleOwnershipKind::Visible); |
| 804 |
cast<Decl>(DC)->setLocalOwningModule(Mod); |
| 805 |
} |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
void Sema::ActOnAnnotModuleEnd(SourceLocation EomLoc, Module *Mod) { |
| 810 |
if (getLangOpts().ModulesLocalVisibility) { |
| 811 |
VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules); |
| 812 |
// Leaving a module hides namespace names, so our visible namespace cache |
| 813 |
// is now out of date. |
| 814 |
VisibleNamespaceCache.clear(); |
| 815 |
} |
| 816 |
|
| 817 |
assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod && |
| 818 |
"left the wrong module scope"); |
| 819 |
ModuleScopes.pop_back(); |
| 820 |
|
| 821 |
// We got to the end of processing a local module. Create an |
| 822 |
// ImportDecl as we would for an imported module. |
| 823 |
FileID File = getSourceManager().getFileID(EomLoc); |
| 824 |
SourceLocation DirectiveLoc; |
| 825 |
if (EomLoc == getSourceManager().getLocForEndOfFile(File)) { |
| 826 |
// We reached the end of a #included module header. Use the #include loc. |
| 827 |
assert(File != getSourceManager().getMainFileID() && |
| 828 |
"end of submodule in main source file"); |
| 829 |
DirectiveLoc = getSourceManager().getIncludeLoc(File); |
| 830 |
} else { |
| 831 |
// We reached an EOM pragma. Use the pragma location. |
| 832 |
DirectiveLoc = EomLoc; |
| 833 |
} |
| 834 |
BuildModuleInclude(DirectiveLoc, Mod); |
| 835 |
|
| 836 |
// Any further declarations are in whatever module we returned to. |
| 837 |
if (getLangOpts().trackLocalOwningModule()) { |
| 838 |
// The parser guarantees that this is the same context that we entered |
| 839 |
// the module within. |
| 840 |
for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) { |
| 841 |
cast<Decl>(DC)->setLocalOwningModule(getCurrentModule()); |
| 842 |
if (!getCurrentModule()) |
| 843 |
cast<Decl>(DC)->setModuleOwnershipKind( |
| 844 |
Decl::ModuleOwnershipKind::Unowned); |
| 845 |
} |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc, |
| 850 |
Module *Mod) { |
| 851 |
// Bail if we're not allowed to implicitly import a module here. |
| 852 |
if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery || |
| 853 |
VisibleModules.isVisible(Mod)) |
| 854 |
return; |
| 855 |
|
| 856 |
// Create the implicit import declaration. |
| 857 |
TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); |
| 858 |
ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, |
| 859 |
Loc, Mod, Loc); |
| 860 |
TU->addDecl(ImportD); |
| 861 |
Consumer.HandleImplicitImportDecl(ImportD); |
| 862 |
|
| 863 |
// Make the module visible. |
| 864 |
getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc); |
| 865 |
VisibleModules.setVisible(Mod, Loc); |
| 866 |
} |
| 867 |
|
| 868 |
Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc, |
| 869 |
SourceLocation LBraceLoc) { |
| 870 |
ExportDecl *D = ExportDecl::Create(Context, CurContext, ExportLoc); |
| 871 |
|
| 872 |
// Set this temporarily so we know the export-declaration was braced. |
| 873 |
D->setRBraceLoc(LBraceLoc); |
| 874 |
|
| 875 |
CurContext->addDecl(D); |
| 876 |
PushDeclContext(S, D); |
| 877 |
|
| 878 |
// C++2a [module.interface]p1: |
| 879 |
// An export-declaration shall appear only [...] in the purview of a module |
| 880 |
// interface unit. An export-declaration shall not appear directly or |
| 881 |
// indirectly within [...] a private-module-fragment. |
| 882 |
if (!getLangOpts().HLSL) { |
| 883 |
if (!isCurrentModulePurview()) { |
| 884 |
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0; |
| 885 |
D->setInvalidDecl(); |
| 886 |
return D; |
| 887 |
} else if (currentModuleIsImplementation()) { |
| 888 |
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1; |
| 889 |
Diag(ModuleScopes.back().BeginLoc, |
| 890 |
diag::note_not_module_interface_add_export) |
| 891 |
<< FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export "); |
| 892 |
D->setInvalidDecl(); |
| 893 |
return D; |
| 894 |
} else if (ModuleScopes.back().Module->Kind == |
| 895 |
Module::PrivateModuleFragment) { |
| 896 |
Diag(ExportLoc, diag::err_export_in_private_module_fragment); |
| 897 |
Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment); |
| 898 |
D->setInvalidDecl(); |
| 899 |
return D; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
for (const DeclContext *DC = CurContext; DC; DC = DC->getLexicalParent()) { |
| 904 |
if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) { |
| 905 |
// An export-declaration shall not appear directly or indirectly within |
| 906 |
// an unnamed namespace [...] |
| 907 |
if (ND->isAnonymousNamespace()) { |
| 908 |
Diag(ExportLoc, diag::err_export_within_anonymous_namespace); |
| 909 |
Diag(ND->getLocation(), diag::note_anonymous_namespace); |
| 910 |
// Don't diagnose internal-linkage declarations in this region. |
| 911 |
D->setInvalidDecl(); |
| 912 |
return D; |
| 913 |
} |
| 914 |
|
| 915 |
// A declaration is exported if it is [...] a namespace-definition |
| 916 |
// that contains an exported declaration. |
| 917 |
// |
| 918 |
// Defer exporting the namespace until after we leave it, in order to |
| 919 |
// avoid marking all subsequent declarations in the namespace as exported. |
| 920 |
if (!getLangOpts().HLSL && !DeferredExportedNamespaces.insert(ND).second) |
| 921 |
break; |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
// [...] its declaration or declaration-seq shall not contain an |
| 926 |
// export-declaration. |
| 927 |
if (auto *ED = getEnclosingExportDecl(D)) { |
| 928 |
Diag(ExportLoc, diag::err_export_within_export); |
| 929 |
if (ED->hasBraces()) |
| 930 |
Diag(ED->getLocation(), diag::note_export); |
| 931 |
D->setInvalidDecl(); |
| 932 |
return D; |
| 933 |
} |
| 934 |
|
| 935 |
if (!getLangOpts().HLSL) |
| 936 |
D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported); |
| 937 |
|
| 938 |
return D; |
| 939 |
} |
| 940 |
|
| 941 |
static bool checkExportedDecl(Sema &, Decl *, SourceLocation); |
| 942 |
|
| 943 |
/// Check that it's valid to export all the declarations in \p DC. |
| 944 |
static bool checkExportedDeclContext(Sema &S, DeclContext *DC, |
| 945 |
SourceLocation BlockStart) { |
| 946 |
bool AllUnnamed = true; |
| 947 |
for (auto *D : DC->decls()) |
| 948 |
AllUnnamed &= checkExportedDecl(S, D, BlockStart); |
| 949 |
return AllUnnamed; |
| 950 |
} |
| 951 |
|
| 952 |
/// Check that it's valid to export \p D. |
| 953 |
static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) { |
| 954 |
|
| 955 |
// HLSL: export declaration is valid only on functions |
| 956 |
if (S.getLangOpts().HLSL) { |
| 957 |
// Export-within-export was already diagnosed in ActOnStartExportDecl |
| 958 |
if (!isa<FunctionDecl, ExportDecl>(D)) { |
| 959 |
S.Diag(D->getBeginLoc(), diag::err_hlsl_export_not_on_function); |
| 960 |
D->setInvalidDecl(); |
| 961 |
return false; |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
// C++20 [module.interface]p3: |
| 966 |
// [...] it shall not declare a name with internal linkage. |
| 967 |
bool HasName = false; |
| 968 |
if (auto *ND = dyn_cast<NamedDecl>(D)) { |
| 969 |
// Don't diagnose anonymous union objects; we'll diagnose their members |
| 970 |
// instead. |
| 971 |
HasName = (bool)ND->getDeclName(); |
| 972 |
if (HasName && ND->getFormalLinkage() == Linkage::Internal) { |
| 973 |
S.Diag(ND->getLocation(), diag::err_export_internal) << ND; |
| 974 |
if (BlockStart.isValid()) |
| 975 |
S.Diag(BlockStart, diag::note_export); |
| 976 |
return false; |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
// C++2a [module.interface]p5: |
| 981 |
// all entities to which all of the using-declarators ultimately refer |
| 982 |
// shall have been introduced with a name having external linkage |
| 983 |
if (auto *USD = dyn_cast<UsingShadowDecl>(D)) { |
| 984 |
NamedDecl *Target = USD->getUnderlyingDecl(); |
| 985 |
Linkage Lk = Target->getFormalLinkage(); |
| 986 |
if (Lk == Linkage::Internal || Lk == Linkage::Module) { |
| 987 |
S.Diag(USD->getLocation(), diag::err_export_using_internal) |
| 988 |
<< (Lk == Linkage::Internal ? 0 : 1) << Target; |
| 989 |
S.Diag(Target->getLocation(), diag::note_using_decl_target); |
| 990 |
if (BlockStart.isValid()) |
| 991 |
S.Diag(BlockStart, diag::note_export); |
| 992 |
return false; |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
// Recurse into namespace-scope DeclContexts. (Only namespace-scope |
| 997 |
// declarations are exported). |
| 998 |
if (auto *DC = dyn_cast<DeclContext>(D)) { |
| 999 |
if (!isa<NamespaceDecl>(D)) |
| 1000 |
return true; |
| 1001 |
|
| 1002 |
if (auto *ND = dyn_cast<NamedDecl>(D)) { |
| 1003 |
if (!ND->getDeclName()) { |
| 1004 |
S.Diag(ND->getLocation(), diag::err_export_anon_ns_internal); |
| 1005 |
if (BlockStart.isValid()) |
| 1006 |
S.Diag(BlockStart, diag::note_export); |
| 1007 |
return false; |
| 1008 |
} else if (!DC->decls().empty() && |
| 1009 |
DC->getRedeclContext()->isFileContext()) { |
| 1010 |
return checkExportedDeclContext(S, DC, BlockStart); |
| 1011 |
} |
| 1012 |
} |
| 1013 |
} |
| 1014 |
return true; |
| 1015 |
} |
| 1016 |
|
| 1017 |
Decl *Sema::ActOnFinishExportDecl(Scope *S, Decl *D, SourceLocation RBraceLoc) { |
| 1018 |
auto *ED = cast<ExportDecl>(D); |
| 1019 |
if (RBraceLoc.isValid()) |
| 1020 |
ED->setRBraceLoc(RBraceLoc); |
| 1021 |
|
| 1022 |
PopDeclContext(); |
| 1023 |
|
| 1024 |
if (!D->isInvalidDecl()) { |
| 1025 |
SourceLocation BlockStart = |
| 1026 |
ED->hasBraces() ? ED->getBeginLoc() : SourceLocation(); |
| 1027 |
for (auto *Child : ED->decls()) { |
| 1028 |
checkExportedDecl(*this, Child, BlockStart); |
| 1029 |
if (auto *FD = dyn_cast<FunctionDecl>(Child)) { |
| 1030 |
// [dcl.inline]/7 |
| 1031 |
// If an inline function or variable that is attached to a named module |
| 1032 |
// is declared in a definition domain, it shall be defined in that |
| 1033 |
// domain. |
| 1034 |
// So, if the current declaration does not have a definition, we must |
| 1035 |
// check at the end of the TU (or when the PMF starts) to see that we |
| 1036 |
// have a definition at that point. |
| 1037 |
if (FD->isInlineSpecified() && !FD->isDefined()) |
| 1038 |
PendingInlineFuncDecls.insert(FD); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
// Anything exported from a module should never be considered unused. |
| 1044 |
for (auto *Exported : ED->decls()) |
| 1045 |
Exported->markUsed(getASTContext()); |
| 1046 |
|
| 1047 |
return D; |
| 1048 |
} |
| 1049 |
|
| 1050 |
Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc) { |
| 1051 |
// We shouldn't create new global module fragment if there is already |
| 1052 |
// one. |
| 1053 |
if (!TheGlobalModuleFragment) { |
| 1054 |
ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap(); |
| 1055 |
TheGlobalModuleFragment = Map.createGlobalModuleFragmentForModuleUnit( |
| 1056 |
BeginLoc, getCurrentModule()); |
| 1057 |
} |
| 1058 |
|
| 1059 |
assert(TheGlobalModuleFragment && "module creation should not fail"); |
| 1060 |
|
| 1061 |
// Enter the scope of the global module. |
| 1062 |
ModuleScopes.push_back({BeginLoc, TheGlobalModuleFragment, |
| 1063 |
/*OuterVisibleModules=*/{}}); |
| 1064 |
VisibleModules.setVisible(TheGlobalModuleFragment, BeginLoc); |
| 1065 |
|
| 1066 |
return TheGlobalModuleFragment; |
| 1067 |
} |
| 1068 |
|
| 1069 |
void Sema::PopGlobalModuleFragment() { |
| 1070 |
assert(!ModuleScopes.empty() && |
| 1071 |
getCurrentModule()->isExplicitGlobalModule() && |
| 1072 |
"left the wrong module scope, which is not global module fragment"); |
| 1073 |
ModuleScopes.pop_back(); |
| 1074 |
} |
| 1075 |
|
| 1076 |
Module *Sema::PushImplicitGlobalModuleFragment(SourceLocation BeginLoc) { |
| 1077 |
if (!TheImplicitGlobalModuleFragment) { |
| 1078 |
ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap(); |
| 1079 |
TheImplicitGlobalModuleFragment = |
| 1080 |
Map.createImplicitGlobalModuleFragmentForModuleUnit(BeginLoc, |
| 1081 |
getCurrentModule()); |
| 1082 |
} |
| 1083 |
assert(TheImplicitGlobalModuleFragment && "module creation should not fail"); |
| 1084 |
|
| 1085 |
// Enter the scope of the global module. |
| 1086 |
ModuleScopes.push_back({BeginLoc, TheImplicitGlobalModuleFragment, |
| 1087 |
/*OuterVisibleModules=*/{}}); |
| 1088 |
VisibleModules.setVisible(TheImplicitGlobalModuleFragment, BeginLoc); |
| 1089 |
return TheImplicitGlobalModuleFragment; |
| 1090 |
} |
| 1091 |
|
| 1092 |
void Sema::PopImplicitGlobalModuleFragment() { |
| 1093 |
assert(!ModuleScopes.empty() && |
| 1094 |
getCurrentModule()->isImplicitGlobalModule() && |
| 1095 |
"left the wrong module scope, which is not global module fragment"); |
| 1096 |
ModuleScopes.pop_back(); |
| 1097 |
} |
| 1098 |
|
| 1099 |
bool Sema::isCurrentModulePurview() const { |
| 1100 |
if (!getCurrentModule()) |
| 1101 |
return false; |
| 1102 |
|
| 1103 |
/// Does this Module scope describe part of the purview of a standard named |
| 1104 |
/// C++ module? |
| 1105 |
switch (getCurrentModule()->Kind) { |
| 1106 |
case Module::ModuleInterfaceUnit: |
| 1107 |
case Module::ModuleImplementationUnit: |
| 1108 |
case Module::ModulePartitionInterface: |
| 1109 |
case Module::ModulePartitionImplementation: |
| 1110 |
case Module::PrivateModuleFragment: |
| 1111 |
case Module::ImplicitGlobalModuleFragment: |
| 1112 |
return true; |
| 1113 |
default: |
| 1114 |
return false; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
//===----------------------------------------------------------------------===// |
| 1119 |
// Checking Exposure in modules // |
| 1120 |
//===----------------------------------------------------------------------===// |
| 1121 |
|
| 1122 |
namespace { |
| 1123 |
class ExposureChecker { |
| 1124 |
public: |
| 1125 |
ExposureChecker(Sema &S) : SemaRef(S) {} |
| 1126 |
|
| 1127 |
bool checkExposure(const VarDecl *D, bool Diag); |
| 1128 |
bool checkExposure(const CXXRecordDecl *D, bool Diag); |
| 1129 |
bool checkExposure(const Stmt *S, bool Diag); |
| 1130 |
bool checkExposure(const FunctionDecl *D, bool Diag); |
| 1131 |
bool checkExposure(const NamedDecl *D, bool Diag); |
| 1132 |
void checkExposureInContext(const DeclContext *DC); |
| 1133 |
bool isExposureCandidate(const NamedDecl *D); |
| 1134 |
|
| 1135 |
bool isTULocal(QualType Ty); |
| 1136 |
bool isTULocal(const NamedDecl *ND); |
| 1137 |
bool isTULocal(const Expr *E); |
| 1138 |
|
| 1139 |
Sema &SemaRef; |
| 1140 |
|
| 1141 |
private: |
| 1142 |
llvm::DenseSet<const NamedDecl *> ExposureSet; |
| 1143 |
llvm::DenseSet<const NamedDecl *> KnownNonExposureSet; |
| 1144 |
llvm::DenseSet<const NamedDecl *> CheckingDecls; |
| 1145 |
}; |
| 1146 |
|
| 1147 |
bool ExposureChecker::isTULocal(QualType Ty) { |
| 1148 |
// [basic.link]p15: |
| 1149 |
// An entity is TU-local if it is |
| 1150 |
// - a type, type alias, namespace, namespace alias, function, variable, or |
| 1151 |
// template that |
| 1152 |
// -- has internal linkage, or |
| 1153 |
return Ty->getLinkage() == Linkage::Internal; |
| 1154 |
|
| 1155 |
// TODO: |
| 1156 |
// [basic.link]p15.2: |
| 1157 |
// a type with no name that is defined outside a class-specifier, function |
| 1158 |
// body, or initializer or is introduced by a defining-type-specifier that |
| 1159 |
// is used to declare only TU-local entities, |
| 1160 |
} |
| 1161 |
|
| 1162 |
bool ExposureChecker::isTULocal(const NamedDecl *D) { |
| 1163 |
if (!D) |
| 1164 |
return false; |
| 1165 |
|
| 1166 |
// [basic.link]p15: |
| 1167 |
// An entity is TU-local if it is |
| 1168 |
// - a type, type alias, namespace, namespace alias, function, variable, or |
| 1169 |
// template that |
| 1170 |
// -- has internal linkage, or |
| 1171 |
if (D->getLinkageInternal() == Linkage::Internal) |
| 1172 |
return true; |
| 1173 |
|
| 1174 |
if (D->isInAnonymousNamespace()) |
| 1175 |
return true; |
| 1176 |
|
| 1177 |
// [basic.link]p15.1.2: |
| 1178 |
// does not have a name with linkage and is declared, or introduced by a |
| 1179 |
// lambda-expression, within the definition of a TU-local entity, |
| 1180 |
if (D->getLinkageInternal() == Linkage::None) |
| 1181 |
if (auto *ND = dyn_cast<NamedDecl>(D->getDeclContext()); |
| 1182 |
ND && isTULocal(ND)) |
| 1183 |
return true; |
| 1184 |
|
| 1185 |
// [basic.link]p15.3, p15.4: |
| 1186 |
// - a specialization of a TU-local template, |
| 1187 |
// - a specialization of a template with any TU-local template argument, or |
| 1188 |
ArrayRef<TemplateArgument> TemplateArgs; |
| 1189 |
NamedDecl *PrimaryTemplate = nullptr; |
| 1190 |
if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 1191 |
TemplateArgs = CTSD->getTemplateArgs().asArray(); |
| 1192 |
PrimaryTemplate = CTSD->getSpecializedTemplate(); |
| 1193 |
if (isTULocal(PrimaryTemplate)) |
| 1194 |
return true; |
| 1195 |
} else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) { |
| 1196 |
TemplateArgs = VTSD->getTemplateArgs().asArray(); |
| 1197 |
PrimaryTemplate = VTSD->getSpecializedTemplate(); |
| 1198 |
if (isTULocal(PrimaryTemplate)) |
| 1199 |
return true; |
| 1200 |
} else if (auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1201 |
if (auto *TAList = FD->getTemplateSpecializationArgs()) |
| 1202 |
TemplateArgs = TAList->asArray(); |
| 1203 |
|
| 1204 |
PrimaryTemplate = FD->getPrimaryTemplate(); |
| 1205 |
if (isTULocal(PrimaryTemplate)) |
| 1206 |
return true; |
| 1207 |
} |
| 1208 |
|
| 1209 |
if (!PrimaryTemplate) |
| 1210 |
// Following off, we only check for specializations. |
| 1211 |
return false; |
| 1212 |
|
| 1213 |
if (KnownNonExposureSet.count(D)) |
| 1214 |
return false; |
| 1215 |
|
| 1216 |
for (auto &TA : TemplateArgs) { |
| 1217 |
switch (TA.getKind()) { |
| 1218 |
case TemplateArgument::Type: |
| 1219 |
if (isTULocal(TA.getAsType())) |
| 1220 |
return true; |
| 1221 |
break; |
| 1222 |
case TemplateArgument::Declaration: |
| 1223 |
if (isTULocal(TA.getAsDecl())) |
| 1224 |
return true; |
| 1225 |
break; |
| 1226 |
default: |
| 1227 |
break; |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
// Avoid recursions. |
| 1232 |
if (CheckingDecls.count(D)) |
| 1233 |
return false; |
| 1234 |
CheckingDecls.insert(D); |
| 1235 |
llvm::scope_exit RemoveCheckingDecls([&] { CheckingDecls.erase(D); }); |
| 1236 |
|
| 1237 |
// [basic.link]p15.5 |
| 1238 |
// - a specialization of a template whose (possibly instantiated) declaration |
| 1239 |
// is an exposure. |
| 1240 |
if (ExposureSet.count(PrimaryTemplate) || |
| 1241 |
checkExposure(PrimaryTemplate, /*Diag=*/false)) |
| 1242 |
return true; |
| 1243 |
|
| 1244 |
// Avoid calling checkExposure again since it is expensive. |
| 1245 |
KnownNonExposureSet.insert(D); |
| 1246 |
return false; |
| 1247 |
} |
| 1248 |
|
| 1249 |
bool ExposureChecker::isTULocal(const Expr *E) { |
| 1250 |
if (!E) |
| 1251 |
return false; |
| 1252 |
|
| 1253 |
// [basic.link]p16: |
| 1254 |
// A value or object is TU-local if either |
| 1255 |
// - it is of TU-local type, |
| 1256 |
if (isTULocal(E->getType())) |
| 1257 |
return true; |
| 1258 |
|
| 1259 |
E = E->IgnoreParenImpCasts(); |
| 1260 |
// [basic.link]p16.2: |
| 1261 |
// - it is, or is a pointer to, a TU-local function or the object associated |
| 1262 |
// with a TU-local variable, |
| 1263 |
// - it is an object of class or array type and any of its subobjects or any |
| 1264 |
// of the objects or functions to which its non-static data members of |
| 1265 |
// reference type refer is TU-local and is usable in constant expressions, or |
| 1266 |
// FIXME: But how can we know the value of pointers or arrays at compile time? |
| 1267 |
if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 1268 |
if (auto *FD = dyn_cast_or_null<FunctionDecl>(DRE->getFoundDecl())) |
| 1269 |
return isTULocal(FD); |
| 1270 |
else if (auto *VD = dyn_cast_or_null<VarDecl>(DRE->getFoundDecl())) |
| 1271 |
return isTULocal(VD); |
| 1272 |
else if (auto *RD = dyn_cast_or_null<CXXRecordDecl>(DRE->getFoundDecl())) |
| 1273 |
return isTULocal(RD); |
| 1274 |
} |
| 1275 |
|
| 1276 |
// TODO: |
| 1277 |
// [basic.link]p16.4: |
| 1278 |
// it is a reflection value that represents... |
| 1279 |
|
| 1280 |
return false; |
| 1281 |
} |
| 1282 |
|
| 1283 |
bool ExposureChecker::isExposureCandidate(const NamedDecl *D) { |
| 1284 |
if (!D) |
| 1285 |
return false; |
| 1286 |
|
| 1287 |
// [basic.link]p17: |
| 1288 |
// If a (possibly instantiated) declaration of, or a deduction guide for, |
| 1289 |
// a non-TU-local entity in a module interface unit |
| 1290 |
// (outside the private-module-fragment, if any) or |
| 1291 |
// module partition is an exposure, the program is ill-formed. |
| 1292 |
Module *M = D->getOwningModule(); |
| 1293 |
if (!M) |
| 1294 |
return false; |
| 1295 |
// If M is implicit global module, the declaration must be in the purview of |
| 1296 |
// a module unit. |
| 1297 |
if (M->isImplicitGlobalModule()) { |
| 1298 |
M = M->Parent; |
| 1299 |
assert(M && "Implicit global module must have a parent"); |
| 1300 |
} |
| 1301 |
|
| 1302 |
if (!M->isInterfaceOrPartition()) |
| 1303 |
return false; |
| 1304 |
|
| 1305 |
if (D->isImplicit()) |
| 1306 |
return false; |
| 1307 |
|
| 1308 |
// [basic.link]p14: |
| 1309 |
// A declaration is an exposure if it either names a TU-local entity |
| 1310 |
// (defined below), ignoring: |
| 1311 |
// ... |
| 1312 |
// - friend declarations in a class definition |
| 1313 |
if (D->getFriendObjectKind() && |
| 1314 |
isa<CXXRecordDecl>(D->getLexicalDeclContext())) |
| 1315 |
return false; |
| 1316 |
|
| 1317 |
return true; |
| 1318 |
} |
| 1319 |
|
| 1320 |
bool ExposureChecker::checkExposure(const NamedDecl *D, bool Diag) { |
| 1321 |
if (!isExposureCandidate(D)) |
| 1322 |
return false; |
| 1323 |
|
| 1324 |
if (auto *FD = dyn_cast<FunctionDecl>(D)) |
| 1325 |
return checkExposure(FD, Diag); |
| 1326 |
if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) |
| 1327 |
return checkExposure(FTD->getTemplatedDecl(), Diag); |
| 1328 |
|
| 1329 |
if (auto *VD = dyn_cast<VarDecl>(D)) |
| 1330 |
return checkExposure(VD, Diag); |
| 1331 |
if (auto *VTD = dyn_cast<VarTemplateDecl>(D)) |
| 1332 |
return checkExposure(VTD->getTemplatedDecl(), Diag); |
| 1333 |
|
| 1334 |
if (auto *RD = dyn_cast<CXXRecordDecl>(D)) |
| 1335 |
return checkExposure(RD, Diag); |
| 1336 |
|
| 1337 |
if (auto *CTD = dyn_cast<ClassTemplateDecl>(D)) |
| 1338 |
return checkExposure(CTD->getTemplatedDecl(), Diag); |
| 1339 |
|
| 1340 |
return false; |
| 1341 |
} |
| 1342 |
|
| 1343 |
bool ExposureChecker::checkExposure(const FunctionDecl *FD, bool Diag) { |
| 1344 |
bool IsExposure = false; |
| 1345 |
if (isTULocal(FD->getReturnType())) { |
| 1346 |
IsExposure = true; |
| 1347 |
if (Diag) |
| 1348 |
SemaRef.Diag(FD->getReturnTypeSourceRange().getBegin(), |
| 1349 |
diag::warn_exposure) |
| 1350 |
<< FD->getReturnType(); |
| 1351 |
} |
| 1352 |
|
| 1353 |
for (ParmVarDecl *Parms : FD->parameters()) |
| 1354 |
if (isTULocal(Parms->getType())) { |
| 1355 |
IsExposure = true; |
| 1356 |
if (Diag) |
| 1357 |
SemaRef.Diag(Parms->getLocation(), diag::warn_exposure) |
| 1358 |
<< Parms->getType(); |
| 1359 |
} |
| 1360 |
|
| 1361 |
bool IsImplicitInstantiation = |
| 1362 |
FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation; |
| 1363 |
|
| 1364 |
// [basic.link]p14: |
| 1365 |
// A declaration is an exposure if it either names a TU-local entity |
| 1366 |
// (defined below), ignoring: |
| 1367 |
// - the function-body for a non-inline function or function template |
| 1368 |
// (but not the deduced return |
| 1369 |
// type for a (possibly instantiated) definition of a function with a |
| 1370 |
// declared return type that uses a placeholder type |
| 1371 |
// ([dcl.spec.auto])), |
| 1372 |
Diag &= |
| 1373 |
(FD->isInlined() || IsImplicitInstantiation) && !FD->isDependentContext(); |
| 1374 |
|
| 1375 |
IsExposure |= checkExposure(FD->getBody(), Diag); |
| 1376 |
if (IsExposure) |
| 1377 |
ExposureSet.insert(FD); |
| 1378 |
|
| 1379 |
return IsExposure; |
| 1380 |
} |
| 1381 |
|
| 1382 |
bool ExposureChecker::checkExposure(const VarDecl *VD, bool Diag) { |
| 1383 |
bool IsExposure = false; |
| 1384 |
// [basic.link]p14: |
| 1385 |
// A declaration is an exposure if it either names a TU-local entity (defined |
| 1386 |
// below), ignoring: |
| 1387 |
// ... |
| 1388 |
// or defines a constexpr variable initialized to a TU-local value (defined |
| 1389 |
// below). |
| 1390 |
if (VD->isConstexpr() && isTULocal(VD->getInit())) { |
| 1391 |
IsExposure = true; |
| 1392 |
if (Diag) |
| 1393 |
SemaRef.Diag(VD->getInit()->getExprLoc(), diag::warn_exposure) |
| 1394 |
<< VD->getInit(); |
| 1395 |
} |
| 1396 |
|
| 1397 |
if (isTULocal(VD->getType())) { |
| 1398 |
IsExposure = true; |
| 1399 |
if (Diag) |
| 1400 |
SemaRef.Diag(VD->getLocation(), diag::warn_exposure) << VD->getType(); |
| 1401 |
} |
| 1402 |
|
| 1403 |
// [basic.link]p14: |
| 1404 |
// ..., ignoring: |
| 1405 |
// - the initializer for a variable or variable template (but not the |
| 1406 |
// variable's type), |
| 1407 |
// |
| 1408 |
// Note: although the spec says to ignore the initializer for all variable, |
| 1409 |
// for the code we generated now for inline variables, it is dangerous if the |
| 1410 |
// initializer of an inline variable is TULocal. |
| 1411 |
Diag &= !VD->getDeclContext()->isDependentContext() && VD->isInline(); |
| 1412 |
IsExposure |= checkExposure(VD->getInit(), Diag); |
| 1413 |
if (IsExposure) |
| 1414 |
ExposureSet.insert(VD); |
| 1415 |
|
| 1416 |
return IsExposure; |
| 1417 |
} |
| 1418 |
|
| 1419 |
bool ExposureChecker::checkExposure(const CXXRecordDecl *RD, bool Diag) { |
| 1420 |
if (!RD->hasDefinition()) |
| 1421 |
return false; |
| 1422 |
|
| 1423 |
bool IsExposure = false; |
| 1424 |
for (CXXMethodDecl *Method : RD->methods()) |
| 1425 |
IsExposure |= checkExposure(Method, Diag); |
| 1426 |
|
| 1427 |
for (FieldDecl *FD : RD->fields()) { |
| 1428 |
if (isTULocal(FD->getType())) { |
| 1429 |
IsExposure = true; |
| 1430 |
if (Diag) |
| 1431 |
SemaRef.Diag(FD->getLocation(), diag::warn_exposure) << FD->getType(); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
|
| 1435 |
for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1436 |
if (isTULocal(Base.getType())) { |
| 1437 |
IsExposure = true; |
| 1438 |
if (Diag) |
| 1439 |
SemaRef.Diag(Base.getBaseTypeLoc(), diag::warn_exposure) |
| 1440 |
<< Base.getType(); |
| 1441 |
} |
| 1442 |
} |
| 1443 |
|
| 1444 |
if (IsExposure) |
| 1445 |
ExposureSet.insert(RD); |
| 1446 |
|
| 1447 |
return IsExposure; |
| 1448 |
} |
| 1449 |
|
| 1450 |
class ReferenceTULocalChecker : public DynamicRecursiveASTVisitor { |
| 1451 |
public: |
| 1452 |
using CallbackTy = std::function<void(DeclRefExpr *, ValueDecl *)>; |
| 1453 |
|
| 1454 |
ReferenceTULocalChecker(ExposureChecker &C, CallbackTy &&Callback) |
| 1455 |
: Checker(C), Callback(std::move(Callback)) {} |
| 1456 |
|
| 1457 |
bool VisitDeclRefExpr(DeclRefExpr *DRE) override { |
| 1458 |
ValueDecl *Referenced = DRE->getDecl(); |
| 1459 |
if (!Referenced) |
| 1460 |
return true; |
| 1461 |
|
| 1462 |
if (!Checker.isTULocal(Referenced)) |
| 1463 |
// We don't care if the referenced declaration is not TU-local. |
| 1464 |
return true; |
| 1465 |
|
| 1466 |
Qualifiers Qual = DRE->getType().getQualifiers(); |
| 1467 |
// [basic.link]p14: |
| 1468 |
// A declaration is an exposure if it either names a TU-local entity |
| 1469 |
// (defined below), ignoring: |
| 1470 |
// ... |
| 1471 |
// - any reference to a non-volatile const object ... |
| 1472 |
if (Qual.hasConst() && !Qual.hasVolatile()) |
| 1473 |
return true; |
| 1474 |
|
| 1475 |
// [basic.link]p14: |
| 1476 |
// ..., ignoring: |
| 1477 |
// ... |
| 1478 |
// (p14.4) - ... or reference with internal or no linkage initialized with |
| 1479 |
// a constant expression that is not an odr-use |
| 1480 |
ASTContext &Context = Referenced->getASTContext(); |
| 1481 |
Linkage L = Referenced->getLinkageInternal(); |
| 1482 |
if (DRE->isNonOdrUse() && (L == Linkage::Internal || L == Linkage::None)) |
| 1483 |
if (auto *VD = dyn_cast<VarDecl>(Referenced); |
| 1484 |
VD && VD->getInit() && !VD->getInit()->isValueDependent() && |
| 1485 |
VD->getInit()->isConstantInitializer(Context, /*IsForRef=*/false)) |
| 1486 |
return true; |
| 1487 |
|
| 1488 |
Callback(DRE, Referenced); |
| 1489 |
return true; |
| 1490 |
} |
| 1491 |
|
| 1492 |
ExposureChecker &Checker; |
| 1493 |
CallbackTy Callback; |
| 1494 |
}; |
| 1495 |
|
| 1496 |
bool ExposureChecker::checkExposure(const Stmt *S, bool Diag) { |
| 1497 |
if (!S) |
| 1498 |
return false; |
| 1499 |
|
| 1500 |
bool HasReferencedTULocals = false; |
| 1501 |
ReferenceTULocalChecker Checker( |
| 1502 |
*this, [this, &HasReferencedTULocals, Diag](DeclRefExpr *DRE, |
| 1503 |
ValueDecl *Referenced) { |
| 1504 |
if (Diag) { |
| 1505 |
SemaRef.Diag(DRE->getExprLoc(), diag::warn_exposure) << Referenced; |
| 1506 |
} |
| 1507 |
HasReferencedTULocals = true; |
| 1508 |
}); |
| 1509 |
Checker.TraverseStmt(const_cast<Stmt *>(S)); |
| 1510 |
return HasReferencedTULocals; |
| 1511 |
} |
| 1512 |
|
| 1513 |
void ExposureChecker::checkExposureInContext(const DeclContext *DC) { |
| 1514 |
for (auto *TopD : DC->noload_decls()) { |
| 1515 |
if (auto *Export = dyn_cast<ExportDecl>(TopD)) { |
| 1516 |
checkExposureInContext(Export); |
| 1517 |
continue; |
| 1518 |
} |
| 1519 |
|
| 1520 |
if (auto *LinkageSpec = dyn_cast<LinkageSpecDecl>(TopD)) { |
| 1521 |
checkExposureInContext(LinkageSpec); |
| 1522 |
continue; |
| 1523 |
} |
| 1524 |
|
| 1525 |
auto *TopND = dyn_cast<NamedDecl>(TopD); |
| 1526 |
if (!TopND) |
| 1527 |
continue; |
| 1528 |
|
| 1529 |
if (auto *Namespace = dyn_cast<NamespaceDecl>(TopND)) { |
| 1530 |
checkExposureInContext(Namespace); |
| 1531 |
continue; |
| 1532 |
} |
| 1533 |
|
| 1534 |
// [basic.link]p17: |
| 1535 |
// If a (possibly instantiated) declaration of, or a deduction guide for, |
| 1536 |
// a non-TU-local entity in a module interface unit |
| 1537 |
// (outside the private-module-fragment, if any) or |
| 1538 |
// module partition is an exposure, the program is ill-formed. |
| 1539 |
if (!TopND->isFromASTFile() && isExposureCandidate(TopND) && |
| 1540 |
!isTULocal(TopND)) |
| 1541 |
checkExposure(TopND, /*Diag=*/true); |
| 1542 |
} |
| 1543 |
} |
| 1544 |
|
| 1545 |
} // namespace |
| 1546 |
|
| 1547 |
void Sema::checkExposure(const TranslationUnitDecl *TU) { |
| 1548 |
if (!TU) |
| 1549 |
return; |
| 1550 |
|
| 1551 |
ExposureChecker Checker(*this); |
| 1552 |
|
| 1553 |
Module *M = TU->getOwningModule(); |
| 1554 |
if (M && M->isInterfaceOrPartition()) |
| 1555 |
Checker.checkExposureInContext(TU); |
| 1556 |
|
| 1557 |
// [basic.link]p18: |
| 1558 |
// If a declaration that appears in one translation unit names a TU-local |
| 1559 |
// entity declared in another translation unit that is not a header unit, |
| 1560 |
// the program is ill-formed. |
| 1561 |
for (auto FDAndInstantiationLocPair : PendingCheckReferenceForTULocal) { |
| 1562 |
FunctionDecl *FD = FDAndInstantiationLocPair.first; |
| 1563 |
SourceLocation PointOfInstantiation = FDAndInstantiationLocPair.second; |
| 1564 |
|
| 1565 |
if (!FD->hasBody()) |
| 1566 |
continue; |
| 1567 |
|
| 1568 |
ReferenceTULocalChecker(Checker, [&, this](DeclRefExpr *DRE, |
| 1569 |
ValueDecl *Referenced) { |
| 1570 |
// A "defect" in current implementation. Now an implicit instantiation of |
| 1571 |
// a template, the instantiation is considered to be in the same module |
| 1572 |
// unit as the template instead of the module unit where the instantiation |
| 1573 |
// happens. |
| 1574 |
// |
| 1575 |
// See test/Modules/Exposre-2.cppm for example. |
| 1576 |
if (!Referenced->isFromASTFile()) |
| 1577 |
return; |
| 1578 |
|
| 1579 |
if (!Referenced->isInAnotherModuleUnit()) |
| 1580 |
return; |
| 1581 |
|
| 1582 |
// This is not standard conforming. But given there are too many static |
| 1583 |
// (inline) functions in headers in existing code, it is more user |
| 1584 |
// friendly to ignore them temporarily now. maybe we can have another flag |
| 1585 |
// for this. |
| 1586 |
if (Referenced->getOwningModule()->isExplicitGlobalModule() && |
| 1587 |
isa<FunctionDecl>(Referenced)) |
| 1588 |
return; |
| 1589 |
|
| 1590 |
Diag(PointOfInstantiation, |
| 1591 |
diag::warn_reference_tu_local_entity_in_other_tu) |
| 1592 |
<< FD << Referenced |
| 1593 |
<< Referenced->getOwningModule()->getTopLevelModuleName(); |
| 1594 |
}).TraverseStmt(FD->getBody()); |
| 1595 |
} |
| 1596 |
} |
| 1597 |
|
| 1598 |
void Sema::checkReferenceToTULocalFromOtherTU( |
| 1599 |
FunctionDecl *FD, SourceLocation PointOfInstantiation) { |
| 1600 |
// Checking if a declaration have any reference to TU-local entities in other |
| 1601 |
// TU is expensive. Try to avoid it as much as possible. |
| 1602 |
if (!FD || !HadImportedNamedModules) |
| 1603 |
return; |
| 1604 |
|
| 1605 |
PendingCheckReferenceForTULocal.push_back( |
| 1606 |
std::make_pair(FD, PointOfInstantiation)); |
| 1607 |
} |