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