Decl.cpp revision d98114647e16796a976b04af79975b4f0eacf22b
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"
28f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "llvm/Support/ErrorHandling.h"
2927f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
32d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner//===----------------------------------------------------------------------===//
334afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// NamedDecl Implementation
345239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
355239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis
36e7bc9722c807030409178d4af8ce8d1260bbd821John McCallstatic const VisibilityAttr *GetExplicitVisibility(const Decl *D) {
37e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  // If the decl is redeclarable, make sure we use the explicit
38e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  // visibility attribute from the most recent declaration.
39e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  //
40e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  // Note that this isn't necessary for tags, which can't have their
41e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  // visibility adjusted.
42e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  if (isa<VarDecl>(D)) {
43e7bc9722c807030409178d4af8ce8d1260bbd821John McCall    return cast<VarDecl>(D)->getMostRecentDeclaration()
44e7bc9722c807030409178d4af8ce8d1260bbd821John McCall      ->getAttr<VisibilityAttr>();
45e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  } else if (isa<FunctionDecl>(D)) {
46e7bc9722c807030409178d4af8ce8d1260bbd821John McCall    return cast<FunctionDecl>(D)->getMostRecentDeclaration()
47e7bc9722c807030409178d4af8ce8d1260bbd821John McCall      ->getAttr<VisibilityAttr>();
48e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  } else {
49e7bc9722c807030409178d4af8ce8d1260bbd821John McCall    return D->getAttr<VisibilityAttr>();
50e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  }
51e7bc9722c807030409178d4af8ce8d1260bbd821John McCall}
52e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
531fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
541fb0caaa7bef765b85972274e3b434af2572c141John McCall  switch (A->getVisibility()) {
551fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Default:
561fb0caaa7bef765b85972274e3b434af2572c141John McCall    return DefaultVisibility;
571fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Hidden:
581fb0caaa7bef765b85972274e3b434af2572c141John McCall    return HiddenVisibility;
591fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Protected:
601fb0caaa7bef765b85972274e3b434af2572c141John McCall    return ProtectedVisibility;
611fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
621fb0caaa7bef765b85972274e3b434af2572c141John McCall  return DefaultVisibility;
631fb0caaa7bef765b85972274e3b434af2572c141John McCall}
641fb0caaa7bef765b85972274e3b434af2572c141John McCall
65af14603ca61757cf4361b583b45639a04c57e651John McCalltypedef NamedDecl::LinkageInfo LinkageInfo;
661fb0caaa7bef765b85972274e3b434af2572c141John McCalltypedef std::pair<Linkage,Visibility> LVPair;
67af14603ca61757cf4361b583b45639a04c57e651John McCall
681fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair merge(LVPair L, LVPair R) {
691fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LVPair(minLinkage(L.first, R.first),
701fb0caaa7bef765b85972274e3b434af2572c141John McCall                minVisibility(L.second, R.second));
711fb0caaa7bef765b85972274e3b434af2572c141John McCall}
721fb0caaa7bef765b85972274e3b434af2572c141John McCall
73af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair merge(LVPair L, LinkageInfo R) {
74af14603ca61757cf4361b583b45639a04c57e651John McCall  return LVPair(minLinkage(L.first, R.linkage()),
75af14603ca61757cf4361b583b45639a04c57e651John McCall                minVisibility(L.second, R.visibility()));
76af14603ca61757cf4361b583b45639a04c57e651John McCall}
77af14603ca61757cf4361b583b45639a04c57e651John McCall
78752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramernamespace {
793698748400478880d2a146ef9eaa111cd0e60522John McCall/// Flags controlling the computation of linkage and visibility.
803698748400478880d2a146ef9eaa111cd0e60522John McCallstruct LVFlags {
813698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderGlobalVisibility;
823698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderVisibilityAttributes;
833698748400478880d2a146ef9eaa111cd0e60522John McCall
843698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags() : ConsiderGlobalVisibility(true),
853698748400478880d2a146ef9eaa111cd0e60522John McCall              ConsiderVisibilityAttributes(true) {
863698748400478880d2a146ef9eaa111cd0e60522John McCall  }
873698748400478880d2a146ef9eaa111cd0e60522John McCall
883698748400478880d2a146ef9eaa111cd0e60522John McCall  /// Returns a set of flags, otherwise based on these, which ignores
893698748400478880d2a146ef9eaa111cd0e60522John McCall  /// off all sources of visibility except template arguments.
903698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags onlyTemplateVisibility() const {
913698748400478880d2a146ef9eaa111cd0e60522John McCall    LVFlags F = *this;
923698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderGlobalVisibility = false;
933698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderVisibilityAttributes = false;
943698748400478880d2a146ef9eaa111cd0e60522John McCall    return F;
953698748400478880d2a146ef9eaa111cd0e60522John McCall  }
963698748400478880d2a146ef9eaa111cd0e60522John McCall};
97752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramer} // end anonymous namespace
983698748400478880d2a146ef9eaa111cd0e60522John McCall
990b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
1000b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
1011fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair
1021fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) {
1031fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1040b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1050b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
1060b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
1070b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
1080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
1091fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
1100b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
1110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
1120b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1130b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
1140b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
115af14603ca61757cf4361b583b45639a04c57e651John McCall      LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
1160b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1170b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1180b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1191fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1210b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1220b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
1230b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
1241fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
1251fb0caaa7bef765b85972274e3b434af2572c141John McCall                                           unsigned NumArgs) {
1261fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1270b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1290b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
1300b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
1310b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
1320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
1330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
1361fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
1401fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
1411fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
1421fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
1431fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1441fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV = merge(LV, ND->getLinkageAndVisibility());
1451fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
146af14603ca61757cf4361b583b45639a04c57e651John McCall          LV = merge(LV, VD->getLinkageAndVisibility());
1471fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
1511fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
1521fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, Template->getLinkageAndVisibility());
1530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
1561fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
1571fb0caaa7bef765b85972274e3b434af2572c141John McCall                                                  Args[I].pack_size()));
1580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1621fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
165af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair
166af14603ca61757cf4361b583b45639a04c57e651John McCallgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
167910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  return getLVForTemplateArgumentList(TArgs.data(), TArgs.size());
1683cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
1693cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
1700df9587ab011c12968fcbe3518666b2117afe350John McCall/// getLVForDecl - Get the cached linkage and visibility for the given
1710df9587ab011c12968fcbe3518666b2117afe350John McCall/// declaration.
1723698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
1733698748400478880d2a146ef9eaa111cd0e60522John McCall
1743698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
1757a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
176d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
177d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
178d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
179d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
180d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
181d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
182d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
183d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
184d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
185d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
186d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
187d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
188af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
189d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
190d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
191d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
192d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
193d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
194d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
195e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
196d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
197d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
198d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
199d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
200d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
201d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
2020b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
203d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
204d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
205d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
206af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::internal();
207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
2090b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
2100b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
2110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
220d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
221af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo(InternalLinkage, DefaultVisibility, false);
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
224d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
225af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
226d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
227d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2281fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (D->isInAnonymousNamespace())
229af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
230e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2311fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2321fb0caaa7bef765b85972274e3b434af2572c141John McCall
2331fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2341fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2351fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2361fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
237af14603ca61757cf4361b583b45639a04c57e651John McCall  LinkageInfo LV;
238af14603ca61757cf4361b583b45639a04c57e651John McCall
2393698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
2403698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
2413698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.setVisibility(GetVisibilityFromAttr(VA), true);
2423698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
2433698748400478880d2a146ef9eaa111cd0e60522John McCall    }
244af14603ca61757cf4361b583b45639a04c57e651John McCall  }
2451fb0caaa7bef765b85972274e3b434af2572c141John McCall
246d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
2471fb0caaa7bef765b85972274e3b434af2572c141John McCall
248d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
249d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
250d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
251d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
252d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
253110e8e56af30363072c140285961592b0107f789John McCall    // GCC applies the following optimization to variables and static
254110e8e56af30363072c140285961592b0107f789John McCall    // data members, but not to functions:
255110e8e56af30363072c140285961592b0107f789John McCall    //
2561fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
2571fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
2581fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
2591fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
2601fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
2611fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
2621fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
2631fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
2641fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
2651fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
2661fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
2671fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
2681fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
269ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
270ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
271ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
2721fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
2731fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
274ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
2751fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
2761fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
277af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
278af14603ca61757cf4361b583b45639a04c57e651John McCall      if (!LV.visibilityExplicit())
279af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(TypeLV.second);
280110e8e56af30363072c140285961592b0107f789John McCall    }
281110e8e56af30363072c140285961592b0107f789John McCall
28235cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Var->getStorageClass() == SC_PrivateExtern)
28335cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
28435cebc3eea898637057b10b5cf7dd08b1d788980John McCall
285d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
286d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
287d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
2881fb0caaa7bef765b85972274e3b434af2572c141John McCall
289d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
290d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
291d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
292d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
293d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
294d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
295d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
296d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
297d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
298d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
299af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo PrevLV = PrevVar->getLinkageAndVisibility();
300af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
301af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
302d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
303d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
304d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
305d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
3061fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
30767fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
30867fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
30967fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
31067fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
3111fb0caaa7bef765b85972274e3b434af2572c141John McCall
31235cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Function->getStorageClass() == SC_PrivateExtern)
31335cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
31435cebc3eea898637057b10b5cf7dd08b1d788980John McCall
315d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
316d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
317d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
318d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
319d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
320d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
321d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
322d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
323d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
324d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
325d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
326d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
327d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
328d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
329d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
330d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
331d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
332d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
333d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
334af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo PrevLV = PrevFunc->getLinkageAndVisibility();
335af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
336af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
337d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
338d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
339d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
3410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
3423698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(SpecInfo->getTemplate(),
3433698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
3440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
345af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateArgumentList(TemplateArgs));
3460b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
3470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
348d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
349d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
350d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
351d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
3541fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
3551fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
3561fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
357af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
3581fb0caaa7bef765b85972274e3b434af2572c141John McCall
3591fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
3601fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
3611fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (const ClassTemplateSpecializationDecl *Spec
3621fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
3633698748400478880d2a146ef9eaa111cd0e60522John McCall      // From the template.
3643698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
3653698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
3661fb0caaa7bef765b85972274e3b434af2572c141John McCall
3671fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The arguments at which the template was instantiated.
3681fb0caaa7bef765b85972274e3b434af2572c141John McCall      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
369af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateArgumentList(TemplateArgs));
3700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
371d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
372ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
3733698748400478880d2a146ef9eaa111cd0e60522John McCall    if (F.ConsiderGlobalVisibility)
3743698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility =
375ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
376ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
3771fb0caaa7bef765b85972274e3b434af2572c141John McCall
378d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
3791fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
380af14603ca61757cf4361b583b45639a04c57e651John McCall    LinkageInfo EnumLV =
3811fb0caaa7bef765b85972274e3b434af2572c141John McCall      cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
382af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!isExternalLinkage(EnumLV.linkage()))
383af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
384af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(EnumLV);
385d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
386d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
387d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
3881fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
389af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
3900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
391d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
392d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
3931fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
3941fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
3951fb0caaa7bef765b85972274e3b434af2572c141John McCall
3961fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
3971fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
3981fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
3991fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
4001fb0caaa7bef765b85972274e3b434af2572c141John McCall
4011fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
4021fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
403af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4041fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4051fb0caaa7bef765b85972274e3b434af2572c141John McCall
4061fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
4071fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
408af14603ca61757cf4361b583b45639a04c57e651John McCall  if (LV.linkage() != ExternalLinkage)
409af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo(LV.linkage(), DefaultVisibility, false);
4101fb0caaa7bef765b85972274e3b434af2572c141John McCall
4111fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
4121fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
4133698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility)
414af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
415d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4161fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
417d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
418d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4193698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
4201fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
4211fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
4221fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
4231fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
4243cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
4253cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
4261fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
4273cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
4283cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall         (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
429af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4303cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4313698748400478880d2a146ef9eaa111cd0e60522John McCall  LinkageInfo LV;
4323698748400478880d2a146ef9eaa111cd0e60522John McCall
4333698748400478880d2a146ef9eaa111cd0e60522John McCall  // The flags we're going to use to compute the class's visibility.
4343698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags ClassF = F;
4353698748400478880d2a146ef9eaa111cd0e60522John McCall
4363698748400478880d2a146ef9eaa111cd0e60522John McCall  // If we have an explicit visibility attribute, merge that in.
4373698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
4383698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
4393698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
4403698748400478880d2a146ef9eaa111cd0e60522John McCall
4413698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore global visibility later, but not this attribute.
4423698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
4433698748400478880d2a146ef9eaa111cd0e60522John McCall
4443698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore both global visibility and attributes when computing our
4453698748400478880d2a146ef9eaa111cd0e60522John McCall      // parent's visibility.
4463698748400478880d2a146ef9eaa111cd0e60522John McCall      ClassF = F.onlyTemplateVisibility();
4473698748400478880d2a146ef9eaa111cd0e60522John McCall    }
4483698748400478880d2a146ef9eaa111cd0e60522John McCall  }
449af14603ca61757cf4361b583b45639a04c57e651John McCall
450af14603ca61757cf4361b583b45639a04c57e651John McCall  // Class members only have linkage if their class has external
4513698748400478880d2a146ef9eaa111cd0e60522John McCall  // linkage.
4523698748400478880d2a146ef9eaa111cd0e60522John McCall  LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
4533698748400478880d2a146ef9eaa111cd0e60522John McCall  if (!isExternalLinkage(LV.linkage()))
454af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4553cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4563cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
4573698748400478880d2a146ef9eaa111cd0e60522John McCall  if (LV.linkage() == UniqueExternalLinkage)
458af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
4591fb0caaa7bef765b85972274e3b434af2572c141John McCall
4603cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
461110e8e56af30363072c140285961592b0107f789John McCall    TemplateSpecializationKind TSK = TSK_Undeclared;
462110e8e56af30363072c140285961592b0107f789John McCall
4631fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
4641fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
4651fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (FunctionTemplateSpecializationInfo *Spec
4663cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
467af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments));
468af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateParameterList(
4691fb0caaa7bef765b85972274e3b434af2572c141John McCall                              Spec->getTemplate()->getTemplateParameters()));
470110e8e56af30363072c140285961592b0107f789John McCall
471110e8e56af30363072c140285961592b0107f789John McCall      TSK = Spec->getTemplateSpecializationKind();
472110e8e56af30363072c140285961592b0107f789John McCall    } else if (MemberSpecializationInfo *MSI =
473110e8e56af30363072c140285961592b0107f789John McCall                 MD->getMemberSpecializationInfo()) {
474110e8e56af30363072c140285961592b0107f789John McCall      TSK = MSI->getTemplateSpecializationKind();
4753cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
4763cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
477110e8e56af30363072c140285961592b0107f789John McCall    // If we're paying attention to global visibility, apply
478110e8e56af30363072c140285961592b0107f789John McCall    // -finline-visibility-hidden if this is an inline method.
479110e8e56af30363072c140285961592b0107f789John McCall    //
480af14603ca61757cf4361b583b45639a04c57e651John McCall    // Note that ConsiderGlobalVisibility doesn't yet have information
481af14603ca61757cf4361b583b45639a04c57e651John McCall    // about whether containing classes have visibility attributes,
482af14603ca61757cf4361b583b45639a04c57e651John McCall    // and that's intentional.
483af14603ca61757cf4361b583b45639a04c57e651John McCall    if (TSK != TSK_ExplicitInstantiationDeclaration &&
4843698748400478880d2a146ef9eaa111cd0e60522John McCall        F.ConsiderGlobalVisibility &&
48566cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
48666cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // InlineVisibilityHidden only applies to definitions, and
48766cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // isInlined() only gives meaningful answers on definitions
48866cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // anyway.
48966cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      const FunctionDecl *Def = 0;
49066cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      if (MD->hasBody(Def) && Def->isInlined())
49166cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        LV.setVisibility(HiddenVisibility);
49266cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall    }
4931fb0caaa7bef765b85972274e3b434af2572c141John McCall
494110e8e56af30363072c140285961592b0107f789John McCall    // Note that in contrast to basically every other situation, we
495110e8e56af30363072c140285961592b0107f789John McCall    // *do* apply -fvisibility to method declarations.
496110e8e56af30363072c140285961592b0107f789John McCall
497110e8e56af30363072c140285961592b0107f789John McCall  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
498110e8e56af30363072c140285961592b0107f789John McCall    if (const ClassTemplateSpecializationDecl *Spec
499110e8e56af30363072c140285961592b0107f789John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
500110e8e56af30363072c140285961592b0107f789John McCall      // Merge template argument/parameter information for member
501110e8e56af30363072c140285961592b0107f789John McCall      // class template specializations.
502af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs()));
503af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateParameterList(
5041fb0caaa7bef765b85972274e3b434af2572c141John McCall                    Spec->getSpecializedTemplate()->getTemplateParameters()));
505110e8e56af30363072c140285961592b0107f789John McCall    }
506110e8e56af30363072c140285961592b0107f789John McCall
507110e8e56af30363072c140285961592b0107f789John McCall  // Static data members.
508110e8e56af30363072c140285961592b0107f789John McCall  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
509ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // Modify the variable's linkage by its type, but ignore the
510ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // type's visibility unless it's a definition.
511ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
512ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (TypeLV.first != ExternalLinkage)
513af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeLinkage(UniqueExternalLinkage);
514af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!LV.visibilityExplicit())
515af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeVisibility(TypeLV.second);
516110e8e56af30363072c140285961592b0107f789John McCall  }
517110e8e56af30363072c140285961592b0107f789John McCall
5183698748400478880d2a146ef9eaa111cd0e60522John McCall  F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
519110e8e56af30363072c140285961592b0107f789John McCall
520110e8e56af30363072c140285961592b0107f789John McCall  // Apply -fvisibility if desired.
5213698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
522af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
5233cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
5243cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5251fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
5263cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
5273cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
528af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const {
5293698748400478880d2a146ef9eaa111cd0e60522John McCall  return getLVForDecl(this, LVFlags());
5300df9587ab011c12968fcbe3518666b2117afe350John McCall}
531becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
5323698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
533becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
534becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
5350df9587ab011c12968fcbe3518666b2117afe350John McCall  switch (D->getKind()) {
536becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
537becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
5381fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
5391fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
540becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
541becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
542becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
543becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
544becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
545becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
546becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
547becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
548becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
549becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
550af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::external();
551becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
552becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
553d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
5540df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->getRedeclContext()->isFileContext())
5553698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForNamespaceScopeDecl(D, Flags);
556d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
557d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
558d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
559d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
560d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
561d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
562d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
563d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
5640df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->isRecord())
5653698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForClassMember(D, Flags);
566d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
567d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
568d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
569d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
570d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
571d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
572d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
573d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
574d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
575d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
576d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
577d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
5780df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
5790df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
5800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Function->isInAnonymousNamespace())
581af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
5821fb0caaa7bef765b85972274e3b434af2572c141John McCall
583af14603ca61757cf4361b583b45639a04c57e651John McCall      LinkageInfo LV;
584e7bc9722c807030409178d4af8ce8d1260bbd821John McCall      if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
585af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.setVisibility(GetVisibilityFromAttr(VA));
5861fb0caaa7bef765b85972274e3b434af2572c141John McCall
5871fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
588af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
589af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
590af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
5911fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
5920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
5931fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
594d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
595d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
5960df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const VarDecl *Var = dyn_cast<VarDecl>(D))
597d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
598d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
5990b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Var->isInAnonymousNamespace())
600af14603ca61757cf4361b583b45639a04c57e651John McCall          return LinkageInfo::uniqueExternal();
6011fb0caaa7bef765b85972274e3b434af2572c141John McCall
602af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo LV;
6031fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
604af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(HiddenVisibility);
605e7bc9722c807030409178d4af8ce8d1260bbd821John McCall        else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
606af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(GetVisibilityFromAttr(VA));
6070b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
6081fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
609af14603ca61757cf4361b583b45639a04c57e651John McCall          LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
610af14603ca61757cf4361b583b45639a04c57e651John McCall          if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
611af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.mergeVisibility(PrevLV);
6121fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
6131fb0caaa7bef765b85972274e3b434af2572c141John McCall
6141fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
615d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
616d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
617d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
618d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
619d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
620af14603ca61757cf4361b583b45639a04c57e651John McCall  return LinkageInfo::none();
6211fb0caaa7bef765b85972274e3b434af2572c141John McCall}
622d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
62347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
6243a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
6253a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
6263a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
6273a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
62847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
62947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
63047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
63147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
63247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
63368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
63468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
63568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
63668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
63768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
63868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
63968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
64068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
64168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
64268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
64368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
64468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
64568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
64668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
6471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
64868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
649f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
650f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
651f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
652910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.data(),
653910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.size(),
6543a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
65568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
65668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
6576be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
65868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
6596be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
66068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
66168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
66268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
66368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
66468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
66568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
66668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
6673521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
6683521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
6693521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
6703521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
67168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
6723521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
6733521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
6743521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
6753521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
67668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
6773521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
6783521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
67968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
6803521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
6813521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
6823521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
6833521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
68468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
68568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
6863521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
6873521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
68868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
68968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
69068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
69168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
69268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
69347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
69447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
6958472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
69668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
6978472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
69868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
69947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
70068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
70147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
70247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
7034afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
7046ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
7056ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
7062a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
7072a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
7082a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
7092a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
7102a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
7112a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
7121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7136ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
7146ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
7156ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
7166ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
717e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
718e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
719e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
720e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
721e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
722e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
723e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
7241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7250de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
7260de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
7270de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
7281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
729f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
730f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
731f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
7329488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
7339488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
7349488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
7359488ea120e093068021f944176c3d610dd540914John McCall
736c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis  if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
737c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis    return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
738c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis           cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
739c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis
7406ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
7416ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
7426ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
7436ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
7446ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
7456ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
746d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
747d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
748d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
7494afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
750e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
751e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
752e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
7539488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
754e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
755e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
756e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
757e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
758e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
759e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
760e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
761e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
762e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
763161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
764161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
765161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
766161755a09898c95d21bfff33707da9ca41cd53c5John McCall
767161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
768161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
769161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
770161755a09898c95d21bfff33707da9ca41cd53c5John McCall
77187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
772161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
773161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
774161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
775161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
776161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
777161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
778161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
779161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
780161755a09898c95d21bfff33707da9ca41cd53c5John McCall
7815239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
782a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
783a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
784a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
7851693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
7861693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
7871693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
7881693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
7891693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
7901693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
7911693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
7921693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
793a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
7944e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
7954e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
796a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
797a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
798a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
799b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
800b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                      SourceRange QualifierRange) {
801b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
802b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
803b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
804b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
805b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
806b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
807b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
808b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
809b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
810b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
811b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
812b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
813b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
814b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
815b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
816b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
817b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
818b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
819b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
820b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
821b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
822b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
823b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
824b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
825b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
826b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
827b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
828b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
8291693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
8301693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
8311693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
8321693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
8339b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
834c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
835c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
8369b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
8379b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
8389b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
8399b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || NNS) &&
8409b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Nonempty array of template parameters with no qualifier!");
8419b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
8429b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
8439b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
844c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
8459b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
8469b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
8479b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
8489b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
8499b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
850c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
8519b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
8529b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
8539b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
8549b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
8559b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
8569b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
857a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
85899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
85999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
86099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
8617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
8627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
863d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_None:          break;
864d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Auto:          return "auto"; break;
865d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Extern:        return "extern"; break;
866d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_PrivateExtern: return "__private_extern__"; break;
867d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Register:      return "register"; break;
868d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Static:        return "static"; break;
8697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
8707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
8727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
8737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8754afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
876a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
87716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
87816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
87999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
88099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
8811693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation VarDecl::getInnerLocStart() const {
88233e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
88333e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (Start.isInvalid())
88433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    Start = getLocation();
8851693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return Start;
8861693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
8871693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
8881693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
88955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
8901693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
8911693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), getLocation());
89255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
89355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
8947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
8957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
8967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
8977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
898d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
8997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
9007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
9027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl       DC = DC->getParent()) {
9037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
9047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
905d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
9067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
9087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
9097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (DC->isFunctionOrMethod())
9117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      return false;
9127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
9157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
9187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
9197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
921e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
922e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
923e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
924e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
925e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
926e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
927e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
928e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
929e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
930e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
931e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
932e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
933e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
934e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
935e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
936e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
937e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
938e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
939e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
940e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
941e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
942e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
943e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
944e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
945e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
946e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
947e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
948e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
9492bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
950d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
951d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
9522bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
9532bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
9542bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
9552bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
9562bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
9572bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
958e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
959e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
960e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
961e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
962e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
963e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
964e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
965e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
966e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
967e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
968e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
969e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
970e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
971e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
972e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
973e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
974e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
975e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
976f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
977e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
978e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
979e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
980e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
981e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
982e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
983e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
984e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
985e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
986e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
987e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
988e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
989e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
990e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
991e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
992e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
993e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
994e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
995e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
996e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
997e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
99831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
99931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
100031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
100131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
1002e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
1003e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1004e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
100531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
100631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
100731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
100831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
1009e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1010e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1011110e8e56af30363072c140285961592b0107f789John McCallVarDecl::DefinitionKind VarDecl::hasDefinition() const {
1012110e8e56af30363072c140285961592b0107f789John McCall  DefinitionKind Kind = DeclarationOnly;
1013110e8e56af30363072c140285961592b0107f789John McCall
1014110e8e56af30363072c140285961592b0107f789John McCall  const VarDecl *First = getFirstDeclaration();
1015110e8e56af30363072c140285961592b0107f789John McCall  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1016110e8e56af30363072c140285961592b0107f789John McCall       I != E; ++I)
1017110e8e56af30363072c140285961592b0107f789John McCall    Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1018110e8e56af30363072c140285961592b0107f789John McCall
1019110e8e56af30363072c140285961592b0107f789John McCall  return Kind;
1020110e8e56af30363072c140285961592b0107f789John McCall}
1021110e8e56af30363072c140285961592b0107f789John McCall
102231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
10237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
10247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
10257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
10267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
102831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
10297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
10307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
10317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
10327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10341028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
10351028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Decl::isOutOfLine())
10361028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
10378761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
10388761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
10398761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
10408761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
10411028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
10421028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
10431028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
10441028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
10451028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
10461028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
10471028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
10481028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
10491028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
10500d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
10510d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
10520d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
10530d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
10540d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
10550d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
10560d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
10570d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
10580d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
10590d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
10600d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
10610d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
10620d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
1063838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
10647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
10657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
1066838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
10677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
10687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
10707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10721028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1073b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1074251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1075251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1076251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1077251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1078251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1079663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1080e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1081251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1082251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1083251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1084251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1085251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
10861028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1087b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1088b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1089b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
10900a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
10910a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1092b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1093251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1094251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
10950a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
10960a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
10970a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
10980a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
10997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
11007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
11017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
11027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
11037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1104275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
11057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
11067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
11077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
110816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
110916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
111016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
111116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1112275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1113275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
11147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
11157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
11167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
11177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
11187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
11207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
11217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
11227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
11247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
11277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const CXXExprWithTemporaries *E =
11287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        dyn_cast<CXXExprWithTemporaries>(getInit()))
11297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
1130275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
1131c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
1132275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1133275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
11347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
11357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
11367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
11377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
11397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
11407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
11437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
11447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
11457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
11477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
11487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1150fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1151fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
115299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
11538a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
11548a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
11558a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1156136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCallvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1157136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        const PrintingPolicy &Policy,
1158136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        bool Qualified) const {
1159136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1160136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1161136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  if (TemplateArgs)
1162136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall    S += TemplateSpecializationType::PrintTemplateArgumentList(
1163910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                         TemplateArgs->data(),
1164910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                         TemplateArgs->size(),
1165136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                                               Policy);
1166136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
1167136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall}
116827f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
11699498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
11709498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
11719498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
11729498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
11739498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
11749498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
117506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
117606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
117706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (I->Body) {
117806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
117906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
118006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
118106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
118206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
118306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
118406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
118506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
11866fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1187c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1188c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1189c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1190c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
1191f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1192f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1193f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1194f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
119755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
119855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
11991a5364e0fa0482d8d477d6f136d52e503bbe13f4Argyrios Kyrtzidis  if (B)
120055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
120155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
120255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
12032138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
12042138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
12052138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
12062138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
12072138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
12082138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
12092138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
121048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
121148a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
121207a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
12137a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    getDeclContext()->getRedeclContext()->isTranslationUnit() &&
121404495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
121504495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
121604495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
121748a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
121848a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
12196393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
12206393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
12216393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1222d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
12236393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
12256393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor       DC = DC->getParent()) {
12266393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
12276393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1228d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
122940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
12306393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
12316393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
12326393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
123345975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor
123445975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor    if (DC->isRecord())
123545975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor      break;
12366393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
12376393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
12380bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
12396393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
12406393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
12418499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
12428499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
12438499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
12448499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1245d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
12468499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
12478499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
12498499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
12508499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
12518499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
12528499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
12538499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
12548499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
12558499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
12568499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
12578499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
12588499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
12598499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
12608499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
12617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
12627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
12637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
12647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
12667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
12677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
12687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
12697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
12707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
12717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
12747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
12757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
12787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
12797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12813e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
12823e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
12833e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
12843e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
12853e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
12863e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
12873e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
12893e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
12907814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
12917814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
12923c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
12933c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
12943c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12953c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
12963c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
12973c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
12983c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12993c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
13003c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
13013c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
13023c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13039add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1304d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
13059add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
13069add31798f621f843233dbff8bba103fca64447bDouglas Gregor
13073c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
13083c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
13093c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
13103c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
13113c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
13123c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13133c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
13143c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
13153c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
13161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
13173c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
131840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
13193c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
13203c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13213c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
13223e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
13233e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
13243e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
13253e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
13261ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
13272dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner/// based on its FunctionType.  This is the length of the PararmInfo array
13281ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
13291ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1330183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
133172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1332d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
133372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
13341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13376b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
13386b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                             ParmVarDecl **NewParamInfo, unsigned NumParams) {
13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
13402dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
13411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
13446b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1345fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
134755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
134896888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
134996888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1350cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
135155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13558123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
13568123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
13578123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
13589e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner/// arguments (in C++).
13598123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
13608123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  unsigned NumRequiredArgs = getNumParams();
13618123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  while (NumRequiredArgs > 0
1362ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
13638123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
13648123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
13658123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
13668123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
13678123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
13687ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
136948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // FIXME: This is not enough. Consider:
137048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
137148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // inline void f();
137248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // void f() { }
137348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
137448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // f is inlined, but does not have inline specified.
137548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // To fix this we should add an 'inline' flag to FunctionDecl.
137648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isInlineSpecified())
13777d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
137848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
137948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
138048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
138148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
138248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
13837d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
13847d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
13857d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
13867d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
13877d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
13887d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
13897d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
13907d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
13917d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
13927d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
13937d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
13947d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
13957d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
13967d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
139706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
13987d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
139906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
14007d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
140106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
14027d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
14037d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
14047d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
14057ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
14067ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
14077d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
14081fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
14091fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
14101fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
14111fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
14121fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
14131fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
14141fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
14151fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
14161e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
14171fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
14181fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
14191fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
14201fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
14211fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
14221fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
14231fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
14241fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
14251fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
14267ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
14277d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
14281fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
14297d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
14301fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU inline semantics. Based on a number of examples, we came up with the
14311fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // following heuristic: if the "inline" keyword is present on a
14321fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration of the function but "extern" is not present on that
14331fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration, then the symbol is externally visible. Otherwise, the GNU
14341fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // "extern inline" semantics applies and the symbol is not externally
14351fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // visible.
14361fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
14371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
14381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
1439d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
14401fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
14411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    }
14421fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
14431fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU "extern inline" semantics; no externally visible symbol.
14449f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
14451fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
14461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
14471fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
14481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
14491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
14501fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
14511fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
14521fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
14531fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
14541fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
14551fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
14561fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
14571fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1458d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
14591fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
14601fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
14611fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
14621fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
14631fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
14641fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
14651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
14669f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
14679f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
14689f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
14691cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
14701cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
14711cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1472e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1473e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
14741cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
14751cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
14761cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
14771cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1478a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1479a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1480a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1481a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1482a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1483a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1484a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1485a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1486a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1487d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1488d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1489d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1490d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1491d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1492d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1493d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1494d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1495d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1496d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1497d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1498d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1499d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1500d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1501d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1502d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1503d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
15042db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1505b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
15062db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
15072db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
15082db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
15092db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
15102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1511b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1512b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1513b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1514b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
15152db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
15166b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
15176b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
15182db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
15192db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
15202db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
15212db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
15226b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
15232db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
15242db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
15252db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
15263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
15276cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
15286cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
15293b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
15303b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15313b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
15323b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
15333b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
15343b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
15353b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
15363b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
15383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
15393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
15413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
15423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
15433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
15443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
15463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
154706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
15483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
154906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
15503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
15523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
15533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
15543b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
155506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
15563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
15573b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15587ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
15593b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
15603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15613b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
15623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
15633b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
15643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
15653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
15663b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
15673b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
15683b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15693b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
15703b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
15713b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15723b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
15733b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
15743b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15753b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
15763b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
15773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
157816e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
15791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
158016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
158116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
15821fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
158316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
158416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
158516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
158616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
158716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
158816e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
15891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1590fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1591fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
159216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
159316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
159416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
159516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
159616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1597e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1598e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1599e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1600e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1601e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1602e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1603e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1604e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1605e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1606e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
16071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
16086b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
16096b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
1610127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1611b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1612e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
16137b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
16147b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1615b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1616b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
16171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
161816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
16191637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1620a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1621a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
1622a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
1623a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
16241637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
16251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1626127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1627b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1628b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1629b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1630b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
16312c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
16322c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
16332c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
16342c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
1635b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1636b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
16372c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
16382c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
16392c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
1640b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
16411637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
16421637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1643af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1644af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1645af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1646af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1647af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1648af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1649af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
165021c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1651af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1652af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1653af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1654af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1655af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1656af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1657af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1658af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1659af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1660af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1661af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1662af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1663af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1664af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1665af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1666af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1667af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1668af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1669af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1670af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1671af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1672af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1673af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1674af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1675af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1676af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1677d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
16781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1679d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
16802db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
16811fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
16822db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
16832db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1684d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
16852db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
16862db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
16872db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
16882db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
16892db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
16902db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
16911fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
16921fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
16931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
16940a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
16950a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
16962db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
16972db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
16980a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
16992db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
17000a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
17010a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
17020a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
17030a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
17040a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
17050a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
17062db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
17070a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
17080a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
17090a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
17100a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
17110a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
17122db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
17131fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
17141fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
17150a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
17160a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
17170a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
17180a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
17190a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
17200a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
17210a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
17220a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
17230a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
17240a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
17250a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
17260a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
17279f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
17289f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (Decl::isOutOfLine())
17299f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
17309f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
17319f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
17329f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
17339f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
17349f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
173506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
17369f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
17379f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
17389f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
17399f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
17409f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
17419f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
17429f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
174306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
17449f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
17459f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
17469f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
17479f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
17489f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
17499f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
17508a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
17517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
17527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
17537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
17557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             IdentifierInfo *Id, QualType T,
17567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
17577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
17587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
17617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
17627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
17637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
17657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
17667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
17687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1771bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
17724b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
17734b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
17741693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
17751693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
17761693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
17771693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1778f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1779f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
17801693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
1781f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1782f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1783b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
17848e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1785b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1786b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
178760e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
178860e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
178960e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
179060e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor    TypeForDecl->ClearLinkageCache();
179160e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
179260e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
17930b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
1794ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
179586ff308724171494395a840fd2efbe25e62f352eJohn McCall
179686ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
179786ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
179886ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
179986ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
18002243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
18012243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
180286ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
18030b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
18040b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
18050b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
18065cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
18075cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
18085cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
18095cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
18100b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
1811ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
1812565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
1813565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
1814565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
18150b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
18160b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
1817952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
18188e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
18198e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
1820220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1821220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
18221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
18248e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
18258e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
18268e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
18271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18288e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
18294b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
18304b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1831b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1832b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                               SourceRange QualifierRange) {
1833b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
1834b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
1835b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
1836b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1837b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1838b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
1839b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
1840b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1841b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
1842b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1843b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
1844b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
1845b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
1846b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
1847b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1848b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1849b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1850b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
18514b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
18527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
18537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
18547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
18567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
18571274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                           EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
18581274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
18591274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                    IsScoped, IsFixed);
18607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
18617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
18627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
18637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1864b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
18651274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
18661274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                          false, false);
1867b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1868b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1869838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
18701b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
18711b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
18721b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
18737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
18741274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
18751274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
18767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
18771b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
18781b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
18797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
18807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
18817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
18838a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
18848a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
18855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
188635bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
18878e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
18888e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
18898e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
18906359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
1891bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
1892082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
1893eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
18946359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
18956359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
18966359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
18976359792ca92e7ca2f416cb804c6604358174e994Ted KremenekRecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
18984b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
1899741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
19001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19018e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
19024b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
19034b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
19046359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
19056359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
1906b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisRecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1907b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1908b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis                            SourceLocation());
1909b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1910b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1911c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
19121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1913c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1914c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
1915c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
1916eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
1917eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1918eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
1919eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1920eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
1921eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
1922eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
192344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// completeDefinition - Notes that the definition of this type is now
192444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// complete.
1925838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid RecordDecl::completeDefinition() {
19265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!isDefinition() && "Cannot redefine record!");
19270b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  TagDecl::completeDefinition();
19285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1930bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCallValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1931bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  // Force the decl chain to come into existence properly.
1932bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  if (!getNextDeclInContext()) getParent()->decls_begin();
1933bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
1934bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(isAnonymousStructOrUnion());
1935bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1936bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->isRecordType());
1937bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1938bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  return D;
1939bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall}
1940bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
1941eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
1942eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
1943eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
1944eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1945eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
1946eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
1947eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1948eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<Decl*, 64> Decls;
1949eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1950eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
1951eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1952eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
1953eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
1954eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
1955eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
1956eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
1957eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1958eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = true;
1959eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1960eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
1961eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
1962eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1963eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1964eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
1965eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
196656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
196756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
196856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
196956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
1970838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
1971e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
1972e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
19731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1974e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
1975e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
1976e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
1977838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
1978e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1979e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1980e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
1981e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
1982e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1983e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffunsigned BlockDecl::getNumParams() const {
1984e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  return NumParams;
1985e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
19867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
19907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
19937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
19947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
19977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
19987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
19997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
200106c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
200206c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
200306c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
200406c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
200506c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
20067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
20077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    SourceLocation L, IdentifierInfo *Id, QualType T) {
20087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
20097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
20122577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
20132577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
201416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   StorageClass S, StorageClass SCAsWritten,
201516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   bool isInline, bool hasWrittenPrototype) {
20162577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
201716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                           S, SCAsWritten, isInline);
20187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
20197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
20207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
20237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
20247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
20277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
20287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
20297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
20307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
20317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2033d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl *
2034d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2035d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          IdentifierInfo *Id, QualType T, NamedDecl **CH,
2036d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          unsigned CHS) {
203787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
203887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
203987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
20408e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
20418e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
20428e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
20438e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
20448e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
20458e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
20468e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
20477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
20487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
20497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
20507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
20517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
20547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
20557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
20567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
20577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
2058