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