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