Decl.cpp revision 198893
1//===--- Decl.cpp - Declaration AST Node Implementation -------------------===// 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// 10// This file implements the Decl subclasses. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/AST/Decl.h" 15#include "clang/AST/DeclCXX.h" 16#include "clang/AST/DeclObjC.h" 17#include "clang/AST/DeclTemplate.h" 18#include "clang/AST/ASTContext.h" 19#include "clang/AST/TypeLoc.h" 20#include "clang/AST/Stmt.h" 21#include "clang/AST/Expr.h" 22#include "clang/AST/PrettyPrinter.h" 23#include "clang/Basic/Builtins.h" 24#include "clang/Basic/IdentifierTable.h" 25#include "clang/Parse/DeclSpec.h" 26#include "llvm/Support/ErrorHandling.h" 27#include <vector> 28 29using namespace clang; 30 31void Attr::Destroy(ASTContext &C) { 32 if (Next) { 33 Next->Destroy(C); 34 Next = 0; 35 } 36 this->~Attr(); 37 C.Deallocate((void*)this); 38} 39 40/// \brief Return the TypeLoc wrapper for the type source info. 41TypeLoc DeclaratorInfo::getTypeLoc() const { 42 return TypeLoc(Ty, (void*)(this + 1)); 43} 44 45//===----------------------------------------------------------------------===// 46// Decl Allocation/Deallocation Method Implementations 47//===----------------------------------------------------------------------===// 48 49 50TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { 51 return new (C) TranslationUnitDecl(C); 52} 53 54NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, 55 SourceLocation L, IdentifierInfo *Id) { 56 return new (C) NamespaceDecl(DC, L, Id); 57} 58 59void NamespaceDecl::Destroy(ASTContext& C) { 60 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations 61 // together. They are all top-level Decls. 62 63 this->~NamespaceDecl(); 64 C.Deallocate((void *)this); 65} 66 67 68ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, 69 SourceLocation L, IdentifierInfo *Id, QualType T) { 70 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); 71} 72 73const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { 74 switch (SC) { 75 case VarDecl::None: break; 76 case VarDecl::Auto: return "auto"; break; 77 case VarDecl::Extern: return "extern"; break; 78 case VarDecl::PrivateExtern: return "__private_extern__"; break; 79 case VarDecl::Register: return "register"; break; 80 case VarDecl::Static: return "static"; break; 81 } 82 83 assert(0 && "Invalid storage class"); 84 return 0; 85} 86 87ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, 88 SourceLocation L, IdentifierInfo *Id, 89 QualType T, DeclaratorInfo *DInfo, 90 StorageClass S, Expr *DefArg) { 91 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, DInfo, S, DefArg); 92} 93 94SourceRange ParmVarDecl::getDefaultArgRange() const { 95 if (const Expr *E = getInit()) 96 return E->getSourceRange(); 97 98 if (const Expr *E = getUninstantiatedDefaultArg()) 99 return E->getSourceRange(); 100 101 return SourceRange(); 102} 103 104void VarDecl::setInit(ASTContext &C, Expr *I) { 105 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { 106 Eval->~EvaluatedStmt(); 107 C.Deallocate(Eval); 108 } 109 110 Init = I; 111} 112 113bool VarDecl::isExternC() const { 114 ASTContext &Context = getASTContext(); 115 if (!Context.getLangOptions().CPlusPlus) 116 return (getDeclContext()->isTranslationUnit() && 117 getStorageClass() != Static) || 118 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); 119 120 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); 121 DC = DC->getParent()) { 122 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { 123 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) 124 return getStorageClass() != Static; 125 126 break; 127 } 128 129 if (DC->isFunctionOrMethod()) 130 return false; 131 } 132 133 return false; 134} 135 136FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, 137 SourceLocation L, 138 DeclarationName N, QualType T, 139 DeclaratorInfo *DInfo, 140 StorageClass S, bool isInline, 141 bool hasWrittenPrototype) { 142 FunctionDecl *New 143 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline); 144 New->HasWrittenPrototype = hasWrittenPrototype; 145 return New; 146} 147 148BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { 149 return new (C) BlockDecl(DC, L); 150} 151 152FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 153 IdentifierInfo *Id, QualType T, 154 DeclaratorInfo *DInfo, Expr *BW, bool Mutable) { 155 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable); 156} 157 158bool FieldDecl::isAnonymousStructOrUnion() const { 159 if (!isImplicit() || getDeclName()) 160 return false; 161 162 if (const RecordType *Record = getType()->getAs<RecordType>()) 163 return Record->getDecl()->isAnonymousStructOrUnion(); 164 165 return false; 166} 167 168EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, 169 SourceLocation L, 170 IdentifierInfo *Id, QualType T, 171 Expr *E, const llvm::APSInt &V) { 172 return new (C) EnumConstantDecl(CD, L, Id, T, E, V); 173} 174 175void EnumConstantDecl::Destroy(ASTContext& C) { 176 if (Init) Init->Destroy(C); 177 Decl::Destroy(C); 178} 179 180TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, 181 SourceLocation L, IdentifierInfo *Id, 182 DeclaratorInfo *DInfo) { 183 return new (C) TypedefDecl(DC, L, Id, DInfo); 184} 185 186EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 187 IdentifierInfo *Id, SourceLocation TKL, 188 EnumDecl *PrevDecl) { 189 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL); 190 C.getTypeDeclType(Enum, PrevDecl); 191 return Enum; 192} 193 194void EnumDecl::Destroy(ASTContext& C) { 195 Decl::Destroy(C); 196} 197 198void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) { 199 assert(!isDefinition() && "Cannot redefine enums!"); 200 IntegerType = NewType; 201 TagDecl::completeDefinition(); 202} 203 204FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, 205 SourceLocation L, 206 StringLiteral *Str) { 207 return new (C) FileScopeAsmDecl(DC, L, Str); 208} 209 210//===----------------------------------------------------------------------===// 211// NamedDecl Implementation 212//===----------------------------------------------------------------------===// 213 214std::string NamedDecl::getQualifiedNameAsString() const { 215 return getQualifiedNameAsString(getASTContext().getLangOptions()); 216} 217 218std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { 219 // FIXME: Collect contexts, then accumulate names to avoid unnecessary 220 // std::string thrashing. 221 std::vector<std::string> Names; 222 std::string QualName; 223 const DeclContext *Ctx = getDeclContext(); 224 225 if (Ctx->isFunctionOrMethod()) 226 return getNameAsString(); 227 228 while (Ctx) { 229 if (Ctx->isFunctionOrMethod()) 230 // FIXME: That probably will happen, when D was member of local 231 // scope class/struct/union. How do we handle this case? 232 break; 233 234 if (const ClassTemplateSpecializationDecl *Spec 235 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { 236 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 237 std::string TemplateArgsStr 238 = TemplateSpecializationType::PrintTemplateArgumentList( 239 TemplateArgs.getFlatArgumentList(), 240 TemplateArgs.flat_size(), 241 P); 242 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr); 243 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) 244 Names.push_back(ND->getNameAsString()); 245 else 246 break; 247 248 Ctx = Ctx->getParent(); 249 } 250 251 std::vector<std::string>::reverse_iterator 252 I = Names.rbegin(), 253 End = Names.rend(); 254 255 for (; I!=End; ++I) 256 QualName += *I + "::"; 257 258 QualName += getNameAsString(); 259 260 return QualName; 261} 262 263bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { 264 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); 265 266 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. 267 // We want to keep it, unless it nominates same namespace. 268 if (getKind() == Decl::UsingDirective) { 269 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == 270 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); 271 } 272 273 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) 274 // For function declarations, we keep track of redeclarations. 275 return FD->getPreviousDeclaration() == OldD; 276 277 // For function templates, the underlying function declarations are linked. 278 if (const FunctionTemplateDecl *FunctionTemplate 279 = dyn_cast<FunctionTemplateDecl>(this)) 280 if (const FunctionTemplateDecl *OldFunctionTemplate 281 = dyn_cast<FunctionTemplateDecl>(OldD)) 282 return FunctionTemplate->getTemplatedDecl() 283 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); 284 285 // For method declarations, we keep track of redeclarations. 286 if (isa<ObjCMethodDecl>(this)) 287 return false; 288 289 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) 290 return true; 291 292 // For non-function declarations, if the declarations are of the 293 // same kind then this must be a redeclaration, or semantic analysis 294 // would not have given us the new declaration. 295 return this->getKind() == OldD->getKind(); 296} 297 298bool NamedDecl::hasLinkage() const { 299 if (const VarDecl *VD = dyn_cast<VarDecl>(this)) 300 return VD->hasExternalStorage() || VD->isFileVarDecl(); 301 302 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this)) 303 return true; 304 305 return false; 306} 307 308NamedDecl *NamedDecl::getUnderlyingDecl() { 309 NamedDecl *ND = this; 310 while (true) { 311 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND)) 312 ND = UD->getTargetDecl(); 313 else if (ObjCCompatibleAliasDecl *AD 314 = dyn_cast<ObjCCompatibleAliasDecl>(ND)) 315 return AD->getClassInterface(); 316 else 317 return ND; 318 } 319} 320 321//===----------------------------------------------------------------------===// 322// DeclaratorDecl Implementation 323//===----------------------------------------------------------------------===// 324 325SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { 326 if (DeclInfo) { 327 TypeLoc TL = DeclInfo->getTypeLoc(); 328 while (true) { 329 TypeLoc NextTL = TL.getNextTypeLoc(); 330 if (!NextTL) 331 return TL.getSourceRange().getBegin(); 332 TL = NextTL; 333 } 334 } 335 return SourceLocation(); 336} 337 338//===----------------------------------------------------------------------===// 339// VarDecl Implementation 340//===----------------------------------------------------------------------===// 341 342VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 343 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo, 344 StorageClass S) { 345 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S); 346} 347 348void VarDecl::Destroy(ASTContext& C) { 349 Expr *Init = getInit(); 350 if (Init) { 351 Init->Destroy(C); 352 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) { 353 Eval->~EvaluatedStmt(); 354 C.Deallocate(Eval); 355 } 356 } 357 this->~VarDecl(); 358 C.Deallocate((void *)this); 359} 360 361VarDecl::~VarDecl() { 362} 363 364SourceRange VarDecl::getSourceRange() const { 365 if (getInit()) 366 return SourceRange(getLocation(), getInit()->getLocEnd()); 367 return SourceRange(getLocation(), getLocation()); 368} 369 370bool VarDecl::isOutOfLine() const { 371 if (!isStaticDataMember()) 372 return false; 373 374 if (Decl::isOutOfLine()) 375 return true; 376 377 // If this static data member was instantiated from a static data member of 378 // a class template, check whether that static data member was defined 379 // out-of-line. 380 if (VarDecl *VD = getInstantiatedFromStaticDataMember()) 381 return VD->isOutOfLine(); 382 383 return false; 384} 385 386VarDecl *VarDecl::getOutOfLineDefinition() { 387 if (!isStaticDataMember()) 388 return 0; 389 390 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); 391 RD != RDEnd; ++RD) { 392 if (RD->getLexicalDeclContext()->isFileContext()) 393 return *RD; 394 } 395 396 return 0; 397} 398 399VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { 400 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 401 return cast<VarDecl>(MSI->getInstantiatedFrom()); 402 403 return 0; 404} 405 406TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { 407 if (MemberSpecializationInfo *MSI 408 = getASTContext().getInstantiatedFromStaticDataMember(this)) 409 return MSI->getTemplateSpecializationKind(); 410 411 return TSK_Undeclared; 412} 413 414MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { 415 return getASTContext().getInstantiatedFromStaticDataMember(this); 416} 417 418void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 419 SourceLocation PointOfInstantiation) { 420 MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); 421 assert(MSI && "Not an instantiated static data member?"); 422 MSI->setTemplateSpecializationKind(TSK); 423 if (TSK != TSK_ExplicitSpecialization && 424 PointOfInstantiation.isValid() && 425 MSI->getPointOfInstantiation().isInvalid()) 426 MSI->setPointOfInstantiation(PointOfInstantiation); 427} 428 429bool VarDecl::isTentativeDefinition(ASTContext &Context) const { 430 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus) 431 return false; 432 433 const VarDecl *Def = 0; 434 return (!getDefinition(Def) && 435 (getStorageClass() == None || getStorageClass() == Static)); 436} 437 438const Expr *VarDecl::getDefinition(const VarDecl *&Def) const { 439 redecl_iterator I = redecls_begin(), E = redecls_end(); 440 while (I != E && !I->getInit()) 441 ++I; 442 443 if (I != E) { 444 Def = *I; 445 return I->getInit(); 446 } 447 return 0; 448} 449 450VarDecl *VarDecl::getCanonicalDecl() { 451 return getFirstDeclaration(); 452} 453 454//===----------------------------------------------------------------------===// 455// FunctionDecl Implementation 456//===----------------------------------------------------------------------===// 457 458void FunctionDecl::Destroy(ASTContext& C) { 459 if (Body && Body.isOffset()) 460 Body.get(C.getExternalSource())->Destroy(C); 461 462 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) 463 (*I)->Destroy(C); 464 465 FunctionTemplateSpecializationInfo *FTSInfo 466 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 467 if (FTSInfo) 468 C.Deallocate(FTSInfo); 469 470 MemberSpecializationInfo *MSInfo 471 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 472 if (MSInfo) 473 C.Deallocate(MSInfo); 474 475 C.Deallocate(ParamInfo); 476 477 Decl::Destroy(C); 478} 479 480void FunctionDecl::getNameForDiagnostic(std::string &S, 481 const PrintingPolicy &Policy, 482 bool Qualified) const { 483 NamedDecl::getNameForDiagnostic(S, Policy, Qualified); 484 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); 485 if (TemplateArgs) 486 S += TemplateSpecializationType::PrintTemplateArgumentList( 487 TemplateArgs->getFlatArgumentList(), 488 TemplateArgs->flat_size(), 489 Policy); 490 491} 492 493Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { 494 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 495 if (I->Body) { 496 Definition = *I; 497 return I->Body.get(getASTContext().getExternalSource()); 498 } 499 } 500 501 return 0; 502} 503 504void FunctionDecl::setBody(Stmt *B) { 505 Body = B; 506 if (B) 507 EndRangeLoc = B->getLocEnd(); 508} 509 510bool FunctionDecl::isMain() const { 511 ASTContext &Context = getASTContext(); 512 return !Context.getLangOptions().Freestanding && 513 getDeclContext()->getLookupContext()->isTranslationUnit() && 514 getIdentifier() && getIdentifier()->isStr("main"); 515} 516 517bool FunctionDecl::isExternC() const { 518 ASTContext &Context = getASTContext(); 519 // In C, any non-static, non-overloadable function has external 520 // linkage. 521 if (!Context.getLangOptions().CPlusPlus) 522 return getStorageClass() != Static && !getAttr<OverloadableAttr>(); 523 524 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); 525 DC = DC->getParent()) { 526 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { 527 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) 528 return getStorageClass() != Static && 529 !getAttr<OverloadableAttr>(); 530 531 break; 532 } 533 } 534 535 return false; 536} 537 538bool FunctionDecl::isGlobal() const { 539 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) 540 return Method->isStatic(); 541 542 if (getStorageClass() == Static) 543 return false; 544 545 for (const DeclContext *DC = getDeclContext(); 546 DC->isNamespace(); 547 DC = DC->getParent()) { 548 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { 549 if (!Namespace->getDeclName()) 550 return false; 551 break; 552 } 553 } 554 555 return true; 556} 557 558/// \brief Returns a value indicating whether this function 559/// corresponds to a builtin function. 560/// 561/// The function corresponds to a built-in function if it is 562/// declared at translation scope or within an extern "C" block and 563/// its name matches with the name of a builtin. The returned value 564/// will be 0 for functions that do not correspond to a builtin, a 565/// value of type \c Builtin::ID if in the target-independent range 566/// \c [1,Builtin::First), or a target-specific builtin value. 567unsigned FunctionDecl::getBuiltinID() const { 568 ASTContext &Context = getASTContext(); 569 if (!getIdentifier() || !getIdentifier()->getBuiltinID()) 570 return 0; 571 572 unsigned BuiltinID = getIdentifier()->getBuiltinID(); 573 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 574 return BuiltinID; 575 576 // This function has the name of a known C library 577 // function. Determine whether it actually refers to the C library 578 // function or whether it just has the same name. 579 580 // If this is a static function, it's not a builtin. 581 if (getStorageClass() == Static) 582 return 0; 583 584 // If this function is at translation-unit scope and we're not in 585 // C++, it refers to the C library function. 586 if (!Context.getLangOptions().CPlusPlus && 587 getDeclContext()->isTranslationUnit()) 588 return BuiltinID; 589 590 // If the function is in an extern "C" linkage specification and is 591 // not marked "overloadable", it's the real function. 592 if (isa<LinkageSpecDecl>(getDeclContext()) && 593 cast<LinkageSpecDecl>(getDeclContext())->getLanguage() 594 == LinkageSpecDecl::lang_c && 595 !getAttr<OverloadableAttr>()) 596 return BuiltinID; 597 598 // Not a builtin 599 return 0; 600} 601 602 603/// getNumParams - Return the number of parameters this function must have 604/// based on its FunctionType. This is the length of the PararmInfo array 605/// after it has been created. 606unsigned FunctionDecl::getNumParams() const { 607 const FunctionType *FT = getType()->getAs<FunctionType>(); 608 if (isa<FunctionNoProtoType>(FT)) 609 return 0; 610 return cast<FunctionProtoType>(FT)->getNumArgs(); 611 612} 613 614void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, 615 unsigned NumParams) { 616 assert(ParamInfo == 0 && "Already has param info!"); 617 assert(NumParams == getNumParams() && "Parameter count mismatch!"); 618 619 // Zero params -> null pointer. 620 if (NumParams) { 621 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); 622 ParamInfo = new (Mem) ParmVarDecl*[NumParams]; 623 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); 624 625 // Update source range. The check below allows us to set EndRangeLoc before 626 // setting the parameters. 627 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) 628 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); 629 } 630} 631 632/// getMinRequiredArguments - Returns the minimum number of arguments 633/// needed to call this function. This may be fewer than the number of 634/// function parameters, if some of the parameters have default 635/// arguments (in C++). 636unsigned FunctionDecl::getMinRequiredArguments() const { 637 unsigned NumRequiredArgs = getNumParams(); 638 while (NumRequiredArgs > 0 639 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) 640 --NumRequiredArgs; 641 642 return NumRequiredArgs; 643} 644 645bool FunctionDecl::isInlined() const { 646 if (isInlineSpecified() || (isa<CXXMethodDecl>(this) && !isOutOfLine())) 647 return true; 648 649 switch (getTemplateSpecializationKind()) { 650 case TSK_Undeclared: 651 case TSK_ExplicitSpecialization: 652 return false; 653 654 case TSK_ImplicitInstantiation: 655 case TSK_ExplicitInstantiationDeclaration: 656 case TSK_ExplicitInstantiationDefinition: 657 // Handle below. 658 break; 659 } 660 661 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 662 Stmt *Pattern = 0; 663 if (PatternDecl) 664 Pattern = PatternDecl->getBody(PatternDecl); 665 666 if (Pattern && PatternDecl) 667 return PatternDecl->isInlined(); 668 669 return false; 670} 671 672/// \brief For an inline function definition in C or C++, determine whether the 673/// definition will be externally visible. 674/// 675/// Inline function definitions are always available for inlining optimizations. 676/// However, depending on the language dialect, declaration specifiers, and 677/// attributes, the definition of an inline function may or may not be 678/// "externally" visible to other translation units in the program. 679/// 680/// In C99, inline definitions are not externally visible by default. However, 681/// if even one of the globa-scope declarations is marked "extern inline", the 682/// inline definition becomes externally visible (C99 6.7.4p6). 683/// 684/// In GNU89 mode, or if the gnu_inline attribute is attached to the function 685/// definition, we use the GNU semantics for inline, which are nearly the 686/// opposite of C99 semantics. In particular, "inline" by itself will create 687/// an externally visible symbol, but "extern inline" will not create an 688/// externally visible symbol. 689bool FunctionDecl::isInlineDefinitionExternallyVisible() const { 690 assert(isThisDeclarationADefinition() && "Must have the function definition"); 691 assert(isInlined() && "Function must be inline"); 692 ASTContext &Context = getASTContext(); 693 694 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { 695 // GNU inline semantics. Based on a number of examples, we came up with the 696 // following heuristic: if the "inline" keyword is present on a 697 // declaration of the function but "extern" is not present on that 698 // declaration, then the symbol is externally visible. Otherwise, the GNU 699 // "extern inline" semantics applies and the symbol is not externally 700 // visible. 701 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); 702 Redecl != RedeclEnd; 703 ++Redecl) { 704 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern) 705 return true; 706 } 707 708 // GNU "extern inline" semantics; no externally visible symbol. 709 return false; 710 } 711 712 // C99 6.7.4p6: 713 // [...] If all of the file scope declarations for a function in a 714 // translation unit include the inline function specifier without extern, 715 // then the definition in that translation unit is an inline definition. 716 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); 717 Redecl != RedeclEnd; 718 ++Redecl) { 719 // Only consider file-scope declarations in this test. 720 if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) 721 continue; 722 723 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern) 724 return true; // Not an inline definition 725 } 726 727 // C99 6.7.4p6: 728 // An inline definition does not provide an external definition for the 729 // function, and does not forbid an external definition in another 730 // translation unit. 731 return false; 732} 733 734void 735FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { 736 redeclarable_base::setPreviousDeclaration(PrevDecl); 737 738 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { 739 FunctionTemplateDecl *PrevFunTmpl 740 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; 741 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); 742 FunTmpl->setPreviousDeclaration(PrevFunTmpl); 743 } 744} 745 746const FunctionDecl *FunctionDecl::getCanonicalDecl() const { 747 return getFirstDeclaration(); 748} 749 750FunctionDecl *FunctionDecl::getCanonicalDecl() { 751 return getFirstDeclaration(); 752} 753 754/// getOverloadedOperator - Which C++ overloaded operator this 755/// function represents, if any. 756OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { 757 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) 758 return getDeclName().getCXXOverloadedOperator(); 759 else 760 return OO_None; 761} 762 763FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { 764 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) 765 return cast<FunctionDecl>(Info->getInstantiatedFrom()); 766 767 return 0; 768} 769 770MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { 771 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 772} 773 774void 775FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD, 776 TemplateSpecializationKind TSK) { 777 assert(TemplateOrSpecialization.isNull() && 778 "Member function is already a specialization"); 779 MemberSpecializationInfo *Info 780 = new (getASTContext()) MemberSpecializationInfo(FD, TSK); 781 TemplateOrSpecialization = Info; 782} 783 784bool FunctionDecl::isImplicitlyInstantiable() const { 785 // If this function already has a definition or is invalid, it can't be 786 // implicitly instantiated. 787 if (isInvalidDecl() || getBody()) 788 return false; 789 790 switch (getTemplateSpecializationKind()) { 791 case TSK_Undeclared: 792 case TSK_ExplicitSpecialization: 793 case TSK_ExplicitInstantiationDefinition: 794 return false; 795 796 case TSK_ImplicitInstantiation: 797 return true; 798 799 case TSK_ExplicitInstantiationDeclaration: 800 // Handled below. 801 break; 802 } 803 804 // Find the actual template from which we will instantiate. 805 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 806 Stmt *Pattern = 0; 807 if (PatternDecl) 808 Pattern = PatternDecl->getBody(PatternDecl); 809 810 // C++0x [temp.explicit]p9: 811 // Except for inline functions, other explicit instantiation declarations 812 // have the effect of suppressing the implicit instantiation of the entity 813 // to which they refer. 814 if (!Pattern || !PatternDecl) 815 return true; 816 817 return PatternDecl->isInlined(); 818} 819 820FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { 821 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { 822 while (Primary->getInstantiatedFromMemberTemplate()) { 823 // If we have hit a point where the user provided a specialization of 824 // this template, we're done looking. 825 if (Primary->isMemberSpecialization()) 826 break; 827 828 Primary = Primary->getInstantiatedFromMemberTemplate(); 829 } 830 831 return Primary->getTemplatedDecl(); 832 } 833 834 return getInstantiatedFromMemberFunction(); 835} 836 837FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { 838 if (FunctionTemplateSpecializationInfo *Info 839 = TemplateOrSpecialization 840 .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 841 return Info->Template.getPointer(); 842 } 843 return 0; 844} 845 846const TemplateArgumentList * 847FunctionDecl::getTemplateSpecializationArgs() const { 848 if (FunctionTemplateSpecializationInfo *Info 849 = TemplateOrSpecialization 850 .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 851 return Info->TemplateArguments; 852 } 853 return 0; 854} 855 856void 857FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context, 858 FunctionTemplateDecl *Template, 859 const TemplateArgumentList *TemplateArgs, 860 void *InsertPos, 861 TemplateSpecializationKind TSK) { 862 assert(TSK != TSK_Undeclared && 863 "Must specify the type of function template specialization"); 864 FunctionTemplateSpecializationInfo *Info 865 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 866 if (!Info) 867 Info = new (Context) FunctionTemplateSpecializationInfo; 868 869 Info->Function = this; 870 Info->Template.setPointer(Template); 871 Info->Template.setInt(TSK - 1); 872 Info->TemplateArguments = TemplateArgs; 873 TemplateOrSpecialization = Info; 874 875 // Insert this function template specialization into the set of known 876 // function template specializations. 877 if (InsertPos) 878 Template->getSpecializations().InsertNode(Info, InsertPos); 879 else { 880 // Try to insert the new node. If there is an existing node, remove it 881 // first. 882 FunctionTemplateSpecializationInfo *Existing 883 = Template->getSpecializations().GetOrInsertNode(Info); 884 if (Existing) { 885 Template->getSpecializations().RemoveNode(Existing); 886 Template->getSpecializations().GetOrInsertNode(Info); 887 } 888 } 889} 890 891TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { 892 // For a function template specialization, query the specialization 893 // information object. 894 FunctionTemplateSpecializationInfo *FTSInfo 895 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 896 if (FTSInfo) 897 return FTSInfo->getTemplateSpecializationKind(); 898 899 MemberSpecializationInfo *MSInfo 900 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 901 if (MSInfo) 902 return MSInfo->getTemplateSpecializationKind(); 903 904 return TSK_Undeclared; 905} 906 907void 908FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 909 SourceLocation PointOfInstantiation) { 910 if (FunctionTemplateSpecializationInfo *FTSInfo 911 = TemplateOrSpecialization.dyn_cast< 912 FunctionTemplateSpecializationInfo*>()) { 913 FTSInfo->setTemplateSpecializationKind(TSK); 914 if (TSK != TSK_ExplicitSpecialization && 915 PointOfInstantiation.isValid() && 916 FTSInfo->getPointOfInstantiation().isInvalid()) 917 FTSInfo->setPointOfInstantiation(PointOfInstantiation); 918 } else if (MemberSpecializationInfo *MSInfo 919 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { 920 MSInfo->setTemplateSpecializationKind(TSK); 921 if (TSK != TSK_ExplicitSpecialization && 922 PointOfInstantiation.isValid() && 923 MSInfo->getPointOfInstantiation().isInvalid()) 924 MSInfo->setPointOfInstantiation(PointOfInstantiation); 925 } else 926 assert(false && "Function cannot have a template specialization kind"); 927} 928 929SourceLocation FunctionDecl::getPointOfInstantiation() const { 930 if (FunctionTemplateSpecializationInfo *FTSInfo 931 = TemplateOrSpecialization.dyn_cast< 932 FunctionTemplateSpecializationInfo*>()) 933 return FTSInfo->getPointOfInstantiation(); 934 else if (MemberSpecializationInfo *MSInfo 935 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) 936 return MSInfo->getPointOfInstantiation(); 937 938 return SourceLocation(); 939} 940 941bool FunctionDecl::isOutOfLine() const { 942 if (Decl::isOutOfLine()) 943 return true; 944 945 // If this function was instantiated from a member function of a 946 // class template, check whether that member function was defined out-of-line. 947 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { 948 const FunctionDecl *Definition; 949 if (FD->getBody(Definition)) 950 return Definition->isOutOfLine(); 951 } 952 953 // If this function was instantiated from a function template, 954 // check whether that function template was defined out-of-line. 955 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { 956 const FunctionDecl *Definition; 957 if (FunTmpl->getTemplatedDecl()->getBody(Definition)) 958 return Definition->isOutOfLine(); 959 } 960 961 return false; 962} 963 964//===----------------------------------------------------------------------===// 965// TagDecl Implementation 966//===----------------------------------------------------------------------===// 967 968SourceRange TagDecl::getSourceRange() const { 969 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); 970 return SourceRange(TagKeywordLoc, E); 971} 972 973TagDecl* TagDecl::getCanonicalDecl() { 974 return getFirstDeclaration(); 975} 976 977void TagDecl::startDefinition() { 978 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { 979 TagT->decl.setPointer(this); 980 TagT->decl.setInt(1); 981 } 982} 983 984void TagDecl::completeDefinition() { 985 IsDefinition = true; 986 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { 987 assert(TagT->decl.getPointer() == this && 988 "Attempt to redefine a tag definition?"); 989 TagT->decl.setInt(0); 990 } 991} 992 993TagDecl* TagDecl::getDefinition(ASTContext& C) const { 994 if (isDefinition()) 995 return const_cast<TagDecl *>(this); 996 997 for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); 998 R != REnd; ++R) 999 if (R->isDefinition()) 1000 return *R; 1001 1002 return 0; 1003} 1004 1005TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) { 1006 switch (TypeSpec) { 1007 default: llvm::llvm_unreachable("unexpected type specifier"); 1008 case DeclSpec::TST_struct: return TK_struct; 1009 case DeclSpec::TST_class: return TK_class; 1010 case DeclSpec::TST_union: return TK_union; 1011 case DeclSpec::TST_enum: return TK_enum; 1012 } 1013} 1014 1015//===----------------------------------------------------------------------===// 1016// RecordDecl Implementation 1017//===----------------------------------------------------------------------===// 1018 1019RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, 1020 IdentifierInfo *Id, RecordDecl *PrevDecl, 1021 SourceLocation TKL) 1022 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { 1023 HasFlexibleArrayMember = false; 1024 AnonymousStructOrUnion = false; 1025 HasObjectMember = false; 1026 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); 1027} 1028 1029RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, 1030 SourceLocation L, IdentifierInfo *Id, 1031 SourceLocation TKL, RecordDecl* PrevDecl) { 1032 1033 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); 1034 C.getTypeDeclType(R, PrevDecl); 1035 return R; 1036} 1037 1038RecordDecl::~RecordDecl() { 1039} 1040 1041void RecordDecl::Destroy(ASTContext& C) { 1042 TagDecl::Destroy(C); 1043} 1044 1045bool RecordDecl::isInjectedClassName() const { 1046 return isImplicit() && getDeclName() && getDeclContext()->isRecord() && 1047 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); 1048} 1049 1050/// completeDefinition - Notes that the definition of this type is now 1051/// complete. 1052void RecordDecl::completeDefinition(ASTContext& C) { 1053 assert(!isDefinition() && "Cannot redefine record!"); 1054 TagDecl::completeDefinition(); 1055} 1056 1057//===----------------------------------------------------------------------===// 1058// BlockDecl Implementation 1059//===----------------------------------------------------------------------===// 1060 1061BlockDecl::~BlockDecl() { 1062} 1063 1064void BlockDecl::Destroy(ASTContext& C) { 1065 if (Body) 1066 Body->Destroy(C); 1067 1068 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) 1069 (*I)->Destroy(C); 1070 1071 C.Deallocate(ParamInfo); 1072 Decl::Destroy(C); 1073} 1074 1075void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, 1076 unsigned NParms) { 1077 assert(ParamInfo == 0 && "Already has param info!"); 1078 1079 // Zero params -> null pointer. 1080 if (NParms) { 1081 NumParams = NParms; 1082 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); 1083 ParamInfo = new (Mem) ParmVarDecl*[NumParams]; 1084 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); 1085 } 1086} 1087 1088unsigned BlockDecl::getNumParams() const { 1089 return NumParams; 1090} 1091