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 | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Expr.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
| 18 | #include "clang/Parse/DeclSpec.h" |
| 19 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===/ |
| 25 | // Template Instantiation Support |
| 26 | //===----------------------------------------------------------------------===/ |
| 27 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 28 | /// \brief Retrieve the template argument list that should be used to |
| 29 | /// instantiate the given declaration. |
| 30 | const TemplateArgumentList & |
| 31 | Sema::getTemplateInstantiationArgs(NamedDecl *D) { |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 32 | // Template arguments for a class template specialization. |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 33 | if (ClassTemplateSpecializationDecl *Spec |
| 34 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) |
| 35 | return Spec->getTemplateArgs(); |
| 36 | |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 37 | // Template arguments for a function template specialization. |
| 38 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 39 | if (const TemplateArgumentList *TemplateArgs |
| 40 | = Function->getTemplateSpecializationArgs()) |
| 41 | return *TemplateArgs; |
| 42 | |
| 43 | // Template arguments for a member of a class template specialization. |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 44 | DeclContext *EnclosingTemplateCtx = D->getDeclContext(); |
| 45 | while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) { |
| 46 | assert(!EnclosingTemplateCtx->isFileContext() && |
| 47 | "Tried to get the instantiation arguments of a non-template"); |
| 48 | EnclosingTemplateCtx = EnclosingTemplateCtx->getParent(); |
| 49 | } |
| 50 | |
| 51 | ClassTemplateSpecializationDecl *EnclosingTemplate |
| 52 | = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx); |
| 53 | return EnclosingTemplate->getTemplateArgs(); |
| 54 | } |
| 55 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 56 | Sema::InstantiatingTemplate:: |
| 57 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 58 | Decl *Entity, |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 59 | SourceRange InstantiationRange) |
| 60 | : SemaRef(SemaRef) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 61 | |
| 62 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 63 | InstantiationRange); |
| 64 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 65 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 66 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 67 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 68 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 69 | Inst.TemplateArgs = 0; |
| 70 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 71 | Inst.InstantiationRange = InstantiationRange; |
| 72 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 73 | Invalid = false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 78 | SourceLocation PointOfInstantiation, |
| 79 | TemplateDecl *Template, |
| 80 | const TemplateArgument *TemplateArgs, |
| 81 | unsigned NumTemplateArgs, |
| 82 | SourceRange InstantiationRange) |
| 83 | : SemaRef(SemaRef) { |
| 84 | |
| 85 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 86 | InstantiationRange); |
| 87 | if (!Invalid) { |
| 88 | ActiveTemplateInstantiation Inst; |
| 89 | Inst.Kind |
| 90 | = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; |
| 91 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 92 | Inst.Entity = reinterpret_cast<uintptr_t>(Template); |
| 93 | Inst.TemplateArgs = TemplateArgs; |
| 94 | Inst.NumTemplateArgs = NumTemplateArgs; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 95 | Inst.InstantiationRange = InstantiationRange; |
| 96 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 97 | Invalid = false; |
| 98 | } |
| 99 | } |
| 100 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 101 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 102 | SourceLocation PointOfInstantiation, |
| 103 | ClassTemplatePartialSpecializationDecl *PartialSpec, |
| 104 | const TemplateArgument *TemplateArgs, |
| 105 | unsigned NumTemplateArgs, |
| 106 | SourceRange InstantiationRange) |
| 107 | : SemaRef(SemaRef) { |
| 108 | |
| 109 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 110 | InstantiationRange); |
| 111 | if (!Invalid) { |
| 112 | ActiveTemplateInstantiation Inst; |
| 113 | Inst.Kind |
| 114 | = ActiveTemplateInstantiation::PartialSpecDeductionInstantiation; |
| 115 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 116 | Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec); |
| 117 | Inst.TemplateArgs = TemplateArgs; |
| 118 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 119 | Inst.InstantiationRange = InstantiationRange; |
| 120 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 121 | Invalid = false; |
| 122 | } |
| 123 | } |
| 124 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 125 | void Sema::InstantiatingTemplate::Clear() { |
| 126 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 127 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 128 | Invalid = true; |
| 129 | } |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 132 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 133 | SourceLocation PointOfInstantiation, |
| 134 | SourceRange InstantiationRange) { |
| 135 | if (SemaRef.ActiveTemplateInstantiations.size() |
| 136 | <= SemaRef.getLangOptions().InstantiationDepth) |
| 137 | return false; |
| 138 | |
| 139 | SemaRef.Diag(PointOfInstantiation, |
| 140 | diag::err_template_recursion_depth_exceeded) |
| 141 | << SemaRef.getLangOptions().InstantiationDepth |
| 142 | << InstantiationRange; |
| 143 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
| 144 | << SemaRef.getLangOptions().InstantiationDepth; |
| 145 | return true; |
| 146 | } |
| 147 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 148 | /// \brief Prints the current instantiation stack through a series of |
| 149 | /// notes. |
| 150 | void Sema::PrintInstantiationStack() { |
| 151 | for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator |
| 152 | Active = ActiveTemplateInstantiations.rbegin(), |
| 153 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 154 | Active != ActiveEnd; |
| 155 | ++Active) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 156 | switch (Active->Kind) { |
| 157 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 158 | Decl *D = reinterpret_cast<Decl *>(Active->Entity); |
| 159 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 160 | unsigned DiagID = diag::note_template_member_class_here; |
| 161 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 162 | DiagID = diag::note_template_class_instantiation_here; |
| 163 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 164 | DiagID) |
| 165 | << Context.getTypeDeclType(Record) |
| 166 | << Active->InstantiationRange; |
| 167 | } else { |
| 168 | FunctionDecl *Function = cast<FunctionDecl>(D); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 169 | unsigned DiagID; |
| 170 | if (Function->getPrimaryTemplate()) |
| 171 | DiagID = diag::note_function_template_spec_here; |
| 172 | else |
| 173 | DiagID = diag::note_template_member_function_here; |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 174 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 175 | DiagID) |
| 176 | << Function |
| 177 | << Active->InstantiationRange; |
| 178 | } |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | |
| 182 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { |
| 183 | TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); |
| 184 | std::string TemplateArgsStr |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 185 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 186 | Active->TemplateArgs, |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 187 | Active->NumTemplateArgs, |
| 188 | Context.PrintingPolicy); |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 189 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 190 | diag::note_default_arg_instantiation_here) |
| 191 | << (Template->getNameAsString() + TemplateArgsStr) |
| 192 | << Active->InstantiationRange; |
| 193 | break; |
| 194 | } |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 195 | |
| 196 | case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation: { |
| 197 | ClassTemplatePartialSpecializationDecl *PartialSpec |
| 198 | = cast<ClassTemplatePartialSpecializationDecl>((Decl *)Active->Entity); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 199 | // FIXME: The active template instantiation's template arguments |
| 200 | // are interesting, too. We should add something like [with T = |
| 201 | // foo, U = bar, etc.] to the string. |
| 202 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 203 | diag::note_partial_spec_deduct_instantiation_here) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 204 | << Context.getTypeDeclType(PartialSpec) |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 205 | << Active->InstantiationRange; |
| 206 | break; |
| 207 | } |
| 208 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 209 | } |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 213 | bool Sema::isSFINAEContext() const { |
| 214 | using llvm::SmallVector; |
| 215 | for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator |
| 216 | Active = ActiveTemplateInstantiations.rbegin(), |
| 217 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 218 | Active != ActiveEnd; |
| 219 | ++Active) { |
| 220 | |
| 221 | switch(Active->Kind) { |
| 222 | case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation: |
| 223 | // We're in a template argument deduction context, so SFINAE |
| 224 | // applies. |
| 225 | return true; |
| 226 | |
| 227 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: |
| 228 | // A default template argument instantiation may or may not be a |
| 229 | // SFINAE context; look further up the stack. |
| 230 | break; |
| 231 | |
| 232 | case ActiveTemplateInstantiation::TemplateInstantiation: |
| 233 | // This is a template instantiation, so there is no SFINAE. |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 241 | //===----------------------------------------------------------------------===/ |
| 242 | // Template Instantiation for Types |
| 243 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 244 | namespace { |
| 245 | class VISIBILITY_HIDDEN TemplateTypeInstantiator { |
| 246 | Sema &SemaRef; |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 247 | const TemplateArgumentList &TemplateArgs; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 248 | SourceLocation Loc; |
| 249 | DeclarationName Entity; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 251 | public: |
| 252 | TemplateTypeInstantiator(Sema &SemaRef, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 253 | const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 254 | SourceLocation Loc, |
| 255 | DeclarationName Entity) |
| 256 | : SemaRef(SemaRef), TemplateArgs(TemplateArgs), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 257 | Loc(Loc), Entity(Entity) { } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 258 | |
| 259 | QualType operator()(QualType T) const { return Instantiate(T); } |
| 260 | |
| 261 | QualType Instantiate(QualType T) const; |
| 262 | |
| 263 | // Declare instantiate functions for each type. |
| 264 | #define TYPE(Class, Base) \ |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 265 | QualType Instantiate##Class##Type(const Class##Type *T) const; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 266 | #define ABSTRACT_TYPE(Class, Base) |
| 267 | #include "clang/AST/TypeNodes.def" |
| 268 | }; |
| 269 | } |
| 270 | |
| 271 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 272 | TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 273 | // FIXME: Implement this |
| 274 | assert(false && "Cannot instantiate ExtQualType yet"); |
| 275 | return QualType(); |
| 276 | } |
| 277 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 278 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 279 | TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 280 | assert(false && "Builtin types are not dependent and cannot be instantiated"); |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 281 | return QualType(T, 0); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 284 | QualType |
| 285 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 286 | InstantiateFixedWidthIntType(const FixedWidthIntType *T) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 287 | // FIXME: Implement this |
| 288 | assert(false && "Cannot instantiate FixedWidthIntType yet"); |
| 289 | return QualType(); |
| 290 | } |
| 291 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 292 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 293 | TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 294 | // FIXME: Implement this |
| 295 | assert(false && "Cannot instantiate ComplexType yet"); |
| 296 | return QualType(); |
| 297 | } |
| 298 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 299 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 300 | TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T) const { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 301 | QualType PointeeType = Instantiate(T->getPointeeType()); |
| 302 | if (PointeeType.isNull()) |
| 303 | return QualType(); |
| 304 | |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 305 | return SemaRef.BuildPointerType(PointeeType, 0, Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 308 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 309 | TemplateTypeInstantiator::InstantiateBlockPointerType( |
| 310 | const BlockPointerType *T) const { |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 311 | QualType PointeeType = Instantiate(T->getPointeeType()); |
| 312 | if (PointeeType.isNull()) |
| 313 | return QualType(); |
| 314 | |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 315 | return SemaRef.BuildBlockPointerType(PointeeType, 0, Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 318 | QualType |
| 319 | TemplateTypeInstantiator::InstantiateLValueReferenceType( |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 320 | const LValueReferenceType *T) const { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 321 | QualType ReferentType = Instantiate(T->getPointeeType()); |
| 322 | if (ReferentType.isNull()) |
| 323 | return QualType(); |
| 324 | |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 325 | return SemaRef.BuildReferenceType(ReferentType, true, 0, Loc, Entity); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | QualType |
| 329 | TemplateTypeInstantiator::InstantiateRValueReferenceType( |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 330 | const RValueReferenceType *T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 331 | QualType ReferentType = Instantiate(T->getPointeeType()); |
| 332 | if (ReferentType.isNull()) |
| 333 | return QualType(); |
| 334 | |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 335 | return SemaRef.BuildReferenceType(ReferentType, false, 0, Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 338 | QualType |
| 339 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 340 | InstantiateMemberPointerType(const MemberPointerType *T) const { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 341 | QualType PointeeType = Instantiate(T->getPointeeType()); |
| 342 | if (PointeeType.isNull()) |
| 343 | return QualType(); |
| 344 | |
| 345 | QualType ClassType = Instantiate(QualType(T->getClass(), 0)); |
| 346 | if (ClassType.isNull()) |
| 347 | return QualType(); |
| 348 | |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 349 | return SemaRef.BuildMemberPointerType(PointeeType, ClassType, 0, Loc, |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 350 | Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 353 | QualType |
| 354 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 355 | InstantiateConstantArrayType(const ConstantArrayType *T) const { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 356 | QualType ElementType = Instantiate(T->getElementType()); |
| 357 | if (ElementType.isNull()) |
| 358 | return ElementType; |
| 359 | |
| 360 | // Build a temporary integer literal to specify the size for |
| 361 | // BuildArrayType. Since we have already checked the size as part of |
| 362 | // creating the dependent array type in the first place, we know |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 363 | // there aren't any errors. However, we do need to determine what |
| 364 | // C++ type to give the size expression. |
| 365 | llvm::APInt Size = T->getSize(); |
| 366 | QualType Types[] = { |
| 367 | SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy, |
| 368 | SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy, |
| 369 | SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty |
| 370 | }; |
| 371 | const unsigned NumTypes = sizeof(Types) / sizeof(QualType); |
| 372 | QualType SizeType; |
| 373 | for (unsigned I = 0; I != NumTypes; ++I) |
| 374 | if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) { |
| 375 | SizeType = Types[I]; |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | if (SizeType.isNull()) |
| 380 | SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false); |
| 381 | |
| 382 | IntegerLiteral ArraySize(Size, SizeType, Loc); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 383 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
| 384 | &ArraySize, T->getIndexTypeQualifier(), |
| 385 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 388 | QualType |
| 389 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 390 | InstantiateIncompleteArrayType(const IncompleteArrayType *T) const { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 391 | QualType ElementType = Instantiate(T->getElementType()); |
| 392 | if (ElementType.isNull()) |
| 393 | return ElementType; |
| 394 | |
| 395 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
| 396 | 0, T->getIndexTypeQualifier(), |
| 397 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 400 | QualType |
| 401 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 402 | InstantiateVariableArrayType(const VariableArrayType *T) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 403 | // FIXME: Implement this |
| 404 | assert(false && "Cannot instantiate VariableArrayType yet"); |
| 405 | return QualType(); |
| 406 | } |
| 407 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 408 | QualType |
| 409 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 410 | InstantiateDependentSizedArrayType(const DependentSizedArrayType *T) const { |
Anders Carlsson | 76b1c84 | 2009-03-15 20:12:13 +0000 | [diff] [blame] | 411 | Expr *ArraySize = T->getSizeExpr(); |
| 412 | assert(ArraySize->isValueDependent() && |
| 413 | "dependent sized array types must have value dependent size expr"); |
| 414 | |
| 415 | // Instantiate the element type if needed |
| 416 | QualType ElementType = T->getElementType(); |
| 417 | if (ElementType->isDependentType()) { |
| 418 | ElementType = Instantiate(ElementType); |
| 419 | if (ElementType.isNull()) |
| 420 | return QualType(); |
| 421 | } |
| 422 | |
| 423 | // Instantiate the size expression |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 424 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
Anders Carlsson | 76b1c84 | 2009-03-15 20:12:13 +0000 | [diff] [blame] | 425 | Sema::OwningExprResult InstantiatedArraySize = |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 426 | SemaRef.InstantiateExpr(ArraySize, TemplateArgs); |
Anders Carlsson | 76b1c84 | 2009-03-15 20:12:13 +0000 | [diff] [blame] | 427 | if (InstantiatedArraySize.isInvalid()) |
| 428 | return QualType(); |
| 429 | |
| 430 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 431 | InstantiatedArraySize.takeAs<Expr>(), |
Anders Carlsson | 76b1c84 | 2009-03-15 20:12:13 +0000 | [diff] [blame] | 432 | T->getIndexTypeQualifier(), Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 435 | QualType |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 436 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 437 | InstantiateDependentSizedExtVectorType( |
| 438 | const DependentSizedExtVectorType *T) const { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 439 | |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 440 | // Instantiate the element type if needed. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 441 | QualType ElementType = T->getElementType(); |
| 442 | if (ElementType->isDependentType()) { |
| 443 | ElementType = Instantiate(ElementType); |
| 444 | if (ElementType.isNull()) |
| 445 | return QualType(); |
| 446 | } |
| 447 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 448 | // The expression in a dependent-sized extended vector type is not |
| 449 | // potentially evaluated. |
| 450 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
| 451 | |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 452 | // Instantiate the size expression. |
| 453 | const Expr *SizeExpr = T->getSizeExpr(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 454 | Sema::OwningExprResult InstantiatedArraySize = |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 455 | SemaRef.InstantiateExpr(const_cast<Expr *>(SizeExpr), TemplateArgs); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 456 | if (InstantiatedArraySize.isInvalid()) |
| 457 | return QualType(); |
| 458 | |
| 459 | return SemaRef.BuildExtVectorType(ElementType, |
| 460 | SemaRef.Owned( |
| 461 | InstantiatedArraySize.takeAs<Expr>()), |
| 462 | T->getAttributeLoc()); |
| 463 | } |
| 464 | |
| 465 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 466 | TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 467 | // FIXME: Implement this |
| 468 | assert(false && "Cannot instantiate VectorType yet"); |
| 469 | return QualType(); |
| 470 | } |
| 471 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 472 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 473 | TemplateTypeInstantiator::InstantiateExtVectorType( |
| 474 | const ExtVectorType *T) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 475 | // FIXME: Implement this |
| 476 | assert(false && "Cannot instantiate ExtVectorType yet"); |
| 477 | return QualType(); |
| 478 | } |
| 479 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 480 | QualType |
| 481 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 482 | InstantiateFunctionProtoType(const FunctionProtoType *T) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 483 | QualType ResultType = Instantiate(T->getResultType()); |
| 484 | if (ResultType.isNull()) |
| 485 | return ResultType; |
| 486 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 487 | llvm::SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 488 | for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(), |
| 489 | ParamEnd = T->arg_type_end(); |
| 490 | Param != ParamEnd; ++Param) { |
| 491 | QualType P = Instantiate(*Param); |
| 492 | if (P.isNull()) |
| 493 | return P; |
| 494 | |
| 495 | ParamTypes.push_back(P); |
| 496 | } |
| 497 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 498 | return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(), |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 499 | ParamTypes.size(), |
| 500 | T->isVariadic(), T->getTypeQuals(), |
| 501 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 504 | QualType |
| 505 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 506 | InstantiateFunctionNoProtoType(const FunctionNoProtoType *T) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 507 | assert(false && "Functions without prototypes cannot be dependent."); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 508 | return QualType(); |
| 509 | } |
| 510 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 511 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 512 | TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T) const { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 513 | TypedefDecl *Typedef |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 514 | = cast_or_null<TypedefDecl>( |
| 515 | SemaRef.InstantiateCurrentDeclRef(T->getDecl())); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 516 | if (!Typedef) |
| 517 | return QualType(); |
| 518 | |
| 519 | return SemaRef.Context.getTypeDeclType(Typedef); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 522 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 523 | TemplateTypeInstantiator::InstantiateTypeOfExprType( |
| 524 | const TypeOfExprType *T) const { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 525 | // The expression in a typeof is not potentially evaluated. |
| 526 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
| 527 | |
Douglas Gregor | 5f8bd59 | 2009-05-26 22:09:24 +0000 | [diff] [blame] | 528 | Sema::OwningExprResult E |
| 529 | = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs); |
| 530 | if (E.isInvalid()) |
| 531 | return QualType(); |
| 532 | |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 533 | return SemaRef.BuildTypeofExprType(E.takeAs<Expr>()); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 536 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 537 | TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T) const { |
Douglas Gregor | 5f8bd59 | 2009-05-26 22:09:24 +0000 | [diff] [blame] | 538 | QualType Underlying = Instantiate(T->getUnderlyingType()); |
| 539 | if (Underlying.isNull()) |
| 540 | return QualType(); |
| 541 | |
| 542 | return SemaRef.Context.getTypeOfType(Underlying); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 545 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 546 | TemplateTypeInstantiator::InstantiateDecltypeType(const DecltypeType *T) const { |
Anders Carlsson | 87471f5 | 2009-06-26 03:02:18 +0000 | [diff] [blame] | 547 | // C++0x [dcl.type.simple]p4: |
| 548 | // The operand of the decltype specifier is an unevaluated operand. |
| 549 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 550 | Action::Unevaluated); |
| 551 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 552 | Sema::OwningExprResult E |
| 553 | = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs); |
| 554 | |
| 555 | if (E.isInvalid()) |
| 556 | return QualType(); |
| 557 | |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 558 | return SemaRef.BuildDecltypeType(E.takeAs<Expr>()); |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 561 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 562 | TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T) const { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 563 | RecordDecl *Record |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 564 | = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl())); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 565 | if (!Record) |
| 566 | return QualType(); |
| 567 | |
| 568 | return SemaRef.Context.getTypeDeclType(Record); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 571 | QualType |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 572 | TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T) const { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 573 | EnumDecl *Enum |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 574 | = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl())); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 575 | if (!Enum) |
| 576 | return QualType(); |
| 577 | |
| 578 | return SemaRef.Context.getTypeDeclType(Enum); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 581 | QualType |
| 582 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 583 | InstantiateTemplateTypeParmType(const TemplateTypeParmType *T) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 584 | if (T->getDepth() == 0) { |
| 585 | // Replace the template type parameter with its corresponding |
| 586 | // template argument. |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame^] | 587 | |
| 588 | // If the corresponding template argument is NULL or doesn't exist, it's |
| 589 | // because we are performing instantiation from explicitly-specified |
| 590 | // template arguments in a function template class, but there were some |
| 591 | // arguments left unspecified. |
| 592 | if (T->getIndex() >= TemplateArgs.size() || |
| 593 | TemplateArgs[T->getIndex()].isNull()) |
| 594 | return QualType(T, 0); // Would be nice to keep the original type here |
| 595 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 596 | assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type && |
| 597 | "Template argument kind mismatch"); |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 598 | return TemplateArgs[T->getIndex()].getAsType(); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | // The template type parameter comes from an inner template (e.g., |
| 602 | // the template parameter list of a member template inside the |
| 603 | // template we are instantiating). Create a new template type |
| 604 | // parameter with the template "level" reduced by one. |
| 605 | return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1, |
| 606 | T->getIndex(), |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 607 | T->isParameterPack(), |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 608 | T->getName()); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 611 | QualType |
| 612 | TemplateTypeInstantiator:: |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 613 | InstantiateTemplateSpecializationType( |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 614 | const TemplateSpecializationType *T) const { |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 615 | llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 616 | InstantiatedTemplateArgs.reserve(T->getNumArgs()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 617 | for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 618 | Arg != ArgEnd; ++Arg) { |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 619 | TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs); |
| 620 | if (InstArg.isNull()) |
| 621 | return QualType(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 622 | |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 623 | InstantiatedTemplateArgs.push_back(InstArg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 626 | // FIXME: We're missing the locations of the template name, '<', and '>'. |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 627 | |
| 628 | TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(), |
| 629 | Loc, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 630 | TemplateArgs); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 631 | |
| 632 | return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(), |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 633 | InstantiatedTemplateArgs.data(), |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 634 | InstantiatedTemplateArgs.size(), |
| 635 | SourceLocation()); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 638 | QualType |
| 639 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 640 | InstantiateQualifiedNameType(const QualifiedNameType *T) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 641 | // When we instantiated a qualified name type, there's no point in |
| 642 | // keeping the qualification around in the instantiated result. So, |
| 643 | // just instantiate the named type. |
| 644 | return (*this)(T->getNamedType()); |
| 645 | } |
| 646 | |
| 647 | QualType |
| 648 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 649 | InstantiateTypenameType(const TypenameType *T) const { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 650 | if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) { |
| 651 | // When the typename type refers to a template-id, the template-id |
| 652 | // is dependent and has enough information to instantiate the |
| 653 | // result of the typename type. Since we don't care about keeping |
| 654 | // the spelling of the typename type in template instantiations, |
| 655 | // we just instantiate the template-id. |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 656 | return InstantiateTemplateSpecializationType(TemplateId); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 659 | NestedNameSpecifier *NNS |
| 660 | = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(), |
| 661 | SourceRange(Loc), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 662 | TemplateArgs); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 663 | if (!NNS) |
| 664 | return QualType(); |
| 665 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 666 | return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc)); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | QualType |
| 670 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 671 | InstantiateObjCObjectPointerType(const ObjCObjectPointerType *T) const { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 672 | assert(false && "Objective-C types cannot be dependent"); |
| 673 | return QualType(); |
| 674 | } |
| 675 | |
| 676 | QualType |
| 677 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 678 | InstantiateObjCInterfaceType(const ObjCInterfaceType *T) const { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 679 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 680 | return QualType(); |
| 681 | } |
| 682 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 683 | QualType |
| 684 | TemplateTypeInstantiator:: |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 685 | InstantiateObjCQualifiedInterfaceType( |
| 686 | const ObjCQualifiedInterfaceType *T) const { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 687 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 688 | return QualType(); |
| 689 | } |
| 690 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 691 | /// \brief The actual implementation of Sema::InstantiateType(). |
| 692 | QualType TemplateTypeInstantiator::Instantiate(QualType T) const { |
| 693 | // If T is not a dependent type, there is nothing to do. |
| 694 | if (!T->isDependentType()) |
| 695 | return T; |
| 696 | |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 697 | QualType Result; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 698 | switch (T->getTypeClass()) { |
| 699 | #define TYPE(Class, Base) \ |
| 700 | case Type::Class: \ |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 701 | Result = Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr())); \ |
| 702 | break; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 703 | #define ABSTRACT_TYPE(Class, Base) |
| 704 | #include "clang/AST/TypeNodes.def" |
| 705 | } |
Douglas Gregor | 8a5cb11 | 2009-06-26 21:40:05 +0000 | [diff] [blame] | 706 | |
| 707 | // C++ [dcl.ref]p1: |
| 708 | // [...] Cv-qualified references are ill-formed except when |
| 709 | // the cv-qualifiers are introduced through the use of a |
| 710 | // typedef (7.1.3) or of a template type argument (14.3), in |
| 711 | // which case the cv-qualifiers are ignored. |
| 712 | // |
| 713 | // The same rule applies to function types. |
| 714 | if (!Result.isNull() && T.getCVRQualifiers() && |
| 715 | !Result->isFunctionType() && !Result->isReferenceType()) |
| 716 | Result = Result.getWithAdditionalQualifiers(T.getCVRQualifiers()); |
| 717 | return Result; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 718 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 719 | |
| 720 | /// \brief Instantiate the type T with a given set of template arguments. |
| 721 | /// |
| 722 | /// This routine substitutes the given template arguments into the |
| 723 | /// type T and produces the instantiated type. |
| 724 | /// |
| 725 | /// \param T the type into which the template arguments will be |
| 726 | /// substituted. If this type is not dependent, it will be returned |
| 727 | /// immediately. |
| 728 | /// |
| 729 | /// \param TemplateArgs the template arguments that will be |
| 730 | /// substituted for the top-level template parameters within T. |
| 731 | /// |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 732 | /// \param Loc the location in the source code where this substitution |
| 733 | /// is being performed. It will typically be the location of the |
| 734 | /// declarator (if we're instantiating the type of some declaration) |
| 735 | /// or the location of the type in the source code (if, e.g., we're |
| 736 | /// instantiating the type of a cast expression). |
| 737 | /// |
| 738 | /// \param Entity the name of the entity associated with a declaration |
| 739 | /// being instantiated (if any). May be empty to indicate that there |
| 740 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 741 | /// a cast expression) or that the entity has no name (e.g., an |
| 742 | /// unnamed function parameter). |
| 743 | /// |
| 744 | /// \returns If the instantiation succeeds, the instantiated |
| 745 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
| 746 | QualType Sema::InstantiateType(QualType T, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 747 | const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 748 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 749 | assert(!ActiveTemplateInstantiations.empty() && |
| 750 | "Cannot perform an instantiation without some context on the " |
| 751 | "instantiation stack"); |
| 752 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 753 | // If T is not a dependent type, there is nothing to do. |
| 754 | if (!T->isDependentType()) |
| 755 | return T; |
| 756 | |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 757 | TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 758 | return Instantiator(T); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 759 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 760 | |
| 761 | /// \brief Instantiate the base class specifiers of the given class |
| 762 | /// template specialization. |
| 763 | /// |
| 764 | /// Produces a diagnostic and returns true on error, returns false and |
| 765 | /// attaches the instantiated base classes to the class template |
| 766 | /// specialization if successful. |
| 767 | bool |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 768 | Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 769 | CXXRecordDecl *Pattern, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 770 | const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 771 | bool Invalid = false; |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 772 | llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 773 | for (ClassTemplateSpecializationDecl::base_class_iterator |
| 774 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 775 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 776 | if (!Base->getType()->isDependentType()) { |
| 777 | // FIXME: Allocate via ASTContext |
| 778 | InstantiatedBases.push_back(new CXXBaseSpecifier(*Base)); |
| 779 | continue; |
| 780 | } |
| 781 | |
| 782 | QualType BaseType = InstantiateType(Base->getType(), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 783 | TemplateArgs, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 784 | Base->getSourceRange().getBegin(), |
| 785 | DeclarationName()); |
| 786 | if (BaseType.isNull()) { |
| 787 | Invalid = true; |
| 788 | continue; |
| 789 | } |
| 790 | |
| 791 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 792 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 793 | Base->getSourceRange(), |
| 794 | Base->isVirtual(), |
| 795 | Base->getAccessSpecifierAsWritten(), |
| 796 | BaseType, |
| 797 | /*FIXME: Not totally accurate */ |
| 798 | Base->getSourceRange().getBegin())) |
| 799 | InstantiatedBases.push_back(InstantiatedBase); |
| 800 | else |
| 801 | Invalid = true; |
| 802 | } |
| 803 | |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 804 | if (!Invalid && |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 805 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 806 | InstantiatedBases.size())) |
| 807 | Invalid = true; |
| 808 | |
| 809 | return Invalid; |
| 810 | } |
| 811 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 812 | /// \brief Instantiate the definition of a class from a given pattern. |
| 813 | /// |
| 814 | /// \param PointOfInstantiation The point of instantiation within the |
| 815 | /// source code. |
| 816 | /// |
| 817 | /// \param Instantiation is the declaration whose definition is being |
| 818 | /// instantiated. This will be either a class template specialization |
| 819 | /// or a member class of a class template specialization. |
| 820 | /// |
| 821 | /// \param Pattern is the pattern from which the instantiation |
| 822 | /// occurs. This will be either the declaration of a class template or |
| 823 | /// the declaration of a member class of a class template. |
| 824 | /// |
| 825 | /// \param TemplateArgs The template arguments to be substituted into |
| 826 | /// the pattern. |
| 827 | /// |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 828 | /// \returns true if an error occurred, false otherwise. |
| 829 | bool |
| 830 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 831 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 832 | const TemplateArgumentList &TemplateArgs, |
| 833 | bool ExplicitInstantiation) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 834 | bool Invalid = false; |
| 835 | |
| 836 | CXXRecordDecl *PatternDef |
| 837 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 838 | if (!PatternDef) { |
| 839 | if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
| 840 | Diag(PointOfInstantiation, |
| 841 | diag::err_implicit_instantiate_member_undefined) |
| 842 | << Context.getTypeDeclType(Instantiation); |
| 843 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 844 | } else { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 845 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 846 | << ExplicitInstantiation |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 847 | << Context.getTypeDeclType(Instantiation); |
| 848 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 849 | } |
| 850 | return true; |
| 851 | } |
| 852 | Pattern = PatternDef; |
| 853 | |
Douglas Gregor | d048bb7 | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 854 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 855 | if (Inst) |
| 856 | return true; |
| 857 | |
| 858 | // Enter the scope of this instantiation. We don't use |
| 859 | // PushDeclContext because we don't have a scope. |
| 860 | DeclContext *PreviousContext = CurContext; |
| 861 | CurContext = Instantiation; |
| 862 | |
| 863 | // Start the definition of this instantiation. |
| 864 | Instantiation->startDefinition(); |
| 865 | |
| 866 | // Instantiate the base class specifiers. |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 867 | if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 868 | Invalid = true; |
| 869 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 870 | llvm::SmallVector<DeclPtrTy, 4> Fields; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 871 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
| 872 | MemberEnd = Pattern->decls_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 873 | Member != MemberEnd; ++Member) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 874 | Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 875 | if (NewMember) { |
| 876 | if (NewMember->isInvalidDecl()) |
| 877 | Invalid = true; |
| 878 | else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 879 | Fields.push_back(DeclPtrTy::make(Field)); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 880 | } else { |
| 881 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 882 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 883 | // 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] | 884 | } |
| 885 | } |
| 886 | |
| 887 | // Finish checking fields. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 888 | ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 889 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 890 | 0); |
| 891 | |
| 892 | // Add any implicitly-declared members that we might need. |
| 893 | AddImplicitlyDeclaredMembersToClass(Instantiation); |
| 894 | |
| 895 | // Exit the scope of this instantiation. |
| 896 | CurContext = PreviousContext; |
| 897 | |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 898 | if (!Invalid) |
| 899 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 900 | |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 901 | // If this is an explicit instantiation, instantiate our members, too. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 902 | if (!Invalid && ExplicitInstantiation) { |
| 903 | Inst.Clear(); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 904 | InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 905 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 906 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 907 | return Invalid; |
| 908 | } |
| 909 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 910 | bool |
| 911 | Sema::InstantiateClassTemplateSpecialization( |
| 912 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 913 | bool ExplicitInstantiation) { |
| 914 | // Perform the actual instantiation on the canonical declaration. |
| 915 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
| 916 | Context.getCanonicalDecl(ClassTemplateSpec)); |
| 917 | |
| 918 | // We can only instantiate something that hasn't already been |
| 919 | // instantiated or specialized. Fail without any diagnostics: our |
| 920 | // caller will provide an error message. |
| 921 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) |
| 922 | return true; |
| 923 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 924 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 925 | CXXRecordDecl *Pattern = Template->getTemplatedDecl(); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 926 | const TemplateArgumentList *TemplateArgs |
| 927 | = &ClassTemplateSpec->getTemplateArgs(); |
| 928 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 929 | // C++ [temp.class.spec.match]p1: |
| 930 | // When a class template is used in a context that requires an |
| 931 | // instantiation of the class, it is necessary to determine |
| 932 | // whether the instantiation is to be generated using the primary |
| 933 | // template or one of the partial specializations. This is done by |
| 934 | // matching the template arguments of the class template |
| 935 | // specialization with the template argument lists of the partial |
| 936 | // specializations. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 937 | typedef std::pair<ClassTemplatePartialSpecializationDecl *, |
| 938 | TemplateArgumentList *> MatchResult; |
| 939 | llvm::SmallVector<MatchResult, 4> Matched; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 940 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 941 | Partial = Template->getPartialSpecializations().begin(), |
| 942 | PartialEnd = Template->getPartialSpecializations().end(); |
| 943 | Partial != PartialEnd; |
| 944 | ++Partial) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 945 | TemplateDeductionInfo Info(Context); |
| 946 | if (TemplateDeductionResult Result |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 947 | = DeduceTemplateArguments(&*Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 948 | ClassTemplateSpec->getTemplateArgs(), |
| 949 | Info)) { |
| 950 | // FIXME: Store the failed-deduction information for use in |
| 951 | // diagnostics, later. |
| 952 | (void)Result; |
| 953 | } else { |
| 954 | Matched.push_back(std::make_pair(&*Partial, Info.take())); |
| 955 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | if (Matched.size() == 1) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 959 | // -- If exactly one matching specialization is found, the |
| 960 | // instantiation is generated from that specialization. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 961 | Pattern = Matched[0].first; |
| 962 | TemplateArgs = Matched[0].second; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 963 | } else if (Matched.size() > 1) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 964 | // -- If more than one matching specialization is found, the |
| 965 | // partial order rules (14.5.4.2) are used to determine |
| 966 | // whether one of the specializations is more specialized |
| 967 | // than the others. If none of the specializations is more |
| 968 | // specialized than all of the other matching |
| 969 | // specializations, then the use of the class template is |
| 970 | // ambiguous and the program is ill-formed. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 971 | // FIXME: Implement partial ordering of class template partial |
| 972 | // specializations. |
| 973 | Diag(ClassTemplateSpec->getLocation(), |
| 974 | diag::unsup_template_partial_spec_ordering); |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 975 | } else { |
| 976 | // -- If no matches are found, the instantiation is generated |
| 977 | // from the primary template. |
| 978 | |
| 979 | // Since we initialized the pattern and template arguments from |
| 980 | // the primary template, there is nothing more we need to do here. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 981 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 982 | |
| 983 | // Note that this is an instantiation. |
| 984 | ClassTemplateSpec->setSpecializationKind( |
| 985 | ExplicitInstantiation? TSK_ExplicitInstantiation |
| 986 | : TSK_ImplicitInstantiation); |
| 987 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 988 | bool Result = InstantiateClass(ClassTemplateSpec->getLocation(), |
| 989 | ClassTemplateSpec, Pattern, *TemplateArgs, |
| 990 | ExplicitInstantiation); |
| 991 | |
| 992 | for (unsigned I = 0, N = Matched.size(); I != N; ++I) { |
| 993 | // FIXME: Implement TemplateArgumentList::Destroy! |
| 994 | // if (Matched[I].first != Pattern) |
| 995 | // Matched[I].second->Destroy(Context); |
| 996 | } |
| 997 | |
| 998 | return Result; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 999 | } |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1000 | |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1001 | /// \brief Instantiate the definitions of all of the member of the |
| 1002 | /// given class, which is an instantiation of a class template or a |
| 1003 | /// member class of a template. |
| 1004 | void |
| 1005 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
| 1006 | CXXRecordDecl *Instantiation, |
| 1007 | const TemplateArgumentList &TemplateArgs) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1008 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(), |
| 1009 | DEnd = Instantiation->decls_end(); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1010 | D != DEnd; ++D) { |
| 1011 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1012 | if (!Function->getBody()) |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1013 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1014 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
| 1015 | const VarDecl *Def = 0; |
| 1016 | if (!Var->getDefinition(Def)) |
| 1017 | InstantiateVariableDefinition(Var); |
| 1018 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
| 1019 | if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) { |
| 1020 | assert(Record->getInstantiatedFromMemberClass() && |
| 1021 | "Missing instantiated-from-template information"); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1022 | InstantiateClass(PointOfInstantiation, Record, |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1023 | Record->getInstantiatedFromMemberClass(), |
| 1024 | TemplateArgs, true); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /// \brief Instantiate the definitions of all of the members of the |
| 1031 | /// given class template specialization, which was named as part of an |
| 1032 | /// explicit instantiation. |
| 1033 | void Sema::InstantiateClassTemplateSpecializationMembers( |
| 1034 | SourceLocation PointOfInstantiation, |
| 1035 | ClassTemplateSpecializationDecl *ClassTemplateSpec) { |
| 1036 | // C++0x [temp.explicit]p7: |
| 1037 | // An explicit instantiation that names a class template |
| 1038 | // specialization is an explicit instantion of the same kind |
| 1039 | // (declaration or definition) of each of its members (not |
| 1040 | // including members inherited from base classes) that has not |
| 1041 | // been previously explicitly specialized in the translation unit |
| 1042 | // containing the explicit instantiation, except as described |
| 1043 | // below. |
| 1044 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
| 1045 | ClassTemplateSpec->getTemplateArgs()); |
| 1046 | } |
| 1047 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1048 | /// \brief Instantiate a nested-name-specifier. |
| 1049 | NestedNameSpecifier * |
| 1050 | Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1051 | SourceRange Range, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1052 | const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1053 | // Instantiate the prefix of this nested name specifier. |
| 1054 | NestedNameSpecifier *Prefix = NNS->getPrefix(); |
| 1055 | if (Prefix) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1056 | Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1057 | if (!Prefix) |
| 1058 | return 0; |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1061 | switch (NNS->getKind()) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1062 | case NestedNameSpecifier::Identifier: { |
| 1063 | assert(Prefix && |
| 1064 | "Can't have an identifier nested-name-specifier with no prefix"); |
| 1065 | CXXScopeSpec SS; |
| 1066 | // FIXME: The source location information is all wrong. |
| 1067 | SS.setRange(Range); |
| 1068 | SS.setScopeRep(Prefix); |
| 1069 | return static_cast<NestedNameSpecifier *>( |
| 1070 | ActOnCXXNestedNameSpecifier(0, SS, |
| 1071 | Range.getEnd(), |
| 1072 | Range.getEnd(), |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1073 | *NNS->getAsIdentifier())); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1074 | break; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1075 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1076 | |
| 1077 | case NestedNameSpecifier::Namespace: |
| 1078 | case NestedNameSpecifier::Global: |
| 1079 | return NNS; |
| 1080 | |
| 1081 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1082 | case NestedNameSpecifier::TypeSpec: { |
| 1083 | QualType T = QualType(NNS->getAsType(), 0); |
| 1084 | if (!T->isDependentType()) |
| 1085 | return NNS; |
| 1086 | |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1087 | T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1088 | if (T.isNull()) |
| 1089 | return 0; |
| 1090 | |
Eli Friedman | 923f753 | 2009-06-13 04:51:30 +0000 | [diff] [blame] | 1091 | if (T->isDependentType() || T->isRecordType() || |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1092 | (getLangOptions().CPlusPlus0x && T->isEnumeralType())) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1093 | assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1094 | return NestedNameSpecifier::Create(Context, Prefix, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1095 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1096 | T.getTypePtr()); |
| 1097 | } |
| 1098 | |
| 1099 | Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T; |
| 1100 | return 0; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1104 | // Required to silence a GCC warning |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1105 | return 0; |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1106 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1107 | |
| 1108 | TemplateName |
| 1109 | Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1110 | const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1111 | if (TemplateTemplateParmDecl *TTP |
| 1112 | = dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 1113 | Name.getAsTemplateDecl())) { |
| 1114 | assert(TTP->getDepth() == 0 && |
| 1115 | "Cannot reduce depth of a template template parameter"); |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 1116 | assert(TemplateArgs[TTP->getPosition()].getAsDecl() && |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1117 | "Wrong kind of template template argument"); |
| 1118 | ClassTemplateDecl *ClassTemplate |
| 1119 | = dyn_cast<ClassTemplateDecl>( |
| 1120 | TemplateArgs[TTP->getPosition()].getAsDecl()); |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 1121 | assert(ClassTemplate && "Expected a class template"); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1122 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) { |
| 1123 | NestedNameSpecifier *NNS |
| 1124 | = InstantiateNestedNameSpecifier(QTN->getQualifier(), |
| 1125 | /*FIXME=*/SourceRange(Loc), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1126 | TemplateArgs); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1127 | if (NNS) |
| 1128 | return Context.getQualifiedTemplateName(NNS, |
| 1129 | QTN->hasTemplateKeyword(), |
| 1130 | ClassTemplate); |
| 1131 | } |
| 1132 | |
| 1133 | return TemplateName(ClassTemplate); |
| 1134 | } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
| 1135 | NestedNameSpecifier *NNS |
| 1136 | = InstantiateNestedNameSpecifier(DTN->getQualifier(), |
| 1137 | /*FIXME=*/SourceRange(Loc), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1138 | TemplateArgs); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1139 | |
| 1140 | if (!NNS) // FIXME: Not the best recovery strategy. |
| 1141 | return Name; |
| 1142 | |
| 1143 | if (NNS->isDependent()) |
| 1144 | return Context.getDependentTemplateName(NNS, DTN->getName()); |
| 1145 | |
| 1146 | // Somewhat redundant with ActOnDependentTemplateName. |
| 1147 | CXXScopeSpec SS; |
| 1148 | SS.setRange(SourceRange(Loc)); |
| 1149 | SS.setScopeRep(NNS); |
| 1150 | TemplateTy Template; |
| 1151 | TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS); |
| 1152 | if (TNK == TNK_Non_template) { |
| 1153 | Diag(Loc, diag::err_template_kw_refers_to_non_template) |
| 1154 | << DTN->getName(); |
| 1155 | return Name; |
| 1156 | } else if (TNK == TNK_Function_template) { |
| 1157 | Diag(Loc, diag::err_template_kw_refers_to_non_template) |
| 1158 | << DTN->getName(); |
| 1159 | return Name; |
| 1160 | } |
| 1161 | |
| 1162 | return Template.getAsVal<TemplateName>(); |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1167 | // FIXME: Even if we're referring to a Decl that isn't a template template |
| 1168 | // parameter, we may need to instantiate the outer contexts of that |
| 1169 | // Decl. However, this won't be needed until we implement member templates. |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1170 | return Name; |
| 1171 | } |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1172 | |
| 1173 | TemplateArgument Sema::Instantiate(TemplateArgument Arg, |
| 1174 | const TemplateArgumentList &TemplateArgs) { |
| 1175 | switch (Arg.getKind()) { |
| 1176 | case TemplateArgument::Null: |
| 1177 | assert(false && "Should never have a NULL template argument"); |
| 1178 | break; |
| 1179 | |
| 1180 | case TemplateArgument::Type: { |
| 1181 | QualType T = InstantiateType(Arg.getAsType(), TemplateArgs, |
| 1182 | Arg.getLocation(), DeclarationName()); |
| 1183 | if (T.isNull()) |
| 1184 | return TemplateArgument(); |
| 1185 | |
| 1186 | return TemplateArgument(Arg.getLocation(), T); |
| 1187 | } |
| 1188 | |
| 1189 | case TemplateArgument::Declaration: |
| 1190 | // FIXME: Template instantiation for template template parameters. |
| 1191 | return Arg; |
| 1192 | |
| 1193 | case TemplateArgument::Integral: |
| 1194 | return Arg; |
| 1195 | |
| 1196 | case TemplateArgument::Expression: { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1197 | // Template argument expressions are not potentially evaluated. |
| 1198 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 1199 | |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1200 | Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs); |
| 1201 | if (E.isInvalid()) |
| 1202 | return TemplateArgument(); |
| 1203 | return TemplateArgument(E.takeAs<Expr>()); |
| 1204 | } |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1205 | |
| 1206 | case TemplateArgument::Pack: |
| 1207 | assert(0 && "FIXME: Implement!"); |
| 1208 | break; |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | assert(false && "Unhandled template argument kind"); |
| 1212 | return TemplateArgument(); |
| 1213 | } |