@@ -263,6 +263,7 @@ struct PrintWithOpaqueResultTypeKeywordRAII {
263263};
264264
265265PrintOptions PrintOptions::printSwiftInterfaceFile (ModuleDecl *ModuleToPrint,
266+ bool useModuleSelectors,
266267 bool preferTypeRepr,
267268 bool printFullConvention,
268269 InterfaceMode interfaceMode,
@@ -276,6 +277,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
276277 result.PrintLongAttrsOnSeparateLines = true ;
277278 result.TypeDefinitions = true ;
278279 result.CurrentModule = ModuleToPrint;
280+ result.UseModuleSelectors = useModuleSelectors;
279281 result.FullyQualifiedTypes = true ;
280282 result.FullyQualifiedTypesIfAmbiguous = true ;
281283 result.FullyQualifiedExtendedTypesIfAmbiguous = true ;
@@ -6058,9 +6060,8 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
60586060 return Options.CurrentModule ->getVisibleClangModules (Options.InterfaceContentKind );
60596061 }
60606062
6061- template <typename T>
6062- void printModuleContext (T *Ty) {
6063- FileUnit *File = cast<FileUnit>(Ty->getDecl ()->getModuleScopeContext ());
6063+ void printModuleContext (GenericTypeDecl *TyDecl) {
6064+ FileUnit *File = cast<FileUnit>(TyDecl->getModuleScopeContext ());
60646065 ModuleDecl *Mod = File->getParentModule ();
60656066 StringRef ExportedModuleName = File->getExportedModuleName ();
60666067
@@ -6069,7 +6070,7 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
60696070 // all of these modules may be visible. We therefore need to make sure we
60706071 // choose a module that is visible from the current module. This is possible
60716072 // only if we know what the current module is.
6072- const clang::Decl *ClangDecl = Ty-> getDecl () ->getClangDecl ();
6073+ const clang::Decl *ClangDecl = TyDecl ->getClangDecl ();
60736074 if (ClangDecl && Options.CurrentModule ) {
60746075 for (auto *Redecl : ClangDecl->redecls ()) {
60756076 auto *owningModule = Redecl->getOwningModule ();
@@ -6106,12 +6107,10 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
61066107 }
61076108
61086109 if (Options.UseOriginallyDefinedInModuleNames ) {
6109- Decl *D = Ty-> getDecl ();
6110- for ( auto attr: D ->getAttrs ().getAttributes <OriginallyDefinedInAttr>()) {
6110+ if ( auto attr =
6111+ TyDecl ->getAttrs ().getAttribute <OriginallyDefinedInAttr>()) {
61116112 Name = Mod->getASTContext ().getIdentifier (
6112- const_cast <OriginallyDefinedInAttr *>(attr)
6113- ->getManglingModuleName ());
6114- break ;
6113+ attr->getManglingModuleName ());
61156114 }
61166115 }
61176116
@@ -6122,7 +6121,6 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
61226121 }
61236122
61246123 Printer.printModuleRef (Mod, Name);
6125- Printer << " ." ;
61266124 }
61276125
61286126 template <typename T>
@@ -6140,7 +6138,42 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
61406138 return M->getRealName ().str ().starts_with (LLDB_EXPRESSIONS_MODULE_NAME_PREFIX);
61416139 }
61426140
6141+ bool isMemberOfGenericParameter (TypeBase *T) {
6142+ Type parent = nullptr ;
6143+ if (auto alias = dyn_cast<TypeAliasType>(T))
6144+ parent = alias->getParent ();
6145+ else if (auto generic = T->getAs <AnyGenericType>())
6146+ parent = generic->getParent ();
6147+ return parent && parent->isTypeParameter ();
6148+ }
6149+
6150+ bool shouldPrintModuleSelector (TypeBase *T) {
6151+ if (!Options.UseModuleSelectors )
6152+ return false ;
6153+
6154+ GenericTypeDecl *GTD = T->getAnyGeneric ();
6155+ if (!GTD && isa<TypeAliasType>(T))
6156+ GTD = cast<TypeAliasType>(T)->getDecl ();
6157+ if (!GTD)
6158+ return false ;
6159+
6160+ // Builtin types must always be qualified somehow.
6161+ ModuleDecl *M = GTD->getDeclContext ()->getParentModule ();
6162+ if (M->isBuiltinModule ())
6163+ return true ;
6164+
6165+ // A member of a generic parameter can't be qualified by a module selector.
6166+ if (isMemberOfGenericParameter (T))
6167+ return false ;
6168+
6169+ // Module selectors skip over local types, so don't add one.
6170+ return GTD->getLocalContext () == nullptr ;
6171+ }
6172+
61436173 bool shouldPrintFullyQualified (TypeBase *T) {
6174+ if (Options.UseModuleSelectors )
6175+ return false ;
6176+
61446177 if (Options.FullyQualifiedTypes )
61456178 return true ;
61466179
@@ -6197,7 +6230,15 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
61976230 printParentType (parent);
61986231 NameContext = PrintNameContext::TypeMember;
61996232 } else if (shouldPrintFullyQualified (Ty)) {
6200- printModuleContext (Ty);
6233+ printModuleContext (Ty->getDecl ());
6234+ Printer << " ." ;
6235+ NameContext = PrintNameContext::TypeMember;
6236+ }
6237+
6238+ // We print module selectors whether or not we printed a parent type.
6239+ if (shouldPrintModuleSelector (Ty)) {
6240+ printModuleContext (Ty->getDecl ());
6241+ Printer << " ::" ;
62016242 NameContext = PrintNameContext::TypeMember;
62026243 }
62036244
0 commit comments