Decl.cpp revision a7fc901a2e39bfe55bfcff5934b2d9fdf9656491
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:
182a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
183a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      if (TemplateDecl *Template
184a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
18589d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        LV = merge(LV, getLVForDecl(Template, F));
1860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
1891fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
190381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  Args[I].pack_size(),
191381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  F));
1920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1961fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1970b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1980b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
199af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair
200381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
201381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                             LVFlags &F) {
202381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
2033cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
2043cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2053698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
2067a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
209d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
210d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
211d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
218d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
219af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
224d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
225d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
226e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
227d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
228d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
229d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
231d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
232d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
2330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
237af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::internal();
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
2400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
2410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
2420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
244d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
245d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
246d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
247d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
248d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
249d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
250d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
251d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
252af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo(InternalLinkage, DefaultVisibility, false);
253d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
254d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
255d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
256af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
257d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
258d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2591fb0caaa7bef765b85972274e3b434af2572c141John McCall  if (D->isInAnonymousNamespace())
260af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
261e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2621fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2631fb0caaa7bef765b85972274e3b434af2572c141John McCall
2641fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2651fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2661fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2671fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
268af14603ca61757cf4361b583b45639a04c57e651John McCall  LinkageInfo LV;
269af14603ca61757cf4361b583b45639a04c57e651John McCall
2703698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
2713698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
2723698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.setVisibility(GetVisibilityFromAttr(VA), true);
2733698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
27490f1450c109fbbd333001165bbd986061f7c4513John McCall    } else {
27590f1450c109fbbd333001165bbd986061f7c4513John McCall      // If we're declared in a namespace with a visibility attribute,
27690f1450c109fbbd333001165bbd986061f7c4513John McCall      // use that namespace's visibility, but don't call it explicit.
27790f1450c109fbbd333001165bbd986061f7c4513John McCall      for (const DeclContext *DC = D->getDeclContext();
27890f1450c109fbbd333001165bbd986061f7c4513John McCall           !isa<TranslationUnitDecl>(DC);
27990f1450c109fbbd333001165bbd986061f7c4513John McCall           DC = DC->getParent()) {
28090f1450c109fbbd333001165bbd986061f7c4513John McCall        if (!isa<NamespaceDecl>(DC)) continue;
28190f1450c109fbbd333001165bbd986061f7c4513John McCall        if (const VisibilityAttr *VA =
28290f1450c109fbbd333001165bbd986061f7c4513John McCall              cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
28390f1450c109fbbd333001165bbd986061f7c4513John McCall          LV.setVisibility(GetVisibilityFromAttr(VA), false);
28490f1450c109fbbd333001165bbd986061f7c4513John McCall          F.ConsiderGlobalVisibility = false;
28590f1450c109fbbd333001165bbd986061f7c4513John McCall          break;
28690f1450c109fbbd333001165bbd986061f7c4513John McCall        }
28790f1450c109fbbd333001165bbd986061f7c4513John McCall      }
2883698748400478880d2a146ef9eaa111cd0e60522John McCall    }
289af14603ca61757cf4361b583b45639a04c57e651John McCall  }
2901fb0caaa7bef765b85972274e3b434af2572c141John McCall
291d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
2921fb0caaa7bef765b85972274e3b434af2572c141John McCall
293d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
294d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
295d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
296d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
297d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
298110e8e56af30363072c140285961592b0107f789John McCall    // GCC applies the following optimization to variables and static
299110e8e56af30363072c140285961592b0107f789John McCall    // data members, but not to functions:
300110e8e56af30363072c140285961592b0107f789John McCall    //
3011fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
3021fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
3031fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
3041fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
3051fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
3061fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
3071fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
3081fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
3091fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
3101fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
3111fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
3121fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
3131fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
314ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
315ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
316ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
3171fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
3181fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
319ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
3201fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
3211fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
322af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
323af14603ca61757cf4361b583b45639a04c57e651John McCall      if (!LV.visibilityExplicit())
324af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(TypeLV.second);
325110e8e56af30363072c140285961592b0107f789John McCall    }
326110e8e56af30363072c140285961592b0107f789John McCall
32735cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Var->getStorageClass() == SC_PrivateExtern)
32835cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
32935cebc3eea898637057b10b5cf7dd08b1d788980John McCall
330d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
331d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
332d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
3331fb0caaa7bef765b85972274e3b434af2572c141John McCall
334d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
335d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
336d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
337d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
338d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
339d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
340d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
341d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
342d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
343d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
344381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
345af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
346af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
347d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
348d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
349d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
350d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
3511fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
35267fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
35367fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
35467fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
35567fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
3561fb0caaa7bef765b85972274e3b434af2572c141John McCall
35735cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Function->getStorageClass() == SC_PrivateExtern)
35835cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
35935cebc3eea898637057b10b5cf7dd08b1d788980John McCall
360d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
361d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
362d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
363d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
364d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
365d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
366d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
367d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
368d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
369d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
370d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
371d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
372d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
373d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
374d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
375d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
376d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
377d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
378d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
379381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
380af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
381af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
382d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
383d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
384d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
3860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
3873698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(SpecInfo->getTemplate(),
3883698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
3890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
390381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
3910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
3920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
393d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
394d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
395d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
396d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
397d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
398d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
3991fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
4001fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
4011fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
402af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
4031fb0caaa7bef765b85972274e3b434af2572c141John McCall
4041fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
4051fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
4061fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (const ClassTemplateSpecializationDecl *Spec
4071fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
4083698748400478880d2a146ef9eaa111cd0e60522John McCall      // From the template.
4093698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
4103698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
4111fb0caaa7bef765b85972274e3b434af2572c141John McCall
4121fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The arguments at which the template was instantiated.
4131fb0caaa7bef765b85972274e3b434af2572c141John McCall      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
414381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4150b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
416d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
417ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
4183698748400478880d2a146ef9eaa111cd0e60522John McCall    if (F.ConsiderGlobalVisibility)
4193698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility =
420ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
421ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
4221fb0caaa7bef765b85972274e3b434af2572c141John McCall
423d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
4241fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
425381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
426af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!isExternalLinkage(EnumLV.linkage()))
427af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
428af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(EnumLV);
429d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
430d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
431d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
4321fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
433af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
4340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
435d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
436d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
4371fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
4381fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
4391fb0caaa7bef765b85972274e3b434af2572c141John McCall
4401fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
4411fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
4421fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
4431fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
4441fb0caaa7bef765b85972274e3b434af2572c141John McCall
4451fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
4461fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
447af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4481fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4491fb0caaa7bef765b85972274e3b434af2572c141John McCall
4501fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
4511fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
452af14603ca61757cf4361b583b45639a04c57e651John McCall  if (LV.linkage() != ExternalLinkage)
453af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo(LV.linkage(), DefaultVisibility, false);
4541fb0caaa7bef765b85972274e3b434af2572c141John McCall
4551fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
4561fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
4573698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility)
458af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
459d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4601fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
461d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
462d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4633698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
4641fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
4651fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
4661fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
4671fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
4683cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
4693cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
4701fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
4713cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
4723cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall         (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
473af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4743cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4753698748400478880d2a146ef9eaa111cd0e60522John McCall  LinkageInfo LV;
4763698748400478880d2a146ef9eaa111cd0e60522John McCall
4773698748400478880d2a146ef9eaa111cd0e60522John McCall  // The flags we're going to use to compute the class's visibility.
4783698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags ClassF = F;
4793698748400478880d2a146ef9eaa111cd0e60522John McCall
4803698748400478880d2a146ef9eaa111cd0e60522John McCall  // If we have an explicit visibility attribute, merge that in.
4813698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
4823698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
4833698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
4843698748400478880d2a146ef9eaa111cd0e60522John McCall
4853698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore global visibility later, but not this attribute.
4863698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
4873698748400478880d2a146ef9eaa111cd0e60522John McCall
4883698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore both global visibility and attributes when computing our
4893698748400478880d2a146ef9eaa111cd0e60522John McCall      // parent's visibility.
4903698748400478880d2a146ef9eaa111cd0e60522John McCall      ClassF = F.onlyTemplateVisibility();
4913698748400478880d2a146ef9eaa111cd0e60522John McCall    }
4923698748400478880d2a146ef9eaa111cd0e60522John McCall  }
493af14603ca61757cf4361b583b45639a04c57e651John McCall
494af14603ca61757cf4361b583b45639a04c57e651John McCall  // Class members only have linkage if their class has external
4953698748400478880d2a146ef9eaa111cd0e60522John McCall  // linkage.
4963698748400478880d2a146ef9eaa111cd0e60522John McCall  LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
4973698748400478880d2a146ef9eaa111cd0e60522John McCall  if (!isExternalLinkage(LV.linkage()))
498af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4993cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5003cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
5013698748400478880d2a146ef9eaa111cd0e60522John McCall  if (LV.linkage() == UniqueExternalLinkage)
502af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
5031fb0caaa7bef765b85972274e3b434af2572c141John McCall
5043cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
505110e8e56af30363072c140285961592b0107f789John McCall    TemplateSpecializationKind TSK = TSK_Undeclared;
506110e8e56af30363072c140285961592b0107f789John McCall
5071fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
5081fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
5091fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (FunctionTemplateSpecializationInfo *Spec
5103cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
511381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
512af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateParameterList(
5131fb0caaa7bef765b85972274e3b434af2572c141John McCall                              Spec->getTemplate()->getTemplateParameters()));
514110e8e56af30363072c140285961592b0107f789John McCall
515110e8e56af30363072c140285961592b0107f789John McCall      TSK = Spec->getTemplateSpecializationKind();
516110e8e56af30363072c140285961592b0107f789John McCall    } else if (MemberSpecializationInfo *MSI =
517110e8e56af30363072c140285961592b0107f789John McCall                 MD->getMemberSpecializationInfo()) {
518110e8e56af30363072c140285961592b0107f789John McCall      TSK = MSI->getTemplateSpecializationKind();
5193cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
5203cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
521110e8e56af30363072c140285961592b0107f789John McCall    // If we're paying attention to global visibility, apply
522110e8e56af30363072c140285961592b0107f789John McCall    // -finline-visibility-hidden if this is an inline method.
523110e8e56af30363072c140285961592b0107f789John McCall    //
524af14603ca61757cf4361b583b45639a04c57e651John McCall    // Note that ConsiderGlobalVisibility doesn't yet have information
525af14603ca61757cf4361b583b45639a04c57e651John McCall    // about whether containing classes have visibility attributes,
526af14603ca61757cf4361b583b45639a04c57e651John McCall    // and that's intentional.
527af14603ca61757cf4361b583b45639a04c57e651John McCall    if (TSK != TSK_ExplicitInstantiationDeclaration &&
5283698748400478880d2a146ef9eaa111cd0e60522John McCall        F.ConsiderGlobalVisibility &&
52966cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
53066cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // InlineVisibilityHidden only applies to definitions, and
53166cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // isInlined() only gives meaningful answers on definitions
53266cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // anyway.
53366cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      const FunctionDecl *Def = 0;
53466cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      if (MD->hasBody(Def) && Def->isInlined())
53566cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        LV.setVisibility(HiddenVisibility);
53666cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall    }
5371fb0caaa7bef765b85972274e3b434af2572c141John McCall
538110e8e56af30363072c140285961592b0107f789John McCall    // Note that in contrast to basically every other situation, we
539110e8e56af30363072c140285961592b0107f789John McCall    // *do* apply -fvisibility to method declarations.
540110e8e56af30363072c140285961592b0107f789John McCall
541110e8e56af30363072c140285961592b0107f789John McCall  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
542110e8e56af30363072c140285961592b0107f789John McCall    if (const ClassTemplateSpecializationDecl *Spec
543110e8e56af30363072c140285961592b0107f789John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
544110e8e56af30363072c140285961592b0107f789John McCall      // Merge template argument/parameter information for member
545110e8e56af30363072c140285961592b0107f789John McCall      // class template specializations.
546381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
547af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateParameterList(
5481fb0caaa7bef765b85972274e3b434af2572c141John McCall                    Spec->getSpecializedTemplate()->getTemplateParameters()));
549110e8e56af30363072c140285961592b0107f789John McCall    }
550110e8e56af30363072c140285961592b0107f789John McCall
551110e8e56af30363072c140285961592b0107f789John McCall  // Static data members.
552110e8e56af30363072c140285961592b0107f789John McCall  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
553ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // Modify the variable's linkage by its type, but ignore the
554ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // type's visibility unless it's a definition.
555ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
556ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (TypeLV.first != ExternalLinkage)
557af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeLinkage(UniqueExternalLinkage);
558af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!LV.visibilityExplicit())
559af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeVisibility(TypeLV.second);
560110e8e56af30363072c140285961592b0107f789John McCall  }
561110e8e56af30363072c140285961592b0107f789John McCall
5623698748400478880d2a146ef9eaa111cd0e60522John McCall  F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
563110e8e56af30363072c140285961592b0107f789John McCall
564110e8e56af30363072c140285961592b0107f789John McCall  // Apply -fvisibility if desired.
5653698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
566af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
5673cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
5683cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5691fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
5703cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
5713cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
572381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorLinkage NamedDecl::getLinkage() const {
573381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (HasCachedLinkage) {
57456ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer    assert(Linkage(CachedLinkage) ==
57556ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer             getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
576381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return Linkage(CachedLinkage);
577381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
578381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
579381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = getLVForDecl(this,
580381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                               LVFlags::CreateOnlyDeclLinkage()).linkage();
581381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
582381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return Linkage(CachedLinkage);
583381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
584381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
585af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const {
586381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  LinkageInfo LI = getLVForDecl(this, LVFlags());
58756ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer  assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
588381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
589381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = LI.linkage();
590381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return LI;
5910df9587ab011c12968fcbe3518666b2117afe350John McCall}
592becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
5933698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
594becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
595becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
5960df9587ab011c12968fcbe3518666b2117afe350John McCall  switch (D->getKind()) {
597becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
598becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
5991fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
6001fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
601becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
602becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
603becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
604becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
605becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
606becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
607becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
608becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
609becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
610becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
611af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::external();
612becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
613becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
614d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
6150df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->getRedeclContext()->isFileContext())
6163698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForNamespaceScopeDecl(D, Flags);
617d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
618d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
619d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
620d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
621d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
622d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
623d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
624d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
6250df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->isRecord())
6263698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForClassMember(D, Flags);
627d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
628d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
629d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
630d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
631d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
632d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
633d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
634d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
635d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
636d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
637d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
638d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
6390df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
6400df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
6410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Function->isInAnonymousNamespace())
642af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
6431fb0caaa7bef765b85972274e3b434af2572c141John McCall
644af14603ca61757cf4361b583b45639a04c57e651John McCall      LinkageInfo LV;
645381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      if (Flags.ConsiderVisibilityAttributes) {
646381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
647381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LV.setVisibility(GetVisibilityFromAttr(VA));
648381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      }
649381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
6501fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
651381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
652af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
653af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
6541fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
6550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
6561fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
657d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
658d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
6590df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const VarDecl *Var = dyn_cast<VarDecl>(D))
660d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
661d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
6620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Var->isInAnonymousNamespace())
663af14603ca61757cf4361b583b45639a04c57e651John McCall          return LinkageInfo::uniqueExternal();
6641fb0caaa7bef765b85972274e3b434af2572c141John McCall
665af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo LV;
6661fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
667af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(HiddenVisibility);
668381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        else if (Flags.ConsiderVisibilityAttributes) {
669381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
670381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor            LV.setVisibility(GetVisibilityFromAttr(VA));
671381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        }
672381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
6731fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
674381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
675af14603ca61757cf4361b583b45639a04c57e651John McCall          if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
676af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.mergeVisibility(PrevLV);
6771fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
6781fb0caaa7bef765b85972274e3b434af2572c141John McCall
6791fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
680d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
681d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
682d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
683d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
684d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
685af14603ca61757cf4361b583b45639a04c57e651John McCall  return LinkageInfo::none();
6861fb0caaa7bef765b85972274e3b434af2572c141John McCall}
687d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
68847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
6893a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
6903a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
6913a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
6923a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
69347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
69447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
69547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
69647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
69747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
69868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
69968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
70068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
70168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
70268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
70368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
70468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
70568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
70668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
70768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
70868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
70968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
71068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
71168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
7121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
71368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
714f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
715f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
716f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
717910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.data(),
718910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.size(),
7193a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
72068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
72168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
7226be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
72368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
7246be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
72568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
72668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
72768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
72868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
72968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
73068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
73168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
7323521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
7333521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
7343521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
7353521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
73668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
7373521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
7383521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
7393521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
7403521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
74168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
7423521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
7433521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
74468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
7453521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
7463521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
7473521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
7483521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
74968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
75068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
7513521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
7523521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
75368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
75468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
75568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
75668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
75768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
75847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
75947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
7608472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
76168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
7628472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
76368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
76447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
76568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
76647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
76747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
7684afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
7696ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
7706ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
7712a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
7722a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
7732a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
7742a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
7752a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
7762a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7786ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
7796ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
7806ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
7816ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
782e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
783e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
784e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
785e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
786e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
787e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
788e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
7891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7900de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
7910de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
7920de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
7931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
794f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
795f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
796f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
7979488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
7989488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
7999488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
8009488ea120e093068021f944176c3d610dd540914John McCall
801c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis  if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
802c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis    return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
803c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis           cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
804c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis
8056ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
8066ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
8076ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
8086ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
8096ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
8106ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
811d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
812d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
813d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
8144afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
815e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
816e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
817e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
8189488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
819e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
820e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
821e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
822e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
823e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
824e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
825e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
826e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
827e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
828161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
829161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
830161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
831161755a09898c95d21bfff33707da9ca41cd53c5John McCall
832161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
833161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
834161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
835161755a09898c95d21bfff33707da9ca41cd53c5John McCall
83687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
837161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
838161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
839161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
840161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
841161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
842161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
843161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
844161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
845161755a09898c95d21bfff33707da9ca41cd53c5John McCall
8465239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
847a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
848a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
849a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
8501693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
8511693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
8521693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
8531693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
8541693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
8551693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
8561693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
8571693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
858a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
8594e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
8604e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
861a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
862a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
863a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
864b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
865b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                      SourceRange QualifierRange) {
866b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
867b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
868b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
869b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
870b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
871b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
872b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
873b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
874b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
875b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
876b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
877b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
878b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
879b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
880b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
881b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
882b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
883b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
884b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
885b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
886b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
887b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
888b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
889b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
890b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
891b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
892b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
893b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
8941693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
8951693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
8961693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
8971693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
8989b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
899c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
900c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
9019b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
9029b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
9039b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
9049b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || NNS) &&
9059b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Nonempty array of template parameters with no qualifier!");
9069b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
9079b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
9089b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
909c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
9109b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
9119b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
9129b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
9139b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
9149b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
915c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
9169b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
9179b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
9189b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
9199b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
9209b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
9219b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
922a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
92399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
92499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
92599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
9267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
9277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
928d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_None:          break;
929d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Auto:          return "auto"; break;
930d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Extern:        return "extern"; break;
931d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_PrivateExtern: return "__private_extern__"; break;
932d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Register:      return "register"; break;
933d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Static:        return "static"; break;
9347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
9377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
9387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9404afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
941a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
94216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
94316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
94499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
94599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
946381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid VarDecl::setStorageClass(StorageClass SC) {
947381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForVariable(SC));
948381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
949381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
950381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
951381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
952381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
953381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
9541693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation VarDecl::getInnerLocStart() const {
95533e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
95633e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (Start.isInvalid())
95733e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    Start = getLocation();
9581693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return Start;
9591693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9601693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
9611693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
96255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
9631693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
9641693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), getLocation());
96555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
96655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
9677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
9687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
9697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
9707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
971d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
9727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
9737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
9757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl       DC = DC->getParent()) {
9767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
9777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
978d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
9797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
9817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
9827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (DC->isFunctionOrMethod())
9847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      return false;
9857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
9887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
9917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
9927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
994e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
995e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
996e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
997e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
998e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
999e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
1000e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
1001e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
1002e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
1003e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
1004e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
1005e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1006e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
1007e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
1008e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
1009e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1010e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
1011e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
1012e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
1013e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
1014e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
1015e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
1016e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
1017e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
1018e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
1019e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
1020e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
1021e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
10222bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
1023d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
1024d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
10252bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
10262bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
10272bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
10282bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
10292bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
10302bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
1031e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
1032e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
1033e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
1034e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
1035e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
1036e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1037e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
1038e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1039e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
1040e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
1041e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
1042e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1043e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1044e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
1045e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1046e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1047e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
1048e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1049f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
1050e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
1051e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1052e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
1053e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
1054e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
1055e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
1056e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
1057e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
1058e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1059e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
1060e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1061e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1062e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
1063e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1064e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1065e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
1066e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1067e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1068e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
1069e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
1070e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
107131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
107231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
107331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
107431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
1075e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
1076e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1077e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
107831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
107931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
108031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
108131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
1082e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1083e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1084110e8e56af30363072c140285961592b0107f789John McCallVarDecl::DefinitionKind VarDecl::hasDefinition() const {
1085110e8e56af30363072c140285961592b0107f789John McCall  DefinitionKind Kind = DeclarationOnly;
1086110e8e56af30363072c140285961592b0107f789John McCall
1087110e8e56af30363072c140285961592b0107f789John McCall  const VarDecl *First = getFirstDeclaration();
1088110e8e56af30363072c140285961592b0107f789John McCall  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1089110e8e56af30363072c140285961592b0107f789John McCall       I != E; ++I)
1090110e8e56af30363072c140285961592b0107f789John McCall    Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1091110e8e56af30363072c140285961592b0107f789John McCall
1092110e8e56af30363072c140285961592b0107f789John McCall  return Kind;
1093110e8e56af30363072c140285961592b0107f789John McCall}
1094110e8e56af30363072c140285961592b0107f789John McCall
109531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
10967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
10977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
10987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
10997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
110131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
11027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
11037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
11057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11071028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
11081028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Decl::isOutOfLine())
11091028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
11108761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
11118761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
11128761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
11138761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
11141028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
11151028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
11161028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
11171028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
11181028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
11191028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
11201028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
11211028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
11221028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
11230d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
11240d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
11250d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
11260d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
11270d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
11280d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
11290d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
11300d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
11310d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
11320d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
11330d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
11340d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
11350d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
1136838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
11377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
11387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
1139838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
11407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
11437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11451028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1146b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1147251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1148251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1149251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1150251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1151251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1152663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1153e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1154251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1155251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1156251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1157251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1158251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
11591028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1160b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1161b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1162b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
11630a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
11640a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1165b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1166251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1167251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
11680a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
11690a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
11700a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
11710a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
11727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
11737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
11747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
11757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
11767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1177275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
11787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
11797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
11807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
118116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
118216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
118316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
118416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1185275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1186275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
11877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
11887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
11897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
11907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
11917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
11934765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
11947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
11957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
11977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
12004765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
12017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
1202275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
1203c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
1204275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1205275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
12067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
12077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
12087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
12097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12104765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
12117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
12127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
12157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
12167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
12177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
12197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
12207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1222fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1223fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
122499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
12258a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
12268a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
12278a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1228136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCallvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1229136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        const PrintingPolicy &Policy,
1230136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        bool Qualified) const {
1231136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1232136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1233136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  if (TemplateArgs)
1234136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall    S += TemplateSpecializationType::PrintTemplateArgumentList(
1235910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                         TemplateArgs->data(),
1236910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                         TemplateArgs->size(),
1237136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                                               Policy);
1238136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
1239136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall}
124027f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
12419498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
12429498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
12439498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
12449498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
12459498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
12469498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
124706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
124806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
124906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (I->Body) {
125006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
125106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
125206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
125306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
125406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
125506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
125606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
125706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
12586fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1259c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1260c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1261c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1262c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
1263f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1264f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1265f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1266f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
12675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
126955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
127055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
1271b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  if (B)
127255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
127355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
127455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
12752138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
12762138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
12772138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
12782138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
12792138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
12802138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
12812138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
128248a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
128348a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
128407a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
12857a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    getDeclContext()->getRedeclContext()->isTranslationUnit() &&
128604495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
128704495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
128804495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
128948a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
129048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
12916393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
12926393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
12936393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1294d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
12956393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
12961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
12976393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor       DC = DC->getParent()) {
12986393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
12996393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1300d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
130140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
13026393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13036393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
13046393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
130545975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor
130645975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor    if (DC->isRecord())
130745975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor      break;
13086393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
13096393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13100bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
13116393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
13126393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13138499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
13148499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
13158499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
13168499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1317d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
13188499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
13198499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
13201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
13218499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
13228499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
13238499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
13248499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
13258499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
13268499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
13278499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
13288499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
13298499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
13308499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
13318499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
13328499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
13337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
13347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
13357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
13367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
13387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
13397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
13407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
13417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
13427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
13438f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
13448f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (PrevDecl->IsInline)
13458f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    IsInline = true;
13467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
13497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
13507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
13537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
13547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1356381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid FunctionDecl::setStorageClass(StorageClass SC) {
1357381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForFunction(SC));
1358381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1359381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1360381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1361381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1362381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1363381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
13643e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
13653e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
13663e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
13673e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
13683e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
13693e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
13703e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
13711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
13723e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
13737814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
13747814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
13753c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
13763c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
13773c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13783c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
13793c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
13803c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
13813c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13823c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
13833c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
13843c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
13853c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13869add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1387d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
13889add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
13899add31798f621f843233dbff8bba103fca64447bDouglas Gregor
13903c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
13913c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
13923c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
13933c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
13943c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
13953c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
13963c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
13973c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
13983c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
13991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
14003c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
140140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
14023c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
14033c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14043c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
14053e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
14063e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
14073e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
14083e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
14091ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
14102dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner/// based on its FunctionType.  This is the length of the PararmInfo array
14111ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
14121ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1413183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
141472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1415d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
141672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14206b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
14216b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                             ParmVarDecl **NewParamInfo, unsigned NumParams) {
14225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
14232dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
14241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
14265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
14276b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1428fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
143055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
143196888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
143296888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1433cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
143455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14388123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
14398123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
14408123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
14419e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner/// arguments (in C++).
14428123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
14438123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  unsigned NumRequiredArgs = getNumParams();
14448123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  while (NumRequiredArgs > 0
1445ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
14468123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
14478123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
14488123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
14498123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
14508123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
14517ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
14528f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (IsInline)
14537d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
145448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
145548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
145648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
145748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
145848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
14597d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
14607d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
14617d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
14627d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
14637d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
14647d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
14657d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
14667d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
14677d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
14687d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
14697d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
14707d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
14717d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
14727d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
147306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
14747d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
147506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
14767d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
147706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
14787d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
14797d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
14807d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
14817ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
14827ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
14837d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
14841fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
14851fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
14861fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
14871fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
14881fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
14891fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
14901fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
14911fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
14921e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
14931fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
14941fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
14951fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
14961fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
14971fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
14981fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
14991fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
15001fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
15011fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
15027ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
15037d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
15041fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
15057d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
15068f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If it's not the case that both 'inline' and 'extern' are
15078f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // specified on the definition, then this inline definition is
15088f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // externally visible.
15098f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
15108f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      return true;
15118f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
15128f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If any declaration is 'inline' but not 'extern', then this definition
15138f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // is externally visible.
15141fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
15151fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
15161fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
15178f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      if (Redecl->isInlineSpecified() &&
15188f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor          Redecl->getStorageClassAsWritten() != SC_Extern)
15191fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
15208f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    }
15211fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
15229f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
15231fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
15241fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
15251fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
15261fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
15271fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
15281fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
15291fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
15301fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
15311fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
15321fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
15331fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
15341fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
15351fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1536d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
15371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
15381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
15391fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
15401fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
15411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
15421fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
15431fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
15449f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
15459f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
15469f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
15471cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
15481cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
15491cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1550e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1551e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
15521cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
15531cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
15541cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
15551cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1556a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1557a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1558a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1559a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1560a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1561a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1562a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1563a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1564a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1565d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1566d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1567d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1568d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1569d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1570d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1571d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1572d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1573d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1574d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1575d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1576d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1577d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1578d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1579d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1580d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1581d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
15822db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1583b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
15842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
15852db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
15862db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
15872db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
15882db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1589b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1590b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1591b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1592b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
15932db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
15946b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
15956b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
15962db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
15972db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
15982db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
15992db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
16006b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
16012db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
16022db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
16032db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
16043b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
16056cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
16066cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
16073b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
16083b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16093b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
16103b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
16113b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
16123b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
16133b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
16143b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16153b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
16163b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
16173b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16183b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
16193b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
16203b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
16213b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
16223b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16233b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
16243b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
162506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
16263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
162706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
16283b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16293b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
16303b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
16313b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
16323b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
163306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
16343b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
16353b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16367ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
16373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
16383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
16403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
16413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
16423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
16433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
16443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
16453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
16463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16473b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
16483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
16493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
16513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
16523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
16533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
16543b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
16553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
165616e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
16571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
165816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
165916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
16601fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
166116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
166216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
166316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
166416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
166516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
166616e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
16671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1668fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1669fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
167016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
167116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
167216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
167316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
167416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1675e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1676e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1677e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1678e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1679e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1680e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1681e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1682e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1683e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1684e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
16851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
16866b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
16876b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
1688127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1689b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1690e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
16917b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
16927b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1693b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1694b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
16951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
169616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
16971637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1698a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1699a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
1700a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
1701a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
17021637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
17031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1704127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1705b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1706b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1707b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1708b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
17092c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
17102c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
17112c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
17122c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
1713b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1714b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
17152c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
17162c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
17172c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
1718b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
17191637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
17201637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1721af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1722af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1723af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1724af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1725af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1726af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1727af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
172821c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1729af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1730af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1731af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1732af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1733af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1734af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1735af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1736af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1737af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1738af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1739af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1740af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1741af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1742af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1743af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1744af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1745af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1746af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1747af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1748af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1749af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1750af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1751af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1752af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1753af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1754af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1755d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
17561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1757d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
17582db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
17591fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
17602db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
17612db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1762d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
17632db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
17642db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
17652db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
17662db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
17672db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
17682db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
17691fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
17701fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
17711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
17720a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
17730a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
17742db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
17752db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
17760a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
17772db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
17780a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
17790a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
17800a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
17810a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
17820a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
17830a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
17842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
17850a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
17860a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
17870a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
17880a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
17890a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
17902db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
17911fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
17921fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
17930a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
17940a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
17950a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
17960a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
17970a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
17980a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
17990a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
18000a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
18010a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
18020a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
18030a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
18040a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
18059f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
18069f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (Decl::isOutOfLine())
18079f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
18089f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
18099f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
18109f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
18119f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
18129f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
181306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
18149f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
18159f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
18169f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
18179f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
18189f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
18199f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
18209f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
182106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
18229f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
18239f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
18249f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
18259f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
18269f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
18279f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
18288a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
18297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
18307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
18317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
18337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             IdentifierInfo *Id, QualType T,
18347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
18357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
18367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
18377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
18397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
18407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
18417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
18437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
18447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
18467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
18477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
18487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1849bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
18504b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
18514b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
18521693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
18531693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
18541693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
18551693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1856f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1857f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
18581693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
1859f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1860f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1861b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
18628e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1863b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1864b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
186560e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
186660e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
186760e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
186860e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor    TypeForDecl->ClearLinkageCache();
1869381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  ClearLinkageCache();
187060e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
187160e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
18720b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
1873ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
187486ff308724171494395a840fd2efbe25e62f352eJohn McCall
187586ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
187686ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
187786ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
187886ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
18792243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
18802243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
188186ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
18820b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
18830b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
18840b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
18855cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
18865cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
18875cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
18885cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
18890b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
1890ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
1891565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
1892565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
1893565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
18940b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
18950b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
1896952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
18978e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
18988e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
1899220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1900220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
19011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
19038e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
19048e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
19058e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
19061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19078e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
19084b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
19094b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1910b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1911b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                               SourceRange QualifierRange) {
1912b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
1913b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
1914b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
1915b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1916b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1917b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
1918b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
1919b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1920b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
1921b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1922b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
1923b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
1924b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
1925b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
1926b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1927b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1928b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1929b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
19304b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
19317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
19327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
19357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
1936a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           EnumDecl *PrevDecl, bool IsScoped,
1937a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           bool IsScopedUsingClassTag, bool IsFixed) {
19381274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1939a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    IsScoped, IsScopedUsingClassTag, IsFixed);
19407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
19417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
19427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1944b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
19451274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1946a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                          false, false, false);
1947b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1948b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1949838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
19501b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
19511b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
19521b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
19537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
19541274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
19551274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
19567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
19571b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
19581b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
19597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
19607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19638a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
19648a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
19655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
196635bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
19678e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
19688e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
19698e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
19706359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
1971bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
1972082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
1973eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
19746359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
19756359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
19766359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
19776359792ca92e7ca2f416cb804c6604358174e994Ted KremenekRecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
19784b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
1979741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
19801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19818e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
19824b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
19834b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
19846359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
19856359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
1986b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisRecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1987b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1988b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis                            SourceLocation());
1989b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1990b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1991c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
19921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1993c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1994c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
1995c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
1996eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
1997eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1998eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
1999eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2000eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
2001eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2002eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
200344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// completeDefinition - Notes that the definition of this type is now
200444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// complete.
2005838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid RecordDecl::completeDefinition() {
20065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!isDefinition() && "Cannot redefine record!");
20070b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  TagDecl::completeDefinition();
20085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2010eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
2011eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
2012eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
2013eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2014eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
2015eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
2016eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2017eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<Decl*, 64> Decls;
2018eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2019eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2020eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2021eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
2022eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
2023eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
2024eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
2025eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
2026eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2027eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = true;
2028eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2029eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
2030eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2031eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2032eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2033eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2034eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
203556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
203656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
203756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
203856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
2039838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
2040e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
2041e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
20421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2043e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
2044e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
2045e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
2046838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
2047e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2048e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2049e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
2050e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
2051e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
2052e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffunsigned BlockDecl::getNumParams() const {
2053e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  return NumParams;
2054e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
20557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20562fcbceff97e065cff499e6cc563ca25c762bf547Douglas GregorSourceRange BlockDecl::getSourceRange() const {
20572fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor  return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
20582fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor}
20597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
20617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
20627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
20637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
20657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
20667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
20697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
20707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
20717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
207306c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
207406c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
207506c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
207606c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
207706c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
20787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
20797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    SourceLocation L, IdentifierInfo *Id, QualType T) {
20807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
20817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
20842577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
20852577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
208616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   StorageClass S, StorageClass SCAsWritten,
20878f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool isInlineSpecified,
20888f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool hasWrittenPrototype) {
20892577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
20908f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                           S, SCAsWritten, isInlineSpecified);
20917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
20927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
20937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
20967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
20977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
21007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
21017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
21027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
21037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
21047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2106d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl *
2107d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2108d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          IdentifierInfo *Id, QualType T, NamedDecl **CH,
2109d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          unsigned CHS) {
211087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
211187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
211287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
21138e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
21148e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
21158e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
21168e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
21178e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
21188e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
21198e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
21207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
21217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
21227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
21237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
21247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
21277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
21287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
21297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
21307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
2131