Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template instantiation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 15 | #include "clang/Sema/DeclSpec.h" |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Initialization.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Lookup.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Template.h" |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 19 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
| 22 | #include "clang/AST/Expr.h" |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 24 | #include "clang/Basic/LangOptions.h" |
| 25 | |
| 26 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 27 | using namespace sema; |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===/ |
| 30 | // Template Instantiation Support |
| 31 | //===----------------------------------------------------------------------===/ |
| 32 | |
Douglas Gregor | 01afeef | 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 | 36d7c5f | 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 | 8c70253 | 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 | 1bd7a94 | 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 | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 50 | MultiLevelTemplateArgumentList |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 51 | Sema::getTemplateInstantiationArgs(NamedDecl *D, |
Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 52 | const TemplateArgumentList *Innermost, |
Douglas Gregor | 1bd7a94 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 53 | bool RelativeToPrimary, |
| 54 | const FunctionDecl *Pattern) { |
Douglas Gregor | a654dd8 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 58 | if (Innermost) |
| 59 | Result.addOuterTemplateArguments(Innermost); |
| 60 | |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 61 | DeclContext *Ctx = dyn_cast<DeclContext>(D); |
Douglas Gregor | a51c9cc | 2011-05-22 00:21:10 +0000 | [diff] [blame] | 62 | if (!Ctx) { |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 63 | Ctx = D->getDeclContext(); |
Douglas Gregor | a51c9cc | 2011-05-22 00:21:10 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | 5546262 | 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) |
| 75 | Result.addOuterTemplateArguments(0, 0); |
| 76 | return Result; |
| 77 | } |
| 78 | } |
Douglas Gregor | a51c9cc | 2011-05-22 00:21:10 +0000 | [diff] [blame] | 79 | } |
| 80 | |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 81 | while (!Ctx->isFileContext()) { |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 82 | // Add template arguments from a class template instantiation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | a654dd8 | 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 | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 86 | if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization && |
| 87 | !isa<ClassTemplatePartialSpecializationDecl>(Spec)) |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 88 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 90 | Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); |
Douglas Gregor | cf91555 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | } |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 98 | // Add template arguments from a function template specialization. |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 99 | else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) { |
Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 100 | if (!RelativeToPrimary && |
| 101 | Function->getTemplateSpecializationKind() |
| 102 | == TSK_ExplicitSpecialization) |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 103 | break; |
| 104 | |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 105 | if (const TemplateArgumentList *TemplateArgs |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 106 | = Function->getTemplateSpecializationArgs()) { |
| 107 | // Add the template arguments for this specialization. |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 108 | Result.addOuterTemplateArguments(TemplateArgs); |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 109 | |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 110 | // If this function was instantiated from a specialized member that is |
| 111 | // a function template, we're done. |
| 112 | assert(Function->getPrimaryTemplate() && "No function template?"); |
| 113 | if (Function->getPrimaryTemplate()->isMemberSpecialization()) |
| 114 | break; |
Douglas Gregor | 43669f8 | 2011-03-05 17:54:25 +0000 | [diff] [blame] | 115 | } else if (FunctionTemplateDecl *FunTmpl |
| 116 | = Function->getDescribedFunctionTemplate()) { |
| 117 | // Add the "injected" template arguments. |
| 118 | std::pair<const TemplateArgument *, unsigned> |
| 119 | Injected = FunTmpl->getInjectedTemplateArgs(); |
| 120 | Result.addOuterTemplateArguments(Injected.first, Injected.second); |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 123 | // If this is a friend declaration and it declares an entity at |
| 124 | // namespace scope, take arguments from its lexical parent |
Douglas Gregor | 1bd7a94 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 125 | // instead of its semantic parent, unless of course the pattern we're |
| 126 | // instantiating actually comes from the file's context! |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 127 | if (Function->getFriendObjectKind() && |
Douglas Gregor | 1bd7a94 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 128 | Function->getDeclContext()->isFileContext() && |
| 129 | (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) { |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 130 | Ctx = Function->getLexicalDeclContext(); |
Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 131 | RelativeToPrimary = false; |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 132 | continue; |
| 133 | } |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 134 | } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 135 | if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) { |
| 136 | QualType T = ClassTemplate->getInjectedClassNameSpecialization(); |
| 137 | const TemplateSpecializationType *TST |
| 138 | = cast<TemplateSpecializationType>(Context.getCanonicalType(T)); |
| 139 | Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs()); |
| 140 | if (ClassTemplate->isMemberSpecialization()) |
| 141 | break; |
| 142 | } |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 143 | } |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 144 | |
| 145 | Ctx = Ctx->getParent(); |
Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 146 | RelativeToPrimary = false; |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 147 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 149 | return Result; |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 152 | bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const { |
| 153 | switch (Kind) { |
| 154 | case TemplateInstantiation: |
| 155 | case DefaultTemplateArgumentInstantiation: |
| 156 | case DefaultFunctionArgumentInstantiation: |
| 157 | return true; |
| 158 | |
| 159 | case ExplicitTemplateArgumentSubstitution: |
| 160 | case DeducedTemplateArgumentSubstitution: |
| 161 | case PriorTemplateArgumentSubstitution: |
| 162 | case DefaultTemplateArgumentChecking: |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | return true; |
| 167 | } |
| 168 | |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 169 | Sema::InstantiatingTemplate:: |
| 170 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 171 | Decl *Entity, |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 172 | SourceRange InstantiationRange) |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 173 | : SemaRef(SemaRef), |
| 174 | SavedInNonInstantiationSFINAEContext( |
| 175 | SemaRef.InNonInstantiationSFINAEContext) |
| 176 | { |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 177 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 178 | InstantiationRange); |
| 179 | if (!Invalid) { |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 180 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 181 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 182 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 183 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | c922083 | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 184 | Inst.TemplateArgs = 0; |
| 185 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 186 | Inst.InstantiationRange = InstantiationRange; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 187 | SemaRef.InNonInstantiationSFINAEContext = false; |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 188 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 193 | SourceLocation PointOfInstantiation, |
| 194 | TemplateDecl *Template, |
| 195 | const TemplateArgument *TemplateArgs, |
| 196 | unsigned NumTemplateArgs, |
| 197 | SourceRange InstantiationRange) |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 198 | : SemaRef(SemaRef), |
| 199 | SavedInNonInstantiationSFINAEContext( |
| 200 | SemaRef.InNonInstantiationSFINAEContext) |
| 201 | { |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 202 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 203 | InstantiationRange); |
| 204 | if (!Invalid) { |
| 205 | ActiveTemplateInstantiation Inst; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | Inst.Kind |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 207 | = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; |
| 208 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 209 | Inst.Entity = reinterpret_cast<uintptr_t>(Template); |
| 210 | Inst.TemplateArgs = TemplateArgs; |
| 211 | Inst.NumTemplateArgs = NumTemplateArgs; |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 212 | Inst.InstantiationRange = InstantiationRange; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 213 | SemaRef.InNonInstantiationSFINAEContext = false; |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 214 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 219 | SourceLocation PointOfInstantiation, |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 220 | FunctionTemplateDecl *FunctionTemplate, |
| 221 | const TemplateArgument *TemplateArgs, |
| 222 | unsigned NumTemplateArgs, |
| 223 | ActiveTemplateInstantiation::InstantiationKind Kind, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 224 | sema::TemplateDeductionInfo &DeductionInfo, |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 225 | SourceRange InstantiationRange) |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 226 | : SemaRef(SemaRef), |
| 227 | SavedInNonInstantiationSFINAEContext( |
| 228 | SemaRef.InNonInstantiationSFINAEContext) |
| 229 | { |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 230 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 231 | InstantiationRange); |
| 232 | if (!Invalid) { |
| 233 | ActiveTemplateInstantiation Inst; |
| 234 | Inst.Kind = Kind; |
| 235 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 236 | Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate); |
| 237 | Inst.TemplateArgs = TemplateArgs; |
| 238 | Inst.NumTemplateArgs = NumTemplateArgs; |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 239 | Inst.DeductionInfo = &DeductionInfo; |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 240 | Inst.InstantiationRange = InstantiationRange; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 241 | SemaRef.InNonInstantiationSFINAEContext = false; |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 242 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 243 | |
| 244 | if (!Inst.isInstantiationRecord()) |
| 245 | ++SemaRef.NonInstantiationEntries; |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 250 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 251 | ClassTemplatePartialSpecializationDecl *PartialSpec, |
| 252 | const TemplateArgument *TemplateArgs, |
| 253 | unsigned NumTemplateArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 254 | sema::TemplateDeductionInfo &DeductionInfo, |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 255 | SourceRange InstantiationRange) |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 256 | : SemaRef(SemaRef), |
| 257 | SavedInNonInstantiationSFINAEContext( |
| 258 | SemaRef.InNonInstantiationSFINAEContext) |
| 259 | { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 260 | Invalid = false; |
| 261 | |
| 262 | ActiveTemplateInstantiation Inst; |
| 263 | Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution; |
| 264 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 265 | Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec); |
| 266 | Inst.TemplateArgs = TemplateArgs; |
| 267 | Inst.NumTemplateArgs = NumTemplateArgs; |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 268 | Inst.DeductionInfo = &DeductionInfo; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 269 | Inst.InstantiationRange = InstantiationRange; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 270 | SemaRef.InNonInstantiationSFINAEContext = false; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 271 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 272 | |
| 273 | assert(!Inst.isInstantiationRecord()); |
| 274 | ++SemaRef.NonInstantiationEntries; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 278 | SourceLocation PointOfInstantiation, |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 279 | ParmVarDecl *Param, |
| 280 | const TemplateArgument *TemplateArgs, |
| 281 | unsigned NumTemplateArgs, |
| 282 | SourceRange InstantiationRange) |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 283 | : SemaRef(SemaRef), |
| 284 | SavedInNonInstantiationSFINAEContext( |
| 285 | SemaRef.InNonInstantiationSFINAEContext) |
| 286 | { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 287 | Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange); |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 288 | |
| 289 | if (!Invalid) { |
| 290 | ActiveTemplateInstantiation Inst; |
| 291 | Inst.Kind |
| 292 | = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 293 | Inst.PointOfInstantiation = PointOfInstantiation; |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 294 | Inst.Entity = reinterpret_cast<uintptr_t>(Param); |
| 295 | Inst.TemplateArgs = TemplateArgs; |
| 296 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 297 | Inst.InstantiationRange = InstantiationRange; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 298 | SemaRef.InNonInstantiationSFINAEContext = false; |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 299 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | Sema::InstantiatingTemplate:: |
| 304 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 305 | NamedDecl *Template, |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 306 | NonTypeTemplateParmDecl *Param, |
| 307 | const TemplateArgument *TemplateArgs, |
| 308 | unsigned NumTemplateArgs, |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 309 | SourceRange InstantiationRange) |
| 310 | : SemaRef(SemaRef), |
| 311 | SavedInNonInstantiationSFINAEContext( |
| 312 | SemaRef.InNonInstantiationSFINAEContext) |
| 313 | { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 314 | Invalid = false; |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 315 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 316 | ActiveTemplateInstantiation Inst; |
| 317 | Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution; |
| 318 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 319 | Inst.Template = Template; |
| 320 | Inst.Entity = reinterpret_cast<uintptr_t>(Param); |
| 321 | Inst.TemplateArgs = TemplateArgs; |
| 322 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 323 | Inst.InstantiationRange = InstantiationRange; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 324 | SemaRef.InNonInstantiationSFINAEContext = false; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 325 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 326 | |
| 327 | assert(!Inst.isInstantiationRecord()); |
| 328 | ++SemaRef.NonInstantiationEntries; |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | Sema::InstantiatingTemplate:: |
| 332 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 333 | NamedDecl *Template, |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 334 | TemplateTemplateParmDecl *Param, |
| 335 | const TemplateArgument *TemplateArgs, |
| 336 | unsigned NumTemplateArgs, |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 337 | SourceRange InstantiationRange) |
| 338 | : SemaRef(SemaRef), |
| 339 | SavedInNonInstantiationSFINAEContext( |
| 340 | SemaRef.InNonInstantiationSFINAEContext) |
| 341 | { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 342 | Invalid = false; |
| 343 | ActiveTemplateInstantiation Inst; |
| 344 | Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution; |
| 345 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 346 | Inst.Template = Template; |
| 347 | Inst.Entity = reinterpret_cast<uintptr_t>(Param); |
| 348 | Inst.TemplateArgs = TemplateArgs; |
| 349 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 350 | Inst.InstantiationRange = InstantiationRange; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 351 | SemaRef.InNonInstantiationSFINAEContext = false; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 352 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 353 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 354 | assert(!Inst.isInstantiationRecord()); |
| 355 | ++SemaRef.NonInstantiationEntries; |
| 356 | } |
| 357 | |
| 358 | Sema::InstantiatingTemplate:: |
| 359 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 360 | TemplateDecl *Template, |
| 361 | NamedDecl *Param, |
| 362 | const TemplateArgument *TemplateArgs, |
| 363 | unsigned NumTemplateArgs, |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 364 | SourceRange InstantiationRange) |
| 365 | : SemaRef(SemaRef), |
| 366 | SavedInNonInstantiationSFINAEContext( |
| 367 | SemaRef.InNonInstantiationSFINAEContext) |
| 368 | { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 369 | Invalid = false; |
| 370 | |
| 371 | ActiveTemplateInstantiation Inst; |
| 372 | Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking; |
| 373 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 374 | Inst.Template = Template; |
| 375 | Inst.Entity = reinterpret_cast<uintptr_t>(Param); |
| 376 | Inst.TemplateArgs = TemplateArgs; |
| 377 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 378 | Inst.InstantiationRange = InstantiationRange; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 379 | SemaRef.InNonInstantiationSFINAEContext = false; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 380 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 381 | |
| 382 | assert(!Inst.isInstantiationRecord()); |
| 383 | ++SemaRef.NonInstantiationEntries; |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 386 | void Sema::InstantiatingTemplate::Clear() { |
| 387 | if (!Invalid) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 388 | if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) { |
| 389 | assert(SemaRef.NonInstantiationEntries > 0); |
| 390 | --SemaRef.NonInstantiationEntries; |
| 391 | } |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 392 | SemaRef.InNonInstantiationSFINAEContext |
| 393 | = SavedInNonInstantiationSFINAEContext; |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 394 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 395 | Invalid = true; |
| 396 | } |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 399 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 400 | SourceLocation PointOfInstantiation, |
| 401 | SourceRange InstantiationRange) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 402 | assert(SemaRef.NonInstantiationEntries <= |
| 403 | SemaRef.ActiveTemplateInstantiations.size()); |
| 404 | if ((SemaRef.ActiveTemplateInstantiations.size() - |
| 405 | SemaRef.NonInstantiationEntries) |
| 406 | <= SemaRef.getLangOptions().InstantiationDepth) |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 407 | return false; |
| 408 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | SemaRef.Diag(PointOfInstantiation, |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 410 | diag::err_template_recursion_depth_exceeded) |
| 411 | << SemaRef.getLangOptions().InstantiationDepth |
| 412 | << InstantiationRange; |
| 413 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
| 414 | << SemaRef.getLangOptions().InstantiationDepth; |
| 415 | return true; |
| 416 | } |
| 417 | |
Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 418 | /// \brief Prints the current instantiation stack through a series of |
| 419 | /// notes. |
| 420 | void Sema::PrintInstantiationStack() { |
Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 421 | // Determine which template instantiations to skip, if any. |
| 422 | unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart; |
| 423 | unsigned Limit = Diags.getTemplateBacktraceLimit(); |
| 424 | if (Limit && Limit < ActiveTemplateInstantiations.size()) { |
| 425 | SkipStart = Limit / 2 + Limit % 2; |
| 426 | SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2; |
| 427 | } |
| 428 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 429 | // FIXME: In all of these cases, we need to show the template arguments |
Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 430 | unsigned InstantiationIdx = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 431 | for (SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator |
Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 432 | Active = ActiveTemplateInstantiations.rbegin(), |
| 433 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 434 | Active != ActiveEnd; |
Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 435 | ++Active, ++InstantiationIdx) { |
| 436 | // Skip this instantiation? |
| 437 | if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) { |
| 438 | if (InstantiationIdx == SkipStart) { |
| 439 | // Note that we're skipping instantiations. |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 440 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | ffed1cb | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 441 | diag::note_instantiation_contexts_suppressed) |
| 442 | << unsigned(ActiveTemplateInstantiations.size() - Limit); |
| 443 | } |
| 444 | continue; |
| 445 | } |
| 446 | |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 447 | switch (Active->Kind) { |
| 448 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 449 | Decl *D = reinterpret_cast<Decl *>(Active->Entity); |
| 450 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 451 | unsigned DiagID = diag::note_template_member_class_here; |
| 452 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 453 | DiagID = diag::note_template_class_instantiation_here; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 454 | Diags.Report(Active->PointOfInstantiation, DiagID) |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 455 | << Context.getTypeDeclType(Record) |
| 456 | << Active->InstantiationRange; |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 457 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 458 | unsigned DiagID; |
| 459 | if (Function->getPrimaryTemplate()) |
| 460 | DiagID = diag::note_function_template_spec_here; |
| 461 | else |
| 462 | DiagID = diag::note_template_member_function_here; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 463 | Diags.Report(Active->PointOfInstantiation, DiagID) |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 464 | << Function |
| 465 | << Active->InstantiationRange; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 466 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 467 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 468 | diag::note_template_static_data_member_def_here) |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 469 | << VD |
| 470 | << Active->InstantiationRange; |
| 471 | } else { |
| 472 | Diags.Report(Active->PointOfInstantiation, |
| 473 | diag::note_template_type_alias_instantiation_here) |
| 474 | << cast<TypeAliasTemplateDecl>(D) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 475 | << Active->InstantiationRange; |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 476 | } |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 477 | break; |
| 478 | } |
| 479 | |
| 480 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { |
| 481 | TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); |
| 482 | std::string TemplateArgsStr |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 483 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | Active->TemplateArgs, |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 485 | Active->NumTemplateArgs, |
| 486 | Context.PrintingPolicy); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 487 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 488 | diag::note_default_arg_instantiation_here) |
| 489 | << (Template->getNameAsString() + TemplateArgsStr) |
| 490 | << Active->InstantiationRange; |
| 491 | break; |
| 492 | } |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 494 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | FunctionTemplateDecl *FnTmpl |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 496 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 497 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 498 | diag::note_explicit_template_arg_substitution_here) |
Douglas Gregor | 607f141 | 2010-03-30 20:35:20 +0000 | [diff] [blame] | 499 | << FnTmpl |
| 500 | << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), |
| 501 | Active->TemplateArgs, |
| 502 | Active->NumTemplateArgs) |
| 503 | << Active->InstantiationRange; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 504 | break; |
| 505 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 506 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 507 | case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: |
| 508 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 509 | = dyn_cast<ClassTemplatePartialSpecializationDecl>( |
| 510 | (Decl *)Active->Entity)) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 511 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 512 | diag::note_partial_spec_deduct_instantiation_here) |
| 513 | << Context.getTypeDeclType(PartialSpec) |
Douglas Gregor | 607f141 | 2010-03-30 20:35:20 +0000 | [diff] [blame] | 514 | << getTemplateArgumentBindingsText( |
| 515 | PartialSpec->getTemplateParameters(), |
| 516 | Active->TemplateArgs, |
| 517 | Active->NumTemplateArgs) |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 518 | << Active->InstantiationRange; |
| 519 | } else { |
| 520 | FunctionTemplateDecl *FnTmpl |
| 521 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 522 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 523 | diag::note_function_template_deduction_instantiation_here) |
Douglas Gregor | 607f141 | 2010-03-30 20:35:20 +0000 | [diff] [blame] | 524 | << FnTmpl |
| 525 | << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), |
| 526 | Active->TemplateArgs, |
| 527 | Active->NumTemplateArgs) |
| 528 | << Active->InstantiationRange; |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 529 | } |
| 530 | break; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 531 | |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 532 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: { |
| 533 | ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity); |
| 534 | FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 536 | std::string TemplateArgsStr |
| 537 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | Active->TemplateArgs, |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 539 | Active->NumTemplateArgs, |
| 540 | Context.PrintingPolicy); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 541 | Diags.Report(Active->PointOfInstantiation, |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 542 | diag::note_default_function_arg_instantiation_here) |
Anders Carlsson | dc6d2c3 | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 543 | << (FD->getNameAsString() + TemplateArgsStr) |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 544 | << Active->InstantiationRange; |
| 545 | break; |
| 546 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 548 | case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: { |
| 549 | NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity); |
| 550 | std::string Name; |
| 551 | if (!Parm->getName().empty()) |
| 552 | Name = std::string(" '") + Parm->getName().str() + "'"; |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 553 | |
| 554 | TemplateParameterList *TemplateParams = 0; |
| 555 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) |
| 556 | TemplateParams = Template->getTemplateParameters(); |
| 557 | else |
| 558 | TemplateParams = |
| 559 | cast<ClassTemplatePartialSpecializationDecl>(Active->Template) |
| 560 | ->getTemplateParameters(); |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 561 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 562 | diag::note_prior_template_arg_substitution) |
| 563 | << isa<TemplateTemplateParmDecl>(Parm) |
| 564 | << Name |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 565 | << getTemplateArgumentBindingsText(TemplateParams, |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 566 | Active->TemplateArgs, |
| 567 | Active->NumTemplateArgs) |
| 568 | << Active->InstantiationRange; |
| 569 | break; |
| 570 | } |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 571 | |
| 572 | case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 573 | TemplateParameterList *TemplateParams = 0; |
| 574 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) |
| 575 | TemplateParams = Template->getTemplateParameters(); |
| 576 | else |
| 577 | TemplateParams = |
| 578 | cast<ClassTemplatePartialSpecializationDecl>(Active->Template) |
| 579 | ->getTemplateParameters(); |
| 580 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 581 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 582 | diag::note_template_default_arg_checking) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 583 | << getTemplateArgumentBindingsText(TemplateParams, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 584 | Active->TemplateArgs, |
| 585 | Active->NumTemplateArgs) |
| 586 | << Active->InstantiationRange; |
| 587 | break; |
| 588 | } |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 589 | } |
Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 593 | llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const { |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 594 | if (InNonInstantiationSFINAEContext) |
| 595 | return llvm::Optional<TemplateDeductionInfo *>(0); |
| 596 | |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 597 | for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator |
| 598 | Active = ActiveTemplateInstantiations.rbegin(), |
| 599 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 600 | Active != ActiveEnd; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 601 | ++Active) |
| 602 | { |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 603 | switch(Active->Kind) { |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 604 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 605 | case ActiveTemplateInstantiation::TemplateInstantiation: |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 606 | // This is a template instantiation, so there is no SFINAE. |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 607 | return llvm::Optional<TemplateDeductionInfo *>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 609 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 610 | case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 611 | case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 612 | // A default template argument instantiation and substitution into |
| 613 | // template parameters with arguments for prior parameters may or may |
| 614 | // not be a SFINAE context; look further up the stack. |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 615 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 617 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: |
| 618 | case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: |
| 619 | // We're either substitution explicitly-specified template arguments |
| 620 | // or deduced template arguments, so SFINAE applies. |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 621 | assert(Active->DeductionInfo && "Missing deduction info pointer"); |
| 622 | return Active->DeductionInfo; |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 626 | return llvm::Optional<TemplateDeductionInfo *>(); |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 629 | /// \brief Retrieve the depth and index of a parameter pack. |
| 630 | static std::pair<unsigned, unsigned> |
| 631 | getDepthAndIndex(NamedDecl *ND) { |
| 632 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 633 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 634 | |
| 635 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 636 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
| 637 | |
| 638 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 639 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 640 | } |
| 641 | |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 642 | //===----------------------------------------------------------------------===/ |
| 643 | // Template Instantiation for Types |
| 644 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 645 | namespace { |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 646 | class TemplateInstantiator : public TreeTransform<TemplateInstantiator> { |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 647 | const MultiLevelTemplateArgumentList &TemplateArgs; |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 648 | SourceLocation Loc; |
| 649 | DeclarationName Entity; |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 650 | |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 651 | public: |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 652 | typedef TreeTransform<TemplateInstantiator> inherited; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | |
| 654 | TemplateInstantiator(Sema &SemaRef, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 655 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 656 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | DeclarationName Entity) |
| 658 | : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 659 | Entity(Entity) { } |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 660 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 662 | /// transformed. |
| 663 | /// |
| 664 | /// For the purposes of template instantiation, a type has already been |
| 665 | /// transformed if it is NULL or if it is not dependent. |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 666 | bool AlreadyTransformed(QualType T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 668 | /// \brief Returns the location of the entity being instantiated, if known. |
| 669 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 671 | /// \brief Returns the name of the entity being instantiated, if any. |
| 672 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 674 | /// \brief Sets the "base" location and entity when that |
| 675 | /// information is known based on another transformation. |
| 676 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 677 | this->Loc = Loc; |
| 678 | this->Entity = Entity; |
| 679 | } |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 680 | |
| 681 | bool TryExpandParameterPacks(SourceLocation EllipsisLoc, |
| 682 | SourceRange PatternRange, |
| 683 | const UnexpandedParameterPack *Unexpanded, |
| 684 | unsigned NumUnexpanded, |
| 685 | bool &ShouldExpand, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 686 | bool &RetainExpansion, |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 687 | llvm::Optional<unsigned> &NumExpansions) { |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 688 | return getSema().CheckParameterPacksForExpansion(EllipsisLoc, |
| 689 | PatternRange, Unexpanded, |
| 690 | NumUnexpanded, |
| 691 | TemplateArgs, |
| 692 | ShouldExpand, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 693 | RetainExpansion, |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 694 | NumExpansions); |
| 695 | } |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 696 | |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 697 | void ExpandingFunctionParameterPack(ParmVarDecl *Pack) { |
| 698 | SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack); |
| 699 | } |
| 700 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 701 | TemplateArgument ForgetPartiallySubstitutedPack() { |
| 702 | TemplateArgument Result; |
| 703 | if (NamedDecl *PartialPack |
| 704 | = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
| 705 | MultiLevelTemplateArgumentList &TemplateArgs |
| 706 | = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
| 707 | unsigned Depth, Index; |
| 708 | llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack); |
| 709 | if (TemplateArgs.hasTemplateArgument(Depth, Index)) { |
| 710 | Result = TemplateArgs(Depth, Index); |
| 711 | TemplateArgs.setArgument(Depth, Index, TemplateArgument()); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | return Result; |
| 716 | } |
| 717 | |
| 718 | void RememberPartiallySubstitutedPack(TemplateArgument Arg) { |
| 719 | if (Arg.isNull()) |
| 720 | return; |
| 721 | |
| 722 | if (NamedDecl *PartialPack |
| 723 | = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
| 724 | MultiLevelTemplateArgumentList &TemplateArgs |
| 725 | = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
| 726 | unsigned Depth, Index; |
| 727 | llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack); |
| 728 | TemplateArgs.setArgument(Depth, Index, Arg); |
| 729 | } |
| 730 | } |
| 731 | |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 732 | /// \brief Transform the given declaration by instantiating a reference to |
| 733 | /// this declaration. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 734 | Decl *TransformDecl(SourceLocation Loc, Decl *D); |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 735 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 736 | /// \brief Transform the definition of the given declaration by |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 737 | /// instantiating it. |
Douglas Gregor | 2528936 | 2010-03-01 17:25:41 +0000 | [diff] [blame] | 738 | Decl *TransformDefinition(SourceLocation Loc, Decl *D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | |
Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 740 | /// \bried Transform the first qualifier within a scope by instantiating the |
| 741 | /// declaration. |
| 742 | NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); |
| 743 | |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 744 | /// \brief Rebuild the exception declaration and register the declaration |
| 745 | /// as an instantiated local. |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 746 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 747 | TypeSourceInfo *Declarator, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 748 | SourceLocation StartLoc, |
| 749 | SourceLocation NameLoc, |
| 750 | IdentifierInfo *Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | |
Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 752 | /// \brief Rebuild the Objective-C exception declaration and register the |
| 753 | /// declaration as an instantiated local. |
| 754 | VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
| 755 | TypeSourceInfo *TSInfo, QualType T); |
| 756 | |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 757 | /// \brief Check for tag mismatches when instantiating an |
| 758 | /// elaborated type. |
John McCall | 954b5de | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 759 | QualType RebuildElaboratedType(SourceLocation KeywordLoc, |
| 760 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 761 | NestedNameSpecifierLoc QualifierLoc, |
| 762 | QualType T); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 763 | |
Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 764 | TemplateName TransformTemplateName(CXXScopeSpec &SS, |
| 765 | TemplateName Name, |
| 766 | SourceLocation NameLoc, |
| 767 | QualType ObjectType = QualType(), |
| 768 | NamedDecl *FirstQualifierInScope = 0); |
| 769 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 770 | ExprResult TransformPredefinedExpr(PredefinedExpr *E); |
| 771 | ExprResult TransformDeclRefExpr(DeclRefExpr *E); |
| 772 | ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
| 773 | ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E, |
Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 774 | NonTypeTemplateParmDecl *D); |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 775 | ExprResult TransformSubstNonTypeTemplateParmPackExpr( |
| 776 | SubstNonTypeTemplateParmPackExpr *E); |
| 777 | |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 778 | QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
John McCall | 31f8272 | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 779 | FunctionProtoTypeLoc TL); |
Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 780 | ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm, |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 781 | int indexAdjustment, |
Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 782 | llvm::Optional<unsigned> NumExpansions); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 783 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | /// \brief Transforms a template type parameter type by performing |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 785 | /// substitution of the corresponding template type argument. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 786 | QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
John McCall | 31f8272 | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 787 | TemplateTypeParmTypeLoc TL); |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 788 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 789 | /// \brief Transforms an already-substituted template type parameter pack |
| 790 | /// into either itself (if we aren't substituting into its pack expansion) |
| 791 | /// or the appropriate substituted argument. |
| 792 | QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB, |
| 793 | SubstTemplateTypeParmPackTypeLoc TL); |
| 794 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 795 | ExprResult TransformCallExpr(CallExpr *CE) { |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 796 | getSema().CallsUndergoingInstantiation.push_back(CE); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 797 | ExprResult Result = |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 798 | TreeTransform<TemplateInstantiator>::TransformCallExpr(CE); |
| 799 | getSema().CallsUndergoingInstantiation.pop_back(); |
| 800 | return move(Result); |
| 801 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 802 | |
| 803 | private: |
| 804 | ExprResult transformNonTypeTemplateParmRef(NonTypeTemplateParmDecl *parm, |
| 805 | SourceLocation loc, |
| 806 | const TemplateArgument &arg); |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 807 | }; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 810 | bool TemplateInstantiator::AlreadyTransformed(QualType T) { |
| 811 | if (T.isNull()) |
| 812 | return true; |
| 813 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 814 | if (T->isInstantiationDependentType() || T->isVariablyModifiedType()) |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 815 | return false; |
| 816 | |
| 817 | getSema().MarkDeclarationsReferencedInType(Loc, T); |
| 818 | return true; |
| 819 | } |
| 820 | |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 821 | Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) { |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 822 | if (!D) |
| 823 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 825 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 826 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | b9397108 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 827 | // If the corresponding template argument is NULL or non-existent, it's |
| 828 | // because we are performing instantiation from explicitly-specified |
| 829 | // template arguments in a function template, but there were some |
| 830 | // arguments left unspecified. |
| 831 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
| 832 | TTP->getPosition())) |
| 833 | return D; |
| 834 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 835 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
| 836 | |
| 837 | if (TTP->isParameterPack()) { |
| 838 | assert(Arg.getKind() == TemplateArgument::Pack && |
| 839 | "Missing argument pack"); |
| 840 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 841 | assert(getSema().ArgumentPackSubstitutionIndex >= 0); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 842 | assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 843 | Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; |
| 844 | } |
| 845 | |
| 846 | TemplateName Template = Arg.getAsTemplate(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 847 | assert(!Template.isNull() && Template.getAsTemplateDecl() && |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 848 | "Wrong kind of template template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 849 | return Template.getAsTemplateDecl(); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 850 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 852 | // Fall through to find the instantiated declaration for this template |
| 853 | // template parameter. |
Douglas Gregor | 71dc509 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 854 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 856 | return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs); |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Douglas Gregor | 2528936 | 2010-03-01 17:25:41 +0000 | [diff] [blame] | 859 | Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) { |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 860 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 861 | if (!Inst) |
| 862 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 864 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 865 | return Inst; |
| 866 | } |
| 867 | |
Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 868 | NamedDecl * |
| 869 | TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
| 870 | SourceLocation Loc) { |
| 871 | // If the first part of the nested-name-specifier was a template type |
| 872 | // parameter, instantiate that type parameter down to a tag type. |
| 873 | if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
| 874 | const TemplateTypeParmType *TTP |
| 875 | = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
Douglas Gregor | 53c3f4e | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 876 | |
Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 877 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | 53c3f4e | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 878 | // FIXME: This needs testing w/ member access expressions. |
| 879 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex()); |
| 880 | |
| 881 | if (TTP->isParameterPack()) { |
| 882 | assert(Arg.getKind() == TemplateArgument::Pack && |
| 883 | "Missing argument pack"); |
| 884 | |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 885 | if (getSema().ArgumentPackSubstitutionIndex == -1) |
Douglas Gregor | 53c3f4e | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 886 | return 0; |
Douglas Gregor | 53c3f4e | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 887 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 888 | assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); |
Douglas Gregor | 53c3f4e | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 889 | Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; |
| 890 | } |
| 891 | |
| 892 | QualType T = Arg.getAsType(); |
Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 893 | if (T.isNull()) |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 894 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 895 | |
| 896 | if (const TagType *Tag = T->getAs<TagType>()) |
| 897 | return Tag->getDecl(); |
| 898 | |
| 899 | // The resulting type is not a tag; complain. |
| 900 | getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
| 901 | return 0; |
| 902 | } |
| 903 | } |
| 904 | |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 905 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 908 | VarDecl * |
| 909 | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 910 | TypeSourceInfo *Declarator, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 911 | SourceLocation StartLoc, |
| 912 | SourceLocation NameLoc, |
| 913 | IdentifierInfo *Name) { |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 914 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 915 | StartLoc, NameLoc, Name); |
Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 916 | if (Var) |
| 917 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 918 | return Var; |
| 919 | } |
| 920 | |
| 921 | VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
| 922 | TypeSourceInfo *TSInfo, |
| 923 | QualType T) { |
| 924 | VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T); |
| 925 | if (Var) |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 926 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 927 | return Var; |
| 928 | } |
| 929 | |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 930 | QualType |
John McCall | 954b5de | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 931 | TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc, |
| 932 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 933 | NestedNameSpecifierLoc QualifierLoc, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 934 | QualType T) { |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 935 | if (const TagType *TT = T->getAs<TagType>()) { |
| 936 | TagDecl* TD = TT->getDecl(); |
| 937 | |
John McCall | 954b5de | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 938 | SourceLocation TagLocation = KeywordLoc; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 939 | |
| 940 | // FIXME: type might be anonymous. |
| 941 | IdentifierInfo *Id = TD->getIdentifier(); |
| 942 | |
| 943 | // TODO: should we even warn on struct/class mismatches for this? Seems |
| 944 | // like it's likely to produce a lot of spurious errors. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 945 | if (Keyword != ETK_None && Keyword != ETK_Typename) { |
| 946 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 947 | if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, /*isDefinition*/false, |
| 948 | TagLocation, *Id)) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 949 | SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) |
| 950 | << Id |
| 951 | << FixItHint::CreateReplacement(SourceRange(TagLocation), |
| 952 | TD->getKindName()); |
| 953 | SemaRef.Diag(TD->getLocation(), diag::note_previous_use); |
| 954 | } |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
John McCall | 954b5de | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 958 | return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc, |
| 959 | Keyword, |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 960 | QualifierLoc, |
| 961 | T); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 964 | TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS, |
| 965 | TemplateName Name, |
| 966 | SourceLocation NameLoc, |
| 967 | QualType ObjectType, |
| 968 | NamedDecl *FirstQualifierInScope) { |
| 969 | if (TemplateTemplateParmDecl *TTP |
| 970 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) { |
| 971 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 972 | // If the corresponding template argument is NULL or non-existent, it's |
| 973 | // because we are performing instantiation from explicitly-specified |
| 974 | // template arguments in a function template, but there were some |
| 975 | // arguments left unspecified. |
| 976 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
| 977 | TTP->getPosition())) |
| 978 | return Name; |
| 979 | |
| 980 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
| 981 | |
| 982 | if (TTP->isParameterPack()) { |
| 983 | assert(Arg.getKind() == TemplateArgument::Pack && |
| 984 | "Missing argument pack"); |
| 985 | |
| 986 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 987 | // We have the template argument pack to substitute, but we're not |
| 988 | // actually expanding the enclosing pack expansion yet. So, just |
| 989 | // keep the entire argument pack. |
| 990 | return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg); |
| 991 | } |
| 992 | |
| 993 | assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); |
| 994 | Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; |
| 995 | } |
| 996 | |
| 997 | TemplateName Template = Arg.getAsTemplate(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 998 | assert(!Template.isNull() && "Null template template argument"); |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 999 | |
Douglas Gregor | 9d9f8db | 2011-03-05 20:06:51 +0000 | [diff] [blame] | 1000 | // We don't ever want to substitute for a qualified template name, since |
| 1001 | // the qualifier is handled separately. So, look through the qualified |
| 1002 | // template name to its underlying declaration. |
| 1003 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 1004 | Template = TemplateName(QTN->getTemplateDecl()); |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1005 | |
| 1006 | Template = getSema().Context.getSubstTemplateTemplateParm(TTP, Template); |
Douglas Gregor | 9db5350 | 2011-03-02 18:07:45 +0000 | [diff] [blame] | 1007 | return Template; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | if (SubstTemplateTemplateParmPackStorage *SubstPack |
| 1012 | = Name.getAsSubstTemplateTemplateParmPack()) { |
| 1013 | if (getSema().ArgumentPackSubstitutionIndex == -1) |
| 1014 | return Name; |
| 1015 | |
| 1016 | const TemplateArgument &ArgPack = SubstPack->getArgumentPack(); |
| 1017 | assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() && |
| 1018 | "Pack substitution index out-of-range"); |
| 1019 | return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex] |
| 1020 | .getAsTemplate(); |
| 1021 | } |
| 1022 | |
| 1023 | return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType, |
| 1024 | FirstQualifierInScope); |
| 1025 | } |
| 1026 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1027 | ExprResult |
John McCall | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 1028 | TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1029 | if (!E->isTypeDependent()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 1030 | return SemaRef.Owned(E); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1031 | |
| 1032 | FunctionDecl *currentDecl = getSema().getCurFunctionDecl(); |
| 1033 | assert(currentDecl && "Must have current function declaration when " |
| 1034 | "instantiating."); |
| 1035 | |
| 1036 | PredefinedExpr::IdentType IT = E->getIdentType(); |
| 1037 | |
Anders Carlsson | 5bd8d19 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 1038 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1039 | |
| 1040 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1041 | QualType ResTy = getSema().Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1042 | ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI, |
| 1043 | ArrayType::Normal, 0); |
| 1044 | PredefinedExpr *PE = |
| 1045 | new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT); |
| 1046 | return getSema().Owned(PE); |
| 1047 | } |
| 1048 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1049 | ExprResult |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1050 | TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E, |
Douglas Gregor | 6c379e2 | 2010-02-08 23:41:45 +0000 | [diff] [blame] | 1051 | NonTypeTemplateParmDecl *NTTP) { |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1052 | // If the corresponding template argument is NULL or non-existent, it's |
| 1053 | // because we are performing instantiation from explicitly-specified |
| 1054 | // template arguments in a function template, but there were some |
| 1055 | // arguments left unspecified. |
| 1056 | if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
| 1057 | NTTP->getPosition())) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 1058 | return SemaRef.Owned(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 1060 | TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition()); |
| 1061 | if (NTTP->isParameterPack()) { |
| 1062 | assert(Arg.getKind() == TemplateArgument::Pack && |
| 1063 | "Missing argument pack"); |
| 1064 | |
| 1065 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 1066 | // We have an argument pack, but we can't select a particular argument |
| 1067 | // out of it yet. Therefore, we'll build an expression to hold on to that |
| 1068 | // argument pack. |
| 1069 | QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs, |
| 1070 | E->getLocation(), |
| 1071 | NTTP->getDeclName()); |
| 1072 | if (TargetType.isNull()) |
| 1073 | return ExprError(); |
| 1074 | |
| 1075 | return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType, |
| 1076 | NTTP, |
| 1077 | E->getLocation(), |
| 1078 | Arg); |
Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 1081 | assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); |
Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 1082 | Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; |
| 1083 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1085 | return transformNonTypeTemplateParmRef(NTTP, E->getLocation(), Arg); |
| 1086 | } |
| 1087 | |
| 1088 | ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef( |
| 1089 | NonTypeTemplateParmDecl *parm, |
| 1090 | SourceLocation loc, |
| 1091 | const TemplateArgument &arg) { |
| 1092 | ExprResult result; |
| 1093 | QualType type; |
| 1094 | |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1095 | // The template argument itself might be an expression, in which |
| 1096 | // case we just return that expression. |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1097 | if (arg.getKind() == TemplateArgument::Expression) { |
| 1098 | Expr *argExpr = arg.getAsExpr(); |
| 1099 | result = SemaRef.Owned(argExpr); |
| 1100 | type = argExpr->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1102 | } else if (arg.getKind() == TemplateArgument::Declaration) { |
| 1103 | ValueDecl *VD = cast<ValueDecl>(arg.getAsDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | |
John McCall | 15dda37 | 2010-02-06 10:23:53 +0000 | [diff] [blame] | 1105 | // Find the instantiation of the template argument. This is |
| 1106 | // required for nested templates. |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1107 | VD = cast_or_null<ValueDecl>( |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1108 | getSema().FindInstantiatedDecl(loc, VD, TemplateArgs)); |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1109 | if (!VD) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1110 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
John McCall | 15dda37 | 2010-02-06 10:23:53 +0000 | [diff] [blame] | 1112 | // Derive the type we want the substituted decl to have. This had |
| 1113 | // better be non-dependent, or these checks will have serious problems. |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1114 | if (parm->isExpandedParameterPack()) { |
| 1115 | type = parm->getExpansionType(SemaRef.ArgumentPackSubstitutionIndex); |
| 1116 | } else if (parm->isParameterPack() && |
| 1117 | isa<PackExpansionType>(parm->getType())) { |
| 1118 | type = SemaRef.SubstType( |
| 1119 | cast<PackExpansionType>(parm->getType())->getPattern(), |
| 1120 | TemplateArgs, loc, parm->getDeclName()); |
| 1121 | } else { |
| 1122 | type = SemaRef.SubstType(parm->getType(), TemplateArgs, |
| 1123 | loc, parm->getDeclName()); |
| 1124 | } |
| 1125 | assert(!type.isNull() && "type substitution failed for param type"); |
| 1126 | assert(!type->isDependentType() && "param type still dependent"); |
| 1127 | result = SemaRef.BuildExpressionFromDeclTemplateArgument(arg, type, loc); |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1128 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1129 | if (!result.isInvalid()) type = result.get()->getType(); |
| 1130 | } else { |
| 1131 | result = SemaRef.BuildExpressionFromIntegralTemplateArgument(arg, loc); |
| 1132 | |
| 1133 | // Note that this type can be different from the type of 'result', |
| 1134 | // e.g. if it's an enum type. |
| 1135 | type = arg.getIntegralType(); |
| 1136 | } |
| 1137 | if (result.isInvalid()) return ExprError(); |
| 1138 | |
| 1139 | Expr *resultExpr = result.take(); |
| 1140 | return SemaRef.Owned(new (SemaRef.Context) |
| 1141 | SubstNonTypeTemplateParmExpr(type, |
| 1142 | resultExpr->getValueKind(), |
| 1143 | loc, parm, resultExpr)); |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 1146 | ExprResult |
| 1147 | TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr( |
| 1148 | SubstNonTypeTemplateParmPackExpr *E) { |
| 1149 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 1150 | // We aren't expanding the parameter pack, so just return ourselves. |
| 1151 | return getSema().Owned(E); |
| 1152 | } |
| 1153 | |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 1154 | const TemplateArgument &ArgPack = E->getArgumentPack(); |
| 1155 | unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex; |
| 1156 | assert(Index < ArgPack.pack_size() && "Substitution index out-of-range"); |
| 1157 | |
| 1158 | const TemplateArgument &Arg = ArgPack.pack_begin()[Index]; |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1159 | return transformNonTypeTemplateParmRef(E->getParameterPack(), |
| 1160 | E->getParameterPackLocation(), |
| 1161 | Arg); |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 1162 | } |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1163 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1164 | ExprResult |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 1165 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
| 1166 | NamedDecl *D = E->getDecl(); |
| 1167 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
| 1168 | if (NTTP->getDepth() < TemplateArgs.getNumLevels()) |
| 1169 | return TransformTemplateParmRefExpr(E, NTTP); |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1170 | |
| 1171 | // We have a non-type template parameter that isn't fully substituted; |
| 1172 | // FindInstantiatedDecl will find it in the local instantiation scope. |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1173 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | |
John McCall | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 1175 | return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E); |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1178 | ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr( |
John McCall | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 1179 | CXXDefaultArgExpr *E) { |
Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 1180 | assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())-> |
| 1181 | getDescribedFunctionTemplate() && |
| 1182 | "Default arg expressions are never formed in dependent cases."); |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 1183 | return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(), |
| 1184 | cast<FunctionDecl>(E->getParam()->getDeclContext()), |
| 1185 | E->getParam()); |
Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1188 | QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB, |
John McCall | 31f8272 | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 1189 | FunctionProtoTypeLoc TL) { |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1190 | // We need a local instantiation scope for this function prototype. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1191 | LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true); |
John McCall | 31f8272 | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 1192 | return inherited::TransformFunctionProtoType(TLB, TL); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | ParmVarDecl * |
Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 1196 | TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm, |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1197 | int indexAdjustment, |
Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 1198 | llvm::Optional<unsigned> NumExpansions) { |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1199 | return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment, |
Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 1200 | NumExpansions); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | QualType |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1204 | TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
John McCall | 31f8272 | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 1205 | TemplateTypeParmTypeLoc TL) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1206 | const TemplateTypeParmType *T = TL.getTypePtr(); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1207 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1208 | // Replace the template type parameter with its corresponding |
| 1209 | // template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | |
| 1211 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 1212 | // because we are performing instantiation from explicitly-specified |
| 1213 | // template arguments in a function template class, but there were some |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1214 | // arguments left unspecified. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1215 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
| 1216 | TemplateTypeParmTypeLoc NewTL |
| 1217 | = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
| 1218 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1219 | return TL.getType(); |
| 1220 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1222 | TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex()); |
| 1223 | |
| 1224 | if (T->isParameterPack()) { |
| 1225 | assert(Arg.getKind() == TemplateArgument::Pack && |
| 1226 | "Missing argument pack"); |
| 1227 | |
| 1228 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 1229 | // We have the template argument pack, but we're not expanding the |
| 1230 | // enclosing pack expansion yet. Just save the template argument |
| 1231 | // pack for later substitution. |
| 1232 | QualType Result |
| 1233 | = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg); |
| 1234 | SubstTemplateTypeParmPackTypeLoc NewTL |
| 1235 | = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result); |
| 1236 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1237 | return Result; |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 1240 | assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1241 | Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; |
| 1242 | } |
| 1243 | |
| 1244 | assert(Arg.getKind() == TemplateArgument::Type && |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1245 | "Template argument kind mismatch"); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1246 | |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 1247 | QualType Replacement = Arg.getAsType(); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1248 | |
| 1249 | // TODO: only do this uniquing once, at the start of instantiation. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1250 | QualType Result |
| 1251 | = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); |
| 1252 | SubstTemplateTypeParmTypeLoc NewTL |
| 1253 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 1254 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1255 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | } |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1257 | |
| 1258 | // The template type parameter comes from an inner template (e.g., |
| 1259 | // the template parameter list of a member template inside the |
| 1260 | // template we are instantiating). Create a new template type |
| 1261 | // parameter with the template "level" reduced by one. |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1262 | TemplateTypeParmDecl *NewTTPDecl = 0; |
| 1263 | if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl()) |
| 1264 | NewTTPDecl = cast_or_null<TemplateTypeParmDecl>( |
| 1265 | TransformDecl(TL.getNameLoc(), OldTTPDecl)); |
| 1266 | |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1267 | QualType Result |
| 1268 | = getSema().Context.getTemplateTypeParmType(T->getDepth() |
| 1269 | - TemplateArgs.getNumLevels(), |
| 1270 | T->getIndex(), |
| 1271 | T->isParameterPack(), |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1272 | NewTTPDecl); |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1273 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 1274 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1275 | return Result; |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1276 | } |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1277 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 1278 | QualType |
| 1279 | TemplateInstantiator::TransformSubstTemplateTypeParmPackType( |
| 1280 | TypeLocBuilder &TLB, |
| 1281 | SubstTemplateTypeParmPackTypeLoc TL) { |
| 1282 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 1283 | // We aren't expanding the parameter pack, so just return ourselves. |
| 1284 | SubstTemplateTypeParmPackTypeLoc NewTL |
| 1285 | = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType()); |
| 1286 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1287 | return TL.getType(); |
| 1288 | } |
| 1289 | |
| 1290 | const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack(); |
| 1291 | unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex; |
| 1292 | assert(Index < ArgPack.pack_size() && "Substitution index out-of-range"); |
| 1293 | |
| 1294 | QualType Result = ArgPack.pack_begin()[Index].getAsType(); |
| 1295 | Result = getSema().Context.getSubstTemplateTypeParmType( |
| 1296 | TL.getTypePtr()->getReplacedParameter(), |
| 1297 | Result); |
| 1298 | SubstTemplateTypeParmTypeLoc NewTL |
| 1299 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 1300 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1301 | return Result; |
| 1302 | } |
| 1303 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1304 | /// \brief Perform substitution on the type T with a given set of template |
| 1305 | /// arguments. |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1306 | /// |
| 1307 | /// This routine substitutes the given template arguments into the |
| 1308 | /// type T and produces the instantiated type. |
| 1309 | /// |
| 1310 | /// \param T the type into which the template arguments will be |
| 1311 | /// substituted. If this type is not dependent, it will be returned |
| 1312 | /// immediately. |
| 1313 | /// |
| 1314 | /// \param TemplateArgs the template arguments that will be |
| 1315 | /// substituted for the top-level template parameters within T. |
| 1316 | /// |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1317 | /// \param Loc the location in the source code where this substitution |
| 1318 | /// is being performed. It will typically be the location of the |
| 1319 | /// declarator (if we're instantiating the type of some declaration) |
| 1320 | /// or the location of the type in the source code (if, e.g., we're |
| 1321 | /// instantiating the type of a cast expression). |
| 1322 | /// |
| 1323 | /// \param Entity the name of the entity associated with a declaration |
| 1324 | /// being instantiated (if any). May be empty to indicate that there |
| 1325 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 1326 | /// a cast expression) or that the entity has no name (e.g., an |
| 1327 | /// unnamed function parameter). |
| 1328 | /// |
| 1329 | /// \returns If the instantiation succeeds, the instantiated |
| 1330 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1331 | TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T, |
John McCall | 609459e | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 1332 | const MultiLevelTemplateArgumentList &Args, |
| 1333 | SourceLocation Loc, |
| 1334 | DeclarationName Entity) { |
| 1335 | assert(!ActiveTemplateInstantiations.empty() && |
| 1336 | "Cannot perform an instantiation without some context on the " |
| 1337 | "instantiation stack"); |
| 1338 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1339 | if (!T->getType()->isInstantiationDependentType() && |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 1340 | !T->getType()->isVariablyModifiedType()) |
John McCall | 609459e | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 1341 | return T; |
| 1342 | |
| 1343 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 1344 | return Instantiator.TransformType(T); |
| 1345 | } |
| 1346 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1347 | TypeSourceInfo *Sema::SubstType(TypeLoc TL, |
| 1348 | const MultiLevelTemplateArgumentList &Args, |
| 1349 | SourceLocation Loc, |
| 1350 | DeclarationName Entity) { |
| 1351 | assert(!ActiveTemplateInstantiations.empty() && |
| 1352 | "Cannot perform an instantiation without some context on the " |
| 1353 | "instantiation stack"); |
| 1354 | |
| 1355 | if (TL.getType().isNull()) |
| 1356 | return 0; |
| 1357 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1358 | if (!TL.getType()->isInstantiationDependentType() && |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1359 | !TL.getType()->isVariablyModifiedType()) { |
| 1360 | // FIXME: Make a copy of the TypeLoc data here, so that we can |
| 1361 | // return a new TypeSourceInfo. Inefficient! |
| 1362 | TypeLocBuilder TLB; |
| 1363 | TLB.pushFullCopy(TL); |
| 1364 | return TLB.getTypeSourceInfo(Context, TL.getType()); |
| 1365 | } |
| 1366 | |
| 1367 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 1368 | TypeLocBuilder TLB; |
| 1369 | TLB.reserve(TL.getFullDataSize()); |
| 1370 | QualType Result = Instantiator.TransformType(TLB, TL); |
| 1371 | if (Result.isNull()) |
| 1372 | return 0; |
| 1373 | |
| 1374 | return TLB.getTypeSourceInfo(Context, Result); |
| 1375 | } |
| 1376 | |
John McCall | 609459e | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 1377 | /// Deprecated form of the above. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | QualType Sema::SubstType(QualType T, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1379 | const MultiLevelTemplateArgumentList &TemplateArgs, |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1380 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1381 | assert(!ActiveTemplateInstantiations.empty() && |
| 1382 | "Cannot perform an instantiation without some context on the " |
| 1383 | "instantiation stack"); |
| 1384 | |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 1385 | // If T is not a dependent type or a variably-modified type, there |
| 1386 | // is nothing to do. |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1387 | if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType()) |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1388 | return T; |
| 1389 | |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 1390 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 1391 | return Instantiator.TransformType(T); |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1392 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1393 | |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1394 | static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) { |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1395 | if (T->getType()->isInstantiationDependentType() || |
| 1396 | T->getType()->isVariablyModifiedType()) |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1397 | return true; |
| 1398 | |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1399 | TypeLoc TL = T->getTypeLoc().IgnoreParens(); |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1400 | if (!isa<FunctionProtoTypeLoc>(TL)) |
| 1401 | return false; |
| 1402 | |
| 1403 | FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL); |
| 1404 | for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) { |
| 1405 | ParmVarDecl *P = FP.getArg(I); |
| 1406 | |
Douglas Gregor | a7203e5 | 2011-05-09 20:45:16 +0000 | [diff] [blame] | 1407 | // The parameter's type as written might be dependent even if the |
| 1408 | // decayed type was not dependent. |
| 1409 | if (TypeSourceInfo *TSInfo = P->getTypeSourceInfo()) |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1410 | if (TSInfo->getType()->isInstantiationDependentType()) |
Douglas Gregor | a7203e5 | 2011-05-09 20:45:16 +0000 | [diff] [blame] | 1411 | return true; |
| 1412 | |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1413 | // TODO: currently we always rebuild expressions. When we |
| 1414 | // properly get lazier about this, we should use the same |
| 1415 | // logic to avoid rebuilding prototypes here. |
Douglas Gregor | 9cc27822 | 2011-01-05 21:14:17 +0000 | [diff] [blame] | 1416 | if (P->hasDefaultArg()) |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1417 | return true; |
| 1418 | } |
| 1419 | |
| 1420 | return false; |
| 1421 | } |
| 1422 | |
| 1423 | /// A form of SubstType intended specifically for instantiating the |
| 1424 | /// type of a FunctionDecl. Its purpose is solely to force the |
| 1425 | /// instantiation of default-argument expressions. |
| 1426 | TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T, |
| 1427 | const MultiLevelTemplateArgumentList &Args, |
| 1428 | SourceLocation Loc, |
| 1429 | DeclarationName Entity) { |
| 1430 | assert(!ActiveTemplateInstantiations.empty() && |
| 1431 | "Cannot perform an instantiation without some context on the " |
| 1432 | "instantiation stack"); |
| 1433 | |
| 1434 | if (!NeedsInstantiationAsFunctionType(T)) |
| 1435 | return T; |
| 1436 | |
| 1437 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 1438 | |
| 1439 | TypeLocBuilder TLB; |
| 1440 | |
| 1441 | TypeLoc TL = T->getTypeLoc(); |
| 1442 | TLB.reserve(TL.getFullDataSize()); |
| 1443 | |
John McCall | 31f8272 | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 1444 | QualType Result = Instantiator.TransformType(TLB, TL); |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1445 | if (Result.isNull()) |
| 1446 | return 0; |
| 1447 | |
| 1448 | return TLB.getTypeSourceInfo(Context, Result); |
| 1449 | } |
| 1450 | |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1451 | ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm, |
Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 1452 | const MultiLevelTemplateArgumentList &TemplateArgs, |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1453 | int indexAdjustment, |
Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 1454 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1455 | TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo(); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1456 | TypeSourceInfo *NewDI = 0; |
| 1457 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1458 | TypeLoc OldTL = OldDI->getTypeLoc(); |
| 1459 | if (isa<PackExpansionTypeLoc>(OldTL)) { |
| 1460 | PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1461 | |
| 1462 | // We have a function parameter pack. Substitute into the pattern of the |
| 1463 | // expansion. |
| 1464 | NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs, |
| 1465 | OldParm->getLocation(), OldParm->getDeclName()); |
| 1466 | if (!NewDI) |
| 1467 | return 0; |
| 1468 | |
| 1469 | if (NewDI->getType()->containsUnexpandedParameterPack()) { |
| 1470 | // We still have unexpanded parameter packs, which means that |
| 1471 | // our function parameter is still a function parameter pack. |
| 1472 | // Therefore, make its type a pack expansion type. |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 1473 | NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(), |
Douglas Gregor | 715e461 | 2011-01-14 22:40:04 +0000 | [diff] [blame] | 1474 | NumExpansions); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } else { |
| 1477 | NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(), |
| 1478 | OldParm->getDeclName()); |
| 1479 | } |
| 1480 | |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1481 | if (!NewDI) |
| 1482 | return 0; |
| 1483 | |
| 1484 | if (NewDI->getType()->isVoidType()) { |
| 1485 | Diag(OldParm->getLocation(), diag::err_param_with_void_type); |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
| 1489 | ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1490 | OldParm->getInnerLocStart(), |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1491 | OldParm->getLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1492 | OldParm->getIdentifier(), |
| 1493 | NewDI->getType(), NewDI, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1494 | OldParm->getStorageClass(), |
| 1495 | OldParm->getStorageClassAsWritten()); |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1496 | if (!NewParm) |
| 1497 | return 0; |
Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 1498 | |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1499 | // Mark the (new) default argument as uninstantiated (if any). |
| 1500 | if (OldParm->hasUninstantiatedDefaultArg()) { |
| 1501 | Expr *Arg = OldParm->getUninstantiatedDefaultArg(); |
| 1502 | NewParm->setUninstantiatedDefaultArg(Arg); |
Douglas Gregor | 758cb67 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 1503 | } else if (OldParm->hasUnparsedDefaultArg()) { |
| 1504 | NewParm->setUnparsedDefaultArg(); |
| 1505 | UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm); |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1506 | } else if (Expr *Arg = OldParm->getDefaultArg()) |
| 1507 | NewParm->setUninstantiatedDefaultArg(Arg); |
| 1508 | |
| 1509 | NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg()); |
| 1510 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1511 | // FIXME: When OldParm is a parameter pack and NewParm is not a parameter |
| 1512 | // pack, we actually have a set of instantiated locations. Maintain this set! |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1513 | if (OldParm->isParameterPack() && !NewParm->isParameterPack()) { |
| 1514 | // Add the new parameter to |
| 1515 | CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm); |
| 1516 | } else { |
| 1517 | // Introduce an Old -> New mapping |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1518 | CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1519 | } |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1520 | |
Argyrios Kyrtzidis | 3816ed4 | 2010-07-19 10:14:41 +0000 | [diff] [blame] | 1521 | // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext |
| 1522 | // can be anything, is this right ? |
Fariborz Jahanian | 714447b | 2010-07-13 21:05:02 +0000 | [diff] [blame] | 1523 | NewParm->setDeclContext(CurContext); |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1524 | |
| 1525 | NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(), |
| 1526 | OldParm->getFunctionScopeIndex() + indexAdjustment); |
Fariborz Jahanian | a6c7efe | 2010-07-13 20:05:58 +0000 | [diff] [blame] | 1527 | |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1528 | return NewParm; |
| 1529 | } |
| 1530 | |
Douglas Gregor | dd47216 | 2011-01-07 00:20:55 +0000 | [diff] [blame] | 1531 | /// \brief Substitute the given template arguments into the given set of |
| 1532 | /// parameters, producing the set of parameter types that would be generated |
| 1533 | /// from such a substitution. |
| 1534 | bool Sema::SubstParmTypes(SourceLocation Loc, |
| 1535 | ParmVarDecl **Params, unsigned NumParams, |
| 1536 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 1537 | SmallVectorImpl<QualType> &ParamTypes, |
| 1538 | SmallVectorImpl<ParmVarDecl *> *OutParams) { |
Douglas Gregor | dd47216 | 2011-01-07 00:20:55 +0000 | [diff] [blame] | 1539 | assert(!ActiveTemplateInstantiations.empty() && |
| 1540 | "Cannot perform an instantiation without some context on the " |
| 1541 | "instantiation stack"); |
| 1542 | |
| 1543 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 1544 | DeclarationName()); |
| 1545 | return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0, |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1546 | ParamTypes, OutParams); |
Douglas Gregor | dd47216 | 2011-01-07 00:20:55 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1549 | /// \brief Perform substitution on the base class specifiers of the |
| 1550 | /// given class template specialization. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1551 | /// |
| 1552 | /// Produces a diagnostic and returns true on error, returns false and |
| 1553 | /// attaches the instantiated base classes to the class template |
| 1554 | /// specialization if successful. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | bool |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1556 | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 1557 | CXXRecordDecl *Pattern, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1558 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1559 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 1560 | SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | for (ClassTemplateSpecializationDecl::base_class_iterator |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1562 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | 2a72edd | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 1563 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1564 | if (!Base->getType()->isDependentType()) { |
Fariborz Jahanian | 5c14ec3 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 1565 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1566 | continue; |
| 1567 | } |
| 1568 | |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1569 | SourceLocation EllipsisLoc; |
Douglas Gregor | c52264e | 2011-03-02 02:04:06 +0000 | [diff] [blame] | 1570 | TypeSourceInfo *BaseTypeLoc; |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1571 | if (Base->isPackExpansion()) { |
| 1572 | // This is a pack expansion. See whether we should expand it now, or |
| 1573 | // wait until later. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 1574 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1575 | collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(), |
| 1576 | Unexpanded); |
| 1577 | bool ShouldExpand = false; |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 1578 | bool RetainExpansion = false; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 1579 | llvm::Optional<unsigned> NumExpansions; |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1580 | if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(), |
| 1581 | Base->getSourceRange(), |
| 1582 | Unexpanded.data(), Unexpanded.size(), |
| 1583 | TemplateArgs, ShouldExpand, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 1584 | RetainExpansion, |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1585 | NumExpansions)) { |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1586 | Invalid = true; |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1587 | continue; |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | // If we should expand this pack expansion now, do so. |
| 1591 | if (ShouldExpand) { |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 1592 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1593 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 1594 | |
| 1595 | TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(), |
| 1596 | TemplateArgs, |
| 1597 | Base->getSourceRange().getBegin(), |
| 1598 | DeclarationName()); |
| 1599 | if (!BaseTypeLoc) { |
| 1600 | Invalid = true; |
| 1601 | continue; |
| 1602 | } |
| 1603 | |
| 1604 | if (CXXBaseSpecifier *InstantiatedBase |
| 1605 | = CheckBaseSpecifier(Instantiation, |
| 1606 | Base->getSourceRange(), |
| 1607 | Base->isVirtual(), |
| 1608 | Base->getAccessSpecifierAsWritten(), |
| 1609 | BaseTypeLoc, |
| 1610 | SourceLocation())) |
| 1611 | InstantiatedBases.push_back(InstantiatedBase); |
| 1612 | else |
| 1613 | Invalid = true; |
| 1614 | } |
| 1615 | |
| 1616 | continue; |
| 1617 | } |
| 1618 | |
| 1619 | // The resulting base specifier will (still) be a pack expansion. |
| 1620 | EllipsisLoc = Base->getEllipsisLoc(); |
Douglas Gregor | c52264e | 2011-03-02 02:04:06 +0000 | [diff] [blame] | 1621 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
| 1622 | BaseTypeLoc = SubstType(Base->getTypeSourceInfo(), |
| 1623 | TemplateArgs, |
| 1624 | Base->getSourceRange().getBegin(), |
| 1625 | DeclarationName()); |
| 1626 | } else { |
| 1627 | BaseTypeLoc = SubstType(Base->getTypeSourceInfo(), |
| 1628 | TemplateArgs, |
| 1629 | Base->getSourceRange().getBegin(), |
| 1630 | DeclarationName()); |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1633 | if (!BaseTypeLoc) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1634 | Invalid = true; |
| 1635 | continue; |
| 1636 | } |
| 1637 | |
| 1638 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1639 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1640 | Base->getSourceRange(), |
| 1641 | Base->isVirtual(), |
| 1642 | Base->getAccessSpecifierAsWritten(), |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1643 | BaseTypeLoc, |
| 1644 | EllipsisLoc)) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1645 | InstantiatedBases.push_back(InstantiatedBase); |
| 1646 | else |
| 1647 | Invalid = true; |
| 1648 | } |
| 1649 | |
Douglas Gregor | 2a72edd | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 1650 | if (!Invalid && |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1651 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1652 | InstantiatedBases.size())) |
| 1653 | Invalid = true; |
| 1654 | |
| 1655 | return Invalid; |
| 1656 | } |
| 1657 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1658 | /// \brief Instantiate the definition of a class from a given pattern. |
| 1659 | /// |
| 1660 | /// \param PointOfInstantiation The point of instantiation within the |
| 1661 | /// source code. |
| 1662 | /// |
| 1663 | /// \param Instantiation is the declaration whose definition is being |
| 1664 | /// instantiated. This will be either a class template specialization |
| 1665 | /// or a member class of a class template specialization. |
| 1666 | /// |
| 1667 | /// \param Pattern is the pattern from which the instantiation |
| 1668 | /// occurs. This will be either the declaration of a class template or |
| 1669 | /// the declaration of a member class of a class template. |
| 1670 | /// |
| 1671 | /// \param TemplateArgs The template arguments to be substituted into |
| 1672 | /// the pattern. |
| 1673 | /// |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1674 | /// \param TSK the kind of implicit or explicit instantiation to perform. |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1675 | /// |
| 1676 | /// \param Complain whether to complain if the class cannot be instantiated due |
| 1677 | /// to the lack of a definition. |
| 1678 | /// |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1679 | /// \returns true if an error occurred, false otherwise. |
| 1680 | bool |
| 1681 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 1682 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1683 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1684 | TemplateSpecializationKind TSK, |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1685 | bool Complain) { |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1686 | bool Invalid = false; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1687 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | CXXRecordDecl *PatternDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1689 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
John McCall | 5476666 | 2011-04-27 06:46:31 +0000 | [diff] [blame] | 1690 | if (!PatternDef || PatternDef->isBeingDefined()) { |
| 1691 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1692 | // Say nothing |
John McCall | 5476666 | 2011-04-27 06:46:31 +0000 | [diff] [blame] | 1693 | } else if (PatternDef) { |
| 1694 | assert(PatternDef->isBeingDefined()); |
| 1695 | Diag(PointOfInstantiation, |
| 1696 | diag::err_template_instantiate_within_definition) |
| 1697 | << (TSK != TSK_ImplicitInstantiation) |
| 1698 | << Context.getTypeDeclType(Instantiation); |
| 1699 | // Not much point in noting the template declaration here, since |
| 1700 | // we're lexically inside it. |
| 1701 | Instantiation->setInvalidDecl(); |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1702 | } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1703 | Diag(PointOfInstantiation, |
| 1704 | diag::err_implicit_instantiate_member_undefined) |
| 1705 | << Context.getTypeDeclType(Instantiation); |
| 1706 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 1707 | } else { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 1708 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1709 | << (TSK != TSK_ImplicitInstantiation) |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1710 | << Context.getTypeDeclType(Instantiation); |
| 1711 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 1712 | } |
| 1713 | return true; |
| 1714 | } |
| 1715 | Pattern = PatternDef; |
| 1716 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 1717 | // \brief Record the point of instantiation. |
| 1718 | if (MemberSpecializationInfo *MSInfo |
| 1719 | = Instantiation->getMemberSpecializationInfo()) { |
| 1720 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1721 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1722 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1723 | = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { |
| 1724 | Spec->setTemplateSpecializationKind(TSK); |
| 1725 | Spec->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Douglas Gregor | f3430ae | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 1728 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1729 | if (Inst) |
| 1730 | return true; |
| 1731 | |
| 1732 | // Enter the scope of this instantiation. We don't use |
| 1733 | // PushDeclContext because we don't have a scope. |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 1734 | ContextRAII SavedContext(*this, Instantiation); |
Douglas Gregor | 1715842 | 2010-05-12 17:27:19 +0000 | [diff] [blame] | 1735 | EnterExpressionEvaluationContext EvalContext(*this, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1736 | Sema::PotentiallyEvaluated); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1737 | |
Douglas Gregor | 5112157 | 2010-03-24 01:33:17 +0000 | [diff] [blame] | 1738 | // If this is an instantiation of a local class, merge this local |
| 1739 | // instantiation scope with the enclosing scope. Otherwise, every |
| 1740 | // instantiation of a class has its own local instantiation scope. |
| 1741 | bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod(); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1742 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Douglas Gregor | 5112157 | 2010-03-24 01:33:17 +0000 | [diff] [blame] | 1743 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 1744 | // Pull attributes from the pattern onto the instantiation. |
| 1745 | InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
| 1746 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1747 | // Start the definition of this instantiation. |
| 1748 | Instantiation->startDefinition(); |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1749 | |
| 1750 | Instantiation->setTagKind(Pattern->getTagKind()); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1751 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1752 | // Do substitution on the base class specifiers. |
| 1753 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1754 | Invalid = true; |
| 1755 | |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1756 | TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 1757 | SmallVector<Decl*, 4> Fields; |
| 1758 | SmallVector<std::pair<FieldDecl*, FieldDecl*>, 4> |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1759 | FieldsWithMemberInitializers; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1760 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | MemberEnd = Pattern->decls_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1762 | Member != MemberEnd; ++Member) { |
Argyrios Kyrtzidis | 9a94d9b | 2010-11-04 03:18:57 +0000 | [diff] [blame] | 1763 | // Don't instantiate members not belonging in this semantic context. |
| 1764 | // e.g. for: |
| 1765 | // @code |
| 1766 | // template <int i> class A { |
| 1767 | // class B *g; |
| 1768 | // }; |
| 1769 | // @endcode |
| 1770 | // 'class B' has the template as lexical context but semantically it is |
| 1771 | // introduced in namespace scope. |
| 1772 | if ((*Member)->getDeclContext() != Pattern) |
| 1773 | continue; |
| 1774 | |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1775 | if ((*Member)->isInvalidDecl()) { |
| 1776 | Invalid = true; |
| 1777 | continue; |
| 1778 | } |
| 1779 | |
| 1780 | Decl *NewMember = Instantiator.Visit(*Member); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1781 | if (NewMember) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1782 | if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1783 | Fields.push_back(Field); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1784 | FieldDecl *OldField = cast<FieldDecl>(*Member); |
| 1785 | if (OldField->getInClassInitializer()) |
| 1786 | FieldsWithMemberInitializers.push_back(std::make_pair(OldField, |
| 1787 | Field)); |
| 1788 | } else if (NewMember->isInvalidDecl()) |
Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 1789 | Invalid = true; |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1790 | } else { |
| 1791 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1792 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 1793 | // true. For now, we expect to skip some members that we can't yet handle. |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | // Finish checking fields. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1798 | ActOnFields(0, Instantiation->getLocation(), Instantiation, |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1799 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1800 | 0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1801 | CheckCompletedCXXClass(Instantiation); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1802 | |
| 1803 | // Attach any in-class member initializers now the class is complete. |
| 1804 | for (unsigned I = 0, N = FieldsWithMemberInitializers.size(); I != N; ++I) { |
| 1805 | FieldDecl *OldField = FieldsWithMemberInitializers[I].first; |
| 1806 | FieldDecl *NewField = FieldsWithMemberInitializers[I].second; |
| 1807 | Expr *OldInit = OldField->getInClassInitializer(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1808 | |
Richard Smith | e3daab2 | 2011-07-20 00:12:52 +0000 | [diff] [blame] | 1809 | SourceLocation LParenLoc, RParenLoc; |
| 1810 | ASTOwningVector<Expr*> NewArgs(*this); |
| 1811 | if (InstantiateInitializer(OldInit, TemplateArgs, LParenLoc, NewArgs, |
| 1812 | RParenLoc)) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1813 | NewField->setInvalidDecl(); |
Richard Smith | e3daab2 | 2011-07-20 00:12:52 +0000 | [diff] [blame] | 1814 | else { |
| 1815 | assert(NewArgs.size() == 1 && "wrong number of in-class initializers"); |
| 1816 | ActOnCXXInClassMemberInitializer(NewField, LParenLoc, NewArgs[0]); |
| 1817 | } |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | if (!FieldsWithMemberInitializers.empty()) |
| 1821 | ActOnFinishDelayedMemberInitializers(Instantiation); |
| 1822 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1823 | if (Instantiation->isInvalidDecl()) |
| 1824 | Invalid = true; |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1825 | else { |
| 1826 | // Instantiate any out-of-line class template partial |
| 1827 | // specializations now. |
| 1828 | for (TemplateDeclInstantiator::delayed_partial_spec_iterator |
| 1829 | P = Instantiator.delayed_partial_spec_begin(), |
| 1830 | PEnd = Instantiator.delayed_partial_spec_end(); |
| 1831 | P != PEnd; ++P) { |
| 1832 | if (!Instantiator.InstantiateClassTemplatePartialSpecialization( |
| 1833 | P->first, |
| 1834 | P->second)) { |
| 1835 | Invalid = true; |
| 1836 | break; |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1841 | // Exit the scope of this instantiation. |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 1842 | SavedContext.pop(); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1843 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1844 | if (!Invalid) { |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 1845 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 1846 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1847 | // Always emit the vtable for an explicit instantiation definition |
| 1848 | // of a polymorphic class template specialization. |
| 1849 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 1850 | MarkVTableUsed(PointOfInstantiation, Instantiation, true); |
| 1851 | } |
| 1852 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1853 | return Invalid; |
| 1854 | } |
| 1855 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1856 | namespace { |
| 1857 | /// \brief A partial specialization whose template arguments have matched |
| 1858 | /// a given template-id. |
| 1859 | struct PartialSpecMatchResult { |
| 1860 | ClassTemplatePartialSpecializationDecl *Partial; |
| 1861 | TemplateArgumentList *Args; |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1862 | }; |
| 1863 | } |
| 1864 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | bool |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1866 | Sema::InstantiateClassTemplateSpecialization( |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1867 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1868 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1869 | TemplateSpecializationKind TSK, |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1870 | bool Complain) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1871 | // Perform the actual instantiation on the canonical declaration. |
| 1872 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 1873 | ClassTemplateSpec->getCanonicalDecl()); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1874 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1875 | // Check whether we have already instantiated or specialized this class |
| 1876 | // template specialization. |
| 1877 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) { |
| 1878 | if (ClassTemplateSpec->getSpecializationKind() == |
| 1879 | TSK_ExplicitInstantiationDeclaration && |
| 1880 | TSK == TSK_ExplicitInstantiationDefinition) { |
| 1881 | // An explicit instantiation definition follows an explicit instantiation |
| 1882 | // declaration (C++0x [temp.explicit]p10); go ahead and perform the |
| 1883 | // explicit instantiation. |
| 1884 | ClassTemplateSpec->setSpecializationKind(TSK); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1885 | |
| 1886 | // If this is an explicit instantiation definition, mark the |
| 1887 | // vtable as used. |
| 1888 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 1889 | MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true); |
| 1890 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1891 | return false; |
| 1892 | } |
| 1893 | |
| 1894 | // We can only instantiate something that hasn't already been |
| 1895 | // instantiated or specialized. Fail without any diagnostics: our |
| 1896 | // caller will provide an error message. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1897 | return true; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1898 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1899 | |
Douglas Gregor | 00a511f | 2009-09-15 16:51:42 +0000 | [diff] [blame] | 1900 | if (ClassTemplateSpec->isInvalidDecl()) |
| 1901 | return true; |
| 1902 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1903 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1904 | CXXRecordDecl *Pattern = 0; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1905 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1906 | // C++ [temp.class.spec.match]p1: |
| 1907 | // When a class template is used in a context that requires an |
| 1908 | // instantiation of the class, it is necessary to determine |
| 1909 | // whether the instantiation is to be generated using the primary |
| 1910 | // template or one of the partial specializations. This is done by |
| 1911 | // matching the template arguments of the class template |
| 1912 | // specialization with the template argument lists of the partial |
| 1913 | // specializations. |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1914 | typedef PartialSpecMatchResult MatchResult; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 1915 | SmallVector<MatchResult, 4> Matched; |
| 1916 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
Douglas Gregor | 407e961 | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 1917 | Template->getPartialSpecializations(PartialSpecs); |
| 1918 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 1919 | ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1920 | TemplateDeductionInfo Info(Context, PointOfInstantiation); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1921 | if (TemplateDeductionResult Result |
Douglas Gregor | 407e961 | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 1922 | = DeduceTemplateArguments(Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1923 | ClassTemplateSpec->getTemplateArgs(), |
| 1924 | Info)) { |
| 1925 | // FIXME: Store the failed-deduction information for use in |
| 1926 | // diagnostics, later. |
| 1927 | (void)Result; |
| 1928 | } else { |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1929 | Matched.push_back(PartialSpecMatchResult()); |
| 1930 | Matched.back().Partial = Partial; |
| 1931 | Matched.back().Args = Info.take(); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1932 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1935 | // If we're dealing with a member template where the template parameters |
| 1936 | // have been instantiated, this provides the original template parameters |
| 1937 | // from which the member template's parameters were instantiated. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 1938 | SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1939 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1940 | if (Matched.size() >= 1) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 1941 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1942 | if (Matched.size() == 1) { |
| 1943 | // -- If exactly one matching specialization is found, the |
| 1944 | // instantiation is generated from that specialization. |
| 1945 | // We don't need to do anything for this. |
| 1946 | } else { |
| 1947 | // -- If more than one matching specialization is found, the |
| 1948 | // partial order rules (14.5.4.2) are used to determine |
| 1949 | // whether one of the specializations is more specialized |
| 1950 | // than the others. If none of the specializations is more |
| 1951 | // specialized than all of the other matching |
| 1952 | // specializations, then the use of the class template is |
| 1953 | // ambiguous and the program is ill-formed. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 1954 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1955 | PEnd = Matched.end(); |
| 1956 | P != PEnd; ++P) { |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1957 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1958 | PointOfInstantiation) |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1959 | == P->Partial) |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1960 | Best = P; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1961 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1962 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1963 | // Determine if the best partial specialization is more specialized than |
| 1964 | // the others. |
| 1965 | bool Ambiguous = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 1966 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1967 | PEnd = Matched.end(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1968 | P != PEnd; ++P) { |
| 1969 | if (P != Best && |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1970 | getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1971 | PointOfInstantiation) |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1972 | != Best->Partial) { |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1973 | Ambiguous = true; |
| 1974 | break; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | if (Ambiguous) { |
| 1979 | // Partial ordering did not produce a clear winner. Complain. |
| 1980 | ClassTemplateSpec->setInvalidDecl(); |
| 1981 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 1982 | << ClassTemplateSpec; |
| 1983 | |
| 1984 | // Print the matching partial specializations. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 1985 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1986 | PEnd = Matched.end(); |
| 1987 | P != PEnd; ++P) |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1988 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 1989 | << getTemplateArgumentBindingsText( |
| 1990 | P->Partial->getTemplateParameters(), |
| 1991 | *P->Args); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1992 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1993 | return true; |
| 1994 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | // Instantiate using the best class template partial specialization. |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1998 | ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1999 | while (OrigPartialSpec->getInstantiatedFromMember()) { |
| 2000 | // If we've found an explicit specialization of this class template, |
| 2001 | // stop here and use that as the pattern. |
| 2002 | if (OrigPartialSpec->isMemberSpecialization()) |
| 2003 | break; |
| 2004 | |
| 2005 | OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember(); |
| 2006 | } |
| 2007 | |
| 2008 | Pattern = OrigPartialSpec; |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2009 | ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args); |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2010 | } else { |
| 2011 | // -- If no matches are found, the instantiation is generated |
| 2012 | // from the primary template. |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2013 | ClassTemplateDecl *OrigTemplate = Template; |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 2014 | while (OrigTemplate->getInstantiatedFromMemberTemplate()) { |
| 2015 | // If we've found an explicit specialization of this class template, |
| 2016 | // stop here and use that as the pattern. |
| 2017 | if (OrigTemplate->isMemberSpecialization()) |
| 2018 | break; |
| 2019 | |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2020 | OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2023 | Pattern = OrigTemplate->getTemplatedDecl(); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2024 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 2026 | bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec, |
| 2027 | Pattern, |
| 2028 | getTemplateInstantiationArgs(ClassTemplateSpec), |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2029 | TSK, |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2030 | Complain); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2032 | return Result; |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2033 | } |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 2034 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2035 | /// \brief Instantiates the definitions of all of the member |
| 2036 | /// of the given class, which is an instantiation of a class template |
| 2037 | /// or a member class of a template. |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2038 | void |
| 2039 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2040 | CXXRecordDecl *Instantiation, |
| 2041 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 2042 | TemplateSpecializationKind TSK) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2043 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(), |
| 2044 | DEnd = Instantiation->decls_end(); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2045 | D != DEnd; ++D) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2046 | bool SuppressNew = false; |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2047 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2048 | if (FunctionDecl *Pattern |
| 2049 | = Function->getInstantiatedFromMemberFunction()) { |
| 2050 | MemberSpecializationInfo *MSInfo |
| 2051 | = Function->getMemberSpecializationInfo(); |
| 2052 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 06aa5041 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 2053 | if (MSInfo->getTemplateSpecializationKind() |
| 2054 | == TSK_ExplicitSpecialization) |
| 2055 | continue; |
| 2056 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2057 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 2058 | Function, |
| 2059 | MSInfo->getTemplateSpecializationKind(), |
Nico Weber | d75488d | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 2060 | MSInfo->getPointOfInstantiation(), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2061 | SuppressNew) || |
| 2062 | SuppressNew) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2063 | continue; |
| 2064 | |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2065 | if (Function->isDefined()) |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2066 | continue; |
| 2067 | |
| 2068 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 2069 | // C++0x [temp.explicit]p8: |
| 2070 | // An explicit instantiation definition that names a class template |
| 2071 | // specialization explicitly instantiates the class template |
| 2072 | // specialization and is only an explicit instantiation definition |
| 2073 | // of members whose definition is visible at the point of |
| 2074 | // instantiation. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2075 | if (!Pattern->isDefined()) |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2076 | continue; |
| 2077 | |
| 2078 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 2079 | |
| 2080 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
| 2081 | } else { |
| 2082 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 2083 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2084 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2085 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2086 | if (Var->isStaticDataMember()) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2087 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 2088 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 06aa5041 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 2089 | if (MSInfo->getTemplateSpecializationKind() |
| 2090 | == TSK_ExplicitSpecialization) |
| 2091 | continue; |
| 2092 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2093 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 2094 | Var, |
| 2095 | MSInfo->getTemplateSpecializationKind(), |
Nico Weber | d75488d | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 2096 | MSInfo->getPointOfInstantiation(), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2097 | SuppressNew) || |
| 2098 | SuppressNew) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2099 | continue; |
| 2100 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2101 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 2102 | // C++0x [temp.explicit]p8: |
| 2103 | // An explicit instantiation definition that names a class template |
| 2104 | // specialization explicitly instantiates the class template |
| 2105 | // specialization and is only an explicit instantiation definition |
| 2106 | // of members whose definition is visible at the point of |
| 2107 | // instantiation. |
| 2108 | if (!Var->getInstantiatedFromStaticDataMember() |
| 2109 | ->getOutOfLineDefinition()) |
| 2110 | continue; |
| 2111 | |
| 2112 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2113 | InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2114 | } else { |
| 2115 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 2116 | } |
| 2117 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2118 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
Douglas Gregor | 1da2225 | 2010-04-18 18:11:38 +0000 | [diff] [blame] | 2119 | // Always skip the injected-class-name, along with any |
| 2120 | // redeclarations of nested classes, since both would cause us |
| 2121 | // to try to instantiate the members of a class twice. |
| 2122 | if (Record->isInjectedClassName() || Record->getPreviousDeclaration()) |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2123 | continue; |
| 2124 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2125 | MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); |
| 2126 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 06aa5041 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 2127 | |
| 2128 | if (MSInfo->getTemplateSpecializationKind() |
| 2129 | == TSK_ExplicitSpecialization) |
| 2130 | continue; |
Nico Weber | d75488d | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 2131 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2132 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 2133 | Record, |
| 2134 | MSInfo->getTemplateSpecializationKind(), |
Nico Weber | d75488d | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 2135 | MSInfo->getPointOfInstantiation(), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2136 | SuppressNew) || |
| 2137 | SuppressNew) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2138 | continue; |
| 2139 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2140 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 2141 | assert(Pattern && "Missing instantiated-from-template information"); |
| 2142 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2143 | if (!Record->getDefinition()) { |
| 2144 | if (!Pattern->getDefinition()) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2145 | // C++0x [temp.explicit]p8: |
| 2146 | // An explicit instantiation definition that names a class template |
| 2147 | // specialization explicitly instantiates the class template |
| 2148 | // specialization and is only an explicit instantiation definition |
| 2149 | // of members whose definition is visible at the point of |
| 2150 | // instantiation. |
| 2151 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 2152 | MSInfo->setTemplateSpecializationKind(TSK); |
| 2153 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2154 | } |
| 2155 | |
| 2156 | continue; |
| 2157 | } |
| 2158 | |
| 2159 | InstantiateClass(PointOfInstantiation, Record, Pattern, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2160 | TemplateArgs, |
| 2161 | TSK); |
Nico Weber | d75488d | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 2162 | } else { |
| 2163 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 2164 | Record->getTemplateSpecializationKind() == |
| 2165 | TSK_ExplicitInstantiationDeclaration) { |
| 2166 | Record->setTemplateSpecializationKind(TSK); |
| 2167 | MarkVTableUsed(PointOfInstantiation, Record, true); |
| 2168 | } |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2169 | } |
Douglas Gregor | c093c1d | 2009-10-08 01:19:17 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2171 | Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2172 | if (Pattern) |
| 2173 | InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, |
| 2174 | TSK); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2175 | } |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | /// \brief Instantiate the definitions of all of the members of the |
| 2180 | /// given class template specialization, which was named as part of an |
| 2181 | /// explicit instantiation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2182 | void |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2183 | Sema::InstantiateClassTemplateSpecializationMembers( |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2184 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2185 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 2186 | TemplateSpecializationKind TSK) { |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2187 | // C++0x [temp.explicit]p7: |
| 2188 | // An explicit instantiation that names a class template |
| 2189 | // specialization is an explicit instantion of the same kind |
| 2190 | // (declaration or definition) of each of its members (not |
| 2191 | // including members inherited from base classes) that has not |
| 2192 | // been previously explicitly specialized in the translation unit |
| 2193 | // containing the explicit instantiation, except as described |
| 2194 | // below. |
| 2195 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2196 | getTemplateInstantiationArgs(ClassTemplateSpec), |
| 2197 | TSK); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2200 | StmtResult |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2201 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 2202 | if (!S) |
| 2203 | return Owned(S); |
| 2204 | |
| 2205 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 2206 | SourceLocation(), |
| 2207 | DeclarationName()); |
| 2208 | return Instantiator.TransformStmt(S); |
| 2209 | } |
| 2210 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2211 | ExprResult |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2212 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 2213 | if (!E) |
| 2214 | return Owned(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2215 | |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 2216 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 2217 | SourceLocation(), |
| 2218 | DeclarationName()); |
| 2219 | return Instantiator.TransformExpr(E); |
| 2220 | } |
| 2221 | |
Douglas Gregor | 2cd32a0 | 2011-01-07 19:35:17 +0000 | [diff] [blame] | 2222 | bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall, |
| 2223 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 2224 | SmallVectorImpl<Expr *> &Outputs) { |
Douglas Gregor | 2cd32a0 | 2011-01-07 19:35:17 +0000 | [diff] [blame] | 2225 | if (NumExprs == 0) |
| 2226 | return false; |
| 2227 | |
| 2228 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 2229 | SourceLocation(), |
| 2230 | DeclarationName()); |
| 2231 | return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs); |
| 2232 | } |
| 2233 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2234 | NestedNameSpecifierLoc |
| 2235 | Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, |
| 2236 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 2237 | if (!NNS) |
| 2238 | return NestedNameSpecifierLoc(); |
| 2239 | |
| 2240 | TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(), |
| 2241 | DeclarationName()); |
| 2242 | return Instantiator.TransformNestedNameSpecifierLoc(NNS); |
| 2243 | } |
| 2244 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2245 | /// \brief Do template substitution on declaration name info. |
| 2246 | DeclarationNameInfo |
| 2247 | Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo, |
| 2248 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 2249 | TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(), |
| 2250 | NameInfo.getName()); |
| 2251 | return Instantiator.TransformDeclarationNameInfo(NameInfo); |
| 2252 | } |
| 2253 | |
Douglas Gregor | aa59489 | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 2254 | TemplateName |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2255 | Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc, |
| 2256 | TemplateName Name, SourceLocation Loc, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2257 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 71dc509 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 2258 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 2259 | DeclarationName()); |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2260 | CXXScopeSpec SS; |
| 2261 | SS.Adopt(QualifierLoc); |
| 2262 | return Instantiator.TransformTemplateName(SS, Name, Loc); |
Douglas Gregor | aa59489 | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 2263 | } |
Douglas Gregor | c43620d | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 2264 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2265 | bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs, |
| 2266 | TemplateArgumentListInfo &Result, |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2267 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | e922c77 | 2009-08-04 22:27:00 +0000 | [diff] [blame] | 2268 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 2269 | DeclarationName()); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2270 | |
| 2271 | return Instantiator.TransformTemplateArguments(Args, NumArgs, Result); |
Douglas Gregor | c43620d | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 2272 | } |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2273 | |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2274 | llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> * |
| 2275 | LocalInstantiationScope::findInstantiationOf(const Decl *D) { |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 2276 | for (LocalInstantiationScope *Current = this; Current; |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2277 | Current = Current->Outer) { |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 2278 | |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2279 | // Check if we found something within this scope. |
Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 2280 | const Decl *CheckD = D; |
| 2281 | do { |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2282 | LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD); |
Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 2283 | if (Found != Current->LocalDecls.end()) |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2284 | return &Found->second; |
Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 2285 | |
| 2286 | // If this is a tag declaration, it's possible that we need to look for |
| 2287 | // a previous declaration. |
| 2288 | if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD)) |
| 2289 | CheckD = Tag->getPreviousDeclaration(); |
| 2290 | else |
| 2291 | CheckD = 0; |
| 2292 | } while (CheckD); |
| 2293 | |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2294 | // If we aren't combined with our outer scope, we're done. |
| 2295 | if (!Current->CombineWithOuterScope) |
| 2296 | break; |
| 2297 | } |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 2298 | |
| 2299 | // If we didn't find the decl, then we either have a sema bug, or we have a |
| 2300 | // forward reference to a label declaration. Return null to indicate that |
| 2301 | // we have an uninstantiated label. |
| 2302 | assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope"); |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2303 | return 0; |
| 2304 | } |
| 2305 | |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2306 | void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) { |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2307 | llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2308 | if (Stored.isNull()) |
| 2309 | Stored = Inst; |
| 2310 | else if (Stored.is<Decl *>()) { |
| 2311 | assert(Stored.get<Decl *>() == Inst && "Already instantiated this local"); |
| 2312 | Stored = Inst; |
| 2313 | } else |
| 2314 | LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst); |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2315 | } |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2316 | |
| 2317 | void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D, |
| 2318 | Decl *Inst) { |
| 2319 | DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>(); |
| 2320 | Pack->push_back(Inst); |
| 2321 | } |
| 2322 | |
| 2323 | void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) { |
| 2324 | llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; |
| 2325 | assert(Stored.isNull() && "Already instantiated this local"); |
| 2326 | DeclArgumentPack *Pack = new DeclArgumentPack; |
| 2327 | Stored = Pack; |
| 2328 | ArgumentPacks.push_back(Pack); |
| 2329 | } |
| 2330 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2331 | void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack, |
| 2332 | const TemplateArgument *ExplicitArgs, |
| 2333 | unsigned NumExplicitArgs) { |
| 2334 | assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) && |
| 2335 | "Already have a partially-substituted pack"); |
| 2336 | assert((!PartiallySubstitutedPack |
| 2337 | || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) && |
| 2338 | "Wrong number of arguments in partially-substituted pack"); |
| 2339 | PartiallySubstitutedPack = Pack; |
| 2340 | ArgsInPartiallySubstitutedPack = ExplicitArgs; |
| 2341 | NumArgsInPartiallySubstitutedPack = NumExplicitArgs; |
| 2342 | } |
| 2343 | |
| 2344 | NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack( |
| 2345 | const TemplateArgument **ExplicitArgs, |
| 2346 | unsigned *NumExplicitArgs) const { |
| 2347 | if (ExplicitArgs) |
| 2348 | *ExplicitArgs = 0; |
| 2349 | if (NumExplicitArgs) |
| 2350 | *NumExplicitArgs = 0; |
| 2351 | |
| 2352 | for (const LocalInstantiationScope *Current = this; Current; |
| 2353 | Current = Current->Outer) { |
| 2354 | if (Current->PartiallySubstitutedPack) { |
| 2355 | if (ExplicitArgs) |
| 2356 | *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack; |
| 2357 | if (NumExplicitArgs) |
| 2358 | *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack; |
| 2359 | |
| 2360 | return Current->PartiallySubstitutedPack; |
| 2361 | } |
| 2362 | |
| 2363 | if (!Current->CombineWithOuterScope) |
| 2364 | break; |
| 2365 | } |
| 2366 | |
| 2367 | return 0; |
| 2368 | } |