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