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