SemaTemplateInstantiateDecl.cpp revision 60c93c9981c467738369702e7aa23fd58c2b6aac
1//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl 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 for declarations. 10// 11//===----------------------------------------------------------------------===/ 12#include "Sema.h" 13#include "Lookup.h" 14#include "clang/AST/ASTConsumer.h" 15#include "clang/AST/ASTContext.h" 16#include "clang/AST/DeclTemplate.h" 17#include "clang/AST/DeclVisitor.h" 18#include "clang/AST/Expr.h" 19#include "clang/AST/ExprCXX.h" 20#include "clang/Basic/PrettyStackTrace.h" 21#include "clang/Lex/Preprocessor.h" 22 23using namespace clang; 24 25namespace { 26 class TemplateDeclInstantiator 27 : public DeclVisitor<TemplateDeclInstantiator, Decl *> { 28 Sema &SemaRef; 29 DeclContext *Owner; 30 const MultiLevelTemplateArgumentList &TemplateArgs; 31 32 void InstantiateAttrs(Decl *Tmpl, Decl *New); 33 34 public: 35 typedef Sema::OwningExprResult OwningExprResult; 36 37 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, 38 const MultiLevelTemplateArgumentList &TemplateArgs) 39 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { } 40 41 // FIXME: Once we get closer to completion, replace these manually-written 42 // declarations with automatically-generated ones from 43 // clang/AST/DeclNodes.def. 44 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); 45 Decl *VisitNamespaceDecl(NamespaceDecl *D); 46 Decl *VisitTypedefDecl(TypedefDecl *D); 47 Decl *VisitVarDecl(VarDecl *D); 48 Decl *VisitFieldDecl(FieldDecl *D); 49 Decl *VisitStaticAssertDecl(StaticAssertDecl *D); 50 Decl *VisitEnumDecl(EnumDecl *D); 51 Decl *VisitEnumConstantDecl(EnumConstantDecl *D); 52 Decl *VisitFriendDecl(FriendDecl *D); 53 Decl *VisitFunctionDecl(FunctionDecl *D, 54 TemplateParameterList *TemplateParams = 0); 55 Decl *VisitCXXRecordDecl(CXXRecordDecl *D); 56 Decl *VisitCXXMethodDecl(CXXMethodDecl *D, 57 TemplateParameterList *TemplateParams = 0); 58 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); 59 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); 60 Decl *VisitCXXConversionDecl(CXXConversionDecl *D); 61 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); 62 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); 63 Decl *VisitClassTemplatePartialSpecializationDecl( 64 ClassTemplatePartialSpecializationDecl *D); 65 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 66 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); 67 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); 68 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); 69 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 70 Decl *VisitUsingDecl(UsingDecl *D); 71 Decl *VisitUsingShadowDecl(UsingShadowDecl *D); 72 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 73 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 74 75 // Base case. FIXME: Remove once we can instantiate everything. 76 Decl *VisitDecl(Decl *D) { 77 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( 78 Diagnostic::Error, 79 "cannot instantiate %0 yet"); 80 SemaRef.Diag(D->getLocation(), DiagID) 81 << D->getDeclKindName(); 82 83 return 0; 84 } 85 86 const LangOptions &getLangOptions() { 87 return SemaRef.getLangOptions(); 88 } 89 90 // Helper functions for instantiating methods. 91 QualType SubstFunctionType(FunctionDecl *D, 92 llvm::SmallVectorImpl<ParmVarDecl *> &Params); 93 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl); 94 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); 95 96 TemplateParameterList * 97 SubstTemplateParams(TemplateParameterList *List); 98 99 bool InstantiateClassTemplatePartialSpecialization( 100 ClassTemplateDecl *ClassTemplate, 101 ClassTemplatePartialSpecializationDecl *PartialSpec); 102 }; 103} 104 105// FIXME: Is this too simple? 106void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) { 107 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr; 108 TmplAttr = TmplAttr->getNext()) { 109 110 // FIXME: Is cloning correct for all attributes? 111 Attr *NewAttr = TmplAttr->clone(SemaRef.Context); 112 113 New->addAttr(NewAttr); 114 } 115} 116 117Decl * 118TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 119 assert(false && "Translation units cannot be instantiated"); 120 return D; 121} 122 123Decl * 124TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { 125 assert(false && "Namespaces cannot be instantiated"); 126 return D; 127} 128 129Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { 130 bool Invalid = false; 131 TypeSourceInfo *DI = D->getTypeSourceInfo(); 132 if (DI->getType()->isDependentType()) { 133 DI = SemaRef.SubstType(DI, TemplateArgs, 134 D->getLocation(), D->getDeclName()); 135 if (!DI) { 136 Invalid = true; 137 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); 138 } 139 } 140 141 // Create the new typedef 142 TypedefDecl *Typedef 143 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), 144 D->getIdentifier(), DI); 145 if (Invalid) 146 Typedef->setInvalidDecl(); 147 148 if (TypedefDecl *Prev = D->getPreviousDeclaration()) { 149 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(Prev, TemplateArgs); 150 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev)); 151 } 152 153 Typedef->setAccess(D->getAccess()); 154 Owner->addDecl(Typedef); 155 156 return Typedef; 157} 158 159/// \brief Instantiate the arguments provided as part of initialization. 160/// 161/// \returns true if an error occurred, false otherwise. 162static bool InstantiateInitializationArguments(Sema &SemaRef, 163 Expr **Args, unsigned NumArgs, 164 const MultiLevelTemplateArgumentList &TemplateArgs, 165 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs, 166 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) { 167 for (unsigned I = 0; I != NumArgs; ++I) { 168 // When we hit the first defaulted argument, break out of the loop: 169 // we don't pass those default arguments on. 170 if (Args[I]->isDefaultArgument()) 171 break; 172 173 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs); 174 if (Arg.isInvalid()) 175 return true; 176 177 Expr *ArgExpr = (Expr *)Arg.get(); 178 InitArgs.push_back(Arg.release()); 179 180 // FIXME: We're faking all of the comma locations. Do we need them? 181 FakeCommaLocs.push_back( 182 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd())); 183 } 184 185 return false; 186} 187 188Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { 189 // Do substitution on the type of the declaration 190 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(), 191 TemplateArgs, 192 D->getTypeSpecStartLoc(), 193 D->getDeclName()); 194 if (!DI) 195 return 0; 196 197 // Build the instantiated declaration 198 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, 199 D->getLocation(), D->getIdentifier(), 200 DI->getType(), DI, 201 D->getStorageClass()); 202 Var->setThreadSpecified(D->isThreadSpecified()); 203 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer()); 204 Var->setDeclaredInCondition(D->isDeclaredInCondition()); 205 206 // If we are instantiating a static data member defined 207 // out-of-line, the instantiation will have the same lexical 208 // context (which will be a namespace scope) as the template. 209 if (D->isOutOfLine()) 210 Var->setLexicalDeclContext(D->getLexicalDeclContext()); 211 212 Var->setAccess(D->getAccess()); 213 214 // FIXME: In theory, we could have a previous declaration for variables that 215 // are not static data members. 216 bool Redeclaration = false; 217 // FIXME: having to fake up a LookupResult is dumb. 218 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(), 219 Sema::LookupOrdinaryName); 220 if (D->isStaticDataMember()) 221 SemaRef.LookupQualifiedName(Previous, Owner, false); 222 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration); 223 224 if (D->isOutOfLine()) { 225 D->getLexicalDeclContext()->addDecl(Var); 226 Owner->makeDeclVisibleInContext(Var); 227 } else { 228 Owner->addDecl(Var); 229 } 230 231 // Link instantiations of static data members back to the template from 232 // which they were instantiated. 233 if (Var->isStaticDataMember()) 234 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D, 235 TSK_ImplicitInstantiation); 236 237 if (Var->getAnyInitializer()) { 238 // We already have an initializer in the class. 239 } else if (D->getInit()) { 240 if (Var->isStaticDataMember() && !D->isOutOfLine()) 241 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated); 242 else 243 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); 244 245 // Extract the initializer, skipping through any temporary-binding 246 // expressions and look at the subexpression as it was written. 247 Expr *DInit = D->getInit(); 248 if (CXXExprWithTemporaries *ExprTemp 249 = dyn_cast<CXXExprWithTemporaries>(DInit)) 250 DInit = ExprTemp->getSubExpr(); 251 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(DInit)) 252 DInit = Binder->getSubExpr(); 253 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(DInit)) 254 DInit = ICE->getSubExprAsWritten(); 255 256 if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(DInit)) { 257 // The initializer is a parenthesized list of expressions that is 258 // type-dependent. Instantiate each of the expressions; we'll be 259 // performing direct initialization with them. 260 llvm::SmallVector<SourceLocation, 4> CommaLocs; 261 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef); 262 if (!InstantiateInitializationArguments(SemaRef, 263 PLE->getExprs(), 264 PLE->getNumExprs(), 265 TemplateArgs, 266 CommaLocs, InitArgs)) { 267 // Add the direct initializer to the declaration. 268 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var), 269 PLE->getLParenLoc(), 270 move_arg(InitArgs), 271 CommaLocs.data(), 272 PLE->getRParenLoc()); 273 } 274 } else if (CXXConstructExpr *Construct =dyn_cast<CXXConstructExpr>(DInit)) { 275 // The initializer resolved to a constructor. Instantiate the constructor 276 // arguments. 277 llvm::SmallVector<SourceLocation, 4> CommaLocs; 278 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef); 279 280 if (!InstantiateInitializationArguments(SemaRef, 281 Construct->getArgs(), 282 Construct->getNumArgs(), 283 TemplateArgs, 284 CommaLocs, InitArgs)) { 285 if (D->hasCXXDirectInitializer()) { 286 SourceLocation FakeLParenLoc = 287 SemaRef.PP.getLocForEndOfToken(D->getLocation()); 288 SourceLocation FakeRParenLoc = CommaLocs.empty()? FakeLParenLoc 289 : CommaLocs.back(); 290 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var), 291 FakeLParenLoc, 292 move_arg(InitArgs), 293 CommaLocs.data(), 294 FakeRParenLoc); 295 } else if (InitArgs.size() == 1) { 296 Expr *Init = (Expr*)(InitArgs.take()[0]); 297 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), 298 SemaRef.Owned(Init), 299 false); 300 } else { 301 assert(InitArgs.size() == 0); 302 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false); 303 } 304 } 305 } else { 306 OwningExprResult Init 307 = SemaRef.SubstExpr(D->getInit(), TemplateArgs); 308 309 // FIXME: Not happy about invalidating decls just because of a bad 310 // initializer, unless it affects the type. 311 if (Init.isInvalid()) 312 Var->setInvalidDecl(); 313 else 314 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), 315 D->hasCXXDirectInitializer()); 316 } 317 318 SemaRef.PopExpressionEvaluationContext(); 319 } else if (!Var->isStaticDataMember() || Var->isOutOfLine()) 320 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false); 321 322 return Var; 323} 324 325Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { 326 bool Invalid = false; 327 TypeSourceInfo *DI = D->getTypeSourceInfo(); 328 if (DI->getType()->isDependentType()) { 329 DI = SemaRef.SubstType(DI, TemplateArgs, 330 D->getLocation(), D->getDeclName()); 331 if (!DI) { 332 DI = D->getTypeSourceInfo(); 333 Invalid = true; 334 } else if (DI->getType()->isFunctionType()) { 335 // C++ [temp.arg.type]p3: 336 // If a declaration acquires a function type through a type 337 // dependent on a template-parameter and this causes a 338 // declaration that does not use the syntactic form of a 339 // function declarator to have function type, the program is 340 // ill-formed. 341 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) 342 << DI->getType(); 343 Invalid = true; 344 } 345 } 346 347 Expr *BitWidth = D->getBitWidth(); 348 if (Invalid) 349 BitWidth = 0; 350 else if (BitWidth) { 351 // The bit-width expression is not potentially evaluated. 352 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); 353 354 OwningExprResult InstantiatedBitWidth 355 = SemaRef.SubstExpr(BitWidth, TemplateArgs); 356 if (InstantiatedBitWidth.isInvalid()) { 357 Invalid = true; 358 BitWidth = 0; 359 } else 360 BitWidth = InstantiatedBitWidth.takeAs<Expr>(); 361 } 362 363 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), 364 DI->getType(), DI, 365 cast<RecordDecl>(Owner), 366 D->getLocation(), 367 D->isMutable(), 368 BitWidth, 369 D->getTypeSpecStartLoc(), 370 D->getAccess(), 371 0); 372 if (!Field) { 373 cast<Decl>(Owner)->setInvalidDecl(); 374 return 0; 375 } 376 377 InstantiateAttrs(D, Field); 378 379 if (Invalid) 380 Field->setInvalidDecl(); 381 382 if (!Field->getDeclName()) { 383 // Keep track of where this decl came from. 384 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); 385 } 386 387 Field->setImplicit(D->isImplicit()); 388 Field->setAccess(D->getAccess()); 389 Owner->addDecl(Field); 390 391 return Field; 392} 393 394Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { 395 FriendDecl::FriendUnion FU; 396 397 // Handle friend type expressions by simply substituting template 398 // parameters into the pattern type. 399 if (Type *Ty = D->getFriendType()) { 400 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs, 401 D->getLocation(), DeclarationName()); 402 if (T.isNull()) return 0; 403 404 assert(getLangOptions().CPlusPlus0x || T->isRecordType()); 405 FU = T.getTypePtr(); 406 407 // Handle everything else by appropriate substitution. 408 } else { 409 NamedDecl *ND = D->getFriendDecl(); 410 assert(ND && "friend decl must be a decl or a type!"); 411 412 // FIXME: We have a problem here, because the nested call to Visit(ND) 413 // will inject the thing that the friend references into the current 414 // owner, which is wrong. 415 Decl *NewND; 416 417 // Hack to make this work almost well pending a rewrite. 418 if (ND->getDeclContext()->isRecord()) { 419 if (!ND->getDeclContext()->isDependentContext()) { 420 NewND = SemaRef.FindInstantiatedDecl(ND, TemplateArgs); 421 } else { 422 // FIXME: Hack to avoid crashing when incorrectly trying to instantiate 423 // templated friend declarations. This doesn't produce a correct AST; 424 // however this is sufficient for some AST analysis. The real solution 425 // must be put in place during the pending rewrite. See PR5848. 426 return 0; 427 } 428 } else if (D->wasSpecialization()) { 429 // Totally egregious hack to work around PR5866 430 return 0; 431 } else 432 NewND = Visit(ND); 433 if (!NewND) return 0; 434 435 FU = cast<NamedDecl>(NewND); 436 } 437 438 FriendDecl *FD = 439 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU, 440 D->getFriendLoc()); 441 FD->setAccess(AS_public); 442 Owner->addDecl(FD); 443 return FD; 444} 445 446Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { 447 Expr *AssertExpr = D->getAssertExpr(); 448 449 // The expression in a static assertion is not potentially evaluated. 450 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); 451 452 OwningExprResult InstantiatedAssertExpr 453 = SemaRef.SubstExpr(AssertExpr, TemplateArgs); 454 if (InstantiatedAssertExpr.isInvalid()) 455 return 0; 456 457 OwningExprResult Message(SemaRef, D->getMessage()); 458 D->getMessage()->Retain(); 459 Decl *StaticAssert 460 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), 461 move(InstantiatedAssertExpr), 462 move(Message)).getAs<Decl>(); 463 return StaticAssert; 464} 465 466Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { 467 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, 468 D->getLocation(), D->getIdentifier(), 469 D->getTagKeywordLoc(), 470 /*PrevDecl=*/0); 471 Enum->setInstantiationOfMemberEnum(D); 472 Enum->setAccess(D->getAccess()); 473 Owner->addDecl(Enum); 474 Enum->startDefinition(); 475 476 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators; 477 478 EnumConstantDecl *LastEnumConst = 0; 479 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), 480 ECEnd = D->enumerator_end(); 481 EC != ECEnd; ++EC) { 482 // The specified value for the enumerator. 483 OwningExprResult Value = SemaRef.Owned((Expr *)0); 484 if (Expr *UninstValue = EC->getInitExpr()) { 485 // The enumerator's value expression is not potentially evaluated. 486 EnterExpressionEvaluationContext Unevaluated(SemaRef, 487 Action::Unevaluated); 488 489 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); 490 } 491 492 // Drop the initial value and continue. 493 bool isInvalid = false; 494 if (Value.isInvalid()) { 495 Value = SemaRef.Owned((Expr *)0); 496 isInvalid = true; 497 } 498 499 EnumConstantDecl *EnumConst 500 = SemaRef.CheckEnumConstant(Enum, LastEnumConst, 501 EC->getLocation(), EC->getIdentifier(), 502 move(Value)); 503 504 if (isInvalid) { 505 if (EnumConst) 506 EnumConst->setInvalidDecl(); 507 Enum->setInvalidDecl(); 508 } 509 510 if (EnumConst) { 511 EnumConst->setAccess(Enum->getAccess()); 512 Enum->addDecl(EnumConst); 513 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst)); 514 LastEnumConst = EnumConst; 515 } 516 } 517 518 // FIXME: Fixup LBraceLoc and RBraceLoc 519 // FIXME: Empty Scope and AttributeList (required to handle attribute packed). 520 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(), 521 Sema::DeclPtrTy::make(Enum), 522 &Enumerators[0], Enumerators.size(), 523 0, 0); 524 525 return Enum; 526} 527 528Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { 529 assert(false && "EnumConstantDecls can only occur within EnumDecls."); 530 return 0; 531} 532 533namespace { 534 class SortDeclByLocation { 535 SourceManager &SourceMgr; 536 537 public: 538 explicit SortDeclByLocation(SourceManager &SourceMgr) 539 : SourceMgr(SourceMgr) { } 540 541 bool operator()(const Decl *X, const Decl *Y) const { 542 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(), 543 Y->getLocation()); 544 } 545 }; 546} 547 548Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { 549 // Create a local instantiation scope for this class template, which 550 // will contain the instantiations of the template parameters. 551 Sema::LocalInstantiationScope Scope(SemaRef); 552 TemplateParameterList *TempParams = D->getTemplateParameters(); 553 TemplateParameterList *InstParams = SubstTemplateParams(TempParams); 554 if (!InstParams) 555 return NULL; 556 557 CXXRecordDecl *Pattern = D->getTemplatedDecl(); 558 CXXRecordDecl *RecordInst 559 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner, 560 Pattern->getLocation(), Pattern->getIdentifier(), 561 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL, 562 /*DelayTypeCreation=*/true); 563 564 ClassTemplateDecl *Inst 565 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), 566 D->getIdentifier(), InstParams, RecordInst, 0); 567 RecordInst->setDescribedClassTemplate(Inst); 568 if (D->getFriendObjectKind()) 569 Inst->setObjectOfFriendDecl(true); 570 else 571 Inst->setAccess(D->getAccess()); 572 Inst->setInstantiatedFromMemberTemplate(D); 573 574 // Trigger creation of the type for the instantiation. 575 SemaRef.Context.getTypeDeclType(RecordInst); 576 577 // Finish handling of friends. 578 if (Inst->getFriendObjectKind()) { 579 return Inst; 580 } 581 582 Inst->setAccess(D->getAccess()); 583 Owner->addDecl(Inst); 584 585 // First, we sort the partial specializations by location, so 586 // that we instantiate them in the order they were declared. 587 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; 588 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator 589 P = D->getPartialSpecializations().begin(), 590 PEnd = D->getPartialSpecializations().end(); 591 P != PEnd; ++P) 592 PartialSpecs.push_back(&*P); 593 std::sort(PartialSpecs.begin(), PartialSpecs.end(), 594 SortDeclByLocation(SemaRef.SourceMgr)); 595 596 // Instantiate all of the partial specializations of this member class 597 // template. 598 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) 599 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]); 600 601 return Inst; 602} 603 604Decl * 605TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( 606 ClassTemplatePartialSpecializationDecl *D) { 607 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); 608 609 // Lookup the already-instantiated declaration in the instantiation 610 // of the class template and return that. 611 DeclContext::lookup_result Found 612 = Owner->lookup(ClassTemplate->getDeclName()); 613 if (Found.first == Found.second) 614 return 0; 615 616 ClassTemplateDecl *InstClassTemplate 617 = dyn_cast<ClassTemplateDecl>(*Found.first); 618 if (!InstClassTemplate) 619 return 0; 620 621 Decl *DCanon = D->getCanonicalDecl(); 622 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator 623 P = InstClassTemplate->getPartialSpecializations().begin(), 624 PEnd = InstClassTemplate->getPartialSpecializations().end(); 625 P != PEnd; ++P) { 626 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon) 627 return &*P; 628 } 629 630 return 0; 631} 632 633Decl * 634TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 635 // Create a local instantiation scope for this function template, which 636 // will contain the instantiations of the template parameters and then get 637 // merged with the local instantiation scope for the function template 638 // itself. 639 Sema::LocalInstantiationScope Scope(SemaRef); 640 641 TemplateParameterList *TempParams = D->getTemplateParameters(); 642 TemplateParameterList *InstParams = SubstTemplateParams(TempParams); 643 if (!InstParams) 644 return NULL; 645 646 FunctionDecl *Instantiated = 0; 647 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) 648 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, 649 InstParams)); 650 else 651 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( 652 D->getTemplatedDecl(), 653 InstParams)); 654 655 if (!Instantiated) 656 return 0; 657 658 Instantiated->setAccess(D->getAccess()); 659 660 // Link the instantiated function template declaration to the function 661 // template from which it was instantiated. 662 FunctionTemplateDecl *InstTemplate 663 = Instantiated->getDescribedFunctionTemplate(); 664 InstTemplate->setAccess(D->getAccess()); 665 assert(InstTemplate && 666 "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); 667 668 // Link the instantiation back to the pattern *unless* this is a 669 // non-definition friend declaration. 670 if (!InstTemplate->getInstantiatedFromMemberTemplate() && 671 !(InstTemplate->getFriendObjectKind() && 672 !D->getTemplatedDecl()->isThisDeclarationADefinition())) 673 InstTemplate->setInstantiatedFromMemberTemplate(D); 674 675 // Add non-friends into the owner. 676 if (!InstTemplate->getFriendObjectKind()) 677 Owner->addDecl(InstTemplate); 678 return InstTemplate; 679} 680 681Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { 682 CXXRecordDecl *PrevDecl = 0; 683 if (D->isInjectedClassName()) 684 PrevDecl = cast<CXXRecordDecl>(Owner); 685 else if (D->getPreviousDeclaration()) { 686 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getPreviousDeclaration(), 687 TemplateArgs); 688 if (!Prev) return 0; 689 PrevDecl = cast<CXXRecordDecl>(Prev); 690 } 691 692 CXXRecordDecl *Record 693 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, 694 D->getLocation(), D->getIdentifier(), 695 D->getTagKeywordLoc(), PrevDecl); 696 Record->setImplicit(D->isImplicit()); 697 // FIXME: Check against AS_none is an ugly hack to work around the issue that 698 // the tag decls introduced by friend class declarations don't have an access 699 // specifier. Remove once this area of the code gets sorted out. 700 if (D->getAccess() != AS_none) 701 Record->setAccess(D->getAccess()); 702 if (!D->isInjectedClassName()) 703 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); 704 705 // If the original function was part of a friend declaration, 706 // inherit its namespace state. 707 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) 708 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared); 709 710 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion()); 711 712 Owner->addDecl(Record); 713 return Record; 714} 715 716/// Normal class members are of more specific types and therefore 717/// don't make it here. This function serves two purposes: 718/// 1) instantiating function templates 719/// 2) substituting friend declarations 720/// FIXME: preserve function definitions in case #2 721Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, 722 TemplateParameterList *TemplateParams) { 723 // Check whether there is already a function template specialization for 724 // this declaration. 725 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); 726 void *InsertPos = 0; 727 if (FunctionTemplate && !TemplateParams) { 728 llvm::FoldingSetNodeID ID; 729 FunctionTemplateSpecializationInfo::Profile(ID, 730 TemplateArgs.getInnermost().getFlatArgumentList(), 731 TemplateArgs.getInnermost().flat_size(), 732 SemaRef.Context); 733 734 FunctionTemplateSpecializationInfo *Info 735 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, 736 InsertPos); 737 738 // If we already have a function template specialization, return it. 739 if (Info) 740 return Info->Function; 741 } 742 743 bool MergeWithParentScope = (TemplateParams != 0) || 744 !(isa<Decl>(Owner) && 745 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); 746 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); 747 748 llvm::SmallVector<ParmVarDecl *, 4> Params; 749 QualType T = SubstFunctionType(D, Params); 750 if (T.isNull()) 751 return 0; 752 753 // If we're instantiating a local function declaration, put the result 754 // in the owner; otherwise we need to find the instantiated context. 755 DeclContext *DC; 756 if (D->getDeclContext()->isFunctionOrMethod()) 757 DC = Owner; 758 else 759 DC = SemaRef.FindInstantiatedContext(D->getDeclContext(), TemplateArgs); 760 761 FunctionDecl *Function = 762 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(), 763 D->getDeclName(), T, D->getTypeSourceInfo(), 764 D->getStorageClass(), 765 D->isInlineSpecified(), D->hasWrittenPrototype()); 766 Function->setLexicalDeclContext(Owner); 767 768 // Attach the parameters 769 for (unsigned P = 0; P < Params.size(); ++P) 770 Params[P]->setOwningFunction(Function); 771 Function->setParams(SemaRef.Context, Params.data(), Params.size()); 772 773 if (TemplateParams) { 774 // Our resulting instantiation is actually a function template, since we 775 // are substituting only the outer template parameters. For example, given 776 // 777 // template<typename T> 778 // struct X { 779 // template<typename U> friend void f(T, U); 780 // }; 781 // 782 // X<int> x; 783 // 784 // We are instantiating the friend function template "f" within X<int>, 785 // which means substituting int for T, but leaving "f" as a friend function 786 // template. 787 // Build the function template itself. 788 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner, 789 Function->getLocation(), 790 Function->getDeclName(), 791 TemplateParams, Function); 792 Function->setDescribedFunctionTemplate(FunctionTemplate); 793 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); 794 } else if (FunctionTemplate) { 795 // Record this function template specialization. 796 Function->setFunctionTemplateSpecialization(SemaRef.Context, 797 FunctionTemplate, 798 &TemplateArgs.getInnermost(), 799 InsertPos); 800 } 801 802 if (InitFunctionInstantiation(Function, D)) 803 Function->setInvalidDecl(); 804 805 bool Redeclaration = false; 806 bool OverloadableAttrRequired = false; 807 808 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(), 809 Sema::LookupOrdinaryName, Sema::ForRedeclaration); 810 811 if (TemplateParams || !FunctionTemplate) { 812 // Look only into the namespace where the friend would be declared to 813 // find a previous declaration. This is the innermost enclosing namespace, 814 // as described in ActOnFriendFunctionDecl. 815 SemaRef.LookupQualifiedName(Previous, DC); 816 817 // In C++, the previous declaration we find might be a tag type 818 // (class or enum). In this case, the new declaration will hide the 819 // tag type. Note that this does does not apply if we're declaring a 820 // typedef (C++ [dcl.typedef]p4). 821 if (Previous.isSingleTagDecl()) 822 Previous.clear(); 823 } 824 825 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous, 826 false, Redeclaration, 827 /*FIXME:*/OverloadableAttrRequired); 828 829 // If the original function was part of a friend declaration, 830 // inherit its namespace state and add it to the owner. 831 NamedDecl *FromFriendD 832 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D; 833 if (FromFriendD->getFriendObjectKind()) { 834 NamedDecl *ToFriendD = 0; 835 NamedDecl *PrevDecl; 836 if (TemplateParams) { 837 ToFriendD = cast<NamedDecl>(FunctionTemplate); 838 PrevDecl = FunctionTemplate->getPreviousDeclaration(); 839 } else { 840 ToFriendD = Function; 841 PrevDecl = Function->getPreviousDeclaration(); 842 } 843 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL); 844 if (!Owner->isDependentContext() && !PrevDecl) 845 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false); 846 847 if (!TemplateParams) 848 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); 849 } 850 851 return Function; 852} 853 854Decl * 855TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, 856 TemplateParameterList *TemplateParams) { 857 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); 858 void *InsertPos = 0; 859 if (FunctionTemplate && !TemplateParams) { 860 // We are creating a function template specialization from a function 861 // template. Check whether there is already a function template 862 // specialization for this particular set of template arguments. 863 llvm::FoldingSetNodeID ID; 864 FunctionTemplateSpecializationInfo::Profile(ID, 865 TemplateArgs.getInnermost().getFlatArgumentList(), 866 TemplateArgs.getInnermost().flat_size(), 867 SemaRef.Context); 868 869 FunctionTemplateSpecializationInfo *Info 870 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, 871 InsertPos); 872 873 // If we already have a function template specialization, return it. 874 if (Info) 875 return Info->Function; 876 } 877 878 bool MergeWithParentScope = (TemplateParams != 0) || 879 !(isa<Decl>(Owner) && 880 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); 881 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); 882 883 llvm::SmallVector<ParmVarDecl *, 4> Params; 884 QualType T = SubstFunctionType(D, Params); 885 if (T.isNull()) 886 return 0; 887 888 // Build the instantiated method declaration. 889 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 890 CXXMethodDecl *Method = 0; 891 892 DeclarationName Name = D->getDeclName(); 893 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { 894 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); 895 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName( 896 SemaRef.Context.getCanonicalType(ClassTy)); 897 Method = CXXConstructorDecl::Create(SemaRef.Context, Record, 898 Constructor->getLocation(), 899 Name, T, 900 Constructor->getTypeSourceInfo(), 901 Constructor->isExplicit(), 902 Constructor->isInlineSpecified(), false); 903 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { 904 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); 905 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName( 906 SemaRef.Context.getCanonicalType(ClassTy)); 907 Method = CXXDestructorDecl::Create(SemaRef.Context, Record, 908 Destructor->getLocation(), Name, 909 T, Destructor->isInlineSpecified(), false); 910 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { 911 CanQualType ConvTy 912 = SemaRef.Context.getCanonicalType( 913 T->getAs<FunctionType>()->getResultType()); 914 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName( 915 ConvTy); 916 Method = CXXConversionDecl::Create(SemaRef.Context, Record, 917 Conversion->getLocation(), Name, 918 T, Conversion->getTypeSourceInfo(), 919 Conversion->isInlineSpecified(), 920 Conversion->isExplicit()); 921 } else { 922 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), 923 D->getDeclName(), T, D->getTypeSourceInfo(), 924 D->isStatic(), D->isInlineSpecified()); 925 } 926 927 if (TemplateParams) { 928 // Our resulting instantiation is actually a function template, since we 929 // are substituting only the outer template parameters. For example, given 930 // 931 // template<typename T> 932 // struct X { 933 // template<typename U> void f(T, U); 934 // }; 935 // 936 // X<int> x; 937 // 938 // We are instantiating the member template "f" within X<int>, which means 939 // substituting int for T, but leaving "f" as a member function template. 940 // Build the function template itself. 941 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, 942 Method->getLocation(), 943 Method->getDeclName(), 944 TemplateParams, Method); 945 if (D->isOutOfLine()) 946 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); 947 Method->setDescribedFunctionTemplate(FunctionTemplate); 948 } else if (FunctionTemplate) { 949 // Record this function template specialization. 950 Method->setFunctionTemplateSpecialization(SemaRef.Context, 951 FunctionTemplate, 952 &TemplateArgs.getInnermost(), 953 InsertPos); 954 } else { 955 // Record that this is an instantiation of a member function. 956 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); 957 } 958 959 // If we are instantiating a member function defined 960 // out-of-line, the instantiation will have the same lexical 961 // context (which will be a namespace scope) as the template. 962 if (D->isOutOfLine()) 963 Method->setLexicalDeclContext(D->getLexicalDeclContext()); 964 965 // Attach the parameters 966 for (unsigned P = 0; P < Params.size(); ++P) 967 Params[P]->setOwningFunction(Method); 968 Method->setParams(SemaRef.Context, Params.data(), Params.size()); 969 970 if (InitMethodInstantiation(Method, D)) 971 Method->setInvalidDecl(); 972 973 LookupResult Previous(SemaRef, Name, SourceLocation(), 974 Sema::LookupOrdinaryName, Sema::ForRedeclaration); 975 976 if (!FunctionTemplate || TemplateParams) { 977 SemaRef.LookupQualifiedName(Previous, Owner); 978 979 // In C++, the previous declaration we find might be a tag type 980 // (class or enum). In this case, the new declaration will hide the 981 // tag type. Note that this does does not apply if we're declaring a 982 // typedef (C++ [dcl.typedef]p4). 983 if (Previous.isSingleTagDecl()) 984 Previous.clear(); 985 } 986 987 bool Redeclaration = false; 988 bool OverloadableAttrRequired = false; 989 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration, 990 /*FIXME:*/OverloadableAttrRequired); 991 992 if (D->isPure()) 993 SemaRef.CheckPureMethod(Method, SourceRange()); 994 995 Method->setAccess(D->getAccess()); 996 997 if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) && 998 !Method->getFriendObjectKind()) 999 Owner->addDecl(Method); 1000 1001 return Method; 1002} 1003 1004Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 1005 return VisitCXXMethodDecl(D); 1006} 1007 1008Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 1009 return VisitCXXMethodDecl(D); 1010} 1011 1012Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { 1013 return VisitCXXMethodDecl(D); 1014} 1015 1016ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { 1017 QualType T; 1018 TypeSourceInfo *DI = D->getTypeSourceInfo(); 1019 if (DI) { 1020 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(), 1021 D->getDeclName()); 1022 if (DI) T = DI->getType(); 1023 } else { 1024 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(), 1025 D->getDeclName()); 1026 DI = 0; 1027 } 1028 1029 if (T.isNull()) 1030 return 0; 1031 1032 T = SemaRef.adjustParameterType(T); 1033 1034 // Allocate the parameter 1035 ParmVarDecl *Param 1036 = ParmVarDecl::Create(SemaRef.Context, 1037 SemaRef.Context.getTranslationUnitDecl(), 1038 D->getLocation(), 1039 D->getIdentifier(), T, DI, D->getStorageClass(), 0); 1040 1041 // Mark the default argument as being uninstantiated. 1042 if (D->hasUninstantiatedDefaultArg()) 1043 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg()); 1044 else if (Expr *Arg = D->getDefaultArg()) 1045 Param->setUninstantiatedDefaultArg(Arg); 1046 1047 // Note: we don't try to instantiate function parameters until after 1048 // we've instantiated the function's type. Therefore, we don't have 1049 // to check for 'void' parameter types here. 1050 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); 1051 return Param; 1052} 1053 1054Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( 1055 TemplateTypeParmDecl *D) { 1056 // TODO: don't always clone when decls are refcounted. 1057 const Type* T = D->getTypeForDecl(); 1058 assert(T->isTemplateTypeParmType()); 1059 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>(); 1060 1061 TemplateTypeParmDecl *Inst = 1062 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), 1063 TTPT->getDepth() - 1, TTPT->getIndex(), 1064 TTPT->getName(), 1065 D->wasDeclaredWithTypename(), 1066 D->isParameterPack()); 1067 1068 if (D->hasDefaultArgument()) 1069 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false); 1070 1071 // Introduce this template parameter's instantiation into the instantiation 1072 // scope. 1073 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); 1074 1075 return Inst; 1076} 1077 1078Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( 1079 NonTypeTemplateParmDecl *D) { 1080 // Substitute into the type of the non-type template parameter. 1081 QualType T; 1082 TypeSourceInfo *DI = D->getTypeSourceInfo(); 1083 if (DI) { 1084 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(), 1085 D->getDeclName()); 1086 if (DI) T = DI->getType(); 1087 } else { 1088 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(), 1089 D->getDeclName()); 1090 DI = 0; 1091 } 1092 if (T.isNull()) 1093 return 0; 1094 1095 // Check that this type is acceptable for a non-type template parameter. 1096 bool Invalid = false; 1097 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation()); 1098 if (T.isNull()) { 1099 T = SemaRef.Context.IntTy; 1100 Invalid = true; 1101 } 1102 1103 NonTypeTemplateParmDecl *Param 1104 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), 1105 D->getDepth() - 1, D->getPosition(), 1106 D->getIdentifier(), T, DI); 1107 if (Invalid) 1108 Param->setInvalidDecl(); 1109 1110 Param->setDefaultArgument(D->getDefaultArgument()); 1111 1112 // Introduce this template parameter's instantiation into the instantiation 1113 // scope. 1114 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); 1115 return Param; 1116} 1117 1118Decl * 1119TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( 1120 TemplateTemplateParmDecl *D) { 1121 // Instantiate the template parameter list of the template template parameter. 1122 TemplateParameterList *TempParams = D->getTemplateParameters(); 1123 TemplateParameterList *InstParams; 1124 { 1125 // Perform the actual substitution of template parameters within a new, 1126 // local instantiation scope. 1127 Sema::LocalInstantiationScope Scope(SemaRef); 1128 InstParams = SubstTemplateParams(TempParams); 1129 if (!InstParams) 1130 return NULL; 1131 } 1132 1133 // Build the template template parameter. 1134 TemplateTemplateParmDecl *Param 1135 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), 1136 D->getDepth() - 1, D->getPosition(), 1137 D->getIdentifier(), InstParams); 1138 Param->setDefaultArgument(D->getDefaultArgument()); 1139 1140 // Introduce this template parameter's instantiation into the instantiation 1141 // scope. 1142 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); 1143 1144 return Param; 1145} 1146 1147Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 1148 // Using directives are never dependent, so they require no explicit 1149 1150 UsingDirectiveDecl *Inst 1151 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), 1152 D->getNamespaceKeyLocation(), 1153 D->getQualifierRange(), D->getQualifier(), 1154 D->getIdentLocation(), 1155 D->getNominatedNamespace(), 1156 D->getCommonAncestor()); 1157 Owner->addDecl(Inst); 1158 return Inst; 1159} 1160 1161Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { 1162 // The nested name specifier is non-dependent, so no transformation 1163 // is required. 1164 1165 // We only need to do redeclaration lookups if we're in a class 1166 // scope (in fact, it's not really even possible in non-class 1167 // scopes). 1168 bool CheckRedeclaration = Owner->isRecord(); 1169 1170 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(), 1171 Sema::LookupUsingDeclName, Sema::ForRedeclaration); 1172 1173 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, 1174 D->getLocation(), 1175 D->getNestedNameRange(), 1176 D->getUsingLocation(), 1177 D->getTargetNestedNameDecl(), 1178 D->getDeclName(), 1179 D->isTypeName()); 1180 1181 CXXScopeSpec SS; 1182 SS.setScopeRep(D->getTargetNestedNameDecl()); 1183 SS.setRange(D->getNestedNameRange()); 1184 1185 if (CheckRedeclaration) { 1186 Prev.setHideTags(false); 1187 SemaRef.LookupQualifiedName(Prev, Owner); 1188 1189 // Check for invalid redeclarations. 1190 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(), 1191 D->isTypeName(), SS, 1192 D->getLocation(), Prev)) 1193 NewUD->setInvalidDecl(); 1194 1195 } 1196 1197 if (!NewUD->isInvalidDecl() && 1198 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS, 1199 D->getLocation())) 1200 NewUD->setInvalidDecl(); 1201 1202 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); 1203 NewUD->setAccess(D->getAccess()); 1204 Owner->addDecl(NewUD); 1205 1206 // Don't process the shadow decls for an invalid decl. 1207 if (NewUD->isInvalidDecl()) 1208 return NewUD; 1209 1210 bool isFunctionScope = Owner->isFunctionOrMethod(); 1211 1212 // Process the shadow decls. 1213 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end(); 1214 I != E; ++I) { 1215 UsingShadowDecl *Shadow = *I; 1216 NamedDecl *InstTarget = 1217 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(), 1218 TemplateArgs)); 1219 1220 if (CheckRedeclaration && 1221 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev)) 1222 continue; 1223 1224 UsingShadowDecl *InstShadow 1225 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget); 1226 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); 1227 1228 if (isFunctionScope) 1229 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); 1230 } 1231 1232 return NewUD; 1233} 1234 1235Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { 1236 // Ignore these; we handle them in bulk when processing the UsingDecl. 1237 return 0; 1238} 1239 1240Decl * TemplateDeclInstantiator 1241 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 1242 NestedNameSpecifier *NNS = 1243 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), 1244 D->getTargetNestedNameRange(), 1245 TemplateArgs); 1246 if (!NNS) 1247 return 0; 1248 1249 CXXScopeSpec SS; 1250 SS.setRange(D->getTargetNestedNameRange()); 1251 SS.setScopeRep(NNS); 1252 1253 NamedDecl *UD = 1254 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), 1255 D->getUsingLoc(), SS, D->getLocation(), 1256 D->getDeclName(), 0, 1257 /*instantiation*/ true, 1258 /*typename*/ true, D->getTypenameLoc()); 1259 if (UD) 1260 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); 1261 1262 return UD; 1263} 1264 1265Decl * TemplateDeclInstantiator 1266 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1267 NestedNameSpecifier *NNS = 1268 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), 1269 D->getTargetNestedNameRange(), 1270 TemplateArgs); 1271 if (!NNS) 1272 return 0; 1273 1274 CXXScopeSpec SS; 1275 SS.setRange(D->getTargetNestedNameRange()); 1276 SS.setScopeRep(NNS); 1277 1278 NamedDecl *UD = 1279 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), 1280 D->getUsingLoc(), SS, D->getLocation(), 1281 D->getDeclName(), 0, 1282 /*instantiation*/ true, 1283 /*typename*/ false, SourceLocation()); 1284 if (UD) 1285 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); 1286 1287 return UD; 1288} 1289 1290Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, 1291 const MultiLevelTemplateArgumentList &TemplateArgs) { 1292 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); 1293 return Instantiator.Visit(D); 1294} 1295 1296/// \brief Instantiates a nested template parameter list in the current 1297/// instantiation context. 1298/// 1299/// \param L The parameter list to instantiate 1300/// 1301/// \returns NULL if there was an error 1302TemplateParameterList * 1303TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { 1304 // Get errors for all the parameters before bailing out. 1305 bool Invalid = false; 1306 1307 unsigned N = L->size(); 1308 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector; 1309 ParamVector Params; 1310 Params.reserve(N); 1311 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); 1312 PI != PE; ++PI) { 1313 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI)); 1314 Params.push_back(D); 1315 Invalid = Invalid || !D || D->isInvalidDecl(); 1316 } 1317 1318 // Clean up if we had an error. 1319 if (Invalid) { 1320 for (ParamVector::iterator PI = Params.begin(), PE = Params.end(); 1321 PI != PE; ++PI) 1322 if (*PI) 1323 (*PI)->Destroy(SemaRef.Context); 1324 return NULL; 1325 } 1326 1327 TemplateParameterList *InstL 1328 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), 1329 L->getLAngleLoc(), &Params.front(), N, 1330 L->getRAngleLoc()); 1331 return InstL; 1332} 1333 1334/// \brief Instantiate the declaration of a class template partial 1335/// specialization. 1336/// 1337/// \param ClassTemplate the (instantiated) class template that is partially 1338// specialized by the instantiation of \p PartialSpec. 1339/// 1340/// \param PartialSpec the (uninstantiated) class template partial 1341/// specialization that we are instantiating. 1342/// 1343/// \returns true if there was an error, false otherwise. 1344bool 1345TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( 1346 ClassTemplateDecl *ClassTemplate, 1347 ClassTemplatePartialSpecializationDecl *PartialSpec) { 1348 // Create a local instantiation scope for this class template partial 1349 // specialization, which will contain the instantiations of the template 1350 // parameters. 1351 Sema::LocalInstantiationScope Scope(SemaRef); 1352 1353 // Substitute into the template parameters of the class template partial 1354 // specialization. 1355 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); 1356 TemplateParameterList *InstParams = SubstTemplateParams(TempParams); 1357 if (!InstParams) 1358 return true; 1359 1360 // Substitute into the template arguments of the class template partial 1361 // specialization. 1362 const TemplateArgumentLoc *PartialSpecTemplateArgs 1363 = PartialSpec->getTemplateArgsAsWritten(); 1364 unsigned N = PartialSpec->getNumTemplateArgsAsWritten(); 1365 1366 TemplateArgumentListInfo InstTemplateArgs; // no angle locations 1367 for (unsigned I = 0; I != N; ++I) { 1368 TemplateArgumentLoc Loc; 1369 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs)) 1370 return true; 1371 InstTemplateArgs.addArgument(Loc); 1372 } 1373 1374 1375 // Check that the template argument list is well-formed for this 1376 // class template. 1377 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), 1378 InstTemplateArgs.size()); 1379 if (SemaRef.CheckTemplateArgumentList(ClassTemplate, 1380 PartialSpec->getLocation(), 1381 InstTemplateArgs, 1382 false, 1383 Converted)) 1384 return true; 1385 1386 // Figure out where to insert this class template partial specialization 1387 // in the member template's set of class template partial specializations. 1388 llvm::FoldingSetNodeID ID; 1389 ClassTemplatePartialSpecializationDecl::Profile(ID, 1390 Converted.getFlatArguments(), 1391 Converted.flatSize(), 1392 SemaRef.Context); 1393 void *InsertPos = 0; 1394 ClassTemplateSpecializationDecl *PrevDecl 1395 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, 1396 InsertPos); 1397 1398 // Build the canonical type that describes the converted template 1399 // arguments of the class template partial specialization. 1400 QualType CanonType 1401 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), 1402 Converted.getFlatArguments(), 1403 Converted.flatSize()); 1404 1405 // Build the fully-sugared type for this class template 1406 // specialization as the user wrote in the specialization 1407 // itself. This means that we'll pretty-print the type retrieved 1408 // from the specialization's declaration the way that the user 1409 // actually wrote the specialization, rather than formatting the 1410 // name based on the "canonical" representation used to store the 1411 // template arguments in the specialization. 1412 QualType WrittenTy 1413 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), 1414 InstTemplateArgs, 1415 CanonType); 1416 1417 if (PrevDecl) { 1418 // We've already seen a partial specialization with the same template 1419 // parameters and template arguments. This can happen, for example, when 1420 // substituting the outer template arguments ends up causing two 1421 // class template partial specializations of a member class template 1422 // to have identical forms, e.g., 1423 // 1424 // template<typename T, typename U> 1425 // struct Outer { 1426 // template<typename X, typename Y> struct Inner; 1427 // template<typename Y> struct Inner<T, Y>; 1428 // template<typename Y> struct Inner<U, Y>; 1429 // }; 1430 // 1431 // Outer<int, int> outer; // error: the partial specializations of Inner 1432 // // have the same signature. 1433 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) 1434 << WrittenTy; 1435 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) 1436 << SemaRef.Context.getTypeDeclType(PrevDecl); 1437 return true; 1438 } 1439 1440 1441 // Create the class template partial specialization declaration. 1442 ClassTemplatePartialSpecializationDecl *InstPartialSpec 1443 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner, 1444 PartialSpec->getLocation(), 1445 InstParams, 1446 ClassTemplate, 1447 Converted, 1448 InstTemplateArgs, 1449 0); 1450 InstPartialSpec->setInstantiatedFromMember(PartialSpec); 1451 InstPartialSpec->setTypeAsWritten(WrittenTy); 1452 1453 // Add this partial specialization to the set of class template partial 1454 // specializations. 1455 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec, 1456 InsertPos); 1457 return false; 1458} 1459 1460/// \brief Does substitution on the type of the given function, including 1461/// all of the function parameters. 1462/// 1463/// \param D The function whose type will be the basis of the substitution 1464/// 1465/// \param Params the instantiated parameter declarations 1466 1467/// \returns the instantiated function's type if successful, a NULL 1468/// type if there was an error. 1469QualType 1470TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, 1471 llvm::SmallVectorImpl<ParmVarDecl *> &Params) { 1472 bool InvalidDecl = false; 1473 1474 // Substitute all of the function's formal parameter types. 1475 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs); 1476 llvm::SmallVector<QualType, 4> ParamTys; 1477 for (FunctionDecl::param_iterator P = D->param_begin(), 1478 PEnd = D->param_end(); 1479 P != PEnd; ++P) { 1480 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { 1481 if (PInst->getType()->isVoidType()) { 1482 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); 1483 PInst->setInvalidDecl(); 1484 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), 1485 PInst->getType(), 1486 diag::err_abstract_type_in_decl, 1487 Sema::AbstractParamType)) 1488 PInst->setInvalidDecl(); 1489 1490 Params.push_back(PInst); 1491 ParamTys.push_back(PInst->getType()); 1492 1493 if (PInst->isInvalidDecl()) 1494 InvalidDecl = true; 1495 } else 1496 InvalidDecl = true; 1497 } 1498 1499 // FIXME: Deallocate dead declarations. 1500 if (InvalidDecl) 1501 return QualType(); 1502 1503 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>(); 1504 assert(Proto && "Missing prototype?"); 1505 QualType ResultType 1506 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs, 1507 D->getLocation(), D->getDeclName()); 1508 if (ResultType.isNull()) 1509 return QualType(); 1510 1511 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(), 1512 Proto->isVariadic(), Proto->getTypeQuals(), 1513 D->getLocation(), D->getDeclName()); 1514} 1515 1516/// \brief Initializes the common fields of an instantiation function 1517/// declaration (New) from the corresponding fields of its template (Tmpl). 1518/// 1519/// \returns true if there was an error 1520bool 1521TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, 1522 FunctionDecl *Tmpl) { 1523 if (Tmpl->isDeleted()) 1524 New->setDeleted(); 1525 1526 // If we are performing substituting explicitly-specified template arguments 1527 // or deduced template arguments into a function template and we reach this 1528 // point, we are now past the point where SFINAE applies and have committed 1529 // to keeping the new function template specialization. We therefore 1530 // convert the active template instantiation for the function template 1531 // into a template instantiation for this specific function template 1532 // specialization, which is not a SFINAE context, so that we diagnose any 1533 // further errors in the declaration itself. 1534 typedef Sema::ActiveTemplateInstantiation ActiveInstType; 1535 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); 1536 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || 1537 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { 1538 if (FunctionTemplateDecl *FunTmpl 1539 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) { 1540 assert(FunTmpl->getTemplatedDecl() == Tmpl && 1541 "Deduction from the wrong function template?"); 1542 (void) FunTmpl; 1543 ActiveInst.Kind = ActiveInstType::TemplateInstantiation; 1544 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New); 1545 --SemaRef.NonInstantiationEntries; 1546 } 1547 } 1548 1549 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); 1550 assert(Proto && "Function template without prototype?"); 1551 1552 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() || 1553 Proto->getNoReturnAttr()) { 1554 // The function has an exception specification or a "noreturn" 1555 // attribute. Substitute into each of the exception types. 1556 llvm::SmallVector<QualType, 4> Exceptions; 1557 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) { 1558 // FIXME: Poor location information! 1559 QualType T 1560 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs, 1561 New->getLocation(), New->getDeclName()); 1562 if (T.isNull() || 1563 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation())) 1564 continue; 1565 1566 Exceptions.push_back(T); 1567 } 1568 1569 // Rebuild the function type 1570 1571 const FunctionProtoType *NewProto 1572 = New->getType()->getAs<FunctionProtoType>(); 1573 assert(NewProto && "Template instantiation without function prototype?"); 1574 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(), 1575 NewProto->arg_type_begin(), 1576 NewProto->getNumArgs(), 1577 NewProto->isVariadic(), 1578 NewProto->getTypeQuals(), 1579 Proto->hasExceptionSpec(), 1580 Proto->hasAnyExceptionSpec(), 1581 Exceptions.size(), 1582 Exceptions.data(), 1583 Proto->getNoReturnAttr(), 1584 Proto->getCallConv())); 1585 } 1586 1587 return false; 1588} 1589 1590/// \brief Initializes common fields of an instantiated method 1591/// declaration (New) from the corresponding fields of its template 1592/// (Tmpl). 1593/// 1594/// \returns true if there was an error 1595bool 1596TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, 1597 CXXMethodDecl *Tmpl) { 1598 if (InitFunctionInstantiation(New, Tmpl)) 1599 return true; 1600 1601 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 1602 New->setAccess(Tmpl->getAccess()); 1603 if (Tmpl->isVirtualAsWritten()) 1604 Record->setMethodAsVirtual(New); 1605 1606 // FIXME: attributes 1607 // FIXME: New needs a pointer to Tmpl 1608 return false; 1609} 1610 1611/// \brief Instantiate the definition of the given function from its 1612/// template. 1613/// 1614/// \param PointOfInstantiation the point at which the instantiation was 1615/// required. Note that this is not precisely a "point of instantiation" 1616/// for the function, but it's close. 1617/// 1618/// \param Function the already-instantiated declaration of a 1619/// function template specialization or member function of a class template 1620/// specialization. 1621/// 1622/// \param Recursive if true, recursively instantiates any functions that 1623/// are required by this instantiation. 1624/// 1625/// \param DefinitionRequired if true, then we are performing an explicit 1626/// instantiation where the body of the function is required. Complain if 1627/// there is no such body. 1628void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, 1629 FunctionDecl *Function, 1630 bool Recursive, 1631 bool DefinitionRequired) { 1632 if (Function->isInvalidDecl()) 1633 return; 1634 1635 assert(!Function->getBody() && "Already instantiated!"); 1636 1637 // Never instantiate an explicit specialization. 1638 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) 1639 return; 1640 1641 // Find the function body that we'll be substituting. 1642 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); 1643 Stmt *Pattern = 0; 1644 if (PatternDecl) 1645 Pattern = PatternDecl->getBody(PatternDecl); 1646 1647 if (!Pattern) { 1648 if (DefinitionRequired) { 1649 if (Function->getPrimaryTemplate()) 1650 Diag(PointOfInstantiation, 1651 diag::err_explicit_instantiation_undefined_func_template) 1652 << Function->getPrimaryTemplate(); 1653 else 1654 Diag(PointOfInstantiation, 1655 diag::err_explicit_instantiation_undefined_member) 1656 << 1 << Function->getDeclName() << Function->getDeclContext(); 1657 1658 if (PatternDecl) 1659 Diag(PatternDecl->getLocation(), 1660 diag::note_explicit_instantiation_here); 1661 } 1662 1663 return; 1664 } 1665 1666 // C++0x [temp.explicit]p9: 1667 // Except for inline functions, other explicit instantiation declarations 1668 // have the effect of suppressing the implicit instantiation of the entity 1669 // to which they refer. 1670 if (Function->getTemplateSpecializationKind() 1671 == TSK_ExplicitInstantiationDeclaration && 1672 !PatternDecl->isInlined()) 1673 return; 1674 1675 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); 1676 if (Inst) 1677 return; 1678 1679 // If we're performing recursive template instantiation, create our own 1680 // queue of pending implicit instantiations that we will instantiate later, 1681 // while we're still within our own instantiation context. 1682 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; 1683 if (Recursive) 1684 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 1685 1686 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function)); 1687 1688 // Introduce a new scope where local variable instantiations will be 1689 // recorded, unless we're actually a member function within a local 1690 // class, in which case we need to merge our results with the parent 1691 // scope (of the enclosing function). 1692 bool MergeWithParentScope = false; 1693 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) 1694 MergeWithParentScope = Rec->isLocalClass(); 1695 1696 LocalInstantiationScope Scope(*this, MergeWithParentScope); 1697 1698 // Introduce the instantiated function parameters into the local 1699 // instantiation scope. 1700 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) 1701 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I), 1702 Function->getParamDecl(I)); 1703 1704 // Enter the scope of this instantiation. We don't use 1705 // PushDeclContext because we don't have a scope. 1706 DeclContext *PreviousContext = CurContext; 1707 CurContext = Function; 1708 1709 MultiLevelTemplateArgumentList TemplateArgs = 1710 getTemplateInstantiationArgs(Function); 1711 1712 // If this is a constructor, instantiate the member initializers. 1713 if (const CXXConstructorDecl *Ctor = 1714 dyn_cast<CXXConstructorDecl>(PatternDecl)) { 1715 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, 1716 TemplateArgs); 1717 } 1718 1719 // Instantiate the function body. 1720 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs); 1721 1722 if (Body.isInvalid()) 1723 Function->setInvalidDecl(); 1724 1725 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body), 1726 /*IsInstantiation=*/true); 1727 1728 CurContext = PreviousContext; 1729 1730 DeclGroupRef DG(Function); 1731 Consumer.HandleTopLevelDecl(DG); 1732 1733 // This class may have local implicit instantiations that need to be 1734 // instantiation within this scope. 1735 PerformPendingImplicitInstantiations(/*LocalOnly=*/true); 1736 Scope.Exit(); 1737 1738 if (Recursive) { 1739 // Instantiate any pending implicit instantiations found during the 1740 // instantiation of this template. 1741 PerformPendingImplicitInstantiations(); 1742 1743 // Restore the set of pending implicit instantiations. 1744 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 1745 } 1746} 1747 1748/// \brief Instantiate the definition of the given variable from its 1749/// template. 1750/// 1751/// \param PointOfInstantiation the point at which the instantiation was 1752/// required. Note that this is not precisely a "point of instantiation" 1753/// for the function, but it's close. 1754/// 1755/// \param Var the already-instantiated declaration of a static member 1756/// variable of a class template specialization. 1757/// 1758/// \param Recursive if true, recursively instantiates any functions that 1759/// are required by this instantiation. 1760/// 1761/// \param DefinitionRequired if true, then we are performing an explicit 1762/// instantiation where an out-of-line definition of the member variable 1763/// is required. Complain if there is no such definition. 1764void Sema::InstantiateStaticDataMemberDefinition( 1765 SourceLocation PointOfInstantiation, 1766 VarDecl *Var, 1767 bool Recursive, 1768 bool DefinitionRequired) { 1769 if (Var->isInvalidDecl()) 1770 return; 1771 1772 // Find the out-of-line definition of this static data member. 1773 VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); 1774 assert(Def && "This data member was not instantiated from a template?"); 1775 assert(Def->isStaticDataMember() && "Not a static data member?"); 1776 Def = Def->getOutOfLineDefinition(); 1777 1778 if (!Def) { 1779 // We did not find an out-of-line definition of this static data member, 1780 // so we won't perform any instantiation. Rather, we rely on the user to 1781 // instantiate this definition (or provide a specialization for it) in 1782 // another translation unit. 1783 if (DefinitionRequired) { 1784 Def = Var->getInstantiatedFromStaticDataMember(); 1785 Diag(PointOfInstantiation, 1786 diag::err_explicit_instantiation_undefined_member) 1787 << 2 << Var->getDeclName() << Var->getDeclContext(); 1788 Diag(Def->getLocation(), diag::note_explicit_instantiation_here); 1789 } 1790 1791 return; 1792 } 1793 1794 // Never instantiate an explicit specialization. 1795 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) 1796 return; 1797 1798 // C++0x [temp.explicit]p9: 1799 // Except for inline functions, other explicit instantiation declarations 1800 // have the effect of suppressing the implicit instantiation of the entity 1801 // to which they refer. 1802 if (Var->getTemplateSpecializationKind() 1803 == TSK_ExplicitInstantiationDeclaration) 1804 return; 1805 1806 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); 1807 if (Inst) 1808 return; 1809 1810 // If we're performing recursive template instantiation, create our own 1811 // queue of pending implicit instantiations that we will instantiate later, 1812 // while we're still within our own instantiation context. 1813 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; 1814 if (Recursive) 1815 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 1816 1817 // Enter the scope of this instantiation. We don't use 1818 // PushDeclContext because we don't have a scope. 1819 DeclContext *PreviousContext = CurContext; 1820 CurContext = Var->getDeclContext(); 1821 1822 VarDecl *OldVar = Var; 1823 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), 1824 getTemplateInstantiationArgs(Var))); 1825 CurContext = PreviousContext; 1826 1827 if (Var) { 1828 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo(); 1829 assert(MSInfo && "Missing member specialization information?"); 1830 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(), 1831 MSInfo->getPointOfInstantiation()); 1832 DeclGroupRef DG(Var); 1833 Consumer.HandleTopLevelDecl(DG); 1834 } 1835 1836 if (Recursive) { 1837 // Instantiate any pending implicit instantiations found during the 1838 // instantiation of this template. 1839 PerformPendingImplicitInstantiations(); 1840 1841 // Restore the set of pending implicit instantiations. 1842 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 1843 } 1844} 1845 1846void 1847Sema::InstantiateMemInitializers(CXXConstructorDecl *New, 1848 const CXXConstructorDecl *Tmpl, 1849 const MultiLevelTemplateArgumentList &TemplateArgs) { 1850 1851 llvm::SmallVector<MemInitTy*, 4> NewInits; 1852 bool AnyErrors = false; 1853 1854 // Instantiate all the initializers. 1855 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), 1856 InitsEnd = Tmpl->init_end(); 1857 Inits != InitsEnd; ++Inits) { 1858 CXXBaseOrMemberInitializer *Init = *Inits; 1859 1860 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this); 1861 llvm::SmallVector<SourceLocation, 4> CommaLocs; 1862 1863 // Instantiate all the arguments. 1864 Expr *InitE = Init->getInit(); 1865 if (!InitE) { 1866 // Nothing to instantiate; 1867 } else if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(InitE)) { 1868 if (InstantiateInitializationArguments(*this, ParenList->getExprs(), 1869 ParenList->getNumExprs(), 1870 TemplateArgs, CommaLocs, 1871 NewArgs)) { 1872 AnyErrors = true; 1873 continue; 1874 } 1875 } else { 1876 OwningExprResult InitArg = SubstExpr(InitE, TemplateArgs); 1877 if (InitArg.isInvalid()) { 1878 AnyErrors = true; 1879 continue; 1880 } 1881 1882 NewArgs.push_back(InitArg.release()); 1883 } 1884 1885 MemInitResult NewInit; 1886 if (Init->isBaseInitializer()) { 1887 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(), 1888 TemplateArgs, 1889 Init->getSourceLocation(), 1890 New->getDeclName()); 1891 if (!BaseTInfo) { 1892 AnyErrors = true; 1893 New->setInvalidDecl(); 1894 continue; 1895 } 1896 1897 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo, 1898 (Expr **)NewArgs.data(), 1899 NewArgs.size(), 1900 Init->getLParenLoc(), 1901 Init->getRParenLoc(), 1902 New->getParent()); 1903 } else if (Init->isMemberInitializer()) { 1904 FieldDecl *Member; 1905 1906 // Is this an anonymous union? 1907 if (FieldDecl *UnionInit = Init->getAnonUnionMember()) 1908 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs)); 1909 else 1910 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(), 1911 TemplateArgs)); 1912 1913 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(), 1914 NewArgs.size(), 1915 Init->getSourceLocation(), 1916 Init->getLParenLoc(), 1917 Init->getRParenLoc()); 1918 } 1919 1920 if (NewInit.isInvalid()) { 1921 AnyErrors = true; 1922 New->setInvalidDecl(); 1923 } else { 1924 // FIXME: It would be nice if ASTOwningVector had a release function. 1925 NewArgs.take(); 1926 1927 NewInits.push_back((MemInitTy *)NewInit.get()); 1928 } 1929 } 1930 1931 // Assign all the initializers to the new constructor. 1932 ActOnMemInitializers(DeclPtrTy::make(New), 1933 /*FIXME: ColonLoc */ 1934 SourceLocation(), 1935 NewInits.data(), NewInits.size(), 1936 AnyErrors); 1937} 1938 1939// TODO: this could be templated if the various decl types used the 1940// same method name. 1941static bool isInstantiationOf(ClassTemplateDecl *Pattern, 1942 ClassTemplateDecl *Instance) { 1943 Pattern = Pattern->getCanonicalDecl(); 1944 1945 do { 1946 Instance = Instance->getCanonicalDecl(); 1947 if (Pattern == Instance) return true; 1948 Instance = Instance->getInstantiatedFromMemberTemplate(); 1949 } while (Instance); 1950 1951 return false; 1952} 1953 1954static bool isInstantiationOf(FunctionTemplateDecl *Pattern, 1955 FunctionTemplateDecl *Instance) { 1956 Pattern = Pattern->getCanonicalDecl(); 1957 1958 do { 1959 Instance = Instance->getCanonicalDecl(); 1960 if (Pattern == Instance) return true; 1961 Instance = Instance->getInstantiatedFromMemberTemplate(); 1962 } while (Instance); 1963 1964 return false; 1965} 1966 1967static bool 1968isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, 1969 ClassTemplatePartialSpecializationDecl *Instance) { 1970 Pattern 1971 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); 1972 do { 1973 Instance = cast<ClassTemplatePartialSpecializationDecl>( 1974 Instance->getCanonicalDecl()); 1975 if (Pattern == Instance) 1976 return true; 1977 Instance = Instance->getInstantiatedFromMember(); 1978 } while (Instance); 1979 1980 return false; 1981} 1982 1983static bool isInstantiationOf(CXXRecordDecl *Pattern, 1984 CXXRecordDecl *Instance) { 1985 Pattern = Pattern->getCanonicalDecl(); 1986 1987 do { 1988 Instance = Instance->getCanonicalDecl(); 1989 if (Pattern == Instance) return true; 1990 Instance = Instance->getInstantiatedFromMemberClass(); 1991 } while (Instance); 1992 1993 return false; 1994} 1995 1996static bool isInstantiationOf(FunctionDecl *Pattern, 1997 FunctionDecl *Instance) { 1998 Pattern = Pattern->getCanonicalDecl(); 1999 2000 do { 2001 Instance = Instance->getCanonicalDecl(); 2002 if (Pattern == Instance) return true; 2003 Instance = Instance->getInstantiatedFromMemberFunction(); 2004 } while (Instance); 2005 2006 return false; 2007} 2008 2009static bool isInstantiationOf(EnumDecl *Pattern, 2010 EnumDecl *Instance) { 2011 Pattern = Pattern->getCanonicalDecl(); 2012 2013 do { 2014 Instance = Instance->getCanonicalDecl(); 2015 if (Pattern == Instance) return true; 2016 Instance = Instance->getInstantiatedFromMemberEnum(); 2017 } while (Instance); 2018 2019 return false; 2020} 2021 2022static bool isInstantiationOf(UsingShadowDecl *Pattern, 2023 UsingShadowDecl *Instance, 2024 ASTContext &C) { 2025 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern; 2026} 2027 2028static bool isInstantiationOf(UsingDecl *Pattern, 2029 UsingDecl *Instance, 2030 ASTContext &C) { 2031 return C.getInstantiatedFromUsingDecl(Instance) == Pattern; 2032} 2033 2034static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern, 2035 UsingDecl *Instance, 2036 ASTContext &C) { 2037 return C.getInstantiatedFromUsingDecl(Instance) == Pattern; 2038} 2039 2040static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern, 2041 UsingDecl *Instance, 2042 ASTContext &C) { 2043 return C.getInstantiatedFromUsingDecl(Instance) == Pattern; 2044} 2045 2046static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, 2047 VarDecl *Instance) { 2048 assert(Instance->isStaticDataMember()); 2049 2050 Pattern = Pattern->getCanonicalDecl(); 2051 2052 do { 2053 Instance = Instance->getCanonicalDecl(); 2054 if (Pattern == Instance) return true; 2055 Instance = Instance->getInstantiatedFromStaticDataMember(); 2056 } while (Instance); 2057 2058 return false; 2059} 2060 2061// Other is the prospective instantiation 2062// D is the prospective pattern 2063static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { 2064 if (D->getKind() != Other->getKind()) { 2065 if (UnresolvedUsingTypenameDecl *UUD 2066 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { 2067 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { 2068 return isInstantiationOf(UUD, UD, Ctx); 2069 } 2070 } 2071 2072 if (UnresolvedUsingValueDecl *UUD 2073 = dyn_cast<UnresolvedUsingValueDecl>(D)) { 2074 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { 2075 return isInstantiationOf(UUD, UD, Ctx); 2076 } 2077 } 2078 2079 return false; 2080 } 2081 2082 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) 2083 return isInstantiationOf(cast<CXXRecordDecl>(D), Record); 2084 2085 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) 2086 return isInstantiationOf(cast<FunctionDecl>(D), Function); 2087 2088 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) 2089 return isInstantiationOf(cast<EnumDecl>(D), Enum); 2090 2091 if (VarDecl *Var = dyn_cast<VarDecl>(Other)) 2092 if (Var->isStaticDataMember()) 2093 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); 2094 2095 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) 2096 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); 2097 2098 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other)) 2099 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); 2100 2101 if (ClassTemplatePartialSpecializationDecl *PartialSpec 2102 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) 2103 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), 2104 PartialSpec); 2105 2106 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { 2107 if (!Field->getDeclName()) { 2108 // This is an unnamed field. 2109 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == 2110 cast<FieldDecl>(D); 2111 } 2112 } 2113 2114 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other)) 2115 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); 2116 2117 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other)) 2118 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); 2119 2120 return D->getDeclName() && isa<NamedDecl>(Other) && 2121 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); 2122} 2123 2124template<typename ForwardIterator> 2125static NamedDecl *findInstantiationOf(ASTContext &Ctx, 2126 NamedDecl *D, 2127 ForwardIterator first, 2128 ForwardIterator last) { 2129 for (; first != last; ++first) 2130 if (isInstantiationOf(Ctx, D, *first)) 2131 return cast<NamedDecl>(*first); 2132 2133 return 0; 2134} 2135 2136/// \brief Finds the instantiation of the given declaration context 2137/// within the current instantiation. 2138/// 2139/// \returns NULL if there was an error 2140DeclContext *Sema::FindInstantiatedContext(DeclContext* DC, 2141 const MultiLevelTemplateArgumentList &TemplateArgs) { 2142 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { 2143 Decl* ID = FindInstantiatedDecl(D, TemplateArgs); 2144 return cast_or_null<DeclContext>(ID); 2145 } else return DC; 2146} 2147 2148/// \brief Find the instantiation of the given declaration within the 2149/// current instantiation. 2150/// 2151/// This routine is intended to be used when \p D is a declaration 2152/// referenced from within a template, that needs to mapped into the 2153/// corresponding declaration within an instantiation. For example, 2154/// given: 2155/// 2156/// \code 2157/// template<typename T> 2158/// struct X { 2159/// enum Kind { 2160/// KnownValue = sizeof(T) 2161/// }; 2162/// 2163/// bool getKind() const { return KnownValue; } 2164/// }; 2165/// 2166/// template struct X<int>; 2167/// \endcode 2168/// 2169/// In the instantiation of X<int>::getKind(), we need to map the 2170/// EnumConstantDecl for KnownValue (which refers to 2171/// X<T>::<Kind>::KnownValue) to its instantiation 2172/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs 2173/// this mapping from within the instantiation of X<int>. 2174NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D, 2175 const MultiLevelTemplateArgumentList &TemplateArgs) { 2176 DeclContext *ParentDC = D->getDeclContext(); 2177 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || 2178 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || 2179 ParentDC->isFunctionOrMethod()) { 2180 // D is a local of some kind. Look into the map of local 2181 // declarations to their instantiations. 2182 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D)); 2183 } 2184 2185 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { 2186 if (!Record->isDependentContext()) 2187 return D; 2188 2189 // If the RecordDecl is actually the injected-class-name or a 2190 // "templated" declaration for a class template, class template 2191 // partial specialization, or a member class of a class template, 2192 // substitute into the injected-class-name of the class template 2193 // or partial specialization to find the new DeclContext. 2194 QualType T; 2195 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); 2196 2197 if (ClassTemplate) { 2198 T = ClassTemplate->getInjectedClassNameType(Context); 2199 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec 2200 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { 2201 T = Context.getTypeDeclType(Record); 2202 ClassTemplate = PartialSpec->getSpecializedTemplate(); 2203 } 2204 2205 if (!T.isNull()) { 2206 // Substitute into the injected-class-name to get the type 2207 // corresponding to the instantiation we want, which may also be 2208 // the current instantiation (if we're in a template 2209 // definition). This substitution should never fail, since we 2210 // know we can instantiate the injected-class-name or we 2211 // wouldn't have gotten to the injected-class-name! 2212 2213 // FIXME: Can we use the CurrentInstantiationScope to avoid this 2214 // extra instantiation in the common case? 2215 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName()); 2216 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail."); 2217 2218 if (!T->isDependentType()) { 2219 assert(T->isRecordType() && "Instantiation must produce a record type"); 2220 return T->getAs<RecordType>()->getDecl(); 2221 } 2222 2223 // We are performing "partial" template instantiation to create 2224 // the member declarations for the members of a class template 2225 // specialization. Therefore, D is actually referring to something 2226 // in the current instantiation. Look through the current 2227 // context, which contains actual instantiations, to find the 2228 // instantiation of the "current instantiation" that D refers 2229 // to. 2230 bool SawNonDependentContext = false; 2231 for (DeclContext *DC = CurContext; !DC->isFileContext(); 2232 DC = DC->getParent()) { 2233 if (ClassTemplateSpecializationDecl *Spec 2234 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) 2235 if (isInstantiationOf(ClassTemplate, 2236 Spec->getSpecializedTemplate())) 2237 return Spec; 2238 2239 if (!DC->isDependentContext()) 2240 SawNonDependentContext = true; 2241 } 2242 2243 // We're performing "instantiation" of a member of the current 2244 // instantiation while we are type-checking the 2245 // definition. Compute the declaration context and return that. 2246 assert(!SawNonDependentContext && 2247 "No dependent context while instantiating record"); 2248 DeclContext *DC = computeDeclContext(T); 2249 assert(DC && 2250 "Unable to find declaration for the current instantiation"); 2251 return cast<CXXRecordDecl>(DC); 2252 } 2253 2254 // Fall through to deal with other dependent record types (e.g., 2255 // anonymous unions in class templates). 2256 } 2257 2258 if (!ParentDC->isDependentContext()) 2259 return D; 2260 2261 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs); 2262 if (!ParentDC) 2263 return 0; 2264 2265 if (ParentDC != D->getDeclContext()) { 2266 // We performed some kind of instantiation in the parent context, 2267 // so now we need to look into the instantiated parent context to 2268 // find the instantiation of the declaration D. 2269 NamedDecl *Result = 0; 2270 if (D->getDeclName()) { 2271 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); 2272 Result = findInstantiationOf(Context, D, Found.first, Found.second); 2273 } else { 2274 // Since we don't have a name for the entity we're looking for, 2275 // our only option is to walk through all of the declarations to 2276 // find that name. This will occur in a few cases: 2277 // 2278 // - anonymous struct/union within a template 2279 // - unnamed class/struct/union/enum within a template 2280 // 2281 // FIXME: Find a better way to find these instantiations! 2282 Result = findInstantiationOf(Context, D, 2283 ParentDC->decls_begin(), 2284 ParentDC->decls_end()); 2285 } 2286 2287 // UsingShadowDecls can instantiate to nothing because of using hiding. 2288 assert((Result || isa<UsingShadowDecl>(D)) 2289 && "Unable to find instantiation of declaration!"); 2290 2291 D = Result; 2292 } 2293 2294 return D; 2295} 2296 2297/// \brief Performs template instantiation for all implicit template 2298/// instantiations we have seen until this point. 2299void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) { 2300 while (!PendingLocalImplicitInstantiations.empty() || 2301 (!LocalOnly && !PendingImplicitInstantiations.empty())) { 2302 PendingImplicitInstantiation Inst; 2303 2304 if (PendingLocalImplicitInstantiations.empty()) { 2305 Inst = PendingImplicitInstantiations.front(); 2306 PendingImplicitInstantiations.pop_front(); 2307 } else { 2308 Inst = PendingLocalImplicitInstantiations.front(); 2309 PendingLocalImplicitInstantiations.pop_front(); 2310 } 2311 2312 // Instantiate function definitions 2313 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { 2314 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function), 2315 Function->getLocation(), *this, 2316 Context.getSourceManager(), 2317 "instantiating function definition"); 2318 2319 if (!Function->getBody()) 2320 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true); 2321 continue; 2322 } 2323 2324 // Instantiate static data member definitions. 2325 VarDecl *Var = cast<VarDecl>(Inst.first); 2326 assert(Var->isStaticDataMember() && "Not a static data member?"); 2327 2328 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var), 2329 Var->getLocation(), *this, 2330 Context.getSourceManager(), 2331 "instantiating static data member " 2332 "definition"); 2333 2334 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true); 2335 } 2336} 2337