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