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