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