Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template instantiation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 15 | #include "clang/Sema/DeclSpec.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Lookup.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 23 | #include "clang/Basic/LangOptions.h" |
| 24 | |
| 25 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 26 | using namespace sema; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 27 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===/ |
| 29 | // Template Instantiation Support |
| 30 | //===----------------------------------------------------------------------===/ |
| 31 | |
Douglas Gregor | d6350ae | 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 | 0f8716b | 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 | 525f96c | 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 | e7089b0 | 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 | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 49 | MultiLevelTemplateArgumentList |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 50 | Sema::getTemplateInstantiationArgs(NamedDecl *D, |
Douglas Gregor | 525f96c | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 51 | const TemplateArgumentList *Innermost, |
Douglas Gregor | e7089b0 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 52 | bool RelativeToPrimary, |
| 53 | const FunctionDecl *Pattern) { |
Douglas Gregor | d110243 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 57 | if (Innermost) |
| 58 | Result.addOuterTemplateArguments(Innermost); |
| 59 | |
Douglas Gregor | d110243 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 64 | while (!Ctx->isFileContext()) { |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 65 | // Add template arguments from a class template instantiation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | d110243 | 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 | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 69 | if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization && |
| 70 | !isa<ClassTemplatePartialSpecializationDecl>(Spec)) |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 71 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 73 | Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); |
Douglas Gregor | fd056bc | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | } |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 81 | // Add template arguments from a function template specialization. |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 82 | else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) { |
Douglas Gregor | 525f96c | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 83 | if (!RelativeToPrimary && |
| 84 | Function->getTemplateSpecializationKind() |
| 85 | == TSK_ExplicitSpecialization) |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 86 | break; |
| 87 | |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 88 | if (const TemplateArgumentList *TemplateArgs |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 89 | = Function->getTemplateSpecializationArgs()) { |
| 90 | // Add the template arguments for this specialization. |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 91 | Result.addOuterTemplateArguments(TemplateArgs); |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 92 | |
Douglas Gregor | fd056bc | 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 | f181d8a | 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 | e7089b0 | 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 | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 104 | if (Function->getFriendObjectKind() && |
Douglas Gregor | e7089b0 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 105 | Function->getDeclContext()->isFileContext() && |
| 106 | (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) { |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 107 | Ctx = Function->getLexicalDeclContext(); |
Douglas Gregor | 525f96c | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 108 | RelativeToPrimary = false; |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 109 | continue; |
| 110 | } |
Douglas Gregor | 24bae92 | 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 | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 120 | } |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 121 | |
| 122 | Ctx = Ctx->getParent(); |
Douglas Gregor | 525f96c | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 123 | RelativeToPrimary = false; |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 126 | return Result; |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Douglas Gregor | f35f828 | 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 | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 146 | Sema::InstantiatingTemplate:: |
| 147 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 148 | Decl *Entity, |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 149 | SourceRange InstantiationRange) |
| 150 | : SemaRef(SemaRef) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 151 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 152 | InstantiationRange); |
| 153 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 154 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 155 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 156 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 157 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 158 | Inst.TemplateArgs = 0; |
| 159 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 160 | Inst.InstantiationRange = InstantiationRange; |
| 161 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | df667e7 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | Inst.Kind |
Douglas Gregor | df667e7 | 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 | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 183 | Inst.InstantiationRange = InstantiationRange; |
| 184 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 189 | SourceLocation PointOfInstantiation, |
Douglas Gregor | cca9e96 | 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 | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 194 | sema::TemplateDeductionInfo &DeductionInfo, |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 195 | SourceRange InstantiationRange) |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 196 | : SemaRef(SemaRef) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Douglas Gregor | cca9e96 | 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 | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 207 | Inst.DeductionInfo = &DeductionInfo; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 208 | Inst.InstantiationRange = InstantiationRange; |
| 209 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 210 | |
| 211 | if (!Inst.isInstantiationRecord()) |
| 212 | ++SemaRef.NonInstantiationEntries; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 217 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 218 | ClassTemplatePartialSpecializationDecl *PartialSpec, |
| 219 | const TemplateArgument *TemplateArgs, |
| 220 | unsigned NumTemplateArgs, |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 221 | sema::TemplateDeductionInfo &DeductionInfo, |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 222 | SourceRange InstantiationRange) |
| 223 | : SemaRef(SemaRef) { |
| 224 | |
Douglas Gregor | f35f828 | 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 | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 233 | Inst.DeductionInfo = &DeductionInfo; |
Douglas Gregor | f35f828 | 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 | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 242 | SourceLocation PointOfInstantiation, |
Anders Carlsson | 25cae7f | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 249 | Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 250 | |
| 251 | if (!Invalid) { |
| 252 | ActiveTemplateInstantiation Inst; |
| 253 | Inst.Kind |
| 254 | = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 255 | Inst.PointOfInstantiation = PointOfInstantiation; |
Anders Carlsson | 25cae7f | 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 | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | Sema::InstantiatingTemplate:: |
| 265 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 266 | NamedDecl *Template, |
Douglas Gregor | 9148c3f | 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 | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 271 | Invalid = false; |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 272 | |
Douglas Gregor | f35f828 | 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 | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | Sema::InstantiatingTemplate:: |
| 288 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 289 | NamedDecl *Template, |
Douglas Gregor | 9148c3f | 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 | f35f828 | 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 | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 304 | |
Douglas Gregor | f35f828 | 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 | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 332 | void Sema::InstantiatingTemplate::Clear() { |
| 333 | if (!Invalid) { |
Douglas Gregor | f35f828 | 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 | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 339 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 340 | Invalid = true; |
| 341 | } |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 344 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 345 | SourceLocation PointOfInstantiation, |
| 346 | SourceRange InstantiationRange) { |
Douglas Gregor | f35f828 | 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 | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 352 | return false; |
| 353 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | SemaRef.Diag(PointOfInstantiation, |
Douglas Gregor | df667e7 | 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 | ee1828a | 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 | 575cf37 | 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 | cca9e96 | 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 | 575cf37 | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 375 | unsigned InstantiationIdx = 0; |
Douglas Gregor | ee1828a | 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 | 575cf37 | 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 | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 385 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | 575cf37 | 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 | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 392 | switch (Active->Kind) { |
| 393 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
Douglas Gregor | f3e7ce4 | 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 | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 399 | Diags.Report(Active->PointOfInstantiation, DiagID) |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 400 | << Context.getTypeDeclType(Record) |
| 401 | << Active->InstantiationRange; |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 402 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 1637be7 | 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 | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 408 | Diags.Report(Active->PointOfInstantiation, DiagID) |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 409 | << Function |
| 410 | << Active->InstantiationRange; |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 411 | } else { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 412 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | 7caa682 | 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 | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 416 | } |
Douglas Gregor | df667e7 | 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 | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 423 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | Active->TemplateArgs, |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 425 | Active->NumTemplateArgs, |
| 426 | Context.PrintingPolicy); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 427 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | df667e7 | 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 | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 433 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 434 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | FunctionTemplateDecl *FnTmpl |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 436 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 437 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 438 | diag::note_explicit_template_arg_substitution_here) |
Douglas Gregor | 5e40291 | 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 | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 444 | break; |
| 445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Douglas Gregor | cca9e96 | 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 | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 451 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 452 | diag::note_partial_spec_deduct_instantiation_here) |
| 453 | << Context.getTypeDeclType(PartialSpec) |
Douglas Gregor | 5e40291 | 2010-03-30 20:35:20 +0000 | [diff] [blame] | 454 | << getTemplateArgumentBindingsText( |
| 455 | PartialSpec->getTemplateParameters(), |
| 456 | Active->TemplateArgs, |
| 457 | Active->NumTemplateArgs) |
Douglas Gregor | cca9e96 | 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 | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 462 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 463 | diag::note_function_template_deduction_instantiation_here) |
Douglas Gregor | 5e40291 | 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 | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 469 | } |
| 470 | break; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 471 | |
Anders Carlsson | 25cae7f | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 476 | std::string TemplateArgsStr |
| 477 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | Active->TemplateArgs, |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 479 | Active->NumTemplateArgs, |
| 480 | Context.PrintingPolicy); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 481 | Diags.Report(Active->PointOfInstantiation, |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 482 | diag::note_default_function_arg_instantiation_here) |
Anders Carlsson | 6bc107b | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 483 | << (FD->getNameAsString() + TemplateArgsStr) |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 484 | << Active->InstantiationRange; |
| 485 | break; |
| 486 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Douglas Gregor | 9148c3f | 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 | 54c53cc | 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 | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 501 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 502 | diag::note_prior_template_arg_substitution) |
| 503 | << isa<TemplateTemplateParmDecl>(Parm) |
| 504 | << Name |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 505 | << getTemplateArgumentBindingsText(TemplateParams, |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 506 | Active->TemplateArgs, |
| 507 | Active->NumTemplateArgs) |
| 508 | << Active->InstantiationRange; |
| 509 | break; |
| 510 | } |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 511 | |
| 512 | case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: { |
Douglas Gregor | 54c53cc | 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 | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 521 | Diags.Report(Active->PointOfInstantiation, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 522 | diag::note_template_default_arg_checking) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 523 | << getTemplateArgumentBindingsText(TemplateParams, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 524 | Active->TemplateArgs, |
| 525 | Active->NumTemplateArgs) |
| 526 | << Active->InstantiationRange; |
| 527 | break; |
| 528 | } |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 529 | } |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 533 | TemplateDeductionInfo *Sema::isSFINAEContext() const { |
Douglas Gregor | 5e9f35c | 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 | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 539 | ++Active) |
| 540 | { |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 541 | switch(Active->Kind) { |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 542 | case ActiveTemplateInstantiation::TemplateInstantiation: |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 543 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 544 | // This is a template instantiation, so there is no SFINAE. |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 545 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 547 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 548 | case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 549 | case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: |
Douglas Gregor | 9148c3f | 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 | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 553 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Douglas Gregor | cca9e96 | 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 | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 559 | assert(Active->DeductionInfo && "Missing deduction info pointer"); |
| 560 | return Active->DeductionInfo; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 564 | return 0; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 567 | //===----------------------------------------------------------------------===/ |
| 568 | // Template Instantiation for Types |
| 569 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 570 | namespace { |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 571 | class TemplateInstantiator : public TreeTransform<TemplateInstantiator> { |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 572 | const MultiLevelTemplateArgumentList &TemplateArgs; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 573 | SourceLocation Loc; |
| 574 | DeclarationName Entity; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 575 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 576 | public: |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 577 | typedef TreeTransform<TemplateInstantiator> inherited; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | |
| 579 | TemplateInstantiator(Sema &SemaRef, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 580 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 581 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | DeclarationName Entity) |
| 583 | : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 584 | Entity(Entity) { } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 585 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 587 | /// transformed. |
| 588 | /// |
| 589 | /// For the purposes of template instantiation, a type has already been |
| 590 | /// transformed if it is NULL or if it is not dependent. |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 591 | bool AlreadyTransformed(QualType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 593 | /// \brief Returns the location of the entity being instantiated, if known. |
| 594 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 596 | /// \brief Returns the name of the entity being instantiated, if any. |
| 597 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 599 | /// \brief Sets the "base" location and entity when that |
| 600 | /// information is known based on another transformation. |
| 601 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 602 | this->Loc = Loc; |
| 603 | this->Entity = Entity; |
| 604 | } |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 605 | |
| 606 | bool TryExpandParameterPacks(SourceLocation EllipsisLoc, |
| 607 | SourceRange PatternRange, |
| 608 | const UnexpandedParameterPack *Unexpanded, |
| 609 | unsigned NumUnexpanded, |
| 610 | bool &ShouldExpand, |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 611 | unsigned &NumExpansions) { |
| 612 | return getSema().CheckParameterPacksForExpansion(EllipsisLoc, |
| 613 | PatternRange, Unexpanded, |
| 614 | NumUnexpanded, |
| 615 | TemplateArgs, |
| 616 | ShouldExpand, |
| 617 | NumExpansions); |
| 618 | } |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 619 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 620 | /// \brief Transform the given declaration by instantiating a reference to |
| 621 | /// this declaration. |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 622 | Decl *TransformDecl(SourceLocation Loc, Decl *D); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 623 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | /// \brief Transform the definition of the given declaration by |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 625 | /// instantiating it. |
Douglas Gregor | aac571c | 2010-03-01 17:25:41 +0000 | [diff] [blame] | 626 | Decl *TransformDefinition(SourceLocation Loc, Decl *D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 628 | /// \bried Transform the first qualifier within a scope by instantiating the |
| 629 | /// declaration. |
| 630 | NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); |
| 631 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 632 | /// \brief Rebuild the exception declaration and register the declaration |
| 633 | /// as an instantiated local. |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 634 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 635 | TypeSourceInfo *Declarator, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 636 | IdentifierInfo *Name, |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 637 | SourceLocation Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Douglas Gregor | be270a0 | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 639 | /// \brief Rebuild the Objective-C exception declaration and register the |
| 640 | /// declaration as an instantiated local. |
| 641 | VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
| 642 | TypeSourceInfo *TSInfo, QualType T); |
| 643 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 644 | /// \brief Check for tag mismatches when instantiating an |
| 645 | /// elaborated type. |
John McCall | 21e413f | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 646 | QualType RebuildElaboratedType(SourceLocation KeywordLoc, |
| 647 | ElaboratedTypeKeyword Keyword, |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 648 | NestedNameSpecifier *NNS, QualType T); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 649 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 650 | ExprResult TransformPredefinedExpr(PredefinedExpr *E); |
| 651 | ExprResult TransformDeclRefExpr(DeclRefExpr *E); |
| 652 | ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
| 653 | ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E, |
Douglas Gregor | 56bc983 | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 654 | NonTypeTemplateParmDecl *D); |
Sebastian Redl | a29e51b | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 655 | |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 656 | QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
John McCall | 43fed0d | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 657 | FunctionProtoTypeLoc TL); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 658 | ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm); |
| 659 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | /// \brief Transforms a template type parameter type by performing |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 661 | /// substitution of the corresponding template type argument. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 662 | QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
John McCall | 43fed0d | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 663 | TemplateTypeParmTypeLoc TL); |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 664 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 665 | ExprResult TransformCallExpr(CallExpr *CE) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 666 | getSema().CallsUndergoingInstantiation.push_back(CE); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 667 | ExprResult Result = |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 668 | TreeTransform<TemplateInstantiator>::TransformCallExpr(CE); |
| 669 | getSema().CallsUndergoingInstantiation.pop_back(); |
| 670 | return move(Result); |
| 671 | } |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 672 | }; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 675 | bool TemplateInstantiator::AlreadyTransformed(QualType T) { |
| 676 | if (T.isNull()) |
| 677 | return true; |
| 678 | |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 679 | if (T->isDependentType() || T->isVariablyModifiedType()) |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 680 | return false; |
| 681 | |
| 682 | getSema().MarkDeclarationsReferencedInType(Loc, T); |
| 683 | return true; |
| 684 | } |
| 685 | |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 686 | Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) { |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 687 | if (!D) |
| 688 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 690 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 691 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | 6d3e627 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 692 | // If the corresponding template argument is NULL or non-existent, it's |
| 693 | // because we are performing instantiation from explicitly-specified |
| 694 | // template arguments in a function template, but there were some |
| 695 | // arguments left unspecified. |
| 696 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
| 697 | TTP->getPosition())) |
| 698 | return D; |
| 699 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame^] | 700 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
| 701 | |
| 702 | if (TTP->isParameterPack()) { |
| 703 | assert(Arg.getKind() == TemplateArgument::Pack && |
| 704 | "Missing argument pack"); |
| 705 | |
| 706 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 707 | // FIXME: Variadic templates fun case. |
| 708 | getSema().Diag(Loc, diag::err_pack_expansion_mismatch_unsupported); |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; |
| 713 | } |
| 714 | |
| 715 | TemplateName Template = Arg.getAsTemplate(); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 716 | assert(!Template.isNull() && Template.getAsTemplateDecl() && |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 717 | "Wrong kind of template template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 718 | return Template.getAsTemplateDecl(); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 719 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 721 | // Fall through to find the instantiated declaration for this template |
| 722 | // template parameter. |
Douglas Gregor | d1067e5 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 723 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 725 | return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs); |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Douglas Gregor | aac571c | 2010-03-01 17:25:41 +0000 | [diff] [blame] | 728 | Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 729 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 730 | if (!Inst) |
| 731 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 732 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 733 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 734 | return Inst; |
| 735 | } |
| 736 | |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 737 | NamedDecl * |
| 738 | TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
| 739 | SourceLocation Loc) { |
| 740 | // If the first part of the nested-name-specifier was a template type |
| 741 | // parameter, instantiate that type parameter down to a tag type. |
| 742 | if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
| 743 | const TemplateTypeParmType *TTP |
| 744 | = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
Douglas Gregor | 984a58b | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 745 | |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 746 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | 984a58b | 2010-12-20 22:48:17 +0000 | [diff] [blame] | 747 | // FIXME: This needs testing w/ member access expressions. |
| 748 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex()); |
| 749 | |
| 750 | if (TTP->isParameterPack()) { |
| 751 | assert(Arg.getKind() == TemplateArgument::Pack && |
| 752 | "Missing argument pack"); |
| 753 | |
| 754 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 755 | // FIXME: Variadic templates fun case. |
| 756 | getSema().Diag(Loc, diag::err_pack_expansion_mismatch_unsupported); |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; |
| 761 | } |
| 762 | |
| 763 | QualType T = Arg.getAsType(); |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 764 | if (T.isNull()) |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 765 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 766 | |
| 767 | if (const TagType *Tag = T->getAs<TagType>()) |
| 768 | return Tag->getDecl(); |
| 769 | |
| 770 | // The resulting type is not a tag; complain. |
| 771 | getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
| 772 | return 0; |
| 773 | } |
| 774 | } |
| 775 | |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 776 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 779 | VarDecl * |
| 780 | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 781 | TypeSourceInfo *Declarator, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 782 | IdentifierInfo *Name, |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 783 | SourceLocation Loc) { |
| 784 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator, |
| 785 | Name, Loc); |
Douglas Gregor | be270a0 | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 786 | if (Var) |
| 787 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 788 | return Var; |
| 789 | } |
| 790 | |
| 791 | VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
| 792 | TypeSourceInfo *TSInfo, |
| 793 | QualType T) { |
| 794 | VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T); |
| 795 | if (Var) |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 796 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 797 | return Var; |
| 798 | } |
| 799 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 800 | QualType |
John McCall | 21e413f | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 801 | TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc, |
| 802 | ElaboratedTypeKeyword Keyword, |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 803 | NestedNameSpecifier *NNS, |
| 804 | QualType T) { |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 805 | if (const TagType *TT = T->getAs<TagType>()) { |
| 806 | TagDecl* TD = TT->getDecl(); |
| 807 | |
John McCall | 21e413f | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 808 | SourceLocation TagLocation = KeywordLoc; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 809 | |
| 810 | // FIXME: type might be anonymous. |
| 811 | IdentifierInfo *Id = TD->getIdentifier(); |
| 812 | |
| 813 | // TODO: should we even warn on struct/class mismatches for this? Seems |
| 814 | // like it's likely to produce a lot of spurious errors. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 815 | if (Keyword != ETK_None && Keyword != ETK_Typename) { |
| 816 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword); |
| 817 | if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) { |
| 818 | SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) |
| 819 | << Id |
| 820 | << FixItHint::CreateReplacement(SourceRange(TagLocation), |
| 821 | TD->getKindName()); |
| 822 | SemaRef.Diag(TD->getLocation(), diag::note_previous_use); |
| 823 | } |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
John McCall | 21e413f | 2010-11-04 19:04:38 +0000 | [diff] [blame] | 827 | return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc, |
| 828 | Keyword, |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 829 | NNS, T); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 830 | } |
| 831 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 832 | ExprResult |
John McCall | 454feb9 | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 833 | TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 834 | if (!E->isTypeDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 835 | return SemaRef.Owned(E); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 836 | |
| 837 | FunctionDecl *currentDecl = getSema().getCurFunctionDecl(); |
| 838 | assert(currentDecl && "Must have current function declaration when " |
| 839 | "instantiating."); |
| 840 | |
| 841 | PredefinedExpr::IdentType IT = E->getIdentType(); |
| 842 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 843 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 844 | |
| 845 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 846 | QualType ResTy = getSema().Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 847 | ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI, |
| 848 | ArrayType::Normal, 0); |
| 849 | PredefinedExpr *PE = |
| 850 | new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT); |
| 851 | return getSema().Owned(PE); |
| 852 | } |
| 853 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 854 | ExprResult |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 855 | TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E, |
Douglas Gregor | dcee980 | 2010-02-08 23:41:45 +0000 | [diff] [blame] | 856 | NonTypeTemplateParmDecl *NTTP) { |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 857 | // If the corresponding template argument is NULL or non-existent, it's |
| 858 | // because we are performing instantiation from explicitly-specified |
| 859 | // template arguments in a function template, but there were some |
| 860 | // arguments left unspecified. |
| 861 | if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
| 862 | NTTP->getPosition())) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 863 | return SemaRef.Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | |
Douglas Gregor | 56bc983 | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 865 | TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition()); |
| 866 | if (NTTP->isParameterPack()) { |
| 867 | assert(Arg.getKind() == TemplateArgument::Pack && |
| 868 | "Missing argument pack"); |
| 869 | |
| 870 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 871 | // FIXME: Variadic templates fun case. |
| 872 | getSema().Diag(Loc, diag::err_pack_expansion_mismatch_unsupported); |
| 873 | return ExprError(); |
| 874 | } |
| 875 | |
| 876 | Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; |
| 877 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 879 | // The template argument itself might be an expression, in which |
| 880 | // case we just return that expression. |
| 881 | if (Arg.getKind() == TemplateArgument::Expression) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 882 | return SemaRef.Owned(Arg.getAsExpr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 884 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 885 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | |
John McCall | 645cf44 | 2010-02-06 10:23:53 +0000 | [diff] [blame] | 887 | // Find the instantiation of the template argument. This is |
| 888 | // required for nested templates. |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 889 | VD = cast_or_null<ValueDecl>( |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 890 | getSema().FindInstantiatedDecl(E->getLocation(), |
| 891 | VD, TemplateArgs)); |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 892 | if (!VD) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 893 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
John McCall | 645cf44 | 2010-02-06 10:23:53 +0000 | [diff] [blame] | 895 | // Derive the type we want the substituted decl to have. This had |
| 896 | // better be non-dependent, or these checks will have serious problems. |
| 897 | QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs, |
Douglas Gregor | dcee980 | 2010-02-08 23:41:45 +0000 | [diff] [blame] | 898 | E->getLocation(), |
| 899 | DeclarationName()); |
John McCall | 645cf44 | 2010-02-06 10:23:53 +0000 | [diff] [blame] | 900 | assert(!TargetType.isNull() && "type substitution failed for param type"); |
| 901 | assert(!TargetType->isDependentType() && "param type still dependent"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 902 | return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, |
| 903 | TargetType, |
| 904 | E->getLocation()); |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 907 | return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg, |
| 908 | E->getSourceRange().getBegin()); |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 912 | ExprResult |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 913 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
| 914 | NamedDecl *D = E->getDecl(); |
| 915 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
| 916 | if (NTTP->getDepth() < TemplateArgs.getNumLevels()) |
| 917 | return TransformTemplateParmRefExpr(E, NTTP); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 918 | |
| 919 | // We have a non-type template parameter that isn't fully substituted; |
| 920 | // FindInstantiatedDecl will find it in the local instantiation scope. |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | |
John McCall | 454feb9 | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 923 | return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 924 | } |
| 925 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 926 | ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr( |
John McCall | 454feb9 | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 927 | CXXDefaultArgExpr *E) { |
Sebastian Redl | a29e51b | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 928 | assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())-> |
| 929 | getDescribedFunctionTemplate() && |
| 930 | "Default arg expressions are never formed in dependent cases."); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 931 | return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(), |
| 932 | cast<FunctionDecl>(E->getParam()->getDeclContext()), |
| 933 | E->getParam()); |
Sebastian Redl | a29e51b | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 936 | QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB, |
John McCall | 43fed0d | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 937 | FunctionProtoTypeLoc TL) { |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 938 | // We need a local instantiation scope for this function prototype. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 939 | LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true); |
John McCall | 43fed0d | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 940 | return inherited::TransformFunctionProtoType(TLB, TL); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | ParmVarDecl * |
| 944 | TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) { |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 945 | return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | QualType |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 949 | TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
John McCall | 43fed0d | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 950 | TemplateTypeParmTypeLoc TL) { |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 951 | TemplateTypeParmType *T = TL.getTypePtr(); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 952 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 953 | // Replace the template type parameter with its corresponding |
| 954 | // template argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
| 956 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 957 | // because we are performing instantiation from explicitly-specified |
| 958 | // template arguments in a function template class, but there were some |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 959 | // arguments left unspecified. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 960 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
| 961 | TemplateTypeParmTypeLoc NewTL |
| 962 | = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
| 963 | NewTL.setNameLoc(TL.getNameLoc()); |
| 964 | return TL.getType(); |
| 965 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 967 | TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex()); |
| 968 | |
| 969 | if (T->isParameterPack()) { |
| 970 | assert(Arg.getKind() == TemplateArgument::Pack && |
| 971 | "Missing argument pack"); |
| 972 | |
| 973 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
| 974 | // FIXME: Variadic templates fun case. |
| 975 | getSema().Diag(TL.getSourceRange().getBegin(), |
| 976 | diag::err_pack_expansion_mismatch_unsupported); |
| 977 | return QualType(); |
| 978 | } |
| 979 | |
| 980 | Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; |
| 981 | } |
| 982 | |
| 983 | assert(Arg.getKind() == TemplateArgument::Type && |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 984 | "Template argument kind mismatch"); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 985 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 986 | QualType Replacement = Arg.getAsType(); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 987 | |
| 988 | // TODO: only do this uniquing once, at the start of instantiation. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 989 | QualType Result |
| 990 | = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); |
| 991 | SubstTemplateTypeParmTypeLoc NewTL |
| 992 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 993 | NewTL.setNameLoc(TL.getNameLoc()); |
| 994 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 996 | |
| 997 | // The template type parameter comes from an inner template (e.g., |
| 998 | // the template parameter list of a member template inside the |
| 999 | // template we are instantiating). Create a new template type |
| 1000 | // parameter with the template "level" reduced by one. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1001 | QualType Result |
| 1002 | = getSema().Context.getTemplateTypeParmType(T->getDepth() |
| 1003 | - TemplateArgs.getNumLevels(), |
| 1004 | T->getIndex(), |
| 1005 | T->isParameterPack(), |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1006 | T->getName()); |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 1007 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 1008 | NewTL.setNameLoc(TL.getNameLoc()); |
| 1009 | return Result; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1010 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1011 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1012 | /// \brief Perform substitution on the type T with a given set of template |
| 1013 | /// arguments. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1014 | /// |
| 1015 | /// This routine substitutes the given template arguments into the |
| 1016 | /// type T and produces the instantiated type. |
| 1017 | /// |
| 1018 | /// \param T the type into which the template arguments will be |
| 1019 | /// substituted. If this type is not dependent, it will be returned |
| 1020 | /// immediately. |
| 1021 | /// |
| 1022 | /// \param TemplateArgs the template arguments that will be |
| 1023 | /// substituted for the top-level template parameters within T. |
| 1024 | /// |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1025 | /// \param Loc the location in the source code where this substitution |
| 1026 | /// is being performed. It will typically be the location of the |
| 1027 | /// declarator (if we're instantiating the type of some declaration) |
| 1028 | /// or the location of the type in the source code (if, e.g., we're |
| 1029 | /// instantiating the type of a cast expression). |
| 1030 | /// |
| 1031 | /// \param Entity the name of the entity associated with a declaration |
| 1032 | /// being instantiated (if any). May be empty to indicate that there |
| 1033 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 1034 | /// a cast expression) or that the entity has no name (e.g., an |
| 1035 | /// unnamed function parameter). |
| 1036 | /// |
| 1037 | /// \returns If the instantiation succeeds, the instantiated |
| 1038 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1039 | TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T, |
John McCall | cd7ba1c | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 1040 | const MultiLevelTemplateArgumentList &Args, |
| 1041 | SourceLocation Loc, |
| 1042 | DeclarationName Entity) { |
| 1043 | assert(!ActiveTemplateInstantiations.empty() && |
| 1044 | "Cannot perform an instantiation without some context on the " |
| 1045 | "instantiation stack"); |
| 1046 | |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 1047 | if (!T->getType()->isDependentType() && |
| 1048 | !T->getType()->isVariablyModifiedType()) |
John McCall | cd7ba1c | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 1049 | return T; |
| 1050 | |
| 1051 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 1052 | return Instantiator.TransformType(T); |
| 1053 | } |
| 1054 | |
| 1055 | /// Deprecated form of the above. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | QualType Sema::SubstType(QualType T, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1057 | const MultiLevelTemplateArgumentList &TemplateArgs, |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1058 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1059 | assert(!ActiveTemplateInstantiations.empty() && |
| 1060 | "Cannot perform an instantiation without some context on the " |
| 1061 | "instantiation stack"); |
| 1062 | |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 1063 | // If T is not a dependent type or a variably-modified type, there |
| 1064 | // is nothing to do. |
| 1065 | if (!T->isDependentType() && !T->isVariablyModifiedType()) |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1066 | return T; |
| 1067 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 1068 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 1069 | return Instantiator.TransformType(T); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1070 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1071 | |
John McCall | 6cd3b9f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1072 | static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) { |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 1073 | if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType()) |
John McCall | 6cd3b9f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1074 | return true; |
| 1075 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1076 | TypeLoc TL = T->getTypeLoc().IgnoreParens(); |
John McCall | 6cd3b9f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1077 | if (!isa<FunctionProtoTypeLoc>(TL)) |
| 1078 | return false; |
| 1079 | |
| 1080 | FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL); |
| 1081 | for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) { |
| 1082 | ParmVarDecl *P = FP.getArg(I); |
| 1083 | |
| 1084 | // TODO: currently we always rebuild expressions. When we |
| 1085 | // properly get lazier about this, we should use the same |
| 1086 | // logic to avoid rebuilding prototypes here. |
| 1087 | if (P->hasInit()) |
| 1088 | return true; |
| 1089 | } |
| 1090 | |
| 1091 | return false; |
| 1092 | } |
| 1093 | |
| 1094 | /// A form of SubstType intended specifically for instantiating the |
| 1095 | /// type of a FunctionDecl. Its purpose is solely to force the |
| 1096 | /// instantiation of default-argument expressions. |
| 1097 | TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T, |
| 1098 | const MultiLevelTemplateArgumentList &Args, |
| 1099 | SourceLocation Loc, |
| 1100 | DeclarationName Entity) { |
| 1101 | assert(!ActiveTemplateInstantiations.empty() && |
| 1102 | "Cannot perform an instantiation without some context on the " |
| 1103 | "instantiation stack"); |
| 1104 | |
| 1105 | if (!NeedsInstantiationAsFunctionType(T)) |
| 1106 | return T; |
| 1107 | |
| 1108 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 1109 | |
| 1110 | TypeLocBuilder TLB; |
| 1111 | |
| 1112 | TypeLoc TL = T->getTypeLoc(); |
| 1113 | TLB.reserve(TL.getFullDataSize()); |
| 1114 | |
John McCall | 43fed0d | 2010-11-12 08:19:04 +0000 | [diff] [blame] | 1115 | QualType Result = Instantiator.TransformType(TLB, TL); |
John McCall | 6cd3b9f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1116 | if (Result.isNull()) |
| 1117 | return 0; |
| 1118 | |
| 1119 | return TLB.getTypeSourceInfo(Context, Result); |
| 1120 | } |
| 1121 | |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1122 | ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm, |
| 1123 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 1124 | TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo(); |
| 1125 | TypeSourceInfo *NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(), |
| 1126 | OldParm->getDeclName()); |
| 1127 | if (!NewDI) |
| 1128 | return 0; |
| 1129 | |
| 1130 | if (NewDI->getType()->isVoidType()) { |
| 1131 | Diag(OldParm->getLocation(), diag::err_param_with_void_type); |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(), |
| 1136 | NewDI, NewDI->getType(), |
| 1137 | OldParm->getIdentifier(), |
| 1138 | OldParm->getLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1139 | OldParm->getStorageClass(), |
| 1140 | OldParm->getStorageClassAsWritten()); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1141 | if (!NewParm) |
| 1142 | return 0; |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 1143 | |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1144 | // Mark the (new) default argument as uninstantiated (if any). |
| 1145 | if (OldParm->hasUninstantiatedDefaultArg()) { |
| 1146 | Expr *Arg = OldParm->getUninstantiatedDefaultArg(); |
| 1147 | NewParm->setUninstantiatedDefaultArg(Arg); |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 1148 | } else if (OldParm->hasUnparsedDefaultArg()) { |
| 1149 | NewParm->setUnparsedDefaultArg(); |
| 1150 | UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1151 | } else if (Expr *Arg = OldParm->getDefaultArg()) |
| 1152 | NewParm->setUninstantiatedDefaultArg(Arg); |
| 1153 | |
| 1154 | NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg()); |
| 1155 | |
| 1156 | CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm); |
Argyrios Kyrtzidis | e3041be | 2010-07-19 10:14:41 +0000 | [diff] [blame] | 1157 | // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext |
| 1158 | // can be anything, is this right ? |
Fariborz Jahanian | 55a17c0 | 2010-07-13 21:05:02 +0000 | [diff] [blame] | 1159 | NewParm->setDeclContext(CurContext); |
Fariborz Jahanian | e7ffbe2 | 2010-07-13 20:05:58 +0000 | [diff] [blame] | 1160 | |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1161 | return NewParm; |
| 1162 | } |
| 1163 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1164 | /// \brief Perform substitution on the base class specifiers of the |
| 1165 | /// given class template specialization. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1166 | /// |
| 1167 | /// Produces a diagnostic and returns true on error, returns false and |
| 1168 | /// attaches the instantiated base classes to the class template |
| 1169 | /// specialization if successful. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | bool |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1171 | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 1172 | CXXRecordDecl *Pattern, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1173 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1174 | bool Invalid = false; |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 1175 | llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | for (ClassTemplateSpecializationDecl::base_class_iterator |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1177 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 1178 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1179 | if (!Base->getType()->isDependentType()) { |
Fariborz Jahanian | 71c6e71 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 1180 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1181 | continue; |
| 1182 | } |
| 1183 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1184 | SourceLocation EllipsisLoc; |
| 1185 | if (Base->isPackExpansion()) { |
| 1186 | // This is a pack expansion. See whether we should expand it now, or |
| 1187 | // wait until later. |
| 1188 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 1189 | collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(), |
| 1190 | Unexpanded); |
| 1191 | bool ShouldExpand = false; |
| 1192 | unsigned NumExpansions = 0; |
| 1193 | if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(), |
| 1194 | Base->getSourceRange(), |
| 1195 | Unexpanded.data(), Unexpanded.size(), |
| 1196 | TemplateArgs, ShouldExpand, |
| 1197 | NumExpansions)) { |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1198 | Invalid = true; |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1199 | continue; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | // If we should expand this pack expansion now, do so. |
| 1203 | if (ShouldExpand) { |
| 1204 | for (unsigned I = 0; I != NumExpansions; ++I) { |
| 1205 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 1206 | |
| 1207 | TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(), |
| 1208 | TemplateArgs, |
| 1209 | Base->getSourceRange().getBegin(), |
| 1210 | DeclarationName()); |
| 1211 | if (!BaseTypeLoc) { |
| 1212 | Invalid = true; |
| 1213 | continue; |
| 1214 | } |
| 1215 | |
| 1216 | if (CXXBaseSpecifier *InstantiatedBase |
| 1217 | = CheckBaseSpecifier(Instantiation, |
| 1218 | Base->getSourceRange(), |
| 1219 | Base->isVirtual(), |
| 1220 | Base->getAccessSpecifierAsWritten(), |
| 1221 | BaseTypeLoc, |
| 1222 | SourceLocation())) |
| 1223 | InstantiatedBases.push_back(InstantiatedBase); |
| 1224 | else |
| 1225 | Invalid = true; |
| 1226 | } |
| 1227 | |
| 1228 | continue; |
| 1229 | } |
| 1230 | |
| 1231 | // The resulting base specifier will (still) be a pack expansion. |
| 1232 | EllipsisLoc = Base->getEllipsisLoc(); |
| 1233 | } |
| 1234 | |
| 1235 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1236 | TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(), |
| 1237 | TemplateArgs, |
| 1238 | Base->getSourceRange().getBegin(), |
| 1239 | DeclarationName()); |
| 1240 | if (!BaseTypeLoc) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1241 | Invalid = true; |
| 1242 | continue; |
| 1243 | } |
| 1244 | |
| 1245 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1246 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1247 | Base->getSourceRange(), |
| 1248 | Base->isVirtual(), |
| 1249 | Base->getAccessSpecifierAsWritten(), |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1250 | BaseTypeLoc, |
| 1251 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1252 | InstantiatedBases.push_back(InstantiatedBase); |
| 1253 | else |
| 1254 | Invalid = true; |
| 1255 | } |
| 1256 | |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 1257 | if (!Invalid && |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1258 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1259 | InstantiatedBases.size())) |
| 1260 | Invalid = true; |
| 1261 | |
| 1262 | return Invalid; |
| 1263 | } |
| 1264 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1265 | /// \brief Instantiate the definition of a class from a given pattern. |
| 1266 | /// |
| 1267 | /// \param PointOfInstantiation The point of instantiation within the |
| 1268 | /// source code. |
| 1269 | /// |
| 1270 | /// \param Instantiation is the declaration whose definition is being |
| 1271 | /// instantiated. This will be either a class template specialization |
| 1272 | /// or a member class of a class template specialization. |
| 1273 | /// |
| 1274 | /// \param Pattern is the pattern from which the instantiation |
| 1275 | /// occurs. This will be either the declaration of a class template or |
| 1276 | /// the declaration of a member class of a class template. |
| 1277 | /// |
| 1278 | /// \param TemplateArgs The template arguments to be substituted into |
| 1279 | /// the pattern. |
| 1280 | /// |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1281 | /// \param TSK the kind of implicit or explicit instantiation to perform. |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1282 | /// |
| 1283 | /// \param Complain whether to complain if the class cannot be instantiated due |
| 1284 | /// to the lack of a definition. |
| 1285 | /// |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1286 | /// \returns true if an error occurred, false otherwise. |
| 1287 | bool |
| 1288 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 1289 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1290 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1291 | TemplateSpecializationKind TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1292 | bool Complain) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1293 | bool Invalid = false; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1294 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | CXXRecordDecl *PatternDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1296 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1297 | if (!PatternDef) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1298 | if (!Complain) { |
| 1299 | // Say nothing |
| 1300 | } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1301 | Diag(PointOfInstantiation, |
| 1302 | diag::err_implicit_instantiate_member_undefined) |
| 1303 | << Context.getTypeDeclType(Instantiation); |
| 1304 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 1305 | } else { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 1306 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1307 | << (TSK != TSK_ImplicitInstantiation) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1308 | << Context.getTypeDeclType(Instantiation); |
| 1309 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 1310 | } |
| 1311 | return true; |
| 1312 | } |
| 1313 | Pattern = PatternDef; |
| 1314 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 1315 | // \brief Record the point of instantiation. |
| 1316 | if (MemberSpecializationInfo *MSInfo |
| 1317 | = Instantiation->getMemberSpecializationInfo()) { |
| 1318 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1319 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1320 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1321 | = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { |
| 1322 | Spec->setTemplateSpecializationKind(TSK); |
| 1323 | Spec->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Douglas Gregor | d048bb7 | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 1326 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1327 | if (Inst) |
| 1328 | return true; |
| 1329 | |
| 1330 | // Enter the scope of this instantiation. We don't use |
| 1331 | // PushDeclContext because we don't have a scope. |
John McCall | f581382 | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 1332 | ContextRAII SavedContext(*this, Instantiation); |
Douglas Gregor | 9679caf | 2010-05-12 17:27:19 +0000 | [diff] [blame] | 1333 | EnterExpressionEvaluationContext EvalContext(*this, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1334 | Sema::PotentiallyEvaluated); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1335 | |
Douglas Gregor | 05030bb | 2010-03-24 01:33:17 +0000 | [diff] [blame] | 1336 | // If this is an instantiation of a local class, merge this local |
| 1337 | // instantiation scope with the enclosing scope. Otherwise, every |
| 1338 | // instantiation of a class has its own local instantiation scope. |
| 1339 | bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod(); |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1340 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Douglas Gregor | 05030bb | 2010-03-24 01:33:17 +0000 | [diff] [blame] | 1341 | |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 1342 | // Pull attributes from the pattern onto the instantiation. |
| 1343 | InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
| 1344 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1345 | // Start the definition of this instantiation. |
| 1346 | Instantiation->startDefinition(); |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1347 | |
| 1348 | Instantiation->setTagKind(Pattern->getTagKind()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1349 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1350 | // Do substitution on the base class specifiers. |
| 1351 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1352 | Invalid = true; |
| 1353 | |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1354 | TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1355 | llvm::SmallVector<Decl*, 4> Fields; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1356 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1357 | MemberEnd = Pattern->decls_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1358 | Member != MemberEnd; ++Member) { |
Argyrios Kyrtzidis | bb5e431 | 2010-11-04 03:18:57 +0000 | [diff] [blame] | 1359 | // Don't instantiate members not belonging in this semantic context. |
| 1360 | // e.g. for: |
| 1361 | // @code |
| 1362 | // template <int i> class A { |
| 1363 | // class B *g; |
| 1364 | // }; |
| 1365 | // @endcode |
| 1366 | // 'class B' has the template as lexical context but semantically it is |
| 1367 | // introduced in namespace scope. |
| 1368 | if ((*Member)->getDeclContext() != Pattern) |
| 1369 | continue; |
| 1370 | |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1371 | if ((*Member)->isInvalidDecl()) { |
| 1372 | Invalid = true; |
| 1373 | continue; |
| 1374 | } |
| 1375 | |
| 1376 | Decl *NewMember = Instantiator.Visit(*Member); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1377 | if (NewMember) { |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 1378 | if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1379 | Fields.push_back(Field); |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 1380 | else if (NewMember->isInvalidDecl()) |
| 1381 | Invalid = true; |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1382 | } else { |
| 1383 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1384 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 1385 | // true. For now, we expect to skip some members that we can't yet handle. |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | // Finish checking fields. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1390 | ActOnFields(0, Instantiation->getLocation(), Instantiation, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1391 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1392 | 0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1393 | CheckCompletedCXXClass(Instantiation); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1394 | if (Instantiation->isInvalidDecl()) |
| 1395 | Invalid = true; |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1396 | else { |
| 1397 | // Instantiate any out-of-line class template partial |
| 1398 | // specializations now. |
| 1399 | for (TemplateDeclInstantiator::delayed_partial_spec_iterator |
| 1400 | P = Instantiator.delayed_partial_spec_begin(), |
| 1401 | PEnd = Instantiator.delayed_partial_spec_end(); |
| 1402 | P != PEnd; ++P) { |
| 1403 | if (!Instantiator.InstantiateClassTemplatePartialSpecialization( |
| 1404 | P->first, |
| 1405 | P->second)) { |
| 1406 | Invalid = true; |
| 1407 | break; |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1412 | // Exit the scope of this instantiation. |
John McCall | f581382 | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 1413 | SavedContext.pop(); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1414 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1415 | if (!Invalid) { |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 1416 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 1417 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1418 | // Always emit the vtable for an explicit instantiation definition |
| 1419 | // of a polymorphic class template specialization. |
| 1420 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 1421 | MarkVTableUsed(PointOfInstantiation, Instantiation, true); |
| 1422 | } |
| 1423 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1424 | return Invalid; |
| 1425 | } |
| 1426 | |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1427 | namespace { |
| 1428 | /// \brief A partial specialization whose template arguments have matched |
| 1429 | /// a given template-id. |
| 1430 | struct PartialSpecMatchResult { |
| 1431 | ClassTemplatePartialSpecializationDecl *Partial; |
| 1432 | TemplateArgumentList *Args; |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1433 | }; |
| 1434 | } |
| 1435 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | bool |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1437 | Sema::InstantiateClassTemplateSpecialization( |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1438 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1439 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1440 | TemplateSpecializationKind TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1441 | bool Complain) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1442 | // Perform the actual instantiation on the canonical declaration. |
| 1443 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 1444 | ClassTemplateSpec->getCanonicalDecl()); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1445 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1446 | // Check whether we have already instantiated or specialized this class |
| 1447 | // template specialization. |
| 1448 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) { |
| 1449 | if (ClassTemplateSpec->getSpecializationKind() == |
| 1450 | TSK_ExplicitInstantiationDeclaration && |
| 1451 | TSK == TSK_ExplicitInstantiationDefinition) { |
| 1452 | // An explicit instantiation definition follows an explicit instantiation |
| 1453 | // declaration (C++0x [temp.explicit]p10); go ahead and perform the |
| 1454 | // explicit instantiation. |
| 1455 | ClassTemplateSpec->setSpecializationKind(TSK); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1456 | |
| 1457 | // If this is an explicit instantiation definition, mark the |
| 1458 | // vtable as used. |
| 1459 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 1460 | MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true); |
| 1461 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1462 | return false; |
| 1463 | } |
| 1464 | |
| 1465 | // We can only instantiate something that hasn't already been |
| 1466 | // instantiated or specialized. Fail without any diagnostics: our |
| 1467 | // caller will provide an error message. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1468 | return true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1469 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1470 | |
Douglas Gregor | 9eea08b | 2009-09-15 16:51:42 +0000 | [diff] [blame] | 1471 | if (ClassTemplateSpec->isInvalidDecl()) |
| 1472 | return true; |
| 1473 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1474 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1475 | CXXRecordDecl *Pattern = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1476 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1477 | // C++ [temp.class.spec.match]p1: |
| 1478 | // When a class template is used in a context that requires an |
| 1479 | // instantiation of the class, it is necessary to determine |
| 1480 | // whether the instantiation is to be generated using the primary |
| 1481 | // template or one of the partial specializations. This is done by |
| 1482 | // matching the template arguments of the class template |
| 1483 | // specialization with the template argument lists of the partial |
| 1484 | // specializations. |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1485 | typedef PartialSpecMatchResult MatchResult; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1486 | llvm::SmallVector<MatchResult, 4> Matched; |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 1487 | llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 1488 | Template->getPartialSpecializations(PartialSpecs); |
| 1489 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 1490 | ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1491 | TemplateDeductionInfo Info(Context, PointOfInstantiation); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1492 | if (TemplateDeductionResult Result |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 1493 | = DeduceTemplateArguments(Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1494 | ClassTemplateSpec->getTemplateArgs(), |
| 1495 | Info)) { |
| 1496 | // FIXME: Store the failed-deduction information for use in |
| 1497 | // diagnostics, later. |
| 1498 | (void)Result; |
| 1499 | } else { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1500 | Matched.push_back(PartialSpecMatchResult()); |
| 1501 | Matched.back().Partial = Partial; |
| 1502 | Matched.back().Args = Info.take(); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1503 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1506 | if (Matched.size() >= 1) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1507 | llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1508 | if (Matched.size() == 1) { |
| 1509 | // -- If exactly one matching specialization is found, the |
| 1510 | // instantiation is generated from that specialization. |
| 1511 | // We don't need to do anything for this. |
| 1512 | } else { |
| 1513 | // -- If more than one matching specialization is found, the |
| 1514 | // partial order rules (14.5.4.2) are used to determine |
| 1515 | // whether one of the specializations is more specialized |
| 1516 | // than the others. If none of the specializations is more |
| 1517 | // specialized than all of the other matching |
| 1518 | // specializations, then the use of the class template is |
| 1519 | // ambiguous and the program is ill-formed. |
| 1520 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 1521 | PEnd = Matched.end(); |
| 1522 | P != PEnd; ++P) { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1523 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1524 | PointOfInstantiation) |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1525 | == P->Partial) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1526 | Best = P; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1527 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1528 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1529 | // Determine if the best partial specialization is more specialized than |
| 1530 | // the others. |
| 1531 | bool Ambiguous = false; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1532 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1533 | PEnd = Matched.end(); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1534 | P != PEnd; ++P) { |
| 1535 | if (P != Best && |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1536 | getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1537 | PointOfInstantiation) |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1538 | != Best->Partial) { |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1539 | Ambiguous = true; |
| 1540 | break; |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | if (Ambiguous) { |
| 1545 | // Partial ordering did not produce a clear winner. Complain. |
| 1546 | ClassTemplateSpec->setInvalidDecl(); |
| 1547 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 1548 | << ClassTemplateSpec; |
| 1549 | |
| 1550 | // Print the matching partial specializations. |
| 1551 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1552 | PEnd = Matched.end(); |
| 1553 | P != PEnd; ++P) |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1554 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 1555 | << getTemplateArgumentBindingsText( |
| 1556 | P->Partial->getTemplateParameters(), |
| 1557 | *P->Args); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1558 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1559 | return true; |
| 1560 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | // Instantiate using the best class template partial specialization. |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1564 | ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1565 | while (OrigPartialSpec->getInstantiatedFromMember()) { |
| 1566 | // If we've found an explicit specialization of this class template, |
| 1567 | // stop here and use that as the pattern. |
| 1568 | if (OrigPartialSpec->isMemberSpecialization()) |
| 1569 | break; |
| 1570 | |
| 1571 | OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember(); |
| 1572 | } |
| 1573 | |
| 1574 | Pattern = OrigPartialSpec; |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1575 | ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args); |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1576 | } else { |
| 1577 | // -- If no matches are found, the instantiation is generated |
| 1578 | // from the primary template. |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1579 | ClassTemplateDecl *OrigTemplate = Template; |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1580 | while (OrigTemplate->getInstantiatedFromMemberTemplate()) { |
| 1581 | // If we've found an explicit specialization of this class template, |
| 1582 | // stop here and use that as the pattern. |
| 1583 | if (OrigTemplate->isMemberSpecialization()) |
| 1584 | break; |
| 1585 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1586 | OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1589 | Pattern = OrigTemplate->getTemplatedDecl(); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1590 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1591 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1592 | bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec, |
| 1593 | Pattern, |
| 1594 | getTemplateInstantiationArgs(ClassTemplateSpec), |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1595 | TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1596 | Complain); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1598 | return Result; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1599 | } |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1600 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1601 | /// \brief Instantiates the definitions of all of the member |
| 1602 | /// of the given class, which is an instantiation of a class template |
| 1603 | /// or a member class of a template. |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1604 | void |
| 1605 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1606 | CXXRecordDecl *Instantiation, |
| 1607 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 1608 | TemplateSpecializationKind TSK) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1609 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(), |
| 1610 | DEnd = Instantiation->decls_end(); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1611 | D != DEnd; ++D) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1612 | bool SuppressNew = false; |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1613 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1614 | if (FunctionDecl *Pattern |
| 1615 | = Function->getInstantiatedFromMemberFunction()) { |
| 1616 | MemberSpecializationInfo *MSInfo |
| 1617 | = Function->getMemberSpecializationInfo(); |
| 1618 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 1619 | if (MSInfo->getTemplateSpecializationKind() |
| 1620 | == TSK_ExplicitSpecialization) |
| 1621 | continue; |
| 1622 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1623 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1624 | Function, |
| 1625 | MSInfo->getTemplateSpecializationKind(), |
Nico Weber | c956b6e | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 1626 | MSInfo->getPointOfInstantiation(), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1627 | SuppressNew) || |
| 1628 | SuppressNew) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1629 | continue; |
| 1630 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1631 | if (Function->hasBody()) |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1632 | continue; |
| 1633 | |
| 1634 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 1635 | // C++0x [temp.explicit]p8: |
| 1636 | // An explicit instantiation definition that names a class template |
| 1637 | // specialization explicitly instantiates the class template |
| 1638 | // specialization and is only an explicit instantiation definition |
| 1639 | // of members whose definition is visible at the point of |
| 1640 | // instantiation. |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1641 | if (!Pattern->hasBody()) |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1642 | continue; |
| 1643 | |
| 1644 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1645 | |
| 1646 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
| 1647 | } else { |
| 1648 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1649 | } |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1650 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1651 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1652 | if (Var->isStaticDataMember()) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1653 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 1654 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 1655 | if (MSInfo->getTemplateSpecializationKind() |
| 1656 | == TSK_ExplicitSpecialization) |
| 1657 | continue; |
| 1658 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1659 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1660 | Var, |
| 1661 | MSInfo->getTemplateSpecializationKind(), |
Nico Weber | c956b6e | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 1662 | MSInfo->getPointOfInstantiation(), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1663 | SuppressNew) || |
| 1664 | SuppressNew) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1665 | continue; |
| 1666 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1667 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 1668 | // C++0x [temp.explicit]p8: |
| 1669 | // An explicit instantiation definition that names a class template |
| 1670 | // specialization explicitly instantiates the class template |
| 1671 | // specialization and is only an explicit instantiation definition |
| 1672 | // of members whose definition is visible at the point of |
| 1673 | // instantiation. |
| 1674 | if (!Var->getInstantiatedFromStaticDataMember() |
| 1675 | ->getOutOfLineDefinition()) |
| 1676 | continue; |
| 1677 | |
| 1678 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1679 | InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1680 | } else { |
| 1681 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1682 | } |
| 1683 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1684 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
Douglas Gregor | a77eaa9 | 2010-04-18 18:11:38 +0000 | [diff] [blame] | 1685 | // Always skip the injected-class-name, along with any |
| 1686 | // redeclarations of nested classes, since both would cause us |
| 1687 | // to try to instantiate the members of a class twice. |
| 1688 | if (Record->isInjectedClassName() || Record->getPreviousDeclaration()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1689 | continue; |
| 1690 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1691 | MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); |
| 1692 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 1693 | |
| 1694 | if (MSInfo->getTemplateSpecializationKind() |
| 1695 | == TSK_ExplicitSpecialization) |
| 1696 | continue; |
Nico Weber | c956b6e | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 1697 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1698 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1699 | Record, |
| 1700 | MSInfo->getTemplateSpecializationKind(), |
Nico Weber | c956b6e | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 1701 | MSInfo->getPointOfInstantiation(), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1702 | SuppressNew) || |
| 1703 | SuppressNew) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1704 | continue; |
| 1705 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1706 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 1707 | assert(Pattern && "Missing instantiated-from-template information"); |
| 1708 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1709 | if (!Record->getDefinition()) { |
| 1710 | if (!Pattern->getDefinition()) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1711 | // C++0x [temp.explicit]p8: |
| 1712 | // An explicit instantiation definition that names a class template |
| 1713 | // specialization explicitly instantiates the class template |
| 1714 | // specialization and is only an explicit instantiation definition |
| 1715 | // of members whose definition is visible at the point of |
| 1716 | // instantiation. |
| 1717 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 1718 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1719 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1720 | } |
| 1721 | |
| 1722 | continue; |
| 1723 | } |
| 1724 | |
| 1725 | InstantiateClass(PointOfInstantiation, Record, Pattern, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1726 | TemplateArgs, |
| 1727 | TSK); |
Nico Weber | c956b6e | 2010-09-27 21:02:09 +0000 | [diff] [blame] | 1728 | } else { |
| 1729 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 1730 | Record->getTemplateSpecializationKind() == |
| 1731 | TSK_ExplicitInstantiationDeclaration) { |
| 1732 | Record->setTemplateSpecializationKind(TSK); |
| 1733 | MarkVTableUsed(PointOfInstantiation, Record, true); |
| 1734 | } |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1735 | } |
Douglas Gregor | e9374d5 | 2009-10-08 01:19:17 +0000 | [diff] [blame] | 1736 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1737 | Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1738 | if (Pattern) |
| 1739 | InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, |
| 1740 | TSK); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | /// \brief Instantiate the definitions of all of the members of the |
| 1746 | /// given class template specialization, which was named as part of an |
| 1747 | /// explicit instantiation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | void |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1749 | Sema::InstantiateClassTemplateSpecializationMembers( |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1750 | SourceLocation PointOfInstantiation, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1751 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 1752 | TemplateSpecializationKind TSK) { |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1753 | // C++0x [temp.explicit]p7: |
| 1754 | // An explicit instantiation that names a class template |
| 1755 | // specialization is an explicit instantion of the same kind |
| 1756 | // (declaration or definition) of each of its members (not |
| 1757 | // including members inherited from base classes) that has not |
| 1758 | // been previously explicitly specialized in the translation unit |
| 1759 | // containing the explicit instantiation, except as described |
| 1760 | // below. |
| 1761 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1762 | getTemplateInstantiationArgs(ClassTemplateSpec), |
| 1763 | TSK); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1766 | StmtResult |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1767 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 1768 | if (!S) |
| 1769 | return Owned(S); |
| 1770 | |
| 1771 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1772 | SourceLocation(), |
| 1773 | DeclarationName()); |
| 1774 | return Instantiator.TransformStmt(S); |
| 1775 | } |
| 1776 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1777 | ExprResult |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1778 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1779 | if (!E) |
| 1780 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1782 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1783 | SourceLocation(), |
| 1784 | DeclarationName()); |
| 1785 | return Instantiator.TransformExpr(E); |
| 1786 | } |
| 1787 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1788 | /// \brief Do template substitution on a nested-name-specifier. |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1789 | NestedNameSpecifier * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1790 | Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1791 | SourceRange Range, |
| 1792 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | dcee1a1 | 2009-08-06 05:28:30 +0000 | [diff] [blame] | 1793 | TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(), |
| 1794 | DeclarationName()); |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 1795 | return Instantiator.TransformNestedNameSpecifier(NNS, Range); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1796 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1797 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1798 | /// \brief Do template substitution on declaration name info. |
| 1799 | DeclarationNameInfo |
| 1800 | Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo, |
| 1801 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 1802 | TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(), |
| 1803 | NameInfo.getName()); |
| 1804 | return Instantiator.TransformDeclarationNameInfo(NameInfo); |
| 1805 | } |
| 1806 | |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1807 | TemplateName |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1808 | Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1809 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | d1067e5 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 1810 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 1811 | DeclarationName()); |
| 1812 | return Instantiator.TransformTemplateName(Name); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1813 | } |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1814 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1815 | bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs, |
| 1816 | TemplateArgumentListInfo &Result, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1817 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 670444e | 2009-08-04 22:27:00 +0000 | [diff] [blame] | 1818 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 1819 | DeclarationName()); |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1820 | |
| 1821 | return Instantiator.TransformTemplateArguments(Args, NumArgs, Result); |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1822 | } |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1823 | |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1824 | Decl *LocalInstantiationScope::getInstantiationOf(const Decl *D) { |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1825 | for (LocalInstantiationScope *Current = this; Current; |
| 1826 | Current = Current->Outer) { |
| 1827 | // Check if we found something within this scope. |
Douglas Gregor | ebb1c56 | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 1828 | const Decl *CheckD = D; |
| 1829 | do { |
| 1830 | llvm::DenseMap<const Decl *, Decl *>::iterator Found |
| 1831 | = Current->LocalDecls.find(CheckD); |
| 1832 | if (Found != Current->LocalDecls.end()) |
| 1833 | return Found->second; |
| 1834 | |
| 1835 | // If this is a tag declaration, it's possible that we need to look for |
| 1836 | // a previous declaration. |
| 1837 | if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD)) |
| 1838 | CheckD = Tag->getPreviousDeclaration(); |
| 1839 | else |
| 1840 | CheckD = 0; |
| 1841 | } while (CheckD); |
| 1842 | |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1843 | // If we aren't combined with our outer scope, we're done. |
| 1844 | if (!Current->CombineWithOuterScope) |
| 1845 | break; |
| 1846 | } |
| 1847 | |
| 1848 | assert(D->isInvalidDecl() && |
| 1849 | "declaration was not instantiated in this scope!"); |
| 1850 | return 0; |
| 1851 | } |
| 1852 | |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1853 | void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) { |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1854 | Decl *&Stored = LocalDecls[D]; |
| 1855 | assert((!Stored || Stored == Inst)&& "Already instantiated this local"); |
| 1856 | Stored = Inst; |
| 1857 | } |