SemaTemplateInstantiate.cpp revision c86a6e988184867b09aa17a619402d0e81d0fda0
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 "TreeTransform.h" 15#include "clang/AST/ASTConsumer.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/Expr.h" 18#include "clang/AST/DeclTemplate.h" 19#include "clang/Parse/DeclSpec.h" 20#include "clang/Basic/LangOptions.h" 21#include "llvm/Support/Compiler.h" 22 23using namespace clang; 24 25//===----------------------------------------------------------------------===/ 26// Template Instantiation Support 27//===----------------------------------------------------------------------===/ 28 29/// \brief Retrieve the template argument list(s) that should be used to 30/// instantiate the definition of the given declaration. 31MultiLevelTemplateArgumentList 32Sema::getTemplateInstantiationArgs(NamedDecl *D) { 33 // Accumulate the set of template argument lists in this structure. 34 MultiLevelTemplateArgumentList Result; 35 36 DeclContext *Ctx = dyn_cast<DeclContext>(D); 37 if (!Ctx) 38 Ctx = D->getDeclContext(); 39 40 while (!Ctx->isFileContext()) { 41 // Add template arguments from a class template instantiation. 42 if (ClassTemplateSpecializationDecl *Spec 43 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { 44 // We're done when we hit an explicit specialization. 45 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization) 46 break; 47 48 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); 49 50 // If this class template specialization was instantiated from a 51 // specialized member that is a class template, we're done. 52 assert(Spec->getSpecializedTemplate() && "No class template?"); 53 if (Spec->getSpecializedTemplate()->isMemberSpecialization()) 54 break; 55 } 56 // Add template arguments from a function template specialization. 57 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) { 58 if (Function->getTemplateSpecializationKind() 59 == TSK_ExplicitSpecialization) 60 break; 61 62 if (const TemplateArgumentList *TemplateArgs 63 = Function->getTemplateSpecializationArgs()) { 64 // Add the template arguments for this specialization. 65 Result.addOuterTemplateArguments(TemplateArgs); 66 67 // If this function was instantiated from a specialized member that is 68 // a function template, we're done. 69 assert(Function->getPrimaryTemplate() && "No function template?"); 70 if (Function->getPrimaryTemplate()->isMemberSpecialization()) 71 break; 72 } 73 74 // If this is a friend declaration and it declares an entity at 75 // namespace scope, take arguments from its lexical parent 76 // instead of its semantic parent. 77 if (Function->getFriendObjectKind() && 78 Function->getDeclContext()->isFileContext()) { 79 Ctx = Function->getLexicalDeclContext(); 80 continue; 81 } 82 } 83 84 Ctx = Ctx->getParent(); 85 } 86 87 return Result; 88} 89 90Sema::InstantiatingTemplate:: 91InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, 92 Decl *Entity, 93 SourceRange InstantiationRange) 94 : SemaRef(SemaRef) { 95 96 Invalid = CheckInstantiationDepth(PointOfInstantiation, 97 InstantiationRange); 98 if (!Invalid) { 99 ActiveTemplateInstantiation Inst; 100 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; 101 Inst.PointOfInstantiation = PointOfInstantiation; 102 Inst.Entity = reinterpret_cast<uintptr_t>(Entity); 103 Inst.TemplateArgs = 0; 104 Inst.NumTemplateArgs = 0; 105 Inst.InstantiationRange = InstantiationRange; 106 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 107 Invalid = false; 108 } 109} 110 111Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, 112 SourceLocation PointOfInstantiation, 113 TemplateDecl *Template, 114 const TemplateArgument *TemplateArgs, 115 unsigned NumTemplateArgs, 116 SourceRange InstantiationRange) 117 : SemaRef(SemaRef) { 118 119 Invalid = CheckInstantiationDepth(PointOfInstantiation, 120 InstantiationRange); 121 if (!Invalid) { 122 ActiveTemplateInstantiation Inst; 123 Inst.Kind 124 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; 125 Inst.PointOfInstantiation = PointOfInstantiation; 126 Inst.Entity = reinterpret_cast<uintptr_t>(Template); 127 Inst.TemplateArgs = TemplateArgs; 128 Inst.NumTemplateArgs = NumTemplateArgs; 129 Inst.InstantiationRange = InstantiationRange; 130 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 131 Invalid = false; 132 } 133} 134 135Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, 136 SourceLocation PointOfInstantiation, 137 FunctionTemplateDecl *FunctionTemplate, 138 const TemplateArgument *TemplateArgs, 139 unsigned NumTemplateArgs, 140 ActiveTemplateInstantiation::InstantiationKind Kind, 141 SourceRange InstantiationRange) 142: SemaRef(SemaRef) { 143 144 Invalid = CheckInstantiationDepth(PointOfInstantiation, 145 InstantiationRange); 146 if (!Invalid) { 147 ActiveTemplateInstantiation Inst; 148 Inst.Kind = Kind; 149 Inst.PointOfInstantiation = PointOfInstantiation; 150 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate); 151 Inst.TemplateArgs = TemplateArgs; 152 Inst.NumTemplateArgs = NumTemplateArgs; 153 Inst.InstantiationRange = InstantiationRange; 154 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 155 Invalid = false; 156 } 157} 158 159Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, 160 SourceLocation PointOfInstantiation, 161 ClassTemplatePartialSpecializationDecl *PartialSpec, 162 const TemplateArgument *TemplateArgs, 163 unsigned NumTemplateArgs, 164 SourceRange InstantiationRange) 165 : SemaRef(SemaRef) { 166 167 Invalid = CheckInstantiationDepth(PointOfInstantiation, 168 InstantiationRange); 169 if (!Invalid) { 170 ActiveTemplateInstantiation Inst; 171 Inst.Kind 172 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution; 173 Inst.PointOfInstantiation = PointOfInstantiation; 174 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec); 175 Inst.TemplateArgs = TemplateArgs; 176 Inst.NumTemplateArgs = NumTemplateArgs; 177 Inst.InstantiationRange = InstantiationRange; 178 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 179 Invalid = false; 180 } 181} 182 183Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, 184 SourceLocation PointOfInstantation, 185 ParmVarDecl *Param, 186 const TemplateArgument *TemplateArgs, 187 unsigned NumTemplateArgs, 188 SourceRange InstantiationRange) 189 : SemaRef(SemaRef) { 190 191 Invalid = CheckInstantiationDepth(PointOfInstantation, InstantiationRange); 192 193 if (!Invalid) { 194 ActiveTemplateInstantiation Inst; 195 Inst.Kind 196 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; 197 Inst.PointOfInstantiation = PointOfInstantation; 198 Inst.Entity = reinterpret_cast<uintptr_t>(Param); 199 Inst.TemplateArgs = TemplateArgs; 200 Inst.NumTemplateArgs = NumTemplateArgs; 201 Inst.InstantiationRange = InstantiationRange; 202 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 203 Invalid = false; 204 } 205} 206 207void Sema::InstantiatingTemplate::Clear() { 208 if (!Invalid) { 209 SemaRef.ActiveTemplateInstantiations.pop_back(); 210 Invalid = true; 211 } 212} 213 214bool Sema::InstantiatingTemplate::CheckInstantiationDepth( 215 SourceLocation PointOfInstantiation, 216 SourceRange InstantiationRange) { 217 if (SemaRef.ActiveTemplateInstantiations.size() 218 <= SemaRef.getLangOptions().InstantiationDepth) 219 return false; 220 221 SemaRef.Diag(PointOfInstantiation, 222 diag::err_template_recursion_depth_exceeded) 223 << SemaRef.getLangOptions().InstantiationDepth 224 << InstantiationRange; 225 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) 226 << SemaRef.getLangOptions().InstantiationDepth; 227 return true; 228} 229 230/// \brief Prints the current instantiation stack through a series of 231/// notes. 232void Sema::PrintInstantiationStack() { 233 // FIXME: In all of these cases, we need to show the template arguments 234 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator 235 Active = ActiveTemplateInstantiations.rbegin(), 236 ActiveEnd = ActiveTemplateInstantiations.rend(); 237 Active != ActiveEnd; 238 ++Active) { 239 switch (Active->Kind) { 240 case ActiveTemplateInstantiation::TemplateInstantiation: { 241 Decl *D = reinterpret_cast<Decl *>(Active->Entity); 242 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { 243 unsigned DiagID = diag::note_template_member_class_here; 244 if (isa<ClassTemplateSpecializationDecl>(Record)) 245 DiagID = diag::note_template_class_instantiation_here; 246 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), 247 DiagID) 248 << Context.getTypeDeclType(Record) 249 << Active->InstantiationRange; 250 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 251 unsigned DiagID; 252 if (Function->getPrimaryTemplate()) 253 DiagID = diag::note_function_template_spec_here; 254 else 255 DiagID = diag::note_template_member_function_here; 256 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), 257 DiagID) 258 << Function 259 << Active->InstantiationRange; 260 } else { 261 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), 262 diag::note_template_static_data_member_def_here) 263 << cast<VarDecl>(D) 264 << Active->InstantiationRange; 265 } 266 break; 267 } 268 269 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { 270 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); 271 std::string TemplateArgsStr 272 = TemplateSpecializationType::PrintTemplateArgumentList( 273 Active->TemplateArgs, 274 Active->NumTemplateArgs, 275 Context.PrintingPolicy); 276 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), 277 diag::note_default_arg_instantiation_here) 278 << (Template->getNameAsString() + TemplateArgsStr) 279 << Active->InstantiationRange; 280 break; 281 } 282 283 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: { 284 FunctionTemplateDecl *FnTmpl 285 = cast<FunctionTemplateDecl>((Decl *)Active->Entity); 286 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), 287 diag::note_explicit_template_arg_substitution_here) 288 << FnTmpl << Active->InstantiationRange; 289 break; 290 } 291 292 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: 293 if (ClassTemplatePartialSpecializationDecl *PartialSpec 294 = dyn_cast<ClassTemplatePartialSpecializationDecl>( 295 (Decl *)Active->Entity)) { 296 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), 297 diag::note_partial_spec_deduct_instantiation_here) 298 << Context.getTypeDeclType(PartialSpec) 299 << Active->InstantiationRange; 300 } else { 301 FunctionTemplateDecl *FnTmpl 302 = cast<FunctionTemplateDecl>((Decl *)Active->Entity); 303 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), 304 diag::note_function_template_deduction_instantiation_here) 305 << FnTmpl << Active->InstantiationRange; 306 } 307 break; 308 309 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: { 310 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity); 311 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); 312 313 std::string TemplateArgsStr 314 = TemplateSpecializationType::PrintTemplateArgumentList( 315 Active->TemplateArgs, 316 Active->NumTemplateArgs, 317 Context.PrintingPolicy); 318 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), 319 diag::note_default_function_arg_instantiation_here) 320 << (FD->getNameAsString() + TemplateArgsStr) 321 << Active->InstantiationRange; 322 break; 323 } 324 325 } 326 } 327} 328 329bool Sema::isSFINAEContext() const { 330 using llvm::SmallVector; 331 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator 332 Active = ActiveTemplateInstantiations.rbegin(), 333 ActiveEnd = ActiveTemplateInstantiations.rend(); 334 Active != ActiveEnd; 335 ++Active) { 336 337 switch(Active->Kind) { 338 case ActiveTemplateInstantiation::TemplateInstantiation: 339 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: 340 341 // This is a template instantiation, so there is no SFINAE. 342 return false; 343 344 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: 345 // A default template argument instantiation may or may not be a 346 // SFINAE context; look further up the stack. 347 break; 348 349 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: 350 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: 351 // We're either substitution explicitly-specified template arguments 352 // or deduced template arguments, so SFINAE applies. 353 return true; 354 } 355 } 356 357 return false; 358} 359 360//===----------------------------------------------------------------------===/ 361// Template Instantiation for Types 362//===----------------------------------------------------------------------===/ 363namespace { 364 class VISIBILITY_HIDDEN TemplateInstantiator 365 : public TreeTransform<TemplateInstantiator> { 366 const MultiLevelTemplateArgumentList &TemplateArgs; 367 SourceLocation Loc; 368 DeclarationName Entity; 369 370 public: 371 typedef TreeTransform<TemplateInstantiator> inherited; 372 373 TemplateInstantiator(Sema &SemaRef, 374 const MultiLevelTemplateArgumentList &TemplateArgs, 375 SourceLocation Loc, 376 DeclarationName Entity) 377 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), 378 Entity(Entity) { } 379 380 /// \brief Determine whether the given type \p T has already been 381 /// transformed. 382 /// 383 /// For the purposes of template instantiation, a type has already been 384 /// transformed if it is NULL or if it is not dependent. 385 bool AlreadyTransformed(QualType T) { 386 return T.isNull() || !T->isDependentType(); 387 } 388 389 /// \brief Returns the location of the entity being instantiated, if known. 390 SourceLocation getBaseLocation() { return Loc; } 391 392 /// \brief Returns the name of the entity being instantiated, if any. 393 DeclarationName getBaseEntity() { return Entity; } 394 395 /// \brief Sets the "base" location and entity when that 396 /// information is known based on another transformation. 397 void setBase(SourceLocation Loc, DeclarationName Entity) { 398 this->Loc = Loc; 399 this->Entity = Entity; 400 } 401 402 /// \brief Transform the given declaration by instantiating a reference to 403 /// this declaration. 404 Decl *TransformDecl(Decl *D); 405 406 /// \brief Transform the definition of the given declaration by 407 /// instantiating it. 408 Decl *TransformDefinition(Decl *D); 409 410 /// \bried Transform the first qualifier within a scope by instantiating the 411 /// declaration. 412 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); 413 414 /// \brief Rebuild the exception declaration and register the declaration 415 /// as an instantiated local. 416 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T, 417 DeclaratorInfo *Declarator, 418 IdentifierInfo *Name, 419 SourceLocation Loc, SourceRange TypeRange); 420 421 /// \brief Check for tag mismatches when instantiating an 422 /// elaborated type. 423 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag); 424 425 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E, 426 bool isAddressOfOperand); 427 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E, 428 bool isAddressOfOperand); 429 430 /// \brief Transforms a template type parameter type by performing 431 /// substitution of the corresponding template type argument. 432 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, 433 TemplateTypeParmTypeLoc TL); 434 }; 435} 436 437Decl *TemplateInstantiator::TransformDecl(Decl *D) { 438 if (!D) 439 return 0; 440 441 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { 442 if (TTP->getDepth() < TemplateArgs.getNumLevels()) { 443 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() && 444 "Wrong kind of template template argument"); 445 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(), 446 TTP->getPosition()).getAsDecl()); 447 } 448 449 // If the corresponding template argument is NULL or non-existent, it's 450 // because we are performing instantiation from explicitly-specified 451 // template arguments in a function template, but there were some 452 // arguments left unspecified. 453 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), 454 TTP->getPosition())) 455 return D; 456 457 // FIXME: Implement depth reduction of template template parameters 458 assert(false && 459 "Reducing depth of template template parameters is not yet implemented"); 460 } 461 462 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs); 463} 464 465Decl *TemplateInstantiator::TransformDefinition(Decl *D) { 466 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); 467 if (!Inst) 468 return 0; 469 470 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); 471 return Inst; 472} 473 474NamedDecl * 475TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, 476 SourceLocation Loc) { 477 // If the first part of the nested-name-specifier was a template type 478 // parameter, instantiate that type parameter down to a tag type. 479 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { 480 const TemplateTypeParmType *TTP 481 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); 482 if (TTP->getDepth() < TemplateArgs.getNumLevels()) { 483 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType(); 484 if (T.isNull()) 485 return cast_or_null<NamedDecl>(TransformDecl(D)); 486 487 if (const TagType *Tag = T->getAs<TagType>()) 488 return Tag->getDecl(); 489 490 // The resulting type is not a tag; complain. 491 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; 492 return 0; 493 } 494 } 495 496 return cast_or_null<NamedDecl>(TransformDecl(D)); 497} 498 499VarDecl * 500TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, 501 QualType T, 502 DeclaratorInfo *Declarator, 503 IdentifierInfo *Name, 504 SourceLocation Loc, 505 SourceRange TypeRange) { 506 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator, 507 Name, Loc, TypeRange); 508 if (Var && !Var->isInvalidDecl()) 509 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); 510 return Var; 511} 512 513QualType 514TemplateInstantiator::RebuildElaboratedType(QualType T, 515 ElaboratedType::TagKind Tag) { 516 if (const TagType *TT = T->getAs<TagType>()) { 517 TagDecl* TD = TT->getDecl(); 518 519 // FIXME: this location is very wrong; we really need typelocs. 520 SourceLocation TagLocation = TD->getTagKeywordLoc(); 521 522 // FIXME: type might be anonymous. 523 IdentifierInfo *Id = TD->getIdentifier(); 524 525 // TODO: should we even warn on struct/class mismatches for this? Seems 526 // like it's likely to produce a lot of spurious errors. 527 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) { 528 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) 529 << Id 530 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation), 531 TD->getKindName()); 532 SemaRef.Diag(TD->getLocation(), diag::note_previous_use); 533 } 534 } 535 536 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag); 537} 538 539Sema::OwningExprResult 540TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E, 541 bool isAddressOfOperand) { 542 if (!E->isTypeDependent()) 543 return SemaRef.Owned(E->Retain()); 544 545 FunctionDecl *currentDecl = getSema().getCurFunctionDecl(); 546 assert(currentDecl && "Must have current function declaration when " 547 "instantiating."); 548 549 PredefinedExpr::IdentType IT = E->getIdentType(); 550 551 unsigned Length = 552 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length(); 553 554 llvm::APInt LengthI(32, Length + 1); 555 QualType ResTy = getSema().Context.CharTy.withConst(); 556 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI, 557 ArrayType::Normal, 0); 558 PredefinedExpr *PE = 559 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT); 560 return getSema().Owned(PE); 561} 562 563Sema::OwningExprResult 564TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E, 565 bool isAddressOfOperand) { 566 // FIXME: Clean this up a bit 567 NamedDecl *D = E->getDecl(); 568 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { 569 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) { 570 571 // If the corresponding template argument is NULL or non-existent, it's 572 // because we are performing instantiation from explicitly-specified 573 // template arguments in a function template, but there were some 574 // arguments left unspecified. 575 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), 576 NTTP->getPosition())) 577 return SemaRef.Owned(E->Retain()); 578 579 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(), 580 NTTP->getPosition()); 581 582 // The template argument itself might be an expression, in which 583 // case we just return that expression. 584 if (Arg.getKind() == TemplateArgument::Expression) 585 return SemaRef.Owned(Arg.getAsExpr()->Retain()); 586 587 if (Arg.getKind() == TemplateArgument::Declaration) { 588 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); 589 590 VD = cast_or_null<ValueDecl>( 591 getSema().FindInstantiatedDecl(VD, TemplateArgs)); 592 if (!VD) 593 return SemaRef.ExprError(); 594 595 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(), 596 /*FIXME:*/false, /*FIXME:*/false); 597 } 598 599 assert(Arg.getKind() == TemplateArgument::Integral); 600 QualType T = Arg.getIntegralType(); 601 if (T->isCharType() || T->isWideCharType()) 602 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral( 603 Arg.getAsIntegral()->getZExtValue(), 604 T->isWideCharType(), 605 T, 606 E->getSourceRange().getBegin())); 607 if (T->isBooleanType()) 608 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr( 609 Arg.getAsIntegral()->getBoolValue(), 610 T, 611 E->getSourceRange().getBegin())); 612 613 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T)); 614 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral( 615 *Arg.getAsIntegral(), 616 T, 617 E->getSourceRange().getBegin())); 618 } 619 620 // We have a non-type template parameter that isn't fully substituted; 621 // FindInstantiatedDecl will find it in the local instantiation scope. 622 } 623 624 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs); 625 if (!InstD) 626 return SemaRef.ExprError(); 627 628 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl, 629 // we need to get the underlying decl. 630 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this? 631 InstD = InstD->getUnderlyingDecl(); 632 633 CXXScopeSpec SS; 634 NestedNameSpecifier *Qualifier = 0; 635 if (E->getQualifier()) { 636 Qualifier = TransformNestedNameSpecifier(E->getQualifier(), 637 E->getQualifierRange()); 638 if (!Qualifier) 639 return SemaRef.ExprError(); 640 641 SS.setScopeRep(Qualifier); 642 SS.setRange(E->getQualifierRange()); 643 } 644 645 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD, 646 /*FIXME:*/false, 647 &SS, 648 isAddressOfOperand); 649} 650 651QualType 652TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, 653 TemplateTypeParmTypeLoc TL) { 654 TemplateTypeParmType *T = TL.getTypePtr(); 655 if (T->getDepth() < TemplateArgs.getNumLevels()) { 656 // Replace the template type parameter with its corresponding 657 // template argument. 658 659 // If the corresponding template argument is NULL or doesn't exist, it's 660 // because we are performing instantiation from explicitly-specified 661 // template arguments in a function template class, but there were some 662 // arguments left unspecified. 663 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { 664 TemplateTypeParmTypeLoc NewTL 665 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); 666 NewTL.setNameLoc(TL.getNameLoc()); 667 return TL.getType(); 668 } 669 670 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind() 671 == TemplateArgument::Type && 672 "Template argument kind mismatch"); 673 674 QualType Replacement 675 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType(); 676 677 // TODO: only do this uniquing once, at the start of instantiation. 678 QualType Result 679 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); 680 SubstTemplateTypeParmTypeLoc NewTL 681 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); 682 NewTL.setNameLoc(TL.getNameLoc()); 683 return Result; 684 } 685 686 // The template type parameter comes from an inner template (e.g., 687 // the template parameter list of a member template inside the 688 // template we are instantiating). Create a new template type 689 // parameter with the template "level" reduced by one. 690 QualType Result 691 = getSema().Context.getTemplateTypeParmType(T->getDepth() 692 - TemplateArgs.getNumLevels(), 693 T->getIndex(), 694 T->isParameterPack(), 695 T->getName()); 696 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); 697 NewTL.setNameLoc(TL.getNameLoc()); 698 return Result; 699} 700 701/// \brief Perform substitution on the type T with a given set of template 702/// arguments. 703/// 704/// This routine substitutes the given template arguments into the 705/// type T and produces the instantiated type. 706/// 707/// \param T the type into which the template arguments will be 708/// substituted. If this type is not dependent, it will be returned 709/// immediately. 710/// 711/// \param TemplateArgs the template arguments that will be 712/// substituted for the top-level template parameters within T. 713/// 714/// \param Loc the location in the source code where this substitution 715/// is being performed. It will typically be the location of the 716/// declarator (if we're instantiating the type of some declaration) 717/// or the location of the type in the source code (if, e.g., we're 718/// instantiating the type of a cast expression). 719/// 720/// \param Entity the name of the entity associated with a declaration 721/// being instantiated (if any). May be empty to indicate that there 722/// is no such entity (if, e.g., this is a type that occurs as part of 723/// a cast expression) or that the entity has no name (e.g., an 724/// unnamed function parameter). 725/// 726/// \returns If the instantiation succeeds, the instantiated 727/// type. Otherwise, produces diagnostics and returns a NULL type. 728DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T, 729 const MultiLevelTemplateArgumentList &Args, 730 SourceLocation Loc, 731 DeclarationName Entity) { 732 assert(!ActiveTemplateInstantiations.empty() && 733 "Cannot perform an instantiation without some context on the " 734 "instantiation stack"); 735 736 if (!T->getType()->isDependentType()) 737 return T; 738 739 TemplateInstantiator Instantiator(*this, Args, Loc, Entity); 740 return Instantiator.TransformType(T); 741} 742 743/// Deprecated form of the above. 744QualType Sema::SubstType(QualType T, 745 const MultiLevelTemplateArgumentList &TemplateArgs, 746 SourceLocation Loc, DeclarationName Entity) { 747 assert(!ActiveTemplateInstantiations.empty() && 748 "Cannot perform an instantiation without some context on the " 749 "instantiation stack"); 750 751 // If T is not a dependent type, there is nothing to do. 752 if (!T->isDependentType()) 753 return T; 754 755 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); 756 return Instantiator.TransformType(T); 757} 758 759/// \brief Perform substitution on the base class specifiers of the 760/// given class template specialization. 761/// 762/// Produces a diagnostic and returns true on error, returns false and 763/// attaches the instantiated base classes to the class template 764/// specialization if successful. 765bool 766Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, 767 CXXRecordDecl *Pattern, 768 const MultiLevelTemplateArgumentList &TemplateArgs) { 769 bool Invalid = false; 770 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; 771 for (ClassTemplateSpecializationDecl::base_class_iterator 772 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); 773 Base != BaseEnd; ++Base) { 774 if (!Base->getType()->isDependentType()) { 775 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); 776 continue; 777 } 778 779 QualType BaseType = SubstType(Base->getType(), 780 TemplateArgs, 781 Base->getSourceRange().getBegin(), 782 DeclarationName()); 783 if (BaseType.isNull()) { 784 Invalid = true; 785 continue; 786 } 787 788 if (CXXBaseSpecifier *InstantiatedBase 789 = CheckBaseSpecifier(Instantiation, 790 Base->getSourceRange(), 791 Base->isVirtual(), 792 Base->getAccessSpecifierAsWritten(), 793 BaseType, 794 /*FIXME: Not totally accurate */ 795 Base->getSourceRange().getBegin())) 796 InstantiatedBases.push_back(InstantiatedBase); 797 else 798 Invalid = true; 799 } 800 801 if (!Invalid && 802 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), 803 InstantiatedBases.size())) 804 Invalid = true; 805 806 return Invalid; 807} 808 809/// \brief Instantiate the definition of a class from a given pattern. 810/// 811/// \param PointOfInstantiation The point of instantiation within the 812/// source code. 813/// 814/// \param Instantiation is the declaration whose definition is being 815/// instantiated. This will be either a class template specialization 816/// or a member class of a class template specialization. 817/// 818/// \param Pattern is the pattern from which the instantiation 819/// occurs. This will be either the declaration of a class template or 820/// the declaration of a member class of a class template. 821/// 822/// \param TemplateArgs The template arguments to be substituted into 823/// the pattern. 824/// 825/// \param TSK the kind of implicit or explicit instantiation to perform. 826/// 827/// \param Complain whether to complain if the class cannot be instantiated due 828/// to the lack of a definition. 829/// 830/// \returns true if an error occurred, false otherwise. 831bool 832Sema::InstantiateClass(SourceLocation PointOfInstantiation, 833 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, 834 const MultiLevelTemplateArgumentList &TemplateArgs, 835 TemplateSpecializationKind TSK, 836 bool Complain) { 837 bool Invalid = false; 838 839 CXXRecordDecl *PatternDef 840 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); 841 if (!PatternDef) { 842 if (!Complain) { 843 // Say nothing 844 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { 845 Diag(PointOfInstantiation, 846 diag::err_implicit_instantiate_member_undefined) 847 << Context.getTypeDeclType(Instantiation); 848 Diag(Pattern->getLocation(), diag::note_member_of_template_here); 849 } else { 850 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) 851 << (TSK != TSK_ImplicitInstantiation) 852 << Context.getTypeDeclType(Instantiation); 853 Diag(Pattern->getLocation(), diag::note_template_decl_here); 854 } 855 return true; 856 } 857 Pattern = PatternDef; 858 859 // \brief Record the point of instantiation. 860 if (MemberSpecializationInfo *MSInfo 861 = Instantiation->getMemberSpecializationInfo()) { 862 MSInfo->setTemplateSpecializationKind(TSK); 863 MSInfo->setPointOfInstantiation(PointOfInstantiation); 864 } else if (ClassTemplateSpecializationDecl *Spec 865 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { 866 Spec->setTemplateSpecializationKind(TSK); 867 Spec->setPointOfInstantiation(PointOfInstantiation); 868 } 869 870 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); 871 if (Inst) 872 return true; 873 874 // Enter the scope of this instantiation. We don't use 875 // PushDeclContext because we don't have a scope. 876 DeclContext *PreviousContext = CurContext; 877 CurContext = Instantiation; 878 879 // Start the definition of this instantiation. 880 Instantiation->startDefinition(); 881 882 // Do substitution on the base class specifiers. 883 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) 884 Invalid = true; 885 886 llvm::SmallVector<DeclPtrTy, 4> Fields; 887 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), 888 MemberEnd = Pattern->decls_end(); 889 Member != MemberEnd; ++Member) { 890 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs); 891 if (NewMember) { 892 if (NewMember->isInvalidDecl()) 893 Invalid = true; 894 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) 895 Fields.push_back(DeclPtrTy::make(Field)); 896 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember)) 897 Instantiation->addDecl(UD); 898 } else { 899 // FIXME: Eventually, a NULL return will mean that one of the 900 // instantiations was a semantic disaster, and we'll want to set Invalid = 901 // true. For now, we expect to skip some members that we can't yet handle. 902 } 903 } 904 905 // Finish checking fields. 906 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation), 907 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), 908 0); 909 if (Instantiation->isInvalidDecl()) 910 Invalid = true; 911 912 // Add any implicitly-declared members that we might need. 913 if (!Invalid) 914 AddImplicitlyDeclaredMembersToClass(Instantiation); 915 916 // Exit the scope of this instantiation. 917 CurContext = PreviousContext; 918 919 if (!Invalid) 920 Consumer.HandleTagDeclDefinition(Instantiation); 921 922 return Invalid; 923} 924 925bool 926Sema::InstantiateClassTemplateSpecialization( 927 SourceLocation PointOfInstantiation, 928 ClassTemplateSpecializationDecl *ClassTemplateSpec, 929 TemplateSpecializationKind TSK, 930 bool Complain) { 931 // Perform the actual instantiation on the canonical declaration. 932 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( 933 ClassTemplateSpec->getCanonicalDecl()); 934 935 // Check whether we have already instantiated or specialized this class 936 // template specialization. 937 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) { 938 if (ClassTemplateSpec->getSpecializationKind() == 939 TSK_ExplicitInstantiationDeclaration && 940 TSK == TSK_ExplicitInstantiationDefinition) { 941 // An explicit instantiation definition follows an explicit instantiation 942 // declaration (C++0x [temp.explicit]p10); go ahead and perform the 943 // explicit instantiation. 944 ClassTemplateSpec->setSpecializationKind(TSK); 945 return false; 946 } 947 948 // We can only instantiate something that hasn't already been 949 // instantiated or specialized. Fail without any diagnostics: our 950 // caller will provide an error message. 951 return true; 952 } 953 954 if (ClassTemplateSpec->isInvalidDecl()) 955 return true; 956 957 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); 958 CXXRecordDecl *Pattern = 0; 959 960 // C++ [temp.class.spec.match]p1: 961 // When a class template is used in a context that requires an 962 // instantiation of the class, it is necessary to determine 963 // whether the instantiation is to be generated using the primary 964 // template or one of the partial specializations. This is done by 965 // matching the template arguments of the class template 966 // specialization with the template argument lists of the partial 967 // specializations. 968 typedef std::pair<ClassTemplatePartialSpecializationDecl *, 969 TemplateArgumentList *> MatchResult; 970 llvm::SmallVector<MatchResult, 4> Matched; 971 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator 972 Partial = Template->getPartialSpecializations().begin(), 973 PartialEnd = Template->getPartialSpecializations().end(); 974 Partial != PartialEnd; 975 ++Partial) { 976 TemplateDeductionInfo Info(Context); 977 if (TemplateDeductionResult Result 978 = DeduceTemplateArguments(&*Partial, 979 ClassTemplateSpec->getTemplateArgs(), 980 Info)) { 981 // FIXME: Store the failed-deduction information for use in 982 // diagnostics, later. 983 (void)Result; 984 } else { 985 Matched.push_back(std::make_pair(&*Partial, Info.take())); 986 } 987 } 988 989 if (Matched.size() >= 1) { 990 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); 991 if (Matched.size() == 1) { 992 // -- If exactly one matching specialization is found, the 993 // instantiation is generated from that specialization. 994 // We don't need to do anything for this. 995 } else { 996 // -- If more than one matching specialization is found, the 997 // partial order rules (14.5.4.2) are used to determine 998 // whether one of the specializations is more specialized 999 // than the others. If none of the specializations is more 1000 // specialized than all of the other matching 1001 // specializations, then the use of the class template is 1002 // ambiguous and the program is ill-formed. 1003 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1, 1004 PEnd = Matched.end(); 1005 P != PEnd; ++P) { 1006 if (getMoreSpecializedPartialSpecialization(P->first, Best->first) 1007 == P->first) 1008 Best = P; 1009 } 1010 1011 // Determine if the best partial specialization is more specialized than 1012 // the others. 1013 bool Ambiguous = false; 1014 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), 1015 PEnd = Matched.end(); 1016 P != PEnd; ++P) { 1017 if (P != Best && 1018 getMoreSpecializedPartialSpecialization(P->first, Best->first) 1019 != Best->first) { 1020 Ambiguous = true; 1021 break; 1022 } 1023 } 1024 1025 if (Ambiguous) { 1026 // Partial ordering did not produce a clear winner. Complain. 1027 ClassTemplateSpec->setInvalidDecl(); 1028 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) 1029 << ClassTemplateSpec; 1030 1031 // Print the matching partial specializations. 1032 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), 1033 PEnd = Matched.end(); 1034 P != PEnd; ++P) 1035 Diag(P->first->getLocation(), diag::note_partial_spec_match) 1036 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(), 1037 *P->second); 1038 1039 return true; 1040 } 1041 } 1042 1043 // Instantiate using the best class template partial specialization. 1044 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first; 1045 while (OrigPartialSpec->getInstantiatedFromMember()) { 1046 // If we've found an explicit specialization of this class template, 1047 // stop here and use that as the pattern. 1048 if (OrigPartialSpec->isMemberSpecialization()) 1049 break; 1050 1051 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember(); 1052 } 1053 1054 Pattern = OrigPartialSpec; 1055 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second); 1056 } else { 1057 // -- If no matches are found, the instantiation is generated 1058 // from the primary template. 1059 ClassTemplateDecl *OrigTemplate = Template; 1060 while (OrigTemplate->getInstantiatedFromMemberTemplate()) { 1061 // If we've found an explicit specialization of this class template, 1062 // stop here and use that as the pattern. 1063 if (OrigTemplate->isMemberSpecialization()) 1064 break; 1065 1066 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); 1067 } 1068 1069 Pattern = OrigTemplate->getTemplatedDecl(); 1070 } 1071 1072 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec, 1073 Pattern, 1074 getTemplateInstantiationArgs(ClassTemplateSpec), 1075 TSK, 1076 Complain); 1077 1078 for (unsigned I = 0, N = Matched.size(); I != N; ++I) { 1079 // FIXME: Implement TemplateArgumentList::Destroy! 1080 // if (Matched[I].first != Pattern) 1081 // Matched[I].second->Destroy(Context); 1082 } 1083 1084 return Result; 1085} 1086 1087/// \brief Instantiates the definitions of all of the member 1088/// of the given class, which is an instantiation of a class template 1089/// or a member class of a template. 1090void 1091Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, 1092 CXXRecordDecl *Instantiation, 1093 const MultiLevelTemplateArgumentList &TemplateArgs, 1094 TemplateSpecializationKind TSK) { 1095 for (DeclContext::decl_iterator D = Instantiation->decls_begin(), 1096 DEnd = Instantiation->decls_end(); 1097 D != DEnd; ++D) { 1098 bool SuppressNew = false; 1099 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { 1100 if (FunctionDecl *Pattern 1101 = Function->getInstantiatedFromMemberFunction()) { 1102 MemberSpecializationInfo *MSInfo 1103 = Function->getMemberSpecializationInfo(); 1104 assert(MSInfo && "No member specialization information?"); 1105 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, 1106 Function, 1107 MSInfo->getTemplateSpecializationKind(), 1108 MSInfo->getPointOfInstantiation(), 1109 SuppressNew) || 1110 SuppressNew) 1111 continue; 1112 1113 if (Function->getBody()) 1114 continue; 1115 1116 if (TSK == TSK_ExplicitInstantiationDefinition) { 1117 // C++0x [temp.explicit]p8: 1118 // An explicit instantiation definition that names a class template 1119 // specialization explicitly instantiates the class template 1120 // specialization and is only an explicit instantiation definition 1121 // of members whose definition is visible at the point of 1122 // instantiation. 1123 if (!Pattern->getBody()) 1124 continue; 1125 1126 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); 1127 1128 InstantiateFunctionDefinition(PointOfInstantiation, Function); 1129 } else { 1130 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); 1131 } 1132 } 1133 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { 1134 if (Var->isStaticDataMember()) { 1135 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); 1136 assert(MSInfo && "No member specialization information?"); 1137 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, 1138 Var, 1139 MSInfo->getTemplateSpecializationKind(), 1140 MSInfo->getPointOfInstantiation(), 1141 SuppressNew) || 1142 SuppressNew) 1143 continue; 1144 1145 if (TSK == TSK_ExplicitInstantiationDefinition) { 1146 // C++0x [temp.explicit]p8: 1147 // An explicit instantiation definition that names a class template 1148 // specialization explicitly instantiates the class template 1149 // specialization and is only an explicit instantiation definition 1150 // of members whose definition is visible at the point of 1151 // instantiation. 1152 if (!Var->getInstantiatedFromStaticDataMember() 1153 ->getOutOfLineDefinition()) 1154 continue; 1155 1156 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); 1157 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); 1158 } else { 1159 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); 1160 } 1161 } 1162 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { 1163 if (Record->isInjectedClassName()) 1164 continue; 1165 1166 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); 1167 assert(MSInfo && "No member specialization information?"); 1168 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, 1169 Record, 1170 MSInfo->getTemplateSpecializationKind(), 1171 MSInfo->getPointOfInstantiation(), 1172 SuppressNew) || 1173 SuppressNew) 1174 continue; 1175 1176 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); 1177 assert(Pattern && "Missing instantiated-from-template information"); 1178 1179 if (!Record->getDefinition(Context)) { 1180 if (!Pattern->getDefinition(Context)) { 1181 // C++0x [temp.explicit]p8: 1182 // An explicit instantiation definition that names a class template 1183 // specialization explicitly instantiates the class template 1184 // specialization and is only an explicit instantiation definition 1185 // of members whose definition is visible at the point of 1186 // instantiation. 1187 if (TSK == TSK_ExplicitInstantiationDeclaration) { 1188 MSInfo->setTemplateSpecializationKind(TSK); 1189 MSInfo->setPointOfInstantiation(PointOfInstantiation); 1190 } 1191 1192 continue; 1193 } 1194 1195 InstantiateClass(PointOfInstantiation, Record, Pattern, 1196 TemplateArgs, 1197 TSK); 1198 } 1199 1200 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context)); 1201 if (Pattern) 1202 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, 1203 TSK); 1204 } 1205 } 1206} 1207 1208/// \brief Instantiate the definitions of all of the members of the 1209/// given class template specialization, which was named as part of an 1210/// explicit instantiation. 1211void 1212Sema::InstantiateClassTemplateSpecializationMembers( 1213 SourceLocation PointOfInstantiation, 1214 ClassTemplateSpecializationDecl *ClassTemplateSpec, 1215 TemplateSpecializationKind TSK) { 1216 // C++0x [temp.explicit]p7: 1217 // An explicit instantiation that names a class template 1218 // specialization is an explicit instantion of the same kind 1219 // (declaration or definition) of each of its members (not 1220 // including members inherited from base classes) that has not 1221 // been previously explicitly specialized in the translation unit 1222 // containing the explicit instantiation, except as described 1223 // below. 1224 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, 1225 getTemplateInstantiationArgs(ClassTemplateSpec), 1226 TSK); 1227} 1228 1229Sema::OwningStmtResult 1230Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { 1231 if (!S) 1232 return Owned(S); 1233 1234 TemplateInstantiator Instantiator(*this, TemplateArgs, 1235 SourceLocation(), 1236 DeclarationName()); 1237 return Instantiator.TransformStmt(S); 1238} 1239 1240Sema::OwningExprResult 1241Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { 1242 if (!E) 1243 return Owned(E); 1244 1245 TemplateInstantiator Instantiator(*this, TemplateArgs, 1246 SourceLocation(), 1247 DeclarationName()); 1248 return Instantiator.TransformExpr(E); 1249} 1250 1251/// \brief Do template substitution on a nested-name-specifier. 1252NestedNameSpecifier * 1253Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS, 1254 SourceRange Range, 1255 const MultiLevelTemplateArgumentList &TemplateArgs) { 1256 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(), 1257 DeclarationName()); 1258 return Instantiator.TransformNestedNameSpecifier(NNS, Range); 1259} 1260 1261TemplateName 1262Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc, 1263 const MultiLevelTemplateArgumentList &TemplateArgs) { 1264 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, 1265 DeclarationName()); 1266 return Instantiator.TransformTemplateName(Name); 1267} 1268 1269bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output, 1270 const MultiLevelTemplateArgumentList &TemplateArgs) { 1271 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), 1272 DeclarationName()); 1273 1274 return Instantiator.TransformTemplateArgument(Input, Output); 1275} 1276