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