SemaTemplateInstantiate.cpp revision a0e500db61f5fcf3ff6de6af1494f0cea9f6f8db
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" 16#include "clang/AST/ExprCXX.h" 17#include "clang/AST/DeclTemplate.h" 18#include "clang/Parse/DeclSpec.h" 19#include "clang/Basic/LangOptions.h" 20#include "llvm/Support/Compiler.h" 21 22using namespace clang; 23 24//===----------------------------------------------------------------------===/ 25// Template Instantiation Support 26//===----------------------------------------------------------------------===/ 27 28Sema::InstantiatingTemplate:: 29InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, 30 ClassTemplateSpecializationDecl *Entity, 31 SourceRange InstantiationRange) 32 : SemaRef(SemaRef) { 33 34 Invalid = CheckInstantiationDepth(PointOfInstantiation, 35 InstantiationRange); 36 if (!Invalid) { 37 ActiveTemplateInstantiation Inst; 38 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; 39 Inst.PointOfInstantiation = PointOfInstantiation; 40 Inst.Entity = reinterpret_cast<uintptr_t>(Entity); 41 Inst.InstantiationRange = InstantiationRange; 42 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 43 Invalid = false; 44 } 45} 46 47Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, 48 SourceLocation PointOfInstantiation, 49 TemplateDecl *Template, 50 const TemplateArgument *TemplateArgs, 51 unsigned NumTemplateArgs, 52 SourceRange InstantiationRange) 53 : SemaRef(SemaRef) { 54 55 Invalid = CheckInstantiationDepth(PointOfInstantiation, 56 InstantiationRange); 57 if (!Invalid) { 58 ActiveTemplateInstantiation Inst; 59 Inst.Kind 60 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; 61 Inst.PointOfInstantiation = PointOfInstantiation; 62 Inst.Entity = reinterpret_cast<uintptr_t>(Template); 63 Inst.TemplateArgs = TemplateArgs; 64 Inst.NumTemplateArgs = NumTemplateArgs; 65 Inst.InstantiationRange = InstantiationRange; 66 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 67 Invalid = false; 68 } 69} 70 71Sema::InstantiatingTemplate::~InstantiatingTemplate() { 72 if (!Invalid) 73 SemaRef.ActiveTemplateInstantiations.pop_back(); 74} 75 76bool Sema::InstantiatingTemplate::CheckInstantiationDepth( 77 SourceLocation PointOfInstantiation, 78 SourceRange InstantiationRange) { 79 if (SemaRef.ActiveTemplateInstantiations.size() 80 <= SemaRef.getLangOptions().InstantiationDepth) 81 return false; 82 83 SemaRef.Diag(PointOfInstantiation, 84 diag::err_template_recursion_depth_exceeded) 85 << SemaRef.getLangOptions().InstantiationDepth 86 << InstantiationRange; 87 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) 88 << SemaRef.getLangOptions().InstantiationDepth; 89 return true; 90} 91 92/// \brief Post-diagnostic hook for printing the instantiation stack. 93void Sema::PrintInstantiationStackHook(unsigned, void *Cookie) { 94 Sema &SemaRef = *static_cast<Sema*>(Cookie); 95 SemaRef.PrintInstantiationStack(); 96 SemaRef.LastTemplateInstantiationErrorContext 97 = SemaRef.ActiveTemplateInstantiations.back(); 98} 99 100/// \brief Prints the current instantiation stack through a series of 101/// notes. 102void Sema::PrintInstantiationStack() { 103 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator 104 Active = ActiveTemplateInstantiations.rbegin(), 105 ActiveEnd = ActiveTemplateInstantiations.rend(); 106 Active != ActiveEnd; 107 ++Active) { 108 switch (Active->Kind) { 109 case ActiveTemplateInstantiation::TemplateInstantiation: { 110 ClassTemplateSpecializationDecl *Spec 111 = cast<ClassTemplateSpecializationDecl>((Decl*)Active->Entity); 112 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), 113 diag::note_template_class_instantiation_here) 114 << Context.getTypeDeclType(Spec) 115 << Active->InstantiationRange; 116 break; 117 } 118 119 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { 120 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); 121 std::string TemplateArgsStr 122 = ClassTemplateSpecializationType::PrintTemplateArgumentList( 123 Active->TemplateArgs, 124 Active->NumTemplateArgs); 125 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), 126 diag::note_default_arg_instantiation_here) 127 << (Template->getNameAsString() + TemplateArgsStr) 128 << Active->InstantiationRange; 129 break; 130 } 131 } 132 } 133} 134 135//===----------------------------------------------------------------------===/ 136// Template Instantiation for Types 137//===----------------------------------------------------------------------===/ 138namespace { 139 class VISIBILITY_HIDDEN TemplateTypeInstantiator { 140 Sema &SemaRef; 141 const TemplateArgument *TemplateArgs; 142 unsigned NumTemplateArgs; 143 SourceLocation Loc; 144 DeclarationName Entity; 145 146 public: 147 TemplateTypeInstantiator(Sema &SemaRef, 148 const TemplateArgument *TemplateArgs, 149 unsigned NumTemplateArgs, 150 SourceLocation Loc, 151 DeclarationName Entity) 152 : SemaRef(SemaRef), TemplateArgs(TemplateArgs), 153 NumTemplateArgs(NumTemplateArgs), Loc(Loc), Entity(Entity) { } 154 155 QualType operator()(QualType T) const { return Instantiate(T); } 156 157 QualType Instantiate(QualType T) const; 158 159 // Declare instantiate functions for each type. 160#define TYPE(Class, Base) \ 161 QualType Instantiate##Class##Type(const Class##Type *T, \ 162 unsigned Quals) const; 163#define ABSTRACT_TYPE(Class, Base) 164#include "clang/AST/TypeNodes.def" 165 }; 166} 167 168QualType 169TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T, 170 unsigned Quals) const { 171 // FIXME: Implement this 172 assert(false && "Cannot instantiate ExtQualType yet"); 173 return QualType(); 174} 175 176QualType 177TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T, 178 unsigned Quals) const { 179 assert(false && "Builtin types are not dependent and cannot be instantiated"); 180 return QualType(T, Quals); 181} 182 183QualType 184TemplateTypeInstantiator:: 185InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const { 186 // FIXME: Implement this 187 assert(false && "Cannot instantiate FixedWidthIntType yet"); 188 return QualType(); 189} 190 191QualType 192TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T, 193 unsigned Quals) const { 194 // FIXME: Implement this 195 assert(false && "Cannot instantiate ComplexType yet"); 196 return QualType(); 197} 198 199QualType 200TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T, 201 unsigned Quals) const { 202 QualType PointeeType = Instantiate(T->getPointeeType()); 203 if (PointeeType.isNull()) 204 return QualType(); 205 206 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity); 207} 208 209QualType 210TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T, 211 unsigned Quals) const { 212 // FIXME: Implement this 213 assert(false && "Cannot instantiate BlockPointerType yet"); 214 return QualType(); 215} 216 217QualType 218TemplateTypeInstantiator::InstantiateReferenceType(const ReferenceType *T, 219 unsigned Quals) const { 220 QualType ReferentType = Instantiate(T->getPointeeType()); 221 if (ReferentType.isNull()) 222 return QualType(); 223 224 return SemaRef.BuildReferenceType(ReferentType, Quals, Loc, Entity); 225} 226 227QualType 228TemplateTypeInstantiator:: 229InstantiateMemberPointerType(const MemberPointerType *T, 230 unsigned Quals) const { 231 // FIXME: Implement this 232 assert(false && "Cannot instantiate MemberPointerType yet"); 233 return QualType(); 234} 235 236QualType 237TemplateTypeInstantiator:: 238InstantiateConstantArrayType(const ConstantArrayType *T, 239 unsigned Quals) const { 240 QualType ElementType = Instantiate(T->getElementType()); 241 if (ElementType.isNull()) 242 return ElementType; 243 244 // Build a temporary integer literal to specify the size for 245 // BuildArrayType. Since we have already checked the size as part of 246 // creating the dependent array type in the first place, we know 247 // there aren't any errors. 248 // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes 249 // problems that I have yet to investigate. 250 IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc); 251 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), 252 &ArraySize, T->getIndexTypeQualifier(), 253 Loc, Entity); 254} 255 256QualType 257TemplateTypeInstantiator:: 258InstantiateIncompleteArrayType(const IncompleteArrayType *T, 259 unsigned Quals) const { 260 QualType ElementType = Instantiate(T->getElementType()); 261 if (ElementType.isNull()) 262 return ElementType; 263 264 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), 265 0, T->getIndexTypeQualifier(), 266 Loc, Entity); 267} 268 269QualType 270TemplateTypeInstantiator:: 271InstantiateVariableArrayType(const VariableArrayType *T, 272 unsigned Quals) const { 273 // FIXME: Implement this 274 assert(false && "Cannot instantiate VariableArrayType yet"); 275 return QualType(); 276} 277 278QualType 279TemplateTypeInstantiator:: 280InstantiateDependentSizedArrayType(const DependentSizedArrayType *T, 281 unsigned Quals) const { 282 // FIXME: Implement this 283 assert(false && "Cannot instantiate DependentSizedArrayType yet"); 284 return QualType(); 285} 286 287QualType 288TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T, 289 unsigned Quals) const { 290 // FIXME: Implement this 291 assert(false && "Cannot instantiate VectorType yet"); 292 return QualType(); 293} 294 295QualType 296TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T, 297 unsigned Quals) const { 298 // FIXME: Implement this 299 assert(false && "Cannot instantiate ExtVectorType yet"); 300 return QualType(); 301} 302 303QualType 304TemplateTypeInstantiator:: 305InstantiateFunctionProtoType(const FunctionProtoType *T, 306 unsigned Quals) const { 307 QualType ResultType = Instantiate(T->getResultType()); 308 if (ResultType.isNull()) 309 return ResultType; 310 311 llvm::SmallVector<QualType, 16> ParamTypes; 312 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(), 313 ParamEnd = T->arg_type_end(); 314 Param != ParamEnd; ++Param) { 315 QualType P = Instantiate(*Param); 316 if (P.isNull()) 317 return P; 318 319 ParamTypes.push_back(P); 320 } 321 322 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0], 323 ParamTypes.size(), 324 T->isVariadic(), T->getTypeQuals(), 325 Loc, Entity); 326} 327 328QualType 329TemplateTypeInstantiator:: 330InstantiateFunctionNoProtoType(const FunctionNoProtoType *T, 331 unsigned Quals) const { 332 assert(false && "Functions without prototypes cannot be dependent."); 333 return QualType(); 334} 335 336QualType 337TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T, 338 unsigned Quals) const { 339 // FIXME: Implement this 340 assert(false && "Cannot instantiate TypedefType yet"); 341 return QualType(); 342} 343 344QualType 345TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T, 346 unsigned Quals) const { 347 // FIXME: Implement this 348 assert(false && "Cannot instantiate TypeOfExprType yet"); 349 return QualType(); 350} 351 352QualType 353TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T, 354 unsigned Quals) const { 355 // FIXME: Implement this 356 assert(false && "Cannot instantiate TypeOfType yet"); 357 return QualType(); 358} 359 360QualType 361TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T, 362 unsigned Quals) const { 363 // FIXME: Implement this 364 assert(false && "Cannot instantiate RecordType yet"); 365 return QualType(); 366} 367 368QualType 369TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T, 370 unsigned Quals) const { 371 // FIXME: Implement this 372 assert(false && "Cannot instantiate EnumType yet"); 373 return QualType(); 374} 375 376QualType 377TemplateTypeInstantiator:: 378InstantiateTemplateTypeParmType(const TemplateTypeParmType *T, 379 unsigned Quals) const { 380 if (T->getDepth() == 0) { 381 // Replace the template type parameter with its corresponding 382 // template argument. 383 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args"); 384 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type && 385 "Template argument kind mismatch"); 386 QualType Result = TemplateArgs[T->getIndex()].getAsType(); 387 if (Result.isNull() || !Quals) 388 return Result; 389 390 // C++ [dcl.ref]p1: 391 // [...] Cv-qualified references are ill-formed except when 392 // the cv-qualifiers are introduced through the use of a 393 // typedef (7.1.3) or of a template type argument (14.3), in 394 // which case the cv-qualifiers are ignored. 395 if (Quals && Result->isReferenceType()) 396 Quals = 0; 397 398 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers()); 399 } 400 401 // The template type parameter comes from an inner template (e.g., 402 // the template parameter list of a member template inside the 403 // template we are instantiating). Create a new template type 404 // parameter with the template "level" reduced by one. 405 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1, 406 T->getIndex(), 407 T->getName()) 408 .getQualifiedType(Quals); 409} 410 411QualType 412TemplateTypeInstantiator:: 413InstantiateClassTemplateSpecializationType( 414 const ClassTemplateSpecializationType *T, 415 unsigned Quals) const { 416 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs; 417 InstantiatedTemplateArgs.reserve(T->getNumArgs()); 418 for (ClassTemplateSpecializationType::iterator Arg = T->begin(), 419 ArgEnd = T->end(); 420 Arg != ArgEnd; ++Arg) { 421 switch (Arg->getKind()) { 422 case TemplateArgument::Type: { 423 QualType T = SemaRef.InstantiateType(Arg->getAsType(), 424 TemplateArgs, NumTemplateArgs, 425 Arg->getLocation(), 426 DeclarationName()); 427 if (T.isNull()) 428 return QualType(); 429 430 InstantiatedTemplateArgs.push_back( 431 TemplateArgument(Arg->getLocation(), T)); 432 break; 433 } 434 435 case TemplateArgument::Declaration: 436 case TemplateArgument::Integral: 437 InstantiatedTemplateArgs.push_back(*Arg); 438 break; 439 440 case TemplateArgument::Expression: 441 assert(false && "Cannot instantiate expressions yet"); 442 break; 443 } 444 } 445 446 // FIXME: We're missing the locations of the template name, '<', and 447 // '>'. 448 return SemaRef.CheckClassTemplateId(cast<ClassTemplateDecl>(T->getTemplate()), 449 Loc, 450 SourceLocation(), 451 &InstantiatedTemplateArgs[0], 452 InstantiatedTemplateArgs.size(), 453 SourceLocation()); 454} 455 456QualType 457TemplateTypeInstantiator:: 458InstantiateObjCInterfaceType(const ObjCInterfaceType *T, 459 unsigned Quals) const { 460 assert(false && "Objective-C types cannot be dependent"); 461 return QualType(); 462} 463 464QualType 465TemplateTypeInstantiator:: 466InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T, 467 unsigned Quals) const { 468 assert(false && "Objective-C types cannot be dependent"); 469 return QualType(); 470} 471 472QualType 473TemplateTypeInstantiator:: 474InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T, 475 unsigned Quals) const { 476 assert(false && "Objective-C types cannot be dependent"); 477 return QualType(); 478} 479 480QualType 481TemplateTypeInstantiator:: 482InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T, 483 unsigned Quals) const { 484 assert(false && "Objective-C types cannot be dependent"); 485 return QualType(); 486} 487 488/// \brief The actual implementation of Sema::InstantiateType(). 489QualType TemplateTypeInstantiator::Instantiate(QualType T) const { 490 // If T is not a dependent type, there is nothing to do. 491 if (!T->isDependentType()) 492 return T; 493 494 switch (T->getTypeClass()) { 495#define TYPE(Class, Base) \ 496 case Type::Class: \ 497 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \ 498 T.getCVRQualifiers()); 499#define ABSTRACT_TYPE(Class, Base) 500#include "clang/AST/TypeNodes.def" 501 } 502 503 assert(false && "Not all types have been decoded for instantiation"); 504 return QualType(); 505} 506 507/// \brief Instantiate the type T with a given set of template arguments. 508/// 509/// This routine substitutes the given template arguments into the 510/// type T and produces the instantiated type. 511/// 512/// \param T the type into which the template arguments will be 513/// substituted. If this type is not dependent, it will be returned 514/// immediately. 515/// 516/// \param TemplateArgs the template arguments that will be 517/// substituted for the top-level template parameters within T. 518/// 519/// \param NumTemplateArgs the number of template arguments provided 520/// by TemplateArgs. 521/// 522/// \param Loc the location in the source code where this substitution 523/// is being performed. It will typically be the location of the 524/// declarator (if we're instantiating the type of some declaration) 525/// or the location of the type in the source code (if, e.g., we're 526/// instantiating the type of a cast expression). 527/// 528/// \param Entity the name of the entity associated with a declaration 529/// being instantiated (if any). May be empty to indicate that there 530/// is no such entity (if, e.g., this is a type that occurs as part of 531/// a cast expression) or that the entity has no name (e.g., an 532/// unnamed function parameter). 533/// 534/// \returns If the instantiation succeeds, the instantiated 535/// type. Otherwise, produces diagnostics and returns a NULL type. 536QualType Sema::InstantiateType(QualType T, 537 const TemplateArgument *TemplateArgs, 538 unsigned NumTemplateArgs, 539 SourceLocation Loc, DeclarationName Entity) { 540 assert(!ActiveTemplateInstantiations.empty() && 541 "Cannot perform an instantiation without some context on the " 542 "instantiation stack"); 543 544 // If T is not a dependent type, there is nothing to do. 545 if (!T->isDependentType()) 546 return T; 547 548 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs, 549 Loc, Entity); 550 return Instantiator(T); 551} 552 553//===----------------------------------------------------------------------===/ 554// Template Instantiation for Expressions 555//===----------------------------------------------------------------------===/ 556Sema::OwningExprResult 557Sema::InstantiateExpr(Expr *E, const TemplateArgument *TemplateArgs, 558 unsigned NumTemplateArgs) { 559 if (IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) 560 return Owned(new (Context) IntegerLiteral(IL->getValue(), IL->getType(), 561 IL->getSourceRange().getBegin())); 562 else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 563 Decl *D = DRE->getDecl(); 564 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { 565 assert(NTTP->getDepth() == 0 && "No nested templates yet"); 566 QualType T = NTTP->getType(); 567 if (T->isDependentType()) { 568 // FIXME: We'll be doing this instantiation a lot. Should we 569 // cache this information in the TemplateArgument itself? 570 T = InstantiateType(T, TemplateArgs, NumTemplateArgs, 571 E->getSourceRange().getBegin(), 572 NTTP->getDeclName()); 573 if (T.isNull()) 574 return ExprError(); 575 } 576 return Owned(new (Context) IntegerLiteral( 577 *TemplateArgs[NTTP->getPosition()].getAsIntegral(), 578 T, E->getSourceRange().getBegin())); 579 } else 580 assert(false && "Yes, this is lame"); 581 } else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { 582 OwningExprResult SubExpr 583 = InstantiateExpr(PE->getSubExpr(), TemplateArgs, 584 NumTemplateArgs); 585 if (SubExpr.isInvalid()) 586 return ExprError(); 587 588 return Owned(new (Context) ParenExpr(E->getSourceRange().getBegin(), 589 E->getSourceRange().getEnd(), 590 (Expr *)SubExpr.release())); 591 } else 592 assert(false && "Yes, this is lame"); 593 594 return ExprError(); 595} 596 597/// \brief Instantiate the base class specifiers of the given class 598/// template specialization. 599/// 600/// Produces a diagnostic and returns true on error, returns false and 601/// attaches the instantiated base classes to the class template 602/// specialization if successful. 603bool 604Sema::InstantiateBaseSpecifiers( 605 ClassTemplateSpecializationDecl *ClassTemplateSpec, 606 ClassTemplateDecl *ClassTemplate) { 607 bool Invalid = false; 608 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases; 609 for (ClassTemplateSpecializationDecl::base_class_iterator 610 Base = ClassTemplate->getTemplatedDecl()->bases_begin(), 611 BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end(); 612 Base != BaseEnd; ++Base) { 613 if (!Base->getType()->isDependentType()) { 614 // FIXME: Allocate via ASTContext 615 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base)); 616 continue; 617 } 618 619 QualType BaseType = InstantiateType(Base->getType(), 620 ClassTemplateSpec->getTemplateArgs(), 621 ClassTemplateSpec->getNumTemplateArgs(), 622 Base->getSourceRange().getBegin(), 623 DeclarationName()); 624 if (BaseType.isNull()) { 625 Invalid = true; 626 continue; 627 } 628 629 if (CXXBaseSpecifier *InstantiatedBase 630 = CheckBaseSpecifier(ClassTemplateSpec, 631 Base->getSourceRange(), 632 Base->isVirtual(), 633 Base->getAccessSpecifierAsWritten(), 634 BaseType, 635 /*FIXME: Not totally accurate */ 636 Base->getSourceRange().getBegin())) 637 InstantiatedBases.push_back(InstantiatedBase); 638 else 639 Invalid = true; 640 } 641 642 if (!Invalid && 643 AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0], 644 InstantiatedBases.size())) 645 Invalid = true; 646 647 return Invalid; 648} 649 650bool 651Sema::InstantiateClassTemplateSpecialization( 652 ClassTemplateSpecializationDecl *ClassTemplateSpec, 653 bool ExplicitInstantiation) { 654 // Perform the actual instantiation on the canonical declaration. 655 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( 656 Context.getCanonicalDecl(ClassTemplateSpec)); 657 658 // We can only instantiate something that hasn't already been 659 // instantiated or specialized. Fail without any diagnostics: our 660 // caller will provide an error message. 661 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) 662 return true; 663 664 // FIXME: Push this class template instantiation onto the 665 // instantiation stack, checking for recursion that exceeds a 666 // certain depth. 667 668 // FIXME: Perform class template partial specialization to select 669 // the best template. 670 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); 671 672 if (!Template->getTemplatedDecl()->getDefinition(Context)) { 673 Diag(ClassTemplateSpec->getLocation(), 674 diag::err_template_implicit_instantiate_undefined) 675 << Context.getTypeDeclType(ClassTemplateSpec); 676 Diag(Template->getTemplatedDecl()->getLocation(), 677 diag::note_template_decl_here); 678 return true; 679 } 680 681 // Note that this is an instantiation. 682 ClassTemplateSpec->setSpecializationKind( 683 ExplicitInstantiation? TSK_ExplicitInstantiation 684 : TSK_ImplicitInstantiation); 685 686 687 bool Invalid = false; 688 689 InstantiatingTemplate Inst(*this, ClassTemplateSpec->getLocation(), 690 ClassTemplateSpec); 691 if (Inst) 692 return true; 693 694 // Enter the scope of this instantiation. We don't use 695 // PushDeclContext because we don't have a scope. 696 DeclContext *PreviousContext = CurContext; 697 CurContext = ClassTemplateSpec; 698 699 // Start the definition of this instantiation. 700 ClassTemplateSpec->startDefinition(); 701 702 703 // Instantiate the base class specifiers. 704 if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template)) 705 Invalid = true; 706 707 // FIXME: Create the injected-class-name for the 708 // instantiation. Should this be a typedef or something like it? 709 710 RecordDecl *Pattern = Template->getTemplatedDecl(); 711 llvm::SmallVector<DeclTy *, 32> Fields; 712 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), 713 MemberEnd = Pattern->decls_end(); 714 Member != MemberEnd; ++Member) { 715 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(*Member)) { 716 // FIXME: Simplified instantiation of typedefs needs to be made 717 // "real". 718 QualType T = Typedef->getUnderlyingType(); 719 if (T->isDependentType()) { 720 T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(), 721 ClassTemplateSpec->getNumTemplateArgs(), 722 Typedef->getLocation(), 723 Typedef->getDeclName()); 724 if (T.isNull()) { 725 Invalid = true; 726 T = Context.IntTy; 727 } 728 } 729 730 // Create the new typedef 731 TypedefDecl *New 732 = TypedefDecl::Create(Context, ClassTemplateSpec, 733 Typedef->getLocation(), 734 Typedef->getIdentifier(), 735 T); 736 ClassTemplateSpec->addDecl(New); 737 } 738 else if (FieldDecl *Field = dyn_cast<FieldDecl>(*Member)) { 739 // FIXME: Simplified instantiation of fields needs to be made 740 // "real". 741 bool InvalidDecl = false; 742 QualType T = Field->getType(); 743 if (T->isDependentType()) { 744 T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(), 745 ClassTemplateSpec->getNumTemplateArgs(), 746 Field->getLocation(), 747 Field->getDeclName()); 748 if (!T.isNull() && T->isFunctionType()) { 749 // C++ [temp.arg.type]p3: 750 // If a declaration acquires a function type through a type 751 // dependent on a template-parameter and this causes a 752 // declaration that does not use the syntactic form of a 753 // function declarator to have function type, the program is 754 // ill-formed. 755 Diag(Field->getLocation(), diag::err_field_instantiates_to_function) 756 << T; 757 T = QualType(); 758 InvalidDecl = true; 759 } 760 } 761 762 Expr *BitWidth = Field->getBitWidth(); 763 if (InvalidDecl) 764 BitWidth = 0; 765 if (BitWidth && 766 (BitWidth->isTypeDependent() || BitWidth->isValueDependent())) { 767 OwningExprResult InstantiatedBitWidth 768 = InstantiateExpr(BitWidth, 769 ClassTemplateSpec->getTemplateArgs(), 770 ClassTemplateSpec->getNumTemplateArgs()); 771 if (InstantiatedBitWidth.isInvalid()) { 772 Invalid = InvalidDecl = true; 773 BitWidth = 0; 774 } else 775 BitWidth = (Expr *)InstantiatedBitWidth.release(); 776 } 777 778 FieldDecl *New = CheckFieldDecl(Field->getDeclName(), T, 779 ClassTemplateSpec, 780 Field->getLocation(), 781 Field->isMutable(), 782 BitWidth, 783 Field->getAccess(), 784 0); 785 if (New) { 786 ClassTemplateSpec->addDecl(New); 787 Fields.push_back(New); 788 789 if (InvalidDecl) 790 New->setInvalidDecl(); 791 792 if (New->isInvalidDecl()) 793 Invalid = true; 794 } 795 } 796 } 797 798 // Finish checking fields. 799 ActOnFields(0, ClassTemplateSpec->getLocation(), ClassTemplateSpec, 800 &Fields[0], Fields.size(), SourceLocation(), SourceLocation(), 801 0); 802 803 // Add any implicitly-declared members that we might need. 804 AddImplicitlyDeclaredMembersToClass(ClassTemplateSpec); 805 806 // Exit the scope of this instantiation. 807 CurContext = PreviousContext; 808 809 return Invalid; 810} 811