clang 22.0.0git
SemaModule.cpp
Go to the documentation of this file.
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
21#include "llvm/ADT/ScopeExit.h"
22#include "llvm/ADT/StringExtras.h"
23
24using namespace clang;
25using namespace sema;
26
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()) {
35 if (ExternCLoc.isInvalid())
36 ExternCLoc = LSD->getBeginLoc();
37 break;
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.
66static 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.
86static bool
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.
131static void
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
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
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
191void 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.
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()) !=
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
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;
278
279 bool IsPartition = !Partition.empty();
280 if (IsPartition)
281 switch (MDK) {
284 break;
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()) {
299 // It's OK to compile a module interface as a normal translation unit.
300 break;
301
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 ");
311 break;
312
314 Diag(ModuleLoc, diag::err_module_decl_in_module_map_module);
315 return nullptr;
316
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())
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) {
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);
417 assert(Mod && "module creation should not fail");
418 break;
419 }
420
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},
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
449 // Create an interface, but note that it is an implementation
450 // unit.
451 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
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.
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.
482
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
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
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) {
531 Diag(PrivateLoc, diag::err_private_module_fragment_not_module);
532 return nullptr;
533
535 Diag(PrivateLoc, diag::err_private_module_fragment_redefined);
536 Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition);
537 return nullptr;
538
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
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.
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
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
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.
641static 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
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
662 "We can only import a partition unit in a named module.");
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)
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() &&
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
742 checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
743 BuildModuleInclude(DirectiveLoc, Mod);
744}
745
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 =
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) {
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
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() ==
780 ThisModule) &&
781 "was expecting a module if building a Clang module");
782 }
783}
784
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
804 cast<Decl>(DC)->setLocalOwningModule(Mod);
805 }
806 }
807}
808
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.
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(
845 }
846 }
847}
848
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.
859 Loc, Mod, Loc);
860 TU->addDecl(ImportD);
861 Consumer.HandleImplicitImportDecl(ImportD);
862
863 // Make the module visible.
865 VisibleModules.setVisible(Mod, Loc);
866}
867
869 SourceLocation LBraceLoc) {
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 ==
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)
937
938 return D;
939}
940
941static bool checkExportedDecl(Sema &, Decl *, SourceLocation);
942
943/// Check that it's valid to export all the declarations in \p 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.
953static 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
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
1018 auto *ED = cast<ExportDecl>(D);
1019 if (RBraceLoc.isValid())
1020 ED->setRBraceLoc(RBraceLoc);
1021
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
1050Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc) {
1051 // We shouldn't create new global module fragment if there is already
1052 // one.
1053 if (!TheGlobalModuleFragment) {
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
1069void 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
1076Module *Sema::PushImplicitGlobalModuleFragment(SourceLocation BeginLoc) {
1077 if (!TheImplicitGlobalModuleFragment) {
1078 ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap();
1079 TheImplicitGlobalModuleFragment =
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
1092void 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
1099bool 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) {
1112 return true;
1113 default:
1114 return false;
1115 }
1116}
1117
1118//===----------------------------------------------------------------------===//
1119// Checking Exposure in modules //
1120//===----------------------------------------------------------------------===//
1121
1122namespace {
1123class ExposureChecker {
1124public:
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
1141private:
1142 llvm::DenseSet<const NamedDecl *> ExposureSet;
1143 llvm::DenseSet<const NamedDecl *> KnownNonExposureSet;
1144 llvm::DenseSet<const NamedDecl *> CheckingDecls;
1145};
1146
1147bool 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
1162bool 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
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,
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()) {
1219 if (isTULocal(TA.getAsType()))
1220 return true;
1221 break;
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
1249bool 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
1283bool 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() &&
1315 return false;
1316
1317 return true;
1318}
1319
1320bool 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
1343bool 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 =
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
1382bool 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
1419bool 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
1450class ReferenceTULocalChecker : public DynamicRecursiveASTVisitor {
1451public:
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
1496bool 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
1513void 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
1547void 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
1598void 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}
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
llvm::MachO::Target Target
Definition MachO.h:51
Defines the clang::Preprocessor interface.
static void makeTransitiveImportsVisible(ASTContext &Ctx, VisibleModuleSet &VisibleModules, Module *Imported, Module *CurrentModule, SourceLocation ImportLoc, bool IsImportingPrimaryModuleInterface=false)
[module.import]p7: Additionally, when a module-import-declaration in a module unit of some module M i...
static bool DiagReservedModuleName(Sema &S, const IdentifierInfo *II, SourceLocation Loc)
Tests whether the given identifier is reserved as a module name and diagnoses if it is.
static const ExportDecl * getEnclosingExportDecl(const Decl *D)
Determine whether D is lexically within an export-declaration.
static bool checkExportedDecl(Sema &, Decl *, SourceLocation)
Check that it's valid to export D.
static std::string stringFromPath(ModuleIdPath Path)
static void checkModuleImportContext(Sema &S, Module *M, SourceLocation ImportLoc, DeclContext *DC, bool FromInclude=false)
static bool checkExportedDeclContext(Sema &S, DeclContext *DC, SourceLocation BlockStart)
Check that it's valid to export all the declarations in DC.
static bool isImportingModuleUnitFromSameModule(ASTContext &Ctx, Module *Imported, Module *CurrentModule, Module *&FoundPrimaryModuleInterface)
Helper function for makeTransitiveImportsVisible to decide whether the.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
TranslationUnitDecl * getTranslationUnitDecl() const
void setCurrentNamedModule(Module *M)
Set the (C++20) module we are building.
bool isInSameModule(const Module *M1, const Module *M2) const
If the two module M1 and M2 are in the same module.
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
base_class_range bases()
Definition DeclCXX.h:608
method_range methods() const
Definition DeclCXX.h:650
bool hasDefinition() const
Definition DeclCXX.h:561
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1449
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2109
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
DeclContext * getLexicalParent()
getLexicalParent - Returns the containing lexical DeclContext.
Definition DeclBase.h:2125
void addDecl(Decl *D)
Add the declaration D into this context.
decl_range noload_decls() const
noload_decls_begin/end - Iterate over the declarations stored in this context that are currently load...
Definition DeclBase.h:2381
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition DeclBase.h:2373
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1270
ValueDecl * getDecl()
Definition Expr.h:1338
NonOdrUseReason isNonOdrUse() const
Is this expression a non-odr-use reference, and if so, why?
Definition Expr.h:1468
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
FriendObjectKind getFriendObjectKind() const
Determines whether this declaration is the object of a friend declaration and, if so,...
Definition DeclBase.h:1226
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:546
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition DeclBase.h:593
void setInvalidDecl(bool Invalid=true)
setInvalidDecl - Indicates the Decl had a semantic error.
Definition DeclBase.cpp:178
bool isInAnotherModuleUnit() const
Whether this declaration comes from another module unit.
Module * getOwningModule() const
Get the module that owns this declaration (for visibility purposes).
Definition DeclBase.h:842
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
Definition DeclBase.h:793
bool isInvalidDecl() const
Definition DeclBase.h:588
SourceLocation getLocation() const
Definition DeclBase.h:439
DeclContext * getDeclContext()
Definition DeclBase.h:448
bool isInAnonymousNamespace() const
Definition DeclBase.cpp:439
SourceLocation getBeginLoc() const LLVM_READONLY
Definition DeclBase.h:431
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
Definition DeclBase.h:918
@ VisibleWhenImported
This declaration has an owning module, and is visible when that module is imported.
Definition DeclBase.h:229
@ Unowned
This declaration is not owned by a module.
Definition DeclBase.h:218
@ ReachableWhenImported
This declaration has an owning module, and is visible to lookups that occurs within that module.
Definition DeclBase.h:234
@ ModulePrivate
This declaration has an owning module, but is only visible to lookups that occur within that module.
Definition DeclBase.h:240
@ Visible
This declaration has an owning module, but is globally visible (typically because its owning module i...
Definition DeclBase.h:225
void setModuleOwnershipKind(ModuleOwnershipKind MOK)
Set whether this declaration is hidden from name lookup.
Definition DeclBase.h:881
Represents a standard C++ module export declaration.
Definition Decl.h:5131
static ExportDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation ExportLoc)
Definition Decl.cpp:6085
void setRBraceLoc(SourceLocation L)
Definition Decl.h:5151
This represents one expression.
Definition Expr.h:112
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition Expr.h:177
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition Expr.cpp:3089
bool isConstantInitializer(ASTContext &Ctx, bool ForRef, const Expr **Culprit=nullptr) const
isConstantInitializer - Returns true if this expression can be emitted to IR as a constant,...
Definition Expr.cpp:3346
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:276
QualType getType() const
Definition Expr.h:144
Represents a member of a struct/union/class.
Definition Decl.h:3160
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition Diagnostic.h:103
Represents a function declaration or definition.
Definition Decl.h:2000
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3279
SourceRange getReturnTypeSourceRange() const
Attempt to compute an informative source range covering the function return type.
Definition Decl.cpp:4024
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition Decl.h:2921
QualType getReturnType() const
Definition Decl.h:2845
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2774
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine what kind of template instantiation this function represents.
Definition Decl.cpp:4417
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3199
ModuleMap & getModuleMap()
Retrieve the module map.
One of these records is kept for each identifier that is lexed.
ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const
Determine whether this is a name reserved for the implementation (C99 7.1.3, C++ [lib....
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
A simple pair of identifier info and location.
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition Decl.h:5052
static ImportDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, Module *Imported, ArrayRef< SourceLocation > IdentifierLocs)
Create a new module import declaration.
Definition Decl.cpp:6041
static ImportDecl * CreateImplicit(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, Module *Imported, SourceLocation EndLoc)
Create a new module import declaration for an implicitly-generated import.
Definition Decl.cpp:6049
@ CMK_None
Not compiling a module interface at all.
@ CMK_HeaderUnit
Compiling a module header unit.
@ CMK_ModuleMap
Compiling a module from a module map.
@ CMK_ModuleInterface
Compiling a C++ modules interface unit.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
std::string CurrentModule
The name of the current module, of which the main source file is a part.
virtual ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective)=0
Attempt to load the given module.
virtual void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility, SourceLocation ImportLoc)=0
Make the given module visible.
Module * createGlobalModuleFragmentForModuleUnit(SourceLocation Loc, Module *Parent=nullptr)
Create a global module fragment for a C++ module unit.
Module * createImplicitGlobalModuleFragmentForModuleUnit(SourceLocation Loc, Module *Parent)
Describes a module or submodule.
Definition Module.h:144
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition Module.h:732
SmallVector< ExportDecl, 2 > Exports
The set of export declarations.
Definition Module.h:471
bool isForBuilding(const LangOptions &LangOpts) const
Determine whether this module can be built in this compilation.
Definition Module.cpp:155
bool isInterfaceOrPartition() const
Definition Module.h:671
bool isModulePartitionImplementation() const
Is this a module partition implementation unit.
Definition Module.h:659
@ AllVisible
All of the names in this module are visible.
Definition Module.h:447
Module(ModuleConstructorTag, StringRef Name, SourceLocation DefinitionLoc, Module *Parent, bool IsFramework, bool IsExplicit, unsigned VisibilityID)
Construct a new module or submodule.
Definition Module.cpp:36
Module * Parent
The parent of this module.
Definition Module.h:193
ModuleKind Kind
The kind of this module.
Definition Module.h:189
llvm::SmallSetVector< Module *, 2 > Imports
The set of modules imported by this module, and on which this module depends.
Definition Module.h:458
std::string Name
The name of this module.
Definition Module.h:147
unsigned IsExternC
Whether this is an 'extern "C"' module (which implicitly puts all headers in it within an 'extern "C"...
Definition Module.h:395
StringRef getPrimaryModuleInterfaceName() const
Get the primary module interface name from a partition.
Definition Module.h:687
bool isModulePartition() const
Is this a module partition.
Definition Module.h:653
bool isExplicitGlobalModule() const
Definition Module.h:242
bool isImplicitGlobalModule() const
Definition Module.h:245
bool isHeaderUnit() const
Is this module a header unit.
Definition Module.h:669
@ ModuleImplementationUnit
This is a C++20 module implementation unit.
Definition Module.h:167
@ ModuleMapModule
This is a module that was defined by a module map and built out of header files.
Definition Module.h:158
@ ImplicitGlobalModuleFragment
This is an implicit fragment of the global module which contains only language linkage declarations (...
Definition Module.h:185
@ ModulePartitionInterface
This is a C++20 module partition interface.
Definition Module.h:170
@ ModuleInterfaceUnit
This is a C++20 module interface unit.
Definition Module.h:164
@ ModuleHeaderUnit
This is a C++20 header unit.
Definition Module.h:161
@ ModulePartitionImplementation
This is a C++20 module partition implementation.
Definition Module.h:173
@ PrivateModuleFragment
This is the private module fragment within some C++ module.
Definition Module.h:180
@ ExplicitGlobalModuleFragment
This is the explicit Global Module Fragment of a modular TU.
Definition Module.h:177
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
Definition Module.cpp:239
bool isNamedModule() const
Does this Module is a named module of a standard named module?
Definition Module.h:224
This represents a decl that may have a name.
Definition Decl.h:274
Linkage getLinkageInternal() const
Determine what kind of linkage this entity has.
Definition Decl.cpp:1182
Represents a parameter to a function.
Definition Decl.h:1790
HeaderSearch & getHeaderSearchInfo() const
A (possibly-)qualified type.
Definition TypeBase.h:937
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition TypeBase.h:8332
bool hasConst() const
Definition TypeBase.h:457
bool hasVolatile() const
Definition TypeBase.h:467
field_range fields() const
Definition Decl.h:4527
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition SemaBase.cpp:61
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:855
void ActOnAnnotModuleBegin(SourceLocation DirectiveLoc, Module *Mod)
The parsed has entered a submodule.
void ActOnAnnotModuleInclude(SourceLocation DirectiveLoc, Module *Mod)
The parser has processed a module import translated from a include or similar preprocessing directive...
const TranslationUnitKind TUKind
The kind of translation unit we are processing.
Definition Sema.h:1241
@ PartitionImplementation
'module X:Y;'
Definition Sema.h:9875
@ Interface
'export module X;'
Definition Sema.h:9872
@ Implementation
'module X;'
Definition Sema.h:9873
@ PartitionInterface
'export module X:Y;'
Definition Sema.h:9874
llvm::DenseMap< NamedDecl *, NamedDecl * > VisibleNamespaceCache
Map from the most recent declaration of a namespace to the most recent visible declaration of that na...
Definition Sema.h:13592
ASTContext & Context
Definition Sema.h:1283
void ActOnAnnotModuleEnd(SourceLocation DirectiveLoc, Module *Mod)
The parser has left a submodule.
bool currentModuleIsImplementation() const
Is the module scope we are an implementation unit?
Definition Sema.h:9859
DeclResult ActOnModuleImport(SourceLocation StartLoc, SourceLocation ExportLoc, SourceLocation ImportLoc, ModuleIdPath Path, bool IsPartition=false)
The parser has processed a module import declaration.
SemaObjC & ObjC()
Definition Sema.h:1488
DeclGroupPtrTy ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType=nullptr)
Definition SemaDecl.cpp:79
ASTContext & getASTContext() const
Definition Sema.h:926
Decl * ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc, SourceLocation LBraceLoc)
We have parsed the start of an export declaration, including the '{' (if present).
const LangOptions & getLangOpts() const
Definition Sema.h:919
Preprocessor & PP
Definition Sema.h:1282
SemaHLSL & HLSL()
Definition Sema.h:1453
void ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind)
Definition Sema.cpp:1171
DeclGroupPtrTy ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc)
The parser has processed a global-module-fragment declaration that begins the definition of the globa...
DeclGroupPtrTy ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc, ModuleDeclKind MDK, ModuleIdPath Path, ModuleIdPath Partition, ModuleImportState &ImportState, bool SeenNoTrivialPPDirective)
The parser has processed a module-declaration that begins the definition of a module interface or imp...
Module * getCurrentModule() const
Get the module unit whose scope we are currently within.
Definition Sema.h:9854
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
Definition Sema.h:1416
DeclGroupPtrTy ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc, SourceLocation PrivateLoc)
The parser has processed a private-module-fragment declaration that begins the definition of the priv...
SourceManager & getSourceManager() const
Definition Sema.h:924
void BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod)
bool isModuleVisible(const Module *M, bool ModulePrivate=false)
bool isSFINAEContext() const
Definition Sema.h:13671
ASTConsumer & Consumer
Definition Sema.h:1284
ModuleImportState
An enumeration to represent the transition of states in parsing module fragments and imports.
Definition Sema.h:9881
@ FirstDecl
Parsing the first decl in a TU.
Definition Sema.h:9882
@ GlobalFragment
after 'module;' but before 'module X;'
Definition Sema.h:9883
@ NotACXX20Module
Not a C++20 TU, or an invalid state was found.
Definition Sema.h:9890
@ ImportAllowed
after 'module X;' but before any non-import decl.
Definition Sema.h:9884
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with the preprocessor.
Definition Sema.cpp:109
void PushDeclContext(Scope *S, DeclContext *DC)
Set the current declaration context until it gets popped.
void PopDeclContext()
Decl * ActOnFinishExportDecl(Scope *S, Decl *ExportDecl, SourceLocation RBraceLoc)
Complete the definition of an export declaration.
OpaquePtr< DeclGroupRef > DeclGroupPtrTy
Definition Sema.h:1274
ASTMutationListener * getASTMutationListener() const
Definition Sema.cpp:651
void createImplicitModuleImportForErrorRecovery(SourceLocation Loc, Module *Mod)
Create an implicit import of the given module at the given source location, for error recovery,...
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
SourceLocation getIncludeLoc(FileID FID) const
Returns the include location if FID is a #include'd file otherwise it returns an invalid location.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
bool isWrittenInMainFile(SourceLocation Loc) const
Returns true if the spelling location for the given location is in the main file buffer.
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
Stmt - This represents one statement.
Definition Stmt.h:85
@ Declaration
The template argument is a declaration that was provided for a pointer, reference,...
@ Type
The template argument is a type.
The top declaration context.
Definition Decl.h:105
Linkage getLinkage() const
Determine the linkage of this type.
Definition Type.cpp:4892
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:926
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition Decl.h:1569
bool isInline() const
Whether this variable is (C++1z) inline.
Definition Decl.h:1551
const Expr * getInit() const
Definition Decl.h:1368
A set of visible modules.
Definition Module.h:866
void setVisible(Module *M, SourceLocation Loc, bool IncludeExports=true, VisibleCallback Vis=[](Module *) {}, ConflictCallback Cb=[](ArrayRef< Module * >, Module *, StringRef) {})
Make a specific module visible.
Definition Module.cpp:662
#define bool
Definition gpuintrin.h:32
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
ArrayRef< IdentifierLoc > ModuleIdPath
A sequence of identifier/location pairs used to describe a particular module or submodule,...
ActionResult< Decl * > DeclResult
Definition Ownership.h:255
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition Linkage.h:24
@ None
No linkage, which means that the entity is unique and can only be referred to from within its scope.
Definition Linkage.h:30
@ Internal
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition Linkage.h:35
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
@ Global
The global module fragment, between 'module;' and a module-declaration.
Definition Sema.h:488
@ Normal
A normal translation unit fragment.
Definition Sema.h:492
@ TU_ClangModule
The translation unit is a clang module.
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
@ TSK_ImplicitInstantiation
This template specialization was implicitly instantiated from a template.
Definition Specifiers.h:194
U cast(CodeGen::Address addr)
Definition Address.h:327
int const char * function
Definition c++config.h:31
Information about a header directive as found in the module map file.
Definition Module.h:287