@@ -2871,8 +2871,8 @@ void ClangImporter::lookupBridgingHeaderDecls(
2871
2871
for (auto *ClangD : Impl.BridgeHeaderTopLevelDecls ) {
2872
2872
if (filter (ClangD)) {
2873
2873
if (auto *ND = dyn_cast<clang::NamedDecl>(ClangD)) {
2874
- if (Decl * imported = Impl.importDeclReal (ND, Impl.CurrentVersion ))
2875
- receiver (imported);
2874
+ if (auto imported = Impl.importDeclReal (ND, Impl.CurrentVersion ))
2875
+ receiver (imported. getValue () );
2876
2876
}
2877
2877
}
2878
2878
}
@@ -2937,8 +2937,8 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
2937
2937
continue ;
2938
2938
if (filter (ClangD)) {
2939
2939
if (auto *ND = dyn_cast<clang::NamedDecl>(ClangD)) {
2940
- if (Decl * imported = Impl.importDeclReal (ND, Impl.CurrentVersion ))
2941
- receiver (imported);
2940
+ if (auto imported = Impl.importDeclReal (ND, Impl.CurrentVersion ))
2941
+ receiver (imported. getValue () );
2942
2942
}
2943
2943
}
2944
2944
}
@@ -3035,13 +3035,18 @@ void ClangImporter::lookupTypeDecl(
3035
3035
!isa<clang::ObjCCompatibleAliasDecl>(clangDecl)) {
3036
3036
continue ;
3037
3037
}
3038
- Decl *imported = Impl.importDecl (clangDecl, Impl.CurrentVersion );
3038
+ const Optional<Decl *> imported =
3039
+ Impl.importDecl (clangDecl, Impl.CurrentVersion );
3040
+ if (!imported.hasValue ())
3041
+ continue ;
3039
3042
3040
3043
// Namespaces are imported as extensions for enums.
3041
- if (auto ext = dyn_cast_or_null<ExtensionDecl>(imported)) {
3042
- imported = ext->getExtendedNominal ();
3044
+ Decl *importedExtendedNominal = imported.getValue ();
3045
+ if (auto ext = dyn_cast_or_null<ExtensionDecl>(imported.getValue ())) {
3046
+ importedExtendedNominal = ext->getExtendedNominal ();
3043
3047
}
3044
- if (auto *importedType = dyn_cast_or_null<TypeDecl>(imported)) {
3048
+ if (auto *importedType =
3049
+ dyn_cast_or_null<TypeDecl>(importedExtendedNominal)) {
3045
3050
foundViaClang = true ;
3046
3051
receiver (importedType);
3047
3052
}
@@ -3147,10 +3152,15 @@ void ClangModuleUnit::getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {
3147
3152
3148
3153
// Add the extensions produced by importing categories.
3149
3154
for (auto category : lookupTable->categories ()) {
3150
- if (auto extension = cast_or_null<ExtensionDecl>(
3151
- owner.importDecl (category, owner.CurrentVersion ,
3152
- /* UseCanonical*/ false ))) {
3153
- results.push_back (extension);
3155
+ const Optional<Decl *> extension =
3156
+ owner.importDecl (category,
3157
+ owner.CurrentVersion , /* UseCanonical*/ false );
3158
+ if (!extension.hasValue ())
3159
+ continue ;
3160
+
3161
+ if (auto extensionValue =
3162
+ cast_or_null<ExtensionDecl>(extension.getValue ())) {
3163
+ results.push_back (extensionValue);
3154
3164
}
3155
3165
}
3156
3166
@@ -3170,32 +3180,34 @@ void ClangModuleUnit::getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {
3170
3180
llvm::SmallPtrSet<ExtensionDecl *, 8 > knownExtensions;
3171
3181
for (auto entry : lookupTable->allGlobalsAsMembers ()) {
3172
3182
auto decl = entry.get <clang::NamedDecl *>();
3173
- Decl *importedDecl = owner.importDecl (decl, owner.CurrentVersion );
3183
+ const Optional<Decl *> importedDecl =
3184
+ owner.importDecl (decl, owner.CurrentVersion );
3174
3185
if (!importedDecl) continue ;
3175
3186
3176
3187
// Find the enclosing extension, if there is one.
3177
- ExtensionDecl *ext = findEnclosingExtension (importedDecl);
3188
+ ExtensionDecl *ext = findEnclosingExtension (importedDecl. getValue () );
3178
3189
if (ext && knownExtensions.insert (ext).second )
3179
3190
results.push_back (ext);
3180
3191
3181
3192
// If this is a compatibility typealias, the canonical type declaration
3182
3193
// may exist in another extension.
3183
- auto alias = dyn_cast<TypeAliasDecl>(importedDecl);
3194
+ auto alias = dyn_cast<TypeAliasDecl>(importedDecl. getValue () );
3184
3195
if (!alias || !alias->isCompatibilityAlias ()) continue ;
3185
3196
3186
3197
auto aliasedTy = alias->getUnderlyingType ();
3187
3198
ext = nullptr ;
3188
- importedDecl = nullptr ;
3189
3199
3190
3200
// Note: We can't use getAnyGeneric() here because `aliasedTy`
3191
3201
// might be typealias.
3202
+ Decl *aliasTypeDecl = nullptr ;
3192
3203
if (auto Ty = dyn_cast<TypeAliasType>(aliasedTy.getPointer ()))
3193
- importedDecl = Ty->getDecl ();
3204
+ aliasTypeDecl = Ty->getDecl ();
3194
3205
else if (auto Ty = dyn_cast<AnyGenericType>(aliasedTy.getPointer ()))
3195
- importedDecl = Ty->getDecl ();
3196
- if (!importedDecl) continue ;
3206
+ aliasTypeDecl = Ty->getDecl ();
3207
+ else
3208
+ continue ;
3197
3209
3198
- ext = findEnclosingExtension (importedDecl );
3210
+ ext = findEnclosingExtension (aliasTypeDecl );
3199
3211
if (ext && knownExtensions.insert (ext).second )
3200
3212
results.push_back (ext);
3201
3213
}
@@ -3275,7 +3287,7 @@ void ClangModuleUnit::lookupValue(DeclName name, NLKind lookupKind,
3275
3287
if (auto lookupTable = owner.findLookupTable (clangModule)) {
3276
3288
// Search it.
3277
3289
owner.lookupValue (*lookupTable, name, *consumer);
3278
- if (getASTContext ().LangOpts .EnableExperimentalClangImporterDiagnostics ) {
3290
+ if (getASTContext ().LangOpts .EnableExperimentalClangImporterDiagnostics ) {
3279
3291
if (results.empty ()) {
3280
3292
owner.diagnoseValue (*lookupTable, name);
3281
3293
}
@@ -3359,23 +3371,26 @@ ClangModuleUnit::lookupNestedType(Identifier name,
3359
3371
if (!newName.getDeclName ().isSimpleName (name))
3360
3372
return true ;
3361
3373
3362
- auto decl = dyn_cast_or_null<TypeDecl>(
3363
- owner.importDeclReal (clangTypeDecl, nameVersion));
3364
- if (!decl)
3374
+ const Optional<Decl *> decl =
3375
+ owner.importDeclReal (clangTypeDecl, nameVersion);
3376
+ if (!decl.hasValue ())
3377
+ return false ;
3378
+ auto declValue = dyn_cast_or_null<TypeDecl>(decl.getValue ());
3379
+ if (!declValue)
3365
3380
return false ;
3366
3381
3367
3382
if (!originalDecl)
3368
- originalDecl = decl ;
3369
- else if (originalDecl == decl )
3383
+ originalDecl = declValue ;
3384
+ else if (originalDecl == declValue )
3370
3385
return true ;
3371
3386
3372
- auto *importedContext = decl ->getDeclContext ()->getSelfNominalTypeDecl ();
3387
+ auto *importedContext = declValue ->getDeclContext ()->getSelfNominalTypeDecl ();
3373
3388
if (importedContext != baseType)
3374
3389
return true ;
3375
3390
3376
- assert (decl ->getName () == name &&
3391
+ assert (declValue ->getName () == name &&
3377
3392
" importFullName behaved differently from importDecl" );
3378
- results.push_back (decl );
3393
+ results.push_back (declValue );
3379
3394
anyMatching = true ;
3380
3395
return true ;
3381
3396
});
@@ -3458,8 +3473,9 @@ void ClangImporter::loadObjCMethods(
3458
3473
(void )Impl.importDecl (objcMethod->findPropertyDecl (true ),
3459
3474
Impl.CurrentVersion );
3460
3475
3461
- method = dyn_cast_or_null<AbstractFunctionDecl>(
3462
- Impl.importDecl (objcMethod, Impl.CurrentVersion ));
3476
+ if (const Optional<Decl *> methodOpt =
3477
+ Impl.importDecl (objcMethod, Impl.CurrentVersion ))
3478
+ method = dyn_cast_or_null<AbstractFunctionDecl>(methodOpt.getValue ());
3463
3479
}
3464
3480
3465
3481
// If we didn't find anything, we're done.
@@ -3541,14 +3557,15 @@ void ClangModuleUnit::lookupObjCMethods(
3541
3557
if (objcMethod->isPropertyAccessor ())
3542
3558
(void )owner.importDecl (objcMethod->findPropertyDecl (true ),
3543
3559
owner.CurrentVersion );
3544
- Decl *imported = owner.importDecl (objcMethod, owner.CurrentVersion );
3560
+ Optional<Decl *> imported =
3561
+ owner.importDecl (objcMethod, owner.CurrentVersion );
3545
3562
if (!imported) continue ;
3546
3563
3547
- if (auto func = dyn_cast<AbstractFunctionDecl>(imported))
3564
+ if (auto func = dyn_cast<AbstractFunctionDecl>(imported. getValue () ))
3548
3565
results.push_back (func);
3549
3566
3550
3567
// If there is an alternate declaration, also look at it.
3551
- for (auto alternate : owner.getAlternateDecls (imported)) {
3568
+ for (auto alternate : owner.getAlternateDecls (imported. getValue () )) {
3552
3569
if (auto func = dyn_cast<AbstractFunctionDecl>(alternate))
3553
3570
results.push_back (func);
3554
3571
}
@@ -3630,7 +3647,8 @@ std::string ClangImporter::getClangModuleHash() const {
3630
3647
return Impl.Invocation ->getModuleHash (Impl.Instance ->getDiagnostics ());
3631
3648
}
3632
3649
3633
- Decl *ClangImporter::importDeclCached (const clang::NamedDecl *ClangDecl) {
3650
+ Optional<Decl *>
3651
+ ClangImporter::importDeclCached (const clang::NamedDecl *ClangDecl) {
3634
3652
return Impl.importDeclCached (ClangDecl, Impl.CurrentVersion );
3635
3653
}
3636
3654
@@ -4005,8 +4023,11 @@ bool ClangImporter::Implementation::lookupValue(SwiftLookupTable &table,
4005
4023
if (name.isOperator ()) {
4006
4024
for (auto entry : table.lookupMemberOperators (name.getBaseName ())) {
4007
4025
if (isVisibleClangEntry (entry)) {
4008
- if (auto decl = dyn_cast_or_null<ValueDecl>(
4009
- importDeclReal (entry->getMostRecentDecl (), CurrentVersion))) {
4026
+ const Optional<Decl *> declOpt =
4027
+ importDeclReal (entry->getMostRecentDecl (), CurrentVersion);
4028
+ if (!declOpt.hasValue ())
4029
+ continue ;
4030
+ if (auto decl = dyn_cast_or_null<ValueDecl>(declOpt.getValue ())) {
4010
4031
consumer.foundDecl (decl, DeclVisibilityKind::VisibleAtTopLevel);
4011
4032
declFound = true ;
4012
4033
}
@@ -4022,14 +4043,14 @@ bool ClangImporter::Implementation::lookupValue(SwiftLookupTable &table,
4022
4043
// If it's a Clang declaration, try to import it.
4023
4044
if (auto clangDecl = entry.dyn_cast <clang::NamedDecl *>()) {
4024
4045
bool isNamespace = isa<clang::NamespaceDecl>(clangDecl);
4025
- Decl * realDecl =
4046
+ auto realDecl =
4026
4047
importDeclReal (clangDecl->getMostRecentDecl (), CurrentVersion,
4027
4048
/* useCanonicalDecl*/ !isNamespace);
4028
4049
4029
- if (!realDecl)
4050
+ if (!realDecl.hasValue () || !realDecl.getValue () ||
4051
+ !isa<ValueDecl>(realDecl.getValue ()))
4030
4052
continue ;
4031
- decl = cast<ValueDecl>(realDecl);
4032
- if (!decl) continue ;
4053
+ decl = cast<ValueDecl>(realDecl.getValue ());
4033
4054
} else if (!name.isSpecial ()) {
4034
4055
// Try to import a macro.
4035
4056
if (auto modMacro = entry.dyn_cast <clang::ModuleMacro *>())
@@ -4096,9 +4117,12 @@ bool ClangImporter::Implementation::lookupValue(SwiftLookupTable &table,
4096
4117
return ;
4097
4118
4098
4119
// Then try to import the decl under the alternate name.
4120
+ const Optional<Decl *> alternateNamedDeclOpt =
4121
+ importDeclReal (recentClangDecl, nameVersion);
4122
+ if (!alternateNamedDeclOpt.hasValue ())
4123
+ return ;
4099
4124
auto alternateNamedDecl =
4100
- cast_or_null<ValueDecl>(importDeclReal (recentClangDecl,
4101
- nameVersion));
4125
+ cast_or_null<ValueDecl>(alternateNamedDeclOpt.getValue ());
4102
4126
if (!alternateNamedDecl || alternateNamedDecl == decl)
4103
4127
return ;
4104
4128
assert (alternateNamedDecl->getName ().matchesRef (name) &&
@@ -4144,8 +4168,10 @@ void ClangImporter::Implementation::lookupObjCMembers(
4144
4168
[&](ImportedName importedName,
4145
4169
ImportNameVersion nameVersion) -> bool {
4146
4170
// Import the declaration.
4147
- auto decl =
4148
- cast_or_null<ValueDecl>(importDeclReal (clangDecl, nameVersion));
4171
+ const Optional<Decl *> declOpt = importDeclReal (clangDecl, nameVersion);
4172
+ if (!declOpt.hasValue ())
4173
+ return false ;
4174
+ auto decl = cast_or_null<ValueDecl>(declOpt.getValue ());
4149
4175
if (!decl)
4150
4176
return false ;
4151
4177
@@ -4716,8 +4742,10 @@ ClangImporter::instantiateCXXClassTemplate(
4716
4742
assert (isa<clang::RecordType>(CanonType) &&
4717
4743
" type of non-dependent specialization is not a RecordType" );
4718
4744
4719
- return dyn_cast_or_null<StructDecl>(
4720
- Impl.importDecl (ctsd, Impl.CurrentVersion ));
4745
+ Optional<Decl *> structDecl = Impl.importDecl (ctsd, Impl.CurrentVersion );
4746
+ if (!structDecl.hasValue ())
4747
+ return nullptr ;
4748
+ return dyn_cast_or_null<StructDecl>(structDecl.getValue ());
4721
4749
}
4722
4750
4723
4751
bool ClangImporter::isCXXMethodMutating (const clang::CXXMethodDecl *method) {
0 commit comments