| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/ |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 6 | //===----------------------------------------------------------------------===/ |
| 7 | // |
| 8 | // This file implements C++ template instantiation. |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===/ |
| 11 | |
| John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 12 | #include "clang/Sema/SemaInternal.h" |
| Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 13 | #include "TreeTransform.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTLambda.h" |
| Richard Smith | 4b054b2 | 2016-08-24 21:25:37 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTMutationListener.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
| 19 | #include "clang/AST/Expr.h" |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 20 | #include "clang/AST/PrettyDeclStackTrace.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
| John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DeclSpec.h" |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Initialization.h" |
| Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Lookup.h" |
| John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 25 | #include "clang/Sema/Template.h" |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 26 | #include "clang/Sema/TemplateDeduction.h" |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 27 | #include "clang/Sema/TemplateInstCallback.h" |
| Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/TimeProfiler.h" |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace clang; |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 31 | using namespace sema; |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 32 | |
| Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===/ |
| 34 | // Template Instantiation Support |
| 35 | //===----------------------------------------------------------------------===/ |
| 36 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 37 | /// Retrieve the template argument list(s) that should be used to |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 38 | /// instantiate the definition of the given declaration. |
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 39 | /// |
| 40 | /// \param D the declaration for which we are computing template instantiation |
| 41 | /// arguments. |
| 42 | /// |
| 43 | /// \param Innermost if non-NULL, the innermost template argument list. |
| Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 44 | /// |
| 45 | /// \param RelativeToPrimary true if we should get the template |
| 46 | /// arguments relative to the primary template, even when we're |
| 47 | /// dealing with a specialization. This is only relevant for function |
| 48 | /// template specializations. |
| Douglas Gregor | 1bd7a94 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 49 | /// |
| 50 | /// \param Pattern If non-NULL, indicates the pattern from which we will be |
| 51 | /// instantiating the definition of the given declaration, \p D. This is |
| 52 | /// used to determine the proper set of template instantiation arguments for |
| 53 | /// friend function template specializations. |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 54 | MultiLevelTemplateArgumentList |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 55 | Sema::getTemplateInstantiationArgs(NamedDecl *D, |
| Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 56 | const TemplateArgumentList *Innermost, |
| Douglas Gregor | 1bd7a94 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 57 | bool RelativeToPrimary, |
| 58 | const FunctionDecl *Pattern) { |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 59 | // Accumulate the set of template argument lists in this structure. |
| 60 | MultiLevelTemplateArgumentList Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 62 | if (Innermost) |
| 63 | Result.addOuterTemplateArguments(Innermost); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 64 | |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 65 | DeclContext *Ctx = dyn_cast<DeclContext>(D); |
| Douglas Gregor | a51c9cc | 2011-05-22 00:21:10 +0000 | [diff] [blame] | 66 | if (!Ctx) { |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 67 | Ctx = D->getDeclContext(); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 68 | |
| Richard Smith | 59d0500 | 2019-04-26 02:11:23 +0000 | [diff] [blame] | 69 | // Add template arguments from a variable template instantiation. For a |
| 70 | // class-scope explicit specialization, there are no template arguments |
| 71 | // at this level, but there may be enclosing template arguments. |
| 72 | VarTemplateSpecializationDecl *Spec = |
| 73 | dyn_cast<VarTemplateSpecializationDecl>(D); |
| 74 | if (Spec && !Spec->isClassScopeExplicitSpecialization()) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 75 | // We're done when we hit an explicit specialization. |
| 76 | if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization && |
| 77 | !isa<VarTemplatePartialSpecializationDecl>(Spec)) |
| 78 | return Result; |
| 79 | |
| 80 | Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); |
| 81 | |
| 82 | // If this variable template specialization was instantiated from a |
| 83 | // specialized member that is a variable template, we're done. |
| 84 | assert(Spec->getSpecializedTemplate() && "No variable template?"); |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 85 | llvm::PointerUnion<VarTemplateDecl*, |
| 86 | VarTemplatePartialSpecializationDecl*> Specialized |
| 87 | = Spec->getSpecializedTemplateOrPartial(); |
| 88 | if (VarTemplatePartialSpecializationDecl *Partial = |
| 89 | Specialized.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 90 | if (Partial->isMemberSpecialization()) |
| 91 | return Result; |
| 92 | } else { |
| 93 | VarTemplateDecl *Tmpl = Specialized.get<VarTemplateDecl *>(); |
| 94 | if (Tmpl->isMemberSpecialization()) |
| 95 | return Result; |
| 96 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| Douglas Gregor | 5546262 | 2011-06-15 14:20:42 +0000 | [diff] [blame] | 99 | // If we have a template template parameter with translation unit context, |
| 100 | // then we're performing substitution into a default template argument of |
| 101 | // this template template parameter before we've constructed the template |
| 102 | // that will own this template template parameter. In this case, we |
| 103 | // use empty template parameter lists for all of the outer templates |
| 104 | // to avoid performing any substitutions. |
| 105 | if (Ctx->isTranslationUnit()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 106 | if (TemplateTemplateParmDecl *TTP |
| Douglas Gregor | 5546262 | 2011-06-15 14:20:42 +0000 | [diff] [blame] | 107 | = dyn_cast<TemplateTemplateParmDecl>(D)) { |
| 108 | for (unsigned I = 0, N = TTP->getDepth() + 1; I != N; ++I) |
| Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 109 | Result.addOuterTemplateArguments(None); |
| Douglas Gregor | 5546262 | 2011-06-15 14:20:42 +0000 | [diff] [blame] | 110 | return Result; |
| 111 | } |
| 112 | } |
| Douglas Gregor | a51c9cc | 2011-05-22 00:21:10 +0000 | [diff] [blame] | 113 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 114 | |
| John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 115 | while (!Ctx->isFileContext()) { |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 116 | // Add template arguments from a class template instantiation. |
| Richard Smith | 59d0500 | 2019-04-26 02:11:23 +0000 | [diff] [blame] | 117 | ClassTemplateSpecializationDecl *Spec |
| 118 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx); |
| 119 | if (Spec && !Spec->isClassScopeExplicitSpecialization()) { |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 120 | // We're done when we hit an explicit specialization. |
| Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 121 | if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization && |
| 122 | !isa<ClassTemplatePartialSpecializationDecl>(Spec)) |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 123 | break; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 125 | Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 126 | |
| 127 | // If this class template specialization was instantiated from a |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 128 | // specialized member that is a class template, we're done. |
| 129 | assert(Spec->getSpecializedTemplate() && "No class template?"); |
| 130 | if (Spec->getSpecializedTemplate()->isMemberSpecialization()) |
| 131 | break; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | } |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 133 | // Add template arguments from a function template specialization. |
| John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 134 | else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) { |
| Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 135 | if (!RelativeToPrimary && |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 136 | Function->getTemplateSpecializationKindForInstantiation() == |
| 137 | TSK_ExplicitSpecialization) |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 138 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 139 | |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 140 | if (const TemplateArgumentList *TemplateArgs |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 141 | = Function->getTemplateSpecializationArgs()) { |
| 142 | // Add the template arguments for this specialization. |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 143 | Result.addOuterTemplateArguments(TemplateArgs); |
| John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 144 | |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 145 | // If this function was instantiated from a specialized member that is |
| 146 | // a function template, we're done. |
| 147 | assert(Function->getPrimaryTemplate() && "No function template?"); |
| 148 | if (Function->getPrimaryTemplate()->isMemberSpecialization()) |
| 149 | break; |
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 150 | |
| 151 | // If this function is a generic lambda specialization, we are done. |
| 152 | if (isGenericLambdaCallOperatorSpecialization(Function)) |
| 153 | break; |
| 154 | |
| Douglas Gregor | 43669f8 | 2011-03-05 17:54:25 +0000 | [diff] [blame] | 155 | } else if (FunctionTemplateDecl *FunTmpl |
| 156 | = Function->getDescribedFunctionTemplate()) { |
| 157 | // Add the "injected" template arguments. |
| Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 158 | Result.addOuterTemplateArguments(FunTmpl->getInjectedTemplateArgs()); |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 159 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 160 | |
| John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 161 | // If this is a friend declaration and it declares an entity at |
| 162 | // namespace scope, take arguments from its lexical parent |
| Douglas Gregor | 1bd7a94 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 163 | // instead of its semantic parent, unless of course the pattern we're |
| 164 | // instantiating actually comes from the file's context! |
| John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 165 | if (Function->getFriendObjectKind() && |
| Douglas Gregor | 1bd7a94 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 166 | Function->getDeclContext()->isFileContext() && |
| 167 | (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) { |
| John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 168 | Ctx = Function->getLexicalDeclContext(); |
| Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 169 | RelativeToPrimary = false; |
| John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 170 | continue; |
| 171 | } |
| Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 172 | } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 173 | if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) { |
| 174 | QualType T = ClassTemplate->getInjectedClassNameSpecialization(); |
| Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 175 | const TemplateSpecializationType *TST = |
| 176 | cast<TemplateSpecializationType>(Context.getCanonicalType(T)); |
| 177 | Result.addOuterTemplateArguments( |
| 178 | llvm::makeArrayRef(TST->getArgs(), TST->getNumArgs())); |
| Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 179 | if (ClassTemplate->isMemberSpecialization()) |
| 180 | break; |
| 181 | } |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 182 | } |
| John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 183 | |
| 184 | Ctx = Ctx->getParent(); |
| Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 185 | RelativeToPrimary = false; |
| Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 186 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
| Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 188 | return Result; |
| Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 191 | bool Sema::CodeSynthesisContext::isInstantiationRecord() const { |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 192 | switch (Kind) { |
| 193 | case TemplateInstantiation: |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 194 | case ExceptionSpecInstantiation: |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 195 | case DefaultTemplateArgumentInstantiation: |
| 196 | case DefaultFunctionArgumentInstantiation: |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 197 | case ExplicitTemplateArgumentSubstitution: |
| 198 | case DeducedTemplateArgumentSubstitution: |
| 199 | case PriorTemplateArgumentSubstitution: |
| Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 200 | return true; |
| 201 | |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 202 | case DefaultTemplateArgumentChecking: |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 203 | case DeclaringSpecialMember: |
| Richard Smith | 883dbc4 | 2017-05-25 22:47:05 +0000 | [diff] [blame] | 204 | case DefiningSynthesizedFunction: |
| Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 205 | case ExceptionSpecEvaluation: |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 206 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 207 | |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 208 | // This function should never be called when Kind's value is Memoization. |
| 209 | case Memoization: |
| 210 | break; |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 211 | } |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 212 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 213 | llvm_unreachable("Invalid SynthesisKind!"); |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 216 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 217 | Sema &SemaRef, CodeSynthesisContext::SynthesisKind Kind, |
| Stephan Tolksdorf | 232670f | 2014-03-13 20:34:22 +0000 | [diff] [blame] | 218 | SourceLocation PointOfInstantiation, SourceRange InstantiationRange, |
| 219 | Decl *Entity, NamedDecl *Template, ArrayRef<TemplateArgument> TemplateArgs, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 220 | sema::TemplateDeductionInfo *DeductionInfo) |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 221 | : SemaRef(SemaRef) { |
| Akira Hatanaka | 43556c1 | 2016-11-03 15:04:58 +0000 | [diff] [blame] | 222 | // Don't allow further instantiation if a fatal error and an uncompilable |
| Akira Hatanaka | 40c15ab | 2016-11-03 17:11:28 +0000 | [diff] [blame] | 223 | // error have occurred. Any diagnostics we might have raised will not be |
| Akira Hatanaka | 43556c1 | 2016-11-03 15:04:58 +0000 | [diff] [blame] | 224 | // visible, and we do not need to construct a correct AST. |
| 225 | if (SemaRef.Diags.hasFatalErrorOccurred() && |
| 226 | SemaRef.Diags.hasUncompilableErrorOccurred()) { |
| David Majnemer | 8c969ea | 2015-01-30 05:01:23 +0000 | [diff] [blame] | 227 | Invalid = true; |
| 228 | return; |
| 229 | } |
| Stephan Tolksdorf | 232670f | 2014-03-13 20:34:22 +0000 | [diff] [blame] | 230 | Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange); |
| 231 | if (!Invalid) { |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 232 | CodeSynthesisContext Inst; |
| Stephan Tolksdorf | 232670f | 2014-03-13 20:34:22 +0000 | [diff] [blame] | 233 | Inst.Kind = Kind; |
| 234 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 235 | Inst.Entity = Entity; |
| 236 | Inst.Template = Template; |
| 237 | Inst.TemplateArgs = TemplateArgs.data(); |
| 238 | Inst.NumTemplateArgs = TemplateArgs.size(); |
| 239 | Inst.DeductionInfo = DeductionInfo; |
| 240 | Inst.InstantiationRange = InstantiationRange; |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 241 | SemaRef.pushCodeSynthesisContext(Inst); |
| 242 | |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 243 | AlreadyInstantiating = |
| 244 | !SemaRef.InstantiatingSpecializations |
| 245 | .insert(std::make_pair(Inst.Entity->getCanonicalDecl(), Inst.Kind)) |
| 246 | .second; |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 247 | atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, Inst); |
| Stephan Tolksdorf | 232670f | 2014-03-13 20:34:22 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 251 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 252 | Sema &SemaRef, SourceLocation PointOfInstantiation, Decl *Entity, |
| 253 | SourceRange InstantiationRange) |
| 254 | : InstantiatingTemplate(SemaRef, |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 255 | CodeSynthesisContext::TemplateInstantiation, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 256 | PointOfInstantiation, InstantiationRange, Entity) {} |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 257 | |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 258 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 259 | Sema &SemaRef, SourceLocation PointOfInstantiation, FunctionDecl *Entity, |
| 260 | ExceptionSpecification, SourceRange InstantiationRange) |
| 261 | : InstantiatingTemplate( |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 262 | SemaRef, CodeSynthesisContext::ExceptionSpecInstantiation, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 263 | PointOfInstantiation, InstantiationRange, Entity) {} |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 264 | |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 265 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 266 | Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateParameter Param, |
| 267 | TemplateDecl *Template, ArrayRef<TemplateArgument> TemplateArgs, |
| 268 | SourceRange InstantiationRange) |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 269 | : InstantiatingTemplate( |
| 270 | SemaRef, |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 271 | CodeSynthesisContext::DefaultTemplateArgumentInstantiation, |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 272 | PointOfInstantiation, InstantiationRange, getAsNamedDecl(Param), |
| 273 | Template, TemplateArgs) {} |
| Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 274 | |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 275 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 276 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 277 | FunctionTemplateDecl *FunctionTemplate, |
| 278 | ArrayRef<TemplateArgument> TemplateArgs, |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 279 | CodeSynthesisContext::SynthesisKind Kind, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 280 | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
| 281 | : InstantiatingTemplate(SemaRef, Kind, PointOfInstantiation, |
| 282 | InstantiationRange, FunctionTemplate, nullptr, |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 283 | TemplateArgs, &DeductionInfo) { |
| 284 | assert( |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 285 | Kind == CodeSynthesisContext::ExplicitTemplateArgumentSubstitution || |
| 286 | Kind == CodeSynthesisContext::DeducedTemplateArgumentSubstitution); |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 287 | } |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 288 | |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 289 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 290 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 291 | TemplateDecl *Template, |
| 292 | ArrayRef<TemplateArgument> TemplateArgs, |
| 293 | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
| 294 | : InstantiatingTemplate( |
| 295 | SemaRef, |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 296 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 297 | PointOfInstantiation, InstantiationRange, Template, nullptr, |
| 298 | TemplateArgs, &DeductionInfo) {} |
| 299 | |
| 300 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 301 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 302 | ClassTemplatePartialSpecializationDecl *PartialSpec, |
| 303 | ArrayRef<TemplateArgument> TemplateArgs, |
| 304 | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
| 305 | : InstantiatingTemplate( |
| 306 | SemaRef, |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 307 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 308 | PointOfInstantiation, InstantiationRange, PartialSpec, nullptr, |
| 309 | TemplateArgs, &DeductionInfo) {} |
| Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 310 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 311 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 312 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 313 | VarTemplatePartialSpecializationDecl *PartialSpec, |
| 314 | ArrayRef<TemplateArgument> TemplateArgs, |
| 315 | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 316 | : InstantiatingTemplate( |
| 317 | SemaRef, |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 318 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 319 | PointOfInstantiation, InstantiationRange, PartialSpec, nullptr, |
| 320 | TemplateArgs, &DeductionInfo) {} |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 321 | |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 322 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 323 | Sema &SemaRef, SourceLocation PointOfInstantiation, ParmVarDecl *Param, |
| 324 | ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange) |
| 325 | : InstantiatingTemplate( |
| 326 | SemaRef, |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 327 | CodeSynthesisContext::DefaultFunctionArgumentInstantiation, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 328 | PointOfInstantiation, InstantiationRange, Param, nullptr, |
| 329 | TemplateArgs) {} |
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 330 | |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 331 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 332 | Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template, |
| 333 | NonTypeTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
| 334 | SourceRange InstantiationRange) |
| 335 | : InstantiatingTemplate( |
| 336 | SemaRef, |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 337 | CodeSynthesisContext::PriorTemplateArgumentSubstitution, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 338 | PointOfInstantiation, InstantiationRange, Param, Template, |
| 339 | TemplateArgs) {} |
| Stephan Tolksdorf | 232670f | 2014-03-13 20:34:22 +0000 | [diff] [blame] | 340 | |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 341 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 342 | Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template, |
| 343 | TemplateTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
| 344 | SourceRange InstantiationRange) |
| 345 | : InstantiatingTemplate( |
| 346 | SemaRef, |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 347 | CodeSynthesisContext::PriorTemplateArgumentSubstitution, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 348 | PointOfInstantiation, InstantiationRange, Param, Template, |
| 349 | TemplateArgs) {} |
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 350 | |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 351 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 352 | Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateDecl *Template, |
| 353 | NamedDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
| 354 | SourceRange InstantiationRange) |
| 355 | : InstantiatingTemplate( |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 356 | SemaRef, CodeSynthesisContext::DefaultTemplateArgumentChecking, |
| Benjamin Kramer | 7761a04 | 2015-03-06 16:36:50 +0000 | [diff] [blame] | 357 | PointOfInstantiation, InstantiationRange, Param, Template, |
| 358 | TemplateArgs) {} |
| Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 359 | |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 360 | void Sema::pushCodeSynthesisContext(CodeSynthesisContext Ctx) { |
| 361 | Ctx.SavedInNonInstantiationSFINAEContext = InNonInstantiationSFINAEContext; |
| 362 | InNonInstantiationSFINAEContext = false; |
| 363 | |
| 364 | CodeSynthesisContexts.push_back(Ctx); |
| 365 | |
| 366 | if (!Ctx.isInstantiationRecord()) |
| 367 | ++NonInstantiationEntries; |
| 368 | } |
| 369 | |
| 370 | void Sema::popCodeSynthesisContext() { |
| 371 | auto &Active = CodeSynthesisContexts.back(); |
| 372 | if (!Active.isInstantiationRecord()) { |
| 373 | assert(NonInstantiationEntries > 0); |
| 374 | --NonInstantiationEntries; |
| 375 | } |
| 376 | |
| 377 | InNonInstantiationSFINAEContext = Active.SavedInNonInstantiationSFINAEContext; |
| 378 | |
| 379 | // Name lookup no longer looks in this template's defining module. |
| 380 | assert(CodeSynthesisContexts.size() >= |
| 381 | CodeSynthesisContextLookupModules.size() && |
| 382 | "forgot to remove a lookup module for a template instantiation"); |
| 383 | if (CodeSynthesisContexts.size() == |
| 384 | CodeSynthesisContextLookupModules.size()) { |
| 385 | if (Module *M = CodeSynthesisContextLookupModules.back()) |
| 386 | LookupModulesCache.erase(M); |
| 387 | CodeSynthesisContextLookupModules.pop_back(); |
| 388 | } |
| 389 | |
| 390 | // If we've left the code synthesis context for the current context stack, |
| 391 | // stop remembering that we've emitted that stack. |
| 392 | if (CodeSynthesisContexts.size() == |
| 393 | LastEmittedCodeSynthesisContextDepth) |
| 394 | LastEmittedCodeSynthesisContextDepth = 0; |
| 395 | |
| 396 | CodeSynthesisContexts.pop_back(); |
| 397 | } |
| 398 | |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 399 | void Sema::InstantiatingTemplate::Clear() { |
| 400 | if (!Invalid) { |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 401 | if (!AlreadyInstantiating) { |
| 402 | auto &Active = SemaRef.CodeSynthesisContexts.back(); |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 403 | SemaRef.InstantiatingSpecializations.erase( |
| 404 | std::make_pair(Active.Entity, Active.Kind)); |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 405 | } |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 406 | |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 407 | atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef, |
| 408 | SemaRef.CodeSynthesisContexts.back()); |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 409 | |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 410 | SemaRef.popCodeSynthesisContext(); |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 411 | Invalid = true; |
| 412 | } |
| Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 415 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 416 | SourceLocation PointOfInstantiation, |
| 417 | SourceRange InstantiationRange) { |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 418 | assert(SemaRef.NonInstantiationEntries <= |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 419 | SemaRef.CodeSynthesisContexts.size()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 420 | if ((SemaRef.CodeSynthesisContexts.size() - |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 421 | SemaRef.NonInstantiationEntries) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 422 | <= SemaRef.getLangOpts().InstantiationDepth) |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 423 | return false; |
| 424 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | SemaRef.Diag(PointOfInstantiation, |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 426 | diag::err_template_recursion_depth_exceeded) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 427 | << SemaRef.getLangOpts().InstantiationDepth |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 428 | << InstantiationRange; |
| 429 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 430 | << SemaRef.getLangOpts().InstantiationDepth; |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 431 | return true; |
| 432 | } |
| 433 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 434 | /// Prints the current instantiation stack through a series of |
| Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 435 | /// notes. |
| 436 | void Sema::PrintInstantiationStack() { |
| Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 437 | // Determine which template instantiations to skip, if any. |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 438 | unsigned SkipStart = CodeSynthesisContexts.size(), SkipEnd = SkipStart; |
| Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 439 | unsigned Limit = Diags.getTemplateBacktraceLimit(); |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 440 | if (Limit && Limit < CodeSynthesisContexts.size()) { |
| Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 441 | SkipStart = Limit / 2 + Limit % 2; |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 442 | SkipEnd = CodeSynthesisContexts.size() - Limit / 2; |
| Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 445 | // FIXME: In all of these cases, we need to show the template arguments |
| Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 446 | unsigned InstantiationIdx = 0; |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 447 | for (SmallVectorImpl<CodeSynthesisContext>::reverse_iterator |
| 448 | Active = CodeSynthesisContexts.rbegin(), |
| 449 | ActiveEnd = CodeSynthesisContexts.rend(); |
| Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 450 | Active != ActiveEnd; |
| Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 451 | ++Active, ++InstantiationIdx) { |
| 452 | // Skip this instantiation? |
| 453 | if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) { |
| 454 | if (InstantiationIdx == SkipStart) { |
| 455 | // Note that we're skipping instantiations. |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 456 | Diags.Report(Active->PointOfInstantiation, |
| Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 457 | diag::note_instantiation_contexts_suppressed) |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 458 | << unsigned(CodeSynthesisContexts.size() - Limit); |
| Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 459 | } |
| 460 | continue; |
| 461 | } |
| 462 | |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 463 | switch (Active->Kind) { |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 464 | case CodeSynthesisContext::TemplateInstantiation: { |
| Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 465 | Decl *D = Active->Entity; |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 466 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 467 | unsigned DiagID = diag::note_template_member_class_here; |
| 468 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 469 | DiagID = diag::note_template_class_instantiation_here; |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 470 | Diags.Report(Active->PointOfInstantiation, DiagID) |
| Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 471 | << Record << Active->InstantiationRange; |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 472 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 473 | unsigned DiagID; |
| 474 | if (Function->getPrimaryTemplate()) |
| 475 | DiagID = diag::note_function_template_spec_here; |
| 476 | else |
| 477 | DiagID = diag::note_template_member_function_here; |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 478 | Diags.Report(Active->PointOfInstantiation, DiagID) |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 479 | << Function |
| 480 | << Active->InstantiationRange; |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 481 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 482 | Diags.Report(Active->PointOfInstantiation, |
| Larisse Voufo | dbd6577 | 2013-08-14 20:15:02 +0000 | [diff] [blame] | 483 | VD->isStaticDataMember()? |
| 484 | diag::note_template_static_data_member_def_here |
| 485 | : diag::note_template_variable_def_here) |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 486 | << VD |
| 487 | << Active->InstantiationRange; |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 488 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 489 | Diags.Report(Active->PointOfInstantiation, |
| 490 | diag::note_template_enum_def_here) |
| 491 | << ED |
| 492 | << Active->InstantiationRange; |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 493 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 494 | Diags.Report(Active->PointOfInstantiation, |
| 495 | diag::note_template_nsdmi_here) |
| 496 | << FD << Active->InstantiationRange; |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 497 | } else { |
| 498 | Diags.Report(Active->PointOfInstantiation, |
| 499 | diag::note_template_type_alias_instantiation_here) |
| 500 | << cast<TypeAliasTemplateDecl>(D) |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 501 | << Active->InstantiationRange; |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 502 | } |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 506 | case CodeSynthesisContext::DefaultTemplateArgumentInstantiation: { |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 507 | TemplateDecl *Template = cast<TemplateDecl>(Active->Template); |
| Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 508 | SmallVector<char, 128> TemplateArgsStr; |
| 509 | llvm::raw_svector_ostream OS(TemplateArgsStr); |
| 510 | Template->printName(OS); |
| Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 511 | printTemplateArgumentList(OS, Active->template_arguments(), |
| 512 | getPrintingPolicy()); |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 513 | Diags.Report(Active->PointOfInstantiation, |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 514 | diag::note_default_arg_instantiation_here) |
| Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 515 | << OS.str() |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 516 | << Active->InstantiationRange; |
| 517 | break; |
| 518 | } |
| Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 519 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 520 | case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution: { |
| Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 521 | FunctionTemplateDecl *FnTmpl = cast<FunctionTemplateDecl>(Active->Entity); |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 522 | Diags.Report(Active->PointOfInstantiation, |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 523 | diag::note_explicit_template_arg_substitution_here) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 524 | << FnTmpl |
| 525 | << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), |
| 526 | Active->TemplateArgs, |
| Douglas Gregor | 607f141 | 2010-03-30 20:35:20 +0000 | [diff] [blame] | 527 | Active->NumTemplateArgs) |
| 528 | << Active->InstantiationRange; |
| Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 529 | break; |
| 530 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 532 | case CodeSynthesisContext::DeducedTemplateArgumentSubstitution: { |
| Richard Smith | e68a38f | 2016-12-24 04:20:31 +0000 | [diff] [blame] | 533 | if (FunctionTemplateDecl *FnTmpl = |
| 534 | dyn_cast<FunctionTemplateDecl>(Active->Entity)) { |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 535 | Diags.Report(Active->PointOfInstantiation, |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 536 | diag::note_function_template_deduction_instantiation_here) |
| Douglas Gregor | 607f141 | 2010-03-30 20:35:20 +0000 | [diff] [blame] | 537 | << FnTmpl |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 538 | << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), |
| 539 | Active->TemplateArgs, |
| Douglas Gregor | 607f141 | 2010-03-30 20:35:20 +0000 | [diff] [blame] | 540 | Active->NumTemplateArgs) |
| 541 | << Active->InstantiationRange; |
| Richard Smith | e68a38f | 2016-12-24 04:20:31 +0000 | [diff] [blame] | 542 | } else { |
| 543 | bool IsVar = isa<VarTemplateDecl>(Active->Entity) || |
| 544 | isa<VarTemplateSpecializationDecl>(Active->Entity); |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 545 | bool IsTemplate = false; |
| Richard Smith | e68a38f | 2016-12-24 04:20:31 +0000 | [diff] [blame] | 546 | TemplateParameterList *Params; |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 547 | if (auto *D = dyn_cast<TemplateDecl>(Active->Entity)) { |
| 548 | IsTemplate = true; |
| 549 | Params = D->getTemplateParameters(); |
| 550 | } else if (auto *D = dyn_cast<ClassTemplatePartialSpecializationDecl>( |
| Richard Smith | e68a38f | 2016-12-24 04:20:31 +0000 | [diff] [blame] | 551 | Active->Entity)) { |
| 552 | Params = D->getTemplateParameters(); |
| 553 | } else if (auto *D = dyn_cast<VarTemplatePartialSpecializationDecl>( |
| 554 | Active->Entity)) { |
| 555 | Params = D->getTemplateParameters(); |
| 556 | } else { |
| 557 | llvm_unreachable("unexpected template kind"); |
| 558 | } |
| 559 | |
| Richard Smith | e68a38f | 2016-12-24 04:20:31 +0000 | [diff] [blame] | 560 | Diags.Report(Active->PointOfInstantiation, |
| 561 | diag::note_deduced_template_arg_substitution_here) |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 562 | << IsVar << IsTemplate << cast<NamedDecl>(Active->Entity) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 563 | << getTemplateArgumentBindingsText(Params, Active->TemplateArgs, |
| Richard Smith | e68a38f | 2016-12-24 04:20:31 +0000 | [diff] [blame] | 564 | Active->NumTemplateArgs) |
| 565 | << Active->InstantiationRange; |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 566 | } |
| 567 | break; |
| Richard Smith | e68a38f | 2016-12-24 04:20:31 +0000 | [diff] [blame] | 568 | } |
| Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 569 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 570 | case CodeSynthesisContext::DefaultFunctionArgumentInstantiation: { |
| Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 571 | ParmVarDecl *Param = cast<ParmVarDecl>(Active->Entity); |
| Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 572 | FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
| Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 574 | SmallVector<char, 128> TemplateArgsStr; |
| 575 | llvm::raw_svector_ostream OS(TemplateArgsStr); |
| 576 | FD->printName(OS); |
| Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 577 | printTemplateArgumentList(OS, Active->template_arguments(), |
| 578 | getPrintingPolicy()); |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 579 | Diags.Report(Active->PointOfInstantiation, |
| Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 580 | diag::note_default_function_arg_instantiation_here) |
| Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 581 | << OS.str() |
| Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 582 | << Active->InstantiationRange; |
| 583 | break; |
| 584 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 586 | case CodeSynthesisContext::PriorTemplateArgumentSubstitution: { |
| Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 587 | NamedDecl *Parm = cast<NamedDecl>(Active->Entity); |
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 588 | std::string Name; |
| 589 | if (!Parm->getName().empty()) |
| 590 | Name = std::string(" '") + Parm->getName().str() + "'"; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 591 | |
| 592 | TemplateParameterList *TemplateParams = nullptr; |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 593 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) |
| 594 | TemplateParams = Template->getTemplateParameters(); |
| 595 | else |
| 596 | TemplateParams = |
| 597 | cast<ClassTemplatePartialSpecializationDecl>(Active->Template) |
| 598 | ->getTemplateParameters(); |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 599 | Diags.Report(Active->PointOfInstantiation, |
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 600 | diag::note_prior_template_arg_substitution) |
| 601 | << isa<TemplateTemplateParmDecl>(Parm) |
| 602 | << Name |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 603 | << getTemplateArgumentBindingsText(TemplateParams, |
| 604 | Active->TemplateArgs, |
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 605 | Active->NumTemplateArgs) |
| 606 | << Active->InstantiationRange; |
| 607 | break; |
| 608 | } |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 609 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 610 | case CodeSynthesisContext::DefaultTemplateArgumentChecking: { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 611 | TemplateParameterList *TemplateParams = nullptr; |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 612 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) |
| 613 | TemplateParams = Template->getTemplateParameters(); |
| 614 | else |
| 615 | TemplateParams = |
| 616 | cast<ClassTemplatePartialSpecializationDecl>(Active->Template) |
| 617 | ->getTemplateParameters(); |
| 618 | |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 619 | Diags.Report(Active->PointOfInstantiation, |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 620 | diag::note_template_default_arg_checking) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 621 | << getTemplateArgumentBindingsText(TemplateParams, |
| 622 | Active->TemplateArgs, |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 623 | Active->NumTemplateArgs) |
| 624 | << Active->InstantiationRange; |
| 625 | break; |
| 626 | } |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 627 | |
| Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 628 | case CodeSynthesisContext::ExceptionSpecEvaluation: |
| 629 | Diags.Report(Active->PointOfInstantiation, |
| 630 | diag::note_evaluating_exception_spec_here) |
| 631 | << cast<FunctionDecl>(Active->Entity); |
| 632 | break; |
| 633 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 634 | case CodeSynthesisContext::ExceptionSpecInstantiation: |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 635 | Diags.Report(Active->PointOfInstantiation, |
| 636 | diag::note_template_exception_spec_instantiation_here) |
| Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 637 | << cast<FunctionDecl>(Active->Entity) |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 638 | << Active->InstantiationRange; |
| 639 | break; |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 640 | |
| 641 | case CodeSynthesisContext::DeclaringSpecialMember: |
| 642 | Diags.Report(Active->PointOfInstantiation, |
| 643 | diag::note_in_declaration_of_implicit_special_member) |
| 644 | << cast<CXXRecordDecl>(Active->Entity) << Active->SpecialMember; |
| 645 | break; |
| Richard Smith | 883dbc4 | 2017-05-25 22:47:05 +0000 | [diff] [blame] | 646 | |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 647 | case CodeSynthesisContext::DefiningSynthesizedFunction: { |
| Richard Smith | 883dbc4 | 2017-05-25 22:47:05 +0000 | [diff] [blame] | 648 | // FIXME: For synthesized members other than special members, produce a note. |
| 649 | auto *MD = dyn_cast<CXXMethodDecl>(Active->Entity); |
| 650 | auto CSM = MD ? getSpecialMember(MD) : CXXInvalid; |
| 651 | if (CSM != CXXInvalid) { |
| 652 | Diags.Report(Active->PointOfInstantiation, |
| 653 | diag::note_member_synthesized_at) |
| 654 | << CSM << Context.getTagDeclType(MD->getParent()); |
| 655 | } |
| 656 | break; |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 657 | } |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 658 | |
| 659 | case CodeSynthesisContext::Memoization: |
| 660 | break; |
| 661 | } |
| Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 665 | Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const { |
| Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 666 | if (InNonInstantiationSFINAEContext) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 667 | return Optional<TemplateDeductionInfo *>(nullptr); |
| Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 668 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 669 | for (SmallVectorImpl<CodeSynthesisContext>::const_reverse_iterator |
| 670 | Active = CodeSynthesisContexts.rbegin(), |
| 671 | ActiveEnd = CodeSynthesisContexts.rend(); |
| Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 672 | Active != ActiveEnd; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 673 | ++Active) |
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 674 | { |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 675 | switch (Active->Kind) { |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 676 | case CodeSynthesisContext::TemplateInstantiation: |
| Richard Smith | 72249ba | 2012-04-26 07:24:08 +0000 | [diff] [blame] | 677 | // An instantiation of an alias template may or may not be a SFINAE |
| 678 | // context, depending on what else is on the stack. |
| Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 679 | if (isa<TypeAliasTemplateDecl>(Active->Entity)) |
| Richard Smith | 72249ba | 2012-04-26 07:24:08 +0000 | [diff] [blame] | 680 | break; |
| Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 681 | LLVM_FALLTHROUGH; |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 682 | case CodeSynthesisContext::DefaultFunctionArgumentInstantiation: |
| 683 | case CodeSynthesisContext::ExceptionSpecInstantiation: |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 684 | // This is a template instantiation, so there is no SFINAE. |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 685 | return None; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 687 | case CodeSynthesisContext::DefaultTemplateArgumentInstantiation: |
| 688 | case CodeSynthesisContext::PriorTemplateArgumentSubstitution: |
| 689 | case CodeSynthesisContext::DefaultTemplateArgumentChecking: |
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 690 | // A default template argument instantiation and substitution into |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 691 | // template parameters with arguments for prior parameters may or may |
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 692 | // not be a SFINAE context; look further up the stack. |
| Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 693 | break; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 695 | case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution: |
| 696 | case CodeSynthesisContext::DeducedTemplateArgumentSubstitution: |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 697 | // We're either substitution explicitly-specified template arguments |
| 698 | // or deduced template arguments, so SFINAE applies. |
| Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 699 | assert(Active->DeductionInfo && "Missing deduction info pointer"); |
| 700 | return Active->DeductionInfo; |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 701 | |
| 702 | case CodeSynthesisContext::DeclaringSpecialMember: |
| Richard Smith | 883dbc4 | 2017-05-25 22:47:05 +0000 | [diff] [blame] | 703 | case CodeSynthesisContext::DefiningSynthesizedFunction: |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 704 | // This happens in a context unrelated to template instantiation, so |
| 705 | // there is no SFINAE. |
| 706 | return None; |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 707 | |
| Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 708 | case CodeSynthesisContext::ExceptionSpecEvaluation: |
| 709 | // FIXME: This should not be treated as a SFINAE context, because |
| 710 | // we will cache an incorrect exception specification. However, clang |
| 711 | // bootstrap relies this! See PR31692. |
| 712 | break; |
| 713 | |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 714 | case CodeSynthesisContext::Memoization: |
| 715 | break; |
| Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 716 | } |
| Richard Smith | 1338122 | 2017-02-23 21:43:43 +0000 | [diff] [blame] | 717 | |
| 718 | // The inner context was transparent for SFINAE. If it occurred within a |
| 719 | // non-instantiation SFINAE context, then SFINAE applies. |
| 720 | if (Active->SavedInNonInstantiationSFINAEContext) |
| 721 | return Optional<TemplateDeductionInfo *>(nullptr); |
| Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 724 | return None; |
| Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 727 | //===----------------------------------------------------------------------===/ |
| 728 | // Template Instantiation for Types |
| 729 | //===----------------------------------------------------------------------===/ |
| Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 730 | namespace { |
| Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 731 | class TemplateInstantiator : public TreeTransform<TemplateInstantiator> { |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 732 | const MultiLevelTemplateArgumentList &TemplateArgs; |
| Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 733 | SourceLocation Loc; |
| 734 | DeclarationName Entity; |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 735 | |
| Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 736 | public: |
| Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 737 | typedef TreeTransform<TemplateInstantiator> inherited; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | |
| 739 | TemplateInstantiator(Sema &SemaRef, |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 740 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 741 | SourceLocation Loc, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | DeclarationName Entity) |
| 743 | : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
| Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 744 | Entity(Entity) { } |
| Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 745 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 746 | /// Determine whether the given type \p T has already been |
| Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 747 | /// transformed. |
| 748 | /// |
| 749 | /// For the purposes of template instantiation, a type has already been |
| 750 | /// transformed if it is NULL or if it is not dependent. |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 751 | bool AlreadyTransformed(QualType T); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 753 | /// Returns the location of the entity being instantiated, if known. |
| Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 754 | SourceLocation getBaseLocation() { return Loc; } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 756 | /// Returns the name of the entity being instantiated, if any. |
| Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 757 | DeclarationName getBaseEntity() { return Entity; } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 759 | /// Sets the "base" location and entity when that |
| Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 760 | /// information is known based on another transformation. |
| 761 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 762 | this->Loc = Loc; |
| 763 | this->Entity = Entity; |
| 764 | } |
| Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 765 | |
| 766 | bool TryExpandParameterPacks(SourceLocation EllipsisLoc, |
| 767 | SourceRange PatternRange, |
| Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 768 | ArrayRef<UnexpandedParameterPack> Unexpanded, |
| 769 | bool &ShouldExpand, bool &RetainExpansion, |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 770 | Optional<unsigned> &NumExpansions) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 771 | return getSema().CheckParameterPacksForExpansion(EllipsisLoc, |
| Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 772 | PatternRange, Unexpanded, |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 773 | TemplateArgs, |
| Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 774 | ShouldExpand, |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 775 | RetainExpansion, |
| Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 776 | NumExpansions); |
| 777 | } |
| Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 778 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 779 | void ExpandingFunctionParameterPack(ParmVarDecl *Pack) { |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 780 | SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack); |
| 781 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 782 | |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 783 | TemplateArgument ForgetPartiallySubstitutedPack() { |
| 784 | TemplateArgument Result; |
| 785 | if (NamedDecl *PartialPack |
| 786 | = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
| 787 | MultiLevelTemplateArgumentList &TemplateArgs |
| 788 | = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
| 789 | unsigned Depth, Index; |
| Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 790 | std::tie(Depth, Index) = getDepthAndIndex(PartialPack); |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 791 | if (TemplateArgs.hasTemplateArgument(Depth, Index)) { |
| 792 | Result = TemplateArgs(Depth, Index); |
| 793 | TemplateArgs.setArgument(Depth, Index, TemplateArgument()); |
| 794 | } |
| 795 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 796 | |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 797 | return Result; |
| 798 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 799 | |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 800 | void RememberPartiallySubstitutedPack(TemplateArgument Arg) { |
| 801 | if (Arg.isNull()) |
| 802 | return; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 803 | |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 804 | if (NamedDecl *PartialPack |
| 805 | = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
| 806 | MultiLevelTemplateArgumentList &TemplateArgs |
| 807 | = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
| 808 | unsigned Depth, Index; |
| Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 809 | std::tie(Depth, Index) = getDepthAndIndex(PartialPack); |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 810 | TemplateArgs.setArgument(Depth, Index, Arg); |
| 811 | } |
| 812 | } |
| 813 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 814 | /// Transform the given declaration by instantiating a reference to |
| Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 815 | /// this declaration. |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 816 | Decl *TransformDecl(SourceLocation Loc, Decl *D); |
| Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 817 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 818 | void transformAttrs(Decl *Old, Decl *New) { |
| Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 819 | SemaRef.InstantiateAttrs(TemplateArgs, Old, New); |
| 820 | } |
| 821 | |
| 822 | void transformedLocalDecl(Decl *Old, Decl *New) { |
| Richard Smith | c38498f | 2015-04-27 21:27:54 +0000 | [diff] [blame] | 823 | // If we've instantiated the call operator of a lambda or the call |
| 824 | // operator template of a generic lambda, update the "instantiation of" |
| 825 | // information. |
| 826 | auto *NewMD = dyn_cast<CXXMethodDecl>(New); |
| 827 | if (NewMD && isLambdaCallOperator(NewMD)) { |
| 828 | auto *OldMD = dyn_cast<CXXMethodDecl>(Old); |
| 829 | if (auto *NewTD = NewMD->getDescribedFunctionTemplate()) |
| 830 | NewTD->setInstantiatedFromMemberTemplate( |
| 831 | OldMD->getDescribedFunctionTemplate()); |
| 832 | else |
| 833 | NewMD->setInstantiationOfMemberFunction(OldMD, |
| 834 | TSK_ImplicitInstantiation); |
| 835 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 836 | |
| Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 837 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Old, New); |
| Richard Smith | c7649dc | 2016-03-23 20:07:07 +0000 | [diff] [blame] | 838 | |
| 839 | // We recreated a local declaration, but not by instantiating it. There |
| 840 | // may be pending dependent diagnostics to produce. |
| 841 | if (auto *DC = dyn_cast<DeclContext>(Old)) |
| 842 | SemaRef.PerformDependentDiagnostics(DC, TemplateArgs); |
| Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 843 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 844 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 845 | /// Transform the definition of the given declaration by |
| Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 846 | /// instantiating it. |
| Douglas Gregor | 2528936 | 2010-03-01 17:25:41 +0000 | [diff] [blame] | 847 | Decl *TransformDefinition(SourceLocation Loc, Decl *D); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 849 | /// Transform the first qualifier within a scope by instantiating the |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 850 | /// declaration. |
| 851 | NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 852 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 853 | /// Rebuild the exception declaration and register the declaration |
| Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 854 | /// as an instantiated local. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 855 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 856 | TypeSourceInfo *Declarator, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 857 | SourceLocation StartLoc, |
| 858 | SourceLocation NameLoc, |
| 859 | IdentifierInfo *Name); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 860 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 861 | /// Rebuild the Objective-C exception declaration and register the |
| Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 862 | /// declaration as an instantiated local. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 863 | VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
| Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 864 | TypeSourceInfo *TSInfo, QualType T); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 865 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 866 | /// Check for tag mismatches when instantiating an |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 867 | /// elaborated type. |
| John McCall | 954b5de | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 868 | QualType RebuildElaboratedType(SourceLocation KeywordLoc, |
| 869 | ElaboratedTypeKeyword Keyword, |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 870 | NestedNameSpecifierLoc QualifierLoc, |
| 871 | QualType T); |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 872 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 873 | TemplateName |
| 874 | TransformTemplateName(CXXScopeSpec &SS, TemplateName Name, |
| 875 | SourceLocation NameLoc, |
| 876 | QualType ObjectType = QualType(), |
| Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 877 | NamedDecl *FirstQualifierInScope = nullptr, |
| 878 | bool AllowInjectedClassName = false); |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 879 | |
| Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 880 | const LoopHintAttr *TransformLoopHintAttr(const LoopHintAttr *LH); |
| 881 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 882 | ExprResult TransformPredefinedExpr(PredefinedExpr *E); |
| 883 | ExprResult TransformDeclRefExpr(DeclRefExpr *E); |
| 884 | ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 885 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 886 | ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E, |
| Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 887 | NonTypeTemplateParmDecl *D); |
| Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 888 | ExprResult TransformSubstNonTypeTemplateParmPackExpr( |
| 889 | SubstNonTypeTemplateParmPackExpr *E); |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 890 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 891 | /// Rebuild a DeclRefExpr for a ParmVarDecl reference. |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 892 | ExprResult RebuildParmVarDeclRefExpr(ParmVarDecl *PD, SourceLocation Loc); |
| 893 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 894 | /// Transform a reference to a function parameter pack. |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 895 | ExprResult TransformFunctionParmPackRefExpr(DeclRefExpr *E, |
| 896 | ParmVarDecl *PD); |
| 897 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 898 | /// Transform a FunctionParmPackExpr which was built when we couldn't |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 899 | /// expand a function parameter pack reference which refers to an expanded |
| 900 | /// pack. |
| 901 | ExprResult TransformFunctionParmPackExpr(FunctionParmPackExpr *E); |
| 902 | |
| Hans Wennborg | e113c20 | 2014-09-18 16:01:32 +0000 | [diff] [blame] | 903 | QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 904 | FunctionProtoTypeLoc TL) { |
| 905 | // Call the base version; it will forward to our overridden version below. |
| 906 | return inherited::TransformFunctionProtoType(TLB, TL); |
| 907 | } |
| 908 | |
| 909 | template<typename Fn> |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 910 | QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
| 911 | FunctionProtoTypeLoc TL, |
| 912 | CXXRecordDecl *ThisContext, |
| Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 913 | Qualifiers ThisTypeQuals, |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 914 | Fn TransformExceptionSpec); |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 915 | |
| Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 916 | ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm, |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 917 | int indexAdjustment, |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 918 | Optional<unsigned> NumExpansions, |
| Douglas Gregor | 0dd22bc | 2012-01-25 16:15:54 +0000 | [diff] [blame] | 919 | bool ExpectParameterPack); |
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 920 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 921 | /// Transforms a template type parameter type by performing |
| Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 922 | /// substitution of the corresponding template type argument. |
| John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 923 | QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| John McCall | 31f8272 | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 924 | TemplateTypeParmTypeLoc TL); |
| Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 925 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 926 | /// Transforms an already-substituted template type parameter pack |
| Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 927 | /// into either itself (if we aren't substituting into its pack expansion) |
| 928 | /// or the appropriate substituted argument. |
| 929 | QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB, |
| 930 | SubstTemplateTypeParmPackTypeLoc TL); |
| 931 | |
| Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 932 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 933 | LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true); |
| 934 | return TreeTransform<TemplateInstantiator>::TransformLambdaExpr(E); |
| 935 | } |
| 936 | |
| David Majnemer | b100410 | 2014-03-02 18:46:05 +0000 | [diff] [blame] | 937 | TemplateParameterList *TransformTemplateParameterList( |
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 938 | TemplateParameterList *OrigTPL) { |
| 939 | if (!OrigTPL || !OrigTPL->size()) return OrigTPL; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 940 | |
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 941 | DeclContext *Owner = OrigTPL->getParam(0)->getDeclContext(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 942 | TemplateDeclInstantiator DeclInstantiator(getSema(), |
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 943 | /* DeclContext *Owner */ Owner, TemplateArgs); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 944 | return DeclInstantiator.SubstTemplateParams(OrigTPL); |
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 945 | } |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 946 | private: |
| 947 | ExprResult transformNonTypeTemplateParmRef(NonTypeTemplateParmDecl *parm, |
| 948 | SourceLocation loc, |
| Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 949 | TemplateArgument arg); |
| Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 950 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 951 | } |
| Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 952 | |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 953 | bool TemplateInstantiator::AlreadyTransformed(QualType T) { |
| 954 | if (T.isNull()) |
| 955 | return true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 956 | |
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 957 | if (T->isInstantiationDependentType() || T->isVariablyModifiedType()) |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 958 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 959 | |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 960 | getSema().MarkDeclarationsReferencedInType(Loc, T); |
| 961 | return true; |
| 962 | } |
| 963 | |
| Eli Friedman | 8917ad5 | 2013-07-19 19:40:38 +0000 | [diff] [blame] | 964 | static TemplateArgument |
| 965 | getPackSubstitutedTemplateArgument(Sema &S, TemplateArgument Arg) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 966 | assert(S.ArgumentPackSubstitutionIndex >= 0); |
| Eli Friedman | 8917ad5 | 2013-07-19 19:40:38 +0000 | [diff] [blame] | 967 | assert(S.ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); |
| 968 | Arg = Arg.pack_begin()[S.ArgumentPackSubstitutionIndex]; |
| 969 | if (Arg.isPackExpansion()) |
| 970 | Arg = Arg.getPackExpansionPattern(); |
| 971 | return Arg; |
| 972 | } |
| 973 | |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 974 | Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) { |
| Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 975 | if (!D) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 976 | return nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | |
| Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 978 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 979 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| Douglas Gregor | b9397108 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 980 | // If the corresponding template argument is NULL or non-existent, it's |
| 981 | // because we are performing instantiation from explicitly-specified |
| 982 | // template arguments in a function template, but there were some |
| 983 | // arguments left unspecified. |
| 984 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
| 985 | TTP->getPosition())) |
| 986 | return D; |
| 987 | |
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 988 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 989 | |
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 990 | if (TTP->isParameterPack()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 991 | assert(Arg.getKind() == TemplateArgument::Pack && |
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 992 | "Missing argument pack"); |
| Eli Friedman | 8917ad5 | 2013-07-19 19:40:38 +0000 | [diff] [blame] | 993 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| Richard Smith | 1abacfc | 2017-08-29 22:14:43 +0000 | [diff] [blame] | 996 | TemplateName Template = Arg.getAsTemplate().getNameToSubstitute(); |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 997 | assert(!Template.isNull() && Template.getAsTemplateDecl() && |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 998 | "Wrong kind of template template argument"); |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 999 | return Template.getAsTemplateDecl(); |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1000 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1002 | // Fall through to find the instantiated declaration for this template |
| 1003 | // template parameter. |
| Douglas Gregor | 71dc509 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 1004 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1006 | return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs); |
| Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| Douglas Gregor | 2528936 | 2010-03-01 17:25:41 +0000 | [diff] [blame] | 1009 | Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) { |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1010 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
| Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 1011 | if (!Inst) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1012 | return nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | |
| Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 1014 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 1015 | return Inst; |
| 1016 | } |
| 1017 | |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 1018 | NamedDecl * |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1019 | TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 1020 | SourceLocation Loc) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1021 | // If the first part of the nested-name-specifier was a template type |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 1022 | // parameter, instantiate that type parameter down to a tag type. |
| 1023 | if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1024 | const TemplateTypeParmType *TTP |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 1025 | = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1026 | |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 1027 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| Douglas Gregor | 53c3f4e | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 1028 | // FIXME: This needs testing w/ member access expressions. |
| 1029 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1030 | |
| Douglas Gregor | 53c3f4e | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 1031 | if (TTP->isParameterPack()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1032 | assert(Arg.getKind() == TemplateArgument::Pack && |
| Douglas Gregor | 53c3f4e | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 1033 | "Missing argument pack"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1034 | |
| Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 1035 | if (getSema().ArgumentPackSubstitutionIndex == -1) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1036 | return nullptr; |
| 1037 | |
| Eli Friedman | 8917ad5 | 2013-07-19 19:40:38 +0000 | [diff] [blame] | 1038 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
| Douglas Gregor | 53c3f4e | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | QualType T = Arg.getAsType(); |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 1042 | if (T.isNull()) |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1043 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1044 | |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 1045 | if (const TagType *Tag = T->getAs<TagType>()) |
| 1046 | return Tag->getDecl(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1047 | |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 1048 | // The resulting type is not a tag; complain. |
| 1049 | getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1050 | return nullptr; |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 1051 | } |
| 1052 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1053 | |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1054 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
| Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 1057 | VarDecl * |
| 1058 | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1059 | TypeSourceInfo *Declarator, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1060 | SourceLocation StartLoc, |
| 1061 | SourceLocation NameLoc, |
| 1062 | IdentifierInfo *Name) { |
| Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 1063 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1064 | StartLoc, NameLoc, Name); |
| Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 1065 | if (Var) |
| 1066 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 1067 | return Var; |
| 1068 | } |
| 1069 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1070 | VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
| 1071 | TypeSourceInfo *TSInfo, |
| Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 1072 | QualType T) { |
| 1073 | VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T); |
| 1074 | if (Var) |
| Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 1075 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 1076 | return Var; |
| 1077 | } |
| 1078 | |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1079 | QualType |
| John McCall | 954b5de | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 1080 | TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc, |
| 1081 | ElaboratedTypeKeyword Keyword, |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1082 | NestedNameSpecifierLoc QualifierLoc, |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1083 | QualType T) { |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1084 | if (const TagType *TT = T->getAs<TagType>()) { |
| 1085 | TagDecl* TD = TT->getDecl(); |
| 1086 | |
| John McCall | 954b5de | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 1087 | SourceLocation TagLocation = KeywordLoc; |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1088 | |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1089 | IdentifierInfo *Id = TD->getIdentifier(); |
| 1090 | |
| 1091 | // TODO: should we even warn on struct/class mismatches for this? Seems |
| 1092 | // like it's likely to produce a lot of spurious errors. |
| Richard Smith | 80b3c5a | 2012-08-17 00:12:27 +0000 | [diff] [blame] | 1093 | if (Id && Keyword != ETK_None && Keyword != ETK_Typename) { |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1094 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword); |
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1095 | if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, /*isDefinition*/false, |
| Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1096 | TagLocation, Id)) { |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1097 | SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) |
| 1098 | << Id |
| 1099 | << FixItHint::CreateReplacement(SourceRange(TagLocation), |
| 1100 | TD->getKindName()); |
| 1101 | SemaRef.Diag(TD->getLocation(), diag::note_previous_use); |
| 1102 | } |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
| John McCall | 954b5de | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 1106 | return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc, |
| 1107 | Keyword, |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1108 | QualifierLoc, |
| 1109 | T); |
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 1112 | TemplateName TemplateInstantiator::TransformTemplateName( |
| 1113 | CXXScopeSpec &SS, TemplateName Name, SourceLocation NameLoc, |
| 1114 | QualType ObjectType, NamedDecl *FirstQualifierInScope, |
| 1115 | bool AllowInjectedClassName) { |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1116 | if (TemplateTemplateParmDecl *TTP |
| 1117 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) { |
| 1118 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 1119 | // If the corresponding template argument is NULL or non-existent, it's |
| 1120 | // because we are performing instantiation from explicitly-specified |
| 1121 | // template arguments in a function template, but there were some |
| 1122 | // arguments left unspecified. |
| 1123 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
| 1124 | TTP->getPosition())) |
| 1125 | return Name; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1126 | |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1127 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1128 | |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1129 | if (TTP->isParameterPack()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1130 | assert(Arg.getKind() == TemplateArgument::Pack && |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1131 | "Missing argument pack"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1132 | |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1133 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 1134 | // We have the template argument pack to substitute, but we're not |
| 1135 | // actually expanding the enclosing pack expansion yet. So, just |
| 1136 | // keep the entire argument pack. |
| 1137 | return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg); |
| 1138 | } |
| Eli Friedman | 8917ad5 | 2013-07-19 19:40:38 +0000 | [diff] [blame] | 1139 | |
| 1140 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1141 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1142 | |
| Richard Smith | 1abacfc | 2017-08-29 22:14:43 +0000 | [diff] [blame] | 1143 | TemplateName Template = Arg.getAsTemplate().getNameToSubstitute(); |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1144 | assert(!Template.isNull() && "Null template template argument"); |
| Richard Smith | 1abacfc | 2017-08-29 22:14:43 +0000 | [diff] [blame] | 1145 | assert(!Template.getAsQualifiedTemplateName() && |
| 1146 | "template decl to substitute is qualified?"); |
| John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1147 | |
| 1148 | Template = getSema().Context.getSubstTemplateTemplateParm(TTP, Template); |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1149 | return Template; |
| 1150 | } |
| 1151 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1152 | |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1153 | if (SubstTemplateTemplateParmPackStorage *SubstPack |
| 1154 | = Name.getAsSubstTemplateTemplateParmPack()) { |
| 1155 | if (getSema().ArgumentPackSubstitutionIndex == -1) |
| 1156 | return Name; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1157 | |
| Eli Friedman | 8917ad5 | 2013-07-19 19:40:38 +0000 | [diff] [blame] | 1158 | TemplateArgument Arg = SubstPack->getArgumentPack(); |
| 1159 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
| Richard Smith | 1abacfc | 2017-08-29 22:14:43 +0000 | [diff] [blame] | 1160 | return Arg.getAsTemplate().getNameToSubstitute(); |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1161 | } |
| Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 1162 | |
| 1163 | return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType, |
| 1164 | FirstQualifierInScope, |
| 1165 | AllowInjectedClassName); |
| Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1168 | ExprResult |
| John McCall | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 1169 | TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { |
| Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1170 | if (!E->isTypeDependent()) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1171 | return E; |
| Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1172 | |
| Bruno Ricci | 17ff026 | 2018-10-27 19:21:19 +0000 | [diff] [blame] | 1173 | return getSema().BuildPredefinedExpr(E->getLocation(), E->getIdentKind()); |
| Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1176 | ExprResult |
| John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1177 | TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E, |
| Douglas Gregor | 6c379e2 | 2010-02-08 23:41:45 +0000 | [diff] [blame] | 1178 | NonTypeTemplateParmDecl *NTTP) { |
| John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1179 | // If the corresponding template argument is NULL or non-existent, it's |
| 1180 | // because we are performing instantiation from explicitly-specified |
| 1181 | // template arguments in a function template, but there were some |
| 1182 | // arguments left unspecified. |
| 1183 | if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
| 1184 | NTTP->getPosition())) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1185 | return E; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | |
| Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 1187 | TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition()); |
| Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1188 | |
| 1189 | if (TemplateArgs.getNumLevels() != TemplateArgs.getNumSubstitutedLevels()) { |
| 1190 | // We're performing a partial substitution, so the substituted argument |
| 1191 | // could be dependent. As a result we can't create a SubstNonType*Expr |
| 1192 | // node now, since that represents a fully-substituted argument. |
| 1193 | // FIXME: We should have some AST representation for this. |
| 1194 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 1195 | // FIXME: This won't work for alias templates. |
| 1196 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() && |
| 1197 | "unexpected pack arguments in partial substitution"); |
| 1198 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 1199 | } |
| 1200 | assert(Arg.getKind() == TemplateArgument::Expression && |
| 1201 | "unexpected nontype template argument kind in partial substitution"); |
| 1202 | return Arg.getAsExpr(); |
| 1203 | } |
| 1204 | |
| Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 1205 | if (NTTP->isParameterPack()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1206 | assert(Arg.getKind() == TemplateArgument::Pack && |
| Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 1207 | "Missing argument pack"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1208 | |
| Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 1209 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 1210 | // We have an argument pack, but we can't select a particular argument |
| 1211 | // out of it yet. Therefore, we'll build an expression to hold on to that |
| 1212 | // argument pack. |
| 1213 | QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs, |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1214 | E->getLocation(), |
| Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 1215 | NTTP->getDeclName()); |
| 1216 | if (TargetType.isNull()) |
| 1217 | return ExprError(); |
| Richard Smith | f1f20e6 | 2018-02-14 02:07:53 +0000 | [diff] [blame] | 1218 | |
| 1219 | return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr( |
| 1220 | TargetType.getNonLValueExprType(SemaRef.Context), |
| 1221 | TargetType->isReferenceType() ? VK_LValue : VK_RValue, NTTP, |
| 1222 | E->getLocation(), Arg); |
| Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 1223 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1224 | |
| Eli Friedman | 8917ad5 | 2013-07-19 19:40:38 +0000 | [diff] [blame] | 1225 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
| Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 1226 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1228 | return transformNonTypeTemplateParmRef(NTTP, E->getLocation(), Arg); |
| 1229 | } |
| 1230 | |
| Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 1231 | const LoopHintAttr * |
| 1232 | TemplateInstantiator::TransformLoopHintAttr(const LoopHintAttr *LH) { |
| 1233 | Expr *TransformedExpr = getDerived().TransformExpr(LH->getValue()).get(); |
| 1234 | |
| 1235 | if (TransformedExpr == LH->getValue()) |
| 1236 | return LH; |
| 1237 | |
| 1238 | // Generate error if there is a problem with the value. |
| 1239 | if (getSema().CheckLoopHintExpr(TransformedExpr, LH->getLocation())) |
| 1240 | return LH; |
| 1241 | |
| 1242 | // Create new LoopHintValueAttr with integral expression in place of the |
| 1243 | // non-type template parameter. |
| 1244 | return LoopHintAttr::CreateImplicit( |
| 1245 | getSema().Context, LH->getSemanticSpelling(), LH->getOption(), |
| 1246 | LH->getState(), TransformedExpr, LH->getRange()); |
| 1247 | } |
| 1248 | |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1249 | ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef( |
| 1250 | NonTypeTemplateParmDecl *parm, |
| 1251 | SourceLocation loc, |
| Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 1252 | TemplateArgument arg) { |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1253 | ExprResult result; |
| 1254 | QualType type; |
| 1255 | |
| John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1256 | // The template argument itself might be an expression, in which |
| 1257 | // case we just return that expression. |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1258 | if (arg.getKind() == TemplateArgument::Expression) { |
| 1259 | Expr *argExpr = arg.getAsExpr(); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1260 | result = argExpr; |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1261 | type = argExpr->getType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | |
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1263 | } else if (arg.getKind() == TemplateArgument::Declaration || |
| 1264 | arg.getKind() == TemplateArgument::NullPtr) { |
| Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 1265 | ValueDecl *VD; |
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1266 | if (arg.getKind() == TemplateArgument::Declaration) { |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1267 | VD = arg.getAsDecl(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | |
| Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 1269 | // Find the instantiation of the template argument. This is |
| 1270 | // required for nested templates. |
| 1271 | VD = cast_or_null<ValueDecl>( |
| 1272 | getSema().FindInstantiatedDecl(loc, VD, TemplateArgs)); |
| 1273 | if (!VD) |
| 1274 | return ExprError(); |
| 1275 | } else { |
| 1276 | // Propagate NULL template argument. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1277 | VD = nullptr; |
| Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 1278 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1279 | |
| John McCall | 15dda37 | 2010-02-06 10:23:53 +0000 | [diff] [blame] | 1280 | // Derive the type we want the substituted decl to have. This had |
| 1281 | // better be non-dependent, or these checks will have serious problems. |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1282 | if (parm->isExpandedParameterPack()) { |
| 1283 | type = parm->getExpansionType(SemaRef.ArgumentPackSubstitutionIndex); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1284 | } else if (parm->isParameterPack() && |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1285 | isa<PackExpansionType>(parm->getType())) { |
| 1286 | type = SemaRef.SubstType( |
| 1287 | cast<PackExpansionType>(parm->getType())->getPattern(), |
| 1288 | TemplateArgs, loc, parm->getDeclName()); |
| 1289 | } else { |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1290 | type = SemaRef.SubstType(VD ? arg.getParamTypeForDecl() : arg.getNullPtrType(), |
| 1291 | TemplateArgs, loc, parm->getDeclName()); |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1292 | } |
| 1293 | assert(!type.isNull() && "type substitution failed for param type"); |
| 1294 | assert(!type->isDependentType() && "param type still dependent"); |
| 1295 | result = SemaRef.BuildExpressionFromDeclTemplateArgument(arg, type, loc); |
| John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1296 | |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1297 | if (!result.isInvalid()) type = result.get()->getType(); |
| 1298 | } else { |
| 1299 | result = SemaRef.BuildExpressionFromIntegralTemplateArgument(arg, loc); |
| 1300 | |
| 1301 | // Note that this type can be different from the type of 'result', |
| 1302 | // e.g. if it's an enum type. |
| 1303 | type = arg.getIntegralType(); |
| 1304 | } |
| 1305 | if (result.isInvalid()) return ExprError(); |
| 1306 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1307 | Expr *resultExpr = result.get(); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1308 | return new (SemaRef.Context) SubstNonTypeTemplateParmExpr( |
| 1309 | type, resultExpr->getValueKind(), loc, parm, resultExpr); |
| John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1310 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1311 | |
| 1312 | ExprResult |
| Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 1313 | TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr( |
| 1314 | SubstNonTypeTemplateParmPackExpr *E) { |
| 1315 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 1316 | // We aren't expanding the parameter pack, so just return ourselves. |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1317 | return E; |
| Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 1318 | } |
| Eli Friedman | 8917ad5 | 2013-07-19 19:40:38 +0000 | [diff] [blame] | 1319 | |
| 1320 | TemplateArgument Arg = E->getArgumentPack(); |
| 1321 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1322 | return transformNonTypeTemplateParmRef(E->getParameterPack(), |
| 1323 | E->getParameterPackLocation(), |
| 1324 | Arg); |
| Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 1325 | } |
| John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1326 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1327 | ExprResult |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 1328 | TemplateInstantiator::RebuildParmVarDeclRefExpr(ParmVarDecl *PD, |
| 1329 | SourceLocation Loc) { |
| 1330 | DeclarationNameInfo NameInfo(PD->getDeclName(), Loc); |
| 1331 | return getSema().BuildDeclarationNameExpr(CXXScopeSpec(), NameInfo, PD); |
| 1332 | } |
| 1333 | |
| 1334 | ExprResult |
| 1335 | TemplateInstantiator::TransformFunctionParmPackExpr(FunctionParmPackExpr *E) { |
| 1336 | if (getSema().ArgumentPackSubstitutionIndex != -1) { |
| 1337 | // We can expand this parameter pack now. |
| 1338 | ParmVarDecl *D = E->getExpansion(getSema().ArgumentPackSubstitutionIndex); |
| 1339 | ValueDecl *VD = cast_or_null<ValueDecl>(TransformDecl(E->getExprLoc(), D)); |
| 1340 | if (!VD) |
| 1341 | return ExprError(); |
| 1342 | return RebuildParmVarDeclRefExpr(cast<ParmVarDecl>(VD), E->getExprLoc()); |
| 1343 | } |
| 1344 | |
| 1345 | QualType T = TransformType(E->getType()); |
| 1346 | if (T.isNull()) |
| 1347 | return ExprError(); |
| 1348 | |
| 1349 | // Transform each of the parameter expansions into the corresponding |
| 1350 | // parameters in the instantiation of the function decl. |
| James Y Knight | 48fefa3 | 2015-09-30 14:04:23 +0000 | [diff] [blame] | 1351 | SmallVector<ParmVarDecl *, 8> Parms; |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 1352 | Parms.reserve(E->getNumExpansions()); |
| 1353 | for (FunctionParmPackExpr::iterator I = E->begin(), End = E->end(); |
| 1354 | I != End; ++I) { |
| 1355 | ParmVarDecl *D = |
| 1356 | cast_or_null<ParmVarDecl>(TransformDecl(E->getExprLoc(), *I)); |
| 1357 | if (!D) |
| 1358 | return ExprError(); |
| 1359 | Parms.push_back(D); |
| 1360 | } |
| 1361 | |
| 1362 | return FunctionParmPackExpr::Create(getSema().Context, T, |
| 1363 | E->getParameterPack(), |
| 1364 | E->getParameterPackLocation(), Parms); |
| 1365 | } |
| 1366 | |
| 1367 | ExprResult |
| 1368 | TemplateInstantiator::TransformFunctionParmPackRefExpr(DeclRefExpr *E, |
| 1369 | ParmVarDecl *PD) { |
| 1370 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 1371 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found |
| 1372 | = getSema().CurrentInstantiationScope->findInstantiationOf(PD); |
| 1373 | assert(Found && "no instantiation for parameter pack"); |
| 1374 | |
| 1375 | Decl *TransformedDecl; |
| 1376 | if (DeclArgumentPack *Pack = Found->dyn_cast<DeclArgumentPack *>()) { |
| Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1377 | // If this is a reference to a function parameter pack which we can |
| 1378 | // substitute but can't yet expand, build a FunctionParmPackExpr for it. |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 1379 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 1380 | QualType T = TransformType(E->getType()); |
| 1381 | if (T.isNull()) |
| 1382 | return ExprError(); |
| 1383 | return FunctionParmPackExpr::Create(getSema().Context, T, PD, |
| 1384 | E->getExprLoc(), *Pack); |
| 1385 | } |
| 1386 | |
| 1387 | TransformedDecl = (*Pack)[getSema().ArgumentPackSubstitutionIndex]; |
| 1388 | } else { |
| 1389 | TransformedDecl = Found->get<Decl*>(); |
| 1390 | } |
| 1391 | |
| 1392 | // We have either an unexpanded pack or a specific expansion. |
| 1393 | return RebuildParmVarDeclRefExpr(cast<ParmVarDecl>(TransformedDecl), |
| 1394 | E->getExprLoc()); |
| 1395 | } |
| 1396 | |
| 1397 | ExprResult |
| John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1398 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
| 1399 | NamedDecl *D = E->getDecl(); |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Handle references to non-type template parameters and non-type template |
| 1402 | // parameter packs. |
| John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1403 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
| 1404 | if (NTTP->getDepth() < TemplateArgs.getNumLevels()) |
| 1405 | return TransformTemplateParmRefExpr(E, NTTP); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1406 | |
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1407 | // We have a non-type template parameter that isn't fully substituted; |
| 1408 | // FindInstantiatedDecl will find it in the local instantiation scope. |
| Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1409 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 1411 | // Handle references to function parameter packs. |
| 1412 | if (ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D)) |
| 1413 | if (PD->isParameterPack()) |
| 1414 | return TransformFunctionParmPackRefExpr(E, PD); |
| 1415 | |
| John McCall | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 1416 | return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E); |
| Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1419 | ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr( |
| John McCall | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 1420 | CXXDefaultArgExpr *E) { |
| Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 1421 | assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())-> |
| 1422 | getDescribedFunctionTemplate() && |
| 1423 | "Default arg expressions are never formed in dependent cases."); |
| Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 1424 | return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(), |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1425 | cast<FunctionDecl>(E->getParam()->getDeclContext()), |
| Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 1426 | E->getParam()); |
| Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 1429 | template<typename Fn> |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1430 | QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB, |
| 1431 | FunctionProtoTypeLoc TL, |
| 1432 | CXXRecordDecl *ThisContext, |
| Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 1433 | Qualifiers ThisTypeQuals, |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 1434 | Fn TransformExceptionSpec) { |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1435 | // We need a local instantiation scope for this function prototype. |
| 1436 | LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true); |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 1437 | return inherited::TransformFunctionProtoType( |
| 1438 | TLB, TL, ThisContext, ThisTypeQuals, TransformExceptionSpec); |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1441 | ParmVarDecl * |
| Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 1442 | TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm, |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1443 | int indexAdjustment, |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1444 | Optional<unsigned> NumExpansions, |
| Douglas Gregor | 0dd22bc | 2012-01-25 16:15:54 +0000 | [diff] [blame] | 1445 | bool ExpectParameterPack) { |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1446 | return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment, |
| Douglas Gregor | 0dd22bc | 2012-01-25 16:15:54 +0000 | [diff] [blame] | 1447 | NumExpansions, ExpectParameterPack); |
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | QualType |
| John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1451 | TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| John McCall | 31f8272 | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 1452 | TemplateTypeParmTypeLoc TL) { |
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1453 | const TemplateTypeParmType *T = TL.getTypePtr(); |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1454 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1455 | // Replace the template type parameter with its corresponding |
| 1456 | // template argument. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
| 1458 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 1459 | // because we are performing instantiation from explicitly-specified |
| 1460 | // template arguments in a function template class, but there were some |
| Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1461 | // arguments left unspecified. |
| John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1462 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
| 1463 | TemplateTypeParmTypeLoc NewTL |
| 1464 | = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
| 1465 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1466 | return TL.getType(); |
| 1467 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
| Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1469 | TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1470 | |
| Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1471 | if (T->isParameterPack()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1472 | assert(Arg.getKind() == TemplateArgument::Pack && |
| Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1473 | "Missing argument pack"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1474 | |
| Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1475 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 1476 | // We have the template argument pack, but we're not expanding the |
| 1477 | // enclosing pack expansion yet. Just save the template argument |
| 1478 | // pack for later substitution. |
| 1479 | QualType Result |
| 1480 | = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg); |
| 1481 | SubstTemplateTypeParmPackTypeLoc NewTL |
| 1482 | = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result); |
| 1483 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1484 | return Result; |
| Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1485 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1486 | |
| Eli Friedman | 8917ad5 | 2013-07-19 19:40:38 +0000 | [diff] [blame] | 1487 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
| Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1488 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1489 | |
| Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1490 | assert(Arg.getKind() == TemplateArgument::Type && |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1491 | "Template argument kind mismatch"); |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1492 | |
| Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1493 | QualType Replacement = Arg.getAsType(); |
| John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1494 | |
| 1495 | // TODO: only do this uniquing once, at the start of instantiation. |
| John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1496 | QualType Result |
| 1497 | = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); |
| 1498 | SubstTemplateTypeParmTypeLoc NewTL |
| 1499 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 1500 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1501 | return Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | } |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1503 | |
| 1504 | // The template type parameter comes from an inner template (e.g., |
| 1505 | // the template parameter list of a member template inside the |
| 1506 | // template we are instantiating). Create a new template type |
| 1507 | // parameter with the template "level" reduced by one. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1508 | TemplateTypeParmDecl *NewTTPDecl = nullptr; |
| Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1509 | if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl()) |
| 1510 | NewTTPDecl = cast_or_null<TemplateTypeParmDecl>( |
| 1511 | TransformDecl(TL.getNameLoc(), OldTTPDecl)); |
| 1512 | |
| Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1513 | QualType Result = getSema().Context.getTemplateTypeParmType( |
| 1514 | T->getDepth() - TemplateArgs.getNumSubstitutedLevels(), T->getIndex(), |
| 1515 | T->isParameterPack(), NewTTPDecl); |
| John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1516 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 1517 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1518 | return Result; |
| Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1519 | } |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1520 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1521 | QualType |
| Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 1522 | TemplateInstantiator::TransformSubstTemplateTypeParmPackType( |
| 1523 | TypeLocBuilder &TLB, |
| 1524 | SubstTemplateTypeParmPackTypeLoc TL) { |
| 1525 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 1526 | // We aren't expanding the parameter pack, so just return ourselves. |
| 1527 | SubstTemplateTypeParmPackTypeLoc NewTL |
| 1528 | = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType()); |
| 1529 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1530 | return TL.getType(); |
| 1531 | } |
| Eli Friedman | 8917ad5 | 2013-07-19 19:40:38 +0000 | [diff] [blame] | 1532 | |
| 1533 | TemplateArgument Arg = TL.getTypePtr()->getArgumentPack(); |
| 1534 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
| 1535 | QualType Result = Arg.getAsType(); |
| 1536 | |
| Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 1537 | Result = getSema().Context.getSubstTemplateTypeParmType( |
| 1538 | TL.getTypePtr()->getReplacedParameter(), |
| 1539 | Result); |
| 1540 | SubstTemplateTypeParmTypeLoc NewTL |
| 1541 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 1542 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1543 | return Result; |
| 1544 | } |
| 1545 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1546 | /// Perform substitution on the type T with a given set of template |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1547 | /// arguments. |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1548 | /// |
| 1549 | /// This routine substitutes the given template arguments into the |
| 1550 | /// type T and produces the instantiated type. |
| 1551 | /// |
| 1552 | /// \param T the type into which the template arguments will be |
| 1553 | /// substituted. If this type is not dependent, it will be returned |
| 1554 | /// immediately. |
| 1555 | /// |
| James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1556 | /// \param Args the template arguments that will be |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1557 | /// substituted for the top-level template parameters within T. |
| 1558 | /// |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1559 | /// \param Loc the location in the source code where this substitution |
| 1560 | /// is being performed. It will typically be the location of the |
| 1561 | /// declarator (if we're instantiating the type of some declaration) |
| 1562 | /// or the location of the type in the source code (if, e.g., we're |
| 1563 | /// instantiating the type of a cast expression). |
| 1564 | /// |
| 1565 | /// \param Entity the name of the entity associated with a declaration |
| 1566 | /// being instantiated (if any). May be empty to indicate that there |
| 1567 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 1568 | /// a cast expression) or that the entity has no name (e.g., an |
| 1569 | /// unnamed function parameter). |
| 1570 | /// |
| Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 1571 | /// \param AllowDeducedTST Whether a DeducedTemplateSpecializationType is |
| 1572 | /// acceptable as the top level type of the result. |
| 1573 | /// |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1574 | /// \returns If the instantiation succeeds, the instantiated |
| 1575 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1576 | TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T, |
| John McCall | 609459e | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 1577 | const MultiLevelTemplateArgumentList &Args, |
| 1578 | SourceLocation Loc, |
| Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 1579 | DeclarationName Entity, |
| 1580 | bool AllowDeducedTST) { |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 1581 | assert(!CodeSynthesisContexts.empty() && |
| John McCall | 609459e | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 1582 | "Cannot perform an instantiation without some context on the " |
| 1583 | "instantiation stack"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1584 | |
| 1585 | if (!T->getType()->isInstantiationDependentType() && |
| Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 1586 | !T->getType()->isVariablyModifiedType()) |
| John McCall | 609459e | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 1587 | return T; |
| 1588 | |
| 1589 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 1590 | return AllowDeducedTST ? Instantiator.TransformTypeWithDeducedTST(T) |
| 1591 | : Instantiator.TransformType(T); |
| John McCall | 609459e | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1594 | TypeSourceInfo *Sema::SubstType(TypeLoc TL, |
| 1595 | const MultiLevelTemplateArgumentList &Args, |
| 1596 | SourceLocation Loc, |
| 1597 | DeclarationName Entity) { |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 1598 | assert(!CodeSynthesisContexts.empty() && |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1599 | "Cannot perform an instantiation without some context on the " |
| 1600 | "instantiation stack"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1601 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1602 | if (TL.getType().isNull()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1603 | return nullptr; |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1604 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1605 | if (!TL.getType()->isInstantiationDependentType() && |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1606 | !TL.getType()->isVariablyModifiedType()) { |
| 1607 | // FIXME: Make a copy of the TypeLoc data here, so that we can |
| 1608 | // return a new TypeSourceInfo. Inefficient! |
| 1609 | TypeLocBuilder TLB; |
| 1610 | TLB.pushFullCopy(TL); |
| 1611 | return TLB.getTypeSourceInfo(Context, TL.getType()); |
| 1612 | } |
| 1613 | |
| 1614 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 1615 | TypeLocBuilder TLB; |
| 1616 | TLB.reserve(TL.getFullDataSize()); |
| 1617 | QualType Result = Instantiator.TransformType(TLB, TL); |
| 1618 | if (Result.isNull()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1619 | return nullptr; |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1620 | |
| 1621 | return TLB.getTypeSourceInfo(Context, Result); |
| 1622 | } |
| 1623 | |
| John McCall | 609459e | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 1624 | /// Deprecated form of the above. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | QualType Sema::SubstType(QualType T, |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1626 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1627 | SourceLocation Loc, DeclarationName Entity) { |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 1628 | assert(!CodeSynthesisContexts.empty() && |
| Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1629 | "Cannot perform an instantiation without some context on the " |
| 1630 | "instantiation stack"); |
| 1631 | |
| Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 1632 | // If T is not a dependent type or a variably-modified type, there |
| 1633 | // is nothing to do. |
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1634 | if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType()) |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1635 | return T; |
| 1636 | |
| Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 1637 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 1638 | return Instantiator.TransformType(T); |
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1639 | } |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1640 | |
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1641 | static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) { |
| Nico Weber | c097337 | 2016-02-01 22:31:51 +0000 | [diff] [blame] | 1642 | if (T->getType()->isInstantiationDependentType() || |
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1643 | T->getType()->isVariablyModifiedType()) |
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1644 | return true; |
| 1645 | |
| Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1646 | TypeLoc TL = T->getTypeLoc().IgnoreParens(); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 1647 | if (!TL.getAs<FunctionProtoTypeLoc>()) |
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1648 | return false; |
| 1649 | |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 1650 | FunctionProtoTypeLoc FP = TL.castAs<FunctionProtoTypeLoc>(); |
| Nico Weber | c097337 | 2016-02-01 22:31:51 +0000 | [diff] [blame] | 1651 | for (ParmVarDecl *P : FP.getParams()) { |
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 1652 | // This must be synthesized from a typedef. |
| 1653 | if (!P) continue; |
| 1654 | |
| Nico Weber | c097337 | 2016-02-01 22:31:51 +0000 | [diff] [blame] | 1655 | // If there are any parameters, a new TypeSourceInfo that refers to the |
| 1656 | // instantiated parameters must be built. |
| 1657 | return true; |
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | return false; |
| 1661 | } |
| 1662 | |
| 1663 | /// A form of SubstType intended specifically for instantiating the |
| 1664 | /// type of a FunctionDecl. Its purpose is solely to force the |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 1665 | /// instantiation of default-argument expressions and to avoid |
| 1666 | /// instantiating an exception-specification. |
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1667 | TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T, |
| 1668 | const MultiLevelTemplateArgumentList &Args, |
| 1669 | SourceLocation Loc, |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1670 | DeclarationName Entity, |
| 1671 | CXXRecordDecl *ThisContext, |
| Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 1672 | Qualifiers ThisTypeQuals) { |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 1673 | assert(!CodeSynthesisContexts.empty() && |
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1674 | "Cannot perform an instantiation without some context on the " |
| 1675 | "instantiation stack"); |
| Nico Weber | c097337 | 2016-02-01 22:31:51 +0000 | [diff] [blame] | 1676 | |
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1677 | if (!NeedsInstantiationAsFunctionType(T)) |
| 1678 | return T; |
| 1679 | |
| 1680 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 1681 | |
| 1682 | TypeLocBuilder TLB; |
| 1683 | |
| 1684 | TypeLoc TL = T->getTypeLoc(); |
| 1685 | TLB.reserve(TL.getFullDataSize()); |
| 1686 | |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1687 | QualType Result; |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 1688 | |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 1689 | if (FunctionProtoTypeLoc Proto = |
| 1690 | TL.IgnoreParens().getAs<FunctionProtoTypeLoc>()) { |
| 1691 | // Instantiate the type, other than its exception specification. The |
| 1692 | // exception specification is instantiated in InitFunctionInstantiation |
| 1693 | // once we've built the FunctionDecl. |
| 1694 | // FIXME: Set the exception specification to EST_Uninstantiated here, |
| 1695 | // instead of rebuilding the function type again later. |
| 1696 | Result = Instantiator.TransformFunctionProtoType( |
| 1697 | TLB, Proto, ThisContext, ThisTypeQuals, |
| 1698 | [](FunctionProtoType::ExceptionSpecInfo &ESI, |
| 1699 | bool &Changed) { return false; }); |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1700 | } else { |
| 1701 | Result = Instantiator.TransformType(TLB, TL); |
| 1702 | } |
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1703 | if (Result.isNull()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1704 | return nullptr; |
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1705 | |
| 1706 | return TLB.getTypeSourceInfo(Context, Result); |
| 1707 | } |
| 1708 | |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1709 | bool Sema::SubstExceptionSpec(SourceLocation Loc, |
| 1710 | FunctionProtoType::ExceptionSpecInfo &ESI, |
| 1711 | SmallVectorImpl<QualType> &ExceptionStorage, |
| 1712 | const MultiLevelTemplateArgumentList &Args) { |
| 1713 | assert(ESI.Type != EST_Uninstantiated); |
| 1714 | |
| 1715 | bool Changed = false; |
| 1716 | TemplateInstantiator Instantiator(*this, Args, Loc, DeclarationName()); |
| 1717 | return Instantiator.TransformExceptionSpec(Loc, ESI, ExceptionStorage, |
| 1718 | Changed); |
| 1719 | } |
| 1720 | |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 1721 | void Sema::SubstExceptionSpec(FunctionDecl *New, const FunctionProtoType *Proto, |
| 1722 | const MultiLevelTemplateArgumentList &Args) { |
| 1723 | FunctionProtoType::ExceptionSpecInfo ESI = |
| 1724 | Proto->getExtProtoInfo().ExceptionSpec; |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 1725 | |
| 1726 | SmallVector<QualType, 4> ExceptionStorage; |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1727 | if (SubstExceptionSpec(New->getTypeSourceInfo()->getTypeLoc().getEndLoc(), |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1728 | ESI, ExceptionStorage, Args)) |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 1729 | // On error, recover by dropping the exception specification. |
| 1730 | ESI.Type = EST_None; |
| 1731 | |
| 1732 | UpdateExceptionSpec(New, ESI); |
| 1733 | } |
| 1734 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1735 | ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm, |
| Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 1736 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1737 | int indexAdjustment, |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1738 | Optional<unsigned> NumExpansions, |
| Douglas Gregor | 0dd22bc | 2012-01-25 16:15:54 +0000 | [diff] [blame] | 1739 | bool ExpectParameterPack) { |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1740 | TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1741 | TypeSourceInfo *NewDI = nullptr; |
| 1742 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1743 | TypeLoc OldTL = OldDI->getTypeLoc(); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 1744 | if (PackExpansionTypeLoc ExpansionTL = OldTL.getAs<PackExpansionTypeLoc>()) { |
| 1745 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1746 | // We have a function parameter pack. Substitute into the pattern of the |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1747 | // expansion. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1748 | NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs, |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1749 | OldParm->getLocation(), OldParm->getDeclName()); |
| 1750 | if (!NewDI) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1751 | return nullptr; |
| 1752 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1753 | if (NewDI->getType()->containsUnexpandedParameterPack()) { |
| 1754 | // We still have unexpanded parameter packs, which means that |
| 1755 | // our function parameter is still a function parameter pack. |
| 1756 | // Therefore, make its type a pack expansion type. |
| Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 1757 | NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(), |
| Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 1758 | NumExpansions); |
| Douglas Gregor | 0dd22bc | 2012-01-25 16:15:54 +0000 | [diff] [blame] | 1759 | } else if (ExpectParameterPack) { |
| 1760 | // We expected to get a parameter pack but didn't (because the type |
| 1761 | // itself is not a pack expansion type), so complain. This can occur when |
| 1762 | // the substitution goes through an alias template that "loses" the |
| 1763 | // pack expansion. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1764 | Diag(OldParm->getLocation(), |
| Douglas Gregor | 0dd22bc | 2012-01-25 16:15:54 +0000 | [diff] [blame] | 1765 | diag::err_function_parameter_pack_without_parameter_packs) |
| 1766 | << NewDI->getType(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1767 | return nullptr; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1768 | } |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1769 | } else { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1770 | NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(), |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1771 | OldParm->getDeclName()); |
| 1772 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1773 | |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1774 | if (!NewDI) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1775 | return nullptr; |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1776 | |
| 1777 | if (NewDI->getType()->isVoidType()) { |
| 1778 | Diag(OldParm->getLocation(), diag::err_param_with_void_type); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1779 | return nullptr; |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1783 | OldParm->getInnerLocStart(), |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1784 | OldParm->getLocation(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1785 | OldParm->getIdentifier(), |
| 1786 | NewDI->getType(), NewDI, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1787 | OldParm->getStorageClass()); |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1788 | if (!NewParm) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1789 | return nullptr; |
| 1790 | |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1791 | // Mark the (new) default argument as uninstantiated (if any). |
| 1792 | if (OldParm->hasUninstantiatedDefaultArg()) { |
| 1793 | Expr *Arg = OldParm->getUninstantiatedDefaultArg(); |
| 1794 | NewParm->setUninstantiatedDefaultArg(Arg); |
| Douglas Gregor | 758cb67 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 1795 | } else if (OldParm->hasUnparsedDefaultArg()) { |
| 1796 | NewParm->setUnparsedDefaultArg(); |
| 1797 | UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm); |
| Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 1798 | } else if (Expr *Arg = OldParm->getDefaultArg()) { |
| 1799 | FunctionDecl *OwningFunc = cast<FunctionDecl>(OldParm->getDeclContext()); |
| Serge Pavlov | 73c6a24 | 2015-08-23 10:22:28 +0000 | [diff] [blame] | 1800 | if (OwningFunc->isLexicallyWithinFunctionOrMethod()) { |
| 1801 | // Instantiate default arguments for methods of local classes (DR1484) |
| 1802 | // and non-defining declarations. |
| 1803 | Sema::ContextRAII SavedContext(*this, OwningFunc); |
| Akira Hatanaka | d644e02 | 2016-12-16 03:19:41 +0000 | [diff] [blame] | 1804 | LocalInstantiationScope Local(*this, true); |
| Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 1805 | ExprResult NewArg = SubstExpr(Arg, TemplateArgs); |
| John McCall | dc40b61 | 2015-12-11 01:56:36 +0000 | [diff] [blame] | 1806 | if (NewArg.isUsable()) { |
| 1807 | // It would be nice if we still had this. |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1808 | SourceLocation EqualLoc = NewArg.get()->getBeginLoc(); |
| John McCall | dc40b61 | 2015-12-11 01:56:36 +0000 | [diff] [blame] | 1809 | SetParamDefaultArgument(NewParm, NewArg.get(), EqualLoc); |
| 1810 | } |
| Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 1811 | } else { |
| 1812 | // FIXME: if we non-lazily instantiated non-dependent default args for |
| 1813 | // non-dependent parameter types we could remove a bunch of duplicate |
| 1814 | // conversion warnings for such arguments. |
| 1815 | NewParm->setUninstantiatedDefaultArg(Arg); |
| 1816 | } |
| 1817 | } |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1818 | |
| 1819 | NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1820 | |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1821 | if (OldParm->isParameterPack() && !NewParm->isParameterPack()) { |
| Richard Smith | 928be49 | 2012-01-25 02:14:59 +0000 | [diff] [blame] | 1822 | // Add the new parameter to the instantiated parameter pack. |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1823 | CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm); |
| 1824 | } else { |
| 1825 | // Introduce an Old -> New mapping |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1826 | CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm); |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1827 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1828 | |
| Argyrios Kyrtzidis | 3816ed4 | 2010-07-19 10:14:41 +0000 | [diff] [blame] | 1829 | // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext |
| 1830 | // can be anything, is this right ? |
| Fariborz Jahanian | 714447b | 2010-07-13 21:05:02 +0000 | [diff] [blame] | 1831 | NewParm->setDeclContext(CurContext); |
| John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1832 | |
| 1833 | NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(), |
| 1834 | OldParm->getFunctionScopeIndex() + indexAdjustment); |
| Jordan Rose | a0a86be | 2013-03-08 22:25:36 +0000 | [diff] [blame] | 1835 | |
| 1836 | InstantiateAttrs(TemplateArgs, OldParm, NewParm); |
| 1837 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1838 | return NewParm; |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1841 | /// Substitute the given template arguments into the given set of |
| Douglas Gregor | dd47216 | 2011-01-07 00:20:55 +0000 | [diff] [blame] | 1842 | /// parameters, producing the set of parameter types that would be generated |
| 1843 | /// from such a substitution. |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 1844 | bool Sema::SubstParmTypes( |
| 1845 | SourceLocation Loc, ArrayRef<ParmVarDecl *> Params, |
| 1846 | const FunctionProtoType::ExtParameterInfo *ExtParamInfos, |
| 1847 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 1848 | SmallVectorImpl<QualType> &ParamTypes, |
| 1849 | SmallVectorImpl<ParmVarDecl *> *OutParams, |
| 1850 | ExtParameterInfoBuilder &ParamInfos) { |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 1851 | assert(!CodeSynthesisContexts.empty() && |
| Douglas Gregor | dd47216 | 2011-01-07 00:20:55 +0000 | [diff] [blame] | 1852 | "Cannot perform an instantiation without some context on the " |
| 1853 | "instantiation stack"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1854 | |
| 1855 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| Douglas Gregor | dd47216 | 2011-01-07 00:20:55 +0000 | [diff] [blame] | 1856 | DeclarationName()); |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 1857 | return Instantiator.TransformFunctionTypeParams( |
| 1858 | Loc, Params, nullptr, ExtParamInfos, ParamTypes, OutParams, ParamInfos); |
| Douglas Gregor | dd47216 | 2011-01-07 00:20:55 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1861 | /// Perform substitution on the base class specifiers of the |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1862 | /// given class template specialization. |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1863 | /// |
| 1864 | /// Produces a diagnostic and returns true on error, returns false and |
| 1865 | /// attaches the instantiated base classes to the class template |
| 1866 | /// specialization if successful. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | bool |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1868 | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 1869 | CXXRecordDecl *Pattern, |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1870 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1871 | bool Invalid = false; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1872 | SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
| Richard Trieu | b584133 | 2015-04-15 01:21:42 +0000 | [diff] [blame] | 1873 | for (const auto &Base : Pattern->bases()) { |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1874 | if (!Base.getType()->isDependentType()) { |
| 1875 | if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl()) { |
| Matt Beaumont-Gay | 76d0b46 | 2013-06-21 18:58:32 +0000 | [diff] [blame] | 1876 | if (RD->isInvalidDecl()) |
| 1877 | Instantiation->setInvalidDecl(); |
| 1878 | } |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1879 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(Base)); |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1880 | continue; |
| 1881 | } |
| 1882 | |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1883 | SourceLocation EllipsisLoc; |
| Douglas Gregor | c52264e | 2011-03-02 02:04:06 +0000 | [diff] [blame] | 1884 | TypeSourceInfo *BaseTypeLoc; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1885 | if (Base.isPackExpansion()) { |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1886 | // This is a pack expansion. See whether we should expand it now, or |
| 1887 | // wait until later. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1888 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1889 | collectUnexpandedParameterPacks(Base.getTypeSourceInfo()->getTypeLoc(), |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1890 | Unexpanded); |
| 1891 | bool ShouldExpand = false; |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 1892 | bool RetainExpansion = false; |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1893 | Optional<unsigned> NumExpansions; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1894 | if (CheckParameterPacksForExpansion(Base.getEllipsisLoc(), |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1895 | Base.getSourceRange(), |
| David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 1896 | Unexpanded, |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1897 | TemplateArgs, ShouldExpand, |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 1898 | RetainExpansion, |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1899 | NumExpansions)) { |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1900 | Invalid = true; |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1901 | continue; |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1902 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1903 | |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1904 | // If we should expand this pack expansion now, do so. |
| 1905 | if (ShouldExpand) { |
| Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 1906 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1907 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1908 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1909 | TypeSourceInfo *BaseTypeLoc = SubstType(Base.getTypeSourceInfo(), |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1910 | TemplateArgs, |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1911 | Base.getSourceRange().getBegin(), |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1912 | DeclarationName()); |
| 1913 | if (!BaseTypeLoc) { |
| 1914 | Invalid = true; |
| 1915 | continue; |
| 1916 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1917 | |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1918 | if (CXXBaseSpecifier *InstantiatedBase |
| 1919 | = CheckBaseSpecifier(Instantiation, |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1920 | Base.getSourceRange(), |
| 1921 | Base.isVirtual(), |
| 1922 | Base.getAccessSpecifierAsWritten(), |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1923 | BaseTypeLoc, |
| 1924 | SourceLocation())) |
| 1925 | InstantiatedBases.push_back(InstantiatedBase); |
| 1926 | else |
| 1927 | Invalid = true; |
| 1928 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1929 | |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1930 | continue; |
| 1931 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1932 | |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1933 | // The resulting base specifier will (still) be a pack expansion. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1934 | EllipsisLoc = Base.getEllipsisLoc(); |
| Douglas Gregor | c52264e | 2011-03-02 02:04:06 +0000 | [diff] [blame] | 1935 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1936 | BaseTypeLoc = SubstType(Base.getTypeSourceInfo(), |
| Douglas Gregor | c52264e | 2011-03-02 02:04:06 +0000 | [diff] [blame] | 1937 | TemplateArgs, |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1938 | Base.getSourceRange().getBegin(), |
| Douglas Gregor | c52264e | 2011-03-02 02:04:06 +0000 | [diff] [blame] | 1939 | DeclarationName()); |
| 1940 | } else { |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1941 | BaseTypeLoc = SubstType(Base.getTypeSourceInfo(), |
| Douglas Gregor | c52264e | 2011-03-02 02:04:06 +0000 | [diff] [blame] | 1942 | TemplateArgs, |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1943 | Base.getSourceRange().getBegin(), |
| Douglas Gregor | c52264e | 2011-03-02 02:04:06 +0000 | [diff] [blame] | 1944 | DeclarationName()); |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1945 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1946 | |
| Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1947 | if (!BaseTypeLoc) { |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1948 | Invalid = true; |
| 1949 | continue; |
| 1950 | } |
| 1951 | |
| 1952 | if (CXXBaseSpecifier *InstantiatedBase |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1953 | = CheckBaseSpecifier(Instantiation, |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1954 | Base.getSourceRange(), |
| 1955 | Base.isVirtual(), |
| 1956 | Base.getAccessSpecifierAsWritten(), |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1957 | BaseTypeLoc, |
| 1958 | EllipsisLoc)) |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1959 | InstantiatedBases.push_back(InstantiatedBase); |
| 1960 | else |
| 1961 | Invalid = true; |
| 1962 | } |
| 1963 | |
| Craig Topper | aa700cb | 2015-12-27 21:55:19 +0000 | [diff] [blame] | 1964 | if (!Invalid && AttachBaseSpecifiers(Instantiation, InstantiatedBases)) |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1965 | Invalid = true; |
| 1966 | |
| 1967 | return Invalid; |
| 1968 | } |
| 1969 | |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1970 | // Defined via #include from SemaTemplateInstantiateDecl.cpp |
| Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 1971 | namespace clang { |
| 1972 | namespace sema { |
| 1973 | Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, Sema &S, |
| 1974 | const MultiLevelTemplateArgumentList &TemplateArgs); |
| Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 1975 | Attr *instantiateTemplateAttributeForDecl( |
| 1976 | const Attr *At, ASTContext &C, Sema &S, |
| 1977 | const MultiLevelTemplateArgumentList &TemplateArgs); |
| Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 1978 | } |
| 1979 | } |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1980 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1981 | /// Instantiate the definition of a class from a given pattern. |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1982 | /// |
| 1983 | /// \param PointOfInstantiation The point of instantiation within the |
| 1984 | /// source code. |
| 1985 | /// |
| 1986 | /// \param Instantiation is the declaration whose definition is being |
| 1987 | /// instantiated. This will be either a class template specialization |
| 1988 | /// or a member class of a class template specialization. |
| 1989 | /// |
| 1990 | /// \param Pattern is the pattern from which the instantiation |
| 1991 | /// occurs. This will be either the declaration of a class template or |
| 1992 | /// the declaration of a member class of a class template. |
| 1993 | /// |
| 1994 | /// \param TemplateArgs The template arguments to be substituted into |
| 1995 | /// the pattern. |
| 1996 | /// |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1997 | /// \param TSK the kind of implicit or explicit instantiation to perform. |
| Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1998 | /// |
| 1999 | /// \param Complain whether to complain if the class cannot be instantiated due |
| 2000 | /// to the lack of a definition. |
| 2001 | /// |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2002 | /// \returns true if an error occurred, false otherwise. |
| 2003 | bool |
| 2004 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 2005 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2006 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2007 | TemplateSpecializationKind TSK, |
| Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2008 | bool Complain) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | CXXRecordDecl *PatternDef |
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2010 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
| Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 2011 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation, |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2012 | Instantiation->getInstantiatedFromMemberClass(), |
| 2013 | Pattern, PatternDef, TSK, Complain)) |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2014 | return true; |
| Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 2015 | |
| 2016 | llvm::TimeTraceScope TimeScope("InstantiateClass", [&]() { |
| Lubos Lunak | ab8cde4 | 2019-05-12 10:39:21 +0000 | [diff] [blame] | 2017 | std::string Name; |
| 2018 | llvm::raw_string_ostream OS(Name); |
| 2019 | Instantiation->getNameForDiagnostic(OS, getPrintingPolicy(), |
| 2020 | /*Qualified=*/true); |
| 2021 | return Name; |
| Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 2022 | }); |
| 2023 | |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2024 | Pattern = PatternDef; |
| 2025 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2026 | // Record the point of instantiation. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2027 | if (MemberSpecializationInfo *MSInfo |
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 2028 | = Instantiation->getMemberSpecializationInfo()) { |
| 2029 | MSInfo->setTemplateSpecializationKind(TSK); |
| 2030 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2031 | } else if (ClassTemplateSpecializationDecl *Spec |
| Nico Weber | 3ffc4c9 | 2011-12-20 20:32:49 +0000 | [diff] [blame] | 2032 | = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { |
| Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 2033 | Spec->setTemplateSpecializationKind(TSK); |
| 2034 | Spec->setPointOfInstantiation(PointOfInstantiation); |
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 2035 | } |
| Richard Smith | a108760 | 2014-03-10 00:04:29 +0000 | [diff] [blame] | 2036 | |
| Douglas Gregor | f3430ae | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 2037 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2038 | if (Inst.isInvalid()) |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2039 | return true; |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 2040 | assert(!Inst.isAlreadyInstantiating() && "should have been caught by caller"); |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 2041 | PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
| Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 2042 | "instantiating class definition"); |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2043 | |
| 2044 | // Enter the scope of this instantiation. We don't use |
| 2045 | // PushDeclContext because we don't have a scope. |
| John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2046 | ContextRAII SavedContext(*this, Instantiation); |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2047 | EnterExpressionEvaluationContext EvalContext( |
| 2048 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2049 | |
| Douglas Gregor | 5112157 | 2010-03-24 01:33:17 +0000 | [diff] [blame] | 2050 | // If this is an instantiation of a local class, merge this local |
| 2051 | // instantiation scope with the enclosing scope. Otherwise, every |
| 2052 | // instantiation of a class has its own local instantiation scope. |
| 2053 | bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod(); |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2054 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
| Douglas Gregor | 5112157 | 2010-03-24 01:33:17 +0000 | [diff] [blame] | 2055 | |
| Volodymyr Sapsai | 4fbaa62 | 2017-09-21 19:54:12 +0000 | [diff] [blame] | 2056 | // Some class state isn't processed immediately but delayed till class |
| 2057 | // instantiation completes. We may not be ready to handle any delayed state |
| 2058 | // already on the stack as it might correspond to a different class, so save |
| 2059 | // it now and put it back later. |
| 2060 | SavePendingParsedClassStateRAII SavedPendingParsedClassState(*this); |
| Reid Kleckner | 5b64034 | 2016-02-26 19:51:02 +0000 | [diff] [blame] | 2061 | |
| John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 2062 | // Pull attributes from the pattern onto the instantiation. |
| 2063 | InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
| 2064 | |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2065 | // Start the definition of this instantiation. |
| 2066 | Instantiation->startDefinition(); |
| Richard Smith | a108760 | 2014-03-10 00:04:29 +0000 | [diff] [blame] | 2067 | |
| 2068 | // The instantiation is visible here, even if it was first declared in an |
| 2069 | // unimported module. |
| Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 2070 | Instantiation->setVisibleDespiteOwningModule(); |
| Richard Smith | a108760 | 2014-03-10 00:04:29 +0000 | [diff] [blame] | 2071 | |
| 2072 | // FIXME: This loses the as-written tag kind for an explicit instantiation. |
| Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2073 | Instantiation->setTagKind(Pattern->getTagKind()); |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2074 | |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2075 | // Do substitution on the base class specifiers. |
| 2076 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
| Douglas Gregor | b9b8b81 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 2077 | Instantiation->setInvalidDecl(); |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2078 | |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2079 | TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2080 | SmallVector<Decl*, 4> Fields; |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 2081 | // Delay instantiation of late parsed attributes. |
| 2082 | LateInstantiatedAttrVec LateAttrs; |
| 2083 | Instantiator.enableLateAttributeInstantiation(&LateAttrs); |
| 2084 | |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 2085 | bool MightHaveConstexprVirtualFunctions = false; |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2086 | for (auto *Member : Pattern->decls()) { |
| Argyrios Kyrtzidis | 9a94d9b | 2010-11-04 03:18:57 +0000 | [diff] [blame] | 2087 | // Don't instantiate members not belonging in this semantic context. |
| 2088 | // e.g. for: |
| 2089 | // @code |
| 2090 | // template <int i> class A { |
| 2091 | // class B *g; |
| 2092 | // }; |
| 2093 | // @endcode |
| 2094 | // 'class B' has the template as lexical context but semantically it is |
| 2095 | // introduced in namespace scope. |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2096 | if (Member->getDeclContext() != Pattern) |
| Argyrios Kyrtzidis | 9a94d9b | 2010-11-04 03:18:57 +0000 | [diff] [blame] | 2097 | continue; |
| 2098 | |
| Erik Pilkington | a3a462e | 2018-07-23 22:47:37 +0000 | [diff] [blame] | 2099 | // BlockDecls can appear in a default-member-initializer. They must be the |
| 2100 | // child of a BlockExpr, so we only know how to instantiate them from there. |
| 2101 | if (isa<BlockDecl>(Member)) |
| 2102 | continue; |
| 2103 | |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2104 | if (Member->isInvalidDecl()) { |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 2105 | Instantiation->setInvalidDecl(); |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2106 | continue; |
| 2107 | } |
| 2108 | |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2109 | Decl *NewMember = Instantiator.Visit(Member); |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2110 | if (NewMember) { |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2111 | if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2112 | Fields.push_back(Field); |
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2113 | } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) { |
| 2114 | // C++11 [temp.inst]p1: The implicit instantiation of a class template |
| 2115 | // specialization causes the implicit instantiation of the definitions |
| 2116 | // of unscoped member enumerations. |
| 2117 | // Record a point of instantiation for this implicit instantiation. |
| Richard Smith | b66d777 | 2012-03-23 23:09:08 +0000 | [diff] [blame] | 2118 | if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() && |
| 2119 | Enum->isCompleteDefinition()) { |
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2120 | MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo(); |
| 2121 | assert(MSInfo && "no spec info for member enum specialization"); |
| 2122 | MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation); |
| 2123 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2124 | } |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 2125 | } else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(NewMember)) { |
| 2126 | if (SA->isFailed()) { |
| 2127 | // A static_assert failed. Bail out; instantiating this |
| 2128 | // class is probably not meaningful. |
| 2129 | Instantiation->setInvalidDecl(); |
| 2130 | break; |
| 2131 | } |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 2132 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewMember)) { |
| 2133 | if (MD->isConstexpr() && !MD->getFriendObjectKind() && |
| 2134 | (MD->isVirtualAsWritten() || Instantiation->getNumBases())) |
| 2135 | MightHaveConstexprVirtualFunctions = true; |
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | if (NewMember->isInvalidDecl()) |
| Douglas Gregor | b9b8b81 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 2139 | Instantiation->setInvalidDecl(); |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2140 | } else { |
| 2141 | // FIXME: Eventually, a NULL return will mean that one of the |
| Douglas Gregor | b9b8b81 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 2142 | // instantiations was a semantic disaster, and we'll want to mark the |
| 2143 | // declaration invalid. |
| 2144 | // For now, we expect to skip some members that we can't yet handle. |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | // Finish checking fields. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2149 | ActOnFields(nullptr, Instantiation->getLocation(), Instantiation, Fields, |
| Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2150 | SourceLocation(), SourceLocation(), ParsedAttributesView()); |
| Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2151 | CheckCompletedCXXClass(Instantiation); |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2152 | |
| Reid Kleckner | 93f661a | 2015-03-17 21:51:43 +0000 | [diff] [blame] | 2153 | // Default arguments are parsed, if not instantiated. We can go instantiate |
| 2154 | // default arg exprs for default constructors if necessary now. |
| Hans Wennborg | 99000c2 | 2015-08-15 01:18:16 +0000 | [diff] [blame] | 2155 | ActOnFinishCXXNonNestedClass(Instantiation); |
| Reid Kleckner | 93f661a | 2015-03-17 21:51:43 +0000 | [diff] [blame] | 2156 | |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 2157 | // Instantiate late parsed attributes, and attach them to their decls. |
| 2158 | // See Sema::InstantiateAttrs |
| 2159 | for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(), |
| 2160 | E = LateAttrs.end(); I != E; ++I) { |
| 2161 | assert(CurrentInstantiationScope == Instantiator.getStartingScope()); |
| 2162 | CurrentInstantiationScope = I->Scope; |
| Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 2163 | |
| 2164 | // Allow 'this' within late-parsed attributes. |
| 2165 | NamedDecl *ND = dyn_cast<NamedDecl>(I->NewDecl); |
| 2166 | CXXRecordDecl *ThisContext = |
| 2167 | dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
| Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 2168 | CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(), |
| Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 2169 | ND && ND->isCXXInstanceMember()); |
| 2170 | |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 2171 | Attr *NewAttr = |
| 2172 | instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs); |
| 2173 | I->NewDecl->addAttr(NewAttr); |
| 2174 | LocalInstantiationScope::deleteScopes(I->Scope, |
| 2175 | Instantiator.getStartingScope()); |
| 2176 | } |
| 2177 | Instantiator.disableLateAttributeInstantiation(); |
| 2178 | LateAttrs.clear(); |
| 2179 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2180 | ActOnFinishDelayedMemberInitializers(Instantiation); |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2181 | |
| Richard Smith | a108760 | 2014-03-10 00:04:29 +0000 | [diff] [blame] | 2182 | // FIXME: We should do something similar for explicit instantiations so they |
| 2183 | // end up in the right module. |
| Abramo Bagnara | 12dcbf3 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 2184 | if (TSK == TSK_ImplicitInstantiation) { |
| Argyrios Kyrtzidis | e378948 | 2012-02-11 01:59:57 +0000 | [diff] [blame] | 2185 | Instantiation->setLocation(Pattern->getLocation()); |
| Abramo Bagnara | 12dcbf3 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 2186 | Instantiation->setLocStart(Pattern->getInnerLocStart()); |
| Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 2187 | Instantiation->setBraceRange(Pattern->getBraceRange()); |
| Abramo Bagnara | 12dcbf3 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 2188 | } |
| Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2189 | |
| Douglas Gregor | b9b8b81 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 2190 | if (!Instantiation->isInvalidDecl()) { |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2191 | // Perform any dependent diagnostics from the pattern. |
| 2192 | PerformDependentDiagnostics(Pattern, TemplateArgs); |
| 2193 | |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2194 | // Instantiate any out-of-line class template partial |
| 2195 | // specializations now. |
| Richard Smith | 10b55fc | 2013-09-26 03:49:48 +0000 | [diff] [blame] | 2196 | for (TemplateDeclInstantiator::delayed_partial_spec_iterator |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2197 | P = Instantiator.delayed_partial_spec_begin(), |
| 2198 | PEnd = Instantiator.delayed_partial_spec_end(); |
| 2199 | P != PEnd; ++P) { |
| 2200 | if (!Instantiator.InstantiateClassTemplatePartialSpecialization( |
| Richard Smith | 10b55fc | 2013-09-26 03:49:48 +0000 | [diff] [blame] | 2201 | P->first, P->second)) { |
| 2202 | Instantiation->setInvalidDecl(); |
| 2203 | break; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | // Instantiate any out-of-line variable template partial |
| 2208 | // specializations now. |
| 2209 | for (TemplateDeclInstantiator::delayed_var_partial_spec_iterator |
| 2210 | P = Instantiator.delayed_var_partial_spec_begin(), |
| 2211 | PEnd = Instantiator.delayed_var_partial_spec_end(); |
| 2212 | P != PEnd; ++P) { |
| 2213 | if (!Instantiator.InstantiateVarTemplatePartialSpecialization( |
| 2214 | P->first, P->second)) { |
| Douglas Gregor | b9b8b81 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 2215 | Instantiation->setInvalidDecl(); |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2216 | break; |
| 2217 | } |
| 2218 | } |
| 2219 | } |
| 2220 | |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2221 | // Exit the scope of this instantiation. |
| John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2222 | SavedContext.pop(); |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2223 | |
| Douglas Gregor | b9b8b81 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 2224 | if (!Instantiation->isInvalidDecl()) { |
| Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 2225 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 2226 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2227 | // Always emit the vtable for an explicit instantiation definition |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 2228 | // of a polymorphic class template specialization. Otherwise, eagerly |
| 2229 | // instantiate only constexpr virtual functions in preparation for their use |
| 2230 | // in constant evaluation. |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2231 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 2232 | MarkVTableUsed(PointOfInstantiation, Instantiation, true); |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 2233 | else if (MightHaveConstexprVirtualFunctions) |
| 2234 | MarkVirtualMembersReferenced(PointOfInstantiation, Instantiation, |
| 2235 | /*ConstexprOnly*/ true); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
| Douglas Gregor | b9b8b81 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 2238 | return Instantiation->isInvalidDecl(); |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2241 | /// Instantiate the definition of an enum from a given pattern. |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2242 | /// |
| 2243 | /// \param PointOfInstantiation The point of instantiation within the |
| 2244 | /// source code. |
| 2245 | /// \param Instantiation is the declaration whose definition is being |
| 2246 | /// instantiated. This will be a member enumeration of a class |
| 2247 | /// temploid specialization, or a local enumeration within a |
| 2248 | /// function temploid specialization. |
| 2249 | /// \param Pattern The templated declaration from which the instantiation |
| 2250 | /// occurs. |
| 2251 | /// \param TemplateArgs The template arguments to be substituted into |
| 2252 | /// the pattern. |
| 2253 | /// \param TSK The kind of implicit or explicit instantiation to perform. |
| 2254 | /// |
| 2255 | /// \return \c true if an error occurred, \c false otherwise. |
| 2256 | bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation, |
| 2257 | EnumDecl *Instantiation, EnumDecl *Pattern, |
| 2258 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 2259 | TemplateSpecializationKind TSK) { |
| 2260 | EnumDecl *PatternDef = Pattern->getDefinition(); |
| Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 2261 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation, |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2262 | Instantiation->getInstantiatedFromMemberEnum(), |
| 2263 | Pattern, PatternDef, TSK,/*Complain*/true)) |
| 2264 | return true; |
| 2265 | Pattern = PatternDef; |
| 2266 | |
| 2267 | // Record the point of instantiation. |
| 2268 | if (MemberSpecializationInfo *MSInfo |
| 2269 | = Instantiation->getMemberSpecializationInfo()) { |
| 2270 | MSInfo->setTemplateSpecializationKind(TSK); |
| 2271 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2272 | } |
| 2273 | |
| 2274 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2275 | if (Inst.isInvalid()) |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2276 | return true; |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 2277 | if (Inst.isAlreadyInstantiating()) |
| 2278 | return false; |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 2279 | PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
| Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 2280 | "instantiating enum definition"); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2281 | |
| Richard Smith | a108760 | 2014-03-10 00:04:29 +0000 | [diff] [blame] | 2282 | // The instantiation is visible here, even if it was first declared in an |
| 2283 | // unimported module. |
| Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 2284 | Instantiation->setVisibleDespiteOwningModule(); |
| Richard Smith | a108760 | 2014-03-10 00:04:29 +0000 | [diff] [blame] | 2285 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2286 | // Enter the scope of this instantiation. We don't use |
| 2287 | // PushDeclContext because we don't have a scope. |
| 2288 | ContextRAII SavedContext(*this, Instantiation); |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2289 | EnterExpressionEvaluationContext EvalContext( |
| 2290 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2291 | |
| 2292 | LocalInstantiationScope Scope(*this, /*MergeWithParentScope*/true); |
| 2293 | |
| 2294 | // Pull attributes from the pattern onto the instantiation. |
| 2295 | InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
| 2296 | |
| 2297 | TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); |
| 2298 | Instantiator.InstantiateEnumDefinition(Instantiation, Pattern); |
| 2299 | |
| 2300 | // Exit the scope of this instantiation. |
| 2301 | SavedContext.pop(); |
| 2302 | |
| 2303 | return Instantiation->isInvalidDecl(); |
| 2304 | } |
| 2305 | |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2306 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2307 | /// Instantiate the definition of a field from the given pattern. |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2308 | /// |
| 2309 | /// \param PointOfInstantiation The point of instantiation within the |
| 2310 | /// source code. |
| 2311 | /// \param Instantiation is the declaration whose definition is being |
| 2312 | /// instantiated. This will be a class of a class temploid |
| 2313 | /// specialization, or a local enumeration within a function temploid |
| 2314 | /// specialization. |
| 2315 | /// \param Pattern The templated declaration from which the instantiation |
| 2316 | /// occurs. |
| 2317 | /// \param TemplateArgs The template arguments to be substituted into |
| 2318 | /// the pattern. |
| 2319 | /// |
| 2320 | /// \return \c true if an error occurred, \c false otherwise. |
| 2321 | bool Sema::InstantiateInClassInitializer( |
| 2322 | SourceLocation PointOfInstantiation, FieldDecl *Instantiation, |
| 2323 | FieldDecl *Pattern, const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 2324 | // If there is no initializer, we don't need to do anything. |
| 2325 | if (!Pattern->hasInClassInitializer()) |
| 2326 | return false; |
| 2327 | |
| 2328 | assert(Instantiation->getInClassInitStyle() == |
| 2329 | Pattern->getInClassInitStyle() && |
| 2330 | "pattern and instantiation disagree about init style"); |
| 2331 | |
| 2332 | // Error out if we haven't parsed the initializer of the pattern yet because |
| 2333 | // we are waiting for the closing brace of the outer class. |
| 2334 | Expr *OldInit = Pattern->getInClassInitializer(); |
| 2335 | if (!OldInit) { |
| 2336 | RecordDecl *PatternRD = Pattern->getParent(); |
| 2337 | RecordDecl *OutermostClass = PatternRD->getOuterLexicalRecordContext(); |
| Richard Smith | 8dbc6b2 | 2016-11-22 22:55:12 +0000 | [diff] [blame] | 2338 | Diag(PointOfInstantiation, |
| 2339 | diag::err_in_class_initializer_not_yet_parsed) |
| 2340 | << OutermostClass << Pattern; |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2341 | Diag(Pattern->getEndLoc(), diag::note_in_class_initializer_not_yet_parsed); |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2342 | Instantiation->setInvalidDecl(); |
| 2343 | return true; |
| 2344 | } |
| 2345 | |
| 2346 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
| 2347 | if (Inst.isInvalid()) |
| 2348 | return true; |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 2349 | if (Inst.isAlreadyInstantiating()) { |
| 2350 | // Error out if we hit an instantiation cycle for this initializer. |
| 2351 | Diag(PointOfInstantiation, diag::err_in_class_initializer_cycle) |
| 2352 | << Instantiation; |
| 2353 | return true; |
| 2354 | } |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 2355 | PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
| Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 2356 | "instantiating default member init"); |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2357 | |
| 2358 | // Enter the scope of this instantiation. We don't use PushDeclContext because |
| 2359 | // we don't have a scope. |
| 2360 | ContextRAII SavedContext(*this, Instantiation->getParent()); |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2361 | EnterExpressionEvaluationContext EvalContext( |
| 2362 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2363 | |
| Serge Pavlov | 907233f | 2015-04-28 17:58:47 +0000 | [diff] [blame] | 2364 | LocalInstantiationScope Scope(*this, true); |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2365 | |
| 2366 | // Instantiate the initializer. |
| 2367 | ActOnStartCXXInClassMemberInitializer(); |
| Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 2368 | CXXThisScopeRAII ThisScope(*this, Instantiation->getParent(), Qualifiers()); |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2369 | |
| 2370 | ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs, |
| 2371 | /*CXXDirectInit=*/false); |
| 2372 | Expr *Init = NewInit.get(); |
| 2373 | assert((!Init || !isa<ParenListExpr>(Init)) && "call-style init in class"); |
| 2374 | ActOnFinishCXXInClassMemberInitializer( |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2375 | Instantiation, Init ? Init->getBeginLoc() : SourceLocation(), Init); |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2376 | |
| Richard Smith | 4b054b2 | 2016-08-24 21:25:37 +0000 | [diff] [blame] | 2377 | if (auto *L = getASTMutationListener()) |
| 2378 | L->DefaultMemberInitializerInstantiated(Instantiation); |
| 2379 | |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2380 | // Return true if the in-class initializer is still missing. |
| 2381 | return !Instantiation->getInClassInitializer(); |
| 2382 | } |
| 2383 | |
| Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2384 | namespace { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2385 | /// A partial specialization whose template arguments have matched |
| Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2386 | /// a given template-id. |
| 2387 | struct PartialSpecMatchResult { |
| 2388 | ClassTemplatePartialSpecializationDecl *Partial; |
| 2389 | TemplateArgumentList *Args; |
| Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2390 | }; |
| 2391 | } |
| 2392 | |
| Richard Smith | e6d4b77 | 2017-06-07 02:42:27 +0000 | [diff] [blame] | 2393 | bool Sema::usesPartialOrExplicitSpecialization( |
| 2394 | SourceLocation Loc, ClassTemplateSpecializationDecl *ClassTemplateSpec) { |
| 2395 | if (ClassTemplateSpec->getTemplateSpecializationKind() == |
| 2396 | TSK_ExplicitSpecialization) |
| 2397 | return true; |
| 2398 | |
| 2399 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2400 | ClassTemplateSpec->getSpecializedTemplate() |
| 2401 | ->getPartialSpecializations(PartialSpecs); |
| 2402 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2403 | TemplateDeductionInfo Info(Loc); |
| 2404 | if (!DeduceTemplateArguments(PartialSpecs[I], |
| 2405 | ClassTemplateSpec->getTemplateArgs(), Info)) |
| 2406 | return true; |
| 2407 | } |
| 2408 | |
| 2409 | return false; |
| 2410 | } |
| 2411 | |
| Richard Smith | 32b4376 | 2017-01-08 22:45:21 +0000 | [diff] [blame] | 2412 | /// Get the instantiation pattern to use to instantiate the definition of a |
| 2413 | /// given ClassTemplateSpecializationDecl (either the pattern of the primary |
| 2414 | /// template or of a partial specialization). |
| 2415 | static CXXRecordDecl * |
| 2416 | getPatternForClassTemplateSpecialization( |
| 2417 | Sema &S, SourceLocation PointOfInstantiation, |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 2418 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 2419 | TemplateSpecializationKind TSK, bool Complain) { |
| Richard Smith | 32b4376 | 2017-01-08 22:45:21 +0000 | [diff] [blame] | 2420 | Sema::InstantiatingTemplate Inst(S, PointOfInstantiation, ClassTemplateSpec); |
| 2421 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
| 2422 | return nullptr; |
| 2423 | |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2424 | llvm::PointerUnion<ClassTemplateDecl *, |
| 2425 | ClassTemplatePartialSpecializationDecl *> |
| 2426 | Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial(); |
| 2427 | if (!Specialized.is<ClassTemplatePartialSpecializationDecl *>()) { |
| 2428 | // Find best matching specialization. |
| 2429 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2430 | |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2431 | // C++ [temp.class.spec.match]p1: |
| 2432 | // When a class template is used in a context that requires an |
| 2433 | // instantiation of the class, it is necessary to determine |
| 2434 | // whether the instantiation is to be generated using the primary |
| 2435 | // template or one of the partial specializations. This is done by |
| 2436 | // matching the template arguments of the class template |
| 2437 | // specialization with the template argument lists of the partial |
| 2438 | // specializations. |
| 2439 | typedef PartialSpecMatchResult MatchResult; |
| 2440 | SmallVector<MatchResult, 4> Matched; |
| 2441 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2442 | Template->getPartialSpecializations(PartialSpecs); |
| 2443 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation); |
| 2444 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2445 | ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2446 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2447 | if (Sema::TemplateDeductionResult Result = S.DeduceTemplateArguments( |
| 2448 | Partial, ClassTemplateSpec->getTemplateArgs(), Info)) { |
| 2449 | // Store the failed-deduction information for use in diagnostics, later. |
| 2450 | // TODO: Actually use the failed-deduction info? |
| 2451 | FailedCandidates.addCandidate().set( |
| 2452 | DeclAccessPair::make(Template, AS_public), Partial, |
| 2453 | MakeDeductionFailureInfo(S.Context, Result, Info)); |
| 2454 | (void)Result; |
| 2455 | } else { |
| 2456 | Matched.push_back(PartialSpecMatchResult()); |
| 2457 | Matched.back().Partial = Partial; |
| 2458 | Matched.back().Args = Info.take(); |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | // If we're dealing with a member template where the template parameters |
| 2463 | // have been instantiated, this provides the original template parameters |
| 2464 | // from which the member template's parameters were instantiated. |
| 2465 | |
| 2466 | if (Matched.size() >= 1) { |
| 2467 | SmallVectorImpl<MatchResult>::iterator Best = Matched.begin(); |
| 2468 | if (Matched.size() == 1) { |
| 2469 | // -- If exactly one matching specialization is found, the |
| 2470 | // instantiation is generated from that specialization. |
| 2471 | // We don't need to do anything for this. |
| 2472 | } else { |
| 2473 | // -- If more than one matching specialization is found, the |
| 2474 | // partial order rules (14.5.4.2) are used to determine |
| 2475 | // whether one of the specializations is more specialized |
| 2476 | // than the others. If none of the specializations is more |
| 2477 | // specialized than all of the other matching |
| 2478 | // specializations, then the use of the class template is |
| 2479 | // ambiguous and the program is ill-formed. |
| 2480 | for (SmallVectorImpl<MatchResult>::iterator P = Best + 1, |
| 2481 | PEnd = Matched.end(); |
| 2482 | P != PEnd; ++P) { |
| 2483 | if (S.getMoreSpecializedPartialSpecialization( |
| 2484 | P->Partial, Best->Partial, PointOfInstantiation) == |
| 2485 | P->Partial) |
| 2486 | Best = P; |
| 2487 | } |
| 2488 | |
| 2489 | // Determine if the best partial specialization is more specialized than |
| 2490 | // the others. |
| 2491 | bool Ambiguous = false; |
| 2492 | for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(), |
| 2493 | PEnd = Matched.end(); |
| 2494 | P != PEnd; ++P) { |
| 2495 | if (P != Best && S.getMoreSpecializedPartialSpecialization( |
| 2496 | P->Partial, Best->Partial, |
| 2497 | PointOfInstantiation) != Best->Partial) { |
| 2498 | Ambiguous = true; |
| 2499 | break; |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | if (Ambiguous) { |
| 2504 | // Partial ordering did not produce a clear winner. Complain. |
| 2505 | Inst.Clear(); |
| 2506 | ClassTemplateSpec->setInvalidDecl(); |
| 2507 | S.Diag(PointOfInstantiation, |
| 2508 | diag::err_partial_spec_ordering_ambiguous) |
| 2509 | << ClassTemplateSpec; |
| 2510 | |
| 2511 | // Print the matching partial specializations. |
| 2512 | for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(), |
| 2513 | PEnd = Matched.end(); |
| 2514 | P != PEnd; ++P) |
| 2515 | S.Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2516 | << S.getTemplateArgumentBindingsText( |
| 2517 | P->Partial->getTemplateParameters(), *P->Args); |
| 2518 | |
| 2519 | return nullptr; |
| 2520 | } |
| 2521 | } |
| 2522 | |
| 2523 | ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args); |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2524 | } else { |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2525 | // -- If no matches are found, the instantiation is generated |
| 2526 | // from the primary template. |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2527 | } |
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2530 | CXXRecordDecl *Pattern = nullptr; |
| 2531 | Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial(); |
| 2532 | if (auto *PartialSpec = |
| 2533 | Specialized.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 2534 | // Instantiate using the best class template partial specialization. |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2535 | while (PartialSpec->getInstantiatedFromMember()) { |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2536 | // If we've found an explicit specialization of this class template, |
| 2537 | // stop here and use that as the pattern. |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2538 | if (PartialSpec->isMemberSpecialization()) |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2539 | break; |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2540 | |
| 2541 | PartialSpec = PartialSpec->getInstantiatedFromMember(); |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2542 | } |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2543 | Pattern = PartialSpec; |
| Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2544 | } else { |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2545 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
| 2546 | while (Template->getInstantiatedFromMemberTemplate()) { |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 2547 | // If we've found an explicit specialization of this class template, |
| 2548 | // stop here and use that as the pattern. |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2549 | if (Template->isMemberSpecialization()) |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 2550 | break; |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2551 | |
| 2552 | Template = Template->getInstantiatedFromMemberTemplate(); |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 2553 | } |
| Volodymyr Sapsai | 9b97348 | 2018-05-16 18:28:58 +0000 | [diff] [blame] | 2554 | Pattern = Template->getTemplatedDecl(); |
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2555 | } |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2556 | |
| Richard Smith | 32b4376 | 2017-01-08 22:45:21 +0000 | [diff] [blame] | 2557 | return Pattern; |
| 2558 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | |
| Richard Smith | 32b4376 | 2017-01-08 22:45:21 +0000 | [diff] [blame] | 2560 | bool Sema::InstantiateClassTemplateSpecialization( |
| 2561 | SourceLocation PointOfInstantiation, |
| 2562 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 2563 | TemplateSpecializationKind TSK, bool Complain) { |
| 2564 | // Perform the actual instantiation on the canonical declaration. |
| 2565 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
| 2566 | ClassTemplateSpec->getCanonicalDecl()); |
| 2567 | if (ClassTemplateSpec->isInvalidDecl()) |
| 2568 | return true; |
| 2569 | |
| 2570 | CXXRecordDecl *Pattern = getPatternForClassTemplateSpecialization( |
| 2571 | *this, PointOfInstantiation, ClassTemplateSpec, TSK, Complain); |
| 2572 | if (!Pattern) |
| 2573 | return true; |
| 2574 | |
| 2575 | return InstantiateClass(PointOfInstantiation, ClassTemplateSpec, Pattern, |
| 2576 | getTemplateInstantiationArgs(ClassTemplateSpec), TSK, |
| 2577 | Complain); |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2578 | } |
| Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 2579 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2580 | /// Instantiates the definitions of all of the member |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2581 | /// of the given class, which is an instantiation of a class template |
| 2582 | /// or a member class of a template. |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2583 | void |
| 2584 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2585 | CXXRecordDecl *Instantiation, |
| 2586 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 2587 | TemplateSpecializationKind TSK) { |
| Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 2588 | // FIXME: We need to notify the ASTMutationListener that we did all of these |
| 2589 | // things, in case we have an explicit instantiation definition in a PCM, a |
| 2590 | // module, or preamble, and the declaration is in an imported AST. |
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 2591 | assert( |
| 2592 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 2593 | TSK == TSK_ExplicitInstantiationDeclaration || |
| 2594 | (TSK == TSK_ImplicitInstantiation && Instantiation->isLocalClass())) && |
| 2595 | "Unexpected template specialization kind!"); |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2596 | for (auto *D : Instantiation->decls()) { |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2597 | bool SuppressNew = false; |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2598 | if (auto *Function = dyn_cast<FunctionDecl>(D)) { |
| Louis Dionne | d269579 | 2018-10-04 15:49:42 +0000 | [diff] [blame] | 2599 | if (FunctionDecl *Pattern = |
| 2600 | Function->getInstantiatedFromMemberFunction()) { |
| 2601 | |
| 2602 | if (Function->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
| 2603 | continue; |
| 2604 | |
| 2605 | MemberSpecializationInfo *MSInfo = |
| 2606 | Function->getMemberSpecializationInfo(); |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2607 | assert(MSInfo && "No member specialization information?"); |
| Douglas Gregor | 06aa5041 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 2608 | if (MSInfo->getTemplateSpecializationKind() |
| 2609 | == TSK_ExplicitSpecialization) |
| 2610 | continue; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2611 | |
| 2612 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 2613 | Function, |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2614 | MSInfo->getTemplateSpecializationKind(), |
| Nico Weber | d75488d | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 2615 | MSInfo->getPointOfInstantiation(), |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2616 | SuppressNew) || |
| 2617 | SuppressNew) |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2618 | continue; |
| Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 2619 | |
| 2620 | // C++11 [temp.explicit]p8: |
| 2621 | // An explicit instantiation definition that names a class template |
| 2622 | // specialization explicitly instantiates the class template |
| 2623 | // specialization and is only an explicit instantiation definition |
| 2624 | // of members whose definition is visible at the point of |
| 2625 | // instantiation. |
| 2626 | if (TSK == TSK_ExplicitInstantiationDefinition && !Pattern->isDefined()) |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2627 | continue; |
| 2628 | |
| Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 2629 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 2630 | |
| 2631 | if (Function->isDefined()) { |
| 2632 | // Let the ASTConsumer know that this function has been explicitly |
| 2633 | // instantiated now, and its linkage might have changed. |
| 2634 | Consumer.HandleTopLevelDecl(DeclGroupRef(Function)); |
| 2635 | } else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2636 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
| Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 2637 | } else if (TSK == TSK_ImplicitInstantiation) { |
| 2638 | PendingLocalImplicitInstantiations.push_back( |
| 2639 | std::make_pair(Function, PointOfInstantiation)); |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2640 | } |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2641 | } |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2642 | } else if (auto *Var = dyn_cast<VarDecl>(D)) { |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2643 | if (isa<VarTemplateSpecializationDecl>(Var)) |
| 2644 | continue; |
| 2645 | |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2646 | if (Var->isStaticDataMember()) { |
| Louis Dionne | d269579 | 2018-10-04 15:49:42 +0000 | [diff] [blame] | 2647 | if (Var->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
| 2648 | continue; |
| 2649 | |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2650 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 2651 | assert(MSInfo && "No member specialization information?"); |
| Douglas Gregor | 06aa5041 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 2652 | if (MSInfo->getTemplateSpecializationKind() |
| 2653 | == TSK_ExplicitSpecialization) |
| 2654 | continue; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2655 | |
| 2656 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 2657 | Var, |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2658 | MSInfo->getTemplateSpecializationKind(), |
| Nico Weber | d75488d | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 2659 | MSInfo->getPointOfInstantiation(), |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2660 | SuppressNew) || |
| 2661 | SuppressNew) |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2662 | continue; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2663 | |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2664 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 2665 | // C++0x [temp.explicit]p8: |
| 2666 | // An explicit instantiation definition that names a class template |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2667 | // specialization explicitly instantiates the class template |
| 2668 | // specialization and is only an explicit instantiation definition |
| 2669 | // of members whose definition is visible at the point of |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2670 | // instantiation. |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 2671 | if (!Var->getInstantiatedFromStaticDataMember()->getDefinition()) |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2672 | continue; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2673 | |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2674 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2675 | InstantiateVariableDefinition(PointOfInstantiation, Var); |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2676 | } else { |
| 2677 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 2678 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2679 | } |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2680 | } else if (auto *Record = dyn_cast<CXXRecordDecl>(D)) { |
| Louis Dionne | d269579 | 2018-10-04 15:49:42 +0000 | [diff] [blame] | 2681 | if (Record->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
| 2682 | continue; |
| 2683 | |
| Douglas Gregor | 1da2225 | 2010-04-18 18:11:38 +0000 | [diff] [blame] | 2684 | // Always skip the injected-class-name, along with any |
| 2685 | // redeclarations of nested classes, since both would cause us |
| 2686 | // to try to instantiate the members of a class twice. |
| Richard Smith | 069ecf6 | 2014-11-20 22:56:34 +0000 | [diff] [blame] | 2687 | // Skip closure types; they'll get instantiated when we instantiate |
| 2688 | // the corresponding lambda-expression. |
| 2689 | if (Record->isInjectedClassName() || Record->getPreviousDecl() || |
| 2690 | Record->isLambda()) |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2691 | continue; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2692 | |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2693 | MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); |
| 2694 | assert(MSInfo && "No member specialization information?"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2695 | |
| Douglas Gregor | 06aa5041 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 2696 | if (MSInfo->getTemplateSpecializationKind() |
| 2697 | == TSK_ExplicitSpecialization) |
| 2698 | continue; |
| Nico Weber | d75488d | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 2699 | |
| Reid Kleckner | 5f789ba | 2019-04-29 21:32:05 +0000 | [diff] [blame] | 2700 | if (Context.getTargetInfo().getTriple().isOSWindows() && |
| Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 2701 | TSK == TSK_ExplicitInstantiationDeclaration) { |
| Reid Kleckner | 5f789ba | 2019-04-29 21:32:05 +0000 | [diff] [blame] | 2702 | // On Windows, explicit instantiation decl of the outer class doesn't |
| 2703 | // affect the inner class. Typically extern template declarations are |
| 2704 | // used in combination with dll import/export annotations, but those |
| 2705 | // are not propagated from the outer class templates to inner classes. |
| 2706 | // Therefore, do not instantiate inner classes on this platform, so |
| 2707 | // that users don't end up with undefined symbols during linking. |
| Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 2708 | continue; |
| 2709 | } |
| 2710 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2711 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 2712 | Record, |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2713 | MSInfo->getTemplateSpecializationKind(), |
| Nico Weber | d75488d | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 2714 | MSInfo->getPointOfInstantiation(), |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2715 | SuppressNew) || |
| 2716 | SuppressNew) |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2717 | continue; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2718 | |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2719 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 2720 | assert(Pattern && "Missing instantiated-from-template information"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2721 | |
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2722 | if (!Record->getDefinition()) { |
| 2723 | if (!Pattern->getDefinition()) { |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2724 | // C++0x [temp.explicit]p8: |
| 2725 | // An explicit instantiation definition that names a class template |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2726 | // specialization explicitly instantiates the class template |
| 2727 | // specialization and is only an explicit instantiation definition |
| 2728 | // of members whose definition is visible at the point of |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2729 | // instantiation. |
| 2730 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 2731 | MSInfo->setTemplateSpecializationKind(TSK); |
| 2732 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2733 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2734 | |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2735 | continue; |
| 2736 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2737 | |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2738 | InstantiateClass(PointOfInstantiation, Record, Pattern, |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2739 | TemplateArgs, |
| 2740 | TSK); |
| Nico Weber | d75488d | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 2741 | } else { |
| 2742 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 2743 | Record->getTemplateSpecializationKind() == |
| 2744 | TSK_ExplicitInstantiationDeclaration) { |
| 2745 | Record->setTemplateSpecializationKind(TSK); |
| 2746 | MarkVTableUsed(PointOfInstantiation, Record, true); |
| 2747 | } |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2748 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2749 | |
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2750 | Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2751 | if (Pattern) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2752 | InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, |
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2753 | TSK); |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2754 | } else if (auto *Enum = dyn_cast<EnumDecl>(D)) { |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2755 | MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo(); |
| 2756 | assert(MSInfo && "No member specialization information?"); |
| 2757 | |
| 2758 | if (MSInfo->getTemplateSpecializationKind() |
| 2759 | == TSK_ExplicitSpecialization) |
| 2760 | continue; |
| 2761 | |
| 2762 | if (CheckSpecializationInstantiationRedecl( |
| 2763 | PointOfInstantiation, TSK, Enum, |
| 2764 | MSInfo->getTemplateSpecializationKind(), |
| 2765 | MSInfo->getPointOfInstantiation(), SuppressNew) || |
| 2766 | SuppressNew) |
| 2767 | continue; |
| 2768 | |
| 2769 | if (Enum->getDefinition()) |
| 2770 | continue; |
| 2771 | |
| Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2772 | EnumDecl *Pattern = Enum->getTemplateInstantiationPattern(); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2773 | assert(Pattern && "Missing instantiated-from-template information"); |
| 2774 | |
| 2775 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 2776 | if (!Pattern->getDefinition()) |
| 2777 | continue; |
| 2778 | |
| 2779 | InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK); |
| 2780 | } else { |
| 2781 | MSInfo->setTemplateSpecializationKind(TSK); |
| 2782 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2783 | } |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2784 | } else if (auto *Field = dyn_cast<FieldDecl>(D)) { |
| 2785 | // No need to instantiate in-class initializers during explicit |
| 2786 | // instantiation. |
| 2787 | if (Field->hasInClassInitializer() && TSK == TSK_ImplicitInstantiation) { |
| 2788 | CXXRecordDecl *ClassPattern = |
| 2789 | Instantiation->getTemplateInstantiationPattern(); |
| 2790 | DeclContext::lookup_result Lookup = |
| 2791 | ClassPattern->lookup(Field->getDeclName()); |
| David Majnemer | 76a2562 | 2016-06-09 05:26:56 +0000 | [diff] [blame] | 2792 | FieldDecl *Pattern = cast<FieldDecl>(Lookup.front()); |
| Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2793 | InstantiateInClassInitializer(PointOfInstantiation, Field, Pattern, |
| 2794 | TemplateArgs); |
| 2795 | } |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2796 | } |
| 2797 | } |
| 2798 | } |
| 2799 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2800 | /// Instantiate the definitions of all of the members of the |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2801 | /// given class template specialization, which was named as part of an |
| 2802 | /// explicit instantiation. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2803 | void |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2804 | Sema::InstantiateClassTemplateSpecializationMembers( |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2805 | SourceLocation PointOfInstantiation, |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2806 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 2807 | TemplateSpecializationKind TSK) { |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2808 | // C++0x [temp.explicit]p7: |
| 2809 | // An explicit instantiation that names a class template |
| 2810 | // specialization is an explicit instantion of the same kind |
| 2811 | // (declaration or definition) of each of its members (not |
| 2812 | // including members inherited from base classes) that has not |
| 2813 | // been previously explicitly specialized in the translation unit |
| 2814 | // containing the explicit instantiation, except as described |
| 2815 | // below. |
| 2816 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2817 | getTemplateInstantiationArgs(ClassTemplateSpec), |
| 2818 | TSK); |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2821 | StmtResult |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2822 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 2823 | if (!S) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2824 | return S; |
| Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 2825 | |
| 2826 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 2827 | SourceLocation(), |
| 2828 | DeclarationName()); |
| 2829 | return Instantiator.TransformStmt(S); |
| 2830 | } |
| 2831 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2832 | ExprResult |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2833 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 2834 | if (!E) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2835 | return E; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | |
| Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 2837 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 2838 | SourceLocation(), |
| 2839 | DeclarationName()); |
| 2840 | return Instantiator.TransformExpr(E); |
| 2841 | } |
| 2842 | |
| Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2843 | ExprResult Sema::SubstInitializer(Expr *Init, |
| 2844 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 2845 | bool CXXDirectInit) { |
| 2846 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 2847 | SourceLocation(), |
| 2848 | DeclarationName()); |
| 2849 | return Instantiator.TransformInitializer(Init, CXXDirectInit); |
| 2850 | } |
| 2851 | |
| Craig Topper | 99d2353 | 2015-12-24 23:58:29 +0000 | [diff] [blame] | 2852 | bool Sema::SubstExprs(ArrayRef<Expr *> Exprs, bool IsCall, |
| Douglas Gregor | 2cd32a0 | 2011-01-07 19:35:17 +0000 | [diff] [blame] | 2853 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2854 | SmallVectorImpl<Expr *> &Outputs) { |
| Craig Topper | 99d2353 | 2015-12-24 23:58:29 +0000 | [diff] [blame] | 2855 | if (Exprs.empty()) |
| Douglas Gregor | 2cd32a0 | 2011-01-07 19:35:17 +0000 | [diff] [blame] | 2856 | return false; |
| Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2857 | |
| Douglas Gregor | 2cd32a0 | 2011-01-07 19:35:17 +0000 | [diff] [blame] | 2858 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 2859 | SourceLocation(), |
| 2860 | DeclarationName()); |
| Craig Topper | 99d2353 | 2015-12-24 23:58:29 +0000 | [diff] [blame] | 2861 | return Instantiator.TransformExprs(Exprs.data(), Exprs.size(), |
| 2862 | IsCall, Outputs); |
| Douglas Gregor | 2cd32a0 | 2011-01-07 19:35:17 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2865 | NestedNameSpecifierLoc |
| 2866 | Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2867 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2868 | if (!NNS) |
| 2869 | return NestedNameSpecifierLoc(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2870 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2871 | TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(), |
| 2872 | DeclarationName()); |
| 2873 | return Instantiator.TransformNestedNameSpecifierLoc(NNS); |
| 2874 | } |
| 2875 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2876 | /// Do template substitution on declaration name info. |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2877 | DeclarationNameInfo |
| 2878 | Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo, |
| 2879 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 2880 | TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(), |
| 2881 | NameInfo.getName()); |
| 2882 | return Instantiator.TransformDeclarationNameInfo(NameInfo); |
| 2883 | } |
| 2884 | |
| Douglas Gregor | aa59489 | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 2885 | TemplateName |
| Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2886 | Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc, |
| 2887 | TemplateName Name, SourceLocation Loc, |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2888 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Douglas Gregor | 71dc509 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 2889 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 2890 | DeclarationName()); |
| Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2891 | CXXScopeSpec SS; |
| 2892 | SS.Adopt(QualifierLoc); |
| 2893 | return Instantiator.TransformTemplateName(SS, Name, Loc); |
| Douglas Gregor | aa59489 | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 2894 | } |
| Douglas Gregor | c43620d | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 2895 | |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2896 | bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs, |
| 2897 | TemplateArgumentListInfo &Result, |
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2898 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Douglas Gregor | e922c77 | 2009-08-04 22:27:00 +0000 | [diff] [blame] | 2899 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 2900 | DeclarationName()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2901 | |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2902 | return Instantiator.TransformTemplateArguments(Args, NumArgs, Result); |
| Douglas Gregor | c43620d | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 2903 | } |
| Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2904 | |
| Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2905 | static const Decl *getCanonicalParmVarDecl(const Decl *D) { |
| DeLesley Hutchins | f39c0c2 | 2012-09-26 17:57:31 +0000 | [diff] [blame] | 2906 | // When storing ParmVarDecls in the local instantiation scope, we always |
| 2907 | // want to use the ParmVarDecl from the canonical function declaration, |
| 2908 | // since the map is then valid for any redeclaration or definition of that |
| 2909 | // function. |
| 2910 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(D)) { |
| 2911 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { |
| 2912 | unsigned i = PV->getFunctionScopeIndex(); |
| Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2913 | // This parameter might be from a freestanding function type within the |
| 2914 | // function and isn't necessarily referring to one of FD's parameters. |
| Dmitri Gribenko | 66b6bb1 | 2019-04-10 20:25:07 +0000 | [diff] [blame] | 2915 | if (i < FD->getNumParams() && FD->getParamDecl(i) == PV) |
| Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2916 | return FD->getCanonicalDecl()->getParamDecl(i); |
| DeLesley Hutchins | f39c0c2 | 2012-09-26 17:57:31 +0000 | [diff] [blame] | 2917 | } |
| 2918 | } |
| 2919 | return D; |
| 2920 | } |
| 2921 | |
| 2922 | |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2923 | llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> * |
| 2924 | LocalInstantiationScope::findInstantiationOf(const Decl *D) { |
| DeLesley Hutchins | f39c0c2 | 2012-09-26 17:57:31 +0000 | [diff] [blame] | 2925 | D = getCanonicalParmVarDecl(D); |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 2926 | for (LocalInstantiationScope *Current = this; Current; |
| Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2927 | Current = Current->Outer) { |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 2928 | |
| Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2929 | // Check if we found something within this scope. |
| Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 2930 | const Decl *CheckD = D; |
| 2931 | do { |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2932 | LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD); |
| Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 2933 | if (Found != Current->LocalDecls.end()) |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2934 | return &Found->second; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2935 | |
| Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 2936 | // If this is a tag declaration, it's possible that we need to look for |
| 2937 | // a previous declaration. |
| 2938 | if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD)) |
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2939 | CheckD = Tag->getPreviousDecl(); |
| Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 2940 | else |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2941 | CheckD = nullptr; |
| Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 2942 | } while (CheckD); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2943 | |
| 2944 | // If we aren't combined with our outer scope, we're done. |
| Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2945 | if (!Current->CombineWithOuterScope) |
| 2946 | break; |
| 2947 | } |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 2948 | |
| Serge Pavlov | 7cd8f60 | 2013-07-15 06:14:07 +0000 | [diff] [blame] | 2949 | // If we're performing a partial substitution during template argument |
| 2950 | // deduction, we may not have values for template parameters yet. |
| 2951 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
| 2952 | isa<TemplateTemplateParmDecl>(D)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2953 | return nullptr; |
| Serge Pavlov | 7cd8f60 | 2013-07-15 06:14:07 +0000 | [diff] [blame] | 2954 | |
| Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 2955 | // Local types referenced prior to definition may require instantiation. |
| 2956 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 2957 | if (RD->isLocalClass()) |
| 2958 | return nullptr; |
| 2959 | |
| 2960 | // Enumeration types referenced prior to definition may appear as a result of |
| 2961 | // error recovery. |
| 2962 | if (isa<EnumDecl>(D)) |
| Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 2963 | return nullptr; |
| 2964 | |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 2965 | // If we didn't find the decl, then we either have a sema bug, or we have a |
| 2966 | // forward reference to a label declaration. Return null to indicate that |
| 2967 | // we have an uninstantiated label. |
| 2968 | assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope"); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2969 | return nullptr; |
| Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2972 | void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) { |
| DeLesley Hutchins | f39c0c2 | 2012-09-26 17:57:31 +0000 | [diff] [blame] | 2973 | D = getCanonicalParmVarDecl(D); |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2974 | llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; |
| Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2975 | if (Stored.isNull()) { |
| 2976 | #ifndef NDEBUG |
| 2977 | // It should not be present in any surrounding scope either. |
| 2978 | LocalInstantiationScope *Current = this; |
| 2979 | while (Current->CombineWithOuterScope && Current->Outer) { |
| 2980 | Current = Current->Outer; |
| 2981 | assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() && |
| 2982 | "Instantiated local in inner and outer scopes"); |
| 2983 | } |
| 2984 | #endif |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2985 | Stored = Inst; |
| Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2986 | } else if (DeclArgumentPack *Pack = Stored.dyn_cast<DeclArgumentPack *>()) { |
| James Y Knight | 48fefa3 | 2015-09-30 14:04:23 +0000 | [diff] [blame] | 2987 | Pack->push_back(cast<ParmVarDecl>(Inst)); |
| Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2988 | } else { |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2989 | assert(Stored.get<Decl *>() == Inst && "Already instantiated this local"); |
| Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2990 | } |
| Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2991 | } |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2992 | |
| James Y Knight | 48fefa3 | 2015-09-30 14:04:23 +0000 | [diff] [blame] | 2993 | void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D, |
| 2994 | ParmVarDecl *Inst) { |
| DeLesley Hutchins | f39c0c2 | 2012-09-26 17:57:31 +0000 | [diff] [blame] | 2995 | D = getCanonicalParmVarDecl(D); |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2996 | DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>(); |
| 2997 | Pack->push_back(Inst); |
| 2998 | } |
| 2999 | |
| 3000 | void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) { |
| Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 3001 | #ifndef NDEBUG |
| 3002 | // This should be the first time we've been told about this decl. |
| 3003 | for (LocalInstantiationScope *Current = this; |
| 3004 | Current && Current->CombineWithOuterScope; Current = Current->Outer) |
| 3005 | assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() && |
| 3006 | "Creating local pack after instantiation of local"); |
| 3007 | #endif |
| 3008 | |
| DeLesley Hutchins | f39c0c2 | 2012-09-26 17:57:31 +0000 | [diff] [blame] | 3009 | D = getCanonicalParmVarDecl(D); |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3010 | llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3011 | DeclArgumentPack *Pack = new DeclArgumentPack; |
| 3012 | Stored = Pack; |
| 3013 | ArgumentPacks.push_back(Pack); |
| 3014 | } |
| 3015 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3016 | void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack, |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3017 | const TemplateArgument *ExplicitArgs, |
| 3018 | unsigned NumExplicitArgs) { |
| 3019 | assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) && |
| 3020 | "Already have a partially-substituted pack"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3021 | assert((!PartiallySubstitutedPack |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3022 | || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) && |
| 3023 | "Wrong number of arguments in partially-substituted pack"); |
| 3024 | PartiallySubstitutedPack = Pack; |
| 3025 | ArgsInPartiallySubstitutedPack = ExplicitArgs; |
| 3026 | NumArgsInPartiallySubstitutedPack = NumExplicitArgs; |
| 3027 | } |
| 3028 | |
| 3029 | NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack( |
| 3030 | const TemplateArgument **ExplicitArgs, |
| 3031 | unsigned *NumExplicitArgs) const { |
| 3032 | if (ExplicitArgs) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3033 | *ExplicitArgs = nullptr; |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3034 | if (NumExplicitArgs) |
| 3035 | *NumExplicitArgs = 0; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3036 | |
| 3037 | for (const LocalInstantiationScope *Current = this; Current; |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3038 | Current = Current->Outer) { |
| 3039 | if (Current->PartiallySubstitutedPack) { |
| 3040 | if (ExplicitArgs) |
| 3041 | *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack; |
| 3042 | if (NumExplicitArgs) |
| 3043 | *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3044 | |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3045 | return Current->PartiallySubstitutedPack; |
| 3046 | } |
| 3047 | |
| 3048 | if (!Current->CombineWithOuterScope) |
| 3049 | break; |
| 3050 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3051 | |
| 3052 | return nullptr; |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3053 | } |