27
27
#include " swift/AST/Type.h"
28
28
#include " swift/AST/Types.h"
29
29
#include " swift/Basic/SourceManager.h"
30
+ #include " swift/IDE/CodeCompletion.h"
30
31
#include " swift/Sema/IDETypeChecking.h"
31
32
#include " clang/AST/Attr.h"
32
33
#include " clang/AST/Decl.h"
@@ -279,19 +280,21 @@ static void collectPossibleCalleesByQualifiedLookup(
279
280
DeclContext &DC, Type baseTy, DeclNameRef name,
280
281
SmallVectorImpl<FunctionTypeAndDecl> &candidates) {
281
282
bool isOnMetaType = baseTy->is <AnyMetatypeType>();
283
+ auto baseInstanceTy = baseTy->getMetatypeInstanceType ();
282
284
283
285
SmallVector<ValueDecl *, 2 > decls;
284
- if (!DC.lookupQualified (baseTy-> getMetatypeInstanceType () ,
286
+ if (!DC.lookupQualified (baseInstanceTy ,
285
287
name.withoutArgumentLabels (),
286
288
NL_QualifiedDefault | NL_ProtocolMembers,
287
289
decls))
288
290
return ;
289
291
292
+ auto *baseNominal = baseInstanceTy->getAnyNominal ();
290
293
for (auto *VD : decls) {
291
294
if ((!isa<AbstractFunctionDecl>(VD) && !isa<SubscriptDecl>(VD)) ||
292
295
VD->shouldHideFromEditor ())
293
296
continue ;
294
- if (!isMemberDeclApplied (&DC, baseTy-> getMetatypeInstanceType () , VD))
297
+ if (!isMemberDeclApplied (&DC, baseInstanceTy , VD))
295
298
continue ;
296
299
Type declaredMemberType = VD->getInterfaceType ();
297
300
if (!declaredMemberType->is <AnyFunctionType>())
@@ -314,16 +317,15 @@ static void collectPossibleCalleesByQualifiedLookup(
314
317
}
315
318
}
316
319
317
- auto subs = baseTy-> getMetatypeInstanceType () ->getMemberSubstitutionMap (
320
+ auto subs = baseInstanceTy ->getMemberSubstitutionMap (
318
321
DC.getParentModule (), VD,
319
322
VD->getInnermostDeclContext ()->getGenericEnvironmentOfContext ());
320
323
auto fnType = declaredMemberType.subst (subs);
321
324
if (!fnType)
322
325
continue ;
323
326
324
327
if (fnType->is <AnyFunctionType>()) {
325
- auto baseInstanceTy = baseTy->getMetatypeInstanceType ();
326
- // If we are calling to typealias type,
328
+ // If we are calling to typealias type,
327
329
if (isa<SugarType>(baseInstanceTy.getPointer ())) {
328
330
auto canBaseTy = baseInstanceTy->getCanonicalType ();
329
331
fnType = fnType.transform ([&](Type t) -> Type {
@@ -332,7 +334,13 @@ static void collectPossibleCalleesByQualifiedLookup(
332
334
return t;
333
335
});
334
336
}
335
- candidates.emplace_back (fnType->castTo <AnyFunctionType>(), VD);
337
+ auto semanticContext = SemanticContextKind::CurrentNominal;
338
+ if (baseNominal &&
339
+ VD->getDeclContext ()->getSelfNominalTypeDecl () != baseNominal)
340
+ semanticContext = SemanticContextKind::Super;
341
+
342
+ candidates.emplace_back (fnType->castTo <AnyFunctionType>(), VD,
343
+ semanticContext);
336
344
}
337
345
}
338
346
}
@@ -347,10 +355,16 @@ static void collectPossibleCalleesByQualifiedLookup(
347
355
DC.getASTContext (), &DC, CompletionTypeCheckKind::Normal, baseExpr, ref);
348
356
if (!baseTyOpt)
349
357
return ;
350
- auto baseTy = (*baseTyOpt)->getRValueType ();
358
+
359
+ auto baseTy = (*baseTyOpt)->getWithoutSpecifierType ();
351
360
if (!baseTy->getMetatypeInstanceType ()->mayHaveMembers ())
352
361
return ;
353
362
363
+ // Use metatype for lookup 'super.init' if it's inside constructors.
364
+ if (isa<SuperRefExpr>(baseExpr) && isa<ConstructorDecl>(DC) &&
365
+ name == DeclNameRef::createConstructor ())
366
+ baseTy = MetatypeType::get (baseTy);
367
+
354
368
collectPossibleCalleesByQualifiedLookup (DC, baseTy, name, candidates);
355
369
}
356
370
@@ -360,24 +374,17 @@ static bool collectPossibleCalleesForApply(
360
374
SmallVectorImpl<FunctionTypeAndDecl> &candidates) {
361
375
auto *fnExpr = callExpr->getFn ();
362
376
363
- if (auto type = fnExpr->getType ()) {
364
- if (!type->hasUnresolvedType () && !type->hasError ()) {
365
- if (auto *funcType = type->getAs <AnyFunctionType>()) {
366
- auto refDecl = fnExpr->getReferencedDecl ();
367
- if (!refDecl)
368
- if (auto apply = dyn_cast<ApplyExpr>(fnExpr))
369
- refDecl = apply->getFn ()->getReferencedDecl ();
370
- candidates.emplace_back (funcType, refDecl.getDecl ());
371
- return true ;
372
- }
373
- }
374
- }
375
-
376
377
if (auto *DRE = dyn_cast<DeclRefExpr>(fnExpr)) {
377
378
if (auto *decl = DRE->getDecl ()) {
378
- if (decl->hasInterfaceType ())
379
- if (auto *funcType = decl->getInterfaceType ()->getAs <AnyFunctionType>())
380
- candidates.emplace_back (funcType, decl);
379
+ Type fnType = fnExpr->getType ();
380
+ if ((!fnType || fnType->hasError () || fnType->hasUnresolvedType ()) &&
381
+ decl->hasInterfaceType ())
382
+ fnType = decl->getInterfaceType ();
383
+ if (fnType) {
384
+ fnType = fnType->getWithoutSpecifierType ();
385
+ if (auto *funcTy = fnType->getAs <AnyFunctionType>())
386
+ candidates.emplace_back (funcTy, decl);
387
+ }
381
388
}
382
389
} else if (auto *OSRE = dyn_cast<OverloadSetRefExpr>(fnExpr)) {
383
390
for (auto *decl : OSRE->getDecls ()) {
@@ -386,30 +393,49 @@ static bool collectPossibleCalleesForApply(
386
393
candidates.emplace_back (funcType, decl);
387
394
}
388
395
} else if (auto *UDE = dyn_cast<UnresolvedDotExpr>(fnExpr)) {
389
- collectPossibleCalleesByQualifiedLookup (
390
- DC, UDE-> getBase (), UDE-> getName (), candidates);
396
+ collectPossibleCalleesByQualifiedLookup (DC, UDE-> getBase (), UDE-> getName (),
397
+ candidates);
391
398
} else if (auto *DSCE = dyn_cast<DotSyntaxCallExpr>(fnExpr)) {
392
399
if (auto *DRE = dyn_cast<DeclRefExpr>(DSCE->getFn ())) {
393
- collectPossibleCalleesByQualifiedLookup (
394
- DC, DSCE->getArg (), DeclNameRef (DRE->getDecl ()->getFullName ()),
395
- candidates);
400
+ collectPossibleCalleesByQualifiedLookup (
401
+ DC, DSCE->getArg (), DeclNameRef (DRE->getDecl ()->getFullName ()),
402
+ candidates);
396
403
}
404
+ } else if (auto CRCE = dyn_cast<ConstructorRefCallExpr>(fnExpr)) {
405
+ collectPossibleCalleesByQualifiedLookup (
406
+ DC, CRCE->getArg (), DeclNameRef::createConstructor (), candidates);
397
407
}
398
408
399
- if (candidates.empty ()) {
400
- ConcreteDeclRef ref = nullptr ;
401
- auto fnType = getTypeOfCompletionContextExpr (
402
- DC.getASTContext (), &DC, CompletionTypeCheckKind::Normal, fnExpr, ref);
403
- if (!fnType)
404
- return false ;
409
+ if (!candidates.empty ())
410
+ return true ;
405
411
406
- if (auto *AFT = (*fnType)->getAs <AnyFunctionType>()) {
407
- candidates.emplace_back (AFT, ref.getDecl ());
408
- } else if (auto *AMT = (*fnType)->getAs <AnyMetatypeType>()) {
409
- auto baseTy = AMT->getInstanceType ();
410
- if (baseTy->mayHaveMembers ())
411
- collectPossibleCalleesByQualifiedLookup (
412
- DC, AMT, DeclNameRef::createConstructor (), candidates);
412
+ ConcreteDeclRef refDecl = nullptr ;
413
+ Type fnType = fnExpr->getType ();
414
+ if (fnType) {
415
+ refDecl = fnExpr->getReferencedDecl ();
416
+ if (!refDecl)
417
+ if (auto apply = dyn_cast<ApplyExpr>(fnExpr))
418
+ refDecl = apply->getFn ()->getReferencedDecl ();
419
+ }
420
+ if (!fnType) {
421
+ auto fnTypeOpt = getTypeOfCompletionContextExpr (
422
+ DC.getASTContext (), &DC, CompletionTypeCheckKind::Normal, fnExpr,
423
+ refDecl);
424
+ if (fnTypeOpt)
425
+ fnType = *fnTypeOpt;
426
+ }
427
+
428
+ if (!fnType || fnType->hasUnresolvedType () || fnType->hasError ())
429
+ return false ;
430
+ fnType = fnType->getWithoutSpecifierType ();
431
+
432
+ if (auto *AFT = fnType->getAs <AnyFunctionType>()) {
433
+ candidates.emplace_back (AFT, refDecl.getDecl ());
434
+ } else if (auto *AMT = fnType->getAs <AnyMetatypeType>()) {
435
+ auto baseTy = AMT->getInstanceType ();
436
+ if (isa<TypeExpr>(fnExpr) && baseTy->mayHaveMembers ()) {
437
+ collectPossibleCalleesByQualifiedLookup (
438
+ DC, AMT, DeclNameRef::createConstructor (), candidates);
413
439
}
414
440
}
415
441
@@ -454,7 +480,7 @@ static bool collectPossibleCalleesForUnresolvedMember(
454
480
members);
455
481
for (auto member : members) {
456
482
if (isReferenceableByImplicitMemberExpr (currModule, &DC, expectedTy,
457
- member.second ))
483
+ member.Decl ))
458
484
candidates.push_back (member);
459
485
}
460
486
}
@@ -549,12 +575,12 @@ class ExprContextAnalyzer {
549
575
SmallPtrSet<Identifier, 4 > seenNames;
550
576
for (auto &typeAndDecl : Candidates) {
551
577
DeclContext *memberDC = nullptr ;
552
- if (typeAndDecl.second )
553
- memberDC = typeAndDecl.second ->getInnermostDeclContext ();
578
+ if (typeAndDecl.Decl )
579
+ memberDC = typeAndDecl.Decl ->getInnermostDeclContext ();
554
580
555
- auto Params = typeAndDecl.first ->getParams ();
581
+ auto Params = typeAndDecl.Type ->getParams ();
556
582
ParameterList *paramList = nullptr ;
557
- if (auto VD = typeAndDecl.second ) {
583
+ if (auto VD = typeAndDecl.Decl ) {
558
584
if (auto FD = dyn_cast<AbstractFunctionDecl>(VD))
559
585
paramList = FD->getParameters ();
560
586
else if (auto SD = dyn_cast<SubscriptDecl>(VD))
0 commit comments