@@ -440,6 +440,66 @@ ClangImporter::~ClangImporter() {
440440
441441#pragma mark Module loading
442442
443+ static Optional<StringRef> getModuleMapFilePath (StringRef name,
444+ SearchPathOptions &Opts,
445+ llvm::Triple triple,
446+ SmallVectorImpl<char > &buffer) {
447+ StringRef platform = swift::getPlatformNameForTriple (triple);
448+ StringRef arch = swift::getMajorArchitectureName (triple);
449+
450+ StringRef SDKPath = Opts.getSDKPath ();
451+ if (!SDKPath.empty ()) {
452+ buffer.clear ();
453+ buffer.append (SDKPath.begin (), SDKPath.end ());
454+ llvm::sys::path::append (buffer, " usr" , " lib" , " swift" );
455+ llvm::sys::path::append (buffer, platform, arch, name);
456+
457+ // Only specify the module map if that file actually exists. It may not;
458+ // for example in the case that `swiftc -target x86_64-unknown-linux-gnu
459+ // -emit-ir` is invoked using a Swift compiler not built for Linux targets.
460+ if (llvm::sys::fs::exists (buffer))
461+ return StringRef (buffer.data (), buffer.size ());
462+ }
463+
464+ if (!Opts.RuntimeResourcePath .empty ()) {
465+ buffer.clear ();
466+ buffer.append (Opts.RuntimeResourcePath .begin (),
467+ Opts.RuntimeResourcePath .end ());
468+ llvm::sys::path::append (buffer, platform, arch, name);
469+
470+ // Only specify the module map if that file actually exists. It may not;
471+ // for example in the case that `swiftc -target x86_64-unknown-linux-gnu
472+ // -emit-ir` is invoked using a Swift compiler not built for Linux targets.
473+ if (llvm::sys::fs::exists (buffer))
474+ return StringRef (buffer.data (), buffer.size ());
475+ }
476+
477+ return None;
478+ }
479+
480+ // / Finds the glibc.modulemap file relative to the provided resource dir.
481+ // /
482+ // / Note that the module map used for Glibc depends on the target we're
483+ // / compiling for, and is not included in the resource directory with the other
484+ // / implicit module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.
485+ static Optional<StringRef>
486+ getGlibcModuleMapPath (SearchPathOptions &Opts, llvm::Triple triple,
487+ SmallVectorImpl<char > &buffer) {
488+ return getModuleMapFilePath (" glibc.modulemap" , Opts, triple, buffer);
489+ }
490+
491+ static Optional<StringRef>
492+ getLibStdCxxModuleMapPath (SearchPathOptions &opts, llvm::Triple triple,
493+ SmallVectorImpl<char > &buffer) {
494+ return getModuleMapFilePath (" libstdcxx.modulemap" , opts, triple, buffer);
495+ }
496+
497+ static Optional<StringRef>
498+ getLibShimCxxModuleMapPath (SearchPathOptions &Opts, llvm::Triple triple,
499+ SmallVectorImpl<char > &buffer) {
500+ return getModuleMapFilePath (" libcxxshim.modulemap" , Opts, triple, buffer);
501+ }
502+
443503static bool clangSupportsPragmaAttributeWithSwiftAttr () {
444504 clang::AttributeCommonInfo swiftAttrInfo (clang::SourceRange (),
445505 clang::AttributeCommonInfo::AT_SwiftAttr,
@@ -553,6 +613,11 @@ importer::getNormalInvocationArguments(
553613 });
554614 }
555615
616+ SmallString<128 > buffer;
617+ if (auto path = getLibShimCxxModuleMapPath (searchPathOpts, triple, buffer)) {
618+ invocationArgStrs.push_back ((Twine (" -fmodule-map-file=" ) + *path).str ());
619+ }
620+
556621 // Set C language options.
557622 if (triple.isOSDarwin ()) {
558623 invocationArgStrs.insert (invocationArgStrs.end (), {
@@ -4387,10 +4452,12 @@ DeclRefExpr *getInteropStaticCastDeclRefExpr(ASTContext &ctx,
43874452 derived = derived->wrapInPointer (PTK_UnsafePointer);
43884453 }
43894454
4390- // Lookup our static cast helper function.
4391- // TODO: change this to stdlib or something.
4392- auto wrapperModule =
4393- ctx.getClangModuleLoader ()->getWrapperForModule (owningModule);
4455+ // Lookup our static cast helper function in the C++ shim module.
4456+ auto wrapperModule = ctx.getLoadedModule (ctx.getIdentifier (" CxxShim" ));
4457+ assert (wrapperModule &&
4458+ " CxxShim module is required when using members of a base class. "
4459+ " Make sure you `import CxxShim`." );
4460+
43944461 SmallVector<ValueDecl *, 1 > results;
43954462 ctx.lookupInModule (wrapperModule, " __swift_interopStaticCast" , results);
43964463 assert (
0 commit comments