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