xref: /llvm-project/clang/lib/Sema/SemaTemplateDeductionGuide.cpp (revision 4dd55c567aaed30c6842812e0798a70fee324c98)
1 //===- SemaTemplateDeductionGude.cpp - Template Argument Deduction---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements deduction guides for C++ class template argument
10 // deduction.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "TreeTransform.h"
15 #include "TypeLocBuilder.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclFriend.h"
22 #include "clang/AST/DeclTemplate.h"
23 #include "clang/AST/DeclarationName.h"
24 #include "clang/AST/Expr.h"
25 #include "clang/AST/ExprCXX.h"
26 #include "clang/AST/OperationKinds.h"
27 #include "clang/AST/RecursiveASTVisitor.h"
28 #include "clang/AST/TemplateBase.h"
29 #include "clang/AST/TemplateName.h"
30 #include "clang/AST/Type.h"
31 #include "clang/AST/TypeLoc.h"
32 #include "clang/Basic/LLVM.h"
33 #include "clang/Basic/SourceLocation.h"
34 #include "clang/Basic/Specifiers.h"
35 #include "clang/Basic/TypeTraits.h"
36 #include "clang/Sema/DeclSpec.h"
37 #include "clang/Sema/Initialization.h"
38 #include "clang/Sema/Lookup.h"
39 #include "clang/Sema/Overload.h"
40 #include "clang/Sema/Ownership.h"
41 #include "clang/Sema/Scope.h"
42 #include "clang/Sema/SemaInternal.h"
43 #include "clang/Sema/Template.h"
44 #include "clang/Sema/TemplateDeduction.h"
45 #include "llvm/ADT/ArrayRef.h"
46 #include "llvm/ADT/DenseSet.h"
47 #include "llvm/ADT/STLExtras.h"
48 #include "llvm/ADT/SmallVector.h"
49 #include "llvm/Support/Casting.h"
50 #include "llvm/Support/ErrorHandling.h"
51 #include <cassert>
52 #include <optional>
53 #include <utility>
54 
55 using namespace clang;
56 using namespace sema;
57 
58 namespace {
59 /// Tree transform to "extract" a transformed type from a class template's
60 /// constructor to a deduction guide.
61 class ExtractTypeForDeductionGuide
62     : public TreeTransform<ExtractTypeForDeductionGuide> {
63   llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs;
64   ClassTemplateDecl *NestedPattern;
65   const MultiLevelTemplateArgumentList *OuterInstantiationArgs;
66   std::optional<TemplateDeclInstantiator> TypedefNameInstantiator;
67 
68 public:
69   typedef TreeTransform<ExtractTypeForDeductionGuide> Base;
70   ExtractTypeForDeductionGuide(
71       Sema &SemaRef,
72       llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs,
73       ClassTemplateDecl *NestedPattern = nullptr,
74       const MultiLevelTemplateArgumentList *OuterInstantiationArgs = nullptr)
75       : Base(SemaRef), MaterializedTypedefs(MaterializedTypedefs),
76         NestedPattern(NestedPattern),
77         OuterInstantiationArgs(OuterInstantiationArgs) {
78     if (OuterInstantiationArgs)
79       TypedefNameInstantiator.emplace(
80           SemaRef, SemaRef.getASTContext().getTranslationUnitDecl(),
81           *OuterInstantiationArgs);
82   }
83 
84   TypeSourceInfo *transform(TypeSourceInfo *TSI) { return TransformType(TSI); }
85 
86   /// Returns true if it's safe to substitute \p Typedef with
87   /// \p OuterInstantiationArgs.
88   bool mightReferToOuterTemplateParameters(TypedefNameDecl *Typedef) {
89     if (!NestedPattern)
90       return false;
91 
92     static auto WalkUp = [](DeclContext *DC, DeclContext *TargetDC) {
93       if (DC->Equals(TargetDC))
94         return true;
95       while (DC->isRecord()) {
96         if (DC->Equals(TargetDC))
97           return true;
98         DC = DC->getParent();
99       }
100       return false;
101     };
102 
103     if (WalkUp(Typedef->getDeclContext(), NestedPattern->getTemplatedDecl()))
104       return true;
105     if (WalkUp(NestedPattern->getTemplatedDecl(), Typedef->getDeclContext()))
106       return true;
107     return false;
108   }
109 
110   QualType
111   RebuildTemplateSpecializationType(TemplateName Template,
112                                     SourceLocation TemplateNameLoc,
113                                     TemplateArgumentListInfo &TemplateArgs) {
114     if (!OuterInstantiationArgs ||
115         !isa_and_present<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()))
116       return Base::RebuildTemplateSpecializationType(Template, TemplateNameLoc,
117                                                      TemplateArgs);
118 
119     auto *TATD = cast<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
120     auto *Pattern = TATD;
121     while (Pattern->getInstantiatedFromMemberTemplate())
122       Pattern = Pattern->getInstantiatedFromMemberTemplate();
123     if (!mightReferToOuterTemplateParameters(Pattern->getTemplatedDecl()))
124       return Base::RebuildTemplateSpecializationType(Template, TemplateNameLoc,
125                                                      TemplateArgs);
126 
127     Decl *NewD =
128         TypedefNameInstantiator->InstantiateTypeAliasTemplateDecl(TATD);
129     if (!NewD)
130       return QualType();
131 
132     auto *NewTATD = cast<TypeAliasTemplateDecl>(NewD);
133     MaterializedTypedefs.push_back(NewTATD->getTemplatedDecl());
134 
135     return Base::RebuildTemplateSpecializationType(
136         TemplateName(NewTATD), TemplateNameLoc, TemplateArgs);
137   }
138 
139   QualType TransformTypedefType(TypeLocBuilder &TLB, TypedefTypeLoc TL) {
140     ASTContext &Context = SemaRef.getASTContext();
141     TypedefNameDecl *OrigDecl = TL.getTypedefNameDecl();
142     TypedefNameDecl *Decl = OrigDecl;
143     // Transform the underlying type of the typedef and clone the Decl only if
144     // the typedef has a dependent context.
145     bool InDependentContext = OrigDecl->getDeclContext()->isDependentContext();
146 
147     // A typedef/alias Decl within the NestedPattern may reference the outer
148     // template parameters. They're substituted with corresponding instantiation
149     // arguments here and in RebuildTemplateSpecializationType() above.
150     // Otherwise, we would have a CTAD guide with "dangling" template
151     // parameters.
152     // For example,
153     //   template <class T> struct Outer {
154     //     using Alias = S<T>;
155     //     template <class U> struct Inner {
156     //       Inner(Alias);
157     //     };
158     //   };
159     if (OuterInstantiationArgs && InDependentContext &&
160         TL.getTypePtr()->isInstantiationDependentType()) {
161       Decl = cast_if_present<TypedefNameDecl>(
162           TypedefNameInstantiator->InstantiateTypedefNameDecl(
163               OrigDecl, /*IsTypeAlias=*/isa<TypeAliasDecl>(OrigDecl)));
164       if (!Decl)
165         return QualType();
166       MaterializedTypedefs.push_back(Decl);
167     } else if (InDependentContext) {
168       TypeLocBuilder InnerTLB;
169       QualType Transformed =
170           TransformType(InnerTLB, OrigDecl->getTypeSourceInfo()->getTypeLoc());
171       TypeSourceInfo *TSI = InnerTLB.getTypeSourceInfo(Context, Transformed);
172       if (isa<TypeAliasDecl>(OrigDecl))
173         Decl = TypeAliasDecl::Create(
174             Context, Context.getTranslationUnitDecl(), OrigDecl->getBeginLoc(),
175             OrigDecl->getLocation(), OrigDecl->getIdentifier(), TSI);
176       else {
177         assert(isa<TypedefDecl>(OrigDecl) && "Not a Type alias or typedef");
178         Decl = TypedefDecl::Create(
179             Context, Context.getTranslationUnitDecl(), OrigDecl->getBeginLoc(),
180             OrigDecl->getLocation(), OrigDecl->getIdentifier(), TSI);
181       }
182       MaterializedTypedefs.push_back(Decl);
183     }
184 
185     QualType TDTy = Context.getTypedefType(Decl);
186     TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(TDTy);
187     TypedefTL.setNameLoc(TL.getNameLoc());
188 
189     return TDTy;
190   }
191 };
192 
193 // Build a deduction guide using the provided information.
194 //
195 // A deduction guide can be either a template or a non-template function
196 // declaration. If \p TemplateParams is null, a non-template function
197 // declaration will be created.
198 NamedDecl *buildDeductionGuide(
199     Sema &SemaRef, TemplateDecl *OriginalTemplate,
200     TemplateParameterList *TemplateParams, CXXConstructorDecl *Ctor,
201     ExplicitSpecifier ES, TypeSourceInfo *TInfo, SourceLocation LocStart,
202     SourceLocation Loc, SourceLocation LocEnd, bool IsImplicit,
203     llvm::ArrayRef<TypedefNameDecl *> MaterializedTypedefs = {}) {
204   DeclContext *DC = OriginalTemplate->getDeclContext();
205   auto DeductionGuideName =
206       SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(
207           OriginalTemplate);
208 
209   DeclarationNameInfo Name(DeductionGuideName, Loc);
210   ArrayRef<ParmVarDecl *> Params =
211       TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams();
212 
213   // Build the implicit deduction guide template.
214   auto *Guide =
215       CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, ES, Name,
216                                     TInfo->getType(), TInfo, LocEnd, Ctor);
217   Guide->setImplicit(IsImplicit);
218   Guide->setParams(Params);
219 
220   for (auto *Param : Params)
221     Param->setDeclContext(Guide);
222   for (auto *TD : MaterializedTypedefs)
223     TD->setDeclContext(Guide);
224   if (isa<CXXRecordDecl>(DC))
225     Guide->setAccess(AS_public);
226 
227   if (!TemplateParams) {
228     DC->addDecl(Guide);
229     return Guide;
230   }
231 
232   auto *GuideTemplate = FunctionTemplateDecl::Create(
233       SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide);
234   GuideTemplate->setImplicit(IsImplicit);
235   Guide->setDescribedFunctionTemplate(GuideTemplate);
236 
237   if (isa<CXXRecordDecl>(DC))
238     GuideTemplate->setAccess(AS_public);
239 
240   DC->addDecl(GuideTemplate);
241   return GuideTemplate;
242 }
243 
244 // Transform a given template type parameter `TTP`.
245 TemplateTypeParmDecl *transformTemplateTypeParam(
246     Sema &SemaRef, DeclContext *DC, TemplateTypeParmDecl *TTP,
247     MultiLevelTemplateArgumentList &Args, unsigned NewDepth, unsigned NewIndex,
248     bool EvaluateConstraint) {
249   // TemplateTypeParmDecl's index cannot be changed after creation, so
250   // substitute it directly.
251   auto *NewTTP = TemplateTypeParmDecl::Create(
252       SemaRef.Context, DC, TTP->getBeginLoc(), TTP->getLocation(), NewDepth,
253       NewIndex, TTP->getIdentifier(), TTP->wasDeclaredWithTypename(),
254       TTP->isParameterPack(), TTP->hasTypeConstraint(),
255       TTP->isExpandedParameterPack()
256           ? std::optional<unsigned>(TTP->getNumExpansionParameters())
257           : std::nullopt);
258   if (const auto *TC = TTP->getTypeConstraint())
259     SemaRef.SubstTypeConstraint(NewTTP, TC, Args,
260                                 /*EvaluateConstraint=*/EvaluateConstraint);
261   if (TTP->hasDefaultArgument()) {
262     TemplateArgumentLoc InstantiatedDefaultArg;
263     if (!SemaRef.SubstTemplateArgument(
264             TTP->getDefaultArgument(), Args, InstantiatedDefaultArg,
265             TTP->getDefaultArgumentLoc(), TTP->getDeclName()))
266       NewTTP->setDefaultArgument(SemaRef.Context, InstantiatedDefaultArg);
267   }
268   SemaRef.CurrentInstantiationScope->InstantiatedLocal(TTP, NewTTP);
269   return NewTTP;
270 }
271 // Similar to above, but for non-type template or template template parameters.
272 template <typename NonTypeTemplateOrTemplateTemplateParmDecl>
273 NonTypeTemplateOrTemplateTemplateParmDecl *
274 transformTemplateParam(Sema &SemaRef, DeclContext *DC,
275                        NonTypeTemplateOrTemplateTemplateParmDecl *OldParam,
276                        MultiLevelTemplateArgumentList &Args, unsigned NewIndex,
277                        unsigned NewDepth) {
278   // Ask the template instantiator to do the heavy lifting for us, then adjust
279   // the index of the parameter once it's done.
280   auto *NewParam = cast<NonTypeTemplateOrTemplateTemplateParmDecl>(
281       SemaRef.SubstDecl(OldParam, DC, Args));
282   NewParam->setPosition(NewIndex);
283   NewParam->setDepth(NewDepth);
284   return NewParam;
285 }
286 
287 NamedDecl *transformTemplateParameter(Sema &SemaRef, DeclContext *DC,
288                                       NamedDecl *TemplateParam,
289                                       MultiLevelTemplateArgumentList &Args,
290                                       unsigned NewIndex, unsigned NewDepth,
291                                       bool EvaluateConstraint = true) {
292   if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
293     return transformTemplateTypeParam(
294         SemaRef, DC, TTP, Args, NewDepth, NewIndex,
295         /*EvaluateConstraint=*/EvaluateConstraint);
296   if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
297     return transformTemplateParam(SemaRef, DC, TTP, Args, NewIndex, NewDepth);
298   if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
299     return transformTemplateParam(SemaRef, DC, NTTP, Args, NewIndex, NewDepth);
300   llvm_unreachable("Unhandled template parameter types");
301 }
302 
303 /// Transform to convert portions of a constructor declaration into the
304 /// corresponding deduction guide, per C++1z [over.match.class.deduct]p1.
305 struct ConvertConstructorToDeductionGuideTransform {
306   ConvertConstructorToDeductionGuideTransform(Sema &S,
307                                               ClassTemplateDecl *Template)
308       : SemaRef(S), Template(Template) {
309     // If the template is nested, then we need to use the original
310     // pattern to iterate over the constructors.
311     ClassTemplateDecl *Pattern = Template;
312     while (Pattern->getInstantiatedFromMemberTemplate()) {
313       if (Pattern->isMemberSpecialization())
314         break;
315       Pattern = Pattern->getInstantiatedFromMemberTemplate();
316       NestedPattern = Pattern;
317     }
318 
319     if (NestedPattern)
320       OuterInstantiationArgs = SemaRef.getTemplateInstantiationArgs(Template);
321   }
322 
323   Sema &SemaRef;
324   ClassTemplateDecl *Template;
325   ClassTemplateDecl *NestedPattern = nullptr;
326 
327   DeclContext *DC = Template->getDeclContext();
328   CXXRecordDecl *Primary = Template->getTemplatedDecl();
329   DeclarationName DeductionGuideName =
330       SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template);
331 
332   QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary);
333 
334   // Index adjustment to apply to convert depth-1 template parameters into
335   // depth-0 template parameters.
336   unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size();
337 
338   // Instantiation arguments for the outermost depth-1 templates
339   // when the template is nested
340   MultiLevelTemplateArgumentList OuterInstantiationArgs;
341 
342   /// Transform a constructor declaration into a deduction guide.
343   NamedDecl *transformConstructor(FunctionTemplateDecl *FTD,
344                                   CXXConstructorDecl *CD) {
345     SmallVector<TemplateArgument, 16> SubstArgs;
346 
347     LocalInstantiationScope Scope(SemaRef);
348 
349     // C++ [over.match.class.deduct]p1:
350     // -- For each constructor of the class template designated by the
351     //    template-name, a function template with the following properties:
352 
353     //    -- The template parameters are the template parameters of the class
354     //       template followed by the template parameters (including default
355     //       template arguments) of the constructor, if any.
356     TemplateParameterList *TemplateParams =
357         SemaRef.GetTemplateParameterList(Template);
358     if (FTD) {
359       TemplateParameterList *InnerParams = FTD->getTemplateParameters();
360       SmallVector<NamedDecl *, 16> AllParams;
361       SmallVector<TemplateArgument, 16> Depth1Args;
362       AllParams.reserve(TemplateParams->size() + InnerParams->size());
363       AllParams.insert(AllParams.begin(), TemplateParams->begin(),
364                        TemplateParams->end());
365       SubstArgs.reserve(InnerParams->size());
366       Depth1Args.reserve(InnerParams->size());
367 
368       // Later template parameters could refer to earlier ones, so build up
369       // a list of substituted template arguments as we go.
370       for (NamedDecl *Param : *InnerParams) {
371         MultiLevelTemplateArgumentList Args;
372         Args.setKind(TemplateSubstitutionKind::Rewrite);
373         Args.addOuterTemplateArguments(Depth1Args);
374         Args.addOuterRetainedLevel();
375         if (NestedPattern)
376           Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
377         auto [Depth, Index] = getDepthAndIndex(Param);
378         NamedDecl *NewParam = transformTemplateParameter(
379             SemaRef, DC, Param, Args, Index + Depth1IndexAdjustment, Depth - 1);
380         if (!NewParam)
381           return nullptr;
382         // Constraints require that we substitute depth-1 arguments
383         // to match depths when substituted for evaluation later
384         Depth1Args.push_back(SemaRef.Context.getInjectedTemplateArg(NewParam));
385 
386         if (NestedPattern) {
387           auto [Depth, Index] = getDepthAndIndex(NewParam);
388           NewParam = transformTemplateParameter(
389               SemaRef, DC, NewParam, OuterInstantiationArgs, Index,
390               Depth - OuterInstantiationArgs.getNumSubstitutedLevels(),
391               /*EvaluateConstraint=*/false);
392         }
393 
394         assert(NewParam->getTemplateDepth() == 0 &&
395                "Unexpected template parameter depth");
396 
397         AllParams.push_back(NewParam);
398         SubstArgs.push_back(SemaRef.Context.getInjectedTemplateArg(NewParam));
399       }
400 
401       // Substitute new template parameters into requires-clause if present.
402       Expr *RequiresClause = nullptr;
403       if (Expr *InnerRC = InnerParams->getRequiresClause()) {
404         MultiLevelTemplateArgumentList Args;
405         Args.setKind(TemplateSubstitutionKind::Rewrite);
406         Args.addOuterTemplateArguments(Depth1Args);
407         Args.addOuterRetainedLevel();
408         if (NestedPattern)
409           Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
410         ExprResult E = SemaRef.SubstExpr(InnerRC, Args);
411         if (E.isInvalid())
412           return nullptr;
413         RequiresClause = E.getAs<Expr>();
414       }
415 
416       TemplateParams = TemplateParameterList::Create(
417           SemaRef.Context, InnerParams->getTemplateLoc(),
418           InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(),
419           RequiresClause);
420     }
421 
422     // If we built a new template-parameter-list, track that we need to
423     // substitute references to the old parameters into references to the
424     // new ones.
425     MultiLevelTemplateArgumentList Args;
426     Args.setKind(TemplateSubstitutionKind::Rewrite);
427     if (FTD) {
428       Args.addOuterTemplateArguments(SubstArgs);
429       Args.addOuterRetainedLevel();
430     }
431 
432     FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()
433                                     ->getTypeLoc()
434                                     .getAsAdjusted<FunctionProtoTypeLoc>();
435     assert(FPTL && "no prototype for constructor declaration");
436 
437     // Transform the type of the function, adjusting the return type and
438     // replacing references to the old parameters with references to the
439     // new ones.
440     TypeLocBuilder TLB;
441     SmallVector<ParmVarDecl *, 8> Params;
442     SmallVector<TypedefNameDecl *, 4> MaterializedTypedefs;
443     QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args,
444                                                   MaterializedTypedefs);
445     if (NewType.isNull())
446       return nullptr;
447     TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType);
448 
449     return buildDeductionGuide(
450         SemaRef, Template, TemplateParams, CD, CD->getExplicitSpecifier(),
451         NewTInfo, CD->getBeginLoc(), CD->getLocation(), CD->getEndLoc(),
452         /*IsImplicit=*/true, MaterializedTypedefs);
453   }
454 
455   /// Build a deduction guide with the specified parameter types.
456   NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) {
457     SourceLocation Loc = Template->getLocation();
458 
459     // Build the requested type.
460     FunctionProtoType::ExtProtoInfo EPI;
461     EPI.HasTrailingReturn = true;
462     QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc,
463                                                 DeductionGuideName, EPI);
464     TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc);
465     if (NestedPattern)
466       TSI = SemaRef.SubstType(TSI, OuterInstantiationArgs, Loc,
467                               DeductionGuideName);
468 
469     if (!TSI)
470       return nullptr;
471 
472     FunctionProtoTypeLoc FPTL =
473         TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>();
474 
475     // Build the parameters, needed during deduction / substitution.
476     SmallVector<ParmVarDecl *, 4> Params;
477     for (auto T : ParamTypes) {
478       auto *TSI = SemaRef.Context.getTrivialTypeSourceInfo(T, Loc);
479       if (NestedPattern)
480         TSI = SemaRef.SubstType(TSI, OuterInstantiationArgs, Loc,
481                                 DeclarationName());
482       if (!TSI)
483         return nullptr;
484 
485       ParmVarDecl *NewParam =
486           ParmVarDecl::Create(SemaRef.Context, DC, Loc, Loc, nullptr,
487                               TSI->getType(), TSI, SC_None, nullptr);
488       NewParam->setScopeInfo(0, Params.size());
489       FPTL.setParam(Params.size(), NewParam);
490       Params.push_back(NewParam);
491     }
492 
493     return buildDeductionGuide(
494         SemaRef, Template, SemaRef.GetTemplateParameterList(Template), nullptr,
495         ExplicitSpecifier(), TSI, Loc, Loc, Loc, /*IsImplicit=*/true);
496   }
497 
498 private:
499   QualType transformFunctionProtoType(
500       TypeLocBuilder &TLB, FunctionProtoTypeLoc TL,
501       SmallVectorImpl<ParmVarDecl *> &Params,
502       MultiLevelTemplateArgumentList &Args,
503       SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs) {
504     SmallVector<QualType, 4> ParamTypes;
505     const FunctionProtoType *T = TL.getTypePtr();
506 
507     //    -- The types of the function parameters are those of the constructor.
508     for (auto *OldParam : TL.getParams()) {
509       ParmVarDecl *NewParam = OldParam;
510       // Given
511       //   template <class T> struct C {
512       //     template <class U> struct D {
513       //       template <class V> D(U, V);
514       //     };
515       //   };
516       // First, transform all the references to template parameters that are
517       // defined outside of the surrounding class template. That is T in the
518       // above example.
519       if (NestedPattern) {
520         NewParam = transformFunctionTypeParam(
521             NewParam, OuterInstantiationArgs, MaterializedTypedefs,
522             /*TransformingOuterPatterns=*/true);
523         if (!NewParam)
524           return QualType();
525       }
526       // Then, transform all the references to template parameters that are
527       // defined at the class template and the constructor. In this example,
528       // they're U and V, respectively.
529       NewParam =
530           transformFunctionTypeParam(NewParam, Args, MaterializedTypedefs,
531                                      /*TransformingOuterPatterns=*/false);
532       if (!NewParam)
533         return QualType();
534       ParamTypes.push_back(NewParam->getType());
535       Params.push_back(NewParam);
536     }
537 
538     //    -- The return type is the class template specialization designated by
539     //       the template-name and template arguments corresponding to the
540     //       template parameters obtained from the class template.
541     //
542     // We use the injected-class-name type of the primary template instead.
543     // This has the convenient property that it is different from any type that
544     // the user can write in a deduction-guide (because they cannot enter the
545     // context of the template), so implicit deduction guides can never collide
546     // with explicit ones.
547     QualType ReturnType = DeducedType;
548     TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation());
549 
550     // Resolving a wording defect, we also inherit the variadicness of the
551     // constructor.
552     FunctionProtoType::ExtProtoInfo EPI;
553     EPI.Variadic = T->isVariadic();
554     EPI.HasTrailingReturn = true;
555 
556     QualType Result = SemaRef.BuildFunctionType(
557         ReturnType, ParamTypes, TL.getBeginLoc(), DeductionGuideName, EPI);
558     if (Result.isNull())
559       return QualType();
560 
561     FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result);
562     NewTL.setLocalRangeBegin(TL.getLocalRangeBegin());
563     NewTL.setLParenLoc(TL.getLParenLoc());
564     NewTL.setRParenLoc(TL.getRParenLoc());
565     NewTL.setExceptionSpecRange(SourceRange());
566     NewTL.setLocalRangeEnd(TL.getLocalRangeEnd());
567     for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I)
568       NewTL.setParam(I, Params[I]);
569 
570     return Result;
571   }
572 
573   ParmVarDecl *transformFunctionTypeParam(
574       ParmVarDecl *OldParam, MultiLevelTemplateArgumentList &Args,
575       llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs,
576       bool TransformingOuterPatterns) {
577     TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo();
578     TypeSourceInfo *NewDI;
579     if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) {
580       // Expand out the one and only element in each inner pack.
581       Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0);
582       NewDI =
583           SemaRef.SubstType(PackTL.getPatternLoc(), Args,
584                             OldParam->getLocation(), OldParam->getDeclName());
585       if (!NewDI)
586         return nullptr;
587       NewDI =
588           SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(),
589                                      PackTL.getTypePtr()->getNumExpansions());
590     } else
591       NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(),
592                                 OldParam->getDeclName());
593     if (!NewDI)
594       return nullptr;
595 
596     // Extract the type. This (for instance) replaces references to typedef
597     // members of the current instantiations with the definitions of those
598     // typedefs, avoiding triggering instantiation of the deduced type during
599     // deduction.
600     NewDI = ExtractTypeForDeductionGuide(
601                 SemaRef, MaterializedTypedefs, NestedPattern,
602                 TransformingOuterPatterns ? &Args : nullptr)
603                 .transform(NewDI);
604 
605     // Resolving a wording defect, we also inherit default arguments from the
606     // constructor.
607     ExprResult NewDefArg;
608     if (OldParam->hasDefaultArg()) {
609       // We don't care what the value is (we won't use it); just create a
610       // placeholder to indicate there is a default argument.
611       QualType ParamTy = NewDI->getType();
612       NewDefArg = new (SemaRef.Context)
613           OpaqueValueExpr(OldParam->getDefaultArgRange().getBegin(),
614                           ParamTy.getNonLValueExprType(SemaRef.Context),
615                           ParamTy->isLValueReferenceType()   ? VK_LValue
616                           : ParamTy->isRValueReferenceType() ? VK_XValue
617                                                              : VK_PRValue);
618     }
619     // Handle arrays and functions decay.
620     auto NewType = NewDI->getType();
621     if (NewType->isArrayType() || NewType->isFunctionType())
622       NewType = SemaRef.Context.getDecayedType(NewType);
623 
624     ParmVarDecl *NewParam = ParmVarDecl::Create(
625         SemaRef.Context, DC, OldParam->getInnerLocStart(),
626         OldParam->getLocation(), OldParam->getIdentifier(), NewType, NewDI,
627         OldParam->getStorageClass(), NewDefArg.get());
628     NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(),
629                            OldParam->getFunctionScopeIndex());
630     SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, NewParam);
631     return NewParam;
632   }
633 };
634 
635 // Find all template parameters that appear in the given DeducedArgs.
636 // Return the indices of the template parameters in the TemplateParams.
637 SmallVector<unsigned> TemplateParamsReferencedInTemplateArgumentList(
638     const TemplateParameterList *TemplateParamsList,
639     ArrayRef<TemplateArgument> DeducedArgs) {
640   struct TemplateParamsReferencedFinder
641       : public RecursiveASTVisitor<TemplateParamsReferencedFinder> {
642     const TemplateParameterList *TemplateParamList;
643     llvm::BitVector ReferencedTemplateParams;
644 
645     TemplateParamsReferencedFinder(
646         const TemplateParameterList *TemplateParamList)
647         : TemplateParamList(TemplateParamList),
648           ReferencedTemplateParams(TemplateParamList->size()) {}
649 
650     bool VisitTemplateTypeParmType(TemplateTypeParmType *TTP) {
651       // We use the index and depth to retrieve the corresponding template
652       // parameter from the parameter list, which is more robost.
653       Mark(TTP->getDepth(), TTP->getIndex());
654       return true;
655     }
656 
657     bool VisitDeclRefExpr(DeclRefExpr *DRE) {
658       MarkAppeared(DRE->getFoundDecl());
659       return true;
660     }
661 
662     bool TraverseTemplateName(TemplateName Template) {
663       if (auto *TD = Template.getAsTemplateDecl())
664         MarkAppeared(TD);
665       return RecursiveASTVisitor::TraverseTemplateName(Template);
666     }
667 
668     void MarkAppeared(NamedDecl *ND) {
669       if (llvm::isa<NonTypeTemplateParmDecl, TemplateTypeParmDecl,
670                     TemplateTemplateParmDecl>(ND)) {
671         auto [Depth, Index] = getDepthAndIndex(ND);
672         Mark(Depth, Index);
673       }
674     }
675     void Mark(unsigned Depth, unsigned Index) {
676       if (Index < TemplateParamList->size() &&
677           TemplateParamList->getParam(Index)->getTemplateDepth() == Depth)
678         ReferencedTemplateParams.set(Index);
679     }
680   };
681   TemplateParamsReferencedFinder Finder(TemplateParamsList);
682   Finder.TraverseTemplateArguments(DeducedArgs);
683 
684   SmallVector<unsigned> Results;
685   for (unsigned Index = 0; Index < TemplateParamsList->size(); ++Index) {
686     if (Finder.ReferencedTemplateParams[Index])
687       Results.push_back(Index);
688   }
689   return Results;
690 }
691 
692 bool hasDeclaredDeductionGuides(DeclarationName Name, DeclContext *DC) {
693   // Check whether we've already declared deduction guides for this template.
694   // FIXME: Consider storing a flag on the template to indicate this.
695   assert(Name.getNameKind() ==
696              DeclarationName::NameKind::CXXDeductionGuideName &&
697          "name must be a deduction guide name");
698   auto Existing = DC->lookup(Name);
699   for (auto *D : Existing)
700     if (D->isImplicit())
701       return true;
702   return false;
703 }
704 
705 // Build the associated constraints for the alias deduction guides.
706 // C++ [over.match.class.deduct]p3.3:
707 //   The associated constraints ([temp.constr.decl]) are the conjunction of the
708 //   associated constraints of g and a constraint that is satisfied if and only
709 //   if the arguments of A are deducible (see below) from the return type.
710 //
711 // The return result is expected to be the require-clause for the synthesized
712 // alias deduction guide.
713 Expr *
714 buildAssociatedConstraints(Sema &SemaRef, FunctionTemplateDecl *F,
715                            TypeAliasTemplateDecl *AliasTemplate,
716                            ArrayRef<DeducedTemplateArgument> DeduceResults,
717                            unsigned FirstUndeducedParamIdx, Expr *IsDeducible) {
718   Expr *RC = F->getTemplateParameters()->getRequiresClause();
719   if (!RC)
720     return IsDeducible;
721 
722   ASTContext &Context = SemaRef.Context;
723   LocalInstantiationScope Scope(SemaRef);
724 
725   // In the clang AST, constraint nodes are deliberately not instantiated unless
726   // they are actively being evaluated. Consequently, occurrences of template
727   // parameters in the require-clause expression have a subtle "depth"
728   // difference compared to normal occurrences in places, such as function
729   // parameters. When transforming the require-clause, we must take this
730   // distinction into account:
731   //
732   //   1) In the transformed require-clause, occurrences of template parameters
733   //   must use the "uninstantiated" depth;
734   //   2) When substituting on the require-clause expr of the underlying
735   //   deduction guide, we must use the entire set of template argument lists;
736   //
737   // It's important to note that we're performing this transformation on an
738   // *instantiated* AliasTemplate.
739 
740   // For 1), if the alias template is nested within a class template, we
741   // calcualte the 'uninstantiated' depth by adding the substitution level back.
742   unsigned AdjustDepth = 0;
743   if (auto *PrimaryTemplate =
744           AliasTemplate->getInstantiatedFromMemberTemplate())
745     AdjustDepth = PrimaryTemplate->getTemplateDepth();
746 
747   // We rebuild all template parameters with the uninstantiated depth, and
748   // build template arguments refer to them.
749   SmallVector<TemplateArgument> AdjustedAliasTemplateArgs;
750 
751   for (auto *TP : *AliasTemplate->getTemplateParameters()) {
752     // Rebuild any internal references to earlier parameters and reindex
753     // as we go.
754     MultiLevelTemplateArgumentList Args;
755     Args.setKind(TemplateSubstitutionKind::Rewrite);
756     Args.addOuterTemplateArguments(AdjustedAliasTemplateArgs);
757     NamedDecl *NewParam = transformTemplateParameter(
758         SemaRef, AliasTemplate->getDeclContext(), TP, Args,
759         /*NewIndex=*/AdjustedAliasTemplateArgs.size(),
760         getDepthAndIndex(TP).first + AdjustDepth);
761 
762     TemplateArgument NewTemplateArgument =
763         Context.getInjectedTemplateArg(NewParam);
764     AdjustedAliasTemplateArgs.push_back(NewTemplateArgument);
765   }
766   // Template arguments used to transform the template arguments in
767   // DeducedResults.
768   SmallVector<TemplateArgument> InnerArgsForBuildingRC(
769       F->getTemplateParameters()->size());
770   // Transform the transformed template args
771   MultiLevelTemplateArgumentList Args;
772   Args.setKind(TemplateSubstitutionKind::Rewrite);
773   Args.addOuterTemplateArguments(AdjustedAliasTemplateArgs);
774 
775   for (unsigned Index = 0; Index < DeduceResults.size(); ++Index) {
776     const auto &D = DeduceResults[Index];
777     if (D.isNull()) { // non-deduced template parameters of f
778       NamedDecl *TP = F->getTemplateParameters()->getParam(Index);
779       MultiLevelTemplateArgumentList Args;
780       Args.setKind(TemplateSubstitutionKind::Rewrite);
781       Args.addOuterTemplateArguments(InnerArgsForBuildingRC);
782       // Rebuild the template parameter with updated depth and index.
783       NamedDecl *NewParam =
784           transformTemplateParameter(SemaRef, F->getDeclContext(), TP, Args,
785                                      /*NewIndex=*/FirstUndeducedParamIdx,
786                                      getDepthAndIndex(TP).first + AdjustDepth);
787       FirstUndeducedParamIdx += 1;
788       assert(InnerArgsForBuildingRC[Index].isNull());
789       InnerArgsForBuildingRC[Index] = Context.getInjectedTemplateArg(NewParam);
790       continue;
791     }
792     TemplateArgumentLoc Input =
793         SemaRef.getTrivialTemplateArgumentLoc(D, QualType(), SourceLocation{});
794     TemplateArgumentLoc Output;
795     if (!SemaRef.SubstTemplateArgument(Input, Args, Output)) {
796       assert(InnerArgsForBuildingRC[Index].isNull() &&
797              "InstantiatedArgs must be null before setting");
798       InnerArgsForBuildingRC[Index] = Output.getArgument();
799     }
800   }
801 
802   // A list of template arguments for transforming the require-clause using
803   // the transformed template arguments as the template argument list of F.
804   //
805   // For 2), if the underlying deduction guide F is nested in a class template,
806   // we need the entire template argument list, as the constraint AST in the
807   // require-clause of F remains completely uninstantiated.
808   //
809   // For example:
810   //   template <typename T> // depth 0
811   //   struct Outer {
812   //      template <typename U>
813   //      struct Foo { Foo(U); };
814   //
815   //      template <typename U> // depth 1
816   //      requires C<U>
817   //      Foo(U) -> Foo<int>;
818   //   };
819   //   template <typename U>
820   //   using AFoo = Outer<int>::Foo<U>;
821   //
822   // In this scenario, the deduction guide for `Foo` inside `Outer<int>`:
823   //   - The occurrence of U in the require-expression is [depth:1, index:0]
824   //   - The occurrence of U in the function parameter is [depth:0, index:0]
825   //   - The template parameter of U is [depth:0, index:0]
826   //
827   // NOTE: The underlying deduction guide F is instantiated -- either from an
828   // explicitly-written deduction guide member, or from a constructor.
829   MultiLevelTemplateArgumentList ArgsForBuildingRC =
830       SemaRef.getTemplateInstantiationArgs(F, F->getLexicalDeclContext(),
831                                            /*Final=*/false,
832                                            /*Innermost=*/InnerArgsForBuildingRC,
833                                            /*RelativeToPrimary=*/true,
834                                            /*ForConstraintInstantiation=*/true);
835   ArgsForBuildingRC.setKind(clang::TemplateSubstitutionKind::Rewrite);
836 
837   ExprResult E = SemaRef.SubstExpr(RC, ArgsForBuildingRC);
838   if (E.isInvalid())
839     return nullptr;
840 
841   auto Conjunction =
842       SemaRef.BuildBinOp(SemaRef.getCurScope(), SourceLocation{},
843                          BinaryOperatorKind::BO_LAnd, E.get(), IsDeducible);
844   if (Conjunction.isInvalid())
845     return nullptr;
846   return Conjunction.getAs<Expr>();
847 }
848 // Build the is_deducible constraint for the alias deduction guides.
849 // [over.match.class.deduct]p3.3:
850 //    ... and a constraint that is satisfied if and only if the arguments
851 //    of A are deducible (see below) from the return type.
852 Expr *buildIsDeducibleConstraint(Sema &SemaRef,
853                                  TypeAliasTemplateDecl *AliasTemplate,
854                                  QualType ReturnType,
855                                  SmallVector<NamedDecl *> TemplateParams) {
856   ASTContext &Context = SemaRef.Context;
857   // Constraint AST nodes must use uninstantiated depth.
858   if (auto *PrimaryTemplate =
859           AliasTemplate->getInstantiatedFromMemberTemplate();
860       PrimaryTemplate && TemplateParams.size() > 0) {
861     LocalInstantiationScope Scope(SemaRef);
862 
863     // Adjust the depth for TemplateParams.
864     unsigned AdjustDepth = PrimaryTemplate->getTemplateDepth();
865     SmallVector<TemplateArgument> TransformedTemplateArgs;
866     for (auto *TP : TemplateParams) {
867       // Rebuild any internal references to earlier parameters and reindex
868       // as we go.
869       MultiLevelTemplateArgumentList Args;
870       Args.setKind(TemplateSubstitutionKind::Rewrite);
871       Args.addOuterTemplateArguments(TransformedTemplateArgs);
872       NamedDecl *NewParam = transformTemplateParameter(
873           SemaRef, AliasTemplate->getDeclContext(), TP, Args,
874           /*NewIndex=*/TransformedTemplateArgs.size(),
875           getDepthAndIndex(TP).first + AdjustDepth);
876 
877       TemplateArgument NewTemplateArgument =
878           Context.getInjectedTemplateArg(NewParam);
879       TransformedTemplateArgs.push_back(NewTemplateArgument);
880     }
881     // Transformed the ReturnType to restore the uninstantiated depth.
882     MultiLevelTemplateArgumentList Args;
883     Args.setKind(TemplateSubstitutionKind::Rewrite);
884     Args.addOuterTemplateArguments(TransformedTemplateArgs);
885     ReturnType = SemaRef.SubstType(
886         ReturnType, Args, AliasTemplate->getLocation(),
887         Context.DeclarationNames.getCXXDeductionGuideName(AliasTemplate));
888   };
889 
890   SmallVector<TypeSourceInfo *> IsDeducibleTypeTraitArgs = {
891       Context.getTrivialTypeSourceInfo(
892           Context.getDeducedTemplateSpecializationType(
893               TemplateName(AliasTemplate), /*DeducedType=*/QualType(),
894               /*IsDependent=*/true)), // template specialization type whose
895                                       // arguments will be deduced.
896       Context.getTrivialTypeSourceInfo(
897           ReturnType), // type from which template arguments are deduced.
898   };
899   return TypeTraitExpr::Create(
900       Context, Context.getLogicalOperationType(), AliasTemplate->getLocation(),
901       TypeTrait::BTT_IsDeducible, IsDeducibleTypeTraitArgs,
902       AliasTemplate->getLocation(), /*Value*/ false);
903 }
904 
905 std::pair<TemplateDecl *, llvm::ArrayRef<TemplateArgument>>
906 getRHSTemplateDeclAndArgs(Sema &SemaRef, TypeAliasTemplateDecl *AliasTemplate) {
907   // Unwrap the sugared ElaboratedType.
908   auto RhsType = AliasTemplate->getTemplatedDecl()
909                      ->getUnderlyingType()
910                      .getSingleStepDesugaredType(SemaRef.Context);
911   TemplateDecl *Template = nullptr;
912   llvm::ArrayRef<TemplateArgument> AliasRhsTemplateArgs;
913   if (const auto *TST = RhsType->getAs<TemplateSpecializationType>()) {
914     // Cases where the RHS of the alias is dependent. e.g.
915     //   template<typename T>
916     //   using AliasFoo1 = Foo<T>; // a class/type alias template specialization
917     Template = TST->getTemplateName().getAsTemplateDecl();
918     AliasRhsTemplateArgs = TST->template_arguments();
919   } else if (const auto *RT = RhsType->getAs<RecordType>()) {
920     // Cases where template arguments in the RHS of the alias are not
921     // dependent. e.g.
922     //   using AliasFoo = Foo<bool>;
923     if (const auto *CTSD = llvm::dyn_cast<ClassTemplateSpecializationDecl>(
924             RT->getAsCXXRecordDecl())) {
925       Template = CTSD->getSpecializedTemplate();
926       AliasRhsTemplateArgs = CTSD->getTemplateArgs().asArray();
927     }
928   } else {
929     assert(false && "unhandled RHS type of the alias");
930   }
931   return {Template, AliasRhsTemplateArgs};
932 }
933 
934 // Build deduction guides for a type alias template from the given underlying
935 // deduction guide F.
936 FunctionTemplateDecl *
937 BuildDeductionGuideForTypeAlias(Sema &SemaRef,
938                                 TypeAliasTemplateDecl *AliasTemplate,
939                                 FunctionTemplateDecl *F, SourceLocation Loc) {
940   LocalInstantiationScope Scope(SemaRef);
941   Sema::InstantiatingTemplate BuildingDeductionGuides(
942       SemaRef, AliasTemplate->getLocation(), F,
943       Sema::InstantiatingTemplate::BuildingDeductionGuidesTag{});
944   if (BuildingDeductionGuides.isInvalid())
945     return nullptr;
946 
947   auto &Context = SemaRef.Context;
948   auto [Template, AliasRhsTemplateArgs] =
949       getRHSTemplateDeclAndArgs(SemaRef, AliasTemplate);
950 
951   auto RType = F->getTemplatedDecl()->getReturnType();
952   // The (trailing) return type of the deduction guide.
953   const TemplateSpecializationType *FReturnType =
954       RType->getAs<TemplateSpecializationType>();
955   if (const auto *InjectedCNT = RType->getAs<InjectedClassNameType>())
956     // implicitly-generated deduction guide.
957     FReturnType = InjectedCNT->getInjectedTST();
958   else if (const auto *ET = RType->getAs<ElaboratedType>())
959     // explicit deduction guide.
960     FReturnType = ET->getNamedType()->getAs<TemplateSpecializationType>();
961   assert(FReturnType && "expected to see a return type");
962   // Deduce template arguments of the deduction guide f from the RHS of
963   // the alias.
964   //
965   // C++ [over.match.class.deduct]p3: ...For each function or function
966   // template f in the guides of the template named by the
967   // simple-template-id of the defining-type-id, the template arguments
968   // of the return type of f are deduced from the defining-type-id of A
969   // according to the process in [temp.deduct.type] with the exception
970   // that deduction does not fail if not all template arguments are
971   // deduced.
972   //
973   //
974   //  template<typename X, typename Y>
975   //  f(X, Y) -> f<Y, X>;
976   //
977   //  template<typename U>
978   //  using alias = f<int, U>;
979   //
980   // The RHS of alias is f<int, U>, we deduced the template arguments of
981   // the return type of the deduction guide from it: Y->int, X->U
982   sema::TemplateDeductionInfo TDeduceInfo(Loc);
983   // Must initialize n elements, this is required by DeduceTemplateArguments.
984   SmallVector<DeducedTemplateArgument> DeduceResults(
985       F->getTemplateParameters()->size());
986 
987   // FIXME: DeduceTemplateArguments stops immediately at the first
988   // non-deducible template argument. However, this doesn't seem to casue
989   // issues for practice cases, we probably need to extend it to continue
990   // performing deduction for rest of arguments to align with the C++
991   // standard.
992   SemaRef.DeduceTemplateArguments(
993       F->getTemplateParameters(), FReturnType->template_arguments(),
994       AliasRhsTemplateArgs, TDeduceInfo, DeduceResults,
995       /*NumberOfArgumentsMustMatch=*/false);
996 
997   SmallVector<TemplateArgument> DeducedArgs;
998   SmallVector<unsigned> NonDeducedTemplateParamsInFIndex;
999   // !!NOTE: DeduceResults respects the sequence of template parameters of
1000   // the deduction guide f.
1001   for (unsigned Index = 0; Index < DeduceResults.size(); ++Index) {
1002     if (const auto &D = DeduceResults[Index]; !D.isNull()) // Deduced
1003       DeducedArgs.push_back(D);
1004     else
1005       NonDeducedTemplateParamsInFIndex.push_back(Index);
1006   }
1007   auto DeducedAliasTemplateParams =
1008       TemplateParamsReferencedInTemplateArgumentList(
1009           AliasTemplate->getTemplateParameters(), DeducedArgs);
1010   // All template arguments null by default.
1011   SmallVector<TemplateArgument> TemplateArgsForBuildingFPrime(
1012       F->getTemplateParameters()->size());
1013 
1014   // Create a template parameter list for the synthesized deduction guide f'.
1015   //
1016   // C++ [over.match.class.deduct]p3.2:
1017   //   If f is a function template, f' is a function template whose template
1018   //   parameter list consists of all the template parameters of A
1019   //   (including their default template arguments) that appear in the above
1020   //   deductions or (recursively) in their default template arguments
1021   SmallVector<NamedDecl *> FPrimeTemplateParams;
1022   // Store template arguments that refer to the newly-created template
1023   // parameters, used for building `TemplateArgsForBuildingFPrime`.
1024   SmallVector<TemplateArgument, 16> TransformedDeducedAliasArgs(
1025       AliasTemplate->getTemplateParameters()->size());
1026 
1027   for (unsigned AliasTemplateParamIdx : DeducedAliasTemplateParams) {
1028     auto *TP =
1029         AliasTemplate->getTemplateParameters()->getParam(AliasTemplateParamIdx);
1030     // Rebuild any internal references to earlier parameters and reindex as
1031     // we go.
1032     MultiLevelTemplateArgumentList Args;
1033     Args.setKind(TemplateSubstitutionKind::Rewrite);
1034     Args.addOuterTemplateArguments(TransformedDeducedAliasArgs);
1035     NamedDecl *NewParam = transformTemplateParameter(
1036         SemaRef, AliasTemplate->getDeclContext(), TP, Args,
1037         /*NewIndex=*/FPrimeTemplateParams.size(), getDepthAndIndex(TP).first);
1038     FPrimeTemplateParams.push_back(NewParam);
1039 
1040     TemplateArgument NewTemplateArgument =
1041         Context.getInjectedTemplateArg(NewParam);
1042     TransformedDeducedAliasArgs[AliasTemplateParamIdx] = NewTemplateArgument;
1043   }
1044   unsigned FirstUndeducedParamIdx = FPrimeTemplateParams.size();
1045   //   ...followed by the template parameters of f that were not deduced
1046   //   (including their default template arguments)
1047   for (unsigned FTemplateParamIdx : NonDeducedTemplateParamsInFIndex) {
1048     auto *TP = F->getTemplateParameters()->getParam(FTemplateParamIdx);
1049     MultiLevelTemplateArgumentList Args;
1050     Args.setKind(TemplateSubstitutionKind::Rewrite);
1051     // We take a shortcut here, it is ok to reuse the
1052     // TemplateArgsForBuildingFPrime.
1053     Args.addOuterTemplateArguments(TemplateArgsForBuildingFPrime);
1054     NamedDecl *NewParam = transformTemplateParameter(
1055         SemaRef, F->getDeclContext(), TP, Args, FPrimeTemplateParams.size(),
1056         getDepthAndIndex(TP).first);
1057     FPrimeTemplateParams.push_back(NewParam);
1058 
1059     assert(TemplateArgsForBuildingFPrime[FTemplateParamIdx].isNull() &&
1060            "The argument must be null before setting");
1061     TemplateArgsForBuildingFPrime[FTemplateParamIdx] =
1062         Context.getInjectedTemplateArg(NewParam);
1063   }
1064 
1065   // To form a deduction guide f' from f, we leverage clang's instantiation
1066   // mechanism, we construct a template argument list where the template
1067   // arguments refer to the newly-created template parameters of f', and
1068   // then apply instantiation on this template argument list to instantiate
1069   // f, this ensures all template parameter occurrences are updated
1070   // correctly.
1071   //
1072   // The template argument list is formed from the `DeducedArgs`, two parts:
1073   //  1) appeared template parameters of alias: transfrom the deduced
1074   //  template argument;
1075   //  2) non-deduced template parameters of f: rebuild a
1076   //  template argument;
1077   //
1078   // 2) has been built already (when rebuilding the new template
1079   // parameters), we now perform 1).
1080   MultiLevelTemplateArgumentList Args;
1081   Args.setKind(TemplateSubstitutionKind::Rewrite);
1082   Args.addOuterTemplateArguments(TransformedDeducedAliasArgs);
1083   for (unsigned Index = 0; Index < DeduceResults.size(); ++Index) {
1084     const auto &D = DeduceResults[Index];
1085     if (D.isNull()) {
1086       // 2): Non-deduced template parameter has been built already.
1087       assert(!TemplateArgsForBuildingFPrime[Index].isNull() &&
1088              "template arguments for non-deduced template parameters should "
1089              "be been set!");
1090       continue;
1091     }
1092     TemplateArgumentLoc Input =
1093         SemaRef.getTrivialTemplateArgumentLoc(D, QualType(), SourceLocation{});
1094     TemplateArgumentLoc Output;
1095     if (!SemaRef.SubstTemplateArgument(Input, Args, Output)) {
1096       assert(TemplateArgsForBuildingFPrime[Index].isNull() &&
1097              "InstantiatedArgs must be null before setting");
1098       TemplateArgsForBuildingFPrime[Index] = Output.getArgument();
1099     }
1100   }
1101 
1102   auto *TemplateArgListForBuildingFPrime =
1103       TemplateArgumentList::CreateCopy(Context, TemplateArgsForBuildingFPrime);
1104   // Form the f' by substituting the template arguments into f.
1105   if (auto *FPrime = SemaRef.InstantiateFunctionDeclaration(
1106           F, TemplateArgListForBuildingFPrime, AliasTemplate->getLocation(),
1107           Sema::CodeSynthesisContext::BuildingDeductionGuides)) {
1108     auto *GG = cast<CXXDeductionGuideDecl>(FPrime);
1109 
1110     Expr *IsDeducible = buildIsDeducibleConstraint(
1111         SemaRef, AliasTemplate, FPrime->getReturnType(), FPrimeTemplateParams);
1112     Expr *RequiresClause =
1113         buildAssociatedConstraints(SemaRef, F, AliasTemplate, DeduceResults,
1114                                    FirstUndeducedParamIdx, IsDeducible);
1115 
1116     auto *FPrimeTemplateParamList = TemplateParameterList::Create(
1117         Context, AliasTemplate->getTemplateParameters()->getTemplateLoc(),
1118         AliasTemplate->getTemplateParameters()->getLAngleLoc(),
1119         FPrimeTemplateParams,
1120         AliasTemplate->getTemplateParameters()->getRAngleLoc(),
1121         /*RequiresClause=*/RequiresClause);
1122     auto *Result = cast<FunctionTemplateDecl>(buildDeductionGuide(
1123         SemaRef, AliasTemplate, FPrimeTemplateParamList,
1124         GG->getCorrespondingConstructor(), GG->getExplicitSpecifier(),
1125         GG->getTypeSourceInfo(), AliasTemplate->getBeginLoc(),
1126         AliasTemplate->getLocation(), AliasTemplate->getEndLoc(),
1127         F->isImplicit()));
1128     cast<CXXDeductionGuideDecl>(Result->getTemplatedDecl())
1129         ->setDeductionCandidateKind(GG->getDeductionCandidateKind());
1130     return Result;
1131   }
1132   return nullptr;
1133 }
1134 
1135 void DeclareImplicitDeductionGuidesForTypeAlias(
1136     Sema &SemaRef, TypeAliasTemplateDecl *AliasTemplate, SourceLocation Loc) {
1137   if (AliasTemplate->isInvalidDecl())
1138     return;
1139   auto &Context = SemaRef.Context;
1140   // FIXME: if there is an explicit deduction guide after the first use of the
1141   // type alias usage, we will not cover this explicit deduction guide. fix this
1142   // case.
1143   if (hasDeclaredDeductionGuides(
1144           Context.DeclarationNames.getCXXDeductionGuideName(AliasTemplate),
1145           AliasTemplate->getDeclContext()))
1146     return;
1147   auto [Template, AliasRhsTemplateArgs] =
1148       getRHSTemplateDeclAndArgs(SemaRef, AliasTemplate);
1149   if (!Template)
1150     return;
1151   DeclarationNameInfo NameInfo(
1152       Context.DeclarationNames.getCXXDeductionGuideName(Template), Loc);
1153   LookupResult Guides(SemaRef, NameInfo, clang::Sema::LookupOrdinaryName);
1154   SemaRef.LookupQualifiedName(Guides, Template->getDeclContext());
1155   Guides.suppressDiagnostics();
1156 
1157   for (auto *G : Guides) {
1158     if (auto *DG = dyn_cast<CXXDeductionGuideDecl>(G)) {
1159       // The deduction guide is a non-template function decl, we just clone it.
1160       auto *FunctionType =
1161           SemaRef.Context.getTrivialTypeSourceInfo(DG->getType());
1162       FunctionProtoTypeLoc FPTL =
1163           FunctionType->getTypeLoc().castAs<FunctionProtoTypeLoc>();
1164 
1165       // Clone the parameters.
1166       for (unsigned I = 0, N = DG->getNumParams(); I != N; ++I) {
1167         const auto *P = DG->getParamDecl(I);
1168         auto *TSI = SemaRef.Context.getTrivialTypeSourceInfo(P->getType());
1169         ParmVarDecl *NewParam = ParmVarDecl::Create(
1170             SemaRef.Context, G->getDeclContext(),
1171             DG->getParamDecl(I)->getBeginLoc(), P->getLocation(), nullptr,
1172             TSI->getType(), TSI, SC_None, nullptr);
1173         NewParam->setScopeInfo(0, I);
1174         FPTL.setParam(I, NewParam);
1175       }
1176       auto *Transformed = cast<FunctionDecl>(buildDeductionGuide(
1177           SemaRef, AliasTemplate, /*TemplateParams=*/nullptr,
1178           /*Constructor=*/nullptr, DG->getExplicitSpecifier(), FunctionType,
1179           AliasTemplate->getBeginLoc(), AliasTemplate->getLocation(),
1180           AliasTemplate->getEndLoc(), DG->isImplicit()));
1181 
1182       // FIXME: Here the synthesized deduction guide is not a templated
1183       // function. Per [dcl.decl]p4, the requires-clause shall be present only
1184       // if the declarator declares a templated function, a bug in standard?
1185       auto *Constraint = buildIsDeducibleConstraint(
1186           SemaRef, AliasTemplate, Transformed->getReturnType(), {});
1187       if (auto *RC = DG->getTrailingRequiresClause()) {
1188         auto Conjunction =
1189             SemaRef.BuildBinOp(SemaRef.getCurScope(), SourceLocation{},
1190                                BinaryOperatorKind::BO_LAnd, RC, Constraint);
1191         if (!Conjunction.isInvalid())
1192           Constraint = Conjunction.getAs<Expr>();
1193       }
1194       Transformed->setTrailingRequiresClause(Constraint);
1195     }
1196     FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(G);
1197     if (!F)
1198       continue;
1199     // The **aggregate** deduction guides are handled in a different code path
1200     // (DeclareAggregateDeductionGuideFromInitList), which involves the tricky
1201     // cache.
1202     if (cast<CXXDeductionGuideDecl>(F->getTemplatedDecl())
1203             ->getDeductionCandidateKind() == DeductionCandidate::Aggregate)
1204       continue;
1205 
1206     BuildDeductionGuideForTypeAlias(SemaRef, AliasTemplate, F, Loc);
1207   }
1208 }
1209 
1210 // Build an aggregate deduction guide for a type alias template.
1211 FunctionTemplateDecl *DeclareAggregateDeductionGuideForTypeAlias(
1212     Sema &SemaRef, TypeAliasTemplateDecl *AliasTemplate,
1213     MutableArrayRef<QualType> ParamTypes, SourceLocation Loc) {
1214   TemplateDecl *RHSTemplate =
1215       getRHSTemplateDeclAndArgs(SemaRef, AliasTemplate).first;
1216   if (!RHSTemplate)
1217     return nullptr;
1218 
1219   llvm::SmallVector<TypedefNameDecl *> TypedefDecls;
1220   llvm::SmallVector<QualType> NewParamTypes;
1221   ExtractTypeForDeductionGuide TypeAliasTransformer(SemaRef, TypedefDecls);
1222   for (QualType P : ParamTypes) {
1223     QualType Type = TypeAliasTransformer.TransformType(P);
1224     if (Type.isNull())
1225       return nullptr;
1226     NewParamTypes.push_back(Type);
1227   }
1228 
1229   auto *RHSDeductionGuide = SemaRef.DeclareAggregateDeductionGuideFromInitList(
1230       RHSTemplate, NewParamTypes, Loc);
1231   if (!RHSDeductionGuide)
1232     return nullptr;
1233 
1234   for (TypedefNameDecl *TD : TypedefDecls)
1235     TD->setDeclContext(RHSDeductionGuide->getTemplatedDecl());
1236 
1237   return BuildDeductionGuideForTypeAlias(SemaRef, AliasTemplate,
1238                                          RHSDeductionGuide, Loc);
1239 }
1240 
1241 } // namespace
1242 
1243 FunctionTemplateDecl *Sema::DeclareAggregateDeductionGuideFromInitList(
1244     TemplateDecl *Template, MutableArrayRef<QualType> ParamTypes,
1245     SourceLocation Loc) {
1246   llvm::FoldingSetNodeID ID;
1247   ID.AddPointer(Template);
1248   for (auto &T : ParamTypes)
1249     T.getCanonicalType().Profile(ID);
1250   unsigned Hash = ID.ComputeHash();
1251 
1252   auto Found = AggregateDeductionCandidates.find(Hash);
1253   if (Found != AggregateDeductionCandidates.end()) {
1254     CXXDeductionGuideDecl *GD = Found->getSecond();
1255     return GD->getDescribedFunctionTemplate();
1256   }
1257 
1258   if (auto *AliasTemplate = llvm::dyn_cast<TypeAliasTemplateDecl>(Template)) {
1259     if (auto *FTD = DeclareAggregateDeductionGuideForTypeAlias(
1260             *this, AliasTemplate, ParamTypes, Loc)) {
1261       auto *GD = cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl());
1262       GD->setDeductionCandidateKind(DeductionCandidate::Aggregate);
1263       AggregateDeductionCandidates[Hash] = GD;
1264       return FTD;
1265     }
1266   }
1267 
1268   if (CXXRecordDecl *DefRecord =
1269           cast<CXXRecordDecl>(Template->getTemplatedDecl())->getDefinition()) {
1270     if (TemplateDecl *DescribedTemplate =
1271             DefRecord->getDescribedClassTemplate())
1272       Template = DescribedTemplate;
1273   }
1274 
1275   DeclContext *DC = Template->getDeclContext();
1276   if (DC->isDependentContext())
1277     return nullptr;
1278 
1279   ConvertConstructorToDeductionGuideTransform Transform(
1280       *this, cast<ClassTemplateDecl>(Template));
1281   if (!isCompleteType(Loc, Transform.DeducedType))
1282     return nullptr;
1283 
1284   // In case we were expanding a pack when we attempted to declare deduction
1285   // guides, turn off pack expansion for everything we're about to do.
1286   ArgumentPackSubstitutionIndexRAII SubstIndex(*this,
1287                                                /*NewSubstitutionIndex=*/-1);
1288   // Create a template instantiation record to track the "instantiation" of
1289   // constructors into deduction guides.
1290   InstantiatingTemplate BuildingDeductionGuides(
1291       *this, Loc, Template,
1292       Sema::InstantiatingTemplate::BuildingDeductionGuidesTag{});
1293   if (BuildingDeductionGuides.isInvalid())
1294     return nullptr;
1295 
1296   ClassTemplateDecl *Pattern =
1297       Transform.NestedPattern ? Transform.NestedPattern : Transform.Template;
1298   ContextRAII SavedContext(*this, Pattern->getTemplatedDecl());
1299 
1300   auto *FTD = cast<FunctionTemplateDecl>(
1301       Transform.buildSimpleDeductionGuide(ParamTypes));
1302   SavedContext.pop();
1303   auto *GD = cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl());
1304   GD->setDeductionCandidateKind(DeductionCandidate::Aggregate);
1305   AggregateDeductionCandidates[Hash] = GD;
1306   return FTD;
1307 }
1308 
1309 void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template,
1310                                           SourceLocation Loc) {
1311   if (auto *AliasTemplate = llvm::dyn_cast<TypeAliasTemplateDecl>(Template)) {
1312     DeclareImplicitDeductionGuidesForTypeAlias(*this, AliasTemplate, Loc);
1313     return;
1314   }
1315   if (CXXRecordDecl *DefRecord =
1316           cast<CXXRecordDecl>(Template->getTemplatedDecl())->getDefinition()) {
1317     if (TemplateDecl *DescribedTemplate =
1318             DefRecord->getDescribedClassTemplate())
1319       Template = DescribedTemplate;
1320   }
1321 
1322   DeclContext *DC = Template->getDeclContext();
1323   if (DC->isDependentContext())
1324     return;
1325 
1326   ConvertConstructorToDeductionGuideTransform Transform(
1327       *this, cast<ClassTemplateDecl>(Template));
1328   if (!isCompleteType(Loc, Transform.DeducedType))
1329     return;
1330 
1331   if (hasDeclaredDeductionGuides(Transform.DeductionGuideName, DC))
1332     return;
1333 
1334   // In case we were expanding a pack when we attempted to declare deduction
1335   // guides, turn off pack expansion for everything we're about to do.
1336   ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
1337   // Create a template instantiation record to track the "instantiation" of
1338   // constructors into deduction guides.
1339   InstantiatingTemplate BuildingDeductionGuides(
1340       *this, Loc, Template,
1341       Sema::InstantiatingTemplate::BuildingDeductionGuidesTag{});
1342   if (BuildingDeductionGuides.isInvalid())
1343     return;
1344 
1345   // Convert declared constructors into deduction guide templates.
1346   // FIXME: Skip constructors for which deduction must necessarily fail (those
1347   // for which some class template parameter without a default argument never
1348   // appears in a deduced context).
1349   ClassTemplateDecl *Pattern =
1350       Transform.NestedPattern ? Transform.NestedPattern : Transform.Template;
1351   ContextRAII SavedContext(*this, Pattern->getTemplatedDecl());
1352   llvm::SmallPtrSet<NamedDecl *, 8> ProcessedCtors;
1353   bool AddedAny = false;
1354   for (NamedDecl *D : LookupConstructors(Pattern->getTemplatedDecl())) {
1355     D = D->getUnderlyingDecl();
1356     if (D->isInvalidDecl() || D->isImplicit())
1357       continue;
1358 
1359     D = cast<NamedDecl>(D->getCanonicalDecl());
1360 
1361     // Within C++20 modules, we may have multiple same constructors in
1362     // multiple same RecordDecls. And it doesn't make sense to create
1363     // duplicated deduction guides for the duplicated constructors.
1364     if (ProcessedCtors.count(D))
1365       continue;
1366 
1367     auto *FTD = dyn_cast<FunctionTemplateDecl>(D);
1368     auto *CD =
1369         dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl() : D);
1370     // Class-scope explicit specializations (MS extension) do not result in
1371     // deduction guides.
1372     if (!CD || (!FTD && CD->isFunctionTemplateSpecialization()))
1373       continue;
1374 
1375     // Cannot make a deduction guide when unparsed arguments are present.
1376     if (llvm::any_of(CD->parameters(), [](ParmVarDecl *P) {
1377           return !P || P->hasUnparsedDefaultArg();
1378         }))
1379       continue;
1380 
1381     ProcessedCtors.insert(D);
1382     Transform.transformConstructor(FTD, CD);
1383     AddedAny = true;
1384   }
1385 
1386   // C++17 [over.match.class.deduct]
1387   //    --  If C is not defined or does not declare any constructors, an
1388   //    additional function template derived as above from a hypothetical
1389   //    constructor C().
1390   if (!AddedAny)
1391     Transform.buildSimpleDeductionGuide({});
1392 
1393   //    -- An additional function template derived as above from a hypothetical
1394   //    constructor C(C), called the copy deduction candidate.
1395   cast<CXXDeductionGuideDecl>(
1396       cast<FunctionTemplateDecl>(
1397           Transform.buildSimpleDeductionGuide(Transform.DeducedType))
1398           ->getTemplatedDecl())
1399       ->setDeductionCandidateKind(DeductionCandidate::Copy);
1400 
1401   SavedContext.pop();
1402 }
1403