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