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