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