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