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