@@ -554,6 +554,10 @@ ClangImporter::Implementation::getClangSubmoduleForDecl(
554
554
actual = TD->getDefinition ();
555
555
if (!actual && !allowForwardDeclaration)
556
556
return Nothing;
557
+ } else if (auto OPD = dyn_cast<clang::ObjCProtocolDecl>(D)) {
558
+ actual = OPD->getDefinition ();
559
+ if (!actual && !allowForwardDeclaration)
560
+ return Nothing;
557
561
}
558
562
559
563
if (!actual)
@@ -1224,22 +1228,6 @@ void ClangImporter::lookupValue(Identifier name, VisibleDeclConsumer &consumer){
1224
1228
auto &pp = Impl.Instance ->getPreprocessor ();
1225
1229
auto &sema = Impl.Instance ->getSema ();
1226
1230
1227
- // If the given name matches one of a special set of renamed
1228
- // protocol names, perform protocol lookup. For example, the
1229
- // NSObject protocol is named NSObjectProto so that it does not
1230
- // conflict with the NSObject class.
1231
- // FIXME: It would be better to put protocols into a submodule, so
1232
- // that normal name lookup would prefer the class (NSObject) but
1233
- // protocols would be visible with, e.g., protocols.NSObject.
1234
- auto lookupNameKind = clang::Sema::LookupOrdinaryName;
1235
- if (false ) { }
1236
- #define RENAMED_PROTOCOL (ObjCName, SwiftName ) \
1237
- else if (name.str ().equals (#SwiftName)) { \
1238
- name = Impl.SwiftContext .getIdentifier (#ObjCName); \
1239
- lookupNameKind = clang::Sema::LookupObjCProtocolName; \
1240
- }
1241
- #include " swift/ClangImporter/RenamedProtocols.def"
1242
-
1243
1231
// Map the name. If we can't represent the Swift name in Clang, bail out now.
1244
1232
auto clangName = Impl.importName (name);
1245
1233
if (!clangName)
@@ -1255,73 +1243,71 @@ void ClangImporter::lookupValue(Identifier name, VisibleDeclConsumer &consumer){
1255
1243
}
1256
1244
}
1257
1245
1258
- // Perform name lookup into the global scope.
1259
- // FIXME: Map source locations over.
1260
- clang::LookupResult lookupResult (sema, clangName, clang::SourceLocation (),
1261
- lookupNameKind);
1262
1246
bool FoundType = false ;
1263
1247
bool FoundAny = false ;
1264
- if (sema. LookupName (lookupResult, /* Scope= */ 0 ) ) {
1248
+ auto processResults = [&](clang::LookupResult &result ) {
1265
1249
// FIXME: Filter based on access path? C++ access control?
1266
- for (auto decl : lookupResult ) {
1267
- if (auto swiftDecl = Impl.importDeclReal (decl->getUnderlyingDecl ()))
1250
+ for (auto decl : result ) {
1251
+ if (auto swiftDecl = Impl.importDeclReal (decl->getUnderlyingDecl ())) {
1268
1252
if (auto valueDecl = dyn_cast<ValueDecl>(swiftDecl)) {
1269
1253
// If the importer gave us a declaration from the stdlib, make sure
1270
1254
// it does not show up in the lookup results for the imported module.
1271
1255
if (valueDecl->getDeclContext ()->isModuleScopeContext () &&
1272
1256
valueDecl->getModuleContext () == Impl.getStdlibModule ())
1273
1257
continue ;
1258
+ // Check that we didn't pick up something with a remapped name.
1259
+ if (valueDecl->getName () != name)
1260
+ continue ;
1274
1261
1275
1262
consumer.foundDecl (valueDecl, DeclVisibilityKind::VisibleAtTopLevel);
1276
1263
FoundType = FoundType || isa<TypeDecl>(valueDecl);
1277
1264
FoundAny = true ;
1278
1265
}
1266
+ }
1279
1267
}
1280
- }
1268
+ };
1269
+
1270
+ // Perform name lookup into the global scope.
1271
+ // FIXME: Map source locations over.
1272
+ clang::LookupResult lookupResult (sema, clangName, clang::SourceLocation (),
1273
+ clang::Sema::LookupOrdinaryName);
1274
+ if (sema.LookupName (lookupResult, /* Scope=*/ nullptr ))
1275
+ processResults (lookupResult);
1281
1276
1282
- if (lookupNameKind == clang::Sema::LookupOrdinaryName && !FoundType) {
1277
+ if (!FoundType) {
1283
1278
// Look up a tag name if we did not find a type with this name already.
1284
1279
// We don't want to introduce multiple types with same name.
1285
1280
lookupResult.clear (clang::Sema::LookupTagName);
1286
- if (sema.LookupName (lookupResult, /* Scope=*/ 0 )) {
1287
- // FIXME: Filter based on access path? C++ access control?
1288
- for (auto decl : lookupResult) {
1289
- if (auto swiftDecl = Impl.importDeclReal (decl->getUnderlyingDecl ()))
1290
- if (auto valueDecl = dyn_cast<ValueDecl>(swiftDecl)) {
1291
- consumer.foundDecl (valueDecl, DeclVisibilityKind::VisibleAtTopLevel);
1292
- FoundAny = true ;
1293
- }
1294
- }
1295
- }
1281
+ if (sema.LookupName (lookupResult, /* Scope=*/ nullptr ))
1282
+ processResults (lookupResult);
1296
1283
}
1297
1284
1298
- if (lookupNameKind == clang::Sema::LookupOrdinaryName && !FoundAny) {
1299
- // Look up a protocol name if we did not find anything with this
1300
- // name already.
1285
+ // Look up protocol names as well.
1286
+ lookupResult.clear (clang::Sema::LookupObjCProtocolName);
1287
+ if (sema.LookupName (lookupResult, /* Scope=*/ nullptr )) {
1288
+ processResults (lookupResult);
1289
+
1290
+ } else if (!FoundAny && name.str ().endswith (SWIFT_PROTOCOL_SUFFIX)) {
1291
+ auto noProtoNameStr = name.str ().drop_back (strlen (SWIFT_PROTOCOL_SUFFIX));
1292
+ auto protoIdent = &Impl.getClangASTContext ().Idents .get (noProtoNameStr);
1301
1293
lookupResult.clear (clang::Sema::LookupObjCProtocolName);
1302
- if (sema.LookupName (lookupResult, /* Scope=*/ 0 )) {
1303
- // FIXME: Filter based on access path? C++ access control?
1304
- for (auto decl : lookupResult) {
1305
- if (auto swiftDecl = Impl.importDeclReal (decl->getUnderlyingDecl ())) {
1306
- if (auto valueDecl = dyn_cast<ValueDecl>(swiftDecl)) {
1307
- consumer.foundDecl (valueDecl, DeclVisibilityKind::VisibleAtTopLevel);
1308
- FoundAny = true ;
1309
- }
1310
- }
1311
- }
1312
- }
1294
+ lookupResult.setLookupName (protoIdent);
1295
+
1296
+ if (sema.LookupName (lookupResult, /* Scope=*/ nullptr ))
1297
+ processResults (lookupResult);
1298
+
1299
+ lookupResult.setLookupName (clangName);
1313
1300
}
1314
1301
1315
1302
// If we *still* haven't found anything, try looking for '<name>Ref'.
1316
1303
// Eventually, this should be optimized by recognizing this case when
1317
1304
// generating the clang module.
1318
- if (lookupNameKind == clang::Sema::LookupOrdinaryName &&
1319
- !FoundAny && clangID && Impl.SwiftContext .LangOpts .ImportCFTypes ) {
1305
+ if (!FoundAny && clangID && Impl.SwiftContext .LangOpts .ImportCFTypes ) {
1320
1306
lookupResult.clear (clang::Sema::LookupOrdinaryName);
1321
1307
1322
1308
llvm::SmallString<128 > buffer;
1323
1309
buffer += clangID->getName ();
1324
- buffer += " Ref " ;
1310
+ buffer += SWIFT_CFTYPE_SUFFIX ;
1325
1311
auto refIdent = &Impl.Instance ->getASTContext ().Idents .get (buffer.str ());
1326
1312
lookupResult.setLookupName (refIdent);
1327
1313
0 commit comments