Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template instantiation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | |
| 13 | #include "Sema.h" |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Expr.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
| 19 | #include "clang/Parse/DeclSpec.h" |
| 20 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===/ |
| 26 | // Template Instantiation Support |
| 27 | //===----------------------------------------------------------------------===/ |
| 28 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 29 | /// \brief Retrieve the template argument list(s) that should be used to |
| 30 | /// instantiate the definition of the given declaration. |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 31 | MultiLevelTemplateArgumentList |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 32 | Sema::getTemplateInstantiationArgs(NamedDecl *D) { |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 33 | // Accumulate the set of template argument lists in this structure. |
| 34 | MultiLevelTemplateArgumentList Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 36 | DeclContext *Ctx = dyn_cast<DeclContext>(D); |
| 37 | if (!Ctx) |
| 38 | Ctx = D->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 40 | while (!Ctx->isFileContext()) { |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 41 | // Add template arguments from a class template instantiation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 43 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { |
| 44 | // We're done when we hit an explicit specialization. |
| 45 | if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization) |
| 46 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 48 | Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 49 | |
| 50 | // If this class template specialization was instantiated from a |
| 51 | // specialized member that is a class template, we're done. |
| 52 | assert(Spec->getSpecializedTemplate() && "No class template?"); |
| 53 | if (Spec->getSpecializedTemplate()->isMemberSpecialization()) |
| 54 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | } |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 56 | // Add template arguments from a function template specialization. |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 57 | else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) { |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 58 | if (Function->getTemplateSpecializationKind() |
| 59 | == TSK_ExplicitSpecialization) |
| 60 | break; |
| 61 | |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 62 | if (const TemplateArgumentList *TemplateArgs |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 63 | = Function->getTemplateSpecializationArgs()) { |
| 64 | // Add the template arguments for this specialization. |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 65 | Result.addOuterTemplateArguments(TemplateArgs); |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 66 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 67 | // If this function was instantiated from a specialized member that is |
| 68 | // a function template, we're done. |
| 69 | assert(Function->getPrimaryTemplate() && "No function template?"); |
| 70 | if (Function->getPrimaryTemplate()->isMemberSpecialization()) |
| 71 | break; |
| 72 | } |
| 73 | |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 74 | // If this is a friend declaration and it declares an entity at |
| 75 | // namespace scope, take arguments from its lexical parent |
| 76 | // instead of its semantic parent. |
| 77 | if (Function->getFriendObjectKind() && |
| 78 | Function->getDeclContext()->isFileContext()) { |
| 79 | Ctx = Function->getLexicalDeclContext(); |
| 80 | continue; |
| 81 | } |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 82 | } |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 83 | |
| 84 | Ctx = Ctx->getParent(); |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 85 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Douglas Gregor | d110243 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 87 | return Result; |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 90 | Sema::InstantiatingTemplate:: |
| 91 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 92 | Decl *Entity, |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 93 | SourceRange InstantiationRange) |
| 94 | : SemaRef(SemaRef) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 95 | |
| 96 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 97 | InstantiationRange); |
| 98 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 99 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 100 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 101 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 102 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 103 | Inst.TemplateArgs = 0; |
| 104 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 105 | Inst.InstantiationRange = InstantiationRange; |
| 106 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 107 | Invalid = false; |
| 108 | } |
| 109 | } |
| 110 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 112 | SourceLocation PointOfInstantiation, |
| 113 | TemplateDecl *Template, |
| 114 | const TemplateArgument *TemplateArgs, |
| 115 | unsigned NumTemplateArgs, |
| 116 | SourceRange InstantiationRange) |
| 117 | : SemaRef(SemaRef) { |
| 118 | |
| 119 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 120 | InstantiationRange); |
| 121 | if (!Invalid) { |
| 122 | ActiveTemplateInstantiation Inst; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | Inst.Kind |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 124 | = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; |
| 125 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 126 | Inst.Entity = reinterpret_cast<uintptr_t>(Template); |
| 127 | Inst.TemplateArgs = TemplateArgs; |
| 128 | Inst.NumTemplateArgs = NumTemplateArgs; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 129 | Inst.InstantiationRange = InstantiationRange; |
| 130 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 131 | Invalid = false; |
| 132 | } |
| 133 | } |
| 134 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 136 | SourceLocation PointOfInstantiation, |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 137 | FunctionTemplateDecl *FunctionTemplate, |
| 138 | const TemplateArgument *TemplateArgs, |
| 139 | unsigned NumTemplateArgs, |
| 140 | ActiveTemplateInstantiation::InstantiationKind Kind, |
| 141 | SourceRange InstantiationRange) |
| 142 | : SemaRef(SemaRef) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 144 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 145 | InstantiationRange); |
| 146 | if (!Invalid) { |
| 147 | ActiveTemplateInstantiation Inst; |
| 148 | Inst.Kind = Kind; |
| 149 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 150 | Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate); |
| 151 | Inst.TemplateArgs = TemplateArgs; |
| 152 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 153 | Inst.InstantiationRange = InstantiationRange; |
| 154 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 155 | Invalid = false; |
| 156 | } |
| 157 | } |
| 158 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 160 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 161 | ClassTemplatePartialSpecializationDecl *PartialSpec, |
| 162 | const TemplateArgument *TemplateArgs, |
| 163 | unsigned NumTemplateArgs, |
| 164 | SourceRange InstantiationRange) |
| 165 | : SemaRef(SemaRef) { |
| 166 | |
| 167 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 168 | InstantiationRange); |
| 169 | if (!Invalid) { |
| 170 | ActiveTemplateInstantiation Inst; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | Inst.Kind |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 172 | = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 173 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 174 | Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec); |
| 175 | Inst.TemplateArgs = TemplateArgs; |
| 176 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 177 | Inst.InstantiationRange = InstantiationRange; |
| 178 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 179 | Invalid = false; |
| 180 | } |
| 181 | } |
| 182 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 184 | SourceLocation PointOfInstantation, |
| 185 | ParmVarDecl *Param, |
| 186 | const TemplateArgument *TemplateArgs, |
| 187 | unsigned NumTemplateArgs, |
| 188 | SourceRange InstantiationRange) |
| 189 | : SemaRef(SemaRef) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 191 | Invalid = CheckInstantiationDepth(PointOfInstantation, InstantiationRange); |
| 192 | |
| 193 | if (!Invalid) { |
| 194 | ActiveTemplateInstantiation Inst; |
| 195 | Inst.Kind |
| 196 | = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; |
| 197 | Inst.PointOfInstantiation = PointOfInstantation; |
| 198 | Inst.Entity = reinterpret_cast<uintptr_t>(Param); |
| 199 | Inst.TemplateArgs = TemplateArgs; |
| 200 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 201 | Inst.InstantiationRange = InstantiationRange; |
| 202 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 203 | Invalid = false; |
| 204 | } |
| 205 | } |
| 206 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 207 | void Sema::InstantiatingTemplate::Clear() { |
| 208 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 209 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 210 | Invalid = true; |
| 211 | } |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 214 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 215 | SourceLocation PointOfInstantiation, |
| 216 | SourceRange InstantiationRange) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | if (SemaRef.ActiveTemplateInstantiations.size() |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 218 | <= SemaRef.getLangOptions().InstantiationDepth) |
| 219 | return false; |
| 220 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | SemaRef.Diag(PointOfInstantiation, |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 222 | diag::err_template_recursion_depth_exceeded) |
| 223 | << SemaRef.getLangOptions().InstantiationDepth |
| 224 | << InstantiationRange; |
| 225 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
| 226 | << SemaRef.getLangOptions().InstantiationDepth; |
| 227 | return true; |
| 228 | } |
| 229 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 230 | /// \brief Prints the current instantiation stack through a series of |
| 231 | /// notes. |
| 232 | void Sema::PrintInstantiationStack() { |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 233 | // FIXME: In all of these cases, we need to show the template arguments |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 234 | for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator |
| 235 | Active = ActiveTemplateInstantiations.rbegin(), |
| 236 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 237 | Active != ActiveEnd; |
| 238 | ++Active) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 239 | switch (Active->Kind) { |
| 240 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 241 | Decl *D = reinterpret_cast<Decl *>(Active->Entity); |
| 242 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 243 | unsigned DiagID = diag::note_template_member_class_here; |
| 244 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 245 | DiagID = diag::note_template_class_instantiation_here; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 247 | DiagID) |
| 248 | << Context.getTypeDeclType(Record) |
| 249 | << Active->InstantiationRange; |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 250 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 251 | unsigned DiagID; |
| 252 | if (Function->getPrimaryTemplate()) |
| 253 | DiagID = diag::note_function_template_spec_here; |
| 254 | else |
| 255 | DiagID = diag::note_template_member_function_here; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 257 | DiagID) |
| 258 | << Function |
| 259 | << Active->InstantiationRange; |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 260 | } else { |
| 261 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 262 | diag::note_template_static_data_member_def_here) |
| 263 | << cast<VarDecl>(D) |
| 264 | << Active->InstantiationRange; |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 265 | } |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | |
| 269 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { |
| 270 | TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); |
| 271 | std::string TemplateArgsStr |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 272 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | Active->TemplateArgs, |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 274 | Active->NumTemplateArgs, |
| 275 | Context.PrintingPolicy); |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 276 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 277 | diag::note_default_arg_instantiation_here) |
| 278 | << (Template->getNameAsString() + TemplateArgsStr) |
| 279 | << Active->InstantiationRange; |
| 280 | break; |
| 281 | } |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 282 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 283 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | FunctionTemplateDecl *FnTmpl |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 285 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 286 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 287 | diag::note_explicit_template_arg_substitution_here) |
| 288 | << FnTmpl << Active->InstantiationRange; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 289 | break; |
| 290 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 292 | case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: |
| 293 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 294 | = dyn_cast<ClassTemplatePartialSpecializationDecl>( |
| 295 | (Decl *)Active->Entity)) { |
| 296 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 297 | diag::note_partial_spec_deduct_instantiation_here) |
| 298 | << Context.getTypeDeclType(PartialSpec) |
| 299 | << Active->InstantiationRange; |
| 300 | } else { |
| 301 | FunctionTemplateDecl *FnTmpl |
| 302 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
| 303 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 304 | diag::note_function_template_deduction_instantiation_here) |
| 305 | << FnTmpl << Active->InstantiationRange; |
| 306 | } |
| 307 | break; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 308 | |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 309 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: { |
| 310 | ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity); |
| 311 | FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 313 | std::string TemplateArgsStr |
| 314 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | Active->TemplateArgs, |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 316 | Active->NumTemplateArgs, |
| 317 | Context.PrintingPolicy); |
| 318 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 319 | diag::note_default_function_arg_instantiation_here) |
Anders Carlsson | 6bc107b | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 320 | << (FD->getNameAsString() + TemplateArgsStr) |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 321 | << Active->InstantiationRange; |
| 322 | break; |
| 323 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 325 | } |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 329 | bool Sema::isSFINAEContext() const { |
| 330 | using llvm::SmallVector; |
| 331 | for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator |
| 332 | Active = ActiveTemplateInstantiations.rbegin(), |
| 333 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 334 | Active != ActiveEnd; |
| 335 | ++Active) { |
| 336 | |
| 337 | switch(Active->Kind) { |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 338 | case ActiveTemplateInstantiation::TemplateInstantiation: |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 339 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: |
| 340 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 341 | // This is a template instantiation, so there is no SFINAE. |
| 342 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 344 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: |
| 345 | // A default template argument instantiation may or may not be a |
| 346 | // SFINAE context; look further up the stack. |
| 347 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 349 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: |
| 350 | case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: |
| 351 | // We're either substitution explicitly-specified template arguments |
| 352 | // or deduced template arguments, so SFINAE applies. |
| 353 | return true; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | return false; |
| 358 | } |
| 359 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 360 | //===----------------------------------------------------------------------===/ |
| 361 | // Template Instantiation for Types |
| 362 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 363 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | class VISIBILITY_HIDDEN TemplateInstantiator |
| 365 | : public TreeTransform<TemplateInstantiator> { |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 366 | const MultiLevelTemplateArgumentList &TemplateArgs; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 367 | SourceLocation Loc; |
| 368 | DeclarationName Entity; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 370 | public: |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 371 | typedef TreeTransform<TemplateInstantiator> inherited; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
| 373 | TemplateInstantiator(Sema &SemaRef, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 374 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 375 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | DeclarationName Entity) |
| 377 | : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 378 | Entity(Entity) { } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 379 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 381 | /// transformed. |
| 382 | /// |
| 383 | /// For the purposes of template instantiation, a type has already been |
| 384 | /// transformed if it is NULL or if it is not dependent. |
| 385 | bool AlreadyTransformed(QualType T) { |
| 386 | return T.isNull() || !T->isDependentType(); |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 387 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 389 | /// \brief Returns the location of the entity being instantiated, if known. |
| 390 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 392 | /// \brief Returns the name of the entity being instantiated, if any. |
| 393 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 395 | /// \brief Transform the given declaration by instantiating a reference to |
| 396 | /// this declaration. |
| 397 | Decl *TransformDecl(Decl *D); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 398 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | /// \brief Transform the definition of the given declaration by |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 400 | /// instantiating it. |
| 401 | Decl *TransformDefinition(Decl *D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 403 | /// \bried Transform the first qualifier within a scope by instantiating the |
| 404 | /// declaration. |
| 405 | NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); |
| 406 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 407 | /// \brief Rebuild the exception declaration and register the declaration |
| 408 | /// as an instantiated local. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 410 | DeclaratorInfo *Declarator, |
| 411 | IdentifierInfo *Name, |
| 412 | SourceLocation Loc, SourceRange TypeRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 414 | /// \brief Check for tag mismatches when instantiating an |
| 415 | /// elaborated type. |
| 416 | QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag); |
| 417 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 418 | Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 419 | Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
| 421 | /// \brief Transforms a template type parameter type by performing |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 422 | /// substitution of the corresponding template type argument. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 423 | QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| 424 | TemplateTypeParmTypeLoc TL); |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 425 | }; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 428 | Decl *TemplateInstantiator::TransformDecl(Decl *D) { |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 429 | if (!D) |
| 430 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 432 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 433 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 434 | assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() && |
| 435 | "Wrong kind of template template argument"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 437 | TTP->getPosition()).getAsDecl()); |
| 438 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
| 440 | // If the corresponding template argument is NULL or non-existent, it's |
| 441 | // because we are performing instantiation from explicitly-specified |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 442 | // template arguments in a function template, but there were some |
| 443 | // arguments left unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 445 | TTP->getPosition())) |
| 446 | return D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 448 | // FIXME: Implement depth reduction of template template parameters |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | assert(false && |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 450 | "Reducing depth of template template parameters is not yet implemented"); |
Douglas Gregor | d1067e5 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 451 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 453 | return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs); |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 456 | Decl *TemplateInstantiator::TransformDefinition(Decl *D) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 457 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 458 | if (!Inst) |
| 459 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 461 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 462 | return Inst; |
| 463 | } |
| 464 | |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 465 | NamedDecl * |
| 466 | TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
| 467 | SourceLocation Loc) { |
| 468 | // If the first part of the nested-name-specifier was a template type |
| 469 | // parameter, instantiate that type parameter down to a tag type. |
| 470 | if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
| 471 | const TemplateTypeParmType *TTP |
| 472 | = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
| 473 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 474 | QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType(); |
| 475 | if (T.isNull()) |
| 476 | return cast_or_null<NamedDecl>(TransformDecl(D)); |
| 477 | |
| 478 | if (const TagType *Tag = T->getAs<TagType>()) |
| 479 | return Tag->getDecl(); |
| 480 | |
| 481 | // The resulting type is not a tag; complain. |
| 482 | getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
| 483 | return 0; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | return cast_or_null<NamedDecl>(TransformDecl(D)); |
| 488 | } |
| 489 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 490 | VarDecl * |
| 491 | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | QualType T, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 493 | DeclaratorInfo *Declarator, |
| 494 | IdentifierInfo *Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | SourceLocation Loc, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 496 | SourceRange TypeRange) { |
| 497 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator, |
| 498 | Name, Loc, TypeRange); |
| 499 | if (Var && !Var->isInvalidDecl()) |
| 500 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 501 | return Var; |
| 502 | } |
| 503 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 504 | QualType |
| 505 | TemplateInstantiator::RebuildElaboratedType(QualType T, |
| 506 | ElaboratedType::TagKind Tag) { |
| 507 | if (const TagType *TT = T->getAs<TagType>()) { |
| 508 | TagDecl* TD = TT->getDecl(); |
| 509 | |
| 510 | // FIXME: this location is very wrong; we really need typelocs. |
| 511 | SourceLocation TagLocation = TD->getTagKeywordLoc(); |
| 512 | |
| 513 | // FIXME: type might be anonymous. |
| 514 | IdentifierInfo *Id = TD->getIdentifier(); |
| 515 | |
| 516 | // TODO: should we even warn on struct/class mismatches for this? Seems |
| 517 | // like it's likely to produce a lot of spurious errors. |
| 518 | if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) { |
| 519 | SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) |
| 520 | << Id |
| 521 | << CodeModificationHint::CreateReplacement(SourceRange(TagLocation), |
| 522 | TD->getKindName()); |
| 523 | SemaRef.Diag(TD->getLocation(), diag::note_previous_use); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag); |
| 528 | } |
| 529 | |
| 530 | Sema::OwningExprResult |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 531 | TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { |
| 532 | if (!E->isTypeDependent()) |
| 533 | return SemaRef.Owned(E->Retain()); |
| 534 | |
| 535 | FunctionDecl *currentDecl = getSema().getCurFunctionDecl(); |
| 536 | assert(currentDecl && "Must have current function declaration when " |
| 537 | "instantiating."); |
| 538 | |
| 539 | PredefinedExpr::IdentType IT = E->getIdentType(); |
| 540 | |
| 541 | unsigned Length = |
| 542 | PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length(); |
| 543 | |
| 544 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 545 | QualType ResTy = getSema().Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 546 | ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI, |
| 547 | ArrayType::Normal, 0); |
| 548 | PredefinedExpr *PE = |
| 549 | new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT); |
| 550 | return getSema().Owned(PE); |
| 551 | } |
| 552 | |
| 553 | Sema::OwningExprResult |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 554 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
| 555 | // FIXME: Clean this up a bit |
| 556 | NamedDecl *D = E->getDecl(); |
| 557 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 558 | if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) { |
| 559 | assert(false && "Cannot reduce non-type template parameter depth yet"); |
| 560 | return getSema().ExprError(); |
| 561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
| 563 | // If the corresponding template argument is NULL or non-existent, it's |
| 564 | // because we are performing instantiation from explicitly-specified |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 565 | // template arguments in a function template, but there were some |
| 566 | // arguments left unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 568 | NTTP->getPosition())) |
| 569 | return SemaRef.Owned(E->Retain()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
| 571 | const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 572 | NTTP->getPosition()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 574 | // The template argument itself might be an expression, in which |
| 575 | // case we just return that expression. |
| 576 | if (Arg.getKind() == TemplateArgument::Expression) |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 577 | return SemaRef.Owned(Arg.getAsExpr()->Retain()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 579 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 580 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 582 | VD = cast_or_null<ValueDecl>( |
| 583 | getSema().FindInstantiatedDecl(VD, TemplateArgs)); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 584 | if (!VD) |
| 585 | return SemaRef.ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | |
| 587 | return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 588 | /*FIXME:*/false, /*FIXME:*/false); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 589 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 591 | assert(Arg.getKind() == TemplateArgument::Integral); |
| 592 | QualType T = Arg.getIntegralType(); |
| 593 | if (T->isCharType() || T->isWideCharType()) |
| 594 | return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral( |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 595 | Arg.getAsIntegral()->getZExtValue(), |
| 596 | T->isWideCharType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | T, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 598 | E->getSourceRange().getBegin())); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 599 | if (T->isBooleanType()) |
| 600 | return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr( |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 601 | Arg.getAsIntegral()->getBoolValue(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | T, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 603 | E->getSourceRange().getBegin())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 605 | assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T)); |
| 606 | return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral( |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 607 | *Arg.getAsIntegral(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | T, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 609 | E->getSourceRange().getBegin())); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 610 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 612 | NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 613 | if (!InstD) |
| 614 | return SemaRef.ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 616 | // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | // we need to get the underlying decl. |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 618 | // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this? |
| 619 | InstD = InstD->getUnderlyingDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 621 | // FIXME: nested-name-specifier for QualifiedDeclRefExpr |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD, |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 623 | /*FIXME:*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | /*FIXME:*/0, |
| 625 | /*FIXME:*/false); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | QualType |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 629 | TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| 630 | TemplateTypeParmTypeLoc TL) { |
| 631 | TemplateTypeParmType *T = TL.getTypePtr(); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 632 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 633 | // Replace the template type parameter with its corresponding |
| 634 | // template argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
| 636 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 637 | // because we are performing instantiation from explicitly-specified |
| 638 | // template arguments in a function template class, but there were some |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 639 | // arguments left unspecified. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 640 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
| 641 | TemplateTypeParmTypeLoc NewTL |
| 642 | = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
| 643 | NewTL.setNameLoc(TL.getNameLoc()); |
| 644 | return TL.getType(); |
| 645 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | |
| 647 | assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind() |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 648 | == TemplateArgument::Type && |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 649 | "Template argument kind mismatch"); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 650 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 651 | QualType Replacement |
| 652 | = TemplateArgs(T->getDepth(), T->getIndex()).getAsType(); |
| 653 | |
| 654 | // TODO: only do this uniquing once, at the start of instantiation. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 655 | QualType Result |
| 656 | = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); |
| 657 | SubstTemplateTypeParmTypeLoc NewTL |
| 658 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 659 | NewTL.setNameLoc(TL.getNameLoc()); |
| 660 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 662 | |
| 663 | // The template type parameter comes from an inner template (e.g., |
| 664 | // the template parameter list of a member template inside the |
| 665 | // template we are instantiating). Create a new template type |
| 666 | // parameter with the template "level" reduced by one. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 667 | QualType Result |
| 668 | = getSema().Context.getTemplateTypeParmType(T->getDepth() |
| 669 | - TemplateArgs.getNumLevels(), |
| 670 | T->getIndex(), |
| 671 | T->isParameterPack(), |
| 672 | T->getName()); |
| 673 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 674 | NewTL.setNameLoc(TL.getNameLoc()); |
| 675 | return Result; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 676 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 677 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 678 | /// \brief Perform substitution on the type T with a given set of template |
| 679 | /// arguments. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 680 | /// |
| 681 | /// This routine substitutes the given template arguments into the |
| 682 | /// type T and produces the instantiated type. |
| 683 | /// |
| 684 | /// \param T the type into which the template arguments will be |
| 685 | /// substituted. If this type is not dependent, it will be returned |
| 686 | /// immediately. |
| 687 | /// |
| 688 | /// \param TemplateArgs the template arguments that will be |
| 689 | /// substituted for the top-level template parameters within T. |
| 690 | /// |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 691 | /// \param Loc the location in the source code where this substitution |
| 692 | /// is being performed. It will typically be the location of the |
| 693 | /// declarator (if we're instantiating the type of some declaration) |
| 694 | /// or the location of the type in the source code (if, e.g., we're |
| 695 | /// instantiating the type of a cast expression). |
| 696 | /// |
| 697 | /// \param Entity the name of the entity associated with a declaration |
| 698 | /// being instantiated (if any). May be empty to indicate that there |
| 699 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 700 | /// a cast expression) or that the entity has no name (e.g., an |
| 701 | /// unnamed function parameter). |
| 702 | /// |
| 703 | /// \returns If the instantiation succeeds, the instantiated |
| 704 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
John McCall | cd7ba1c | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 705 | DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T, |
| 706 | const MultiLevelTemplateArgumentList &Args, |
| 707 | SourceLocation Loc, |
| 708 | DeclarationName Entity) { |
| 709 | assert(!ActiveTemplateInstantiations.empty() && |
| 710 | "Cannot perform an instantiation without some context on the " |
| 711 | "instantiation stack"); |
| 712 | |
| 713 | if (!T->getType()->isDependentType()) |
| 714 | return T; |
| 715 | |
| 716 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 717 | return Instantiator.TransformType(T); |
| 718 | } |
| 719 | |
| 720 | /// Deprecated form of the above. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | QualType Sema::SubstType(QualType T, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 722 | const MultiLevelTemplateArgumentList &TemplateArgs, |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 723 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 724 | assert(!ActiveTemplateInstantiations.empty() && |
| 725 | "Cannot perform an instantiation without some context on the " |
| 726 | "instantiation stack"); |
| 727 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 728 | // If T is not a dependent type, there is nothing to do. |
| 729 | if (!T->isDependentType()) |
| 730 | return T; |
| 731 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 732 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 733 | return Instantiator.TransformType(T); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 734 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 735 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 736 | /// \brief Perform substitution on the base class specifiers of the |
| 737 | /// given class template specialization. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 738 | /// |
| 739 | /// Produces a diagnostic and returns true on error, returns false and |
| 740 | /// attaches the instantiated base classes to the class template |
| 741 | /// specialization if successful. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | bool |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 743 | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 744 | CXXRecordDecl *Pattern, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 745 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 746 | bool Invalid = false; |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 747 | llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | for (ClassTemplateSpecializationDecl::base_class_iterator |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 749 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 750 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 751 | if (!Base->getType()->isDependentType()) { |
Fariborz Jahanian | 71c6e71 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 752 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 753 | continue; |
| 754 | } |
| 755 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | QualType BaseType = SubstType(Base->getType(), |
| 757 | TemplateArgs, |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 758 | Base->getSourceRange().getBegin(), |
| 759 | DeclarationName()); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 760 | if (BaseType.isNull()) { |
| 761 | Invalid = true; |
| 762 | continue; |
| 763 | } |
| 764 | |
| 765 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 766 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 767 | Base->getSourceRange(), |
| 768 | Base->isVirtual(), |
| 769 | Base->getAccessSpecifierAsWritten(), |
| 770 | BaseType, |
| 771 | /*FIXME: Not totally accurate */ |
| 772 | Base->getSourceRange().getBegin())) |
| 773 | InstantiatedBases.push_back(InstantiatedBase); |
| 774 | else |
| 775 | Invalid = true; |
| 776 | } |
| 777 | |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 778 | if (!Invalid && |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 779 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 780 | InstantiatedBases.size())) |
| 781 | Invalid = true; |
| 782 | |
| 783 | return Invalid; |
| 784 | } |
| 785 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 786 | /// \brief Instantiate the definition of a class from a given pattern. |
| 787 | /// |
| 788 | /// \param PointOfInstantiation The point of instantiation within the |
| 789 | /// source code. |
| 790 | /// |
| 791 | /// \param Instantiation is the declaration whose definition is being |
| 792 | /// instantiated. This will be either a class template specialization |
| 793 | /// or a member class of a class template specialization. |
| 794 | /// |
| 795 | /// \param Pattern is the pattern from which the instantiation |
| 796 | /// occurs. This will be either the declaration of a class template or |
| 797 | /// the declaration of a member class of a class template. |
| 798 | /// |
| 799 | /// \param TemplateArgs The template arguments to be substituted into |
| 800 | /// the pattern. |
| 801 | /// |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 802 | /// \param TSK the kind of implicit or explicit instantiation to perform. |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 803 | /// |
| 804 | /// \param Complain whether to complain if the class cannot be instantiated due |
| 805 | /// to the lack of a definition. |
| 806 | /// |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 807 | /// \returns true if an error occurred, false otherwise. |
| 808 | bool |
| 809 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 810 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 811 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 812 | TemplateSpecializationKind TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 813 | bool Complain) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 814 | bool Invalid = false; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 815 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | CXXRecordDecl *PatternDef |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 817 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 818 | if (!PatternDef) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 819 | if (!Complain) { |
| 820 | // Say nothing |
| 821 | } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 822 | Diag(PointOfInstantiation, |
| 823 | diag::err_implicit_instantiate_member_undefined) |
| 824 | << Context.getTypeDeclType(Instantiation); |
| 825 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 826 | } else { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 827 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 828 | << (TSK != TSK_ImplicitInstantiation) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 829 | << Context.getTypeDeclType(Instantiation); |
| 830 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 831 | } |
| 832 | return true; |
| 833 | } |
| 834 | Pattern = PatternDef; |
| 835 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 836 | // \brief Record the point of instantiation. |
| 837 | if (MemberSpecializationInfo *MSInfo |
| 838 | = Instantiation->getMemberSpecializationInfo()) { |
| 839 | MSInfo->setTemplateSpecializationKind(TSK); |
| 840 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 841 | } |
| 842 | |
Douglas Gregor | d048bb7 | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 843 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 844 | if (Inst) |
| 845 | return true; |
| 846 | |
| 847 | // Enter the scope of this instantiation. We don't use |
| 848 | // PushDeclContext because we don't have a scope. |
| 849 | DeclContext *PreviousContext = CurContext; |
| 850 | CurContext = Instantiation; |
| 851 | |
| 852 | // Start the definition of this instantiation. |
| 853 | Instantiation->startDefinition(); |
| 854 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 855 | // Do substitution on the base class specifiers. |
| 856 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 857 | Invalid = true; |
| 858 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 859 | llvm::SmallVector<DeclPtrTy, 4> Fields; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 860 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | MemberEnd = Pattern->decls_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 862 | Member != MemberEnd; ++Member) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 863 | Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 864 | if (NewMember) { |
| 865 | if (NewMember->isInvalidDecl()) |
| 866 | Invalid = true; |
| 867 | else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 868 | Fields.push_back(DeclPtrTy::make(Field)); |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 869 | else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember)) |
| 870 | Instantiation->addDecl(UD); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 871 | } else { |
| 872 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 873 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 874 | // true. For now, we expect to skip some members that we can't yet handle. |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
| 878 | // Finish checking fields. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 879 | ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 880 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 881 | 0); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 882 | if (Instantiation->isInvalidDecl()) |
| 883 | Invalid = true; |
| 884 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 885 | // Add any implicitly-declared members that we might need. |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 886 | if (!Invalid) |
| 887 | AddImplicitlyDeclaredMembersToClass(Instantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 888 | |
| 889 | // Exit the scope of this instantiation. |
| 890 | CurContext = PreviousContext; |
| 891 | |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 892 | if (!Invalid) |
| 893 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 894 | |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 895 | // If this is an explicit instantiation, instantiate our members, too. |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 896 | if (!Invalid && TSK != TSK_ImplicitInstantiation) { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 897 | Inst.Clear(); |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 898 | InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs, |
| 899 | TSK); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 900 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 901 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 902 | return Invalid; |
| 903 | } |
| 904 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | bool |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 906 | Sema::InstantiateClassTemplateSpecialization( |
| 907 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 908 | TemplateSpecializationKind TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 909 | bool Complain) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 910 | // Perform the actual instantiation on the canonical declaration. |
| 911 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 912 | ClassTemplateSpec->getCanonicalDecl()); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 913 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 914 | // Check whether we have already instantiated or specialized this class |
| 915 | // template specialization. |
| 916 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) { |
| 917 | if (ClassTemplateSpec->getSpecializationKind() == |
| 918 | TSK_ExplicitInstantiationDeclaration && |
| 919 | TSK == TSK_ExplicitInstantiationDefinition) { |
| 920 | // An explicit instantiation definition follows an explicit instantiation |
| 921 | // declaration (C++0x [temp.explicit]p10); go ahead and perform the |
| 922 | // explicit instantiation. |
| 923 | ClassTemplateSpec->setSpecializationKind(TSK); |
| 924 | InstantiateClassTemplateSpecializationMembers( |
| 925 | /*FIXME?*/ClassTemplateSpec->getPointOfInstantiation(), |
| 926 | ClassTemplateSpec, |
| 927 | TSK); |
| 928 | return false; |
| 929 | } |
| 930 | |
| 931 | // We can only instantiate something that hasn't already been |
| 932 | // instantiated or specialized. Fail without any diagnostics: our |
| 933 | // caller will provide an error message. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 934 | return true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 935 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | 9eea08b | 2009-09-15 16:51:42 +0000 | [diff] [blame] | 937 | if (ClassTemplateSpec->isInvalidDecl()) |
| 938 | return true; |
| 939 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 940 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 941 | CXXRecordDecl *Pattern = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 942 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 943 | // C++ [temp.class.spec.match]p1: |
| 944 | // When a class template is used in a context that requires an |
| 945 | // instantiation of the class, it is necessary to determine |
| 946 | // whether the instantiation is to be generated using the primary |
| 947 | // template or one of the partial specializations. This is done by |
| 948 | // matching the template arguments of the class template |
| 949 | // specialization with the template argument lists of the partial |
| 950 | // specializations. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 951 | typedef std::pair<ClassTemplatePartialSpecializationDecl *, |
| 952 | TemplateArgumentList *> MatchResult; |
| 953 | llvm::SmallVector<MatchResult, 4> Matched; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 955 | Partial = Template->getPartialSpecializations().begin(), |
| 956 | PartialEnd = Template->getPartialSpecializations().end(); |
| 957 | Partial != PartialEnd; |
| 958 | ++Partial) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 959 | TemplateDeductionInfo Info(Context); |
| 960 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | = DeduceTemplateArguments(&*Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 962 | ClassTemplateSpec->getTemplateArgs(), |
| 963 | Info)) { |
| 964 | // FIXME: Store the failed-deduction information for use in |
| 965 | // diagnostics, later. |
| 966 | (void)Result; |
| 967 | } else { |
| 968 | Matched.push_back(std::make_pair(&*Partial, Info.take())); |
| 969 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | if (Matched.size() == 1) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 973 | // -- If exactly one matching specialization is found, the |
| 974 | // instantiation is generated from that specialization. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 975 | Pattern = Matched[0].first; |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 976 | ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 977 | } else if (Matched.size() > 1) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 978 | // -- If more than one matching specialization is found, the |
| 979 | // partial order rules (14.5.4.2) are used to determine |
| 980 | // whether one of the specializations is more specialized |
| 981 | // than the others. If none of the specializations is more |
| 982 | // specialized than all of the other matching |
| 983 | // specializations, then the use of the class template is |
| 984 | // ambiguous and the program is ill-formed. |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 985 | llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 986 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 987 | PEnd = Matched.end(); |
| 988 | P != PEnd; ++P) { |
| 989 | if (getMoreSpecializedPartialSpecialization(P->first, Best->first) |
| 990 | == P->first) |
| 991 | Best = P; |
| 992 | } |
| 993 | |
| 994 | // Determine if the best partial specialization is more specialized than |
| 995 | // the others. |
| 996 | bool Ambiguous = false; |
| 997 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 998 | PEnd = Matched.end(); |
| 999 | P != PEnd; ++P) { |
| 1000 | if (P != Best && |
| 1001 | getMoreSpecializedPartialSpecialization(P->first, Best->first) |
| 1002 | != Best->first) { |
| 1003 | Ambiguous = true; |
| 1004 | break; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | if (Ambiguous) { |
| 1009 | // Partial ordering did not produce a clear winner. Complain. |
| 1010 | ClassTemplateSpec->setInvalidDecl(); |
| 1011 | Diag(ClassTemplateSpec->getPointOfInstantiation(), |
| 1012 | diag::err_partial_spec_ordering_ambiguous) |
| 1013 | << ClassTemplateSpec; |
| 1014 | |
| 1015 | // Print the matching partial specializations. |
| 1016 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1017 | PEnd = Matched.end(); |
| 1018 | P != PEnd; ++P) |
| 1019 | Diag(P->first->getLocation(), diag::note_partial_spec_match) |
| 1020 | << getTemplateArgumentBindingsText(P->first->getTemplateParameters(), |
| 1021 | *P->second); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1022 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1023 | return true; |
| 1024 | } |
| 1025 | |
| 1026 | // Instantiate using the best class template partial specialization. |
| 1027 | Pattern = Best->first; |
| 1028 | ClassTemplateSpec->setInstantiationOf(Best->first, Best->second); |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1029 | } else { |
| 1030 | // -- If no matches are found, the instantiation is generated |
| 1031 | // from the primary template. |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1032 | ClassTemplateDecl *OrigTemplate = Template; |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1033 | while (OrigTemplate->getInstantiatedFromMemberTemplate()) { |
| 1034 | // If we've found an explicit specialization of this class template, |
| 1035 | // stop here and use that as the pattern. |
| 1036 | if (OrigTemplate->isMemberSpecialization()) |
| 1037 | break; |
| 1038 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1039 | OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1042 | Pattern = OrigTemplate->getTemplatedDecl(); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1043 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1044 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1045 | // Note that this is an instantiation. |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1046 | ClassTemplateSpec->setSpecializationKind(TSK); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1047 | |
John McCall | 9cc7807 | 2009-09-11 07:25:08 +0000 | [diff] [blame] | 1048 | bool Result = InstantiateClass(ClassTemplateSpec->getPointOfInstantiation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | ClassTemplateSpec, Pattern, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1050 | getTemplateInstantiationArgs(ClassTemplateSpec), |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1051 | TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1052 | Complain); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1054 | for (unsigned I = 0, N = Matched.size(); I != N; ++I) { |
| 1055 | // FIXME: Implement TemplateArgumentList::Destroy! |
| 1056 | // if (Matched[I].first != Pattern) |
| 1057 | // Matched[I].second->Destroy(Context); |
| 1058 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1060 | return Result; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1061 | } |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1062 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1063 | /// \brief Instantiates the definitions of all of the member |
| 1064 | /// of the given class, which is an instantiation of a class template |
| 1065 | /// or a member class of a template. |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1066 | void |
| 1067 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1068 | CXXRecordDecl *Instantiation, |
| 1069 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 1070 | TemplateSpecializationKind TSK) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1071 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(), |
| 1072 | DEnd = Instantiation->decls_end(); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1073 | D != DEnd; ++D) { |
| 1074 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1075 | if (Function->getInstantiatedFromMemberFunction()) { |
| 1076 | // If this member was explicitly specialized, do nothing. |
| 1077 | if (Function->getTemplateSpecializationKind() == |
| 1078 | TSK_ExplicitSpecialization) |
| 1079 | continue; |
| 1080 | |
Douglas Gregor | 86035ae | 2009-10-15 23:05:15 +0000 | [diff] [blame] | 1081 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | if (!Function->getBody() && TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1085 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1086 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1087 | if (Var->isStaticDataMember()) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1088 | // If this member was explicitly specialized, do nothing. |
| 1089 | if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 1090 | continue; |
| 1091 | |
Douglas Gregor | 86035ae | 2009-10-15 23:05:15 +0000 | [diff] [blame] | 1092 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1093 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1094 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1095 | InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); |
| 1096 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1097 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1098 | if (Record->isInjectedClassName()) |
| 1099 | continue; |
| 1100 | |
| 1101 | assert(Record->getInstantiatedFromMemberClass() && |
| 1102 | "Missing instantiated-from-template information"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1103 | |
| 1104 | // If this member was explicitly specialized, do nothing. |
| 1105 | if (Record->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 1106 | continue; |
| 1107 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1108 | if (!Record->getDefinition(Context)) |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1109 | InstantiateClass(PointOfInstantiation, Record, |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1110 | Record->getInstantiatedFromMemberClass(), |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1111 | TemplateArgs, |
| 1112 | TSK); |
Douglas Gregor | e9374d5 | 2009-10-08 01:19:17 +0000 | [diff] [blame] | 1113 | |
| 1114 | InstantiateClassMembers(PointOfInstantiation, Record, TemplateArgs, |
| 1115 | TSK); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | /// \brief Instantiate the definitions of all of the members of the |
| 1121 | /// given class template specialization, which was named as part of an |
| 1122 | /// explicit instantiation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | void |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1124 | Sema::InstantiateClassTemplateSpecializationMembers( |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1125 | SourceLocation PointOfInstantiation, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1126 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 1127 | TemplateSpecializationKind TSK) { |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1128 | // C++0x [temp.explicit]p7: |
| 1129 | // An explicit instantiation that names a class template |
| 1130 | // specialization is an explicit instantion of the same kind |
| 1131 | // (declaration or definition) of each of its members (not |
| 1132 | // including members inherited from base classes) that has not |
| 1133 | // been previously explicitly specialized in the translation unit |
| 1134 | // containing the explicit instantiation, except as described |
| 1135 | // below. |
| 1136 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1137 | getTemplateInstantiationArgs(ClassTemplateSpec), |
| 1138 | TSK); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | Sema::OwningStmtResult |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1142 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 1143 | if (!S) |
| 1144 | return Owned(S); |
| 1145 | |
| 1146 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1147 | SourceLocation(), |
| 1148 | DeclarationName()); |
| 1149 | return Instantiator.TransformStmt(S); |
| 1150 | } |
| 1151 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | Sema::OwningExprResult |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1153 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1154 | if (!E) |
| 1155 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1157 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1158 | SourceLocation(), |
| 1159 | DeclarationName()); |
| 1160 | return Instantiator.TransformExpr(E); |
| 1161 | } |
| 1162 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1163 | /// \brief Do template substitution on a nested-name-specifier. |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1164 | NestedNameSpecifier * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1165 | Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1166 | SourceRange Range, |
| 1167 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | dcee1a1 | 2009-08-06 05:28:30 +0000 | [diff] [blame] | 1168 | TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(), |
| 1169 | DeclarationName()); |
| 1170 | return Instantiator.TransformNestedNameSpecifier(NNS, Range); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1171 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1172 | |
| 1173 | TemplateName |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1174 | Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1175 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | d1067e5 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 1176 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 1177 | DeclarationName()); |
| 1178 | return Instantiator.TransformTemplateName(Name); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1179 | } |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1180 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | TemplateArgument Sema::Subst(TemplateArgument Arg, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1182 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 670444e | 2009-08-04 22:27:00 +0000 | [diff] [blame] | 1183 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 1184 | DeclarationName()); |
| 1185 | return Instantiator.TransformTemplateArgument(Arg); |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1186 | } |