clang 20.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
19#include "llvm/ADT/StringExtras.h"
20#include <optional>
21
22using namespace clang;
23using namespace sema;
24
26 SourceLocation ImportLoc, DeclContext *DC,
27 bool FromInclude = false) {
28 SourceLocation ExternCLoc;
29
30 if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) {
31 switch (LSD->getLanguage()) {
32 case LinkageSpecLanguageIDs::C:
33 if (ExternCLoc.isInvalid())
34 ExternCLoc = LSD->getBeginLoc();
35 break;
36 case LinkageSpecLanguageIDs::CXX:
37 break;
38 }
39 DC = LSD->getParent();
40 }
41
42 while (isa<LinkageSpecDecl>(DC) || isa<ExportDecl>(DC))
43 DC = DC->getParent();
44
45 if (!isa<TranslationUnitDecl>(DC)) {
46 S.Diag(ImportLoc, (FromInclude && S.isModuleVisible(M))
47 ? diag::ext_module_import_not_at_top_level_noop
48 : diag::err_module_import_not_at_top_level_fatal)
49 << M->getFullModuleName() << DC;
50 S.Diag(cast<Decl>(DC)->getBeginLoc(),
51 diag::note_module_import_not_at_top_level)
52 << DC;
53 } else if (!M->IsExternC && ExternCLoc.isValid()) {
54 S.Diag(ImportLoc, diag::ext_module_import_in_extern_c)
55 << M->getFullModuleName();
56 S.Diag(ExternCLoc, diag::note_extern_c_begins_here);
57 }
58}
59
60// We represent the primary and partition names as 'Paths' which are sections
61// of the hierarchical access path for a clang module. However for C++20
62// the periods in a name are just another character, and we will need to
63// flatten them into a string.
64static std::string stringFromPath(ModuleIdPath Path) {
65 std::string Name;
66 if (Path.empty())
67 return Name;
68
69 for (auto &Piece : Path) {
70 if (!Name.empty())
71 Name += ".";
72 Name += Piece.first->getName();
73 }
74 return Name;
75}
76
77/// Helper function for makeTransitiveImportsVisible to decide whether
78/// the \param Imported module unit is in the same module with the \param
79/// CurrentModule.
80/// \param FoundPrimaryModuleInterface is a helper parameter to record the
81/// primary module interface unit corresponding to the module \param
82/// CurrentModule. Since currently it is expensive to decide whether two module
83/// units come from the same module by comparing the module name.
84static bool
86 Module *CurrentModule,
87 Module *&FoundPrimaryModuleInterface) {
88 if (!Imported->isNamedModule())
89 return false;
90
91 // The a partition unit we're importing must be in the same module of the
92 // current module.
93 if (Imported->isModulePartition())
94 return true;
95
96 // If we found the primary module interface during the search process, we can
97 // return quickly to avoid expensive string comparison.
98 if (FoundPrimaryModuleInterface)
99 return Imported == FoundPrimaryModuleInterface;
100
101 if (!CurrentModule)
102 return false;
103
104 // Then the imported module must be a primary module interface unit. It
105 // is only allowed to import the primary module interface unit from the same
106 // module in the implementation unit and the implementation partition unit.
107
108 // Since we'll handle implementation unit above. We can only care
109 // about the implementation partition unit here.
110 if (!CurrentModule->isModulePartitionImplementation())
111 return false;
112
113 if (Ctx.isInSameModule(Imported, CurrentModule)) {
114 assert(!FoundPrimaryModuleInterface ||
115 FoundPrimaryModuleInterface == Imported);
116 FoundPrimaryModuleInterface = Imported;
117 return true;
118 }
119
120 return false;
121}
122
123/// [module.import]p7:
124/// Additionally, when a module-import-declaration in a module unit of some
125/// module M imports another module unit U of M, it also imports all
126/// translation units imported by non-exported module-import-declarations in
127/// the module unit purview of U. These rules can in turn lead to the
128/// importation of yet more translation units.
129static void
131 Module *Imported, Module *CurrentModule,
132 SourceLocation ImportLoc,
133 bool IsImportingPrimaryModuleInterface = false) {
134 assert(Imported->isNamedModule() &&
135 "'makeTransitiveImportsVisible()' is intended for standard C++ named "
136 "modules only.");
137
139 Worklist.push_back(Imported);
140
141 Module *FoundPrimaryModuleInterface =
142 IsImportingPrimaryModuleInterface ? Imported : nullptr;
143
144 while (!Worklist.empty()) {
145 Module *Importing = Worklist.pop_back_val();
146
147 if (VisibleModules.isVisible(Importing))
148 continue;
149
150 // FIXME: The ImportLoc here is not meaningful. It may be problematic if we
151 // use the sourcelocation loaded from the visible modules.
152 VisibleModules.setVisible(Importing, ImportLoc);
153
154 if (isImportingModuleUnitFromSameModule(Ctx, Importing, CurrentModule,
155 FoundPrimaryModuleInterface))
156 for (Module *TransImported : Importing->Imports)
157 if (!VisibleModules.isVisible(TransImported))
158 Worklist.push_back(TransImported);
159 }
160}
161
164 // We start in the global module;
165 Module *GlobalModule =
166 PushGlobalModuleFragment(ModuleLoc);
167
168 // All declarations created from now on are owned by the global module.
169 auto *TU = Context.getTranslationUnitDecl();
170 // [module.global.frag]p2
171 // A global-module-fragment specifies the contents of the global module
172 // fragment for a module unit. The global module fragment can be used to
173 // provide declarations that are attached to the global module and usable
174 // within the module unit.
175 //
176 // So the declations in the global module shouldn't be visible by default.
178 TU->setLocalOwningModule(GlobalModule);
179
180 // FIXME: Consider creating an explicit representation of this declaration.
181 return nullptr;
182}
183
184void Sema::HandleStartOfHeaderUnit() {
185 assert(getLangOpts().CPlusPlusModules &&
186 "Header units are only valid for C++20 modules");
187 SourceLocation StartOfTU =
189
190 StringRef HUName = getLangOpts().CurrentModule;
191 if (HUName.empty()) {
192 HUName =
194 const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str();
195 }
196
197 // TODO: Make the C++20 header lookup independent.
198 // When the input is pre-processed source, we need a file ref to the original
199 // file for the header map.
201 // For the sake of error recovery (if someone has moved the original header
202 // after creating the pre-processed output) fall back to obtaining the file
203 // ref for the input file, which must be present.
204 if (!F)
206 assert(F && "failed to find the header unit source?");
207 Module::Header H{HUName.str(), HUName.str(), *F};
208 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
209 Module *Mod = Map.createHeaderUnit(StartOfTU, HUName, H);
210 assert(Mod && "module creation should not fail");
211 ModuleScopes.push_back({}); // No GMF
212 ModuleScopes.back().BeginLoc = StartOfTU;
213 ModuleScopes.back().Module = Mod;
214 VisibleModules.setVisible(Mod, StartOfTU);
215
216 // From now on, we have an owning module for all declarations we see.
217 // All of these are implicitly exported.
218 auto *TU = Context.getTranslationUnitDecl();
220 TU->setLocalOwningModule(Mod);
221}
222
223/// Tests whether the given identifier is reserved as a module name and
224/// diagnoses if it is. Returns true if a diagnostic is emitted and false
225/// otherwise.
228 enum {
229 Valid = -1,
230 Invalid = 0,
231 Reserved = 1,
232 } Reason = Valid;
233
234 if (II->isStr("module") || II->isStr("import"))
235 Reason = Invalid;
236 else if (II->isReserved(S.getLangOpts()) !=
238 Reason = Reserved;
239
240 // If the identifier is reserved (not invalid) but is in a system header,
241 // we do not diagnose (because we expect system headers to use reserved
242 // identifiers).
243 if (Reason == Reserved && S.getSourceManager().isInSystemHeader(Loc))
244 Reason = Valid;
245
246 switch (Reason) {
247 case Valid:
248 return false;
249 case Invalid:
250 return S.Diag(Loc, diag::err_invalid_module_name) << II;
251 case Reserved:
252 S.Diag(Loc, diag::warn_reserved_module_name) << II;
253 return false;
254 }
255 llvm_unreachable("fell off a fully covered switch");
256}
257
261 ModuleIdPath Partition, ModuleImportState &ImportState) {
262 assert(getLangOpts().CPlusPlusModules &&
263 "should only have module decl in standard C++ modules");
264
265 bool IsFirstDecl = ImportState == ModuleImportState::FirstDecl;
266 bool SeenGMF = ImportState == ModuleImportState::GlobalFragment;
267 // If any of the steps here fail, we count that as invalidating C++20
268 // module state;
270
271 bool IsPartition = !Partition.empty();
272 if (IsPartition)
273 switch (MDK) {
276 break;
279 break;
280 default:
281 llvm_unreachable("how did we get a partition type set?");
282 }
283
284 // A (non-partition) module implementation unit requires that we are not
285 // compiling a module of any kind. A partition implementation emits an
286 // interface (and the AST for the implementation), which will subsequently
287 // be consumed to emit a binary.
288 // A module interface unit requires that we are not compiling a module map.
289 switch (getLangOpts().getCompilingModule()) {
291 // It's OK to compile a module interface as a normal translation unit.
292 break;
293
296 break;
297
298 // We were asked to compile a module interface unit but this is a module
299 // implementation unit.
300 Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch)
301 << FixItHint::CreateInsertion(ModuleLoc, "export ");
303 break;
304
306 Diag(ModuleLoc, diag::err_module_decl_in_module_map_module);
307 return nullptr;
308
310 Diag(ModuleLoc, diag::err_module_decl_in_header_unit);
311 return nullptr;
312 }
313
314 assert(ModuleScopes.size() <= 1 && "expected to be at global module scope");
315
316 // FIXME: Most of this work should be done by the preprocessor rather than
317 // here, in order to support macro import.
318
319 // Only one module-declaration is permitted per source file.
320 if (isCurrentModulePurview()) {
321 Diag(ModuleLoc, diag::err_module_redeclaration);
322 Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module),
323 diag::note_prev_module_declaration);
324 return nullptr;
325 }
326
327 assert((!getLangOpts().CPlusPlusModules ||
328 SeenGMF == (bool)this->TheGlobalModuleFragment) &&
329 "mismatched global module state");
330
331 // In C++20, the module-declaration must be the first declaration if there
332 // is no global module fragment.
333 if (getLangOpts().CPlusPlusModules && !IsFirstDecl && !SeenGMF) {
334 Diag(ModuleLoc, diag::err_module_decl_not_at_start);
335 SourceLocation BeginLoc =
336 ModuleScopes.empty()
338 : ModuleScopes.back().BeginLoc;
339 if (BeginLoc.isValid()) {
340 Diag(BeginLoc, diag::note_global_module_introducer_missing)
341 << FixItHint::CreateInsertion(BeginLoc, "module;\n");
342 }
343 }
344
345 // C++23 [module.unit]p1: ... The identifiers module and import shall not
346 // appear as identifiers in a module-name or module-partition. All
347 // module-names either beginning with an identifier consisting of std
348 // followed by zero or more digits or containing a reserved identifier
349 // ([lex.name]) are reserved and shall not be specified in a
350 // module-declaration; no diagnostic is required.
351
352 // Test the first part of the path to see if it's std[0-9]+ but allow the
353 // name in a system header.
354 StringRef FirstComponentName = Path[0].first->getName();
355 if (!getSourceManager().isInSystemHeader(Path[0].second) &&
356 (FirstComponentName == "std" ||
357 (FirstComponentName.starts_with("std") &&
358 llvm::all_of(FirstComponentName.drop_front(3), &llvm::isDigit))))
359 Diag(Path[0].second, diag::warn_reserved_module_name) << Path[0].first;
360
361 // Then test all of the components in the path to see if any of them are
362 // using another kind of reserved or invalid identifier.
363 for (auto Part : Path) {
364 if (DiagReservedModuleName(*this, Part.first, Part.second))
365 return nullptr;
366 }
367
368 // Flatten the dots in a module name. Unlike Clang's hierarchical module map
369 // modules, the dots here are just another character that can appear in a
370 // module name.
371 std::string ModuleName = stringFromPath(Path);
372 if (IsPartition) {
373 ModuleName += ":";
374 ModuleName += stringFromPath(Partition);
375 }
376 // If a module name was explicitly specified on the command line, it must be
377 // correct.
378 if (!getLangOpts().CurrentModule.empty() &&
379 getLangOpts().CurrentModule != ModuleName) {
380 Diag(Path.front().second, diag::err_current_module_name_mismatch)
381 << SourceRange(Path.front().second, IsPartition
382 ? Partition.back().second
383 : Path.back().second)
385 return nullptr;
386 }
387 const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
388
389 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
390 Module *Mod; // The module we are creating.
391 Module *Interface = nullptr; // The interface for an implementation.
392 switch (MDK) {
395 // We can't have parsed or imported a definition of this module or parsed a
396 // module map defining it already.
397 if (auto *M = Map.findModule(ModuleName)) {
398 Diag(Path[0].second, diag::err_module_redefinition) << ModuleName;
399 if (M->DefinitionLoc.isValid())
400 Diag(M->DefinitionLoc, diag::note_prev_module_definition);
401 else if (OptionalFileEntryRef FE = M->getASTFile())
402 Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
403 << FE->getName();
404 Mod = M;
405 break;
406 }
407
408 // Create a Module for the module that we're defining.
409 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
412 assert(Mod && "module creation should not fail");
413 break;
414 }
415
417 // C++20 A module-declaration that contains neither an export-
418 // keyword nor a module-partition implicitly imports the primary
419 // module interface unit of the module as if by a module-import-
420 // declaration.
421 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc(
422 PP.getIdentifierInfo(ModuleName), Path[0].second);
423
424 // The module loader will assume we're trying to import the module that
425 // we're building if `LangOpts.CurrentModule` equals to 'ModuleName'.
426 // Change the value for `LangOpts.CurrentModule` temporarily to make the
427 // module loader work properly.
428 const_cast<LangOptions &>(getLangOpts()).CurrentModule = "";
429 Interface = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc},
431 /*IsInclusionDirective=*/false);
432 const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
433
434 if (!Interface) {
435 Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName;
436 // Create an empty module interface unit for error recovery.
437 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
438 } else {
439 Mod = Map.createModuleForImplementationUnit(ModuleLoc, ModuleName);
440 }
441 } break;
442
444 // Create an interface, but note that it is an implementation
445 // unit.
446 Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName);
448 break;
449 }
450
451 if (!this->TheGlobalModuleFragment) {
452 ModuleScopes.push_back({});
453 if (getLangOpts().ModulesLocalVisibility)
454 ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
455 } else {
456 // We're done with the global module fragment now.
458 }
459
460 // Switch from the global module fragment (if any) to the named module.
461 ModuleScopes.back().BeginLoc = StartLoc;
462 ModuleScopes.back().Module = Mod;
463 VisibleModules.setVisible(Mod, ModuleLoc);
464
465 // From now on, we have an owning module for all declarations we see.
466 // In C++20 modules, those declaration would be reachable when imported
467 // unless explicitily exported.
468 // Otherwise, those declarations are module-private unless explicitly
469 // exported.
470 auto *TU = Context.getTranslationUnitDecl();
472 TU->setLocalOwningModule(Mod);
473
474 // We are in the module purview, but before any other (non import)
475 // statements, so imports are allowed.
477
479
480 if (auto *Listener = getASTMutationListener())
481 Listener->EnteringModulePurview();
482
483 // We already potentially made an implicit import (in the case of a module
484 // implementation unit importing its interface). Make this module visible
485 // and return the import decl to be added to the current TU.
486 if (Interface) {
487
489 Mod, ModuleLoc,
490 /*IsImportingPrimaryModuleInterface=*/true);
491
492 // Make the import decl for the interface in the impl module.
493 ImportDecl *Import = ImportDecl::Create(Context, CurContext, ModuleLoc,
494 Interface, Path[0].second);
495 CurContext->addDecl(Import);
496
497 // Sequence initialization of the imported module before that of the current
498 // module, if any.
499 Context.addModuleInitializer(ModuleScopes.back().Module, Import);
500 Mod->Imports.insert(Interface); // As if we imported it.
501 // Also save this as a shortcut to checking for decls in the interface
502 ThePrimaryInterface = Interface;
503 // If we made an implicit import of the module interface, then return the
504 // imported module decl.
505 return ConvertDeclToDeclGroup(Import);
506 }
507
508 return nullptr;
509}
510
513 SourceLocation PrivateLoc) {
514 // C++20 [basic.link]/2:
515 // A private-module-fragment shall appear only in a primary module
516 // interface unit.
517 switch (ModuleScopes.empty() ? Module::ExplicitGlobalModuleFragment
518 : ModuleScopes.back().Module->Kind) {
525 Diag(PrivateLoc, diag::err_private_module_fragment_not_module);
526 return nullptr;
527
529 Diag(PrivateLoc, diag::err_private_module_fragment_redefined);
530 Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition);
531 return nullptr;
532
534 Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface);
535 Diag(ModuleScopes.back().BeginLoc,
536 diag::note_not_module_interface_add_export)
537 << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
538 return nullptr;
539
541 break;
542 }
543
544 // FIXME: Check that this translation unit does not import any partitions;
545 // such imports would violate [basic.link]/2's "shall be the only module unit"
546 // restriction.
547
548 // We've finished the public fragment of the translation unit.
550
551 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
552 Module *PrivateModuleFragment =
553 Map.createPrivateModuleFragmentForInterfaceUnit(
554 ModuleScopes.back().Module, PrivateLoc);
555 assert(PrivateModuleFragment && "module creation should not fail");
556
557 // Enter the scope of the private module fragment.
558 ModuleScopes.push_back({});
559 ModuleScopes.back().BeginLoc = ModuleLoc;
560 ModuleScopes.back().Module = PrivateModuleFragment;
561 VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc);
562
563 // All declarations created from now on are scoped to the private module
564 // fragment (and are neither visible nor reachable in importers of the module
565 // interface).
566 auto *TU = Context.getTranslationUnitDecl();
568 TU->setLocalOwningModule(PrivateModuleFragment);
569
570 // FIXME: Consider creating an explicit representation of this declaration.
571 return nullptr;
572}
573
575 SourceLocation ExportLoc,
577 bool IsPartition) {
578 assert((!IsPartition || getLangOpts().CPlusPlusModules) &&
579 "partition seen in non-C++20 code?");
580
581 // For a C++20 module name, flatten into a single identifier with the source
582 // location of the first component.
583 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
584
585 std::string ModuleName;
586 if (IsPartition) {
587 // We already checked that we are in a module purview in the parser.
588 assert(!ModuleScopes.empty() && "in a module purview, but no module?");
589 Module *NamedMod = ModuleScopes.back().Module;
590 // If we are importing into a partition, find the owning named module,
591 // otherwise, the name of the importing named module.
592 ModuleName = NamedMod->getPrimaryModuleInterfaceName().str();
593 ModuleName += ":";
594 ModuleName += stringFromPath(Path);
595 ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
596 Path = ModuleIdPath(ModuleNameLoc);
597 } else if (getLangOpts().CPlusPlusModules) {
598 ModuleName = stringFromPath(Path);
599 ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
600 Path = ModuleIdPath(ModuleNameLoc);
601 }
602
603 // Diagnose self-import before attempting a load.
604 // [module.import]/9
605 // A module implementation unit of a module M that is not a module partition
606 // shall not contain a module-import-declaration nominating M.
607 // (for an implementation, the module interface is imported implicitly,
608 // but that's handled in the module decl code).
609
610 if (getLangOpts().CPlusPlusModules && isCurrentModulePurview() &&
611 getCurrentModule()->Name == ModuleName) {
612 Diag(ImportLoc, diag::err_module_self_import_cxx20)
613 << ModuleName << currentModuleIsImplementation();
614 return true;
615 }
616
618 ImportLoc, Path, Module::AllVisible, /*IsInclusionDirective=*/false);
619 if (!Mod)
620 return true;
621
622 if (!Mod->isInterfaceOrPartition() && !ModuleName.empty() &&
623 !getLangOpts().ObjC) {
624 Diag(ImportLoc, diag::err_module_import_non_interface_nor_parition)
625 << ModuleName;
626 return true;
627 }
628
629 return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
630}
631
632/// Determine whether \p D is lexically within an export-declaration.
634 for (auto *DC = D->getLexicalDeclContext(); DC; DC = DC->getLexicalParent())
635 if (auto *ED = dyn_cast<ExportDecl>(DC))
636 return ED;
637 return nullptr;
638}
639
641 SourceLocation ExportLoc,
642 SourceLocation ImportLoc, Module *Mod,
644 if (Mod->isHeaderUnit())
645 Diag(ImportLoc, diag::warn_experimental_header_unit);
646
647 if (Mod->isNamedModule())
648 makeTransitiveImportsVisible(getASTContext(), VisibleModules, Mod,
649 getCurrentModule(), ImportLoc);
650 else
651 VisibleModules.setVisible(Mod, ImportLoc);
652
653 checkModuleImportContext(*this, Mod, ImportLoc, CurContext);
654
655 // FIXME: we should support importing a submodule within a different submodule
656 // of the same top-level module. Until we do, make it an error rather than
657 // silently ignoring the import.
658 // FIXME: Should we warn on a redundant import of the current module?
659 if (Mod->isForBuilding(getLangOpts())) {
660 Diag(ImportLoc, getLangOpts().isCompilingModule()
661 ? diag::err_module_self_import
662 : diag::err_module_import_in_implementation)
664 }
665
666 SmallVector<SourceLocation, 2> IdentifierLocs;
667
668 if (Path.empty()) {
669 // If this was a header import, pad out with dummy locations.
670 // FIXME: Pass in and use the location of the header-name token in this
671 // case.
672 for (Module *ModCheck = Mod; ModCheck; ModCheck = ModCheck->Parent)
673 IdentifierLocs.push_back(SourceLocation());
674 } else if (getLangOpts().CPlusPlusModules && !Mod->Parent) {
675 // A single identifier for the whole name.
676 IdentifierLocs.push_back(Path[0].second);
677 } else {
678 Module *ModCheck = Mod;
679 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
680 // If we've run out of module parents, just drop the remaining
681 // identifiers. We need the length to be consistent.
682 if (!ModCheck)
683 break;
684 ModCheck = ModCheck->Parent;
685
686 IdentifierLocs.push_back(Path[I].second);
687 }
688 }
689
690 ImportDecl *Import = ImportDecl::Create(Context, CurContext, StartLoc,
691 Mod, IdentifierLocs);
692 CurContext->addDecl(Import);
693
694 // Sequence initialization of the imported module before that of the current
695 // module, if any.
696 if (!ModuleScopes.empty())
697 Context.addModuleInitializer(ModuleScopes.back().Module, Import);
698
699 // A module (partition) implementation unit shall not be exported.
700 if (getLangOpts().CPlusPlusModules && ExportLoc.isValid() &&
702 Diag(ExportLoc, diag::err_export_partition_impl)
703 << SourceRange(ExportLoc, Path.back().second);
704 } else if (!ModuleScopes.empty() && !currentModuleIsImplementation()) {
705 // Re-export the module if the imported module is exported.
706 // Note that we don't need to add re-exported module to Imports field
707 // since `Exports` implies the module is imported already.
708 if (ExportLoc.isValid() || getEnclosingExportDecl(Import))
709 getCurrentModule()->Exports.emplace_back(Mod, false);
710 else
711 getCurrentModule()->Imports.insert(Mod);
712 } else if (ExportLoc.isValid()) {
713 // [module.interface]p1:
714 // An export-declaration shall inhabit a namespace scope and appear in the
715 // purview of a module interface unit.
716 Diag(ExportLoc, diag::err_export_not_in_module_interface);
717 }
718
719 return Import;
720}
721
723 checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
724 BuildModuleInclude(DirectiveLoc, Mod);
725}
726
728 // Determine whether we're in the #include buffer for a module. The #includes
729 // in that buffer do not qualify as module imports; they're just an
730 // implementation detail of us building the module.
731 //
732 // FIXME: Should we even get ActOnAnnotModuleInclude calls for those?
733 bool IsInModuleIncludes =
736
737 // If we are really importing a module (not just checking layering) due to an
738 // #include in the main file, synthesize an ImportDecl.
739 if (getLangOpts().Modules && !IsInModuleIncludes) {
742 DirectiveLoc, Mod,
743 DirectiveLoc);
744 if (!ModuleScopes.empty())
745 Context.addModuleInitializer(ModuleScopes.back().Module, ImportD);
746 TU->addDecl(ImportD);
748 }
749
751 VisibleModules.setVisible(Mod, DirectiveLoc);
752
753 if (getLangOpts().isCompilingModule()) {
755 getLangOpts().CurrentModule, DirectiveLoc, false, false);
756 (void)ThisModule;
757 assert(ThisModule && "was expecting a module if building one");
758 }
759}
760
762 checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
763
764 ModuleScopes.push_back({});
765 ModuleScopes.back().Module = Mod;
766 if (getLangOpts().ModulesLocalVisibility)
767 ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
768
769 VisibleModules.setVisible(Mod, DirectiveLoc);
770
771 // The enclosing context is now part of this module.
772 // FIXME: Consider creating a child DeclContext to hold the entities
773 // lexically within the module.
774 if (getLangOpts().trackLocalOwningModule()) {
775 for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
776 cast<Decl>(DC)->setModuleOwnershipKind(
777 getLangOpts().ModulesLocalVisibility
780 cast<Decl>(DC)->setLocalOwningModule(Mod);
781 }
782 }
783}
784
786 if (getLangOpts().ModulesLocalVisibility) {
787 VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules);
788 // Leaving a module hides namespace names, so our visible namespace cache
789 // is now out of date.
790 VisibleNamespaceCache.clear();
791 }
792
793 assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod &&
794 "left the wrong module scope");
795 ModuleScopes.pop_back();
796
797 // We got to the end of processing a local module. Create an
798 // ImportDecl as we would for an imported module.
800 SourceLocation DirectiveLoc;
801 if (EomLoc == getSourceManager().getLocForEndOfFile(File)) {
802 // We reached the end of a #included module header. Use the #include loc.
803 assert(File != getSourceManager().getMainFileID() &&
804 "end of submodule in main source file");
805 DirectiveLoc = getSourceManager().getIncludeLoc(File);
806 } else {
807 // We reached an EOM pragma. Use the pragma location.
808 DirectiveLoc = EomLoc;
809 }
810 BuildModuleInclude(DirectiveLoc, Mod);
811
812 // Any further declarations are in whatever module we returned to.
813 if (getLangOpts().trackLocalOwningModule()) {
814 // The parser guarantees that this is the same context that we entered
815 // the module within.
816 for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
817 cast<Decl>(DC)->setLocalOwningModule(getCurrentModule());
818 if (!getCurrentModule())
819 cast<Decl>(DC)->setModuleOwnershipKind(
821 }
822 }
823}
824
826 Module *Mod) {
827 // Bail if we're not allowed to implicitly import a module here.
828 if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery ||
829 VisibleModules.isVisible(Mod))
830 return;
831
832 // Create the implicit import declaration.
835 Loc, Mod, Loc);
836 TU->addDecl(ImportD);
838
839 // Make the module visible.
841 VisibleModules.setVisible(Mod, Loc);
842}
843
845 SourceLocation LBraceLoc) {
847
848 // Set this temporarily so we know the export-declaration was braced.
849 D->setRBraceLoc(LBraceLoc);
850
852 PushDeclContext(S, D);
853
854 // C++2a [module.interface]p1:
855 // An export-declaration shall appear only [...] in the purview of a module
856 // interface unit. An export-declaration shall not appear directly or
857 // indirectly within [...] a private-module-fragment.
858 if (!getLangOpts().HLSL) {
859 if (!isCurrentModulePurview()) {
860 Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
861 D->setInvalidDecl();
862 return D;
863 } else if (currentModuleIsImplementation()) {
864 Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
865 Diag(ModuleScopes.back().BeginLoc,
866 diag::note_not_module_interface_add_export)
867 << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
868 D->setInvalidDecl();
869 return D;
870 } else if (ModuleScopes.back().Module->Kind ==
872 Diag(ExportLoc, diag::err_export_in_private_module_fragment);
873 Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
874 D->setInvalidDecl();
875 return D;
876 }
877 }
878
879 for (const DeclContext *DC = CurContext; DC; DC = DC->getLexicalParent()) {
880 if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
881 // An export-declaration shall not appear directly or indirectly within
882 // an unnamed namespace [...]
883 if (ND->isAnonymousNamespace()) {
884 Diag(ExportLoc, diag::err_export_within_anonymous_namespace);
885 Diag(ND->getLocation(), diag::note_anonymous_namespace);
886 // Don't diagnose internal-linkage declarations in this region.
887 D->setInvalidDecl();
888 return D;
889 }
890
891 // A declaration is exported if it is [...] a namespace-definition
892 // that contains an exported declaration.
893 //
894 // Defer exporting the namespace until after we leave it, in order to
895 // avoid marking all subsequent declarations in the namespace as exported.
896 if (!getLangOpts().HLSL && !DeferredExportedNamespaces.insert(ND).second)
897 break;
898 }
899 }
900
901 // [...] its declaration or declaration-seq shall not contain an
902 // export-declaration.
903 if (auto *ED = getEnclosingExportDecl(D)) {
904 Diag(ExportLoc, diag::err_export_within_export);
905 if (ED->hasBraces())
906 Diag(ED->getLocation(), diag::note_export);
907 D->setInvalidDecl();
908 return D;
909 }
910
911 if (!getLangOpts().HLSL)
913
914 return D;
915}
916
917static bool checkExportedDecl(Sema &, Decl *, SourceLocation);
918
919/// Check that it's valid to export all the declarations in \p DC.
921 SourceLocation BlockStart) {
922 bool AllUnnamed = true;
923 for (auto *D : DC->decls())
924 AllUnnamed &= checkExportedDecl(S, D, BlockStart);
925 return AllUnnamed;
926}
927
928/// Check that it's valid to export \p D.
929static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
930
931 // HLSL: export declaration is valid only on functions
932 if (S.getLangOpts().HLSL) {
933 // Export-within-export was already diagnosed in ActOnStartExportDecl
934 if (!dyn_cast<FunctionDecl>(D) && !dyn_cast<ExportDecl>(D)) {
935 S.Diag(D->getBeginLoc(), diag::err_hlsl_export_not_on_function);
936 D->setInvalidDecl();
937 return false;
938 }
939 }
940
941 // C++20 [module.interface]p3:
942 // [...] it shall not declare a name with internal linkage.
943 bool HasName = false;
944 if (auto *ND = dyn_cast<NamedDecl>(D)) {
945 // Don't diagnose anonymous union objects; we'll diagnose their members
946 // instead.
947 HasName = (bool)ND->getDeclName();
948 if (HasName && ND->getFormalLinkage() == Linkage::Internal) {
949 S.Diag(ND->getLocation(), diag::err_export_internal) << ND;
950 if (BlockStart.isValid())
951 S.Diag(BlockStart, diag::note_export);
952 return false;
953 }
954 }
955
956 // C++2a [module.interface]p5:
957 // all entities to which all of the using-declarators ultimately refer
958 // shall have been introduced with a name having external linkage
959 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
962 if (Lk == Linkage::Internal || Lk == Linkage::Module) {
963 S.Diag(USD->getLocation(), diag::err_export_using_internal)
964 << (Lk == Linkage::Internal ? 0 : 1) << Target;
965 S.Diag(Target->getLocation(), diag::note_using_decl_target);
966 if (BlockStart.isValid())
967 S.Diag(BlockStart, diag::note_export);
968 return false;
969 }
970 }
971
972 // Recurse into namespace-scope DeclContexts. (Only namespace-scope
973 // declarations are exported).
974 if (auto *DC = dyn_cast<DeclContext>(D)) {
975 if (!isa<NamespaceDecl>(D))
976 return true;
977
978 if (auto *ND = dyn_cast<NamedDecl>(D)) {
979 if (!ND->getDeclName()) {
980 S.Diag(ND->getLocation(), diag::err_export_anon_ns_internal);
981 if (BlockStart.isValid())
982 S.Diag(BlockStart, diag::note_export);
983 return false;
984 } else if (!DC->decls().empty() &&
985 DC->getRedeclContext()->isFileContext()) {
986 return checkExportedDeclContext(S, DC, BlockStart);
987 }
988 }
989 }
990 return true;
991}
992
994 auto *ED = cast<ExportDecl>(D);
995 if (RBraceLoc.isValid())
996 ED->setRBraceLoc(RBraceLoc);
997
999
1000 if (!D->isInvalidDecl()) {
1001 SourceLocation BlockStart =
1002 ED->hasBraces() ? ED->getBeginLoc() : SourceLocation();
1003 for (auto *Child : ED->decls()) {
1004 checkExportedDecl(*this, Child, BlockStart);
1005 if (auto *FD = dyn_cast<FunctionDecl>(Child)) {
1006 // [dcl.inline]/7
1007 // If an inline function or variable that is attached to a named module
1008 // is declared in a definition domain, it shall be defined in that
1009 // domain.
1010 // So, if the current declaration does not have a definition, we must
1011 // check at the end of the TU (or when the PMF starts) to see that we
1012 // have a definition at that point.
1013 if (FD->isInlineSpecified() && !FD->isDefined())
1014 PendingInlineFuncDecls.insert(FD);
1015 }
1016 }
1017 }
1018
1019 // Anything exported from a module should never be considered unused.
1020 for (auto *Exported : ED->decls())
1021 Exported->markUsed(getASTContext());
1022
1023 return D;
1024}
1025
1026Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc) {
1027 // We shouldn't create new global module fragment if there is already
1028 // one.
1029 if (!TheGlobalModuleFragment) {
1031 TheGlobalModuleFragment = Map.createGlobalModuleFragmentForModuleUnit(
1032 BeginLoc, getCurrentModule());
1033 }
1034
1035 assert(TheGlobalModuleFragment && "module creation should not fail");
1036
1037 // Enter the scope of the global module.
1038 ModuleScopes.push_back({BeginLoc, TheGlobalModuleFragment,
1039 /*OuterVisibleModules=*/{}});
1040 VisibleModules.setVisible(TheGlobalModuleFragment, BeginLoc);
1041
1042 return TheGlobalModuleFragment;
1043}
1044
1045void Sema::PopGlobalModuleFragment() {
1046 assert(!ModuleScopes.empty() &&
1047 getCurrentModule()->isExplicitGlobalModule() &&
1048 "left the wrong module scope, which is not global module fragment");
1049 ModuleScopes.pop_back();
1050}
1051
1052Module *Sema::PushImplicitGlobalModuleFragment(SourceLocation BeginLoc) {
1053 if (!TheImplicitGlobalModuleFragment) {
1055 TheImplicitGlobalModuleFragment =
1058 }
1059 assert(TheImplicitGlobalModuleFragment && "module creation should not fail");
1060
1061 // Enter the scope of the global module.
1062 ModuleScopes.push_back({BeginLoc, TheImplicitGlobalModuleFragment,
1063 /*OuterVisibleModules=*/{}});
1064 VisibleModules.setVisible(TheImplicitGlobalModuleFragment, BeginLoc);
1065 return TheImplicitGlobalModuleFragment;
1066}
1067
1068void Sema::PopImplicitGlobalModuleFragment() {
1069 assert(!ModuleScopes.empty() &&
1070 getCurrentModule()->isImplicitGlobalModule() &&
1071 "left the wrong module scope, which is not global module fragment");
1072 ModuleScopes.pop_back();
1073}
1074
1075bool Sema::isCurrentModulePurview() const {
1076 if (!getCurrentModule())
1077 return false;
1078
1079 /// Does this Module scope describe part of the purview of a standard named
1080 /// C++ module?
1081 switch (getCurrentModule()->Kind) {
1088 return true;
1089 default:
1090 return false;
1091 }
1092}
const Decl * D
IndirectLocalPath & Path
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...
Definition: SemaModule.cpp:130
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.
Definition: SemaModule.cpp:226
static const ExportDecl * getEnclosingExportDecl(const Decl *D)
Determine whether D is lexically within an export-declaration.
Definition: SemaModule.cpp:633
static bool checkExportedDecl(Sema &, Decl *, SourceLocation)
Check that it's valid to export D.
Definition: SemaModule.cpp:929
static std::string stringFromPath(ModuleIdPath Path)
Definition: SemaModule.cpp:64
static void checkModuleImportContext(Sema &S, Module *M, SourceLocation ImportLoc, DeclContext *DC, bool FromInclude=false)
Definition: SemaModule.cpp:25
static bool checkExportedDeclContext(Sema &S, DeclContext *DC, SourceLocation BlockStart)
Check that it's valid to export all the declarations in DC.
Definition: SemaModule.cpp:920
static bool isImportingModuleUnitFromSameModule(ASTContext &Ctx, Module *Imported, Module *CurrentModule, Module *&FoundPrimaryModuleInterface)
Helper function for makeTransitiveImportsVisible to decide whether the.
Definition: SemaModule.cpp:85
SourceLocation Loc
Definition: SemaObjC.cpp:759
virtual void HandleImplicitImportDecl(ImportDecl *D)
Handle an ImportDecl that was implicitly created due to an inclusion directive.
Definition: ASTConsumer.cpp:28
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1101
void setCurrentNamedModule(Module *M)
Set the (C++20) module we are building.
void addModuleInitializer(Module *M, Decl *Init)
Add a declaration to the list of declarations that are initialized for a module.
bool isInSameModule(const Module *M1, const Module *M2)
If the two module M1 and M2 are in the same module.
The result of parsing/analyzing an expression, statement etc.
Definition: Ownership.h:153
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1436
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2090
DeclContext * getLexicalParent()
getLexicalParent - Returns the containing lexical DeclContext.
Definition: DeclBase.h:2106
void addDecl(Decl *D)
Add the declaration D into this context.
Definition: DeclBase.cpp:1766
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:2350
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
void setInvalidDecl(bool Invalid=true)
setInvalidDecl - Indicates the Decl had a semantic error.
Definition: DeclBase.cpp:154
bool isInvalidDecl() const
Definition: DeclBase.h:595
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: DeclBase.h:438
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
Definition: DeclBase.h:908
@ VisibleWhenImported
This declaration has an owning module, and is visible when that module is imported.
@ Unowned
This declaration is not owned by a module.
@ ReachableWhenImported
This declaration has an owning module, and is visible to lookups that occurs within that module.
@ ModulePrivate
This declaration has an owning module, but is only visible to lookups that occur within that module.
@ Visible
This declaration has an owning module, but is globally visible (typically because its owning module i...
void setModuleOwnershipKind(ModuleOwnershipKind MOK)
Set whether this declaration is hidden from name lookup.
Definition: DeclBase.h:871
Represents a standard C++ module export declaration.
Definition: Decl.h:4862
static ExportDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation ExportLoc)
Definition: Decl.cpp:5750
StringRef getName() const
The name of this FileEntry.
Definition: FileEntry.h:61
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Get a FileEntryRef if it exists, without doing anything on error.
Definition: FileManager.h:240
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:97
Module * lookupModule(StringRef ModuleName, SourceLocation ImportLoc=SourceLocation(), bool AllowSearch=true, bool AllowExtraModuleMapSearch=false)
Lookup a module Search for a module with the given name.
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:833
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.
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition: Decl.h:4783
static ImportDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, Module *Imported, ArrayRef< SourceLocation > IdentifierLocs)
Create a new module import declaration.
Definition: Decl.cpp:5704
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:5712
@ CMK_None
Not compiling a module interface at all.
Definition: LangOptions.h:99
@ CMK_HeaderUnit
Compiling a module header unit.
Definition: LangOptions.h:105
@ CMK_ModuleMap
Compiling a module from a module map.
Definition: LangOptions.h:102
@ CMK_ModuleInterface
Compiling a C++ modules interface unit.
Definition: LangOptions.h:108
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:476
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:531
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.
Definition: ModuleMap.cpp:871
Module * createImplicitGlobalModuleFragmentForModuleUnit(SourceLocation Loc, Module *Parent)
Definition: ModuleMap.cpp:884
Describes a module or submodule.
Definition: Module.h:105
SmallVector< ExportDecl, 2 > Exports
The set of export declarations.
Definition: Module.h:415
bool isForBuilding(const LangOptions &LangOpts) const
Determine whether this module can be built in this compilation.
Definition: Module.cpp:160
bool isInterfaceOrPartition() const
Definition: Module.h:615
bool isModulePartitionImplementation() const
Is this a module partition implementation unit.
Definition: Module.h:603
@ AllVisible
All of the names in this module are visible.
Definition: Module.h:391
Module * Parent
The parent of this module.
Definition: Module.h:154
ModuleKind Kind
The kind of this module.
Definition: Module.h:150
llvm::SmallSetVector< Module *, 2 > Imports
The set of modules imported by this module, and on which this module depends.
Definition: Module.h:402
unsigned IsExternC
Whether this is an 'extern "C"' module (which implicitly puts all headers in it within an 'extern "C"...
Definition: Module.h:339
StringRef getPrimaryModuleInterfaceName() const
Get the primary module interface name from a partition.
Definition: Module.h:631
bool isModulePartition() const
Is this a module partition.
Definition: Module.h:597
bool isHeaderUnit() const
Is this module a header unit.
Definition: Module.h:613
@ ModuleImplementationUnit
This is a C++20 module implementation unit.
Definition: Module.h:128
@ ModuleMapModule
This is a module that was defined by a module map and built out of header files.
Definition: Module.h:119
@ ImplicitGlobalModuleFragment
This is an implicit fragment of the global module which contains only language linkage declarations (...
Definition: Module.h:146
@ ModulePartitionInterface
This is a C++20 module partition interface.
Definition: Module.h:131
@ ModuleInterfaceUnit
This is a C++20 module interface unit.
Definition: Module.h:125
@ ModuleHeaderUnit
This is a C++20 header unit.
Definition: Module.h:122
@ ModulePartitionImplementation
This is a C++20 module partition implementation.
Definition: Module.h:134
@ PrivateModuleFragment
This is the private module fragment within some C++ module.
Definition: Module.h:141
@ ExplicitGlobalModuleFragment
This is the explicit Global Module Fragment of a modular TU.
Definition: Module.h:138
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:244
bool isNamedModule() const
Does this Module is a named module of a standard named module?
Definition: Module.h:185
This represents a decl that may have a name.
Definition: Decl.h:249
NamedDecl * getUnderlyingDecl()
Looks through UsingDecls and ObjCCompatibleAliasDecls for the underlying named decl.
Definition: Decl.h:462
Linkage getFormalLinkage() const
Get the linkage from a semantic point of view.
Definition: Decl.cpp:1200
Wrapper for void* pointer.
Definition: Ownership.h:50
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
HeaderSearch & getHeaderSearchInfo() const
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, bool DeferHint=false)
Emit a diagnostic.
Definition: SemaBase.cpp:60
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:493
void ActOnAnnotModuleBegin(SourceLocation DirectiveLoc, Module *Mod)
The parsed has entered a submodule.
Definition: SemaModule.cpp:761
void ActOnAnnotModuleInclude(SourceLocation DirectiveLoc, Module *Mod)
The parser has processed a module import translated from a #include or similar preprocessing directiv...
Definition: SemaModule.cpp:722
const TranslationUnitKind TUKind
The kind of translation unit we are processing.
Definition: Sema.h:918
ModuleDeclKind
Definition: Sema.h:9630
@ PartitionImplementation
'module X:Y;'
@ Interface
'export module X;'
@ PartitionInterface
'export module X:Y;'
DeclGroupPtrTy ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc, ModuleDeclKind MDK, ModuleIdPath Path, ModuleIdPath Partition, ModuleImportState &ImportState)
The parser has processed a module-declaration that begins the definition of a module interface or imp...
Definition: SemaModule.cpp:259
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:13159
ASTContext & Context
Definition: Sema.h:962
void ActOnAnnotModuleEnd(SourceLocation DirectiveLoc, Module *Mod)
The parser has left a submodule.
Definition: SemaModule.cpp:785
bool currentModuleIsImplementation() const
Is the module scope we are an implementation unit?
Definition: Sema.h:9618
DeclResult ActOnModuleImport(SourceLocation StartLoc, SourceLocation ExportLoc, SourceLocation ImportLoc, ModuleIdPath Path, bool IsPartition=false)
The parser has processed a module import declaration.
Definition: SemaModule.cpp:574
SemaObjC & ObjC()
Definition: Sema.h:1164
DeclGroupPtrTy ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType=nullptr)
Definition: SemaDecl.cpp:72
ASTContext & getASTContext() const
Definition: Sema.h:560
Decl * ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc, SourceLocation LBraceLoc)
We have parsed the start of an export declaration, including the '{' (if present).
Definition: SemaModule.cpp:844
const LangOptions & getLangOpts() const
Definition: Sema.h:553
Preprocessor & PP
Definition: Sema.h:961
SemaHLSL & HLSL()
Definition: Sema.h:1129
void ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind)
Definition: Sema.cpp:1107
DeclGroupPtrTy ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc)
The parser has processed a global-module-fragment declaration that begins the definition of the globa...
Definition: SemaModule.cpp:163
std::optional< sema::TemplateDeductionInfo * > isSFINAEContext() const
Determines whether we are currently in a context where template argument substitution failures are no...
Module * getCurrentModule() const
Get the module unit whose scope we are currently within.
Definition: Sema.h:9613
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
Definition: Sema.h:1097
DeclGroupPtrTy ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc, SourceLocation PrivateLoc)
The parser has processed a private-module-fragment declaration that begins the definition of the priv...
Definition: SemaModule.cpp:512
SourceManager & getSourceManager() const
Definition: Sema.h:558
void BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod)
Definition: SemaModule.cpp:727
bool isModuleVisible(const Module *M, bool ModulePrivate=false)
@ Global
The global module fragment, between 'module;' and a module-declaration.
Definition: Sema.h:646
@ Normal
A normal translation unit fragment.
Definition: Sema.h:650
ASTConsumer & Consumer
Definition: Sema.h:963
ModuleImportState
An enumeration to represent the transition of states in parsing module fragments and imports.
Definition: Sema.h:9640
@ FirstDecl
Parsing the first decl in a TU.
@ GlobalFragment
after 'module;' but before 'module X;'
@ NotACXX20Module
Not a C++20 TU, or an invalid state was found.
@ ImportAllowed
after 'module X;' but before any non-import decl.
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with the preprocessor.
Definition: Sema.cpp:88
void PushDeclContext(Scope *S, DeclContext *DC)
Set the current declaration context until it gets popped.
Definition: SemaDecl.cpp:1307
SourceManager & SourceMgr
Definition: Sema.h:965
void PopDeclContext()
Definition: SemaDecl.cpp:1314
Decl * ActOnFinishExportDecl(Scope *S, Decl *ExportDecl, SourceLocation RBraceLoc)
Complete the definition of an export declaration.
Definition: SemaModule.cpp:993
ASTMutationListener * getASTMutationListener() const
Definition: Sema.cpp:600
void createImplicitModuleImportForErrorRecovery(SourceLocation Loc, Module *Mod)
Create an implicit import of the given module at the given source location, for error recovery,...
Definition: SemaModule.cpp:825
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.
OptionalFileEntryRef getFileEntryRefForID(FileID FID) const
Returns the FileEntryRef for the provided FileID.
FileManager & getFileManager() const
FileID getMainFileID() const
Returns the FileID of the main source file.
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.
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file.
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.
The top declaration context.
Definition: Decl.h:84
A set of visible modules.
Definition: Module.h:811
SourceLocation getImportLoc(const Module *M) const
Get the location at which the import of a module was triggered.
Definition: Module.h:840
bool isVisible(const Module *M) const
Determine whether a module is visible.
Definition: Module.h:835
void setVisible(Module *M, SourceLocation Loc, VisibleCallback Vis=[](Module *) {}, ConflictCallback Cb=[](ArrayRef< Module * >, Module *, StringRef) {})
Make a specific module visible.
Definition: Module.cpp:681
The JSON file list parser is used to communicate input to InstallAPI.
ArrayRef< std::pair< IdentifierInfo *, SourceLocation > > ModuleIdPath
A sequence of identifier/location pairs used to describe a particular module or submodule,...
Definition: ModuleLoader.h:32
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:24
@ Internal
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
@ TU_ClangModule
The translation unit is a clang module.
Definition: LangOptions.h:1074
#define bool
Definition: stdbool.h:24
Information about a header directive as found in the module map file.
Definition: Module.h:250