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