/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaModule.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- SemaModule.cpp - Semantic Analysis for Modules -------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements semantic analysis for modules (C++ modules syntax, |
10 | | // Objective-C modules syntax, and Clang header modules). |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/AST/ASTConsumer.h" |
15 | | #include "clang/Lex/HeaderSearch.h" |
16 | | #include "clang/Lex/Preprocessor.h" |
17 | | #include "clang/Sema/SemaInternal.h" |
18 | | |
19 | | using namespace clang; |
20 | | using namespace sema; |
21 | | |
22 | | static void checkModuleImportContext(Sema &S, Module *M, |
23 | | SourceLocation ImportLoc, DeclContext *DC, |
24 | 144k | bool FromInclude = false) { |
25 | 144k | SourceLocation ExternCLoc; |
26 | | |
27 | 144k | if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) { |
28 | 41.9k | switch (LSD->getLanguage()) { |
29 | 37.0k | case LinkageSpecDecl::lang_c: |
30 | 37.0k | if (ExternCLoc.isInvalid()) |
31 | 37.0k | ExternCLoc = LSD->getBeginLoc(); |
32 | 37.0k | break; |
33 | 4.91k | case LinkageSpecDecl::lang_cxx: |
34 | 4.91k | break; |
35 | 41.9k | } |
36 | 41.9k | DC = LSD->getParent(); |
37 | 41.9k | } |
38 | | |
39 | 149k | while (144k isa<LinkageSpecDecl>(DC) || isa<ExportDecl>(DC)144k ) |
40 | 5.54k | DC = DC->getParent(); |
41 | | |
42 | 144k | if (!isa<TranslationUnitDecl>(DC)) { |
43 | 19 | S.Diag(ImportLoc, (FromInclude && S.isModuleVisible(M)) |
44 | 19 | ? diag::ext_module_import_not_at_top_level_noop10 |
45 | 19 | : diag::err_module_import_not_at_top_level_fatal9 ) |
46 | 19 | << M->getFullModuleName() << DC; |
47 | 19 | S.Diag(cast<Decl>(DC)->getBeginLoc(), |
48 | 19 | diag::note_module_import_not_at_top_level) |
49 | 19 | << DC; |
50 | 144k | } else if (!M->IsExternC && ExternCLoc.isValid()66.5k ) { |
51 | 10 | S.Diag(ImportLoc, diag::ext_module_import_in_extern_c) |
52 | 10 | << M->getFullModuleName(); |
53 | 10 | S.Diag(ExternCLoc, diag::note_extern_c_begins_here); |
54 | 10 | } |
55 | 144k | } |
56 | | |
57 | | // We represent the primary and partition names as 'Paths' which are sections |
58 | | // of the hierarchical access path for a clang module. However for C++20 |
59 | | // the periods in a name are just another character, and we will need to |
60 | | // flatten them into a string. |
61 | 475 | static std::string stringFromPath(ModuleIdPath Path) { |
62 | 475 | std::string Name; |
63 | 475 | if (Path.empty()) |
64 | 0 | return Name; |
65 | | |
66 | 493 | for (auto &Piece : Path)475 { |
67 | 493 | if (!Name.empty()) |
68 | 18 | Name += "."; |
69 | 493 | Name += Piece.first->getName(); |
70 | 493 | } |
71 | 475 | return Name; |
72 | 475 | } |
73 | | |
74 | | Sema::DeclGroupPtrTy |
75 | 185 | Sema::ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc) { |
76 | 185 | if (!ModuleScopes.empty() && |
77 | 185 | ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment1 ) { |
78 | | // Under -std=c++2a -fmodules-ts, we can find an explicit 'module;' after |
79 | | // already implicitly entering the global module fragment. That's OK. |
80 | 1 | assert(getLangOpts().CPlusPlusModules && getLangOpts().ModulesTS && |
81 | 1 | "unexpectedly encountered multiple global module fragment decls"); |
82 | 0 | ModuleScopes.back().BeginLoc = ModuleLoc; |
83 | 1 | return nullptr; |
84 | 1 | } |
85 | | |
86 | | // We start in the global module; all those declarations are implicitly |
87 | | // module-private (though they do not have module linkage). |
88 | 184 | Module *GlobalModule = |
89 | 184 | PushGlobalModuleFragment(ModuleLoc, /*IsImplicit=*/false); |
90 | | |
91 | | // All declarations created from now on are owned by the global module. |
92 | 184 | auto *TU = Context.getTranslationUnitDecl(); |
93 | | // [module.global.frag]p2 |
94 | | // A global-module-fragment specifies the contents of the global module |
95 | | // fragment for a module unit. The global module fragment can be used to |
96 | | // provide declarations that are attached to the global module and usable |
97 | | // within the module unit. |
98 | | // |
99 | | // So the declations in the global module shouldn't be visible by default. |
100 | 184 | TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported); |
101 | 184 | TU->setLocalOwningModule(GlobalModule); |
102 | | |
103 | | // FIXME: Consider creating an explicit representation of this declaration. |
104 | 184 | return nullptr; |
105 | 185 | } |
106 | | |
107 | 20 | void Sema::HandleStartOfHeaderUnit() { |
108 | 20 | assert(getLangOpts().CPlusPlusModules && |
109 | 20 | "Header units are only valid for C++20 modules"); |
110 | 0 | SourceLocation StartOfTU = |
111 | 20 | SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
112 | | |
113 | 20 | StringRef HUName = getLangOpts().CurrentModule; |
114 | 20 | if (HUName.empty()) { |
115 | 3 | HUName = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())->getName(); |
116 | 3 | const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str(); |
117 | 3 | } |
118 | | |
119 | | // TODO: Make the C++20 header lookup independent. |
120 | | // When the input is pre-processed source, we need a file ref to the original |
121 | | // file for the header map. |
122 | 20 | auto F = SourceMgr.getFileManager().getFile(HUName); |
123 | | // For the sake of error recovery (if someone has moved the original header |
124 | | // after creating the pre-processed output) fall back to obtaining the file |
125 | | // ref for the input file, which must be present. |
126 | 20 | if (!F) |
127 | 0 | F = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); |
128 | 20 | assert(F && "failed to find the header unit source?"); |
129 | 0 | Module::Header H{HUName.str(), HUName.str(), *F}; |
130 | 20 | auto &Map = PP.getHeaderSearchInfo().getModuleMap(); |
131 | 20 | Module *Mod = Map.createHeaderUnit(StartOfTU, HUName, H); |
132 | 20 | assert(Mod && "module creation should not fail"); |
133 | 0 | ModuleScopes.push_back({}); // No GMF |
134 | 20 | ModuleScopes.back().BeginLoc = StartOfTU; |
135 | 20 | ModuleScopes.back().Module = Mod; |
136 | 20 | ModuleScopes.back().ModuleInterface = true; |
137 | 20 | ModuleScopes.back().IsPartition = false; |
138 | 20 | VisibleModules.setVisible(Mod, StartOfTU); |
139 | | |
140 | | // From now on, we have an owning module for all declarations we see. |
141 | | // All of these are implicitly exported. |
142 | 20 | auto *TU = Context.getTranslationUnitDecl(); |
143 | 20 | TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::Visible); |
144 | 20 | TU->setLocalOwningModule(Mod); |
145 | 20 | } |
146 | | |
147 | | Sema::DeclGroupPtrTy |
148 | | Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc, |
149 | | ModuleDeclKind MDK, ModuleIdPath Path, |
150 | 279 | ModuleIdPath Partition, ModuleImportState &ImportState) { |
151 | 279 | assert((getLangOpts().ModulesTS || getLangOpts().CPlusPlusModules) && |
152 | 279 | "should only have module decl in Modules TS or C++20"); |
153 | | |
154 | 0 | bool IsFirstDecl = ImportState == ModuleImportState::FirstDecl; |
155 | 279 | bool SeenGMF = ImportState == ModuleImportState::GlobalFragment; |
156 | | // If any of the steps here fail, we count that as invalidating C++20 |
157 | | // module state; |
158 | 279 | ImportState = ModuleImportState::NotACXX20Module; |
159 | | |
160 | 279 | bool IsPartition = !Partition.empty(); |
161 | 279 | if (IsPartition) |
162 | 33 | switch (MDK) { |
163 | 14 | case ModuleDeclKind::Implementation: |
164 | 14 | MDK = ModuleDeclKind::PartitionImplementation; |
165 | 14 | break; |
166 | 19 | case ModuleDeclKind::Interface: |
167 | 19 | MDK = ModuleDeclKind::PartitionInterface; |
168 | 19 | break; |
169 | 0 | default: |
170 | 0 | llvm_unreachable("how did we get a partition type set?"); |
171 | 33 | } |
172 | | |
173 | | // A (non-partition) module implementation unit requires that we are not |
174 | | // compiling a module of any kind. A partition implementation emits an |
175 | | // interface (and the AST for the implementation), which will subsequently |
176 | | // be consumed to emit a binary. |
177 | | // A module interface unit requires that we are not compiling a module map. |
178 | 279 | switch (getLangOpts().getCompilingModule()) { |
179 | 123 | case LangOptions::CMK_None: |
180 | | // It's OK to compile a module interface as a normal translation unit. |
181 | 123 | break; |
182 | | |
183 | 156 | case LangOptions::CMK_ModuleInterface: |
184 | 156 | if (MDK != ModuleDeclKind::Implementation) |
185 | 155 | break; |
186 | | |
187 | | // We were asked to compile a module interface unit but this is a module |
188 | | // implementation unit. |
189 | 1 | Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch) |
190 | 1 | << FixItHint::CreateInsertion(ModuleLoc, "export "); |
191 | 1 | MDK = ModuleDeclKind::Interface; |
192 | 1 | break; |
193 | | |
194 | 0 | case LangOptions::CMK_ModuleMap: |
195 | 0 | Diag(ModuleLoc, diag::err_module_decl_in_module_map_module); |
196 | 0 | return nullptr; |
197 | | |
198 | 0 | case LangOptions::CMK_HeaderModule: |
199 | 0 | case LangOptions::CMK_HeaderUnit: |
200 | 0 | Diag(ModuleLoc, diag::err_module_decl_in_header_module); |
201 | 0 | return nullptr; |
202 | 279 | } |
203 | | |
204 | 279 | assert(ModuleScopes.size() <= 1 && "expected to be at global module scope"); |
205 | | |
206 | | // FIXME: Most of this work should be done by the preprocessor rather than |
207 | | // here, in order to support macro import. |
208 | | |
209 | | // Only one module-declaration is permitted per source file. |
210 | 279 | if (!ModuleScopes.empty() && |
211 | 279 | ModuleScopes.back().Module->isModulePurview()137 ) { |
212 | 4 | Diag(ModuleLoc, diag::err_module_redeclaration); |
213 | 4 | Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module), |
214 | 4 | diag::note_prev_module_declaration); |
215 | 4 | return nullptr; |
216 | 4 | } |
217 | | |
218 | | // Find the global module fragment we're adopting into this module, if any. |
219 | 275 | Module *GlobalModuleFragment = nullptr; |
220 | 275 | if (!ModuleScopes.empty() && |
221 | 275 | ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment133 ) |
222 | 133 | GlobalModuleFragment = ModuleScopes.back().Module; |
223 | | |
224 | 275 | assert((!getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS || |
225 | 275 | SeenGMF == (bool)GlobalModuleFragment) && |
226 | 275 | "mismatched global module state"); |
227 | | |
228 | | // In C++20, the module-declaration must be the first declaration if there |
229 | | // is no global module fragment. |
230 | 275 | if (getLangOpts().CPlusPlusModules && !IsFirstDecl200 && !SeenGMF59 ) { |
231 | 2 | Diag(ModuleLoc, diag::err_module_decl_not_at_start); |
232 | 2 | SourceLocation BeginLoc = |
233 | 2 | ModuleScopes.empty() |
234 | 2 | ? SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()) |
235 | 2 | : ModuleScopes.back().BeginLoc0 ; |
236 | 2 | if (BeginLoc.isValid()) { |
237 | 2 | Diag(BeginLoc, diag::note_global_module_introducer_missing) |
238 | 2 | << FixItHint::CreateInsertion(BeginLoc, "module;\n"); |
239 | 2 | } |
240 | 2 | } |
241 | | |
242 | | // Flatten the dots in a module name. Unlike Clang's hierarchical module map |
243 | | // modules, the dots here are just another character that can appear in a |
244 | | // module name. |
245 | 275 | std::string ModuleName = stringFromPath(Path); |
246 | 275 | if (IsPartition) { |
247 | 33 | ModuleName += ":"; |
248 | 33 | ModuleName += stringFromPath(Partition); |
249 | 33 | } |
250 | | // If a module name was explicitly specified on the command line, it must be |
251 | | // correct. |
252 | 275 | if (!getLangOpts().CurrentModule.empty() && |
253 | 275 | getLangOpts().CurrentModule != ModuleName0 ) { |
254 | 0 | Diag(Path.front().second, diag::err_current_module_name_mismatch) |
255 | 0 | << SourceRange(Path.front().second, IsPartition |
256 | 0 | ? Partition.back().second |
257 | 0 | : Path.back().second) |
258 | 0 | << getLangOpts().CurrentModule; |
259 | 0 | return nullptr; |
260 | 0 | } |
261 | 275 | const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName; |
262 | | |
263 | 275 | auto &Map = PP.getHeaderSearchInfo().getModuleMap(); |
264 | 275 | Module *Mod; |
265 | | |
266 | 275 | switch (MDK) { |
267 | 207 | case ModuleDeclKind::Interface: |
268 | 226 | case ModuleDeclKind::PartitionInterface: { |
269 | | // We can't have parsed or imported a definition of this module or parsed a |
270 | | // module map defining it already. |
271 | 226 | if (auto *M = Map.findModule(ModuleName)) { |
272 | 3 | Diag(Path[0].second, diag::err_module_redefinition) << ModuleName; |
273 | 3 | if (M->DefinitionLoc.isValid()) |
274 | 0 | Diag(M->DefinitionLoc, diag::note_prev_module_definition); |
275 | 3 | else if (Optional<FileEntryRef> FE = M->getASTFile()) |
276 | 3 | Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file) |
277 | 3 | << FE->getName(); |
278 | 3 | Mod = M; |
279 | 3 | break; |
280 | 3 | } |
281 | | |
282 | | // Create a Module for the module that we're defining. |
283 | 223 | Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName, |
284 | 223 | GlobalModuleFragment); |
285 | 223 | if (MDK == ModuleDeclKind::PartitionInterface) |
286 | 19 | Mod->Kind = Module::ModulePartitionInterface; |
287 | 223 | assert(Mod && "module creation should not fail"); |
288 | 0 | break; |
289 | 226 | } |
290 | | |
291 | 35 | case ModuleDeclKind::Implementation: { |
292 | 35 | std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc( |
293 | 35 | PP.getIdentifierInfo(ModuleName), Path[0].second); |
294 | | // C++20 A module-declaration that contains neither an export- |
295 | | // keyword nor a module-partition implicitly imports the primary |
296 | | // module interface unit of the module as if by a module-import- |
297 | | // declaration. |
298 | 35 | Mod = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc}, |
299 | 35 | Module::AllVisible, |
300 | 35 | /*IsInclusionDirective=*/false); |
301 | 35 | if (!Mod) { |
302 | 2 | Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName; |
303 | | // Create an empty module interface unit for error recovery. |
304 | 2 | Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName, |
305 | 2 | GlobalModuleFragment); |
306 | 2 | } |
307 | 35 | } break; |
308 | | |
309 | 14 | case ModuleDeclKind::PartitionImplementation: |
310 | | // Create an interface, but note that it is an implementation |
311 | | // unit. |
312 | 14 | Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName, |
313 | 14 | GlobalModuleFragment); |
314 | 14 | Mod->Kind = Module::ModulePartitionImplementation; |
315 | 14 | break; |
316 | 275 | } |
317 | | |
318 | 275 | if (!GlobalModuleFragment) { |
319 | 142 | ModuleScopes.push_back({}); |
320 | 142 | if (getLangOpts().ModulesLocalVisibility) |
321 | 142 | ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules); |
322 | 142 | } else { |
323 | | // We're done with the global module fragment now. |
324 | 133 | ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global); |
325 | 133 | } |
326 | | |
327 | | // Switch from the global module fragment (if any) to the named module. |
328 | 275 | ModuleScopes.back().BeginLoc = StartLoc; |
329 | 275 | ModuleScopes.back().Module = Mod; |
330 | 275 | ModuleScopes.back().ModuleInterface = MDK != ModuleDeclKind::Implementation; |
331 | 275 | ModuleScopes.back().IsPartition = IsPartition; |
332 | 275 | VisibleModules.setVisible(Mod, ModuleLoc); |
333 | | |
334 | | // From now on, we have an owning module for all declarations we see. |
335 | | // In C++20 modules, those declaration would be reachable when imported |
336 | | // unless explicitily exported. |
337 | | // Otherwise, those declarations are module-private unless explicitly |
338 | | // exported. |
339 | 275 | auto *TU = Context.getTranslationUnitDecl(); |
340 | 275 | TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ReachableWhenImported); |
341 | 275 | TU->setLocalOwningModule(Mod); |
342 | | |
343 | | // We are in the module purview, but before any other (non import) |
344 | | // statements, so imports are allowed. |
345 | 275 | ImportState = ModuleImportState::ImportAllowed; |
346 | | |
347 | | // FIXME: Create a ModuleDecl. |
348 | 275 | return nullptr; |
349 | 275 | } |
350 | | |
351 | | Sema::DeclGroupPtrTy |
352 | | Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc, |
353 | 19 | SourceLocation PrivateLoc) { |
354 | | // C++20 [basic.link]/2: |
355 | | // A private-module-fragment shall appear only in a primary module |
356 | | // interface unit. |
357 | 19 | switch (ModuleScopes.empty() ? Module::GlobalModuleFragment1 |
358 | 19 | : ModuleScopes.back().Module->Kind18 ) { |
359 | 0 | case Module::ModuleMapModule: |
360 | 2 | case Module::GlobalModuleFragment: |
361 | 3 | case Module::ModulePartitionImplementation: |
362 | 4 | case Module::ModulePartitionInterface: |
363 | 4 | case Module::ModuleHeaderUnit: |
364 | 4 | Diag(PrivateLoc, diag::err_private_module_fragment_not_module); |
365 | 4 | return nullptr; |
366 | | |
367 | 3 | case Module::PrivateModuleFragment: |
368 | 3 | Diag(PrivateLoc, diag::err_private_module_fragment_redefined); |
369 | 3 | Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition); |
370 | 3 | return nullptr; |
371 | | |
372 | 12 | case Module::ModuleInterfaceUnit: |
373 | 12 | break; |
374 | 19 | } |
375 | | |
376 | 12 | if (!ModuleScopes.back().ModuleInterface) { |
377 | 1 | Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface); |
378 | 1 | Diag(ModuleScopes.back().BeginLoc, |
379 | 1 | diag::note_not_module_interface_add_export) |
380 | 1 | << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export "); |
381 | 1 | return nullptr; |
382 | 1 | } |
383 | | |
384 | | // FIXME: Check this isn't a module interface partition. |
385 | | // FIXME: Check that this translation unit does not import any partitions; |
386 | | // such imports would violate [basic.link]/2's "shall be the only module unit" |
387 | | // restriction. |
388 | | |
389 | | // We've finished the public fragment of the translation unit. |
390 | 11 | ActOnEndOfTranslationUnitFragment(TUFragmentKind::Normal); |
391 | | |
392 | 11 | auto &Map = PP.getHeaderSearchInfo().getModuleMap(); |
393 | 11 | Module *PrivateModuleFragment = |
394 | 11 | Map.createPrivateModuleFragmentForInterfaceUnit( |
395 | 11 | ModuleScopes.back().Module, PrivateLoc); |
396 | 11 | assert(PrivateModuleFragment && "module creation should not fail"); |
397 | | |
398 | | // Enter the scope of the private module fragment. |
399 | 0 | ModuleScopes.push_back({}); |
400 | 11 | ModuleScopes.back().BeginLoc = ModuleLoc; |
401 | 11 | ModuleScopes.back().Module = PrivateModuleFragment; |
402 | 11 | ModuleScopes.back().ModuleInterface = true; |
403 | 11 | VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc); |
404 | | |
405 | | // All declarations created from now on are scoped to the private module |
406 | | // fragment (and are neither visible nor reachable in importers of the module |
407 | | // interface). |
408 | 11 | auto *TU = Context.getTranslationUnitDecl(); |
409 | 11 | TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate); |
410 | 11 | TU->setLocalOwningModule(PrivateModuleFragment); |
411 | | |
412 | | // FIXME: Consider creating an explicit representation of this declaration. |
413 | 11 | return nullptr; |
414 | 12 | } |
415 | | |
416 | | DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc, |
417 | | SourceLocation ExportLoc, |
418 | | SourceLocation ImportLoc, ModuleIdPath Path, |
419 | 1.83k | bool IsPartition) { |
420 | | |
421 | 1.83k | bool Cxx20Mode = getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS1.73k ; |
422 | 1.83k | assert((!IsPartition || Cxx20Mode) && "partition seen in non-C++20 code?"); |
423 | | |
424 | | // For a C++20 module name, flatten into a single identifier with the source |
425 | | // location of the first component. |
426 | 0 | std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc; |
427 | | |
428 | 1.83k | std::string ModuleName; |
429 | 1.83k | if (IsPartition) { |
430 | | // We already checked that we are in a module purview in the parser. |
431 | 27 | assert(!ModuleScopes.empty() && "in a module purview, but no module?"); |
432 | 0 | Module *NamedMod = ModuleScopes.back().Module; |
433 | | // If we are importing into a partition, find the owning named module, |
434 | | // otherwise, the name of the importing named module. |
435 | 27 | ModuleName = NamedMod->getPrimaryModuleInterfaceName().str(); |
436 | 27 | ModuleName += ":"; |
437 | 27 | ModuleName += stringFromPath(Path); |
438 | 27 | ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second}; |
439 | 27 | Path = ModuleIdPath(ModuleNameLoc); |
440 | 1.81k | } else if (Cxx20Mode) { |
441 | 140 | ModuleName = stringFromPath(Path); |
442 | 140 | ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second}; |
443 | 140 | Path = ModuleIdPath(ModuleNameLoc); |
444 | 140 | } |
445 | | |
446 | | // Diagnose self-import before attempting a load. |
447 | | // [module.import]/9 |
448 | | // A module implementation unit of a module M that is not a module partition |
449 | | // shall not contain a module-import-declaration nominating M. |
450 | | // (for an implementation, the module interface is imported implicitly, |
451 | | // but that's handled in the module decl code). |
452 | | |
453 | 1.83k | if (getLangOpts().CPlusPlusModules && isCurrentModulePurview()99 && |
454 | 1.83k | getCurrentModule()->Name == ModuleName63 ) { |
455 | 4 | Diag(ImportLoc, diag::err_module_self_import_cxx20) |
456 | 4 | << ModuleName << !ModuleScopes.back().ModuleInterface; |
457 | 4 | return true; |
458 | 4 | } |
459 | | |
460 | 1.83k | Module *Mod = getModuleLoader().loadModule( |
461 | 1.83k | ImportLoc, Path, Module::AllVisible, /*IsInclusionDirective=*/false); |
462 | 1.83k | if (!Mod) |
463 | 77 | return true; |
464 | | |
465 | 1.75k | return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path); |
466 | 1.83k | } |
467 | | |
468 | | /// Determine whether \p D is lexically within an export-declaration. |
469 | 309 | static const ExportDecl *getEnclosingExportDecl(const Decl *D) { |
470 | 639 | for (auto *DC = D->getLexicalDeclContext(); DC; DC = DC->getLexicalParent()330 ) |
471 | 337 | if (auto *ED = dyn_cast<ExportDecl>(DC)) |
472 | 7 | return ED; |
473 | 302 | return nullptr; |
474 | 309 | } |
475 | | |
476 | | DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc, |
477 | | SourceLocation ExportLoc, |
478 | | SourceLocation ImportLoc, Module *Mod, |
479 | 1.78k | ModuleIdPath Path) { |
480 | 1.78k | VisibleModules.setVisible(Mod, ImportLoc); |
481 | | |
482 | 1.78k | checkModuleImportContext(*this, Mod, ImportLoc, CurContext); |
483 | | |
484 | | // FIXME: we should support importing a submodule within a different submodule |
485 | | // of the same top-level module. Until we do, make it an error rather than |
486 | | // silently ignoring the import. |
487 | | // FIXME: Should we warn on a redundant import of the current module? |
488 | 1.78k | if (Mod->getTopLevelModuleName() == getLangOpts().CurrentModule && |
489 | 1.78k | (10 getLangOpts().isCompilingModule()10 || !getLangOpts().ModulesTS9 )) { |
490 | 4 | Diag(ImportLoc, getLangOpts().isCompilingModule() |
491 | 4 | ? diag::err_module_self_import1 |
492 | 4 | : diag::err_module_import_in_implementation3 ) |
493 | 4 | << Mod->getFullModuleName() << getLangOpts().CurrentModule; |
494 | 4 | } |
495 | | |
496 | 1.78k | SmallVector<SourceLocation, 2> IdentifierLocs; |
497 | | |
498 | 1.78k | if (Path.empty()) { |
499 | | // If this was a header import, pad out with dummy locations. |
500 | | // FIXME: Pass in and use the location of the header-name token in this |
501 | | // case. |
502 | 58 | for (Module *ModCheck = Mod; ModCheck; ModCheck = ModCheck->Parent30 ) |
503 | 30 | IdentifierLocs.push_back(SourceLocation()); |
504 | 1.75k | } else if (getLangOpts().CPlusPlusModules && !Mod->Parent92 ) { |
505 | | // A single identifier for the whole name. |
506 | 92 | IdentifierLocs.push_back(Path[0].second); |
507 | 1.66k | } else { |
508 | 1.66k | Module *ModCheck = Mod; |
509 | 3.46k | for (unsigned I = 0, N = Path.size(); I != N; ++I1.79k ) { |
510 | | // If we've run out of module parents, just drop the remaining |
511 | | // identifiers. We need the length to be consistent. |
512 | 1.80k | if (!ModCheck) |
513 | 6 | break; |
514 | 1.79k | ModCheck = ModCheck->Parent; |
515 | | |
516 | 1.79k | IdentifierLocs.push_back(Path[I].second); |
517 | 1.79k | } |
518 | 1.66k | } |
519 | | |
520 | 1.78k | ImportDecl *Import = ImportDecl::Create(Context, CurContext, StartLoc, |
521 | 1.78k | Mod, IdentifierLocs); |
522 | 1.78k | CurContext->addDecl(Import); |
523 | | |
524 | | // Sequence initialization of the imported module before that of the current |
525 | | // module, if any. |
526 | 1.78k | if (!ModuleScopes.empty()) |
527 | 307 | Context.addModuleInitializer(ModuleScopes.back().Module, Import); |
528 | | |
529 | | // A module (partition) implementation unit shall not be exported. |
530 | 1.78k | if (getLangOpts().CPlusPlusModules && ExportLoc.isValid()120 && |
531 | 1.78k | Mod->Kind == Module::ModuleKind::ModulePartitionImplementation19 ) { |
532 | 1 | Diag(ExportLoc, diag::err_export_partition_impl) |
533 | 1 | << SourceRange(ExportLoc, Path.back().second); |
534 | 1.78k | } else if (!ModuleScopes.empty() && |
535 | 1.78k | (306 ModuleScopes.back().ModuleInterface306 || |
536 | 306 | (222 getLangOpts().CPlusPlusModules222 && |
537 | 222 | ModuleScopes.back().Module->isGlobalModule()11 ))) { |
538 | 92 | assert((!ModuleScopes.back().Module->isGlobalModule() || |
539 | 92 | Mod->Kind == Module::ModuleKind::ModuleHeaderUnit) && |
540 | 92 | "should only be importing a header unit into the GMF"); |
541 | | // Re-export the module if the imported module is exported. |
542 | | // Note that we don't need to add re-exported module to Imports field |
543 | | // since `Exports` implies the module is imported already. |
544 | 92 | if (ExportLoc.isValid() || getEnclosingExportDecl(Import)76 ) |
545 | 20 | getCurrentModule()->Exports.emplace_back(Mod, false); |
546 | 72 | else |
547 | 72 | getCurrentModule()->Imports.insert(Mod); |
548 | 1.69k | } else if (ExportLoc.isValid()) { |
549 | | // [module.interface]p1: |
550 | | // An export-declaration shall inhabit a namespace scope and appear in the |
551 | | // purview of a module interface unit. |
552 | 2 | Diag(ExportLoc, diag::err_export_not_in_module_interface) |
553 | 2 | << (!ModuleScopes.empty() && |
554 | 2 | !ModuleScopes.back().ImplicitGlobalModuleFragment1 ); |
555 | 1.69k | } else if (getLangOpts().isCompilingModule()) { |
556 | 161 | Module *ThisModule = PP.getHeaderSearchInfo().lookupModule( |
557 | 161 | getLangOpts().CurrentModule, ExportLoc, false, false); |
558 | 161 | (void)ThisModule; |
559 | 161 | assert(ThisModule && "was expecting a module if building one"); |
560 | 161 | } |
561 | | |
562 | | // In some cases we need to know if an entity was present in a directly- |
563 | | // imported module (as opposed to a transitive import). This avoids |
564 | | // searching both Imports and Exports. |
565 | 0 | DirectModuleImports.insert(Mod); |
566 | | |
567 | 1.78k | return Import; |
568 | 1.78k | } |
569 | | |
570 | 34.3k | void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { |
571 | 34.3k | checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true); |
572 | 34.3k | BuildModuleInclude(DirectiveLoc, Mod); |
573 | 34.3k | } |
574 | | |
575 | 142k | void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { |
576 | | // Determine whether we're in the #include buffer for a module. The #includes |
577 | | // in that buffer do not qualify as module imports; they're just an |
578 | | // implementation detail of us building the module. |
579 | | // |
580 | | // FIXME: Should we even get ActOnModuleInclude calls for those? |
581 | 142k | bool IsInModuleIncludes = |
582 | 142k | TUKind == TU_Module && |
583 | 142k | getSourceManager().isWrittenInMainFile(DirectiveLoc)140k ; |
584 | | |
585 | 142k | bool ShouldAddImport = !IsInModuleIncludes; |
586 | | |
587 | | // If this module import was due to an inclusion directive, create an |
588 | | // implicit import declaration to capture it in the AST. |
589 | 142k | if (ShouldAddImport) { |
590 | 138k | TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); |
591 | 138k | ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, |
592 | 138k | DirectiveLoc, Mod, |
593 | 138k | DirectiveLoc); |
594 | 138k | if (!ModuleScopes.empty()) |
595 | 117k | Context.addModuleInitializer(ModuleScopes.back().Module, ImportD); |
596 | 138k | TU->addDecl(ImportD); |
597 | 138k | Consumer.HandleImplicitImportDecl(ImportD); |
598 | 138k | } |
599 | | |
600 | 142k | getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc); |
601 | 142k | VisibleModules.setVisible(Mod, DirectiveLoc); |
602 | | |
603 | 142k | if (getLangOpts().isCompilingModule()) { |
604 | 140k | Module *ThisModule = PP.getHeaderSearchInfo().lookupModule( |
605 | 140k | getLangOpts().CurrentModule, DirectiveLoc, false, false); |
606 | 140k | (void)ThisModule; |
607 | 140k | assert(ThisModule && "was expecting a module if building one"); |
608 | 140k | } |
609 | 142k | } |
610 | | |
611 | 108k | void Sema::ActOnModuleBegin(SourceLocation DirectiveLoc, Module *Mod) { |
612 | 108k | checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true); |
613 | | |
614 | 108k | ModuleScopes.push_back({}); |
615 | 108k | ModuleScopes.back().Module = Mod; |
616 | 108k | if (getLangOpts().ModulesLocalVisibility) |
617 | 1.60k | ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules); |
618 | | |
619 | 108k | VisibleModules.setVisible(Mod, DirectiveLoc); |
620 | | |
621 | | // The enclosing context is now part of this module. |
622 | | // FIXME: Consider creating a child DeclContext to hold the entities |
623 | | // lexically within the module. |
624 | 108k | if (getLangOpts().trackLocalOwningModule()) { |
625 | 251k | for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()143k ) { |
626 | 143k | cast<Decl>(DC)->setModuleOwnershipKind( |
627 | 143k | getLangOpts().ModulesLocalVisibility |
628 | 143k | ? Decl::ModuleOwnershipKind::VisibleWhenImported2.32k |
629 | 143k | : Decl::ModuleOwnershipKind::Visible141k ); |
630 | 143k | cast<Decl>(DC)->setLocalOwningModule(Mod); |
631 | 143k | } |
632 | 107k | } |
633 | 108k | } |
634 | | |
635 | 108k | void Sema::ActOnModuleEnd(SourceLocation EomLoc, Module *Mod) { |
636 | 108k | if (getLangOpts().ModulesLocalVisibility) { |
637 | 1.60k | VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules); |
638 | | // Leaving a module hides namespace names, so our visible namespace cache |
639 | | // is now out of date. |
640 | 1.60k | VisibleNamespaceCache.clear(); |
641 | 1.60k | } |
642 | | |
643 | 108k | assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod && |
644 | 108k | "left the wrong module scope"); |
645 | 0 | ModuleScopes.pop_back(); |
646 | | |
647 | | // We got to the end of processing a local module. Create an |
648 | | // ImportDecl as we would for an imported module. |
649 | 108k | FileID File = getSourceManager().getFileID(EomLoc); |
650 | 108k | SourceLocation DirectiveLoc; |
651 | 108k | if (EomLoc == getSourceManager().getLocForEndOfFile(File)) { |
652 | | // We reached the end of a #included module header. Use the #include loc. |
653 | 483 | assert(File != getSourceManager().getMainFileID() && |
654 | 483 | "end of submodule in main source file"); |
655 | 0 | DirectiveLoc = getSourceManager().getIncludeLoc(File); |
656 | 107k | } else { |
657 | | // We reached an EOM pragma. Use the pragma location. |
658 | 107k | DirectiveLoc = EomLoc; |
659 | 107k | } |
660 | 0 | BuildModuleInclude(DirectiveLoc, Mod); |
661 | | |
662 | | // Any further declarations are in whatever module we returned to. |
663 | 108k | if (getLangOpts().trackLocalOwningModule()) { |
664 | | // The parser guarantees that this is the same context that we entered |
665 | | // the module within. |
666 | 251k | for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()143k ) { |
667 | 143k | cast<Decl>(DC)->setLocalOwningModule(getCurrentModule()); |
668 | 143k | if (!getCurrentModule()) |
669 | 25.3k | cast<Decl>(DC)->setModuleOwnershipKind( |
670 | 25.3k | Decl::ModuleOwnershipKind::Unowned); |
671 | 143k | } |
672 | 107k | } |
673 | 108k | } |
674 | | |
675 | | void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc, |
676 | 709 | Module *Mod) { |
677 | | // Bail if we're not allowed to implicitly import a module here. |
678 | 709 | if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery || |
679 | 709 | VisibleModules.isVisible(Mod)88 ) |
680 | 644 | return; |
681 | | |
682 | | // Create the implicit import declaration. |
683 | 65 | TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); |
684 | 65 | ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, |
685 | 65 | Loc, Mod, Loc); |
686 | 65 | TU->addDecl(ImportD); |
687 | 65 | Consumer.HandleImplicitImportDecl(ImportD); |
688 | | |
689 | | // Make the module visible. |
690 | 65 | getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc); |
691 | 65 | VisibleModules.setVisible(Mod, Loc); |
692 | 65 | } |
693 | | |
694 | | /// We have parsed the start of an export declaration, including the '{' |
695 | | /// (if present). |
696 | | Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc, |
697 | 248 | SourceLocation LBraceLoc) { |
698 | 248 | ExportDecl *D = ExportDecl::Create(Context, CurContext, ExportLoc); |
699 | | |
700 | | // Set this temporarily so we know the export-declaration was braced. |
701 | 248 | D->setRBraceLoc(LBraceLoc); |
702 | | |
703 | 248 | CurContext->addDecl(D); |
704 | 248 | PushDeclContext(S, D); |
705 | | |
706 | | // C++2a [module.interface]p1: |
707 | | // An export-declaration shall appear only [...] in the purview of a module |
708 | | // interface unit. An export-declaration shall not appear directly or |
709 | | // indirectly within [...] a private-module-fragment. |
710 | 248 | if (ModuleScopes.empty() || !ModuleScopes.back().Module->isModulePurview()247 ) { |
711 | 7 | Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0; |
712 | 7 | D->setInvalidDecl(); |
713 | 7 | return D; |
714 | 241 | } else if (!ModuleScopes.back().ModuleInterface) { |
715 | 3 | Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1; |
716 | 3 | Diag(ModuleScopes.back().BeginLoc, |
717 | 3 | diag::note_not_module_interface_add_export) |
718 | 3 | << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export "); |
719 | 3 | D->setInvalidDecl(); |
720 | 3 | return D; |
721 | 238 | } else if (ModuleScopes.back().Module->Kind == |
722 | 238 | Module::PrivateModuleFragment) { |
723 | 1 | Diag(ExportLoc, diag::err_export_in_private_module_fragment); |
724 | 1 | Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment); |
725 | 1 | D->setInvalidDecl(); |
726 | 1 | return D; |
727 | 1 | } |
728 | | |
729 | 725 | for (const DeclContext *DC = CurContext; 237 DC; DC = DC->getLexicalParent()488 ) { |
730 | 499 | if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) { |
731 | | // An export-declaration shall not appear directly or indirectly within |
732 | | // an unnamed namespace [...] |
733 | 32 | if (ND->isAnonymousNamespace()) { |
734 | 4 | Diag(ExportLoc, diag::err_export_within_anonymous_namespace); |
735 | 4 | Diag(ND->getLocation(), diag::note_anonymous_namespace); |
736 | | // Don't diagnose internal-linkage declarations in this region. |
737 | 4 | D->setInvalidDecl(); |
738 | 4 | return D; |
739 | 4 | } |
740 | | |
741 | | // A declaration is exported if it is [...] a namespace-definition |
742 | | // that contains an exported declaration. |
743 | | // |
744 | | // Defer exporting the namespace until after we leave it, in order to |
745 | | // avoid marking all subsequent declarations in the namespace as exported. |
746 | 28 | if (!DeferredExportedNamespaces.insert(ND).second) |
747 | 7 | break; |
748 | 28 | } |
749 | 499 | } |
750 | | |
751 | | // [...] its declaration or declaration-seq shall not contain an |
752 | | // export-declaration. |
753 | 233 | if (auto *ED = getEnclosingExportDecl(D)) { |
754 | 3 | Diag(ExportLoc, diag::err_export_within_export); |
755 | 3 | if (ED->hasBraces()) |
756 | 2 | Diag(ED->getLocation(), diag::note_export); |
757 | 3 | D->setInvalidDecl(); |
758 | 3 | return D; |
759 | 3 | } |
760 | | |
761 | 230 | D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported); |
762 | 230 | return D; |
763 | 233 | } |
764 | | |
765 | | static bool checkExportedDeclContext(Sema &S, DeclContext *DC, |
766 | | SourceLocation BlockStart); |
767 | | |
768 | | namespace { |
769 | | enum class UnnamedDeclKind { |
770 | | Empty, |
771 | | StaticAssert, |
772 | | Asm, |
773 | | UsingDirective, |
774 | | Namespace, |
775 | | Context |
776 | | }; |
777 | | } |
778 | | |
779 | 408 | static llvm::Optional<UnnamedDeclKind> getUnnamedDeclKind(Decl *D) { |
780 | 408 | if (isa<EmptyDecl>(D)) |
781 | 5 | return UnnamedDeclKind::Empty; |
782 | 403 | if (isa<StaticAssertDecl>(D)) |
783 | 4 | return UnnamedDeclKind::StaticAssert; |
784 | 399 | if (isa<FileScopeAsmDecl>(D)) |
785 | 1 | return UnnamedDeclKind::Asm; |
786 | 398 | if (isa<UsingDirectiveDecl>(D)) |
787 | 3 | return UnnamedDeclKind::UsingDirective; |
788 | | // Everything else either introduces one or more names or is ill-formed. |
789 | 395 | return llvm::None; |
790 | 398 | } |
791 | | |
792 | 17 | unsigned getUnnamedDeclDiag(UnnamedDeclKind UDK, bool InBlock) { |
793 | 17 | switch (UDK) { |
794 | 5 | case UnnamedDeclKind::Empty: |
795 | 9 | case UnnamedDeclKind::StaticAssert: |
796 | | // Allow empty-declarations and static_asserts in an export block as an |
797 | | // extension. |
798 | 9 | return InBlock ? diag::ext_export_no_name_block4 : diag::err_export_no_name5 ; |
799 | | |
800 | 3 | case UnnamedDeclKind::UsingDirective: |
801 | | // Allow exporting using-directives as an extension. |
802 | 3 | return diag::ext_export_using_directive; |
803 | | |
804 | 1 | case UnnamedDeclKind::Namespace: |
805 | | // Anonymous namespace with no content. |
806 | 1 | return diag::introduces_no_names; |
807 | | |
808 | 3 | case UnnamedDeclKind::Context: |
809 | | // Allow exporting DeclContexts that transitively contain no declarations |
810 | | // as an extension. |
811 | 3 | return diag::ext_export_no_names; |
812 | | |
813 | 1 | case UnnamedDeclKind::Asm: |
814 | 1 | return diag::err_export_no_name; |
815 | 17 | } |
816 | 0 | llvm_unreachable("unknown kind"); |
817 | 0 | } |
818 | | |
819 | | static void diagExportedUnnamedDecl(Sema &S, UnnamedDeclKind UDK, Decl *D, |
820 | 17 | SourceLocation BlockStart) { |
821 | 17 | S.Diag(D->getLocation(), getUnnamedDeclDiag(UDK, BlockStart.isValid())) |
822 | 17 | << (unsigned)UDK; |
823 | 17 | if (BlockStart.isValid()) |
824 | 6 | S.Diag(BlockStart, diag::note_export); |
825 | 17 | } |
826 | | |
827 | | /// Check that it's valid to export \p D. |
828 | 408 | static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) { |
829 | | // C++2a [module.interface]p3: |
830 | | // An exported declaration shall declare at least one name |
831 | 408 | if (auto UDK = getUnnamedDeclKind(D)) |
832 | 13 | diagExportedUnnamedDecl(S, *UDK, D, BlockStart); |
833 | | |
834 | | // [...] shall not declare a name with internal linkage. |
835 | 408 | bool HasName = false; |
836 | 408 | if (auto *ND = dyn_cast<NamedDecl>(D)) { |
837 | | // Don't diagnose anonymous union objects; we'll diagnose their members |
838 | | // instead. |
839 | 383 | HasName = (bool)ND->getDeclName(); |
840 | 383 | if (HasName && ND->getFormalLinkage() == InternalLinkage362 ) { |
841 | 11 | S.Diag(ND->getLocation(), diag::err_export_internal) << ND; |
842 | 11 | if (BlockStart.isValid()) |
843 | 2 | S.Diag(BlockStart, diag::note_export); |
844 | 11 | } |
845 | 383 | } |
846 | | |
847 | | // C++2a [module.interface]p5: |
848 | | // all entities to which all of the using-declarators ultimately refer |
849 | | // shall have been introduced with a name having external linkage |
850 | 408 | if (auto *USD = dyn_cast<UsingShadowDecl>(D)) { |
851 | 49 | NamedDecl *Target = USD->getUnderlyingDecl(); |
852 | 49 | Linkage Lk = Target->getFormalLinkage(); |
853 | 49 | if (Lk == InternalLinkage || Lk == ModuleLinkage29 ) { |
854 | 32 | S.Diag(USD->getLocation(), diag::err_export_using_internal) |
855 | 32 | << (Lk == InternalLinkage ? 020 : 112 ) << Target; |
856 | 32 | S.Diag(Target->getLocation(), diag::note_using_decl_target); |
857 | 32 | if (BlockStart.isValid()) |
858 | 28 | S.Diag(BlockStart, diag::note_export); |
859 | 32 | } |
860 | 49 | } |
861 | | |
862 | | // Recurse into namespace-scope DeclContexts. (Only namespace-scope |
863 | | // declarations are exported.). |
864 | 408 | if (auto *DC = dyn_cast<DeclContext>(D)) { |
865 | 149 | if (isa<NamespaceDecl>(D) && DC->decls().empty()22 ) { |
866 | 4 | if (!HasName) |
867 | | // We don't allow an empty anonymous namespace (we don't allow decls |
868 | | // in them either, but that's handled in the recursion). |
869 | 1 | diagExportedUnnamedDecl(S, UnnamedDeclKind::Namespace, D, BlockStart); |
870 | | // We allow an empty named namespace decl. |
871 | 145 | } else if (DC->getRedeclContext()->isFileContext() && !isa<EnumDecl>(D)33 ) |
872 | 29 | return checkExportedDeclContext(S, DC, BlockStart); |
873 | 149 | } |
874 | 379 | return false; |
875 | 408 | } |
876 | | |
877 | | /// Check that it's valid to export all the declarations in \p DC. |
878 | | static bool checkExportedDeclContext(Sema &S, DeclContext *DC, |
879 | 29 | SourceLocation BlockStart) { |
880 | 29 | bool AllUnnamed = true; |
881 | 29 | for (auto *D : DC->decls()) |
882 | 62 | AllUnnamed &= checkExportedDecl(S, D, BlockStart); |
883 | 29 | return AllUnnamed; |
884 | 29 | } |
885 | | |
886 | | /// Complete the definition of an export declaration. |
887 | 248 | Decl *Sema::ActOnFinishExportDecl(Scope *S, Decl *D, SourceLocation RBraceLoc) { |
888 | 248 | auto *ED = cast<ExportDecl>(D); |
889 | 248 | if (RBraceLoc.isValid()) |
890 | 23 | ED->setRBraceLoc(RBraceLoc); |
891 | | |
892 | 248 | PopDeclContext(); |
893 | | |
894 | 248 | if (!D->isInvalidDecl()) { |
895 | 230 | SourceLocation BlockStart = |
896 | 230 | ED->hasBraces() ? ED->getBeginLoc()22 : SourceLocation()208 ; |
897 | 346 | for (auto *Child : ED->decls()) { |
898 | 346 | if (checkExportedDecl(*this, Child, BlockStart)) { |
899 | | // If a top-level child is a linkage-spec declaration, it might contain |
900 | | // no declarations (transitively), in which case it's ill-formed. |
901 | 3 | diagExportedUnnamedDecl(*this, UnnamedDeclKind::Context, Child, |
902 | 3 | BlockStart); |
903 | 3 | } |
904 | 346 | } |
905 | 230 | } |
906 | | |
907 | 248 | return D; |
908 | 248 | } |
909 | | |
910 | | Module *Sema::PushGlobalModuleFragment(SourceLocation BeginLoc, |
911 | 205 | bool IsImplicit) { |
912 | | // We shouldn't create new global module fragment if there is already |
913 | | // one. |
914 | 205 | if (!GlobalModuleFragment) { |
915 | 187 | ModuleMap &Map = PP.getHeaderSearchInfo().getModuleMap(); |
916 | 187 | GlobalModuleFragment = Map.createGlobalModuleFragmentForModuleUnit( |
917 | 187 | BeginLoc, getCurrentModule()); |
918 | 187 | } |
919 | | |
920 | 205 | assert(GlobalModuleFragment && "module creation should not fail"); |
921 | | |
922 | | // Enter the scope of the global module. |
923 | 0 | ModuleScopes.push_back({BeginLoc, GlobalModuleFragment, |
924 | 205 | /*ModuleInterface=*/false, |
925 | 205 | /*IsPartition=*/false, |
926 | 205 | /*ImplicitGlobalModuleFragment=*/IsImplicit, |
927 | 205 | /*OuterVisibleModules=*/{}}); |
928 | 205 | VisibleModules.setVisible(GlobalModuleFragment, BeginLoc); |
929 | | |
930 | 205 | return GlobalModuleFragment; |
931 | 205 | } |
932 | | |
933 | 21 | void Sema::PopGlobalModuleFragment() { |
934 | 21 | assert(!ModuleScopes.empty() && getCurrentModule()->isGlobalModule() && |
935 | 21 | "left the wrong module scope, which is not global module fragment"); |
936 | 0 | ModuleScopes.pop_back(); |
937 | 21 | } |
938 | | |
939 | 18 | bool Sema::isModuleUnitOfCurrentTU(const Module *M) const { |
940 | 18 | assert(M); |
941 | | |
942 | 0 | Module *CurrentModuleUnit = getCurrentModule(); |
943 | | |
944 | | // If we are not in a module currently, M must not be the module unit of |
945 | | // current TU. |
946 | 18 | if (!CurrentModuleUnit) |
947 | 18 | return false; |
948 | | |
949 | 0 | return M->isSubModuleOf(CurrentModuleUnit->getTopLevelModule()); |
950 | 18 | } |