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