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