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