@@ -220,7 +220,7 @@ bool SwiftLookupTable::contextRequiresName(ContextKind kind) {
220220
221221// / Try to translate the given Clang declaration into a context.
222222static std::optional<SwiftLookupTable::StoredContext>
223- translateDeclToContext (clang::NamedDecl *decl) {
223+ translateDeclToContext (const clang::NamedDecl *decl) {
224224 // Tag declaration.
225225 if (auto tag = dyn_cast<clang::TagDecl>(decl)) {
226226 if (tag->getIdentifier ())
@@ -325,22 +325,49 @@ SwiftLookupTable::translateContext(EffectiveClangContext context) {
325325
326326// / Lookup an unresolved context name and resolve it to a Clang
327327// / declaration context or typedef name.
328- clang::NamedDecl *SwiftLookupTable::resolveContext (StringRef unresolvedName) {
328+ const clang::NamedDecl *
329+ SwiftLookupTable::resolveContext (StringRef unresolvedName) {
330+ SmallVector<StringRef, 1 > nameComponents;
331+ unresolvedName.split (nameComponents, ' .' );
332+
333+ EffectiveClangContext parentContext;
334+
329335 // Look for a context with the given Swift name.
330- for (auto entry :
331- lookup (SerializedSwiftName (unresolvedName),
332- std::make_pair (ContextKind::TranslationUnit, StringRef ()))) {
333- if (auto decl = entry.dyn_cast <clang::NamedDecl *>()) {
334- if (isa<clang::TagDecl>(decl) ||
335- isa<clang::ObjCInterfaceDecl>(decl) ||
336- isa<clang::TypedefNameDecl>(decl))
337- return decl;
336+ for (auto nameComponent : nameComponents) {
337+ auto entries =
338+ parentContext
339+ ? lookup (SerializedSwiftName (nameComponent), parentContext)
340+ : lookup (SerializedSwiftName (nameComponent),
341+ std::make_pair (ContextKind::TranslationUnit, StringRef ()));
342+ bool entryFound = false ;
343+ for (auto entry : entries) {
344+ if (auto decl = entry.dyn_cast <clang::NamedDecl *>()) {
345+ if (isa<clang::TagDecl>(decl) ||
346+ isa<clang::ObjCInterfaceDecl>(decl) ||
347+ isa<clang::NamespaceDecl>(decl)) {
348+ entryFound = true ;
349+ parentContext = EffectiveClangContext (cast<clang::DeclContext>(decl));
350+ break ;
351+ }
352+ if (auto typedefDecl = dyn_cast<clang::TypedefNameDecl>(decl)) {
353+ entryFound = true ;
354+ parentContext = EffectiveClangContext (typedefDecl);
355+ break ;
356+ }
357+ }
338358 }
339- }
340359
341- // FIXME: Search imported modules to resolve the context.
360+ // If we could not resolve this component of the qualified name, bail.
361+ if (!entryFound)
362+ break ;
363+ }
364+
365+ if (!parentContext)
366+ return nullptr ;
342367
343- return nullptr ;
368+ return parentContext.getAsDeclContext ()
369+ ? cast<clang::NamedDecl>(parentContext.getAsDeclContext ())
370+ : parentContext.getTypedefName ();
344371}
345372
346373void SwiftLookupTable::addCategory (clang::ObjCCategoryDecl *category) {
0 commit comments