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