Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template instantiation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | |
| 13 | #include "Sema.h" |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Expr.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
| 19 | #include "clang/Parse/DeclSpec.h" |
| 20 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===/ |
| 26 | // Template Instantiation Support |
| 27 | //===----------------------------------------------------------------------===/ |
| 28 | |
Douglas Gregor | 54dabfc | 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 | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 33 | // Template arguments for a class template specialization. |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 34 | if (ClassTemplateSpecializationDecl *Spec |
| 35 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 36 | return Spec->getTemplateInstantiationArgs(); |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 37 | |
Douglas Gregor | 1637be7 | 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 | 54dabfc | 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 | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 54 | return EnclosingTemplate->getTemplateInstantiationArgs(); |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 57 | Sema::InstantiatingTemplate:: |
| 58 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 59 | Decl *Entity, |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 60 | SourceRange InstantiationRange) |
| 61 | : SemaRef(SemaRef) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 62 | |
| 63 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 64 | InstantiationRange); |
| 65 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 66 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 67 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 68 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 69 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 70 | Inst.TemplateArgs = 0; |
| 71 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | df667e7 | 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 | 26dce44 | 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 | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 102 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 103 | SourceLocation PointOfInstantiation, |
Douglas Gregor | cca9e96 | 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 | 637a409 | 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 | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 139 | = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution; |
Douglas Gregor | 637a409 | 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 | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 150 | void Sema::InstantiatingTemplate::Clear() { |
| 151 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 152 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 153 | Invalid = true; |
| 154 | } |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Douglas Gregor | df667e7 | 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 | ee1828a | 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 | cca9e96 | 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 | ee1828a | 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 | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 182 | switch (Active->Kind) { |
| 183 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
Douglas Gregor | f3e7ce4 | 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 | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 193 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 1637be7 | 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 | f3e7ce4 | 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 | 7caa682 | 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 | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 208 | } |
Douglas Gregor | df667e7 | 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 | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 215 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 216 | Active->TemplateArgs, |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 217 | Active->NumTemplateArgs, |
| 218 | Context.PrintingPolicy); |
Douglas Gregor | df667e7 | 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 | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 225 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 226 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: { |
| 227 | FunctionTemplateDecl *FnTmpl |
| 228 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 229 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 230 | diag::note_explicit_template_arg_substitution_here) |
| 231 | << FnTmpl << Active->InstantiationRange; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 232 | break; |
| 233 | } |
Douglas Gregor | cca9e96 | 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 | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 252 | } |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Douglas Gregor | 5e9f35c | 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 | cca9e96 | 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 | 5e9f35c | 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 | cca9e96 | 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 | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | return false; |
| 283 | } |
| 284 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 285 | //===----------------------------------------------------------------------===/ |
| 286 | // Template Instantiation for Types |
| 287 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 288 | namespace { |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 289 | class VISIBILITY_HIDDEN TemplateInstantiator |
| 290 | : public TreeTransform<TemplateInstantiator> |
| 291 | { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 292 | const TemplateArgumentList &TemplateArgs; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 293 | SourceLocation Loc; |
| 294 | DeclarationName Entity; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 295 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 296 | public: |
Douglas Gregor | 577f75a | 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 | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 303 | |
Douglas Gregor | 577f75a | 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 | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 311 | } |
Douglas Gregor | 577f75a | 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 | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 322 | |
Douglas Gregor | 577f75a | 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 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 334 | /// \brief Transforms a template type parameter type by performing |
| 335 | /// substitution of the corresponding template type argument. |
| 336 | QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T); |
| 337 | }; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 340 | Sema::OwningExprResult TemplateInstantiator::TransformExpr(Expr *E) { |
| 341 | return getSema().InstantiateExpr(E, TemplateArgs); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 344 | Decl *TemplateInstantiator::TransformDecl(Decl *D) { |
| 345 | return SemaRef.InstantiateCurrentDeclRef(cast_or_null<NamedDecl>(D)); |
| 346 | } |
| 347 | |
| 348 | NestedNameSpecifier * |
| 349 | TemplateInstantiator::TransformNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 350 | SourceRange Range) { |
| 351 | return getSema().InstantiateNestedNameSpecifier(NNS, Range, TemplateArgs); |
| 352 | } |
| 353 | |
| 354 | TemplateName |
| 355 | TemplateInstantiator::TransformTemplateName(TemplateName Template) { |
| 356 | return getSema().InstantiateTemplateName(Template, /*FIXME*/Loc, |
| 357 | TemplateArgs); |
| 358 | } |
| 359 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 360 | QualType |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 361 | TemplateInstantiator::TransformTemplateTypeParmType( |
| 362 | const TemplateTypeParmType *T) { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 363 | if (T->getDepth() == 0) { |
| 364 | // Replace the template type parameter with its corresponding |
| 365 | // template argument. |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 366 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 367 | // FIXME: When dealing with member templates, we might end up with multiple |
| 368 | /// levels of template arguments that we're substituting into concurrently. |
| 369 | |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 370 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 371 | // because we are performing instantiation from explicitly-specified |
| 372 | // template arguments in a function template class, but there were some |
| 373 | // arguments left unspecified. |
| 374 | if (T->getIndex() >= TemplateArgs.size() || |
| 375 | TemplateArgs[T->getIndex()].isNull()) |
| 376 | return QualType(T, 0); // Would be nice to keep the original type here |
| 377 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 378 | assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type && |
| 379 | "Template argument kind mismatch"); |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 380 | return TemplateArgs[T->getIndex()].getAsType(); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // The template type parameter comes from an inner template (e.g., |
| 384 | // the template parameter list of a member template inside the |
| 385 | // template we are instantiating). Create a new template type |
| 386 | // parameter with the template "level" reduced by one. |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 387 | return getSema().Context.getTemplateTypeParmType(T->getDepth() - 1, |
| 388 | T->getIndex(), |
| 389 | T->isParameterPack(), |
| 390 | T->getName()); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 391 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 392 | |
| 393 | /// \brief Instantiate the type T with a given set of template arguments. |
| 394 | /// |
| 395 | /// This routine substitutes the given template arguments into the |
| 396 | /// type T and produces the instantiated type. |
| 397 | /// |
| 398 | /// \param T the type into which the template arguments will be |
| 399 | /// substituted. If this type is not dependent, it will be returned |
| 400 | /// immediately. |
| 401 | /// |
| 402 | /// \param TemplateArgs the template arguments that will be |
| 403 | /// substituted for the top-level template parameters within T. |
| 404 | /// |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 405 | /// \param Loc the location in the source code where this substitution |
| 406 | /// is being performed. It will typically be the location of the |
| 407 | /// declarator (if we're instantiating the type of some declaration) |
| 408 | /// or the location of the type in the source code (if, e.g., we're |
| 409 | /// instantiating the type of a cast expression). |
| 410 | /// |
| 411 | /// \param Entity the name of the entity associated with a declaration |
| 412 | /// being instantiated (if any). May be empty to indicate that there |
| 413 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 414 | /// a cast expression) or that the entity has no name (e.g., an |
| 415 | /// unnamed function parameter). |
| 416 | /// |
| 417 | /// \returns If the instantiation succeeds, the instantiated |
| 418 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
| 419 | QualType Sema::InstantiateType(QualType T, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 420 | const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 421 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 422 | assert(!ActiveTemplateInstantiations.empty() && |
| 423 | "Cannot perform an instantiation without some context on the " |
| 424 | "instantiation stack"); |
| 425 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 426 | // If T is not a dependent type, there is nothing to do. |
| 427 | if (!T->isDependentType()) |
| 428 | return T; |
| 429 | |
Douglas Gregor | 577f75a | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 430 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 431 | return Instantiator.TransformType(T); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 432 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 433 | |
| 434 | /// \brief Instantiate the base class specifiers of the given class |
| 435 | /// template specialization. |
| 436 | /// |
| 437 | /// Produces a diagnostic and returns true on error, returns false and |
| 438 | /// attaches the instantiated base classes to the class template |
| 439 | /// specialization if successful. |
| 440 | bool |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 441 | Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 442 | CXXRecordDecl *Pattern, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 443 | const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 444 | bool Invalid = false; |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 445 | llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 446 | for (ClassTemplateSpecializationDecl::base_class_iterator |
| 447 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 448 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 449 | if (!Base->getType()->isDependentType()) { |
Fariborz Jahanian | 71c6e71 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 450 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 451 | continue; |
| 452 | } |
| 453 | |
| 454 | QualType BaseType = InstantiateType(Base->getType(), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 455 | TemplateArgs, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 456 | Base->getSourceRange().getBegin(), |
| 457 | DeclarationName()); |
| 458 | if (BaseType.isNull()) { |
| 459 | Invalid = true; |
| 460 | continue; |
| 461 | } |
| 462 | |
| 463 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 464 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 465 | Base->getSourceRange(), |
| 466 | Base->isVirtual(), |
| 467 | Base->getAccessSpecifierAsWritten(), |
| 468 | BaseType, |
| 469 | /*FIXME: Not totally accurate */ |
| 470 | Base->getSourceRange().getBegin())) |
| 471 | InstantiatedBases.push_back(InstantiatedBase); |
| 472 | else |
| 473 | Invalid = true; |
| 474 | } |
| 475 | |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 476 | if (!Invalid && |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 477 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 478 | InstantiatedBases.size())) |
| 479 | Invalid = true; |
| 480 | |
| 481 | return Invalid; |
| 482 | } |
| 483 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 484 | /// \brief Instantiate the definition of a class from a given pattern. |
| 485 | /// |
| 486 | /// \param PointOfInstantiation The point of instantiation within the |
| 487 | /// source code. |
| 488 | /// |
| 489 | /// \param Instantiation is the declaration whose definition is being |
| 490 | /// instantiated. This will be either a class template specialization |
| 491 | /// or a member class of a class template specialization. |
| 492 | /// |
| 493 | /// \param Pattern is the pattern from which the instantiation |
| 494 | /// occurs. This will be either the declaration of a class template or |
| 495 | /// the declaration of a member class of a class template. |
| 496 | /// |
| 497 | /// \param TemplateArgs The template arguments to be substituted into |
| 498 | /// the pattern. |
| 499 | /// |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 500 | /// \returns true if an error occurred, false otherwise. |
| 501 | bool |
| 502 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 503 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 504 | const TemplateArgumentList &TemplateArgs, |
| 505 | bool ExplicitInstantiation) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 506 | bool Invalid = false; |
| 507 | |
| 508 | CXXRecordDecl *PatternDef |
| 509 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 510 | if (!PatternDef) { |
| 511 | if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
| 512 | Diag(PointOfInstantiation, |
| 513 | diag::err_implicit_instantiate_member_undefined) |
| 514 | << Context.getTypeDeclType(Instantiation); |
| 515 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 516 | } else { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 517 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 518 | << ExplicitInstantiation |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 519 | << Context.getTypeDeclType(Instantiation); |
| 520 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 521 | } |
| 522 | return true; |
| 523 | } |
| 524 | Pattern = PatternDef; |
| 525 | |
Douglas Gregor | d048bb7 | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 526 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 527 | if (Inst) |
| 528 | return true; |
| 529 | |
| 530 | // Enter the scope of this instantiation. We don't use |
| 531 | // PushDeclContext because we don't have a scope. |
| 532 | DeclContext *PreviousContext = CurContext; |
| 533 | CurContext = Instantiation; |
| 534 | |
| 535 | // Start the definition of this instantiation. |
| 536 | Instantiation->startDefinition(); |
| 537 | |
| 538 | // Instantiate the base class specifiers. |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 539 | if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 540 | Invalid = true; |
| 541 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 542 | llvm::SmallVector<DeclPtrTy, 4> Fields; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 543 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
| 544 | MemberEnd = Pattern->decls_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 545 | Member != MemberEnd; ++Member) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 546 | Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 547 | if (NewMember) { |
| 548 | if (NewMember->isInvalidDecl()) |
| 549 | Invalid = true; |
| 550 | else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 551 | Fields.push_back(DeclPtrTy::make(Field)); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 552 | } else { |
| 553 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 554 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 555 | // true. For now, we expect to skip some members that we can't yet handle. |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | // Finish checking fields. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 560 | ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 561 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 562 | 0); |
| 563 | |
| 564 | // Add any implicitly-declared members that we might need. |
| 565 | AddImplicitlyDeclaredMembersToClass(Instantiation); |
| 566 | |
| 567 | // Exit the scope of this instantiation. |
| 568 | CurContext = PreviousContext; |
| 569 | |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 570 | if (!Invalid) |
| 571 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 572 | |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 573 | // If this is an explicit instantiation, instantiate our members, too. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 574 | if (!Invalid && ExplicitInstantiation) { |
| 575 | Inst.Clear(); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 576 | InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 577 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 578 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 579 | return Invalid; |
| 580 | } |
| 581 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 582 | bool |
| 583 | Sema::InstantiateClassTemplateSpecialization( |
| 584 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 585 | bool ExplicitInstantiation) { |
| 586 | // Perform the actual instantiation on the canonical declaration. |
| 587 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 588 | ClassTemplateSpec->getCanonicalDecl()); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 589 | |
| 590 | // We can only instantiate something that hasn't already been |
| 591 | // instantiated or specialized. Fail without any diagnostics: our |
| 592 | // caller will provide an error message. |
| 593 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) |
| 594 | return true; |
| 595 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 596 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 597 | CXXRecordDecl *Pattern = Template->getTemplatedDecl(); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 598 | const TemplateArgumentList *TemplateArgs |
| 599 | = &ClassTemplateSpec->getTemplateArgs(); |
| 600 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 601 | // C++ [temp.class.spec.match]p1: |
| 602 | // When a class template is used in a context that requires an |
| 603 | // instantiation of the class, it is necessary to determine |
| 604 | // whether the instantiation is to be generated using the primary |
| 605 | // template or one of the partial specializations. This is done by |
| 606 | // matching the template arguments of the class template |
| 607 | // specialization with the template argument lists of the partial |
| 608 | // specializations. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 609 | typedef std::pair<ClassTemplatePartialSpecializationDecl *, |
| 610 | TemplateArgumentList *> MatchResult; |
| 611 | llvm::SmallVector<MatchResult, 4> Matched; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 612 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 613 | Partial = Template->getPartialSpecializations().begin(), |
| 614 | PartialEnd = Template->getPartialSpecializations().end(); |
| 615 | Partial != PartialEnd; |
| 616 | ++Partial) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 617 | TemplateDeductionInfo Info(Context); |
| 618 | if (TemplateDeductionResult Result |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 619 | = DeduceTemplateArguments(&*Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 620 | ClassTemplateSpec->getTemplateArgs(), |
| 621 | Info)) { |
| 622 | // FIXME: Store the failed-deduction information for use in |
| 623 | // diagnostics, later. |
| 624 | (void)Result; |
| 625 | } else { |
| 626 | Matched.push_back(std::make_pair(&*Partial, Info.take())); |
| 627 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | if (Matched.size() == 1) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 631 | // -- If exactly one matching specialization is found, the |
| 632 | // instantiation is generated from that specialization. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 633 | Pattern = Matched[0].first; |
| 634 | TemplateArgs = Matched[0].second; |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 635 | ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 636 | } else if (Matched.size() > 1) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 637 | // -- If more than one matching specialization is found, the |
| 638 | // partial order rules (14.5.4.2) are used to determine |
| 639 | // whether one of the specializations is more specialized |
| 640 | // than the others. If none of the specializations is more |
| 641 | // specialized than all of the other matching |
| 642 | // specializations, then the use of the class template is |
| 643 | // ambiguous and the program is ill-formed. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 644 | // FIXME: Implement partial ordering of class template partial |
| 645 | // specializations. |
| 646 | Diag(ClassTemplateSpec->getLocation(), |
| 647 | diag::unsup_template_partial_spec_ordering); |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 648 | } else { |
| 649 | // -- If no matches are found, the instantiation is generated |
| 650 | // from the primary template. |
| 651 | |
| 652 | // Since we initialized the pattern and template arguments from |
| 653 | // the primary template, there is nothing more we need to do here. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 654 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 655 | |
| 656 | // Note that this is an instantiation. |
| 657 | ClassTemplateSpec->setSpecializationKind( |
| 658 | ExplicitInstantiation? TSK_ExplicitInstantiation |
| 659 | : TSK_ImplicitInstantiation); |
| 660 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 661 | bool Result = InstantiateClass(ClassTemplateSpec->getLocation(), |
| 662 | ClassTemplateSpec, Pattern, *TemplateArgs, |
| 663 | ExplicitInstantiation); |
| 664 | |
| 665 | for (unsigned I = 0, N = Matched.size(); I != N; ++I) { |
| 666 | // FIXME: Implement TemplateArgumentList::Destroy! |
| 667 | // if (Matched[I].first != Pattern) |
| 668 | // Matched[I].second->Destroy(Context); |
| 669 | } |
| 670 | |
| 671 | return Result; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 672 | } |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 673 | |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 674 | /// \brief Instantiate the definitions of all of the member of the |
| 675 | /// given class, which is an instantiation of a class template or a |
| 676 | /// member class of a template. |
| 677 | void |
| 678 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
| 679 | CXXRecordDecl *Instantiation, |
| 680 | const TemplateArgumentList &TemplateArgs) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 681 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(), |
| 682 | DEnd = Instantiation->decls_end(); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 683 | D != DEnd; ++D) { |
| 684 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 685 | if (!Function->getBody()) |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 686 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 687 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 688 | if (Var->isStaticDataMember()) |
| 689 | InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 690 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
| 691 | if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) { |
| 692 | assert(Record->getInstantiatedFromMemberClass() && |
| 693 | "Missing instantiated-from-template information"); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 694 | InstantiateClass(PointOfInstantiation, Record, |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 695 | Record->getInstantiatedFromMemberClass(), |
| 696 | TemplateArgs, true); |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /// \brief Instantiate the definitions of all of the members of the |
| 703 | /// given class template specialization, which was named as part of an |
| 704 | /// explicit instantiation. |
| 705 | void Sema::InstantiateClassTemplateSpecializationMembers( |
| 706 | SourceLocation PointOfInstantiation, |
| 707 | ClassTemplateSpecializationDecl *ClassTemplateSpec) { |
| 708 | // C++0x [temp.explicit]p7: |
| 709 | // An explicit instantiation that names a class template |
| 710 | // specialization is an explicit instantion of the same kind |
| 711 | // (declaration or definition) of each of its members (not |
| 712 | // including members inherited from base classes) that has not |
| 713 | // been previously explicitly specialized in the translation unit |
| 714 | // containing the explicit instantiation, except as described |
| 715 | // below. |
| 716 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
| 717 | ClassTemplateSpec->getTemplateArgs()); |
| 718 | } |
| 719 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 720 | /// \brief Instantiate a nested-name-specifier. |
| 721 | NestedNameSpecifier * |
| 722 | Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 723 | SourceRange Range, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 724 | const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 725 | // Instantiate the prefix of this nested name specifier. |
| 726 | NestedNameSpecifier *Prefix = NNS->getPrefix(); |
| 727 | if (Prefix) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 728 | Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 729 | if (!Prefix) |
| 730 | return 0; |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 733 | switch (NNS->getKind()) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 734 | case NestedNameSpecifier::Identifier: { |
| 735 | assert(Prefix && |
| 736 | "Can't have an identifier nested-name-specifier with no prefix"); |
| 737 | CXXScopeSpec SS; |
| 738 | // FIXME: The source location information is all wrong. |
| 739 | SS.setRange(Range); |
| 740 | SS.setScopeRep(Prefix); |
| 741 | return static_cast<NestedNameSpecifier *>( |
| 742 | ActOnCXXNestedNameSpecifier(0, SS, |
| 743 | Range.getEnd(), |
| 744 | Range.getEnd(), |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 745 | *NNS->getAsIdentifier())); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 746 | break; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 747 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 748 | |
| 749 | case NestedNameSpecifier::Namespace: |
| 750 | case NestedNameSpecifier::Global: |
| 751 | return NNS; |
| 752 | |
| 753 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 754 | case NestedNameSpecifier::TypeSpec: { |
| 755 | QualType T = QualType(NNS->getAsType(), 0); |
| 756 | if (!T->isDependentType()) |
| 757 | return NNS; |
| 758 | |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 759 | T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 760 | if (T.isNull()) |
| 761 | return 0; |
| 762 | |
Eli Friedman | 923f753 | 2009-06-13 04:51:30 +0000 | [diff] [blame] | 763 | if (T->isDependentType() || T->isRecordType() || |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 764 | (getLangOptions().CPlusPlus0x && T->isEnumeralType())) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 765 | assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 766 | return NestedNameSpecifier::Create(Context, Prefix, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 767 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 768 | T.getTypePtr()); |
| 769 | } |
| 770 | |
| 771 | Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T; |
| 772 | return 0; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 776 | // Required to silence a GCC warning |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 777 | return 0; |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 778 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 779 | |
| 780 | TemplateName |
| 781 | Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 782 | const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 783 | if (TemplateTemplateParmDecl *TTP |
| 784 | = dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 785 | Name.getAsTemplateDecl())) { |
| 786 | assert(TTP->getDepth() == 0 && |
| 787 | "Cannot reduce depth of a template template parameter"); |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 788 | assert(TemplateArgs[TTP->getPosition()].getAsDecl() && |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 789 | "Wrong kind of template template argument"); |
| 790 | ClassTemplateDecl *ClassTemplate |
| 791 | = dyn_cast<ClassTemplateDecl>( |
| 792 | TemplateArgs[TTP->getPosition()].getAsDecl()); |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 793 | assert(ClassTemplate && "Expected a class template"); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 794 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) { |
| 795 | NestedNameSpecifier *NNS |
| 796 | = InstantiateNestedNameSpecifier(QTN->getQualifier(), |
| 797 | /*FIXME=*/SourceRange(Loc), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 798 | TemplateArgs); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 799 | if (NNS) |
| 800 | return Context.getQualifiedTemplateName(NNS, |
| 801 | QTN->hasTemplateKeyword(), |
| 802 | ClassTemplate); |
| 803 | } |
| 804 | |
| 805 | return TemplateName(ClassTemplate); |
| 806 | } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
| 807 | NestedNameSpecifier *NNS |
| 808 | = InstantiateNestedNameSpecifier(DTN->getQualifier(), |
| 809 | /*FIXME=*/SourceRange(Loc), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 810 | TemplateArgs); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 811 | |
| 812 | if (!NNS) // FIXME: Not the best recovery strategy. |
| 813 | return Name; |
| 814 | |
| 815 | if (NNS->isDependent()) |
| 816 | return Context.getDependentTemplateName(NNS, DTN->getName()); |
| 817 | |
| 818 | // Somewhat redundant with ActOnDependentTemplateName. |
| 819 | CXXScopeSpec SS; |
| 820 | SS.setRange(SourceRange(Loc)); |
| 821 | SS.setScopeRep(NNS); |
| 822 | TemplateTy Template; |
| 823 | TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS); |
| 824 | if (TNK == TNK_Non_template) { |
| 825 | Diag(Loc, diag::err_template_kw_refers_to_non_template) |
| 826 | << DTN->getName(); |
| 827 | return Name; |
| 828 | } else if (TNK == TNK_Function_template) { |
| 829 | Diag(Loc, diag::err_template_kw_refers_to_non_template) |
| 830 | << DTN->getName(); |
| 831 | return Name; |
| 832 | } |
| 833 | |
| 834 | return Template.getAsVal<TemplateName>(); |
| 835 | } |
| 836 | |
| 837 | |
| 838 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 839 | // FIXME: Even if we're referring to a Decl that isn't a template template |
| 840 | // parameter, we may need to instantiate the outer contexts of that |
| 841 | // Decl. However, this won't be needed until we implement member templates. |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 842 | return Name; |
| 843 | } |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 844 | |
| 845 | TemplateArgument Sema::Instantiate(TemplateArgument Arg, |
| 846 | const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 670444e | 2009-08-04 22:27:00 +0000 | [diff] [blame] | 847 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 848 | DeclarationName()); |
| 849 | return Instantiator.TransformTemplateArgument(Arg); |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 850 | } |