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