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