Douglas Gregor | fe1e110 | 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 | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Expr.h" |
Douglas Gregor | fe1e110 | 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 | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===/ |
| 26 | // Template Instantiation Support |
| 27 | //===----------------------------------------------------------------------===/ |
| 28 | |
Douglas Gregor | 01afeef | 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 | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 31 | MultiLevelTemplateArgumentList |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 32 | Sema::getTemplateInstantiationArgs(NamedDecl *D) { |
Douglas Gregor | a654dd8 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | a654dd8 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 40 | while (!Ctx->isFileContext()) { |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 41 | // Add template arguments from a class template instantiation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | a654dd8 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 48 | Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); |
Douglas Gregor | cf91555 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | } |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 56 | // Add template arguments from a function template specialization. |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 57 | else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) { |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 58 | if (Function->getTemplateSpecializationKind() |
| 59 | == TSK_ExplicitSpecialization) |
| 60 | break; |
| 61 | |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 62 | if (const TemplateArgumentList *TemplateArgs |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 63 | = Function->getTemplateSpecializationArgs()) { |
| 64 | // Add the template arguments for this specialization. |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 65 | Result.addOuterTemplateArguments(TemplateArgs); |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 66 | |
Douglas Gregor | cf91555 | 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 | 970d530 | 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 | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 82 | } |
John McCall | 970d530 | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 83 | |
| 84 | Ctx = Ctx->getParent(); |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 85 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Douglas Gregor | a654dd8 | 2009-08-28 17:37:35 +0000 | [diff] [blame] | 87 | return Result; |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 90 | Sema::InstantiatingTemplate:: |
| 91 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 92 | Decl *Entity, |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 93 | SourceRange InstantiationRange) |
| 94 | : SemaRef(SemaRef) { |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 95 | |
| 96 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 97 | InstantiationRange); |
| 98 | if (!Invalid) { |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 99 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 100 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 101 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 102 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | c922083 | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 103 | Inst.TemplateArgs = 0; |
| 104 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | 79cf603 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 79cf603 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | Inst.Kind |
Douglas Gregor | 79cf603 | 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 | fcd5db3 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 136 | SourceLocation PointOfInstantiation, |
Douglas Gregor | ff6cbdf | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Douglas Gregor | ff6cbdf | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 160 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 637d998 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | Inst.Kind |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 172 | = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution; |
Douglas Gregor | 637d998 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
Anders Carlsson | 657bad4 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Anders Carlsson | 657bad4 | 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 | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 207 | void Sema::InstantiatingTemplate::Clear() { |
| 208 | if (!Invalid) { |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 209 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 210 | Invalid = true; |
| 211 | } |
Douglas Gregor | fcd5db3 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 214 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 215 | SourceLocation PointOfInstantiation, |
| 216 | SourceRange InstantiationRange) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | if (SemaRef.ActiveTemplateInstantiations.size() |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 218 | <= SemaRef.getLangOptions().InstantiationDepth) |
| 219 | return false; |
| 220 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | SemaRef.Diag(PointOfInstantiation, |
Douglas Gregor | 79cf603 | 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 | 4ea568f | 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 | ff6cbdf | 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 | 4ea568f | 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 | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 239 | switch (Active->Kind) { |
| 240 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
Douglas Gregor | 8567358 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 247 | DiagID) |
| 248 | << Context.getTypeDeclType(Record) |
| 249 | << Active->InstantiationRange; |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 250 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 4adbc6d | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 257 | DiagID) |
| 258 | << Function |
| 259 | << Active->InstantiationRange; |
Douglas Gregor | a6ef8f0 | 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 | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 265 | } |
Douglas Gregor | 79cf603 | 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 | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 272 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | Active->TemplateArgs, |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 274 | Active->NumTemplateArgs, |
| 275 | Context.PrintingPolicy); |
Douglas Gregor | 79cf603 | 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 | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 282 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 283 | case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | FunctionTemplateDecl *FnTmpl |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 285 | = cast<FunctionTemplateDecl>((Decl *)Active->Entity); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 286 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 287 | diag::note_explicit_template_arg_substitution_here) |
| 288 | << FnTmpl << Active->InstantiationRange; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 289 | break; |
| 290 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Douglas Gregor | ff6cbdf | 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 | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 308 | |
Anders Carlsson | 657bad4 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 313 | std::string TemplateArgsStr |
| 314 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | Active->TemplateArgs, |
Anders Carlsson | 657bad4 | 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 | dc6d2c3 | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 320 | << (FD->getNameAsString() + TemplateArgsStr) |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 321 | << Active->InstantiationRange; |
| 322 | break; |
| 323 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 325 | } |
Douglas Gregor | 4ea568f | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Douglas Gregor | 3383451 | 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 | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 338 | case ActiveTemplateInstantiation::TemplateInstantiation: |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 339 | case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: |
| 340 | |
Douglas Gregor | ff6cbdf | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Douglas Gregor | 3383451 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Douglas Gregor | ff6cbdf | 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 | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | return false; |
| 358 | } |
| 359 | |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 360 | //===----------------------------------------------------------------------===/ |
| 361 | // Template Instantiation for Types |
| 362 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 363 | namespace { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | class VISIBILITY_HIDDEN TemplateInstantiator |
| 365 | : public TreeTransform<TemplateInstantiator> { |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 366 | const MultiLevelTemplateArgumentList &TemplateArgs; |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 367 | SourceLocation Loc; |
| 368 | DeclarationName Entity; |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 370 | public: |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 371 | typedef TreeTransform<TemplateInstantiator> inherited; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
| 373 | TemplateInstantiator(Sema &SemaRef, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 374 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 375 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | DeclarationName Entity) |
| 377 | : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 378 | Entity(Entity) { } |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 379 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | d6ff332 | 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 | f61eca9 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 387 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Douglas Gregor | d6ff332 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | d6ff332 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 395 | /// \brief Sets the "base" location and entity when that |
| 396 | /// information is known based on another transformation. |
| 397 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 398 | this->Loc = Loc; |
| 399 | this->Entity = Entity; |
| 400 | } |
| 401 | |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 402 | /// \brief Transform the given declaration by instantiating a reference to |
| 403 | /// this declaration. |
| 404 | Decl *TransformDecl(Decl *D); |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 405 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | /// \brief Transform the definition of the given declaration by |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 407 | /// instantiating it. |
| 408 | Decl *TransformDefinition(Decl *D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 410 | /// \bried Transform the first qualifier within a scope by instantiating the |
| 411 | /// declaration. |
| 412 | NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); |
| 413 | |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 414 | /// \brief Rebuild the exception declaration and register the declaration |
| 415 | /// as an instantiated local. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T, |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 417 | DeclaratorInfo *Declarator, |
| 418 | IdentifierInfo *Name, |
| 419 | SourceLocation Loc, SourceRange TypeRange); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 421 | /// \brief Check for tag mismatches when instantiating an |
| 422 | /// elaborated type. |
| 423 | QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag); |
| 424 | |
Douglas Gregor | c95a1fa | 2009-11-04 07:01:15 +0000 | [diff] [blame] | 425 | Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E, |
| 426 | bool isAddressOfOperand); |
| 427 | Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E, |
| 428 | bool isAddressOfOperand); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame^] | 430 | Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E, |
| 431 | bool isAddressOfOperand); |
| 432 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | /// \brief Transforms a template type parameter type by performing |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 434 | /// substitution of the corresponding template type argument. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 435 | QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| 436 | TemplateTypeParmTypeLoc TL); |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 437 | }; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 440 | Decl *TemplateInstantiator::TransformDecl(Decl *D) { |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 441 | if (!D) |
| 442 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 444 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 445 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 446 | assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() && |
| 447 | "Wrong kind of template template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(), |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 449 | TTP->getPosition()).getAsDecl()); |
| 450 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | |
| 452 | // If the corresponding template argument is NULL or non-existent, it's |
| 453 | // because we are performing instantiation from explicitly-specified |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 454 | // template arguments in a function template, but there were some |
| 455 | // arguments left unspecified. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 457 | TTP->getPosition())) |
| 458 | return D; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 460 | // FIXME: Implement depth reduction of template template parameters |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | assert(false && |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 462 | "Reducing depth of template template parameters is not yet implemented"); |
Douglas Gregor | 71dc509 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 463 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 465 | return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs); |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 468 | Decl *TemplateInstantiator::TransformDefinition(Decl *D) { |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 469 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 470 | if (!Inst) |
| 471 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 473 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 474 | return Inst; |
| 475 | } |
| 476 | |
Douglas Gregor | a5cb6da | 2009-10-20 05:58:46 +0000 | [diff] [blame] | 477 | NamedDecl * |
| 478 | TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
| 479 | SourceLocation Loc) { |
| 480 | // If the first part of the nested-name-specifier was a template type |
| 481 | // parameter, instantiate that type parameter down to a tag type. |
| 482 | if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
| 483 | const TemplateTypeParmType *TTP |
| 484 | = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
| 485 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 486 | QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType(); |
| 487 | if (T.isNull()) |
| 488 | return cast_or_null<NamedDecl>(TransformDecl(D)); |
| 489 | |
| 490 | if (const TagType *Tag = T->getAs<TagType>()) |
| 491 | return Tag->getDecl(); |
| 492 | |
| 493 | // The resulting type is not a tag; complain. |
| 494 | getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
| 495 | return 0; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return cast_or_null<NamedDecl>(TransformDecl(D)); |
| 500 | } |
| 501 | |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 502 | VarDecl * |
| 503 | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | QualType T, |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 505 | DeclaratorInfo *Declarator, |
| 506 | IdentifierInfo *Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | SourceLocation Loc, |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 508 | SourceRange TypeRange) { |
| 509 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator, |
| 510 | Name, Loc, TypeRange); |
| 511 | if (Var && !Var->isInvalidDecl()) |
| 512 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 513 | return Var; |
| 514 | } |
| 515 | |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 516 | QualType |
| 517 | TemplateInstantiator::RebuildElaboratedType(QualType T, |
| 518 | ElaboratedType::TagKind Tag) { |
| 519 | if (const TagType *TT = T->getAs<TagType>()) { |
| 520 | TagDecl* TD = TT->getDecl(); |
| 521 | |
| 522 | // FIXME: this location is very wrong; we really need typelocs. |
| 523 | SourceLocation TagLocation = TD->getTagKeywordLoc(); |
| 524 | |
| 525 | // FIXME: type might be anonymous. |
| 526 | IdentifierInfo *Id = TD->getIdentifier(); |
| 527 | |
| 528 | // TODO: should we even warn on struct/class mismatches for this? Seems |
| 529 | // like it's likely to produce a lot of spurious errors. |
| 530 | if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) { |
| 531 | SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) |
| 532 | << Id |
| 533 | << CodeModificationHint::CreateReplacement(SourceRange(TagLocation), |
| 534 | TD->getKindName()); |
| 535 | SemaRef.Diag(TD->getLocation(), diag::note_previous_use); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag); |
| 540 | } |
| 541 | |
| 542 | Sema::OwningExprResult |
Douglas Gregor | c95a1fa | 2009-11-04 07:01:15 +0000 | [diff] [blame] | 543 | TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E, |
| 544 | bool isAddressOfOperand) { |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 545 | if (!E->isTypeDependent()) |
| 546 | return SemaRef.Owned(E->Retain()); |
| 547 | |
| 548 | FunctionDecl *currentDecl = getSema().getCurFunctionDecl(); |
| 549 | assert(currentDecl && "Must have current function declaration when " |
| 550 | "instantiating."); |
| 551 | |
| 552 | PredefinedExpr::IdentType IT = E->getIdentType(); |
| 553 | |
| 554 | unsigned Length = |
| 555 | PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length(); |
| 556 | |
| 557 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 558 | QualType ResTy = getSema().Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 559 | ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI, |
| 560 | ArrayType::Normal, 0); |
| 561 | PredefinedExpr *PE = |
| 562 | new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT); |
| 563 | return getSema().Owned(PE); |
| 564 | } |
| 565 | |
| 566 | Sema::OwningExprResult |
Douglas Gregor | c95a1fa | 2009-11-04 07:01:15 +0000 | [diff] [blame] | 567 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E, |
| 568 | bool isAddressOfOperand) { |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 569 | // FIXME: Clean this up a bit |
| 570 | NamedDecl *D = E->getDecl(); |
| 571 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 572 | if (NTTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 573 | |
| 574 | // If the corresponding template argument is NULL or non-existent, it's |
| 575 | // because we are performing instantiation from explicitly-specified |
| 576 | // template arguments in a function template, but there were some |
| 577 | // arguments left unspecified. |
| 578 | if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
| 579 | NTTP->getPosition())) |
| 580 | return SemaRef.Owned(E->Retain()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 582 | const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(), |
| 583 | NTTP->getPosition()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 585 | // The template argument itself might be an expression, in which |
| 586 | // case we just return that expression. |
| 587 | if (Arg.getKind() == TemplateArgument::Expression) |
| 588 | return SemaRef.Owned(Arg.getAsExpr()->Retain()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 590 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 591 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 593 | VD = cast_or_null<ValueDecl>( |
Douglas Gregor | c95a1fa | 2009-11-04 07:01:15 +0000 | [diff] [blame] | 594 | getSema().FindInstantiatedDecl(VD, TemplateArgs)); |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 595 | if (!VD) |
| 596 | return SemaRef.ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 598 | return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(), |
| 599 | /*FIXME:*/false, /*FIXME:*/false); |
| 600 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 602 | assert(Arg.getKind() == TemplateArgument::Integral); |
| 603 | QualType T = Arg.getIntegralType(); |
| 604 | if (T->isCharType() || T->isWideCharType()) |
| 605 | return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral( |
| 606 | Arg.getAsIntegral()->getZExtValue(), |
| 607 | T->isWideCharType(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | T, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 609 | E->getSourceRange().getBegin())); |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 610 | if (T->isBooleanType()) |
| 611 | return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr( |
| 612 | Arg.getAsIntegral()->getBoolValue(), |
| 613 | T, |
| 614 | E->getSourceRange().getBegin())); |
| 615 | |
| 616 | assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T)); |
| 617 | return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral( |
| 618 | *Arg.getAsIntegral(), |
| 619 | T, |
| 620 | E->getSourceRange().getBegin())); |
| 621 | } |
| 622 | |
| 623 | // We have a non-type template parameter that isn't fully substituted; |
| 624 | // FindInstantiatedDecl will find it in the local instantiation scope. |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 625 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 627 | NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs); |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 628 | if (!InstD) |
| 629 | return SemaRef.ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 630 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 631 | // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | // we need to get the underlying decl. |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 633 | // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this? |
| 634 | InstD = InstD->getUnderlyingDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 636 | CXXScopeSpec SS; |
| 637 | NestedNameSpecifier *Qualifier = 0; |
| 638 | if (E->getQualifier()) { |
| 639 | Qualifier = TransformNestedNameSpecifier(E->getQualifier(), |
| 640 | E->getQualifierRange()); |
| 641 | if (!Qualifier) |
| 642 | return SemaRef.ExprError(); |
| 643 | |
| 644 | SS.setScopeRep(Qualifier); |
| 645 | SS.setRange(E->getQualifierRange()); |
| 646 | } |
| 647 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD, |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 649 | /*FIXME:*/false, |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 650 | &SS, |
Douglas Gregor | c95a1fa | 2009-11-04 07:01:15 +0000 | [diff] [blame] | 651 | isAddressOfOperand); |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame^] | 654 | Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr( |
| 655 | CXXDefaultArgExpr *E, bool isAddressOfOperand) { |
| 656 | assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())-> |
| 657 | getDescribedFunctionTemplate() && |
| 658 | "Default arg expressions are never formed in dependent cases."); |
| 659 | return SemaRef.Owned(E->Retain()); |
| 660 | } |
| 661 | |
| 662 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | QualType |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 664 | TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| 665 | TemplateTypeParmTypeLoc TL) { |
| 666 | TemplateTypeParmType *T = TL.getTypePtr(); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 667 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 668 | // Replace the template type parameter with its corresponding |
| 669 | // template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | |
| 671 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 672 | // because we are performing instantiation from explicitly-specified |
| 673 | // template arguments in a function template class, but there were some |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 674 | // arguments left unspecified. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 675 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
| 676 | TemplateTypeParmTypeLoc NewTL |
| 677 | = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
| 678 | NewTL.setNameLoc(TL.getNameLoc()); |
| 679 | return TL.getType(); |
| 680 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | |
| 682 | assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind() |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 683 | == TemplateArgument::Type && |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 684 | "Template argument kind mismatch"); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 685 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 686 | QualType Replacement |
| 687 | = TemplateArgs(T->getDepth(), T->getIndex()).getAsType(); |
| 688 | |
| 689 | // TODO: only do this uniquing once, at the start of instantiation. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 690 | QualType Result |
| 691 | = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); |
| 692 | SubstTemplateTypeParmTypeLoc NewTL |
| 693 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 694 | NewTL.setNameLoc(TL.getNameLoc()); |
| 695 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | } |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 697 | |
| 698 | // The template type parameter comes from an inner template (e.g., |
| 699 | // the template parameter list of a member template inside the |
| 700 | // template we are instantiating). Create a new template type |
| 701 | // parameter with the template "level" reduced by one. |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 702 | QualType Result |
| 703 | = getSema().Context.getTemplateTypeParmType(T->getDepth() |
| 704 | - TemplateArgs.getNumLevels(), |
| 705 | T->getIndex(), |
| 706 | T->isParameterPack(), |
| 707 | T->getName()); |
| 708 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 709 | NewTL.setNameLoc(TL.getNameLoc()); |
| 710 | return Result; |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 711 | } |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 712 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 713 | /// \brief Perform substitution on the type T with a given set of template |
| 714 | /// arguments. |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 715 | /// |
| 716 | /// This routine substitutes the given template arguments into the |
| 717 | /// type T and produces the instantiated type. |
| 718 | /// |
| 719 | /// \param T the type into which the template arguments will be |
| 720 | /// substituted. If this type is not dependent, it will be returned |
| 721 | /// immediately. |
| 722 | /// |
| 723 | /// \param TemplateArgs the template arguments that will be |
| 724 | /// substituted for the top-level template parameters within T. |
| 725 | /// |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 726 | /// \param Loc the location in the source code where this substitution |
| 727 | /// is being performed. It will typically be the location of the |
| 728 | /// declarator (if we're instantiating the type of some declaration) |
| 729 | /// or the location of the type in the source code (if, e.g., we're |
| 730 | /// instantiating the type of a cast expression). |
| 731 | /// |
| 732 | /// \param Entity the name of the entity associated with a declaration |
| 733 | /// being instantiated (if any). May be empty to indicate that there |
| 734 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 735 | /// a cast expression) or that the entity has no name (e.g., an |
| 736 | /// unnamed function parameter). |
| 737 | /// |
| 738 | /// \returns If the instantiation succeeds, the instantiated |
| 739 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
John McCall | 609459e | 2009-10-21 00:58:09 +0000 | [diff] [blame] | 740 | DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T, |
| 741 | const MultiLevelTemplateArgumentList &Args, |
| 742 | SourceLocation Loc, |
| 743 | DeclarationName Entity) { |
| 744 | assert(!ActiveTemplateInstantiations.empty() && |
| 745 | "Cannot perform an instantiation without some context on the " |
| 746 | "instantiation stack"); |
| 747 | |
| 748 | if (!T->getType()->isDependentType()) |
| 749 | return T; |
| 750 | |
| 751 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 752 | return Instantiator.TransformType(T); |
| 753 | } |
| 754 | |
| 755 | /// Deprecated form of the above. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | QualType Sema::SubstType(QualType T, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 757 | const MultiLevelTemplateArgumentList &TemplateArgs, |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 758 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 759 | assert(!ActiveTemplateInstantiations.empty() && |
| 760 | "Cannot perform an instantiation without some context on the " |
| 761 | "instantiation stack"); |
| 762 | |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 763 | // If T is not a dependent type, there is nothing to do. |
| 764 | if (!T->isDependentType()) |
| 765 | return T; |
| 766 | |
Douglas Gregor | d6ff332 | 2009-08-04 16:50:30 +0000 | [diff] [blame] | 767 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 768 | return Instantiator.TransformType(T); |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 769 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 770 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 771 | /// \brief Perform substitution on the base class specifiers of the |
| 772 | /// given class template specialization. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 773 | /// |
| 774 | /// Produces a diagnostic and returns true on error, returns false and |
| 775 | /// attaches the instantiated base classes to the class template |
| 776 | /// specialization if successful. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | bool |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 778 | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 779 | CXXRecordDecl *Pattern, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 780 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 781 | bool Invalid = false; |
Douglas Gregor | 6181ded | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 782 | llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | for (ClassTemplateSpecializationDecl::base_class_iterator |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 784 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | 2a72edd | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 785 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 786 | if (!Base->getType()->isDependentType()) { |
Fariborz Jahanian | 5c14ec3 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 787 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 788 | continue; |
| 789 | } |
| 790 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | QualType BaseType = SubstType(Base->getType(), |
| 792 | TemplateArgs, |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 793 | Base->getSourceRange().getBegin(), |
| 794 | DeclarationName()); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 795 | if (BaseType.isNull()) { |
| 796 | Invalid = true; |
| 797 | continue; |
| 798 | } |
| 799 | |
| 800 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 801 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 802 | Base->getSourceRange(), |
| 803 | Base->isVirtual(), |
| 804 | Base->getAccessSpecifierAsWritten(), |
| 805 | BaseType, |
| 806 | /*FIXME: Not totally accurate */ |
| 807 | Base->getSourceRange().getBegin())) |
| 808 | InstantiatedBases.push_back(InstantiatedBase); |
| 809 | else |
| 810 | Invalid = true; |
| 811 | } |
| 812 | |
Douglas Gregor | 2a72edd | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 813 | if (!Invalid && |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 814 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 815 | InstantiatedBases.size())) |
| 816 | Invalid = true; |
| 817 | |
| 818 | return Invalid; |
| 819 | } |
| 820 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 821 | /// \brief Instantiate the definition of a class from a given pattern. |
| 822 | /// |
| 823 | /// \param PointOfInstantiation The point of instantiation within the |
| 824 | /// source code. |
| 825 | /// |
| 826 | /// \param Instantiation is the declaration whose definition is being |
| 827 | /// instantiated. This will be either a class template specialization |
| 828 | /// or a member class of a class template specialization. |
| 829 | /// |
| 830 | /// \param Pattern is the pattern from which the instantiation |
| 831 | /// occurs. This will be either the declaration of a class template or |
| 832 | /// the declaration of a member class of a class template. |
| 833 | /// |
| 834 | /// \param TemplateArgs The template arguments to be substituted into |
| 835 | /// the pattern. |
| 836 | /// |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 837 | /// \param TSK the kind of implicit or explicit instantiation to perform. |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 838 | /// |
| 839 | /// \param Complain whether to complain if the class cannot be instantiated due |
| 840 | /// to the lack of a definition. |
| 841 | /// |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 842 | /// \returns true if an error occurred, false otherwise. |
| 843 | bool |
| 844 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 845 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 846 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 847 | TemplateSpecializationKind TSK, |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 848 | bool Complain) { |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 849 | bool Invalid = false; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 850 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | CXXRecordDecl *PatternDef |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 852 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 853 | if (!PatternDef) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 854 | if (!Complain) { |
| 855 | // Say nothing |
| 856 | } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 857 | Diag(PointOfInstantiation, |
| 858 | diag::err_implicit_instantiate_member_undefined) |
| 859 | << Context.getTypeDeclType(Instantiation); |
| 860 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 861 | } else { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 862 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 863 | << (TSK != TSK_ImplicitInstantiation) |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 864 | << Context.getTypeDeclType(Instantiation); |
| 865 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 866 | } |
| 867 | return true; |
| 868 | } |
| 869 | Pattern = PatternDef; |
| 870 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 871 | // \brief Record the point of instantiation. |
| 872 | if (MemberSpecializationInfo *MSInfo |
| 873 | = Instantiation->getMemberSpecializationInfo()) { |
| 874 | MSInfo->setTemplateSpecializationKind(TSK); |
| 875 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 876 | } else if (ClassTemplateSpecializationDecl *Spec |
| 877 | = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { |
| 878 | Spec->setTemplateSpecializationKind(TSK); |
| 879 | Spec->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Douglas Gregor | f3430ae | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 882 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 883 | if (Inst) |
| 884 | return true; |
| 885 | |
| 886 | // Enter the scope of this instantiation. We don't use |
| 887 | // PushDeclContext because we don't have a scope. |
| 888 | DeclContext *PreviousContext = CurContext; |
| 889 | CurContext = Instantiation; |
| 890 | |
| 891 | // Start the definition of this instantiation. |
| 892 | Instantiation->startDefinition(); |
| 893 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 894 | // Do substitution on the base class specifiers. |
| 895 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 896 | Invalid = true; |
| 897 | |
Douglas Gregor | 6181ded | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 898 | llvm::SmallVector<DeclPtrTy, 4> Fields; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 899 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | MemberEnd = Pattern->decls_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 901 | Member != MemberEnd; ++Member) { |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 902 | Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 903 | if (NewMember) { |
| 904 | if (NewMember->isInvalidDecl()) |
| 905 | Invalid = true; |
| 906 | else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 907 | Fields.push_back(DeclPtrTy::make(Field)); |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 908 | else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember)) |
| 909 | Instantiation->addDecl(UD); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 910 | } else { |
| 911 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 912 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 913 | // true. For now, we expect to skip some members that we can't yet handle. |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | |
| 917 | // Finish checking fields. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 918 | ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation), |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 919 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 920 | 0); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 921 | if (Instantiation->isInvalidDecl()) |
| 922 | Invalid = true; |
| 923 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 924 | // Add any implicitly-declared members that we might need. |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 925 | if (!Invalid) |
| 926 | AddImplicitlyDeclaredMembersToClass(Instantiation); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 927 | |
| 928 | // Exit the scope of this instantiation. |
| 929 | CurContext = PreviousContext; |
| 930 | |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 931 | if (!Invalid) |
| 932 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 933 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 934 | return Invalid; |
| 935 | } |
| 936 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | bool |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 938 | Sema::InstantiateClassTemplateSpecialization( |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 939 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 940 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 941 | TemplateSpecializationKind TSK, |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 942 | bool Complain) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 943 | // Perform the actual instantiation on the canonical declaration. |
| 944 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 945 | ClassTemplateSpec->getCanonicalDecl()); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 946 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 947 | // Check whether we have already instantiated or specialized this class |
| 948 | // template specialization. |
| 949 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) { |
| 950 | if (ClassTemplateSpec->getSpecializationKind() == |
| 951 | TSK_ExplicitInstantiationDeclaration && |
| 952 | TSK == TSK_ExplicitInstantiationDefinition) { |
| 953 | // An explicit instantiation definition follows an explicit instantiation |
| 954 | // declaration (C++0x [temp.explicit]p10); go ahead and perform the |
| 955 | // explicit instantiation. |
| 956 | ClassTemplateSpec->setSpecializationKind(TSK); |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 957 | return false; |
| 958 | } |
| 959 | |
| 960 | // We can only instantiate something that hasn't already been |
| 961 | // instantiated or specialized. Fail without any diagnostics: our |
| 962 | // caller will provide an error message. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 963 | return true; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 964 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 965 | |
Douglas Gregor | 00a511f | 2009-09-15 16:51:42 +0000 | [diff] [blame] | 966 | if (ClassTemplateSpec->isInvalidDecl()) |
| 967 | return true; |
| 968 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 969 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 970 | CXXRecordDecl *Pattern = 0; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 971 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 972 | // C++ [temp.class.spec.match]p1: |
| 973 | // When a class template is used in a context that requires an |
| 974 | // instantiation of the class, it is necessary to determine |
| 975 | // whether the instantiation is to be generated using the primary |
| 976 | // template or one of the partial specializations. This is done by |
| 977 | // matching the template arguments of the class template |
| 978 | // specialization with the template argument lists of the partial |
| 979 | // specializations. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 980 | typedef std::pair<ClassTemplatePartialSpecializationDecl *, |
| 981 | TemplateArgumentList *> MatchResult; |
| 982 | llvm::SmallVector<MatchResult, 4> Matched; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 984 | Partial = Template->getPartialSpecializations().begin(), |
| 985 | PartialEnd = Template->getPartialSpecializations().end(); |
| 986 | Partial != PartialEnd; |
| 987 | ++Partial) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 988 | TemplateDeductionInfo Info(Context); |
| 989 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 990 | = DeduceTemplateArguments(&*Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 991 | ClassTemplateSpec->getTemplateArgs(), |
| 992 | Info)) { |
| 993 | // FIXME: Store the failed-deduction information for use in |
| 994 | // diagnostics, later. |
| 995 | (void)Result; |
| 996 | } else { |
| 997 | Matched.push_back(std::make_pair(&*Partial, Info.take())); |
| 998 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1001 | if (Matched.size() >= 1) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1002 | llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1003 | if (Matched.size() == 1) { |
| 1004 | // -- If exactly one matching specialization is found, the |
| 1005 | // instantiation is generated from that specialization. |
| 1006 | // We don't need to do anything for this. |
| 1007 | } else { |
| 1008 | // -- If more than one matching specialization is found, the |
| 1009 | // partial order rules (14.5.4.2) are used to determine |
| 1010 | // whether one of the specializations is more specialized |
| 1011 | // than the others. If none of the specializations is more |
| 1012 | // specialized than all of the other matching |
| 1013 | // specializations, then the use of the class template is |
| 1014 | // ambiguous and the program is ill-formed. |
| 1015 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 1016 | PEnd = Matched.end(); |
| 1017 | P != PEnd; ++P) { |
| 1018 | if (getMoreSpecializedPartialSpecialization(P->first, Best->first) |
| 1019 | == P->first) |
| 1020 | Best = P; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1021 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1022 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1023 | // Determine if the best partial specialization is more specialized than |
| 1024 | // the others. |
| 1025 | bool Ambiguous = false; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1026 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1027 | PEnd = Matched.end(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1028 | P != PEnd; ++P) { |
| 1029 | if (P != Best && |
| 1030 | getMoreSpecializedPartialSpecialization(P->first, Best->first) |
| 1031 | != Best->first) { |
| 1032 | Ambiguous = true; |
| 1033 | break; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | if (Ambiguous) { |
| 1038 | // Partial ordering did not produce a clear winner. Complain. |
| 1039 | ClassTemplateSpec->setInvalidDecl(); |
| 1040 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 1041 | << ClassTemplateSpec; |
| 1042 | |
| 1043 | // Print the matching partial specializations. |
| 1044 | for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 1045 | PEnd = Matched.end(); |
| 1046 | P != PEnd; ++P) |
| 1047 | Diag(P->first->getLocation(), diag::note_partial_spec_match) |
| 1048 | << getTemplateArgumentBindingsText(P->first->getTemplateParameters(), |
| 1049 | *P->second); |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1050 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1051 | return true; |
| 1052 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | // Instantiate using the best class template partial specialization. |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1056 | ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first; |
| 1057 | while (OrigPartialSpec->getInstantiatedFromMember()) { |
| 1058 | // If we've found an explicit specialization of this class template, |
| 1059 | // stop here and use that as the pattern. |
| 1060 | if (OrigPartialSpec->isMemberSpecialization()) |
| 1061 | break; |
| 1062 | |
| 1063 | OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember(); |
| 1064 | } |
| 1065 | |
| 1066 | Pattern = OrigPartialSpec; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1067 | ClassTemplateSpec->setInstantiationOf(Best->first, Best->second); |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1068 | } else { |
| 1069 | // -- If no matches are found, the instantiation is generated |
| 1070 | // from the primary template. |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1071 | ClassTemplateDecl *OrigTemplate = Template; |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1072 | while (OrigTemplate->getInstantiatedFromMemberTemplate()) { |
| 1073 | // If we've found an explicit specialization of this class template, |
| 1074 | // stop here and use that as the pattern. |
| 1075 | if (OrigTemplate->isMemberSpecialization()) |
| 1076 | break; |
| 1077 | |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1078 | OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1081 | Pattern = OrigTemplate->getTemplatedDecl(); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 1082 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1083 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1084 | bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec, |
| 1085 | Pattern, |
| 1086 | getTemplateInstantiationArgs(ClassTemplateSpec), |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1087 | TSK, |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1088 | Complain); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1090 | for (unsigned I = 0, N = Matched.size(); I != N; ++I) { |
| 1091 | // FIXME: Implement TemplateArgumentList::Destroy! |
| 1092 | // if (Matched[I].first != Pattern) |
| 1093 | // Matched[I].second->Destroy(Context); |
| 1094 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1096 | return Result; |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1097 | } |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1098 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1099 | /// \brief Instantiates the definitions of all of the member |
| 1100 | /// of the given class, which is an instantiation of a class template |
| 1101 | /// or a member class of a template. |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1102 | void |
| 1103 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1104 | CXXRecordDecl *Instantiation, |
| 1105 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 1106 | TemplateSpecializationKind TSK) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1107 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(), |
| 1108 | DEnd = Instantiation->decls_end(); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1109 | D != DEnd; ++D) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1110 | bool SuppressNew = false; |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1111 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1112 | if (FunctionDecl *Pattern |
| 1113 | = Function->getInstantiatedFromMemberFunction()) { |
| 1114 | MemberSpecializationInfo *MSInfo |
| 1115 | = Function->getMemberSpecializationInfo(); |
| 1116 | assert(MSInfo && "No member specialization information?"); |
| 1117 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1118 | Function, |
| 1119 | MSInfo->getTemplateSpecializationKind(), |
| 1120 | MSInfo->getPointOfInstantiation(), |
| 1121 | SuppressNew) || |
| 1122 | SuppressNew) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1123 | continue; |
| 1124 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1125 | if (Function->getBody()) |
| 1126 | continue; |
| 1127 | |
| 1128 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 1129 | // C++0x [temp.explicit]p8: |
| 1130 | // An explicit instantiation definition that names a class template |
| 1131 | // specialization explicitly instantiates the class template |
| 1132 | // specialization and is only an explicit instantiation definition |
| 1133 | // of members whose definition is visible at the point of |
| 1134 | // instantiation. |
| 1135 | if (!Pattern->getBody()) |
| 1136 | continue; |
| 1137 | |
| 1138 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1139 | |
| 1140 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
| 1141 | } else { |
| 1142 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1143 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1144 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1145 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1146 | if (Var->isStaticDataMember()) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1147 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 1148 | assert(MSInfo && "No member specialization information?"); |
| 1149 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1150 | Var, |
| 1151 | MSInfo->getTemplateSpecializationKind(), |
| 1152 | MSInfo->getPointOfInstantiation(), |
| 1153 | SuppressNew) || |
| 1154 | SuppressNew) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1155 | continue; |
| 1156 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1157 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 1158 | // C++0x [temp.explicit]p8: |
| 1159 | // An explicit instantiation definition that names a class template |
| 1160 | // specialization explicitly instantiates the class template |
| 1161 | // specialization and is only an explicit instantiation definition |
| 1162 | // of members whose definition is visible at the point of |
| 1163 | // instantiation. |
| 1164 | if (!Var->getInstantiatedFromStaticDataMember() |
| 1165 | ->getOutOfLineDefinition()) |
| 1166 | continue; |
| 1167 | |
| 1168 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1169 | InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1170 | } else { |
| 1171 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 1172 | } |
| 1173 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1174 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1175 | if (Record->isInjectedClassName()) |
| 1176 | continue; |
| 1177 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1178 | MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); |
| 1179 | assert(MSInfo && "No member specialization information?"); |
| 1180 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 1181 | Record, |
| 1182 | MSInfo->getTemplateSpecializationKind(), |
| 1183 | MSInfo->getPointOfInstantiation(), |
| 1184 | SuppressNew) || |
| 1185 | SuppressNew) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1186 | continue; |
| 1187 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1188 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 1189 | assert(Pattern && "Missing instantiated-from-template information"); |
| 1190 | |
| 1191 | if (!Record->getDefinition(Context)) { |
| 1192 | if (!Pattern->getDefinition(Context)) { |
| 1193 | // C++0x [temp.explicit]p8: |
| 1194 | // An explicit instantiation definition that names a class template |
| 1195 | // specialization explicitly instantiates the class template |
| 1196 | // specialization and is only an explicit instantiation definition |
| 1197 | // of members whose definition is visible at the point of |
| 1198 | // instantiation. |
| 1199 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 1200 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1201 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1202 | } |
| 1203 | |
| 1204 | continue; |
| 1205 | } |
| 1206 | |
| 1207 | InstantiateClass(PointOfInstantiation, Record, Pattern, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1208 | TemplateArgs, |
| 1209 | TSK); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1210 | } |
Douglas Gregor | c093c1d | 2009-10-08 01:19:17 +0000 | [diff] [blame] | 1211 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1212 | Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context)); |
| 1213 | if (Pattern) |
| 1214 | InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, |
| 1215 | TSK); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | /// \brief Instantiate the definitions of all of the members of the |
| 1221 | /// given class template specialization, which was named as part of an |
| 1222 | /// explicit instantiation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | void |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1224 | Sema::InstantiateClassTemplateSpecializationMembers( |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1225 | SourceLocation PointOfInstantiation, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1226 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 1227 | TemplateSpecializationKind TSK) { |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1228 | // C++0x [temp.explicit]p7: |
| 1229 | // An explicit instantiation that names a class template |
| 1230 | // specialization is an explicit instantion of the same kind |
| 1231 | // (declaration or definition) of each of its members (not |
| 1232 | // including members inherited from base classes) that has not |
| 1233 | // been previously explicitly specialized in the translation unit |
| 1234 | // containing the explicit instantiation, except as described |
| 1235 | // below. |
| 1236 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1237 | getTemplateInstantiationArgs(ClassTemplateSpec), |
| 1238 | TSK); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | Sema::OwningStmtResult |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1242 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | ebe1010 | 2009-08-20 07:17:43 +0000 | [diff] [blame] | 1243 | if (!S) |
| 1244 | return Owned(S); |
| 1245 | |
| 1246 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1247 | SourceLocation(), |
| 1248 | DeclarationName()); |
| 1249 | return Instantiator.TransformStmt(S); |
| 1250 | } |
| 1251 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | Sema::OwningExprResult |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1253 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1254 | if (!E) |
| 1255 | return Owned(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | |
Douglas Gregor | a16548e | 2009-08-11 05:31:07 +0000 | [diff] [blame] | 1257 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 1258 | SourceLocation(), |
| 1259 | DeclarationName()); |
| 1260 | return Instantiator.TransformExpr(E); |
| 1261 | } |
| 1262 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1263 | /// \brief Do template substitution on a nested-name-specifier. |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1264 | NestedNameSpecifier * |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1265 | Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1266 | SourceRange Range, |
| 1267 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 1135c35 | 2009-08-06 05:28:30 +0000 | [diff] [blame] | 1268 | TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(), |
| 1269 | DeclarationName()); |
| 1270 | return Instantiator.TransformNestedNameSpecifier(NNS, Range); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1271 | } |
Douglas Gregor | aa59489 | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1272 | |
| 1273 | TemplateName |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1274 | Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1275 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 71dc509 | 2009-08-06 06:41:21 +0000 | [diff] [blame] | 1276 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 1277 | DeclarationName()); |
| 1278 | return Instantiator.TransformTemplateName(Name); |
Douglas Gregor | aa59489 | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1279 | } |
Douglas Gregor | c43620d | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1280 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1281 | bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output, |
| 1282 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | e922c77 | 2009-08-04 22:27:00 +0000 | [diff] [blame] | 1283 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 1284 | DeclarationName()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1285 | |
| 1286 | return Instantiator.TransformTemplateArgument(Input, Output); |
Douglas Gregor | c43620d | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1287 | } |