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