Decl.cpp revision 201361
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 if (getInit()) 633 return SourceRange(getLocation(), getInit()->getLocEnd()); 634 return SourceRange(getLocation(), getLocation()); 635} 636 637bool VarDecl::isOutOfLine() const { 638 if (!isStaticDataMember()) 639 return false; 640 641 if (Decl::isOutOfLine()) 642 return true; 643 644 // If this static data member was instantiated from a static data member of 645 // a class template, check whether that static data member was defined 646 // out-of-line. 647 if (VarDecl *VD = getInstantiatedFromStaticDataMember()) 648 return VD->isOutOfLine(); 649 650 return false; 651} 652 653VarDecl *VarDecl::getOutOfLineDefinition() { 654 if (!isStaticDataMember()) 655 return 0; 656 657 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); 658 RD != RDEnd; ++RD) { 659 if (RD->getLexicalDeclContext()->isFileContext()) 660 return *RD; 661 } 662 663 return 0; 664} 665 666VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { 667 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 668 return cast<VarDecl>(MSI->getInstantiatedFrom()); 669 670 return 0; 671} 672 673TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { 674 if (MemberSpecializationInfo *MSI 675 = getASTContext().getInstantiatedFromStaticDataMember(this)) 676 return MSI->getTemplateSpecializationKind(); 677 678 return TSK_Undeclared; 679} 680 681MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { 682 return getASTContext().getInstantiatedFromStaticDataMember(this); 683} 684 685void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 686 SourceLocation PointOfInstantiation) { 687 MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); 688 assert(MSI && "Not an instantiated static data member?"); 689 MSI->setTemplateSpecializationKind(TSK); 690 if (TSK != TSK_ExplicitSpecialization && 691 PointOfInstantiation.isValid() && 692 MSI->getPointOfInstantiation().isInvalid()) 693 MSI->setPointOfInstantiation(PointOfInstantiation); 694} 695 696bool VarDecl::isTentativeDefinition(ASTContext &Context) const { 697 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus) 698 return false; 699 700 const VarDecl *Def = 0; 701 return (!getDefinition(Def) && 702 (getStorageClass() == None || getStorageClass() == Static)); 703} 704 705const Expr *VarDecl::getDefinition(const VarDecl *&Def) const { 706 redecl_iterator I = redecls_begin(), E = redecls_end(); 707 while (I != E && !I->getInit()) 708 ++I; 709 710 if (I != E) { 711 Def = *I; 712 return I->getInit(); 713 } 714 return 0; 715} 716 717VarDecl *VarDecl::getCanonicalDecl() { 718 return getFirstDeclaration(); 719} 720 721//===----------------------------------------------------------------------===// 722// FunctionDecl Implementation 723//===----------------------------------------------------------------------===// 724 725void FunctionDecl::Destroy(ASTContext& C) { 726 if (Body && Body.isOffset()) 727 Body.get(C.getExternalSource())->Destroy(C); 728 729 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) 730 (*I)->Destroy(C); 731 732 FunctionTemplateSpecializationInfo *FTSInfo 733 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 734 if (FTSInfo) 735 C.Deallocate(FTSInfo); 736 737 MemberSpecializationInfo *MSInfo 738 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 739 if (MSInfo) 740 C.Deallocate(MSInfo); 741 742 C.Deallocate(ParamInfo); 743 744 Decl::Destroy(C); 745} 746 747void FunctionDecl::getNameForDiagnostic(std::string &S, 748 const PrintingPolicy &Policy, 749 bool Qualified) const { 750 NamedDecl::getNameForDiagnostic(S, Policy, Qualified); 751 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); 752 if (TemplateArgs) 753 S += TemplateSpecializationType::PrintTemplateArgumentList( 754 TemplateArgs->getFlatArgumentList(), 755 TemplateArgs->flat_size(), 756 Policy); 757 758} 759 760Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { 761 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 762 if (I->Body) { 763 Definition = *I; 764 return I->Body.get(getASTContext().getExternalSource()); 765 } 766 } 767 768 return 0; 769} 770 771void FunctionDecl::setBody(Stmt *B) { 772 Body = B; 773 if (B) 774 EndRangeLoc = B->getLocEnd(); 775} 776 777bool FunctionDecl::isMain() const { 778 ASTContext &Context = getASTContext(); 779 return !Context.getLangOptions().Freestanding && 780 getDeclContext()->getLookupContext()->isTranslationUnit() && 781 getIdentifier() && getIdentifier()->isStr("main"); 782} 783 784bool FunctionDecl::isExternC() const { 785 ASTContext &Context = getASTContext(); 786 // In C, any non-static, non-overloadable function has external 787 // linkage. 788 if (!Context.getLangOptions().CPlusPlus) 789 return getStorageClass() != Static && !getAttr<OverloadableAttr>(); 790 791 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); 792 DC = DC->getParent()) { 793 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { 794 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) 795 return getStorageClass() != Static && 796 !getAttr<OverloadableAttr>(); 797 798 break; 799 } 800 } 801 802 return false; 803} 804 805bool FunctionDecl::isGlobal() const { 806 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) 807 return Method->isStatic(); 808 809 if (getStorageClass() == Static) 810 return false; 811 812 for (const DeclContext *DC = getDeclContext(); 813 DC->isNamespace(); 814 DC = DC->getParent()) { 815 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { 816 if (!Namespace->getDeclName()) 817 return false; 818 break; 819 } 820 } 821 822 return true; 823} 824 825/// \brief Returns a value indicating whether this function 826/// corresponds to a builtin function. 827/// 828/// The function corresponds to a built-in function if it is 829/// declared at translation scope or within an extern "C" block and 830/// its name matches with the name of a builtin. The returned value 831/// will be 0 for functions that do not correspond to a builtin, a 832/// value of type \c Builtin::ID if in the target-independent range 833/// \c [1,Builtin::First), or a target-specific builtin value. 834unsigned FunctionDecl::getBuiltinID() const { 835 ASTContext &Context = getASTContext(); 836 if (!getIdentifier() || !getIdentifier()->getBuiltinID()) 837 return 0; 838 839 unsigned BuiltinID = getIdentifier()->getBuiltinID(); 840 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 841 return BuiltinID; 842 843 // This function has the name of a known C library 844 // function. Determine whether it actually refers to the C library 845 // function or whether it just has the same name. 846 847 // If this is a static function, it's not a builtin. 848 if (getStorageClass() == Static) 849 return 0; 850 851 // If this function is at translation-unit scope and we're not in 852 // C++, it refers to the C library function. 853 if (!Context.getLangOptions().CPlusPlus && 854 getDeclContext()->isTranslationUnit()) 855 return BuiltinID; 856 857 // If the function is in an extern "C" linkage specification and is 858 // not marked "overloadable", it's the real function. 859 if (isa<LinkageSpecDecl>(getDeclContext()) && 860 cast<LinkageSpecDecl>(getDeclContext())->getLanguage() 861 == LinkageSpecDecl::lang_c && 862 !getAttr<OverloadableAttr>()) 863 return BuiltinID; 864 865 // Not a builtin 866 return 0; 867} 868 869 870/// getNumParams - Return the number of parameters this function must have 871/// based on its FunctionType. This is the length of the PararmInfo array 872/// after it has been created. 873unsigned FunctionDecl::getNumParams() const { 874 const FunctionType *FT = getType()->getAs<FunctionType>(); 875 if (isa<FunctionNoProtoType>(FT)) 876 return 0; 877 return cast<FunctionProtoType>(FT)->getNumArgs(); 878 879} 880 881void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, 882 unsigned NumParams) { 883 assert(ParamInfo == 0 && "Already has param info!"); 884 assert(NumParams == getNumParams() && "Parameter count mismatch!"); 885 886 // Zero params -> null pointer. 887 if (NumParams) { 888 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); 889 ParamInfo = new (Mem) ParmVarDecl*[NumParams]; 890 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); 891 892 // Update source range. The check below allows us to set EndRangeLoc before 893 // setting the parameters. 894 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) 895 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); 896 } 897} 898 899/// getMinRequiredArguments - Returns the minimum number of arguments 900/// needed to call this function. This may be fewer than the number of 901/// function parameters, if some of the parameters have default 902/// arguments (in C++). 903unsigned FunctionDecl::getMinRequiredArguments() const { 904 unsigned NumRequiredArgs = getNumParams(); 905 while (NumRequiredArgs > 0 906 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) 907 --NumRequiredArgs; 908 909 return NumRequiredArgs; 910} 911 912bool FunctionDecl::isInlined() const { 913 // FIXME: This is not enough. Consider: 914 // 915 // inline void f(); 916 // void f() { } 917 // 918 // f is inlined, but does not have inline specified. 919 // To fix this we should add an 'inline' flag to FunctionDecl. 920 if (isInlineSpecified()) 921 return true; 922 923 if (isa<CXXMethodDecl>(this)) { 924 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) 925 return true; 926 } 927 928 switch (getTemplateSpecializationKind()) { 929 case TSK_Undeclared: 930 case TSK_ExplicitSpecialization: 931 return false; 932 933 case TSK_ImplicitInstantiation: 934 case TSK_ExplicitInstantiationDeclaration: 935 case TSK_ExplicitInstantiationDefinition: 936 // Handle below. 937 break; 938 } 939 940 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 941 Stmt *Pattern = 0; 942 if (PatternDecl) 943 Pattern = PatternDecl->getBody(PatternDecl); 944 945 if (Pattern && PatternDecl) 946 return PatternDecl->isInlined(); 947 948 return false; 949} 950 951/// \brief For an inline function definition in C or C++, determine whether the 952/// definition will be externally visible. 953/// 954/// Inline function definitions are always available for inlining optimizations. 955/// However, depending on the language dialect, declaration specifiers, and 956/// attributes, the definition of an inline function may or may not be 957/// "externally" visible to other translation units in the program. 958/// 959/// In C99, inline definitions are not externally visible by default. However, 960/// if even one of the globa-scope declarations is marked "extern inline", the 961/// inline definition becomes externally visible (C99 6.7.4p6). 962/// 963/// In GNU89 mode, or if the gnu_inline attribute is attached to the function 964/// definition, we use the GNU semantics for inline, which are nearly the 965/// opposite of C99 semantics. In particular, "inline" by itself will create 966/// an externally visible symbol, but "extern inline" will not create an 967/// externally visible symbol. 968bool FunctionDecl::isInlineDefinitionExternallyVisible() const { 969 assert(isThisDeclarationADefinition() && "Must have the function definition"); 970 assert(isInlined() && "Function must be inline"); 971 ASTContext &Context = getASTContext(); 972 973 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { 974 // GNU inline semantics. Based on a number of examples, we came up with the 975 // following heuristic: if the "inline" keyword is present on a 976 // declaration of the function but "extern" is not present on that 977 // declaration, then the symbol is externally visible. Otherwise, the GNU 978 // "extern inline" semantics applies and the symbol is not externally 979 // visible. 980 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); 981 Redecl != RedeclEnd; 982 ++Redecl) { 983 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern) 984 return true; 985 } 986 987 // GNU "extern inline" semantics; no externally visible symbol. 988 return false; 989 } 990 991 // C99 6.7.4p6: 992 // [...] If all of the file scope declarations for a function in a 993 // translation unit include the inline function specifier without extern, 994 // then the definition in that translation unit is an inline definition. 995 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); 996 Redecl != RedeclEnd; 997 ++Redecl) { 998 // Only consider file-scope declarations in this test. 999 if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) 1000 continue; 1001 1002 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern) 1003 return true; // Not an inline definition 1004 } 1005 1006 // C99 6.7.4p6: 1007 // An inline definition does not provide an external definition for the 1008 // function, and does not forbid an external definition in another 1009 // translation unit. 1010 return false; 1011} 1012 1013void 1014FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { 1015 redeclarable_base::setPreviousDeclaration(PrevDecl); 1016 1017 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { 1018 FunctionTemplateDecl *PrevFunTmpl 1019 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; 1020 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); 1021 FunTmpl->setPreviousDeclaration(PrevFunTmpl); 1022 } 1023} 1024 1025const FunctionDecl *FunctionDecl::getCanonicalDecl() const { 1026 return getFirstDeclaration(); 1027} 1028 1029FunctionDecl *FunctionDecl::getCanonicalDecl() { 1030 return getFirstDeclaration(); 1031} 1032 1033/// getOverloadedOperator - Which C++ overloaded operator this 1034/// function represents, if any. 1035OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { 1036 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) 1037 return getDeclName().getCXXOverloadedOperator(); 1038 else 1039 return OO_None; 1040} 1041 1042FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { 1043 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) 1044 return cast<FunctionDecl>(Info->getInstantiatedFrom()); 1045 1046 return 0; 1047} 1048 1049MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { 1050 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 1051} 1052 1053void 1054FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD, 1055 TemplateSpecializationKind TSK) { 1056 assert(TemplateOrSpecialization.isNull() && 1057 "Member function is already a specialization"); 1058 MemberSpecializationInfo *Info 1059 = new (getASTContext()) MemberSpecializationInfo(FD, TSK); 1060 TemplateOrSpecialization = Info; 1061} 1062 1063bool FunctionDecl::isImplicitlyInstantiable() const { 1064 // If this function already has a definition or is invalid, it can't be 1065 // implicitly instantiated. 1066 if (isInvalidDecl() || getBody()) 1067 return false; 1068 1069 switch (getTemplateSpecializationKind()) { 1070 case TSK_Undeclared: 1071 case TSK_ExplicitSpecialization: 1072 case TSK_ExplicitInstantiationDefinition: 1073 return false; 1074 1075 case TSK_ImplicitInstantiation: 1076 return true; 1077 1078 case TSK_ExplicitInstantiationDeclaration: 1079 // Handled below. 1080 break; 1081 } 1082 1083 // Find the actual template from which we will instantiate. 1084 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 1085 Stmt *Pattern = 0; 1086 if (PatternDecl) 1087 Pattern = PatternDecl->getBody(PatternDecl); 1088 1089 // C++0x [temp.explicit]p9: 1090 // Except for inline functions, other explicit instantiation declarations 1091 // have the effect of suppressing the implicit instantiation of the entity 1092 // to which they refer. 1093 if (!Pattern || !PatternDecl) 1094 return true; 1095 1096 return PatternDecl->isInlined(); 1097} 1098 1099FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { 1100 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { 1101 while (Primary->getInstantiatedFromMemberTemplate()) { 1102 // If we have hit a point where the user provided a specialization of 1103 // this template, we're done looking. 1104 if (Primary->isMemberSpecialization()) 1105 break; 1106 1107 Primary = Primary->getInstantiatedFromMemberTemplate(); 1108 } 1109 1110 return Primary->getTemplatedDecl(); 1111 } 1112 1113 return getInstantiatedFromMemberFunction(); 1114} 1115 1116FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { 1117 if (FunctionTemplateSpecializationInfo *Info 1118 = TemplateOrSpecialization 1119 .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 1120 return Info->Template.getPointer(); 1121 } 1122 return 0; 1123} 1124 1125const TemplateArgumentList * 1126FunctionDecl::getTemplateSpecializationArgs() const { 1127 if (FunctionTemplateSpecializationInfo *Info 1128 = TemplateOrSpecialization 1129 .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 1130 return Info->TemplateArguments; 1131 } 1132 return 0; 1133} 1134 1135void 1136FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context, 1137 FunctionTemplateDecl *Template, 1138 const TemplateArgumentList *TemplateArgs, 1139 void *InsertPos, 1140 TemplateSpecializationKind TSK) { 1141 assert(TSK != TSK_Undeclared && 1142 "Must specify the type of function template specialization"); 1143 FunctionTemplateSpecializationInfo *Info 1144 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 1145 if (!Info) 1146 Info = new (Context) FunctionTemplateSpecializationInfo; 1147 1148 Info->Function = this; 1149 Info->Template.setPointer(Template); 1150 Info->Template.setInt(TSK - 1); 1151 Info->TemplateArguments = TemplateArgs; 1152 TemplateOrSpecialization = Info; 1153 1154 // Insert this function template specialization into the set of known 1155 // function template specializations. 1156 if (InsertPos) 1157 Template->getSpecializations().InsertNode(Info, InsertPos); 1158 else { 1159 // Try to insert the new node. If there is an existing node, remove it 1160 // first. 1161 FunctionTemplateSpecializationInfo *Existing 1162 = Template->getSpecializations().GetOrInsertNode(Info); 1163 if (Existing) { 1164 Template->getSpecializations().RemoveNode(Existing); 1165 Template->getSpecializations().GetOrInsertNode(Info); 1166 } 1167 } 1168} 1169 1170TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { 1171 // For a function template specialization, query the specialization 1172 // information object. 1173 FunctionTemplateSpecializationInfo *FTSInfo 1174 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 1175 if (FTSInfo) 1176 return FTSInfo->getTemplateSpecializationKind(); 1177 1178 MemberSpecializationInfo *MSInfo 1179 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 1180 if (MSInfo) 1181 return MSInfo->getTemplateSpecializationKind(); 1182 1183 return TSK_Undeclared; 1184} 1185 1186void 1187FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 1188 SourceLocation PointOfInstantiation) { 1189 if (FunctionTemplateSpecializationInfo *FTSInfo 1190 = TemplateOrSpecialization.dyn_cast< 1191 FunctionTemplateSpecializationInfo*>()) { 1192 FTSInfo->setTemplateSpecializationKind(TSK); 1193 if (TSK != TSK_ExplicitSpecialization && 1194 PointOfInstantiation.isValid() && 1195 FTSInfo->getPointOfInstantiation().isInvalid()) 1196 FTSInfo->setPointOfInstantiation(PointOfInstantiation); 1197 } else if (MemberSpecializationInfo *MSInfo 1198 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { 1199 MSInfo->setTemplateSpecializationKind(TSK); 1200 if (TSK != TSK_ExplicitSpecialization && 1201 PointOfInstantiation.isValid() && 1202 MSInfo->getPointOfInstantiation().isInvalid()) 1203 MSInfo->setPointOfInstantiation(PointOfInstantiation); 1204 } else 1205 assert(false && "Function cannot have a template specialization kind"); 1206} 1207 1208SourceLocation FunctionDecl::getPointOfInstantiation() const { 1209 if (FunctionTemplateSpecializationInfo *FTSInfo 1210 = TemplateOrSpecialization.dyn_cast< 1211 FunctionTemplateSpecializationInfo*>()) 1212 return FTSInfo->getPointOfInstantiation(); 1213 else if (MemberSpecializationInfo *MSInfo 1214 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) 1215 return MSInfo->getPointOfInstantiation(); 1216 1217 return SourceLocation(); 1218} 1219 1220bool FunctionDecl::isOutOfLine() const { 1221 if (Decl::isOutOfLine()) 1222 return true; 1223 1224 // If this function was instantiated from a member function of a 1225 // class template, check whether that member function was defined out-of-line. 1226 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { 1227 const FunctionDecl *Definition; 1228 if (FD->getBody(Definition)) 1229 return Definition->isOutOfLine(); 1230 } 1231 1232 // If this function was instantiated from a function template, 1233 // check whether that function template was defined out-of-line. 1234 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { 1235 const FunctionDecl *Definition; 1236 if (FunTmpl->getTemplatedDecl()->getBody(Definition)) 1237 return Definition->isOutOfLine(); 1238 } 1239 1240 return false; 1241} 1242 1243//===----------------------------------------------------------------------===// 1244// TagDecl Implementation 1245//===----------------------------------------------------------------------===// 1246 1247SourceRange TagDecl::getSourceRange() const { 1248 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); 1249 return SourceRange(TagKeywordLoc, E); 1250} 1251 1252TagDecl* TagDecl::getCanonicalDecl() { 1253 return getFirstDeclaration(); 1254} 1255 1256void TagDecl::startDefinition() { 1257 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { 1258 TagT->decl.setPointer(this); 1259 TagT->decl.setInt(1); 1260 } 1261} 1262 1263void TagDecl::completeDefinition() { 1264 IsDefinition = true; 1265 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { 1266 assert(TagT->decl.getPointer() == this && 1267 "Attempt to redefine a tag definition?"); 1268 TagT->decl.setInt(0); 1269 } 1270} 1271 1272TagDecl* TagDecl::getDefinition(ASTContext& C) const { 1273 if (isDefinition()) 1274 return const_cast<TagDecl *>(this); 1275 1276 for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); 1277 R != REnd; ++R) 1278 if (R->isDefinition()) 1279 return *R; 1280 1281 return 0; 1282} 1283 1284TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) { 1285 switch (TypeSpec) { 1286 default: llvm_unreachable("unexpected type specifier"); 1287 case DeclSpec::TST_struct: return TK_struct; 1288 case DeclSpec::TST_class: return TK_class; 1289 case DeclSpec::TST_union: return TK_union; 1290 case DeclSpec::TST_enum: return TK_enum; 1291 } 1292} 1293 1294//===----------------------------------------------------------------------===// 1295// RecordDecl Implementation 1296//===----------------------------------------------------------------------===// 1297 1298RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, 1299 IdentifierInfo *Id, RecordDecl *PrevDecl, 1300 SourceLocation TKL) 1301 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { 1302 HasFlexibleArrayMember = false; 1303 AnonymousStructOrUnion = false; 1304 HasObjectMember = false; 1305 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); 1306} 1307 1308RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, 1309 SourceLocation L, IdentifierInfo *Id, 1310 SourceLocation TKL, RecordDecl* PrevDecl) { 1311 1312 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); 1313 C.getTypeDeclType(R, PrevDecl); 1314 return R; 1315} 1316 1317RecordDecl::~RecordDecl() { 1318} 1319 1320void RecordDecl::Destroy(ASTContext& C) { 1321 TagDecl::Destroy(C); 1322} 1323 1324bool RecordDecl::isInjectedClassName() const { 1325 return isImplicit() && getDeclName() && getDeclContext()->isRecord() && 1326 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); 1327} 1328 1329/// completeDefinition - Notes that the definition of this type is now 1330/// complete. 1331void RecordDecl::completeDefinition(ASTContext& C) { 1332 assert(!isDefinition() && "Cannot redefine record!"); 1333 TagDecl::completeDefinition(); 1334} 1335 1336//===----------------------------------------------------------------------===// 1337// BlockDecl Implementation 1338//===----------------------------------------------------------------------===// 1339 1340BlockDecl::~BlockDecl() { 1341} 1342 1343void BlockDecl::Destroy(ASTContext& C) { 1344 if (Body) 1345 Body->Destroy(C); 1346 1347 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) 1348 (*I)->Destroy(C); 1349 1350 C.Deallocate(ParamInfo); 1351 Decl::Destroy(C); 1352} 1353 1354void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, 1355 unsigned NParms) { 1356 assert(ParamInfo == 0 && "Already has param info!"); 1357 1358 // Zero params -> null pointer. 1359 if (NParms) { 1360 NumParams = NParms; 1361 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); 1362 ParamInfo = new (Mem) ParmVarDecl*[NumParams]; 1363 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); 1364 } 1365} 1366 1367unsigned BlockDecl::getNumParams() const { 1368 return NumParams; 1369} 1370