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 | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 395 | /// \brief Sets the "base" location and entity when that |
| 396 | /// information is known based on another transformation. |
| 397 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 398 | this->Loc = Loc; |
| 399 | this->Entity = Entity; |
| 400 | } |
| 401 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 402 | /// \brief Transform the given declaration by instantiating a reference to |
| 403 | /// this declaration. |
| 404 | Decl *TransformDecl(Decl *D); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 405 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | /// \brief Transform the definition of the given declaration by |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 407 | /// instantiating it. |
| 408 | Decl *TransformDefinition(Decl *D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 410 | /// \bried Transform the first qualifier within a scope by instantiating the |
| 411 | /// declaration. |
| 412 | NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); |
| 413 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 414 | /// \brief Rebuild the exception declaration and register the declaration |
| 415 | /// as an instantiated local. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 417 | DeclaratorInfo *Declarator, |
| 418 | IdentifierInfo *Name, |
| 419 | SourceLocation Loc, SourceRange TypeRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 421 | /// \brief Check for tag mismatches when instantiating an |
| 422 | /// elaborated type. |
| 423 | QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag); |
| 424 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 425 | Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 426 | Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
| 428 | /// \brief Transforms a template type parameter type by performing |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 429 | /// substitution of the corresponding template type argument. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 430 | QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| 431 | TemplateTypeParmTypeLoc TL); |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 432 | }; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 435 | Decl *TemplateInstantiator::TransformDecl(Decl *D) { |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 436 | if (!D) |
| 437 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 439 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 440 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 441 | assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() && |
| 442 | "Wrong kind of template template argument"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 444 | TTP->getPosition()).getAsDecl()); |
| 445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
| 447 | // If the corresponding template argument is NULL or non-existent, it's |
| 448 | // because we are performing instantiation from explicitly-specified |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 449 | // template arguments in a function template, but there were some |
| 450 | // arguments left unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 452 | TTP->getPosition())) |
| 453 | return D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 455 | // FIXME: Implement depth reduction of template template parameters |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | assert(false && |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 457 | "Reducing depth of template template parameters is not yet implemented"); |
Douglas Gregor | d1067e5 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 458 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 460 | return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs); |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 463 | Decl *TemplateInstantiator::TransformDefinition(Decl *D) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 464 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 465 | if (!Inst) |
| 466 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 468 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 469 | return Inst; |
| 470 | } |
| 471 | |
Douglas Gregor | 6cd2198 | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 472 | NamedDecl * |
| 473 | TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
| 474 | SourceLocation Loc) { |
| 475 | // If the first part of the nested-name-specifier was a template type |
| 476 | // parameter, instantiate that type parameter down to a tag type. |
| 477 | if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
| 478 | const TemplateTypeParmType *TTP |
| 479 | = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
| 480 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 481 | QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType(); |
| 482 | if (T.isNull()) |
| 483 | return cast_or_null<NamedDecl>(TransformDecl(D)); |
| 484 | |
| 485 | if (const TagType *Tag = T->getAs<TagType>()) |
| 486 | return Tag->getDecl(); |
| 487 | |
| 488 | // The resulting type is not a tag; complain. |
| 489 | getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
| 490 | return 0; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return cast_or_null<NamedDecl>(TransformDecl(D)); |
| 495 | } |
| 496 | |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 497 | VarDecl * |
| 498 | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | QualType T, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 500 | DeclaratorInfo *Declarator, |
| 501 | IdentifierInfo *Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | SourceLocation Loc, |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 503 | SourceRange TypeRange) { |
| 504 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator, |
| 505 | Name, Loc, TypeRange); |
| 506 | if (Var && !Var->isInvalidDecl()) |
| 507 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 508 | return Var; |
| 509 | } |
| 510 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 511 | QualType |
| 512 | TemplateInstantiator::RebuildElaboratedType(QualType T, |
| 513 | ElaboratedType::TagKind Tag) { |
| 514 | if (const TagType *TT = T->getAs<TagType>()) { |
| 515 | TagDecl* TD = TT->getDecl(); |
| 516 | |
| 517 | // FIXME: this location is very wrong; we really need typelocs. |
| 518 | SourceLocation TagLocation = TD->getTagKeywordLoc(); |
| 519 | |
| 520 | // FIXME: type might be anonymous. |
| 521 | IdentifierInfo *Id = TD->getIdentifier(); |
| 522 | |
| 523 | // TODO: should we even warn on struct/class mismatches for this? Seems |
| 524 | // like it's likely to produce a lot of spurious errors. |
| 525 | if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) { |
| 526 | SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) |
| 527 | << Id |
| 528 | << CodeModificationHint::CreateReplacement(SourceRange(TagLocation), |
| 529 | TD->getKindName()); |
| 530 | SemaRef.Diag(TD->getLocation(), diag::note_previous_use); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag); |
| 535 | } |
| 536 | |
| 537 | Sema::OwningExprResult |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 538 | TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { |
| 539 | if (!E->isTypeDependent()) |
| 540 | return SemaRef.Owned(E->Retain()); |
| 541 | |
| 542 | FunctionDecl *currentDecl = getSema().getCurFunctionDecl(); |
| 543 | assert(currentDecl && "Must have current function declaration when " |
| 544 | "instantiating."); |
| 545 | |
| 546 | PredefinedExpr::IdentType IT = E->getIdentType(); |
| 547 | |
| 548 | unsigned Length = |
| 549 | PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length(); |
| 550 | |
| 551 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 552 | QualType ResTy = getSema().Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 553 | ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI, |
| 554 | ArrayType::Normal, 0); |
| 555 | PredefinedExpr *PE = |
| 556 | new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT); |
| 557 | return getSema().Owned(PE); |
| 558 | } |
| 559 | |
| 560 | Sema::OwningExprResult |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 561 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
| 562 | // FIXME: Clean this up a bit |
| 563 | NamedDecl *D = E->getDecl(); |
| 564 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 565 | if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) { |
| 566 | assert(false && "Cannot reduce non-type template parameter depth yet"); |
| 567 | return getSema().ExprError(); |
| 568 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | |
| 570 | // If the corresponding template argument is NULL or non-existent, it's |
| 571 | // because we are performing instantiation from explicitly-specified |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 572 | // template arguments in a function template, but there were some |
| 573 | // arguments left unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 575 | NTTP->getPosition())) |
| 576 | return SemaRef.Owned(E->Retain()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
| 578 | const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 579 | NTTP->getPosition()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 581 | // The template argument itself might be an expression, in which |
| 582 | // case we just return that expression. |
| 583 | if (Arg.getKind() == TemplateArgument::Expression) |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 584 | return SemaRef.Owned(Arg.getAsExpr()->Retain()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 586 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 587 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 589 | VD = cast_or_null<ValueDecl>( |
| 590 | getSema().FindInstantiatedDecl(VD, TemplateArgs)); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 591 | if (!VD) |
| 592 | return SemaRef.ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 593 | |
| 594 | return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 595 | /*FIXME:*/false, /*FIXME:*/false); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 596 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 598 | assert(Arg.getKind() == TemplateArgument::Integral); |
| 599 | QualType T = Arg.getIntegralType(); |
| 600 | if (T->isCharType() || T->isWideCharType()) |
| 601 | return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral( |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 602 | Arg.getAsIntegral()->getZExtValue(), |
| 603 | T->isWideCharType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | T, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 605 | E->getSourceRange().getBegin())); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 606 | if (T->isBooleanType()) |
| 607 | return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr( |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 608 | Arg.getAsIntegral()->getBoolValue(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | T, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 610 | E->getSourceRange().getBegin())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 612 | assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T)); |
| 613 | return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral( |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 614 | *Arg.getAsIntegral(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | T, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 616 | E->getSourceRange().getBegin())); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 617 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 619 | NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 620 | if (!InstD) |
| 621 | return SemaRef.ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 623 | // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | // we need to get the underlying decl. |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 625 | // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this? |
| 626 | InstD = InstD->getUnderlyingDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 628 | CXXScopeSpec SS; |
| 629 | NestedNameSpecifier *Qualifier = 0; |
| 630 | if (E->getQualifier()) { |
| 631 | Qualifier = TransformNestedNameSpecifier(E->getQualifier(), |
| 632 | E->getQualifierRange()); |
| 633 | if (!Qualifier) |
| 634 | return SemaRef.ExprError(); |
| 635 | |
| 636 | SS.setScopeRep(Qualifier); |
| 637 | SS.setRange(E->getQualifierRange()); |
| 638 | } |
| 639 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD, |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 641 | /*FIXME:*/false, |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 642 | &SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 | /*FIXME:*/false); |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | QualType |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 647 | TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| 648 | TemplateTypeParmTypeLoc TL) { |
| 649 | TemplateTypeParmType *T = TL.getTypePtr(); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 650 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 651 | // Replace the template type parameter with its corresponding |
| 652 | // template argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | |
| 654 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 655 | // because we are performing instantiation from explicitly-specified |
| 656 | // template arguments in a function template class, but there were some |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 657 | // arguments left unspecified. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 658 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
| 659 | TemplateTypeParmTypeLoc NewTL |
| 660 | = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
| 661 | NewTL.setNameLoc(TL.getNameLoc()); |
| 662 | return TL.getType(); |
| 663 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
| 665 | assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind() |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 666 | == TemplateArgument::Type && |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 667 | "Template argument kind mismatch"); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 668 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 669 | QualType Replacement |
| 670 | = TemplateArgs(T->getDepth(), T->getIndex()).getAsType(); |
| 671 | |
| 672 | // TODO: only do this uniquing once, at the start of instantiation. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 673 | QualType Result |
| 674 | = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); |
| 675 | SubstTemplateTypeParmTypeLoc NewTL |
| 676 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 677 | NewTL.setNameLoc(TL.getNameLoc()); |
| 678 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 680 | |
| 681 | // The template type parameter comes from an inner template (e.g., |
| 682 | // the template parameter list of a member template inside the |
| 683 | // template we are instantiating). Create a new template type |
| 684 | // parameter with the template "level" reduced by one. |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 685 | QualType Result |
| 686 | = getSema().Context.getTemplateTypeParmType(T->getDepth() |
| 687 | - TemplateArgs.getNumLevels(), |
| 688 | T->getIndex(), |
| 689 | T->isParameterPack(), |
| 690 | T->getName()); |
| 691 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 692 | NewTL.setNameLoc(TL.getNameLoc()); |
| 693 | return Result; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 694 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 695 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 696 | /// \brief Perform substitution on the type T with a given set of template |
| 697 | /// arguments. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 698 | /// |
| 699 | /// This routine substitutes the given template arguments into the |
| 700 | /// type T and produces the instantiated type. |
| 701 | /// |
| 702 | /// \param T the type into which the template arguments will be |
| 703 | /// substituted. If this type is not dependent, it will be returned |
| 704 | /// immediately. |
| 705 | /// |
| 706 | /// \param TemplateArgs the template arguments that will be |
| 707 | /// substituted for the top-level template parameters within T. |
| 708 | /// |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 709 | /// \param Loc the location in the source code where this substitution |
| 710 | /// is being performed. It will typically be the location of the |
| 711 | /// declarator (if we're instantiating the type of some declaration) |
| 712 | /// or the location of the type in the source code (if, e.g., we're |
| 713 | /// instantiating the type of a cast expression). |
| 714 | /// |
| 715 | /// \param Entity the name of the entity associated with a declaration |
| 716 | /// being instantiated (if any). May be empty to indicate that there |
| 717 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 718 | /// a cast expression) or that the entity has no name (e.g., an |
| 719 | /// unnamed function parameter). |
| 720 | /// |
| 721 | /// \returns If the instantiation succeeds, the instantiated |
| 722 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
John McCall | cd7ba1c | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 723 | DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T, |
| 724 | const MultiLevelTemplateArgumentList &Args, |
| 725 | SourceLocation Loc, |
| 726 | DeclarationName Entity) { |
| 727 | assert(!ActiveTemplateInstantiations.empty() && |
| 728 | "Cannot perform an instantiation without some context on the " |
| 729 | "instantiation stack"); |
| 730 | |
| 731 | if (!T->getType()->isDependentType()) |
| 732 | return T; |
| 733 | |
| 734 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 735 | return Instantiator.TransformType(T); |
| 736 | } |
| 737 | |
| 738 | /// Deprecated form of the above. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | QualType Sema::SubstType(QualType T, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 740 | const MultiLevelTemplateArgumentList &TemplateArgs, |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 741 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 742 | assert(!ActiveTemplateInstantiations.empty() && |
| 743 | "Cannot perform an instantiation without some context on the " |
| 744 | "instantiation stack"); |
| 745 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 746 | // If T is not a dependent type, there is nothing to do. |
| 747 | if (!T->isDependentType()) |
| 748 | return T; |
| 749 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 750 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 751 | return Instantiator.TransformType(T); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 752 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 753 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 754 | /// \brief Perform substitution on the base class specifiers of the |
| 755 | /// given class template specialization. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 756 | /// |
| 757 | /// Produces a diagnostic and returns true on error, returns false and |
| 758 | /// attaches the instantiated base classes to the class template |
| 759 | /// specialization if successful. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | bool |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 761 | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 762 | CXXRecordDecl *Pattern, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 763 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 764 | bool Invalid = false; |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 765 | llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | for (ClassTemplateSpecializationDecl::base_class_iterator |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 767 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 768 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 769 | if (!Base->getType()->isDependentType()) { |
Fariborz Jahanian | 71c6e71 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 770 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 771 | continue; |
| 772 | } |
| 773 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | QualType BaseType = SubstType(Base->getType(), |
| 775 | TemplateArgs, |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 776 | Base->getSourceRange().getBegin(), |
| 777 | DeclarationName()); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 778 | if (BaseType.isNull()) { |
| 779 | Invalid = true; |
| 780 | continue; |
| 781 | } |
| 782 | |
| 783 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 784 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 785 | Base->getSourceRange(), |
| 786 | Base->isVirtual(), |
| 787 | Base->getAccessSpecifierAsWritten(), |
| 788 | BaseType, |
| 789 | /*FIXME: Not totally accurate */ |
| 790 | Base->getSourceRange().getBegin())) |
| 791 | InstantiatedBases.push_back(InstantiatedBase); |
| 792 | else |
| 793 | Invalid = true; |
| 794 | } |
| 795 | |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 796 | if (!Invalid && |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 797 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 798 | InstantiatedBases.size())) |
| 799 | Invalid = true; |
| 800 | |
| 801 | return Invalid; |
| 802 | } |
| 803 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 804 | /// \brief Instantiate the definition of a class from a given pattern. |
| 805 | /// |
| 806 | /// \param PointOfInstantiation The point of instantiation within the |
| 807 | /// source code. |
| 808 | /// |
| 809 | /// \param Instantiation is the declaration whose definition is being |
| 810 | /// instantiated. This will be either a class template specialization |
| 811 | /// or a member class of a class template specialization. |
| 812 | /// |
| 813 | /// \param Pattern is the pattern from which the instantiation |
| 814 | /// occurs. This will be either the declaration of a class template or |
| 815 | /// the declaration of a member class of a class template. |
| 816 | /// |
| 817 | /// \param TemplateArgs The template arguments to be substituted into |
| 818 | /// the pattern. |
| 819 | /// |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 820 | /// \param TSK the kind of implicit or explicit instantiation to perform. |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 821 | /// |
| 822 | /// \param Complain whether to complain if the class cannot be instantiated due |
| 823 | /// to the lack of a definition. |
| 824 | /// |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 825 | /// \returns true if an error occurred, false otherwise. |
| 826 | bool |
| 827 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 828 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 829 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 830 | TemplateSpecializationKind TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 831 | bool Complain) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 832 | bool Invalid = false; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 833 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | CXXRecordDecl *PatternDef |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 835 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 836 | if (!PatternDef) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 837 | if (!Complain) { |
| 838 | // Say nothing |
| 839 | } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 840 | Diag(PointOfInstantiation, |
| 841 | diag::err_implicit_instantiate_member_undefined) |
| 842 | << Context.getTypeDeclType(Instantiation); |
| 843 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 844 | } else { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 845 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 846 | << (TSK != TSK_ImplicitInstantiation) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 847 | << Context.getTypeDeclType(Instantiation); |
| 848 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 849 | } |
| 850 | return true; |
| 851 | } |
| 852 | Pattern = PatternDef; |
| 853 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 854 | // \brief Record the point of instantiation. |
| 855 | if (MemberSpecializationInfo *MSInfo |
| 856 | = Instantiation->getMemberSpecializationInfo()) { |
| 857 | MSInfo->setTemplateSpecializationKind(TSK); |
| 858 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 859 | } else if (ClassTemplateSpecializationDecl *Spec |
| 860 | = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { |
| 861 | Spec->setTemplateSpecializationKind(TSK); |
| 862 | Spec->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Douglas Gregor | d048bb7 | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 865 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 866 | if (Inst) |
| 867 | return true; |
| 868 | |
| 869 | // Enter the scope of this instantiation. We don't use |
| 870 | // PushDeclContext because we don't have a scope. |
| 871 | DeclContext *PreviousContext = CurContext; |
| 872 | CurContext = Instantiation; |
| 873 | |
| 874 | // Start the definition of this instantiation. |
| 875 | Instantiation->startDefinition(); |
| 876 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 877 | // Do substitution on the base class specifiers. |
| 878 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 879 | Invalid = true; |
| 880 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 881 | llvm::SmallVector<DeclPtrTy, 4> Fields; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 882 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | MemberEnd = Pattern->decls_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 884 | Member != MemberEnd; ++Member) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 885 | Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 886 | if (NewMember) { |
| 887 | if (NewMember->isInvalidDecl()) |
| 888 | Invalid = true; |
| 889 | else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 890 | Fields.push_back(DeclPtrTy::make(Field)); |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 891 | else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember)) |
| 892 | Instantiation->addDecl(UD); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 893 | } else { |
| 894 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 895 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 896 | // 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] | 897 | } |
| 898 | } |
| 899 | |
| 900 | // Finish checking fields. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 901 | ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 902 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 903 | 0); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 904 | if (Instantiation->isInvalidDecl()) |
| 905 | Invalid = true; |
| 906 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 907 | // Add any implicitly-declared members that we might need. |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 908 | if (!Invalid) |
| 909 | AddImplicitlyDeclaredMembersToClass(Instantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 910 | |
| 911 | // Exit the scope of this instantiation. |
| 912 | CurContext = PreviousContext; |
| 913 | |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 914 | if (!Invalid) |
| 915 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 916 | |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 917 | // If this is an explicit instantiation, instantiate our members, too. |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 918 | if (!Invalid && TSK != TSK_ImplicitInstantiation) { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 919 | Inst.Clear(); |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 920 | InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs, |
| 921 | TSK); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 922 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 923 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 924 | return Invalid; |
| 925 | } |
| 926 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | bool |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 928 | Sema::InstantiateClassTemplateSpecialization( |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 929 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 930 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 931 | TemplateSpecializationKind TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 932 | bool Complain) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 933 | // Perform the actual instantiation on the canonical declaration. |
| 934 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 935 | ClassTemplateSpec->getCanonicalDecl()); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 937 | // Check whether we have already instantiated or specialized this class |
| 938 | // template specialization. |
| 939 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) { |
| 940 | if (ClassTemplateSpec->getSpecializationKind() == |
| 941 | TSK_ExplicitInstantiationDeclaration && |
| 942 | TSK == TSK_ExplicitInstantiationDefinition) { |
| 943 | // An explicit instantiation definition follows an explicit instantiation |
| 944 | // declaration (C++0x [temp.explicit]p10); go ahead and perform the |
| 945 | // explicit instantiation. |
| 946 | ClassTemplateSpec->setSpecializationKind(TSK); |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 947 | InstantiateClassTemplateSpecializationMembers(PointOfInstantiation, |
| 948 | ClassTemplateSpec, |
| 949 | TSK); |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 950 | return false; |
| 951 | } |
| 952 | |
| 953 | // We can only instantiate something that hasn't already been |
| 954 | // instantiated or specialized. Fail without any diagnostics: our |
| 955 | // caller will provide an error message. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 956 | return true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 957 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 958 | |
Douglas Gregor | 9eea08b | 2009-09-15 16:51:42 +0000 | [diff] [blame] | 959 | if (ClassTemplateSpec->isInvalidDecl()) |
| 960 | return true; |
| 961 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 962 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 963 | CXXRecordDecl *Pattern = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 964 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 965 | // C++ [temp.class.spec.match]p1: |
| 966 | // When a class template is used in a context that requires an |
| 967 | // instantiation of the class, it is necessary to determine |
| 968 | // whether the instantiation is to be generated using the primary |
| 969 | // template or one of the partial specializations. This is done by |
| 970 | // matching the template arguments of the class template |
| 971 | // specialization with the template argument lists of the partial |
| 972 | // specializations. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 973 | typedef std::pair<ClassTemplatePartialSpecializationDecl *, |
| 974 | TemplateArgumentList *> MatchResult; |
| 975 | llvm::SmallVector<MatchResult, 4> Matched; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 977 | Partial = Template->getPartialSpecializations().begin(), |
| 978 | PartialEnd = Template->getPartialSpecializations().end(); |
| 979 | Partial != PartialEnd; |
| 980 | ++Partial) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 981 | TemplateDeductionInfo Info(Context); |
| 982 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | = DeduceTemplateArguments(&*Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 984 | ClassTemplateSpec->getTemplateArgs(), |
| 985 | Info)) { |
| 986 | // FIXME: Store the failed-deduction information for use in |
| 987 | // diagnostics, later. |
| 988 | (void)Result; |
| 989 | } else { |
| 990 | Matched.push_back(std::make_pair(&*Partial, Info.take())); |
| 991 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | if (Matched.size() == 1) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 995 | // -- If exactly one matching specialization is found, the |
| 996 | // instantiation is generated from that specialization. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 997 | Pattern = Matched[0].first; |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 998 | ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 999 | } else if (Matched.size() > 1) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1000 | // -- If more than one matching specialization is found, the |
| 1001 | // partial order rules (14.5.4.2) are used to determine |
| 1002 | // whether one of the specializations is more specialized |
| 1003 | // than the others. If none of the specializations is more |
| 1004 | // specialized than all of the other matching |
| 1005 | // specializations, then the use of the class template is |
| 1006 | // ambiguous and the program is ill-formed. |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1007 | llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 1008 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 1009 | PEnd = Matched.end(); |
| 1010 | P != PEnd; ++P) { |
| 1011 | if (getMoreSpecializedPartialSpecialization(P->first, Best->first) |
| 1012 | == P->first) |
| 1013 | Best = P; |
| 1014 | } |
| 1015 | |
| 1016 | // Determine if the best partial specialization is more specialized than |
| 1017 | // the others. |
| 1018 | bool Ambiguous = false; |
| 1019 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1020 | PEnd = Matched.end(); |
| 1021 | P != PEnd; ++P) { |
| 1022 | if (P != Best && |
| 1023 | getMoreSpecializedPartialSpecialization(P->first, Best->first) |
| 1024 | != Best->first) { |
| 1025 | Ambiguous = true; |
| 1026 | break; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | if (Ambiguous) { |
| 1031 | // Partial ordering did not produce a clear winner. Complain. |
| 1032 | ClassTemplateSpec->setInvalidDecl(); |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1033 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1034 | << ClassTemplateSpec; |
| 1035 | |
| 1036 | // Print the matching partial specializations. |
| 1037 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1038 | PEnd = Matched.end(); |
| 1039 | P != PEnd; ++P) |
| 1040 | Diag(P->first->getLocation(), diag::note_partial_spec_match) |
| 1041 | << getTemplateArgumentBindingsText(P->first->getTemplateParameters(), |
| 1042 | *P->second); |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1043 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1044 | return true; |
| 1045 | } |
| 1046 | |
| 1047 | // Instantiate using the best class template partial specialization. |
| 1048 | Pattern = Best->first; |
| 1049 | ClassTemplateSpec->setInstantiationOf(Best->first, Best->second); |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1050 | } else { |
| 1051 | // -- If no matches are found, the instantiation is generated |
| 1052 | // from the primary template. |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1053 | ClassTemplateDecl *OrigTemplate = Template; |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1054 | while (OrigTemplate->getInstantiatedFromMemberTemplate()) { |
| 1055 | // If we've found an explicit specialization of this class template, |
| 1056 | // stop here and use that as the pattern. |
| 1057 | if (OrigTemplate->isMemberSpecialization()) |
| 1058 | break; |
| 1059 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1060 | OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1063 | Pattern = OrigTemplate->getTemplatedDecl(); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1064 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1065 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1066 | bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec, |
| 1067 | Pattern, |
| 1068 | getTemplateInstantiationArgs(ClassTemplateSpec), |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1069 | TSK, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1070 | Complain); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1072 | for (unsigned I = 0, N = Matched.size(); I != N; ++I) { |
| 1073 | // FIXME: Implement TemplateArgumentList::Destroy! |
| 1074 | // if (Matched[I].first != Pattern) |
| 1075 | // Matched[I].second->Destroy(Context); |
| 1076 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1078 | return Result; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1079 | } |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1080 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1081 | /// \brief Instantiates the definitions of all of the member |
| 1082 | /// of the given class, which is an instantiation of a class template |
| 1083 | /// or a member class of a template. |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1084 | void |
| 1085 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1086 | CXXRecordDecl *Instantiation, |
| 1087 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 1088 | TemplateSpecializationKind TSK) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1089 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(), |
| 1090 | DEnd = Instantiation->decls_end(); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1091 | D != DEnd; ++D) { |
| 1092 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1093 | if (Function->getInstantiatedFromMemberFunction()) { |
| 1094 | // If this member was explicitly specialized, do nothing. |
| 1095 | if (Function->getTemplateSpecializationKind() == |
| 1096 | TSK_ExplicitSpecialization) |
| 1097 | continue; |
| 1098 | |
Douglas Gregor | 86035ae | 2009-10-15 23:05:15 +0000 | [diff] [blame] | 1099 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | if (!Function->getBody() && TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1103 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1104 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1105 | if (Var->isStaticDataMember()) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1106 | // If this member was explicitly specialized, do nothing. |
| 1107 | if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 1108 | continue; |
| 1109 | |
Douglas Gregor | 86035ae | 2009-10-15 23:05:15 +0000 | [diff] [blame] | 1110 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1111 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1112 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1113 | InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); |
| 1114 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1115 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1116 | if (Record->isInjectedClassName()) |
| 1117 | continue; |
| 1118 | |
| 1119 | assert(Record->getInstantiatedFromMemberClass() && |
| 1120 | "Missing instantiated-from-template information"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1121 | |
| 1122 | // If this member was explicitly specialized, do nothing. |
| 1123 | if (Record->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 1124 | continue; |
| 1125 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1126 | if (!Record->getDefinition(Context)) |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1127 | InstantiateClass(PointOfInstantiation, Record, |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1128 | Record->getInstantiatedFromMemberClass(), |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1129 | TemplateArgs, |
| 1130 | TSK); |
Douglas Gregor | e9374d5 | 2009-10-08 01:19:17 +0000 | [diff] [blame] | 1131 | |
| 1132 | InstantiateClassMembers(PointOfInstantiation, Record, TemplateArgs, |
| 1133 | TSK); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | /// \brief Instantiate the definitions of all of the members of the |
| 1139 | /// given class template specialization, which was named as part of an |
| 1140 | /// explicit instantiation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | void |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1142 | Sema::InstantiateClassTemplateSpecializationMembers( |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1143 | SourceLocation PointOfInstantiation, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1144 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 1145 | TemplateSpecializationKind TSK) { |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1146 | // C++0x [temp.explicit]p7: |
| 1147 | // An explicit instantiation that names a class template |
| 1148 | // specialization is an explicit instantion of the same kind |
| 1149 | // (declaration or definition) of each of its members (not |
| 1150 | // including members inherited from base classes) that has not |
| 1151 | // been previously explicitly specialized in the translation unit |
| 1152 | // containing the explicit instantiation, except as described |
| 1153 | // below. |
| 1154 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1155 | getTemplateInstantiationArgs(ClassTemplateSpec), |
| 1156 | TSK); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | Sema::OwningStmtResult |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1160 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 43959a9 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 1161 | if (!S) |
| 1162 | return Owned(S); |
| 1163 | |
| 1164 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1165 | SourceLocation(), |
| 1166 | DeclarationName()); |
| 1167 | return Instantiator.TransformStmt(S); |
| 1168 | } |
| 1169 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | Sema::OwningExprResult |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1171 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1172 | if (!E) |
| 1173 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | |
Douglas Gregor | b98b199 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1175 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1176 | SourceLocation(), |
| 1177 | DeclarationName()); |
| 1178 | return Instantiator.TransformExpr(E); |
| 1179 | } |
| 1180 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1181 | /// \brief Do template substitution on a nested-name-specifier. |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1182 | NestedNameSpecifier * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1183 | Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1184 | SourceRange Range, |
| 1185 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | dcee1a1 | 2009-08-06 05:28:30 +0000 | [diff] [blame] | 1186 | TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(), |
| 1187 | DeclarationName()); |
| 1188 | return Instantiator.TransformNestedNameSpecifier(NNS, Range); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1189 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1190 | |
| 1191 | TemplateName |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1192 | Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1193 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | d1067e5 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 1194 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 1195 | DeclarationName()); |
| 1196 | return Instantiator.TransformTemplateName(Name); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1197 | } |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1198 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | TemplateArgument Sema::Subst(TemplateArgument Arg, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1200 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 670444e | 2009-08-04 22:27:00 +0000 | [diff] [blame] | 1201 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 1202 | DeclarationName()); |
| 1203 | return Instantiator.TransformTemplateArgument(Arg); |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1204 | } |