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