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