Decl.cpp revision 67fa6d5ea630c800c3c96e129129aba93d1487c2
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
651fb0caaa7bef765b85972274e3b434af2572c141John McCalltypedef std::pair<Linkage,Visibility> LVPair;
661fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair merge(LVPair L, LVPair R) {
671fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LVPair(minLinkage(L.first, R.first),
681fb0caaa7bef765b85972274e3b434af2572c141John McCall                minVisibility(L.second, R.second));
691fb0caaa7bef765b85972274e3b434af2572c141John McCall}
701fb0caaa7bef765b85972274e3b434af2572c141John McCall
710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
731fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair
741fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) {
751fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
811fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
871fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV =
881fb0caaa7bef765b85972274e3b434af2572c141John McCall        merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
921fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
960b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
971fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
981fb0caaa7bef765b85972274e3b434af2572c141John McCall                                           unsigned NumArgs) {
991fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1000b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1010b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1020b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
1030b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
1040b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
1050b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
1060b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1070b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
1091fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
1100b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1120b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
1131fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
1141fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
1151fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
1161fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1171fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV = merge(LV, ND->getLinkageAndVisibility());
1181fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1191fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV = merge(LV, VD->getType()->getLinkageAndVisibility());
1201fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1210b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1220b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1230b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
1241fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
1251fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, Template->getLinkageAndVisibility());
1260b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1270b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
1291fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
1301fb0caaa7bef765b85972274e3b434af2572c141John McCall                                                  Args[I].pack_size()));
1310b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1351fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1381fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
1391fb0caaa7bef765b85972274e3b434af2572c141John McCall  return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
1401fb0caaa7bef765b85972274e3b434af2572c141John McCall                                      TArgs.flat_size());
1413cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
1423cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
1431fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForNamespaceScopeDecl(const NamedDecl *D) {
1447a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
145d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
146d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
147d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
148d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
149d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
150d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
151d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
152d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
153d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
154d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
155d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
156d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
1571fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(InternalLinkage, DefaultVisibility);
158d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
159d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
160d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
161d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
162d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
163d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
164e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
165d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
166d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
167d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
168d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
169d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
170d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
1710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
172d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
173d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
174d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
1751fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LVPair(InternalLinkage, DefaultVisibility);
176d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
177d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
1780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
1790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
1800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
181d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
182d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
183d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
184d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
185d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
186d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
187d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
188d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
189d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
1901fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(InternalLinkage, DefaultVisibility);
191d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
192d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
193d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
1941fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(InternalLinkage, DefaultVisibility);
195d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
196d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
1971fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (D->isInAnonymousNamespace())
1981fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(UniqueExternalLinkage, DefaultVisibility);
1991fb0caaa7bef765b85972274e3b434af2572c141John McCall
200e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  const VisibilityAttr *ExplicitVisibility = GetExplicitVisibility(D);
201e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2021fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2031fb0caaa7bef765b85972274e3b434af2572c141John McCall
2041fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2051fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2061fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2071fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
2081fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
2091fb0caaa7bef765b85972274e3b434af2572c141John McCall
2101fb0caaa7bef765b85972274e3b434af2572c141John McCall  // We ignore -fvisibility on non-definitions and explicit
2111fb0caaa7bef765b85972274e3b434af2572c141John McCall  // instantiation declarations.
2121fb0caaa7bef765b85972274e3b434af2572c141John McCall  bool ConsiderDashFVisibility = true;
2131fb0caaa7bef765b85972274e3b434af2572c141John McCall
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
2151fb0caaa7bef765b85972274e3b434af2572c141John McCall
216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
2211fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
2221fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
2231fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
2241fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
2251fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
2261fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
2271fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
2281fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
2291fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
2301fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
2311fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
2321fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
2331fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
234ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
235ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
236ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
2371fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
2381fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
239ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC() &&
240e7bc9722c807030409178d4af8ce8d1260bbd821John McCall        !ExplicitVisibility) {
2411fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
2421fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
2431fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LVPair(UniqueExternalLinkage, DefaultVisibility);
2441fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV.second = minVisibility(LV.second, TypeLV.second);
2451fb0caaa7bef765b85972274e3b434af2572c141John McCall    }
2461fb0caaa7bef765b85972274e3b434af2572c141John McCall
247d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
248d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
249d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
2501fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Var->getStorageClass() == SC_PrivateExtern)
2511fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV.second = HiddenVisibility;
2521fb0caaa7bef765b85972274e3b434af2572c141John McCall
253d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
254d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
255d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
256d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
257d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
258d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
259d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
260d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
261d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
262d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
2631fb0caaa7bef765b85972274e3b434af2572c141John McCall        LVPair PrevLV = PrevVar->getLinkageAndVisibility();
2641fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (PrevLV.first) LV.first = PrevLV.first;
2651fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV.second = minVisibility(LV.second, PrevLV.second);
266d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
267d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
268d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
269d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
2701fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
27167fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
27267fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
27367fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
27467fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
2751fb0caaa7bef765b85972274e3b434af2572c141John McCall
276d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
277d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
278d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
279d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
280d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
281d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
282d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
283d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
284d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
285d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
286d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
287d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
288d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
289d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
290d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
291d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
292d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
293d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
294d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
2951fb0caaa7bef765b85972274e3b434af2572c141John McCall        LVPair PrevLV = PrevFunc->getLinkageAndVisibility();
2961fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (PrevLV.first) LV.first = PrevLV.first;
2971fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV.second = minVisibility(LV.second, PrevLV.second);
298d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
299d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
300d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3010b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
3020b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
3031fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility());
3040b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
3051fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
3061fb0caaa7bef765b85972274e3b434af2572c141John McCall
3071fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (SpecInfo->getTemplateSpecializationKind()
3081fb0caaa7bef765b85972274e3b434af2572c141John McCall            == TSK_ExplicitInstantiationDeclaration)
3091fb0caaa7bef765b85972274e3b434af2572c141John McCall        ConsiderDashFVisibility = false;
3100b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
3110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
3121fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (ConsiderDashFVisibility)
3131fb0caaa7bef765b85972274e3b434af2572c141John McCall      ConsiderDashFVisibility = Function->hasBody();
314d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
315d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
316d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
317d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
318d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
319d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
320d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
3211fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
3221fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
3231fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
3241fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(NoLinkage, DefaultVisibility);
3251fb0caaa7bef765b85972274e3b434af2572c141John McCall
3261fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
3271fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
3281fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (const ClassTemplateSpecializationDecl *Spec
3291fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
3301fb0caaa7bef765b85972274e3b434af2572c141John McCall      // From the template.  Note below the restrictions on how we
3311fb0caaa7bef765b85972274e3b434af2572c141John McCall      // compute template visibility.
3321fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility());
3331fb0caaa7bef765b85972274e3b434af2572c141John McCall
3341fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The arguments at which the template was instantiated.
3351fb0caaa7bef765b85972274e3b434af2572c141John McCall      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3361fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
3371fb0caaa7bef765b85972274e3b434af2572c141John McCall
3381fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Spec->getTemplateSpecializationKind()
3391fb0caaa7bef765b85972274e3b434af2572c141John McCall            == TSK_ExplicitInstantiationDeclaration)
3401fb0caaa7bef765b85972274e3b434af2572c141John McCall        ConsiderDashFVisibility = false;
3410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
342d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
343ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
3441fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (ConsiderDashFVisibility)
345ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall      ConsiderDashFVisibility =
346ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
347ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
3481fb0caaa7bef765b85972274e3b434af2572c141John McCall
349d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
3501fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
3511fb0caaa7bef765b85972274e3b434af2572c141John McCall    LVPair EnumLV =
3521fb0caaa7bef765b85972274e3b434af2572c141John McCall      cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
3531fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!isExternalLinkage(EnumLV.first))
3541fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(NoLinkage, DefaultVisibility);
3551fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV = merge(LV, EnumLV);
356d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
357d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
358d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
3591fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
3601fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV = merge(LV, getLVForTemplateParameterList(
3611fb0caaa7bef765b85972274e3b434af2572c141John McCall                                         Template->getTemplateParameters()));
3620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
3631fb0caaa7bef765b85972274e3b434af2572c141John McCall    // We do not want to consider attributes or global settings when
3641fb0caaa7bef765b85972274e3b434af2572c141John McCall    // computing template visibility.
3651fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
366d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
367d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
368d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
3691fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
3701fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
3711fb0caaa7bef765b85972274e3b434af2572c141John McCall
3721fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
3731fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
3741fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
3751fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
3761fb0caaa7bef765b85972274e3b434af2572c141John McCall
3771fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
3781fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
3791fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(NoLinkage, DefaultVisibility);
3801fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
3811fb0caaa7bef765b85972274e3b434af2572c141John McCall
3821fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
3831fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
3841fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (LV.first != ExternalLinkage)
3851fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(LV.first, DefaultVisibility);
3861fb0caaa7bef765b85972274e3b434af2572c141John McCall
3871fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
3881fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
3891fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (LV.second != HiddenVisibility) {
3901fb0caaa7bef765b85972274e3b434af2572c141John McCall    Visibility StandardV;
3911fb0caaa7bef765b85972274e3b434af2572c141John McCall
3921fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If we have an explicit visibility attribute, merge that in.
393e7bc9722c807030409178d4af8ce8d1260bbd821John McCall    if (ExplicitVisibility)
394e7bc9722c807030409178d4af8ce8d1260bbd821John McCall      StandardV = GetVisibilityFromAttr(ExplicitVisibility);
3951fb0caaa7bef765b85972274e3b434af2572c141John McCall    else if (ConsiderDashFVisibility)
3961fb0caaa7bef765b85972274e3b434af2572c141John McCall      StandardV = Context.getLangOptions().getVisibilityMode();
3971fb0caaa7bef765b85972274e3b434af2572c141John McCall    else
3981fb0caaa7bef765b85972274e3b434af2572c141John McCall      StandardV = DefaultVisibility; // no-op
3991fb0caaa7bef765b85972274e3b434af2572c141John McCall
4001fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV.second = minVisibility(LV.second, StandardV);
4011fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
402d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4031fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
404d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
405d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4061fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForClassMember(const NamedDecl *D) {
4071fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
4081fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
4091fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
4101fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
4113cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
4123cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
4131fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
4143cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
4153cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall         (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
4161fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(NoLinkage, DefaultVisibility);
4173cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4183cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // Class members only have linkage if their class has external linkage.
4191fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair ClassLV =
4201fb0caaa7bef765b85972274e3b434af2572c141John McCall    cast<RecordDecl>(D->getDeclContext())->getLinkageAndVisibility();
4211fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (!isExternalLinkage(ClassLV.first))
4221fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(NoLinkage, DefaultVisibility);
4233cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4243cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
4251fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (ClassLV.first == UniqueExternalLinkage)
4261fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(UniqueExternalLinkage, DefaultVisibility);
4271fb0caaa7bef765b85972274e3b434af2572c141John McCall
4281fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Start with the class's linkage and visibility.
4291fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV = ClassLV;
4301fb0caaa7bef765b85972274e3b434af2572c141John McCall
4311fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we have an explicit visibility attribute, merge that in.
432e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  const VisibilityAttr *VA = GetExplicitVisibility(D);
4331fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA));
4341fb0caaa7bef765b85972274e3b434af2572c141John McCall
43567fa6d5ea630c800c3c96e129129aba93d1487c2John McCall  // If it's a variable declaration and we don't have an explicit
43667fa6d5ea630c800c3c96e129129aba93d1487c2John McCall  // visibility attribute, apply the LV from its type.
4371fb0caaa7bef765b85972274e3b434af2572c141John McCall  // See the comment about namespace-scope variable decls above.
43867fa6d5ea630c800c3c96e129129aba93d1487c2John McCall  if (!VA && isa<VarDecl>(D)) {
43967fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    LVPair TypeLV = cast<VarDecl>(D)->getType()->getLinkageAndVisibility();
4401fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (TypeLV.first != ExternalLinkage)
4411fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV.first = minLinkage(LV.first, UniqueExternalLinkage);
4421fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV.second = minVisibility(LV.second, TypeLV.second);
4431fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4443cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4453cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
4461fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
4471fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
4481fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (FunctionTemplateSpecializationInfo *Spec
4493cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
4501fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments));
4511fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateParameterList(
4521fb0caaa7bef765b85972274e3b434af2572c141John McCall                              Spec->getTemplate()->getTemplateParameters()));
4533cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
4543cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4551fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If -fvisibility-inlines-hidden was provided, then inline C++
4561fb0caaa7bef765b85972274e3b434af2572c141John McCall    // member functions get "hidden" visibility if they don't have an
4571fb0caaa7bef765b85972274e3b434af2572c141John McCall    // explicit visibility attribute.
4581fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!VA && MD->isInlined() && LV.second > HiddenVisibility &&
4591fb0caaa7bef765b85972274e3b434af2572c141John McCall        D->getASTContext().getLangOptions().InlineVisibilityHidden)
4601fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV.second = HiddenVisibility;
4611fb0caaa7bef765b85972274e3b434af2572c141John McCall
4623cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // Similarly for member class template specializations.
4633cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  } else if (const ClassTemplateSpecializationDecl *Spec
4643cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall               = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
4651fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs()));
4661fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV = merge(LV, getLVForTemplateParameterList(
4671fb0caaa7bef765b85972274e3b434af2572c141John McCall                    Spec->getSpecializedTemplate()->getTemplateParameters()));
4683cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
4693cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4701fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
4713cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
4723cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4731fb0caaa7bef765b85972274e3b434af2572c141John McCallLVPair NamedDecl::getLinkageAndVisibility() const {
474becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
475becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
476becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
477becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  switch (getKind()) {
478becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
479becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
4801fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
4811fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
482becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
483becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
484becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
485becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
486becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
487becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
488becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
489becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
490becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
491becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
4921fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(ExternalLinkage, DefaultVisibility);
493becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
494becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
495d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
4967a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  if (getDeclContext()->getRedeclContext()->isFileContext())
4971fb0caaa7bef765b85972274e3b434af2572c141John McCall    return getLVForNamespaceScopeDecl(this);
498d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
499d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
500d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
501d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
502d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
503d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
504d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
505d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
5063cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (getDeclContext()->isRecord())
5071fb0caaa7bef765b85972274e3b434af2572c141John McCall    return getLVForClassMember(this);
508d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
509d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
510d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
511d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
512d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
513d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
514d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
515d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
516d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
517d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
518d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
519d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
520d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getLexicalDeclContext()->isFunctionOrMethod()) {
521d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
5220b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Function->isInAnonymousNamespace())
5231fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LVPair(UniqueExternalLinkage, DefaultVisibility);
5241fb0caaa7bef765b85972274e3b434af2572c141John McCall
5251fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair LV(ExternalLinkage, DefaultVisibility);
526e7bc9722c807030409178d4af8ce8d1260bbd821John McCall      if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
5271fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV.second = GetVisibilityFromAttr(VA);
5281fb0caaa7bef765b85972274e3b434af2572c141John McCall
5291fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
5301fb0caaa7bef765b85972274e3b434af2572c141John McCall        LVPair PrevLV = Prev->getLinkageAndVisibility();
5311fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (PrevLV.first) LV.first = PrevLV.first;
5321fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV.second = minVisibility(LV.second, PrevLV.second);
5331fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
5340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
5351fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
536d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
537d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
538d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const VarDecl *Var = dyn_cast<VarDecl>(this))
539d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
540d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
5410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Var->isInAnonymousNamespace())
5421fb0caaa7bef765b85972274e3b434af2572c141John McCall          return LVPair(UniqueExternalLinkage, DefaultVisibility);
5431fb0caaa7bef765b85972274e3b434af2572c141John McCall
5441fb0caaa7bef765b85972274e3b434af2572c141John McCall        LVPair LV(ExternalLinkage, DefaultVisibility);
5451fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
5461fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV.second = HiddenVisibility;
547e7bc9722c807030409178d4af8ce8d1260bbd821John McCall        else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
5481fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV.second = GetVisibilityFromAttr(VA);
5490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
5501fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
5511fb0caaa7bef765b85972274e3b434af2572c141John McCall          LVPair PrevLV = Prev->getLinkageAndVisibility();
5521fb0caaa7bef765b85972274e3b434af2572c141John McCall          if (PrevLV.first) LV.first = PrevLV.first;
5531fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV.second = minVisibility(LV.second, PrevLV.second);
5541fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
5551fb0caaa7bef765b85972274e3b434af2572c141John McCall
5561fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
557d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
558d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
559d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
560d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
561d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
5621fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LVPair(NoLinkage, DefaultVisibility);
5631fb0caaa7bef765b85972274e3b434af2572c141John McCall}
564d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
56547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
5663a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
5673a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
5683a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
5693a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
57047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
57147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
57247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
57347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
57447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
57568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
57668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
57768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
57868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
57968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
58068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
58168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
58268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
58368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
58468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
58568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
58668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
58768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
58868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
5891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
59068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
591f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
592f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
593f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
594f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor                                           TemplateArgs.getFlatArgumentList(),
595d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor                                           TemplateArgs.flat_size(),
5963a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
59768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
59868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
5996be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
60068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
6016be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
60268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
60368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
60468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
60568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
60668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
60768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
60868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
6093521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
6103521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
6113521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
6123521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
61368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
6143521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
6153521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
6163521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
6173521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
61868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
6193521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
6203521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
62168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
6223521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
6233521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
6243521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
6253521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
62668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
62768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
6283521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
6293521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
63068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
63168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
63268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
63368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
63468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
63547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
63647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
6378472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
63868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
6398472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
64068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
64147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
64268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
64347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
64447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
6454afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
6466ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
6476ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
6482a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
6492a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
6502a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
6512a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
6522a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
6532a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6556ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
6566ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
6576ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
6586ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
659e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
660e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
661e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
662e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
663e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
664e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
665e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6670de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
6680de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
6690de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
671f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
672f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
673f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
6749488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
6759488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
6769488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
6779488ea120e093068021f944176c3d610dd540914John McCall
6786ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
6796ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
6806ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
6816ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
6826ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
6836ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
684d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
685d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
686d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
6874afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
688e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
689e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
690e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
6919488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
692e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
693e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
694e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
695e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
696e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
697e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
698e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
699e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
700e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
701161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
702161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
703161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
704161755a09898c95d21bfff33707da9ca41cd53c5John McCall
705161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
706161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
707161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
708161755a09898c95d21bfff33707da9ca41cd53c5John McCall
709161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FieldDecl>(D))
710161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
711161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
712161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
713161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
714161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
715161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
716161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
717161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
718161755a09898c95d21bfff33707da9ca41cd53c5John McCall
7195239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
720a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
721a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
722a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
7231693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
7241693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
7251693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
7261693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
7271693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
7281693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
7291693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
7301693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
731a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
7324e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
7334e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
734a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
735a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
736a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
737b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
738b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                      SourceRange QualifierRange) {
739b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
740b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
741b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
742b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
743b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
744b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
745b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
746b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
747b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
748b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
749b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
750b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
751b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
752b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
753b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
754b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
755b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
756b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
757b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
758b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
759b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
760b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
761b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
762b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
763b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
764b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
765b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
766b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
7671693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
7681693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
7691693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
7701693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
7719b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
772c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
773c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
7749b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
7759b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
7769b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
7779b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || NNS) &&
7789b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Nonempty array of template parameters with no qualifier!");
7799b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
7809b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
7819b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
782c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
7839b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
7849b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
7859b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
7869b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
7879b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
788c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7899b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
7909b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
7919b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
7929b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
7939b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
7949b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
795a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
79699f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
79799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
79899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
7997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
8007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
801d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_None:          break;
802d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Auto:          return "auto"; break;
803d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Extern:        return "extern"; break;
804d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_PrivateExtern: return "__private_extern__"; break;
805d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Register:      return "register"; break;
806d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Static:        return "static"; break;
8077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
8087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
8107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
8117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8134afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
814a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
81516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
81616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
81799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
81899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
8191693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation VarDecl::getInnerLocStart() const {
82033e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
82133e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (Start.isInvalid())
82233e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    Start = getLocation();
8231693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return Start;
8241693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
8251693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
8261693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
82755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
8281693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
8291693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), getLocation());
83055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
83155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
8327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
8337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
8347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
8357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
836d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
8377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
8387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
8407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl       DC = DC->getParent()) {
8417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
8427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
843d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
8447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
8467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
8477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (DC->isFunctionOrMethod())
8497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      return false;
8507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
8517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
8537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
8567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
8577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
859e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
860e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
861e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
862e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
863e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
864e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
865e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
866e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
867e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
868e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
869e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
870e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
871e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
872e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
873e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
874e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
875e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
876e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
877e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
878e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
879e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
880e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
881e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
882e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
883e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
884e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
885e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
886e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
8872bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
888d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
889d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
8902bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
8912bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
8922bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
8932bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
8942bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
8952bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
896e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
897e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
898e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
899e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
900e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
901e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
902e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
903e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
904e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
905e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
906e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
907e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
908e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
909e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
910e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
911e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
912e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
913e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
914f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
915e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
916e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
917e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
918e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
919e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
920e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
921e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
922e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
923e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
924e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
925e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
926e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
927e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
928e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
929e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
930e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
931e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
932e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
933e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
934e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
935e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
93631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
93731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
93831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
93931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
940e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
941e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
942e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
94331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
94431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
94531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
94631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
947e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
948e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
94931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
9507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
9517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
9527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
9537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
95531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
9567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
9577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
9597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9611028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
9621028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Decl::isOutOfLine())
9631028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
9648761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
9658761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
9668761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
9678761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
9681028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
9691028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
9701028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
9711028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
9721028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
9731028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
9741028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
9751028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
9761028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
9770d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
9780d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
9790d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
9800d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
9810d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
9820d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
9830d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
9840d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
9850d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
9860d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
9870d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
9880d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
9890d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
990838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
9917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
9927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
993838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
9947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
9977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9991028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1000b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1001251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1002251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1003251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1004251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1005251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1006663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1007e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1008251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1009251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1010251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1011251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1012251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
10131028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1014b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1015b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1016b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
10170a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
10180a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1019b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1020251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1021251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
10220a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
10230a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
10240a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
10250a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
10267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
10277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
10297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
10307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1031275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
10327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
10337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
10347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
103516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
103616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
103716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
103816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1039275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1040275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
10417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
10427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
10437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
10447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
10457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
10477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
10487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
10497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
10517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
10547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const CXXExprWithTemporaries *E =
10557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        dyn_cast<CXXExprWithTemporaries>(getInit()))
10567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
1057275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
1058c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
1059275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1060275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
10617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
10627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
10637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
10647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
10667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
10677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
10707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
10717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
10727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
10747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
10757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1077fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1078fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
107999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
10808a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
10818a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
10828a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1083136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCallvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1084136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        const PrintingPolicy &Policy,
1085136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        bool Qualified) const {
1086136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1087136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1088136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  if (TemplateArgs)
1089136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall    S += TemplateSpecializationType::PrintTemplateArgumentList(
1090136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->getFlatArgumentList(),
1091136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->flat_size(),
1092136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                                               Policy);
1093136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
1094136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall}
109527f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
10969498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
10979498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
10989498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
10999498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
11009498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
11019498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
110206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
110306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
110406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (I->Body) {
110506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
110606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
110706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
110806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
110906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
111006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
111106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
111206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
11136fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1114c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1115c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1116c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1117c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
1118f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1119f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1120f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1121f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
11225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
112455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
112555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
11261a5364e0fa0482d8d477d6f136d52e503bbe13f4Argyrios Kyrtzidis  if (B)
112755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
112855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
112955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
11302138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
11312138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
11322138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
11332138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
11342138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
11352138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
11362138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
113748a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
113848a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
113907a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
11407a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    getDeclContext()->getRedeclContext()->isTranslationUnit() &&
114104495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
114204495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
114304495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
114448a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
114548a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
11466393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
11476393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
11486393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1149d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
11506393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
11511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
11526393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor       DC = DC->getParent()) {
11536393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
11546393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1155d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
115640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
11576393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
11586393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
11596393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
116045975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor
116145975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor    if (DC->isRecord())
116245975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor      break;
11636393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
11646393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
11650bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
11666393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
11676393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
11688499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
11698499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
11708499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
11718499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1172d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
11738499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
11748499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
11751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
11768499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
11778499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
11788499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
11798499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
11808499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
11818499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
11828499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
11838499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
11848499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
11858499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
11868499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
11878499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
11887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
11897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
11907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
11917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
11937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
11947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
11957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
11967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
11977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
12017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
12027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
12057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
12067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12083e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
12093e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
12103e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
12113e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
12123e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
12133e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
12143e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
12151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
12163e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
12177814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
12187814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
12193c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
12203c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
12213c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12223c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
12233c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
12243c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
12253c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12263c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
12273c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
12283c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
12293c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12309add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1231d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
12329add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
12339add31798f621f843233dbff8bba103fca64447bDouglas Gregor
12343c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
12353c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
12363c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
12373c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
12383c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
12393c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12403c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
12413c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
12423c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
12443c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
124540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
12463c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
12473c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12483c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
12493e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
12503e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
12513e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
12523e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
12531ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
12542dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner/// based on its FunctionType.  This is the length of the PararmInfo array
12551ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
12561ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1257183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
125872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1259d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
126072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
12611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12646b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
12656b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                             ParmVarDecl **NewParamInfo, unsigned NumParams) {
12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
12672dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
12705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
12716b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1272fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
12735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
127455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
127596888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
127696888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1277cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
127855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12828123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
12838123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
12848123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
12859e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner/// arguments (in C++).
12868123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
12878123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  unsigned NumRequiredArgs = getNumParams();
12888123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  while (NumRequiredArgs > 0
1289ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
12908123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
12918123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
12928123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
12938123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
12948123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
12957ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
129648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // FIXME: This is not enough. Consider:
129748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
129848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // inline void f();
129948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // void f() { }
130048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
130148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // f is inlined, but does not have inline specified.
130248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // To fix this we should add an 'inline' flag to FunctionDecl.
130348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isInlineSpecified())
13047d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
130548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
130648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
130748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
130848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
130948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
13107d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
13117d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
13127d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
13137d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
13147d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
13157d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
13167d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
13177d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
13187d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
13197d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
13207d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
13217d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
13227d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
13237d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
132406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
13257d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
132606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
13277d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
132806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
13297d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
13307d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
13317d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
13327ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
13337ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
13347d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
13351fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
13361fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
13371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
13381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
13391fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
13401fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
13411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
13421fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
13431e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
13441fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
13451fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
13461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
13471fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
13481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
13491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
13501fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
13511fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
13521fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
13537ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
13547d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
13551fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
13567d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
13571fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU inline semantics. Based on a number of examples, we came up with the
13581fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // following heuristic: if the "inline" keyword is present on a
13591fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration of the function but "extern" is not present on that
13601fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration, then the symbol is externally visible. Otherwise, the GNU
13611fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // "extern inline" semantics applies and the symbol is not externally
13621fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // visible.
13631fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
13641fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
13651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
1366d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
13671fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
13681fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    }
13691fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
13701fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU "extern inline" semantics; no externally visible symbol.
13719f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
13721fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
13731fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
13741fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
13751fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
13761fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
13771fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
13781fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
13791fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
13801fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
13811fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
13821fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
13831fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
13841fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1385d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
13861fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
13871fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
13881fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
13891fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
13901fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
13911fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
13921fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
13939f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
13949f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
13959f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
13961cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
13971cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
13981cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1399e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1400e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
14011cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
14021cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
14031cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
14041cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1405a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1406a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1407a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1408a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1409a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1410a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1411a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1412a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1413a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1414d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1415d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1416d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1417d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1418d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1419d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1420d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1421d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1422d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1423d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1424d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1425d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1426d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1427d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1428d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1429d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1430d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
14312db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1432b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
14332db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
14342db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
14352db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
14362db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
14372db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1438b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1439b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1440b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1441b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
14422db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
14436b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
14446b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
14452db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
14462db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
14472db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
14482db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
14496b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
14502db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
14512db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
14522db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
14533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
14546cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
14556cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
14563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
14573b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14583b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
14593b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
14603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
14613b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
14623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
14633b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
14653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
14663b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14673b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
14683b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
14693b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
14703b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
14713b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14723b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
14733b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
147406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
14753b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
147606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
14773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14783b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
14793b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
14803b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
14813b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
148206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
14833b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
14843b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14857ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
14863b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
14873b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14883b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
14893b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
14903b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
14913b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
14923b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
14933b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
14943b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
14953b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14963b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
14973b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
14983b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14993b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
15003b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
15013b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
15023b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
15033b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
15043b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
150516e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
15061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
150716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
150816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
15091fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
151016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
151116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
151216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
151316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
151416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
151516e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
15161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1517fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1518fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
151916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
152016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
152116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
152216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
152316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1524e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1525e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1526e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1527e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1528e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1529e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1530e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1531e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1532e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1533e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
15341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
15356b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
15366b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
1537127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1538b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1539e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
15407b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
15417b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1542b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1543b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
154516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
15461637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1547a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1548a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
1549a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
1550a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
15511637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
15521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1553127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1554b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1555b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1556b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1557b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
15582c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
15592c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
15602c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
15612c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
1562b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1563b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
15642c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
15652c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
15662c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
1567b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
15681637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
15691637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1570af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1571af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1572af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1573af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1574af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1575af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1576af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
157721c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1578af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1579af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1580af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1581af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1582af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1583af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1584af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1585af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1586af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1587af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1588af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1589af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1590af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1591af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1592af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1593af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1594af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1595af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1596af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1597af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1598af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1599af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1600af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1601af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1602af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1603af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1604d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
16051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1606d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
16072db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
16081fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
16092db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
16102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1611d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
16122db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
16132db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
16142db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
16152db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
16162db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
16172db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
16181fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
16191fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
16201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
16210a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
16220a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
16232db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
16242db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
16250a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
16262db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
16270a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
16280a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
16290a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
16300a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
16310a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
16320a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
16332db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
16340a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
16350a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
16360a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
16370a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
16380a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
16392db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
16401fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
16411fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
16420a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
16430a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
16440a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
16450a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
16460a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
16470a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
16480a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
16490a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
16500a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
16510a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
16520a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
16530a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
16549f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
16559f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (Decl::isOutOfLine())
16569f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
16579f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
16589f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
16599f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
16609f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
16619f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
166206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
16639f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
16649f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
16659f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
16669f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
16679f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
16689f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
16699f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
167006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
16719f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
16729f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
16739f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
16749f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
16759f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
16769f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
16778a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
16787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
16797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
16807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
16827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             IdentifierInfo *Id, QualType T,
16837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
16847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
16857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
16887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
16897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
16907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
16927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
16937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
16957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1698bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
16994b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
17004b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
17011693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
17021693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
17031693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
17041693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1705f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1706f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
17071693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
1708f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1709f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1710b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
17118e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1712b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1713b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
171460e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
171560e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
171660e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
171760e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor    TypeForDecl->ClearLinkageCache();
171860e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
171960e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
17200b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
1721ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
172286ff308724171494395a840fd2efbe25e62f352eJohn McCall
172386ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
172486ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
172586ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
172686ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
17272243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
17282243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
172986ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
17300b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
17310b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
17320b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
17335cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
17345cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
17355cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
17365cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
17370b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
1738ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
1739565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
1740565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
1741565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
17420b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
17430b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
1744952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
17458e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
17468e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
1747220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1748220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
17491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
17518e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
17528e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
17538e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
17541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17558e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
17564b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
17574b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1758b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1759b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                               SourceRange QualifierRange) {
1760b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
1761b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
1762b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
1763b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1764b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1765b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
1766b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
1767b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1768b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
1769b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1770b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
1771b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
1772b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
1773b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
1774b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1775b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1776b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1777b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
17784b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
17797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
17807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
17817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
17837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
17841274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                           EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
17851274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
17861274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                    IsScoped, IsFixed);
17877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
17887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
17897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1791b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
17921274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
17931274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                          false, false);
1794b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1795b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1796838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
17971b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
17981b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
17991b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
18007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
18011274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
18021274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
18037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
18041b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
18051b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
18067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
18077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
18087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
18108a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
18118a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
18125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
181335bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
18148e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
18158e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
18168e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
18176359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
1818bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
1819082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
1820eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
18216359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
18226359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
18236359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
18246359792ca92e7ca2f416cb804c6604358174e994Ted KremenekRecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
18254b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
1826741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
18271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18288e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
18294b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
18304b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
18316359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
18326359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
1833b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisRecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1834b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1835b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis                            SourceLocation());
1836b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1837b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1838c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
18391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1840c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1841c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
1842c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
1843eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
1844eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1845eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
1846eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1847eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
1848eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
1849eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
185044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// completeDefinition - Notes that the definition of this type is now
185144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// complete.
1852838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid RecordDecl::completeDefinition() {
18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!isDefinition() && "Cannot redefine record!");
18540b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  TagDecl::completeDefinition();
18555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1857bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCallValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1858bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  // Force the decl chain to come into existence properly.
1859bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  if (!getNextDeclInContext()) getParent()->decls_begin();
1860bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
1861bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(isAnonymousStructOrUnion());
1862bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1863bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->isRecordType());
1864bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1865bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  return D;
1866bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall}
1867bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
1868eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
1869eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
1870eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
1871eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1872eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
1873eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
1874eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1875eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<Decl*, 64> Decls;
1876eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1877eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
1878eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1879eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
1880eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
1881eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
1882eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
1883eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
1884eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1885eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = true;
1886eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1887eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
1888eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
1889eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1890eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1891eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
1892eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
189356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
189456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
189556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
189656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
1897838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
1898e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
1899e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
19001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1901e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
1902e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
1903e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
1904838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
1905e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1906e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1907e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
1908e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
1909e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1910e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffunsigned BlockDecl::getNumParams() const {
1911e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  return NumParams;
1912e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
19137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
19177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
19207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
19217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
19247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
19257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
19267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
192806c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
192906c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
193006c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
193106c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
193206c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
19337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
19347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    SourceLocation L, IdentifierInfo *Id, QualType T) {
19357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
19367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
19392577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
19402577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
194116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   StorageClass S, StorageClass SCAsWritten,
194216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   bool isInline, bool hasWrittenPrototype) {
19432577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
194416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                           S, SCAsWritten, isInline);
19457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
19467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
19477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
19507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
19517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
19547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
19557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
19567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
19577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
19587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19608e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
19618e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
19628e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
19638e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
19648e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
19658e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
19668e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
19677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
19687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
19697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
19707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
19717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
19747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
19757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
19767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
19777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
1978