Douglas Gregor | 7429654 | 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 | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | dc18e89 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Expr.h" |
Douglas Gregor | 7429654 | 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 | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Douglas Gregor | fee85d6 | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===/ |
| 26 | // Template Instantiation Support |
| 27 | //===----------------------------------------------------------------------===/ |
| 28 | |
Douglas Gregor | 8dbd038 | 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 | 5b8beb3 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 31 | MultiLevelTemplateArgumentList |
Douglas Gregor | 5f62c5e | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 32 | Sema::getTemplateInstantiationArgs(NamedDecl *D) { |
Douglas Gregor | 5b8beb3 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 33 | // Accumulate the set of template argument lists in this structure. |
| 34 | MultiLevelTemplateArgumentList Result; |
| 35 | |
| 36 | DeclContext *Ctx = dyn_cast<DeclContext>(D); |
| 37 | if (!Ctx) |
| 38 | Ctx = D->getDeclContext(); |
| 39 | |
John McCall | 178664c | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 40 | while (!Ctx->isFileContext()) { |
Douglas Gregor | 5b8beb3 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 41 | // Add template arguments from a class template instantiation. |
| 42 | if (ClassTemplateSpecializationDecl *Spec |
| 43 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { |
| 44 | // We're done when we hit an explicit specialization. |
| 45 | if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization) |
| 46 | break; |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | 5b8beb3 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 48 | Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); |
Douglas Gregor | 5b8beb3 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Add template arguments from a function template specialization. |
John McCall | 178664c | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 52 | else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) { |
Douglas Gregor | 5b8beb3 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 53 | // FIXME: Check whether this is an explicit specialization. |
| 54 | if (const TemplateArgumentList *TemplateArgs |
| 55 | = Function->getTemplateSpecializationArgs()) |
| 56 | Result.addOuterTemplateArguments(TemplateArgs); |
John McCall | 178664c | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 57 | |
| 58 | // If this is a friend declaration and it declares an entity at |
| 59 | // namespace scope, take arguments from its lexical parent |
| 60 | // instead of its semantic parent. |
| 61 | if (Function->getFriendObjectKind() && |
| 62 | Function->getDeclContext()->isFileContext()) { |
| 63 | Ctx = Function->getLexicalDeclContext(); |
| 64 | continue; |
| 65 | } |
Douglas Gregor | 5b8beb3 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 66 | } |
John McCall | 178664c | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 67 | |
| 68 | Ctx = Ctx->getParent(); |
Douglas Gregor | 5f62c5e | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 69 | } |
Douglas Gregor | 5b8beb3 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 70 | |
| 71 | return Result; |
Douglas Gregor | 5f62c5e | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Douglas Gregor | 375733c | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 74 | Sema::InstantiatingTemplate:: |
| 75 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 76 | Decl *Entity, |
Douglas Gregor | 375733c | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 77 | SourceRange InstantiationRange) |
| 78 | : SemaRef(SemaRef) { |
Douglas Gregor | 56d25a7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 79 | |
| 80 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 81 | InstantiationRange); |
| 82 | if (!Invalid) { |
Douglas Gregor | 375733c | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 83 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | 56d25a7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 84 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | 375733c | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 85 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | 56d25a7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 86 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | 95ba128 | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 87 | Inst.TemplateArgs = 0; |
| 88 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | 56d25a7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 89 | Inst.InstantiationRange = InstantiationRange; |
| 90 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 91 | Invalid = false; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 96 | SourceLocation PointOfInstantiation, |
| 97 | TemplateDecl *Template, |
| 98 | const TemplateArgument *TemplateArgs, |
| 99 | unsigned NumTemplateArgs, |
| 100 | SourceRange InstantiationRange) |
| 101 | : SemaRef(SemaRef) { |
| 102 | |
| 103 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 104 | InstantiationRange); |
| 105 | if (!Invalid) { |
| 106 | ActiveTemplateInstantiation Inst; |
| 107 | Inst.Kind |
| 108 | = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; |
| 109 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 110 | Inst.Entity = reinterpret_cast<uintptr_t>(Template); |
| 111 | Inst.TemplateArgs = TemplateArgs; |
| 112 | Inst.NumTemplateArgs = NumTemplateArgs; |
Douglas Gregor | 375733c | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 113 | Inst.InstantiationRange = InstantiationRange; |
| 114 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 115 | Invalid = false; |
| 116 | } |
| 117 | } |
| 118 | |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 119 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 120 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 4c8b2b3 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 121 | FunctionTemplateDecl *FunctionTemplate, |
| 122 | const TemplateArgument *TemplateArgs, |
| 123 | unsigned NumTemplateArgs, |
| 124 | ActiveTemplateInstantiation::InstantiationKind Kind, |
| 125 | SourceRange InstantiationRange) |
| 126 | : SemaRef(SemaRef) { |
| 127 | |
| 128 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 129 | InstantiationRange); |
| 130 | if (!Invalid) { |
| 131 | ActiveTemplateInstantiation Inst; |
| 132 | Inst.Kind = Kind; |
| 133 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 134 | Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate); |
| 135 | Inst.TemplateArgs = TemplateArgs; |
| 136 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 137 | Inst.InstantiationRange = InstantiationRange; |
| 138 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 139 | Invalid = false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 144 | SourceLocation PointOfInstantiation, |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 145 | ClassTemplatePartialSpecializationDecl *PartialSpec, |
| 146 | const TemplateArgument *TemplateArgs, |
| 147 | unsigned NumTemplateArgs, |
| 148 | SourceRange InstantiationRange) |
| 149 | : SemaRef(SemaRef) { |
| 150 | |
| 151 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 152 | InstantiationRange); |
| 153 | if (!Invalid) { |
| 154 | ActiveTemplateInstantiation Inst; |
| 155 | Inst.Kind |
Douglas Gregor | 4c8b2b3 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 156 | = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution; |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 157 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 158 | Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec); |
| 159 | Inst.TemplateArgs = TemplateArgs; |
| 160 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 161 | Inst.InstantiationRange = InstantiationRange; |
| 162 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 163 | Invalid = false; |
| 164 | } |
| 165 | } |
| 166 | |
Anders Carlsson | 21d18f2 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 167 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 168 | SourceLocation PointOfInstantation, |
| 169 | ParmVarDecl *Param, |
| 170 | const TemplateArgument *TemplateArgs, |
| 171 | unsigned NumTemplateArgs, |
| 172 | SourceRange InstantiationRange) |
| 173 | : SemaRef(SemaRef) { |
| 174 | |
| 175 | Invalid = CheckInstantiationDepth(PointOfInstantation, InstantiationRange); |
| 176 | |
| 177 | if (!Invalid) { |
| 178 | ActiveTemplateInstantiation Inst; |
| 179 | Inst.Kind |
| 180 | = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; |
| 181 | Inst.PointOfInstantiation = PointOfInstantation; |
| 182 | Inst.Entity = reinterpret_cast<uintptr_t>(Param); |
| 183 | Inst.TemplateArgs = TemplateArgs; |
| 184 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 185 | Inst.InstantiationRange = InstantiationRange; |
| 186 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 187 | Invalid = false; |
| 188 | } |
| 189 | } |
| 190 | |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 191 | void Sema::InstantiatingTemplate::Clear() { |
| 192 | if (!Invalid) { |
Douglas Gregor | 375733c | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 193 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 194 | Invalid = true; |
| 195 | } |
Douglas Gregor | 375733c | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Douglas Gregor | 56d25a7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 198 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 199 | SourceLocation PointOfInstantiation, |
| 200 | SourceRange InstantiationRange) { |
| 201 | if (SemaRef.ActiveTemplateInstantiations.size() |
| 202 | <= SemaRef.getLangOptions().InstantiationDepth) |
| 203 | return false; |
| 204 | |
| 205 | SemaRef.Diag(PointOfInstantiation, |
| 206 | diag::err_template_recursion_depth_exceeded) |
| 207 | << SemaRef.getLangOptions().InstantiationDepth |
| 208 | << InstantiationRange; |
| 209 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
| 210 | << SemaRef.getLangOptions().InstantiationDepth; |
| 211 | return true; |
| 212 | } |
| 213 | |
Douglas Gregor | fee85d6 | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 214 | /// \brief Prints the current instantiation stack through a series of |
| 215 | /// notes. |
| 216 | void Sema::PrintInstantiationStack() { |
Douglas Gregor | 4c8b2b3 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 217 | // FIXME: In all of these cases, we need to show the template arguments |
Douglas Gregor | fee85d6 | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 218 | for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator |
| 219 | Active = ActiveTemplateInstantiations.rbegin(), |
| 220 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 221 | Active != ActiveEnd; |
| 222 | ++Active) { |
Douglas Gregor | 56d25a7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 223 | switch (Active->Kind) { |
| 224 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 225 | Decl *D = reinterpret_cast<Decl *>(Active->Entity); |
| 226 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 227 | unsigned DiagID = diag::note_template_member_class_here; |
| 228 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 229 | DiagID = diag::note_template_class_instantiation_here; |
| 230 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 231 | DiagID) |
| 232 | << Context.getTypeDeclType(Record) |
| 233 | << Active->InstantiationRange; |
Douglas Gregor | 181fe79 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 234 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 235 | unsigned DiagID; |
| 236 | if (Function->getPrimaryTemplate()) |
| 237 | DiagID = diag::note_function_template_spec_here; |
| 238 | else |
| 239 | DiagID = diag::note_template_member_function_here; |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 240 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 241 | DiagID) |
| 242 | << Function |
| 243 | << Active->InstantiationRange; |
Douglas Gregor | 181fe79 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 244 | } else { |
| 245 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 246 | diag::note_template_static_data_member_def_here) |
| 247 | << cast<VarDecl>(D) |
| 248 | << Active->InstantiationRange; |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 249 | } |
Douglas Gregor | 56d25a7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 250 | break; |
| 251 | } |
| 252 | |
| 253 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { |
| 254 | TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); |
| 255 | std::string TemplateArgsStr |
Douglas Gregor | dd13e84 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 256 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 4c8b2b3 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 257 | Active->TemplateArgs, |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 258 | Active->NumTemplateArgs, |
| 259 | Context.PrintingPolicy); |
Douglas Gregor | 56d25a7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 260 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 261 | diag::note_default_arg_instantiation_here) |
| 262 | << (Template->getNameAsString() + TemplateArgsStr) |
| 263 | << Active->InstantiationRange; |
| 264 | break; |
| 265 | } |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 266 | |
Douglas Gregor | 4c8b2b3 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 267 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: { |
| 268 | FunctionTemplateDecl *FnTmpl |
| 269 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 270 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | 4c8b2b3 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 271 | diag::note_explicit_template_arg_substitution_here) |
| 272 | << FnTmpl << Active->InstantiationRange; |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 273 | break; |
| 274 | } |
Douglas Gregor | 4c8b2b3 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 275 | |
| 276 | case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: |
| 277 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 278 | = dyn_cast<ClassTemplatePartialSpecializationDecl>( |
| 279 | (Decl *)Active->Entity)) { |
| 280 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 281 | diag::note_partial_spec_deduct_instantiation_here) |
| 282 | << Context.getTypeDeclType(PartialSpec) |
| 283 | << Active->InstantiationRange; |
| 284 | } else { |
| 285 | FunctionTemplateDecl *FnTmpl |
| 286 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
| 287 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 288 | diag::note_function_template_deduction_instantiation_here) |
| 289 | << FnTmpl << Active->InstantiationRange; |
| 290 | } |
| 291 | break; |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 292 | |
Anders Carlsson | 21d18f2 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 293 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: { |
| 294 | ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity); |
| 295 | FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); |
Anders Carlsson | 21d18f2 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 296 | |
| 297 | std::string TemplateArgsStr |
| 298 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 299 | Active->TemplateArgs, |
| 300 | Active->NumTemplateArgs, |
| 301 | Context.PrintingPolicy); |
| 302 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 303 | diag::note_default_function_arg_instantiation_here) |
Anders Carlsson | f608ead | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 304 | << (FD->getNameAsString() + TemplateArgsStr) |
Anders Carlsson | 21d18f2 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 305 | << Active->InstantiationRange; |
| 306 | break; |
| 307 | } |
| 308 | |
Douglas Gregor | 56d25a7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 309 | } |
Douglas Gregor | fee85d6 | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Douglas Gregor | 95d6c95 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 313 | bool Sema::isSFINAEContext() const { |
| 314 | using llvm::SmallVector; |
| 315 | for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator |
| 316 | Active = ActiveTemplateInstantiations.rbegin(), |
| 317 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 318 | Active != ActiveEnd; |
| 319 | ++Active) { |
| 320 | |
| 321 | switch(Active->Kind) { |
Douglas Gregor | 4c8b2b3 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 322 | case ActiveTemplateInstantiation::TemplateInstantiation: |
Anders Carlsson | 21d18f2 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 323 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: |
| 324 | |
Douglas Gregor | 4c8b2b3 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 325 | // This is a template instantiation, so there is no SFINAE. |
| 326 | return false; |
| 327 | |
Douglas Gregor | 95d6c95 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 328 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: |
| 329 | // A default template argument instantiation may or may not be a |
| 330 | // SFINAE context; look further up the stack. |
| 331 | break; |
Douglas Gregor | 4c8b2b3 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 332 | |
| 333 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: |
| 334 | case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: |
| 335 | // We're either substitution explicitly-specified template arguments |
| 336 | // or deduced template arguments, so SFINAE applies. |
| 337 | return true; |
Douglas Gregor | 95d6c95 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | return false; |
| 342 | } |
| 343 | |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 344 | //===----------------------------------------------------------------------===/ |
| 345 | // Template Instantiation for Types |
| 346 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 347 | namespace { |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 348 | class VISIBILITY_HIDDEN TemplateInstantiator |
| 349 | : public TreeTransform<TemplateInstantiator> |
| 350 | { |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 351 | const MultiLevelTemplateArgumentList &TemplateArgs; |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 352 | SourceLocation Loc; |
| 353 | DeclarationName Entity; |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 354 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 355 | public: |
Douglas Gregor | 23a44be | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 356 | typedef TreeTransform<TemplateInstantiator> inherited; |
| 357 | |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 358 | TemplateInstantiator(Sema &SemaRef, |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 359 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 360 | SourceLocation Loc, |
| 361 | DeclarationName Entity) |
Douglas Gregor | 23a44be | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 362 | : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
| 363 | Entity(Entity) { } |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 364 | |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 365 | /// \brief Determine whether the given type \p T has already been |
| 366 | /// transformed. |
| 367 | /// |
| 368 | /// For the purposes of template instantiation, a type has already been |
| 369 | /// transformed if it is NULL or if it is not dependent. |
| 370 | bool AlreadyTransformed(QualType T) { |
| 371 | return T.isNull() || !T->isDependentType(); |
Douglas Gregor | 9017791 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 372 | } |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 373 | |
| 374 | /// \brief Returns the location of the entity being instantiated, if known. |
| 375 | SourceLocation getBaseLocation() { return Loc; } |
| 376 | |
| 377 | /// \brief Returns the name of the entity being instantiated, if any. |
| 378 | DeclarationName getBaseEntity() { return Entity; } |
| 379 | |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 380 | /// \brief Transform the given declaration by instantiating a reference to |
| 381 | /// this declaration. |
| 382 | Decl *TransformDecl(Decl *D); |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 383 | |
Douglas Gregor | 23a44be | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 384 | /// \brief Transform the definition of the given declaration by |
| 385 | /// instantiating it. |
| 386 | Decl *TransformDefinition(Decl *D); |
| 387 | |
| 388 | /// \brief Rebuild the exception declaration and register the declaration |
| 389 | /// as an instantiated local. |
| 390 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T, |
| 391 | DeclaratorInfo *Declarator, |
| 392 | IdentifierInfo *Name, |
| 393 | SourceLocation Loc, SourceRange TypeRange); |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 394 | |
| 395 | Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E); |
| 396 | |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 397 | /// \brief Transforms a template type parameter type by performing |
| 398 | /// substitution of the corresponding template type argument. |
| 399 | QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T); |
| 400 | }; |
Douglas Gregor | 1d38113 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 403 | Decl *TemplateInstantiator::TransformDecl(Decl *D) { |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 404 | if (!D) |
| 405 | return 0; |
| 406 | |
| 407 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 408 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 409 | assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() && |
| 410 | "Wrong kind of template template argument"); |
| 411 | return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(), |
| 412 | TTP->getPosition()).getAsDecl()); |
| 413 | } |
| 414 | |
| 415 | // If the corresponding template argument is NULL or non-existent, it's |
| 416 | // because we are performing instantiation from explicitly-specified |
| 417 | // template arguments in a function template, but there were some |
| 418 | // arguments left unspecified. |
| 419 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
| 420 | TTP->getPosition())) |
| 421 | return D; |
| 422 | |
| 423 | // FIXME: Implement depth reduction of template template parameters |
| 424 | assert(false && |
| 425 | "Reducing depth of template template parameters is not yet implemented"); |
Douglas Gregor | 214d046 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 428 | return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D)); |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Douglas Gregor | 23a44be | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 431 | Decl *TemplateInstantiator::TransformDefinition(Decl *D) { |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 432 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
Douglas Gregor | 23a44be | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 433 | if (!Inst) |
| 434 | return 0; |
| 435 | |
| 436 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 437 | return Inst; |
| 438 | } |
| 439 | |
| 440 | VarDecl * |
| 441 | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
| 442 | QualType T, |
| 443 | DeclaratorInfo *Declarator, |
| 444 | IdentifierInfo *Name, |
| 445 | SourceLocation Loc, |
| 446 | SourceRange TypeRange) { |
| 447 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator, |
| 448 | Name, Loc, TypeRange); |
| 449 | if (Var && !Var->isInvalidDecl()) |
| 450 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 451 | return Var; |
| 452 | } |
| 453 | |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 454 | Sema::OwningExprResult |
| 455 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
| 456 | // FIXME: Clean this up a bit |
| 457 | NamedDecl *D = E->getDecl(); |
| 458 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 459 | if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) { |
| 460 | assert(false && "Cannot reduce non-type template parameter depth yet"); |
| 461 | return getSema().ExprError(); |
| 462 | } |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 463 | |
| 464 | // If the corresponding template argument is NULL or non-existent, it's |
| 465 | // because we are performing instantiation from explicitly-specified |
| 466 | // template arguments in a function template, but there were some |
| 467 | // arguments left unspecified. |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 468 | if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
| 469 | NTTP->getPosition())) |
| 470 | return SemaRef.Owned(E->Retain()); |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 471 | |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 472 | const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(), |
| 473 | NTTP->getPosition()); |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 474 | |
| 475 | // The template argument itself might be an expression, in which |
| 476 | // case we just return that expression. |
| 477 | if (Arg.getKind() == TemplateArgument::Expression) |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 478 | return SemaRef.Owned(Arg.getAsExpr()->Retain()); |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 479 | |
| 480 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 481 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 482 | |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 483 | VD = cast_or_null<ValueDecl>(getSema().FindInstantiatedDecl(VD)); |
| 484 | if (!VD) |
| 485 | return SemaRef.ExprError(); |
| 486 | |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 487 | return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(), |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 488 | /*FIXME:*/false, /*FIXME:*/false); |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | assert(Arg.getKind() == TemplateArgument::Integral); |
| 492 | QualType T = Arg.getIntegralType(); |
| 493 | if (T->isCharType() || T->isWideCharType()) |
| 494 | return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral( |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 495 | Arg.getAsIntegral()->getZExtValue(), |
| 496 | T->isWideCharType(), |
| 497 | T, |
| 498 | E->getSourceRange().getBegin())); |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 499 | if (T->isBooleanType()) |
| 500 | return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr( |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 501 | Arg.getAsIntegral()->getBoolValue(), |
| 502 | T, |
| 503 | E->getSourceRange().getBegin())); |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 504 | |
| 505 | assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T)); |
| 506 | return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral( |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 507 | *Arg.getAsIntegral(), |
| 508 | T, |
| 509 | E->getSourceRange().getBegin())); |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 510 | } |
| 511 | |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 512 | NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D); |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 513 | if (!InstD) |
| 514 | return SemaRef.ExprError(); |
| 515 | |
Anders Carlsson | 8197c48 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 516 | // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl, |
| 517 | // we need to get the underlying decl. |
| 518 | // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this? |
| 519 | InstD = InstD->getUnderlyingDecl(); |
| 520 | |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 521 | // FIXME: nested-name-specifier for QualifiedDeclRefExpr |
| 522 | return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD, |
| 523 | /*FIXME:*/false, |
| 524 | /*FIXME:*/0, |
| 525 | /*FIXME:*/false); |
| 526 | } |
| 527 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 528 | QualType |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 529 | TemplateInstantiator::TransformTemplateTypeParmType( |
| 530 | const TemplateTypeParmType *T) { |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 531 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 532 | // Replace the template type parameter with its corresponding |
| 533 | // template argument. |
Douglas Gregor | ecd63b8 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 534 | |
| 535 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 536 | // because we are performing instantiation from explicitly-specified |
| 537 | // template arguments in a function template class, but there were some |
| 538 | // arguments left unspecified. |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 539 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) |
| 540 | return QualType(T, 0); |
| 541 | |
| 542 | assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind() |
| 543 | == TemplateArgument::Type && |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 544 | "Template argument kind mismatch"); |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 545 | |
| 546 | return TemplateArgs(T->getDepth(), T->getIndex()).getAsType(); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | // The template type parameter comes from an inner template (e.g., |
| 550 | // the template parameter list of a member template inside the |
| 551 | // template we are instantiating). Create a new template type |
| 552 | // parameter with the template "level" reduced by one. |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 553 | return getSema().Context.getTemplateTypeParmType( |
| 554 | T->getDepth() - TemplateArgs.getNumLevels(), |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 555 | T->getIndex(), |
| 556 | T->isParameterPack(), |
| 557 | T->getName()); |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 558 | } |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 559 | |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 560 | /// \brief Perform substitution on the type T with a given set of template |
| 561 | /// arguments. |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 562 | /// |
| 563 | /// This routine substitutes the given template arguments into the |
| 564 | /// type T and produces the instantiated type. |
| 565 | /// |
| 566 | /// \param T the type into which the template arguments will be |
| 567 | /// substituted. If this type is not dependent, it will be returned |
| 568 | /// immediately. |
| 569 | /// |
| 570 | /// \param TemplateArgs the template arguments that will be |
| 571 | /// substituted for the top-level template parameters within T. |
| 572 | /// |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 573 | /// \param Loc the location in the source code where this substitution |
| 574 | /// is being performed. It will typically be the location of the |
| 575 | /// declarator (if we're instantiating the type of some declaration) |
| 576 | /// or the location of the type in the source code (if, e.g., we're |
| 577 | /// instantiating the type of a cast expression). |
| 578 | /// |
| 579 | /// \param Entity the name of the entity associated with a declaration |
| 580 | /// being instantiated (if any). May be empty to indicate that there |
| 581 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 582 | /// a cast expression) or that the entity has no name (e.g., an |
| 583 | /// unnamed function parameter). |
| 584 | /// |
| 585 | /// \returns If the instantiation succeeds, the instantiated |
| 586 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 587 | QualType Sema::SubstType(QualType T, |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 588 | const MultiLevelTemplateArgumentList &TemplateArgs, |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 589 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | 56d25a7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 590 | assert(!ActiveTemplateInstantiations.empty() && |
| 591 | "Cannot perform an instantiation without some context on the " |
| 592 | "instantiation stack"); |
| 593 | |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 594 | // If T is not a dependent type, there is nothing to do. |
| 595 | if (!T->isDependentType()) |
| 596 | return T; |
| 597 | |
Douglas Gregor | 841324a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 598 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 599 | return Instantiator.TransformType(T); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 600 | } |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 601 | |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 602 | /// \brief Perform substitution on the base class specifiers of the |
| 603 | /// given class template specialization. |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 604 | /// |
| 605 | /// Produces a diagnostic and returns true on error, returns false and |
| 606 | /// attaches the instantiated base classes to the class template |
| 607 | /// specialization if successful. |
| 608 | bool |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 609 | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 610 | CXXRecordDecl *Pattern, |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 611 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 612 | bool Invalid = false; |
Douglas Gregor | 4ac20ef | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 613 | llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 614 | for (ClassTemplateSpecializationDecl::base_class_iterator |
| 615 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | d9572a1 | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 616 | Base != BaseEnd; ++Base) { |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 617 | if (!Base->getType()->isDependentType()) { |
Fariborz Jahanian | 1373b6f | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 618 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 619 | continue; |
| 620 | } |
| 621 | |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 622 | QualType BaseType = SubstType(Base->getType(), |
| 623 | TemplateArgs, |
| 624 | Base->getSourceRange().getBegin(), |
| 625 | DeclarationName()); |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 626 | if (BaseType.isNull()) { |
| 627 | Invalid = true; |
| 628 | continue; |
| 629 | } |
| 630 | |
| 631 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 632 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 633 | Base->getSourceRange(), |
| 634 | Base->isVirtual(), |
| 635 | Base->getAccessSpecifierAsWritten(), |
| 636 | BaseType, |
| 637 | /*FIXME: Not totally accurate */ |
| 638 | Base->getSourceRange().getBegin())) |
| 639 | InstantiatedBases.push_back(InstantiatedBase); |
| 640 | else |
| 641 | Invalid = true; |
| 642 | } |
| 643 | |
Douglas Gregor | d9572a1 | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 644 | if (!Invalid && |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 645 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 646 | InstantiatedBases.size())) |
| 647 | Invalid = true; |
| 648 | |
| 649 | return Invalid; |
| 650 | } |
| 651 | |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 652 | /// \brief Instantiate the definition of a class from a given pattern. |
| 653 | /// |
| 654 | /// \param PointOfInstantiation The point of instantiation within the |
| 655 | /// source code. |
| 656 | /// |
| 657 | /// \param Instantiation is the declaration whose definition is being |
| 658 | /// instantiated. This will be either a class template specialization |
| 659 | /// or a member class of a class template specialization. |
| 660 | /// |
| 661 | /// \param Pattern is the pattern from which the instantiation |
| 662 | /// occurs. This will be either the declaration of a class template or |
| 663 | /// the declaration of a member class of a class template. |
| 664 | /// |
| 665 | /// \param TemplateArgs The template arguments to be substituted into |
| 666 | /// the pattern. |
| 667 | /// |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 668 | /// \param TSK the kind of implicit or explicit instantiation to perform. |
Douglas Gregor | b35c799 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 669 | /// |
| 670 | /// \param Complain whether to complain if the class cannot be instantiated due |
| 671 | /// to the lack of a definition. |
| 672 | /// |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 673 | /// \returns true if an error occurred, false otherwise. |
| 674 | bool |
| 675 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 676 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 677 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 678 | TemplateSpecializationKind TSK, |
Douglas Gregor | b35c799 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 679 | bool Complain) { |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 680 | bool Invalid = false; |
John McCall | d64b41e | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 681 | |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 682 | CXXRecordDecl *PatternDef |
| 683 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 684 | if (!PatternDef) { |
Douglas Gregor | b35c799 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 685 | if (!Complain) { |
| 686 | // Say nothing |
| 687 | } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 688 | Diag(PointOfInstantiation, |
| 689 | diag::err_implicit_instantiate_member_undefined) |
| 690 | << Context.getTypeDeclType(Instantiation); |
| 691 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 692 | } else { |
Douglas Gregor | fd79ac6 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 693 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 694 | << (TSK != TSK_ImplicitInstantiation) |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 695 | << Context.getTypeDeclType(Instantiation); |
| 696 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 697 | } |
| 698 | return true; |
| 699 | } |
| 700 | Pattern = PatternDef; |
| 701 | |
Douglas Gregor | 42c4852 | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 702 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 703 | if (Inst) |
| 704 | return true; |
| 705 | |
| 706 | // Enter the scope of this instantiation. We don't use |
| 707 | // PushDeclContext because we don't have a scope. |
| 708 | DeclContext *PreviousContext = CurContext; |
| 709 | CurContext = Instantiation; |
| 710 | |
| 711 | // Start the definition of this instantiation. |
| 712 | Instantiation->startDefinition(); |
| 713 | |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 714 | // Do substitution on the base class specifiers. |
| 715 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 716 | Invalid = true; |
| 717 | |
Douglas Gregor | 4ac20ef | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 718 | llvm::SmallVector<DeclPtrTy, 4> Fields; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 719 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
| 720 | MemberEnd = Pattern->decls_end(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 721 | Member != MemberEnd; ++Member) { |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 722 | Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs); |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 723 | if (NewMember) { |
| 724 | if (NewMember->isInvalidDecl()) |
| 725 | Invalid = true; |
| 726 | else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 727 | Fields.push_back(DeclPtrTy::make(Field)); |
Anders Carlsson | 8197c48 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 728 | else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember)) |
| 729 | Instantiation->addDecl(UD); |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 730 | } else { |
| 731 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 732 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 733 | // true. For now, we expect to skip some members that we can't yet handle. |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | // Finish checking fields. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 738 | ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation), |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 739 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 740 | 0); |
| 741 | |
| 742 | // Add any implicitly-declared members that we might need. |
| 743 | AddImplicitlyDeclaredMembersToClass(Instantiation); |
| 744 | |
| 745 | // Exit the scope of this instantiation. |
| 746 | CurContext = PreviousContext; |
| 747 | |
Douglas Gregor | dc18e89 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 748 | if (!Invalid) |
| 749 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 750 | |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 751 | // If this is an explicit instantiation, instantiate our members, too. |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 752 | if (!Invalid && TSK != TSK_ImplicitInstantiation) { |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 753 | Inst.Clear(); |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 754 | InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs, |
| 755 | TSK); |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 756 | } |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 757 | |
Douglas Gregor | cc88797 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 758 | return Invalid; |
| 759 | } |
| 760 | |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 761 | bool |
| 762 | Sema::InstantiateClassTemplateSpecialization( |
| 763 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 764 | TemplateSpecializationKind TSK, |
Douglas Gregor | b35c799 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 765 | bool Complain) { |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 766 | // Perform the actual instantiation on the canonical declaration. |
| 767 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
Argiris Kirtzidis | 17c7cab | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 768 | ClassTemplateSpec->getCanonicalDecl()); |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 769 | |
| 770 | // We can only instantiate something that hasn't already been |
| 771 | // instantiated or specialized. Fail without any diagnostics: our |
| 772 | // caller will provide an error message. |
| 773 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) |
| 774 | return true; |
| 775 | |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 776 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 777 | CXXRecordDecl *Pattern = 0; |
Douglas Gregor | 58944ac | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | 21530c5 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 779 | // C++ [temp.class.spec.match]p1: |
| 780 | // When a class template is used in a context that requires an |
| 781 | // instantiation of the class, it is necessary to determine |
| 782 | // whether the instantiation is to be generated using the primary |
| 783 | // template or one of the partial specializations. This is done by |
| 784 | // matching the template arguments of the class template |
| 785 | // specialization with the template argument lists of the partial |
| 786 | // specializations. |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 787 | typedef std::pair<ClassTemplatePartialSpecializationDecl *, |
| 788 | TemplateArgumentList *> MatchResult; |
| 789 | llvm::SmallVector<MatchResult, 4> Matched; |
Douglas Gregor | 58944ac | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 790 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 791 | Partial = Template->getPartialSpecializations().begin(), |
| 792 | PartialEnd = Template->getPartialSpecializations().end(); |
| 793 | Partial != PartialEnd; |
| 794 | ++Partial) { |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 795 | TemplateDeductionInfo Info(Context); |
| 796 | if (TemplateDeductionResult Result |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 797 | = DeduceTemplateArguments(&*Partial, |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 798 | ClassTemplateSpec->getTemplateArgs(), |
| 799 | Info)) { |
| 800 | // FIXME: Store the failed-deduction information for use in |
| 801 | // diagnostics, later. |
| 802 | (void)Result; |
| 803 | } else { |
| 804 | Matched.push_back(std::make_pair(&*Partial, Info.take())); |
| 805 | } |
Douglas Gregor | 58944ac | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | if (Matched.size() == 1) { |
Douglas Gregor | 21530c5 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 809 | // -- If exactly one matching specialization is found, the |
| 810 | // instantiation is generated from that specialization. |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 811 | Pattern = Matched[0].first; |
Douglas Gregor | 7b0b83f | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 812 | ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second); |
Douglas Gregor | 58944ac | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 813 | } else if (Matched.size() > 1) { |
Douglas Gregor | 21530c5 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 814 | // -- If more than one matching specialization is found, the |
| 815 | // partial order rules (14.5.4.2) are used to determine |
| 816 | // whether one of the specializations is more specialized |
| 817 | // than the others. If none of the specializations is more |
| 818 | // specialized than all of the other matching |
| 819 | // specializations, then the use of the class template is |
| 820 | // ambiguous and the program is ill-formed. |
Douglas Gregor | 58944ac | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 821 | // FIXME: Implement partial ordering of class template partial |
| 822 | // specializations. |
| 823 | Diag(ClassTemplateSpec->getLocation(), |
| 824 | diag::unsup_template_partial_spec_ordering); |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 825 | |
| 826 | // FIXME: Temporary hack to fall back to the primary template |
| 827 | ClassTemplateDecl *OrigTemplate = Template; |
| 828 | while (OrigTemplate->getInstantiatedFromMemberTemplate()) |
| 829 | OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); |
| 830 | |
| 831 | Pattern = OrigTemplate->getTemplatedDecl(); |
Douglas Gregor | 21530c5 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 832 | } else { |
| 833 | // -- If no matches are found, the instantiation is generated |
| 834 | // from the primary template. |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 835 | ClassTemplateDecl *OrigTemplate = Template; |
| 836 | while (OrigTemplate->getInstantiatedFromMemberTemplate()) |
| 837 | OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); |
| 838 | |
| 839 | Pattern = OrigTemplate->getTemplatedDecl(); |
Douglas Gregor | 58944ac | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 840 | } |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 841 | |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 842 | // Note that this is an instantiation. |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 843 | ClassTemplateSpec->setSpecializationKind(TSK); |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 844 | |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 845 | bool Result = InstantiateClass(ClassTemplateSpec->getLocation(), |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 846 | ClassTemplateSpec, Pattern, |
| 847 | getTemplateInstantiationArgs(ClassTemplateSpec), |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 848 | TSK, |
Douglas Gregor | b35c799 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 849 | Complain); |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 850 | |
| 851 | for (unsigned I = 0, N = Matched.size(); I != N; ++I) { |
| 852 | // FIXME: Implement TemplateArgumentList::Destroy! |
| 853 | // if (Matched[I].first != Pattern) |
| 854 | // Matched[I].second->Destroy(Context); |
| 855 | } |
| 856 | |
| 857 | return Result; |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 858 | } |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 859 | |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 860 | /// \brief Instantiates the definitions of all of the member |
| 861 | /// of the given class, which is an instantiation of a class template |
| 862 | /// or a member class of a template. |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 863 | void |
| 864 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 865 | CXXRecordDecl *Instantiation, |
| 866 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 867 | TemplateSpecializationKind TSK) { |
| 868 | // FIXME: extern templates |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 869 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(), |
| 870 | DEnd = Instantiation->decls_end(); |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 871 | D != DEnd; ++D) { |
| 872 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 873 | if (!Function->getBody()) |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 874 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 875 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
Douglas Gregor | 181fe79 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 876 | if (Var->isStaticDataMember()) |
| 877 | InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 878 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
| 879 | if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) { |
| 880 | assert(Record->getInstantiatedFromMemberClass() && |
| 881 | "Missing instantiated-from-template information"); |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 882 | InstantiateClass(PointOfInstantiation, Record, |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 883 | Record->getInstantiatedFromMemberClass(), |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 884 | TemplateArgs, |
| 885 | TSK); |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | /// \brief Instantiate the definitions of all of the members of the |
| 892 | /// given class template specialization, which was named as part of an |
| 893 | /// explicit instantiation. |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 894 | void |
| 895 | Sema::InstantiateClassTemplateSpecializationMembers( |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 896 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 897 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 898 | TemplateSpecializationKind TSK) { |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 899 | // C++0x [temp.explicit]p7: |
| 900 | // An explicit instantiation that names a class template |
| 901 | // specialization is an explicit instantion of the same kind |
| 902 | // (declaration or definition) of each of its members (not |
| 903 | // including members inherited from base classes) that has not |
| 904 | // been previously explicitly specialized in the translation unit |
| 905 | // containing the explicit instantiation, except as described |
| 906 | // below. |
| 907 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
Douglas Gregor | 8e7e460 | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 908 | getTemplateInstantiationArgs(ClassTemplateSpec), |
| 909 | TSK); |
Douglas Gregor | df9f5d1 | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Douglas Gregor | 23a44be | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 912 | Sema::OwningStmtResult |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 913 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 23a44be | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 914 | if (!S) |
| 915 | return Owned(S); |
| 916 | |
| 917 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 918 | SourceLocation(), |
| 919 | DeclarationName()); |
| 920 | return Instantiator.TransformStmt(S); |
| 921 | } |
| 922 | |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 923 | Sema::OwningExprResult |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 924 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 9d87976 | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 925 | if (!E) |
| 926 | return Owned(E); |
| 927 | |
| 928 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 929 | SourceLocation(), |
| 930 | DeclarationName()); |
| 931 | return Instantiator.TransformExpr(E); |
| 932 | } |
| 933 | |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 934 | /// \brief Do template substitution on a nested-name-specifier. |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 935 | NestedNameSpecifier * |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 936 | Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS, |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 937 | SourceRange Range, |
| 938 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 12431cb | 2009-08-06 05:28:30 +0000 | [diff] [blame] | 939 | TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(), |
| 940 | DeclarationName()); |
| 941 | return Instantiator.TransformNestedNameSpecifier(NNS, Range); |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 942 | } |
Douglas Gregor | 15a9285 | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 943 | |
| 944 | TemplateName |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 945 | Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc, |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 946 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 214d046 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 947 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 948 | DeclarationName()); |
| 949 | return Instantiator.TransformTemplateName(Name); |
Douglas Gregor | 15a9285 | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 950 | } |
Douglas Gregor | b320b9e | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 951 | |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 952 | TemplateArgument Sema::Subst(TemplateArgument Arg, |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 953 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 2999faa | 2009-08-04 22:27:00 +0000 | [diff] [blame] | 954 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 955 | DeclarationName()); |
| 956 | return Instantiator.TransformTemplateArgument(Arg); |
Douglas Gregor | b320b9e | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 957 | } |