Decl.cpp revision 5bbcdbf36f8cf79d99703ef20848c55960065e43
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"
27465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara#include "clang/Basic/Specifiers.h"
284421d2b341d041df44013769f23c306308bbab83Douglas Gregor#include "clang/Basic/TargetInfo.h"
29f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "llvm/Support/ErrorHandling.h"
3027f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
33d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner//===----------------------------------------------------------------------===//
344afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// NamedDecl Implementation
355239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
365239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis
374421d2b341d041df44013769f23c306308bbab83Douglas Gregorstatic llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
384421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If this declaration has an explicit visibility attribute, use it.
394421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
404421d2b341d041df44013769f23c306308bbab83Douglas Gregor    switch (A->getVisibility()) {
414421d2b341d041df44013769f23c306308bbab83Douglas Gregor    case VisibilityAttr::Default:
424421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return DefaultVisibility;
434421d2b341d041df44013769f23c306308bbab83Douglas Gregor    case VisibilityAttr::Hidden:
444421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return HiddenVisibility;
454421d2b341d041df44013769f23c306308bbab83Douglas Gregor    case VisibilityAttr::Protected:
464421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return ProtectedVisibility;
474421d2b341d041df44013769f23c306308bbab83Douglas Gregor    }
487f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
494421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return DefaultVisibility;
50e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  }
517f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
524421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If we're on Mac OS X, an 'availability' for Mac OS X attribute
534421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // implies visibility(default).
544421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (D->getASTContext().Target.getTriple().getOS() == llvm::Triple::Darwin) {
554421d2b341d041df44013769f23c306308bbab83Douglas Gregor    for (specific_attr_iterator<AvailabilityAttr>
564421d2b341d041df44013769f23c306308bbab83Douglas Gregor              A = D->specific_attr_begin<AvailabilityAttr>(),
574421d2b341d041df44013769f23c306308bbab83Douglas Gregor           AEnd = D->specific_attr_end<AvailabilityAttr>();
584421d2b341d041df44013769f23c306308bbab83Douglas Gregor         A != AEnd; ++A)
594421d2b341d041df44013769f23c306308bbab83Douglas Gregor      if ((*A)->getPlatform()->getName().equals("macosx"))
604421d2b341d041df44013769f23c306308bbab83Douglas Gregor        return DefaultVisibility;
611fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
624421d2b341d041df44013769f23c306308bbab83Douglas Gregor
634421d2b341d041df44013769f23c306308bbab83Douglas Gregor  return llvm::Optional<Visibility>();
641fb0caaa7bef765b85972274e3b434af2572c141John McCall}
651fb0caaa7bef765b85972274e3b434af2572c141John McCall
66af14603ca61757cf4361b583b45639a04c57e651John McCalltypedef NamedDecl::LinkageInfo LinkageInfo;
671fb0caaa7bef765b85972274e3b434af2572c141John McCalltypedef std::pair<Linkage,Visibility> LVPair;
68af14603ca61757cf4361b583b45639a04c57e651John McCall
691fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair merge(LVPair L, LVPair R) {
701fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LVPair(minLinkage(L.first, R.first),
711fb0caaa7bef765b85972274e3b434af2572c141John McCall                minVisibility(L.second, R.second));
721fb0caaa7bef765b85972274e3b434af2572c141John McCall}
731fb0caaa7bef765b85972274e3b434af2572c141John McCall
74af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair merge(LVPair L, LinkageInfo R) {
75af14603ca61757cf4361b583b45639a04c57e651John McCall  return LVPair(minLinkage(L.first, R.linkage()),
76af14603ca61757cf4361b583b45639a04c57e651John McCall                minVisibility(L.second, R.visibility()));
77af14603ca61757cf4361b583b45639a04c57e651John McCall}
78af14603ca61757cf4361b583b45639a04c57e651John McCall
79752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramernamespace {
803698748400478880d2a146ef9eaa111cd0e60522John McCall/// Flags controlling the computation of linkage and visibility.
813698748400478880d2a146ef9eaa111cd0e60522John McCallstruct LVFlags {
823698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderGlobalVisibility;
833698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderVisibilityAttributes;
841a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  bool ConsiderTemplateParameterTypes;
853698748400478880d2a146ef9eaa111cd0e60522John McCall
863698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags() : ConsiderGlobalVisibility(true),
871a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderVisibilityAttributes(true),
881a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderTemplateParameterTypes(true) {
893698748400478880d2a146ef9eaa111cd0e60522John McCall  }
903698748400478880d2a146ef9eaa111cd0e60522John McCall
91381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// \brief Returns a set of flags that is only useful for computing the
92381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// linkage, not the visibility, of a declaration.
93381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  static LVFlags CreateOnlyDeclLinkage() {
94381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LVFlags F;
95381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderGlobalVisibility = false;
96381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderVisibilityAttributes = false;
971a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
98381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return F;
99381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
100381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1013698748400478880d2a146ef9eaa111cd0e60522John McCall  /// Returns a set of flags, otherwise based on these, which ignores
1023698748400478880d2a146ef9eaa111cd0e60522John McCall  /// off all sources of visibility except template arguments.
1033698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags onlyTemplateVisibility() const {
1043698748400478880d2a146ef9eaa111cd0e60522John McCall    LVFlags F = *this;
1053698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderGlobalVisibility = false;
1063698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderVisibilityAttributes = false;
1071a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
1083698748400478880d2a146ef9eaa111cd0e60522John McCall    return F;
1093698748400478880d2a146ef9eaa111cd0e60522John McCall  }
11089d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor};
111752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramer} // end anonymous namespace
1123698748400478880d2a146ef9eaa111cd0e60522John McCall
1130b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
1140b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
1151fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair
1161fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) {
1171fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1180b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1190b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
1200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
1216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          QualType T = NTTP->getExpansionType(I);
1256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          if (!T->isDependentType())
1266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor            LV = merge(LV, T->getLinkageAndVisibility());
1276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
1286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        continue;
1296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1310b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
1321fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
1330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
1340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
1356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
1380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
139af14603ca61757cf4361b583b45639a04c57e651John McCall      LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
1400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1431fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
146381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor/// getLVForDecl - Get the linkage and visibility for the given declaration.
147381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
148381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
1500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
1511fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
152381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           unsigned NumArgs,
153381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           LVFlags &F) {
1541fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
1580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
1590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
1600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
1610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
1641fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
1650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
1681fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
1691fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
1701fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
17189d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
17289d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor          LV = merge(LV, getLVForDecl(ND, F));
1731fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
177a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
178a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      if (TemplateDecl *Template
179a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
18089d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        LV = merge(LV, getLVForDecl(Template, F));
1810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
1841fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
185381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  Args[I].pack_size(),
186381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  F));
1870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1911fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
194af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair
195381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
196381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                             LVFlags &F) {
197381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
1983cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
1993cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2003698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
2017a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
202d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
203d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
204d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
205d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
206d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
209d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
210d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
211d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
213d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
214af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
221e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
222d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
223d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
224d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
225d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
226d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
227d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
2280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
229d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
231d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
232af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::internal();
233d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
2350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
2360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
2370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
240d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
241d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
242d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
244d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
245d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
246d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
247af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo(InternalLinkage, DefaultVisibility, false);
248d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
249d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
250d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
251af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
252d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
253d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
254094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  if (D->isInAnonymousNamespace()) {
255094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const VarDecl *Var = dyn_cast<VarDecl>(D);
256094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
257094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
258094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth      return LinkageInfo::uniqueExternal();
259094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  }
260e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2611fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2621fb0caaa7bef765b85972274e3b434af2572c141John McCall
2631fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2641fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2651fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2661fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
267af14603ca61757cf4361b583b45639a04c57e651John McCall  LinkageInfo LV;
268af14603ca61757cf4361b583b45639a04c57e651John McCall
2693698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
2704421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
2714421d2b341d041df44013769f23c306308bbab83Douglas Gregor      LV.setVisibility(*Vis, true);
2723698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
27390f1450c109fbbd333001165bbd986061f7c4513John McCall    } else {
27490f1450c109fbbd333001165bbd986061f7c4513John McCall      // If we're declared in a namespace with a visibility attribute,
27590f1450c109fbbd333001165bbd986061f7c4513John McCall      // use that namespace's visibility, but don't call it explicit.
27690f1450c109fbbd333001165bbd986061f7c4513John McCall      for (const DeclContext *DC = D->getDeclContext();
27790f1450c109fbbd333001165bbd986061f7c4513John McCall           !isa<TranslationUnitDecl>(DC);
27890f1450c109fbbd333001165bbd986061f7c4513John McCall           DC = DC->getParent()) {
27990f1450c109fbbd333001165bbd986061f7c4513John McCall        if (!isa<NamespaceDecl>(DC)) continue;
2804421d2b341d041df44013769f23c306308bbab83Douglas Gregor        if (llvm::Optional<Visibility> Vis
2814421d2b341d041df44013769f23c306308bbab83Douglas Gregor                           = cast<NamespaceDecl>(DC)->getExplicitVisibility()) {
2824421d2b341d041df44013769f23c306308bbab83Douglas Gregor          LV.setVisibility(*Vis, false);
28390f1450c109fbbd333001165bbd986061f7c4513John McCall          F.ConsiderGlobalVisibility = false;
28490f1450c109fbbd333001165bbd986061f7c4513John McCall          break;
28590f1450c109fbbd333001165bbd986061f7c4513John McCall        }
28690f1450c109fbbd333001165bbd986061f7c4513John McCall      }
2873698748400478880d2a146ef9eaa111cd0e60522John McCall    }
288af14603ca61757cf4361b583b45639a04c57e651John McCall  }
2891fb0caaa7bef765b85972274e3b434af2572c141John McCall
290d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
2911fb0caaa7bef765b85972274e3b434af2572c141John McCall
292d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
293d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
294d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
295d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
296d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
297110e8e56af30363072c140285961592b0107f789John McCall    // GCC applies the following optimization to variables and static
298110e8e56af30363072c140285961592b0107f789John McCall    // data members, but not to functions:
299110e8e56af30363072c140285961592b0107f789John McCall    //
3001fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
3011fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
3021fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
3031fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
3041fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
3051fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
3061fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
3071fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
3081fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
3091fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
3101fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
3111fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
3121fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
313ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
314ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
315ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
3161fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
3171fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
318ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
3191fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
3201fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
321af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
322af14603ca61757cf4361b583b45639a04c57e651John McCall      if (!LV.visibilityExplicit())
323af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(TypeLV.second);
324110e8e56af30363072c140285961592b0107f789John McCall    }
325110e8e56af30363072c140285961592b0107f789John McCall
32635cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Var->getStorageClass() == SC_PrivateExtern)
32735cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
32835cebc3eea898637057b10b5cf7dd08b1d788980John McCall
329d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
330d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
331d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
3321fb0caaa7bef765b85972274e3b434af2572c141John McCall
333d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
334d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
335d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
336d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
337d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
338d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
339d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
340d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
341d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
342d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
343381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
344af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
345af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
346d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
347d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
348d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
349d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
3501fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
35167fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
35267fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
35367fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
35467fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
3551fb0caaa7bef765b85972274e3b434af2572c141John McCall
35635cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Function->getStorageClass() == SC_PrivateExtern)
35735cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
35835cebc3eea898637057b10b5cf7dd08b1d788980John McCall
359d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
360d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
361d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
362d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
363d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
364d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
365d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
366d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
367d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
368d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
369d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
370d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
371d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
372d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
373d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
374d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
375d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
376d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
377d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
378381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
379af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
380af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
381d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
382d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
383d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
384af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // In C++, then if the type of the function uses a type with
385af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // unique-external linkage, it's not legally usable from outside
386af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // this translation unit.  However, we should use the C linkage
387af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // rules instead for extern "C" declarations.
388af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
389af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall        Function->getType()->getLinkage() == UniqueExternalLinkage)
390af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
391af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
3920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
3930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
3943698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(SpecInfo->getTemplate(),
3953698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
3960b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
397381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
3980b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
3990b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
400d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
401d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
402d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
403d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
404d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
405d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
4061fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
4071fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
4081fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
409af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
4101fb0caaa7bef765b85972274e3b434af2572c141John McCall
4111fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
4121fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
4131fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (const ClassTemplateSpecializationDecl *Spec
4141fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
4153698748400478880d2a146ef9eaa111cd0e60522John McCall      // From the template.
4163698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
4173698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
4181fb0caaa7bef765b85972274e3b434af2572c141John McCall
4191fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The arguments at which the template was instantiated.
4201fb0caaa7bef765b85972274e3b434af2572c141John McCall      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
421381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4220b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
423d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
424ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
4253698748400478880d2a146ef9eaa111cd0e60522John McCall    if (F.ConsiderGlobalVisibility)
4263698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility =
427ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
428ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
4291fb0caaa7bef765b85972274e3b434af2572c141John McCall
430d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
4311fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
432381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
433af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!isExternalLinkage(EnumLV.linkage()))
434af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
435af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(EnumLV);
436d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
437d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
438d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
4391a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
4401a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    if (F.ConsiderTemplateParameterTypes)
4411a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
4420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
443d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
444d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
4451fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
4461fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
4471fb0caaa7bef765b85972274e3b434af2572c141John McCall
4481fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
4491fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
4501fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
4511fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
4521fb0caaa7bef765b85972274e3b434af2572c141John McCall
4531fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
4541fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
455af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4561fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4571fb0caaa7bef765b85972274e3b434af2572c141John McCall
4581fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
4591fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
460af14603ca61757cf4361b583b45639a04c57e651John McCall  if (LV.linkage() != ExternalLinkage)
461af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo(LV.linkage(), DefaultVisibility, false);
4621fb0caaa7bef765b85972274e3b434af2572c141John McCall
4631fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
4641fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
4653698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility)
466af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
467d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4681fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
469d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
470d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4713698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
4721fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
4731fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
4741fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
4751fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
4763cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
4773cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
4781fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
4793cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
4803cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall         (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
481af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4823cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4833698748400478880d2a146ef9eaa111cd0e60522John McCall  LinkageInfo LV;
4843698748400478880d2a146ef9eaa111cd0e60522John McCall
4853698748400478880d2a146ef9eaa111cd0e60522John McCall  // The flags we're going to use to compute the class's visibility.
4863698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags ClassF = F;
4873698748400478880d2a146ef9eaa111cd0e60522John McCall
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
4933698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore global visibility later, but not this attribute.
4943698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
4953698748400478880d2a146ef9eaa111cd0e60522John McCall
4963698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore both global visibility and attributes when computing our
4973698748400478880d2a146ef9eaa111cd0e60522John McCall      // parent's visibility.
4983698748400478880d2a146ef9eaa111cd0e60522John McCall      ClassF = F.onlyTemplateVisibility();
4993698748400478880d2a146ef9eaa111cd0e60522John McCall    }
5003698748400478880d2a146ef9eaa111cd0e60522John McCall  }
501af14603ca61757cf4361b583b45639a04c57e651John McCall
502af14603ca61757cf4361b583b45639a04c57e651John McCall  // Class members only have linkage if their class has external
5033698748400478880d2a146ef9eaa111cd0e60522John McCall  // linkage.
5043698748400478880d2a146ef9eaa111cd0e60522John McCall  LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
5053698748400478880d2a146ef9eaa111cd0e60522John McCall  if (!isExternalLinkage(LV.linkage()))
506af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5073cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5083cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
5093698748400478880d2a146ef9eaa111cd0e60522John McCall  if (LV.linkage() == UniqueExternalLinkage)
510af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
5111fb0caaa7bef765b85972274e3b434af2572c141John McCall
5123cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
513af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // If the type of the function uses a type with unique-external
514af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // linkage, it's not legally usable from outside this translation unit.
515af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (MD->getType()->getLinkage() == UniqueExternalLinkage)
516af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
517af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
518110e8e56af30363072c140285961592b0107f789John McCall    TemplateSpecializationKind TSK = TSK_Undeclared;
519110e8e56af30363072c140285961592b0107f789John McCall
5201fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
5211fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
5221fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (FunctionTemplateSpecializationInfo *Spec
5233cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
524381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
5251a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      if (F.ConsiderTemplateParameterTypes)
5261a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall        LV.merge(getLVForTemplateParameterList(
5271fb0caaa7bef765b85972274e3b434af2572c141John McCall                              Spec->getTemplate()->getTemplateParameters()));
528110e8e56af30363072c140285961592b0107f789John McCall
529110e8e56af30363072c140285961592b0107f789John McCall      TSK = Spec->getTemplateSpecializationKind();
530110e8e56af30363072c140285961592b0107f789John McCall    } else if (MemberSpecializationInfo *MSI =
531110e8e56af30363072c140285961592b0107f789John McCall                 MD->getMemberSpecializationInfo()) {
532110e8e56af30363072c140285961592b0107f789John McCall      TSK = MSI->getTemplateSpecializationKind();
5333cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
5343cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
535110e8e56af30363072c140285961592b0107f789John McCall    // If we're paying attention to global visibility, apply
536110e8e56af30363072c140285961592b0107f789John McCall    // -finline-visibility-hidden if this is an inline method.
537110e8e56af30363072c140285961592b0107f789John McCall    //
538af14603ca61757cf4361b583b45639a04c57e651John McCall    // Note that ConsiderGlobalVisibility doesn't yet have information
539af14603ca61757cf4361b583b45639a04c57e651John McCall    // about whether containing classes have visibility attributes,
540af14603ca61757cf4361b583b45639a04c57e651John McCall    // and that's intentional.
541af14603ca61757cf4361b583b45639a04c57e651John McCall    if (TSK != TSK_ExplicitInstantiationDeclaration &&
5423698748400478880d2a146ef9eaa111cd0e60522John McCall        F.ConsiderGlobalVisibility &&
54366cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
54466cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // InlineVisibilityHidden only applies to definitions, and
54566cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // isInlined() only gives meaningful answers on definitions
54666cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // anyway.
54766cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      const FunctionDecl *Def = 0;
54866cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      if (MD->hasBody(Def) && Def->isInlined())
54966cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        LV.setVisibility(HiddenVisibility);
55066cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall    }
5511fb0caaa7bef765b85972274e3b434af2572c141John McCall
552110e8e56af30363072c140285961592b0107f789John McCall    // Note that in contrast to basically every other situation, we
553110e8e56af30363072c140285961592b0107f789John McCall    // *do* apply -fvisibility to method declarations.
554110e8e56af30363072c140285961592b0107f789John McCall
555110e8e56af30363072c140285961592b0107f789John McCall  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
556110e8e56af30363072c140285961592b0107f789John McCall    if (const ClassTemplateSpecializationDecl *Spec
557110e8e56af30363072c140285961592b0107f789John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
558110e8e56af30363072c140285961592b0107f789John McCall      // Merge template argument/parameter information for member
559110e8e56af30363072c140285961592b0107f789John McCall      // class template specializations.
560381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
5611a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      if (F.ConsiderTemplateParameterTypes)
5621a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall        LV.merge(getLVForTemplateParameterList(
5631fb0caaa7bef765b85972274e3b434af2572c141John McCall                    Spec->getSpecializedTemplate()->getTemplateParameters()));
564110e8e56af30363072c140285961592b0107f789John McCall    }
565110e8e56af30363072c140285961592b0107f789John McCall
566110e8e56af30363072c140285961592b0107f789John McCall  // Static data members.
567110e8e56af30363072c140285961592b0107f789John McCall  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
568ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // Modify the variable's linkage by its type, but ignore the
569ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // type's visibility unless it's a definition.
570ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
571ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (TypeLV.first != ExternalLinkage)
572af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeLinkage(UniqueExternalLinkage);
573af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!LV.visibilityExplicit())
574af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeVisibility(TypeLV.second);
575110e8e56af30363072c140285961592b0107f789John McCall  }
576110e8e56af30363072c140285961592b0107f789John McCall
5773698748400478880d2a146ef9eaa111cd0e60522John McCall  F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
578110e8e56af30363072c140285961592b0107f789John McCall
579110e8e56af30363072c140285961592b0107f789John McCall  // Apply -fvisibility if desired.
5803698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
581af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
5823cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
5833cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5841fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
5853cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
5863cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
587f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallstatic void clearLinkageForClass(const CXXRecordDecl *record) {
588f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  for (CXXRecordDecl::decl_iterator
589f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall         i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
590f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    Decl *child = *i;
591f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    if (isa<NamedDecl>(child))
592f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall      cast<NamedDecl>(child)->ClearLinkageCache();
593f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
594f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
595f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
596f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallvoid NamedDecl::ClearLinkageCache() {
597f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // Note that we can't skip clearing the linkage of children just
598f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // because the parent doesn't have cached linkage:  we don't cache
599f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // when computing linkage for parent contexts.
600f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
601f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  HasCachedLinkage = 0;
602f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
603f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // If we're changing the linkage of a class, we need to reset the
604f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // linkage of child declarations, too.
605f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
606f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
607f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
60815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (ClassTemplateDecl *temp =
60915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
610f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    // Clear linkage for the template pattern.
611f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    CXXRecordDecl *record = temp->getTemplatedDecl();
612f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    record->HasCachedLinkage = 0;
613f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
614f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
61515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // We need to clear linkage for specializations, too.
61615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (ClassTemplateDecl::spec_iterator
61715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
61815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
619f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
62015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
62115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Clear cached linkage for function template decls, too.
62215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (FunctionTemplateDecl *temp =
62378951941f31d3c63c4178a1275e1a2db2e20da11John McCall        dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
62478951941f31d3c63c4178a1275e1a2db2e20da11John McCall    temp->getTemplatedDecl()->ClearLinkageCache();
62515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (FunctionTemplateDecl::spec_iterator
62615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
62715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
62878951941f31d3c63c4178a1275e1a2db2e20da11John McCall  }
62915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
630f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
631f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
632381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorLinkage NamedDecl::getLinkage() const {
633381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (HasCachedLinkage) {
63456ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer    assert(Linkage(CachedLinkage) ==
63556ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer             getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
636381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return Linkage(CachedLinkage);
637381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
638381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
639381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = getLVForDecl(this,
640381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                               LVFlags::CreateOnlyDeclLinkage()).linkage();
641381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
642381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return Linkage(CachedLinkage);
643381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
644381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
645af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const {
646381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  LinkageInfo LI = getLVForDecl(this, LVFlags());
64756ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer  assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
648381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
649381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = LI.linkage();
650381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return LI;
6510df9587ab011c12968fcbe3518666b2117afe350John McCall}
652becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
6534421d2b341d041df44013769f23c306308bbab83Douglas Gregorllvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
6544421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Use the most recent declaration of a variable.
6554421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const VarDecl *var = dyn_cast<VarDecl>(this))
6564421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return getVisibilityOf(var->getMostRecentDeclaration());
6574421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6584421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Use the most recent declaration of a function, and also handle
6594421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // function template specializations.
6604421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
6614421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> V
6624421d2b341d041df44013769f23c306308bbab83Douglas Gregor                            = getVisibilityOf(fn->getMostRecentDeclaration()))
6634421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return V;
6644421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6654421d2b341d041df44013769f23c306308bbab83Douglas Gregor    // If the function is a specialization of a template with an
6664421d2b341d041df44013769f23c306308bbab83Douglas Gregor    // explicit visibility attribute, use that.
6674421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (FunctionTemplateSpecializationInfo *templateInfo
6684421d2b341d041df44013769f23c306308bbab83Douglas Gregor          = fn->getTemplateSpecializationInfo())
6694421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
6704421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6714421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return llvm::Optional<Visibility>();
6724421d2b341d041df44013769f23c306308bbab83Douglas Gregor  }
6734421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6744421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Otherwise, just check the declaration itself first.
6754421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (llvm::Optional<Visibility> V = getVisibilityOf(this))
6764421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return V;
6774421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6784421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If there wasn't explicit visibility there, and this is a
6794421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // specialization of a class template, check for visibility
6804421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // on the pattern.
6814421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const ClassTemplateSpecializationDecl *spec
6824421d2b341d041df44013769f23c306308bbab83Douglas Gregor        = dyn_cast<ClassTemplateSpecializationDecl>(this))
6834421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
6844421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6854421d2b341d041df44013769f23c306308bbab83Douglas Gregor  return llvm::Optional<Visibility>();
6864421d2b341d041df44013769f23c306308bbab83Douglas Gregor}
6874421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6883698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
689becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
690becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
6910df9587ab011c12968fcbe3518666b2117afe350John McCall  switch (D->getKind()) {
692becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
693becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
6941fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
6951fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
696becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
697becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
698becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
699becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
700becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
701becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
702becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
703becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
704becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
705becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
706af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::external();
707becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
708becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
709d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
7100df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->getRedeclContext()->isFileContext())
7113698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForNamespaceScopeDecl(D, Flags);
712d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
713d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
714d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
715d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
716d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
717d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
718d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
719d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
7200df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->isRecord())
7213698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForClassMember(D, Flags);
722d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
723d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
724d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
725d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
726d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
727d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
728d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
729d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
730d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
731d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
732d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
733d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
7340df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
7350df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
73610aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth      if (Function->isInAnonymousNamespace() && !Function->isExternC())
737af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
7381fb0caaa7bef765b85972274e3b434af2572c141John McCall
739af14603ca61757cf4361b583b45639a04c57e651John McCall      LinkageInfo LV;
740381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      if (Flags.ConsiderVisibilityAttributes) {
7414421d2b341d041df44013769f23c306308bbab83Douglas Gregor        if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
7424421d2b341d041df44013769f23c306308bbab83Douglas Gregor          LV.setVisibility(*Vis);
743381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      }
744381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7451fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
746381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
747af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
748af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
7491fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
7500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
7511fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
752d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
753d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
7540df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const VarDecl *Var = dyn_cast<VarDecl>(D))
755d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
756d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
75710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth        if (Var->isInAnonymousNamespace() && !Var->isExternC())
758af14603ca61757cf4361b583b45639a04c57e651John McCall          return LinkageInfo::uniqueExternal();
7591fb0caaa7bef765b85972274e3b434af2572c141John McCall
760af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo LV;
7611fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
762af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(HiddenVisibility);
763381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        else if (Flags.ConsiderVisibilityAttributes) {
7644421d2b341d041df44013769f23c306308bbab83Douglas Gregor          if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
7654421d2b341d041df44013769f23c306308bbab83Douglas Gregor            LV.setVisibility(*Vis);
766381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        }
767381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7681fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
769381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
770af14603ca61757cf4361b583b45639a04c57e651John McCall          if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
771af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.mergeVisibility(PrevLV);
7721fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
7731fb0caaa7bef765b85972274e3b434af2572c141John McCall
7741fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
775d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
776d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
777d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
778d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
779d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
780af14603ca61757cf4361b583b45639a04c57e651John McCall  return LinkageInfo::none();
7811fb0caaa7bef765b85972274e3b434af2572c141John McCall}
782d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
78347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
7843a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
7853a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
7863a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
7873a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
78847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
78947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
79047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
79147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
79247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
79368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
79468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
79568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
79668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
79768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
79868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
79968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
80068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
80168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
80268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
80368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
80468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
80568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
80668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
80868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
809f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
810f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
811f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
812910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.data(),
813910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.size(),
8143a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
81568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
81668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
8176be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
81868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
8196be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
82068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
82168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
82268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
82368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
82468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
82568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
82668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
8273521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
8283521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
8293521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
8303521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
83168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
8323521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
8333521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
8343521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
8353521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
83668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
8373521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
8383521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
83968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
8403521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8413521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
8423521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
8433521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
84468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
84568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
8463521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8473521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
84868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
84968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
85068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
85168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
85268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
85347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
85447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8558472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
85668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
8578472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
85868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
85947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
86068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
86147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
86247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8634afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
8646ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
8656ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
8662a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
8672a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
8682a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
869db9924191092b4d426cc066637d81698211846aaDouglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
870db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace() ==
871db9924191092b4d426cc066637d81698211846aaDouglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
872db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace();
8732a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
8741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8756ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
8766ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
8776ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
8786ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
879e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
880e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
881e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
882e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
883e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
884e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
885e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
8861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8870de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
8880de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
8890de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
891f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
892f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
893f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
8949488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
8959488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
8969488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
8979488ea120e093068021f944176c3d610dd540914John McCall
898dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
899dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    ASTContext &Context = getASTContext();
900dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return Context.getCanonicalNestedNameSpecifier(
901dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                     cast<UsingDecl>(this)->getQualifier()) ==
902dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor           Context.getCanonicalNestedNameSpecifier(
903dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                        cast<UsingDecl>(OldD)->getQualifier());
904dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  }
905c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis
9066ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
9076ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
9086ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
9096ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
9106ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
9116ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
912d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
913d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
914d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
9154afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
916e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
917e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
918e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
9199488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
920e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
921e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
922e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
923e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
924e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
925e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
926e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
927e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
928e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
929161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
930161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
931161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
932161755a09898c95d21bfff33707da9ca41cd53c5John McCall
933161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
934161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
935161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
936161755a09898c95d21bfff33707da9ca41cd53c5John McCall
93787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
938161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
939161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
940161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
941161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
942161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
943161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
944161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
945161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
946161755a09898c95d21bfff33707da9ca41cd53c5John McCall
9475239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
948a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
949a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
950a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
9511693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
9521693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
9531693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
9541693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
9551693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
9561693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
9571693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9581693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
959a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
9604e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
9614e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
962a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
963a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
964a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
965c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
966c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
967b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
968b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
969b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
970b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
971b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
972b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
973b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
974b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
975b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
976b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
977c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
978b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
979b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
980b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
981b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
9827f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      if (getExtInfo()->NumTemplParamLists == 0) {
9837f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Save type source info pointer.
9847f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
9857f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Deallocate the extended decl info.
9867f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getASTContext().Deallocate(getExtInfo());
9877f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Restore savedTInfo into (non-extended) decl info.
9887f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        DeclInfo = savedTInfo;
9897f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      }
9907f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      else
9917f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getExtInfo()->QualifierLoc = QualifierLoc;
992b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
993b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
994b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
995b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
9967f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnaravoid
9977f0a915eb546d353071be08c8adec155e5d9a0dcAbramo BagnaraDeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
9987f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                              unsigned NumTPLists,
9997f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                              TemplateParameterList **TPLists) {
10007f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  assert(NumTPLists > 0);
10017f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Make sure the extended decl info is allocated.
10027f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  if (!hasExtInfo()) {
10037f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Save (non-extended) type source info pointer.
10047f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
10057f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Allocate external info struct.
10067f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    DeclInfo = new (getASTContext()) ExtInfo;
10077f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Restore savedTInfo into (extended) decl info.
10087f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    getExtInfo()->TInfo = savedTInfo;
10097f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  }
10107f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Set the template parameter lists info.
10117f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
10127f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara}
10137f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara
10141693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
10151693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
10161693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
10171693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1018a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnaranamespace {
1019a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1020a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// Helper function: returns true if QT is or contains a type
1021a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// having a postfix component.
1022a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnarabool typeIsPostfix(clang::QualType QT) {
1023a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  while (true) {
1024a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    const Type* T = QT.getTypePtr();
1025a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    switch (T->getTypeClass()) {
1026a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    default:
1027a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      return false;
1028a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::Pointer:
1029a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<PointerType>(T)->getPointeeType();
1030a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1031a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::BlockPointer:
1032a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<BlockPointerType>(T)->getPointeeType();
1033a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1034a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::MemberPointer:
1035a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<MemberPointerType>(T)->getPointeeType();
1036a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1037a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::LValueReference:
1038a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::RValueReference:
1039a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<ReferenceType>(T)->getPointeeType();
1040a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1041a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::PackExpansion:
1042a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<PackExpansionType>(T)->getPattern();
1043a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1044a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::Paren:
1045a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::ConstantArray:
1046a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::DependentSizedArray:
1047a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::IncompleteArray:
1048a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::VariableArray:
1049a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::FunctionProto:
1050a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::FunctionNoProto:
1051a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      return true;
1052a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    }
1053a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
1054a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1055a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1056a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara} // namespace
1057a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1058a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange DeclaratorDecl::getSourceRange() const {
1059a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  SourceLocation RangeEnd = getLocation();
1060a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1061a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    if (typeIsPostfix(TInfo->getType()))
1062a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1063a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
1064a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getOuterLocStart(), RangeEnd);
1065a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1066a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
10679b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
1068c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1069c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
10709b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
10719b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
10729b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
10739b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
10749b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
10759b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
1076c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
10779b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
10789b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
10799b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
10809b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
10819b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
1082c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
10839b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
10849b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
10859b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
10869b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
10879b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
10889b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
1089a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
109099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
109199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
109299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
10937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
10947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
1095d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_None:          break;
1096d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Auto:          return "auto"; break;
1097d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Extern:        return "extern"; break;
1098d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_PrivateExtern: return "__private_extern__"; break;
1099d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Register:      return "register"; break;
1100d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Static:        return "static"; break;
11017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
11047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
11057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1107ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo BagnaraVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1108ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                         SourceLocation StartL, SourceLocation IdL,
1109a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
111016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
1111ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
111299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
111399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
1114381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid VarDecl::setStorageClass(StorageClass SC) {
1115381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForVariable(SC));
1116381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1117381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1118381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1119381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1120381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1121381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
11221693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
112355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
11241693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1125a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return DeclaratorDecl::getSourceRange();
112655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
112755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
11287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
11297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
11307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
11317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
1132d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
11337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
11347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
113510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
113610aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isFunctionOrMethod())
113710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
113810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
113910aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
11407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
11417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1142d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
11437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
11457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
11467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
11507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
11537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
11547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1156e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1157e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
1158e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
1159e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
1160e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
1161e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
1162e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
1163e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
1164e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
1165e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
1166e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
1167e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1168e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
1169e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
1170e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
1171e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1172e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
1173e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
1174e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
1175e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
1176e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
1177e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
1178e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
1179e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
1180e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
1181e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
1182e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
1183e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
11842bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
1185d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
1186d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
11872bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
11882bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
11892bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
11902bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
11912bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
11922bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
1193e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
1194e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
1195e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
1196e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
1197e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
1198e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1199e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
1200e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1201e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
1202e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
1203e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
1204e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1205e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1206e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
1207e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1208e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1209e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
1210e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1211f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
1212e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
1213e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1214e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
1215e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
1216e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
1217e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
1218e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
1219e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
1220e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1221e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
1222e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1223e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1224e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
1225e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1226e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1227e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
1228e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1229e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1230e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
1231e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
1232e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
123331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
123431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
123531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
123631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
1237e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
1238e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1239e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
124031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
124131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
124231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
124331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
1244e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1245e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1246110e8e56af30363072c140285961592b0107f789John McCallVarDecl::DefinitionKind VarDecl::hasDefinition() const {
1247110e8e56af30363072c140285961592b0107f789John McCall  DefinitionKind Kind = DeclarationOnly;
1248110e8e56af30363072c140285961592b0107f789John McCall
1249110e8e56af30363072c140285961592b0107f789John McCall  const VarDecl *First = getFirstDeclaration();
1250110e8e56af30363072c140285961592b0107f789John McCall  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1251110e8e56af30363072c140285961592b0107f789John McCall       I != E; ++I)
1252110e8e56af30363072c140285961592b0107f789John McCall    Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1253110e8e56af30363072c140285961592b0107f789John McCall
1254110e8e56af30363072c140285961592b0107f789John McCall  return Kind;
1255110e8e56af30363072c140285961592b0107f789John McCall}
1256110e8e56af30363072c140285961592b0107f789John McCall
125731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
12587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
12597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
12607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
12617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
126331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
12647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
12657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
12667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
12677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12691028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
1270da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
12711028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
12728761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
12738761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
12748761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
12758761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
12761028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
12771028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
12781028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
12791028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
12801028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
12811028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
12821028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
12831028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
12841028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
12850d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
12860d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
12870d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
12880d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
12890d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
12900d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
12910d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
12920d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
12930d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
12940d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
12950d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
12960d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
12970d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
1298838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
12997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
13007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
1301838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
13027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
13037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
13057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13071028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1308b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1309251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1310251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1311251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1312251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1313251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1314663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1315e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1316251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1317251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1318251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1319251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1320251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
13211028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1322b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1323b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1324b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
13250a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
13260a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1327b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1328251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1329251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
13300a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
13310a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
13320a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
13330a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
13347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
13357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
13367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
13377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
13387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1339275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
13407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1341ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 SourceLocation StartLoc,
1342ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 SourceLocation IdLoc, IdentifierInfo *Id,
13437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
134416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
134516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
1346ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
134716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1348275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1349275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
13507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
13517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
13527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
13537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
13547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
13564765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
13577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
13587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
13607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
13634765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
13647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
1365275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
1366c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
1367275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1368275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
13697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
13707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
13717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
13727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13734765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
13747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
13757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
13787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
13797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
13807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
13827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
13837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1385fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1386fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
13871fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool ParmVarDecl::isParameterPack() const {
13881fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isa<PackExpansionType>(getType());
13891fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
13901fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
139199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
13928a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
13938a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
13948a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1395da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1396da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        const PrintingPolicy &Policy,
1397da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        bool Qualified) const {
1398da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1399da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1400da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (TemplateArgs)
1401da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    S += TemplateSpecializationType::PrintTemplateArgumentList(
1402da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->data(),
1403da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->size(),
1404da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                               Policy);
1405da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
1406da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
1407da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
14089498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
14099498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
14109498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
14119498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
14129498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
14139498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
141406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
141506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
141606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (I->Body) {
141706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
141806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
141906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
142006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
142106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
142206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
142306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
142406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
14256fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1426c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1427c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1428c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1429c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
1430f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1431f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1432f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1433f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
143655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
143755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
1438b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  if (B)
143955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
144055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
144155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
14422138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
14432138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
14442138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
14452138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
14462138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
14472138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
14482138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
144948a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
145048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
145107a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
14527a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    getDeclContext()->getRedeclContext()->isTranslationUnit() &&
145304495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
145404495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
145504495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
145648a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
145748a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
14586393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
14596393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
14606393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1461d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
14626393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
146310aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
146410aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isRecord())
146510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
146610aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
146710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
14686393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
14696393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1470d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
147140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
14726393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
14736393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
14746393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
14756393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
14766393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
14770bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
14786393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
14796393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
14808499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
14818499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
14828499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
14838499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1484d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
14858499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
14868499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
14888499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
14898499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
14908499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
14918499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
14928499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
14938499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
14948499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
14958499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
14968499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
14978499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
14988499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
14998499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
15007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
15017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
15027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
15037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
15057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
15067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
15077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
15087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
15097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
15108f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
15118f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (PrevDecl->IsInline)
15128f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    IsInline = true;
15137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
15167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
15177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
15207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
15217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1523381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid FunctionDecl::setStorageClass(StorageClass SC) {
1524381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForFunction(SC));
1525381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1526381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1527381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1528381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1529381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1530381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
15313e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
15323e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
15333e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
15343e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
15353e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
15363e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
15373e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
15393e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
15407814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
15417814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
15423c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
15433c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
15443c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
15453c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
15463c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
15473c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
15483c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
15493c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
15503c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
15513c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
15523c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
15539add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1554d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
15559add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
15569add31798f621f843233dbff8bba103fca64447bDouglas Gregor
15573c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
15583c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
15593c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
15603c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
15613c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
15623c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
15633c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
15643c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
15653c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
15661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
15673c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
156840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
15693c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
15703c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
15713c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
15723e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
15733e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
15743e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
15753e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
15761ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
15778dbfbf4c95251c69a455d4d016d6c7890c932007Bob Wilson/// based on its FunctionType.  This is the length of the ParamInfo array
15781ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
15791ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1580183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
158172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1582d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
158372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
15841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15876b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
15886b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                             ParmVarDecl **NewParamInfo, unsigned NumParams) {
15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
15902dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
15911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
15946b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1595fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
15965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
159755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
159896888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
159996888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1600cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
160155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
16025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16058123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
16068123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
16078123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
1608f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// arguments (in C++) or the last parameter is a parameter pack.
16098123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
16107d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  if (!getASTContext().getLangOptions().CPlusPlus)
16117d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    return getNumParams();
16127d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
1613f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  unsigned NumRequiredArgs = getNumParams();
1614f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1615f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If the last parameter is a parameter pack, we don't need an argument for
1616f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1617f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (NumRequiredArgs > 0 &&
1618f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1619f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    --NumRequiredArgs;
1620f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1621f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If this parameter has a default argument, we don't need an argument for
1622f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1623f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  while (NumRequiredArgs > 0 &&
1624f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor         getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
16258123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
16268123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
16277d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // We might have parameter packs before the end. These can't be deduced,
16287d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // but they can still handle multiple arguments.
16297d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  unsigned ArgIdx = NumRequiredArgs;
16307d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  while (ArgIdx > 0) {
16317d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (getParamDecl(ArgIdx - 1)->isParameterPack())
16327d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      NumRequiredArgs = ArgIdx;
16337d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
16347d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    --ArgIdx;
16357d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  }
16367d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
16378123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
16388123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
16398123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
16407ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
16418f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (IsInline)
16427d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
164348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
164448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
164548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
164648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
164748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
16487d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
16497d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
16507d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
16517d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
16527d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
16537d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
16547d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
16557d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
16567d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
16577d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
16587d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
16597d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
16607d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
16617d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
166206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
16637d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
166406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
16657d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
166606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
16677d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
16687d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
16697d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
16707ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
16717ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
16727d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
16731fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
16741fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
16751fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
16761fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
16771fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
16781fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
16791fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
16801fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
16811e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
16821fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
16831fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
16841fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
16851fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
16861fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
16871fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
16881fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
16891fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
16901fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
16917ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
16927d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
16931fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16947d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
16958f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If it's not the case that both 'inline' and 'extern' are
16968f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // specified on the definition, then this inline definition is
16978f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // externally visible.
16988f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
16998f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      return true;
17008f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
17018f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If any declaration is 'inline' but not 'extern', then this definition
17028f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // is externally visible.
17031fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
17041fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
17051fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
17068f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      if (Redecl->isInlineSpecified() &&
17078f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor          Redecl->getStorageClassAsWritten() != SC_Extern)
17081fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
17098f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    }
17101fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
17119f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
17121fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
17131fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
17141fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
17151fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
17161fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
17171fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
17181fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
17191fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
17201fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
17211fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
17221fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
17231fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
17241fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1725d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
17261fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
17271fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
17281fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
17291fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
17301fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
17311fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
17321fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
17339f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
17349f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
17359f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
17361cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
17371cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
17381cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1739e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1740e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
17411cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
17421cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
17431cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
17441cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1745a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1746a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1747a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1748a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1749a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1750a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1751a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1752a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1753a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1754d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1755d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1756d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1757d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1758d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1759d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1760d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1761d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1762d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1763d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1764d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1765d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1766d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1767d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1768d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1769d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1770d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
17712db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1772b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
17732db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
17742db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
17752db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
17762db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
17772db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1778b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1779b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1780b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1781b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
17822db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
17836b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
17846b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
17852db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
17862db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
17872db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
17882db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
17896b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
17902db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
17912db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
17922db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
17933b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
17946cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
17956cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
17963b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
17973b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17983b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
17993b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
18003b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
18013b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
18023b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
18033b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18043b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
18053b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
18063b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18073b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
18083b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
18093b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
18103b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
18113b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18123b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
18133b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
181406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
18153b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
181606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
18173b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18183b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
18193b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
18203b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
18213b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
182206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
18233b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
18243b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18257ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
18263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
18273b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18283b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
18293b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
18303b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
18313b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
18323b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
18333b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
18343b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
18353b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18363b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
18373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
18383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
18403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
18413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
18433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
18443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
184516e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
18461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
184716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
184816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
18491fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
185016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
185116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
185216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
185316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
185416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
185516e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
18561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1857fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1858fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
185916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
186016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
186116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
186216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
186316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1864e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1865e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1866e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1867e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1868e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1869e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1870e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1871e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1872e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1873e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
18741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
18756b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
18766b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
1877127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1878b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1879e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
18807b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
18817b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1882b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1883b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
18841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
188516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
18861637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1887a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1888a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
1889a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
1890a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
18911637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
18921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1893127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1894b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1895b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
18965bbcdbf36f8cf79d99703ef20848c55960065e43Sebastian Redl    Template->addSpecialization(Info, InsertPos);
1897b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
18982c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
18992c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
19002c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
19012c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
1902b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1903b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
19042c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
19052c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
19062c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
1907b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
19081637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
19091637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1910af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1911af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1912af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1913af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1914af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1915af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1916af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
191721c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1918af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1919af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1920af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1921af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1922af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1923af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1924af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1925af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1926af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1927af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1928af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1929af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1930af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1931af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1932af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1933af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1934af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1935af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1936af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1937af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1938af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1939af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1940af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1941af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1942af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1943af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1944d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
19451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1946d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
19472db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
19481fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
19492db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
19502db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1951d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
19522db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
19532db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
19542db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
19552db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
19562db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
19572db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
19581fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
19591fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
19601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
19610a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
19620a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
19632db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
19642db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
19650a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
19662db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
19670a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
19680a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
19690a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
19700a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
19710a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
19720a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
19732db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
19740a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
19750a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
19760a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
19770a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
19780a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
19792db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
19801fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
19811fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
19820a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
19830a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
19840a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
19850a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
19860a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
19870a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
19880a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
19890a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
19900a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
19910a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
19920a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
19930a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
19949f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
1995da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
19969f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
19979f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19989f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
19999f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
20009f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
20019f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
200206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
20039f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
20049f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
20059f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
20069f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
20079f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
20089f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
20099f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
201006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
20119f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
20129f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
20139f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
20149f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
20159f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
20169f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
2017a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange FunctionDecl::getSourceRange() const {
2018a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getOuterLocStart(), EndRangeLoc);
2019a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
2020a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
20218a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
20227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
20237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
20247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20254ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadFieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
2026ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                             SourceLocation StartLoc, SourceLocation IdLoc,
2027ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                             IdentifierInfo *Id, QualType T,
20287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
2029ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
2030ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                           BW, Mutable);
20317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
20347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
20357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
20367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
20387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
20397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
20417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2043ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCallunsigned FieldDecl::getFieldIndex() const {
2044ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  if (CachedFieldIndex) return CachedFieldIndex - 1;
2045ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2046ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  unsigned index = 0;
2047ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  RecordDecl::field_iterator
2048ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    i = getParent()->field_begin(), e = getParent()->field_end();
2049ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  while (true) {
2050ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    assert(i != e && "failed to find field in parent!");
2051ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (*i == this)
2052ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      break;
2053ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2054ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    ++i;
2055ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    ++index;
2056ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  }
2057ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2058ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  CachedFieldIndex = index + 1;
2059ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  return index;
2060ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall}
2061ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2062f2cf562cec11dec926c0a29a71769a27fed02962Abramo BagnaraSourceRange FieldDecl::getSourceRange() const {
2063a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (isBitField())
2064a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    return SourceRange(getInnerLocStart(), BitWidth->getLocEnd());
2065a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return DeclaratorDecl::getSourceRange();
2066f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara}
2067f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara
20687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
2069bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
20704b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
20714b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
20721693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
20731693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
20741693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
20751693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
2076f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
2077f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
20781693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
2079f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
2080f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
2081b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
20828e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
2083b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
2084b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
208560e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
208660e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
208760e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
2088f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
2089381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  ClearLinkageCache();
209060e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
209160e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
20920b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
2093ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
209486ff308724171494395a840fd2efbe25e62f352eJohn McCall
209586ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
209686ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
209786ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
209886ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
20992243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
21002243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
210186ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
21020b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
21030b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
21040b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
21055cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
21065cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
21075cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
21085cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
21090b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
2110ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
2111565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
2112565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
2113565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
21140b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
21150b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
2116952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
21178e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
21188e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
2119220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2120220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
21211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
21238e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
21248e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
21258e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
21261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21278e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
21284b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
21294b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
2130c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2131c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
2132b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
2133b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
2134b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2135b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
2136c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
2137b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2138b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
2139b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2140b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
21417f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      if (getExtInfo()->NumTemplParamLists == 0) {
21427f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getASTContext().Deallocate(getExtInfo());
21437f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        TypedefDeclOrQualifier = (TypedefDecl*) 0;
21447f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      }
21457f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      else
21467f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getExtInfo()->QualifierLoc = QualifierLoc;
2147b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
2148b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2149b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
2150b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
21517f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnaravoid TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
21527f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                            unsigned NumTPLists,
21537f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                            TemplateParameterList **TPLists) {
21547f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  assert(NumTPLists > 0);
21557f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Make sure the extended decl info is allocated.
21567f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  if (!hasExtInfo())
21577f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Allocate external info struct.
21587f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
21597f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Set the template parameter lists info.
21607f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
21617f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara}
21627f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara
21634b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
21647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
21657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
21667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2167ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2168ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                           SourceLocation StartLoc, SourceLocation IdLoc,
2169ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                           IdentifierInfo *Id,
2170a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           EnumDecl *PrevDecl, bool IsScoped,
2171a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           bool IsScopedUsingClassTag, bool IsFixed) {
2172ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
2173a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    IsScoped, IsScopedUsingClassTag, IsFixed);
21747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
21757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
21767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2178b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
2179ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2180a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                          false, false, false);
2181b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2182b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2183838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
21841b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
21851b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
21861b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
21877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
21881274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
21891274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
21907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
21911b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
21921b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
21937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
21947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
21978a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
21988a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
21995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2200ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2201ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                       SourceLocation StartLoc, SourceLocation IdLoc,
2202ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                       IdentifierInfo *Id, RecordDecl *PrevDecl)
2203ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
22046359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
2205bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
2206082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
2207eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
22086359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
22096359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
22106359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
22114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
2212ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                               SourceLocation StartLoc, SourceLocation IdLoc,
2213ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                               IdentifierInfo *Id, RecordDecl* PrevDecl) {
2214ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2215ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                     PrevDecl);
22164b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
22174b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
22186359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
22196359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
22204ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
2221ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2222ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            SourceLocation(), 0, 0);
2223b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2224b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2225c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
22261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
2227c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2228c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
2229c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
2230eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
2231eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2232eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
2233eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2234eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
2235eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2236eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2237da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// completeDefinition - Notes that the definition of this type is now
2238da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// complete.
2239da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid RecordDecl::completeDefinition() {
2240da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  assert(!isDefinition() && "Cannot redefine record!");
2241da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  TagDecl::completeDefinition();
2242da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
2243da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
2244eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
2245eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
2246eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
2247eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2248eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
2249eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
2250eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2251eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<Decl*, 64> Decls;
2252eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2253eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2254eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2255eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
2256eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
2257eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
2258eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
2259eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
2260eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2261eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = true;
2262eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2263eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
2264eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2265eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2266eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2267eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2268eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
226956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
227056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
227156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
227256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
2273838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
2274e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
2275e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
22761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2277e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
2278e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
2279e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
2280838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
2281e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2282e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2283e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
2284e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
2285e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
22866b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallvoid BlockDecl::setCaptures(ASTContext &Context,
22876b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *begin,
22886b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *end,
22896b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            bool capturesCXXThis) {
2290469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  CapturesCXXThis = capturesCXXThis;
2291469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
2292469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  if (begin == end) {
22936b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    NumCaptures = 0;
22946b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    Captures = 0;
2295469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    return;
2296469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  }
2297469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
22986b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  NumCaptures = end - begin;
22996b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
23006b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // Avoid new Capture[] because we don't want to provide a default
23016b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // constructor.
23026b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  size_t allocationSize = NumCaptures * sizeof(Capture);
23036b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
23046b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  memcpy(buffer, begin, allocationSize);
23056b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  Captures = static_cast<Capture*>(buffer);
2306e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
23077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23082fcbceff97e065cff499e6cc563ca25c762bf547Douglas GregorSourceRange BlockDecl::getSourceRange() const {
23092fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor  return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
23102fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor}
23117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
23137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
23147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
23157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
23177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
23187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2320ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
23216784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II) {
23226784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
23236784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara}
23246784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara
23256784304db526cde59046d613c4175ce2caf93e44Abramo BagnaraLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
23266784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II,
23276784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation GnuLabelL) {
23286784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
23296784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
2330ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner}
2331ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
2332ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
23337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2334acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                     SourceLocation StartLoc,
2335acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                     SourceLocation IdLoc, IdentifierInfo *Id) {
2336acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara  return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id);
23377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
233906c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
234006c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
234106c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
234206c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
234306c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
23447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2345ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             SourceLocation IdLoc,
2346ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             IdentifierInfo *Id,
2347ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             QualType Type) {
2348ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
23497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
2352ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   SourceLocation StartLoc,
23532577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
23542577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
2355ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StorageClass SC, StorageClass SCAsWritten,
23568f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool isInlineSpecified,
23578f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool hasWrittenPrototype) {
2358ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2359ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           T, TInfo, SC, SCAsWritten,
2360ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           isInlineSpecified);
23617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
23627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
23637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
23667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
23677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
23707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
23717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
23727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
23737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
23747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2376d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl *
2377d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2378d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          IdentifierInfo *Id, QualType T, NamedDecl **CH,
2379d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          unsigned CHS) {
238087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
238187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
238287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
23838e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
23848e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
23858e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
23868e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
23878e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
23888e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
23898e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
23907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2391344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 SourceLocation StartLoc, SourceLocation IdLoc,
2392344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2393344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara  return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
23947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2396a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange TypedefDecl::getSourceRange() const {
2397a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  SourceLocation RangeEnd = getLocation();
2398a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2399a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    if (typeIsPostfix(TInfo->getType()))
2400a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2401a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
2402a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getLocStart(), RangeEnd);
2403a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
2404a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
24057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
240621e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           StringLiteral *Str,
240721e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation AsmLoc,
240821e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation RParenLoc) {
240921e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara  return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
24107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
2411