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