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