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