Decl.cpp revision 33e9abd21083a0191a7676a04b497006d2da184d
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/ExprCXX.h" 23#include "clang/AST/PrettyPrinter.h" 24#include "clang/Basic/Builtins.h" 25#include "clang/Basic/IdentifierTable.h" 26#include "clang/Parse/DeclSpec.h" 27#include "llvm/Support/ErrorHandling.h" 28#include <vector> 29 30using namespace clang; 31 32void Attr::Destroy(ASTContext &C) { 33 if (Next) { 34 Next->Destroy(C); 35 Next = 0; 36 } 37 this->~Attr(); 38 C.Deallocate((void*)this); 39} 40 41/// \brief Return the TypeLoc wrapper for the type source info. 42TypeLoc TypeSourceInfo::getTypeLoc() const { 43 return TypeLoc(Ty, (void*)(this + 1)); 44} 45 46//===----------------------------------------------------------------------===// 47// Decl Allocation/Deallocation Method Implementations 48//===----------------------------------------------------------------------===// 49 50 51TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { 52 return new (C) TranslationUnitDecl(C); 53} 54 55NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, 56 SourceLocation L, IdentifierInfo *Id) { 57 return new (C) NamespaceDecl(DC, L, Id); 58} 59 60void NamespaceDecl::Destroy(ASTContext& C) { 61 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations 62 // together. They are all top-level Decls. 63 64 this->~NamespaceDecl(); 65 C.Deallocate((void *)this); 66} 67 68 69ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, 70 SourceLocation L, IdentifierInfo *Id, QualType T) { 71 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); 72} 73 74const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { 75 switch (SC) { 76 case VarDecl::None: break; 77 case VarDecl::Auto: return "auto"; break; 78 case VarDecl::Extern: return "extern"; break; 79 case VarDecl::PrivateExtern: return "__private_extern__"; break; 80 case VarDecl::Register: return "register"; break; 81 case VarDecl::Static: return "static"; break; 82 } 83 84 assert(0 && "Invalid storage class"); 85 return 0; 86} 87 88ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, 89 SourceLocation L, IdentifierInfo *Id, 90 QualType T, TypeSourceInfo *TInfo, 91 StorageClass S, Expr *DefArg) { 92 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg); 93} 94 95Expr *ParmVarDecl::getDefaultArg() { 96 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); 97 assert(!hasUninstantiatedDefaultArg() && 98 "Default argument is not yet instantiated!"); 99 100 Expr *Arg = getInit(); 101 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg)) 102 return E->getSubExpr(); 103 104 return Arg; 105} 106 107unsigned ParmVarDecl::getNumDefaultArgTemporaries() const { 108 if (const CXXExprWithTemporaries *E = 109 dyn_cast<CXXExprWithTemporaries>(getInit())) 110 return E->getNumTemporaries(); 111 112 return 0; 113} 114 115CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) { 116 assert(getNumDefaultArgTemporaries() && 117 "Default arguments does not have any temporaries!"); 118 119 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit()); 120 return E->getTemporary(i); 121} 122 123SourceRange ParmVarDecl::getDefaultArgRange() const { 124 if (const Expr *E = getInit()) 125 return E->getSourceRange(); 126 127 if (hasUninstantiatedDefaultArg()) 128 return getUninstantiatedDefaultArg()->getSourceRange(); 129 130 return SourceRange(); 131} 132 133void VarDecl::setInit(ASTContext &C, Expr *I) { 134 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { 135 Eval->~EvaluatedStmt(); 136 C.Deallocate(Eval); 137 } 138 139 Init = I; 140} 141 142bool VarDecl::isExternC() const { 143 ASTContext &Context = getASTContext(); 144 if (!Context.getLangOptions().CPlusPlus) 145 return (getDeclContext()->isTranslationUnit() && 146 getStorageClass() != Static) || 147 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); 148 149 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); 150 DC = DC->getParent()) { 151 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { 152 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) 153 return getStorageClass() != Static; 154 155 break; 156 } 157 158 if (DC->isFunctionOrMethod()) 159 return false; 160 } 161 162 return false; 163} 164 165FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, 166 SourceLocation L, 167 DeclarationName N, QualType T, 168 TypeSourceInfo *TInfo, 169 StorageClass S, bool isInline, 170 bool hasWrittenPrototype) { 171 FunctionDecl *New 172 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline); 173 New->HasWrittenPrototype = hasWrittenPrototype; 174 return New; 175} 176 177BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { 178 return new (C) BlockDecl(DC, L); 179} 180 181FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 182 IdentifierInfo *Id, QualType T, 183 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) { 184 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable); 185} 186 187bool FieldDecl::isAnonymousStructOrUnion() const { 188 if (!isImplicit() || getDeclName()) 189 return false; 190 191 if (const RecordType *Record = getType()->getAs<RecordType>()) 192 return Record->getDecl()->isAnonymousStructOrUnion(); 193 194 return false; 195} 196 197EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, 198 SourceLocation L, 199 IdentifierInfo *Id, QualType T, 200 Expr *E, const llvm::APSInt &V) { 201 return new (C) EnumConstantDecl(CD, L, Id, T, E, V); 202} 203 204void EnumConstantDecl::Destroy(ASTContext& C) { 205 if (Init) Init->Destroy(C); 206 Decl::Destroy(C); 207} 208 209TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, 210 SourceLocation L, IdentifierInfo *Id, 211 TypeSourceInfo *TInfo) { 212 return new (C) TypedefDecl(DC, L, Id, TInfo); 213} 214 215// Anchor TypedefDecl's vtable here. 216TypedefDecl::~TypedefDecl() {} 217 218EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 219 IdentifierInfo *Id, SourceLocation TKL, 220 EnumDecl *PrevDecl) { 221 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL); 222 C.getTypeDeclType(Enum, PrevDecl); 223 return Enum; 224} 225 226void EnumDecl::Destroy(ASTContext& C) { 227 Decl::Destroy(C); 228} 229 230void EnumDecl::completeDefinition(ASTContext &C, 231 QualType NewType, 232 QualType NewPromotionType) { 233 assert(!isDefinition() && "Cannot redefine enums!"); 234 IntegerType = NewType; 235 PromotionType = NewPromotionType; 236 TagDecl::completeDefinition(); 237} 238 239FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, 240 SourceLocation L, 241 StringLiteral *Str) { 242 return new (C) FileScopeAsmDecl(DC, L, Str); 243} 244 245//===----------------------------------------------------------------------===// 246// NamedDecl Implementation 247//===----------------------------------------------------------------------===// 248 249static NamedDecl::Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) { 250 assert(D->getDeclContext()->getLookupContext()->isFileContext() && 251 "Not a name having namespace scope"); 252 ASTContext &Context = D->getASTContext(); 253 254 // C++ [basic.link]p3: 255 // A name having namespace scope (3.3.6) has internal linkage if it 256 // is the name of 257 // - an object, reference, function or function template that is 258 // explicitly declared static; or, 259 // (This bullet corresponds to C99 6.2.2p3.) 260 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { 261 // Explicitly declared static. 262 if (Var->getStorageClass() == VarDecl::Static) 263 return NamedDecl::InternalLinkage; 264 265 // - an object or reference that is explicitly declared const 266 // and neither explicitly declared extern nor previously 267 // declared to have external linkage; or 268 // (there is no equivalent in C99) 269 if (Context.getLangOptions().CPlusPlus && 270 Var->getType().isConstant(Context) && 271 Var->getStorageClass() != VarDecl::Extern && 272 Var->getStorageClass() != VarDecl::PrivateExtern) { 273 bool FoundExtern = false; 274 for (const VarDecl *PrevVar = Var->getPreviousDeclaration(); 275 PrevVar && !FoundExtern; 276 PrevVar = PrevVar->getPreviousDeclaration()) 277 if (PrevVar->getLinkage() == NamedDecl::ExternalLinkage) 278 FoundExtern = true; 279 280 if (!FoundExtern) 281 return NamedDecl::InternalLinkage; 282 } 283 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { 284 const FunctionDecl *Function = 0; 285 if (const FunctionTemplateDecl *FunTmpl 286 = dyn_cast<FunctionTemplateDecl>(D)) 287 Function = FunTmpl->getTemplatedDecl(); 288 else 289 Function = cast<FunctionDecl>(D); 290 291 // Explicitly declared static. 292 if (Function->getStorageClass() == FunctionDecl::Static) 293 return NamedDecl::InternalLinkage; 294 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { 295 // - a data member of an anonymous union. 296 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) 297 return NamedDecl::InternalLinkage; 298 } 299 300 // C++ [basic.link]p4: 301 302 // A name having namespace scope has external linkage if it is the 303 // name of 304 // 305 // - an object or reference, unless it has internal linkage; or 306 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { 307 if (!Context.getLangOptions().CPlusPlus && 308 (Var->getStorageClass() == VarDecl::Extern || 309 Var->getStorageClass() == VarDecl::PrivateExtern)) { 310 // C99 6.2.2p4: 311 // For an identifier declared with the storage-class specifier 312 // extern in a scope in which a prior declaration of that 313 // identifier is visible, if the prior declaration specifies 314 // internal or external linkage, the linkage of the identifier 315 // at the later declaration is the same as the linkage 316 // specified at the prior declaration. If no prior declaration 317 // is visible, or if the prior declaration specifies no 318 // linkage, then the identifier has external linkage. 319 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) { 320 if (NamedDecl::Linkage L = PrevVar->getLinkage()) 321 return L; 322 } 323 } 324 325 // C99 6.2.2p5: 326 // If the declaration of an identifier for an object has file 327 // scope and no storage-class specifier, its linkage is 328 // external. 329 return NamedDecl::ExternalLinkage; 330 } 331 332 // - a function, unless it has internal linkage; or 333 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 334 // C99 6.2.2p5: 335 // If the declaration of an identifier for a function has no 336 // storage-class specifier, its linkage is determined exactly 337 // as if it were declared with the storage-class specifier 338 // extern. 339 if (!Context.getLangOptions().CPlusPlus && 340 (Function->getStorageClass() == FunctionDecl::Extern || 341 Function->getStorageClass() == FunctionDecl::PrivateExtern || 342 Function->getStorageClass() == FunctionDecl::None)) { 343 // C99 6.2.2p4: 344 // For an identifier declared with the storage-class specifier 345 // extern in a scope in which a prior declaration of that 346 // identifier is visible, if the prior declaration specifies 347 // internal or external linkage, the linkage of the identifier 348 // at the later declaration is the same as the linkage 349 // specified at the prior declaration. If no prior declaration 350 // is visible, or if the prior declaration specifies no 351 // linkage, then the identifier has external linkage. 352 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) { 353 if (NamedDecl::Linkage L = PrevFunc->getLinkage()) 354 return L; 355 } 356 } 357 358 return NamedDecl::ExternalLinkage; 359 } 360 361 // - a named class (Clause 9), or an unnamed class defined in a 362 // typedef declaration in which the class has the typedef name 363 // for linkage purposes (7.1.3); or 364 // - a named enumeration (7.2), or an unnamed enumeration 365 // defined in a typedef declaration in which the enumeration 366 // has the typedef name for linkage purposes (7.1.3); or 367 if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) 368 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) 369 return NamedDecl::ExternalLinkage; 370 371 // - an enumerator belonging to an enumeration with external linkage; 372 if (isa<EnumConstantDecl>(D)) 373 if (cast<NamedDecl>(D->getDeclContext())->getLinkage() 374 == NamedDecl::ExternalLinkage) 375 return NamedDecl::ExternalLinkage; 376 377 // - a template, unless it is a function template that has 378 // internal linkage (Clause 14); 379 if (isa<TemplateDecl>(D)) 380 return NamedDecl::ExternalLinkage; 381 382 // - a namespace (7.3), unless it is declared within an unnamed 383 // namespace. 384 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) 385 return NamedDecl::ExternalLinkage; 386 387 return NamedDecl::NoLinkage; 388} 389 390NamedDecl::Linkage NamedDecl::getLinkage() const { 391 // Handle linkage for namespace-scope names. 392 if (getDeclContext()->getLookupContext()->isFileContext()) 393 if (Linkage L = getLinkageForNamespaceScopeDecl(this)) 394 return L; 395 396 // C++ [basic.link]p5: 397 // In addition, a member function, static data member, a named 398 // class or enumeration of class scope, or an unnamed class or 399 // enumeration defined in a class-scope typedef declaration such 400 // that the class or enumeration has the typedef name for linkage 401 // purposes (7.1.3), has external linkage if the name of the class 402 // has external linkage. 403 if (getDeclContext()->isRecord() && 404 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) || 405 (isa<TagDecl>(this) && 406 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl()))) && 407 cast<RecordDecl>(getDeclContext())->getLinkage() == ExternalLinkage) 408 return ExternalLinkage; 409 410 // C++ [basic.link]p6: 411 // The name of a function declared in block scope and the name of 412 // an object declared by a block scope extern declaration have 413 // linkage. If there is a visible declaration of an entity with 414 // linkage having the same name and type, ignoring entities 415 // declared outside the innermost enclosing namespace scope, the 416 // block scope declaration declares that same entity and receives 417 // the linkage of the previous declaration. If there is more than 418 // one such matching entity, the program is ill-formed. Otherwise, 419 // if no matching entity is found, the block scope entity receives 420 // external linkage. 421 if (getLexicalDeclContext()->isFunctionOrMethod()) { 422 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) { 423 if (Function->getPreviousDeclaration()) 424 if (Linkage L = Function->getPreviousDeclaration()->getLinkage()) 425 return L; 426 427 return ExternalLinkage; 428 } 429 430 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) 431 if (Var->getStorageClass() == VarDecl::Extern || 432 Var->getStorageClass() == VarDecl::PrivateExtern) { 433 if (Var->getPreviousDeclaration()) 434 if (Linkage L = Var->getPreviousDeclaration()->getLinkage()) 435 return L; 436 437 return ExternalLinkage; 438 } 439 } 440 441 // C++ [basic.link]p6: 442 // Names not covered by these rules have no linkage. 443 return NoLinkage; 444} 445 446std::string NamedDecl::getQualifiedNameAsString() const { 447 return getQualifiedNameAsString(getASTContext().getLangOptions()); 448} 449 450std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { 451 // FIXME: Collect contexts, then accumulate names to avoid unnecessary 452 // std::string thrashing. 453 std::vector<std::string> Names; 454 std::string QualName; 455 const DeclContext *Ctx = getDeclContext(); 456 457 if (Ctx->isFunctionOrMethod()) 458 return getNameAsString(); 459 460 while (Ctx) { 461 if (const ClassTemplateSpecializationDecl *Spec 462 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { 463 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 464 std::string TemplateArgsStr 465 = TemplateSpecializationType::PrintTemplateArgumentList( 466 TemplateArgs.getFlatArgumentList(), 467 TemplateArgs.flat_size(), 468 P); 469 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr); 470 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) { 471 if (ND->isAnonymousNamespace()) 472 Names.push_back("<anonymous namespace>"); 473 else 474 Names.push_back(ND->getNameAsString()); 475 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) { 476 if (!RD->getIdentifier()) { 477 std::string RecordString = "<anonymous "; 478 RecordString += RD->getKindName(); 479 RecordString += ">"; 480 Names.push_back(RecordString); 481 } else { 482 Names.push_back(RD->getNameAsString()); 483 } 484 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) { 485 std::string Proto = FD->getNameAsString(); 486 487 const FunctionProtoType *FT = 0; 488 if (FD->hasWrittenPrototype()) 489 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>()); 490 491 Proto += "("; 492 if (FT) { 493 llvm::raw_string_ostream POut(Proto); 494 unsigned NumParams = FD->getNumParams(); 495 for (unsigned i = 0; i < NumParams; ++i) { 496 if (i) 497 POut << ", "; 498 std::string Param; 499 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P); 500 POut << Param; 501 } 502 503 if (FT->isVariadic()) { 504 if (NumParams > 0) 505 POut << ", "; 506 POut << "..."; 507 } 508 } 509 Proto += ")"; 510 511 Names.push_back(Proto); 512 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) 513 Names.push_back(ND->getNameAsString()); 514 else 515 break; 516 517 Ctx = Ctx->getParent(); 518 } 519 520 std::vector<std::string>::reverse_iterator 521 I = Names.rbegin(), 522 End = Names.rend(); 523 524 for (; I!=End; ++I) 525 QualName += *I + "::"; 526 527 QualName += getNameAsString(); 528 529 return QualName; 530} 531 532bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { 533 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); 534 535 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. 536 // We want to keep it, unless it nominates same namespace. 537 if (getKind() == Decl::UsingDirective) { 538 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == 539 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); 540 } 541 542 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) 543 // For function declarations, we keep track of redeclarations. 544 return FD->getPreviousDeclaration() == OldD; 545 546 // For function templates, the underlying function declarations are linked. 547 if (const FunctionTemplateDecl *FunctionTemplate 548 = dyn_cast<FunctionTemplateDecl>(this)) 549 if (const FunctionTemplateDecl *OldFunctionTemplate 550 = dyn_cast<FunctionTemplateDecl>(OldD)) 551 return FunctionTemplate->getTemplatedDecl() 552 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); 553 554 // For method declarations, we keep track of redeclarations. 555 if (isa<ObjCMethodDecl>(this)) 556 return false; 557 558 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) 559 return true; 560 561 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) 562 return cast<UsingShadowDecl>(this)->getTargetDecl() == 563 cast<UsingShadowDecl>(OldD)->getTargetDecl(); 564 565 // For non-function declarations, if the declarations are of the 566 // same kind then this must be a redeclaration, or semantic analysis 567 // would not have given us the new declaration. 568 return this->getKind() == OldD->getKind(); 569} 570 571bool NamedDecl::hasLinkage() const { 572 return getLinkage() != NoLinkage; 573} 574 575NamedDecl *NamedDecl::getUnderlyingDecl() { 576 NamedDecl *ND = this; 577 while (true) { 578 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) 579 ND = UD->getTargetDecl(); 580 else if (ObjCCompatibleAliasDecl *AD 581 = dyn_cast<ObjCCompatibleAliasDecl>(ND)) 582 return AD->getClassInterface(); 583 else 584 return ND; 585 } 586} 587 588//===----------------------------------------------------------------------===// 589// DeclaratorDecl Implementation 590//===----------------------------------------------------------------------===// 591 592SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { 593 if (DeclInfo) { 594 TypeLoc TL = DeclInfo->getTypeLoc(); 595 while (true) { 596 TypeLoc NextTL = TL.getNextTypeLoc(); 597 if (!NextTL) 598 return TL.getSourceRange().getBegin(); 599 TL = NextTL; 600 } 601 } 602 return SourceLocation(); 603} 604 605//===----------------------------------------------------------------------===// 606// VarDecl Implementation 607//===----------------------------------------------------------------------===// 608 609VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 610 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, 611 StorageClass S) { 612 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S); 613} 614 615void VarDecl::Destroy(ASTContext& C) { 616 Expr *Init = getInit(); 617 if (Init) { 618 Init->Destroy(C); 619 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) { 620 Eval->~EvaluatedStmt(); 621 C.Deallocate(Eval); 622 } 623 } 624 this->~VarDecl(); 625 C.Deallocate((void *)this); 626} 627 628VarDecl::~VarDecl() { 629} 630 631SourceRange VarDecl::getSourceRange() const { 632 SourceLocation Start = getTypeSpecStartLoc(); 633 if (Start.isInvalid()) 634 Start = getLocation(); 635 636 if (getInit()) 637 return SourceRange(Start, getInit()->getLocEnd()); 638 return SourceRange(Start, getLocation()); 639} 640 641bool VarDecl::isOutOfLine() const { 642 if (!isStaticDataMember()) 643 return false; 644 645 if (Decl::isOutOfLine()) 646 return true; 647 648 // If this static data member was instantiated from a static data member of 649 // a class template, check whether that static data member was defined 650 // out-of-line. 651 if (VarDecl *VD = getInstantiatedFromStaticDataMember()) 652 return VD->isOutOfLine(); 653 654 return false; 655} 656 657VarDecl *VarDecl::getOutOfLineDefinition() { 658 if (!isStaticDataMember()) 659 return 0; 660 661 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); 662 RD != RDEnd; ++RD) { 663 if (RD->getLexicalDeclContext()->isFileContext()) 664 return *RD; 665 } 666 667 return 0; 668} 669 670VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { 671 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 672 return cast<VarDecl>(MSI->getInstantiatedFrom()); 673 674 return 0; 675} 676 677TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { 678 if (MemberSpecializationInfo *MSI 679 = getASTContext().getInstantiatedFromStaticDataMember(this)) 680 return MSI->getTemplateSpecializationKind(); 681 682 return TSK_Undeclared; 683} 684 685MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { 686 return getASTContext().getInstantiatedFromStaticDataMember(this); 687} 688 689void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 690 SourceLocation PointOfInstantiation) { 691 MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); 692 assert(MSI && "Not an instantiated static data member?"); 693 MSI->setTemplateSpecializationKind(TSK); 694 if (TSK != TSK_ExplicitSpecialization && 695 PointOfInstantiation.isValid() && 696 MSI->getPointOfInstantiation().isInvalid()) 697 MSI->setPointOfInstantiation(PointOfInstantiation); 698} 699 700bool VarDecl::isTentativeDefinition(ASTContext &Context) const { 701 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus) 702 return false; 703 704 const VarDecl *Def = 0; 705 return (!getDefinition(Def) && 706 (getStorageClass() == None || getStorageClass() == Static)); 707} 708 709const Expr *VarDecl::getDefinition(const VarDecl *&Def) const { 710 redecl_iterator I = redecls_begin(), E = redecls_end(); 711 while (I != E && !I->getInit()) 712 ++I; 713 714 if (I != E) { 715 Def = *I; 716 return I->getInit(); 717 } 718 return 0; 719} 720 721VarDecl *VarDecl::getCanonicalDecl() { 722 return getFirstDeclaration(); 723} 724 725//===----------------------------------------------------------------------===// 726// FunctionDecl Implementation 727//===----------------------------------------------------------------------===// 728 729void FunctionDecl::Destroy(ASTContext& C) { 730 if (Body && Body.isOffset()) 731 Body.get(C.getExternalSource())->Destroy(C); 732 733 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) 734 (*I)->Destroy(C); 735 736 FunctionTemplateSpecializationInfo *FTSInfo 737 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 738 if (FTSInfo) 739 C.Deallocate(FTSInfo); 740 741 MemberSpecializationInfo *MSInfo 742 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 743 if (MSInfo) 744 C.Deallocate(MSInfo); 745 746 C.Deallocate(ParamInfo); 747 748 Decl::Destroy(C); 749} 750 751void FunctionDecl::getNameForDiagnostic(std::string &S, 752 const PrintingPolicy &Policy, 753 bool Qualified) const { 754 NamedDecl::getNameForDiagnostic(S, Policy, Qualified); 755 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); 756 if (TemplateArgs) 757 S += TemplateSpecializationType::PrintTemplateArgumentList( 758 TemplateArgs->getFlatArgumentList(), 759 TemplateArgs->flat_size(), 760 Policy); 761 762} 763 764Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { 765 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 766 if (I->Body) { 767 Definition = *I; 768 return I->Body.get(getASTContext().getExternalSource()); 769 } 770 } 771 772 return 0; 773} 774 775void FunctionDecl::setBody(Stmt *B) { 776 Body = B; 777 if (B) 778 EndRangeLoc = B->getLocEnd(); 779} 780 781bool FunctionDecl::isMain() const { 782 ASTContext &Context = getASTContext(); 783 return !Context.getLangOptions().Freestanding && 784 getDeclContext()->getLookupContext()->isTranslationUnit() && 785 getIdentifier() && getIdentifier()->isStr("main"); 786} 787 788bool FunctionDecl::isExternC() const { 789 ASTContext &Context = getASTContext(); 790 // In C, any non-static, non-overloadable function has external 791 // linkage. 792 if (!Context.getLangOptions().CPlusPlus) 793 return getStorageClass() != Static && !getAttr<OverloadableAttr>(); 794 795 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); 796 DC = DC->getParent()) { 797 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { 798 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) 799 return getStorageClass() != Static && 800 !getAttr<OverloadableAttr>(); 801 802 break; 803 } 804 } 805 806 return false; 807} 808 809bool FunctionDecl::isGlobal() const { 810 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) 811 return Method->isStatic(); 812 813 if (getStorageClass() == Static) 814 return false; 815 816 for (const DeclContext *DC = getDeclContext(); 817 DC->isNamespace(); 818 DC = DC->getParent()) { 819 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { 820 if (!Namespace->getDeclName()) 821 return false; 822 break; 823 } 824 } 825 826 return true; 827} 828 829/// \brief Returns a value indicating whether this function 830/// corresponds to a builtin function. 831/// 832/// The function corresponds to a built-in function if it is 833/// declared at translation scope or within an extern "C" block and 834/// its name matches with the name of a builtin. The returned value 835/// will be 0 for functions that do not correspond to a builtin, a 836/// value of type \c Builtin::ID if in the target-independent range 837/// \c [1,Builtin::First), or a target-specific builtin value. 838unsigned FunctionDecl::getBuiltinID() const { 839 ASTContext &Context = getASTContext(); 840 if (!getIdentifier() || !getIdentifier()->getBuiltinID()) 841 return 0; 842 843 unsigned BuiltinID = getIdentifier()->getBuiltinID(); 844 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 845 return BuiltinID; 846 847 // This function has the name of a known C library 848 // function. Determine whether it actually refers to the C library 849 // function or whether it just has the same name. 850 851 // If this is a static function, it's not a builtin. 852 if (getStorageClass() == Static) 853 return 0; 854 855 // If this function is at translation-unit scope and we're not in 856 // C++, it refers to the C library function. 857 if (!Context.getLangOptions().CPlusPlus && 858 getDeclContext()->isTranslationUnit()) 859 return BuiltinID; 860 861 // If the function is in an extern "C" linkage specification and is 862 // not marked "overloadable", it's the real function. 863 if (isa<LinkageSpecDecl>(getDeclContext()) && 864 cast<LinkageSpecDecl>(getDeclContext())->getLanguage() 865 == LinkageSpecDecl::lang_c && 866 !getAttr<OverloadableAttr>()) 867 return BuiltinID; 868 869 // Not a builtin 870 return 0; 871} 872 873 874/// getNumParams - Return the number of parameters this function must have 875/// based on its FunctionType. This is the length of the PararmInfo array 876/// after it has been created. 877unsigned FunctionDecl::getNumParams() const { 878 const FunctionType *FT = getType()->getAs<FunctionType>(); 879 if (isa<FunctionNoProtoType>(FT)) 880 return 0; 881 return cast<FunctionProtoType>(FT)->getNumArgs(); 882 883} 884 885void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, 886 unsigned NumParams) { 887 assert(ParamInfo == 0 && "Already has param info!"); 888 assert(NumParams == getNumParams() && "Parameter count mismatch!"); 889 890 // Zero params -> null pointer. 891 if (NumParams) { 892 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); 893 ParamInfo = new (Mem) ParmVarDecl*[NumParams]; 894 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); 895 896 // Update source range. The check below allows us to set EndRangeLoc before 897 // setting the parameters. 898 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) 899 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); 900 } 901} 902 903/// getMinRequiredArguments - Returns the minimum number of arguments 904/// needed to call this function. This may be fewer than the number of 905/// function parameters, if some of the parameters have default 906/// arguments (in C++). 907unsigned FunctionDecl::getMinRequiredArguments() const { 908 unsigned NumRequiredArgs = getNumParams(); 909 while (NumRequiredArgs > 0 910 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) 911 --NumRequiredArgs; 912 913 return NumRequiredArgs; 914} 915 916bool FunctionDecl::isInlined() const { 917 // FIXME: This is not enough. Consider: 918 // 919 // inline void f(); 920 // void f() { } 921 // 922 // f is inlined, but does not have inline specified. 923 // To fix this we should add an 'inline' flag to FunctionDecl. 924 if (isInlineSpecified()) 925 return true; 926 927 if (isa<CXXMethodDecl>(this)) { 928 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) 929 return true; 930 } 931 932 switch (getTemplateSpecializationKind()) { 933 case TSK_Undeclared: 934 case TSK_ExplicitSpecialization: 935 return false; 936 937 case TSK_ImplicitInstantiation: 938 case TSK_ExplicitInstantiationDeclaration: 939 case TSK_ExplicitInstantiationDefinition: 940 // Handle below. 941 break; 942 } 943 944 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 945 Stmt *Pattern = 0; 946 if (PatternDecl) 947 Pattern = PatternDecl->getBody(PatternDecl); 948 949 if (Pattern && PatternDecl) 950 return PatternDecl->isInlined(); 951 952 return false; 953} 954 955/// \brief For an inline function definition in C or C++, determine whether the 956/// definition will be externally visible. 957/// 958/// Inline function definitions are always available for inlining optimizations. 959/// However, depending on the language dialect, declaration specifiers, and 960/// attributes, the definition of an inline function may or may not be 961/// "externally" visible to other translation units in the program. 962/// 963/// In C99, inline definitions are not externally visible by default. However, 964/// if even one of the global-scope declarations is marked "extern inline", the 965/// inline definition becomes externally visible (C99 6.7.4p6). 966/// 967/// In GNU89 mode, or if the gnu_inline attribute is attached to the function 968/// definition, we use the GNU semantics for inline, which are nearly the 969/// opposite of C99 semantics. In particular, "inline" by itself will create 970/// an externally visible symbol, but "extern inline" will not create an 971/// externally visible symbol. 972bool FunctionDecl::isInlineDefinitionExternallyVisible() const { 973 assert(isThisDeclarationADefinition() && "Must have the function definition"); 974 assert(isInlined() && "Function must be inline"); 975 ASTContext &Context = getASTContext(); 976 977 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { 978 // GNU inline semantics. Based on a number of examples, we came up with the 979 // following heuristic: if the "inline" keyword is present on a 980 // declaration of the function but "extern" is not present on that 981 // declaration, then the symbol is externally visible. Otherwise, the GNU 982 // "extern inline" semantics applies and the symbol is not externally 983 // visible. 984 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); 985 Redecl != RedeclEnd; 986 ++Redecl) { 987 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern) 988 return true; 989 } 990 991 // GNU "extern inline" semantics; no externally visible symbol. 992 return false; 993 } 994 995 // C99 6.7.4p6: 996 // [...] If all of the file scope declarations for a function in a 997 // translation unit include the inline function specifier without extern, 998 // then the definition in that translation unit is an inline definition. 999 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); 1000 Redecl != RedeclEnd; 1001 ++Redecl) { 1002 // Only consider file-scope declarations in this test. 1003 if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) 1004 continue; 1005 1006 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern) 1007 return true; // Not an inline definition 1008 } 1009 1010 // C99 6.7.4p6: 1011 // An inline definition does not provide an external definition for the 1012 // function, and does not forbid an external definition in another 1013 // translation unit. 1014 return false; 1015} 1016 1017void 1018FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { 1019 redeclarable_base::setPreviousDeclaration(PrevDecl); 1020 1021 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { 1022 FunctionTemplateDecl *PrevFunTmpl 1023 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; 1024 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); 1025 FunTmpl->setPreviousDeclaration(PrevFunTmpl); 1026 } 1027} 1028 1029const FunctionDecl *FunctionDecl::getCanonicalDecl() const { 1030 return getFirstDeclaration(); 1031} 1032 1033FunctionDecl *FunctionDecl::getCanonicalDecl() { 1034 return getFirstDeclaration(); 1035} 1036 1037/// getOverloadedOperator - Which C++ overloaded operator this 1038/// function represents, if any. 1039OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { 1040 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) 1041 return getDeclName().getCXXOverloadedOperator(); 1042 else 1043 return OO_None; 1044} 1045 1046/// getLiteralIdentifier - The literal suffix identifier this function 1047/// represents, if any. 1048const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { 1049 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) 1050 return getDeclName().getCXXLiteralIdentifier(); 1051 else 1052 return 0; 1053} 1054 1055FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { 1056 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) 1057 return cast<FunctionDecl>(Info->getInstantiatedFrom()); 1058 1059 return 0; 1060} 1061 1062MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { 1063 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 1064} 1065 1066void 1067FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD, 1068 TemplateSpecializationKind TSK) { 1069 assert(TemplateOrSpecialization.isNull() && 1070 "Member function is already a specialization"); 1071 MemberSpecializationInfo *Info 1072 = new (getASTContext()) MemberSpecializationInfo(FD, TSK); 1073 TemplateOrSpecialization = Info; 1074} 1075 1076bool FunctionDecl::isImplicitlyInstantiable() const { 1077 // If this function already has a definition or is invalid, it can't be 1078 // implicitly instantiated. 1079 if (isInvalidDecl() || getBody()) 1080 return false; 1081 1082 switch (getTemplateSpecializationKind()) { 1083 case TSK_Undeclared: 1084 case TSK_ExplicitSpecialization: 1085 case TSK_ExplicitInstantiationDefinition: 1086 return false; 1087 1088 case TSK_ImplicitInstantiation: 1089 return true; 1090 1091 case TSK_ExplicitInstantiationDeclaration: 1092 // Handled below. 1093 break; 1094 } 1095 1096 // Find the actual template from which we will instantiate. 1097 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 1098 Stmt *Pattern = 0; 1099 if (PatternDecl) 1100 Pattern = PatternDecl->getBody(PatternDecl); 1101 1102 // C++0x [temp.explicit]p9: 1103 // Except for inline functions, other explicit instantiation declarations 1104 // have the effect of suppressing the implicit instantiation of the entity 1105 // to which they refer. 1106 if (!Pattern || !PatternDecl) 1107 return true; 1108 1109 return PatternDecl->isInlined(); 1110} 1111 1112FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { 1113 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { 1114 while (Primary->getInstantiatedFromMemberTemplate()) { 1115 // If we have hit a point where the user provided a specialization of 1116 // this template, we're done looking. 1117 if (Primary->isMemberSpecialization()) 1118 break; 1119 1120 Primary = Primary->getInstantiatedFromMemberTemplate(); 1121 } 1122 1123 return Primary->getTemplatedDecl(); 1124 } 1125 1126 return getInstantiatedFromMemberFunction(); 1127} 1128 1129FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { 1130 if (FunctionTemplateSpecializationInfo *Info 1131 = TemplateOrSpecialization 1132 .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 1133 return Info->Template.getPointer(); 1134 } 1135 return 0; 1136} 1137 1138const TemplateArgumentList * 1139FunctionDecl::getTemplateSpecializationArgs() const { 1140 if (FunctionTemplateSpecializationInfo *Info 1141 = TemplateOrSpecialization 1142 .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 1143 return Info->TemplateArguments; 1144 } 1145 return 0; 1146} 1147 1148void 1149FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context, 1150 FunctionTemplateDecl *Template, 1151 const TemplateArgumentList *TemplateArgs, 1152 void *InsertPos, 1153 TemplateSpecializationKind TSK) { 1154 assert(TSK != TSK_Undeclared && 1155 "Must specify the type of function template specialization"); 1156 FunctionTemplateSpecializationInfo *Info 1157 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 1158 if (!Info) 1159 Info = new (Context) FunctionTemplateSpecializationInfo; 1160 1161 Info->Function = this; 1162 Info->Template.setPointer(Template); 1163 Info->Template.setInt(TSK - 1); 1164 Info->TemplateArguments = TemplateArgs; 1165 TemplateOrSpecialization = Info; 1166 1167 // Insert this function template specialization into the set of known 1168 // function template specializations. 1169 if (InsertPos) 1170 Template->getSpecializations().InsertNode(Info, InsertPos); 1171 else { 1172 // Try to insert the new node. If there is an existing node, remove it 1173 // first. 1174 FunctionTemplateSpecializationInfo *Existing 1175 = Template->getSpecializations().GetOrInsertNode(Info); 1176 if (Existing) { 1177 Template->getSpecializations().RemoveNode(Existing); 1178 Template->getSpecializations().GetOrInsertNode(Info); 1179 } 1180 } 1181} 1182 1183TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { 1184 // For a function template specialization, query the specialization 1185 // information object. 1186 FunctionTemplateSpecializationInfo *FTSInfo 1187 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 1188 if (FTSInfo) 1189 return FTSInfo->getTemplateSpecializationKind(); 1190 1191 MemberSpecializationInfo *MSInfo 1192 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 1193 if (MSInfo) 1194 return MSInfo->getTemplateSpecializationKind(); 1195 1196 return TSK_Undeclared; 1197} 1198 1199void 1200FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 1201 SourceLocation PointOfInstantiation) { 1202 if (FunctionTemplateSpecializationInfo *FTSInfo 1203 = TemplateOrSpecialization.dyn_cast< 1204 FunctionTemplateSpecializationInfo*>()) { 1205 FTSInfo->setTemplateSpecializationKind(TSK); 1206 if (TSK != TSK_ExplicitSpecialization && 1207 PointOfInstantiation.isValid() && 1208 FTSInfo->getPointOfInstantiation().isInvalid()) 1209 FTSInfo->setPointOfInstantiation(PointOfInstantiation); 1210 } else if (MemberSpecializationInfo *MSInfo 1211 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { 1212 MSInfo->setTemplateSpecializationKind(TSK); 1213 if (TSK != TSK_ExplicitSpecialization && 1214 PointOfInstantiation.isValid() && 1215 MSInfo->getPointOfInstantiation().isInvalid()) 1216 MSInfo->setPointOfInstantiation(PointOfInstantiation); 1217 } else 1218 assert(false && "Function cannot have a template specialization kind"); 1219} 1220 1221SourceLocation FunctionDecl::getPointOfInstantiation() const { 1222 if (FunctionTemplateSpecializationInfo *FTSInfo 1223 = TemplateOrSpecialization.dyn_cast< 1224 FunctionTemplateSpecializationInfo*>()) 1225 return FTSInfo->getPointOfInstantiation(); 1226 else if (MemberSpecializationInfo *MSInfo 1227 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) 1228 return MSInfo->getPointOfInstantiation(); 1229 1230 return SourceLocation(); 1231} 1232 1233bool FunctionDecl::isOutOfLine() const { 1234 if (Decl::isOutOfLine()) 1235 return true; 1236 1237 // If this function was instantiated from a member function of a 1238 // class template, check whether that member function was defined out-of-line. 1239 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { 1240 const FunctionDecl *Definition; 1241 if (FD->getBody(Definition)) 1242 return Definition->isOutOfLine(); 1243 } 1244 1245 // If this function was instantiated from a function template, 1246 // check whether that function template was defined out-of-line. 1247 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { 1248 const FunctionDecl *Definition; 1249 if (FunTmpl->getTemplatedDecl()->getBody(Definition)) 1250 return Definition->isOutOfLine(); 1251 } 1252 1253 return false; 1254} 1255 1256//===----------------------------------------------------------------------===// 1257// TagDecl Implementation 1258//===----------------------------------------------------------------------===// 1259 1260SourceRange TagDecl::getSourceRange() const { 1261 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); 1262 return SourceRange(TagKeywordLoc, E); 1263} 1264 1265TagDecl* TagDecl::getCanonicalDecl() { 1266 return getFirstDeclaration(); 1267} 1268 1269void TagDecl::startDefinition() { 1270 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { 1271 TagT->decl.setPointer(this); 1272 TagT->decl.setInt(1); 1273 } 1274} 1275 1276void TagDecl::completeDefinition() { 1277 IsDefinition = true; 1278 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { 1279 assert(TagT->decl.getPointer() == this && 1280 "Attempt to redefine a tag definition?"); 1281 TagT->decl.setInt(0); 1282 } 1283} 1284 1285TagDecl* TagDecl::getDefinition(ASTContext& C) const { 1286 if (isDefinition()) 1287 return const_cast<TagDecl *>(this); 1288 1289 for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); 1290 R != REnd; ++R) 1291 if (R->isDefinition()) 1292 return *R; 1293 1294 return 0; 1295} 1296 1297TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) { 1298 switch (TypeSpec) { 1299 default: llvm_unreachable("unexpected type specifier"); 1300 case DeclSpec::TST_struct: return TK_struct; 1301 case DeclSpec::TST_class: return TK_class; 1302 case DeclSpec::TST_union: return TK_union; 1303 case DeclSpec::TST_enum: return TK_enum; 1304 } 1305} 1306 1307//===----------------------------------------------------------------------===// 1308// RecordDecl Implementation 1309//===----------------------------------------------------------------------===// 1310 1311RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, 1312 IdentifierInfo *Id, RecordDecl *PrevDecl, 1313 SourceLocation TKL) 1314 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { 1315 HasFlexibleArrayMember = false; 1316 AnonymousStructOrUnion = false; 1317 HasObjectMember = false; 1318 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); 1319} 1320 1321RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, 1322 SourceLocation L, IdentifierInfo *Id, 1323 SourceLocation TKL, RecordDecl* PrevDecl) { 1324 1325 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); 1326 C.getTypeDeclType(R, PrevDecl); 1327 return R; 1328} 1329 1330RecordDecl::~RecordDecl() { 1331} 1332 1333void RecordDecl::Destroy(ASTContext& C) { 1334 TagDecl::Destroy(C); 1335} 1336 1337bool RecordDecl::isInjectedClassName() const { 1338 return isImplicit() && getDeclName() && getDeclContext()->isRecord() && 1339 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); 1340} 1341 1342/// completeDefinition - Notes that the definition of this type is now 1343/// complete. 1344void RecordDecl::completeDefinition(ASTContext& C) { 1345 assert(!isDefinition() && "Cannot redefine record!"); 1346 TagDecl::completeDefinition(); 1347} 1348 1349//===----------------------------------------------------------------------===// 1350// BlockDecl Implementation 1351//===----------------------------------------------------------------------===// 1352 1353BlockDecl::~BlockDecl() { 1354} 1355 1356void BlockDecl::Destroy(ASTContext& C) { 1357 if (Body) 1358 Body->Destroy(C); 1359 1360 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) 1361 (*I)->Destroy(C); 1362 1363 C.Deallocate(ParamInfo); 1364 Decl::Destroy(C); 1365} 1366 1367void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, 1368 unsigned NParms) { 1369 assert(ParamInfo == 0 && "Already has param info!"); 1370 1371 // Zero params -> null pointer. 1372 if (NParms) { 1373 NumParams = NParms; 1374 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); 1375 ParamInfo = new (Mem) ParmVarDecl*[NumParams]; 1376 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); 1377 } 1378} 1379 1380unsigned BlockDecl::getNumParams() const { 1381 return NumParams; 1382} 1383