Decl.cpp revision 7f1b98760d419a09b2261c1ef901f6bc1ff33e19
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
367f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCallstatic const VisibilityAttr *GetExplicitVisibility(const Decl *d) {
377f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // Use the most recent declaration of a variable.
387f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  if (const VarDecl *var = dyn_cast<VarDecl>(d))
397f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    return var->getMostRecentDeclaration()->getAttr<VisibilityAttr>();
407f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
417f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // Use the most recent declaration of a function, and also handle
427f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // function template specializations.
437f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(d)) {
447f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    if (const VisibilityAttr *attr
457f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall          = fn->getMostRecentDeclaration()->getAttr<VisibilityAttr>())
467f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall      return attr;
477f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
487f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    // If the function is a specialization of a template with an
497f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    // explicit visibility attribute, use that.
507f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    if (FunctionTemplateSpecializationInfo *templateInfo
517f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall          = fn->getTemplateSpecializationInfo())
527f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall      return templateInfo->getTemplate()->getTemplatedDecl()
537f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall        ->getAttr<VisibilityAttr>();
547f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
557f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    return 0;
56e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  }
577f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
587f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // Otherwise, just check the declaration itself first.
597f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  if (const VisibilityAttr *attr = d->getAttr<VisibilityAttr>())
607f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    return attr;
617f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
627f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // If there wasn't explicit visibility there, and this is a
637f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // specialization of a class template, check for visibility
647f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // on the pattern.
657f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  if (const ClassTemplateSpecializationDecl *spec
667f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(d))
677f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    return spec->getSpecializedTemplate()->getTemplatedDecl()
687f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall      ->getAttr<VisibilityAttr>();
697f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
707f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  return 0;
71e7bc9722c807030409178d4af8ce8d1260bbd821John McCall}
72e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
731fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
741fb0caaa7bef765b85972274e3b434af2572c141John McCall  switch (A->getVisibility()) {
751fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Default:
761fb0caaa7bef765b85972274e3b434af2572c141John McCall    return DefaultVisibility;
771fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Hidden:
781fb0caaa7bef765b85972274e3b434af2572c141John McCall    return HiddenVisibility;
791fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Protected:
801fb0caaa7bef765b85972274e3b434af2572c141John McCall    return ProtectedVisibility;
811fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
821fb0caaa7bef765b85972274e3b434af2572c141John McCall  return DefaultVisibility;
831fb0caaa7bef765b85972274e3b434af2572c141John McCall}
841fb0caaa7bef765b85972274e3b434af2572c141John McCall
85af14603ca61757cf4361b583b45639a04c57e651John McCalltypedef NamedDecl::LinkageInfo LinkageInfo;
861fb0caaa7bef765b85972274e3b434af2572c141John McCalltypedef std::pair<Linkage,Visibility> LVPair;
87af14603ca61757cf4361b583b45639a04c57e651John McCall
881fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair merge(LVPair L, LVPair R) {
891fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LVPair(minLinkage(L.first, R.first),
901fb0caaa7bef765b85972274e3b434af2572c141John McCall                minVisibility(L.second, R.second));
911fb0caaa7bef765b85972274e3b434af2572c141John McCall}
921fb0caaa7bef765b85972274e3b434af2572c141John McCall
93af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair merge(LVPair L, LinkageInfo R) {
94af14603ca61757cf4361b583b45639a04c57e651John McCall  return LVPair(minLinkage(L.first, R.linkage()),
95af14603ca61757cf4361b583b45639a04c57e651John McCall                minVisibility(L.second, R.visibility()));
96af14603ca61757cf4361b583b45639a04c57e651John McCall}
97af14603ca61757cf4361b583b45639a04c57e651John McCall
98752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramernamespace {
993698748400478880d2a146ef9eaa111cd0e60522John McCall/// Flags controlling the computation of linkage and visibility.
1003698748400478880d2a146ef9eaa111cd0e60522John McCallstruct LVFlags {
1013698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderGlobalVisibility;
1023698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderVisibilityAttributes;
1033698748400478880d2a146ef9eaa111cd0e60522John McCall
1043698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags() : ConsiderGlobalVisibility(true),
1053698748400478880d2a146ef9eaa111cd0e60522John McCall              ConsiderVisibilityAttributes(true) {
1063698748400478880d2a146ef9eaa111cd0e60522John McCall  }
1073698748400478880d2a146ef9eaa111cd0e60522John McCall
108381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// \brief Returns a set of flags that is only useful for computing the
109381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// linkage, not the visibility, of a declaration.
110381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  static LVFlags CreateOnlyDeclLinkage() {
111381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LVFlags F;
112381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderGlobalVisibility = false;
113381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderVisibilityAttributes = false;
114381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return F;
115381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
116381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1173698748400478880d2a146ef9eaa111cd0e60522John McCall  /// Returns a set of flags, otherwise based on these, which ignores
1183698748400478880d2a146ef9eaa111cd0e60522John McCall  /// off all sources of visibility except template arguments.
1193698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags onlyTemplateVisibility() const {
1203698748400478880d2a146ef9eaa111cd0e60522John McCall    LVFlags F = *this;
1213698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderGlobalVisibility = false;
1223698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderVisibilityAttributes = false;
1233698748400478880d2a146ef9eaa111cd0e60522John McCall    return F;
1243698748400478880d2a146ef9eaa111cd0e60522John McCall  }
12589d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor};
126752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramer} // end anonymous namespace
1273698748400478880d2a146ef9eaa111cd0e60522John McCall
1280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
1290b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
1301fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair
1311fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) {
1321fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
1350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
1360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
1381fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
1390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
1400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
1410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
1430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
144af14603ca61757cf4361b583b45639a04c57e651John McCall      LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
1450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1460b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1481fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
151381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor/// getLVForDecl - Get the linkage and visibility for the given declaration.
152381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
153381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
1550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
1561fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
157381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           unsigned NumArgs,
158381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           LVFlags &F) {
1591fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
1630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
1640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
1650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
1660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
1691fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
1700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
1731fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
1741fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
1751fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
17689d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
17789d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor          LV = merge(LV, getLVForDecl(ND, F));
1781fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
18289d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor      if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
18389d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        LV = merge(LV, getLVForDecl(Template, F));
1840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
1871fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
188381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  Args[I].pack_size(),
189381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  F));
1900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1941fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1960b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
197af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair
198381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
199381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                             LVFlags &F) {
200381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
2013cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
2023cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2033698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
2047a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
205d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
206d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
209d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
210d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
211d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
216d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
217af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
224e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
225d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
226d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
227d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
228d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
229d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
2310b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
232d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
233d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
235af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::internal();
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
237d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
2380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
2390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
2400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
241d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
242d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
244d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
245d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
246d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
247d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
248d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
249d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
250af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo(InternalLinkage, DefaultVisibility, false);
251d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
252d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
253d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
254af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
255d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
256d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2571fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (D->isInAnonymousNamespace())
258af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
259e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2601fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2611fb0caaa7bef765b85972274e3b434af2572c141John McCall
2621fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2631fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2641fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2651fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
266af14603ca61757cf4361b583b45639a04c57e651John McCall  LinkageInfo LV;
267af14603ca61757cf4361b583b45639a04c57e651John McCall
2683698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
2693698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
2703698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.setVisibility(GetVisibilityFromAttr(VA), true);
2713698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
27290f1450c109fbbd333001165bbd986061f7c4513John McCall    } else {
27390f1450c109fbbd333001165bbd986061f7c4513John McCall      // If we're declared in a namespace with a visibility attribute,
27490f1450c109fbbd333001165bbd986061f7c4513John McCall      // use that namespace's visibility, but don't call it explicit.
27590f1450c109fbbd333001165bbd986061f7c4513John McCall      for (const DeclContext *DC = D->getDeclContext();
27690f1450c109fbbd333001165bbd986061f7c4513John McCall           !isa<TranslationUnitDecl>(DC);
27790f1450c109fbbd333001165bbd986061f7c4513John McCall           DC = DC->getParent()) {
27890f1450c109fbbd333001165bbd986061f7c4513John McCall        if (!isa<NamespaceDecl>(DC)) continue;
27990f1450c109fbbd333001165bbd986061f7c4513John McCall        if (const VisibilityAttr *VA =
28090f1450c109fbbd333001165bbd986061f7c4513John McCall              cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
28190f1450c109fbbd333001165bbd986061f7c4513John McCall          LV.setVisibility(GetVisibilityFromAttr(VA), false);
28290f1450c109fbbd333001165bbd986061f7c4513John McCall          F.ConsiderGlobalVisibility = false;
28390f1450c109fbbd333001165bbd986061f7c4513John McCall          break;
28490f1450c109fbbd333001165bbd986061f7c4513John McCall        }
28590f1450c109fbbd333001165bbd986061f7c4513John McCall      }
2863698748400478880d2a146ef9eaa111cd0e60522John McCall    }
287af14603ca61757cf4361b583b45639a04c57e651John McCall  }
2881fb0caaa7bef765b85972274e3b434af2572c141John McCall
289d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
2901fb0caaa7bef765b85972274e3b434af2572c141John McCall
291d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
292d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
293d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
294d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
295d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
296110e8e56af30363072c140285961592b0107f789John McCall    // GCC applies the following optimization to variables and static
297110e8e56af30363072c140285961592b0107f789John McCall    // data members, but not to functions:
298110e8e56af30363072c140285961592b0107f789John McCall    //
2991fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
3001fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
3011fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
3021fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
3031fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
3041fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
3051fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
3061fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
3071fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
3081fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
3091fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
3101fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
3111fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
312ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
313ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
314ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
3151fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
3161fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
317ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
3181fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
3191fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
320af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
321af14603ca61757cf4361b583b45639a04c57e651John McCall      if (!LV.visibilityExplicit())
322af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(TypeLV.second);
323110e8e56af30363072c140285961592b0107f789John McCall    }
324110e8e56af30363072c140285961592b0107f789John McCall
32535cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Var->getStorageClass() == SC_PrivateExtern)
32635cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
32735cebc3eea898637057b10b5cf7dd08b1d788980John McCall
328d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
329d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
330d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
3311fb0caaa7bef765b85972274e3b434af2572c141John McCall
332d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
333d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
334d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
335d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
336d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
337d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
338d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
339d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
340d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
341d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
342381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
343af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
344af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
345d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
346d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
347d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
348d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
3491fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
35067fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
35167fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
35267fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
35367fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
3541fb0caaa7bef765b85972274e3b434af2572c141John McCall
35535cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Function->getStorageClass() == SC_PrivateExtern)
35635cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
35735cebc3eea898637057b10b5cf7dd08b1d788980John McCall
358d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
359d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
360d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
361d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
362d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
363d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
364d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
365d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
366d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
367d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
368d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
369d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
370d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
371d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
372d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
373d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
374d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
375d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
376d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
377381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
378af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
379af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
380d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
381d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
382d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
3840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
3853698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(SpecInfo->getTemplate(),
3863698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
3870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
388381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
3890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
3900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
391d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
392d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
393d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
394d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
395d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
396d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
3971fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
3981fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
3991fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
400af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
4011fb0caaa7bef765b85972274e3b434af2572c141John McCall
4021fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
4031fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
4041fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (const ClassTemplateSpecializationDecl *Spec
4051fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
4063698748400478880d2a146ef9eaa111cd0e60522John McCall      // From the template.
4073698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
4083698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
4091fb0caaa7bef765b85972274e3b434af2572c141John McCall
4101fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The arguments at which the template was instantiated.
4111fb0caaa7bef765b85972274e3b434af2572c141John McCall      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
412381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4130b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
414d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
415ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
4163698748400478880d2a146ef9eaa111cd0e60522John McCall    if (F.ConsiderGlobalVisibility)
4173698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility =
418ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
419ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
4201fb0caaa7bef765b85972274e3b434af2572c141John McCall
421d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
4221fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
423381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
424af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!isExternalLinkage(EnumLV.linkage()))
425af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
426af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(EnumLV);
427d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
428d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
429d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
4301fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
431af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
4320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
433d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
434d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
4351fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
4361fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
4371fb0caaa7bef765b85972274e3b434af2572c141John McCall
4381fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
4391fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
4401fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
4411fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
4421fb0caaa7bef765b85972274e3b434af2572c141John McCall
4431fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
4441fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
445af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4461fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4471fb0caaa7bef765b85972274e3b434af2572c141John McCall
4481fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
4491fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
450af14603ca61757cf4361b583b45639a04c57e651John McCall  if (LV.linkage() != ExternalLinkage)
451af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo(LV.linkage(), DefaultVisibility, false);
4521fb0caaa7bef765b85972274e3b434af2572c141John McCall
4531fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
4541fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
4553698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility)
456af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
457d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4581fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
459d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
460d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4613698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
4621fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
4631fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
4641fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
4651fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
4663cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
4673cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
4681fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
4693cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
4703cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall         (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
471af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4723cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4733698748400478880d2a146ef9eaa111cd0e60522John McCall  LinkageInfo LV;
4743698748400478880d2a146ef9eaa111cd0e60522John McCall
4753698748400478880d2a146ef9eaa111cd0e60522John McCall  // The flags we're going to use to compute the class's visibility.
4763698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags ClassF = F;
4773698748400478880d2a146ef9eaa111cd0e60522John McCall
4783698748400478880d2a146ef9eaa111cd0e60522John McCall  // If we have an explicit visibility attribute, merge that in.
4793698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
4803698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
4813698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
4823698748400478880d2a146ef9eaa111cd0e60522John McCall
4833698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore global visibility later, but not this attribute.
4843698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
4853698748400478880d2a146ef9eaa111cd0e60522John McCall
4863698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore both global visibility and attributes when computing our
4873698748400478880d2a146ef9eaa111cd0e60522John McCall      // parent's visibility.
4883698748400478880d2a146ef9eaa111cd0e60522John McCall      ClassF = F.onlyTemplateVisibility();
4893698748400478880d2a146ef9eaa111cd0e60522John McCall    }
4903698748400478880d2a146ef9eaa111cd0e60522John McCall  }
491af14603ca61757cf4361b583b45639a04c57e651John McCall
492af14603ca61757cf4361b583b45639a04c57e651John McCall  // Class members only have linkage if their class has external
4933698748400478880d2a146ef9eaa111cd0e60522John McCall  // linkage.
4943698748400478880d2a146ef9eaa111cd0e60522John McCall  LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
4953698748400478880d2a146ef9eaa111cd0e60522John McCall  if (!isExternalLinkage(LV.linkage()))
496af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4973cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4983cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
4993698748400478880d2a146ef9eaa111cd0e60522John McCall  if (LV.linkage() == UniqueExternalLinkage)
500af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
5011fb0caaa7bef765b85972274e3b434af2572c141John McCall
5023cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
503110e8e56af30363072c140285961592b0107f789John McCall    TemplateSpecializationKind TSK = TSK_Undeclared;
504110e8e56af30363072c140285961592b0107f789John McCall
5051fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
5061fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
5071fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (FunctionTemplateSpecializationInfo *Spec
5083cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
509381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
510af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateParameterList(
5111fb0caaa7bef765b85972274e3b434af2572c141John McCall                              Spec->getTemplate()->getTemplateParameters()));
512110e8e56af30363072c140285961592b0107f789John McCall
513110e8e56af30363072c140285961592b0107f789John McCall      TSK = Spec->getTemplateSpecializationKind();
514110e8e56af30363072c140285961592b0107f789John McCall    } else if (MemberSpecializationInfo *MSI =
515110e8e56af30363072c140285961592b0107f789John McCall                 MD->getMemberSpecializationInfo()) {
516110e8e56af30363072c140285961592b0107f789John McCall      TSK = MSI->getTemplateSpecializationKind();
5173cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
5183cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
519110e8e56af30363072c140285961592b0107f789John McCall    // If we're paying attention to global visibility, apply
520110e8e56af30363072c140285961592b0107f789John McCall    // -finline-visibility-hidden if this is an inline method.
521110e8e56af30363072c140285961592b0107f789John McCall    //
522af14603ca61757cf4361b583b45639a04c57e651John McCall    // Note that ConsiderGlobalVisibility doesn't yet have information
523af14603ca61757cf4361b583b45639a04c57e651John McCall    // about whether containing classes have visibility attributes,
524af14603ca61757cf4361b583b45639a04c57e651John McCall    // and that's intentional.
525af14603ca61757cf4361b583b45639a04c57e651John McCall    if (TSK != TSK_ExplicitInstantiationDeclaration &&
5263698748400478880d2a146ef9eaa111cd0e60522John McCall        F.ConsiderGlobalVisibility &&
52766cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
52866cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // InlineVisibilityHidden only applies to definitions, and
52966cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // isInlined() only gives meaningful answers on definitions
53066cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // anyway.
53166cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      const FunctionDecl *Def = 0;
53266cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      if (MD->hasBody(Def) && Def->isInlined())
53366cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        LV.setVisibility(HiddenVisibility);
53466cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall    }
5351fb0caaa7bef765b85972274e3b434af2572c141John McCall
536110e8e56af30363072c140285961592b0107f789John McCall    // Note that in contrast to basically every other situation, we
537110e8e56af30363072c140285961592b0107f789John McCall    // *do* apply -fvisibility to method declarations.
538110e8e56af30363072c140285961592b0107f789John McCall
539110e8e56af30363072c140285961592b0107f789John McCall  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
540110e8e56af30363072c140285961592b0107f789John McCall    if (const ClassTemplateSpecializationDecl *Spec
541110e8e56af30363072c140285961592b0107f789John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
542110e8e56af30363072c140285961592b0107f789John McCall      // Merge template argument/parameter information for member
543110e8e56af30363072c140285961592b0107f789John McCall      // class template specializations.
544381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
545af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateParameterList(
5461fb0caaa7bef765b85972274e3b434af2572c141John McCall                    Spec->getSpecializedTemplate()->getTemplateParameters()));
547110e8e56af30363072c140285961592b0107f789John McCall    }
548110e8e56af30363072c140285961592b0107f789John McCall
549110e8e56af30363072c140285961592b0107f789John McCall  // Static data members.
550110e8e56af30363072c140285961592b0107f789John McCall  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
551ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // Modify the variable's linkage by its type, but ignore the
552ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // type's visibility unless it's a definition.
553ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
554ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (TypeLV.first != ExternalLinkage)
555af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeLinkage(UniqueExternalLinkage);
556af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!LV.visibilityExplicit())
557af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeVisibility(TypeLV.second);
558110e8e56af30363072c140285961592b0107f789John McCall  }
559110e8e56af30363072c140285961592b0107f789John McCall
5603698748400478880d2a146ef9eaa111cd0e60522John McCall  F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
561110e8e56af30363072c140285961592b0107f789John McCall
562110e8e56af30363072c140285961592b0107f789John McCall  // Apply -fvisibility if desired.
5633698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
564af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
5653cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
5663cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5671fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
5683cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
5693cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
570381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorLinkage NamedDecl::getLinkage() const {
571381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (HasCachedLinkage) {
57256ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer    assert(Linkage(CachedLinkage) ==
57356ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer             getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
574381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return Linkage(CachedLinkage);
575381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
576381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
577381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = getLVForDecl(this,
578381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                               LVFlags::CreateOnlyDeclLinkage()).linkage();
579381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
580381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return Linkage(CachedLinkage);
581381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
582381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
583af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const {
584381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  LinkageInfo LI = getLVForDecl(this, LVFlags());
58556ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer  assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
586381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
587381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = LI.linkage();
588381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return LI;
5890df9587ab011c12968fcbe3518666b2117afe350John McCall}
590becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
5913698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
592becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
593becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
5940df9587ab011c12968fcbe3518666b2117afe350John McCall  switch (D->getKind()) {
595becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
596becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
5971fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
5981fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
599becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
600becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
601becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
602becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
603becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
604becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
605becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
606becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
607becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
608becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
609af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::external();
610becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
611becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
612d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
6130df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->getRedeclContext()->isFileContext())
6143698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForNamespaceScopeDecl(D, Flags);
615d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
616d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
617d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
618d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
619d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
620d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
621d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
622d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
6230df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->isRecord())
6243698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForClassMember(D, Flags);
625d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
626d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
627d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
628d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
629d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
630d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
631d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
632d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
633d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
634d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
635d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
636d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
6370df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
6380df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
6390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Function->isInAnonymousNamespace())
640af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
6411fb0caaa7bef765b85972274e3b434af2572c141John McCall
642af14603ca61757cf4361b583b45639a04c57e651John McCall      LinkageInfo LV;
643381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      if (Flags.ConsiderVisibilityAttributes) {
644381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
645381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LV.setVisibility(GetVisibilityFromAttr(VA));
646381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      }
647381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
6481fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
649381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
650af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
651af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
6521fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
6530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
6541fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
655d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
656d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
6570df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const VarDecl *Var = dyn_cast<VarDecl>(D))
658d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
659d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
6600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Var->isInAnonymousNamespace())
661af14603ca61757cf4361b583b45639a04c57e651John McCall          return LinkageInfo::uniqueExternal();
6621fb0caaa7bef765b85972274e3b434af2572c141John McCall
663af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo LV;
6641fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
665af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(HiddenVisibility);
666381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        else if (Flags.ConsiderVisibilityAttributes) {
667381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
668381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor            LV.setVisibility(GetVisibilityFromAttr(VA));
669381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        }
670381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
6711fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
672381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
673af14603ca61757cf4361b583b45639a04c57e651John McCall          if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
674af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.mergeVisibility(PrevLV);
6751fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
6761fb0caaa7bef765b85972274e3b434af2572c141John McCall
6771fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
678d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
679d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
680d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
681d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
682d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
683af14603ca61757cf4361b583b45639a04c57e651John McCall  return LinkageInfo::none();
6841fb0caaa7bef765b85972274e3b434af2572c141John McCall}
685d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
68647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
6873a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
6883a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
6893a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
6903a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
69147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
69247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
69347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
69447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
69547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
69668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
69768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
69868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
69968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
70068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
70168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
70268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
70368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
70468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
70568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
70668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
70768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
70868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
70968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
71168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
712f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
713f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
714f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
715910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.data(),
716910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.size(),
7173a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
71868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
71968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
7206be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
72168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
7226be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
72368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
72468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
72568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
72668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
72768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
72868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
72968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
7303521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
7313521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
7323521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
7333521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
73468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
7353521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
7363521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
7373521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
7383521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
73968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
7403521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
7413521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
74268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
7433521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
7443521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
7453521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
7463521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
74768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
74868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
7493521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
7503521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
75168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
75268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
75368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
75468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
75568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
75647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
75747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
7588472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
75968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
7608472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
76168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
76247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
76368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
76447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
76547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
7664afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
7676ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
7686ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
7692a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
7702a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
7712a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
7722a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
7732a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
7742a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
7751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7766ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
7776ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
7786ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
7796ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
780e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
781e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
782e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
783e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
784e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
785e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
786e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
7871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7880de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
7890de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
7900de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
792f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
793f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
794f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
7959488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
7969488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
7979488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
7989488ea120e093068021f944176c3d610dd540914John McCall
799c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis  if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
800c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis    return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
801c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis           cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
802c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis
8036ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
8046ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
8056ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
8066ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
8076ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
8086ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
809d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
810d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
811d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
8124afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
813e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
814e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
815e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
8169488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
817e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
818e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
819e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
820e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
821e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
822e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
823e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
824e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
825e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
826161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
827161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
828161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
829161755a09898c95d21bfff33707da9ca41cd53c5John McCall
830161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
831161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
832161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
833161755a09898c95d21bfff33707da9ca41cd53c5John McCall
83487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
835161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
836161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
837161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
838161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
839161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
840161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
841161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
842161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
843161755a09898c95d21bfff33707da9ca41cd53c5John McCall
8445239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
845a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
846a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
847a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
8481693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
8491693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
8501693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
8511693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
8521693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
8531693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
8541693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
8551693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
856a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
8574e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
8584e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
859a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
860a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
861a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
862b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
863b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                      SourceRange QualifierRange) {
864b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
865b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
866b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
867b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
868b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
869b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
870b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
871b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
872b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
873b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
874b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
875b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
876b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
877b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
878b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
879b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
880b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
881b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
882b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
883b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
884b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
885b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
886b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
887b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
888b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
889b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
890b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
891b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
8921693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
8931693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
8941693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
8951693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
8969b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
897c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
898c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
8999b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
9009b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
9019b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
9029b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || NNS) &&
9039b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Nonempty array of template parameters with no qualifier!");
9049b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
9059b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
9069b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
907c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
9089b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
9099b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
9109b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
9119b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
9129b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
913c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
9149b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
9159b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
9169b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
9179b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
9189b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
9199b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
920a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
92199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
92299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
92399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
9247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
9257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
926d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_None:          break;
927d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Auto:          return "auto"; break;
928d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Extern:        return "extern"; break;
929d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_PrivateExtern: return "__private_extern__"; break;
930d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Register:      return "register"; break;
931d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Static:        return "static"; break;
9327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
9357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
9367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9384afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
939a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
94016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
94116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
94299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
94399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
944381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid VarDecl::setStorageClass(StorageClass SC) {
945381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForVariable(SC));
946381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
947381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
948381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
949381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
950381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
951381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
9521693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation VarDecl::getInnerLocStart() const {
95333e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
95433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (Start.isInvalid())
95533e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    Start = getLocation();
9561693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return Start;
9571693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9581693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
9591693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
96055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
9611693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
9621693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), getLocation());
96355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
96455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
9657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
9667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
9677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
9687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
969d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
9707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
9717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
9737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl       DC = DC->getParent()) {
9747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
9757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
976d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
9777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
9797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
9807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (DC->isFunctionOrMethod())
9827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      return false;
9837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
9867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
9897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
9907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
992e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
993e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
994e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
995e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
996e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
997e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
998e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
999e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
1000e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
1001e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
1002e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
1003e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1004e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
1005e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
1006e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
1007e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1008e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
1009e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
1010e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
1011e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
1012e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
1013e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
1014e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
1015e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
1016e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
1017e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
1018e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
1019e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
10202bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
1021d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
1022d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
10232bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
10242bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
10252bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
10262bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
10272bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
10282bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
1029e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
1030e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
1031e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
1032e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
1033e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
1034e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1035e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
1036e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1037e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
1038e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
1039e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
1040e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1041e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1042e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
1043e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1044e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1045e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
1046e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1047f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
1048e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
1049e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1050e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
1051e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
1052e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
1053e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
1054e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
1055e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
1056e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1057e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
1058e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1059e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1060e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
1061e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1062e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1063e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
1064e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1065e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1066e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
1067e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
1068e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
106931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
107031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
107131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
107231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
1073e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
1074e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1075e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
107631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
107731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
107831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
107931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
1080e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1081e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1082110e8e56af30363072c140285961592b0107f789John McCallVarDecl::DefinitionKind VarDecl::hasDefinition() const {
1083110e8e56af30363072c140285961592b0107f789John McCall  DefinitionKind Kind = DeclarationOnly;
1084110e8e56af30363072c140285961592b0107f789John McCall
1085110e8e56af30363072c140285961592b0107f789John McCall  const VarDecl *First = getFirstDeclaration();
1086110e8e56af30363072c140285961592b0107f789John McCall  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1087110e8e56af30363072c140285961592b0107f789John McCall       I != E; ++I)
1088110e8e56af30363072c140285961592b0107f789John McCall    Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1089110e8e56af30363072c140285961592b0107f789John McCall
1090110e8e56af30363072c140285961592b0107f789John McCall  return Kind;
1091110e8e56af30363072c140285961592b0107f789John McCall}
1092110e8e56af30363072c140285961592b0107f789John McCall
109331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
10947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
10957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
10967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
10977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
109931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
11007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
11017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
11037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11051028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
11061028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Decl::isOutOfLine())
11071028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
11088761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
11098761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
11108761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
11118761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
11121028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
11131028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
11141028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
11151028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
11161028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
11171028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
11181028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
11191028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
11201028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
11210d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
11220d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
11230d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
11240d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
11250d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
11260d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
11270d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
11280d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
11290d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
11300d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
11310d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
11320d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
11330d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
1134838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
11357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
11367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
1137838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
11387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
11417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11431028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1144b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1145251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1146251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1147251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1148251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1149251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1150663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1151e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1152251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1153251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1154251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1155251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1156251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
11571028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1158b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1159b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1160b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
11610a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
11620a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1163b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1164251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1165251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
11660a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
11670a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
11680a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
11690a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
11707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
11717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
11727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
11737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
11747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1175275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
11767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
11777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
11787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
117916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
118016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
118116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
118216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1183275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1184275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
11857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
11867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
11877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
11887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
11897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
11914765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
11927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
11937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
11957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
11984765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
11997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
1200275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
1201c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
1202275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1203275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
12047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
12057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
12067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
12077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12084765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
12097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
12107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
12137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
12147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
12157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
12177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
12187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1220fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1221fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
122299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
12238a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
12248a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
12258a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1226136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCallvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1227136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        const PrintingPolicy &Policy,
1228136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        bool Qualified) const {
1229136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1230136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1231136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  if (TemplateArgs)
1232136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall    S += TemplateSpecializationType::PrintTemplateArgumentList(
1233910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                         TemplateArgs->data(),
1234910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                         TemplateArgs->size(),
1235136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                                               Policy);
1236136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
1237136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall}
123827f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
12399498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
12409498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
12419498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
12429498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
12439498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
12449498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
124506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
124606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
124706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (I->Body) {
124806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
124906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
125006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
125106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
125206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
125306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
125406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
125506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
12566fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1257c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1258c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1259c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1260c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
1261f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1262f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1263f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1264f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
12655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
126755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
126855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
1269b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  if (B)
127055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
127155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
127255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
12732138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
12742138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
12752138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
12762138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
12772138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
12782138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
12792138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
128048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
128148a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
128207a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
12837a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    getDeclContext()->getRedeclContext()->isTranslationUnit() &&
128404495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
128504495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
128604495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
128748a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
128848a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
12896393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
12906393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
12916393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1292d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
12936393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
12941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
12956393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor       DC = DC->getParent()) {
12966393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
12976393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1298d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
129940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
13006393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13016393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
13026393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
130345975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor
130445975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor    if (DC->isRecord())
130545975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor      break;
13066393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
13076393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13080bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
13096393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
13106393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13118499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
13128499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
13138499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
13148499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1315d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
13168499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
13178499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
13181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
13198499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
13208499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
13218499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
13228499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
13238499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
13248499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
13258499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
13268499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
13278499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
13288499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
13298499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
13308499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
13317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
13327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
13337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
13347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
13367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
13377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
13387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
13397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
13407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
13418f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
13428f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (PrevDecl->IsInline)
13438f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    IsInline = true;
13447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
13477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
13487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
13517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
13527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1354381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid FunctionDecl::setStorageClass(StorageClass SC) {
1355381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForFunction(SC));
1356381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1357381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1358381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1359381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1360381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1361381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
13623e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
13633e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
13643e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
13653e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
13663e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
13673e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
13683e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
13691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
13703e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
13717814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
13727814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
13733c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
13743c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
13753c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13763c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
13773c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
13783c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
13793c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13803c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
13813c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
13823c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
13833c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13849add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1385d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
13869add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
13879add31798f621f843233dbff8bba103fca64447bDouglas Gregor
13883c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
13893c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
13903c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
13913c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
13923c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
13933c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13943c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
13953c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
13963c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
13971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
13983c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
139940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
14003c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
14013c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14023c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
14033e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
14043e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
14053e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
14063e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
14071ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
14082dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner/// based on its FunctionType.  This is the length of the PararmInfo array
14091ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
14101ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1411183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
141272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1413d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
141472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
14151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14186b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
14196b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                             ParmVarDecl **NewParamInfo, unsigned NumParams) {
14205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
14212dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
14245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
14256b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1426fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
142855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
142996888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
143096888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1431cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
143255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14368123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
14378123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
14388123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
14399e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner/// arguments (in C++).
14408123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
14418123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  unsigned NumRequiredArgs = getNumParams();
14428123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  while (NumRequiredArgs > 0
1443ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
14448123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
14458123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
14468123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
14478123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
14488123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
14497ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
14508f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (IsInline)
14517d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
145248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
145348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
145448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
145548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
145648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
14577d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
14587d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
14597d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
14607d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
14617d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
14627d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
14637d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
14647d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
14657d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
14667d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
14677d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
14687d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
14697d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
14707d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
147106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
14727d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
147306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
14747d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
147506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
14767d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
14777d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
14787d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
14797ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
14807ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
14817d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
14821fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
14831fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
14841fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
14851fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
14861fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
14871fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
14881fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
14891fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
14901e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
14911fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
14921fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
14931fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
14941fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
14951fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
14961fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
14971fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
14981fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
14991fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
15007ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
15017d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
15021fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
15037d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
15048f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If it's not the case that both 'inline' and 'extern' are
15058f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // specified on the definition, then this inline definition is
15068f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // externally visible.
15078f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
15088f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      return true;
15098f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
15108f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If any declaration is 'inline' but not 'extern', then this definition
15118f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // is externally visible.
15121fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
15131fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
15141fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
15158f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      if (Redecl->isInlineSpecified() &&
15168f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor          Redecl->getStorageClassAsWritten() != SC_Extern)
15171fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
15188f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    }
15191fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
15209f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
15211fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
15221fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
15231fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
15241fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
15251fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
15261fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
15271fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
15281fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
15291fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
15301fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
15311fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
15321fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
15331fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1534d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
15351fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
15361fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
15371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
15381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
15391fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
15401fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
15411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
15429f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
15439f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
15449f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
15451cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
15461cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
15471cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1548e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1549e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
15501cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
15511cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
15521cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
15531cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1554a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1555a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1556a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1557a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1558a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1559a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1560a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1561a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1562a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1563d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1564d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1565d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1566d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1567d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1568d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1569d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1570d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1571d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1572d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1573d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1574d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1575d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1576d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1577d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1578d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1579d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
15802db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1581b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
15822db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
15832db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
15842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
15852db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
15862db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1587b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1588b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1589b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1590b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
15912db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
15926b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
15936b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
15942db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
15952db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
15962db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
15972db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
15986b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
15992db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
16002db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
16012db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
16023b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
16036cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
16046cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
16053b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
16063b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16073b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
16083b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
16093b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
16103b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
16113b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
16123b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16133b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
16143b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
16153b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16163b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
16173b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
16183b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
16193b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
16203b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16213b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
16223b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
162306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
16243b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
162506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
16263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16273b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
16283b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
16293b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
16303b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
163106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
16323b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
16333b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16347ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
16353b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
16363b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
16383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
16393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
16403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
16413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
16423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
16433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
16443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
16463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
16473b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
16493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
16503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
16523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
16533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
165416e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
165616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
165716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
16581fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
165916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
166016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
166116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
166216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
166316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
166416e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
16651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1666fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1667fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
166816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
166916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
167016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
167116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
167216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1673e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1674e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1675e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1676e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1677e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1678e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1679e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1680e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1681e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1682e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
16831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
16846b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
16856b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
1686127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1687b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1688e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
16897b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
16907b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1691b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1692b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
16931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
169416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
16951637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1696a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1697a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
1698a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
1699a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
17001637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
17011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1702127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1703b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1704b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1705b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1706b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
17072c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
17082c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
17092c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
17102c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
1711b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1712b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
17132c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
17142c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
17152c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
1716b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
17171637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
17181637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1719af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1720af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1721af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1722af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1723af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1724af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1725af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
172621c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1727af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1728af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1729af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1730af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1731af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1732af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1733af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1734af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1735af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1736af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1737af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1738af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1739af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1740af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1741af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1742af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1743af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1744af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1745af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1746af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1747af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1748af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1749af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1750af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1751af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1752af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1753d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
17541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1755d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
17562db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
17571fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
17582db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
17592db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1760d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
17612db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
17622db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
17632db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
17642db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
17652db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
17662db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
17671fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
17681fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
17691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
17700a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
17710a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
17722db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
17732db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
17740a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
17752db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
17760a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
17770a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
17780a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
17790a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
17800a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
17810a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
17822db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
17830a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
17840a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
17850a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
17860a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
17870a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
17882db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
17891fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
17901fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
17910a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
17920a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
17930a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
17940a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
17950a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
17960a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
17970a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
17980a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
17990a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
18000a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
18010a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
18020a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
18039f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
18049f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (Decl::isOutOfLine())
18059f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
18069f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
18079f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
18089f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
18099f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
18109f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
181106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
18129f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
18139f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
18149f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
18159f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
18169f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
18179f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
18189f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
181906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
18209f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
18219f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
18229f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
18239f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
18249f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
18259f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
18268a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
18277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
18287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
18297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
18317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             IdentifierInfo *Id, QualType T,
18327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
18337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
18347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
18357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
18377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
18387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
18397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
18417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
18427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
18447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
18457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1847bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
18484b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
18494b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
18501693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
18511693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
18521693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
18531693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1854f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1855f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
18561693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
1857f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1858f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1859b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
18608e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1861b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1862b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
186360e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
186460e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
186560e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
186660e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor    TypeForDecl->ClearLinkageCache();
1867381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  ClearLinkageCache();
186860e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
186960e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
18700b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
1871ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
187286ff308724171494395a840fd2efbe25e62f352eJohn McCall
187386ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
187486ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
187586ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
187686ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
18772243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
18782243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
187986ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
18800b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
18810b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
18820b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
18835cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
18845cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
18855cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
18865cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
18870b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
1888ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
1889565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
1890565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
1891565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
18920b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
18930b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
1894952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
18958e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
18968e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
1897220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1898220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
18991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
19018e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
19028e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
19038e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
19041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19058e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
19064b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
19074b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1908b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1909b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                               SourceRange QualifierRange) {
1910b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
1911b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
1912b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
1913b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1914b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1915b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
1916b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
1917b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1918b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
1919b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1920b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
1921b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
1922b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
1923b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
1924b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1925b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1926b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1927b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
19284b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
19297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
19307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
19337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
1934a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           EnumDecl *PrevDecl, bool IsScoped,
1935a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           bool IsScopedUsingClassTag, bool IsFixed) {
19361274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1937a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    IsScoped, IsScopedUsingClassTag, IsFixed);
19387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
19397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
19407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1942b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
19431274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1944a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                          false, false, false);
1945b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1946b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1947838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
19481b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
19491b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
19501b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
19517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
19521274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
19531274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
19547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
19551b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
19561b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
19577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
19587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19618a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
19628a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
19635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
196435bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
19658e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
19668e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
19678e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
19686359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
1969bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
1970082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
1971eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
19726359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
19736359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
19746359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
19756359792ca92e7ca2f416cb804c6604358174e994Ted KremenekRecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
19764b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
1977741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
19781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19798e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
19804b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
19814b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
19826359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
19836359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
1984b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisRecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1985b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1986b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis                            SourceLocation());
1987b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1988b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1989c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
19901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1991c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1992c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
1993c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
1994eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
1995eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1996eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
1997eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1998eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
1999eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2000eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
200144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// completeDefinition - Notes that the definition of this type is now
200244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// complete.
2003838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid RecordDecl::completeDefinition() {
20045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!isDefinition() && "Cannot redefine record!");
20050b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  TagDecl::completeDefinition();
20065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2008bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCallValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
2009bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  // Force the decl chain to come into existence properly.
2010bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  if (!getNextDeclInContext()) getParent()->decls_begin();
2011bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
2012bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(isAnonymousStructOrUnion());
2013bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
2014bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->isRecordType());
2015bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->getAs<RecordType>()->getDecl() == this);
2016bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  return D;
2017bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall}
2018bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
2019eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
2020eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
2021eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
2022eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2023eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
2024eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
2025eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2026eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<Decl*, 64> Decls;
2027eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2028eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2029eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2030eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
2031eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
2032eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
2033eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
2034eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
2035eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2036eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = true;
2037eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2038eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
2039eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2040eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2041eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2042eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2043eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
204456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
204556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
204656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
204756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
2048838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
2049e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
2050e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
20511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2052e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
2053e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
2054e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
2055838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
2056e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2057e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2058e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
2059e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
2060e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
2061e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffunsigned BlockDecl::getNumParams() const {
2062e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  return NumParams;
2063e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
20647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
20677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
20687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
20697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
20717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
20727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
20757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
20767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
20777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
207906c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
208006c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
208106c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
208206c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
208306c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
20847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
20857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    SourceLocation L, IdentifierInfo *Id, QualType T) {
20867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
20877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
20902577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
20912577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
209216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   StorageClass S, StorageClass SCAsWritten,
20938f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool isInlineSpecified,
20948f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool hasWrittenPrototype) {
20952577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
20968f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                           S, SCAsWritten, isInlineSpecified);
20977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
20987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
20997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
21027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
21037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
21067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
21077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
21087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
21097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
21107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2112d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl *
2113d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2114d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          IdentifierInfo *Id, QualType T, NamedDecl **CH,
2115d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          unsigned CHS) {
211687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
211787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
211887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
21198e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
21208e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
21218e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
21228e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
21238e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
21248e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
21258e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
21267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
21277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
21287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
21297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
21307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
21337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
21347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
21357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
21367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
2137