Decl.cpp revision db9924191092b4d426cc066637d81698211846aa
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) {
1366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          QualType T = NTTP->getExpansionType(I);
1406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          if (!T->isDependentType())
1416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor            LV = merge(LV, T->getLinkageAndVisibility());
1426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
1436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        continue;
1446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1460b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
1471fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
1480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
1490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
1506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
1530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
154af14603ca61757cf4361b583b45639a04c57e651John McCall      LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
1550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1581fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
161381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor/// getLVForDecl - Get the linkage and visibility for the given declaration.
162381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
163381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
1650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
1661fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
167381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           unsigned NumArgs,
168381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           LVFlags &F) {
1691fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
1730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
1740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
1750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
1760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
1791fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
1800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
1831fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
1841fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
1851fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
18689d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
18789d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor          LV = merge(LV, getLVForDecl(ND, F));
1881fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
192a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
193a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      if (TemplateDecl *Template
194a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
19589d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        LV = merge(LV, getLVForDecl(Template, F));
1960b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1970b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1980b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
1991fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
200381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  Args[I].pack_size(),
201381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  F));
2020b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
2030b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
2040b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
2050b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2061fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
2070b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
2080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
209af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair
210381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
211381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                             LVFlags &F) {
212381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
2133cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
2143cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2153698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
2167a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
224d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
225d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
226d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
227d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
228d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
229af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
231d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
232d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
233d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
236e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
237d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
238d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
240d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
241d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
242d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
2430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
244d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
245d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
246d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
247af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::internal();
248d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
249d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
2500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
2510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
2520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
253d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
254d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
255d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
256d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
257d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
258d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
259d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
260d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
261d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
262af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo(InternalLinkage, DefaultVisibility, false);
263d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
264d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
265d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
266af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
267d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
268d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
269094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  if (D->isInAnonymousNamespace()) {
270094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const VarDecl *Var = dyn_cast<VarDecl>(D);
271094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
272094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
273094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth      return LinkageInfo::uniqueExternal();
274094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  }
275e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2761fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2771fb0caaa7bef765b85972274e3b434af2572c141John McCall
2781fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2791fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2801fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2811fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
282af14603ca61757cf4361b583b45639a04c57e651John McCall  LinkageInfo LV;
283af14603ca61757cf4361b583b45639a04c57e651John McCall
2843698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
2853698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
2863698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.setVisibility(GetVisibilityFromAttr(VA), true);
2873698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
28890f1450c109fbbd333001165bbd986061f7c4513John McCall    } else {
28990f1450c109fbbd333001165bbd986061f7c4513John McCall      // If we're declared in a namespace with a visibility attribute,
29090f1450c109fbbd333001165bbd986061f7c4513John McCall      // use that namespace's visibility, but don't call it explicit.
29190f1450c109fbbd333001165bbd986061f7c4513John McCall      for (const DeclContext *DC = D->getDeclContext();
29290f1450c109fbbd333001165bbd986061f7c4513John McCall           !isa<TranslationUnitDecl>(DC);
29390f1450c109fbbd333001165bbd986061f7c4513John McCall           DC = DC->getParent()) {
29490f1450c109fbbd333001165bbd986061f7c4513John McCall        if (!isa<NamespaceDecl>(DC)) continue;
29590f1450c109fbbd333001165bbd986061f7c4513John McCall        if (const VisibilityAttr *VA =
29690f1450c109fbbd333001165bbd986061f7c4513John McCall              cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
29790f1450c109fbbd333001165bbd986061f7c4513John McCall          LV.setVisibility(GetVisibilityFromAttr(VA), false);
29890f1450c109fbbd333001165bbd986061f7c4513John McCall          F.ConsiderGlobalVisibility = false;
29990f1450c109fbbd333001165bbd986061f7c4513John McCall          break;
30090f1450c109fbbd333001165bbd986061f7c4513John McCall        }
30190f1450c109fbbd333001165bbd986061f7c4513John McCall      }
3023698748400478880d2a146ef9eaa111cd0e60522John McCall    }
303af14603ca61757cf4361b583b45639a04c57e651John McCall  }
3041fb0caaa7bef765b85972274e3b434af2572c141John McCall
305d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
3061fb0caaa7bef765b85972274e3b434af2572c141John McCall
307d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
308d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
309d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
310d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
311d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
312110e8e56af30363072c140285961592b0107f789John McCall    // GCC applies the following optimization to variables and static
313110e8e56af30363072c140285961592b0107f789John McCall    // data members, but not to functions:
314110e8e56af30363072c140285961592b0107f789John McCall    //
3151fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
3161fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
3171fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
3181fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
3191fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
3201fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
3211fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
3221fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
3231fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
3241fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
3251fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
3261fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
3271fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
328ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
329ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
330ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
3311fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
3321fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
333ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
3341fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
3351fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
336af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
337af14603ca61757cf4361b583b45639a04c57e651John McCall      if (!LV.visibilityExplicit())
338af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(TypeLV.second);
339110e8e56af30363072c140285961592b0107f789John McCall    }
340110e8e56af30363072c140285961592b0107f789John McCall
34135cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Var->getStorageClass() == SC_PrivateExtern)
34235cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
34335cebc3eea898637057b10b5cf7dd08b1d788980John McCall
344d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
345d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
346d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
3471fb0caaa7bef765b85972274e3b434af2572c141John McCall
348d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
349d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
350d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
351d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
355d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
356d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
357d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
358381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
359af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
360af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
361d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
362d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
363d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
364d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
3651fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
36667fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
36767fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
36867fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
36967fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
3701fb0caaa7bef765b85972274e3b434af2572c141John McCall
37135cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Function->getStorageClass() == SC_PrivateExtern)
37235cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
37335cebc3eea898637057b10b5cf7dd08b1d788980John McCall
374d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
375d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
376d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
377d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
378d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
379d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
380d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
381d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
382d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
383d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
384d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
385d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
386d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
387d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
388d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
389d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
390d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
391d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
392d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
393381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
394af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
395af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
396d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
397d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
398d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
399af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // In C++, then if the type of the function uses a type with
400af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // unique-external linkage, it's not legally usable from outside
401af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // this translation unit.  However, we should use the C linkage
402af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // rules instead for extern "C" declarations.
403af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
404af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall        Function->getType()->getLinkage() == UniqueExternalLinkage)
405af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
406af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
4070b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
4080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
4093698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(SpecInfo->getTemplate(),
4103698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
4110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
412381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4130b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
4140b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
415d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
416d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
417d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
418d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
419d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
420d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
4211fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
4221fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
4231fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
424af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
4251fb0caaa7bef765b85972274e3b434af2572c141John McCall
4261fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
4271fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
4281fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (const ClassTemplateSpecializationDecl *Spec
4291fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
4303698748400478880d2a146ef9eaa111cd0e60522John McCall      // From the template.
4313698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
4323698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
4331fb0caaa7bef765b85972274e3b434af2572c141John McCall
4341fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The arguments at which the template was instantiated.
4351fb0caaa7bef765b85972274e3b434af2572c141John McCall      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
436381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
438d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
439ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
4403698748400478880d2a146ef9eaa111cd0e60522John McCall    if (F.ConsiderGlobalVisibility)
4413698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility =
442ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
443ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
4441fb0caaa7bef765b85972274e3b434af2572c141John McCall
445d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
4461fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
447381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
448af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!isExternalLinkage(EnumLV.linkage()))
449af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
450af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(EnumLV);
451d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
452d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
453d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
4541fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
455af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
4560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
457d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
458d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
4591fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
4601fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
4611fb0caaa7bef765b85972274e3b434af2572c141John McCall
4621fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
4631fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
4641fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
4651fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
4661fb0caaa7bef765b85972274e3b434af2572c141John McCall
4671fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
4681fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
469af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4701fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4711fb0caaa7bef765b85972274e3b434af2572c141John McCall
4721fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
4731fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
474af14603ca61757cf4361b583b45639a04c57e651John McCall  if (LV.linkage() != ExternalLinkage)
475af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo(LV.linkage(), DefaultVisibility, false);
4761fb0caaa7bef765b85972274e3b434af2572c141John McCall
4771fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
4781fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
4793698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility)
480af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
481d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4821fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
483d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
484d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4853698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
4861fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
4871fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
4881fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
4891fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
4903cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
4913cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
4921fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
4933cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
4943cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall         (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
495af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4963cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
4973698748400478880d2a146ef9eaa111cd0e60522John McCall  LinkageInfo LV;
4983698748400478880d2a146ef9eaa111cd0e60522John McCall
4993698748400478880d2a146ef9eaa111cd0e60522John McCall  // The flags we're going to use to compute the class's visibility.
5003698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags ClassF = F;
5013698748400478880d2a146ef9eaa111cd0e60522John McCall
5023698748400478880d2a146ef9eaa111cd0e60522John McCall  // If we have an explicit visibility attribute, merge that in.
5033698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
5043698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
5053698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
5063698748400478880d2a146ef9eaa111cd0e60522John McCall
5073698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore global visibility later, but not this attribute.
5083698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
5093698748400478880d2a146ef9eaa111cd0e60522John McCall
5103698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore both global visibility and attributes when computing our
5113698748400478880d2a146ef9eaa111cd0e60522John McCall      // parent's visibility.
5123698748400478880d2a146ef9eaa111cd0e60522John McCall      ClassF = F.onlyTemplateVisibility();
5133698748400478880d2a146ef9eaa111cd0e60522John McCall    }
5143698748400478880d2a146ef9eaa111cd0e60522John McCall  }
515af14603ca61757cf4361b583b45639a04c57e651John McCall
516af14603ca61757cf4361b583b45639a04c57e651John McCall  // Class members only have linkage if their class has external
5173698748400478880d2a146ef9eaa111cd0e60522John McCall  // linkage.
5183698748400478880d2a146ef9eaa111cd0e60522John McCall  LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
5193698748400478880d2a146ef9eaa111cd0e60522John McCall  if (!isExternalLinkage(LV.linkage()))
520af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5213cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5223cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
5233698748400478880d2a146ef9eaa111cd0e60522John McCall  if (LV.linkage() == UniqueExternalLinkage)
524af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
5251fb0caaa7bef765b85972274e3b434af2572c141John McCall
5263cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
527af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // If the type of the function uses a type with unique-external
528af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // linkage, it's not legally usable from outside this translation unit.
529af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (MD->getType()->getLinkage() == UniqueExternalLinkage)
530af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
531af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
532110e8e56af30363072c140285961592b0107f789John McCall    TemplateSpecializationKind TSK = TSK_Undeclared;
533110e8e56af30363072c140285961592b0107f789John McCall
5341fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
5351fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
5361fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (FunctionTemplateSpecializationInfo *Spec
5373cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
538381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
539af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateParameterList(
5401fb0caaa7bef765b85972274e3b434af2572c141John McCall                              Spec->getTemplate()->getTemplateParameters()));
541110e8e56af30363072c140285961592b0107f789John McCall
542110e8e56af30363072c140285961592b0107f789John McCall      TSK = Spec->getTemplateSpecializationKind();
543110e8e56af30363072c140285961592b0107f789John McCall    } else if (MemberSpecializationInfo *MSI =
544110e8e56af30363072c140285961592b0107f789John McCall                 MD->getMemberSpecializationInfo()) {
545110e8e56af30363072c140285961592b0107f789John McCall      TSK = MSI->getTemplateSpecializationKind();
5463cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
5473cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
548110e8e56af30363072c140285961592b0107f789John McCall    // If we're paying attention to global visibility, apply
549110e8e56af30363072c140285961592b0107f789John McCall    // -finline-visibility-hidden if this is an inline method.
550110e8e56af30363072c140285961592b0107f789John McCall    //
551af14603ca61757cf4361b583b45639a04c57e651John McCall    // Note that ConsiderGlobalVisibility doesn't yet have information
552af14603ca61757cf4361b583b45639a04c57e651John McCall    // about whether containing classes have visibility attributes,
553af14603ca61757cf4361b583b45639a04c57e651John McCall    // and that's intentional.
554af14603ca61757cf4361b583b45639a04c57e651John McCall    if (TSK != TSK_ExplicitInstantiationDeclaration &&
5553698748400478880d2a146ef9eaa111cd0e60522John McCall        F.ConsiderGlobalVisibility &&
55666cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
55766cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // InlineVisibilityHidden only applies to definitions, and
55866cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // isInlined() only gives meaningful answers on definitions
55966cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // anyway.
56066cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      const FunctionDecl *Def = 0;
56166cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      if (MD->hasBody(Def) && Def->isInlined())
56266cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        LV.setVisibility(HiddenVisibility);
56366cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall    }
5641fb0caaa7bef765b85972274e3b434af2572c141John McCall
565110e8e56af30363072c140285961592b0107f789John McCall    // Note that in contrast to basically every other situation, we
566110e8e56af30363072c140285961592b0107f789John McCall    // *do* apply -fvisibility to method declarations.
567110e8e56af30363072c140285961592b0107f789John McCall
568110e8e56af30363072c140285961592b0107f789John McCall  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
569110e8e56af30363072c140285961592b0107f789John McCall    if (const ClassTemplateSpecializationDecl *Spec
570110e8e56af30363072c140285961592b0107f789John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
571110e8e56af30363072c140285961592b0107f789John McCall      // Merge template argument/parameter information for member
572110e8e56af30363072c140285961592b0107f789John McCall      // class template specializations.
573381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
574af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.merge(getLVForTemplateParameterList(
5751fb0caaa7bef765b85972274e3b434af2572c141John McCall                    Spec->getSpecializedTemplate()->getTemplateParameters()));
576110e8e56af30363072c140285961592b0107f789John McCall    }
577110e8e56af30363072c140285961592b0107f789John McCall
578110e8e56af30363072c140285961592b0107f789John McCall  // Static data members.
579110e8e56af30363072c140285961592b0107f789John McCall  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
580ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // Modify the variable's linkage by its type, but ignore the
581ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // type's visibility unless it's a definition.
582ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
583ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (TypeLV.first != ExternalLinkage)
584af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeLinkage(UniqueExternalLinkage);
585af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!LV.visibilityExplicit())
586af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeVisibility(TypeLV.second);
587110e8e56af30363072c140285961592b0107f789John McCall  }
588110e8e56af30363072c140285961592b0107f789John McCall
5893698748400478880d2a146ef9eaa111cd0e60522John McCall  F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
590110e8e56af30363072c140285961592b0107f789John McCall
591110e8e56af30363072c140285961592b0107f789John McCall  // Apply -fvisibility if desired.
5923698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
593af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
5943cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
5953cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5961fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
5973cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
5983cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
599f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallstatic void clearLinkageForClass(const CXXRecordDecl *record) {
600f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  for (CXXRecordDecl::decl_iterator
601f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall         i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
602f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    Decl *child = *i;
603f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    if (isa<NamedDecl>(child))
604f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall      cast<NamedDecl>(child)->ClearLinkageCache();
605f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
606f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
607f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
608f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallvoid NamedDecl::ClearLinkageCache() {
609f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // Note that we can't skip clearing the linkage of children just
610f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // because the parent doesn't have cached linkage:  we don't cache
611f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // when computing linkage for parent contexts.
612f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
613f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  HasCachedLinkage = 0;
614f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
615f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // If we're changing the linkage of a class, we need to reset the
616f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // linkage of child declarations, too.
617f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
618f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
619f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
62015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (ClassTemplateDecl *temp =
62115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
622f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    // Clear linkage for the template pattern.
623f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    CXXRecordDecl *record = temp->getTemplatedDecl();
624f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    record->HasCachedLinkage = 0;
625f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
626f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
62715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // We need to clear linkage for specializations, too.
62815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (ClassTemplateDecl::spec_iterator
62915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
63015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
631f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
63215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
63315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Clear cached linkage for function template decls, too.
63415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (FunctionTemplateDecl *temp =
63515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this)))
63615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (FunctionTemplateDecl::spec_iterator
63715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
63815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
63915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
640f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
641f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
642381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorLinkage NamedDecl::getLinkage() const {
643381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (HasCachedLinkage) {
64456ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer    assert(Linkage(CachedLinkage) ==
64556ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer             getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
646381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return Linkage(CachedLinkage);
647381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
648381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
649381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = getLVForDecl(this,
650381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                               LVFlags::CreateOnlyDeclLinkage()).linkage();
651381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
652381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return Linkage(CachedLinkage);
653381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
654381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
655af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const {
656381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  LinkageInfo LI = getLVForDecl(this, LVFlags());
65756ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer  assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
658381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
659381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = LI.linkage();
660381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return LI;
6610df9587ab011c12968fcbe3518666b2117afe350John McCall}
662becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
6633698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
664becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
665becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
6660df9587ab011c12968fcbe3518666b2117afe350John McCall  switch (D->getKind()) {
667becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
668becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
6691fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
6701fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
671becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
672becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
673becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
674becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
675becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
676becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
677becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
678becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
679becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
680becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
681af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::external();
682becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
683becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
684d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
6850df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->getRedeclContext()->isFileContext())
6863698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForNamespaceScopeDecl(D, Flags);
687d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
688d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
689d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
690d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
691d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
692d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
693d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
694d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
6950df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->isRecord())
6963698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForClassMember(D, Flags);
697d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
698d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
699d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
700d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
701d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
702d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
703d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
704d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
705d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
706d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
707d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
708d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
7090df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
7100df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
71110aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth      if (Function->isInAnonymousNamespace() && !Function->isExternC())
712af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
7131fb0caaa7bef765b85972274e3b434af2572c141John McCall
714af14603ca61757cf4361b583b45639a04c57e651John McCall      LinkageInfo LV;
715381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      if (Flags.ConsiderVisibilityAttributes) {
716381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
717381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LV.setVisibility(GetVisibilityFromAttr(VA));
718381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      }
719381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7201fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
721381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
722af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
723af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
7241fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
7250b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
7261fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
727d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
728d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
7290df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const VarDecl *Var = dyn_cast<VarDecl>(D))
730d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
731d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
73210aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth        if (Var->isInAnonymousNamespace() && !Var->isExternC())
733af14603ca61757cf4361b583b45639a04c57e651John McCall          return LinkageInfo::uniqueExternal();
7341fb0caaa7bef765b85972274e3b434af2572c141John McCall
735af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo LV;
7361fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
737af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(HiddenVisibility);
738381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        else if (Flags.ConsiderVisibilityAttributes) {
739381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
740381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor            LV.setVisibility(GetVisibilityFromAttr(VA));
741381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        }
742381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7431fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
744381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
745af14603ca61757cf4361b583b45639a04c57e651John McCall          if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
746af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.mergeVisibility(PrevLV);
7471fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
7481fb0caaa7bef765b85972274e3b434af2572c141John McCall
7491fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
750d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
751d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
752d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
753d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
754d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
755af14603ca61757cf4361b583b45639a04c57e651John McCall  return LinkageInfo::none();
7561fb0caaa7bef765b85972274e3b434af2572c141John McCall}
757d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
75847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
7593a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
7603a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
7613a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
7623a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
76347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
76447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
76547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
76647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
76747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
76868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
76968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
77068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
77168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
77268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
77368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
77468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
77568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
77668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
77768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
77868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
77968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
78068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
78168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
7821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
78368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
784f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
785f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
786f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
787910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.data(),
788910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.size(),
7893a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
79068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
79168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
7926be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
79368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
7946be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
79568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
79668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
79768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
79868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
79968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
80068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
80168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
8023521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
8033521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
8043521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
8053521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
80668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
8073521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
8083521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
8093521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
8103521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
81168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
8123521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
8133521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
81468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
8153521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8163521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
8173521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
8183521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
81968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
82068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
8213521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8223521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
82368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
82468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
82568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
82668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
82768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
82847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
82947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8308472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
83168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
8328472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
83368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
83447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
83568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
83647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
83747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8384afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
8396ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
8406ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
8412a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
8422a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
8432a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
844db9924191092b4d426cc066637d81698211846aaDouglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
845db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace() ==
846db9924191092b4d426cc066637d81698211846aaDouglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
847db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace();
8482a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8506ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
8516ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
8526ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
8536ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
854e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
855e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
856e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
857e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
858e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
859e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
860e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
8611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8620de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
8630de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
8640de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
8651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
866f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
867f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
868f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
8699488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
8709488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
8719488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
8729488ea120e093068021f944176c3d610dd540914John McCall
873dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
874dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    ASTContext &Context = getASTContext();
875dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return Context.getCanonicalNestedNameSpecifier(
876dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                     cast<UsingDecl>(this)->getQualifier()) ==
877dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor           Context.getCanonicalNestedNameSpecifier(
878dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                        cast<UsingDecl>(OldD)->getQualifier());
879dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  }
880c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis
8816ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
8826ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
8836ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
8846ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
8856ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
8866ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
887d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
888d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
889d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
8904afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
891e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
892e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
893e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
8949488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
895e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
896e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
897e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
898e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
899e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
900e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
901e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
902e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
903e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
904161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
905161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
906161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
907161755a09898c95d21bfff33707da9ca41cd53c5John McCall
908161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
909161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
910161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
911161755a09898c95d21bfff33707da9ca41cd53c5John McCall
91287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
913161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
914161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
915161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
916161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
917161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
918161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
919161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
920161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
921161755a09898c95d21bfff33707da9ca41cd53c5John McCall
9225239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
923a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
924a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
925a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
9261693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
9271693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
9281693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
9291693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
9301693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
9311693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
9321693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9331693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
934a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
9354e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
9364e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
937a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
938a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
939a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
940c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
941c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
942b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
943b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
944b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
945b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
946b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
947b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
948b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
949b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
950b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
951b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
952c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
953b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
954b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
955b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
956b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
957b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
958b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
959b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
960b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
961b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
962b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
963b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
964b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
965b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
966b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
9671693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
9681693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
9691693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9701693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
9719b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
972c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
973c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
9749b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
9759b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
9769b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
977c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  assert((NumTPLists == 0 || QualifierLoc) &&
9789b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Nonempty array of template parameters with no qualifier!");
9799b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
9809b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
9819b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
982c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
9839b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
9849b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
9859b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
9869b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
9879b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
988c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
9899b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
9909b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
9919b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
9929b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
9939b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
9949b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
995a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
99699f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
99799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
99899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
9997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
10007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
1001d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_None:          break;
1002d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Auto:          return "auto"; break;
1003d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Extern:        return "extern"; break;
1004d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_PrivateExtern: return "__private_extern__"; break;
1005d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Register:      return "register"; break;
1006d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Static:        return "static"; break;
10077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
10087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
10107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
10117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10134afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1014a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
101516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
101616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
101799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
101899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
1019381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid VarDecl::setStorageClass(StorageClass SC) {
1020381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForVariable(SC));
1021381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1022381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1023381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1024381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1025381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1026381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1027da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas GregorSourceLocation VarDecl::getInnerLocStart() const {
1028da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
1029da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Start.isInvalid())
1030da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    Start = getLocation();
1031da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  return Start;
1032da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
1033da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
10341693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
103555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
10361693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
10371693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), getLocation());
103855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
103955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
10407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
10417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
10427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
10437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
1044d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
10457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
10467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
104710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
104810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isFunctionOrMethod())
104910aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
105010aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
105110aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
10527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
10537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1054d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
10557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
10577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
10587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
10607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
10627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
10657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
10667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1068e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1069e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
1070e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
1071e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
1072e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
1073e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
1074e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
1075e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
1076e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
1077e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
1078e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
1079e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1080e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
1081e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
1082e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
1083e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1084e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
1085e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
1086e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
1087e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
1088e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
1089e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
1090e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
1091e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
1092e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
1093e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
1094e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
1095e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
10962bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
1097d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
1098d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
10992bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
11002bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
11012bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
11022bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
11032bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
11042bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
1105e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
1106e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
1107e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
1108e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
1109e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
1110e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1111e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
1112e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1113e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
1114e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
1115e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
1116e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1117e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1118e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
1119e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1120e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1121e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
1122e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1123f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
1124e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
1125e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1126e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
1127e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
1128e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
1129e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
1130e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
1131e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
1132e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1133e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
1134e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1135e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1136e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
1137e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1138e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1139e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
1140e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1141e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1142e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
1143e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
1144e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
114531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
114631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
114731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
114831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
1149e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
1150e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1151e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
115231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
115331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
115431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
115531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
1156e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1157e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1158110e8e56af30363072c140285961592b0107f789John McCallVarDecl::DefinitionKind VarDecl::hasDefinition() const {
1159110e8e56af30363072c140285961592b0107f789John McCall  DefinitionKind Kind = DeclarationOnly;
1160110e8e56af30363072c140285961592b0107f789John McCall
1161110e8e56af30363072c140285961592b0107f789John McCall  const VarDecl *First = getFirstDeclaration();
1162110e8e56af30363072c140285961592b0107f789John McCall  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1163110e8e56af30363072c140285961592b0107f789John McCall       I != E; ++I)
1164110e8e56af30363072c140285961592b0107f789John McCall    Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1165110e8e56af30363072c140285961592b0107f789John McCall
1166110e8e56af30363072c140285961592b0107f789John McCall  return Kind;
1167110e8e56af30363072c140285961592b0107f789John McCall}
1168110e8e56af30363072c140285961592b0107f789John McCall
116931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
11707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
11717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
11727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
11737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
117531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
11767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
11777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
11797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11811028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
1182da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
11831028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
11848761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
11858761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
11868761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
11878761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
11881028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
11891028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
11901028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
11911028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
11921028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
11931028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
11941028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
11951028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
11961028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
11970d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
11980d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
11990d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
12000d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
12010d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
12020d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
12030d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
12040d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
12050d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
12060d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
12070d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
12080d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
12090d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
1210838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
12117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
12127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
1213838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
12147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
12157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
12177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12191028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1220b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1221251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1222251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1223251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1224251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1225251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1226663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1227e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1228251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1229251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1230251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1231251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1232251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
12331028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1234b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1235b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1236b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
12370a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
12380a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1239b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1240251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1241251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
12420a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
12430a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
12440a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
12450a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
12467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
12477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
12487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
12497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
12507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1251275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
12527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
12537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
12547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
125516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
125616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
125716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
125816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1259275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1260275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
12617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
12627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
12637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
12647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
12657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
12674765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
12687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
12697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
12717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
12744765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
12757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
1276275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
1277c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
1278275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1279275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
12807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
12817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
12827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
12837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12844765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
12857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
12867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
12897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
12907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
12917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
12937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
12947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1296fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1297fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
12981fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool ParmVarDecl::isParameterPack() const {
12991fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isa<PackExpansionType>(getType());
13001fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
13011fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
130299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
13038a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
13048a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
13058a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1306da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1307da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        const PrintingPolicy &Policy,
1308da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        bool Qualified) const {
1309da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1310da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1311da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (TemplateArgs)
1312da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    S += TemplateSpecializationType::PrintTemplateArgumentList(
1313da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->data(),
1314da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->size(),
1315da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                               Policy);
1316da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
1317da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
1318da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
13199498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
13209498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
13219498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
13229498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
13239498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
13249498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
132506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
132606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
132706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (I->Body) {
132806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
132906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
133006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
133106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
133206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
133306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
133406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
133506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
13366fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1337c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1338c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1339c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1340c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
1341f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1342f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1343f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1344f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
134755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
134855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
1349b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  if (B)
135055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
135155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
135255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
13532138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
13542138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
13552138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
13562138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
13572138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
13582138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
13592138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
136048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
136148a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
136207a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
13637a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    getDeclContext()->getRedeclContext()->isTranslationUnit() &&
136404495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
136504495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
136604495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
136748a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
136848a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
13696393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
13706393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
13716393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1372d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
13736393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
137410aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
137510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isRecord())
137610aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
137710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
137810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
13796393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
13806393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1381d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
138240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
13836393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13846393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
13856393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
13866393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
13876393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13880bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
13896393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
13906393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13918499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
13928499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
13938499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
13948499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1395d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
13968499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
13978499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
13981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
13998499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
14008499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
14018499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
14028499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
14038499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
14048499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
14058499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
14068499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
14078499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
14088499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
14098499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
14108499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
14117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
14127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
14137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
14147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
14167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
14177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
14187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
14197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
14207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
14218f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
14228f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (PrevDecl->IsInline)
14238f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    IsInline = true;
14247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
14277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
14287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
14317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
14327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1434381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid FunctionDecl::setStorageClass(StorageClass SC) {
1435381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForFunction(SC));
1436381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1437381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1438381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1439381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1440381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1441381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
14423e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
14433e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
14443e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
14453e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
14463e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
14473e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
14483e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
14503e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
14517814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
14527814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
14533c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
14543c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
14553c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14563c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
14573c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
14583c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
14593c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14603c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
14613c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
14623c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
14633c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14649add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1465d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
14669add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
14679add31798f621f843233dbff8bba103fca64447bDouglas Gregor
14683c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
14693c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
14703c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
14713c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
14723c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
14733c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14743c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
14753c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
14763c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
14771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
14783c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
147940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
14803c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
14813c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14823c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
14833e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
14843e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
14853e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
14863e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
14871ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
14888dbfbf4c95251c69a455d4d016d6c7890c932007Bob Wilson/// based on its FunctionType.  This is the length of the ParamInfo array
14891ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
14901ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1491183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
149272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1493d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
149472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
14951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14986b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
14996b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                             ParmVarDecl **NewParamInfo, unsigned NumParams) {
15005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
15012dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
15021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
15045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
15056b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1506fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
150855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
150996888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
151096888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1511cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
151255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15168123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
15178123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
15188123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
1519f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// arguments (in C++) or the last parameter is a parameter pack.
15208123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
15217d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  if (!getASTContext().getLangOptions().CPlusPlus)
15227d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    return getNumParams();
15237d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
1524f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  unsigned NumRequiredArgs = getNumParams();
1525f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1526f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If the last parameter is a parameter pack, we don't need an argument for
1527f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1528f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (NumRequiredArgs > 0 &&
1529f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1530f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    --NumRequiredArgs;
1531f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1532f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If this parameter has a default argument, we don't need an argument for
1533f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1534f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  while (NumRequiredArgs > 0 &&
1535f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor         getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
15368123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
15378123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
15387d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // We might have parameter packs before the end. These can't be deduced,
15397d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // but they can still handle multiple arguments.
15407d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  unsigned ArgIdx = NumRequiredArgs;
15417d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  while (ArgIdx > 0) {
15427d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (getParamDecl(ArgIdx - 1)->isParameterPack())
15437d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      NumRequiredArgs = ArgIdx;
15447d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
15457d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    --ArgIdx;
15467d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  }
15477d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
15488123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
15498123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
15508123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
15517ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
15528f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (IsInline)
15537d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
155448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
155548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
155648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
155748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
155848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
15597d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
15607d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
15617d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
15627d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
15637d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
15647d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
15657d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
15667d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
15677d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
15687d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
15697d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
15707d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
15717d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
15727d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
157306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
15747d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
157506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
15767d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
157706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
15787d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
15797d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
15807d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
15817ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
15827ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
15837d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
15841fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
15851fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
15861fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
15871fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
15881fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
15891fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
15901fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
15911fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
15921e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
15931fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
15941fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
15951fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
15961fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
15971fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
15981fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
15991fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
16001fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
16011fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
16027ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
16037d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
16041fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16057d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
16068f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If it's not the case that both 'inline' and 'extern' are
16078f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // specified on the definition, then this inline definition is
16088f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // externally visible.
16098f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
16108f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      return true;
16118f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
16128f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If any declaration is 'inline' but not 'extern', then this definition
16138f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // is externally visible.
16141fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
16151fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
16161fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
16178f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      if (Redecl->isInlineSpecified() &&
16188f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor          Redecl->getStorageClassAsWritten() != SC_Extern)
16191fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
16208f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    }
16211fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16229f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
16231fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
16241fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16251fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
16261fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
16271fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
16281fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
16291fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
16301fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
16311fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
16321fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
16331fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
16341fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
16351fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1636d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
16371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
16381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
16391fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16401fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
16411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
16421fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
16431fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
16449f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
16459f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
16469f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
16471cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
16481cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
16491cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1650e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1651e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
16521cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
16531cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
16541cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
16551cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1656a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1657a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1658a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1659a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1660a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1661a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1662a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1663a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1664a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1665d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1666d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1667d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1668d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1669d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1670d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1671d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1672d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1673d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1674d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1675d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1676d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1677d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1678d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1679d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1680d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1681d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
16822db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1683b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
16842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
16852db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
16862db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
16872db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
16882db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1689b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1690b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1691b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1692b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
16932db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
16946b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
16956b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
16962db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
16972db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
16982db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
16992db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
17006b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
17012db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
17022db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
17032db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
17043b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
17056cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
17066cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
17073b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
17083b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17093b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
17103b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
17113b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
17123b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
17133b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
17143b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17153b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
17163b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
17173b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17183b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
17193b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
17203b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
17213b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
17223b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17233b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
17243b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
172506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
17263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
172706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
17283b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17293b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
17303b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
17313b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
17323b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
173306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
17343b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
17353b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17367ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
17373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
17383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
17403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
17413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
17423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
17433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
17443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
17453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
17463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17473b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
17483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
17493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
17513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
17523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
17543b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
17553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
175616e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
17571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
175816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
175916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
17601fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
176116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
176216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
176316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
176416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
176516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
176616e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
17671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1768fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1769fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
177016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
177116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
177216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
177316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
177416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1775e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1776e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1777e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1778e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1779e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1780e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1781e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1782e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1783e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1784e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
17851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
17866b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
17876b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
1788127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1789b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1790e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
17917b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
17927b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1793b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1794b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
17951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
179616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
17971637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1798a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1799a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
1800a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
1801a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
18021637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
18031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1804127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1805b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1806b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1807b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1808b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
18092c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
18102c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
18112c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
18122c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
1813b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1814b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
18152c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
18162c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
18172c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
1818b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
18191637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
18201637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1821af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1822af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1823af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1824af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1825af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1826af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1827af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
182821c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1829af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1830af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1831af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1832af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1833af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1834af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1835af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1836af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1837af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1838af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1839af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1840af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1841af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1842af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1843af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1844af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1845af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1846af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1847af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1848af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1849af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1850af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1851af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1852af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1853af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1854af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1855d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
18561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1857d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
18582db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
18591fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
18602db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
18612db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1862d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
18632db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
18642db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
18652db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
18662db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
18672db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
18682db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
18691fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
18701fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
18711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
18720a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
18730a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
18742db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
18752db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
18760a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
18772db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
18780a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
18790a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
18800a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
18810a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
18820a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
18830a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
18842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
18850a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
18860a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
18870a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
18880a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
18890a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
18902db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
18911fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
18921fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
18930a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
18940a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
18950a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
18960a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
18970a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
18980a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
18990a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
19000a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
19010a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
19020a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
19030a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
19040a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
19059f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
1906da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
19079f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
19089f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19099f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
19109f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
19119f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
19129f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
191306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
19149f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
19159f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
19169f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19179f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
19189f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
19199f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
19209f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
192106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
19229f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
19239f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
19249f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19259f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
19269f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
19279f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19288a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
19297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
19307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19324ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadFieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
19334ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                             SourceLocation L, IdentifierInfo *Id, QualType T,
19347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
19357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
19367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
19397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
19407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
19417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
19437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
19447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
19467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1948ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCallunsigned FieldDecl::getFieldIndex() const {
1949ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  if (CachedFieldIndex) return CachedFieldIndex - 1;
1950ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1951ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  unsigned index = 0;
1952ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  RecordDecl::field_iterator
1953ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    i = getParent()->field_begin(), e = getParent()->field_end();
1954ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  while (true) {
1955ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    assert(i != e && "failed to find field in parent!");
1956ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (*i == this)
1957ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      break;
1958ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1959ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    ++i;
1960ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    ++index;
1961ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  }
1962ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1963ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  CachedFieldIndex = index + 1;
1964ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  return index;
1965ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall}
1966ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
19677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1968bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
19694b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
19704b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
19711693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
19721693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
19731693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
19741693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1975f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1976f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
19771693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
1978f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1979f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1980b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
19818e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1982b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1983b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
198460e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
198560e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
198660e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
1987f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
1988381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  ClearLinkageCache();
198960e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
199060e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
19910b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
1992ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
199386ff308724171494395a840fd2efbe25e62f352eJohn McCall
199486ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
199586ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
199686ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
199786ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
19982243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
19992243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
200086ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
20010b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
20020b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
20030b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
20045cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
20055cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
20065cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
20075cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
20080b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
2009ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
2010565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
2011565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
2012565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
20130b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
20140b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
2015952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
20168e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
20178e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
2018220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2019220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
20201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
20228e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
20238e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
20248e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
20251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20268e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
20274b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
20284b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
2029c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2030c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
2031b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
2032b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
2033b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2034b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
2035c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
2036b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2037b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
2038b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2039b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
2040b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
2041b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
2042b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
2043b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2044b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
2045b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
20464b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
20477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
20487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
20497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
20517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
2052a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           EnumDecl *PrevDecl, bool IsScoped,
2053a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           bool IsScopedUsingClassTag, bool IsFixed) {
20541274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
2055a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    IsScoped, IsScopedUsingClassTag, IsFixed);
20567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
20577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
20587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2060b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
20611274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
2062a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                          false, false, false);
2063b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2064b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2065838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
20661b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
20671b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
20681b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
20697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
20701274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
20711274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
20727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
20731b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
20741b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
20757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
20767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
20798a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
20808a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
20815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
208235bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
20838e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
20848e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
20858e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
20866359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
2087bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
2088082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
2089eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
20906359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
20916359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
20926359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
20934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
20944b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
2095741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
20961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20978e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
20984b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
20994b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
21006359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
21016359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
21024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
2103b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2104b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis                            SourceLocation());
2105b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2106b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2107c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
21081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
2109c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2110c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
2111c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
2112eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
2113eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2114eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
2115eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2116eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
2117eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2118eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2119da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// completeDefinition - Notes that the definition of this type is now
2120da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// complete.
2121da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid RecordDecl::completeDefinition() {
2122da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  assert(!isDefinition() && "Cannot redefine record!");
2123da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  TagDecl::completeDefinition();
2124da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
2125da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
2126eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
2127eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
2128eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
2129eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2130eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
2131eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
2132eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2133eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<Decl*, 64> Decls;
2134eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2135eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2136eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2137eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
2138eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
2139eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
2140eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
2141eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
2142eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2143eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = true;
2144eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2145eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
2146eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2147eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2148eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2149eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2150eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
215156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
215256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
215356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
215456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
2155838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
2156e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
2157e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
21581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2159e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
2160e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
2161e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
2162838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
2163e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2164e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2165e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
2166e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
2167e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
21686b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallvoid BlockDecl::setCaptures(ASTContext &Context,
21696b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *begin,
21706b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *end,
21716b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            bool capturesCXXThis) {
2172469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  CapturesCXXThis = capturesCXXThis;
2173469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
2174469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  if (begin == end) {
21756b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    NumCaptures = 0;
21766b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    Captures = 0;
2177469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    return;
2178469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  }
2179469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
21806b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  NumCaptures = end - begin;
21816b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
21826b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // Avoid new Capture[] because we don't want to provide a default
21836b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // constructor.
21846b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  size_t allocationSize = NumCaptures * sizeof(Capture);
21856b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
21866b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  memcpy(buffer, begin, allocationSize);
21876b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  Captures = static_cast<Capture*>(buffer);
2188e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
21897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21902fcbceff97e065cff499e6cc563ca25c762bf547Douglas GregorSourceRange BlockDecl::getSourceRange() const {
21912fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor  return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
21922fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor}
21937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
21957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
21967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
21977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
21997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
22007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2202ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2203ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner                             SourceLocation L, IdentifierInfo *II) {
2204ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  return new (C) LabelDecl(DC, L, II, 0);
2205ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner}
2206ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
2207ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
22087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
22097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
22107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
22117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
221306c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
221406c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
221506c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
221606c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
221706c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
22187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2219201e519ad9cc2863bc94cf799e407a81ed29181fJohn McCall                                             SourceLocation loc,
2220201e519ad9cc2863bc94cf799e407a81ed29181fJohn McCall                                             IdentifierInfo *name,
2221201e519ad9cc2863bc94cf799e407a81ed29181fJohn McCall                                             QualType type) {
2222201e519ad9cc2863bc94cf799e407a81ed29181fJohn McCall  return new (C) ImplicitParamDecl(DC, loc, name, type);
22237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
22262577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
22272577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
222816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   StorageClass S, StorageClass SCAsWritten,
22298f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool isInlineSpecified,
22308f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool hasWrittenPrototype) {
22312577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
22328f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                           S, SCAsWritten, isInlineSpecified);
22337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
22347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
22357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
22387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
22397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
22427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
22437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
22447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
22457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
22467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2248d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl *
2249d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2250d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          IdentifierInfo *Id, QualType T, NamedDecl **CH,
2251d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          unsigned CHS) {
225287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
225387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
225487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
22558e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
22568e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
22578e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
22588e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
22598e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
22608e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
22618e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
22627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
22637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
22647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
22657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
22667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
22697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
22707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
22717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
22727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
2273