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