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