Decl.cpp revision ffb945ffb5d29b80fd93649c3572b6d87abce3fc
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/AST/ASTMutationListener.h" 25#include "clang/Basic/Builtins.h" 26#include "clang/Basic/IdentifierTable.h" 27#include "clang/Basic/Specifiers.h" 28#include "clang/Basic/TargetInfo.h" 29#include "llvm/Support/ErrorHandling.h" 30 31using namespace clang; 32 33//===----------------------------------------------------------------------===// 34// NamedDecl Implementation 35//===----------------------------------------------------------------------===// 36 37static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) { 38 // If this declaration has an explicit visibility attribute, use it. 39 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) { 40 switch (A->getVisibility()) { 41 case VisibilityAttr::Default: 42 return DefaultVisibility; 43 case VisibilityAttr::Hidden: 44 return HiddenVisibility; 45 case VisibilityAttr::Protected: 46 return ProtectedVisibility; 47 } 48 49 return DefaultVisibility; 50 } 51 52 // If we're on Mac OS X, an 'availability' for Mac OS X attribute 53 // implies visibility(default). 54 if (D->getASTContext().Target.getTriple().isOSDarwin()) { 55 for (specific_attr_iterator<AvailabilityAttr> 56 A = D->specific_attr_begin<AvailabilityAttr>(), 57 AEnd = D->specific_attr_end<AvailabilityAttr>(); 58 A != AEnd; ++A) 59 if ((*A)->getPlatform()->getName().equals("macosx")) 60 return DefaultVisibility; 61 } 62 63 return llvm::Optional<Visibility>(); 64} 65 66typedef NamedDecl::LinkageInfo LinkageInfo; 67typedef std::pair<Linkage,Visibility> LVPair; 68 69static LVPair merge(LVPair L, LVPair R) { 70 return LVPair(minLinkage(L.first, R.first), 71 minVisibility(L.second, R.second)); 72} 73 74static LVPair merge(LVPair L, LinkageInfo R) { 75 return LVPair(minLinkage(L.first, R.linkage()), 76 minVisibility(L.second, R.visibility())); 77} 78 79namespace { 80/// Flags controlling the computation of linkage and visibility. 81struct LVFlags { 82 bool ConsiderGlobalVisibility; 83 bool ConsiderVisibilityAttributes; 84 bool ConsiderTemplateParameterTypes; 85 86 LVFlags() : ConsiderGlobalVisibility(true), 87 ConsiderVisibilityAttributes(true), 88 ConsiderTemplateParameterTypes(true) { 89 } 90 91 /// \brief Returns a set of flags that is only useful for computing the 92 /// linkage, not the visibility, of a declaration. 93 static LVFlags CreateOnlyDeclLinkage() { 94 LVFlags F; 95 F.ConsiderGlobalVisibility = false; 96 F.ConsiderVisibilityAttributes = false; 97 F.ConsiderTemplateParameterTypes = false; 98 return F; 99 } 100 101 /// Returns a set of flags, otherwise based on these, which ignores 102 /// off all sources of visibility except template arguments. 103 LVFlags onlyTemplateVisibility() const { 104 LVFlags F = *this; 105 F.ConsiderGlobalVisibility = false; 106 F.ConsiderVisibilityAttributes = false; 107 F.ConsiderTemplateParameterTypes = false; 108 return F; 109 } 110}; 111} // end anonymous namespace 112 113/// \brief Get the most restrictive linkage for the types in the given 114/// template parameter list. 115static LVPair 116getLVForTemplateParameterList(const TemplateParameterList *Params) { 117 LVPair LV(ExternalLinkage, DefaultVisibility); 118 for (TemplateParameterList::const_iterator P = Params->begin(), 119 PEnd = Params->end(); 120 P != PEnd; ++P) { 121 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { 122 if (NTTP->isExpandedParameterPack()) { 123 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { 124 QualType T = NTTP->getExpansionType(I); 125 if (!T->isDependentType()) 126 LV = merge(LV, T->getLinkageAndVisibility()); 127 } 128 continue; 129 } 130 131 if (!NTTP->getType()->isDependentType()) { 132 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility()); 133 continue; 134 } 135 } 136 137 if (TemplateTemplateParmDecl *TTP 138 = dyn_cast<TemplateTemplateParmDecl>(*P)) { 139 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters())); 140 } 141 } 142 143 return LV; 144} 145 146/// getLVForDecl - Get the linkage and visibility for the given declaration. 147static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F); 148 149/// \brief Get the most restrictive linkage for the types and 150/// declarations in the given template argument list. 151static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args, 152 unsigned NumArgs, 153 LVFlags &F) { 154 LVPair LV(ExternalLinkage, DefaultVisibility); 155 156 for (unsigned I = 0; I != NumArgs; ++I) { 157 switch (Args[I].getKind()) { 158 case TemplateArgument::Null: 159 case TemplateArgument::Integral: 160 case TemplateArgument::Expression: 161 break; 162 163 case TemplateArgument::Type: 164 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility()); 165 break; 166 167 case TemplateArgument::Declaration: 168 // The decl can validly be null as the representation of nullptr 169 // arguments, valid only in C++0x. 170 if (Decl *D = Args[I].getAsDecl()) { 171 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) 172 LV = merge(LV, getLVForDecl(ND, F)); 173 } 174 break; 175 176 case TemplateArgument::Template: 177 case TemplateArgument::TemplateExpansion: 178 if (TemplateDecl *Template 179 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl()) 180 LV = merge(LV, getLVForDecl(Template, F)); 181 break; 182 183 case TemplateArgument::Pack: 184 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(), 185 Args[I].pack_size(), 186 F)); 187 break; 188 } 189 } 190 191 return LV; 192} 193 194static LVPair 195getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, 196 LVFlags &F) { 197 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F); 198} 199 200static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) { 201 assert(D->getDeclContext()->getRedeclContext()->isFileContext() && 202 "Not a name having namespace scope"); 203 ASTContext &Context = D->getASTContext(); 204 205 // C++ [basic.link]p3: 206 // A name having namespace scope (3.3.6) has internal linkage if it 207 // is the name of 208 // - an object, reference, function or function template that is 209 // explicitly declared static; or, 210 // (This bullet corresponds to C99 6.2.2p3.) 211 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { 212 // Explicitly declared static. 213 if (Var->getStorageClass() == SC_Static) 214 return LinkageInfo::internal(); 215 216 // - an object or reference that is explicitly declared const 217 // and neither explicitly declared extern nor previously 218 // declared to have external linkage; or 219 // (there is no equivalent in C99) 220 if (Context.getLangOptions().CPlusPlus && 221 Var->getType().isConstant(Context) && 222 Var->getStorageClass() != SC_Extern && 223 Var->getStorageClass() != SC_PrivateExtern) { 224 bool FoundExtern = false; 225 for (const VarDecl *PrevVar = Var->getPreviousDeclaration(); 226 PrevVar && !FoundExtern; 227 PrevVar = PrevVar->getPreviousDeclaration()) 228 if (isExternalLinkage(PrevVar->getLinkage())) 229 FoundExtern = true; 230 231 if (!FoundExtern) 232 return LinkageInfo::internal(); 233 } 234 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { 235 // C++ [temp]p4: 236 // A non-member function template can have internal linkage; any 237 // other template name shall have external linkage. 238 const FunctionDecl *Function = 0; 239 if (const FunctionTemplateDecl *FunTmpl 240 = dyn_cast<FunctionTemplateDecl>(D)) 241 Function = FunTmpl->getTemplatedDecl(); 242 else 243 Function = cast<FunctionDecl>(D); 244 245 // Explicitly declared static. 246 if (Function->getStorageClass() == SC_Static) 247 return LinkageInfo(InternalLinkage, DefaultVisibility, false); 248 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { 249 // - a data member of an anonymous union. 250 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) 251 return LinkageInfo::internal(); 252 } 253 254 if (D->isInAnonymousNamespace()) { 255 const VarDecl *Var = dyn_cast<VarDecl>(D); 256 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D); 257 if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC())) 258 return LinkageInfo::uniqueExternal(); 259 } 260 261 // Set up the defaults. 262 263 // C99 6.2.2p5: 264 // If the declaration of an identifier for an object has file 265 // scope and no storage-class specifier, its linkage is 266 // external. 267 LinkageInfo LV; 268 269 if (F.ConsiderVisibilityAttributes) { 270 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) { 271 LV.setVisibility(*Vis, true); 272 F.ConsiderGlobalVisibility = false; 273 } else { 274 // If we're declared in a namespace with a visibility attribute, 275 // use that namespace's visibility, but don't call it explicit. 276 for (const DeclContext *DC = D->getDeclContext(); 277 !isa<TranslationUnitDecl>(DC); 278 DC = DC->getParent()) { 279 if (!isa<NamespaceDecl>(DC)) continue; 280 if (llvm::Optional<Visibility> Vis 281 = cast<NamespaceDecl>(DC)->getExplicitVisibility()) { 282 LV.setVisibility(*Vis, false); 283 F.ConsiderGlobalVisibility = false; 284 break; 285 } 286 } 287 } 288 } 289 290 // C++ [basic.link]p4: 291 292 // A name having namespace scope has external linkage if it is the 293 // name of 294 // 295 // - an object or reference, unless it has internal linkage; or 296 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { 297 // GCC applies the following optimization to variables and static 298 // data members, but not to functions: 299 // 300 // Modify the variable's LV by the LV of its type unless this is 301 // C or extern "C". This follows from [basic.link]p9: 302 // A type without linkage shall not be used as the type of a 303 // variable or function with external linkage unless 304 // - the entity has C language linkage, or 305 // - the entity is declared within an unnamed namespace, or 306 // - the entity is not used or is defined in the same 307 // translation unit. 308 // and [basic.link]p10: 309 // ...the types specified by all declarations referring to a 310 // given variable or function shall be identical... 311 // C does not have an equivalent rule. 312 // 313 // Ignore this if we've got an explicit attribute; the user 314 // probably knows what they're doing. 315 // 316 // Note that we don't want to make the variable non-external 317 // because of this, but unique-external linkage suits us. 318 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) { 319 LVPair TypeLV = Var->getType()->getLinkageAndVisibility(); 320 if (TypeLV.first != ExternalLinkage) 321 return LinkageInfo::uniqueExternal(); 322 if (!LV.visibilityExplicit()) 323 LV.mergeVisibility(TypeLV.second); 324 } 325 326 if (Var->getStorageClass() == SC_PrivateExtern) 327 LV.setVisibility(HiddenVisibility, true); 328 329 if (!Context.getLangOptions().CPlusPlus && 330 (Var->getStorageClass() == SC_Extern || 331 Var->getStorageClass() == SC_PrivateExtern)) { 332 333 // C99 6.2.2p4: 334 // For an identifier declared with the storage-class specifier 335 // extern in a scope in which a prior declaration of that 336 // identifier is visible, if the prior declaration specifies 337 // internal or external linkage, the linkage of the identifier 338 // at the later declaration is the same as the linkage 339 // specified at the prior declaration. If no prior declaration 340 // is visible, or if the prior declaration specifies no 341 // linkage, then the identifier has external linkage. 342 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) { 343 LinkageInfo PrevLV = getLVForDecl(PrevVar, F); 344 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); 345 LV.mergeVisibility(PrevLV); 346 } 347 } 348 349 // - a function, unless it has internal linkage; or 350 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 351 // In theory, we can modify the function's LV by the LV of its 352 // type unless it has C linkage (see comment above about variables 353 // for justification). In practice, GCC doesn't do this, so it's 354 // just too painful to make work. 355 356 if (Function->getStorageClass() == SC_PrivateExtern) 357 LV.setVisibility(HiddenVisibility, true); 358 359 // C99 6.2.2p5: 360 // If the declaration of an identifier for a function has no 361 // storage-class specifier, its linkage is determined exactly 362 // as if it were declared with the storage-class specifier 363 // extern. 364 if (!Context.getLangOptions().CPlusPlus && 365 (Function->getStorageClass() == SC_Extern || 366 Function->getStorageClass() == SC_PrivateExtern || 367 Function->getStorageClass() == SC_None)) { 368 // C99 6.2.2p4: 369 // For an identifier declared with the storage-class specifier 370 // extern in a scope in which a prior declaration of that 371 // identifier is visible, if the prior declaration specifies 372 // internal or external linkage, the linkage of the identifier 373 // at the later declaration is the same as the linkage 374 // specified at the prior declaration. If no prior declaration 375 // is visible, or if the prior declaration specifies no 376 // linkage, then the identifier has external linkage. 377 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) { 378 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F); 379 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); 380 LV.mergeVisibility(PrevLV); 381 } 382 } 383 384 // In C++, then if the type of the function uses a type with 385 // unique-external linkage, it's not legally usable from outside 386 // this translation unit. However, we should use the C linkage 387 // rules instead for extern "C" declarations. 388 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() && 389 Function->getType()->getLinkage() == UniqueExternalLinkage) 390 return LinkageInfo::uniqueExternal(); 391 392 if (FunctionTemplateSpecializationInfo *SpecInfo 393 = Function->getTemplateSpecializationInfo()) { 394 LV.merge(getLVForDecl(SpecInfo->getTemplate(), 395 F.onlyTemplateVisibility())); 396 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments; 397 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F)); 398 } 399 400 // - a named class (Clause 9), or an unnamed class defined in a 401 // typedef declaration in which the class has the typedef name 402 // for linkage purposes (7.1.3); or 403 // - a named enumeration (7.2), or an unnamed enumeration 404 // defined in a typedef declaration in which the enumeration 405 // has the typedef name for linkage purposes (7.1.3); or 406 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) { 407 // Unnamed tags have no linkage. 408 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) 409 return LinkageInfo::none(); 410 411 // If this is a class template specialization, consider the 412 // linkage of the template and template arguments. 413 if (const ClassTemplateSpecializationDecl *Spec 414 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { 415 // From the template. 416 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(), 417 F.onlyTemplateVisibility())); 418 419 // The arguments at which the template was instantiated. 420 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 421 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F)); 422 } 423 424 // Consider -fvisibility unless the type has C linkage. 425 if (F.ConsiderGlobalVisibility) 426 F.ConsiderGlobalVisibility = 427 (Context.getLangOptions().CPlusPlus && 428 !Tag->getDeclContext()->isExternCContext()); 429 430 // - an enumerator belonging to an enumeration with external linkage; 431 } else if (isa<EnumConstantDecl>(D)) { 432 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F); 433 if (!isExternalLinkage(EnumLV.linkage())) 434 return LinkageInfo::none(); 435 LV.merge(EnumLV); 436 437 // - a template, unless it is a function template that has 438 // internal linkage (Clause 14); 439 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) { 440 if (F.ConsiderTemplateParameterTypes) 441 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters())); 442 443 // - a namespace (7.3), unless it is declared within an unnamed 444 // namespace. 445 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) { 446 return LV; 447 448 // By extension, we assign external linkage to Objective-C 449 // interfaces. 450 } else if (isa<ObjCInterfaceDecl>(D)) { 451 // fallout 452 453 // Everything not covered here has no linkage. 454 } else { 455 return LinkageInfo::none(); 456 } 457 458 // If we ended up with non-external linkage, visibility should 459 // always be default. 460 if (LV.linkage() != ExternalLinkage) 461 return LinkageInfo(LV.linkage(), DefaultVisibility, false); 462 463 // If we didn't end up with hidden visibility, consider attributes 464 // and -fvisibility. 465 if (F.ConsiderGlobalVisibility) 466 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode()); 467 468 return LV; 469} 470 471static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) { 472 // Only certain class members have linkage. Note that fields don't 473 // really have linkage, but it's convenient to say they do for the 474 // purposes of calculating linkage of pointer-to-data-member 475 // template arguments. 476 if (!(isa<CXXMethodDecl>(D) || 477 isa<VarDecl>(D) || 478 isa<FieldDecl>(D) || 479 (isa<TagDecl>(D) && 480 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl())))) 481 return LinkageInfo::none(); 482 483 LinkageInfo LV; 484 485 // The flags we're going to use to compute the class's visibility. 486 LVFlags ClassF = F; 487 488 // If we have an explicit visibility attribute, merge that in. 489 if (F.ConsiderVisibilityAttributes) { 490 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) { 491 LV.mergeVisibility(*Vis, true); 492 493 // Ignore global visibility later, but not this attribute. 494 F.ConsiderGlobalVisibility = false; 495 496 // Ignore both global visibility and attributes when computing our 497 // parent's visibility. 498 ClassF = F.onlyTemplateVisibility(); 499 } 500 } 501 502 // Class members only have linkage if their class has external 503 // linkage. 504 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF)); 505 if (!isExternalLinkage(LV.linkage())) 506 return LinkageInfo::none(); 507 508 // If the class already has unique-external linkage, we can't improve. 509 if (LV.linkage() == UniqueExternalLinkage) 510 return LinkageInfo::uniqueExternal(); 511 512 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 513 // If the type of the function uses a type with unique-external 514 // linkage, it's not legally usable from outside this translation unit. 515 if (MD->getType()->getLinkage() == UniqueExternalLinkage) 516 return LinkageInfo::uniqueExternal(); 517 518 TemplateSpecializationKind TSK = TSK_Undeclared; 519 520 // If this is a method template specialization, use the linkage for 521 // the template parameters and arguments. 522 if (FunctionTemplateSpecializationInfo *Spec 523 = MD->getTemplateSpecializationInfo()) { 524 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F)); 525 if (F.ConsiderTemplateParameterTypes) 526 LV.merge(getLVForTemplateParameterList( 527 Spec->getTemplate()->getTemplateParameters())); 528 529 TSK = Spec->getTemplateSpecializationKind(); 530 } else if (MemberSpecializationInfo *MSI = 531 MD->getMemberSpecializationInfo()) { 532 TSK = MSI->getTemplateSpecializationKind(); 533 } 534 535 // If we're paying attention to global visibility, apply 536 // -finline-visibility-hidden if this is an inline method. 537 // 538 // Note that ConsiderGlobalVisibility doesn't yet have information 539 // about whether containing classes have visibility attributes, 540 // and that's intentional. 541 if (TSK != TSK_ExplicitInstantiationDeclaration && 542 F.ConsiderGlobalVisibility && 543 MD->getASTContext().getLangOptions().InlineVisibilityHidden) { 544 // InlineVisibilityHidden only applies to definitions, and 545 // isInlined() only gives meaningful answers on definitions 546 // anyway. 547 const FunctionDecl *Def = 0; 548 if (MD->hasBody(Def) && Def->isInlined()) 549 LV.setVisibility(HiddenVisibility); 550 } 551 552 // Note that in contrast to basically every other situation, we 553 // *do* apply -fvisibility to method declarations. 554 555 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 556 if (const ClassTemplateSpecializationDecl *Spec 557 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { 558 // Merge template argument/parameter information for member 559 // class template specializations. 560 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F)); 561 if (F.ConsiderTemplateParameterTypes) 562 LV.merge(getLVForTemplateParameterList( 563 Spec->getSpecializedTemplate()->getTemplateParameters())); 564 } 565 566 // Static data members. 567 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 568 // Modify the variable's linkage by its type, but ignore the 569 // type's visibility unless it's a definition. 570 LVPair TypeLV = VD->getType()->getLinkageAndVisibility(); 571 if (TypeLV.first != ExternalLinkage) 572 LV.mergeLinkage(UniqueExternalLinkage); 573 if (!LV.visibilityExplicit()) 574 LV.mergeVisibility(TypeLV.second); 575 } 576 577 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit(); 578 579 // Apply -fvisibility if desired. 580 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) { 581 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode()); 582 } 583 584 return LV; 585} 586 587static void clearLinkageForClass(const CXXRecordDecl *record) { 588 for (CXXRecordDecl::decl_iterator 589 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) { 590 Decl *child = *i; 591 if (isa<NamedDecl>(child)) 592 cast<NamedDecl>(child)->ClearLinkageCache(); 593 } 594} 595 596void NamedDecl::ClearLinkageCache() { 597 // Note that we can't skip clearing the linkage of children just 598 // because the parent doesn't have cached linkage: we don't cache 599 // when computing linkage for parent contexts. 600 601 HasCachedLinkage = 0; 602 603 // If we're changing the linkage of a class, we need to reset the 604 // linkage of child declarations, too. 605 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this)) 606 clearLinkageForClass(record); 607 608 if (ClassTemplateDecl *temp = 609 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) { 610 // Clear linkage for the template pattern. 611 CXXRecordDecl *record = temp->getTemplatedDecl(); 612 record->HasCachedLinkage = 0; 613 clearLinkageForClass(record); 614 615 // We need to clear linkage for specializations, too. 616 for (ClassTemplateDecl::spec_iterator 617 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) 618 i->ClearLinkageCache(); 619 } 620 621 // Clear cached linkage for function template decls, too. 622 if (FunctionTemplateDecl *temp = 623 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) { 624 temp->getTemplatedDecl()->ClearLinkageCache(); 625 for (FunctionTemplateDecl::spec_iterator 626 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) 627 i->ClearLinkageCache(); 628 } 629 630} 631 632Linkage NamedDecl::getLinkage() const { 633 if (HasCachedLinkage) { 634 assert(Linkage(CachedLinkage) == 635 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage()); 636 return Linkage(CachedLinkage); 637 } 638 639 CachedLinkage = getLVForDecl(this, 640 LVFlags::CreateOnlyDeclLinkage()).linkage(); 641 HasCachedLinkage = 1; 642 return Linkage(CachedLinkage); 643} 644 645LinkageInfo NamedDecl::getLinkageAndVisibility() const { 646 LinkageInfo LI = getLVForDecl(this, LVFlags()); 647 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage()); 648 HasCachedLinkage = 1; 649 CachedLinkage = LI.linkage(); 650 return LI; 651} 652 653llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const { 654 // Use the most recent declaration of a variable. 655 if (const VarDecl *var = dyn_cast<VarDecl>(this)) 656 return getVisibilityOf(var->getMostRecentDeclaration()); 657 658 // Use the most recent declaration of a function, and also handle 659 // function template specializations. 660 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) { 661 if (llvm::Optional<Visibility> V 662 = getVisibilityOf(fn->getMostRecentDeclaration())) 663 return V; 664 665 // If the function is a specialization of a template with an 666 // explicit visibility attribute, use that. 667 if (FunctionTemplateSpecializationInfo *templateInfo 668 = fn->getTemplateSpecializationInfo()) 669 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl()); 670 671 return llvm::Optional<Visibility>(); 672 } 673 674 // Otherwise, just check the declaration itself first. 675 if (llvm::Optional<Visibility> V = getVisibilityOf(this)) 676 return V; 677 678 // If there wasn't explicit visibility there, and this is a 679 // specialization of a class template, check for visibility 680 // on the pattern. 681 if (const ClassTemplateSpecializationDecl *spec 682 = dyn_cast<ClassTemplateSpecializationDecl>(this)) 683 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl()); 684 685 return llvm::Optional<Visibility>(); 686} 687 688static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) { 689 // Objective-C: treat all Objective-C declarations as having external 690 // linkage. 691 switch (D->getKind()) { 692 default: 693 break; 694 case Decl::TemplateTemplateParm: // count these as external 695 case Decl::NonTypeTemplateParm: 696 case Decl::ObjCAtDefsField: 697 case Decl::ObjCCategory: 698 case Decl::ObjCCategoryImpl: 699 case Decl::ObjCCompatibleAlias: 700 case Decl::ObjCForwardProtocol: 701 case Decl::ObjCImplementation: 702 case Decl::ObjCMethod: 703 case Decl::ObjCProperty: 704 case Decl::ObjCPropertyImpl: 705 case Decl::ObjCProtocol: 706 return LinkageInfo::external(); 707 } 708 709 // Handle linkage for namespace-scope names. 710 if (D->getDeclContext()->getRedeclContext()->isFileContext()) 711 return getLVForNamespaceScopeDecl(D, Flags); 712 713 // C++ [basic.link]p5: 714 // In addition, a member function, static data member, a named 715 // class or enumeration of class scope, or an unnamed class or 716 // enumeration defined in a class-scope typedef declaration such 717 // that the class or enumeration has the typedef name for linkage 718 // purposes (7.1.3), has external linkage if the name of the class 719 // has external linkage. 720 if (D->getDeclContext()->isRecord()) 721 return getLVForClassMember(D, Flags); 722 723 // C++ [basic.link]p6: 724 // The name of a function declared in block scope and the name of 725 // an object declared by a block scope extern declaration have 726 // linkage. If there is a visible declaration of an entity with 727 // linkage having the same name and type, ignoring entities 728 // declared outside the innermost enclosing namespace scope, the 729 // block scope declaration declares that same entity and receives 730 // the linkage of the previous declaration. If there is more than 731 // one such matching entity, the program is ill-formed. Otherwise, 732 // if no matching entity is found, the block scope entity receives 733 // external linkage. 734 if (D->getLexicalDeclContext()->isFunctionOrMethod()) { 735 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 736 if (Function->isInAnonymousNamespace() && !Function->isExternC()) 737 return LinkageInfo::uniqueExternal(); 738 739 LinkageInfo LV; 740 if (Flags.ConsiderVisibilityAttributes) { 741 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility()) 742 LV.setVisibility(*Vis); 743 } 744 745 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) { 746 LinkageInfo PrevLV = getLVForDecl(Prev, Flags); 747 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); 748 LV.mergeVisibility(PrevLV); 749 } 750 751 return LV; 752 } 753 754 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) 755 if (Var->getStorageClass() == SC_Extern || 756 Var->getStorageClass() == SC_PrivateExtern) { 757 if (Var->isInAnonymousNamespace() && !Var->isExternC()) 758 return LinkageInfo::uniqueExternal(); 759 760 LinkageInfo LV; 761 if (Var->getStorageClass() == SC_PrivateExtern) 762 LV.setVisibility(HiddenVisibility); 763 else if (Flags.ConsiderVisibilityAttributes) { 764 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility()) 765 LV.setVisibility(*Vis); 766 } 767 768 if (const VarDecl *Prev = Var->getPreviousDeclaration()) { 769 LinkageInfo PrevLV = getLVForDecl(Prev, Flags); 770 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); 771 LV.mergeVisibility(PrevLV); 772 } 773 774 return LV; 775 } 776 } 777 778 // C++ [basic.link]p6: 779 // Names not covered by these rules have no linkage. 780 return LinkageInfo::none(); 781} 782 783std::string NamedDecl::getQualifiedNameAsString() const { 784 return getQualifiedNameAsString(getASTContext().getLangOptions()); 785} 786 787std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { 788 const DeclContext *Ctx = getDeclContext(); 789 790 if (Ctx->isFunctionOrMethod()) 791 return getNameAsString(); 792 793 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy; 794 ContextsTy Contexts; 795 796 // Collect contexts. 797 while (Ctx && isa<NamedDecl>(Ctx)) { 798 Contexts.push_back(Ctx); 799 Ctx = Ctx->getParent(); 800 }; 801 802 std::string QualName; 803 llvm::raw_string_ostream OS(QualName); 804 805 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); 806 I != E; ++I) { 807 if (const ClassTemplateSpecializationDecl *Spec 808 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { 809 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 810 std::string TemplateArgsStr 811 = TemplateSpecializationType::PrintTemplateArgumentList( 812 TemplateArgs.data(), 813 TemplateArgs.size(), 814 P); 815 OS << Spec->getName() << TemplateArgsStr; 816 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { 817 if (ND->isAnonymousNamespace()) 818 OS << "<anonymous namespace>"; 819 else 820 OS << ND; 821 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { 822 if (!RD->getIdentifier()) 823 OS << "<anonymous " << RD->getKindName() << '>'; 824 else 825 OS << RD; 826 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { 827 const FunctionProtoType *FT = 0; 828 if (FD->hasWrittenPrototype()) 829 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>()); 830 831 OS << FD << '('; 832 if (FT) { 833 unsigned NumParams = FD->getNumParams(); 834 for (unsigned i = 0; i < NumParams; ++i) { 835 if (i) 836 OS << ", "; 837 std::string Param; 838 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P); 839 OS << Param; 840 } 841 842 if (FT->isVariadic()) { 843 if (NumParams > 0) 844 OS << ", "; 845 OS << "..."; 846 } 847 } 848 OS << ')'; 849 } else { 850 OS << cast<NamedDecl>(*I); 851 } 852 OS << "::"; 853 } 854 855 if (getDeclName()) 856 OS << this; 857 else 858 OS << "<anonymous>"; 859 860 return OS.str(); 861} 862 863bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { 864 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); 865 866 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. 867 // We want to keep it, unless it nominates same namespace. 868 if (getKind() == Decl::UsingDirective) { 869 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() 870 ->getOriginalNamespace() == 871 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace() 872 ->getOriginalNamespace(); 873 } 874 875 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) 876 // For function declarations, we keep track of redeclarations. 877 return FD->getPreviousDeclaration() == OldD; 878 879 // For function templates, the underlying function declarations are linked. 880 if (const FunctionTemplateDecl *FunctionTemplate 881 = dyn_cast<FunctionTemplateDecl>(this)) 882 if (const FunctionTemplateDecl *OldFunctionTemplate 883 = dyn_cast<FunctionTemplateDecl>(OldD)) 884 return FunctionTemplate->getTemplatedDecl() 885 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); 886 887 // For method declarations, we keep track of redeclarations. 888 if (isa<ObjCMethodDecl>(this)) 889 return false; 890 891 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) 892 return true; 893 894 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) 895 return cast<UsingShadowDecl>(this)->getTargetDecl() == 896 cast<UsingShadowDecl>(OldD)->getTargetDecl(); 897 898 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) { 899 ASTContext &Context = getASTContext(); 900 return Context.getCanonicalNestedNameSpecifier( 901 cast<UsingDecl>(this)->getQualifier()) == 902 Context.getCanonicalNestedNameSpecifier( 903 cast<UsingDecl>(OldD)->getQualifier()); 904 } 905 906 // For non-function declarations, if the declarations are of the 907 // same kind then this must be a redeclaration, or semantic analysis 908 // would not have given us the new declaration. 909 return this->getKind() == OldD->getKind(); 910} 911 912bool NamedDecl::hasLinkage() const { 913 return getLinkage() != NoLinkage; 914} 915 916NamedDecl *NamedDecl::getUnderlyingDecl() { 917 NamedDecl *ND = this; 918 while (true) { 919 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) 920 ND = UD->getTargetDecl(); 921 else if (ObjCCompatibleAliasDecl *AD 922 = dyn_cast<ObjCCompatibleAliasDecl>(ND)) 923 return AD->getClassInterface(); 924 else 925 return ND; 926 } 927} 928 929bool NamedDecl::isCXXInstanceMember() const { 930 assert(isCXXClassMember() && 931 "checking whether non-member is instance member"); 932 933 const NamedDecl *D = this; 934 if (isa<UsingShadowDecl>(D)) 935 D = cast<UsingShadowDecl>(D)->getTargetDecl(); 936 937 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) 938 return true; 939 if (isa<CXXMethodDecl>(D)) 940 return cast<CXXMethodDecl>(D)->isInstance(); 941 if (isa<FunctionTemplateDecl>(D)) 942 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D) 943 ->getTemplatedDecl())->isInstance(); 944 return false; 945} 946 947//===----------------------------------------------------------------------===// 948// DeclaratorDecl Implementation 949//===----------------------------------------------------------------------===// 950 951template <typename DeclT> 952static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { 953 if (decl->getNumTemplateParameterLists() > 0) 954 return decl->getTemplateParameterList(0)->getTemplateLoc(); 955 else 956 return decl->getInnerLocStart(); 957} 958 959SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { 960 TypeSourceInfo *TSI = getTypeSourceInfo(); 961 if (TSI) return TSI->getTypeLoc().getBeginLoc(); 962 return SourceLocation(); 963} 964 965void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { 966 if (QualifierLoc) { 967 // Make sure the extended decl info is allocated. 968 if (!hasExtInfo()) { 969 // Save (non-extended) type source info pointer. 970 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); 971 // Allocate external info struct. 972 DeclInfo = new (getASTContext()) ExtInfo; 973 // Restore savedTInfo into (extended) decl info. 974 getExtInfo()->TInfo = savedTInfo; 975 } 976 // Set qualifier info. 977 getExtInfo()->QualifierLoc = QualifierLoc; 978 } 979 else { 980 // Here Qualifier == 0, i.e., we are removing the qualifier (if any). 981 if (hasExtInfo()) { 982 if (getExtInfo()->NumTemplParamLists == 0) { 983 // Save type source info pointer. 984 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; 985 // Deallocate the extended decl info. 986 getASTContext().Deallocate(getExtInfo()); 987 // Restore savedTInfo into (non-extended) decl info. 988 DeclInfo = savedTInfo; 989 } 990 else 991 getExtInfo()->QualifierLoc = QualifierLoc; 992 } 993 } 994} 995 996void 997DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context, 998 unsigned NumTPLists, 999 TemplateParameterList **TPLists) { 1000 assert(NumTPLists > 0); 1001 // Make sure the extended decl info is allocated. 1002 if (!hasExtInfo()) { 1003 // Save (non-extended) type source info pointer. 1004 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); 1005 // Allocate external info struct. 1006 DeclInfo = new (getASTContext()) ExtInfo; 1007 // Restore savedTInfo into (extended) decl info. 1008 getExtInfo()->TInfo = savedTInfo; 1009 } 1010 // Set the template parameter lists info. 1011 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); 1012} 1013 1014SourceLocation DeclaratorDecl::getOuterLocStart() const { 1015 return getTemplateOrInnerLocStart(this); 1016} 1017 1018namespace { 1019 1020// Helper function: returns true if QT is or contains a type 1021// having a postfix component. 1022bool typeIsPostfix(clang::QualType QT) { 1023 while (true) { 1024 const Type* T = QT.getTypePtr(); 1025 switch (T->getTypeClass()) { 1026 default: 1027 return false; 1028 case Type::Pointer: 1029 QT = cast<PointerType>(T)->getPointeeType(); 1030 break; 1031 case Type::BlockPointer: 1032 QT = cast<BlockPointerType>(T)->getPointeeType(); 1033 break; 1034 case Type::MemberPointer: 1035 QT = cast<MemberPointerType>(T)->getPointeeType(); 1036 break; 1037 case Type::LValueReference: 1038 case Type::RValueReference: 1039 QT = cast<ReferenceType>(T)->getPointeeType(); 1040 break; 1041 case Type::PackExpansion: 1042 QT = cast<PackExpansionType>(T)->getPattern(); 1043 break; 1044 case Type::Paren: 1045 case Type::ConstantArray: 1046 case Type::DependentSizedArray: 1047 case Type::IncompleteArray: 1048 case Type::VariableArray: 1049 case Type::FunctionProto: 1050 case Type::FunctionNoProto: 1051 return true; 1052 } 1053 } 1054} 1055 1056} // namespace 1057 1058SourceRange DeclaratorDecl::getSourceRange() const { 1059 SourceLocation RangeEnd = getLocation(); 1060 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { 1061 if (typeIsPostfix(TInfo->getType())) 1062 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 1063 } 1064 return SourceRange(getOuterLocStart(), RangeEnd); 1065} 1066 1067void 1068QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, 1069 unsigned NumTPLists, 1070 TemplateParameterList **TPLists) { 1071 assert((NumTPLists == 0 || TPLists != 0) && 1072 "Empty array of template parameters with positive size!"); 1073 1074 // Free previous template parameters (if any). 1075 if (NumTemplParamLists > 0) { 1076 Context.Deallocate(TemplParamLists); 1077 TemplParamLists = 0; 1078 NumTemplParamLists = 0; 1079 } 1080 // Set info on matched template parameter lists (if any). 1081 if (NumTPLists > 0) { 1082 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; 1083 NumTemplParamLists = NumTPLists; 1084 for (unsigned i = NumTPLists; i-- > 0; ) 1085 TemplParamLists[i] = TPLists[i]; 1086 } 1087} 1088 1089//===----------------------------------------------------------------------===// 1090// VarDecl Implementation 1091//===----------------------------------------------------------------------===// 1092 1093const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { 1094 switch (SC) { 1095 case SC_None: break; 1096 case SC_Auto: return "auto"; break; 1097 case SC_Extern: return "extern"; break; 1098 case SC_PrivateExtern: return "__private_extern__"; break; 1099 case SC_Register: return "register"; break; 1100 case SC_Static: return "static"; break; 1101 } 1102 1103 assert(0 && "Invalid storage class"); 1104 return 0; 1105} 1106 1107VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, 1108 SourceLocation StartL, SourceLocation IdL, 1109 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, 1110 StorageClass S, StorageClass SCAsWritten) { 1111 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten); 1112} 1113 1114void VarDecl::setStorageClass(StorageClass SC) { 1115 assert(isLegalForVariable(SC)); 1116 if (getStorageClass() != SC) 1117 ClearLinkageCache(); 1118 1119 VarDeclBits.SClass = SC; 1120} 1121 1122SourceRange VarDecl::getSourceRange() const { 1123 if (getInit()) 1124 return SourceRange(getOuterLocStart(), getInit()->getLocEnd()); 1125 return DeclaratorDecl::getSourceRange(); 1126} 1127 1128bool VarDecl::isExternC() const { 1129 ASTContext &Context = getASTContext(); 1130 if (!Context.getLangOptions().CPlusPlus) 1131 return (getDeclContext()->isTranslationUnit() && 1132 getStorageClass() != SC_Static) || 1133 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); 1134 1135 const DeclContext *DC = getDeclContext(); 1136 if (DC->isFunctionOrMethod()) 1137 return false; 1138 1139 for (; !DC->isTranslationUnit(); DC = DC->getParent()) { 1140 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { 1141 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) 1142 return getStorageClass() != SC_Static; 1143 1144 break; 1145 } 1146 1147 } 1148 1149 return false; 1150} 1151 1152VarDecl *VarDecl::getCanonicalDecl() { 1153 return getFirstDeclaration(); 1154} 1155 1156VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const { 1157 // C++ [basic.def]p2: 1158 // A declaration is a definition unless [...] it contains the 'extern' 1159 // specifier or a linkage-specification and neither an initializer [...], 1160 // it declares a static data member in a class declaration [...]. 1161 // C++ [temp.expl.spec]p15: 1162 // An explicit specialization of a static data member of a template is a 1163 // definition if the declaration includes an initializer; otherwise, it is 1164 // a declaration. 1165 if (isStaticDataMember()) { 1166 if (isOutOfLine() && (hasInit() || 1167 getTemplateSpecializationKind() != TSK_ExplicitSpecialization)) 1168 return Definition; 1169 else 1170 return DeclarationOnly; 1171 } 1172 // C99 6.7p5: 1173 // A definition of an identifier is a declaration for that identifier that 1174 // [...] causes storage to be reserved for that object. 1175 // Note: that applies for all non-file-scope objects. 1176 // C99 6.9.2p1: 1177 // If the declaration of an identifier for an object has file scope and an 1178 // initializer, the declaration is an external definition for the identifier 1179 if (hasInit()) 1180 return Definition; 1181 // AST for 'extern "C" int foo;' is annotated with 'extern'. 1182 if (hasExternalStorage()) 1183 return DeclarationOnly; 1184 1185 if (getStorageClassAsWritten() == SC_Extern || 1186 getStorageClassAsWritten() == SC_PrivateExtern) { 1187 for (const VarDecl *PrevVar = getPreviousDeclaration(); 1188 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) { 1189 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit()) 1190 return DeclarationOnly; 1191 } 1192 } 1193 // C99 6.9.2p2: 1194 // A declaration of an object that has file scope without an initializer, 1195 // and without a storage class specifier or the scs 'static', constitutes 1196 // a tentative definition. 1197 // No such thing in C++. 1198 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl()) 1199 return TentativeDefinition; 1200 1201 // What's left is (in C, block-scope) declarations without initializers or 1202 // external storage. These are definitions. 1203 return Definition; 1204} 1205 1206VarDecl *VarDecl::getActingDefinition() { 1207 DefinitionKind Kind = isThisDeclarationADefinition(); 1208 if (Kind != TentativeDefinition) 1209 return 0; 1210 1211 VarDecl *LastTentative = 0; 1212 VarDecl *First = getFirstDeclaration(); 1213 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); 1214 I != E; ++I) { 1215 Kind = (*I)->isThisDeclarationADefinition(); 1216 if (Kind == Definition) 1217 return 0; 1218 else if (Kind == TentativeDefinition) 1219 LastTentative = *I; 1220 } 1221 return LastTentative; 1222} 1223 1224bool VarDecl::isTentativeDefinitionNow() const { 1225 DefinitionKind Kind = isThisDeclarationADefinition(); 1226 if (Kind != TentativeDefinition) 1227 return false; 1228 1229 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 1230 if ((*I)->isThisDeclarationADefinition() == Definition) 1231 return false; 1232 } 1233 return true; 1234} 1235 1236VarDecl *VarDecl::getDefinition() { 1237 VarDecl *First = getFirstDeclaration(); 1238 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); 1239 I != E; ++I) { 1240 if ((*I)->isThisDeclarationADefinition() == Definition) 1241 return *I; 1242 } 1243 return 0; 1244} 1245 1246VarDecl::DefinitionKind VarDecl::hasDefinition() const { 1247 DefinitionKind Kind = DeclarationOnly; 1248 1249 const VarDecl *First = getFirstDeclaration(); 1250 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); 1251 I != E; ++I) 1252 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition()); 1253 1254 return Kind; 1255} 1256 1257const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { 1258 redecl_iterator I = redecls_begin(), E = redecls_end(); 1259 while (I != E && !I->getInit()) 1260 ++I; 1261 1262 if (I != E) { 1263 D = *I; 1264 return I->getInit(); 1265 } 1266 return 0; 1267} 1268 1269bool VarDecl::isOutOfLine() const { 1270 if (Decl::isOutOfLine()) 1271 return true; 1272 1273 if (!isStaticDataMember()) 1274 return false; 1275 1276 // If this static data member was instantiated from a static data member of 1277 // a class template, check whether that static data member was defined 1278 // out-of-line. 1279 if (VarDecl *VD = getInstantiatedFromStaticDataMember()) 1280 return VD->isOutOfLine(); 1281 1282 return false; 1283} 1284 1285VarDecl *VarDecl::getOutOfLineDefinition() { 1286 if (!isStaticDataMember()) 1287 return 0; 1288 1289 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); 1290 RD != RDEnd; ++RD) { 1291 if (RD->getLexicalDeclContext()->isFileContext()) 1292 return *RD; 1293 } 1294 1295 return 0; 1296} 1297 1298void VarDecl::setInit(Expr *I) { 1299 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { 1300 Eval->~EvaluatedStmt(); 1301 getASTContext().Deallocate(Eval); 1302 } 1303 1304 Init = I; 1305} 1306 1307VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { 1308 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 1309 return cast<VarDecl>(MSI->getInstantiatedFrom()); 1310 1311 return 0; 1312} 1313 1314TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { 1315 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 1316 return MSI->getTemplateSpecializationKind(); 1317 1318 return TSK_Undeclared; 1319} 1320 1321MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { 1322 return getASTContext().getInstantiatedFromStaticDataMember(this); 1323} 1324 1325void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 1326 SourceLocation PointOfInstantiation) { 1327 MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); 1328 assert(MSI && "Not an instantiated static data member?"); 1329 MSI->setTemplateSpecializationKind(TSK); 1330 if (TSK != TSK_ExplicitSpecialization && 1331 PointOfInstantiation.isValid() && 1332 MSI->getPointOfInstantiation().isInvalid()) 1333 MSI->setPointOfInstantiation(PointOfInstantiation); 1334} 1335 1336//===----------------------------------------------------------------------===// 1337// ParmVarDecl Implementation 1338//===----------------------------------------------------------------------===// 1339 1340ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, 1341 SourceLocation StartLoc, 1342 SourceLocation IdLoc, IdentifierInfo *Id, 1343 QualType T, TypeSourceInfo *TInfo, 1344 StorageClass S, StorageClass SCAsWritten, 1345 Expr *DefArg) { 1346 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo, 1347 S, SCAsWritten, DefArg); 1348} 1349 1350Expr *ParmVarDecl::getDefaultArg() { 1351 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); 1352 assert(!hasUninstantiatedDefaultArg() && 1353 "Default argument is not yet instantiated!"); 1354 1355 Expr *Arg = getInit(); 1356 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg)) 1357 return E->getSubExpr(); 1358 1359 return Arg; 1360} 1361 1362unsigned ParmVarDecl::getNumDefaultArgTemporaries() const { 1363 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit())) 1364 return E->getNumTemporaries(); 1365 1366 return 0; 1367} 1368 1369CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) { 1370 assert(getNumDefaultArgTemporaries() && 1371 "Default arguments does not have any temporaries!"); 1372 1373 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit()); 1374 return E->getTemporary(i); 1375} 1376 1377SourceRange ParmVarDecl::getDefaultArgRange() const { 1378 if (const Expr *E = getInit()) 1379 return E->getSourceRange(); 1380 1381 if (hasUninstantiatedDefaultArg()) 1382 return getUninstantiatedDefaultArg()->getSourceRange(); 1383 1384 return SourceRange(); 1385} 1386 1387bool ParmVarDecl::isParameterPack() const { 1388 return isa<PackExpansionType>(getType()); 1389} 1390 1391//===----------------------------------------------------------------------===// 1392// FunctionDecl Implementation 1393//===----------------------------------------------------------------------===// 1394 1395void FunctionDecl::getNameForDiagnostic(std::string &S, 1396 const PrintingPolicy &Policy, 1397 bool Qualified) const { 1398 NamedDecl::getNameForDiagnostic(S, Policy, Qualified); 1399 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); 1400 if (TemplateArgs) 1401 S += TemplateSpecializationType::PrintTemplateArgumentList( 1402 TemplateArgs->data(), 1403 TemplateArgs->size(), 1404 Policy); 1405 1406} 1407 1408bool FunctionDecl::isVariadic() const { 1409 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) 1410 return FT->isVariadic(); 1411 return false; 1412} 1413 1414bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { 1415 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 1416 if (I->Body || I->IsLateTemplateParsed) { 1417 Definition = *I; 1418 return true; 1419 } 1420 } 1421 1422 return false; 1423} 1424 1425bool FunctionDecl::hasTrivialBody() const 1426{ 1427 Stmt *S = getBody(); 1428 if (!S) { 1429 // Since we don't have a body for this function, we don't know if it's 1430 // trivial or not. 1431 return false; 1432 } 1433 1434 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) 1435 return true; 1436 return false; 1437} 1438 1439bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const { 1440 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 1441 if (I->IsDeleted || I->Body || I->IsLateTemplateParsed) { 1442 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I; 1443 return true; 1444 } 1445 } 1446 1447 return false; 1448} 1449 1450Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { 1451 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 1452 if (I->Body) { 1453 Definition = *I; 1454 return I->Body.get(getASTContext().getExternalSource()); 1455 } else if (I->IsLateTemplateParsed) { 1456 Definition = *I; 1457 return 0; 1458 } 1459 } 1460 1461 return 0; 1462} 1463 1464void FunctionDecl::setBody(Stmt *B) { 1465 Body = B; 1466 if (B) 1467 EndRangeLoc = B->getLocEnd(); 1468} 1469 1470void FunctionDecl::setPure(bool P) { 1471 IsPure = P; 1472 if (P) 1473 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) 1474 Parent->markedVirtualFunctionPure(); 1475} 1476 1477bool FunctionDecl::isMain() const { 1478 ASTContext &Context = getASTContext(); 1479 return !Context.getLangOptions().Freestanding && 1480 getDeclContext()->getRedeclContext()->isTranslationUnit() && 1481 getIdentifier() && getIdentifier()->isStr("main"); 1482} 1483 1484bool FunctionDecl::isExternC() const { 1485 ASTContext &Context = getASTContext(); 1486 // In C, any non-static, non-overloadable function has external 1487 // linkage. 1488 if (!Context.getLangOptions().CPlusPlus) 1489 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>(); 1490 1491 const DeclContext *DC = getDeclContext(); 1492 if (DC->isRecord()) 1493 return false; 1494 1495 for (; !DC->isTranslationUnit(); DC = DC->getParent()) { 1496 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { 1497 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) 1498 return getStorageClass() != SC_Static && 1499 !getAttr<OverloadableAttr>(); 1500 1501 break; 1502 } 1503 } 1504 1505 return isMain(); 1506} 1507 1508bool FunctionDecl::isGlobal() const { 1509 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) 1510 return Method->isStatic(); 1511 1512 if (getStorageClass() == SC_Static) 1513 return false; 1514 1515 for (const DeclContext *DC = getDeclContext(); 1516 DC->isNamespace(); 1517 DC = DC->getParent()) { 1518 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { 1519 if (!Namespace->getDeclName()) 1520 return false; 1521 break; 1522 } 1523 } 1524 1525 return true; 1526} 1527 1528void 1529FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { 1530 redeclarable_base::setPreviousDeclaration(PrevDecl); 1531 1532 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { 1533 FunctionTemplateDecl *PrevFunTmpl 1534 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; 1535 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); 1536 FunTmpl->setPreviousDeclaration(PrevFunTmpl); 1537 } 1538 1539 if (PrevDecl->IsInline) 1540 IsInline = true; 1541} 1542 1543const FunctionDecl *FunctionDecl::getCanonicalDecl() const { 1544 return getFirstDeclaration(); 1545} 1546 1547FunctionDecl *FunctionDecl::getCanonicalDecl() { 1548 return getFirstDeclaration(); 1549} 1550 1551void FunctionDecl::setStorageClass(StorageClass SC) { 1552 assert(isLegalForFunction(SC)); 1553 if (getStorageClass() != SC) 1554 ClearLinkageCache(); 1555 1556 SClass = SC; 1557} 1558 1559/// \brief Returns a value indicating whether this function 1560/// corresponds to a builtin function. 1561/// 1562/// The function corresponds to a built-in function if it is 1563/// declared at translation scope or within an extern "C" block and 1564/// its name matches with the name of a builtin. The returned value 1565/// will be 0 for functions that do not correspond to a builtin, a 1566/// value of type \c Builtin::ID if in the target-independent range 1567/// \c [1,Builtin::First), or a target-specific builtin value. 1568unsigned FunctionDecl::getBuiltinID() const { 1569 ASTContext &Context = getASTContext(); 1570 if (!getIdentifier() || !getIdentifier()->getBuiltinID()) 1571 return 0; 1572 1573 unsigned BuiltinID = getIdentifier()->getBuiltinID(); 1574 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 1575 return BuiltinID; 1576 1577 // This function has the name of a known C library 1578 // function. Determine whether it actually refers to the C library 1579 // function or whether it just has the same name. 1580 1581 // If this is a static function, it's not a builtin. 1582 if (getStorageClass() == SC_Static) 1583 return 0; 1584 1585 // If this function is at translation-unit scope and we're not in 1586 // C++, it refers to the C library function. 1587 if (!Context.getLangOptions().CPlusPlus && 1588 getDeclContext()->isTranslationUnit()) 1589 return BuiltinID; 1590 1591 // If the function is in an extern "C" linkage specification and is 1592 // not marked "overloadable", it's the real function. 1593 if (isa<LinkageSpecDecl>(getDeclContext()) && 1594 cast<LinkageSpecDecl>(getDeclContext())->getLanguage() 1595 == LinkageSpecDecl::lang_c && 1596 !getAttr<OverloadableAttr>()) 1597 return BuiltinID; 1598 1599 // Not a builtin 1600 return 0; 1601} 1602 1603 1604/// getNumParams - Return the number of parameters this function must have 1605/// based on its FunctionType. This is the length of the ParamInfo array 1606/// after it has been created. 1607unsigned FunctionDecl::getNumParams() const { 1608 const FunctionType *FT = getType()->getAs<FunctionType>(); 1609 if (isa<FunctionNoProtoType>(FT)) 1610 return 0; 1611 return cast<FunctionProtoType>(FT)->getNumArgs(); 1612 1613} 1614 1615void FunctionDecl::setParams(ASTContext &C, 1616 ParmVarDecl **NewParamInfo, unsigned NumParams) { 1617 assert(ParamInfo == 0 && "Already has param info!"); 1618 assert(NumParams == getNumParams() && "Parameter count mismatch!"); 1619 1620 // Zero params -> null pointer. 1621 if (NumParams) { 1622 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); 1623 ParamInfo = new (Mem) ParmVarDecl*[NumParams]; 1624 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); 1625 1626 // Update source range. The check below allows us to set EndRangeLoc before 1627 // setting the parameters. 1628 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) 1629 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); 1630 } 1631} 1632 1633/// getMinRequiredArguments - Returns the minimum number of arguments 1634/// needed to call this function. This may be fewer than the number of 1635/// function parameters, if some of the parameters have default 1636/// arguments (in C++) or the last parameter is a parameter pack. 1637unsigned FunctionDecl::getMinRequiredArguments() const { 1638 if (!getASTContext().getLangOptions().CPlusPlus) 1639 return getNumParams(); 1640 1641 unsigned NumRequiredArgs = getNumParams(); 1642 1643 // If the last parameter is a parameter pack, we don't need an argument for 1644 // it. 1645 if (NumRequiredArgs > 0 && 1646 getParamDecl(NumRequiredArgs - 1)->isParameterPack()) 1647 --NumRequiredArgs; 1648 1649 // If this parameter has a default argument, we don't need an argument for 1650 // it. 1651 while (NumRequiredArgs > 0 && 1652 getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) 1653 --NumRequiredArgs; 1654 1655 // We might have parameter packs before the end. These can't be deduced, 1656 // but they can still handle multiple arguments. 1657 unsigned ArgIdx = NumRequiredArgs; 1658 while (ArgIdx > 0) { 1659 if (getParamDecl(ArgIdx - 1)->isParameterPack()) 1660 NumRequiredArgs = ArgIdx; 1661 1662 --ArgIdx; 1663 } 1664 1665 return NumRequiredArgs; 1666} 1667 1668bool FunctionDecl::isInlined() const { 1669 if (IsInline) 1670 return true; 1671 1672 if (isa<CXXMethodDecl>(this)) { 1673 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) 1674 return true; 1675 } 1676 1677 switch (getTemplateSpecializationKind()) { 1678 case TSK_Undeclared: 1679 case TSK_ExplicitSpecialization: 1680 return false; 1681 1682 case TSK_ImplicitInstantiation: 1683 case TSK_ExplicitInstantiationDeclaration: 1684 case TSK_ExplicitInstantiationDefinition: 1685 // Handle below. 1686 break; 1687 } 1688 1689 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 1690 bool HasPattern = false; 1691 if (PatternDecl) 1692 HasPattern = PatternDecl->hasBody(PatternDecl); 1693 1694 if (HasPattern && PatternDecl) 1695 return PatternDecl->isInlined(); 1696 1697 return false; 1698} 1699 1700/// \brief For an inline function definition in C or C++, determine whether the 1701/// definition will be externally visible. 1702/// 1703/// Inline function definitions are always available for inlining optimizations. 1704/// However, depending on the language dialect, declaration specifiers, and 1705/// attributes, the definition of an inline function may or may not be 1706/// "externally" visible to other translation units in the program. 1707/// 1708/// In C99, inline definitions are not externally visible by default. However, 1709/// if even one of the global-scope declarations is marked "extern inline", the 1710/// inline definition becomes externally visible (C99 6.7.4p6). 1711/// 1712/// In GNU89 mode, or if the gnu_inline attribute is attached to the function 1713/// definition, we use the GNU semantics for inline, which are nearly the 1714/// opposite of C99 semantics. In particular, "inline" by itself will create 1715/// an externally visible symbol, but "extern inline" will not create an 1716/// externally visible symbol. 1717bool FunctionDecl::isInlineDefinitionExternallyVisible() const { 1718 assert(doesThisDeclarationHaveABody() && "Must have the function definition"); 1719 assert(isInlined() && "Function must be inline"); 1720 ASTContext &Context = getASTContext(); 1721 1722 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { 1723 // If it's not the case that both 'inline' and 'extern' are 1724 // specified on the definition, then this inline definition is 1725 // externally visible. 1726 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern)) 1727 return true; 1728 1729 // If any declaration is 'inline' but not 'extern', then this definition 1730 // is externally visible. 1731 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); 1732 Redecl != RedeclEnd; 1733 ++Redecl) { 1734 if (Redecl->isInlineSpecified() && 1735 Redecl->getStorageClassAsWritten() != SC_Extern) 1736 return true; 1737 } 1738 1739 return false; 1740 } 1741 1742 // C99 6.7.4p6: 1743 // [...] If all of the file scope declarations for a function in a 1744 // translation unit include the inline function specifier without extern, 1745 // then the definition in that translation unit is an inline definition. 1746 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); 1747 Redecl != RedeclEnd; 1748 ++Redecl) { 1749 // Only consider file-scope declarations in this test. 1750 if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) 1751 continue; 1752 1753 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) 1754 return true; // Not an inline definition 1755 } 1756 1757 // C99 6.7.4p6: 1758 // An inline definition does not provide an external definition for the 1759 // function, and does not forbid an external definition in another 1760 // translation unit. 1761 return false; 1762} 1763 1764/// getOverloadedOperator - Which C++ overloaded operator this 1765/// function represents, if any. 1766OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { 1767 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) 1768 return getDeclName().getCXXOverloadedOperator(); 1769 else 1770 return OO_None; 1771} 1772 1773/// getLiteralIdentifier - The literal suffix identifier this function 1774/// represents, if any. 1775const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { 1776 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) 1777 return getDeclName().getCXXLiteralIdentifier(); 1778 else 1779 return 0; 1780} 1781 1782FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { 1783 if (TemplateOrSpecialization.isNull()) 1784 return TK_NonTemplate; 1785 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) 1786 return TK_FunctionTemplate; 1787 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) 1788 return TK_MemberSpecialization; 1789 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) 1790 return TK_FunctionTemplateSpecialization; 1791 if (TemplateOrSpecialization.is 1792 <DependentFunctionTemplateSpecializationInfo*>()) 1793 return TK_DependentFunctionTemplateSpecialization; 1794 1795 assert(false && "Did we miss a TemplateOrSpecialization type?"); 1796 return TK_NonTemplate; 1797} 1798 1799FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { 1800 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) 1801 return cast<FunctionDecl>(Info->getInstantiatedFrom()); 1802 1803 return 0; 1804} 1805 1806MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { 1807 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 1808} 1809 1810void 1811FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, 1812 FunctionDecl *FD, 1813 TemplateSpecializationKind TSK) { 1814 assert(TemplateOrSpecialization.isNull() && 1815 "Member function is already a specialization"); 1816 MemberSpecializationInfo *Info 1817 = new (C) MemberSpecializationInfo(FD, TSK); 1818 TemplateOrSpecialization = Info; 1819} 1820 1821bool FunctionDecl::isImplicitlyInstantiable() const { 1822 // If the function is invalid, it can't be implicitly instantiated. 1823 if (isInvalidDecl()) 1824 return false; 1825 1826 switch (getTemplateSpecializationKind()) { 1827 case TSK_Undeclared: 1828 case TSK_ExplicitSpecialization: 1829 case TSK_ExplicitInstantiationDefinition: 1830 return false; 1831 1832 case TSK_ImplicitInstantiation: 1833 return true; 1834 1835 case TSK_ExplicitInstantiationDeclaration: 1836 // Handled below. 1837 break; 1838 } 1839 1840 // Find the actual template from which we will instantiate. 1841 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 1842 bool HasPattern = false; 1843 if (PatternDecl) 1844 HasPattern = PatternDecl->hasBody(PatternDecl); 1845 1846 // C++0x [temp.explicit]p9: 1847 // Except for inline functions, other explicit instantiation declarations 1848 // have the effect of suppressing the implicit instantiation of the entity 1849 // to which they refer. 1850 if (!HasPattern || !PatternDecl) 1851 return true; 1852 1853 return PatternDecl->isInlined(); 1854} 1855 1856FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { 1857 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { 1858 while (Primary->getInstantiatedFromMemberTemplate()) { 1859 // If we have hit a point where the user provided a specialization of 1860 // this template, we're done looking. 1861 if (Primary->isMemberSpecialization()) 1862 break; 1863 1864 Primary = Primary->getInstantiatedFromMemberTemplate(); 1865 } 1866 1867 return Primary->getTemplatedDecl(); 1868 } 1869 1870 return getInstantiatedFromMemberFunction(); 1871} 1872 1873FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { 1874 if (FunctionTemplateSpecializationInfo *Info 1875 = TemplateOrSpecialization 1876 .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 1877 return Info->Template.getPointer(); 1878 } 1879 return 0; 1880} 1881 1882const TemplateArgumentList * 1883FunctionDecl::getTemplateSpecializationArgs() const { 1884 if (FunctionTemplateSpecializationInfo *Info 1885 = TemplateOrSpecialization 1886 .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 1887 return Info->TemplateArguments; 1888 } 1889 return 0; 1890} 1891 1892const TemplateArgumentListInfo * 1893FunctionDecl::getTemplateSpecializationArgsAsWritten() const { 1894 if (FunctionTemplateSpecializationInfo *Info 1895 = TemplateOrSpecialization 1896 .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 1897 return Info->TemplateArgumentsAsWritten; 1898 } 1899 return 0; 1900} 1901 1902void 1903FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, 1904 FunctionTemplateDecl *Template, 1905 const TemplateArgumentList *TemplateArgs, 1906 void *InsertPos, 1907 TemplateSpecializationKind TSK, 1908 const TemplateArgumentListInfo *TemplateArgsAsWritten, 1909 SourceLocation PointOfInstantiation) { 1910 assert(TSK != TSK_Undeclared && 1911 "Must specify the type of function template specialization"); 1912 FunctionTemplateSpecializationInfo *Info 1913 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 1914 if (!Info) 1915 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, 1916 TemplateArgs, 1917 TemplateArgsAsWritten, 1918 PointOfInstantiation); 1919 TemplateOrSpecialization = Info; 1920 1921 // Insert this function template specialization into the set of known 1922 // function template specializations. 1923 if (InsertPos) 1924 Template->addSpecialization(Info, InsertPos); 1925 else { 1926 // Try to insert the new node. If there is an existing node, leave it, the 1927 // set will contain the canonical decls while 1928 // FunctionTemplateDecl::findSpecialization will return 1929 // the most recent redeclarations. 1930 FunctionTemplateSpecializationInfo *Existing 1931 = Template->getSpecializations().GetOrInsertNode(Info); 1932 (void)Existing; 1933 assert((!Existing || Existing->Function->isCanonicalDecl()) && 1934 "Set is supposed to only contain canonical decls"); 1935 } 1936} 1937 1938void 1939FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, 1940 const UnresolvedSetImpl &Templates, 1941 const TemplateArgumentListInfo &TemplateArgs) { 1942 assert(TemplateOrSpecialization.isNull()); 1943 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); 1944 Size += Templates.size() * sizeof(FunctionTemplateDecl*); 1945 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); 1946 void *Buffer = Context.Allocate(Size); 1947 DependentFunctionTemplateSpecializationInfo *Info = 1948 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, 1949 TemplateArgs); 1950 TemplateOrSpecialization = Info; 1951} 1952 1953DependentFunctionTemplateSpecializationInfo:: 1954DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, 1955 const TemplateArgumentListInfo &TArgs) 1956 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { 1957 1958 d.NumTemplates = Ts.size(); 1959 d.NumArgs = TArgs.size(); 1960 1961 FunctionTemplateDecl **TsArray = 1962 const_cast<FunctionTemplateDecl**>(getTemplates()); 1963 for (unsigned I = 0, E = Ts.size(); I != E; ++I) 1964 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); 1965 1966 TemplateArgumentLoc *ArgsArray = 1967 const_cast<TemplateArgumentLoc*>(getTemplateArgs()); 1968 for (unsigned I = 0, E = TArgs.size(); I != E; ++I) 1969 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); 1970} 1971 1972TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { 1973 // For a function template specialization, query the specialization 1974 // information object. 1975 FunctionTemplateSpecializationInfo *FTSInfo 1976 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 1977 if (FTSInfo) 1978 return FTSInfo->getTemplateSpecializationKind(); 1979 1980 MemberSpecializationInfo *MSInfo 1981 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 1982 if (MSInfo) 1983 return MSInfo->getTemplateSpecializationKind(); 1984 1985 return TSK_Undeclared; 1986} 1987 1988void 1989FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 1990 SourceLocation PointOfInstantiation) { 1991 if (FunctionTemplateSpecializationInfo *FTSInfo 1992 = TemplateOrSpecialization.dyn_cast< 1993 FunctionTemplateSpecializationInfo*>()) { 1994 FTSInfo->setTemplateSpecializationKind(TSK); 1995 if (TSK != TSK_ExplicitSpecialization && 1996 PointOfInstantiation.isValid() && 1997 FTSInfo->getPointOfInstantiation().isInvalid()) 1998 FTSInfo->setPointOfInstantiation(PointOfInstantiation); 1999 } else if (MemberSpecializationInfo *MSInfo 2000 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { 2001 MSInfo->setTemplateSpecializationKind(TSK); 2002 if (TSK != TSK_ExplicitSpecialization && 2003 PointOfInstantiation.isValid() && 2004 MSInfo->getPointOfInstantiation().isInvalid()) 2005 MSInfo->setPointOfInstantiation(PointOfInstantiation); 2006 } else 2007 assert(false && "Function cannot have a template specialization kind"); 2008} 2009 2010SourceLocation FunctionDecl::getPointOfInstantiation() const { 2011 if (FunctionTemplateSpecializationInfo *FTSInfo 2012 = TemplateOrSpecialization.dyn_cast< 2013 FunctionTemplateSpecializationInfo*>()) 2014 return FTSInfo->getPointOfInstantiation(); 2015 else if (MemberSpecializationInfo *MSInfo 2016 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) 2017 return MSInfo->getPointOfInstantiation(); 2018 2019 return SourceLocation(); 2020} 2021 2022bool FunctionDecl::isOutOfLine() const { 2023 if (Decl::isOutOfLine()) 2024 return true; 2025 2026 // If this function was instantiated from a member function of a 2027 // class template, check whether that member function was defined out-of-line. 2028 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { 2029 const FunctionDecl *Definition; 2030 if (FD->hasBody(Definition)) 2031 return Definition->isOutOfLine(); 2032 } 2033 2034 // If this function was instantiated from a function template, 2035 // check whether that function template was defined out-of-line. 2036 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { 2037 const FunctionDecl *Definition; 2038 if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) 2039 return Definition->isOutOfLine(); 2040 } 2041 2042 return false; 2043} 2044 2045SourceRange FunctionDecl::getSourceRange() const { 2046 return SourceRange(getOuterLocStart(), EndRangeLoc); 2047} 2048 2049//===----------------------------------------------------------------------===// 2050// FieldDecl Implementation 2051//===----------------------------------------------------------------------===// 2052 2053FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, 2054 SourceLocation StartLoc, SourceLocation IdLoc, 2055 IdentifierInfo *Id, QualType T, 2056 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) { 2057 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, 2058 BW, Mutable); 2059} 2060 2061bool FieldDecl::isAnonymousStructOrUnion() const { 2062 if (!isImplicit() || getDeclName()) 2063 return false; 2064 2065 if (const RecordType *Record = getType()->getAs<RecordType>()) 2066 return Record->getDecl()->isAnonymousStructOrUnion(); 2067 2068 return false; 2069} 2070 2071unsigned FieldDecl::getFieldIndex() const { 2072 if (CachedFieldIndex) return CachedFieldIndex - 1; 2073 2074 unsigned index = 0; 2075 const RecordDecl *RD = getParent(); 2076 const FieldDecl *LastFD = 0; 2077 bool IsMsStruct = RD->hasAttr<MsStructAttr>(); 2078 2079 RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 2080 while (true) { 2081 assert(i != e && "failed to find field in parent!"); 2082 if (*i == this) 2083 break; 2084 2085 if (IsMsStruct) { 2086 // Zero-length bitfields following non-bitfield members are ignored. 2087 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*i), LastFD)) { 2088 ++i; 2089 continue; 2090 } 2091 LastFD = (*i); 2092 } 2093 ++i; 2094 ++index; 2095 } 2096 2097 CachedFieldIndex = index + 1; 2098 return index; 2099} 2100 2101SourceRange FieldDecl::getSourceRange() const { 2102 if (isBitField()) 2103 return SourceRange(getInnerLocStart(), BitWidth->getLocEnd()); 2104 return DeclaratorDecl::getSourceRange(); 2105} 2106 2107//===----------------------------------------------------------------------===// 2108// TagDecl Implementation 2109//===----------------------------------------------------------------------===// 2110 2111SourceLocation TagDecl::getOuterLocStart() const { 2112 return getTemplateOrInnerLocStart(this); 2113} 2114 2115SourceRange TagDecl::getSourceRange() const { 2116 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); 2117 return SourceRange(getOuterLocStart(), E); 2118} 2119 2120TagDecl* TagDecl::getCanonicalDecl() { 2121 return getFirstDeclaration(); 2122} 2123 2124void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { 2125 TypedefNameDeclOrQualifier = TDD; 2126 if (TypeForDecl) 2127 const_cast<Type*>(TypeForDecl)->ClearLinkageCache(); 2128 ClearLinkageCache(); 2129} 2130 2131void TagDecl::startDefinition() { 2132 IsBeingDefined = true; 2133 2134 if (isa<CXXRecordDecl>(this)) { 2135 CXXRecordDecl *D = cast<CXXRecordDecl>(this); 2136 struct CXXRecordDecl::DefinitionData *Data = 2137 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); 2138 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) 2139 cast<CXXRecordDecl>(*I)->DefinitionData = Data; 2140 } 2141} 2142 2143void TagDecl::completeDefinition() { 2144 assert((!isa<CXXRecordDecl>(this) || 2145 cast<CXXRecordDecl>(this)->hasDefinition()) && 2146 "definition completed but not started"); 2147 2148 IsDefinition = true; 2149 IsBeingDefined = false; 2150 2151 if (ASTMutationListener *L = getASTMutationListener()) 2152 L->CompletedTagDefinition(this); 2153} 2154 2155TagDecl* TagDecl::getDefinition() const { 2156 if (isDefinition()) 2157 return const_cast<TagDecl *>(this); 2158 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this)) 2159 return CXXRD->getDefinition(); 2160 2161 for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); 2162 R != REnd; ++R) 2163 if (R->isDefinition()) 2164 return *R; 2165 2166 return 0; 2167} 2168 2169void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { 2170 if (QualifierLoc) { 2171 // Make sure the extended qualifier info is allocated. 2172 if (!hasExtInfo()) 2173 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; 2174 // Set qualifier info. 2175 getExtInfo()->QualifierLoc = QualifierLoc; 2176 } 2177 else { 2178 // Here Qualifier == 0, i.e., we are removing the qualifier (if any). 2179 if (hasExtInfo()) { 2180 if (getExtInfo()->NumTemplParamLists == 0) { 2181 getASTContext().Deallocate(getExtInfo()); 2182 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0; 2183 } 2184 else 2185 getExtInfo()->QualifierLoc = QualifierLoc; 2186 } 2187 } 2188} 2189 2190void TagDecl::setTemplateParameterListsInfo(ASTContext &Context, 2191 unsigned NumTPLists, 2192 TemplateParameterList **TPLists) { 2193 assert(NumTPLists > 0); 2194 // Make sure the extended decl info is allocated. 2195 if (!hasExtInfo()) 2196 // Allocate external info struct. 2197 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; 2198 // Set the template parameter lists info. 2199 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); 2200} 2201 2202//===----------------------------------------------------------------------===// 2203// EnumDecl Implementation 2204//===----------------------------------------------------------------------===// 2205 2206EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, 2207 SourceLocation StartLoc, SourceLocation IdLoc, 2208 IdentifierInfo *Id, 2209 EnumDecl *PrevDecl, bool IsScoped, 2210 bool IsScopedUsingClassTag, bool IsFixed) { 2211 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl, 2212 IsScoped, IsScopedUsingClassTag, IsFixed); 2213 C.getTypeDeclType(Enum, PrevDecl); 2214 return Enum; 2215} 2216 2217EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) { 2218 return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0, 2219 false, false, false); 2220} 2221 2222void EnumDecl::completeDefinition(QualType NewType, 2223 QualType NewPromotionType, 2224 unsigned NumPositiveBits, 2225 unsigned NumNegativeBits) { 2226 assert(!isDefinition() && "Cannot redefine enums!"); 2227 if (!IntegerType) 2228 IntegerType = NewType.getTypePtr(); 2229 PromotionType = NewPromotionType; 2230 setNumPositiveBits(NumPositiveBits); 2231 setNumNegativeBits(NumNegativeBits); 2232 TagDecl::completeDefinition(); 2233} 2234 2235//===----------------------------------------------------------------------===// 2236// RecordDecl Implementation 2237//===----------------------------------------------------------------------===// 2238 2239RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, 2240 SourceLocation StartLoc, SourceLocation IdLoc, 2241 IdentifierInfo *Id, RecordDecl *PrevDecl) 2242 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) { 2243 HasFlexibleArrayMember = false; 2244 AnonymousStructOrUnion = false; 2245 HasObjectMember = false; 2246 LoadedFieldsFromExternalStorage = false; 2247 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); 2248} 2249 2250RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, 2251 SourceLocation StartLoc, SourceLocation IdLoc, 2252 IdentifierInfo *Id, RecordDecl* PrevDecl) { 2253 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id, 2254 PrevDecl); 2255 C.getTypeDeclType(R, PrevDecl); 2256 return R; 2257} 2258 2259RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) { 2260 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 2261 SourceLocation(), 0, 0); 2262} 2263 2264bool RecordDecl::isInjectedClassName() const { 2265 return isImplicit() && getDeclName() && getDeclContext()->isRecord() && 2266 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); 2267} 2268 2269RecordDecl::field_iterator RecordDecl::field_begin() const { 2270 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) 2271 LoadFieldsFromExternalStorage(); 2272 2273 return field_iterator(decl_iterator(FirstDecl)); 2274} 2275 2276/// completeDefinition - Notes that the definition of this type is now 2277/// complete. 2278void RecordDecl::completeDefinition() { 2279 assert(!isDefinition() && "Cannot redefine record!"); 2280 TagDecl::completeDefinition(); 2281} 2282 2283void RecordDecl::LoadFieldsFromExternalStorage() const { 2284 ExternalASTSource *Source = getASTContext().getExternalSource(); 2285 assert(hasExternalLexicalStorage() && Source && "No external storage?"); 2286 2287 // Notify that we have a RecordDecl doing some initialization. 2288 ExternalASTSource::Deserializing TheFields(Source); 2289 2290 llvm::SmallVector<Decl*, 64> Decls; 2291 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) 2292 return; 2293 2294#ifndef NDEBUG 2295 // Check that all decls we got were FieldDecls. 2296 for (unsigned i=0, e=Decls.size(); i != e; ++i) 2297 assert(isa<FieldDecl>(Decls[i])); 2298#endif 2299 2300 LoadedFieldsFromExternalStorage = true; 2301 2302 if (Decls.empty()) 2303 return; 2304 2305 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls); 2306} 2307 2308//===----------------------------------------------------------------------===// 2309// BlockDecl Implementation 2310//===----------------------------------------------------------------------===// 2311 2312void BlockDecl::setParams(ParmVarDecl **NewParamInfo, 2313 unsigned NParms) { 2314 assert(ParamInfo == 0 && "Already has param info!"); 2315 2316 // Zero params -> null pointer. 2317 if (NParms) { 2318 NumParams = NParms; 2319 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams); 2320 ParamInfo = new (Mem) ParmVarDecl*[NumParams]; 2321 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); 2322 } 2323} 2324 2325void BlockDecl::setCaptures(ASTContext &Context, 2326 const Capture *begin, 2327 const Capture *end, 2328 bool capturesCXXThis) { 2329 CapturesCXXThis = capturesCXXThis; 2330 2331 if (begin == end) { 2332 NumCaptures = 0; 2333 Captures = 0; 2334 return; 2335 } 2336 2337 NumCaptures = end - begin; 2338 2339 // Avoid new Capture[] because we don't want to provide a default 2340 // constructor. 2341 size_t allocationSize = NumCaptures * sizeof(Capture); 2342 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*)); 2343 memcpy(buffer, begin, allocationSize); 2344 Captures = static_cast<Capture*>(buffer); 2345} 2346 2347SourceRange BlockDecl::getSourceRange() const { 2348 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation()); 2349} 2350 2351//===----------------------------------------------------------------------===// 2352// Other Decl Allocation/Deallocation Method Implementations 2353//===----------------------------------------------------------------------===// 2354 2355TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { 2356 return new (C) TranslationUnitDecl(C); 2357} 2358 2359LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, 2360 SourceLocation IdentL, IdentifierInfo *II) { 2361 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL); 2362} 2363 2364LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, 2365 SourceLocation IdentL, IdentifierInfo *II, 2366 SourceLocation GnuLabelL) { 2367 assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); 2368 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL); 2369} 2370 2371 2372NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, 2373 SourceLocation StartLoc, 2374 SourceLocation IdLoc, IdentifierInfo *Id) { 2375 return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id); 2376} 2377 2378NamespaceDecl *NamespaceDecl::getNextNamespace() { 2379 return dyn_cast_or_null<NamespaceDecl>( 2380 NextNamespace.get(getASTContext().getExternalSource())); 2381} 2382 2383ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, 2384 SourceLocation IdLoc, 2385 IdentifierInfo *Id, 2386 QualType Type) { 2387 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type); 2388} 2389 2390FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, 2391 SourceLocation StartLoc, 2392 const DeclarationNameInfo &NameInfo, 2393 QualType T, TypeSourceInfo *TInfo, 2394 StorageClass SC, StorageClass SCAsWritten, 2395 bool isInlineSpecified, 2396 bool hasWrittenPrototype) { 2397 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo, 2398 T, TInfo, SC, SCAsWritten, 2399 isInlineSpecified); 2400 New->HasWrittenPrototype = hasWrittenPrototype; 2401 return New; 2402} 2403 2404BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { 2405 return new (C) BlockDecl(DC, L); 2406} 2407 2408EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, 2409 SourceLocation L, 2410 IdentifierInfo *Id, QualType T, 2411 Expr *E, const llvm::APSInt &V) { 2412 return new (C) EnumConstantDecl(CD, L, Id, T, E, V); 2413} 2414 2415IndirectFieldDecl * 2416IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 2417 IdentifierInfo *Id, QualType T, NamedDecl **CH, 2418 unsigned CHS) { 2419 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS); 2420} 2421 2422SourceRange EnumConstantDecl::getSourceRange() const { 2423 SourceLocation End = getLocation(); 2424 if (Init) 2425 End = Init->getLocEnd(); 2426 return SourceRange(getLocation(), End); 2427} 2428 2429TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, 2430 SourceLocation StartLoc, SourceLocation IdLoc, 2431 IdentifierInfo *Id, TypeSourceInfo *TInfo) { 2432 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo); 2433} 2434 2435TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, 2436 SourceLocation StartLoc, 2437 SourceLocation IdLoc, IdentifierInfo *Id, 2438 TypeSourceInfo *TInfo) { 2439 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo); 2440} 2441 2442SourceRange TypedefDecl::getSourceRange() const { 2443 SourceLocation RangeEnd = getLocation(); 2444 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { 2445 if (typeIsPostfix(TInfo->getType())) 2446 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 2447 } 2448 return SourceRange(getLocStart(), RangeEnd); 2449} 2450 2451SourceRange TypeAliasDecl::getSourceRange() const { 2452 SourceLocation RangeEnd = getLocStart(); 2453 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) 2454 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 2455 return SourceRange(getLocStart(), RangeEnd); 2456} 2457 2458FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, 2459 StringLiteral *Str, 2460 SourceLocation AsmLoc, 2461 SourceLocation RParenLoc) { 2462 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); 2463} 2464