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