Douglas Gregor | fe1e110 | 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 | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/Expr.h" |
Douglas Gregor | fe1e110 | 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 | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===/ |
| 26 | // Template Instantiation Support |
| 27 | //===----------------------------------------------------------------------===/ |
| 28 | |
Douglas Gregor | 01afeef | 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 | 36d7c5f | 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 | 8c70253 | 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 | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 41 | MultiLevelTemplateArgumentList |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 42 | Sema::getTemplateInstantiationArgs(NamedDecl *D, |
Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 43 | const TemplateArgumentList *Innermost, |
| 44 | bool RelativeToPrimary) { |
Douglas Gregor | a654dd8 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 48 | if (Innermost) |
| 49 | Result.addOuterTemplateArguments(Innermost); |
| 50 | |
Douglas Gregor | a654dd8 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 55 | while (!Ctx->isFileContext()) { |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 56 | // Add template arguments from a class template instantiation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | a654dd8 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 63 | Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); |
Douglas Gregor | cf91555 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | } |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 71 | // Add template arguments from a function template specialization. |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 72 | else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) { |
Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 73 | if (!RelativeToPrimary && |
| 74 | Function->getTemplateSpecializationKind() |
| 75 | == TSK_ExplicitSpecialization) |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 76 | break; |
| 77 | |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 78 | if (const TemplateArgumentList *TemplateArgs |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 79 | = Function->getTemplateSpecializationArgs()) { |
| 80 | // Add the template arguments for this specialization. |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 81 | Result.addOuterTemplateArguments(TemplateArgs); |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | cf91555 | 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 | 970d530 | 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 | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 96 | RelativeToPrimary = false; |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 97 | continue; |
| 98 | } |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 99 | } |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 100 | |
| 101 | Ctx = Ctx->getParent(); |
Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 102 | RelativeToPrimary = false; |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 103 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 105 | return Result; |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Douglas Gregor | 84d49a2 | 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 | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 125 | Sema::InstantiatingTemplate:: |
| 126 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 127 | Decl *Entity, |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 128 | SourceRange InstantiationRange) |
| 129 | : SemaRef(SemaRef) { |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 130 | |
| 131 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 132 | InstantiationRange); |
| 133 | if (!Invalid) { |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 134 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 135 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 136 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 137 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | c922083 | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 138 | Inst.TemplateArgs = 0; |
| 139 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 140 | Inst.InstantiationRange = InstantiationRange; |
| 141 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 79cf603 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | Inst.Kind |
Douglas Gregor | 79cf603 | 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 | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 163 | Inst.InstantiationRange = InstantiationRange; |
| 164 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 169 | SourceLocation PointOfInstantiation, |
Douglas Gregor | ff6cbdf | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | ff6cbdf | 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 | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 188 | |
| 189 | if (!Inst.isInstantiationRecord()) |
| 190 | ++SemaRef.NonInstantiationEntries; |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 195 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 637d998 | 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 | 84d49a2 | 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 | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 218 | SourceLocation PointOfInstantiation, |
Anders Carlsson | 657bad4 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 225 | Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange); |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 226 | |
| 227 | if (!Invalid) { |
| 228 | ActiveTemplateInstantiation Inst; |
| 229 | Inst.Kind |
| 230 | = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 231 | Inst.PointOfInstantiation = PointOfInstantiation; |
Anders Carlsson | 657bad4 | 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 | e62e6a0 | 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 | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 247 | Invalid = false; |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 248 | |
Douglas Gregor | 84d49a2 | 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 | e62e6a0 | 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 | 84d49a2 | 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 | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | 84d49a2 | 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 | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 308 | void Sema::InstantiatingTemplate::Clear() { |
| 309 | if (!Invalid) { |
Douglas Gregor | 84d49a2 | 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 | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 315 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 316 | Invalid = true; |
| 317 | } |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 320 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 321 | SourceLocation PointOfInstantiation, |
| 322 | SourceRange InstantiationRange) { |
Douglas Gregor | 84d49a2 | 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 | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 328 | return false; |
| 329 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | SemaRef.Diag(PointOfInstantiation, |
Douglas Gregor | 79cf603 | 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 | 4ea568f | 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 | ff6cbdf | 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 | 4ea568f | 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 | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 348 | switch (Active->Kind) { |
| 349 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
Douglas Gregor | 8567358 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 356 | DiagID) |
| 357 | << Context.getTypeDeclType(Record) |
| 358 | << Active->InstantiationRange; |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 359 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 4adbc6d | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 366 | DiagID) |
| 367 | << Function |
| 368 | << Active->InstantiationRange; |
Douglas Gregor | a6ef8f0 | 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 | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 374 | } |
Douglas Gregor | 79cf603 | 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 | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 381 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | Active->TemplateArgs, |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 383 | Active->NumTemplateArgs, |
| 384 | Context.PrintingPolicy); |
Douglas Gregor | 79cf603 | 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 | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 392 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | FunctionTemplateDecl *FnTmpl |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 394 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 395 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 396 | diag::note_explicit_template_arg_substitution_here) |
| 397 | << FnTmpl << Active->InstantiationRange; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 398 | break; |
| 399 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Douglas Gregor | ff6cbdf | 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 | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 417 | |
Anders Carlsson | 657bad4 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 422 | std::string TemplateArgsStr |
| 423 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | Active->TemplateArgs, |
Anders Carlsson | 657bad4 | 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 | dc6d2c3 | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 429 | << (FD->getNameAsString() + TemplateArgsStr) |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 430 | << Active->InstantiationRange; |
| 431 | break; |
| 432 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Douglas Gregor | e62e6a0 | 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 | 84d49a2 | 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 | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 462 | } |
Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Douglas Gregor | 3383451 | 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 | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 472 | ++Active) |
| 473 | { |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 474 | switch(Active->Kind) { |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 475 | case ActiveTemplateInstantiation::TemplateInstantiation: |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 476 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: |
Douglas Gregor | ff6cbdf | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 480 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 481 | case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 482 | case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: |
Douglas Gregor | e62e6a0 | 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 | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 486 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Douglas Gregor | ff6cbdf | 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 | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
| 496 | return false; |
| 497 | } |
| 498 | |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 499 | //===----------------------------------------------------------------------===/ |
| 500 | // Template Instantiation for Types |
| 501 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 502 | namespace { |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 503 | class TemplateInstantiator |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | : public TreeTransform<TemplateInstantiator> { |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 505 | const MultiLevelTemplateArgumentList &TemplateArgs; |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 506 | SourceLocation Loc; |
| 507 | DeclarationName Entity; |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 508 | |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 509 | public: |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 510 | typedef TreeTransform<TemplateInstantiator> inherited; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | |
| 512 | TemplateInstantiator(Sema &SemaRef, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 513 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 514 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | DeclarationName Entity) |
| 516 | : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 517 | Entity(Entity) { } |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 518 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | d6ff332 | 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 | f61eca9 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 526 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Douglas Gregor | d6ff332 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | |
Douglas Gregor | d6ff332 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Douglas Gregor | ef6ab41 | 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 | d6ff332 | 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 | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 544 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | /// \brief Transform the definition of the given declaration by |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 546 | /// instantiating it. |
| 547 | Decl *TransformDefinition(Decl *D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
Douglas Gregor | a5cb6da | 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 | ebe1010 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 556 | TypeSourceInfo *Declarator, |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 557 | IdentifierInfo *Name, |
| 558 | SourceLocation Loc, SourceRange TypeRange); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | |
John McCall | 7f41d98 | 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 | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 564 | Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E); |
| 565 | Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E); |
John McCall | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 566 | Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 567 | Sema::OwningExprResult TransformTemplateParmRefExpr(DeclRefExpr *E, |
| 568 | NonTypeTemplateParmDecl *D); |
Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 569 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | /// \brief Transforms a template type parameter type by performing |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 571 | /// substitution of the corresponding template type argument. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 572 | QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| 573 | TemplateTypeParmTypeLoc TL); |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 574 | }; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 577 | Decl *TemplateInstantiator::TransformDecl(Decl *D) { |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 578 | if (!D) |
| 579 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 581 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 582 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | b9397108 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 583 | // If the corresponding template argument is NULL or non-existent, it's |
| 584 | // because we are performing instantiation from explicitly-specified |
| 585 | // template arguments in a function template, but there were some |
| 586 | // arguments left unspecified. |
| 587 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
| 588 | TTP->getPosition())) |
| 589 | return D; |
| 590 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 591 | TemplateName Template |
| 592 | = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate(); |
| 593 | assert(!Template.isNull() && Template.getAsTemplateDecl() && |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 594 | "Wrong kind of template template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 595 | return Template.getAsTemplateDecl(); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 596 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 598 | // Fall through to find the instantiated declaration for this template |
| 599 | // template parameter. |
Douglas Gregor | 71dc509 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 600 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 602 | return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs); |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 605 | Decl *TemplateInstantiator::TransformDefinition(Decl *D) { |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 606 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 607 | if (!Inst) |
| 608 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 610 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 611 | return Inst; |
| 612 | } |
| 613 | |
Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 614 | NamedDecl * |
| 615 | TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
| 616 | SourceLocation Loc) { |
| 617 | // If the first part of the nested-name-specifier was a template type |
| 618 | // parameter, instantiate that type parameter down to a tag type. |
| 619 | if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
| 620 | const TemplateTypeParmType *TTP |
| 621 | = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
| 622 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 623 | QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType(); |
| 624 | if (T.isNull()) |
| 625 | return cast_or_null<NamedDecl>(TransformDecl(D)); |
| 626 | |
| 627 | if (const TagType *Tag = T->getAs<TagType>()) |
| 628 | return Tag->getDecl(); |
| 629 | |
| 630 | // The resulting type is not a tag; complain. |
| 631 | getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
| 632 | return 0; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | return cast_or_null<NamedDecl>(TransformDecl(D)); |
| 637 | } |
| 638 | |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 639 | VarDecl * |
| 640 | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | QualType T, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 642 | TypeSourceInfo *Declarator, |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 643 | IdentifierInfo *Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | SourceLocation Loc, |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 645 | SourceRange TypeRange) { |
| 646 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator, |
| 647 | Name, Loc, TypeRange); |
| 648 | if (Var && !Var->isInvalidDecl()) |
| 649 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 650 | return Var; |
| 651 | } |
| 652 | |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 653 | QualType |
| 654 | TemplateInstantiator::RebuildElaboratedType(QualType T, |
| 655 | ElaboratedType::TagKind Tag) { |
| 656 | if (const TagType *TT = T->getAs<TagType>()) { |
| 657 | TagDecl* TD = TT->getDecl(); |
| 658 | |
| 659 | // FIXME: this location is very wrong; we really need typelocs. |
| 660 | SourceLocation TagLocation = TD->getTagKeywordLoc(); |
| 661 | |
| 662 | // FIXME: type might be anonymous. |
| 663 | IdentifierInfo *Id = TD->getIdentifier(); |
| 664 | |
| 665 | // TODO: should we even warn on struct/class mismatches for this? Seems |
| 666 | // like it's likely to produce a lot of spurious errors. |
| 667 | if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) { |
| 668 | SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) |
| 669 | << Id |
| 670 | << CodeModificationHint::CreateReplacement(SourceRange(TagLocation), |
| 671 | TD->getKindName()); |
| 672 | SemaRef.Diag(TD->getLocation(), diag::note_previous_use); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag); |
| 677 | } |
| 678 | |
| 679 | Sema::OwningExprResult |
John McCall | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 680 | TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 681 | if (!E->isTypeDependent()) |
| 682 | return SemaRef.Owned(E->Retain()); |
| 683 | |
| 684 | FunctionDecl *currentDecl = getSema().getCurFunctionDecl(); |
| 685 | assert(currentDecl && "Must have current function declaration when " |
| 686 | "instantiating."); |
| 687 | |
| 688 | PredefinedExpr::IdentType IT = E->getIdentType(); |
| 689 | |
| 690 | unsigned Length = |
| 691 | PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length(); |
| 692 | |
| 693 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 694 | QualType ResTy = getSema().Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 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 | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 703 | TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E, |
| 704 | NonTypeTemplateParmDecl *NTTP) { |
| 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 713 | const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(), |
| 714 | NTTP->getPosition()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | |
John McCall | 13481c5 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 721 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 722 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 723 | |
John McCall | 15dda37 | 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 | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 726 | VD = cast_or_null<ValueDecl>( |
Douglas Gregor | c95a1fa | 2009-11-04 07:01:15 +0000 | [diff] [blame] | 727 | getSema().FindInstantiatedDecl(VD, TemplateArgs)); |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 728 | if (!VD) |
| 729 | return SemaRef.ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
John McCall | 15dda37 | 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, |
| 734 | E->getLocation(), DeclarationName()); |
| 735 | assert(!TargetType.isNull() && "type substitution failed for param type"); |
| 736 | assert(!TargetType->isDependentType() && "param type still dependent"); |
| 737 | |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 738 | if (VD->getDeclContext()->isRecord() && |
| 739 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 740 | // If the value is a class member, we might have a pointer-to-member. |
| 741 | // Determine whether the non-type template template parameter is of |
| 742 | // pointer-to-member type. If so, we need to build an appropriate |
| 743 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 744 | // would refer to the member itself. |
John McCall | 15dda37 | 2010-02-06 10:23:53 +0000 | [diff] [blame^] | 745 | if (TargetType->isMemberPointerType()) { |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 746 | QualType ClassType |
| 747 | = SemaRef.Context.getTypeDeclType( |
Douglas Gregor | 4e948ce | 2009-11-12 17:40:13 +0000 | [diff] [blame] | 748 | cast<RecordDecl>(VD->getDeclContext())); |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 749 | NestedNameSpecifier *Qualifier |
| 750 | = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
| 751 | ClassType.getTypePtr()); |
| 752 | CXXScopeSpec SS; |
| 753 | SS.setScopeRep(Qualifier); |
| 754 | OwningExprResult RefExpr |
| 755 | = SemaRef.BuildDeclRefExpr(VD, |
| 756 | VD->getType().getNonReferenceType(), |
| 757 | E->getLocation(), |
| 758 | &SS); |
| 759 | if (RefExpr.isInvalid()) |
| 760 | return SemaRef.ExprError(); |
John McCall | 15dda37 | 2010-02-06 10:23:53 +0000 | [diff] [blame^] | 761 | |
| 762 | RefExpr = SemaRef.CreateBuiltinUnaryOp(E->getLocation(), |
| 763 | UnaryOperator::AddrOf, |
| 764 | move(RefExpr)); |
| 765 | assert(!RefExpr.isInvalid() && |
| 766 | SemaRef.Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
| 767 | TargetType)); |
| 768 | return move(RefExpr); |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 769 | } |
| 770 | } |
John McCall | 15dda37 | 2010-02-06 10:23:53 +0000 | [diff] [blame^] | 771 | |
| 772 | if (TargetType->isPointerType()) { |
| 773 | // C++03 [temp.arg.nontype]p5: |
| 774 | // - For a non-type template-parameter of type pointer to |
| 775 | // object, qualification conversions and the array-to-pointer |
| 776 | // conversion are applied. |
| 777 | // - For a non-type template-parameter of type pointer to |
| 778 | // function, only the function-to-pointer conversion is |
| 779 | // applied. |
| 780 | |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 781 | OwningExprResult RefExpr |
| 782 | = SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), |
| 783 | E->getLocation()); |
| 784 | if (RefExpr.isInvalid()) |
| 785 | return SemaRef.ExprError(); |
Chandler Carruth | 9b1fa25 | 2010-01-31 07:09:11 +0000 | [diff] [blame] | 786 | |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 787 | // Decay functions and arrays. |
| 788 | Expr *RefE = (Expr *)RefExpr.get(); |
| 789 | SemaRef.DefaultFunctionArrayConversion(RefE); |
| 790 | if (RefE != RefExpr.get()) { |
| 791 | RefExpr.release(); |
| 792 | RefExpr = SemaRef.Owned(RefE); |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 793 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
John McCall | 15dda37 | 2010-02-06 10:23:53 +0000 | [diff] [blame^] | 795 | // Qualification conversions. |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 796 | RefExpr.release(); |
John McCall | 15dda37 | 2010-02-06 10:23:53 +0000 | [diff] [blame^] | 797 | SemaRef.ImpCastExprToType(RefE, TargetType.getUnqualifiedType(), |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 798 | CastExpr::CK_NoOp); |
| 799 | return SemaRef.Owned(RefE); |
| 800 | } |
| 801 | |
John McCall | 15dda37 | 2010-02-06 10:23:53 +0000 | [diff] [blame^] | 802 | // FIXME: template parameters can add qualifiers to a reference. |
| 803 | |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 804 | return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), |
| 805 | E->getLocation()); |
| 806 | } |
| 807 | |
| 808 | assert(Arg.getKind() == TemplateArgument::Integral); |
| 809 | QualType T = Arg.getIntegralType(); |
| 810 | if (T->isCharType() || T->isWideCharType()) |
| 811 | return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral( |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 812 | Arg.getAsIntegral()->getZExtValue(), |
| 813 | T->isWideCharType(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 814 | T, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 815 | E->getSourceRange().getBegin())); |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 816 | if (T->isBooleanType()) |
| 817 | return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr( |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 818 | Arg.getAsIntegral()->getBoolValue(), |
| 819 | T, |
| 820 | E->getSourceRange().getBegin())); |
| 821 | |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 822 | assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T)); |
| 823 | return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral( |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 824 | *Arg.getAsIntegral(), |
| 825 | T, |
| 826 | E->getSourceRange().getBegin())); |
John McCall | 13481c5 | 2010-02-06 08:42:39 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | |
| 830 | Sema::OwningExprResult |
| 831 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
| 832 | NamedDecl *D = E->getDecl(); |
| 833 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
| 834 | if (NTTP->getDepth() < TemplateArgs.getNumLevels()) |
| 835 | return TransformTemplateParmRefExpr(E, NTTP); |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 836 | |
| 837 | // We have a non-type template parameter that isn't fully substituted; |
| 838 | // FindInstantiatedDecl will find it in the local instantiation scope. |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 839 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
John McCall | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 841 | return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E); |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 844 | Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr( |
John McCall | 47f29ea | 2009-12-08 09:21:05 +0000 | [diff] [blame] | 845 | CXXDefaultArgExpr *E) { |
Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 846 | assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())-> |
| 847 | getDescribedFunctionTemplate() && |
| 848 | "Default arg expressions are never formed in dependent cases."); |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 849 | return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(), |
| 850 | cast<FunctionDecl>(E->getParam()->getDeclContext()), |
| 851 | E->getParam()); |
Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | QualType |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 856 | TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| 857 | TemplateTypeParmTypeLoc TL) { |
| 858 | TemplateTypeParmType *T = TL.getTypePtr(); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 859 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 860 | // Replace the template type parameter with its corresponding |
| 861 | // template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 862 | |
| 863 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 864 | // because we are performing instantiation from explicitly-specified |
| 865 | // template arguments in a function template class, but there were some |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 866 | // arguments left unspecified. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 867 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
| 868 | TemplateTypeParmTypeLoc NewTL |
| 869 | = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
| 870 | NewTL.setNameLoc(TL.getNameLoc()); |
| 871 | return TL.getType(); |
| 872 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | |
| 874 | assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind() |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 875 | == TemplateArgument::Type && |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 876 | "Template argument kind mismatch"); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 877 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 878 | QualType Replacement |
| 879 | = TemplateArgs(T->getDepth(), T->getIndex()).getAsType(); |
| 880 | |
| 881 | // TODO: only do this uniquing once, at the start of instantiation. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 882 | QualType Result |
| 883 | = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); |
| 884 | SubstTemplateTypeParmTypeLoc NewTL |
| 885 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 886 | NewTL.setNameLoc(TL.getNameLoc()); |
| 887 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | } |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 889 | |
| 890 | // The template type parameter comes from an inner template (e.g., |
| 891 | // the template parameter list of a member template inside the |
| 892 | // template we are instantiating). Create a new template type |
| 893 | // parameter with the template "level" reduced by one. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 894 | QualType Result |
| 895 | = getSema().Context.getTemplateTypeParmType(T->getDepth() |
| 896 | - TemplateArgs.getNumLevels(), |
| 897 | T->getIndex(), |
| 898 | T->isParameterPack(), |
| 899 | T->getName()); |
| 900 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 901 | NewTL.setNameLoc(TL.getNameLoc()); |
| 902 | return Result; |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 903 | } |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 904 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 905 | /// \brief Perform substitution on the type T with a given set of template |
| 906 | /// arguments. |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 907 | /// |
| 908 | /// This routine substitutes the given template arguments into the |
| 909 | /// type T and produces the instantiated type. |
| 910 | /// |
| 911 | /// \param T the type into which the template arguments will be |
| 912 | /// substituted. If this type is not dependent, it will be returned |
| 913 | /// immediately. |
| 914 | /// |
| 915 | /// \param TemplateArgs the template arguments that will be |
| 916 | /// substituted for the top-level template parameters within T. |
| 917 | /// |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 918 | /// \param Loc the location in the source code where this substitution |
| 919 | /// is being performed. It will typically be the location of the |
| 920 | /// declarator (if we're instantiating the type of some declaration) |
| 921 | /// or the location of the type in the source code (if, e.g., we're |
| 922 | /// instantiating the type of a cast expression). |
| 923 | /// |
| 924 | /// \param Entity the name of the entity associated with a declaration |
| 925 | /// being instantiated (if any). May be empty to indicate that there |
| 926 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 927 | /// a cast expression) or that the entity has no name (e.g., an |
| 928 | /// unnamed function parameter). |
| 929 | /// |
| 930 | /// \returns If the instantiation succeeds, the instantiated |
| 931 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 932 | TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T, |
John McCall | 609459e | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 933 | const MultiLevelTemplateArgumentList &Args, |
| 934 | SourceLocation Loc, |
| 935 | DeclarationName Entity) { |
| 936 | assert(!ActiveTemplateInstantiations.empty() && |
| 937 | "Cannot perform an instantiation without some context on the " |
| 938 | "instantiation stack"); |
| 939 | |
| 940 | if (!T->getType()->isDependentType()) |
| 941 | return T; |
| 942 | |
| 943 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 944 | return Instantiator.TransformType(T); |
| 945 | } |
| 946 | |
| 947 | /// Deprecated form of the above. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | QualType Sema::SubstType(QualType T, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 949 | const MultiLevelTemplateArgumentList &TemplateArgs, |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 950 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 951 | assert(!ActiveTemplateInstantiations.empty() && |
| 952 | "Cannot perform an instantiation without some context on the " |
| 953 | "instantiation stack"); |
| 954 | |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 955 | // If T is not a dependent type, there is nothing to do. |
| 956 | if (!T->isDependentType()) |
| 957 | return T; |
| 958 | |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 959 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 960 | return Instantiator.TransformType(T); |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 961 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 962 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 963 | /// \brief Perform substitution on the base class specifiers of the |
| 964 | /// given class template specialization. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 965 | /// |
| 966 | /// Produces a diagnostic and returns true on error, returns false and |
| 967 | /// attaches the instantiated base classes to the class template |
| 968 | /// specialization if successful. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | bool |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 970 | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 971 | CXXRecordDecl *Pattern, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 972 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 973 | bool Invalid = false; |
Douglas Gregor | 6181ded | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 974 | llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | for (ClassTemplateSpecializationDecl::base_class_iterator |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 976 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | 2a72edd | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 977 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 978 | if (!Base->getType()->isDependentType()) { |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 979 | const CXXRecordDecl *BaseDecl = |
| 980 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 981 | |
| 982 | // Make sure to set the attributes from the base. |
| 983 | SetClassDeclAttributesFromBase(Instantiation, BaseDecl, |
| 984 | Base->isVirtual()); |
| 985 | |
Fariborz Jahanian | 5c14ec3 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 986 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 987 | continue; |
| 988 | } |
| 989 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 990 | QualType BaseType = SubstType(Base->getType(), |
| 991 | TemplateArgs, |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 992 | Base->getSourceRange().getBegin(), |
| 993 | DeclarationName()); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 994 | if (BaseType.isNull()) { |
| 995 | Invalid = true; |
| 996 | continue; |
| 997 | } |
| 998 | |
| 999 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1000 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1001 | Base->getSourceRange(), |
| 1002 | Base->isVirtual(), |
| 1003 | Base->getAccessSpecifierAsWritten(), |
| 1004 | BaseType, |
| 1005 | /*FIXME: Not totally accurate */ |
| 1006 | Base->getSourceRange().getBegin())) |
| 1007 | InstantiatedBases.push_back(InstantiatedBase); |
| 1008 | else |
| 1009 | Invalid = true; |
| 1010 | } |
| 1011 | |
Douglas Gregor | 2a72edd | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 1012 | if (!Invalid && |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1013 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1014 | InstantiatedBases.size())) |
| 1015 | Invalid = true; |
| 1016 | |
| 1017 | return Invalid; |
| 1018 | } |
| 1019 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1020 | /// \brief Instantiate the definition of a class from a given pattern. |
| 1021 | /// |
| 1022 | /// \param PointOfInstantiation The point of instantiation within the |
| 1023 | /// source code. |
| 1024 | /// |
| 1025 | /// \param Instantiation is the declaration whose definition is being |
| 1026 | /// instantiated. This will be either a class template specialization |
| 1027 | /// or a member class of a class template specialization. |
| 1028 | /// |
| 1029 | /// \param Pattern is the pattern from which the instantiation |
| 1030 | /// occurs. This will be either the declaration of a class template or |
| 1031 | /// the declaration of a member class of a class template. |
| 1032 | /// |
| 1033 | /// \param TemplateArgs The template arguments to be substituted into |
| 1034 | /// the pattern. |
| 1035 | /// |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1036 | /// \param TSK the kind of implicit or explicit instantiation to perform. |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1037 | /// |
| 1038 | /// \param Complain whether to complain if the class cannot be instantiated due |
| 1039 | /// to the lack of a definition. |
| 1040 | /// |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1041 | /// \returns true if an error occurred, false otherwise. |
| 1042 | bool |
| 1043 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 1044 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1045 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1046 | TemplateSpecializationKind TSK, |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1047 | bool Complain) { |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1048 | bool Invalid = false; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1049 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | CXXRecordDecl *PatternDef |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1051 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 1052 | if (!PatternDef) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1053 | if (!Complain) { |
| 1054 | // Say nothing |
| 1055 | } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1056 | Diag(PointOfInstantiation, |
| 1057 | diag::err_implicit_instantiate_member_undefined) |
| 1058 | << Context.getTypeDeclType(Instantiation); |
| 1059 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 1060 | } else { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 1061 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1062 | << (TSK != TSK_ImplicitInstantiation) |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1063 | << Context.getTypeDeclType(Instantiation); |
| 1064 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 1065 | } |
| 1066 | return true; |
| 1067 | } |
| 1068 | Pattern = PatternDef; |
| 1069 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 1070 | // \brief Record the point of instantiation. |
| 1071 | if (MemberSpecializationInfo *MSInfo |
| 1072 | = Instantiation->getMemberSpecializationInfo()) { |
| 1073 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1074 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1075 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1076 | = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { |
| 1077 | Spec->setTemplateSpecializationKind(TSK); |
| 1078 | Spec->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Douglas Gregor | f3430ae | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 1081 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1082 | if (Inst) |
| 1083 | return true; |
| 1084 | |
| 1085 | // Enter the scope of this instantiation. We don't use |
| 1086 | // PushDeclContext because we don't have a scope. |
| 1087 | DeclContext *PreviousContext = CurContext; |
| 1088 | CurContext = Instantiation; |
| 1089 | |
| 1090 | // Start the definition of this instantiation. |
| 1091 | Instantiation->startDefinition(); |
| 1092 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1093 | // Do substitution on the base class specifiers. |
| 1094 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1095 | Invalid = true; |
| 1096 | |
Douglas Gregor | 6181ded | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 1097 | llvm::SmallVector<DeclPtrTy, 4> Fields; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1098 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | MemberEnd = Pattern->decls_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1100 | Member != MemberEnd; ++Member) { |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1101 | Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1102 | if (NewMember) { |
Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 1103 | if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1104 | Fields.push_back(DeclPtrTy::make(Field)); |
Eli Friedman | d0e8de2 | 2009-12-07 00:22:08 +0000 | [diff] [blame] | 1105 | else if (NewMember->isInvalidDecl()) |
| 1106 | Invalid = true; |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1107 | } else { |
| 1108 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1109 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 1110 | // true. For now, we expect to skip some members that we can't yet handle. |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | // Finish checking fields. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1115 | ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation), |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1116 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1117 | 0); |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 1118 | CheckCompletedCXXClass(Instantiation); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1119 | if (Instantiation->isInvalidDecl()) |
| 1120 | Invalid = true; |
| 1121 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1122 | // Exit the scope of this instantiation. |
| 1123 | CurContext = PreviousContext; |
| 1124 | |
Douglas Gregor | 0a0f04d | 2010-01-06 04:44:19 +0000 | [diff] [blame] | 1125 | // If this is a polymorphic C++ class without a key function, we'll |
| 1126 | // have to mark all of the virtual members to allow emission of a vtable |
| 1127 | // in this translation unit. |
| 1128 | if (Instantiation->isDynamicClass() && !Context.getKeyFunction(Instantiation)) |
| 1129 | ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Instantiation, |
| 1130 | PointOfInstantiation)); |
| 1131 | |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 1132 | if (!Invalid) |
| 1133 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 1134 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1135 | return Invalid; |
| 1136 | } |
| 1137 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | bool |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1139 | Sema::InstantiateClassTemplateSpecialization( |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1140 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1141 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1142 | TemplateSpecializationKind TSK, |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1143 | bool Complain) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1144 | // Perform the actual instantiation on the canonical declaration. |
| 1145 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 1146 | ClassTemplateSpec->getCanonicalDecl()); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1147 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1148 | // Check whether we have already instantiated or specialized this class |
| 1149 | // template specialization. |
| 1150 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) { |
| 1151 | if (ClassTemplateSpec->getSpecializationKind() == |
| 1152 | TSK_ExplicitInstantiationDeclaration && |
| 1153 | TSK == TSK_ExplicitInstantiationDefinition) { |
| 1154 | // An explicit instantiation definition follows an explicit instantiation |
| 1155 | // declaration (C++0x [temp.explicit]p10); go ahead and perform the |
| 1156 | // explicit instantiation. |
| 1157 | ClassTemplateSpec->setSpecializationKind(TSK); |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1158 | return false; |
| 1159 | } |
| 1160 | |
| 1161 | // We can only instantiate something that hasn't already been |
| 1162 | // instantiated or specialized. Fail without any diagnostics: our |
| 1163 | // caller will provide an error message. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1164 | return true; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1165 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1166 | |
Douglas Gregor | 00a511f | 2009-09-15 16:51:42 +0000 | [diff] [blame] | 1167 | if (ClassTemplateSpec->isInvalidDecl()) |
| 1168 | return true; |
| 1169 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1170 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1171 | CXXRecordDecl *Pattern = 0; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1172 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1173 | // C++ [temp.class.spec.match]p1: |
| 1174 | // When a class template is used in a context that requires an |
| 1175 | // instantiation of the class, it is necessary to determine |
| 1176 | // whether the instantiation is to be generated using the primary |
| 1177 | // template or one of the partial specializations. This is done by |
| 1178 | // matching the template arguments of the class template |
| 1179 | // specialization with the template argument lists of the partial |
| 1180 | // specializations. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1181 | typedef std::pair<ClassTemplatePartialSpecializationDecl *, |
| 1182 | TemplateArgumentList *> MatchResult; |
| 1183 | llvm::SmallVector<MatchResult, 4> Matched; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1185 | Partial = Template->getPartialSpecializations().begin(), |
| 1186 | PartialEnd = Template->getPartialSpecializations().end(); |
| 1187 | Partial != PartialEnd; |
| 1188 | ++Partial) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1189 | TemplateDeductionInfo Info(Context); |
| 1190 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | = DeduceTemplateArguments(&*Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1192 | ClassTemplateSpec->getTemplateArgs(), |
| 1193 | Info)) { |
| 1194 | // FIXME: Store the failed-deduction information for use in |
| 1195 | // diagnostics, later. |
| 1196 | (void)Result; |
| 1197 | } else { |
| 1198 | Matched.push_back(std::make_pair(&*Partial, Info.take())); |
| 1199 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1202 | if (Matched.size() >= 1) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1203 | llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1204 | if (Matched.size() == 1) { |
| 1205 | // -- If exactly one matching specialization is found, the |
| 1206 | // instantiation is generated from that specialization. |
| 1207 | // We don't need to do anything for this. |
| 1208 | } else { |
| 1209 | // -- If more than one matching specialization is found, the |
| 1210 | // partial order rules (14.5.4.2) are used to determine |
| 1211 | // whether one of the specializations is more specialized |
| 1212 | // than the others. If none of the specializations is more |
| 1213 | // specialized than all of the other matching |
| 1214 | // specializations, then the use of the class template is |
| 1215 | // ambiguous and the program is ill-formed. |
| 1216 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 1217 | PEnd = Matched.end(); |
| 1218 | P != PEnd; ++P) { |
| 1219 | if (getMoreSpecializedPartialSpecialization(P->first, Best->first) |
| 1220 | == P->first) |
| 1221 | Best = P; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1222 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1223 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1224 | // Determine if the best partial specialization is more specialized than |
| 1225 | // the others. |
| 1226 | bool Ambiguous = false; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1227 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1228 | PEnd = Matched.end(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1229 | P != PEnd; ++P) { |
| 1230 | if (P != Best && |
| 1231 | getMoreSpecializedPartialSpecialization(P->first, Best->first) |
| 1232 | != Best->first) { |
| 1233 | Ambiguous = true; |
| 1234 | break; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | if (Ambiguous) { |
| 1239 | // Partial ordering did not produce a clear winner. Complain. |
| 1240 | ClassTemplateSpec->setInvalidDecl(); |
| 1241 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 1242 | << ClassTemplateSpec; |
| 1243 | |
| 1244 | // Print the matching partial specializations. |
| 1245 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1246 | PEnd = Matched.end(); |
| 1247 | P != PEnd; ++P) |
| 1248 | Diag(P->first->getLocation(), diag::note_partial_spec_match) |
| 1249 | << getTemplateArgumentBindingsText(P->first->getTemplateParameters(), |
| 1250 | *P->second); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1251 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1252 | return true; |
| 1253 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | // Instantiate using the best class template partial specialization. |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1257 | ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first; |
| 1258 | while (OrigPartialSpec->getInstantiatedFromMember()) { |
| 1259 | // If we've found an explicit specialization of this class template, |
| 1260 | // stop here and use that as the pattern. |
| 1261 | if (OrigPartialSpec->isMemberSpecialization()) |
| 1262 | break; |
| 1263 | |
| 1264 | OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember(); |
| 1265 | } |
| 1266 | |
| 1267 | Pattern = OrigPartialSpec; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1268 | ClassTemplateSpec->setInstantiationOf(Best->first, Best->second); |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1269 | } else { |
| 1270 | // -- If no matches are found, the instantiation is generated |
| 1271 | // from the primary template. |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1272 | ClassTemplateDecl *OrigTemplate = Template; |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1273 | while (OrigTemplate->getInstantiatedFromMemberTemplate()) { |
| 1274 | // If we've found an explicit specialization of this class template, |
| 1275 | // stop here and use that as the pattern. |
| 1276 | if (OrigTemplate->isMemberSpecialization()) |
| 1277 | break; |
| 1278 | |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1279 | OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1282 | Pattern = OrigTemplate->getTemplatedDecl(); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1283 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1284 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1285 | bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec, |
| 1286 | Pattern, |
| 1287 | getTemplateInstantiationArgs(ClassTemplateSpec), |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1288 | TSK, |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1289 | Complain); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1291 | for (unsigned I = 0, N = Matched.size(); I != N; ++I) { |
| 1292 | // FIXME: Implement TemplateArgumentList::Destroy! |
| 1293 | // if (Matched[I].first != Pattern) |
| 1294 | // Matched[I].second->Destroy(Context); |
| 1295 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1297 | return Result; |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1298 | } |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1299 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1300 | /// \brief Instantiates the definitions of all of the member |
| 1301 | /// of the given class, which is an instantiation of a class template |
| 1302 | /// or a member class of a template. |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1303 | void |
| 1304 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1305 | CXXRecordDecl *Instantiation, |
| 1306 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 1307 | TemplateSpecializationKind TSK) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1308 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(), |
| 1309 | DEnd = Instantiation->decls_end(); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1310 | D != DEnd; ++D) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1311 | bool SuppressNew = false; |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1312 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1313 | if (FunctionDecl *Pattern |
| 1314 | = Function->getInstantiatedFromMemberFunction()) { |
| 1315 | MemberSpecializationInfo *MSInfo |
| 1316 | = Function->getMemberSpecializationInfo(); |
| 1317 | assert(MSInfo && "No member specialization information?"); |
| 1318 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1319 | Function, |
| 1320 | MSInfo->getTemplateSpecializationKind(), |
| 1321 | MSInfo->getPointOfInstantiation(), |
| 1322 | SuppressNew) || |
| 1323 | SuppressNew) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1324 | continue; |
| 1325 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1326 | if (Function->getBody()) |
| 1327 | continue; |
| 1328 | |
| 1329 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 1330 | // C++0x [temp.explicit]p8: |
| 1331 | // An explicit instantiation definition that names a class template |
| 1332 | // specialization explicitly instantiates the class template |
| 1333 | // specialization and is only an explicit instantiation definition |
| 1334 | // of members whose definition is visible at the point of |
| 1335 | // instantiation. |
| 1336 | if (!Pattern->getBody()) |
| 1337 | continue; |
| 1338 | |
| 1339 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1340 | |
| 1341 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
| 1342 | } else { |
| 1343 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1344 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1345 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1346 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1347 | if (Var->isStaticDataMember()) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1348 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 1349 | assert(MSInfo && "No member specialization information?"); |
| 1350 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1351 | Var, |
| 1352 | MSInfo->getTemplateSpecializationKind(), |
| 1353 | MSInfo->getPointOfInstantiation(), |
| 1354 | SuppressNew) || |
| 1355 | SuppressNew) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1356 | continue; |
| 1357 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1358 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 1359 | // C++0x [temp.explicit]p8: |
| 1360 | // An explicit instantiation definition that names a class template |
| 1361 | // specialization explicitly instantiates the class template |
| 1362 | // specialization and is only an explicit instantiation definition |
| 1363 | // of members whose definition is visible at the point of |
| 1364 | // instantiation. |
| 1365 | if (!Var->getInstantiatedFromStaticDataMember() |
| 1366 | ->getOutOfLineDefinition()) |
| 1367 | continue; |
| 1368 | |
| 1369 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1370 | InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1371 | } else { |
| 1372 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1373 | } |
| 1374 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1375 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1376 | if (Record->isInjectedClassName()) |
| 1377 | continue; |
| 1378 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1379 | MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); |
| 1380 | assert(MSInfo && "No member specialization information?"); |
| 1381 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1382 | Record, |
| 1383 | MSInfo->getTemplateSpecializationKind(), |
| 1384 | MSInfo->getPointOfInstantiation(), |
| 1385 | SuppressNew) || |
| 1386 | SuppressNew) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1387 | continue; |
| 1388 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1389 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 1390 | assert(Pattern && "Missing instantiated-from-template information"); |
| 1391 | |
| 1392 | if (!Record->getDefinition(Context)) { |
| 1393 | if (!Pattern->getDefinition(Context)) { |
| 1394 | // C++0x [temp.explicit]p8: |
| 1395 | // An explicit instantiation definition that names a class template |
| 1396 | // specialization explicitly instantiates the class template |
| 1397 | // specialization and is only an explicit instantiation definition |
| 1398 | // of members whose definition is visible at the point of |
| 1399 | // instantiation. |
| 1400 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 1401 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1402 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1403 | } |
| 1404 | |
| 1405 | continue; |
| 1406 | } |
| 1407 | |
| 1408 | InstantiateClass(PointOfInstantiation, Record, Pattern, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1409 | TemplateArgs, |
| 1410 | TSK); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1411 | } |
Douglas Gregor | c093c1d | 2009-10-08 01:19:17 +0000 | [diff] [blame] | 1412 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1413 | Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context)); |
| 1414 | if (Pattern) |
| 1415 | InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, |
| 1416 | TSK); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | /// \brief Instantiate the definitions of all of the members of the |
| 1422 | /// given class template specialization, which was named as part of an |
| 1423 | /// explicit instantiation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | void |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1425 | Sema::InstantiateClassTemplateSpecializationMembers( |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1426 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1427 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 1428 | TemplateSpecializationKind TSK) { |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1429 | // C++0x [temp.explicit]p7: |
| 1430 | // An explicit instantiation that names a class template |
| 1431 | // specialization is an explicit instantion of the same kind |
| 1432 | // (declaration or definition) of each of its members (not |
| 1433 | // including members inherited from base classes) that has not |
| 1434 | // been previously explicitly specialized in the translation unit |
| 1435 | // containing the explicit instantiation, except as described |
| 1436 | // below. |
| 1437 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1438 | getTemplateInstantiationArgs(ClassTemplateSpec), |
| 1439 | TSK); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | Sema::OwningStmtResult |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1443 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 1444 | if (!S) |
| 1445 | return Owned(S); |
| 1446 | |
| 1447 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1448 | SourceLocation(), |
| 1449 | DeclarationName()); |
| 1450 | return Instantiator.TransformStmt(S); |
| 1451 | } |
| 1452 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | Sema::OwningExprResult |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1454 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1455 | if (!E) |
| 1456 | return Owned(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1458 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1459 | SourceLocation(), |
| 1460 | DeclarationName()); |
| 1461 | return Instantiator.TransformExpr(E); |
| 1462 | } |
| 1463 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1464 | /// \brief Do template substitution on a nested-name-specifier. |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1465 | NestedNameSpecifier * |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1466 | Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1467 | SourceRange Range, |
| 1468 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 1135c35 | 2009-08-06 05:28:30 +0000 | [diff] [blame] | 1469 | TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(), |
| 1470 | DeclarationName()); |
| 1471 | return Instantiator.TransformNestedNameSpecifier(NNS, Range); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1472 | } |
Douglas Gregor | aa59489 | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1473 | |
| 1474 | TemplateName |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1475 | Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1476 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 71dc509 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 1477 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 1478 | DeclarationName()); |
| 1479 | return Instantiator.TransformTemplateName(Name); |
Douglas Gregor | aa59489 | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1480 | } |
Douglas Gregor | c43620d | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1481 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1482 | bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output, |
| 1483 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | e922c77 | 2009-08-04 22:27:00 +0000 | [diff] [blame] | 1484 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 1485 | DeclarationName()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1486 | |
| 1487 | return Instantiator.TransformTemplateArgument(Input, Output); |
Douglas Gregor | c43620d | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1488 | } |