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