15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Decl.cpp - Declaration AST Node Implementation -------------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 10e184baeaa112ceac32420f8ca127b8d4d152d109Argyrios Kyrtzidis// This file implements the Decl subclasses. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Decl.h" 152a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor#include "clang/AST/DeclCXX.h" 160de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff#include "clang/AST/DeclObjC.h" 177da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor#include "clang/AST/DeclTemplate.h" 186c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner#include "clang/AST/ASTContext.h" 19b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h" 20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Stmt.h" 2199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes#include "clang/AST/Expr.h" 22337cba4b3e17b98cfa512dfd12e57f4ccb0859beAnders Carlsson#include "clang/AST/ExprCXX.h" 23d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor#include "clang/AST/PrettyPrinter.h" 24565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis#include "clang/AST/ASTMutationListener.h" 251b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h" 26e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/Basic/IdentifierTable.h" 2715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor#include "clang/Basic/Module.h" 28465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara#include "clang/Basic/Specifiers.h" 294421d2b341d041df44013769f23c306308bbab83Douglas Gregor#include "clang/Basic/TargetInfo.h" 30f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "llvm/Support/ErrorHandling.h" 3127f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek 324278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie#include <algorithm> 334278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 36d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner//===----------------------------------------------------------------------===// 374afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// NamedDecl Implementation 385239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===// 395239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis 404421d2b341d041df44013769f23c306308bbab83Douglas Gregorstatic llvm::Optional<Visibility> getVisibilityOf(const Decl *D) { 414421d2b341d041df44013769f23c306308bbab83Douglas Gregor // If this declaration has an explicit visibility attribute, use it. 424421d2b341d041df44013769f23c306308bbab83Douglas Gregor if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) { 434421d2b341d041df44013769f23c306308bbab83Douglas Gregor switch (A->getVisibility()) { 444421d2b341d041df44013769f23c306308bbab83Douglas Gregor case VisibilityAttr::Default: 454421d2b341d041df44013769f23c306308bbab83Douglas Gregor return DefaultVisibility; 464421d2b341d041df44013769f23c306308bbab83Douglas Gregor case VisibilityAttr::Hidden: 474421d2b341d041df44013769f23c306308bbab83Douglas Gregor return HiddenVisibility; 484421d2b341d041df44013769f23c306308bbab83Douglas Gregor case VisibilityAttr::Protected: 494421d2b341d041df44013769f23c306308bbab83Douglas Gregor return ProtectedVisibility; 504421d2b341d041df44013769f23c306308bbab83Douglas Gregor } 51e7bc9722c807030409178d4af8ce8d1260bbd821John McCall } 527f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall 534421d2b341d041df44013769f23c306308bbab83Douglas Gregor // If we're on Mac OS X, an 'availability' for Mac OS X attribute 544421d2b341d041df44013769f23c306308bbab83Douglas Gregor // implies visibility(default). 55bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) { 564421d2b341d041df44013769f23c306308bbab83Douglas Gregor for (specific_attr_iterator<AvailabilityAttr> 574421d2b341d041df44013769f23c306308bbab83Douglas Gregor A = D->specific_attr_begin<AvailabilityAttr>(), 584421d2b341d041df44013769f23c306308bbab83Douglas Gregor AEnd = D->specific_attr_end<AvailabilityAttr>(); 594421d2b341d041df44013769f23c306308bbab83Douglas Gregor A != AEnd; ++A) 604421d2b341d041df44013769f23c306308bbab83Douglas Gregor if ((*A)->getPlatform()->getName().equals("macosx")) 614421d2b341d041df44013769f23c306308bbab83Douglas Gregor return DefaultVisibility; 621fb0caaa7bef765b85972274e3b434af2572c141John McCall } 634421d2b341d041df44013769f23c306308bbab83Douglas Gregor 644421d2b341d041df44013769f23c306308bbab83Douglas Gregor return llvm::Optional<Visibility>(); 651fb0caaa7bef765b85972274e3b434af2572c141John McCall} 661fb0caaa7bef765b85972274e3b434af2572c141John McCall 67af14603ca61757cf4361b583b45639a04c57e651John McCalltypedef NamedDecl::LinkageInfo LinkageInfo; 68af14603ca61757cf4361b583b45639a04c57e651John McCall 69752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramernamespace { 703698748400478880d2a146ef9eaa111cd0e60522John McCall/// Flags controlling the computation of linkage and visibility. 713698748400478880d2a146ef9eaa111cd0e60522John McCallstruct LVFlags { 720f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola const bool ConsiderGlobalVisibility; 730f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola const bool ConsiderVisibilityAttributes; 740f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola const bool ConsiderTemplateParameterTypes; 753698748400478880d2a146ef9eaa111cd0e60522John McCall 763698748400478880d2a146ef9eaa111cd0e60522John McCall LVFlags() : ConsiderGlobalVisibility(true), 771a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall ConsiderVisibilityAttributes(true), 781a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall ConsiderTemplateParameterTypes(true) { 793698748400478880d2a146ef9eaa111cd0e60522John McCall } 803698748400478880d2a146ef9eaa111cd0e60522John McCall 8162d9f110b83dfa5dcd4a945e3b5f2e9c73d3aa4aRafael Espindola LVFlags(bool Global, bool Attributes, bool Parameters) : 8262d9f110b83dfa5dcd4a945e3b5f2e9c73d3aa4aRafael Espindola ConsiderGlobalVisibility(Global), 8362d9f110b83dfa5dcd4a945e3b5f2e9c73d3aa4aRafael Espindola ConsiderVisibilityAttributes(Attributes), 8462d9f110b83dfa5dcd4a945e3b5f2e9c73d3aa4aRafael Espindola ConsiderTemplateParameterTypes(Parameters) { 8562d9f110b83dfa5dcd4a945e3b5f2e9c73d3aa4aRafael Espindola } 8662d9f110b83dfa5dcd4a945e3b5f2e9c73d3aa4aRafael Espindola 87381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor /// \brief Returns a set of flags that is only useful for computing the 88381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor /// linkage, not the visibility, of a declaration. 89381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor static LVFlags CreateOnlyDeclLinkage() { 9062d9f110b83dfa5dcd4a945e3b5f2e9c73d3aa4aRafael Espindola return LVFlags(false, false, false); 913698748400478880d2a146ef9eaa111cd0e60522John McCall } 9289d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor}; 93752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramer} // end anonymous namespace 943698748400478880d2a146ef9eaa111cd0e60522John McCall 95093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindolastatic LinkageInfo getLVForType(QualType T) { 96093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility(); 97093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola return LinkageInfo(P.first, P.second, T->isVisibilityExplicit()); 98093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola} 99093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola 1000b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given 1010b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list. 102093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindolastatic LinkageInfo 1031fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) { 104093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola LinkageInfo LV(ExternalLinkage, DefaultVisibility, false); 1050b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor for (TemplateParameterList::const_iterator P = Params->begin(), 1060b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor PEnd = Params->end(); 1070b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor P != PEnd; ++P) { 1086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { 1096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor if (NTTP->isExpandedParameterPack()) { 1106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { 1116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor QualType T = NTTP->getExpansionType(I); 1126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor if (!T->isDependentType()) 113093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola LV.merge(getLVForType(T)); 1146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor } 1156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor continue; 1166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor } 117b5d763d87c8ffb969b4d4a59ed98a2e3516e0850Rafael Espindola 1180b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor if (!NTTP->getType()->isDependentType()) { 119093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola LV.merge(getLVForType(NTTP->getType())); 1200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor continue; 1210b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor } 1226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor } 1230b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 1240b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor if (TemplateTemplateParmDecl *TTP 1250b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor = dyn_cast<TemplateTemplateParmDecl>(*P)) { 126093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters())); 1270b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor } 1280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor } 1290b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 1301fb0caaa7bef765b85972274e3b434af2572c141John McCall return LV; 1310b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor} 1320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 133381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor/// getLVForDecl - Get the linkage and visibility for the given declaration. 134381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F); 135381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor 1360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and 1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list. 138093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindolastatic LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args, 139093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola unsigned NumArgs, 140093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola LVFlags &F) { 141093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola LinkageInfo LV(ExternalLinkage, DefaultVisibility, false); 1420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 1430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor for (unsigned I = 0; I != NumArgs; ++I) { 1440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor switch (Args[I].getKind()) { 1450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TemplateArgument::Null: 1460b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TemplateArgument::Integral: 1470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TemplateArgument::Expression: 1480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor break; 149b5d763d87c8ffb969b4d4a59ed98a2e3516e0850Rafael Espindola 1500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TemplateArgument::Type: 151093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola LV.merge(getLVForType(Args[I].getAsType())); 1520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor break; 1530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 1540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TemplateArgument::Declaration: 1551fb0caaa7bef765b85972274e3b434af2572c141John McCall // The decl can validly be null as the representation of nullptr 1561fb0caaa7bef765b85972274e3b434af2572c141John McCall // arguments, valid only in C++0x. 1571fb0caaa7bef765b85972274e3b434af2572c141John McCall if (Decl *D = Args[I].getAsDecl()) { 15889d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) 15989d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor LV = merge(LV, getLVForDecl(ND, F)); 1601fb0caaa7bef765b85972274e3b434af2572c141John McCall } 1610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor break; 1620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 1630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TemplateArgument::Template: 164a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor case TemplateArgument::TemplateExpansion: 165b5d763d87c8ffb969b4d4a59ed98a2e3516e0850Rafael Espindola if (TemplateDecl *Template 166a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl()) 167093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola LV.merge(getLVForDecl(Template, F)); 1680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor break; 1690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 1700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TemplateArgument::Pack: 171860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(), 172860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola Args[I].pack_size(), 173860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola F)); 1740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor break; 1750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor } 1760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor } 1770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 1781fb0caaa7bef765b85972274e3b434af2572c141John McCall return LV; 1790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor} 1800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 181093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindolastatic LinkageInfo 182381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs, 183381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor LVFlags &F) { 184381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F); 1853cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall} 1863cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall 1876ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCallstatic bool shouldConsiderTemplateLV(const FunctionDecl *fn, 1886ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall const FunctionTemplateSpecializationInfo *spec) { 1896ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall return !(spec->isExplicitSpecialization() && 1906ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall fn->hasAttr<VisibilityAttr>()); 1916ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall} 1926ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall 1936ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCallstatic bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) { 1946ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>()); 1956ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall} 1966ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall 1973698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) { 1987a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl assert(D->getDeclContext()->getRedeclContext()->isFileContext() && 199d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor "Not a name having namespace scope"); 200d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor ASTContext &Context = D->getASTContext(); 201d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 202d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // C++ [basic.link]p3: 203d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // A name having namespace scope (3.3.6) has internal linkage if it 204d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // is the name of 205d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // - an object, reference, function or function template that is 206d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // explicitly declared static; or, 207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // (This bullet corresponds to C99 6.2.2p3.) 208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { 209d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // Explicitly declared static. 210d931b086984257de68868a64a235c2b4b34003fbJohn McCall if (Var->getStorageClass() == SC_Static) 211af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::internal(); 212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // - an object or reference that is explicitly declared const 214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // and neither explicitly declared extern nor previously 215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // declared to have external linkage; or 216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // (there is no equivalent in C99) 2174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Context.getLangOpts().CPlusPlus && 218e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman Var->getType().isConstant(Context) && 219d931b086984257de68868a64a235c2b4b34003fbJohn McCall Var->getStorageClass() != SC_Extern && 220d931b086984257de68868a64a235c2b4b34003fbJohn McCall Var->getStorageClass() != SC_PrivateExtern) { 221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor bool FoundExtern = false; 222ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor for (const VarDecl *PrevVar = Var->getPreviousDecl(); 223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor PrevVar && !FoundExtern; 224ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor PrevVar = PrevVar->getPreviousDecl()) 2250b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor if (isExternalLinkage(PrevVar->getLinkage())) 226d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor FoundExtern = true; 227d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 228d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor if (!FoundExtern) 229af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::internal(); 230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } 231c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian if (Var->getStorageClass() == SC_None) { 232ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor const VarDecl *PrevVar = Var->getPreviousDecl(); 233ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor for (; PrevVar; PrevVar = PrevVar->getPreviousDecl()) 234c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian if (PrevVar->getStorageClass() == SC_PrivateExtern) 235c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian break; 236c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian if (PrevVar) 237c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian return PrevVar->getLinkageAndVisibility(); 238c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian } 239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { 2400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor // C++ [temp]p4: 2410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor // A non-member function template can have internal linkage; any 2420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor // other template name shall have external linkage. 243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor const FunctionDecl *Function = 0; 244d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor if (const FunctionTemplateDecl *FunTmpl 245d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor = dyn_cast<FunctionTemplateDecl>(D)) 246d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor Function = FunTmpl->getTemplatedDecl(); 247d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor else 248d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor Function = cast<FunctionDecl>(D); 249d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 250d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // Explicitly declared static. 251d931b086984257de68868a64a235c2b4b34003fbJohn McCall if (Function->getStorageClass() == SC_Static) 252af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo(InternalLinkage, DefaultVisibility, false); 253d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { 254d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // - a data member of an anonymous union. 255d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) 256af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::internal(); 257d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } 258d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 259094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth if (D->isInAnonymousNamespace()) { 260094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth const VarDecl *Var = dyn_cast<VarDecl>(D); 261094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth const FunctionDecl *Func = dyn_cast<FunctionDecl>(D); 262750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman if ((!Var || !Var->getDeclContext()->isExternCContext()) && 263750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman (!Func || !Func->getDeclContext()->isExternCContext())) 264094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth return LinkageInfo::uniqueExternal(); 265094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth } 266e7bc9722c807030409178d4af8ce8d1260bbd821John McCall 2671fb0caaa7bef765b85972274e3b434af2572c141John McCall // Set up the defaults. 2681fb0caaa7bef765b85972274e3b434af2572c141John McCall 2691fb0caaa7bef765b85972274e3b434af2572c141John McCall // C99 6.2.2p5: 2701fb0caaa7bef765b85972274e3b434af2572c141John McCall // If the declaration of an identifier for an object has file 2711fb0caaa7bef765b85972274e3b434af2572c141John McCall // scope and no storage-class specifier, its linkage is 2721fb0caaa7bef765b85972274e3b434af2572c141John McCall // external. 273af14603ca61757cf4361b583b45639a04c57e651John McCall LinkageInfo LV; 2744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie LV.mergeVisibility(Context.getLangOpts().getVisibilityMode()); 275af14603ca61757cf4361b583b45639a04c57e651John McCall 276e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola if (F.ConsiderVisibilityAttributes) { 277e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) { 278e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola LV.setVisibility(*Vis, true); 279e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola } else { 280e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola // If we're declared in a namespace with a visibility attribute, 281e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola // use that namespace's visibility, but don't call it explicit. 282e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola for (const DeclContext *DC = D->getDeclContext(); 283e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola !isa<TranslationUnitDecl>(DC); 284e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola DC = DC->getParent()) { 285e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC); 286e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola if (!ND) continue; 287e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) { 288e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola LV.setVisibility(*Vis, true); 289e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola break; 290e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola } 291e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola } 292e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola } 293e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola } 294e9836a27cf93a58305ff3cf6d75ddc399c7d8ebfRafael Espindola 295d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // C++ [basic.link]p4: 2961fb0caaa7bef765b85972274e3b434af2572c141John McCall 297d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // A name having namespace scope has external linkage if it is the 298d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // name of 299d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // 300d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // - an object or reference, unless it has internal linkage; or 301d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { 302110e8e56af30363072c140285961592b0107f789John McCall // GCC applies the following optimization to variables and static 303110e8e56af30363072c140285961592b0107f789John McCall // data members, but not to functions: 304110e8e56af30363072c140285961592b0107f789John McCall // 3051fb0caaa7bef765b85972274e3b434af2572c141John McCall // Modify the variable's LV by the LV of its type unless this is 3061fb0caaa7bef765b85972274e3b434af2572c141John McCall // C or extern "C". This follows from [basic.link]p9: 3071fb0caaa7bef765b85972274e3b434af2572c141John McCall // A type without linkage shall not be used as the type of a 3081fb0caaa7bef765b85972274e3b434af2572c141John McCall // variable or function with external linkage unless 3091fb0caaa7bef765b85972274e3b434af2572c141John McCall // - the entity has C language linkage, or 3101fb0caaa7bef765b85972274e3b434af2572c141John McCall // - the entity is declared within an unnamed namespace, or 3111fb0caaa7bef765b85972274e3b434af2572c141John McCall // - the entity is not used or is defined in the same 3121fb0caaa7bef765b85972274e3b434af2572c141John McCall // translation unit. 3131fb0caaa7bef765b85972274e3b434af2572c141John McCall // and [basic.link]p10: 3141fb0caaa7bef765b85972274e3b434af2572c141John McCall // ...the types specified by all declarations referring to a 3151fb0caaa7bef765b85972274e3b434af2572c141John McCall // given variable or function shall be identical... 3161fb0caaa7bef765b85972274e3b434af2572c141John McCall // C does not have an equivalent rule. 3171fb0caaa7bef765b85972274e3b434af2572c141John McCall // 318ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall // Ignore this if we've got an explicit attribute; the user 319ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall // probably knows what they're doing. 320ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall // 3211fb0caaa7bef765b85972274e3b434af2572c141John McCall // Note that we don't want to make the variable non-external 3221fb0caaa7bef765b85972274e3b434af2572c141John McCall // because of this, but unique-external linkage suits us. 3234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Context.getLangOpts().CPlusPlus && 324750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman !Var->getDeclContext()->isExternCContext()) { 325093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola LinkageInfo TypeLV = getLVForType(Var->getType()); 326093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola if (TypeLV.linkage() != ExternalLinkage) 327af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::uniqueExternal(); 32854881cb78e2c9054188c002c012b72175429e79bRafael Espindola LV.mergeVisibilityWithMin(TypeLV); 329110e8e56af30363072c140285961592b0107f789John McCall } 330110e8e56af30363072c140285961592b0107f789John McCall 33135cebc3eea898637057b10b5cf7dd08b1d788980John McCall if (Var->getStorageClass() == SC_PrivateExtern) 33235cebc3eea898637057b10b5cf7dd08b1d788980John McCall LV.setVisibility(HiddenVisibility, true); 33335cebc3eea898637057b10b5cf7dd08b1d788980John McCall 3344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Context.getLangOpts().CPlusPlus && 335d931b086984257de68868a64a235c2b4b34003fbJohn McCall (Var->getStorageClass() == SC_Extern || 336d931b086984257de68868a64a235c2b4b34003fbJohn McCall Var->getStorageClass() == SC_PrivateExtern)) { 3371fb0caaa7bef765b85972274e3b434af2572c141John McCall 338d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // C99 6.2.2p4: 339d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // For an identifier declared with the storage-class specifier 340d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // extern in a scope in which a prior declaration of that 341d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // identifier is visible, if the prior declaration specifies 342d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // internal or external linkage, the linkage of the identifier 343d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // at the later declaration is the same as the linkage 344d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // specified at the prior declaration. If no prior declaration 345d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // is visible, or if the prior declaration specifies no 346d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // linkage, then the identifier has external linkage. 347ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor if (const VarDecl *PrevVar = Var->getPreviousDecl()) { 348381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor LinkageInfo PrevLV = getLVForDecl(PrevVar, F); 349af14603ca61757cf4361b583b45639a04c57e651John McCall if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); 350af14603ca61757cf4361b583b45639a04c57e651John McCall LV.mergeVisibility(PrevLV); 351d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } 352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } 353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // - a function, unless it has internal linkage; or 3551fb0caaa7bef765b85972274e3b434af2572c141John McCall } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 35667fa6d5ea630c800c3c96e129129aba93d1487c2John McCall // In theory, we can modify the function's LV by the LV of its 35767fa6d5ea630c800c3c96e129129aba93d1487c2John McCall // type unless it has C linkage (see comment above about variables 35867fa6d5ea630c800c3c96e129129aba93d1487c2John McCall // for justification). In practice, GCC doesn't do this, so it's 35967fa6d5ea630c800c3c96e129129aba93d1487c2John McCall // just too painful to make work. 3601fb0caaa7bef765b85972274e3b434af2572c141John McCall 36135cebc3eea898637057b10b5cf7dd08b1d788980John McCall if (Function->getStorageClass() == SC_PrivateExtern) 36235cebc3eea898637057b10b5cf7dd08b1d788980John McCall LV.setVisibility(HiddenVisibility, true); 36335cebc3eea898637057b10b5cf7dd08b1d788980John McCall 364d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // C99 6.2.2p5: 365d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // If the declaration of an identifier for a function has no 366d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // storage-class specifier, its linkage is determined exactly 367d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // as if it were declared with the storage-class specifier 368d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // extern. 3694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Context.getLangOpts().CPlusPlus && 370d931b086984257de68868a64a235c2b4b34003fbJohn McCall (Function->getStorageClass() == SC_Extern || 371d931b086984257de68868a64a235c2b4b34003fbJohn McCall Function->getStorageClass() == SC_PrivateExtern || 372d931b086984257de68868a64a235c2b4b34003fbJohn McCall Function->getStorageClass() == SC_None)) { 373d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // C99 6.2.2p4: 374d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // For an identifier declared with the storage-class specifier 375d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // extern in a scope in which a prior declaration of that 376d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // identifier is visible, if the prior declaration specifies 377d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // internal or external linkage, the linkage of the identifier 378d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // at the later declaration is the same as the linkage 379d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // specified at the prior declaration. If no prior declaration 380d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // is visible, or if the prior declaration specifies no 381d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // linkage, then the identifier has external linkage. 382ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) { 383381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor LinkageInfo PrevLV = getLVForDecl(PrevFunc, F); 384af14603ca61757cf4361b583b45639a04c57e651John McCall if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); 385af14603ca61757cf4361b583b45639a04c57e651John McCall LV.mergeVisibility(PrevLV); 386d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } 387d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } 388d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 389af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall // In C++, then if the type of the function uses a type with 390af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall // unique-external linkage, it's not legally usable from outside 391af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall // this translation unit. However, we should use the C linkage 392af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall // rules instead for extern "C" declarations. 3934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Context.getLangOpts().CPlusPlus && 394750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman !Function->getDeclContext()->isExternCContext() && 395af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall Function->getType()->getLinkage() == UniqueExternalLinkage) 396af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall return LinkageInfo::uniqueExternal(); 397af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall 3986ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall // Consider LV from the template and the template arguments unless 3996ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall // this is an explicit specialization with a visibility attribute. 4006ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall if (FunctionTemplateSpecializationInfo *specInfo 4010b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor = Function->getTemplateSpecializationInfo()) { 4026ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall if (shouldConsiderTemplateLV(Function, specInfo)) { 4036ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall LV.merge(getLVForDecl(specInfo->getTemplate(), 40462d9f110b83dfa5dcd4a945e3b5f2e9c73d3aa4aRafael Espindola LVFlags::CreateOnlyDeclLinkage())); 4056ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; 406860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs, F)); 4076ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall } 4080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor } 4090b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 410d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // - a named class (Clause 9), or an unnamed class defined in a 411d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // typedef declaration in which the class has the typedef name 412d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // for linkage purposes (7.1.3); or 413d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // - a named enumeration (7.2), or an unnamed enumeration 414d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // defined in a typedef declaration in which the enumeration 415d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // has the typedef name for linkage purposes (7.1.3); or 4161fb0caaa7bef765b85972274e3b434af2572c141John McCall } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) { 4171fb0caaa7bef765b85972274e3b434af2572c141John McCall // Unnamed tags have no linkage. 418162e1c1b487352434552147967c3dd296ebee2f7Richard Smith if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) 419af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::none(); 4201fb0caaa7bef765b85972274e3b434af2572c141John McCall 4211fb0caaa7bef765b85972274e3b434af2572c141John McCall // If this is a class template specialization, consider the 4221fb0caaa7bef765b85972274e3b434af2572c141John McCall // linkage of the template and template arguments. 4236ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall if (const ClassTemplateSpecializationDecl *spec 4241fb0caaa7bef765b85972274e3b434af2572c141John McCall = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { 4256ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall if (shouldConsiderTemplateLV(spec)) { 4266ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall // From the template. 4276ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall LV.merge(getLVForDecl(spec->getSpecializedTemplate(), 42862d9f110b83dfa5dcd4a945e3b5f2e9c73d3aa4aRafael Espindola LVFlags::CreateOnlyDeclLinkage())); 4296ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall 4306ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall // The arguments at which the template was instantiated. 4316ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs(); 432860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs, F)); 4336ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall } 4340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor } 435d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 436d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // - an enumerator belonging to an enumeration with external linkage; 4371fb0caaa7bef765b85972274e3b434af2572c141John McCall } else if (isa<EnumConstantDecl>(D)) { 438381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F); 439af14603ca61757cf4361b583b45639a04c57e651John McCall if (!isExternalLinkage(EnumLV.linkage())) 440af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::none(); 441af14603ca61757cf4361b583b45639a04c57e651John McCall LV.merge(EnumLV); 442d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 443d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // - a template, unless it is a function template that has 444d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // internal linkage (Clause 14); 4451a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) { 4461a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall if (F.ConsiderTemplateParameterTypes) 4471a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters())); 4480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 449d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // - a namespace (7.3), unless it is declared within an unnamed 450d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // namespace. 4511fb0caaa7bef765b85972274e3b434af2572c141John McCall } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) { 4521fb0caaa7bef765b85972274e3b434af2572c141John McCall return LV; 4531fb0caaa7bef765b85972274e3b434af2572c141John McCall 4541fb0caaa7bef765b85972274e3b434af2572c141John McCall // By extension, we assign external linkage to Objective-C 4551fb0caaa7bef765b85972274e3b434af2572c141John McCall // interfaces. 4561fb0caaa7bef765b85972274e3b434af2572c141John McCall } else if (isa<ObjCInterfaceDecl>(D)) { 4571fb0caaa7bef765b85972274e3b434af2572c141John McCall // fallout 4581fb0caaa7bef765b85972274e3b434af2572c141John McCall 4591fb0caaa7bef765b85972274e3b434af2572c141John McCall // Everything not covered here has no linkage. 4601fb0caaa7bef765b85972274e3b434af2572c141John McCall } else { 461af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::none(); 4621fb0caaa7bef765b85972274e3b434af2572c141John McCall } 4631fb0caaa7bef765b85972274e3b434af2572c141John McCall 4641fb0caaa7bef765b85972274e3b434af2572c141John McCall // If we ended up with non-external linkage, visibility should 4651fb0caaa7bef765b85972274e3b434af2572c141John McCall // always be default. 466af14603ca61757cf4361b583b45639a04c57e651John McCall if (LV.linkage() != ExternalLinkage) 467af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo(LV.linkage(), DefaultVisibility, false); 4681fb0caaa7bef765b85972274e3b434af2572c141John McCall 4691fb0caaa7bef765b85972274e3b434af2572c141John McCall return LV; 470d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor} 471d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 4723698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) { 4731fb0caaa7bef765b85972274e3b434af2572c141John McCall // Only certain class members have linkage. Note that fields don't 4741fb0caaa7bef765b85972274e3b434af2572c141John McCall // really have linkage, but it's convenient to say they do for the 4751fb0caaa7bef765b85972274e3b434af2572c141John McCall // purposes of calculating linkage of pointer-to-data-member 4761fb0caaa7bef765b85972274e3b434af2572c141John McCall // template arguments. 4773cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall if (!(isa<CXXMethodDecl>(D) || 4783cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall isa<VarDecl>(D) || 4791fb0caaa7bef765b85972274e3b434af2572c141John McCall isa<FieldDecl>(D) || 4803cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall (isa<TagDecl>(D) && 481162e1c1b487352434552147967c3dd296ebee2f7Richard Smith (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl())))) 482af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::none(); 4833cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall 4843698748400478880d2a146ef9eaa111cd0e60522John McCall LinkageInfo LV; 4854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode()); 4863698748400478880d2a146ef9eaa111cd0e60522John McCall 4870f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola bool DHasExplicitVisibility = false; 4883698748400478880d2a146ef9eaa111cd0e60522John McCall // If we have an explicit visibility attribute, merge that in. 4893698748400478880d2a146ef9eaa111cd0e60522John McCall if (F.ConsiderVisibilityAttributes) { 4904421d2b341d041df44013769f23c306308bbab83Douglas Gregor if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) { 4914421d2b341d041df44013769f23c306308bbab83Douglas Gregor LV.mergeVisibility(*Vis, true); 4923698748400478880d2a146ef9eaa111cd0e60522John McCall 4930f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola DHasExplicitVisibility = true; 4940f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola } 4950f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola } 4960f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola // Ignore both global visibility and attributes when computing our 4970f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola // parent's visibility if we already have an explicit one. 4980f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola LVFlags ClassF = DHasExplicitVisibility ? 4990f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola LVFlags::CreateOnlyDeclLinkage() : F; 5003698748400478880d2a146ef9eaa111cd0e60522John McCall 5010f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola // If we're paying attention to global visibility, apply 5020f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola // -finline-visibility-hidden if this is an inline method. 5030f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola // 5040f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola // Note that we do this before merging information about 5050f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola // the class visibility. 5060f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 5070f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola TemplateSpecializationKind TSK = TSK_Undeclared; 5080f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola if (FunctionTemplateSpecializationInfo *spec 5090f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola = MD->getTemplateSpecializationInfo()) { 5100f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola TSK = spec->getTemplateSpecializationKind(); 5110f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola } else if (MemberSpecializationInfo *MSI = 5120f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola MD->getMemberSpecializationInfo()) { 5130f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola TSK = MSI->getTemplateSpecializationKind(); 5143698748400478880d2a146ef9eaa111cd0e60522John McCall } 5150f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola 5160f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola const FunctionDecl *Def = 0; 5170f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola // InlineVisibilityHidden only applies to definitions, and 5180f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola // isInlined() only gives meaningful answers on definitions 5190f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola // anyway. 5200f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola if (TSK != TSK_ExplicitInstantiationDeclaration && 5210f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola TSK != TSK_ExplicitInstantiationDefinition && 5220f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola F.ConsiderGlobalVisibility && 5230f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola !LV.visibilityExplicit() && 5240f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola MD->getASTContext().getLangOpts().InlineVisibilityHidden && 5250f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola MD->hasBody(Def) && Def->isInlined()) 5260f90590c96375052c67116f620fafa2b1eadb41eRafael Espindola LV.mergeVisibility(HiddenVisibility, true); 5273698748400478880d2a146ef9eaa111cd0e60522John McCall } 528af14603ca61757cf4361b583b45639a04c57e651John McCall 529af14603ca61757cf4361b583b45639a04c57e651John McCall // Class members only have linkage if their class has external 5303698748400478880d2a146ef9eaa111cd0e60522John McCall // linkage. 5313698748400478880d2a146ef9eaa111cd0e60522John McCall LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF)); 5323698748400478880d2a146ef9eaa111cd0e60522John McCall if (!isExternalLinkage(LV.linkage())) 533af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::none(); 5343cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall 5353cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall // If the class already has unique-external linkage, we can't improve. 5363698748400478880d2a146ef9eaa111cd0e60522John McCall if (LV.linkage() == UniqueExternalLinkage) 537af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::uniqueExternal(); 5381fb0caaa7bef765b85972274e3b434af2572c141John McCall 5393cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 540af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall // If the type of the function uses a type with unique-external 541af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall // linkage, it's not legally usable from outside this translation unit. 542af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall if (MD->getType()->getLinkage() == UniqueExternalLinkage) 543af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall return LinkageInfo::uniqueExternal(); 544af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall 5451fb0caaa7bef765b85972274e3b434af2572c141John McCall // If this is a method template specialization, use the linkage for 5461fb0caaa7bef765b85972274e3b434af2572c141John McCall // the template parameters and arguments. 5476ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall if (FunctionTemplateSpecializationInfo *spec 5483cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall = MD->getTemplateSpecializationInfo()) { 5496ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall if (shouldConsiderTemplateLV(MD, spec)) { 550860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments, 551860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola F)); 5526ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall if (F.ConsiderTemplateParameterTypes) 5536ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall LV.merge(getLVForTemplateParameterList( 5546ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall spec->getTemplate()->getTemplateParameters())); 5556ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall } 55666cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall } 5571fb0caaa7bef765b85972274e3b434af2572c141John McCall 558110e8e56af30363072c140285961592b0107f789John McCall // Note that in contrast to basically every other situation, we 559110e8e56af30363072c140285961592b0107f789John McCall // *do* apply -fvisibility to method declarations. 560110e8e56af30363072c140285961592b0107f789John McCall 561110e8e56af30363072c140285961592b0107f789John McCall } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 5626ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall if (const ClassTemplateSpecializationDecl *spec 563110e8e56af30363072c140285961592b0107f789John McCall = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { 5646ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall if (shouldConsiderTemplateLV(spec)) { 5656ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall // Merge template argument/parameter information for member 5666ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall // class template specializations. 567860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(), 568860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola F)); 5691a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall if (F.ConsiderTemplateParameterTypes) 5701a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall LV.merge(getLVForTemplateParameterList( 5716ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall spec->getSpecializedTemplate()->getTemplateParameters())); 5726ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall } 573110e8e56af30363072c140285961592b0107f789John McCall } 574110e8e56af30363072c140285961592b0107f789John McCall 575110e8e56af30363072c140285961592b0107f789John McCall // Static data members. 576110e8e56af30363072c140285961592b0107f789John McCall } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 577ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall // Modify the variable's linkage by its type, but ignore the 578ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall // type's visibility unless it's a definition. 579093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola LinkageInfo TypeLV = getLVForType(VD->getType()); 580093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola if (TypeLV.linkage() != ExternalLinkage) 581af14603ca61757cf4361b583b45639a04c57e651John McCall LV.mergeLinkage(UniqueExternalLinkage); 582af14603ca61757cf4361b583b45639a04c57e651John McCall if (!LV.visibilityExplicit()) 58354881cb78e2c9054188c002c012b72175429e79bRafael Espindola LV.mergeVisibility(TypeLV); 584110e8e56af30363072c140285961592b0107f789John McCall } 585110e8e56af30363072c140285961592b0107f789John McCall 5861fb0caaa7bef765b85972274e3b434af2572c141John McCall return LV; 5873cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall} 5883cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall 589f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallstatic void clearLinkageForClass(const CXXRecordDecl *record) { 590f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall for (CXXRecordDecl::decl_iterator 591f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall i = record->decls_begin(), e = record->decls_end(); i != e; ++i) { 592f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall Decl *child = *i; 593f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall if (isa<NamedDecl>(child)) 594f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall cast<NamedDecl>(child)->ClearLinkageCache(); 595f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall } 596f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall} 597f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall 59899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid NamedDecl::anchor() { } 59999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 600f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallvoid NamedDecl::ClearLinkageCache() { 601f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall // Note that we can't skip clearing the linkage of children just 602f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall // because the parent doesn't have cached linkage: we don't cache 603f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall // when computing linkage for parent contexts. 604f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall 605f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall HasCachedLinkage = 0; 606f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall 607f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall // If we're changing the linkage of a class, we need to reset the 608f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall // linkage of child declarations, too. 609f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this)) 610f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall clearLinkageForClass(record); 611f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall 61215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall if (ClassTemplateDecl *temp = 61315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) { 614f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall // Clear linkage for the template pattern. 615f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall CXXRecordDecl *record = temp->getTemplatedDecl(); 616f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall record->HasCachedLinkage = 0; 617f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall clearLinkageForClass(record); 618f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall 61915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall // We need to clear linkage for specializations, too. 62015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall for (ClassTemplateDecl::spec_iterator 62115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) 62215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall i->ClearLinkageCache(); 623f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall } 62415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall 62515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall // Clear cached linkage for function template decls, too. 62615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall if (FunctionTemplateDecl *temp = 62778951941f31d3c63c4178a1275e1a2db2e20da11John McCall dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) { 62878951941f31d3c63c4178a1275e1a2db2e20da11John McCall temp->getTemplatedDecl()->ClearLinkageCache(); 62915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall for (FunctionTemplateDecl::spec_iterator 63015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) 63115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall i->ClearLinkageCache(); 63278951941f31d3c63c4178a1275e1a2db2e20da11John McCall } 63315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall 634f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall} 635f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall 636381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorLinkage NamedDecl::getLinkage() const { 637381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor if (HasCachedLinkage) { 63856ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer assert(Linkage(CachedLinkage) == 63956ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage()); 640381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor return Linkage(CachedLinkage); 641381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor } 642381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor 643381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor CachedLinkage = getLVForDecl(this, 644381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor LVFlags::CreateOnlyDeclLinkage()).linkage(); 645381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor HasCachedLinkage = 1; 646381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor return Linkage(CachedLinkage); 647381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor} 648381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor 649af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const { 650381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor LinkageInfo LI = getLVForDecl(this, LVFlags()); 65156ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage()); 652381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor HasCachedLinkage = 1; 653381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor CachedLinkage = LI.linkage(); 654381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor return LI; 6550df9587ab011c12968fcbe3518666b2117afe350John McCall} 656becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek 6574421d2b341d041df44013769f23c306308bbab83Douglas Gregorllvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const { 6584421d2b341d041df44013769f23c306308bbab83Douglas Gregor // Use the most recent declaration of a variable. 6594421d2b341d041df44013769f23c306308bbab83Douglas Gregor if (const VarDecl *var = dyn_cast<VarDecl>(this)) 660ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor return getVisibilityOf(var->getMostRecentDecl()); 6614421d2b341d041df44013769f23c306308bbab83Douglas Gregor 6624421d2b341d041df44013769f23c306308bbab83Douglas Gregor // Use the most recent declaration of a function, and also handle 6634421d2b341d041df44013769f23c306308bbab83Douglas Gregor // function template specializations. 6644421d2b341d041df44013769f23c306308bbab83Douglas Gregor if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) { 6654421d2b341d041df44013769f23c306308bbab83Douglas Gregor if (llvm::Optional<Visibility> V 666ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor = getVisibilityOf(fn->getMostRecentDecl())) 6674421d2b341d041df44013769f23c306308bbab83Douglas Gregor return V; 6684421d2b341d041df44013769f23c306308bbab83Douglas Gregor 6694421d2b341d041df44013769f23c306308bbab83Douglas Gregor // If the function is a specialization of a template with an 6704421d2b341d041df44013769f23c306308bbab83Douglas Gregor // explicit visibility attribute, use that. 6714421d2b341d041df44013769f23c306308bbab83Douglas Gregor if (FunctionTemplateSpecializationInfo *templateInfo 6724421d2b341d041df44013769f23c306308bbab83Douglas Gregor = fn->getTemplateSpecializationInfo()) 6734421d2b341d041df44013769f23c306308bbab83Douglas Gregor return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl()); 6744421d2b341d041df44013769f23c306308bbab83Douglas Gregor 675860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola // If the function is a member of a specialization of a class template 676860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola // and the corresponding decl has explicit visibility, use that. 677860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); 678860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola if (InstantiatedFrom) 679860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola return getVisibilityOf(InstantiatedFrom); 680860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola 6814421d2b341d041df44013769f23c306308bbab83Douglas Gregor return llvm::Optional<Visibility>(); 6824421d2b341d041df44013769f23c306308bbab83Douglas Gregor } 6834421d2b341d041df44013769f23c306308bbab83Douglas Gregor 6844421d2b341d041df44013769f23c306308bbab83Douglas Gregor // Otherwise, just check the declaration itself first. 6854421d2b341d041df44013769f23c306308bbab83Douglas Gregor if (llvm::Optional<Visibility> V = getVisibilityOf(this)) 6864421d2b341d041df44013769f23c306308bbab83Douglas Gregor return V; 6874421d2b341d041df44013769f23c306308bbab83Douglas Gregor 6884421d2b341d041df44013769f23c306308bbab83Douglas Gregor // If there wasn't explicit visibility there, and this is a 6894421d2b341d041df44013769f23c306308bbab83Douglas Gregor // specialization of a class template, check for visibility 6904421d2b341d041df44013769f23c306308bbab83Douglas Gregor // on the pattern. 6914421d2b341d041df44013769f23c306308bbab83Douglas Gregor if (const ClassTemplateSpecializationDecl *spec 6924421d2b341d041df44013769f23c306308bbab83Douglas Gregor = dyn_cast<ClassTemplateSpecializationDecl>(this)) 6934421d2b341d041df44013769f23c306308bbab83Douglas Gregor return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl()); 6944421d2b341d041df44013769f23c306308bbab83Douglas Gregor 695860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola // If this is a member class of a specialization of a class template 696860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola // and the corresponding decl has explicit visibility, use that. 697860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) { 698860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); 699860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola if (InstantiatedFrom) 700860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola return getVisibilityOf(InstantiatedFrom); 701860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola } 702860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola 7034421d2b341d041df44013769f23c306308bbab83Douglas Gregor return llvm::Optional<Visibility>(); 7044421d2b341d041df44013769f23c306308bbab83Douglas Gregor} 7054421d2b341d041df44013769f23c306308bbab83Douglas Gregor 7063698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) { 707becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek // Objective-C: treat all Objective-C declarations as having external 708becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek // linkage. 7090df9587ab011c12968fcbe3518666b2117afe350John McCall switch (D->getKind()) { 710becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek default: 711becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek break; 712f8d34ed0d0933350323d9f7a8521011d73dc98d5Argyrios Kyrtzidis case Decl::ParmVar: 713f8d34ed0d0933350323d9f7a8521011d73dc98d5Argyrios Kyrtzidis return LinkageInfo::none(); 7141fb0caaa7bef765b85972274e3b434af2572c141John McCall case Decl::TemplateTemplateParm: // count these as external 7151fb0caaa7bef765b85972274e3b434af2572c141John McCall case Decl::NonTypeTemplateParm: 716becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek case Decl::ObjCAtDefsField: 717becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek case Decl::ObjCCategory: 718becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek case Decl::ObjCCategoryImpl: 719becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek case Decl::ObjCCompatibleAlias: 720becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek case Decl::ObjCImplementation: 721becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek case Decl::ObjCMethod: 722becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek case Decl::ObjCProperty: 723becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek case Decl::ObjCPropertyImpl: 724becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek case Decl::ObjCProtocol: 725af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::external(); 7265878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor 7275878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor case Decl::CXXRecord: { 7285878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor const CXXRecordDecl *Record = cast<CXXRecordDecl>(D); 7295878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor if (Record->isLambda()) { 7305878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor if (!Record->getLambdaManglingNumber()) { 7315878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor // This lambda has no mangling number, so it's internal. 7325878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor return LinkageInfo::internal(); 7335878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor } 7345878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor 7355878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor // This lambda has its linkage/visibility determined by its owner. 7365878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7375878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor if (Decl *ContextDecl = Record->getLambdaContextDecl()) { 7385878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor if (isa<ParmVarDecl>(ContextDecl)) 7395878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor DC = ContextDecl->getDeclContext()->getRedeclContext(); 7405878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor else 7415878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor return getLVForDecl(cast<NamedDecl>(ContextDecl), Flags); 7425878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor } 7435878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor 7445878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) 7455878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor return getLVForDecl(ND, Flags); 7465878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor 7475878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor return LinkageInfo::external(); 7485878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor } 7495878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor 7505878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor break; 7515878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor } 752becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek } 753becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek 754d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // Handle linkage for namespace-scope names. 7550df9587ab011c12968fcbe3518666b2117afe350John McCall if (D->getDeclContext()->getRedeclContext()->isFileContext()) 7563698748400478880d2a146ef9eaa111cd0e60522John McCall return getLVForNamespaceScopeDecl(D, Flags); 757d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 758d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // C++ [basic.link]p5: 759d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // In addition, a member function, static data member, a named 760d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // class or enumeration of class scope, or an unnamed class or 761d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // enumeration defined in a class-scope typedef declaration such 762d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // that the class or enumeration has the typedef name for linkage 763d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // purposes (7.1.3), has external linkage if the name of the class 764d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // has external linkage. 7650df9587ab011c12968fcbe3518666b2117afe350John McCall if (D->getDeclContext()->isRecord()) 7663698748400478880d2a146ef9eaa111cd0e60522John McCall return getLVForClassMember(D, Flags); 767d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 768d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // C++ [basic.link]p6: 769d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // The name of a function declared in block scope and the name of 770d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // an object declared by a block scope extern declaration have 771d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // linkage. If there is a visible declaration of an entity with 772d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // linkage having the same name and type, ignoring entities 773d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // declared outside the innermost enclosing namespace scope, the 774d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // block scope declaration declares that same entity and receives 775d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // the linkage of the previous declaration. If there is more than 776d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // one such matching entity, the program is ill-formed. Otherwise, 777d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // if no matching entity is found, the block scope entity receives 778d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // external linkage. 7790df9587ab011c12968fcbe3518666b2117afe350John McCall if (D->getLexicalDeclContext()->isFunctionOrMethod()) { 7800df9587ab011c12968fcbe3518666b2117afe350John McCall if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 781750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman if (Function->isInAnonymousNamespace() && 782750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman !Function->getDeclContext()->isExternCContext()) 783af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::uniqueExternal(); 7841fb0caaa7bef765b85972274e3b434af2572c141John McCall 785af14603ca61757cf4361b583b45639a04c57e651John McCall LinkageInfo LV; 786381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor if (Flags.ConsiderVisibilityAttributes) { 7874421d2b341d041df44013769f23c306308bbab83Douglas Gregor if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility()) 788074c1919167a272860a65f861c81d7d3ff37cd72Rafael Espindola LV.setVisibility(*Vis, true); 789381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor } 790381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor 791ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor if (const FunctionDecl *Prev = Function->getPreviousDecl()) { 792381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor LinkageInfo PrevLV = getLVForDecl(Prev, Flags); 793af14603ca61757cf4361b583b45639a04c57e651John McCall if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); 794af14603ca61757cf4361b583b45639a04c57e651John McCall LV.mergeVisibility(PrevLV); 7951fb0caaa7bef765b85972274e3b434af2572c141John McCall } 7960b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 7971fb0caaa7bef765b85972274e3b434af2572c141John McCall return LV; 798d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } 799d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 8000df9587ab011c12968fcbe3518666b2117afe350John McCall if (const VarDecl *Var = dyn_cast<VarDecl>(D)) 801d931b086984257de68868a64a235c2b4b34003fbJohn McCall if (Var->getStorageClass() == SC_Extern || 802d931b086984257de68868a64a235c2b4b34003fbJohn McCall Var->getStorageClass() == SC_PrivateExtern) { 803750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman if (Var->isInAnonymousNamespace() && 804750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman !Var->getDeclContext()->isExternCContext()) 805af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::uniqueExternal(); 8061fb0caaa7bef765b85972274e3b434af2572c141John McCall 807af14603ca61757cf4361b583b45639a04c57e651John McCall LinkageInfo LV; 8081fb0caaa7bef765b85972274e3b434af2572c141John McCall if (Var->getStorageClass() == SC_PrivateExtern) 809074c1919167a272860a65f861c81d7d3ff37cd72Rafael Espindola LV.setVisibility(HiddenVisibility, true); 810381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor else if (Flags.ConsiderVisibilityAttributes) { 8114421d2b341d041df44013769f23c306308bbab83Douglas Gregor if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility()) 812074c1919167a272860a65f861c81d7d3ff37cd72Rafael Espindola LV.setVisibility(*Vis, true); 813381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor } 814381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor 815ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor if (const VarDecl *Prev = Var->getPreviousDecl()) { 816381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor LinkageInfo PrevLV = getLVForDecl(Prev, Flags); 817af14603ca61757cf4361b583b45639a04c57e651John McCall if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); 818af14603ca61757cf4361b583b45639a04c57e651John McCall LV.mergeVisibility(PrevLV); 8191fb0caaa7bef765b85972274e3b434af2572c141John McCall } 8201fb0caaa7bef765b85972274e3b434af2572c141John McCall 8211fb0caaa7bef765b85972274e3b434af2572c141John McCall return LV; 822d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } 823d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor } 824d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 825d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // C++ [basic.link]p6: 826d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor // Names not covered by these rules have no linkage. 827af14603ca61757cf4361b583b45639a04c57e651John McCall return LinkageInfo::none(); 8281fb0caaa7bef765b85972274e3b434af2572c141John McCall} 829d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor 83047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const { 831ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor return getQualifiedNameAsString(getASTContext().getPrintingPolicy()); 8323a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson} 8333a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson 8343a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { 83547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor const DeclContext *Ctx = getDeclContext(); 83647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor 83747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor if (Ctx->isFunctionOrMethod()) 83847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor return getNameAsString(); 83947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor 8405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner typedef SmallVector<const DeclContext *, 8> ContextsTy; 84168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer ContextsTy Contexts; 84268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer 84368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer // Collect contexts. 84468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer while (Ctx && isa<NamedDecl>(Ctx)) { 84568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer Contexts.push_back(Ctx); 84668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer Ctx = Ctx->getParent(); 84768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer }; 84868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer 84968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer std::string QualName; 85068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer llvm::raw_string_ostream OS(QualName); 85168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer 85268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); 85368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer I != E; ++I) { 8541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const ClassTemplateSpecializationDecl *Spec 85568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { 856f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 857f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor std::string TemplateArgsStr 858f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor = TemplateSpecializationType::PrintTemplateArgumentList( 859910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor TemplateArgs.data(), 860910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor TemplateArgs.size(), 8613a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson P); 86268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer OS << Spec->getName() << TemplateArgsStr; 86368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { 8646be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig if (ND->isAnonymousNamespace()) 86568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer OS << "<anonymous namespace>"; 8666be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig else 867b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer OS << *ND; 86868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { 86968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer if (!RD->getIdentifier()) 87068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer OS << "<anonymous " << RD->getKindName() << '>'; 87168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer else 872b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer OS << *RD; 87368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { 8743521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig const FunctionProtoType *FT = 0; 8753521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig if (FD->hasWrittenPrototype()) 8763521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>()); 8773521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig 878b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer OS << *FD << '('; 8793521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig if (FT) { 8803521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig unsigned NumParams = FD->getNumParams(); 8813521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig for (unsigned i = 0; i < NumParams; ++i) { 8823521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig if (i) 88368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer OS << ", "; 8843521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig std::string Param; 8853521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig FD->getParamDecl(i)->getType().getAsStringInternal(Param, P); 88668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer OS << Param; 8873521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig } 8883521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig 8893521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig if (FT->isVariadic()) { 8903521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig if (NumParams > 0) 89168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer OS << ", "; 89268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer OS << "..."; 8933521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig } 8943521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig } 89568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer OS << ')'; 89668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer } else { 897b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer OS << *cast<NamedDecl>(*I); 89868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer } 89968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer OS << "::"; 90047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor } 90147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor 9028472af4df9292e02fb25c952d25a81f3ca296252John McCall if (getDeclName()) 903b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer OS << *this; 9048472af4df9292e02fb25c952d25a81f3ca296252John McCall else 90568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer OS << "<anonymous>"; 90647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor 90768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer return OS.str(); 90847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor} 90947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor 9104afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const { 9116ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); 9126ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor 9132a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. 9142a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // We want to keep it, unless it nominates same namespace. 9152a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor if (getKind() == Decl::UsingDirective) { 916db9924191092b4d426cc066637d81698211846aaDouglas Gregor return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() 917db9924191092b4d426cc066637d81698211846aaDouglas Gregor ->getOriginalNamespace() == 918db9924191092b4d426cc066637d81698211846aaDouglas Gregor cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace() 919db9924191092b4d426cc066637d81698211846aaDouglas Gregor ->getOriginalNamespace(); 9202a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor } 9211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9226ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) 9236ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor // For function declarations, we keep track of redeclarations. 924ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor return FD->getPreviousDecl() == OldD; 9256ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor 926e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // For function templates, the underlying function declarations are linked. 927e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (const FunctionTemplateDecl *FunctionTemplate 928e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor = dyn_cast<FunctionTemplateDecl>(this)) 929e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (const FunctionTemplateDecl *OldFunctionTemplate 930e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor = dyn_cast<FunctionTemplateDecl>(OldD)) 931e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return FunctionTemplate->getTemplatedDecl() 932e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); 9331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9340de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff // For method declarations, we keep track of redeclarations. 9350de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff if (isa<ObjCMethodDecl>(this)) 9360de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff return false; 9371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 938f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) 939f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return true; 940f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall 9419488ea120e093068021f944176c3d610dd540914John McCall if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) 9429488ea120e093068021f944176c3d610dd540914John McCall return cast<UsingShadowDecl>(this)->getTargetDecl() == 9439488ea120e093068021f944176c3d610dd540914John McCall cast<UsingShadowDecl>(OldD)->getTargetDecl(); 9449488ea120e093068021f944176c3d610dd540914John McCall 945dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) { 946dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor ASTContext &Context = getASTContext(); 947dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor return Context.getCanonicalNestedNameSpecifier( 948dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor cast<UsingDecl>(this)->getQualifier()) == 949dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor Context.getCanonicalNestedNameSpecifier( 950dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor cast<UsingDecl>(OldD)->getQualifier()); 951dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor } 952c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis 9537a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor // A typedef of an Objective-C class type can replace an Objective-C class 9547a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor // declaration or definition, and vice versa. 9557a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) || 9567a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD))) 9577a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor return true; 9587a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor 9596ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor // For non-function declarations, if the declarations are of the 9606ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor // same kind then this must be a redeclaration, or semantic analysis 9616ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor // would not have given us the new declaration. 9626ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor return this->getKind() == OldD->getKind(); 9636ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor} 9646ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor 965d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const { 966d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor return getLinkage() != NoLinkage; 967d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor} 9684afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor 9696daffa5d6031eee8b25fc2c745dd6b58b039a6ebDaniel DunbarNamedDecl *NamedDecl::getUnderlyingDeclImpl() { 970e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson NamedDecl *ND = this; 97156757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) 97256757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer ND = UD->getTargetDecl(); 97356757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer 97456757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) 97556757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer return AD->getClassInterface(); 97656757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer 97756757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer return ND; 978e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson} 979e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson 980161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const { 9815bc37f6e0c932e7a8e0af92b6266372dc7b94cd9Douglas Gregor if (!isCXXClassMember()) 9825bc37f6e0c932e7a8e0af92b6266372dc7b94cd9Douglas Gregor return false; 9835bc37f6e0c932e7a8e0af92b6266372dc7b94cd9Douglas Gregor 984161755a09898c95d21bfff33707da9ca41cd53c5John McCall const NamedDecl *D = this; 985161755a09898c95d21bfff33707da9ca41cd53c5John McCall if (isa<UsingShadowDecl>(D)) 986161755a09898c95d21bfff33707da9ca41cd53c5John McCall D = cast<UsingShadowDecl>(D)->getTargetDecl(); 987161755a09898c95d21bfff33707da9ca41cd53c5John McCall 98887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) 989161755a09898c95d21bfff33707da9ca41cd53c5John McCall return true; 990161755a09898c95d21bfff33707da9ca41cd53c5John McCall if (isa<CXXMethodDecl>(D)) 991161755a09898c95d21bfff33707da9ca41cd53c5John McCall return cast<CXXMethodDecl>(D)->isInstance(); 992161755a09898c95d21bfff33707da9ca41cd53c5John McCall if (isa<FunctionTemplateDecl>(D)) 993161755a09898c95d21bfff33707da9ca41cd53c5John McCall return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D) 994161755a09898c95d21bfff33707da9ca41cd53c5John McCall ->getTemplatedDecl())->isInstance(); 995161755a09898c95d21bfff33707da9ca41cd53c5John McCall return false; 996161755a09898c95d21bfff33707da9ca41cd53c5John McCall} 997161755a09898c95d21bfff33707da9ca41cd53c5John McCall 9985239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===// 999a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation 1000a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===// 1001a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis 10021693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT> 10031693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { 10041693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor if (decl->getNumTemplateParameterLists() > 0) 10051693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor return decl->getTemplateParameterList(0)->getTemplateLoc(); 10061693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor else 10071693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor return decl->getInnerLocStart(); 10081693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor} 10091693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor 1010a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { 10114e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall TypeSourceInfo *TSI = getTypeSourceInfo(); 10124e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall if (TSI) return TSI->getTypeLoc().getBeginLoc(); 1013a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis return SourceLocation(); 1014a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis} 1015a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis 1016c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { 1017c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor if (QualifierLoc) { 1018b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall // Make sure the extended decl info is allocated. 1019b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall if (!hasExtInfo()) { 1020b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall // Save (non-extended) type source info pointer. 1021b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); 1022b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall // Allocate external info struct. 1023b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall DeclInfo = new (getASTContext()) ExtInfo; 1024b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall // Restore savedTInfo into (extended) decl info. 1025b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall getExtInfo()->TInfo = savedTInfo; 1026b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall } 1027b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall // Set qualifier info. 1028c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor getExtInfo()->QualifierLoc = QualifierLoc; 10293060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier } else { 1030b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall // Here Qualifier == 0, i.e., we are removing the qualifier (if any). 1031b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall if (hasExtInfo()) { 10327f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara if (getExtInfo()->NumTemplParamLists == 0) { 10337f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Save type source info pointer. 10347f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; 10357f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Deallocate the extended decl info. 10367f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara getASTContext().Deallocate(getExtInfo()); 10377f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Restore savedTInfo into (non-extended) decl info. 10387f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara DeclInfo = savedTInfo; 10397f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara } 10407f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara else 10417f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara getExtInfo()->QualifierLoc = QualifierLoc; 1042b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall } 1043b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall } 1044b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall} 1045b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall 10467f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnaravoid 10477f0a915eb546d353071be08c8adec155e5d9a0dcAbramo BagnaraDeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context, 10487f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara unsigned NumTPLists, 10497f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara TemplateParameterList **TPLists) { 10507f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara assert(NumTPLists > 0); 10517f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Make sure the extended decl info is allocated. 10527f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara if (!hasExtInfo()) { 10537f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Save (non-extended) type source info pointer. 10547f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); 10557f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Allocate external info struct. 10567f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara DeclInfo = new (getASTContext()) ExtInfo; 10577f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Restore savedTInfo into (extended) decl info. 10587f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara getExtInfo()->TInfo = savedTInfo; 10597f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara } 10607f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Set the template parameter lists info. 10617f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); 10627f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara} 10637f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara 10641693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const { 10651693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor return getTemplateOrInnerLocStart(this); 10661693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor} 10671693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor 1068a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnaranamespace { 1069a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara 1070a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// Helper function: returns true if QT is or contains a type 1071a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// having a postfix component. 1072a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnarabool typeIsPostfix(clang::QualType QT) { 1073a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara while (true) { 1074a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara const Type* T = QT.getTypePtr(); 1075a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara switch (T->getTypeClass()) { 1076a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara default: 1077a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara return false; 1078a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::Pointer: 1079a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara QT = cast<PointerType>(T)->getPointeeType(); 1080a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara break; 1081a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::BlockPointer: 1082a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara QT = cast<BlockPointerType>(T)->getPointeeType(); 1083a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara break; 1084a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::MemberPointer: 1085a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara QT = cast<MemberPointerType>(T)->getPointeeType(); 1086a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara break; 1087a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::LValueReference: 1088a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::RValueReference: 1089a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara QT = cast<ReferenceType>(T)->getPointeeType(); 1090a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara break; 1091a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::PackExpansion: 1092a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara QT = cast<PackExpansionType>(T)->getPattern(); 1093a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara break; 1094a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::Paren: 1095a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::ConstantArray: 1096a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::DependentSizedArray: 1097a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::IncompleteArray: 1098a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::VariableArray: 1099a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::FunctionProto: 1100a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara case Type::FunctionNoProto: 1101a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara return true; 1102a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara } 1103a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara } 1104a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara} 1105a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara 1106a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara} // namespace 1107a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara 1108a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange DeclaratorDecl::getSourceRange() const { 1109a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara SourceLocation RangeEnd = getLocation(); 1110a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { 1111a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara if (typeIsPostfix(TInfo->getType())) 1112a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 1113a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara } 1114a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara return SourceRange(getOuterLocStart(), RangeEnd); 1115a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara} 1116a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara 11179b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid 1118c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, 1119c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor unsigned NumTPLists, 11209b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara TemplateParameterList **TPLists) { 11219b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara assert((NumTPLists == 0 || TPLists != 0) && 11229b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara "Empty array of template parameters with positive size!"); 11239b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara 11249b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara // Free previous template parameters (if any). 11259b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara if (NumTemplParamLists > 0) { 1126c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor Context.Deallocate(TemplParamLists); 11279b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara TemplParamLists = 0; 11289b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara NumTemplParamLists = 0; 11299b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara } 11309b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara // Set info on matched template parameter lists (if any). 11319b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara if (NumTPLists > 0) { 1132c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; 11339b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara NumTemplParamLists = NumTPLists; 11349b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara for (unsigned i = NumTPLists; i-- > 0; ) 11359b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara TemplParamLists[i] = TPLists[i]; 11369b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara } 11379b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara} 11389b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara 1139a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===// 114099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation 114199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===// 114299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes 11437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { 11447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl switch (SC) { 11458c25fc584ce27d59df9923f153e8a132dde58d04Peter Collingbourne case SC_None: break; 11468be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne case SC_Auto: return "auto"; 11478be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne case SC_Extern: return "extern"; 11488be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>"; 11498be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne case SC_PrivateExtern: return "__private_extern__"; 11508be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne case SC_Register: return "register"; 11518be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne case SC_Static: return "static"; 11527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl } 11537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 11548be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne llvm_unreachable("Invalid storage class"); 11557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 11567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 1157ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo BagnaraVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, 1158ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara SourceLocation StartL, SourceLocation IdL, 1159a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, 116016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor StorageClass S, StorageClass SCAsWritten) { 1161ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten); 116299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes} 116399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes 11641e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorVarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 11651e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl)); 11661e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0, 11671e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor QualType(), 0, SC_None, SC_None); 11681e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 11691e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 1170381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid VarDecl::setStorageClass(StorageClass SC) { 1171381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor assert(isLegalForVariable(SC)); 1172381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor if (getStorageClass() != SC) 1173381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor ClearLinkageCache(); 1174381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor 1175f1e4fbf3112f33ec5b7bc5c57ec148445190d0a8John McCall VarDeclBits.SClass = SC; 1176381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor} 1177381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor 11781693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const { 117955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis if (getInit()) 11801693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor return SourceRange(getOuterLocStart(), getInit()->getLocEnd()); 1181a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara return DeclaratorDecl::getSourceRange(); 118255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis} 118355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis 11847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const { 1185750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman if (getLinkage() != ExternalLinkage) 1186750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman return false; 11877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 118810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth const DeclContext *DC = getDeclContext(); 1189750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman if (DC->isRecord()) 119010aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth return false; 119110aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth 1192750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman ASTContext &Context = getASTContext(); 11934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Context.getLangOpts().CPlusPlus) 1194750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman return true; 1195750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman return DC->isExternCContext(); 11967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 11977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 11987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() { 11997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return getFirstDeclaration(); 12007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 12017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 12023d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel DunbarVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition( 12033d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar ASTContext &C) const 12043d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar{ 1205e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // C++ [basic.def]p2: 1206e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // A declaration is a definition unless [...] it contains the 'extern' 1207e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // specifier or a linkage-specification and neither an initializer [...], 1208e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // it declares a static data member in a class declaration [...]. 1209e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // C++ [temp.expl.spec]p15: 1210e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // An explicit specialization of a static data member of a template is a 1211e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // definition if the declaration includes an initializer; otherwise, it is 1212e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // a declaration. 1213e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl if (isStaticDataMember()) { 1214e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl if (isOutOfLine() && (hasInit() || 1215e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl getTemplateSpecializationKind() != TSK_ExplicitSpecialization)) 1216e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return Definition; 1217e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl else 1218e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return DeclarationOnly; 1219e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl } 1220e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // C99 6.7p5: 1221e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // A definition of an identifier is a declaration for that identifier that 1222e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // [...] causes storage to be reserved for that object. 1223e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // Note: that applies for all non-file-scope objects. 1224e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // C99 6.9.2p1: 1225e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // If the declaration of an identifier for an object has file scope and an 1226e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // initializer, the declaration is an external definition for the identifier 1227e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl if (hasInit()) 1228e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return Definition; 1229e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // AST for 'extern "C" int foo;' is annotated with 'extern'. 1230e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl if (hasExternalStorage()) 1231e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return DeclarationOnly; 12322bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian 1233d931b086984257de68868a64a235c2b4b34003fbJohn McCall if (getStorageClassAsWritten() == SC_Extern || 1234d931b086984257de68868a64a235c2b4b34003fbJohn McCall getStorageClassAsWritten() == SC_PrivateExtern) { 1235ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor for (const VarDecl *PrevVar = getPreviousDecl(); 1236ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor PrevVar; PrevVar = PrevVar->getPreviousDecl()) { 12372bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit()) 12382bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian return DeclarationOnly; 12392bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian } 12402bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian } 1241e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // C99 6.9.2p2: 1242e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // A declaration of an object that has file scope without an initializer, 1243e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // and without a storage class specifier or the scs 'static', constitutes 1244e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // a tentative definition. 1245e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // No such thing in C++. 12464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) 1247e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return TentativeDefinition; 1248e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl 1249e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // What's left is (in C, block-scope) declarations without initializers or 1250e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl // external storage. These are definitions. 1251e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return Definition; 1252e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl} 1253e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl 1254e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() { 1255e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl DefinitionKind Kind = isThisDeclarationADefinition(); 1256e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl if (Kind != TentativeDefinition) 1257e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return 0; 1258e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl 1259f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner VarDecl *LastTentative = 0; 1260e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl VarDecl *First = getFirstDeclaration(); 1261e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); 1262e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl I != E; ++I) { 1263e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl Kind = (*I)->isThisDeclarationADefinition(); 1264e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl if (Kind == Definition) 1265e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return 0; 1266e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl else if (Kind == TentativeDefinition) 1267e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl LastTentative = *I; 1268e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl } 1269e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return LastTentative; 1270e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl} 1271e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl 1272e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const { 1273e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl DefinitionKind Kind = isThisDeclarationADefinition(); 1274e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl if (Kind != TentativeDefinition) 1275e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return false; 1276e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl 1277e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 1278e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl if ((*I)->isThisDeclarationADefinition() == Definition) 1279e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl return false; 1280e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl } 128131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl return true; 128231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl} 128331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl 12843d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel DunbarVarDecl *VarDecl::getDefinition(ASTContext &C) { 1285e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl VarDecl *First = getFirstDeclaration(); 1286e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); 1287e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl I != E; ++I) { 12883d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar if ((*I)->isThisDeclarationADefinition(C) == Definition) 128931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl return *I; 129031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl } 129131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl return 0; 1292e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl} 1293e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl 12943d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel DunbarVarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { 1295110e8e56af30363072c140285961592b0107f789John McCall DefinitionKind Kind = DeclarationOnly; 1296110e8e56af30363072c140285961592b0107f789John McCall 1297110e8e56af30363072c140285961592b0107f789John McCall const VarDecl *First = getFirstDeclaration(); 1298110e8e56af30363072c140285961592b0107f789John McCall for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); 1299047da195aee797341c86d38cc7e3a7e619274dabDaniel Dunbar I != E; ++I) { 13003d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C)); 1301047da195aee797341c86d38cc7e3a7e619274dabDaniel Dunbar if (Kind == Definition) 1302047da195aee797341c86d38cc7e3a7e619274dabDaniel Dunbar break; 1303047da195aee797341c86d38cc7e3a7e619274dabDaniel Dunbar } 1304110e8e56af30363072c140285961592b0107f789John McCall 1305110e8e56af30363072c140285961592b0107f789John McCall return Kind; 1306110e8e56af30363072c140285961592b0107f789John McCall} 1307110e8e56af30363072c140285961592b0107f789John McCall 130831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { 13097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl redecl_iterator I = redecls_begin(), E = redecls_end(); 13107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl while (I != E && !I->getInit()) 13117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl ++I; 13127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 13137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl if (I != E) { 131431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl D = *I; 13157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return I->getInit(); 13167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl } 13177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return 0; 13187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 13197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 13201028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const { 1321da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor if (Decl::isOutOfLine()) 13221028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return true; 13238761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth 13248761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth if (!isStaticDataMember()) 13258761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth return false; 13268761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth 13271028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // If this static data member was instantiated from a static data member of 13281028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // a class template, check whether that static data member was defined 13291028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // out-of-line. 13301028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor if (VarDecl *VD = getInstantiatedFromStaticDataMember()) 13311028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return VD->isOutOfLine(); 13321028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 13331028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return false; 13341028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor} 13351028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 13360d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() { 13370d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor if (!isStaticDataMember()) 13380d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor return 0; 13390d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor 13400d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); 13410d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor RD != RDEnd; ++RD) { 13420d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor if (RD->getLexicalDeclContext()->isFileContext()) 13430d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor return *RD; 13440d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor } 13450d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor 13460d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor return 0; 13470d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor} 13480d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor 1349838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) { 13507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { 13517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl Eval->~EvaluatedStmt(); 1352838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor getASTContext().Deallocate(Eval); 13537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl } 13547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 13557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl Init = I; 13567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 13577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 13583d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbarbool VarDecl::isUsableInConstantExpressions(ASTContext &C) const { 13594e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &Lang = C.getLangOpts(); 13601d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith 136116581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith if (!Lang.CPlusPlus) 136216581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith return false; 136316581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith 136416581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith // In C++11, any variable of reference type can be used in a constant 136516581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith // expression if it is initialized by a constant expression. 136616581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith if (Lang.CPlusPlus0x && getType()->isReferenceType()) 136716581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith return true; 136816581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith 136916581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith // Only const objects can be used in constant expressions in C++. C++98 does 13701d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith // not require the variable to be non-volatile, but we consider this to be a 13711d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith // defect. 137216581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith if (!getType().isConstQualified() || getType().isVolatileQualified()) 13731d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith return false; 13741d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith 13751d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith // In C++, const, non-volatile variables of integral or enumeration types 13761d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith // can be used in constant expressions. 13771d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith if (getType()->isIntegralOrEnumerationType()) 13781d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith return true; 13791d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith 138016581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith // Additionally, in C++11, non-volatile constexpr variables can be used in 138116581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith // constant expressions. 138216581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith return Lang.CPlusPlus0x && isConstexpr(); 13831d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith} 13841d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith 1385099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith/// Convert the initializer for this declaration to the elaborated EvaluatedStmt 1386099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith/// form, which contains extra information on the evaluated value of the 1387099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith/// initializer. 1388099e7f647ccda915513f2b2ec53352dc756082d3Richard SmithEvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { 1389099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>(); 1390099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (!Eval) { 1391099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Stmt *S = Init.get<Stmt *>(); 1392099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval = new (getASTContext()) EvaluatedStmt; 1393099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->Value = S; 1394099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Init = Eval; 1395099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 1396099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return Eval; 1397099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith} 1398099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 13992d6a5670465cb3f1d811695a9f23e372508240d2Richard SmithAPValue *VarDecl::evaluateValue() const { 14002d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith llvm::SmallVector<PartialDiagnosticAt, 8> Notes; 14012d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return evaluateValue(Notes); 14022d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith} 14032d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 14042d6a5670465cb3f1d811695a9f23e372508240d2Richard SmithAPValue *VarDecl::evaluateValue( 14052d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const { 1406099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EvaluatedStmt *Eval = ensureEvaluatedStmt(); 1407099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1408099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // We only produce notes indicating why an initializer is non-constant the 1409099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // first time it is evaluated. FIXME: The notes won't always be emitted the 1410099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // first time we try evaluation, so might not be produced at all. 1411099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (Eval->WasEvaluated) 14122d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated; 1413099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1414099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const Expr *Init = cast<Expr>(Eval->Value); 1415099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith assert(!Init->isValueDependent()); 1416099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1417099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (Eval->IsEvaluating) { 1418099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // FIXME: Produce a diagnostic for self-initialization. 1419099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->CheckedICE = true; 1420099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->IsICE = false; 14212d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return 0; 1422099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 1423099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1424099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->IsEvaluating = true; 1425099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1426099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(), 1427099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith this, Notes); 1428099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1429099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // Ensure the result is an uninitialized APValue if evaluation fails. 1430099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (!Result) 1431099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->Evaluated = APValue(); 1432099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1433099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->IsEvaluating = false; 1434099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->WasEvaluated = true; 1435099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1436099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // In C++11, we have determined whether the initializer was a constant 1437099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // expression as a side-effect. 14384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) { 1439099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->CheckedICE = true; 1440210386eb619ea9feef425636150bdffc0538574dEli Friedman Eval->IsICE = Result && Notes.empty(); 1441099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 1442099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 14432d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return Result ? &Eval->Evaluated : 0; 1444099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith} 1445099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1446099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool VarDecl::checkInitIsICE() const { 144773076431605556fdbf28d287d084a73a24a8b8d4John McCall // Initializers of weak variables are never ICEs. 144873076431605556fdbf28d287d084a73a24a8b8d4John McCall if (isWeak()) 144973076431605556fdbf28d287d084a73a24a8b8d4John McCall return false; 145073076431605556fdbf28d287d084a73a24a8b8d4John McCall 1451099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EvaluatedStmt *Eval = ensureEvaluatedStmt(); 1452099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (Eval->CheckedICE) 1453099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // We have already checked whether this subexpression is an 1454099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // integral constant expression. 1455099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return Eval->IsICE; 1456099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1457099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const Expr *Init = cast<Expr>(Eval->Value); 1458099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith assert(!Init->isValueDependent()); 1459099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1460099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // In C++11, evaluate the initializer to check whether it's a constant 1461099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // expression. 14624e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getASTContext().getLangOpts().CPlusPlus0x) { 1463099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith llvm::SmallVector<PartialDiagnosticAt, 8> Notes; 1464099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith evaluateValue(Notes); 1465099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return Eval->IsICE; 1466099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 1467099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1468099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // It's an ICE whether or not the definition we found is 1469099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // out-of-line. See DR 721 and the discussion in Clang PR 1470099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // 6206 for details. 1471099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1472099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (Eval->CheckingICE) 1473099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return false; 1474099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->CheckingICE = true; 1475099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1476099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->IsICE = Init->isIntegerConstantExpr(getASTContext()); 1477099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->CheckingICE = false; 1478099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Eval->CheckedICE = true; 1479099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return Eval->IsICE; 1480099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith} 1481099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 148203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregorbool VarDecl::extendsLifetimeOfTemporary() const { 14830b5810882bd34183c2b764676cafa4c2ce324740Douglas Gregor assert(getType()->isReferenceType() &&"Non-references never extend lifetime"); 148403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor 148503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor const Expr *E = getInit(); 148603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor if (!E) 148703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor return false; 148803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor 148903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E)) 149003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor E = Cleanups->getSubExpr(); 149103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor 149203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor return isa<MaterializeTemporaryExpr>(E); 149303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor} 149403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor 14951028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { 1496b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 1497251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor return cast<VarDecl>(MSI->getInstantiatedFrom()); 1498251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor 1499251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor return 0; 1500251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor} 1501251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor 1502663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { 1503e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 1504251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor return MSI->getTemplateSpecializationKind(); 1505251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor 1506251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor return TSK_Undeclared; 1507251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor} 1508251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor 15091028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { 1510b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor return getASTContext().getInstantiatedFromStaticDataMember(this); 1511b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor} 1512b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor 15130a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 15140a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor SourceLocation PointOfInstantiation) { 1515b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); 1516251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor assert(MSI && "Not an instantiated static data member?"); 1517251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor MSI->setTemplateSpecializationKind(TSK); 15180a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor if (TSK != TSK_ExplicitSpecialization && 15190a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor PointOfInstantiation.isValid() && 15200a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor MSI->getPointOfInstantiation().isInvalid()) 15210a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor MSI->setPointOfInstantiation(PointOfInstantiation); 15227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor} 15237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 15247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===// 15257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation 15267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===// 1527275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor 15287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, 1529ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara SourceLocation StartLoc, 1530ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara SourceLocation IdLoc, IdentifierInfo *Id, 15317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl QualType T, TypeSourceInfo *TInfo, 153216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor StorageClass S, StorageClass SCAsWritten, 153316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor Expr *DefArg) { 1534ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo, 153516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor S, SCAsWritten, DefArg); 1536275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor} 1537275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor 15381e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 15391e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl)); 15401e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(), 15411e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 0, QualType(), 0, SC_None, SC_None, 0); 15421e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 15431e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 15440bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios KyrtzidisSourceRange ParmVarDecl::getSourceRange() const { 15450bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis if (!hasInheritedDefaultArg()) { 15460bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis SourceRange ArgRange = getDefaultArgRange(); 15470bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis if (ArgRange.isValid()) 15480bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis return SourceRange(getOuterLocStart(), ArgRange.getEnd()); 15490bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis } 15500bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis 15510bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis return DeclaratorDecl::getSourceRange(); 15520bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis} 15530bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis 15547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() { 15557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); 15567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl assert(!hasUninstantiatedDefaultArg() && 15577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl "Default argument is not yet instantiated!"); 15587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 15597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl Expr *Arg = getInit(); 15604765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg)) 15617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return E->getSubExpr(); 15627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 15637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return Arg; 15647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 15657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 15667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const { 15677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl if (const Expr *E = getInit()) 15687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return E->getSourceRange(); 15697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 15707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl if (hasUninstantiatedDefaultArg()) 15717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return getUninstantiatedDefaultArg()->getSourceRange(); 15727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 15737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return SourceRange(); 1574fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis} 1575fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis 15761fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool ParmVarDecl::isParameterPack() const { 15771fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor return isa<PackExpansionType>(getType()); 15781fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor} 15791fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor 1580d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekvoid ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { 1581d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek getASTContext().setParameterIndex(this, parameterIndex); 1582d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; 1583d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek} 1584d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek 1585d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekunsigned ParmVarDecl::getParameterIndexLarge() const { 1586d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek return getASTContext().getParameterIndex(this); 1587d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek} 1588d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek 158999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===// 15908a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation 15918a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===// 15928a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner 1593da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid FunctionDecl::getNameForDiagnostic(std::string &S, 1594da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor const PrintingPolicy &Policy, 1595da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor bool Qualified) const { 1596da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor NamedDecl::getNameForDiagnostic(S, Policy, Qualified); 1597da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); 1598da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor if (TemplateArgs) 1599da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor S += TemplateSpecializationType::PrintTemplateArgumentList( 1600da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor TemplateArgs->data(), 1601da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor TemplateArgs->size(), 1602da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor Policy); 1603da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor 1604da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor} 1605da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor 16069498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const { 16079498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) 16089498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek return FT->isVariadic(); 16099498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek return false; 16109498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek} 16119498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek 161206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { 161306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 16148387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet if (I->Body || I->IsLateTemplateParsed) { 161506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis Definition = *I; 161606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis return true; 161706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis } 161806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis } 161906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis 162006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis return false; 162106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis} 162206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis 1623ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlssonbool FunctionDecl::hasTrivialBody() const 1624ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson{ 1625ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson Stmt *S = getBody(); 1626ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson if (!S) { 1627ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson // Since we don't have a body for this function, we don't know if it's 1628ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson // trivial or not. 1629ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson return false; 1630ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson } 1631ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson 1632ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) 1633ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson return true; 1634ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson return false; 1635ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson} 1636ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson 163710620eb5164e31208fcbf0437cd79ae535ed0559Sean Huntbool FunctionDecl::isDefined(const FunctionDecl *&Definition) const { 163810620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 1639cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) { 164010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt Definition = I->IsDeleted ? I->getCanonicalDecl() : *I; 164110620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt return true; 164210620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt } 164310620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt } 164410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt 164510620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt return false; 164610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt} 164710620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt 16486fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { 1649c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { 1650c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis if (I->Body) { 1651c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis Definition = *I; 1652c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis return I->Body.get(getASTContext().getExternalSource()); 16538387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet } else if (I->IsLateTemplateParsed) { 16548387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet Definition = *I; 16558387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet return 0; 1656f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor } 1657f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor } 1658f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor 1659f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor return 0; 16605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 16615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 166255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) { 166355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis Body = B; 1664b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor if (B) 166555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis EndRangeLoc = B->getLocEnd(); 166655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis} 166755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis 16682138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) { 16692138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor IsPure = P; 16702138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor if (P) 16712138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) 16722138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor Parent->markedVirtualFunctionPure(); 16732138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor} 16742138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor 167548a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const { 167623c608d6815f188cb0bd3444c9708383c6461036John McCall const TranslationUnitDecl *tunit = 167723c608d6815f188cb0bd3444c9708383c6461036John McCall dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); 167823c608d6815f188cb0bd3444c9708383c6461036John McCall return tunit && 16794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !tunit->getASTContext().getLangOpts().Freestanding && 168023c608d6815f188cb0bd3444c9708383c6461036John McCall getIdentifier() && 168123c608d6815f188cb0bd3444c9708383c6461036John McCall getIdentifier()->isStr("main"); 168223c608d6815f188cb0bd3444c9708383c6461036John McCall} 168323c608d6815f188cb0bd3444c9708383c6461036John McCall 168423c608d6815f188cb0bd3444c9708383c6461036John McCallbool FunctionDecl::isReservedGlobalPlacementOperator() const { 168523c608d6815f188cb0bd3444c9708383c6461036John McCall assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); 168623c608d6815f188cb0bd3444c9708383c6461036John McCall assert(getDeclName().getCXXOverloadedOperator() == OO_New || 168723c608d6815f188cb0bd3444c9708383c6461036John McCall getDeclName().getCXXOverloadedOperator() == OO_Delete || 168823c608d6815f188cb0bd3444c9708383c6461036John McCall getDeclName().getCXXOverloadedOperator() == OO_Array_New || 168923c608d6815f188cb0bd3444c9708383c6461036John McCall getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); 169023c608d6815f188cb0bd3444c9708383c6461036John McCall 169123c608d6815f188cb0bd3444c9708383c6461036John McCall if (isa<CXXRecordDecl>(getDeclContext())) return false; 169223c608d6815f188cb0bd3444c9708383c6461036John McCall assert(getDeclContext()->getRedeclContext()->isTranslationUnit()); 169323c608d6815f188cb0bd3444c9708383c6461036John McCall 169423c608d6815f188cb0bd3444c9708383c6461036John McCall const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>(); 169523c608d6815f188cb0bd3444c9708383c6461036John McCall if (proto->getNumArgs() != 2 || proto->isVariadic()) return false; 169623c608d6815f188cb0bd3444c9708383c6461036John McCall 169723c608d6815f188cb0bd3444c9708383c6461036John McCall ASTContext &Context = 169823c608d6815f188cb0bd3444c9708383c6461036John McCall cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) 169923c608d6815f188cb0bd3444c9708383c6461036John McCall ->getASTContext(); 170023c608d6815f188cb0bd3444c9708383c6461036John McCall 170123c608d6815f188cb0bd3444c9708383c6461036John McCall // The result type and first argument type are constant across all 170223c608d6815f188cb0bd3444c9708383c6461036John McCall // these operators. The second argument must be exactly void*. 170323c608d6815f188cb0bd3444c9708383c6461036John McCall return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy); 170404495c859f81e440748a9b86baa2913461652bb0Douglas Gregor} 170504495c859f81e440748a9b86baa2913461652bb0Douglas Gregor 170648a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const { 1707750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman if (getLinkage() != ExternalLinkage) 1708750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman return false; 1709750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman 1710750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman if (getAttr<OverloadableAttr>()) 1711750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman return false; 17126393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor 171310aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth const DeclContext *DC = getDeclContext(); 171410aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth if (DC->isRecord()) 171510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth return false; 171610aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth 1717750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman ASTContext &Context = getASTContext(); 17184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Context.getLangOpts().CPlusPlus) 1719750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman return true; 17206393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor 1721750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman return isMain() || DC->isExternCContext(); 17226393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor} 17236393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor 17248499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const { 17258499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) 17268499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor return Method->isStatic(); 17278499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor 1728d931b086984257de68868a64a235c2b4b34003fbJohn McCall if (getStorageClass() == SC_Static) 17298499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor return false; 17308499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor 17311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (const DeclContext *DC = getDeclContext(); 17328499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor DC->isNamespace(); 17338499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor DC = DC->getParent()) { 17348499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { 17358499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (!Namespace->getDeclName()) 17368499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor return false; 17378499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor break; 17388499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor } 17398499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor } 17408499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor 17418499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor return true; 17428499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor} 17438499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor 17447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid 17457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { 17467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl redeclarable_base::setPreviousDeclaration(PrevDecl); 17477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 17487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { 17497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl FunctionTemplateDecl *PrevFunTmpl 17507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; 17517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); 17527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl FunTmpl->setPreviousDeclaration(PrevFunTmpl); 17537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl } 17548f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor 1755d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann if (PrevDecl && PrevDecl->IsInline) 17568f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor IsInline = true; 17577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 17587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 17597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const { 17607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return getFirstDeclaration(); 17617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 17627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 17637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() { 17647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return getFirstDeclaration(); 17657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 17667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 1767381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid FunctionDecl::setStorageClass(StorageClass SC) { 1768381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor assert(isLegalForFunction(SC)); 1769381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor if (getStorageClass() != SC) 1770381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor ClearLinkageCache(); 1771381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor 1772381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor SClass = SC; 1773381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor} 1774381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor 17753e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function 17763e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function. 17773e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// 17783e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is 17793e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and 17803e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value 17813e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a 17821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range 17833e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value. 17847814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const { 178560d302a707fb35b9acf41bf5495296c4af947045Daniel Dunbar if (!getIdentifier()) 17863c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return 0; 17873c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor 17883c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor unsigned BuiltinID = getIdentifier()->getBuiltinID(); 178960d302a707fb35b9acf41bf5495296c4af947045Daniel Dunbar if (!BuiltinID) 179060d302a707fb35b9acf41bf5495296c4af947045Daniel Dunbar return 0; 179160d302a707fb35b9acf41bf5495296c4af947045Daniel Dunbar 179260d302a707fb35b9acf41bf5495296c4af947045Daniel Dunbar ASTContext &Context = getASTContext(); 17933c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 17943c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return BuiltinID; 17953c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor 17963c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor // This function has the name of a known C library 17973c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor // function. Determine whether it actually refers to the C library 17983c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor // function or whether it just has the same name. 17993c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor 18009add31798f621f843233dbff8bba103fca64447bDouglas Gregor // If this is a static function, it's not a builtin. 1801d931b086984257de68868a64a235c2b4b34003fbJohn McCall if (getStorageClass() == SC_Static) 18029add31798f621f843233dbff8bba103fca64447bDouglas Gregor return 0; 18039add31798f621f843233dbff8bba103fca64447bDouglas Gregor 18043c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor // If this function is at translation-unit scope and we're not in 18053c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor // C++, it refers to the C library function. 18064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Context.getLangOpts().CPlusPlus && 18073c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor getDeclContext()->isTranslationUnit()) 18083c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return BuiltinID; 18093c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor 18103c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor // If the function is in an extern "C" linkage specification and is 18113c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor // not marked "overloadable", it's the real function. 18123c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor if (isa<LinkageSpecDecl>(getDeclContext()) && 18131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump cast<LinkageSpecDecl>(getDeclContext())->getLanguage() 18143c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor == LinkageSpecDecl::lang_c && 181540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis !getAttr<OverloadableAttr>()) 18163c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return BuiltinID; 18173c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor 18183c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor // Not a builtin 18193e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor return 0; 18203e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor} 18213e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor 18223e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor 18231ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have 18248dbfbf4c95251c69a455d4d016d6c7890c932007Bob Wilson/// based on its FunctionType. This is the length of the ParamInfo array 18251ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created. 18261ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const { 1827183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall const FunctionType *FT = getType()->getAs<FunctionType>(); 182872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor if (isa<FunctionNoProtoType>(FT)) 1829d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner return 0; 183072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor return cast<FunctionProtoType>(FT)->getNumArgs(); 18311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 18335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18346b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C, 18354278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie llvm::ArrayRef<ParmVarDecl *> NewParamInfo) { 18365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(ParamInfo == 0 && "Already has param info!"); 18374278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); 18381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Zero params -> null pointer. 18404278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie if (!NewParamInfo.empty()) { 18414278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; 18424278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); 18435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 18455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 184616f1f717af196b1448258857b2e6dcfe144b39d0James Molloyvoid FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) { 184716f1f717af196b1448258857b2e6dcfe144b39d0James Molloy assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!"); 184816f1f717af196b1448258857b2e6dcfe144b39d0James Molloy 184916f1f717af196b1448258857b2e6dcfe144b39d0James Molloy if (!NewDecls.empty()) { 185016f1f717af196b1448258857b2e6dcfe144b39d0James Molloy NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()]; 185116f1f717af196b1448258857b2e6dcfe144b39d0James Molloy std::copy(NewDecls.begin(), NewDecls.end(), A); 185216f1f717af196b1448258857b2e6dcfe144b39d0James Molloy DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size()); 185316f1f717af196b1448258857b2e6dcfe144b39d0James Molloy } 185416f1f717af196b1448258857b2e6dcfe144b39d0James Molloy} 185516f1f717af196b1448258857b2e6dcfe144b39d0James Molloy 18568123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments 18578123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of 18588123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default 1859f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// arguments (in C++) or the last parameter is a parameter pack. 18608123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const { 18614e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getASTContext().getLangOpts().CPlusPlus) 18627d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor return getNumParams(); 18637d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor 1864f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor unsigned NumRequiredArgs = getNumParams(); 1865f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 1866f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // If the last parameter is a parameter pack, we don't need an argument for 1867f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // it. 1868f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (NumRequiredArgs > 0 && 1869f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor getParamDecl(NumRequiredArgs - 1)->isParameterPack()) 1870f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor --NumRequiredArgs; 1871f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 1872f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // If this parameter has a default argument, we don't need an argument for 1873f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // it. 1874f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor while (NumRequiredArgs > 0 && 1875f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) 18768123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner --NumRequiredArgs; 18778123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner 18787d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor // We might have parameter packs before the end. These can't be deduced, 18797d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor // but they can still handle multiple arguments. 18807d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor unsigned ArgIdx = NumRequiredArgs; 18817d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor while (ArgIdx > 0) { 18827d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor if (getParamDecl(ArgIdx - 1)->isParameterPack()) 18837d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor NumRequiredArgs = ArgIdx; 18847d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor 18857d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor --ArgIdx; 18867d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor } 18877d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor 18888123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner return NumRequiredArgs; 18898123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner} 18908123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner 18917ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const { 18928f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor if (IsInline) 18937d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor return true; 189448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson 189548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson if (isa<CXXMethodDecl>(this)) { 189648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) 189748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson return true; 189848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson } 18997d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor 19007d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor switch (getTemplateSpecializationKind()) { 19017d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor case TSK_Undeclared: 19027d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor case TSK_ExplicitSpecialization: 19037d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor return false; 19047d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor 19057d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor case TSK_ImplicitInstantiation: 19067d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor case TSK_ExplicitInstantiationDeclaration: 19077d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor case TSK_ExplicitInstantiationDefinition: 19087d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // Handle below. 19097d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor break; 19107d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor } 19117d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor 19127d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 191306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis bool HasPattern = false; 19147d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor if (PatternDecl) 191506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis HasPattern = PatternDecl->hasBody(PatternDecl); 19167d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor 191706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis if (HasPattern && PatternDecl) 19187d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor return PatternDecl->isInlined(); 19197d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor 19207d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor return false; 19217ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor} 19227ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor 1923a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedmanstatic bool RedeclForcesDefC99(const FunctionDecl *Redecl) { 1924a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // Only consider file-scope declarations in this test. 1925a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) 1926a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return false; 1927a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman 1928a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // Only consider explicit declarations; the presence of a builtin for a 1929a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // libcall shouldn't affect whether a definition is externally visible. 1930a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman if (Redecl->isImplicit()) 1931a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return false; 1932a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman 1933a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) 1934a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return true; // Not an inline definition 1935a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman 1936a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return false; 1937a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman} 1938a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman 1939dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// \brief For a function declaration in C or C++, determine whether this 1940dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// declaration causes the definition to be externally visible. 1941dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// 1942a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman/// Specifically, this determines if adding the current declaration to the set 1943a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman/// of redeclarations of the given functions causes 1944a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman/// isInlineDefinitionExternallyVisible to change from false to true. 1945dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewyckybool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { 1946dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky assert(!doesThisDeclarationHaveABody() && 1947dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky "Must have a declaration without a body."); 1948dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky 1949dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky ASTContext &Context = getASTContext(); 1950dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky 19514e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { 1952a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // With GNU inlining, a declaration with 'inline' but not 'extern', forces 1953a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // an externally visible definition. 1954a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // 1955a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // FIXME: What happens if gnu_inline gets added on after the first 1956a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // declaration? 1957a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern) 1958a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return false; 1959a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman 1960a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman const FunctionDecl *Prev = this; 1961a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman bool FoundBody = false; 1962a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman while ((Prev = Prev->getPreviousDecl())) { 1963a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman FoundBody |= Prev->Body; 1964a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman 1965a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman if (Prev->Body) { 1966a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // If it's not the case that both 'inline' and 'extern' are 1967a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // specified on the definition, then it is always externally visible. 1968a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman if (!Prev->isInlineSpecified() || 1969a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman Prev->getStorageClassAsWritten() != SC_Extern) 1970a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return false; 1971a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman } else if (Prev->isInlineSpecified() && 1972a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman Prev->getStorageClassAsWritten() != SC_Extern) { 1973a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return false; 1974a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman } 1975a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman } 1976a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return FoundBody; 1977a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman } 1978a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman 19794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Context.getLangOpts().CPlusPlus) 1980dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky return false; 1981a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman 1982a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // C99 6.7.4p6: 1983a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // [...] If all of the file scope declarations for a function in a 1984a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // translation unit include the inline function specifier without extern, 1985a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // then the definition in that translation unit is an inline definition. 1986a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman if (isInlineSpecified() && getStorageClass() != SC_Extern) 1987dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky return false; 1988a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman const FunctionDecl *Prev = this; 1989a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman bool FoundBody = false; 1990a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman while ((Prev = Prev->getPreviousDecl())) { 1991a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman FoundBody |= Prev->Body; 1992a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman if (RedeclForcesDefC99(Prev)) 1993a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return false; 1994a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman } 1995a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return FoundBody; 1996dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky} 1997dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky 19987d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the 19991fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible. 20001fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// 20011fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations. 20021fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and 20031fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be 20041fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program. 20051fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// 20061fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However, 20071e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the 20081fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6). 20091fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// 20101fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function 20111fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the 20121fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create 20131fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an 20141fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol. 20151fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const { 201610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt assert(doesThisDeclarationHaveABody() && "Must have the function definition"); 20177ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor assert(isInlined() && "Function must be inline"); 20187d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor ASTContext &Context = getASTContext(); 20191fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor 20204e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { 2021a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // Note: If you change the logic here, please change 2022a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // doesDeclarationForceExternallyVisibleDefinition as well. 2023a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman // 20248f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor // If it's not the case that both 'inline' and 'extern' are 20258f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor // specified on the definition, then this inline definition is 20268f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor // externally visible. 20278f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern)) 20288f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor return true; 20298f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor 20308f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor // If any declaration is 'inline' but not 'extern', then this definition 20318f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor // is externally visible. 20321fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); 20331fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor Redecl != RedeclEnd; 20341fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor ++Redecl) { 20358f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor if (Redecl->isInlineSpecified() && 20368f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor Redecl->getStorageClassAsWritten() != SC_Extern) 20371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor return true; 20388f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor } 20391fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor 20409f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor return false; 20411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor } 2042a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman 20431fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // C99 6.7.4p6: 20441fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // [...] If all of the file scope declarations for a function in a 20451fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // translation unit include the inline function specifier without extern, 20461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // then the definition in that translation unit is an inline definition. 20471fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); 20481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor Redecl != RedeclEnd; 20491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor ++Redecl) { 2050a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman if (RedeclForcesDefC99(*Redecl)) 2051a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman return true; 20521fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor } 20531fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor 20541fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // C99 6.7.4p6: 20551fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // An inline definition does not provide an external definition for the 20561fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // function, and does not forbid an external definition in another 20571fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // translation unit. 20589f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor return false; 20599f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor} 20609f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor 20611cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this 20621cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any. 20631cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { 2064e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) 2065e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor return getDeclName().getCXXOverloadedOperator(); 20661cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor else 20671cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor return OO_None; 20681cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor} 20691cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor 2070a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function 2071a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any. 2072a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { 2073a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) 2074a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt return getDeclName().getCXXLiteralIdentifier(); 2075a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt else 2076a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt return 0; 2077a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt} 2078a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt 2079d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { 2080d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis if (TemplateOrSpecialization.isNull()) 2081d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis return TK_NonTemplate; 2082d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) 2083d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis return TK_FunctionTemplate; 2084d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) 2085d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis return TK_MemberSpecialization; 2086d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) 2087d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis return TK_FunctionTemplateSpecialization; 2088d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis if (TemplateOrSpecialization.is 2089d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis <DependentFunctionTemplateSpecializationInfo*>()) 2090d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis return TK_DependentFunctionTemplateSpecialization; 2091d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis 2092b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); 2093d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis} 2094d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis 20952db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { 2096b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) 20972db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor return cast<FunctionDecl>(Info->getInstantiatedFrom()); 20982db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor 20992db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor return 0; 21002db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor} 21012db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor 2102b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { 2103b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 2104b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor} 2105b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor 21062db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid 21076b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, 21086b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis FunctionDecl *FD, 21092db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor TemplateSpecializationKind TSK) { 21102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor assert(TemplateOrSpecialization.isNull() && 21112db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor "Member function is already a specialization"); 21122db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor MemberSpecializationInfo *Info 21136b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis = new (C) MemberSpecializationInfo(FD, TSK); 21142db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor TemplateOrSpecialization = Info; 21152db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor} 21162db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor 21173b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const { 21186cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor // If the function is invalid, it can't be implicitly instantiated. 21196cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor if (isInvalidDecl()) 21203b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor return false; 21213b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 21223b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor switch (getTemplateSpecializationKind()) { 21233b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor case TSK_Undeclared: 21243b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor case TSK_ExplicitInstantiationDefinition: 21253b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor return false; 21263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 21273b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor case TSK_ImplicitInstantiation: 21283b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor return true; 21293b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 2130af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet // It is possible to instantiate TSK_ExplicitSpecialization kind 2131af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet // if the FunctionDecl has a class scope specialization pattern. 2132af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet case TSK_ExplicitSpecialization: 2133af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet return getClassScopeSpecializationPattern() != 0; 2134af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet 21353b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor case TSK_ExplicitInstantiationDeclaration: 21363b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor // Handled below. 21373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor break; 21383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor } 21393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 21403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor // Find the actual template from which we will instantiate. 21413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 214206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis bool HasPattern = false; 21433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor if (PatternDecl) 214406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis HasPattern = PatternDecl->hasBody(PatternDecl); 21453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 21463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor // C++0x [temp.explicit]p9: 21473b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor // Except for inline functions, other explicit instantiation declarations 21483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor // have the effect of suppressing the implicit instantiation of the entity 21493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor // to which they refer. 215006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis if (!HasPattern || !PatternDecl) 21513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor return true; 21523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 21537ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor return PatternDecl->isInlined(); 215475df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek} 215575df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek 215675df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenekbool FunctionDecl::isTemplateInstantiation() const { 215775df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek switch (getTemplateSpecializationKind()) { 215875df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek case TSK_Undeclared: 215975df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek case TSK_ExplicitSpecialization: 216075df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek return false; 216175df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek case TSK_ImplicitInstantiation: 216275df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek case TSK_ExplicitInstantiationDeclaration: 216375df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek case TSK_ExplicitInstantiationDefinition: 216475df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek return true; 216575df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek } 216675df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek llvm_unreachable("All TSK values handled."); 216775df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek} 21683b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 21693b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { 2170af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet // Handle class scope explicit specialization special case. 2171af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) 2172af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet return getClassScopeSpecializationPattern(); 2173af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet 21743b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { 21753b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor while (Primary->getInstantiatedFromMemberTemplate()) { 21763b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor // If we have hit a point where the user provided a specialization of 21773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor // this template, we're done looking. 21783b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor if (Primary->isMemberSpecialization()) 21793b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor break; 21803b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 21813b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor Primary = Primary->getInstantiatedFromMemberTemplate(); 21823b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor } 21833b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 21843b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor return Primary->getTemplatedDecl(); 21853b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor } 21863b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 21873b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor return getInstantiatedFromMemberFunction(); 21883b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor} 21893b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor 219016e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { 21911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (FunctionTemplateSpecializationInfo *Info 219216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor = TemplateOrSpecialization 219316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 21941fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return Info->Template.getPointer(); 219516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor } 219616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return 0; 219716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor} 219816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor 2199af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const { 2200af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet return getASTContext().getClassScopeSpecializationPattern(this); 2201af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet} 2202af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet 220316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList * 220416e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const { 22051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (FunctionTemplateSpecializationInfo *Info 2206fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor = TemplateOrSpecialization 2207fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 220816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return Info->TemplateArguments; 220916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor } 221016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return 0; 221116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor} 221216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor 221371a7605977113c795edd44fcbd2302ad49506653Argyrios Kyrtzidisconst ASTTemplateArgumentListInfo * 2214e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const { 2215e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara if (FunctionTemplateSpecializationInfo *Info 2216e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara = TemplateOrSpecialization 2217e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 2218e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara return Info->TemplateArgumentsAsWritten; 2219e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara } 2220e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara return 0; 2221e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara} 2222e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara 22231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid 22246b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, 22256b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis FunctionTemplateDecl *Template, 2226127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor const TemplateArgumentList *TemplateArgs, 2227b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor void *InsertPos, 2228e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara TemplateSpecializationKind TSK, 22297b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis const TemplateArgumentListInfo *TemplateArgsAsWritten, 22307b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis SourceLocation PointOfInstantiation) { 2231b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor assert(TSK != TSK_Undeclared && 2232b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor "Must specify the type of function template specialization"); 22331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FunctionTemplateSpecializationInfo *Info 223416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 22351637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor if (!Info) 2236a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, 2237a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis TemplateArgs, 2238a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis TemplateArgsAsWritten, 2239a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis PointOfInstantiation); 22401637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor TemplateOrSpecialization = Info; 22411e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor Template->addSpecialization(Info, InsertPos); 22421637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor} 22431637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor 2244af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid 2245af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, 2246af2094e7cecadf36667deb61a83587ffdd979bd3John McCall const UnresolvedSetImpl &Templates, 2247af2094e7cecadf36667deb61a83587ffdd979bd3John McCall const TemplateArgumentListInfo &TemplateArgs) { 2248af2094e7cecadf36667deb61a83587ffdd979bd3John McCall assert(TemplateOrSpecialization.isNull()); 2249af2094e7cecadf36667deb61a83587ffdd979bd3John McCall size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); 2250af2094e7cecadf36667deb61a83587ffdd979bd3John McCall Size += Templates.size() * sizeof(FunctionTemplateDecl*); 225121c0160959961b3a6ab3308608ee3fde182ecb49John McCall Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); 2252af2094e7cecadf36667deb61a83587ffdd979bd3John McCall void *Buffer = Context.Allocate(Size); 2253af2094e7cecadf36667deb61a83587ffdd979bd3John McCall DependentFunctionTemplateSpecializationInfo *Info = 2254af2094e7cecadf36667deb61a83587ffdd979bd3John McCall new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, 2255af2094e7cecadf36667deb61a83587ffdd979bd3John McCall TemplateArgs); 2256af2094e7cecadf36667deb61a83587ffdd979bd3John McCall TemplateOrSpecialization = Info; 2257af2094e7cecadf36667deb61a83587ffdd979bd3John McCall} 2258af2094e7cecadf36667deb61a83587ffdd979bd3John McCall 2259af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo:: 2260af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, 2261af2094e7cecadf36667deb61a83587ffdd979bd3John McCall const TemplateArgumentListInfo &TArgs) 2262af2094e7cecadf36667deb61a83587ffdd979bd3John McCall : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { 2263af2094e7cecadf36667deb61a83587ffdd979bd3John McCall 2264af2094e7cecadf36667deb61a83587ffdd979bd3John McCall d.NumTemplates = Ts.size(); 2265af2094e7cecadf36667deb61a83587ffdd979bd3John McCall d.NumArgs = TArgs.size(); 2266af2094e7cecadf36667deb61a83587ffdd979bd3John McCall 2267af2094e7cecadf36667deb61a83587ffdd979bd3John McCall FunctionTemplateDecl **TsArray = 2268af2094e7cecadf36667deb61a83587ffdd979bd3John McCall const_cast<FunctionTemplateDecl**>(getTemplates()); 2269af2094e7cecadf36667deb61a83587ffdd979bd3John McCall for (unsigned I = 0, E = Ts.size(); I != E; ++I) 2270af2094e7cecadf36667deb61a83587ffdd979bd3John McCall TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); 2271af2094e7cecadf36667deb61a83587ffdd979bd3John McCall 2272af2094e7cecadf36667deb61a83587ffdd979bd3John McCall TemplateArgumentLoc *ArgsArray = 2273af2094e7cecadf36667deb61a83587ffdd979bd3John McCall const_cast<TemplateArgumentLoc*>(getTemplateArgs()); 2274af2094e7cecadf36667deb61a83587ffdd979bd3John McCall for (unsigned I = 0, E = TArgs.size(); I != E; ++I) 2275af2094e7cecadf36667deb61a83587ffdd979bd3John McCall new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); 2276af2094e7cecadf36667deb61a83587ffdd979bd3John McCall} 2277af2094e7cecadf36667deb61a83587ffdd979bd3John McCall 2278d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { 22791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // For a function template specialization, query the specialization 2280d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor // information object. 22812db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor FunctionTemplateSpecializationInfo *FTSInfo 22821fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); 22832db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor if (FTSInfo) 22842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor return FTSInfo->getTemplateSpecializationKind(); 2285d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor 22862db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor MemberSpecializationInfo *MSInfo 22872db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); 22882db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor if (MSInfo) 22892db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor return MSInfo->getTemplateSpecializationKind(); 22902db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor 22912db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor return TSK_Undeclared; 22921fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor} 22931fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor 22941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid 22950a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 22960a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor SourceLocation PointOfInstantiation) { 22972db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor if (FunctionTemplateSpecializationInfo *FTSInfo 22982db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor = TemplateOrSpecialization.dyn_cast< 22990a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor FunctionTemplateSpecializationInfo*>()) { 23002db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor FTSInfo->setTemplateSpecializationKind(TSK); 23010a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor if (TSK != TSK_ExplicitSpecialization && 23020a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor PointOfInstantiation.isValid() && 23030a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor FTSInfo->getPointOfInstantiation().isInvalid()) 23040a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor FTSInfo->setPointOfInstantiation(PointOfInstantiation); 23050a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor } else if (MemberSpecializationInfo *MSInfo 23060a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { 23072db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor MSInfo->setTemplateSpecializationKind(TSK); 23080a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor if (TSK != TSK_ExplicitSpecialization && 23090a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor PointOfInstantiation.isValid() && 23100a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor MSInfo->getPointOfInstantiation().isInvalid()) 23110a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor MSInfo->setPointOfInstantiation(PointOfInstantiation); 23120a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor } else 2313b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Function cannot have a template specialization kind"); 23141fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor} 23151fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor 23160a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const { 23170a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor if (FunctionTemplateSpecializationInfo *FTSInfo 23180a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor = TemplateOrSpecialization.dyn_cast< 23190a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor FunctionTemplateSpecializationInfo*>()) 23200a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor return FTSInfo->getPointOfInstantiation(); 23210a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor else if (MemberSpecializationInfo *MSInfo 23220a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) 23230a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor return MSInfo->getPointOfInstantiation(); 23240a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor 23250a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor return SourceLocation(); 23260a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor} 23270a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor 23289f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const { 2329da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor if (Decl::isOutOfLine()) 23309f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor return true; 23319f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor 23329f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor // If this function was instantiated from a member function of a 23339f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor // class template, check whether that member function was defined out-of-line. 23349f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { 23359f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor const FunctionDecl *Definition; 233606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis if (FD->hasBody(Definition)) 23379f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor return Definition->isOutOfLine(); 23389f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor } 23399f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor 23409f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor // If this function was instantiated from a function template, 23419f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor // check whether that function template was defined out-of-line. 23429f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { 23439f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor const FunctionDecl *Definition; 234406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) 23459f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor return Definition->isOutOfLine(); 23469f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor } 23479f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor 23489f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor return false; 23499f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor} 23509f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor 2351a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange FunctionDecl::getSourceRange() const { 2352a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara return SourceRange(getOuterLocStart(), EndRangeLoc); 2353a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara} 2354a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara 23559392d4e4da695e2e1a5befbb3a074793a7265471Anna Zaksunsigned FunctionDecl::getMemoryFunctionKind() const { 2356d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks IdentifierInfo *FnInfo = getIdentifier(); 2357d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2358d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks if (!FnInfo) 23590a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return 0; 2360d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2361d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks // Builtin handling. 2362d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks switch (getBuiltinID()) { 2363d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin_memset: 2364d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin___memset_chk: 2365d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BImemset: 23660a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BImemset; 2367d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2368d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin_memcpy: 2369d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin___memcpy_chk: 2370d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BImemcpy: 23710a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BImemcpy; 2372d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2373d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin_memmove: 2374d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin___memmove_chk: 2375d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BImemmove: 23760a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BImemmove; 2377d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2378d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BIstrlcpy: 23790a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrlcpy; 2380d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BIstrlcat: 23810a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrlcat; 2382d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2383d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin_memcmp: 23840a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks case Builtin::BImemcmp: 23850a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BImemcmp; 2386d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2387d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin_strncpy: 2388d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin___strncpy_chk: 2389d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BIstrncpy: 23900a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrncpy; 2391d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2392d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin_strncmp: 23930a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks case Builtin::BIstrncmp: 23940a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrncmp; 2395d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2396d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin_strncasecmp: 23970a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks case Builtin::BIstrncasecmp: 23980a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrncasecmp; 2399d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2400d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin_strncat: 2401c36bedc90c687caa71748480c60707ea4608b092Anna Zaks case Builtin::BI__builtin___strncat_chk: 2402d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BIstrncat: 24030a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrncat; 2404d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2405d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BI__builtin_strndup: 2406d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks case Builtin::BIstrndup: 24070a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrndup; 2408d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 2409c36bedc90c687caa71748480c60707ea4608b092Anna Zaks case Builtin::BI__builtin_strlen: 2410c36bedc90c687caa71748480c60707ea4608b092Anna Zaks case Builtin::BIstrlen: 2411c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return Builtin::BIstrlen; 2412c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 2413d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks default: 2414750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman if (isExternC()) { 2415d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks if (FnInfo->isStr("memset")) 24160a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BImemset; 2417d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks else if (FnInfo->isStr("memcpy")) 24180a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BImemcpy; 2419d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks else if (FnInfo->isStr("memmove")) 24200a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BImemmove; 2421d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks else if (FnInfo->isStr("memcmp")) 24220a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BImemcmp; 2423d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks else if (FnInfo->isStr("strncpy")) 24240a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrncpy; 2425d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks else if (FnInfo->isStr("strncmp")) 24260a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrncmp; 2427d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks else if (FnInfo->isStr("strncasecmp")) 24280a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrncasecmp; 2429d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks else if (FnInfo->isStr("strncat")) 24300a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrncat; 2431d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks else if (FnInfo->isStr("strndup")) 24320a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return Builtin::BIstrndup; 2433c36bedc90c687caa71748480c60707ea4608b092Anna Zaks else if (FnInfo->isStr("strlen")) 2434c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return Builtin::BIstrlen; 2435d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks } 2436d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks break; 2437d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks } 24380a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks return 0; 2439d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks} 2440d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks 24418a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===// 24427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation 24437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===// 24447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 24454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadFieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, 2446ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara SourceLocation StartLoc, SourceLocation IdLoc, 2447ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara IdentifierInfo *Id, QualType T, 24487a614d8380297fcd2bc23986241905d97222948cRichard Smith TypeSourceInfo *TInfo, Expr *BW, bool Mutable, 24497a614d8380297fcd2bc23986241905d97222948cRichard Smith bool HasInit) { 2450ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, 24517a614d8380297fcd2bc23986241905d97222948cRichard Smith BW, Mutable, HasInit); 24527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 24537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 24541e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorFieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 24551e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl)); 24561e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(), 24571e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 0, QualType(), 0, 0, false, false); 24581e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 24591e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 24607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const { 24617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl if (!isImplicit() || getDeclName()) 24627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return false; 24637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 24647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl if (const RecordType *Record = getType()->getAs<RecordType>()) 24657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return Record->getDecl()->isAnonymousStructOrUnion(); 24667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 24677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return false; 24687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 24697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 2470a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smithunsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { 2471a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith assert(isBitField() && "not a bitfield"); 2472a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith Expr *BitWidth = InitializerOrBitWidth.getPointer(); 2473a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue(); 2474a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith} 2475a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith 2476ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCallunsigned FieldDecl::getFieldIndex() const { 2477ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall if (CachedFieldIndex) return CachedFieldIndex - 1; 2478ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall 2479180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned Index = 0; 248007a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian const RecordDecl *RD = getParent(); 248107a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian const FieldDecl *LastFD = 0; 248207a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian bool IsMsStruct = RD->hasAttr<MsStructAttr>(); 2483180f47959a066795cc0f409433023af448bb0328Richard Smith 2484180f47959a066795cc0f409433023af448bb0328Richard Smith for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); 2485180f47959a066795cc0f409433023af448bb0328Richard Smith I != E; ++I, ++Index) { 2486180f47959a066795cc0f409433023af448bb0328Richard Smith (*I)->CachedFieldIndex = Index + 1; 2487ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall 248807a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian if (IsMsStruct) { 248907a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian // Zero-length bitfields following non-bitfield members are ignored. 2490180f47959a066795cc0f409433023af448bb0328Richard Smith if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) { 2491180f47959a066795cc0f409433023af448bb0328Richard Smith --Index; 249207a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian continue; 249307a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian } 2494180f47959a066795cc0f409433023af448bb0328Richard Smith LastFD = (*I); 249507a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian } 2496ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall } 2497ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall 2498180f47959a066795cc0f409433023af448bb0328Richard Smith assert(CachedFieldIndex && "failed to find field in parent"); 2499180f47959a066795cc0f409433023af448bb0328Richard Smith return CachedFieldIndex - 1; 2500ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall} 2501ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall 2502f2cf562cec11dec926c0a29a71769a27fed02962Abramo BagnaraSourceRange FieldDecl::getSourceRange() const { 2503d330e23f183cedb9e6c1cbb809407576f7bbab71Abramo Bagnara if (const Expr *E = InitializerOrBitWidth.getPointer()) 2504d330e23f183cedb9e6c1cbb809407576f7bbab71Abramo Bagnara return SourceRange(getInnerLocStart(), E->getLocEnd()); 2505a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara return DeclaratorDecl::getSourceRange(); 2506f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara} 2507f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara 25087a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid FieldDecl::setInClassInitializer(Expr *Init) { 25097a614d8380297fcd2bc23986241905d97222948cRichard Smith assert(!InitializerOrBitWidth.getPointer() && 25107a614d8380297fcd2bc23986241905d97222948cRichard Smith "bit width or initializer already set"); 25117a614d8380297fcd2bc23986241905d97222948cRichard Smith InitializerOrBitWidth.setPointer(Init); 25127a614d8380297fcd2bc23986241905d97222948cRichard Smith InitializerOrBitWidth.setInt(0); 25137a614d8380297fcd2bc23986241905d97222948cRichard Smith} 25147a614d8380297fcd2bc23986241905d97222948cRichard Smith 25157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===// 2516bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation 25174b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===// 25184b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek 25191693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const { 25201693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor return getTemplateOrInnerLocStart(this); 25211693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor} 25221693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor 2523f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const { 2524f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); 25251693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor return SourceRange(getOuterLocStart(), E); 2526f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis} 2527f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis 2528b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() { 25298e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return getFirstDeclaration(); 2530b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis} 2531b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis 2532162e1c1b487352434552147967c3dd296ebee2f7Richard Smithvoid TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { 2533162e1c1b487352434552147967c3dd296ebee2f7Richard Smith TypedefNameDeclOrQualifier = TDD; 253460e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor if (TypeForDecl) 2535f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall const_cast<Type*>(TypeForDecl)->ClearLinkageCache(); 2536381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor ClearLinkageCache(); 253760e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor} 253860e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor 25390b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() { 2540ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl IsBeingDefined = true; 254186ff308724171494395a840fd2efbe25e62f352eJohn McCall 254286ff308724171494395a840fd2efbe25e62f352eJohn McCall if (isa<CXXRecordDecl>(this)) { 254386ff308724171494395a840fd2efbe25e62f352eJohn McCall CXXRecordDecl *D = cast<CXXRecordDecl>(this); 254486ff308724171494395a840fd2efbe25e62f352eJohn McCall struct CXXRecordDecl::DefinitionData *Data = 254586ff308724171494395a840fd2efbe25e62f352eJohn McCall new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); 25462243288c4826905b5a0837f6f21d9d821688652eJohn McCall for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) 25472243288c4826905b5a0837f6f21d9d821688652eJohn McCall cast<CXXRecordDecl>(*I)->DefinitionData = Data; 254886ff308724171494395a840fd2efbe25e62f352eJohn McCall } 25490b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor} 25500b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor 25510b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() { 25525cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall assert((!isa<CXXRecordDecl>(this) || 25535cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall cast<CXXRecordDecl>(this)->hasDefinition()) && 25545cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall "definition completed but not started"); 25555cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall 25565e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall IsCompleteDefinition = true; 2557ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl IsBeingDefined = false; 2558565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis 2559565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis if (ASTMutationListener *L = getASTMutationListener()) 2560565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis L->CompletedTagDefinition(this); 25610b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor} 25620b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor 25635e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCallTagDecl *TagDecl::getDefinition() const { 25645e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall if (isCompleteDefinition()) 25658e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return const_cast<TagDecl *>(this); 2566220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this)) 2567220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick return CXXRD->getDefinition(); 25681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 25691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); 25708e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor R != REnd; ++R) 25715e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall if (R->isCompleteDefinition()) 25728e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return *R; 25731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 25748e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return 0; 25754b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek} 25764b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek 2577c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { 2578c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor if (QualifierLoc) { 2579b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall // Make sure the extended qualifier info is allocated. 2580b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall if (!hasExtInfo()) 2581162e1c1b487352434552147967c3dd296ebee2f7Richard Smith TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; 2582b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall // Set qualifier info. 2583c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor getExtInfo()->QualifierLoc = QualifierLoc; 25843060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier } else { 2585b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall // Here Qualifier == 0, i.e., we are removing the qualifier (if any). 2586b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall if (hasExtInfo()) { 25877f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara if (getExtInfo()->NumTemplParamLists == 0) { 25887f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara getASTContext().Deallocate(getExtInfo()); 2589162e1c1b487352434552147967c3dd296ebee2f7Richard Smith TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0; 25907f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara } 25917f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara else 25927f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara getExtInfo()->QualifierLoc = QualifierLoc; 2593b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall } 2594b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall } 2595b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall} 2596b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall 25977f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnaravoid TagDecl::setTemplateParameterListsInfo(ASTContext &Context, 25987f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara unsigned NumTPLists, 25997f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara TemplateParameterList **TPLists) { 26007f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara assert(NumTPLists > 0); 26017f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Make sure the extended decl info is allocated. 26027f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara if (!hasExtInfo()) 26037f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Allocate external info struct. 2604162e1c1b487352434552147967c3dd296ebee2f7Richard Smith TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; 26057f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara // Set the template parameter lists info. 26067f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); 26077f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara} 26087f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara 26094b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===// 26107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation 26117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===// 26127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 261399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid EnumDecl::anchor() { } 261499ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 2615ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, 2616ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara SourceLocation StartLoc, SourceLocation IdLoc, 2617ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara IdentifierInfo *Id, 2618a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara EnumDecl *PrevDecl, bool IsScoped, 2619a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara bool IsScopedUsingClassTag, bool IsFixed) { 2620ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl, 2621a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara IsScoped, IsScopedUsingClassTag, IsFixed); 26227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl C.getTypeDeclType(Enum, PrevDecl); 26237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return Enum; 26247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 26257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 26261e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorEnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 26271e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl)); 26281e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0, 26291e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor false, false, false); 2630b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis} 2631b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis 2632838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType, 26331b5a618c59025898806160ed5e7f0ff5bb79e482John McCall QualType NewPromotionType, 26341b5a618c59025898806160ed5e7f0ff5bb79e482John McCall unsigned NumPositiveBits, 26351b5a618c59025898806160ed5e7f0ff5bb79e482John McCall unsigned NumNegativeBits) { 26365e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall assert(!isCompleteDefinition() && "Cannot redefine enums!"); 26371274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor if (!IntegerType) 26381274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor IntegerType = NewType.getTypePtr(); 26397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl PromotionType = NewPromotionType; 26401b5a618c59025898806160ed5e7f0ff5bb79e482John McCall setNumPositiveBits(NumPositiveBits); 26411b5a618c59025898806160ed5e7f0ff5bb79e482John McCall setNumNegativeBits(NumNegativeBits); 26427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl TagDecl::completeDefinition(); 26437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 26447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 26451af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard SmithTemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { 26461af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 26471af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith return MSI->getTemplateSpecializationKind(); 26481af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith 26491af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith return TSK_Undeclared; 26501af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith} 26511af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith 26521af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smithvoid EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 26531af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith SourceLocation PointOfInstantiation) { 26541af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); 26551af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith assert(MSI && "Not an instantiated member enumeration?"); 26561af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith MSI->setTemplateSpecializationKind(TSK); 26571af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith if (TSK != TSK_ExplicitSpecialization && 26581af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith PointOfInstantiation.isValid() && 26591af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith MSI->getPointOfInstantiation().isInvalid()) 26601af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith MSI->setPointOfInstantiation(PointOfInstantiation); 26611af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith} 26621af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith 2663f1c66b40213784a1c4612f04c14cafa2b0e89988Richard SmithEnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { 2664f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith if (SpecializationInfo) 2665f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); 2666f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith 2667f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith return 0; 2668f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith} 2669f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith 2670f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smithvoid EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, 2671f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith TemplateSpecializationKind TSK) { 2672f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith assert(!SpecializationInfo && "Member enum is already a specialization"); 2673f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); 2674f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith} 2675f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith 26767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===// 26778a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation 26788a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===// 26795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2680ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, 2681ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara SourceLocation StartLoc, SourceLocation IdLoc, 2682ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara IdentifierInfo *Id, RecordDecl *PrevDecl) 2683ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) { 26846359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek HasFlexibleArrayMember = false; 2685bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor AnonymousStructOrUnion = false; 2686082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian HasObjectMember = false; 2687eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis LoadedFieldsFromExternalStorage = false; 26886359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); 26896359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek} 26906359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek 26914ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, 2692ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara SourceLocation StartLoc, SourceLocation IdLoc, 2693ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara IdentifierInfo *Id, RecordDecl* PrevDecl) { 2694ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id, 2695ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara PrevDecl); 26964b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek C.getTypeDeclType(R, PrevDecl); 26974b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek return R; 26986359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek} 26996359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek 27001e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorRecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { 27011e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl)); 27021e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 27031e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor SourceLocation(), 0, 0); 2704b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis} 2705b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis 2706c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const { 27071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return isImplicit() && getDeclName() && getDeclContext()->isRecord() && 2708c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); 2709c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor} 2710c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor 2711eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const { 2712eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) 2713eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis LoadFieldsFromExternalStorage(); 2714eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis 2715eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis return field_iterator(decl_iterator(FirstDecl)); 2716eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis} 2717eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis 2718da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// completeDefinition - Notes that the definition of this type is now 2719da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// complete. 2720da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid RecordDecl::completeDefinition() { 27215e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall assert(!isCompleteDefinition() && "Cannot redefine record!"); 2722da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor TagDecl::completeDefinition(); 2723da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor} 2724da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor 2725eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const { 2726eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis ExternalASTSource *Source = getASTContext().getExternalSource(); 2727eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis assert(hasExternalLexicalStorage() && Source && "No external storage?"); 2728eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis 2729eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis // Notify that we have a RecordDecl doing some initialization. 2730eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis ExternalASTSource::Deserializing TheFields(Source); 2731eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis 27325f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<Decl*, 64> Decls; 2733ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor LoadedFieldsFromExternalStorage = true; 2734ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) { 2735ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor case ELR_Success: 2736ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor break; 2737ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor 2738ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor case ELR_AlreadyLoaded: 2739ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor case ELR_Failure: 2740eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis return; 2741ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor } 2742eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis 2743eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG 2744eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis // Check that all decls we got were FieldDecls. 2745eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis for (unsigned i=0, e=Decls.size(); i != e; ++i) 2746eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis assert(isa<FieldDecl>(Decls[i])); 2747eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif 2748eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis 2749eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis if (Decls.empty()) 2750eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis return; 2751eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis 2752ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, 2753ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis /*FieldsAlreadyLoaded=*/false); 2754eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis} 2755eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis 275656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===// 275756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation 275856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===// 275956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff 27604278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikievoid BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) { 2761e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff assert(ParamInfo == 0 && "Already has param info!"); 27621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2763e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff // Zero params -> null pointer. 27644278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie if (!NewParamInfo.empty()) { 27654278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie NumParams = NewParamInfo.size(); 27664278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; 27674278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); 2768e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff } 2769e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff} 2770e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff 27716b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallvoid BlockDecl::setCaptures(ASTContext &Context, 27726b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall const Capture *begin, 27736b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall const Capture *end, 27746b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall bool capturesCXXThis) { 2775469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall CapturesCXXThis = capturesCXXThis; 2776469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall 2777469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall if (begin == end) { 27786b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall NumCaptures = 0; 27796b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall Captures = 0; 2780469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall return; 2781469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall } 2782469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall 27836b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall NumCaptures = end - begin; 27846b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall 27856b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall // Avoid new Capture[] because we don't want to provide a default 27866b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall // constructor. 27876b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall size_t allocationSize = NumCaptures * sizeof(Capture); 27886b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*)); 27896b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall memcpy(buffer, begin, allocationSize); 27906b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall Captures = static_cast<Capture*>(buffer); 2791e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff} 27927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 2793204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCallbool BlockDecl::capturesVariable(const VarDecl *variable) const { 2794204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall for (capture_const_iterator 2795204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall i = capture_begin(), e = capture_end(); i != e; ++i) 2796204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall // Only auto vars can be captured, so no redeclaration worries. 2797204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall if (i->getVariable() == variable) 2798204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall return true; 2799204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall 2800204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall return false; 2801204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall} 2802204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall 28032fcbceff97e065cff499e6cc563ca25c762bf547Douglas GregorSourceRange BlockDecl::getSourceRange() const { 28042fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation()); 28052fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor} 28067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 28077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===// 28087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations 28097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===// 28107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 281199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid TranslationUnitDecl::anchor() { } 281299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 28137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { 28147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return new (C) TranslationUnitDecl(C); 28157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 28167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 281799ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid LabelDecl::anchor() { } 281899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 2819ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, 28206784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara SourceLocation IdentL, IdentifierInfo *II) { 28216784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara return new (C) LabelDecl(DC, IdentL, II, 0, IdentL); 28226784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara} 28236784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara 28246784304db526cde59046d613c4175ce2caf93e44Abramo BagnaraLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, 28256784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara SourceLocation IdentL, IdentifierInfo *II, 28266784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara SourceLocation GnuLabelL) { 28276784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); 28286784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL); 2829ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner} 2830ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner 28311e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorLabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 28321e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl)); 28331e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation()); 283406c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor} 283506c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor 283699ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid ValueDecl::anchor() { } 283799ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 283899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid ImplicitParamDecl::anchor() { } 283999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 28407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, 2841ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara SourceLocation IdLoc, 2842ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara IdentifierInfo *Id, 2843ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara QualType Type) { 2844ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type); 28457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 28467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 28471e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, 28481e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor unsigned ID) { 28491e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl)); 28501e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType()); 28511e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 28521e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 28537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, 2854ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara SourceLocation StartLoc, 28552577743c5650c646fb705df01403707e94f2df04Abramo Bagnara const DeclarationNameInfo &NameInfo, 28562577743c5650c646fb705df01403707e94f2df04Abramo Bagnara QualType T, TypeSourceInfo *TInfo, 2857ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara StorageClass SC, StorageClass SCAsWritten, 28588f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor bool isInlineSpecified, 2859af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith bool hasWrittenPrototype, 2860af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith bool isConstexprSpecified) { 2861ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo, 2862ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara T, TInfo, SC, SCAsWritten, 2863af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith isInlineSpecified, 2864af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith isConstexprSpecified); 28657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl New->HasWrittenPrototype = hasWrittenPrototype; 28667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return New; 28677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 28687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 28691e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorFunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 28701e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl)); 28711e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) FunctionDecl(Function, 0, SourceLocation(), 28721e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor DeclarationNameInfo(), QualType(), 0, 28731e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor SC_None, SC_None, false, false); 28741e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 28751e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 28767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { 28777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return new (C) BlockDecl(DC, L); 28787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 28797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 28801e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorBlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 28811e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl)); 28821e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) BlockDecl(0, SourceLocation()); 28831e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 28841e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 28857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, 28867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl SourceLocation L, 28877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl IdentifierInfo *Id, QualType T, 28887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl Expr *E, const llvm::APSInt &V) { 28897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl return new (C) EnumConstantDecl(CD, L, Id, T, E, V); 28907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 28917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 28921e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorEnumConstantDecl * 28931e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorEnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 28941e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl)); 28951e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0, 28961e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor llvm::APSInt()); 28971e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 28981e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 289999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid IndirectFieldDecl::anchor() { } 290099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 2901d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl * 2902d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 2903d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer IdentifierInfo *Id, QualType T, NamedDecl **CH, 2904d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer unsigned CHS) { 290587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS); 290687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet} 290787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet 29081e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorIndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, 29091e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor unsigned ID) { 29101e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl)); 29111e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(), 29121e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor QualType(), 0, 0); 29131e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 29141e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 29158e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const { 29168e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor SourceLocation End = getLocation(); 29178e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor if (Init) 29188e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor End = Init->getLocEnd(); 29198e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor return SourceRange(getLocation(), End); 29208e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor} 29218e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor 292299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid TypeDecl::anchor() { } 292399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 29247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, 2925344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara SourceLocation StartLoc, SourceLocation IdLoc, 2926344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara IdentifierInfo *Id, TypeSourceInfo *TInfo) { 2927344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo); 29287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 29297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl 293099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid TypedefNameDecl::anchor() { } 293199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 29321e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorTypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 29331e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl)); 29341e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0); 29351e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 29361e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 2937162e1c1b487352434552147967c3dd296ebee2f7Richard SmithTypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, 2938162e1c1b487352434552147967c3dd296ebee2f7Richard Smith SourceLocation StartLoc, 2939162e1c1b487352434552147967c3dd296ebee2f7Richard Smith SourceLocation IdLoc, IdentifierInfo *Id, 2940162e1c1b487352434552147967c3dd296ebee2f7Richard Smith TypeSourceInfo *TInfo) { 2941162e1c1b487352434552147967c3dd296ebee2f7Richard Smith return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo); 2942162e1c1b487352434552147967c3dd296ebee2f7Richard Smith} 2943162e1c1b487352434552147967c3dd296ebee2f7Richard Smith 29441e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorTypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 29451e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl)); 29461e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0); 29471e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 29481e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 2949a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange TypedefDecl::getSourceRange() const { 2950a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara SourceLocation RangeEnd = getLocation(); 2951a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { 2952a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara if (typeIsPostfix(TInfo->getType())) 2953a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 2954a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara } 2955a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara return SourceRange(getLocStart(), RangeEnd); 2956a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara} 2957a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara 2958162e1c1b487352434552147967c3dd296ebee2f7Richard SmithSourceRange TypeAliasDecl::getSourceRange() const { 2959162e1c1b487352434552147967c3dd296ebee2f7Richard Smith SourceLocation RangeEnd = getLocStart(); 2960162e1c1b487352434552147967c3dd296ebee2f7Richard Smith if (TypeSourceInfo *TInfo = getTypeSourceInfo()) 2961162e1c1b487352434552147967c3dd296ebee2f7Richard Smith RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 2962162e1c1b487352434552147967c3dd296ebee2f7Richard Smith return SourceRange(getLocStart(), RangeEnd); 2963162e1c1b487352434552147967c3dd296ebee2f7Richard Smith} 2964162e1c1b487352434552147967c3dd296ebee2f7Richard Smith 296599ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid FileScopeAsmDecl::anchor() { } 296699ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 29677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, 296821e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara StringLiteral *Str, 296921e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara SourceLocation AsmLoc, 297021e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara SourceLocation RParenLoc) { 297121e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); 29727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl} 297315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 29741e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorFileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, 29751e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor unsigned ID) { 29761e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl)); 29771e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation()); 29781e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor} 29791e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor 298015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor//===----------------------------------------------------------------------===// 298115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor// ImportDecl Implementation 298215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor//===----------------------------------------------------------------------===// 298315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 298415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor/// \brief Retrieve the number of module identifiers needed to name the given 298515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor/// module. 298615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregorstatic unsigned getNumModuleIdentifiers(Module *Mod) { 298715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor unsigned Result = 1; 298815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor while (Mod->Parent) { 298915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor Mod = Mod->Parent; 299015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor ++Result; 299115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor } 299215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor return Result; 299315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor} 299415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 29955948ae1021122164b22f74353bb7fe325a64f616Douglas GregorImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, 299615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor Module *Imported, 299715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor ArrayRef<SourceLocation> IdentifierLocs) 29985948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true), 2999e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor NextLocalImport() 300015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor{ 300115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); 300215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1); 300315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor memcpy(StoredLocs, IdentifierLocs.data(), 300415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor IdentifierLocs.size() * sizeof(SourceLocation)); 300515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor} 300615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 30075948ae1021122164b22f74353bb7fe325a64f616Douglas GregorImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, 300815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor Module *Imported, SourceLocation EndLoc) 30095948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false), 3010e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor NextLocalImport() 301115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor{ 301215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc; 301315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor} 301415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 301515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, 30165948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor SourceLocation StartLoc, Module *Imported, 301715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor ArrayRef<SourceLocation> IdentifierLocs) { 301815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor void *Mem = C.Allocate(sizeof(ImportDecl) + 301915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor IdentifierLocs.size() * sizeof(SourceLocation)); 30205948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs); 302115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor} 302215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 302315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, 30245948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor SourceLocation StartLoc, 302515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor Module *Imported, 302615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor SourceLocation EndLoc) { 302715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation)); 30285948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc); 302915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor Import->setImplicit(); 303015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor return Import; 303115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor} 303215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 30331e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, 30341e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor unsigned NumLocations) { 30351e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor void *Mem = AllocateDeserializedDecl(C, ID, 30361e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor (sizeof(ImportDecl) + 30371e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor NumLocations * sizeof(SourceLocation))); 303815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor return new (Mem) ImportDecl(EmptyShell()); 303915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor} 304015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 304115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { 304215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor if (!ImportedAndComplete.getInt()) 304315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor return ArrayRef<SourceLocation>(); 304415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 304515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor const SourceLocation *StoredLocs 304615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor = reinterpret_cast<const SourceLocation *>(this + 1); 304715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor return ArrayRef<SourceLocation>(StoredLocs, 304815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor getNumModuleIdentifiers(getImportedModule())); 304915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor} 305015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 305115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorSourceRange ImportDecl::getSourceRange() const { 305215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor if (!ImportedAndComplete.getInt()) 305315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor return SourceRange(getLocation(), 305415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor *reinterpret_cast<const SourceLocation *>(this + 1)); 305515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor 305615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor return SourceRange(getLocation(), getIdentifierLocs().back()); 305715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor} 3058