SemaTemplateInstantiateDecl.cpp revision 55216ac1d698c5d24c5d6a9f0986404cdc703762
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 "clang/AST/ASTContext.h" 14#include "clang/AST/DeclTemplate.h" 15#include "clang/AST/DeclVisitor.h" 16#include "clang/AST/Expr.h" 17#include "llvm/Support/Compiler.h" 18 19using namespace clang; 20 21namespace { 22 class VISIBILITY_HIDDEN TemplateDeclInstantiator 23 : public DeclVisitor<TemplateDeclInstantiator, Decl *> 24 { 25 Sema &SemaRef; 26 DeclContext *Owner; 27 const TemplateArgument *TemplateArgs; 28 unsigned NumTemplateArgs; 29 30 public: 31 typedef Sema::OwningExprResult OwningExprResult; 32 33 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, 34 const TemplateArgument *TemplateArgs, 35 unsigned NumTemplateArgs) 36 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs), 37 NumTemplateArgs(NumTemplateArgs) { } 38 39 // FIXME: Once we get closer to completion, replace these 40 // manually-written declarations with automatically-generated ones 41 // from clang/AST/DeclNodes.def. 42 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); 43 Decl *VisitNamespaceDecl(NamespaceDecl *D); 44 Decl *VisitTypedefDecl(TypedefDecl *D); 45 Decl *VisitVarDecl(VarDecl *D); 46 Decl *VisitFieldDecl(FieldDecl *D); 47 Decl *VisitStaticAssertDecl(StaticAssertDecl *D); 48 Decl *VisitEnumDecl(EnumDecl *D); 49 Decl *VisitEnumConstantDecl(EnumConstantDecl *D); 50 Decl *VisitCXXRecordDecl(CXXRecordDecl *D); 51 Decl *VisitCXXMethodDecl(CXXMethodDecl *D); 52 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); 53 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); 54 Decl *VisitCXXConversionDecl(CXXConversionDecl *D); 55 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); 56 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D); 57 58 // Base case. FIXME: Remove once we can instantiate everything. 59 Decl *VisitDecl(Decl *) { 60 assert(false && "Template instantiation of unknown declaration kind!"); 61 return 0; 62 } 63 64 // Helper functions for instantiating methods. 65 QualType InstantiateFunctionType(FunctionDecl *D, 66 llvm::SmallVectorImpl<ParmVarDecl *> &Params); 67 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); 68 }; 69} 70 71Decl * 72TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 73 assert(false && "Translation units cannot be instantiated"); 74 return D; 75} 76 77Decl * 78TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { 79 assert(false && "Namespaces cannot be instantiated"); 80 return D; 81} 82 83Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { 84 bool Invalid = false; 85 QualType T = D->getUnderlyingType(); 86 if (T->isDependentType()) { 87 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs, 88 D->getLocation(), 89 D->getDeclName()); 90 if (T.isNull()) { 91 Invalid = true; 92 T = SemaRef.Context.IntTy; 93 } 94 } 95 96 // Create the new typedef 97 TypedefDecl *Typedef 98 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), 99 D->getIdentifier(), T); 100 if (Invalid) 101 Typedef->setInvalidDecl(); 102 103 Owner->addDecl(Typedef); 104 return Typedef; 105} 106 107Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { 108 // Instantiate the type of the declaration 109 QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs, 110 NumTemplateArgs, 111 D->getTypeSpecStartLoc(), 112 D->getDeclName()); 113 if (T.isNull()) 114 return 0; 115 116 // Build the instantiataed declaration 117 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, 118 D->getLocation(), D->getIdentifier(), 119 T, D->getStorageClass(), 120 D->getTypeSpecStartLoc()); 121 Var->setThreadSpecified(D->isThreadSpecified()); 122 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer()); 123 Var->setDeclaredInCondition(D->isDeclaredInCondition()); 124 125 // FIXME: In theory, we could have a previous declaration for 126 // variables that are not static data members. 127 bool Redeclaration = false; 128 if (SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration)) 129 Var->setInvalidDecl(); 130 131 Owner->addDecl(Var); 132 133 if (D->getInit()) { 134 OwningExprResult Init 135 = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs, NumTemplateArgs); 136 if (Init.isInvalid()) 137 Var->setInvalidDecl(); 138 else 139 SemaRef.AddInitializerToDecl(Var, move(Init), 140 D->hasCXXDirectInitializer()); 141 } 142 143 return Var; 144} 145 146Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { 147 bool Invalid = false; 148 QualType T = D->getType(); 149 if (T->isDependentType()) { 150 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs, 151 D->getLocation(), 152 D->getDeclName()); 153 if (!T.isNull() && T->isFunctionType()) { 154 // C++ [temp.arg.type]p3: 155 // If a declaration acquires a function type through a type 156 // dependent on a template-parameter and this causes a 157 // declaration that does not use the syntactic form of a 158 // function declarator to have function type, the program is 159 // ill-formed. 160 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) 161 << T; 162 T = QualType(); 163 Invalid = true; 164 } 165 } 166 167 Expr *BitWidth = D->getBitWidth(); 168 if (Invalid) 169 BitWidth = 0; 170 else if (BitWidth) { 171 OwningExprResult InstantiatedBitWidth 172 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs, NumTemplateArgs); 173 if (InstantiatedBitWidth.isInvalid()) { 174 Invalid = true; 175 BitWidth = 0; 176 } else 177 BitWidth = (Expr *)InstantiatedBitWidth.release(); 178 } 179 180 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T, 181 cast<RecordDecl>(Owner), 182 D->getLocation(), 183 D->isMutable(), 184 BitWidth, 185 D->getAccess(), 186 0); 187 if (Field) { 188 if (Invalid) 189 Field->setInvalidDecl(); 190 191 Owner->addDecl(Field); 192 } 193 194 return Field; 195} 196 197Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { 198 Expr *AssertExpr = D->getAssertExpr(); 199 200 OwningExprResult InstantiatedAssertExpr 201 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs, NumTemplateArgs); 202 if (InstantiatedAssertExpr.isInvalid()) 203 return 0; 204 205 OwningExprResult Message = SemaRef.Clone(D->getMessage()); 206 Decl *StaticAssert 207 = (Decl *)SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), 208 move(InstantiatedAssertExpr), 209 move(Message)); 210 return StaticAssert; 211} 212 213Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { 214 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, 215 D->getLocation(), D->getIdentifier(), 216 /*PrevDecl=*/0); 217 Enum->setAccess(D->getAccess()); 218 Owner->addDecl(Enum); 219 Enum->startDefinition(); 220 221 llvm::SmallVector<Sema::DeclTy *, 16> Enumerators; 222 223 EnumConstantDecl *LastEnumConst = 0; 224 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), 225 ECEnd = D->enumerator_end(); 226 EC != ECEnd; ++EC) { 227 // The specified value for the enumerator. 228 OwningExprResult Value = SemaRef.Owned((Expr *)0); 229 if (Expr *UninstValue = EC->getInitExpr()) 230 Value = SemaRef.InstantiateExpr(UninstValue, 231 TemplateArgs, NumTemplateArgs); 232 233 // Drop the initial value and continue. 234 bool isInvalid = false; 235 if (Value.isInvalid()) { 236 Value = SemaRef.Owned((Expr *)0); 237 isInvalid = true; 238 } 239 240 EnumConstantDecl *EnumConst 241 = SemaRef.CheckEnumConstant(Enum, LastEnumConst, 242 EC->getLocation(), EC->getIdentifier(), 243 move(Value)); 244 245 if (isInvalid) { 246 if (EnumConst) 247 EnumConst->setInvalidDecl(); 248 Enum->setInvalidDecl(); 249 } 250 251 if (EnumConst) { 252 Enum->addDecl(EnumConst); 253 Enumerators.push_back(EnumConst); 254 LastEnumConst = EnumConst; 255 } 256 } 257 258 SemaRef.ActOnEnumBody(Enum->getLocation(), Enum, 259 &Enumerators[0], Enumerators.size()); 260 261 return Enum; 262} 263 264Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { 265 assert(false && "EnumConstantDecls can only occur within EnumDecls."); 266 return 0; 267} 268 269Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { 270 CXXRecordDecl *PrevDecl = 0; 271 if (D->isInjectedClassName()) 272 PrevDecl = cast<CXXRecordDecl>(Owner); 273 274 CXXRecordDecl *Record 275 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, 276 D->getLocation(), D->getIdentifier(), PrevDecl); 277 Record->setImplicit(D->isImplicit()); 278 Record->setAccess(D->getAccess()); 279 280 if (!D->isInjectedClassName()) 281 Record->setInstantiationOfMemberClass(D); 282 else 283 Record->setDescribedClassTemplate(D->getDescribedClassTemplate()); 284 285 Owner->addDecl(Record); 286 return Record; 287} 288 289Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { 290 // Only handle actual methods; we'll deal with constructors, 291 // destructors, etc. separately. 292 if (D->getKind() != Decl::CXXMethod) 293 return 0; 294 295 llvm::SmallVector<ParmVarDecl *, 16> Params; 296 QualType T = InstantiateFunctionType(D, Params); 297 if (T.isNull()) 298 return 0; 299 300 // Build the instantiated method declaration. 301 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 302 CXXMethodDecl *Method 303 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), 304 D->getDeclName(), T, D->isStatic(), 305 D->isInline()); 306 307 // Attach the parameters 308 for (unsigned P = 0; P < Params.size(); ++P) 309 Params[P]->setOwningFunction(Method); 310 Method->setParams(SemaRef.Context, &Params[0], Params.size()); 311 312 if (InitMethodInstantiation(Method, D)) 313 Method->setInvalidDecl(); 314 315 NamedDecl *PrevDecl 316 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(), 317 Sema::LookupOrdinaryName, true); 318 // In C++, the previous declaration we find might be a tag type 319 // (class or enum). In this case, the new declaration will hide the 320 // tag type. Note that this does does not apply if we're declaring a 321 // typedef (C++ [dcl.typedef]p4). 322 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) 323 PrevDecl = 0; 324 bool Redeclaration = false; 325 bool OverloadableAttrRequired = false; 326 if (SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration, 327 /*FIXME:*/OverloadableAttrRequired)) 328 Method->setInvalidDecl(); 329 330 if (!Method->isInvalidDecl() || !PrevDecl) 331 Owner->addDecl(Method); 332 return Method; 333} 334 335Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 336 llvm::SmallVector<ParmVarDecl *, 16> Params; 337 QualType T = InstantiateFunctionType(D, Params); 338 if (T.isNull()) 339 return 0; 340 341 // Build the instantiated method declaration. 342 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 343 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); 344 DeclarationName Name 345 = SemaRef.Context.DeclarationNames.getCXXConstructorName(ClassTy); 346 CXXConstructorDecl *Constructor 347 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(), 348 Name, T, D->isExplicit(), D->isInline(), 349 false); 350 351 // Attach the parameters 352 for (unsigned P = 0; P < Params.size(); ++P) 353 Params[P]->setOwningFunction(Constructor); 354 Constructor->setParams(SemaRef.Context, &Params[0], Params.size()); 355 356 if (InitMethodInstantiation(Constructor, D)) 357 Constructor->setInvalidDecl(); 358 359 NamedDecl *PrevDecl 360 = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true); 361 362 // In C++, the previous declaration we find might be a tag type 363 // (class or enum). In this case, the new declaration will hide the 364 // tag type. Note that this does does not apply if we're declaring a 365 // typedef (C++ [dcl.typedef]p4). 366 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) 367 PrevDecl = 0; 368 bool Redeclaration = false; 369 bool OverloadableAttrRequired = false; 370 if (SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration, 371 /*FIXME:*/OverloadableAttrRequired)) 372 Constructor->setInvalidDecl(); 373 374 if (!Constructor->isInvalidDecl()) 375 Owner->addDecl(Constructor); 376 return Constructor; 377} 378 379Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 380 llvm::SmallVector<ParmVarDecl *, 16> Params; 381 QualType T = InstantiateFunctionType(D, Params); 382 if (T.isNull()) 383 return 0; 384 assert(Params.size() == 0 && "Destructor with parameters?"); 385 386 // Build the instantiated destructor declaration. 387 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 388 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); 389 CXXDestructorDecl *Destructor 390 = CXXDestructorDecl::Create(SemaRef.Context, Record, 391 D->getLocation(), 392 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy), 393 T, D->isInline(), false); 394 if (InitMethodInstantiation(Destructor, D)) 395 Destructor->setInvalidDecl(); 396 397 bool Redeclaration = false; 398 bool OverloadableAttrRequired = false; 399 NamedDecl *PrevDecl = 0; 400 if (SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration, 401 /*FIXME:*/OverloadableAttrRequired)) 402 Destructor->setInvalidDecl(); 403 Owner->addDecl(Destructor); 404 return Destructor; 405} 406 407Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { 408 llvm::SmallVector<ParmVarDecl *, 16> Params; 409 QualType T = InstantiateFunctionType(D, Params); 410 if (T.isNull()) 411 return 0; 412 assert(Params.size() == 0 && "Destructor with parameters?"); 413 414 // Build the instantiated conversion declaration. 415 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 416 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); 417 QualType ConvTy 418 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType()); 419 CXXConversionDecl *Conversion 420 = CXXConversionDecl::Create(SemaRef.Context, Record, 421 D->getLocation(), 422 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy), 423 T, D->isInline(), D->isExplicit()); 424 if (InitMethodInstantiation(Conversion, D)) 425 Conversion->setInvalidDecl(); 426 427 bool Redeclaration = false; 428 bool OverloadableAttrRequired = false; 429 NamedDecl *PrevDecl = 0; 430 if (SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration, 431 /*FIXME:*/OverloadableAttrRequired)) 432 Conversion->setInvalidDecl(); 433 Owner->addDecl(Conversion); 434 return Conversion; 435} 436 437ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { 438 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs, 439 NumTemplateArgs, D->getLocation(), 440 D->getDeclName()); 441 if (OrigT.isNull()) 442 return 0; 443 444 QualType T = SemaRef.adjustParameterType(OrigT); 445 446 if (D->getDefaultArg()) { 447 // FIXME: Leave a marker for "uninstantiated" default 448 // arguments. They only get instantiated on demand at the call 449 // site. 450 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning, 451 "sorry, dropping default argument during template instantiation"); 452 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID) 453 << D->getDefaultArg()->getSourceRange(); 454 } 455 456 // Allocate the parameter 457 ParmVarDecl *Param = 0; 458 if (T == OrigT) 459 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), 460 D->getIdentifier(), T, D->getStorageClass(), 461 0); 462 else 463 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner, 464 D->getLocation(), D->getIdentifier(), 465 T, OrigT, D->getStorageClass(), 0); 466 467 // Note: we don't try to instantiate function parameters until after 468 // we've instantiated the function's type. Therefore, we don't have 469 // to check for 'void' parameter types here. 470 return Param; 471} 472 473Decl * 474TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { 475 // Since parameter types can decay either before or after 476 // instantiation, we simply treat OriginalParmVarDecls as 477 // ParmVarDecls the same way, and create one or the other depending 478 // on what happens after template instantiation. 479 return VisitParmVarDecl(D); 480} 481 482Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner, 483 const TemplateArgument *TemplateArgs, 484 unsigned NumTemplateArgs) { 485 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs, 486 NumTemplateArgs); 487 return Instantiator.Visit(D); 488} 489 490/// \brief Instantiates the type of the given function, including 491/// instantiating all of the function parameters. 492/// 493/// \param D The function that we will be instantiated 494/// 495/// \param Params the instantiated parameter declarations 496 497/// \returns the instantiated function's type if successfull, a NULL 498/// type if there was an error. 499QualType 500TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D, 501 llvm::SmallVectorImpl<ParmVarDecl *> &Params) { 502 bool InvalidDecl = false; 503 504 // Instantiate the function parameters 505 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, 506 TemplateArgs, NumTemplateArgs); 507 llvm::SmallVector<QualType, 16> ParamTys; 508 for (FunctionDecl::param_iterator P = D->param_begin(), 509 PEnd = D->param_end(); 510 P != PEnd; ++P) { 511 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { 512 if (PInst->getType()->isVoidType()) { 513 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); 514 PInst->setInvalidDecl(); 515 } 516 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), 517 PInst->getType(), 518 diag::err_abstract_type_in_decl, 519 Sema::AbstractParamType)) 520 PInst->setInvalidDecl(); 521 522 Params.push_back(PInst); 523 ParamTys.push_back(PInst->getType()); 524 525 if (PInst->isInvalidDecl()) 526 InvalidDecl = true; 527 } else 528 InvalidDecl = true; 529 } 530 531 // FIXME: Deallocate dead declarations. 532 if (InvalidDecl) 533 return QualType(); 534 535 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType(); 536 assert(Proto && "Missing prototype?"); 537 QualType ResultType 538 = SemaRef.InstantiateType(Proto->getResultType(), 539 TemplateArgs, NumTemplateArgs, 540 D->getLocation(), D->getDeclName()); 541 if (ResultType.isNull()) 542 return QualType(); 543 544 return SemaRef.BuildFunctionType(ResultType, &ParamTys[0], ParamTys.size(), 545 Proto->isVariadic(), Proto->getTypeQuals(), 546 D->getLocation(), D->getDeclName()); 547} 548 549/// \brief Initializes common fields of an instantiated method 550/// declaration (New) from the corresponding fields of its template 551/// (Tmpl). 552/// 553/// \returns true if there was an error 554bool 555TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, 556 CXXMethodDecl *Tmpl) { 557 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 558 New->setAccess(Tmpl->getAccess()); 559 if (Tmpl->isVirtual()) { 560 New->setVirtual(); 561 Record->setAggregate(false); 562 Record->setPOD(false); 563 Record->setPolymorphic(true); 564 } 565 if (Tmpl->isDeleted()) 566 New->setDeleted(); 567 if (Tmpl->isPure()) { 568 New->setPure(); 569 Record->setAbstract(true); 570 } 571 572 // FIXME: attributes 573 // FIXME: New needs a pointer to Tmpl 574 return false; 575} 576