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