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