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