Decl.cpp revision 06c919300ce39e50ed7f6dff5025c8ed96dcf221
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
361fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
371fb0caaa7bef765b85972274e3b434af2572c141John McCall  switch (A->getVisibility()) {
381fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Default:
391fb0caaa7bef765b85972274e3b434af2572c141John McCall    return DefaultVisibility;
401fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Hidden:
411fb0caaa7bef765b85972274e3b434af2572c141John McCall    return HiddenVisibility;
421fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Protected:
431fb0caaa7bef765b85972274e3b434af2572c141John McCall    return ProtectedVisibility;
441fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
451fb0caaa7bef765b85972274e3b434af2572c141John McCall  return DefaultVisibility;
461fb0caaa7bef765b85972274e3b434af2572c141John McCall}
471fb0caaa7bef765b85972274e3b434af2572c141John McCall
481fb0caaa7bef765b85972274e3b434af2572c141John McCalltypedef std::pair<Linkage,Visibility> LVPair;
491fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair merge(LVPair L, LVPair R) {
501fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LVPair(minLinkage(L.first, R.first),
511fb0caaa7bef765b85972274e3b434af2572c141John McCall                minVisibility(L.second, R.second));
521fb0caaa7bef765b85972274e3b434af2572c141John McCall}
531fb0caaa7bef765b85972274e3b434af2572c141John McCall
540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
561fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair
571fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) {
581fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
641fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
701fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV =
711fb0caaa7bef765b85972274e3b434af2572c141John McCall        merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
751fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
801fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
811fb0caaa7bef765b85972274e3b434af2572c141John McCall                                           unsigned NumArgs) {
821fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
921fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
961fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
971fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
981fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
991fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1001fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV = merge(LV, ND->getLinkageAndVisibility());
1011fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1021fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV = merge(LV, VD->getType()->getLinkageAndVisibility());
1031fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1040b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1050b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1060b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
1071fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
1081fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, Template->getLinkageAndVisibility());
1090b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1100b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
1121fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
1131fb0caaa7bef765b85972274e3b434af2572c141John McCall                                                  Args[I].pack_size()));
1140b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1150b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1160b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1170b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1181fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1190b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1211fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
1221fb0caaa7bef765b85972274e3b434af2572c141John McCall  return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
1231fb0caaa7bef765b85972274e3b434af2572c141John McCall                                      TArgs.flat_size());
1243cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
1253cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
1261fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForNamespaceScopeDecl(const NamedDecl *D) {
1277a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
128d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
129d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
130d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
131d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
132d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
133d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
134d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
135d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
136d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
137d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
138d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
139d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
1401fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(InternalLinkage, DefaultVisibility);
141d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
142d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
143d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
144d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
145d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
146d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
147e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
148d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
149d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
150d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
151d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
152d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
153d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
1540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
155d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
156d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
157d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
1581fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LVPair(InternalLinkage, DefaultVisibility);
159d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
160d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
1610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
1620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
1630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
164d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
165d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
166d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
167d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
168d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
169d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
170d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
171d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
172d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
1731fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(InternalLinkage, DefaultVisibility);
174d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
175d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
176d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
1771fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(InternalLinkage, DefaultVisibility);
178d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
179d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
1801fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (D->isInAnonymousNamespace())
1811fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(UniqueExternalLinkage, DefaultVisibility);
1821fb0caaa7bef765b85972274e3b434af2572c141John McCall
1831fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
1841fb0caaa7bef765b85972274e3b434af2572c141John McCall
1851fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
1861fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
1871fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
1881fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
1891fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1901fb0caaa7bef765b85972274e3b434af2572c141John McCall
1911fb0caaa7bef765b85972274e3b434af2572c141John McCall  // We ignore -fvisibility on non-definitions and explicit
1921fb0caaa7bef765b85972274e3b434af2572c141John McCall  // instantiation declarations.
1931fb0caaa7bef765b85972274e3b434af2572c141John McCall  bool ConsiderDashFVisibility = true;
1941fb0caaa7bef765b85972274e3b434af2572c141John McCall
195d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
1961fb0caaa7bef765b85972274e3b434af2572c141John McCall
197d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
198d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
199d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
200d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
201d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
2021fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
2031fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
2041fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
2051fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
2061fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
2071fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
2081fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
2091fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
2101fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
2111fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
2121fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
2131fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
2141fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
215ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
216ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
217ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
2181fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
2191fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
220ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC() &&
221ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        !Var->hasAttr<VisibilityAttr>()) {
2221fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
2231fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
2241fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LVPair(UniqueExternalLinkage, DefaultVisibility);
2251fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV.second = minVisibility(LV.second, TypeLV.second);
2261fb0caaa7bef765b85972274e3b434af2572c141John McCall    }
2271fb0caaa7bef765b85972274e3b434af2572c141John McCall
228d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
229d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
230d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
2311fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Var->getStorageClass() == SC_PrivateExtern)
2321fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV.second = HiddenVisibility;
2331fb0caaa7bef765b85972274e3b434af2572c141John McCall
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
237d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
240d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
241d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
242d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
2441fb0caaa7bef765b85972274e3b434af2572c141John McCall        LVPair PrevLV = PrevVar->getLinkageAndVisibility();
2451fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (PrevLV.first) LV.first = PrevLV.first;
2461fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV.second = minVisibility(LV.second, PrevLV.second);
247d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
248d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
249d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
250d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
2511fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
2521fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the function's LV by the LV of its type unless this is
2531fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  See the comment above about variables.
254ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
255ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        !Function->hasAttr<VisibilityAttr>()) {
2561fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Function->getType()->getLinkageAndVisibility();
2571fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
2581fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LVPair(UniqueExternalLinkage, DefaultVisibility);
2591fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV.second = minVisibility(LV.second, TypeLV.second);
2601fb0caaa7bef765b85972274e3b434af2572c141John McCall    }
2611fb0caaa7bef765b85972274e3b434af2572c141John McCall
262d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
263d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
264d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
265d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
266d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
267d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
268d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
269d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
270d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
271d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
272d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
273d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
274d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
275d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
276d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
277d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
278d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
279d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
280d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
2811fb0caaa7bef765b85972274e3b434af2572c141John McCall        LVPair PrevLV = PrevFunc->getLinkageAndVisibility();
2821fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (PrevLV.first) LV.first = PrevLV.first;
2831fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV.second = minVisibility(LV.second, PrevLV.second);
284d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
285d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
286d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
2880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
2891fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility());
2900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
2911fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
2921fb0caaa7bef765b85972274e3b434af2572c141John McCall
2931fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (SpecInfo->getTemplateSpecializationKind()
2941fb0caaa7bef765b85972274e3b434af2572c141John McCall            == TSK_ExplicitInstantiationDeclaration)
2951fb0caaa7bef765b85972274e3b434af2572c141John McCall        ConsiderDashFVisibility = false;
2960b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
2970b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2981fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (ConsiderDashFVisibility)
2991fb0caaa7bef765b85972274e3b434af2572c141John McCall      ConsiderDashFVisibility = Function->hasBody();
300d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
301d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
302d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
303d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
304d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
305d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
306d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
3071fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
3081fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
3091fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
3101fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(NoLinkage, DefaultVisibility);
3111fb0caaa7bef765b85972274e3b434af2572c141John McCall
3121fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
3131fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
3141fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (const ClassTemplateSpecializationDecl *Spec
3151fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
3161fb0caaa7bef765b85972274e3b434af2572c141John McCall      // From the template.  Note below the restrictions on how we
3171fb0caaa7bef765b85972274e3b434af2572c141John McCall      // compute template visibility.
3181fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility());
3191fb0caaa7bef765b85972274e3b434af2572c141John McCall
3201fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The arguments at which the template was instantiated.
3211fb0caaa7bef765b85972274e3b434af2572c141John McCall      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3221fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
3231fb0caaa7bef765b85972274e3b434af2572c141John McCall
3241fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Spec->getTemplateSpecializationKind()
3251fb0caaa7bef765b85972274e3b434af2572c141John McCall            == TSK_ExplicitInstantiationDeclaration)
3261fb0caaa7bef765b85972274e3b434af2572c141John McCall        ConsiderDashFVisibility = false;
3270b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
328d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
329ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
3301fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (ConsiderDashFVisibility)
331ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall      ConsiderDashFVisibility =
332ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
333ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
3341fb0caaa7bef765b85972274e3b434af2572c141John McCall
335d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
3361fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
3371fb0caaa7bef765b85972274e3b434af2572c141John McCall    LVPair EnumLV =
3381fb0caaa7bef765b85972274e3b434af2572c141John McCall      cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
3391fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!isExternalLinkage(EnumLV.first))
3401fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(NoLinkage, DefaultVisibility);
3411fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV = merge(LV, EnumLV);
342d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
343d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
344d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
3451fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
3461fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV = merge(LV, getLVForTemplateParameterList(
3471fb0caaa7bef765b85972274e3b434af2572c141John McCall                                         Template->getTemplateParameters()));
3480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
3491fb0caaa7bef765b85972274e3b434af2572c141John McCall    // We do not want to consider attributes or global settings when
3501fb0caaa7bef765b85972274e3b434af2572c141John McCall    // computing template visibility.
3511fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
3551fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
3561fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
3571fb0caaa7bef765b85972274e3b434af2572c141John McCall
3581fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
3591fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
3601fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
3611fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
3621fb0caaa7bef765b85972274e3b434af2572c141John McCall
3631fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
3641fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
3651fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(NoLinkage, DefaultVisibility);
3661fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
3671fb0caaa7bef765b85972274e3b434af2572c141John McCall
3681fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
3691fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
3701fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (LV.first != ExternalLinkage)
3711fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(LV.first, DefaultVisibility);
3721fb0caaa7bef765b85972274e3b434af2572c141John McCall
3731fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
3741fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
3751fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (LV.second != HiddenVisibility) {
3761fb0caaa7bef765b85972274e3b434af2572c141John McCall    Visibility StandardV;
3771fb0caaa7bef765b85972274e3b434af2572c141John McCall
3781fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If we have an explicit visibility attribute, merge that in.
3791fb0caaa7bef765b85972274e3b434af2572c141John McCall    const VisibilityAttr *VA = D->getAttr<VisibilityAttr>();
3801fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (VA)
3811fb0caaa7bef765b85972274e3b434af2572c141John McCall      StandardV = GetVisibilityFromAttr(VA);
3821fb0caaa7bef765b85972274e3b434af2572c141John McCall    else if (ConsiderDashFVisibility)
3831fb0caaa7bef765b85972274e3b434af2572c141John McCall      StandardV = Context.getLangOptions().getVisibilityMode();
3841fb0caaa7bef765b85972274e3b434af2572c141John McCall    else
3851fb0caaa7bef765b85972274e3b434af2572c141John McCall      StandardV = DefaultVisibility; // no-op
3861fb0caaa7bef765b85972274e3b434af2572c141John McCall
3871fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV.second = minVisibility(LV.second, StandardV);
3881fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
389d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3901fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
391d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
392d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3931fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForClassMember(const NamedDecl *D) {
3941fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
3951fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
3961fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
3971fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
3983cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
3993cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
4001fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
4013cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
4023cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall         (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
4031fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(NoLinkage, DefaultVisibility);
4043cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4053cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // Class members only have linkage if their class has external linkage.
4061fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair ClassLV =
4071fb0caaa7bef765b85972274e3b434af2572c141John McCall    cast<RecordDecl>(D->getDeclContext())->getLinkageAndVisibility();
4081fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (!isExternalLinkage(ClassLV.first))
4091fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(NoLinkage, DefaultVisibility);
4103cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4113cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
4121fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (ClassLV.first == UniqueExternalLinkage)
4131fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LVPair(UniqueExternalLinkage, DefaultVisibility);
4141fb0caaa7bef765b85972274e3b434af2572c141John McCall
4151fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Start with the class's linkage and visibility.
4161fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV = ClassLV;
4171fb0caaa7bef765b85972274e3b434af2572c141John McCall
4181fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we have an explicit visibility attribute, merge that in.
4191fb0caaa7bef765b85972274e3b434af2572c141John McCall  const VisibilityAttr *VA = D->getAttr<VisibilityAttr>();
4201fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA));
4211fb0caaa7bef765b85972274e3b434af2572c141John McCall
4221fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If it's a value declaration, apply the LV from its type.
4231fb0caaa7bef765b85972274e3b434af2572c141John McCall  // See the comment about namespace-scope variable decls above.
4241fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (isa<ValueDecl>(D)) {
4251fb0caaa7bef765b85972274e3b434af2572c141John McCall    LVPair TypeLV = cast<ValueDecl>(D)->getType()->getLinkageAndVisibility();
4261fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (TypeLV.first != ExternalLinkage)
4271fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV.first = minLinkage(LV.first, UniqueExternalLinkage);
4281fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV.second = minVisibility(LV.second, TypeLV.second);
4291fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4303cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4313cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
4321fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
4331fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
4341fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (FunctionTemplateSpecializationInfo *Spec
4353cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
4361fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments));
4371fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateParameterList(
4381fb0caaa7bef765b85972274e3b434af2572c141John McCall                              Spec->getTemplate()->getTemplateParameters()));
4393cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
4403cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4411fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If -fvisibility-inlines-hidden was provided, then inline C++
4421fb0caaa7bef765b85972274e3b434af2572c141John McCall    // member functions get "hidden" visibility if they don't have an
4431fb0caaa7bef765b85972274e3b434af2572c141John McCall    // explicit visibility attribute.
4441fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!VA && MD->isInlined() && LV.second > HiddenVisibility &&
4451fb0caaa7bef765b85972274e3b434af2572c141John McCall        D->getASTContext().getLangOptions().InlineVisibilityHidden)
4461fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV.second = HiddenVisibility;
4471fb0caaa7bef765b85972274e3b434af2572c141John McCall
4483cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // Similarly for member class template specializations.
4493cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  } else if (const ClassTemplateSpecializationDecl *Spec
4503cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall               = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
4511fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs()));
4521fb0caaa7bef765b85972274e3b434af2572c141John McCall    LV = merge(LV, getLVForTemplateParameterList(
4531fb0caaa7bef765b85972274e3b434af2572c141John McCall                    Spec->getSpecializedTemplate()->getTemplateParameters()));
4543cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
4553cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4561fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
4573cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
4583cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4591fb0caaa7bef765b85972274e3b434af2572c141John McCallLVPair NamedDecl::getLinkageAndVisibility() const {
460becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
461becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
462becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
463becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  switch (getKind()) {
464becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
465becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
4661fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
4671fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
468becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
469becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
470becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
471becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
472becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
473becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
474becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
475becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
476becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
477becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
4781fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LVPair(ExternalLinkage, DefaultVisibility);
479becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
480becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
481d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
4827a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  if (getDeclContext()->getRedeclContext()->isFileContext())
4831fb0caaa7bef765b85972274e3b434af2572c141John McCall    return getLVForNamespaceScopeDecl(this);
484d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
485d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
486d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
487d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
488d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
489d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
490d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
491d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
4923cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (getDeclContext()->isRecord())
4931fb0caaa7bef765b85972274e3b434af2572c141John McCall    return getLVForClassMember(this);
494d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
495d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
496d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
497d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
498d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
499d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
500d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
501d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
502d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
503d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
504d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
505d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
506d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getLexicalDeclContext()->isFunctionOrMethod()) {
507d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
5080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Function->isInAnonymousNamespace())
5091fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LVPair(UniqueExternalLinkage, DefaultVisibility);
5101fb0caaa7bef765b85972274e3b434af2572c141John McCall
5111fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair LV(ExternalLinkage, DefaultVisibility);
5121fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const VisibilityAttr *VA = Function->getAttr<VisibilityAttr>())
5131fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV.second = GetVisibilityFromAttr(VA);
5141fb0caaa7bef765b85972274e3b434af2572c141John McCall
5151fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
5161fb0caaa7bef765b85972274e3b434af2572c141John McCall        LVPair PrevLV = Prev->getLinkageAndVisibility();
5171fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (PrevLV.first) LV.first = PrevLV.first;
5181fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV.second = minVisibility(LV.second, PrevLV.second);
5191fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
5200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
5211fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
522d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
523d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
524d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const VarDecl *Var = dyn_cast<VarDecl>(this))
525d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
526d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
5270b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Var->isInAnonymousNamespace())
5281fb0caaa7bef765b85972274e3b434af2572c141John McCall          return LVPair(UniqueExternalLinkage, DefaultVisibility);
5291fb0caaa7bef765b85972274e3b434af2572c141John McCall
5301fb0caaa7bef765b85972274e3b434af2572c141John McCall        LVPair LV(ExternalLinkage, DefaultVisibility);
5311fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
5321fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV.second = HiddenVisibility;
5331fb0caaa7bef765b85972274e3b434af2572c141John McCall        else if (const VisibilityAttr *VA = Var->getAttr<VisibilityAttr>())
5341fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV.second = GetVisibilityFromAttr(VA);
5350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
5361fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
5371fb0caaa7bef765b85972274e3b434af2572c141John McCall          LVPair PrevLV = Prev->getLinkageAndVisibility();
5381fb0caaa7bef765b85972274e3b434af2572c141John McCall          if (PrevLV.first) LV.first = PrevLV.first;
5391fb0caaa7bef765b85972274e3b434af2572c141John McCall          LV.second = minVisibility(LV.second, PrevLV.second);
5401fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
5411fb0caaa7bef765b85972274e3b434af2572c141John McCall
5421fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
543d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
544d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
545d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
546d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
547d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
5481fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LVPair(NoLinkage, DefaultVisibility);
5491fb0caaa7bef765b85972274e3b434af2572c141John McCall}
550d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
55147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
5523a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
5533a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
5543a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
5553a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
55647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
55747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
55847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
55947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
56047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
56168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
56268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
56368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
56468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
56568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
56668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
56768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
56868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
56968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
57068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
57168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
57268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
57368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
57468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
57668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
577f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
578f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
579f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
580f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor                                           TemplateArgs.getFlatArgumentList(),
581d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor                                           TemplateArgs.flat_size(),
5823a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
58368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
58468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
5856be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
58668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
5876be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
58868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
58968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
59068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
59168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
59268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
59368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
59468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
5953521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
5963521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
5973521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
5983521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
59968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
6003521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
6013521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
6023521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
6033521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
60468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
6053521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
6063521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
60768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
6083521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
6093521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
6103521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
6113521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
61268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
61368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
6143521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
6153521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
61668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
61768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
61868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
61968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
62068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
62147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
62247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
6238472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
62468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
6258472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
62668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
62747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
62868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
62947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
63047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
6314afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
6326ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
6336ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
6342a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
6352a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
6362a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
6372a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
6382a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
6392a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6416ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
6426ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
6436ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
6446ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
645e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
646e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
647e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
648e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
649e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
650e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
651e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
6521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6530de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
6540de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
6550de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
657f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
658f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
659f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
6609488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
6619488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
6629488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
6639488ea120e093068021f944176c3d610dd540914John McCall
6646ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
6656ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
6666ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
6676ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
6686ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
6696ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
670d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
671d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
672d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
6734afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
674e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
675e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
676e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
6779488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
678e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
679e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
680e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
681e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
682e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
683e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
684e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
685e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
686e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
687161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
688161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
689161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
690161755a09898c95d21bfff33707da9ca41cd53c5John McCall
691161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
692161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
693161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
694161755a09898c95d21bfff33707da9ca41cd53c5John McCall
695161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FieldDecl>(D))
696161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
697161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
698161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
699161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
700161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
701161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
702161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
703161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
704161755a09898c95d21bfff33707da9ca41cd53c5John McCall
7055239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
706a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
707a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
708a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
7091693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
7101693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
7111693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
7121693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
7131693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
7141693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
7151693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
7161693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
717a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
7184e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
7194e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
720a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
721a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
722a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
723b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
724b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                      SourceRange QualifierRange) {
725b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
726b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
727b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
728b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
729b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
730b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
731b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
732b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
733b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
734b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
735b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
736b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
737b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
738b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
739b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
740b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
741b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
742b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
743b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
744b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
745b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
746b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
747b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
748b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
749b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
750b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
751b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
752b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
7531693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
7541693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
7551693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
7561693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
7579b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
758c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
759c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
7609b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
7619b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
7629b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
7639b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || NNS) &&
7649b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Nonempty array of template parameters with no qualifier!");
7659b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
7669b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
7679b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
768c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
7699b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
7709b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
7719b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
7729b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
7739b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
774c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7759b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
7769b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
7779b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
7789b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
7799b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
7809b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
781a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
78299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
78399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
78499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
7857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
7867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
787d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_None:          break;
788d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Auto:          return "auto"; break;
789d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Extern:        return "extern"; break;
790d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_PrivateExtern: return "__private_extern__"; break;
791d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Register:      return "register"; break;
792d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Static:        return "static"; break;
7937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
7947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
7967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
7977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
7987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7994afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
800a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
80116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
80216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
80399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
80499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
8051693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation VarDecl::getInnerLocStart() const {
80633e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
80733e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (Start.isInvalid())
80833e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    Start = getLocation();
8091693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return Start;
8101693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
8111693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
8121693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
81355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
8141693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
8151693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), getLocation());
81655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
81755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
8187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
8197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
8207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
8217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
822d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
8237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
8247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
8267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl       DC = DC->getParent()) {
8277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
8287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
829d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
8307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
8327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
8337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (DC->isFunctionOrMethod())
8357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      return false;
8367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
8377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
8397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
8427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
8437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
845e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
846e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
847e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
848e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
849e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
850e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
851e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
852e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
853e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
854e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
855e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
856e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
857e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
858e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
859e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
860e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
861e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
862e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
863e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
864e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
865e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
866e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
867e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
868e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
869e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
870e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
871e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
872e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
8732bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
874d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
875d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
8762bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
8772bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
8782bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
8792bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
8802bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
8812bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
882e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
883e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
884e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
885e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
886e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
887e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
888e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
889e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
890e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
891e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
892e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
893e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
894e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
895e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
896e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
897e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
898e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
899e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
900f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
901e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
902e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
903e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
904e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
905e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
906e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
907e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
908e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
909e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
910e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
911e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
912e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
913e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
914e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
915e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
916e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
917e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
918e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
919e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
920e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
921e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
92231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
92331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
92431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
92531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
926e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
927e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
928e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
92931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
93031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
93131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
93231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
933e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
934e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
93531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
9367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
9377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
9387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
9397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
94131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
9427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
9437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
9457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9471028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
9481028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Decl::isOutOfLine())
9491028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
9508761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
9518761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
9528761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
9538761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
9541028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
9551028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
9561028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
9571028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
9581028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
9591028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
9601028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
9611028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
9621028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
9630d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
9640d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
9650d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
9660d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
9670d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
9680d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
9690d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
9700d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
9710d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
9720d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
9730d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
9740d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
9750d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
976838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
9777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
9787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
979838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
9807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
9837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9851028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
986b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
987251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
988251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
989251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
990251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
991251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
992663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
993e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
994251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
995251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
996251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
997251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
998251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
9991028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1000b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1001b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1002b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
10030a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
10040a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1005b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1006251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1007251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
10080a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
10090a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
10100a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
10110a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
10127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
10137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
10157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
10167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1017275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
10187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
10197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
10207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
102116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
102216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
102316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
102416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1025275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1026275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
10277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
10287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
10297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
10307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
10317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
10337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
10347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
10357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
10377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
10407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const CXXExprWithTemporaries *E =
10417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        dyn_cast<CXXExprWithTemporaries>(getInit()))
10427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
1043275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
1044c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
1045275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1046275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
10477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
10487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
10497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
10507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
10527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
10537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
10567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
10577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
10587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
10607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
10617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1063fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1064fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
106599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
10668a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
10678a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
10688a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1069136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCallvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1070136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        const PrintingPolicy &Policy,
1071136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        bool Qualified) const {
1072136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1073136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1074136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  if (TemplateArgs)
1075136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall    S += TemplateSpecializationType::PrintTemplateArgumentList(
1076136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->getFlatArgumentList(),
1077136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->flat_size(),
1078136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                                               Policy);
1079136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
1080136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall}
108127f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
10829498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
10839498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
10849498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
10859498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
10869498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
10879498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
108806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
108906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
109006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (I->Body) {
109106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
109206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
109306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
109406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
109506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
109606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
109706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
109806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
10996fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1100c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1101c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1102c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1103c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
1104f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1105f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1106f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1107f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
111055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
111155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
11121a5364e0fa0482d8d477d6f136d52e503bbe13f4Argyrios Kyrtzidis  if (B)
111355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
111455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
111555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
11162138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
11172138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
11182138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
11192138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
11202138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
11212138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
11222138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
112348a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
112448a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
112507a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
11267a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    getDeclContext()->getRedeclContext()->isTranslationUnit() &&
112704495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
112804495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
112904495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
113048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
113148a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
11326393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
11336393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
11346393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1135d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
11366393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
11371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
11386393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor       DC = DC->getParent()) {
11396393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
11406393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1141d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
114240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
11436393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
11446393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
11456393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
114645975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor
114745975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor    if (DC->isRecord())
114845975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor      break;
11496393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
11506393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
11510bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
11526393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
11536393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
11548499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
11558499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
11568499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
11578499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1158d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
11598499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
11608499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
11611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
11628499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
11638499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
11648499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
11658499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
11668499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
11678499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
11688499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
11698499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
11708499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
11718499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
11728499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
11738499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
11747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
11757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
11767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
11777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
11797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
11807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
11817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
11827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
11837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
11877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
11887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
11917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
11927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11943e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
11953e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
11963e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
11973e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
11983e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
11993e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
12003e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
12011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
12023e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
12037814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
12047814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
12053c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
12063c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
12073c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12083c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
12093c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
12103c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
12113c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12123c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
12133c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
12143c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
12153c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12169add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1217d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
12189add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
12199add31798f621f843233dbff8bba103fca64447bDouglas Gregor
12203c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
12213c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
12223c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
12233c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
12243c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
12253c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12263c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
12273c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
12283c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
12291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
12303c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
123140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
12323c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
12333c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
12343c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
12353e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
12363e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
12373e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
12383e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
12391ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
12402dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner/// based on its FunctionType.  This is the length of the PararmInfo array
12411ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
12421ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1243183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
124472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1245d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
124672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
12471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12506b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
12516b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                             ParmVarDecl **NewParamInfo, unsigned NumParams) {
12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
12532dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
12541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
12565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
12576b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1258fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
12595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
126055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
126196888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
126296888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1263cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
126455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
12655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12688123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
12698123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
12708123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
12719e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner/// arguments (in C++).
12728123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
12738123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  unsigned NumRequiredArgs = getNumParams();
12748123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  while (NumRequiredArgs > 0
1275ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
12768123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
12778123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
12788123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
12798123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
12808123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
12817ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
128248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // FIXME: This is not enough. Consider:
128348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
128448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // inline void f();
128548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // void f() { }
128648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
128748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // f is inlined, but does not have inline specified.
128848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // To fix this we should add an 'inline' flag to FunctionDecl.
128948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isInlineSpecified())
12907d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
129148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
129248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
129348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
129448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
129548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
12967d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
12977d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
12987d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
12997d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
13007d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
13017d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
13027d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
13037d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
13047d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
13057d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
13067d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
13077d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
13087d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
13097d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
131006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
13117d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
131206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
13137d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
131406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
13157d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
13167d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
13177d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
13187ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
13197ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
13207d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
13211fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
13221fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
13231fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
13241fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
13251fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
13261fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
13271fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
13281fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
13291e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
13301fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
13311fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
13321fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
13331fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
13341fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
13351fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
13361fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
13371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
13381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
13397ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
13407d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
13411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
13427d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
13431fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU inline semantics. Based on a number of examples, we came up with the
13441fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // following heuristic: if the "inline" keyword is present on a
13451fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration of the function but "extern" is not present on that
13461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration, then the symbol is externally visible. Otherwise, the GNU
13471fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // "extern inline" semantics applies and the symbol is not externally
13481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // visible.
13491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
13501fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
13511fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
1352d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
13531fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
13541fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    }
13551fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
13561fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU "extern inline" semantics; no externally visible symbol.
13579f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
13581fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
13591fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
13601fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
13611fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
13621fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
13631fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
13641fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
13651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
13661fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
13671fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
13681fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
13691fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
13701fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1371d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
13721fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
13731fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
13741fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
13751fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
13761fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
13771fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
13781fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
13799f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
13809f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
13819f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
13821cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
13831cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
13841cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1385e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1386e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
13871cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
13881cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
13891cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
13901cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1391a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1392a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1393a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1394a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1395a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1396a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1397a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1398a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1399a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1400d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1401d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1402d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1403d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1404d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1405d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1406d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1407d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1408d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1409d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1410d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1411d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1412d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1413d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1414d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1415d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1416d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
14172db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1418b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
14192db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
14202db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
14212db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
14222db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
14232db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1424b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1425b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1426b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1427b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
14282db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
14296b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
14306b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
14312db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
14322db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
14332db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
14342db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
14356b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
14362db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
14372db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
14382db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
14393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
14406cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
14416cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
14423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
14433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
14453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
14463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
14473b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
14483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
14493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
14513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
14523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
14543b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
14553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
14563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
14573b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14583b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
14593b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
146006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
14613b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
146206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
14633b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
14653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
14663b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
14673b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
146806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
14693b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
14703b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14717ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
14723b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
14733b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14743b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
14753b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
14763b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
14773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
14783b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
14793b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
14803b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
14813b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14823b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
14833b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
14843b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14853b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
14863b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
14873b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
14883b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
14893b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
14903b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
149116e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
149316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
149416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
14951fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
149616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
149716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
149816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
149916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
150016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
150116e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
15021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1503fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1504fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
150516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
150616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
150716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
150816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
150916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1510e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1511e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1512e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1513e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1514e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1515e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1516e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1517e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1518e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1519e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
15216b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
15226b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
1523127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1524b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1525e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
15267b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
15277b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1528b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1529b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
15301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
153116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
15321637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1533a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1534a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
1535a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
1536a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
15371637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1539127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1540b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1541b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1542b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1543b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
15442c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
15452c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
15462c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
15472c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
1548b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1549b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
15502c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
15512c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
15522c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
1553b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
15541637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
15551637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1556af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1557af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1558af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1559af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1560af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1561af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1562af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
156321c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1564af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1565af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1566af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1567af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1568af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1569af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1570af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1571af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1572af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1573af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1574af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1575af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1576af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1577af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1578af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1579af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1580af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1581af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1582af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1583af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1584af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1585af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1586af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1587af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1588af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1589af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1590d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
15911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1592d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
15932db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
15941fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
15952db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
15962db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1597d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
15982db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
15992db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
16002db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
16012db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
16022db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
16032db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
16041fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
16051fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
16061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
16070a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
16080a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
16092db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
16102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
16110a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
16122db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
16130a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
16140a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
16150a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
16160a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
16170a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
16180a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
16192db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
16200a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
16210a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
16220a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
16230a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
16240a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
16252db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
16261fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
16271fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
16280a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
16290a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
16300a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
16310a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
16320a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
16330a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
16340a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
16350a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
16360a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
16370a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
16380a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
16390a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
16409f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
16419f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (Decl::isOutOfLine())
16429f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
16439f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
16449f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
16459f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
16469f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
16479f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
164806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
16499f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
16509f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
16519f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
16529f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
16539f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
16549f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
16559f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
165606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
16579f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
16589f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
16599f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
16609f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
16619f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
16629f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
16638a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
16647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
16657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
16667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
16687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             IdentifierInfo *Id, QualType T,
16697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
16707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
16717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
16747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
16757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
16767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
16787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
16797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
16817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1684bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
16854b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
16864b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
16871693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
16881693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
16891693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
16901693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1691f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1692f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
16931693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
1694f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1695f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1696b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
16978e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1698b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1699b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
170060e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
170160e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
170260e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
170360e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor    TypeForDecl->ClearLinkageCache();
170460e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
170560e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
17060b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
1707ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
170886ff308724171494395a840fd2efbe25e62f352eJohn McCall
170986ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
171086ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
171186ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
171286ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
17132243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
17142243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
171586ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
17160b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
17170b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
17180b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
17195cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
17205cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
17215cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
17225cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
17230b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
1724ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
1725565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
1726565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
1727565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
17280b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
17290b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
1730952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
17318e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
17328e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
1733220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1734220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
17351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
17378e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
17388e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
17398e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
17401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17418e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
17424b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
17434b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1744b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1745b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                               SourceRange QualifierRange) {
1746b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
1747b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
1748b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
1749b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1750b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1751b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
1752b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
1753b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1754b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
1755b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1756b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
1757b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
1758b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
1759b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
1760b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1761b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1762b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1763b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
17644b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
17657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
17667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
17677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
17697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
17701274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                           EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
17711274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
17721274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                    IsScoped, IsFixed);
17737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
17747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
17757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1777b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
17781274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
17791274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                          false, false);
1780b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1781b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1782838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
17831b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
17841b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
17851b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
17867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
17871274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
17881274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
17897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
17901b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
17911b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
17927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
17937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
17968a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
17978a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
17985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
179935bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
18008e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
18018e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
18028e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
18036359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
1804bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
1805082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
1806eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
18076359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
18086359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
18096359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
18106359792ca92e7ca2f416cb804c6604358174e994Ted KremenekRecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
18114b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
1812741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
18131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18148e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
18154b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
18164b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
18176359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
18186359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
1819b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisRecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1820b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1821b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis                            SourceLocation());
1822b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1823b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1824c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
18251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1826c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1827c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
1828c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
1829eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
1830eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1831eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
1832eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1833eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
1834eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
1835eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
183644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// completeDefinition - Notes that the definition of this type is now
183744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// complete.
1838838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid RecordDecl::completeDefinition() {
18395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!isDefinition() && "Cannot redefine record!");
18400b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  TagDecl::completeDefinition();
18415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1843bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCallValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1844bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  // Force the decl chain to come into existence properly.
1845bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  if (!getNextDeclInContext()) getParent()->decls_begin();
1846bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
1847bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(isAnonymousStructOrUnion());
1848bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1849bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->isRecordType());
1850bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1851bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  return D;
1852bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall}
1853bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
1854eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
1855eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
1856eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
1857eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1858eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
1859eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
1860eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1861eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<Decl*, 64> Decls;
1862eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1863eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
1864eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1865eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
1866eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
1867eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
1868eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
1869eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
1870eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1871eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = true;
1872eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1873eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
1874eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
1875eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1876eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1877eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
1878eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
187956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
188056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
188156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
188256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
1883838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
1884e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
1885e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
18861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1887e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
1888e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
1889e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
1890838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
1891e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1892e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1893e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
1894e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
1895e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1896e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffunsigned BlockDecl::getNumParams() const {
1897e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  return NumParams;
1898e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
18997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
19037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
19067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
19077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
19107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
19117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
19127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
191406c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
191506c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
191606c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
191706c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
191806c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
19197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
19207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    SourceLocation L, IdentifierInfo *Id, QualType T) {
19217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
19227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
19252577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
19262577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
192716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   StorageClass S, StorageClass SCAsWritten,
192816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   bool isInline, bool hasWrittenPrototype) {
19292577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
193016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                           S, SCAsWritten, isInline);
19317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
19327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
19337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
19367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
19377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
19407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
19417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
19427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
19437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
19447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19468e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
19478e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
19488e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
19498e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
19508e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
19518e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
19528e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
19537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
19547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
19557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
19567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
19577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
19607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
19617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
19627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
19637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
1964