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