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