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