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 | |
| 152 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 153 | InstantiationRange); |
| 154 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 155 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 156 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 157 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 158 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 159 | Inst.TemplateArgs = 0; |
| 160 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 161 | Inst.InstantiationRange = InstantiationRange; |
| 162 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 167 | SourceLocation PointOfInstantiation, |
| 168 | TemplateDecl *Template, |
| 169 | const TemplateArgument *TemplateArgs, |
| 170 | unsigned NumTemplateArgs, |
| 171 | SourceRange InstantiationRange) |
| 172 | : SemaRef(SemaRef) { |
| 173 | |
| 174 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 175 | InstantiationRange); |
| 176 | if (!Invalid) { |
| 177 | ActiveTemplateInstantiation Inst; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | Inst.Kind |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 179 | = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; |
| 180 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 181 | Inst.Entity = reinterpret_cast<uintptr_t>(Template); |
| 182 | Inst.TemplateArgs = TemplateArgs; |
| 183 | Inst.NumTemplateArgs = NumTemplateArgs; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 184 | Inst.InstantiationRange = InstantiationRange; |
| 185 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 190 | SourceLocation PointOfInstantiation, |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 191 | FunctionTemplateDecl *FunctionTemplate, |
| 192 | const TemplateArgument *TemplateArgs, |
| 193 | unsigned NumTemplateArgs, |
| 194 | ActiveTemplateInstantiation::InstantiationKind Kind, |
| 195 | SourceRange InstantiationRange) |
| 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; |
| 207 | Inst.InstantiationRange = InstantiationRange; |
| 208 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 209 | |
| 210 | if (!Inst.isInstantiationRecord()) |
| 211 | ++SemaRef.NonInstantiationEntries; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 216 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 217 | ClassTemplatePartialSpecializationDecl *PartialSpec, |
| 218 | const TemplateArgument *TemplateArgs, |
| 219 | unsigned NumTemplateArgs, |
| 220 | SourceRange InstantiationRange) |
| 221 | : SemaRef(SemaRef) { |
| 222 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 223 | Invalid = false; |
| 224 | |
| 225 | ActiveTemplateInstantiation Inst; |
| 226 | Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution; |
| 227 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 228 | Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec); |
| 229 | Inst.TemplateArgs = TemplateArgs; |
| 230 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 231 | Inst.InstantiationRange = InstantiationRange; |
| 232 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 233 | |
| 234 | assert(!Inst.isInstantiationRecord()); |
| 235 | ++SemaRef.NonInstantiationEntries; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 239 | SourceLocation PointOfInstantiation, |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 240 | ParmVarDecl *Param, |
| 241 | const TemplateArgument *TemplateArgs, |
| 242 | unsigned NumTemplateArgs, |
| 243 | SourceRange InstantiationRange) |
| 244 | : SemaRef(SemaRef) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 246 | Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 247 | |
| 248 | if (!Invalid) { |
| 249 | ActiveTemplateInstantiation Inst; |
| 250 | Inst.Kind |
| 251 | = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 252 | Inst.PointOfInstantiation = PointOfInstantiation; |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 253 | Inst.Entity = reinterpret_cast<uintptr_t>(Param); |
| 254 | Inst.TemplateArgs = TemplateArgs; |
| 255 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 256 | Inst.InstantiationRange = InstantiationRange; |
| 257 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | Sema::InstantiatingTemplate:: |
| 262 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 263 | TemplateDecl *Template, |
| 264 | NonTypeTemplateParmDecl *Param, |
| 265 | const TemplateArgument *TemplateArgs, |
| 266 | unsigned NumTemplateArgs, |
| 267 | SourceRange InstantiationRange) : SemaRef(SemaRef) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 268 | Invalid = false; |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 269 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 270 | ActiveTemplateInstantiation Inst; |
| 271 | Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution; |
| 272 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 273 | Inst.Template = Template; |
| 274 | Inst.Entity = reinterpret_cast<uintptr_t>(Param); |
| 275 | Inst.TemplateArgs = TemplateArgs; |
| 276 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 277 | Inst.InstantiationRange = InstantiationRange; |
| 278 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 279 | |
| 280 | assert(!Inst.isInstantiationRecord()); |
| 281 | ++SemaRef.NonInstantiationEntries; |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | Sema::InstantiatingTemplate:: |
| 285 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 286 | TemplateDecl *Template, |
| 287 | TemplateTemplateParmDecl *Param, |
| 288 | const TemplateArgument *TemplateArgs, |
| 289 | unsigned NumTemplateArgs, |
| 290 | SourceRange InstantiationRange) : SemaRef(SemaRef) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 291 | Invalid = false; |
| 292 | ActiveTemplateInstantiation Inst; |
| 293 | Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution; |
| 294 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 295 | Inst.Template = Template; |
| 296 | Inst.Entity = reinterpret_cast<uintptr_t>(Param); |
| 297 | Inst.TemplateArgs = TemplateArgs; |
| 298 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 299 | Inst.InstantiationRange = InstantiationRange; |
| 300 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 301 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 302 | assert(!Inst.isInstantiationRecord()); |
| 303 | ++SemaRef.NonInstantiationEntries; |
| 304 | } |
| 305 | |
| 306 | Sema::InstantiatingTemplate:: |
| 307 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 308 | TemplateDecl *Template, |
| 309 | NamedDecl *Param, |
| 310 | const TemplateArgument *TemplateArgs, |
| 311 | unsigned NumTemplateArgs, |
| 312 | SourceRange InstantiationRange) : SemaRef(SemaRef) { |
| 313 | Invalid = false; |
| 314 | |
| 315 | ActiveTemplateInstantiation Inst; |
| 316 | Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking; |
| 317 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 318 | Inst.Template = Template; |
| 319 | Inst.Entity = reinterpret_cast<uintptr_t>(Param); |
| 320 | Inst.TemplateArgs = TemplateArgs; |
| 321 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 322 | Inst.InstantiationRange = InstantiationRange; |
| 323 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 324 | |
| 325 | assert(!Inst.isInstantiationRecord()); |
| 326 | ++SemaRef.NonInstantiationEntries; |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 329 | void Sema::InstantiatingTemplate::Clear() { |
| 330 | if (!Invalid) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 331 | if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) { |
| 332 | assert(SemaRef.NonInstantiationEntries > 0); |
| 333 | --SemaRef.NonInstantiationEntries; |
| 334 | } |
| 335 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 336 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 337 | Invalid = true; |
| 338 | } |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 341 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 342 | SourceLocation PointOfInstantiation, |
| 343 | SourceRange InstantiationRange) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 344 | assert(SemaRef.NonInstantiationEntries <= |
| 345 | SemaRef.ActiveTemplateInstantiations.size()); |
| 346 | if ((SemaRef.ActiveTemplateInstantiations.size() - |
| 347 | SemaRef.NonInstantiationEntries) |
| 348 | <= SemaRef.getLangOptions().InstantiationDepth) |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 349 | return false; |
| 350 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | SemaRef.Diag(PointOfInstantiation, |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 352 | diag::err_template_recursion_depth_exceeded) |
| 353 | << SemaRef.getLangOptions().InstantiationDepth |
| 354 | << InstantiationRange; |
| 355 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
| 356 | << SemaRef.getLangOptions().InstantiationDepth; |
| 357 | return true; |
| 358 | } |
| 359 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 360 | /// \brief Prints the current instantiation stack through a series of |
| 361 | /// notes. |
| 362 | void Sema::PrintInstantiationStack() { |
Douglas Gregor | 575cf37 | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 363 | // Determine which template instantiations to skip, if any. |
| 364 | unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart; |
| 365 | unsigned Limit = Diags.getTemplateBacktraceLimit(); |
| 366 | if (Limit && Limit < ActiveTemplateInstantiations.size()) { |
| 367 | SkipStart = Limit / 2 + Limit % 2; |
| 368 | SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2; |
| 369 | } |
| 370 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 371 | // 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] | 372 | unsigned InstantiationIdx = 0; |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 373 | for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator |
| 374 | Active = ActiveTemplateInstantiations.rbegin(), |
| 375 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 376 | Active != ActiveEnd; |
Douglas Gregor | 575cf37 | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 377 | ++Active, ++InstantiationIdx) { |
| 378 | // Skip this instantiation? |
| 379 | if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) { |
| 380 | if (InstantiationIdx == SkipStart) { |
| 381 | // Note that we're skipping instantiations. |
| 382 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 383 | diag::note_instantiation_contexts_suppressed) |
| 384 | << unsigned(ActiveTemplateInstantiations.size() - Limit); |
| 385 | } |
| 386 | continue; |
| 387 | } |
| 388 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 389 | switch (Active->Kind) { |
| 390 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 391 | Decl *D = reinterpret_cast<Decl *>(Active->Entity); |
| 392 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 393 | unsigned DiagID = diag::note_template_member_class_here; |
| 394 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 395 | DiagID = diag::note_template_class_instantiation_here; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 397 | DiagID) |
| 398 | << Context.getTypeDeclType(Record) |
| 399 | << Active->InstantiationRange; |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 400 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 401 | unsigned DiagID; |
| 402 | if (Function->getPrimaryTemplate()) |
| 403 | DiagID = diag::note_function_template_spec_here; |
| 404 | else |
| 405 | DiagID = diag::note_template_member_function_here; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 407 | DiagID) |
| 408 | << Function |
| 409 | << Active->InstantiationRange; |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 410 | } else { |
| 411 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 412 | diag::note_template_static_data_member_def_here) |
| 413 | << cast<VarDecl>(D) |
| 414 | << Active->InstantiationRange; |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 415 | } |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 416 | break; |
| 417 | } |
| 418 | |
| 419 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { |
| 420 | TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); |
| 421 | std::string TemplateArgsStr |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 422 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | Active->TemplateArgs, |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 424 | Active->NumTemplateArgs, |
| 425 | Context.PrintingPolicy); |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 426 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 427 | diag::note_default_arg_instantiation_here) |
| 428 | << (Template->getNameAsString() + TemplateArgsStr) |
| 429 | << Active->InstantiationRange; |
| 430 | break; |
| 431 | } |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 432 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 433 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | FunctionTemplateDecl *FnTmpl |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 435 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 436 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 437 | diag::note_explicit_template_arg_substitution_here) |
Douglas Gregor | 5e40291 | 2010-03-30 20:35:20 +0000 | [diff] [blame] | 438 | << FnTmpl |
| 439 | << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), |
| 440 | Active->TemplateArgs, |
| 441 | Active->NumTemplateArgs) |
| 442 | << Active->InstantiationRange; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 443 | break; |
| 444 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 446 | case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: |
| 447 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 448 | = dyn_cast<ClassTemplatePartialSpecializationDecl>( |
| 449 | (Decl *)Active->Entity)) { |
| 450 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 451 | diag::note_partial_spec_deduct_instantiation_here) |
| 452 | << Context.getTypeDeclType(PartialSpec) |
Douglas Gregor | 5e40291 | 2010-03-30 20:35:20 +0000 | [diff] [blame] | 453 | << getTemplateArgumentBindingsText( |
| 454 | PartialSpec->getTemplateParameters(), |
| 455 | Active->TemplateArgs, |
| 456 | Active->NumTemplateArgs) |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 457 | << Active->InstantiationRange; |
| 458 | } else { |
| 459 | FunctionTemplateDecl *FnTmpl |
| 460 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
| 461 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 462 | diag::note_function_template_deduction_instantiation_here) |
Douglas Gregor | 5e40291 | 2010-03-30 20:35:20 +0000 | [diff] [blame] | 463 | << FnTmpl |
| 464 | << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), |
| 465 | Active->TemplateArgs, |
| 466 | Active->NumTemplateArgs) |
| 467 | << Active->InstantiationRange; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 468 | } |
| 469 | break; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 470 | |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 471 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: { |
| 472 | ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity); |
| 473 | FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 475 | std::string TemplateArgsStr |
| 476 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | Active->TemplateArgs, |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 478 | Active->NumTemplateArgs, |
| 479 | Context.PrintingPolicy); |
| 480 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 481 | diag::note_default_function_arg_instantiation_here) |
Anders Carlsson | 6bc107b | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 482 | << (FD->getNameAsString() + TemplateArgsStr) |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 483 | << Active->InstantiationRange; |
| 484 | break; |
| 485 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 487 | case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: { |
| 488 | NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity); |
| 489 | std::string Name; |
| 490 | if (!Parm->getName().empty()) |
| 491 | Name = std::string(" '") + Parm->getName().str() + "'"; |
| 492 | |
| 493 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 494 | diag::note_prior_template_arg_substitution) |
| 495 | << isa<TemplateTemplateParmDecl>(Parm) |
| 496 | << Name |
| 497 | << getTemplateArgumentBindingsText( |
| 498 | Active->Template->getTemplateParameters(), |
| 499 | Active->TemplateArgs, |
| 500 | Active->NumTemplateArgs) |
| 501 | << Active->InstantiationRange; |
| 502 | break; |
| 503 | } |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 504 | |
| 505 | case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: { |
| 506 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 507 | diag::note_template_default_arg_checking) |
| 508 | << getTemplateArgumentBindingsText( |
| 509 | Active->Template->getTemplateParameters(), |
| 510 | Active->TemplateArgs, |
| 511 | Active->NumTemplateArgs) |
| 512 | << Active->InstantiationRange; |
| 513 | break; |
| 514 | } |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 515 | } |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 519 | bool Sema::isSFINAEContext() const { |
| 520 | using llvm::SmallVector; |
| 521 | for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator |
| 522 | Active = ActiveTemplateInstantiations.rbegin(), |
| 523 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 524 | Active != ActiveEnd; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 525 | ++Active) |
| 526 | { |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 527 | switch(Active->Kind) { |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 528 | case ActiveTemplateInstantiation::TemplateInstantiation: |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 529 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 530 | // This is a template instantiation, so there is no SFINAE. |
| 531 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 533 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 534 | case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 535 | case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 536 | // A default template argument instantiation and substitution into |
| 537 | // template parameters with arguments for prior parameters may or may |
| 538 | // not be a SFINAE context; look further up the stack. |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 539 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 541 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: |
| 542 | case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: |
| 543 | // We're either substitution explicitly-specified template arguments |
| 544 | // or deduced template arguments, so SFINAE applies. |
| 545 | return true; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | return false; |
| 550 | } |
| 551 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 552 | //===----------------------------------------------------------------------===/ |
| 553 | // Template Instantiation for Types |
| 554 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 555 | namespace { |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 556 | class TemplateInstantiator : public TreeTransform<TemplateInstantiator> { |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 557 | const MultiLevelTemplateArgumentList &TemplateArgs; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 558 | SourceLocation Loc; |
| 559 | DeclarationName Entity; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 560 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 561 | public: |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 562 | typedef TreeTransform<TemplateInstantiator> inherited; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | |
| 564 | TemplateInstantiator(Sema &SemaRef, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 565 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 566 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | DeclarationName Entity) |
| 568 | : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 569 | Entity(Entity) { } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 570 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 572 | /// transformed. |
| 573 | /// |
| 574 | /// For the purposes of template instantiation, a type has already been |
| 575 | /// transformed if it is NULL or if it is not dependent. |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 576 | bool AlreadyTransformed(QualType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 578 | /// \brief Returns the location of the entity being instantiated, if known. |
| 579 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 581 | /// \brief Returns the name of the entity being instantiated, if any. |
| 582 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 584 | /// \brief Sets the "base" location and entity when that |
| 585 | /// information is known based on another transformation. |
| 586 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 587 | this->Loc = Loc; |
| 588 | this->Entity = Entity; |
| 589 | } |
| 590 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 591 | /// \brief Transform the given declaration by instantiating a reference to |
| 592 | /// this declaration. |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 593 | Decl *TransformDecl(SourceLocation Loc, Decl *D); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 594 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | /// \brief Transform the definition of the given declaration by |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 596 | /// instantiating it. |
Douglas Gregor | aac571c | 2010-03-01 17:25:41 +0000 | [diff] [blame] | 597 | Decl *TransformDefinition(SourceLocation Loc, Decl *D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 599 | /// \bried Transform the first qualifier within a scope by instantiating the |
| 600 | /// declaration. |
| 601 | NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); |
| 602 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 603 | /// \brief Rebuild the exception declaration and register the declaration |
| 604 | /// as an instantiated local. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 606 | TypeSourceInfo *Declarator, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 607 | IdentifierInfo *Name, |
| 608 | SourceLocation Loc, SourceRange TypeRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | be270a0 | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 610 | /// \brief Rebuild the Objective-C exception declaration and register the |
| 611 | /// declaration as an instantiated local. |
| 612 | VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
| 613 | TypeSourceInfo *TSInfo, QualType T); |
| 614 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 615 | /// \brief Check for tag mismatches when instantiating an |
| 616 | /// elaborated type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 617 | QualType RebuildElaboratedType(ElaboratedTypeKeyword Keyword, |
| 618 | NestedNameSpecifier *NNS, QualType T); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 619 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 620 | ExprResult TransformPredefinedExpr(PredefinedExpr *E); |
| 621 | ExprResult TransformDeclRefExpr(DeclRefExpr *E); |
| 622 | ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
| 623 | ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E, |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 624 | NonTypeTemplateParmDecl *D); |
Sebastian Redl | a29e51b | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 625 | |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 626 | QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
| 627 | FunctionProtoTypeLoc TL, |
| 628 | QualType ObjectType); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 629 | ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm); |
| 630 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | /// \brief Transforms a template type parameter type by performing |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 632 | /// substitution of the corresponding template type argument. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 633 | QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 634 | TemplateTypeParmTypeLoc TL, |
| 635 | QualType ObjectType); |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 636 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 637 | ExprResult TransformCallExpr(CallExpr *CE) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 638 | getSema().CallsUndergoingInstantiation.push_back(CE); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 639 | ExprResult Result = |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 640 | TreeTransform<TemplateInstantiator>::TransformCallExpr(CE); |
| 641 | getSema().CallsUndergoingInstantiation.pop_back(); |
| 642 | return move(Result); |
| 643 | } |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 644 | }; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 647 | bool TemplateInstantiator::AlreadyTransformed(QualType T) { |
| 648 | if (T.isNull()) |
| 649 | return true; |
| 650 | |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 651 | if (T->isDependentType() || T->isVariablyModifiedType()) |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 652 | return false; |
| 653 | |
| 654 | getSema().MarkDeclarationsReferencedInType(Loc, T); |
| 655 | return true; |
| 656 | } |
| 657 | |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 658 | Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) { |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 659 | if (!D) |
| 660 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 662 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 663 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | 6d3e627 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 664 | // If the corresponding template argument is NULL or non-existent, it's |
| 665 | // because we are performing instantiation from explicitly-specified |
| 666 | // template arguments in a function template, but there were some |
| 667 | // arguments left unspecified. |
| 668 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
| 669 | TTP->getPosition())) |
| 670 | return D; |
| 671 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 672 | TemplateName Template |
| 673 | = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate(); |
| 674 | assert(!Template.isNull() && Template.getAsTemplateDecl() && |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 675 | "Wrong kind of template template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 676 | return Template.getAsTemplateDecl(); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 677 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 679 | // Fall through to find the instantiated declaration for this template |
| 680 | // template parameter. |
Douglas Gregor | d1067e5 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 681 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 683 | return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs); |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Douglas Gregor | aac571c | 2010-03-01 17:25:41 +0000 | [diff] [blame] | 686 | Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 687 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 688 | if (!Inst) |
| 689 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 691 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 692 | return Inst; |
| 693 | } |
| 694 | |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 695 | NamedDecl * |
| 696 | TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
| 697 | SourceLocation Loc) { |
| 698 | // If the first part of the nested-name-specifier was a template type |
| 699 | // parameter, instantiate that type parameter down to a tag type. |
| 700 | if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
| 701 | const TemplateTypeParmType *TTP |
| 702 | = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
| 703 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 704 | QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType(); |
| 705 | if (T.isNull()) |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 706 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 707 | |
| 708 | if (const TagType *Tag = T->getAs<TagType>()) |
| 709 | return Tag->getDecl(); |
| 710 | |
| 711 | // The resulting type is not a tag; complain. |
| 712 | getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
| 713 | return 0; |
| 714 | } |
| 715 | } |
| 716 | |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 717 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 720 | VarDecl * |
| 721 | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | QualType T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 723 | TypeSourceInfo *Declarator, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 724 | IdentifierInfo *Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | SourceLocation Loc, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 726 | SourceRange TypeRange) { |
| 727 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator, |
| 728 | Name, Loc, TypeRange); |
Douglas Gregor | be270a0 | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 729 | if (Var) |
| 730 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 731 | return Var; |
| 732 | } |
| 733 | |
| 734 | VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
| 735 | TypeSourceInfo *TSInfo, |
| 736 | QualType T) { |
| 737 | VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T); |
| 738 | if (Var) |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 739 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 740 | return Var; |
| 741 | } |
| 742 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 743 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 744 | TemplateInstantiator::RebuildElaboratedType(ElaboratedTypeKeyword Keyword, |
| 745 | NestedNameSpecifier *NNS, |
| 746 | QualType T) { |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 747 | if (const TagType *TT = T->getAs<TagType>()) { |
| 748 | TagDecl* TD = TT->getDecl(); |
| 749 | |
| 750 | // FIXME: this location is very wrong; we really need typelocs. |
| 751 | SourceLocation TagLocation = TD->getTagKeywordLoc(); |
| 752 | |
| 753 | // FIXME: type might be anonymous. |
| 754 | IdentifierInfo *Id = TD->getIdentifier(); |
| 755 | |
| 756 | // TODO: should we even warn on struct/class mismatches for this? Seems |
| 757 | // like it's likely to produce a lot of spurious errors. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 758 | if (Keyword != ETK_None && Keyword != ETK_Typename) { |
| 759 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword); |
| 760 | if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) { |
| 761 | SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) |
| 762 | << Id |
| 763 | << FixItHint::CreateReplacement(SourceRange(TagLocation), |
| 764 | TD->getKindName()); |
| 765 | SemaRef.Diag(TD->getLocation(), diag::note_previous_use); |
| 766 | } |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 770 | return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(Keyword, |
| 771 | NNS, T); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 772 | } |
| 773 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 774 | ExprResult |
John McCall | 454feb9 | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 775 | TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 776 | if (!E->isTypeDependent()) |
| 777 | return SemaRef.Owned(E->Retain()); |
| 778 | |
| 779 | FunctionDecl *currentDecl = getSema().getCurFunctionDecl(); |
| 780 | assert(currentDecl && "Must have current function declaration when " |
| 781 | "instantiating."); |
| 782 | |
| 783 | PredefinedExpr::IdentType IT = E->getIdentType(); |
| 784 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 785 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 786 | |
| 787 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 788 | QualType ResTy = getSema().Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 789 | ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI, |
| 790 | ArrayType::Normal, 0); |
| 791 | PredefinedExpr *PE = |
| 792 | new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT); |
| 793 | return getSema().Owned(PE); |
| 794 | } |
| 795 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 796 | ExprResult |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 797 | TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E, |
Douglas Gregor | dcee980 | 2010-02-08 23:41:45 +0000 | [diff] [blame] | 798 | NonTypeTemplateParmDecl *NTTP) { |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 799 | // If the corresponding template argument is NULL or non-existent, it's |
| 800 | // because we are performing instantiation from explicitly-specified |
| 801 | // template arguments in a function template, but there were some |
| 802 | // arguments left unspecified. |
| 803 | if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
| 804 | NTTP->getPosition())) |
| 805 | return SemaRef.Owned(E->Retain()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 807 | const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(), |
| 808 | NTTP->getPosition()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 810 | // The template argument itself might be an expression, in which |
| 811 | // case we just return that expression. |
| 812 | if (Arg.getKind() == TemplateArgument::Expression) |
| 813 | return SemaRef.Owned(Arg.getAsExpr()->Retain()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 814 | |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 815 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 816 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | |
John McCall | 645cf44 | 2010-02-06 10:23:53 +0000 | [diff] [blame] | 818 | // Find the instantiation of the template argument. This is |
| 819 | // required for nested templates. |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 820 | VD = cast_or_null<ValueDecl>( |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 821 | getSema().FindInstantiatedDecl(E->getLocation(), |
| 822 | VD, TemplateArgs)); |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 823 | if (!VD) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 824 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
John McCall | 645cf44 | 2010-02-06 10:23:53 +0000 | [diff] [blame] | 826 | // Derive the type we want the substituted decl to have. This had |
| 827 | // better be non-dependent, or these checks will have serious problems. |
| 828 | QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs, |
Douglas Gregor | dcee980 | 2010-02-08 23:41:45 +0000 | [diff] [blame] | 829 | E->getLocation(), |
| 830 | DeclarationName()); |
John McCall | 645cf44 | 2010-02-06 10:23:53 +0000 | [diff] [blame] | 831 | assert(!TargetType.isNull() && "type substitution failed for param type"); |
| 832 | assert(!TargetType->isDependentType() && "param type still dependent"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 833 | return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, |
| 834 | TargetType, |
| 835 | E->getLocation()); |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 838 | return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg, |
| 839 | E->getSourceRange().getBegin()); |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 843 | ExprResult |
John McCall | b8fc053 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 844 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
| 845 | NamedDecl *D = E->getDecl(); |
| 846 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
| 847 | if (NTTP->getDepth() < TemplateArgs.getNumLevels()) |
| 848 | return TransformTemplateParmRefExpr(E, NTTP); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 849 | |
| 850 | // We have a non-type template parameter that isn't fully substituted; |
| 851 | // FindInstantiatedDecl will find it in the local instantiation scope. |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 852 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | |
John McCall | 454feb9 | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 854 | return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 855 | } |
| 856 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 857 | ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr( |
John McCall | 454feb9 | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 858 | CXXDefaultArgExpr *E) { |
Sebastian Redl | a29e51b | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 859 | assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())-> |
| 860 | getDescribedFunctionTemplate() && |
| 861 | "Default arg expressions are never formed in dependent cases."); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 862 | return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(), |
| 863 | cast<FunctionDecl>(E->getParam()->getDeclContext()), |
| 864 | E->getParam()); |
Sebastian Redl | a29e51b | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 867 | QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB, |
| 868 | FunctionProtoTypeLoc TL, |
| 869 | QualType ObjectType) { |
| 870 | // We need a local instantiation scope for this function prototype. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 871 | LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true); |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 872 | return inherited::TransformFunctionProtoType(TLB, TL, ObjectType); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | ParmVarDecl * |
| 876 | TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) { |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 877 | return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | QualType |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 881 | TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 882 | TemplateTypeParmTypeLoc TL, |
| 883 | QualType ObjectType) { |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 884 | TemplateTypeParmType *T = TL.getTypePtr(); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 885 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 886 | // Replace the template type parameter with its corresponding |
| 887 | // template argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | |
| 889 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 890 | // because we are performing instantiation from explicitly-specified |
| 891 | // template arguments in a function template class, but there were some |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 892 | // arguments left unspecified. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 893 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
| 894 | TemplateTypeParmTypeLoc NewTL |
| 895 | = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
| 896 | NewTL.setNameLoc(TL.getNameLoc()); |
| 897 | return TL.getType(); |
| 898 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | |
| 900 | assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind() |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 901 | == TemplateArgument::Type && |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 902 | "Template argument kind mismatch"); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 903 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 904 | QualType Replacement |
| 905 | = TemplateArgs(T->getDepth(), T->getIndex()).getAsType(); |
| 906 | |
| 907 | // TODO: only do this uniquing once, at the start of instantiation. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 908 | QualType Result |
| 909 | = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); |
| 910 | SubstTemplateTypeParmTypeLoc NewTL |
| 911 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 912 | NewTL.setNameLoc(TL.getNameLoc()); |
| 913 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 915 | |
| 916 | // The template type parameter comes from an inner template (e.g., |
| 917 | // the template parameter list of a member template inside the |
| 918 | // template we are instantiating). Create a new template type |
| 919 | // parameter with the template "level" reduced by one. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 920 | QualType Result |
| 921 | = getSema().Context.getTemplateTypeParmType(T->getDepth() |
| 922 | - TemplateArgs.getNumLevels(), |
| 923 | T->getIndex(), |
| 924 | T->isParameterPack(), |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 925 | T->getName()); |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 926 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 927 | NewTL.setNameLoc(TL.getNameLoc()); |
| 928 | return Result; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 929 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 930 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 931 | /// \brief Perform substitution on the type T with a given set of template |
| 932 | /// arguments. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 933 | /// |
| 934 | /// This routine substitutes the given template arguments into the |
| 935 | /// type T and produces the instantiated type. |
| 936 | /// |
| 937 | /// \param T the type into which the template arguments will be |
| 938 | /// substituted. If this type is not dependent, it will be returned |
| 939 | /// immediately. |
| 940 | /// |
| 941 | /// \param TemplateArgs the template arguments that will be |
| 942 | /// substituted for the top-level template parameters within T. |
| 943 | /// |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 944 | /// \param Loc the location in the source code where this substitution |
| 945 | /// is being performed. It will typically be the location of the |
| 946 | /// declarator (if we're instantiating the type of some declaration) |
| 947 | /// or the location of the type in the source code (if, e.g., we're |
| 948 | /// instantiating the type of a cast expression). |
| 949 | /// |
| 950 | /// \param Entity the name of the entity associated with a declaration |
| 951 | /// being instantiated (if any). May be empty to indicate that there |
| 952 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 953 | /// a cast expression) or that the entity has no name (e.g., an |
| 954 | /// unnamed function parameter). |
| 955 | /// |
| 956 | /// \returns If the instantiation succeeds, the instantiated |
| 957 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 958 | TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T, |
John McCall | cd7ba1c | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 959 | const MultiLevelTemplateArgumentList &Args, |
| 960 | SourceLocation Loc, |
| 961 | DeclarationName Entity) { |
| 962 | assert(!ActiveTemplateInstantiations.empty() && |
| 963 | "Cannot perform an instantiation without some context on the " |
| 964 | "instantiation stack"); |
| 965 | |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 966 | if (!T->getType()->isDependentType() && |
| 967 | !T->getType()->isVariablyModifiedType()) |
John McCall | cd7ba1c | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 968 | return T; |
| 969 | |
| 970 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 971 | return Instantiator.TransformType(T); |
| 972 | } |
| 973 | |
| 974 | /// Deprecated form of the above. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | QualType Sema::SubstType(QualType T, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 976 | const MultiLevelTemplateArgumentList &TemplateArgs, |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 977 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 978 | assert(!ActiveTemplateInstantiations.empty() && |
| 979 | "Cannot perform an instantiation without some context on the " |
| 980 | "instantiation stack"); |
| 981 | |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 982 | // If T is not a dependent type or a variably-modified type, there |
| 983 | // is nothing to do. |
| 984 | if (!T->isDependentType() && !T->isVariablyModifiedType()) |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 985 | return T; |
| 986 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 987 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 988 | return Instantiator.TransformType(T); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 989 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 990 | |
John McCall | 6cd3b9f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 991 | static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) { |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 992 | if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType()) |
John McCall | 6cd3b9f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 993 | return true; |
| 994 | |
| 995 | TypeLoc TL = T->getTypeLoc(); |
| 996 | if (!isa<FunctionProtoTypeLoc>(TL)) |
| 997 | return false; |
| 998 | |
| 999 | FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL); |
| 1000 | for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) { |
| 1001 | ParmVarDecl *P = FP.getArg(I); |
| 1002 | |
| 1003 | // TODO: currently we always rebuild expressions. When we |
| 1004 | // properly get lazier about this, we should use the same |
| 1005 | // logic to avoid rebuilding prototypes here. |
| 1006 | if (P->hasInit()) |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
| 1010 | return false; |
| 1011 | } |
| 1012 | |
| 1013 | /// A form of SubstType intended specifically for instantiating the |
| 1014 | /// type of a FunctionDecl. Its purpose is solely to force the |
| 1015 | /// instantiation of default-argument expressions. |
| 1016 | TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T, |
| 1017 | const MultiLevelTemplateArgumentList &Args, |
| 1018 | SourceLocation Loc, |
| 1019 | DeclarationName Entity) { |
| 1020 | assert(!ActiveTemplateInstantiations.empty() && |
| 1021 | "Cannot perform an instantiation without some context on the " |
| 1022 | "instantiation stack"); |
| 1023 | |
| 1024 | if (!NeedsInstantiationAsFunctionType(T)) |
| 1025 | return T; |
| 1026 | |
| 1027 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 1028 | |
| 1029 | TypeLocBuilder TLB; |
| 1030 | |
| 1031 | TypeLoc TL = T->getTypeLoc(); |
| 1032 | TLB.reserve(TL.getFullDataSize()); |
| 1033 | |
| 1034 | QualType Result = Instantiator.TransformType(TLB, TL, QualType()); |
| 1035 | if (Result.isNull()) |
| 1036 | return 0; |
| 1037 | |
| 1038 | return TLB.getTypeSourceInfo(Context, Result); |
| 1039 | } |
| 1040 | |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1041 | ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm, |
| 1042 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 1043 | TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo(); |
| 1044 | TypeSourceInfo *NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(), |
| 1045 | OldParm->getDeclName()); |
| 1046 | if (!NewDI) |
| 1047 | return 0; |
| 1048 | |
| 1049 | if (NewDI->getType()->isVoidType()) { |
| 1050 | Diag(OldParm->getLocation(), diag::err_param_with_void_type); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(), |
| 1055 | NewDI, NewDI->getType(), |
| 1056 | OldParm->getIdentifier(), |
| 1057 | OldParm->getLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1058 | OldParm->getStorageClass(), |
| 1059 | OldParm->getStorageClassAsWritten()); |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1060 | if (!NewParm) |
| 1061 | return 0; |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 1062 | |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1063 | // Mark the (new) default argument as uninstantiated (if any). |
| 1064 | if (OldParm->hasUninstantiatedDefaultArg()) { |
| 1065 | Expr *Arg = OldParm->getUninstantiatedDefaultArg(); |
| 1066 | NewParm->setUninstantiatedDefaultArg(Arg); |
| 1067 | } else if (Expr *Arg = OldParm->getDefaultArg()) |
| 1068 | NewParm->setUninstantiatedDefaultArg(Arg); |
| 1069 | |
| 1070 | NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg()); |
| 1071 | |
| 1072 | CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm); |
Argyrios Kyrtzidis | e3041be | 2010-07-19 10:14:41 +0000 | [diff] [blame] | 1073 | // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext |
| 1074 | // can be anything, is this right ? |
Fariborz Jahanian | 55a17c0 | 2010-07-13 21:05:02 +0000 | [diff] [blame] | 1075 | NewParm->setDeclContext(CurContext); |
Fariborz Jahanian | e7ffbe2 | 2010-07-13 20:05:58 +0000 | [diff] [blame] | 1076 | |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1077 | return NewParm; |
| 1078 | } |
| 1079 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1080 | /// \brief Perform substitution on the base class specifiers of the |
| 1081 | /// given class template specialization. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1082 | /// |
| 1083 | /// Produces a diagnostic and returns true on error, returns false and |
| 1084 | /// attaches the instantiated base classes to the class template |
| 1085 | /// specialization if successful. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | bool |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1087 | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 1088 | CXXRecordDecl *Pattern, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1089 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1090 | bool Invalid = false; |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 1091 | llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | for (ClassTemplateSpecializationDecl::base_class_iterator |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1093 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 1094 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1095 | if (!Base->getType()->isDependentType()) { |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1096 | const CXXRecordDecl *BaseDecl = |
| 1097 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 1098 | |
| 1099 | // Make sure to set the attributes from the base. |
| 1100 | SetClassDeclAttributesFromBase(Instantiation, BaseDecl, |
| 1101 | Base->isVirtual()); |
| 1102 | |
Fariborz Jahanian | 71c6e71 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 1103 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1104 | continue; |
| 1105 | } |
| 1106 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1107 | TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(), |
| 1108 | TemplateArgs, |
| 1109 | Base->getSourceRange().getBegin(), |
| 1110 | DeclarationName()); |
| 1111 | if (!BaseTypeLoc) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1112 | Invalid = true; |
| 1113 | continue; |
| 1114 | } |
| 1115 | |
| 1116 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1117 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1118 | Base->getSourceRange(), |
| 1119 | Base->isVirtual(), |
| 1120 | Base->getAccessSpecifierAsWritten(), |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1121 | BaseTypeLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1122 | InstantiatedBases.push_back(InstantiatedBase); |
| 1123 | else |
| 1124 | Invalid = true; |
| 1125 | } |
| 1126 | |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 1127 | if (!Invalid && |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1128 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1129 | InstantiatedBases.size())) |
| 1130 | Invalid = true; |
| 1131 | |
| 1132 | return Invalid; |
| 1133 | } |
| 1134 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1135 | /// \brief Instantiate the definition of a class from a given pattern. |
| 1136 | /// |
| 1137 | /// \param PointOfInstantiation The point of instantiation within the |
| 1138 | /// source code. |
| 1139 | /// |
| 1140 | /// \param Instantiation is the declaration whose definition is being |
| 1141 | /// instantiated. This will be either a class template specialization |
| 1142 | /// or a member class of a class template specialization. |
| 1143 | /// |
| 1144 | /// \param Pattern is the pattern from which the instantiation |
| 1145 | /// occurs. This will be either the declaration of a class template or |
| 1146 | /// the declaration of a member class of a class template. |
| 1147 | /// |
| 1148 | /// \param TemplateArgs The template arguments to be substituted into |
| 1149 | /// the pattern. |
| 1150 | /// |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1151 | /// \param TSK the kind of implicit or explicit instantiation to perform. |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1152 | /// |
| 1153 | /// \param Complain whether to complain if the class cannot be instantiated due |
| 1154 | /// to the lack of a definition. |
| 1155 | /// |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1156 | /// \returns true if an error occurred, false otherwise. |
| 1157 | bool |
| 1158 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 1159 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1160 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1161 | TemplateSpecializationKind TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1162 | bool Complain) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1163 | bool Invalid = false; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1164 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | CXXRecordDecl *PatternDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1166 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1167 | if (!PatternDef) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1168 | if (!Complain) { |
| 1169 | // Say nothing |
| 1170 | } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1171 | Diag(PointOfInstantiation, |
| 1172 | diag::err_implicit_instantiate_member_undefined) |
| 1173 | << Context.getTypeDeclType(Instantiation); |
| 1174 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 1175 | } else { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 1176 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1177 | << (TSK != TSK_ImplicitInstantiation) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1178 | << Context.getTypeDeclType(Instantiation); |
| 1179 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 1180 | } |
| 1181 | return true; |
| 1182 | } |
| 1183 | Pattern = PatternDef; |
| 1184 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 1185 | // \brief Record the point of instantiation. |
| 1186 | if (MemberSpecializationInfo *MSInfo |
| 1187 | = Instantiation->getMemberSpecializationInfo()) { |
| 1188 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1189 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1190 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1191 | = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { |
| 1192 | Spec->setTemplateSpecializationKind(TSK); |
| 1193 | Spec->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Douglas Gregor | d048bb7 | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 1196 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1197 | if (Inst) |
| 1198 | return true; |
| 1199 | |
| 1200 | // Enter the scope of this instantiation. We don't use |
| 1201 | // PushDeclContext because we don't have a scope. |
John McCall | f581382 | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 1202 | ContextRAII SavedContext(*this, Instantiation); |
Douglas Gregor | 9679caf | 2010-05-12 17:27:19 +0000 | [diff] [blame] | 1203 | EnterExpressionEvaluationContext EvalContext(*this, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1204 | Sema::PotentiallyEvaluated); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1205 | |
Douglas Gregor | 05030bb | 2010-03-24 01:33:17 +0000 | [diff] [blame] | 1206 | // If this is an instantiation of a local class, merge this local |
| 1207 | // instantiation scope with the enclosing scope. Otherwise, every |
| 1208 | // instantiation of a class has its own local instantiation scope. |
| 1209 | bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod(); |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1210 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Douglas Gregor | 05030bb | 2010-03-24 01:33:17 +0000 | [diff] [blame] | 1211 | |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 1212 | // Pull attributes from the pattern onto the instantiation. |
| 1213 | InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
| 1214 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1215 | // Start the definition of this instantiation. |
| 1216 | Instantiation->startDefinition(); |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1217 | |
| 1218 | Instantiation->setTagKind(Pattern->getTagKind()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1219 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1220 | // Do substitution on the base class specifiers. |
| 1221 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1222 | Invalid = true; |
| 1223 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1224 | llvm::SmallVector<Decl*, 4> Fields; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1225 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | MemberEnd = Pattern->decls_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1227 | Member != MemberEnd; ++Member) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1228 | Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1229 | if (NewMember) { |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 1230 | if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1231 | Fields.push_back(Field); |
Eli Friedman | 721e77d | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 1232 | else if (NewMember->isInvalidDecl()) |
| 1233 | Invalid = true; |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1234 | } else { |
| 1235 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1236 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 1237 | // 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] | 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | // Finish checking fields. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1242 | ActOnFields(0, Instantiation->getLocation(), Instantiation, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1243 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1244 | 0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1245 | CheckCompletedCXXClass(Instantiation); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1246 | if (Instantiation->isInvalidDecl()) |
| 1247 | Invalid = true; |
| 1248 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1249 | // Exit the scope of this instantiation. |
John McCall | f581382 | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 1250 | SavedContext.pop(); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1251 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1252 | if (!Invalid) { |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 1253 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 1254 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1255 | // Always emit the vtable for an explicit instantiation definition |
| 1256 | // of a polymorphic class template specialization. |
| 1257 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 1258 | MarkVTableUsed(PointOfInstantiation, Instantiation, true); |
| 1259 | } |
| 1260 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1261 | return Invalid; |
| 1262 | } |
| 1263 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | bool |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1265 | Sema::InstantiateClassTemplateSpecialization( |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1266 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1267 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1268 | TemplateSpecializationKind TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1269 | bool Complain) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1270 | // Perform the actual instantiation on the canonical declaration. |
| 1271 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 1272 | ClassTemplateSpec->getCanonicalDecl()); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1273 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1274 | // Check whether we have already instantiated or specialized this class |
| 1275 | // template specialization. |
| 1276 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) { |
| 1277 | if (ClassTemplateSpec->getSpecializationKind() == |
| 1278 | TSK_ExplicitInstantiationDeclaration && |
| 1279 | TSK == TSK_ExplicitInstantiationDefinition) { |
| 1280 | // An explicit instantiation definition follows an explicit instantiation |
| 1281 | // declaration (C++0x [temp.explicit]p10); go ahead and perform the |
| 1282 | // explicit instantiation. |
| 1283 | ClassTemplateSpec->setSpecializationKind(TSK); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1284 | |
| 1285 | // If this is an explicit instantiation definition, mark the |
| 1286 | // vtable as used. |
| 1287 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 1288 | MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true); |
| 1289 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1290 | return false; |
| 1291 | } |
| 1292 | |
| 1293 | // We can only instantiate something that hasn't already been |
| 1294 | // instantiated or specialized. Fail without any diagnostics: our |
| 1295 | // caller will provide an error message. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1296 | return true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1297 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1298 | |
Douglas Gregor | 9eea08b | 2009-09-15 16:51:42 +0000 | [diff] [blame] | 1299 | if (ClassTemplateSpec->isInvalidDecl()) |
| 1300 | return true; |
| 1301 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1302 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1303 | CXXRecordDecl *Pattern = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1304 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1305 | // C++ [temp.class.spec.match]p1: |
| 1306 | // When a class template is used in a context that requires an |
| 1307 | // instantiation of the class, it is necessary to determine |
| 1308 | // whether the instantiation is to be generated using the primary |
| 1309 | // template or one of the partial specializations. This is done by |
| 1310 | // matching the template arguments of the class template |
| 1311 | // specialization with the template argument lists of the partial |
| 1312 | // specializations. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1313 | typedef std::pair<ClassTemplatePartialSpecializationDecl *, |
| 1314 | TemplateArgumentList *> MatchResult; |
| 1315 | llvm::SmallVector<MatchResult, 4> Matched; |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 1316 | llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 1317 | Template->getPartialSpecializations(PartialSpecs); |
| 1318 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 1319 | ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1320 | TemplateDeductionInfo Info(Context, PointOfInstantiation); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1321 | if (TemplateDeductionResult Result |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 1322 | = DeduceTemplateArguments(Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1323 | ClassTemplateSpec->getTemplateArgs(), |
| 1324 | Info)) { |
| 1325 | // FIXME: Store the failed-deduction information for use in |
| 1326 | // diagnostics, later. |
| 1327 | (void)Result; |
| 1328 | } else { |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 1329 | Matched.push_back(std::make_pair(Partial, Info.take())); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1330 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1333 | if (Matched.size() >= 1) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1334 | llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1335 | if (Matched.size() == 1) { |
| 1336 | // -- If exactly one matching specialization is found, the |
| 1337 | // instantiation is generated from that specialization. |
| 1338 | // We don't need to do anything for this. |
| 1339 | } else { |
| 1340 | // -- If more than one matching specialization is found, the |
| 1341 | // partial order rules (14.5.4.2) are used to determine |
| 1342 | // whether one of the specializations is more specialized |
| 1343 | // than the others. If none of the specializations is more |
| 1344 | // specialized than all of the other matching |
| 1345 | // specializations, then the use of the class template is |
| 1346 | // ambiguous and the program is ill-formed. |
| 1347 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 1348 | PEnd = Matched.end(); |
| 1349 | P != PEnd; ++P) { |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1350 | if (getMoreSpecializedPartialSpecialization(P->first, Best->first, |
| 1351 | PointOfInstantiation) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1352 | == P->first) |
| 1353 | Best = P; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1354 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1355 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1356 | // Determine if the best partial specialization is more specialized than |
| 1357 | // the others. |
| 1358 | bool Ambiguous = false; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1359 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1360 | PEnd = Matched.end(); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1361 | P != PEnd; ++P) { |
| 1362 | if (P != Best && |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1363 | getMoreSpecializedPartialSpecialization(P->first, Best->first, |
| 1364 | PointOfInstantiation) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1365 | != Best->first) { |
| 1366 | Ambiguous = true; |
| 1367 | break; |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | if (Ambiguous) { |
| 1372 | // Partial ordering did not produce a clear winner. Complain. |
| 1373 | ClassTemplateSpec->setInvalidDecl(); |
| 1374 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 1375 | << ClassTemplateSpec; |
| 1376 | |
| 1377 | // Print the matching partial specializations. |
| 1378 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1379 | PEnd = Matched.end(); |
| 1380 | P != PEnd; ++P) |
| 1381 | Diag(P->first->getLocation(), diag::note_partial_spec_match) |
| 1382 | << getTemplateArgumentBindingsText(P->first->getTemplateParameters(), |
| 1383 | *P->second); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1384 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1385 | return true; |
| 1386 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | // Instantiate using the best class template partial specialization. |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1390 | ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first; |
| 1391 | while (OrigPartialSpec->getInstantiatedFromMember()) { |
| 1392 | // If we've found an explicit specialization of this class template, |
| 1393 | // stop here and use that as the pattern. |
| 1394 | if (OrigPartialSpec->isMemberSpecialization()) |
| 1395 | break; |
| 1396 | |
| 1397 | OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember(); |
| 1398 | } |
| 1399 | |
| 1400 | Pattern = OrigPartialSpec; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1401 | ClassTemplateSpec->setInstantiationOf(Best->first, Best->second); |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1402 | } else { |
| 1403 | // -- If no matches are found, the instantiation is generated |
| 1404 | // from the primary template. |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1405 | ClassTemplateDecl *OrigTemplate = Template; |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1406 | while (OrigTemplate->getInstantiatedFromMemberTemplate()) { |
| 1407 | // If we've found an explicit specialization of this class template, |
| 1408 | // stop here and use that as the pattern. |
| 1409 | if (OrigTemplate->isMemberSpecialization()) |
| 1410 | break; |
| 1411 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1412 | OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1415 | Pattern = OrigTemplate->getTemplatedDecl(); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1416 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1417 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1418 | bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec, |
| 1419 | Pattern, |
| 1420 | getTemplateInstantiationArgs(ClassTemplateSpec), |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1421 | TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1422 | Complain); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1424 | return Result; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1425 | } |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1426 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1427 | /// \brief Instantiates the definitions of all of the member |
| 1428 | /// of the given class, which is an instantiation of a class template |
| 1429 | /// or a member class of a template. |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1430 | void |
| 1431 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1432 | CXXRecordDecl *Instantiation, |
| 1433 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 1434 | TemplateSpecializationKind TSK) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1435 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(), |
| 1436 | DEnd = Instantiation->decls_end(); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1437 | D != DEnd; ++D) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1438 | bool SuppressNew = false; |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1439 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1440 | if (FunctionDecl *Pattern |
| 1441 | = Function->getInstantiatedFromMemberFunction()) { |
| 1442 | MemberSpecializationInfo *MSInfo |
| 1443 | = Function->getMemberSpecializationInfo(); |
| 1444 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 1445 | if (MSInfo->getTemplateSpecializationKind() |
| 1446 | == TSK_ExplicitSpecialization) |
| 1447 | continue; |
| 1448 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1449 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1450 | Function, |
| 1451 | MSInfo->getTemplateSpecializationKind(), |
| 1452 | MSInfo->getPointOfInstantiation(), |
| 1453 | SuppressNew) || |
| 1454 | SuppressNew) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1455 | continue; |
| 1456 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1457 | if (Function->hasBody()) |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1458 | continue; |
| 1459 | |
| 1460 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 1461 | // C++0x [temp.explicit]p8: |
| 1462 | // An explicit instantiation definition that names a class template |
| 1463 | // specialization explicitly instantiates the class template |
| 1464 | // specialization and is only an explicit instantiation definition |
| 1465 | // of members whose definition is visible at the point of |
| 1466 | // instantiation. |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1467 | if (!Pattern->hasBody()) |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1468 | continue; |
| 1469 | |
| 1470 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1471 | |
| 1472 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
| 1473 | } else { |
| 1474 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1475 | } |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1476 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1477 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1478 | if (Var->isStaticDataMember()) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1479 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 1480 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 1481 | if (MSInfo->getTemplateSpecializationKind() |
| 1482 | == TSK_ExplicitSpecialization) |
| 1483 | continue; |
| 1484 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1485 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1486 | Var, |
| 1487 | MSInfo->getTemplateSpecializationKind(), |
| 1488 | MSInfo->getPointOfInstantiation(), |
| 1489 | SuppressNew) || |
| 1490 | SuppressNew) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1491 | continue; |
| 1492 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1493 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 1494 | // C++0x [temp.explicit]p8: |
| 1495 | // An explicit instantiation definition that names a class template |
| 1496 | // specialization explicitly instantiates the class template |
| 1497 | // specialization and is only an explicit instantiation definition |
| 1498 | // of members whose definition is visible at the point of |
| 1499 | // instantiation. |
| 1500 | if (!Var->getInstantiatedFromStaticDataMember() |
| 1501 | ->getOutOfLineDefinition()) |
| 1502 | continue; |
| 1503 | |
| 1504 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1505 | InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1506 | } else { |
| 1507 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1508 | } |
| 1509 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1510 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
Douglas Gregor | a77eaa9 | 2010-04-18 18:11:38 +0000 | [diff] [blame] | 1511 | // Always skip the injected-class-name, along with any |
| 1512 | // redeclarations of nested classes, since both would cause us |
| 1513 | // to try to instantiate the members of a class twice. |
| 1514 | if (Record->isInjectedClassName() || Record->getPreviousDeclaration()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1515 | continue; |
| 1516 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1517 | MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); |
| 1518 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 1519 | |
| 1520 | if (MSInfo->getTemplateSpecializationKind() |
| 1521 | == TSK_ExplicitSpecialization) |
| 1522 | continue; |
| 1523 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1524 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1525 | Record, |
| 1526 | MSInfo->getTemplateSpecializationKind(), |
| 1527 | MSInfo->getPointOfInstantiation(), |
| 1528 | SuppressNew) || |
| 1529 | SuppressNew) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1530 | continue; |
| 1531 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1532 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 1533 | assert(Pattern && "Missing instantiated-from-template information"); |
| 1534 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1535 | if (!Record->getDefinition()) { |
| 1536 | if (!Pattern->getDefinition()) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1537 | // C++0x [temp.explicit]p8: |
| 1538 | // An explicit instantiation definition that names a class template |
| 1539 | // specialization explicitly instantiates the class template |
| 1540 | // specialization and is only an explicit instantiation definition |
| 1541 | // of members whose definition is visible at the point of |
| 1542 | // instantiation. |
| 1543 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 1544 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1545 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1546 | } |
| 1547 | |
| 1548 | continue; |
| 1549 | } |
| 1550 | |
| 1551 | InstantiateClass(PointOfInstantiation, Record, Pattern, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1552 | TemplateArgs, |
| 1553 | TSK); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1554 | } |
Douglas Gregor | e9374d5 | 2009-10-08 01:19:17 +0000 | [diff] [blame] | 1555 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1556 | Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1557 | if (Pattern) |
| 1558 | InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, |
| 1559 | TSK); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | /// \brief Instantiate the definitions of all of the members of the |
| 1565 | /// given class template specialization, which was named as part of an |
| 1566 | /// explicit instantiation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | void |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1568 | Sema::InstantiateClassTemplateSpecializationMembers( |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1569 | SourceLocation PointOfInstantiation, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1570 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 1571 | TemplateSpecializationKind TSK) { |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1572 | // C++0x [temp.explicit]p7: |
| 1573 | // An explicit instantiation that names a class template |
| 1574 | // specialization is an explicit instantion of the same kind |
| 1575 | // (declaration or definition) of each of its members (not |
| 1576 | // including members inherited from base classes) that has not |
| 1577 | // been previously explicitly specialized in the translation unit |
| 1578 | // containing the explicit instantiation, except as described |
| 1579 | // below. |
| 1580 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1581 | getTemplateInstantiationArgs(ClassTemplateSpec), |
| 1582 | TSK); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1585 | StmtResult |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1586 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 1587 | if (!S) |
| 1588 | return Owned(S); |
| 1589 | |
| 1590 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1591 | SourceLocation(), |
| 1592 | DeclarationName()); |
| 1593 | return Instantiator.TransformStmt(S); |
| 1594 | } |
| 1595 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1596 | ExprResult |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1597 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1598 | if (!E) |
| 1599 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1601 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1602 | SourceLocation(), |
| 1603 | DeclarationName()); |
| 1604 | return Instantiator.TransformExpr(E); |
| 1605 | } |
| 1606 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1607 | /// \brief Do template substitution on a nested-name-specifier. |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1608 | NestedNameSpecifier * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1609 | Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1610 | SourceRange Range, |
| 1611 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | dcee1a1 | 2009-08-06 05:28:30 +0000 | [diff] [blame] | 1612 | TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(), |
| 1613 | DeclarationName()); |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 1614 | return Instantiator.TransformNestedNameSpecifier(NNS, Range); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1615 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1616 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1617 | /// \brief Do template substitution on declaration name info. |
| 1618 | DeclarationNameInfo |
| 1619 | Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo, |
| 1620 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 1621 | TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(), |
| 1622 | NameInfo.getName()); |
| 1623 | return Instantiator.TransformDeclarationNameInfo(NameInfo); |
| 1624 | } |
| 1625 | |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1626 | TemplateName |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1627 | Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1628 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | d1067e5 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 1629 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 1630 | DeclarationName()); |
| 1631 | return Instantiator.TransformTemplateName(Name); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1632 | } |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1633 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1634 | bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output, |
| 1635 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 670444e | 2009-08-04 22:27:00 +0000 | [diff] [blame] | 1636 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 1637 | DeclarationName()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1638 | |
| 1639 | return Instantiator.TransformTemplateArgument(Input, Output); |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1640 | } |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1641 | |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1642 | Decl *LocalInstantiationScope::getInstantiationOf(const Decl *D) { |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1643 | for (LocalInstantiationScope *Current = this; Current; |
| 1644 | Current = Current->Outer) { |
| 1645 | // Check if we found something within this scope. |
| 1646 | llvm::DenseMap<const Decl *, Decl *>::iterator Found |
| 1647 | = Current->LocalDecls.find(D); |
| 1648 | if (Found != Current->LocalDecls.end()) |
| 1649 | return Found->second; |
| 1650 | |
| 1651 | // If we aren't combined with our outer scope, we're done. |
| 1652 | if (!Current->CombineWithOuterScope) |
| 1653 | break; |
| 1654 | } |
| 1655 | |
| 1656 | assert(D->isInvalidDecl() && |
| 1657 | "declaration was not instantiated in this scope!"); |
| 1658 | return 0; |
| 1659 | } |
| 1660 | |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1661 | void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) { |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1662 | Decl *&Stored = LocalDecls[D]; |
| 1663 | assert((!Stored || Stored == Inst)&& "Already instantiated this local"); |
| 1664 | Stored = Inst; |
| 1665 | } |