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