25
25
#include " swift/AST/LazyResolver.h"
26
26
#include " swift/AST/Requirement.h"
27
27
#include " swift/AST/TypeCheckRequests.h"
28
+ #include " swift/AST/TypeRepr.h"
28
29
#include " swift/Basic/Statistic.h"
29
30
#include " RequirementLowering.h"
30
31
#include < memory>
@@ -357,5 +358,110 @@ AbstractGenericSignatureRequestRQM::evaluate(
357
358
bool hadError = false ;
358
359
359
360
auto result = GenericSignature::get (genericParams, minimalRequirements);
361
+ return GenericSignatureWithError (result, hadError);
362
+ }
363
+
364
+ GenericSignatureWithError
365
+ InferredGenericSignatureRequestRQM::evaluate (
366
+ Evaluator &evaluator,
367
+ ModuleDecl *parentModule,
368
+ const GenericSignatureImpl *parentSigImpl,
369
+ GenericParamList *genericParamList,
370
+ WhereClauseOwner whereClause,
371
+ SmallVector<Requirement, 2 > addedRequirements,
372
+ SmallVector<TypeLoc, 2 > inferenceSources,
373
+ bool allowConcreteGenericParams) const {
374
+ GenericSignature parentSig (parentSigImpl);
375
+
376
+ SmallVector<GenericTypeParamType *, 4 > genericParams (
377
+ parentSig.getGenericParams ().begin (),
378
+ parentSig.getGenericParams ().end ());
379
+
380
+ SmallVector<StructuralRequirement, 4 > requirements;
381
+ for (const auto &req : parentSig.getRequirements ())
382
+ requirements.push_back ({req, SourceLoc (), /* wasInferred=*/ false });
383
+
384
+ const auto visitRequirement = [&](const Requirement &req,
385
+ RequirementRepr *reqRepr) {
386
+ realizeRequirement (req, reqRepr, /* infer=*/ true , requirements);
387
+ return false ;
388
+ };
389
+
390
+ if (genericParamList) {
391
+ // Extensions never have a parent signature.
392
+ assert (genericParamList->getOuterParameters () == nullptr || !parentSig);
393
+
394
+ // Collect all outer generic parameter lists.
395
+ SmallVector<GenericParamList *, 2 > gpLists;
396
+ for (auto *outerParamList = genericParamList;
397
+ outerParamList != nullptr ;
398
+ outerParamList = outerParamList->getOuterParameters ()) {
399
+ gpLists.push_back (outerParamList);
400
+ }
401
+
402
+ // The generic parameter lists must appear from innermost to outermost.
403
+ // We walk them backwards to order outer parameters before inner
404
+ // parameters.
405
+ for (auto *gpList : llvm::reverse (gpLists)) {
406
+ assert (gpList->size () > 0 &&
407
+ " Parsed an empty generic parameter list?" );
408
+
409
+ for (auto *gpDecl : *gpList) {
410
+ auto *gpType = gpDecl->getDeclaredInterfaceType ()
411
+ ->castTo <GenericTypeParamType>();
412
+ genericParams.push_back (gpType);
413
+
414
+ realizeInheritedRequirements (gpDecl, gpType, /* infer=*/ true ,
415
+ requirements);
416
+ }
417
+
418
+ // Add the generic parameter list's 'where' clause to the builder.
419
+ //
420
+ // The only time generic parameter lists have a 'where' clause is
421
+ // in SIL mode; all other generic declarations have a free-standing
422
+ // 'where' clause, which will be visited below.
423
+ WhereClauseOwner (parentModule, gpList)
424
+ .visitRequirements (TypeResolutionStage::Structural,
425
+ visitRequirement);
426
+ }
427
+ }
428
+
429
+ if (whereClause) {
430
+ std::move (whereClause).visitRequirements (
431
+ TypeResolutionStage::Structural,
432
+ visitRequirement);
433
+ }
434
+
435
+ // Perform requirement inference from function parameter and result
436
+ // types and such.
437
+ for (auto sourcePair : inferenceSources) {
438
+ auto *typeRepr = sourcePair.getTypeRepr ();
439
+ auto loc = typeRepr ? typeRepr->getStartLoc () : SourceLoc ();
440
+
441
+ inferRequirements (sourcePair.getType (), loc, requirements);
442
+ }
443
+
444
+ // Finish by adding any remaining requirements. This is used to introduce
445
+ // inferred same-type requirements when building the generic signature of
446
+ // an extension whose extended type is a generic typealias.
447
+ for (const auto &req : addedRequirements)
448
+ requirements.push_back ({req, SourceLoc (), /* wasInferred=*/ true });
449
+
450
+ // Heap-allocate the requirement machine to save stack space.
451
+ std::unique_ptr<RequirementMachine> machine (new RequirementMachine (
452
+ parentModule->getASTContext ().getRewriteContext ()));
453
+
454
+ machine->initWithWrittenRequirements (genericParams, requirements);
455
+
456
+ auto minimalRequirements =
457
+ machine->computeMinimalGenericSignatureRequirements ();
458
+
459
+ // FIXME: Implement this
460
+ bool hadError = false ;
461
+
462
+ auto result = GenericSignature::get (genericParams, minimalRequirements);
463
+
464
+ // FIXME: Handle allowConcreteGenericParams
465
+
360
466
return GenericSignatureWithError (result, hadError);
361
467
}
0 commit comments