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                                      bool FromInclude = false) {
25   SourceLocation ExternCLoc;
26 
27   if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) {
28     switch (LSD->getLanguage()) {
29     case LinkageSpecDecl::lang_c:
30       if (ExternCLoc.isInvalid())
31         ExternCLoc = LSD->getBeginLoc();
32       break;
33     case LinkageSpecDecl::lang_cxx:
34       break;
35     }
36     DC = LSD->getParent();
37   }
38 
39   while (isa<LinkageSpecDecl>(DC) || isa<ExportDecl>(DC))
40     DC = DC->getParent();
41 
42   if (!isa<TranslationUnitDecl>(DC)) {
43     S.Diag(ImportLoc, (FromInclude && S.isModuleVisible(M))
44                           ? diag::ext_module_import_not_at_top_level_noop
45                           : diag::err_module_import_not_at_top_level_fatal)
46         << M->getFullModuleName() << DC;
47     S.Diag(cast<Decl>(DC)->getBeginLoc(),
48            diag::note_module_import_not_at_top_level)
49         << DC;
50   } else if (!M->IsExternC && ExternCLoc.isValid()) {
51     S.Diag(ImportLoc, diag::ext_module_import_in_extern_c)
52       << M->getFullModuleName();
53     S.Diag(ExternCLoc, diag::note_extern_c_begins_here);
54   }
55 }
56 
57 Sema::DeclGroupPtrTy
58 Sema::ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc) {
59   if (!ModuleScopes.empty() &&
60       ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment) {
61     // Under -std=c++2a -fmodules-ts, we can find an explicit 'module;' after
62     // already implicitly entering the global module fragment. That's OK.
63     assert(getLangOpts().CPlusPlusModules && getLangOpts().ModulesTS &&
64            "unexpectedly encountered multiple global module fragment decls");
65     ModuleScopes.back().BeginLoc = ModuleLoc;
66     return nullptr;
67   }
68 
69   // We start in the global module; all those declarations are implicitly
70   // module-private (though they do not have module linkage).
71   auto &Map = PP.getHeaderSearchInfo().getModuleMap();
72   auto *GlobalModule = Map.createGlobalModuleFragmentForModuleUnit(ModuleLoc);
73   assert(GlobalModule && "module creation should not fail");
74 
75   // Enter the scope of the global module.
76   ModuleScopes.push_back({});
77   ModuleScopes.back().BeginLoc = ModuleLoc;
78   ModuleScopes.back().Module = GlobalModule;
79   VisibleModules.setVisible(GlobalModule, ModuleLoc);
80 
81   // All declarations created from now on are owned by the global module.
82   auto *TU = Context.getTranslationUnitDecl();
83   TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::Visible);
84   TU->setLocalOwningModule(GlobalModule);
85 
86   // FIXME: Consider creating an explicit representation of this declaration.
87   return nullptr;
88 }
89 
90 Sema::DeclGroupPtrTy
91 Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
92                       ModuleDeclKind MDK, ModuleIdPath Path, bool IsFirstDecl) {
93   assert((getLangOpts().ModulesTS || getLangOpts().CPlusPlusModules) &&
94          "should only have module decl in Modules TS or C++20");
95 
96   // A module implementation unit requires that we are not compiling a module
97   // of any kind. A module interface unit requires that we are not compiling a
98   // module map.
99   switch (getLangOpts().getCompilingModule()) {
100   case LangOptions::CMK_None:
101     // It's OK to compile a module interface as a normal translation unit.
102     break;
103 
104   case LangOptions::CMK_ModuleInterface:
105     if (MDK != ModuleDeclKind::Implementation)
106       break;
107 
108     // We were asked to compile a module interface unit but this is a module
109     // implementation unit. That indicates the 'export' is missing.
110     Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch)
111       << FixItHint::CreateInsertion(ModuleLoc, "export ");
112     MDK = ModuleDeclKind::Interface;
113     break;
114 
115   case LangOptions::CMK_ModuleMap:
116     Diag(ModuleLoc, diag::err_module_decl_in_module_map_module);
117     return nullptr;
118 
119   case LangOptions::CMK_HeaderModule:
120     Diag(ModuleLoc, diag::err_module_decl_in_header_module);
121     return nullptr;
122   }
123 
124   assert(ModuleScopes.size() <= 1 && "expected to be at global module scope");
125 
126   // FIXME: Most of this work should be done by the preprocessor rather than
127   // here, in order to support macro import.
128 
129   // Only one module-declaration is permitted per source file.
130   if (!ModuleScopes.empty() &&
131       ModuleScopes.back().Module->isModulePurview()) {
132     Diag(ModuleLoc, diag::err_module_redeclaration);
133     Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module),
134          diag::note_prev_module_declaration);
135     return nullptr;
136   }
137 
138   // Find the global module fragment we're adopting into this module, if any.
139   Module *GlobalModuleFragment = nullptr;
140   if (!ModuleScopes.empty() &&
141       ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment)
142     GlobalModuleFragment = ModuleScopes.back().Module;
143 
144   // In C++20, the module-declaration must be the first declaration if there
145   // is no global module fragment.
146   if (getLangOpts().CPlusPlusModules && !IsFirstDecl && !GlobalModuleFragment) {
147     Diag(ModuleLoc, diag::err_module_decl_not_at_start);
148     SourceLocation BeginLoc =
149         ModuleScopes.empty()
150             ? SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID())
151             : ModuleScopes.back().BeginLoc;
152     if (BeginLoc.isValid()) {
153       Diag(BeginLoc, diag::note_global_module_introducer_missing)
154           << FixItHint::CreateInsertion(BeginLoc, "module;\n");
155     }
156   }
157 
158   // Flatten the dots in a module name. Unlike Clang's hierarchical module map
159   // modules, the dots here are just another character that can appear in a
160   // module name.
161   std::string ModuleName;
162   for (auto &Piece : Path) {
163     if (!ModuleName.empty())
164       ModuleName += ".";
165     ModuleName += Piece.first->getName();
166   }
167 
168   // If a module name was explicitly specified on the command line, it must be
169   // correct.
170   if (!getLangOpts().CurrentModule.empty() &&
171       getLangOpts().CurrentModule != ModuleName) {
172     Diag(Path.front().second, diag::err_current_module_name_mismatch)
173         << SourceRange(Path.front().second, Path.back().second)
174         << getLangOpts().CurrentModule;
175     return nullptr;
176   }
177   const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName;
178 
179   auto &Map = PP.getHeaderSearchInfo().getModuleMap();
180   Module *Mod;
181 
182   switch (MDK) {
183   case ModuleDeclKind::Interface: {
184     // We can't have parsed or imported a definition of this module or parsed a
185     // module map defining it already.
186     if (auto *M = Map.findModule(ModuleName)) {
187       Diag(Path[0].second, diag::err_module_redefinition) << ModuleName;
188       if (M->DefinitionLoc.isValid())
189         Diag(M->DefinitionLoc, diag::note_prev_module_definition);
190       else if (const auto *FE = M->getASTFile())
191         Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
192             << FE->getName();
193       Mod = M;
194       break;
195     }
196 
197     // Create a Module for the module that we're defining.
198     Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName,
199                                            GlobalModuleFragment);
200     assert(Mod && "module creation should not fail");
201     break;
202   }
203 
204   case ModuleDeclKind::Implementation:
205     std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc(
206         PP.getIdentifierInfo(ModuleName), Path[0].second);
207     Mod = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc},
208                                        Module::AllVisible,
209                                        /*IsIncludeDirective=*/false);
210     if (!Mod) {
211       Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName;
212       // Create an empty module interface unit for error recovery.
213       Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName,
214                                              GlobalModuleFragment);
215     }
216     break;
217   }
218 
219   if (!GlobalModuleFragment) {
220     ModuleScopes.push_back({});
221     if (getLangOpts().ModulesLocalVisibility)
222       ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
223   } else {
224     // We're done with the global module fragment now.
225     ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global);
226   }
227 
228   // Switch from the global module fragment (if any) to the named module.
229   ModuleScopes.back().BeginLoc = StartLoc;
230   ModuleScopes.back().Module = Mod;
231   ModuleScopes.back().ModuleInterface = MDK != ModuleDeclKind::Implementation;
232   VisibleModules.setVisible(Mod, ModuleLoc);
233 
234   // From now on, we have an owning module for all declarations we see.
235   // However, those declarations are module-private unless explicitly
236   // exported.
237   auto *TU = Context.getTranslationUnitDecl();
238   TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
239   TU->setLocalOwningModule(Mod);
240 
241   // FIXME: Create a ModuleDecl.
242   return nullptr;
243 }
244 
245 Sema::DeclGroupPtrTy
246 Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,
247                                      SourceLocation PrivateLoc) {
248   // C++20 [basic.link]/2:
249   //   A private-module-fragment shall appear only in a primary module
250   //   interface unit.
251   switch (ModuleScopes.empty() ? Module::GlobalModuleFragment
252                                : ModuleScopes.back().Module->Kind) {
253   case Module::ModuleMapModule:
254   case Module::GlobalModuleFragment:
255     Diag(PrivateLoc, diag::err_private_module_fragment_not_module);
256     return nullptr;
257 
258   case Module::PrivateModuleFragment:
259     Diag(PrivateLoc, diag::err_private_module_fragment_redefined);
260     Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition);
261     return nullptr;
262 
263   case Module::ModuleInterfaceUnit:
264     break;
265   }
266 
267   if (!ModuleScopes.back().ModuleInterface) {
268     Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface);
269     Diag(ModuleScopes.back().BeginLoc,
270          diag::note_not_module_interface_add_export)
271         << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
272     return nullptr;
273   }
274 
275   // FIXME: Check this isn't a module interface partition.
276   // FIXME: Check that this translation unit does not import any partitions;
277   // such imports would violate [basic.link]/2's "shall be the only module unit"
278   // restriction.
279 
280   // We've finished the public fragment of the translation unit.
281   ActOnEndOfTranslationUnitFragment(TUFragmentKind::Normal);
282 
283   auto &Map = PP.getHeaderSearchInfo().getModuleMap();
284   Module *PrivateModuleFragment =
285       Map.createPrivateModuleFragmentForInterfaceUnit(
286           ModuleScopes.back().Module, PrivateLoc);
287   assert(PrivateModuleFragment && "module creation should not fail");
288 
289   // Enter the scope of the private module fragment.
290   ModuleScopes.push_back({});
291   ModuleScopes.back().BeginLoc = ModuleLoc;
292   ModuleScopes.back().Module = PrivateModuleFragment;
293   ModuleScopes.back().ModuleInterface = true;
294   VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc);
295 
296   // All declarations created from now on are scoped to the private module
297   // fragment (and are neither visible nor reachable in importers of the module
298   // interface).
299   auto *TU = Context.getTranslationUnitDecl();
300   TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
301   TU->setLocalOwningModule(PrivateModuleFragment);
302 
303   // FIXME: Consider creating an explicit representation of this declaration.
304   return nullptr;
305 }
306 
307 DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
308                                    SourceLocation ExportLoc,
309                                    SourceLocation ImportLoc,
310                                    ModuleIdPath Path) {
311   // Flatten the module path for a Modules TS module name.
312   std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
313   if (getLangOpts().ModulesTS) {
314     std::string ModuleName;
315     for (auto &Piece : Path) {
316       if (!ModuleName.empty())
317         ModuleName += ".";
318       ModuleName += Piece.first->getName();
319     }
320     ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
321     Path = ModuleIdPath(ModuleNameLoc);
322   }
323 
324   Module *Mod =
325       getModuleLoader().loadModule(ImportLoc, Path, Module::AllVisible,
326                                    /*IsIncludeDirective=*/false);
327   if (!Mod)
328     return true;
329 
330   return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
331 }
332 
333 DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
334                                    SourceLocation ExportLoc,
335                                    SourceLocation ImportLoc,
336                                    Module *Mod, ModuleIdPath Path) {
337   VisibleModules.setVisible(Mod, ImportLoc);
338 
339   checkModuleImportContext(*this, Mod, ImportLoc, CurContext);
340 
341   // FIXME: we should support importing a submodule within a different submodule
342   // of the same top-level module. Until we do, make it an error rather than
343   // silently ignoring the import.
344   // Import-from-implementation is valid in the Modules TS. FIXME: Should we
345   // warn on a redundant import of the current module?
346   // FIXME: Import of a module from an implementation partition of the same
347   // module is permitted.
348   if (Mod->getTopLevelModuleName() == getLangOpts().CurrentModule &&
349       (getLangOpts().isCompilingModule() || !getLangOpts().ModulesTS)) {
350     Diag(ImportLoc, getLangOpts().isCompilingModule()
351                         ? diag::err_module_self_import
352                         : diag::err_module_import_in_implementation)
353         << Mod->getFullModuleName() << getLangOpts().CurrentModule;
354   }
355 
356   SmallVector<SourceLocation, 2> IdentifierLocs;
357   Module *ModCheck = Mod;
358   for (unsigned I = 0, N = Path.size(); I != N; ++I) {
359     // If we've run out of module parents, just drop the remaining identifiers.
360     // We need the length to be consistent.
361     if (!ModCheck)
362       break;
363     ModCheck = ModCheck->Parent;
364 
365     IdentifierLocs.push_back(Path[I].second);
366   }
367 
368   // If this was a header import, pad out with dummy locations.
369   // FIXME: Pass in and use the location of the header-name token in this case.
370   if (Path.empty()) {
371     for (; ModCheck; ModCheck = ModCheck->Parent) {
372       IdentifierLocs.push_back(SourceLocation());
373     }
374   }
375 
376   ImportDecl *Import = ImportDecl::Create(Context, CurContext, StartLoc,
377                                           Mod, IdentifierLocs);
378   CurContext->addDecl(Import);
379 
380   // Sequence initialization of the imported module before that of the current
381   // module, if any.
382   if (!ModuleScopes.empty())
383     Context.addModuleInitializer(ModuleScopes.back().Module, Import);
384 
385   // Re-export the module if needed.
386   if (!ModuleScopes.empty() && ModuleScopes.back().ModuleInterface) {
387     if (ExportLoc.isValid() || Import->isExported())
388       getCurrentModule()->Exports.emplace_back(Mod, false);
389   } else if (ExportLoc.isValid()) {
390     Diag(ExportLoc, diag::err_export_not_in_module_interface);
391   }
392 
393   return Import;
394 }
395 
396 void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
397   checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
398   BuildModuleInclude(DirectiveLoc, Mod);
399 }
400 
401 void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
402   // Determine whether we're in the #include buffer for a module. The #includes
403   // in that buffer do not qualify as module imports; they're just an
404   // implementation detail of us building the module.
405   //
406   // FIXME: Should we even get ActOnModuleInclude calls for those?
407   bool IsInModuleIncludes =
408       TUKind == TU_Module &&
409       getSourceManager().isWrittenInMainFile(DirectiveLoc);
410 
411   bool ShouldAddImport = !IsInModuleIncludes;
412 
413   // If this module import was due to an inclusion directive, create an
414   // implicit import declaration to capture it in the AST.
415   if (ShouldAddImport) {
416     TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
417     ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
418                                                      DirectiveLoc, Mod,
419                                                      DirectiveLoc);
420     if (!ModuleScopes.empty())
421       Context.addModuleInitializer(ModuleScopes.back().Module, ImportD);
422     TU->addDecl(ImportD);
423     Consumer.HandleImplicitImportDecl(ImportD);
424   }
425 
426   getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc);
427   VisibleModules.setVisible(Mod, DirectiveLoc);
428 }
429 
430 void Sema::ActOnModuleBegin(SourceLocation DirectiveLoc, Module *Mod) {
431   checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true);
432 
433   ModuleScopes.push_back({});
434   ModuleScopes.back().Module = Mod;
435   if (getLangOpts().ModulesLocalVisibility)
436     ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules);
437 
438   VisibleModules.setVisible(Mod, DirectiveLoc);
439 
440   // The enclosing context is now part of this module.
441   // FIXME: Consider creating a child DeclContext to hold the entities
442   // lexically within the module.
443   if (getLangOpts().trackLocalOwningModule()) {
444     for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
445       cast<Decl>(DC)->setModuleOwnershipKind(
446           getLangOpts().ModulesLocalVisibility
447               ? Decl::ModuleOwnershipKind::VisibleWhenImported
448               : Decl::ModuleOwnershipKind::Visible);
449       cast<Decl>(DC)->setLocalOwningModule(Mod);
450     }
451   }
452 }
453 
454 void Sema::ActOnModuleEnd(SourceLocation EomLoc, Module *Mod) {
455   if (getLangOpts().ModulesLocalVisibility) {
456     VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules);
457     // Leaving a module hides namespace names, so our visible namespace cache
458     // is now out of date.
459     VisibleNamespaceCache.clear();
460   }
461 
462   assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod &&
463          "left the wrong module scope");
464   ModuleScopes.pop_back();
465 
466   // We got to the end of processing a local module. Create an
467   // ImportDecl as we would for an imported module.
468   FileID File = getSourceManager().getFileID(EomLoc);
469   SourceLocation DirectiveLoc;
470   if (EomLoc == getSourceManager().getLocForEndOfFile(File)) {
471     // We reached the end of a #included module header. Use the #include loc.
472     assert(File != getSourceManager().getMainFileID() &&
473            "end of submodule in main source file");
474     DirectiveLoc = getSourceManager().getIncludeLoc(File);
475   } else {
476     // We reached an EOM pragma. Use the pragma location.
477     DirectiveLoc = EomLoc;
478   }
479   BuildModuleInclude(DirectiveLoc, Mod);
480 
481   // Any further declarations are in whatever module we returned to.
482   if (getLangOpts().trackLocalOwningModule()) {
483     // The parser guarantees that this is the same context that we entered
484     // the module within.
485     for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) {
486       cast<Decl>(DC)->setLocalOwningModule(getCurrentModule());
487       if (!getCurrentModule())
488         cast<Decl>(DC)->setModuleOwnershipKind(
489             Decl::ModuleOwnershipKind::Unowned);
490     }
491   }
492 }
493 
494 void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc,
495                                                       Module *Mod) {
496   // Bail if we're not allowed to implicitly import a module here.
497   if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery ||
498       VisibleModules.isVisible(Mod))
499     return;
500 
501   // Create the implicit import declaration.
502   TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
503   ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
504                                                    Loc, Mod, Loc);
505   TU->addDecl(ImportD);
506   Consumer.HandleImplicitImportDecl(ImportD);
507 
508   // Make the module visible.
509   getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc);
510   VisibleModules.setVisible(Mod, Loc);
511 }
512 
513 /// We have parsed the start of an export declaration, including the '{'
514 /// (if present).
515 Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
516                                  SourceLocation LBraceLoc) {
517   ExportDecl *D = ExportDecl::Create(Context, CurContext, ExportLoc);
518 
519   // C++20 [module.interface]p1:
520   //   An export-declaration shall appear only [...] in the purview of a module
521   //   interface unit. An export-declaration shall not appear directly or
522   //   indirectly within an unnamed namespace or a private-module-fragment.
523   // FIXME: Check for the unnamed namespace case.
524   if (ModuleScopes.empty() || !ModuleScopes.back().Module->isModulePurview()) {
525     Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
526   } else if (!ModuleScopes.back().ModuleInterface) {
527     Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
528     Diag(ModuleScopes.back().BeginLoc,
529          diag::note_not_module_interface_add_export)
530         << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
531   } else if (ModuleScopes.back().Module->Kind ==
532              Module::PrivateModuleFragment) {
533     Diag(ExportLoc, diag::err_export_in_private_module_fragment);
534     Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
535   }
536 
537   //   [...] its declaration or declaration-seq shall not contain an
538   //   export-declaration.
539   if (D->isExported())
540     Diag(ExportLoc, diag::err_export_within_export);
541 
542   CurContext->addDecl(D);
543   PushDeclContext(S, D);
544   D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported);
545   return D;
546 }
547 
548 /// Complete the definition of an export declaration.
549 Decl *Sema::ActOnFinishExportDecl(Scope *S, Decl *D, SourceLocation RBraceLoc) {
550   auto *ED = cast<ExportDecl>(D);
551   if (RBraceLoc.isValid())
552     ED->setRBraceLoc(RBraceLoc);
553 
554   // FIXME: Diagnose export of internal-linkage declaration (including
555   // anonymous namespace).
556 
557   PopDeclContext();
558   return D;
559 }
560