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