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