@@ -207,8 +207,8 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
207207 bool useExportedModuleNames,
208208 bool aliasModuleNames,
209209 llvm::SmallSet<StringRef, 4 >
210- *aliasModuleNamesTargets
211- ) {
210+ *aliasModuleNamesTargets,
211+ bool abiComments ) {
212212 PrintOptions result;
213213 result.IsForSwiftInterface = true ;
214214 result.PrintLongAttrsOnSeparateLines = true ;
@@ -230,6 +230,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
230230 result.PreferTypeRepr = preferTypeRepr;
231231 result.AliasModuleNames = aliasModuleNames;
232232 result.AliasModuleNamesTargets = aliasModuleNamesTargets;
233+ result.PrintABIComments = abiComments;
233234 if (printFullConvention)
234235 result.PrintFunctionRepresentationAttrs =
235236 PrintOptions::FunctionRepresentationMode::Full;
@@ -770,19 +771,28 @@ class PrintAST : public ASTVisitor<PrintAST> {
770771 printRawComment (RC);
771772 }
772773
774+ // / If we should print a mangled name for this declaration, return that
775+ // / mangled name.
776+ std::optional<std::string> mangledNameToPrint (const Decl *D);
777+
773778 void printDocumentationComment (const Decl *D) {
774- if (!Options.PrintDocumentationComments )
775- return ;
779+ if (Options.PrintDocumentationComments ) {
780+ // Try to print a comment from Clang.
781+ auto MaybeClangNode = D->getClangNode ();
782+ if (MaybeClangNode) {
783+ if (auto *CD = MaybeClangNode.getAsDecl ())
784+ printClangDocumentationComment (CD);
785+ return ;
786+ }
776787
777- // Try to print a comment from Clang.
778- auto MaybeClangNode = D->getClangNode ();
779- if (MaybeClangNode) {
780- if (auto *CD = MaybeClangNode.getAsDecl ())
781- printClangDocumentationComment (CD);
782- return ;
788+ printSwiftDocumentationComment (D);
783789 }
784790
785- printSwiftDocumentationComment (D);
791+ if (auto mangledName = mangledNameToPrint (D)) {
792+ indent ();
793+ Printer << " // MANGLED NAME: " << *mangledName;
794+ Printer.printNewline ();
795+ }
786796 }
787797
788798 void printStaticKeyword (StaticSpellingKind StaticSpelling) {
@@ -3061,6 +3071,60 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
30613071 }
30623072}
30633073
3074+ std::optional<std::string> PrintAST::mangledNameToPrint (const Decl *D) {
3075+ using ASTMangler = Mangle::ASTMangler;
3076+
3077+ if (!Options.PrintABIComments )
3078+ return std::nullopt ;
3079+
3080+ auto valueDecl = dyn_cast<ValueDecl>(D);
3081+ if (!valueDecl)
3082+ return std::nullopt ;
3083+
3084+ // Anything with an access level less than "package" isn't meant to be
3085+ // referenced from source code outside the module.
3086+ if (valueDecl->getEffectiveAccess () < AccessLevel::Package)
3087+ return std::nullopt ;
3088+
3089+ // For functions, mangle the entity directly.
3090+ if (auto func = dyn_cast<FuncDecl>(D)) {
3091+ ASTMangler mangler;
3092+ return mangler.mangleEntity (func);
3093+ }
3094+
3095+ // For initializers, mangle the allocating initializer.
3096+ if (auto init = dyn_cast<ConstructorDecl>(D)) {
3097+ ASTMangler mangler;
3098+ return mangler.mangleConstructorEntity (init, /* isAllocating=*/ true );
3099+ }
3100+
3101+ // For global and static variables, mangle the entity directly.
3102+ if (auto var = dyn_cast<VarDecl>(D)) {
3103+ if (!var->isInstanceMember ()) {
3104+ ASTMangler mangler;
3105+ return mangler.mangleEntity (var);
3106+ }
3107+ }
3108+
3109+ // For subscripts, mangle the entity directly.
3110+ if (auto subscript = dyn_cast<SubscriptDecl>(D)) {
3111+ ASTMangler mangler;
3112+ return mangler.mangleEntity (subscript);
3113+ }
3114+
3115+ // For nominal types, mangle the type metadata accessor.
3116+ if (auto nominal = dyn_cast<NominalTypeDecl>(D)) {
3117+ if (!isa<ProtocolDecl>(nominal) && !nominal->getGenericSignature ()) {
3118+ ASTMangler mangler;
3119+ std::string name = mangler.mangleNominalType (nominal);
3120+ name += " Ma" ;
3121+ return name;
3122+ }
3123+ }
3124+
3125+ return std::nullopt ;
3126+ }
3127+
30643128static void suppressingFeatureIsolatedAny (PrintOptions &options,
30653129 llvm::function_ref<void ()> action) {
30663130 llvm::SaveAndRestore<bool > scope (options.SuppressIsolatedAny , true );
0 commit comments